Apply post-1.21.0 patches, bump version to 1.21.1
[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 //usage:#define lsattr_trivial_usage
22 //usage:       "[-Radlv] [FILE]..."
23 //usage:#define lsattr_full_usage "\n\n"
24 //usage:       "List file attributes on an ext2 fs\n"
25 //usage:     "\n        -R      Recurse"
26 //usage:     "\n        -a      Don't hide entries starting with ."
27 //usage:     "\n        -d      List directory entries instead of contents"
28 //usage:     "\n        -l      List long flag names"
29 //usage:     "\n        -v      List the file's version/generation number"
30
31 #include "libbb.h"
32 #include "e2fs_lib.h"
33
34 enum {
35         OPT_RECUR      = 0x1,
36         OPT_ALL        = 0x2,
37         OPT_DIRS_OPT   = 0x4,
38         OPT_PF_LONG    = 0x8,
39         OPT_GENERATION = 0x10,
40 };
41
42 static void list_attributes(const char *name)
43 {
44         unsigned long fsflags;
45         unsigned long generation;
46
47         if (fgetflags(name, &fsflags) != 0)
48                 goto read_err;
49
50         if (option_mask32 & OPT_GENERATION) {
51                 if (fgetversion(name, &generation) != 0)
52                         goto read_err;
53                 printf("%5lu ", generation);
54         }
55
56         if (option_mask32 & OPT_PF_LONG) {
57                 printf("%-28s ", name);
58                 print_e2flags(stdout, fsflags, PFOPT_LONG);
59                 bb_putchar('\n');
60         } else {
61                 print_e2flags(stdout, fsflags, 0);
62                 printf(" %s\n", name);
63         }
64
65         return;
66  read_err:
67         bb_perror_msg("reading %s", name);
68 }
69
70 static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
71                 struct dirent *de,
72                 void *private UNUSED_PARAM)
73 {
74         struct stat st;
75         char *path;
76
77         path = concat_path_file(dir_name, de->d_name);
78
79         if (lstat(path, &st) != 0)
80                 bb_perror_msg("stat %s", path);
81         else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
82                 list_attributes(path);
83                 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
84                  && !DOT_OR_DOTDOT(de->d_name)
85                 ) {
86                         printf("\n%s:\n", path);
87                         iterate_on_dir(path, lsattr_dir_proc, NULL);
88                         bb_putchar('\n');
89                 }
90         }
91
92         free(path);
93         return 0;
94 }
95
96 static void lsattr_args(const char *name)
97 {
98         struct stat st;
99
100         if (lstat(name, &st) == -1) {
101                 bb_perror_msg("stat %s", name);
102         } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
103                 iterate_on_dir(name, lsattr_dir_proc, NULL);
104         } else {
105                 list_attributes(name);
106         }
107 }
108
109 int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110 int lsattr_main(int argc UNUSED_PARAM, char **argv)
111 {
112         getopt32(argv, "Radlv");
113         argv += optind;
114
115         if (!*argv)
116                 *--argv = (char*)".";
117         do lsattr_args(*argv++); while (*argv);
118
119         return EXIT_SUCCESS;
120 }