getopt_ulflags -> getopt32.
[oweals/busybox.git] / e2fsprogs / lsattr.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * lsattr.c             - List file attributes on an ext2 file system
4  *
5  * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
6  *                           Laboratoire MASI, Institut Blaise Pascal
7  *                           Universite Pierre et Marie Curie (Paris VI)
8  *
9  * This file can be redistributed under the terms of the GNU General
10  * Public License
11  */
12
13 /*
14  * History:
15  * 93/10/30     - Creation
16  * 93/11/13     - Replace stat() calls by lstat() to avoid loops
17  * 94/02/27     - Integrated in Ted's distribution
18  * 98/12/29     - Display version info only when -V specified (G M Sipe)
19  */
20
21 #include <sys/types.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32
33 #include "ext2fs/ext2_fs.h"
34 #include "e2fsbb.h"
35 #include "e2p/e2p.h"
36
37 #define OPT_RECUR 1
38 #define OPT_ALL 2
39 #define OPT_DIRS_OPT 4
40 #define OPT_PF_LONG 8
41 #define OPT_GENERATION 16
42 static int flags;
43
44 #ifdef CONFIG_LFS
45 # define LSTAT lstat64
46 # define STRUCT_STAT struct stat64
47 #else
48 # define LSTAT lstat
49 # define STRUCT_STAT struct stat
50 #endif
51
52 static void list_attributes(const char *name)
53 {
54         unsigned long fsflags;
55         unsigned long generation;
56
57         if (fgetflags(name, &fsflags) == -1)
58                 goto read_err;
59         if (flags & OPT_GENERATION) {
60                 if (fgetversion(name, &generation) == -1)
61                         goto read_err;
62                 printf("%5lu ", generation);
63         }
64
65         if (flags & OPT_PF_LONG) {
66                 printf("%-28s ", name);
67                 print_flags(stdout, fsflags, PFOPT_LONG);
68                 printf("\n");
69         } else {
70                 print_flags(stdout, fsflags, 0);
71                 printf(" %s\n", name);
72         }
73
74         return;
75 read_err:
76         bb_perror_msg("reading %s", name);
77 }
78
79 static int lsattr_dir_proc(const char *, struct dirent *, void *);
80
81 static void lsattr_args(const char *name)
82 {
83         STRUCT_STAT     st;
84
85         if (LSTAT(name, &st) == -1) {
86                 bb_perror_msg("stating %s", name);
87         } else {
88                 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
89                         iterate_on_dir(name, lsattr_dir_proc, NULL);
90                 else
91                         list_attributes(name);
92         }
93 }
94
95 static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
96                            void *private)
97 {
98         STRUCT_STAT     st;
99         char *path;
100
101         path = concat_path_file(dir_name, de->d_name);
102
103         if (LSTAT(path, &st) == -1)
104                 bb_perror_msg(path);
105         else {
106                 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
107                         list_attributes(path);
108                         if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
109                            (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
110                            (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
111                                 printf("\n%s:\n", path);
112                                 iterate_on_dir(path, lsattr_dir_proc, NULL);
113                                 printf("\n");
114                         }
115                 }
116         }
117
118         free(path);
119
120         return 0;
121 }
122
123 int lsattr_main(int argc, char **argv)
124 {
125         int i;
126
127         flags = getopt32(argc, argv, "Radlv");
128
129         if (optind > argc - 1)
130                 lsattr_args(".");
131         else
132                 for (i = optind; i < argc; i++)
133                         lsattr_args(argv[i]);
134
135         return EXIT_SUCCESS;
136 }