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