diff options
Diffstat (limited to 'util-linux/lsattr.c')
-rw-r--r-- | util-linux/lsattr.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/util-linux/lsattr.c b/util-linux/lsattr.c new file mode 100644 index 000000000..ca523c821 --- /dev/null +++ b/util-linux/lsattr.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | * lsattr.c - List file attributes on an ext2 file system | ||
3 | * | ||
4 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> | ||
5 | * Laboratoire MASI, Institut Blaise Pascal | ||
6 | * Universite Pierre et Marie Curie (Paris VI) | ||
7 | * | ||
8 | * This file can be redistributed under the terms of the GNU General | ||
9 | * Public License | ||
10 | */ | ||
11 | |||
12 | /* | ||
13 | * History: | ||
14 | * 93/10/30 - Creation | ||
15 | * 93/11/13 - Replace stat() calls by lstat() to avoid loops | ||
16 | * 94/02/27 - Integrated in Ted's distribution | ||
17 | * 98/12/29 - Display version info only when -V specified (G M Sipe) | ||
18 | */ | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | #include <dirent.h> | ||
22 | #include <errno.h> | ||
23 | #include <fcntl.h> | ||
24 | #include <getopt.h> | ||
25 | #include <stdio.h> | ||
26 | #include <unistd.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <string.h> | ||
29 | #include <sys/param.h> | ||
30 | #include <sys/stat.h> | ||
31 | |||
32 | #include "ext2_fs.h" | ||
33 | #include "e2fsbb.h" | ||
34 | #include "e2p/e2p.h" | ||
35 | |||
36 | #define main lsattr_main | ||
37 | |||
38 | #ifdef __GNUC__ | ||
39 | #define EXT2FS_ATTR(x) __attribute__(x) | ||
40 | #else | ||
41 | #define EXT2FS_ATTR(x) | ||
42 | #endif | ||
43 | |||
44 | static int all; | ||
45 | static int dirs_opt; | ||
46 | static unsigned pf_options; | ||
47 | static int recursive; | ||
48 | static int verbose; | ||
49 | static int generation_opt; | ||
50 | |||
51 | #ifdef _LFS64_LARGEFILE | ||
52 | #define LSTAT lstat64 | ||
53 | #define STRUCT_STAT struct stat64 | ||
54 | #else | ||
55 | #define LSTAT lstat | ||
56 | #define STRUCT_STAT struct stat | ||
57 | #endif | ||
58 | |||
59 | #if 0 | ||
60 | static void usage(void) | ||
61 | { | ||
62 | fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); | ||
63 | exit(1); | ||
64 | } | ||
65 | #endif | ||
66 | |||
67 | static void list_attributes (const char * name) | ||
68 | { | ||
69 | unsigned long flags; | ||
70 | unsigned long generation; | ||
71 | |||
72 | if (fgetflags (name, &flags) == -1) { | ||
73 | com_err (program_name, errno, _("While reading flags on %s"), | ||
74 | name); | ||
75 | return; | ||
76 | } | ||
77 | if (generation_opt) { | ||
78 | if (fgetversion (name, &generation) == -1) { | ||
79 | com_err (program_name, errno, | ||
80 | _("While reading version on %s"), | ||
81 | name); | ||
82 | return; | ||
83 | } | ||
84 | printf ("%5lu ", generation); | ||
85 | } | ||
86 | if (pf_options & PFOPT_LONG) { | ||
87 | printf("%-28s ", name); | ||
88 | print_flags(stdout, flags, pf_options); | ||
89 | fputc('\n', stdout); | ||
90 | } else { | ||
91 | print_flags(stdout, flags, pf_options); | ||
92 | printf(" %s\n", name); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | static int lsattr_dir_proc (const char *, struct dirent *, void *); | ||
97 | |||
98 | static void lsattr_args (const char * name) | ||
99 | { | ||
100 | STRUCT_STAT st; | ||
101 | |||
102 | if (LSTAT (name, &st) == -1) | ||
103 | com_err (program_name, errno, _("while trying to stat %s"), | ||
104 | name); | ||
105 | else { | ||
106 | if (S_ISDIR(st.st_mode) && !dirs_opt) | ||
107 | iterate_on_dir (name, lsattr_dir_proc, NULL); | ||
108 | else | ||
109 | list_attributes (name); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | static int lsattr_dir_proc (const char * dir_name, struct dirent * de, | ||
114 | void * private EXT2FS_ATTR((unused))) | ||
115 | { | ||
116 | STRUCT_STAT st; | ||
117 | char *path; | ||
118 | int dir_len = strlen(dir_name); | ||
119 | |||
120 | path = malloc(dir_len + strlen (de->d_name) + 2); | ||
121 | |||
122 | if (dir_len && dir_name[dir_len-1] == '/') | ||
123 | sprintf (path, "%s%s", dir_name, de->d_name); | ||
124 | else | ||
125 | sprintf (path, "%s/%s", dir_name, de->d_name); | ||
126 | if (LSTAT (path, &st) == -1) | ||
127 | perror (path); | ||
128 | else { | ||
129 | if (de->d_name[0] != '.' || all) { | ||
130 | list_attributes (path); | ||
131 | if (S_ISDIR(st.st_mode) && recursive && | ||
132 | strcmp(de->d_name, ".") && | ||
133 | strcmp(de->d_name, "..")) { | ||
134 | printf ("\n%s:\n", path); | ||
135 | iterate_on_dir (path, lsattr_dir_proc, NULL); | ||
136 | printf ("\n"); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | free(path); | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | int main (int argc, char ** argv) | ||
145 | { | ||
146 | int c; | ||
147 | int i; | ||
148 | |||
149 | #ifdef ENABLE_NLS | ||
150 | setlocale(LC_MESSAGES, ""); | ||
151 | setlocale(LC_CTYPE, ""); | ||
152 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | ||
153 | textdomain(NLS_CAT_NAME); | ||
154 | #endif | ||
155 | #if 0 | ||
156 | if (argc && *argv) | ||
157 | program_name = *argv; | ||
158 | #endif | ||
159 | while ((c = getopt (argc, argv, "Radlv")) != EOF) | ||
160 | switch (c) | ||
161 | { | ||
162 | case 'R': | ||
163 | recursive = 1; | ||
164 | break; | ||
165 | case 'V': | ||
166 | verbose = 1; | ||
167 | break; | ||
168 | case 'a': | ||
169 | all = 1; | ||
170 | break; | ||
171 | case 'd': | ||
172 | dirs_opt = 1; | ||
173 | break; | ||
174 | case 'l': | ||
175 | pf_options = PFOPT_LONG; | ||
176 | break; | ||
177 | case 'v': | ||
178 | generation_opt = 1; | ||
179 | break; | ||
180 | default: | ||
181 | usage(); | ||
182 | } | ||
183 | |||
184 | #if 0 | ||
185 | if (verbose) | ||
186 | fprintf (stderr, "lsattr %s (%s)\n", | ||
187 | E2FSPROGS_VERSION, E2FSPROGS_DATE); | ||
188 | #endif | ||
189 | if (optind > argc - 1) | ||
190 | lsattr_args ("."); | ||
191 | else | ||
192 | for (i = optind; i < argc; i++) | ||
193 | lsattr_args (argv[i]); | ||
194 | exit(0); | ||
195 | } | ||