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