blob: 5c20d25f381510f58fcfadcee90160d56e50b051 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
3 * lsattr.c - List file attributes on an ext2 file system
4 *
5 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * This file can be redistributed under the terms of the GNU General
10 * Public License
11 */
12
13/*
14 * History:
15 * 93/10/30 - Creation
16 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
17 * 94/02/27 - Integrated in Ted's distribution
18 * 98/12/29 - Display version info only when -V specified (G M Sipe)
19 */
20
21#include <sys/types.h>
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <getopt.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/param.h>
31#include <sys/stat.h>
32
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000033#include "ext2fs/ext2_fs.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +000034#include "e2fsbb.h"
35#include "e2p/e2p.h"
36
Mike Frysingerd2a64d22005-04-25 04:10:35 +000037#define OPT_RECUR 1
38#define OPT_ALL 2
39#define OPT_DIRS_OPT 4
40#define OPT_PF_LONG 8
41#define OPT_GENERATION 16
42static int flags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000043
Mike Frysingerd2a64d22005-04-25 04:10:35 +000044#ifdef CONFIG_LFS
45# define LSTAT lstat64
46# define STRUCT_STAT struct stat64
Mike Frysingerd89e6292005-04-24 05:07:59 +000047#else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000048# define LSTAT lstat
49# define STRUCT_STAT struct stat
Mike Frysingerd89e6292005-04-24 05:07:59 +000050#endif
51
Mike Frysingerd2a64d22005-04-25 04:10:35 +000052static void list_attributes(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000053{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000054 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000055 unsigned long generation;
56
Mike Frysingerea338ff2005-05-07 07:17:43 +000057 if (fgetflags(name, &fsflags) == -1)
58 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000059 if (flags & OPT_GENERATION) {
Mike Frysingerea338ff2005-05-07 07:17:43 +000060 if (fgetversion(name, &generation) == -1)
61 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000062 printf("%5lu ", generation);
Mike Frysingerd89e6292005-04-24 05:07:59 +000063 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000064
Mike Frysingerd2a64d22005-04-25 04:10:35 +000065 if (flags & OPT_PF_LONG) {
Mike Frysingerd89e6292005-04-24 05:07:59 +000066 printf("%-28s ", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +000067 print_flags(stdout, fsflags, PFOPT_LONG);
Mike Frysingerea338ff2005-05-07 07:17:43 +000068 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +000069 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000070 print_flags(stdout, fsflags, 0);
Mike Frysingerd89e6292005-04-24 05:07:59 +000071 printf(" %s\n", name);
72 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000073
74 return;
75read_err:
76 bb_perror_msg("reading %s", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000077}
78
Mike Frysingerd2a64d22005-04-25 04:10:35 +000079static int lsattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +000080
Mike Frysingerd2a64d22005-04-25 04:10:35 +000081static void lsattr_args(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000082{
83 STRUCT_STAT st;
84
Mike Frysingerea338ff2005-05-07 07:17:43 +000085 if (LSTAT(name, &st) == -1) {
86 bb_perror_msg("stating %s", name);
87 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000088 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
89 iterate_on_dir(name, lsattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +000090 else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000091 list_attributes(name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000092 }
93}
94
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
Rob Landley7c94bed2006-05-03 21:58:45 +000096 void *private)
Mike Frysingerd89e6292005-04-24 05:07:59 +000097{
98 STRUCT_STAT st;
99 char *path;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000100
Mike Frysingerea338ff2005-05-07 07:17:43 +0000101 path = concat_path_file(dir_name, de->d_name);
Mike Frysingerc238a972005-04-25 05:24:35 +0000102
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000103 if (LSTAT(path, &st) == -1)
104 bb_perror_msg(path);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000105 else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000106 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
107 list_attributes(path);
108 if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
Mike Frysingerea338ff2005-05-07 07:17:43 +0000109 (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
110 (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000111 printf("\n%s:\n", path);
112 iterate_on_dir(path, lsattr_dir_proc, NULL);
113 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000114 }
115 }
116 }
Mike Frysingerc238a972005-04-25 05:24:35 +0000117
Mike Frysingerd89e6292005-04-24 05:07:59 +0000118 free(path);
Mike Frysingerc238a972005-04-25 05:24:35 +0000119
Mike Frysingerd89e6292005-04-24 05:07:59 +0000120 return 0;
121}
122
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000123int lsattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000124{
Mike Frysingerd89e6292005-04-24 05:07:59 +0000125 int i;
126
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000127 flags = bb_getopt_ulflags(argc, argv, "Radlv");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000128
Mike Frysingerd89e6292005-04-24 05:07:59 +0000129 if (optind > argc - 1)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000130 lsattr_args(".");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000131 else
132 for (i = optind; i < argc; i++)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000133 lsattr_args(argv[i]);
134
135 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000136}