blob: 23a54b72e04507460586e37a32ea2e071a187db8 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* 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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000022#include "e2fs_lib.h"
23
24enum {
25 OPT_RECUR = 0x1,
26 OPT_ALL = 0x2,
27 OPT_DIRS_OPT = 0x4,
28 OPT_PF_LONG = 0x8,
29 OPT_GENERATION = 0x10,
30};
31
32static void list_attributes(const char *name)
33{
34 unsigned long fsflags;
35 unsigned long generation;
36
Denis Vlasenko8acf5212007-04-15 11:48:27 +000037 if (fgetflags(name, &fsflags) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000038 goto read_err;
39
40 if (option_mask32 & OPT_GENERATION) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000041 if (fgetversion(name, &generation) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000042 goto read_err;
43 printf("%5lu ", generation);
44 }
45
46 if (option_mask32 & OPT_PF_LONG) {
47 printf("%-28s ", name);
Denis Vlasenko53354ac2008-06-07 15:10:29 +000048 print_e2flags(stdout, fsflags, PFOPT_LONG);
Denis Vlasenko4daad902007-09-27 10:20:47 +000049 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000050 } else {
Denis Vlasenko53354ac2008-06-07 15:10:29 +000051 print_e2flags(stdout, fsflags, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052 printf(" %s\n", name);
53 }
54
55 return;
56 read_err:
57 bb_perror_msg("reading %s", name);
58}
59
60static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000062{
63 struct stat st;
64 char *path;
65
66 path = concat_path_file(dir_name, de->d_name);
67
Denis Vlasenko8acf5212007-04-15 11:48:27 +000068 if (lstat(path, &st) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000069 bb_perror_msg("stat %s", path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000070 else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
71 list_attributes(path);
72 if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
Denis Vlasenko8acf5212007-04-15 11:48:27 +000073 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000074 ) {
75 printf("\n%s:\n", path);
76 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +000077 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000078 }
79 }
80
81 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000082 return 0;
83}
84
85static void lsattr_args(const char *name)
86{
87 struct stat st;
88
89 if (lstat(name, &st) == -1) {
90 bb_perror_msg("stat %s", name);
91 } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
92 iterate_on_dir(name, lsattr_dir_proc, NULL);
93 } else {
94 list_attributes(name);
95 }
96}
97
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000098int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000099int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000100{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000101 getopt32(argv, "Radlv");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000102 argv += optind;
103
104 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000105 *--argv = (char*)".";
106 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000107
108 return EXIT_SUCCESS;
109}