blob: 43acaceffb1bb893a145edb8118d1cafe3d360f7 [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'oa418d3a1997-04-26 14:00:26 +000022#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <dirent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000024#ifdef HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <errno.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000026#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <fcntl.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000028#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000030#else
31extern int optind;
32extern char *optarg;
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <stdio.h>
35#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000036#include <stdlib.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000037#include <string.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000038#include <sys/param.h>
39#include <sys/stat.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000040
Theodore Ts'o54c637d2001-05-14 11:45:38 +000041#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include "et/com_err.h"
43#include "e2p/e2p.h"
44
45#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000046#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
Theodore Ts'o54434922003-12-07 01:28:50 -050048#ifdef __GNUC__
49#define EXT2FS_ATTR(x) __attribute__(x)
50#else
51#define EXT2FS_ATTR(x)
52#endif
53
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000054static const char * program_name = "lsattr";
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
Theodore Ts'of10748d2000-12-31 13:55:14 +000056static int all;
57static int dirs_opt;
58static unsigned pf_options;
59static int recursive;
60static int verbose;
61static int generation_opt;
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
Theodore Ts'ob7056402001-06-08 02:53:20 +000063#ifdef _LFS64_LARGEFILE
64#define LSTAT lstat64
65#define STRUCT_STAT struct stat64
66#else
67#define LSTAT lstat
68#define STRUCT_STAT struct stat
69#endif
70
Theodore Ts'o818180c1998-06-27 05:11:14 +000071static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000072{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000073 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
Theodore Ts'o818180c1998-06-27 05:11:14 +000074 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000075}
76
Eric Sandeen1b600bf2009-06-18 17:51:07 -050077static int list_attributes (const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +000078{
79 unsigned long flags;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000080 unsigned long generation;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081
Theodore Ts'of10748d2000-12-31 13:55:14 +000082 if (fgetflags (name, &flags) == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000083 com_err (program_name, errno, _("While reading flags on %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -050085 return -1;
Theodore Ts'of10748d2000-12-31 13:55:14 +000086 }
87 if (generation_opt) {
88 if (fgetversion (name, &generation) == -1) {
89 com_err (program_name, errno,
90 _("While reading version on %s"),
91 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -050092 return -1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000093 }
Theodore Ts'of10748d2000-12-31 13:55:14 +000094 printf ("%5lu ", generation);
95 }
96 if (pf_options & PFOPT_LONG) {
97 printf("%-28s ", name);
98 print_flags(stdout, flags, pf_options);
99 fputc('\n', stdout);
100 } else {
101 print_flags(stdout, flags, pf_options);
102 printf(" %s\n", name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 }
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500104 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105}
106
107static int lsattr_dir_proc (const char *, struct dirent *, void *);
108
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500109static int lsattr_args (const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000111 STRUCT_STAT st;
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500112 int retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500114 if (LSTAT (name, &st) == -1) {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000115 com_err (program_name, errno, _("while trying to stat %s"),
116 name);
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500117 retval = -1;
118 } else {
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000119 if (S_ISDIR(st.st_mode) && !dirs_opt)
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500120 retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 else
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500122 retval = list_attributes (name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 }
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500124 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125}
126
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400127static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500128 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000130 STRUCT_STAT st;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000131 char *path;
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400132 int dir_len = strlen(dir_name);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000133
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400134 path = malloc(dir_len + strlen (de->d_name) + 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135
Theodore Ts'o137ce8c2004-09-24 12:16:17 -0400136 if (dir_len && dir_name[dir_len-1] == '/')
137 sprintf (path, "%s%s", dir_name, de->d_name);
138 else
139 sprintf (path, "%s/%s", dir_name, de->d_name);
Theodore Ts'ob7056402001-06-08 02:53:20 +0000140 if (LSTAT (path, &st) == -1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 perror (path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000142 else {
143 if (de->d_name[0] != '.' || all) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 list_attributes (path);
145 if (S_ISDIR(st.st_mode) && recursive &&
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000146 strcmp(de->d_name, ".") &&
147 strcmp(de->d_name, "..")) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 printf ("\n%s:\n", path);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000149 iterate_on_dir (path, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 printf ("\n");
151 }
152 }
153 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000154 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 return 0;
156}
157
Theodore Ts'o00e54331997-09-16 02:13:52 +0000158int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000160 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 int i;
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500162 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000164#ifdef ENABLE_NLS
165 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500166 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000167 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
168 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -0700169 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000170#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 if (argc && *argv)
172 program_name = *argv;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000173 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 switch (c)
175 {
176 case 'R':
177 recursive = 1;
178 break;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000179 case 'V':
180 verbose = 1;
181 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 case 'a':
183 all = 1;
184 break;
185 case 'd':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000186 dirs_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000188 case 'l':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000189 pf_options = PFOPT_LONG;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000190 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191 case 'v':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000192 generation_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 break;
194 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000195 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 }
197
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000198 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400199 fprintf (stderr, "lsattr %s (%s)\n",
200 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Eric Sandeen1b600bf2009-06-18 17:51:07 -0500201 if (optind > argc - 1) {
202 if (lsattr_args (".") == -1)
203 retval = 1;
204 } else {
205 for (i = optind; i < argc; i++) {
206 err = lsattr_args (argv[i]);
207 if (err)
208 retval = 1;
209 }
210 }
211 exit(retval);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212}