2 * lsattr.c - List file attributes on an ext2 file system
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)
8 * This file can be redistributed under the terms of the GNU General
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)
20 #define _LARGEFILE64_SOURCE
22 #include <sys/types.h>
38 #include <sys/param.h>
41 #include "ext2fs/ext2_fs.h"
42 #include "et/com_err.h"
45 #include "../version.h"
46 #include "nls-enable.h"
49 #define EXT2FS_ATTR(x) __attribute__(x)
51 #define EXT2FS_ATTR(x)
54 static const char * program_name = "lsattr";
58 static unsigned pf_options;
61 static int generation_opt;
63 #ifdef _LFS64_LARGEFILE
65 #define STRUCT_STAT struct stat64
68 #define STRUCT_STAT struct stat
71 static void usage(void)
73 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
77 static void list_attributes (const char * name)
80 unsigned long generation;
82 if (fgetflags (name, &flags) == -1) {
83 com_err (program_name, errno, _("While reading flags on %s"),
88 if (fgetversion (name, &generation) == -1) {
89 com_err (program_name, errno,
90 _("While reading version on %s"),
94 printf ("%5lu ", generation);
96 if (pf_options & PFOPT_LONG) {
97 printf("%-28s ", name);
98 print_flags(stdout, flags, pf_options);
101 print_flags(stdout, flags, pf_options);
102 printf(" %s\n", name);
106 static int lsattr_dir_proc (const char *, struct dirent *, void *);
108 static void lsattr_args (const char * name)
112 if (LSTAT (name, &st) == -1)
113 com_err (program_name, errno, _("while trying to stat %s"),
116 if (S_ISDIR(st.st_mode) && !dirs_opt)
117 iterate_on_dir (name, lsattr_dir_proc, NULL);
119 list_attributes (name);
123 static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
124 void * private EXT2FS_ATTR((unused)))
128 int dir_len = strlen(dir_name);
130 path = malloc(dir_len + strlen (de->d_name) + 2);
132 if (dir_len && dir_name[dir_len-1] == '/')
133 sprintf (path, "%s%s", dir_name, de->d_name);
135 sprintf (path, "%s/%s", dir_name, de->d_name);
136 if (LSTAT (path, &st) == -1)
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);
154 int main (int argc, char ** argv)
160 setlocale(LC_MESSAGES, "");
161 setlocale(LC_CTYPE, "");
162 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
163 textdomain(NLS_CAT_NAME);
166 program_name = *argv;
167 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
183 pf_options = PFOPT_LONG;
193 fprintf (stderr, "lsattr %s (%s)\n",
194 E2FSPROGS_VERSION, E2FSPROGS_DATE);
195 if (optind > argc - 1)
198 for (i = optind; i < argc; i++)
199 lsattr_args (argv[i]);