]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
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 | |
a88fa0c0 | 17 | * 98/12/29 - Display version info only when -V specified (G M Sipe) |
3839e657 TT |
18 | */ |
19 | ||
fff18b4e | 20 | #define _LARGEFILE64_SOURCE |
fff18b4e | 21 | |
d1154eb4 | 22 | #include "config.h" |
a418d3ad | 23 | #include <sys/types.h> |
3839e657 | 24 | #include <dirent.h> |
a418d3ad | 25 | #ifdef HAVE_ERRNO_H |
3839e657 | 26 | #include <errno.h> |
a418d3ad | 27 | #endif |
3839e657 | 28 | #include <fcntl.h> |
a418d3ad | 29 | #ifdef HAVE_GETOPT_H |
3839e657 | 30 | #include <getopt.h> |
a418d3ad TT |
31 | #else |
32 | extern int optind; | |
33 | extern char *optarg; | |
34 | #endif | |
3839e657 TT |
35 | #include <stdio.h> |
36 | #include <unistd.h> | |
a418d3ad | 37 | #include <stdlib.h> |
19c78dc0 | 38 | #include <string.h> |
3839e657 TT |
39 | #include <sys/param.h> |
40 | #include <sys/stat.h> | |
3839e657 | 41 | |
54c637d4 | 42 | #include "ext2fs/ext2_fs.h" |
3839e657 TT |
43 | #include "et/com_err.h" |
44 | #include "e2p/e2p.h" | |
45 | ||
46 | #include "../version.h" | |
d9c56d3c | 47 | #include "nls-enable.h" |
3839e657 | 48 | |
54434927 TT |
49 | #ifdef __GNUC__ |
50 | #define EXT2FS_ATTR(x) __attribute__(x) | |
51 | #else | |
52 | #define EXT2FS_ATTR(x) | |
53 | #endif | |
54 | ||
e1a0a3e3 | 55 | static const char * program_name = "lsattr"; |
3839e657 | 56 | |
f10748d8 TT |
57 | static int all; |
58 | static int dirs_opt; | |
59 | static unsigned pf_options; | |
60 | static int recursive; | |
61 | static int verbose; | |
62 | static int generation_opt; | |
3839e657 | 63 | |
b705640a TT |
64 | #ifdef _LFS64_LARGEFILE |
65 | #define LSTAT lstat64 | |
66 | #define STRUCT_STAT struct stat64 | |
67 | #else | |
68 | #define LSTAT lstat | |
69 | #define STRUCT_STAT struct stat | |
70 | #endif | |
71 | ||
818180cd | 72 | static void usage(void) |
3839e657 | 73 | { |
d9c56d3c | 74 | fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); |
818180cd | 75 | exit(1); |
3839e657 TT |
76 | } |
77 | ||
1b600bfa | 78 | static int list_attributes (const char * name) |
3839e657 TT |
79 | { |
80 | unsigned long flags; | |
e1a0a3e3 | 81 | unsigned long generation; |
3839e657 | 82 | |
f10748d8 | 83 | if (fgetflags (name, &flags) == -1) { |
d9c56d3c | 84 | com_err (program_name, errno, _("While reading flags on %s"), |
3839e657 | 85 | name); |
1b600bfa | 86 | return -1; |
f10748d8 TT |
87 | } |
88 | if (generation_opt) { | |
89 | if (fgetversion (name, &generation) == -1) { | |
90 | com_err (program_name, errno, | |
91 | _("While reading version on %s"), | |
92 | name); | |
1b600bfa | 93 | return -1; |
e1a0a3e3 | 94 | } |
f10748d8 TT |
95 | printf ("%5lu ", generation); |
96 | } | |
97 | if (pf_options & PFOPT_LONG) { | |
98 | printf("%-28s ", name); | |
99 | print_flags(stdout, flags, pf_options); | |
100 | fputc('\n', stdout); | |
101 | } else { | |
102 | print_flags(stdout, flags, pf_options); | |
103 | printf(" %s\n", name); | |
3839e657 | 104 | } |
1b600bfa | 105 | return 0; |
3839e657 TT |
106 | } |
107 | ||
108 | static int lsattr_dir_proc (const char *, struct dirent *, void *); | |
109 | ||
1b600bfa | 110 | static int lsattr_args (const char * name) |
3839e657 | 111 | { |
b705640a | 112 | STRUCT_STAT st; |
1b600bfa | 113 | int retval = 0; |
3839e657 | 114 | |
1b600bfa | 115 | if (LSTAT (name, &st) == -1) { |
e1a0a3e3 TT |
116 | com_err (program_name, errno, _("while trying to stat %s"), |
117 | name); | |
1b600bfa ES |
118 | retval = -1; |
119 | } else { | |
e1a0a3e3 | 120 | if (S_ISDIR(st.st_mode) && !dirs_opt) |
1b600bfa | 121 | retval = iterate_on_dir (name, lsattr_dir_proc, NULL); |
3839e657 | 122 | else |
1b600bfa | 123 | retval = list_attributes (name); |
3839e657 | 124 | } |
1b600bfa | 125 | return retval; |
3839e657 TT |
126 | } |
127 | ||
efc6f628 | 128 | static int lsattr_dir_proc (const char * dir_name, struct dirent * de, |
54434927 | 129 | void * private EXT2FS_ATTR((unused))) |
3839e657 | 130 | { |
b705640a | 131 | STRUCT_STAT st; |
a418d3ad | 132 | char *path; |
137ce8ca | 133 | int dir_len = strlen(dir_name); |
a418d3ad | 134 | |
137ce8ca | 135 | path = malloc(dir_len + strlen (de->d_name) + 2); |
3839e657 | 136 | |
137ce8ca TT |
137 | if (dir_len && dir_name[dir_len-1] == '/') |
138 | sprintf (path, "%s%s", dir_name, de->d_name); | |
139 | else | |
140 | sprintf (path, "%s/%s", dir_name, de->d_name); | |
b705640a | 141 | if (LSTAT (path, &st) == -1) |
3839e657 | 142 | perror (path); |
a418d3ad TT |
143 | else { |
144 | if (de->d_name[0] != '.' || all) { | |
3839e657 TT |
145 | list_attributes (path); |
146 | if (S_ISDIR(st.st_mode) && recursive && | |
a418d3ad TT |
147 | strcmp(de->d_name, ".") && |
148 | strcmp(de->d_name, "..")) { | |
3839e657 | 149 | printf ("\n%s:\n", path); |
e1a0a3e3 | 150 | iterate_on_dir (path, lsattr_dir_proc, NULL); |
3839e657 TT |
151 | printf ("\n"); |
152 | } | |
153 | } | |
154 | } | |
a418d3ad | 155 | free(path); |
3839e657 TT |
156 | return 0; |
157 | } | |
158 | ||
00e5433e | 159 | int main (int argc, char ** argv) |
3839e657 | 160 | { |
519149fb | 161 | int c; |
3839e657 | 162 | int i; |
1b600bfa | 163 | int err, retval = 0; |
3839e657 | 164 | |
d9c56d3c TT |
165 | #ifdef ENABLE_NLS |
166 | setlocale(LC_MESSAGES, ""); | |
14308a53 | 167 | setlocale(LC_CTYPE, ""); |
d9c56d3c TT |
168 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
169 | textdomain(NLS_CAT_NAME); | |
9d4507c5 | 170 | set_com_err_gettext(gettext); |
d9c56d3c | 171 | #endif |
3839e657 TT |
172 | if (argc && *argv) |
173 | program_name = *argv; | |
a88fa0c0 | 174 | while ((c = getopt (argc, argv, "RVadlv")) != EOF) |
3839e657 TT |
175 | switch (c) |
176 | { | |
177 | case 'R': | |
178 | recursive = 1; | |
179 | break; | |
a88fa0c0 TT |
180 | case 'V': |
181 | verbose = 1; | |
182 | break; | |
3839e657 TT |
183 | case 'a': |
184 | all = 1; | |
185 | break; | |
186 | case 'd': | |
e1a0a3e3 | 187 | dirs_opt = 1; |
3839e657 | 188 | break; |
f3db3566 | 189 | case 'l': |
e1a0a3e3 | 190 | pf_options = PFOPT_LONG; |
f3db3566 | 191 | break; |
3839e657 | 192 | case 'v': |
e1a0a3e3 | 193 | generation_opt = 1; |
3839e657 TT |
194 | break; |
195 | default: | |
818180cd | 196 | usage(); |
3839e657 TT |
197 | } |
198 | ||
a88fa0c0 | 199 | if (verbose) |
0f8973fb TT |
200 | fprintf (stderr, "lsattr %s (%s)\n", |
201 | E2FSPROGS_VERSION, E2FSPROGS_DATE); | |
1b600bfa ES |
202 | if (optind > argc - 1) { |
203 | if (lsattr_args (".") == -1) | |
204 | retval = 1; | |
205 | } else { | |
206 | for (i = optind; i < argc; i++) { | |
207 | err = lsattr_args (argv[i]); | |
208 | if (err) | |
209 | retval = 1; | |
210 | } | |
211 | } | |
212 | exit(retval); | |
3839e657 | 213 | } |