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