blob: 13d5acf01f06aeb54912386da82fd3218e051641 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
3 * lsattr.c - List file attributes on an ext2 file system
4 *
5 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * This file can be redistributed under the terms of the GNU General
10 * Public License
11 */
12
13/*
14 * History:
15 * 93/10/30 - Creation
16 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
17 * 94/02/27 - Integrated in Ted's distribution
18 * 98/12/29 - Display version info only when -V specified (G M Sipe)
19 */
20
21#include <sys/types.h>
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <getopt.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/param.h>
31#include <sys/stat.h>
32
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000033#include "ext2fs/ext2_fs.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +000034#include "e2fsbb.h"
35#include "e2p/e2p.h"
36
Mike Frysingerd2a64d22005-04-25 04:10:35 +000037#define OPT_RECUR 1
38#define OPT_ALL 2
39#define OPT_DIRS_OPT 4
40#define OPT_PF_LONG 8
41#define OPT_GENERATION 16
42static int flags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000043
Mike Frysingerd2a64d22005-04-25 04:10:35 +000044static void list_attributes(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000045{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000046 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000047 unsigned long generation;
48
Mike Frysingerea338ff2005-05-07 07:17:43 +000049 if (fgetflags(name, &fsflags) == -1)
50 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000051 if (flags & OPT_GENERATION) {
Mike Frysingerea338ff2005-05-07 07:17:43 +000052 if (fgetversion(name, &generation) == -1)
53 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000054 printf("%5lu ", generation);
Mike Frysingerd89e6292005-04-24 05:07:59 +000055 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000056
Mike Frysingerd2a64d22005-04-25 04:10:35 +000057 if (flags & OPT_PF_LONG) {
Mike Frysingerd89e6292005-04-24 05:07:59 +000058 printf("%-28s ", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +000059 print_flags(stdout, fsflags, PFOPT_LONG);
Mike Frysingerea338ff2005-05-07 07:17:43 +000060 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +000061 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000062 print_flags(stdout, fsflags, 0);
Mike Frysingerd89e6292005-04-24 05:07:59 +000063 printf(" %s\n", name);
64 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000065
66 return;
67read_err:
68 bb_perror_msg("reading %s", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000069}
70
Mike Frysingerd2a64d22005-04-25 04:10:35 +000071static int lsattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +000072
Mike Frysingerd2a64d22005-04-25 04:10:35 +000073static void lsattr_args(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000074{
Denis Vlasenko7039a662006-10-08 17:54:47 +000075 struct stat st;
Mike Frysingerd89e6292005-04-24 05:07:59 +000076
Denis Vlasenko7039a662006-10-08 17:54:47 +000077 if (lstat(name, &st) == -1) {
Mike Frysingerea338ff2005-05-07 07:17:43 +000078 bb_perror_msg("stating %s", name);
79 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000080 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
81 iterate_on_dir(name, lsattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +000082 else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000083 list_attributes(name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000084 }
85}
86
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
Rob Landley7c94bed2006-05-03 21:58:45 +000088 void *private)
Mike Frysingerd89e6292005-04-24 05:07:59 +000089{
Denis Vlasenko7039a662006-10-08 17:54:47 +000090 struct stat st;
Mike Frysingerd89e6292005-04-24 05:07:59 +000091 char *path;
Mike Frysingerd89e6292005-04-24 05:07:59 +000092
Mike Frysingerea338ff2005-05-07 07:17:43 +000093 path = concat_path_file(dir_name, de->d_name);
Mike Frysingerc238a972005-04-25 05:24:35 +000094
Denis Vlasenko7039a662006-10-08 17:54:47 +000095 if (lstat(path, &st) == -1)
Mike Frysingerd2a64d22005-04-25 04:10:35 +000096 bb_perror_msg(path);
Mike Frysingerd89e6292005-04-24 05:07:59 +000097 else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000098 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
99 list_attributes(path);
100 if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
Mike Frysingerea338ff2005-05-07 07:17:43 +0000101 (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
102 (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000103 printf("\n%s:\n", path);
104 iterate_on_dir(path, lsattr_dir_proc, NULL);
105 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000106 }
107 }
108 }
Mike Frysingerc238a972005-04-25 05:24:35 +0000109
Mike Frysingerd89e6292005-04-24 05:07:59 +0000110 free(path);
Mike Frysingerc238a972005-04-25 05:24:35 +0000111
Mike Frysingerd89e6292005-04-24 05:07:59 +0000112 return 0;
113}
114
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000115int lsattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000116{
Mike Frysingerd89e6292005-04-24 05:07:59 +0000117 int i;
118
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000119 flags = getopt32(argc, argv, "Radlv");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000120
Mike Frysingerd89e6292005-04-24 05:07:59 +0000121 if (optind > argc - 1)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000122 lsattr_args(".");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000123 else
124 for (i = optind; i < argc; i++)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000125 lsattr_args(argv[i]);
126
127 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000128}