]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/lsattr.c
AOSP: build static version of mke2fs and e2fsdroid for host
[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
d1154eb4 22#include "config.h"
a418d3ad 23#include <sys/types.h>
3839e657 24#include <dirent.h>
a418d3ad 25#ifdef HAVE_ERRNO_H
3839e657 26#include <errno.h>
a418d3ad 27#endif
3839e657 28#include <fcntl.h>
a418d3ad 29#ifdef HAVE_GETOPT_H
3839e657 30#include <getopt.h>
a418d3ad
TT
31#else
32extern int optind;
33extern char *optarg;
34#endif
3839e657
TT
35#include <stdio.h>
36#include <unistd.h>
a418d3ad 37#include <stdlib.h>
19c78dc0 38#include <string.h>
3839e657
TT
39#include <sys/param.h>
40#include <sys/stat.h>
3839e657 41
54c637d4 42#include "ext2fs/ext2_fs.h"
3839e657
TT
43#include "et/com_err.h"
44#include "e2p/e2p.h"
45
99ceb8ec 46#include "support/nls-enable.h"
3839e657
TT
47#include "../version.h"
48
54434927
TT
49#ifdef __GNUC__
50#define EXT2FS_ATTR(x) __attribute__(x)
51#else
52#define EXT2FS_ATTR(x)
53#endif
54
e1a0a3e3 55static const char * program_name = "lsattr";
3839e657 56
f10748d8
TT
57static int all;
58static int dirs_opt;
59static unsigned pf_options;
60static int recursive;
61static int verbose;
62static int generation_opt;
3627e838 63static int project_opt;
3839e657 64
b705640a
TT
65#ifdef _LFS64_LARGEFILE
66#define LSTAT lstat64
67#define STRUCT_STAT struct stat64
68#else
69#define LSTAT lstat
70#define STRUCT_STAT struct stat
71#endif
72
818180cd 73static void usage(void)
3839e657 74{
3627e838 75 fprintf(stderr, _("Usage: %s [-RVadlpv] [files...]\n"), program_name);
818180cd 76 exit(1);
3839e657
TT
77}
78
1b600bfa 79static int list_attributes (const char * name)
3839e657
TT
80{
81 unsigned long flags;
e1a0a3e3 82 unsigned long generation;
3627e838 83 unsigned long project;
3839e657 84
f10748d8 85 if (fgetflags (name, &flags) == -1) {
d9c56d3c 86 com_err (program_name, errno, _("While reading flags on %s"),
3839e657 87 name);
1b600bfa 88 return -1;
f10748d8 89 }
3627e838
LX
90 if (project_opt) {
91 if (fgetproject(name, &project) == -1) {
92 com_err (program_name, errno,
93 _("While reading project on %s"),
94 name);
95 return -1;
96 }
97 printf ("%5lu ", project);
98 }
f10748d8
TT
99 if (generation_opt) {
100 if (fgetversion (name, &generation) == -1) {
101 com_err (program_name, errno,
102 _("While reading version on %s"),
103 name);
1b600bfa 104 return -1;
e1a0a3e3 105 }
1e3f5c88 106 printf ("%-10lu ", generation);
f10748d8
TT
107 }
108 if (pf_options & PFOPT_LONG) {
109 printf("%-28s ", name);
110 print_flags(stdout, flags, pf_options);
111 fputc('\n', stdout);
112 } else {
113 print_flags(stdout, flags, pf_options);
114 printf(" %s\n", name);
3839e657 115 }
1b600bfa 116 return 0;
3839e657
TT
117}
118
119static int lsattr_dir_proc (const char *, struct dirent *, void *);
120
1b600bfa 121static int lsattr_args (const char * name)
3839e657 122{
b705640a 123 STRUCT_STAT st;
1b600bfa 124 int retval = 0;
3839e657 125
1b600bfa 126 if (LSTAT (name, &st) == -1) {
e1a0a3e3
TT
127 com_err (program_name, errno, _("while trying to stat %s"),
128 name);
1b600bfa
ES
129 retval = -1;
130 } else {
e1a0a3e3 131 if (S_ISDIR(st.st_mode) && !dirs_opt)
1b600bfa 132 retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
3839e657 133 else
1b600bfa 134 retval = list_attributes (name);
3839e657 135 }
1b600bfa 136 return retval;
3839e657
TT
137}
138
efc6f628 139static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
54434927 140 void * private EXT2FS_ATTR((unused)))
3839e657 141{
b705640a 142 STRUCT_STAT st;
a418d3ad 143 char *path;
137ce8ca 144 int dir_len = strlen(dir_name);
a418d3ad 145
137ce8ca 146 path = malloc(dir_len + strlen (de->d_name) + 2);
3839e657 147
137ce8ca
TT
148 if (dir_len && dir_name[dir_len-1] == '/')
149 sprintf (path, "%s%s", dir_name, de->d_name);
150 else
151 sprintf (path, "%s/%s", dir_name, de->d_name);
b705640a 152 if (LSTAT (path, &st) == -1)
3839e657 153 perror (path);
a418d3ad
TT
154 else {
155 if (de->d_name[0] != '.' || all) {
3839e657
TT
156 list_attributes (path);
157 if (S_ISDIR(st.st_mode) && recursive &&
a418d3ad
TT
158 strcmp(de->d_name, ".") &&
159 strcmp(de->d_name, "..")) {
3839e657 160 printf ("\n%s:\n", path);
e1a0a3e3 161 iterate_on_dir (path, lsattr_dir_proc, NULL);
3839e657
TT
162 printf ("\n");
163 }
164 }
165 }
a418d3ad 166 free(path);
3839e657
TT
167 return 0;
168}
169
00e5433e 170int main (int argc, char ** argv)
3839e657 171{
519149fb 172 int c;
3839e657 173 int i;
1b600bfa 174 int err, retval = 0;
3839e657 175
d9c56d3c
TT
176#ifdef ENABLE_NLS
177 setlocale(LC_MESSAGES, "");
14308a53 178 setlocale(LC_CTYPE, "");
d9c56d3c
TT
179 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
180 textdomain(NLS_CAT_NAME);
9d4507c5 181 set_com_err_gettext(gettext);
d9c56d3c 182#endif
3839e657
TT
183 if (argc && *argv)
184 program_name = *argv;
3627e838 185 while ((c = getopt (argc, argv, "RVadlvp")) != EOF)
3839e657
TT
186 switch (c)
187 {
188 case 'R':
189 recursive = 1;
190 break;
a88fa0c0
TT
191 case 'V':
192 verbose = 1;
193 break;
3839e657
TT
194 case 'a':
195 all = 1;
196 break;
197 case 'd':
e1a0a3e3 198 dirs_opt = 1;
3839e657 199 break;
f3db3566 200 case 'l':
e1a0a3e3 201 pf_options = PFOPT_LONG;
f3db3566 202 break;
3839e657 203 case 'v':
e1a0a3e3 204 generation_opt = 1;
3839e657 205 break;
3627e838
LX
206 case 'p':
207 project_opt = 1;
208 break;
3839e657 209 default:
818180cd 210 usage();
3839e657
TT
211 }
212
a88fa0c0 213 if (verbose)
0f8973fb
TT
214 fprintf (stderr, "lsattr %s (%s)\n",
215 E2FSPROGS_VERSION, E2FSPROGS_DATE);
1b600bfa
ES
216 if (optind > argc - 1) {
217 if (lsattr_args (".") == -1)
218 retval = 1;
219 } else {
220 for (i = optind; i < argc; i++) {
221 err = lsattr_args (argv[i]);
222 if (err)
223 retval = 1;
224 }
225 }
226 exit(retval);
3839e657 227}