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
23 #include <sys/types.h>
39 #include <sys/param.h>
42 #include "ext2fs/ext2_fs.h"
43 #include "et/com_err.h"
46 #include "../version.h"
47 #include "nls-enable.h"
50 #define EXT2FS_ATTR(x) __attribute__(x)
52 #define EXT2FS_ATTR(x)
55 static const char * program_name = "lsattr";
59 static unsigned pf_options;
62 static int generation_opt;
64 #ifdef _LFS64_LARGEFILE
66 #define STRUCT_STAT struct stat64
69 #define STRUCT_STAT struct stat
72 static void usage(void)
74 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
78 static int list_attributes (const char * name)
81 unsigned long generation;
83 if (fgetflags (name, &flags) == -1) {
84 com_err (program_name, errno, _("While reading flags on %s"),
89 if (fgetversion (name, &generation) == -1) {
90 com_err (program_name, errno,
91 _("While reading version on %s"),
95 printf ("%5lu ", generation);
97 if (pf_options & PFOPT_LONG) {
98 printf("%-28s ", name);
99 print_flags(stdout, flags, pf_options);
102 print_flags(stdout, flags, pf_options);
103 printf(" %s\n", name);
108 static int lsattr_dir_proc (const char *, struct dirent *, void *);
110 static int lsattr_args (const char * name)
115 if (LSTAT (name, &st) == -1) {
116 com_err (program_name, errno, _("while trying to stat %s"),
120 if (S_ISDIR(st.st_mode) && !dirs_opt)
121 retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
123 retval = list_attributes (name);
128 static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
129 void * private EXT2FS_ATTR((unused)))
133 int dir_len = strlen(dir_name);
135 path = malloc(dir_len + strlen (de->d_name) + 2);
137 if (dir_len && dir_name[dir_len-1] == '/')
138 sprintf (path, "%s%s", dir_name, de->d_name);
140 sprintf (path, "%s/%s", dir_name, de->d_name);
141 if (LSTAT (path, &st) == -1)
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);
159 int main (int argc, char ** argv)
166 setlocale(LC_MESSAGES, "");
167 setlocale(LC_CTYPE, "");
168 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
169 textdomain(NLS_CAT_NAME);
170 set_com_err_gettext(gettext);
173 program_name = *argv;
174 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
190 pf_options = PFOPT_LONG;
200 fprintf (stderr, "lsattr %s (%s)\n",
201 E2FSPROGS_VERSION, E2FSPROGS_DATE);
202 if (optind > argc - 1) {
203 if (lsattr_args (".") == -1)
206 for (i = optind; i < argc; i++) {
207 err = lsattr_args (argv[i]);