]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/lsattr.c
ChangeLog, debugfs.c:
[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
TT
20#define _LARGEFILE64_SOURCE
21#define _FILE_OFFSET_BITS 64
22
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
46#include "../version.h"
d9c56d3c 47#include "nls-enable.h"
3839e657 48
e1a0a3e3 49static const char * program_name = "lsattr";
3839e657 50
f10748d8
TT
51static int all;
52static int dirs_opt;
53static unsigned pf_options;
54static int recursive;
55static int verbose;
56static int generation_opt;
3839e657 57
818180cd 58static void usage(void)
3839e657 59{
d9c56d3c 60 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
818180cd 61 exit(1);
3839e657
TT
62}
63
64static void list_attributes (const char * name)
65{
66 unsigned long flags;
e1a0a3e3 67 unsigned long generation;
3839e657 68
f10748d8 69 if (fgetflags (name, &flags) == -1) {
d9c56d3c 70 com_err (program_name, errno, _("While reading flags on %s"),
3839e657 71 name);
f10748d8
TT
72 return;
73 }
74 if (generation_opt) {
75 if (fgetversion (name, &generation) == -1) {
76 com_err (program_name, errno,
77 _("While reading version on %s"),
78 name);
79 return;
e1a0a3e3 80 }
f10748d8
TT
81 printf ("%5lu ", generation);
82 }
83 if (pf_options & PFOPT_LONG) {
84 printf("%-28s ", name);
85 print_flags(stdout, flags, pf_options);
86 fputc('\n', stdout);
87 } else {
88 print_flags(stdout, flags, pf_options);
89 printf(" %s\n", name);
3839e657
TT
90 }
91}
92
93static int lsattr_dir_proc (const char *, struct dirent *, void *);
94
95static void lsattr_args (const char * name)
96{
97 struct stat st;
98
99 if (lstat (name, &st) == -1)
e1a0a3e3
TT
100 com_err (program_name, errno, _("while trying to stat %s"),
101 name);
102 else {
103 if (S_ISDIR(st.st_mode) && !dirs_opt)
104 iterate_on_dir (name, lsattr_dir_proc, NULL);
3839e657
TT
105 else
106 list_attributes (name);
107 }
108}
109
110static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
111{
3839e657 112 struct stat st;
a418d3ad
TT
113 char *path;
114
115 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
3839e657
TT
116
117 sprintf (path, "%s/%s", dir_name, de->d_name);
118 if (lstat (path, &st) == -1)
119 perror (path);
a418d3ad
TT
120 else {
121 if (de->d_name[0] != '.' || all) {
3839e657
TT
122 list_attributes (path);
123 if (S_ISDIR(st.st_mode) && recursive &&
a418d3ad
TT
124 strcmp(de->d_name, ".") &&
125 strcmp(de->d_name, "..")) {
3839e657 126 printf ("\n%s:\n", path);
e1a0a3e3 127 iterate_on_dir (path, lsattr_dir_proc, NULL);
3839e657
TT
128 printf ("\n");
129 }
130 }
131 }
a418d3ad 132 free(path);
3839e657
TT
133 return 0;
134}
135
00e5433e 136int main (int argc, char ** argv)
3839e657 137{
519149fb 138 int c;
3839e657
TT
139 int i;
140
d9c56d3c
TT
141#ifdef ENABLE_NLS
142 setlocale(LC_MESSAGES, "");
143 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
144 textdomain(NLS_CAT_NAME);
145#endif
3839e657
TT
146 if (argc && *argv)
147 program_name = *argv;
a88fa0c0 148 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
3839e657
TT
149 switch (c)
150 {
151 case 'R':
152 recursive = 1;
153 break;
a88fa0c0
TT
154 case 'V':
155 verbose = 1;
156 break;
3839e657
TT
157 case 'a':
158 all = 1;
159 break;
160 case 'd':
e1a0a3e3 161 dirs_opt = 1;
3839e657 162 break;
f3db3566 163 case 'l':
e1a0a3e3 164 pf_options = PFOPT_LONG;
f3db3566 165 break;
3839e657 166 case 'v':
e1a0a3e3 167 generation_opt = 1;
3839e657
TT
168 break;
169 default:
818180cd 170 usage();
3839e657
TT
171 }
172
a88fa0c0 173 if (verbose)
d9c56d3c 174 fprintf (stderr, _("lsattr %s, %s for EXT2 FS %s, %s\n"),
a88fa0c0
TT
175 E2FSPROGS_VERSION, E2FSPROGS_DATE,
176 EXT2FS_VERSION, EXT2FS_DATE);
3839e657
TT
177 if (optind > argc - 1)
178 lsattr_args (".");
179 else
180 for (i = optind; i < argc; i++)
181 lsattr_args (argv[i]);
00e5433e 182 exit(0);
3839e657 183}