blob: 513b41a4e47f2c718ab69ec462470f9313a84160 [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'oe1a0a3e2000-02-11 05:00:19 +000048static const char * program_name = "lsattr";
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
Theodore Ts'of10748d2000-12-31 13:55:14 +000050static int all;
51static int dirs_opt;
52static unsigned pf_options;
53static int recursive;
54static int verbose;
55static int generation_opt;
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'ob7056402001-06-08 02:53:20 +000057#ifdef _LFS64_LARGEFILE
58#define LSTAT lstat64
59#define STRUCT_STAT struct stat64
60#else
61#define LSTAT lstat
62#define STRUCT_STAT struct stat
63#endif
64
Theodore Ts'o818180c1998-06-27 05:11:14 +000065static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000066{
Theodore Ts'od9c56d32000-02-08 00:47:55 +000067 fprintf(stderr, _("Usage: %s [-RVadlv] [files...]\n"), program_name);
Theodore Ts'o818180c1998-06-27 05:11:14 +000068 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069}
70
71static void list_attributes (const char * name)
72{
73 unsigned long flags;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000074 unsigned long generation;
Theodore Ts'o3839e651997-04-26 13:21:57 +000075
Theodore Ts'of10748d2000-12-31 13:55:14 +000076 if (fgetflags (name, &flags) == -1) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +000077 com_err (program_name, errno, _("While reading flags on %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +000078 name);
Theodore Ts'of10748d2000-12-31 13:55:14 +000079 return;
80 }
81 if (generation_opt) {
82 if (fgetversion (name, &generation) == -1) {
83 com_err (program_name, errno,
84 _("While reading version on %s"),
85 name);
86 return;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000087 }
Theodore Ts'of10748d2000-12-31 13:55:14 +000088 printf ("%5lu ", generation);
89 }
90 if (pf_options & PFOPT_LONG) {
91 printf("%-28s ", name);
92 print_flags(stdout, flags, pf_options);
93 fputc('\n', stdout);
94 } else {
95 print_flags(stdout, flags, pf_options);
96 printf(" %s\n", name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 }
98}
99
100static int lsattr_dir_proc (const char *, struct dirent *, void *);
101
102static void lsattr_args (const char * name)
103{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000104 STRUCT_STAT st;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000105
Theodore Ts'ob7056402001-06-08 02:53:20 +0000106 if (LSTAT (name, &st) == -1)
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000107 com_err (program_name, errno, _("while trying to stat %s"),
108 name);
109 else {
110 if (S_ISDIR(st.st_mode) && !dirs_opt)
111 iterate_on_dir (name, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 else
113 list_attributes (name);
114 }
115}
116
117static int lsattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
118{
Theodore Ts'ob7056402001-06-08 02:53:20 +0000119 STRUCT_STAT st;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000120 char *path;
121
122 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123
124 sprintf (path, "%s/%s", dir_name, de->d_name);
Theodore Ts'ob7056402001-06-08 02:53:20 +0000125 if (LSTAT (path, &st) == -1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 perror (path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000127 else {
128 if (de->d_name[0] != '.' || all) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129 list_attributes (path);
130 if (S_ISDIR(st.st_mode) && recursive &&
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000131 strcmp(de->d_name, ".") &&
132 strcmp(de->d_name, "..")) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133 printf ("\n%s:\n", path);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000134 iterate_on_dir (path, lsattr_dir_proc, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135 printf ("\n");
136 }
137 }
138 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000139 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 return 0;
141}
142
Theodore Ts'o00e54331997-09-16 02:13:52 +0000143int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000145 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 int i;
147
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000148#ifdef ENABLE_NLS
149 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500150 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000151 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
152 textdomain(NLS_CAT_NAME);
153#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 if (argc && *argv)
155 program_name = *argv;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000156 while ((c = getopt (argc, argv, "RVadlv")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 switch (c)
158 {
159 case 'R':
160 recursive = 1;
161 break;
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000162 case 'V':
163 verbose = 1;
164 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 case 'a':
166 all = 1;
167 break;
168 case 'd':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000169 dirs_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000171 case 'l':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000172 pf_options = PFOPT_LONG;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000173 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 case 'v':
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000175 generation_opt = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176 break;
177 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000178 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 }
180
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000181 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400182 fprintf (stderr, "lsattr %s (%s)\n",
183 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 if (optind > argc - 1)
185 lsattr_args (".");
186 else
187 for (i = optind; i < argc; i++)
188 lsattr_args (argv[i]);
Theodore Ts'o00e54331997-09-16 02:13:52 +0000189 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190}