Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +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 |
Theodore Ts'o | a88fa0c | 1999-01-05 07:02:39 +0000 | [diff] [blame] | 17 | * 98/12/29 - Display version info only when -V specified (G M Sipe) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Theodore Ts'o | fff18b4 | 2001-02-08 03:06:43 +0000 | [diff] [blame] | 20 | #define _LARGEFILE64_SOURCE |
Theodore Ts'o | fff18b4 | 2001-02-08 03:06:43 +0000 | [diff] [blame] | 21 | |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <dirent.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 24 | #ifdef HAVE_ERRNO_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 26 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 28 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 30 | #else |
| 31 | extern int optind; |
| 32 | extern char *optarg; |
| 33 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <unistd.h> |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 37 | #include <string.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | #include <sys/param.h> |
| 39 | #include <sys/stat.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | |
Theodore Ts'o | 54c637d | 2001-05-14 11:45:38 +0000 | [diff] [blame] | 41 | #include "ext2fs/ext2_fs.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | #include "et/com_err.h" |
| 43 | #include "e2p/e2p.h" |
| 44 | |
| 45 | #include "../version.h" |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 46 | #include "nls-enable.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 48 | static const char * program_name = "lsattr"; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 49 | |
Theodore Ts'o | f10748d | 2000-12-31 13:55:14 +0000 | [diff] [blame] | 50 | static int all; |
| 51 | static int dirs_opt; |
| 52 | static unsigned pf_options; |
| 53 | static int recursive; |
| 54 | static int verbose; |
| 55 | static int generation_opt; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 56 | |
Theodore Ts'o | b705640 | 2001-06-08 02:53:20 +0000 | [diff] [blame] | 57 | #ifdef _LFS64_LARGEFILE |
| 58 | #define LSTAT lstat64 |
| 59 | #define STRUCT_STAT struct stat64 |
| 60 | #else |
| 61 | #define LSTAT lstat |
| 62 | #define STRUCT_STAT struct stat |
| 63 | #endif |
| 64 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 65 | static void usage(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 67 | fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 68 | exit(1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void list_attributes (const char * name) |
| 72 | { |
| 73 | unsigned long flags; |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 74 | unsigned long generation; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | |
Theodore Ts'o | f10748d | 2000-12-31 13:55:14 +0000 | [diff] [blame] | 76 | if (fgetflags (name, &flags) == -1) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 77 | com_err (program_name, errno, _("While reading flags on %s"), |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 78 | name); |
Theodore Ts'o | f10748d | 2000-12-31 13:55:14 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | if (generation_opt) { |
| 82 | if (fgetversion (name, &generation) == -1) { |
| 83 | com_err (program_name, errno, |
| 84 | _("While reading version on %s"), |
| 85 | name); |
| 86 | return; |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 87 | } |
Theodore Ts'o | f10748d | 2000-12-31 13:55:14 +0000 | [diff] [blame] | 88 | printf ("%5lu ", generation); |
| 89 | } |
| 90 | if (pf_options & PFOPT_LONG) { |
| 91 | printf("%-28s ", name); |
| 92 | print_flags(stdout, flags, pf_options); |
| 93 | fputc('\n', stdout); |
| 94 | } else { |
| 95 | print_flags(stdout, flags, pf_options); |
| 96 | printf(" %s\n", name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | static int lsattr_dir_proc (const char *, struct dirent *, void *); |
| 101 | |
| 102 | static void lsattr_args (const char * name) |
| 103 | { |
Theodore Ts'o | b705640 | 2001-06-08 02:53:20 +0000 | [diff] [blame] | 104 | STRUCT_STAT st; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | |
Theodore Ts'o | b705640 | 2001-06-08 02:53:20 +0000 | [diff] [blame] | 106 | if (LSTAT (name, &st) == -1) |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 107 | com_err (program_name, errno, _("while trying to stat %s"), |
| 108 | name); |
| 109 | else { |
| 110 | if (S_ISDIR(st.st_mode) && !dirs_opt) |
| 111 | iterate_on_dir (name, lsattr_dir_proc, NULL); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 112 | else |
| 113 | list_attributes (name); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private) |
| 118 | { |
Theodore Ts'o | b705640 | 2001-06-08 02:53:20 +0000 | [diff] [blame] | 119 | STRUCT_STAT st; |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 120 | char *path; |
| 121 | |
| 122 | path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 123 | |
| 124 | sprintf (path, "%s/%s", dir_name, de->d_name); |
Theodore Ts'o | b705640 | 2001-06-08 02:53:20 +0000 | [diff] [blame] | 125 | if (LSTAT (path, &st) == -1) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 126 | perror (path); |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 127 | else { |
| 128 | if (de->d_name[0] != '.' || all) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 129 | list_attributes (path); |
| 130 | if (S_ISDIR(st.st_mode) && recursive && |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 131 | strcmp(de->d_name, ".") && |
| 132 | strcmp(de->d_name, "..")) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 133 | printf ("\n%s:\n", path); |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 134 | iterate_on_dir (path, lsattr_dir_proc, NULL); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 135 | printf ("\n"); |
| 136 | } |
| 137 | } |
| 138 | } |
Theodore Ts'o | a418d3a | 1997-04-26 14:00:26 +0000 | [diff] [blame] | 139 | free(path); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Theodore Ts'o | 00e5433 | 1997-09-16 02:13:52 +0000 | [diff] [blame] | 143 | int main (int argc, char ** argv) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 144 | { |
Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 145 | int c; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | int i; |
| 147 | |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 148 | #ifdef ENABLE_NLS |
| 149 | setlocale(LC_MESSAGES, ""); |
Theodore Ts'o | 14308a5 | 2002-03-05 03:26:52 -0500 | [diff] [blame] | 150 | setlocale(LC_CTYPE, ""); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 151 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 152 | textdomain(NLS_CAT_NAME); |
| 153 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 154 | if (argc && *argv) |
| 155 | program_name = *argv; |
Theodore Ts'o | a88fa0c | 1999-01-05 07:02:39 +0000 | [diff] [blame] | 156 | while ((c = getopt (argc, argv, "RVadlv")) != EOF) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 157 | switch (c) |
| 158 | { |
| 159 | case 'R': |
| 160 | recursive = 1; |
| 161 | break; |
Theodore Ts'o | a88fa0c | 1999-01-05 07:02:39 +0000 | [diff] [blame] | 162 | case 'V': |
| 163 | verbose = 1; |
| 164 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 165 | case 'a': |
| 166 | all = 1; |
| 167 | break; |
| 168 | case 'd': |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 169 | dirs_opt = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 170 | break; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 171 | case 'l': |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 172 | pf_options = PFOPT_LONG; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 173 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 174 | case 'v': |
Theodore Ts'o | e1a0a3e | 2000-02-11 05:00:19 +0000 | [diff] [blame] | 175 | generation_opt = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 176 | break; |
| 177 | default: |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 178 | usage(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Theodore Ts'o | a88fa0c | 1999-01-05 07:02:39 +0000 | [diff] [blame] | 181 | if (verbose) |
Theodore Ts'o | 0f8973f | 2001-08-27 12:44:23 -0400 | [diff] [blame] | 182 | fprintf (stderr, "lsattr %s (%s)\n", |
| 183 | E2FSPROGS_VERSION, E2FSPROGS_DATE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 184 | if (optind > argc - 1) |
| 185 | lsattr_args ("."); |
| 186 | else |
| 187 | for (i = optind; i < argc; i++) |
| 188 | lsattr_args (argv[i]); |
Theodore Ts'o | 00e5433 | 1997-09-16 02:13:52 +0000 | [diff] [blame] | 189 | exit(0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 190 | } |