Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 22 | #include "e2fs_lib.h" |
| 23 | |
| 24 | enum { |
| 25 | OPT_RECUR = 0x1, |
| 26 | OPT_ALL = 0x2, |
| 27 | OPT_DIRS_OPT = 0x4, |
| 28 | OPT_PF_LONG = 0x8, |
| 29 | OPT_GENERATION = 0x10, |
| 30 | }; |
| 31 | |
| 32 | static void list_attributes(const char *name) |
| 33 | { |
| 34 | unsigned long fsflags; |
| 35 | unsigned long generation; |
| 36 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 37 | if (fgetflags(name, &fsflags) != 0) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 38 | goto read_err; |
| 39 | |
| 40 | if (option_mask32 & OPT_GENERATION) { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 41 | if (fgetversion(name, &generation) != 0) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 42 | goto read_err; |
| 43 | printf("%5lu ", generation); |
| 44 | } |
| 45 | |
| 46 | if (option_mask32 & OPT_PF_LONG) { |
| 47 | printf("%-28s ", name); |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 48 | print_e2flags(stdout, fsflags, PFOPT_LONG); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 49 | bb_putchar('\n'); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 50 | } else { |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 51 | print_e2flags(stdout, fsflags, 0); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 52 | printf(" %s\n", name); |
| 53 | } |
| 54 | |
| 55 | return; |
| 56 | read_err: |
| 57 | bb_perror_msg("reading %s", name); |
| 58 | } |
| 59 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 60 | static int FAST_FUNC lsattr_dir_proc(const char *dir_name, |
| 61 | struct dirent *de, |
| 62 | void *private UNUSED_PARAM) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 63 | { |
| 64 | struct stat st; |
| 65 | char *path; |
| 66 | |
| 67 | path = concat_path_file(dir_name, de->d_name); |
| 68 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 69 | if (lstat(path, &st) != 0) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 70 | bb_perror_msg("stat %s", path); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 71 | else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) { |
| 72 | list_attributes(path); |
| 73 | if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR) |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 74 | && !DOT_OR_DOTDOT(de->d_name) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 75 | ) { |
| 76 | printf("\n%s:\n", path); |
| 77 | iterate_on_dir(path, lsattr_dir_proc, NULL); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 78 | bb_putchar('\n'); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | free(path); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void lsattr_args(const char *name) |
| 87 | { |
| 88 | struct stat st; |
| 89 | |
| 90 | if (lstat(name, &st) == -1) { |
| 91 | bb_perror_msg("stat %s", name); |
| 92 | } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) { |
| 93 | iterate_on_dir(name, lsattr_dir_proc, NULL); |
| 94 | } else { |
| 95 | list_attributes(name); |
| 96 | } |
| 97 | } |
| 98 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 99 | int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 100 | int lsattr_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 101 | { |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 102 | getopt32(argv, "Radlv"); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 103 | argv += optind; |
| 104 | |
| 105 | if (!*argv) |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 106 | *--argv = (char*)"."; |
| 107 | do lsattr_args(*argv++); while (*argv); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 108 | |
| 109 | return EXIT_SUCCESS; |
| 110 | } |