]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/lsattr.c
Two bug fixes to debugfs, courtesy of Jaroslav Drzik <jdrzik@host.sk> for suggesting...
[thirdparty/e2fsprogs.git] / misc / lsattr.c
CommitLineData
3839e657
TT
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
a88fa0c0 17 * 98/12/29 - Display version info only when -V specified (G M Sipe)
3839e657
TT
18 */
19
fff18b4e 20#define _LARGEFILE64_SOURCE
fff18b4e 21
a418d3ad 22#include <sys/types.h>
3839e657 23#include <dirent.h>
a418d3ad 24#ifdef HAVE_ERRNO_H
3839e657 25#include <errno.h>
a418d3ad 26#endif
3839e657 27#include <fcntl.h>
a418d3ad 28#ifdef HAVE_GETOPT_H
3839e657 29#include <getopt.h>
a418d3ad
TT
30#else
31extern int optind;
32extern char *optarg;
33#endif
3839e657
TT
34#include <stdio.h>
35#include <unistd.h>
a418d3ad 36#include <stdlib.h>
19c78dc0 37#include <string.h>
3839e657
TT
38#include <sys/param.h>
39#include <sys/stat.h>
3839e657 40
54c637d4 41#include "ext2fs/ext2_fs.h"
3839e657
TT
42#include "et/com_err.h"
43#include "e2p/e2p.h"
44
45#include "../version.h"
d9c56d3c 46#include "nls-enable.h"
3839e657 47
e1a0a3e3 48static const char * program_name = "lsattr";
3839e657 49
f10748d8
TT
50static int all;
51static int dirs_opt;
52static unsigned pf_options;
53static int recursive;
54static int verbose;
55static int generation_opt;
3839e657 56
b705640a
TT
57#ifdef _LFS64_LARGEFILE
58#define LSTAT lstat64
59#define STRUCT_STAT struct stat64
60#else
61#define LSTAT lstat
62#define STRUCT_STAT struct stat
63#endif
64
818180cd 65static void usage(void)
3839e657 66{
d9c56d3c 67 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
818180cd 68 exit(1);
3839e657
TT
69}
70
71static void list_attributes (const char * name)
72{
73 unsigned long flags;
e1a0a3e3 74 unsigned long generation;
3839e657 75
f10748d8 76 if (fgetflags (name, &flags) == -1) {
d9c56d3c 77 com_err (program_name, errno, _("While reading flags on %s"),
3839e657 78 name);
f10748d8
TT
79 return;
80 }
81 if (generation_opt) {
82 if (fgetversion (name, &generation) == -1) {
83 com_err (program_name, errno,
84 _("While reading version on %s"),
85 name);
86 return;
e1a0a3e3 87 }
f10748d8
TT
88 printf ("%5lu ", generation);
89 }
90 if (pf_options & PFOPT_LONG) {
91 printf("%-28s ", name);
92 print_flags(stdout, flags, pf_options);
93 fputc('\n', stdout);
94 } else {
95 print_flags(stdout, flags, pf_options);
96 printf(" %s\n", name);
3839e657
TT
97 }
98}
99
100static int lsattr_dir_proc (const char *, struct dirent *, void *);
101
102static void lsattr_args (const char * name)
103{
b705640a 104 STRUCT_STAT st;
3839e657 105
b705640a 106 if (LSTAT (name, &st) == -1)
e1a0a3e3
TT
107 com_err (program_name, errno, _("while trying to stat %s"),
108 name);
109 else {
110 if (S_ISDIR(st.st_mode) && !dirs_opt)
111 iterate_on_dir (name, lsattr_dir_proc, NULL);
3839e657
TT
112 else
113 list_attributes (name);
114 }
115}
116
117static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
118{
b705640a 119 STRUCT_STAT st;
a418d3ad
TT
120 char *path;
121
122 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
3839e657
TT
123
124 sprintf (path, "%s/%s", dir_name, de->d_name);
b705640a 125 if (LSTAT (path, &st) == -1)
3839e657 126 perror (path);
a418d3ad
TT
127 else {
128 if (de->d_name[0] != '.' || all) {
3839e657
TT
129 list_attributes (path);
130 if (S_ISDIR(st.st_mode) && recursive &&
a418d3ad
TT
131 strcmp(de->d_name, ".") &&
132 strcmp(de->d_name, "..")) {
3839e657 133 printf ("\n%s:\n", path);
e1a0a3e3 134 iterate_on_dir (path, lsattr_dir_proc, NULL);
3839e657
TT
135 printf ("\n");
136 }
137 }
138 }
a418d3ad 139 free(path);
3839e657
TT
140 return 0;
141}
142
00e5433e 143int main (int argc, char ** argv)
3839e657 144{
519149fb 145 int c;
3839e657
TT
146 int i;
147
d9c56d3c
TT
148#ifdef ENABLE_NLS
149 setlocale(LC_MESSAGES, "");
150 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
151 textdomain(NLS_CAT_NAME);
152#endif
3839e657
TT
153 if (argc && *argv)
154 program_name = *argv;
a88fa0c0 155 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
3839e657
TT
156 switch (c)
157 {
158 case 'R':
159 recursive = 1;
160 break;
a88fa0c0
TT
161 case 'V':
162 verbose = 1;
163 break;
3839e657
TT
164 case 'a':
165 all = 1;
166 break;
167 case 'd':
e1a0a3e3 168 dirs_opt = 1;
3839e657 169 break;
f3db3566 170 case 'l':
e1a0a3e3 171 pf_options = PFOPT_LONG;
f3db3566 172 break;
3839e657 173 case 'v':
e1a0a3e3 174 generation_opt = 1;
3839e657
TT
175 break;
176 default:
818180cd 177 usage();
3839e657
TT
178 }
179
a88fa0c0 180 if (verbose)
0f8973fb
TT
181 fprintf (stderr, "lsattr %s (%s)\n",
182 E2FSPROGS_VERSION, E2FSPROGS_DATE);
3839e657
TT
183 if (optind > argc - 1)
184 lsattr_args (".");
185 else
186 for (i = optind; i < argc; i++)
187 lsattr_args (argv[i]);
00e5433e 188 exit(0);
3839e657 189}