Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / e2fsprogs / lsattr.c
blob9e0e4cb602f97144138b7225e170e750811ae258
1 /* vi: set sw=4 ts=4: */
2 /*
3 * lsattr.c - List file attributes on an ext2 file system
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)
9 * This file can be redistributed under the terms of the GNU General
10 * Public License
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)
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>
33 #include "ext2fs/ext2_fs.h"
34 #include "e2fsbb.h"
35 #include "e2p/e2p.h"
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;
44 static void list_attributes(const char *name)
46 unsigned long fsflags;
47 unsigned long generation;
49 if (fgetflags(name, &fsflags) == -1)
50 goto read_err;
51 if (flags & OPT_GENERATION) {
52 if (fgetversion(name, &generation) == -1)
53 goto read_err;
54 printf("%5lu ", generation);
57 if (flags & 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);
66 return;
67 read_err:
68 bb_perror_msg("reading %s", name);
71 static int lsattr_dir_proc(const char *, struct dirent *, void *);
73 static void lsattr_args(const char *name)
75 struct stat st;
77 if (lstat(name, &st) == -1) {
78 bb_perror_msg("stating %s", name);
79 } else {
80 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
81 iterate_on_dir(name, lsattr_dir_proc, NULL);
82 else
83 list_attributes(name);
87 static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
88 void *private)
90 struct stat st;
91 char *path;
93 path = concat_path_file(dir_name, de->d_name);
95 if (lstat(path, &st) == -1)
96 bb_perror_msg(path);
97 else {
98 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
99 list_attributes(path);
100 if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
101 (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
102 (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
103 printf("\n%s:\n", path);
104 iterate_on_dir(path, lsattr_dir_proc, NULL);
105 bb_putchar('\n');
110 free(path);
112 return 0;
115 int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
116 int lsattr_main(int argc, char **argv)
118 int i;
120 flags = getopt32(argv, "Radlv");
122 if (optind > argc - 1)
123 lsattr_args(".");
124 else
125 for (i = optind; i < argc; i++)
126 lsattr_args(argv[i]);
128 return EXIT_SUCCESS;