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