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
148 if (dir_len && dir_name[dir_len-1] == '/')
149 sprintf (path, "%s%s", dir_name, de->d_name);
150 else
151 sprintf (path, "%s/%s", dir_name, de->d_name);
152 if (LSTAT (path, &st) == -1)
153 perror (path);
154 else {
155 if (de->d_name[0] != '.' || all) {
156 list_attributes (path);
157 if (S_ISDIR(st.st_mode) && recursive &&
158 strcmp(de->d_name, ".") &&
159 strcmp(de->d_name, "..")) {
160 printf ("\n%s:\n", path);
161 iterate_on_dir (path, lsattr_dir_proc, NULL);
162 printf ("\n");
163 }
164 }
165 }
166 free(path);
167 return 0;
168 }
169
main(int argc,char ** argv)170 int main (int argc, char ** argv)
171 {
172 int c;
173 int i;
174 int err, retval = 0;
175
176 #ifdef ENABLE_NLS
177 setlocale(LC_MESSAGES, "");
178 setlocale(LC_CTYPE, "");
179 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
180 textdomain(NLS_CAT_NAME);
181 set_com_err_gettext(gettext);
182 #endif
183 if (argc && *argv)
184 program_name = *argv;
185 while ((c = getopt (argc, argv, "RVadlvp")) != EOF)
186 switch (c)
187 {
188 case 'R':
189 recursive = 1;
190 break;
191 case 'V':
192 verbose = 1;
193 break;
194 case 'a':
195 all = 1;
196 break;
197 case 'd':
198 dirs_opt = 1;
199 break;
200 case 'l':
201 pf_options = PFOPT_LONG;
202 break;
203 case 'v':
204 generation_opt = 1;
205 break;
206 case 'p':
207 project_opt = 1;
208 break;
209 default:
210 usage();
211 }
212
213 if (verbose)
214 fprintf (stderr, "lsattr %s (%s)\n",
215 E2FSPROGS_VERSION, E2FSPROGS_DATE);
216 if (optind > argc - 1) {
217 if (lsattr_args (".") == -1)
218 retval = 1;
219 } else {
220 for (i = optind; i < argc; i++) {
221 err = lsattr_args (argv[i]);
222 if (err)
223 retval = 1;
224 }
225 }
226 exit(retval);
227 }
228