lsattr.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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#define _LARGEFILE64_SOURCE 21 22#include "config.h" 23#include <sys/types.h> 24#include <dirent.h> 25#ifdef HAVE_ERRNO_H 26#include <errno.h> 27#endif 28#include <fcntl.h> 29#ifdef HAVE_GETOPT_H 30#include <getopt.h> 31#else 32extern int optind; 33extern char *optarg; 34#endif 35#include <stdio.h> 36#include <unistd.h> 37#include <stdlib.h> 38#include <string.h> 39#include <sys/param.h> 40#include <sys/stat.h> 41 42#include "ext2fs/ext2_fs.h" 43#include "et/com_err.h" 44#include "e2p/e2p.h" 45 46#include "../version.h" 47#include "nls-enable.h" 48 49#ifdef __GNUC__ 50#define EXT2FS_ATTR(x) __attribute__(x) 51#else 52#define EXT2FS_ATTR(x) 53#endif 54 55static const char * program_name = "lsattr"; 56 57static int all; 58static int dirs_opt; 59static unsigned pf_options; 60static int recursive; 61static int verbose; 62static int generation_opt; 63 64#ifdef _LFS64_LARGEFILE 65#define LSTAT lstat64 66#define STRUCT_STAT struct stat64 67#else 68#define LSTAT lstat 69#define STRUCT_STAT struct stat 70#endif 71 72static void usage(void) 73{ 74 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name); 75 exit(1); 76} 77 78static int list_attributes (const char * name) 79{ 80 unsigned long flags; 81 unsigned long generation; 82 83 if (fgetflags (name, &flags) == -1) { 84 com_err (program_name, errno, _("While reading flags on %s"), 85 name); 86 return -1; 87 } 88 if (generation_opt) { 89 if (fgetversion (name, &generation) == -1) { 90 com_err (program_name, errno, 91 _("While reading version on %s"), 92 name); 93 return -1; 94 } 95 printf ("%5lu ", generation); 96 } 97 if (pf_options & PFOPT_LONG) { 98 printf("%-28s ", name); 99 print_flags(stdout, flags, pf_options); 100 fputc('\n', stdout); 101 } else { 102 print_flags(stdout, flags, pf_options); 103 printf(" %s\n", name); 104 } 105 return 0; 106} 107 108static int lsattr_dir_proc (const char *, struct dirent *, void *); 109 110static int lsattr_args (const char * name) 111{ 112 STRUCT_STAT st; 113 int retval = 0; 114 115 if (LSTAT (name, &st) == -1) { 116 com_err (program_name, errno, _("while trying to stat %s"), 117 name); 118 retval = -1; 119 } else { 120 if (S_ISDIR(st.st_mode) && !dirs_opt) 121 retval = iterate_on_dir (name, lsattr_dir_proc, NULL); 122 else 123 retval = list_attributes (name); 124 } 125 return retval; 126} 127 128static int lsattr_dir_proc (const char * dir_name, struct dirent * de, 129 void * private EXT2FS_ATTR((unused))) 130{ 131 STRUCT_STAT st; 132 char *path; 133 int dir_len = strlen(dir_name); 134 135 path = malloc(dir_len + strlen (de->d_name) + 2); 136 137 if (dir_len && dir_name[dir_len-1] == '/') 138 sprintf (path, "%s%s", dir_name, de->d_name); 139 else 140 sprintf (path, "%s/%s", dir_name, de->d_name); 141 if (LSTAT (path, &st) == -1) 142 perror (path); 143 else { 144 if (de->d_name[0] != '.' || all) { 145 list_attributes (path); 146 if (S_ISDIR(st.st_mode) && recursive && 147 strcmp(de->d_name, ".") && 148 strcmp(de->d_name, "..")) { 149 printf ("\n%s:\n", path); 150 iterate_on_dir (path, lsattr_dir_proc, NULL); 151 printf ("\n"); 152 } 153 } 154 } 155 free(path); 156 return 0; 157} 158 159int main (int argc, char ** argv) 160{ 161 int c; 162 int i; 163 int err, retval = 0; 164 165#ifdef ENABLE_NLS 166 setlocale(LC_MESSAGES, ""); 167 setlocale(LC_CTYPE, ""); 168 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 169 textdomain(NLS_CAT_NAME); 170#endif 171 if (argc && *argv) 172 program_name = *argv; 173 while ((c = getopt (argc, argv, "RVadlv")) != EOF) 174 switch (c) 175 { 176 case 'R': 177 recursive = 1; 178 break; 179 case 'V': 180 verbose = 1; 181 break; 182 case 'a': 183 all = 1; 184 break; 185 case 'd': 186 dirs_opt = 1; 187 break; 188 case 'l': 189 pf_options = PFOPT_LONG; 190 break; 191 case 'v': 192 generation_opt = 1; 193 break; 194 default: 195 usage(); 196 } 197 198 if (verbose) 199 fprintf (stderr, "lsattr %s (%s)\n", 200 E2FSPROGS_VERSION, E2FSPROGS_DATE); 201 if (optind > argc - 1) { 202 if (lsattr_args (".") == -1) 203 retval = 1; 204 } else { 205 for (i = optind; i < argc; i++) { 206 err = lsattr_args (argv[i]); 207 if (err) 208 retval = 1; 209 } 210 } 211 exit(retval); 212} 213