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