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