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