blob: 1312fe7545719eb227ed2569f59d70a6c0c1f39b [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
Pere Orga6a3e01d2011-04-01 22:56:30 +020021//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"
Pere Orga6a3e01d2011-04-01 22:56:30 +020025//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000032#include "e2fs_lib.h"
33
34enum {
35 OPT_RECUR = 0x1,
36 OPT_ALL = 0x2,
37 OPT_DIRS_OPT = 0x4,
38 OPT_PF_LONG = 0x8,
39 OPT_GENERATION = 0x10,
40};
41
42static void list_attributes(const char *name)
43{
44 unsigned long fsflags;
45 unsigned long generation;
46
Denis Vlasenko8acf5212007-04-15 11:48:27 +000047 if (fgetflags(name, &fsflags) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000048 goto read_err;
49
50 if (option_mask32 & OPT_GENERATION) {
Denis Vlasenko8acf5212007-04-15 11:48:27 +000051 if (fgetversion(name, &generation) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000052 goto read_err;
53 printf("%5lu ", generation);
54 }
55
56 if (option_mask32 & OPT_PF_LONG) {
57 printf("%-28s ", name);
Denis Vlasenko53354ac2008-06-07 15:10:29 +000058 print_e2flags(stdout, fsflags, PFOPT_LONG);
Denis Vlasenko4daad902007-09-27 10:20:47 +000059 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000060 } else {
Denis Vlasenko53354ac2008-06-07 15:10:29 +000061 print_e2flags(stdout, fsflags, 0);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000062 printf(" %s\n", name);
63 }
64
65 return;
66 read_err:
67 bb_perror_msg("reading %s", name);
68}
69
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020070static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
71 struct dirent *de,
72 void *private UNUSED_PARAM)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000073{
74 struct stat st;
75 char *path;
76
77 path = concat_path_file(dir_name, de->d_name);
78
Denis Vlasenko8acf5212007-04-15 11:48:27 +000079 if (lstat(path, &st) != 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000080 bb_perror_msg("stat %s", path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000081 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)
Denis Vlasenko8acf5212007-04-15 11:48:27 +000084 && !DOT_OR_DOTDOT(de->d_name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000085 ) {
86 printf("\n%s:\n", path);
87 iterate_on_dir(path, lsattr_dir_proc, NULL);
Denis Vlasenko4daad902007-09-27 10:20:47 +000088 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000089 }
90 }
91
92 free(path);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000093 return 0;
94}
95
96static 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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000109int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000110int lsattr_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000111{
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000112 getopt32(argv, "Radlv");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000113 argv += optind;
114
115 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000116 *--argv = (char*)".";
117 do lsattr_args(*argv++); while (*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000118
119 return EXIT_SUCCESS;
120}