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