]> 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
17 */
18
a418d3ad 19#include <sys/types.h>
3839e657 20#include <dirent.h>
a418d3ad 21#ifdef HAVE_ERRNO_H
3839e657 22#include <errno.h>
a418d3ad 23#endif
3839e657 24#include <fcntl.h>
a418d3ad 25#ifdef HAVE_GETOPT_H
3839e657 26#include <getopt.h>
a418d3ad
TT
27#else
28extern int optind;
29extern char *optarg;
30#endif
3839e657
TT
31#include <stdio.h>
32#include <unistd.h>
a418d3ad 33#include <stdlib.h>
19c78dc0 34#include <string.h>
3839e657
TT
35#include <sys/param.h>
36#include <sys/stat.h>
37#include <linux/ext2_fs.h>
38
39#include "et/com_err.h"
40#include "e2p/e2p.h"
41
42#include "../version.h"
43
44const char * program_name = "lsattr";
45
46int all = 0;
47int d_opt = 0;
f3db3566 48int l_opt = 0;
3839e657
TT
49int recursive = 0;
50int v_opt = 0;
51
52static void volatile usage (void)
53{
f3db3566 54 fprintf (stderr, "Usage: %s [-Radlv] [files...]\n", program_name);
3839e657
TT
55 exit (1);
56}
57
58static void list_attributes (const char * name)
59{
60 unsigned long flags;
61 unsigned long version;
62
63 if (fgetflags (name, &flags) == -1)
64 com_err (program_name, errno, "While reading flags on %s",
65 name);
66 else if (fgetversion (name, &version) == -1)
67 com_err (program_name, errno, "While reading version on %s",
68 name);
69 else
70 {
71 if (v_opt)
72 printf ("%5lu ", version);
f3db3566 73 print_flags (stdout, flags, l_opt);
3839e657
TT
74 printf (" %s\n", name);
75 }
76}
77
78static int lsattr_dir_proc (const char *, struct dirent *, void *);
79
80static void lsattr_args (const char * name)
81{
82 struct stat st;
83
84 if (lstat (name, &st) == -1)
85 com_err (program_name, errno, "while stating %s", name);
86 else
87 {
88 if (S_ISDIR(st.st_mode) && !d_opt)
89 iterate_on_dir (name, lsattr_dir_proc, (void *) NULL);
90 else
91 list_attributes (name);
92 }
93}
94
95static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
96{
3839e657 97 struct stat st;
a418d3ad
TT
98 char *path;
99
100 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
3839e657
TT
101
102 sprintf (path, "%s/%s", dir_name, de->d_name);
103 if (lstat (path, &st) == -1)
104 perror (path);
a418d3ad
TT
105 else {
106 if (de->d_name[0] != '.' || all) {
3839e657
TT
107 list_attributes (path);
108 if (S_ISDIR(st.st_mode) && recursive &&
a418d3ad
TT
109 strcmp(de->d_name, ".") &&
110 strcmp(de->d_name, "..")) {
3839e657 111 printf ("\n%s:\n", path);
a418d3ad
TT
112 iterate_on_dir (path, lsattr_dir_proc,
113 (void *) NULL);
3839e657
TT
114 printf ("\n");
115 }
116 }
117 }
a418d3ad 118 free(path);
3839e657
TT
119 return 0;
120}
121
122void main (int argc, char ** argv)
123{
124 char c;
125 int i;
126
127 fprintf (stderr, "lsattr %s, %s for EXT2 FS %s, %s\n",
128 E2FSPROGS_VERSION, E2FSPROGS_DATE,
129 EXT2FS_VERSION, EXT2FS_DATE);
130 if (argc && *argv)
131 program_name = *argv;
132 while ((c = getopt (argc, argv, "Radlv")) != EOF)
133 switch (c)
134 {
135 case 'R':
136 recursive = 1;
137 break;
138 case 'a':
139 all = 1;
140 break;
141 case 'd':
142 d_opt = 1;
143 break;
f3db3566
TT
144 case 'l':
145 l_opt = 1;
146 break;
3839e657
TT
147 case 'v':
148 v_opt = 1;
149 break;
150 default:
151 usage ();
152 }
153
154 if (optind > argc - 1)
155 lsattr_args (".");
156 else
157 for (i = optind; i < argc; i++)
158 lsattr_args (argv[i]);
159}