blob: e5e59690f493c7f2c5b7b119ae77d1f1ec865248 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +000017 * 98/12/29 - Display version info only when -V specified (G M Sipe)
Theodore Ts'o3839e651997-04-26 13:21:57 +000018 */
19
Theodore Ts'offf18b42001-02-08 03:06:43 +000020#define _LARGEFILE64_SOURCE
Theodore Ts'offf18b42001-02-08 03:06:43 +000021
Theodore Ts'od1154eb2011-09-18 17:34:37 -040022#include "config.h"
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000023#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <dirent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000025#ifdef HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <errno.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000027#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <fcntl.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000029#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#else
32extern int optind;
33extern char *optarg;
34#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <stdio.h>
36#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#include <stdlib.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000038#include <string.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <sys/param.h>
40#include <sys/stat.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000041
Theodore Ts'o54c637d2001-05-14 11:45:38 +000042#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include "et/com_err.h"
44#include "e2p/e2p.h"
45
46#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000047#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
Theodore Ts'o54434922003-12-07 01:28:50 -050049#ifdef __GNUC__
50#define EXT2FS_ATTR(x) __attribute__(x)
51#else
52#define EXT2FS_ATTR(x)
53#endif
54
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000055static const char * program_name = "lsattr";
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'of10748d2000-12-31 13:55:14 +000057static int all;
58static int dirs_opt;
59static unsigned pf_options;
60static int recursive;
61static int verbose;
62static int generation_opt;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063
Theodore Ts'ob7056402001-06-08 02:53:20 +000064#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
Theodore Ts'o818180c1998-06-27 05:11:14 +000072static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000073{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000074 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
Theodore Ts'o818180c1998-06-27 05:11:14 +000075 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000076}
77
Eric Sandeen1b600bf2009-06-18 17:51:07 -050078static int list_attributes (const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +000079{
80 unsigned long flags;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000081 unsigned long generation;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
Theodore Ts'of10748d2000-12-31 13:55:14 +000083 if (fgetflags (name, &flags) == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000084 com_err (program_name, errno, _("While reading flags on %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -050086 return -1;
Theodore Ts'of10748d2000-12-31 13:55:14 +000087 }
88 if (generation_opt) {
89 if (fgetversion (name, &generation) == -1) {
90 com_err (program_name, errno,
91 _("While reading version on %s"),
92 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -050093 return -1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000094 }
Theodore Ts'of10748d2000-12-31 13:55:14 +000095 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);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 }
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500105 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106}
107
108static int lsattr_dir_proc (const char *, struct dirent *, void *);
109
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500110static int lsattr_args (const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000112 STRUCT_STAT st;
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500113 int retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500115 if (LSTAT (name, &st) == -1) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000116 com_err (program_name, errno, _("while trying to stat %s"),
117 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500118 retval = -1;
119 } else {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000120 if (S_ISDIR(st.st_mode) && !dirs_opt)
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500121 retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 else
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500123 retval = list_attributes (name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 }
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500125 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126}
127
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400128static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500129 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000131 STRUCT_STAT st;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000132 char *path;
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400133 int dir_len = strlen(dir_name);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000134
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400135 path = malloc(dir_len + strlen (de->d_name) + 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400137 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);
Theodore Ts'ob7056402001-06-08 02:53:20 +0000141 if (LSTAT (path, &st) == -1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 perror (path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000143 else {
144 if (de->d_name[0] != '.' || all) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145 list_attributes (path);
146 if (S_ISDIR(st.st_mode) && recursive &&
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000147 strcmp(de->d_name, ".") &&
148 strcmp(de->d_name, "..")) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149 printf ("\n%s:\n", path);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000150 iterate_on_dir (path, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 printf ("\n");
152 }
153 }
154 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000155 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 return 0;
157}
158
Theodore Ts'o00e54331997-09-16 02:13:52 +0000159int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000161 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 int i;
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500163 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000165#ifdef ENABLE_NLS
166 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500167 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000168 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
169 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -0400170 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000171#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 if (argc && *argv)
173 program_name = *argv;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000174 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 switch (c)
176 {
177 case 'R':
178 recursive = 1;
179 break;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000180 case 'V':
181 verbose = 1;
182 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 case 'a':
184 all = 1;
185 break;
186 case 'd':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000187 dirs_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000189 case 'l':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000190 pf_options = PFOPT_LONG;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000191 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 case 'v':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000193 generation_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 break;
195 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000196 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 }
198
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000199 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400200 fprintf (stderr, "lsattr %s (%s)\n",
201 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500202 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);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213}