Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * lsattr.c - List file attributes on an ext2 file system |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> |
| 5 | * Laboratoire MASI, Institut Blaise Pascal |
| 6 | * Universite Pierre et Marie Curie (Paris VI) |
| 7 | * |
| 8 | * This file can be redistributed under the terms of the GNU General |
| 9 | * Public License |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * History: |
| 14 | * 93/10/30 - Creation |
| 15 | * 93/11/13 - Replace stat() calls by lstat() to avoid loops |
| 16 | * 94/02/27 - Integrated in Ted's distribution |
| 17 | * 98/12/29 - Display version info only when -V specified (G M Sipe) |
| 18 | */ |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <getopt.h> |
| 25 | #include <stdio.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/param.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
| 32 | #include <ext2fs/ext2_fs.h> |
| 33 | #include "e2fsbb.h" |
| 34 | #include "e2p/e2p.h" |
| 35 | |
| 36 | #ifdef __GNUC__ |
| 37 | #define EXT2FS_ATTR(x) __attribute__(x) |
| 38 | #else |
| 39 | #define EXT2FS_ATTR(x) |
| 40 | #endif |
| 41 | |
| 42 | static int all; |
| 43 | static int dirs_opt; |
| 44 | static unsigned pf_options; |
| 45 | static int recursive; |
| 46 | static int verbose; |
| 47 | static int generation_opt; |
| 48 | |
| 49 | #ifdef _LFS64_LARGEFILE |
| 50 | #define LSTAT lstat64 |
| 51 | #define STRUCT_STAT struct stat64 |
| 52 | #else |
| 53 | #define LSTAT lstat |
| 54 | #define STRUCT_STAT struct stat |
| 55 | #endif |
| 56 | |
| 57 | #if 0 |
| 58 | static void usage(void) |
| 59 | { |
| 60 | fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); |
| 61 | exit(1); |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | static void list_attributes (const char * name) |
| 66 | { |
| 67 | unsigned long flags; |
| 68 | unsigned long generation; |
| 69 | |
| 70 | if (fgetflags (name, &flags) == -1) { |
| 71 | com_err (program_name, errno, _("While reading flags on %s"), |
| 72 | name); |
| 73 | return; |
| 74 | } |
| 75 | if (generation_opt) { |
| 76 | if (fgetversion (name, &generation) == -1) { |
| 77 | com_err (program_name, errno, |
| 78 | _("While reading version on %s"), |
| 79 | name); |
| 80 | return; |
| 81 | } |
| 82 | printf ("%5lu ", generation); |
| 83 | } |
| 84 | if (pf_options & PFOPT_LONG) { |
| 85 | printf("%-28s ", name); |
| 86 | print_flags(stdout, flags, pf_options); |
| 87 | fputc('\n', stdout); |
| 88 | } else { |
| 89 | print_flags(stdout, flags, pf_options); |
| 90 | printf(" %s\n", name); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static int lsattr_dir_proc (const char *, struct dirent *, void *); |
| 95 | |
| 96 | static void lsattr_args (const char * name) |
| 97 | { |
| 98 | STRUCT_STAT st; |
| 99 | |
| 100 | if (LSTAT (name, &st) == -1) |
| 101 | com_err (program_name, errno, _("while trying to stat %s"), |
| 102 | name); |
| 103 | else { |
| 104 | if (S_ISDIR(st.st_mode) && !dirs_opt) |
| 105 | iterate_on_dir (name, lsattr_dir_proc, NULL); |
| 106 | else |
| 107 | list_attributes (name); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static int lsattr_dir_proc (const char * dir_name, struct dirent * de, |
| 112 | void * private EXT2FS_ATTR((unused))) |
| 113 | { |
| 114 | STRUCT_STAT st; |
| 115 | char *path; |
| 116 | int dir_len = strlen(dir_name); |
| 117 | |
| 118 | path = malloc(dir_len + strlen (de->d_name) + 2); |
| 119 | |
| 120 | if (dir_len && dir_name[dir_len-1] == '/') |
| 121 | sprintf (path, "%s%s", dir_name, de->d_name); |
| 122 | else |
| 123 | sprintf (path, "%s/%s", dir_name, de->d_name); |
| 124 | if (LSTAT (path, &st) == -1) |
| 125 | perror (path); |
| 126 | else { |
| 127 | if (de->d_name[0] != '.' || all) { |
| 128 | list_attributes (path); |
| 129 | if (S_ISDIR(st.st_mode) && recursive && |
| 130 | strcmp(de->d_name, ".") && |
| 131 | strcmp(de->d_name, "..")) { |
| 132 | printf ("\n%s:\n", path); |
| 133 | iterate_on_dir (path, lsattr_dir_proc, NULL); |
| 134 | printf ("\n"); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | free(path); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int lsattr_main (int argc, char ** argv) |
| 143 | { |
| 144 | int c; |
| 145 | int i; |
| 146 | |
| 147 | #ifdef ENABLE_NLS |
| 148 | setlocale(LC_MESSAGES, ""); |
| 149 | setlocale(LC_CTYPE, ""); |
| 150 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 151 | textdomain(NLS_CAT_NAME); |
| 152 | #endif |
| 153 | #if 0 |
| 154 | if (argc && *argv) |
| 155 | program_name = *argv; |
| 156 | #endif |
| 157 | while ((c = getopt (argc, argv, "Radlv")) != EOF) |
| 158 | switch (c) |
| 159 | { |
| 160 | case 'R': |
| 161 | recursive = 1; |
| 162 | break; |
| 163 | case 'V': |
| 164 | verbose = 1; |
| 165 | break; |
| 166 | case 'a': |
| 167 | all = 1; |
| 168 | break; |
| 169 | case 'd': |
| 170 | dirs_opt = 1; |
| 171 | break; |
| 172 | case 'l': |
| 173 | pf_options = PFOPT_LONG; |
| 174 | break; |
| 175 | case 'v': |
| 176 | generation_opt = 1; |
| 177 | break; |
| 178 | default: |
| 179 | usage(); |
| 180 | } |
| 181 | |
| 182 | #if 0 |
| 183 | if (verbose) |
| 184 | fprintf (stderr, "lsattr %s (%s)\n", |
| 185 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
| 186 | #endif |
| 187 | if (optind > argc - 1) |
| 188 | lsattr_args ("."); |
| 189 | else |
| 190 | for (i = optind; i < argc; i++) |
| 191 | lsattr_args (argv[i]); |
| 192 | exit(0); |
| 193 | } |