1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "libbb.h"
32#include "e2fs_lib.h"
33
34enum {
35 OPT_RECUR = 0x1,
36 OPT_ALL = 0x2,
37 OPT_DIRS_OPT = 0x4,
38 OPT_PF_LONG = 0x8,
39 OPT_GENERATION = 0x10,
40};
41
42static void list_attributes(const char *name)
43{
44 unsigned long fsflags;
45 unsigned long generation;
46
47 if (fgetflags(name, &fsflags) != 0)
48 goto read_err;
49
50 if (option_mask32 & OPT_GENERATION) {
51 if (fgetversion(name, &generation) != 0)
52 goto read_err;
53 printf("%5lu ", generation);
54 }
55
56 if (option_mask32 & OPT_PF_LONG) {
57 printf("%-28s ", name);
58 print_e2flags(stdout, fsflags, PFOPT_LONG);
59 bb_putchar('\n');
60 } else {
61 print_e2flags(stdout, fsflags, 0);
62 printf(" %s\n", name);
63 }
64
65 return;
66 read_err:
67 bb_perror_msg("reading %s", name);
68}
69
70static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
71 struct dirent *de,
72 void *private UNUSED_PARAM)
73{
74 struct stat st;
75 char *path;
76
77 path = concat_path_file(dir_name, de->d_name);
78
79 if (lstat(path, &st) != 0)
80 bb_perror_msg("stat %s", path);
81 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
82 list_attributes(path);
83 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
84 && !DOT_OR_DOTDOT(de->d_name)
85 ) {
86 printf("\n%s:\n", path);
87 iterate_on_dir(path, lsattr_dir_proc, NULL);
88 bb_putchar('\n');
89 }
90 }
91
92 free(path);
93 return 0;
94}
95
96static void lsattr_args(const char *name)
97{
98 struct stat st;
99
100 if (lstat(name, &st) == -1) {
101 bb_perror_msg("stat %s", name);
102 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
103 iterate_on_dir(name, lsattr_dir_proc, NULL);
104 } else {
105 list_attributes(name);
106 }
107}
108
109int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110int lsattr_main(int argc UNUSED_PARAM, char **argv)
111{
112 getopt32(argv, "Radlv");
113 argv += optind;
114
115 if (!*argv)
116 *--argv = (char*)".";
117 do lsattr_args(*argv++); while (*argv);
118
119 return EXIT_SUCCESS;
120}
121