"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 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 | |
| 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> |
| 32 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 33 | #include "ext2fs/ext2_fs.h" |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 34 | #include "e2fsbb.h" |
| 35 | #include "e2p/e2p.h" |
| 36 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 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; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 43 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 44 | static void list_attributes(const char *name) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 45 | { |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 46 | unsigned long fsflags; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 47 | unsigned long generation; |
| 48 | |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 49 | if (fgetflags(name, &fsflags) == -1) |
| 50 | goto read_err; |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 51 | if (flags & OPT_GENERATION) { |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 52 | if (fgetversion(name, &generation) == -1) |
| 53 | goto read_err; |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 54 | printf("%5lu ", generation); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 55 | } |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 56 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 57 | if (flags & OPT_PF_LONG) { |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 58 | printf("%-28s ", name); |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 59 | print_flags(stdout, fsflags, PFOPT_LONG); |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 60 | printf("\n"); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 61 | } else { |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 62 | print_flags(stdout, fsflags, 0); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 63 | printf(" %s\n", name); |
| 64 | } |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 65 | |
| 66 | return; |
| 67 | read_err: |
| 68 | bb_perror_msg("reading %s", name); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 71 | static int lsattr_dir_proc(const char *, struct dirent *, void *); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 72 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 73 | static void lsattr_args(const char *name) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 74 | { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 75 | struct stat st; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 77 | if (lstat(name, &st) == -1) { |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 78 | bb_perror_msg("stating %s", name); |
| 79 | } else { |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 80 | if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT)) |
| 81 | iterate_on_dir(name, lsattr_dir_proc, NULL); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 82 | else |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 83 | list_attributes(name); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 87 | static int lsattr_dir_proc(const char *dir_name, struct dirent *de, |
Rob Landley | 7c94bed | 2006-05-03 21:58:45 +0000 | [diff] [blame] | 88 | void *private) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 89 | { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 90 | struct stat st; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 91 | char *path; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 92 | |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 93 | path = concat_path_file(dir_name, de->d_name); |
Mike Frysinger | c238a97 | 2005-04-25 05:24:35 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 95 | if (lstat(path, &st) == -1) |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 96 | bb_perror_msg(path); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 97 | else { |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 98 | if (de->d_name[0] != '.' || (flags & OPT_ALL)) { |
| 99 | list_attributes(path); |
| 100 | if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) && |
Mike Frysinger | ea338ff | 2005-05-07 07:17:43 +0000 | [diff] [blame] | 101 | (de->d_name[0] != '.' && (de->d_name[1] != '\0' || |
| 102 | (de->d_name[1] != '.' && de->d_name[2] != '\0')))) { |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 103 | printf("\n%s:\n", path); |
| 104 | iterate_on_dir(path, lsattr_dir_proc, NULL); |
| 105 | printf("\n"); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
Mike Frysinger | c238a97 | 2005-04-25 05:24:35 +0000 | [diff] [blame] | 109 | |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 110 | free(path); |
Mike Frysinger | c238a97 | 2005-04-25 05:24:35 +0000 | [diff] [blame] | 111 | |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 115 | int lsattr_main(int argc, char **argv) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 116 | { |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 117 | int i; |
| 118 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 119 | flags = getopt32(argc, argv, "Radlv"); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 120 | |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 121 | if (optind > argc - 1) |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 122 | lsattr_args("."); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 123 | else |
| 124 | for (i = optind; i < argc; i++) |
Mike Frysinger | d2a64d2 | 2005-04-25 04:10:35 +0000 | [diff] [blame] | 125 | lsattr_args(argv[i]); |
| 126 | |
| 127 | return EXIT_SUCCESS; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 128 | } |