]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/lsattr.c
Many files:
[thirdparty/e2fsprogs.git] / misc / lsattr.c
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 #ifdef HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25 #include <fcntl.h>
26 #ifdef HAVE_GETOPT_H
27 #include <getopt.h>
28 #else
29 extern int optind;
30 extern char *optarg;
31 #endif
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/param.h>
37 #include <sys/stat.h>
38 #include <linux/ext2_fs.h>
39
40 #include "et/com_err.h"
41 #include "e2p/e2p.h"
42
43 #include "../version.h"
44 #include "nls-enable.h"
45
46 static const char * program_name = "lsattr";
47
48 static int all = 0;
49 static int dirs_opt = 0;
50 static unsigned pf_options = 0;
51 static int recursive = 0;
52 static int verbose = 0;
53 static int generation_opt = 0;
54
55 static void usage(void)
56 {
57 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
58 exit(1);
59 }
60
61 static void list_attributes (const char * name)
62 {
63 unsigned long flags;
64 unsigned long generation;
65
66 if (fgetflags (name, &flags) == -1)
67 com_err (program_name, errno, _("While reading flags on %s"),
68 name);
69 else if (fgetversion (name, &generation) == -1)
70 com_err (program_name, errno, _("While reading version on %s"),
71 name);
72 else
73 {
74 if (generation_opt)
75 printf ("%5lu ", generation);
76 if (pf_options & PFOPT_LONG) {
77 printf("%-28s ", name);
78 print_flags(stdout, flags, pf_options);
79 fputc('\n', stdout);
80 } else {
81 print_flags(stdout, flags, pf_options);
82 printf(" %s\n", name);
83 }
84 }
85 }
86
87 static int lsattr_dir_proc (const char *, struct dirent *, void *);
88
89 static void lsattr_args (const char * name)
90 {
91 struct stat st;
92
93 if (lstat (name, &st) == -1)
94 com_err (program_name, errno, _("while trying to stat %s"),
95 name);
96 else {
97 if (S_ISDIR(st.st_mode) && !dirs_opt)
98 iterate_on_dir (name, lsattr_dir_proc, NULL);
99 else
100 list_attributes (name);
101 }
102 }
103
104 static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
105 {
106 struct stat st;
107 char *path;
108
109 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
110
111 sprintf (path, "%s/%s", dir_name, de->d_name);
112 if (lstat (path, &st) == -1)
113 perror (path);
114 else {
115 if (de->d_name[0] != '.' || all) {
116 list_attributes (path);
117 if (S_ISDIR(st.st_mode) && recursive &&
118 strcmp(de->d_name, ".") &&
119 strcmp(de->d_name, "..")) {
120 printf ("\n%s:\n", path);
121 iterate_on_dir (path, lsattr_dir_proc, NULL);
122 printf ("\n");
123 }
124 }
125 }
126 free(path);
127 return 0;
128 }
129
130 int main (int argc, char ** argv)
131 {
132 int c;
133 int i;
134
135 #ifdef ENABLE_NLS
136 setlocale(LC_MESSAGES, "");
137 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
138 textdomain(NLS_CAT_NAME);
139 #endif
140 if (argc && *argv)
141 program_name = *argv;
142 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
143 switch (c)
144 {
145 case 'R':
146 recursive = 1;
147 break;
148 case 'V':
149 verbose = 1;
150 break;
151 case 'a':
152 all = 1;
153 break;
154 case 'd':
155 dirs_opt = 1;
156 break;
157 case 'l':
158 pf_options = PFOPT_LONG;
159 break;
160 case 'v':
161 generation_opt = 1;
162 break;
163 default:
164 usage();
165 }
166
167 if (verbose)
168 fprintf (stderr, _("lsattr %s, %s for EXT2 FS %s, %s\n"),
169 E2FSPROGS_VERSION, E2FSPROGS_DATE,
170 EXT2FS_VERSION, EXT2FS_DATE);
171 if (optind > argc - 1)
172 lsattr_args (".");
173 else
174 for (i = optind; i < argc; i++)
175 lsattr_args (argv[i]);
176 exit(0);
177 }