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