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 "support/nls-enable.h"
47 #include "../version.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;
63 static int project_opt;
65 #ifdef _LFS64_LARGEFILE
67 #define STRUCT_STAT struct stat64
70 #define STRUCT_STAT struct stat
73 static void usage(void)
75 fprintf(stderr, _("Usage: %s [-RVadlpv] [files...]\n"), program_name);
79 static int list_attributes (const char * name)
82 unsigned long generation;
83 unsigned long project;
85 if (fgetflags (name, &flags) == -1) {
86 com_err (program_name, errno, _("While reading flags on %s"),
91 if (fgetproject(name, &project) == -1) {
92 com_err (program_name, errno,
93 _("While reading project on %s"),
97 printf ("%5lu ", project);
100 if (fgetversion (name, &generation) == -1) {
101 com_err (program_name, errno,
102 _("While reading version on %s"),
106 printf ("%-10lu ", generation);
108 if (pf_options & PFOPT_LONG) {
109 printf("%-28s ", name);
110 print_flags(stdout, flags, pf_options);
113 print_flags(stdout, flags, pf_options);
114 printf(" %s\n", name);
119 static int lsattr_dir_proc (const char *, struct dirent *, void *);
121 static int lsattr_args (const char * name)
126 if (LSTAT (name, &st) == -1) {
127 com_err (program_name, errno, _("while trying to stat %s"),
131 if (S_ISDIR(st.st_mode) && !dirs_opt)
132 retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
134 retval = list_attributes (name);
139 static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
140 void * private EXT2FS_ATTR((unused)))
144 int dir_len = strlen(dir_name);
146 path = malloc(dir_len + strlen (de->d_name) + 2);
148 if (dir_len && dir_name[dir_len-1] == '/')
149 sprintf (path, "%s%s", dir_name, de->d_name);
151 sprintf (path, "%s/%s", dir_name, de->d_name);
152 if (LSTAT (path, &st) == -1)
155 if (de->d_name[0] != '.' || all) {
156 list_attributes (path);
157 if (S_ISDIR(st.st_mode) && recursive &&
158 strcmp(de->d_name, ".") &&
159 strcmp(de->d_name, "..")) {
160 printf ("\n%s:\n", path);
161 iterate_on_dir (path, lsattr_dir_proc, NULL);
170 int main (int argc, char ** argv)
177 setlocale(LC_MESSAGES, "");
178 setlocale(LC_CTYPE, "");
179 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
180 textdomain(NLS_CAT_NAME);
181 set_com_err_gettext(gettext);
184 program_name = *argv;
185 while ((c = getopt (argc, argv, "RVadlvp")) != EOF)
201 pf_options = PFOPT_LONG;
214 fprintf (stderr, "lsattr %s (%s)\n",
215 E2FSPROGS_VERSION, E2FSPROGS_DATE);
216 if (optind > argc - 1) {
217 if (lsattr_args (".") == -1)
220 for (i = optind; i < argc; i++) {
221 err = lsattr_args (argv[i]);