blob: 475a66f670d2c505158d84b4cd41069854536924 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +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
17 * 98/12/29 - Display version info only when -V specified (G M Sipe)
18 */
19
20#include <sys/types.h>
21#include <dirent.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/param.h>
30#include <sys/stat.h>
31
32#include <ext2fs/ext2_fs.h>
33#include "e2fsbb.h"
34#include "e2p/e2p.h"
35
36#ifdef __GNUC__
Mike Frysingerea338ff2005-05-07 07:17:43 +000037# define EXT2FS_ATTR(x) __attribute__(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000038#else
Mike Frysingerea338ff2005-05-07 07:17:43 +000039# define EXT2FS_ATTR(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000040#endif
41
Mike Frysingerd2a64d22005-04-25 04:10:35 +000042#define OPT_RECUR 1
43#define OPT_ALL 2
44#define OPT_DIRS_OPT 4
45#define OPT_PF_LONG 8
46#define OPT_GENERATION 16
47static int flags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000048
Mike Frysingerd2a64d22005-04-25 04:10:35 +000049#ifdef CONFIG_LFS
50# define LSTAT lstat64
51# define STRUCT_STAT struct stat64
Mike Frysingerd89e6292005-04-24 05:07:59 +000052#else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000053# define LSTAT lstat
54# define STRUCT_STAT struct stat
Mike Frysingerd89e6292005-04-24 05:07:59 +000055#endif
56
Mike Frysingerd2a64d22005-04-25 04:10:35 +000057static void list_attributes(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000058{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000059 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000060 unsigned long generation;
61
Mike Frysingerea338ff2005-05-07 07:17:43 +000062 if (fgetflags(name, &fsflags) == -1)
63 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000064 if (flags & OPT_GENERATION) {
Mike Frysingerea338ff2005-05-07 07:17:43 +000065 if (fgetversion(name, &generation) == -1)
66 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000067 printf("%5lu ", generation);
Mike Frysingerd89e6292005-04-24 05:07:59 +000068 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000069
Mike Frysingerd2a64d22005-04-25 04:10:35 +000070 if (flags & OPT_PF_LONG) {
Mike Frysingerd89e6292005-04-24 05:07:59 +000071 printf("%-28s ", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +000072 print_flags(stdout, fsflags, PFOPT_LONG);
Mike Frysingerea338ff2005-05-07 07:17:43 +000073 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +000074 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000075 print_flags(stdout, fsflags, 0);
Mike Frysingerd89e6292005-04-24 05:07:59 +000076 printf(" %s\n", name);
77 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000078
79 return;
80read_err:
81 bb_perror_msg("reading %s", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000082}
83
Mike Frysingerd2a64d22005-04-25 04:10:35 +000084static int lsattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +000085
Mike Frysingerd2a64d22005-04-25 04:10:35 +000086static void lsattr_args(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000087{
88 STRUCT_STAT st;
89
Mike Frysingerea338ff2005-05-07 07:17:43 +000090 if (LSTAT(name, &st) == -1) {
91 bb_perror_msg("stating %s", name);
92 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000093 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
94 iterate_on_dir(name, lsattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +000095 else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000096 list_attributes(name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000097 }
98}
99
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000100static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
101 void *private EXT2FS_ATTR((unused)))
Mike Frysingerd89e6292005-04-24 05:07:59 +0000102{
103 STRUCT_STAT st;
104 char *path;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000105
Mike Frysingerea338ff2005-05-07 07:17:43 +0000106 path = concat_path_file(dir_name, de->d_name);
Mike Frysingerc238a972005-04-25 05:24:35 +0000107
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000108 if (LSTAT(path, &st) == -1)
109 bb_perror_msg(path);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000110 else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000111 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
112 list_attributes(path);
113 if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
Mike Frysingerea338ff2005-05-07 07:17:43 +0000114 (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
115 (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000116 printf("\n%s:\n", path);
117 iterate_on_dir(path, lsattr_dir_proc, NULL);
118 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000119 }
120 }
121 }
Mike Frysingerc238a972005-04-25 05:24:35 +0000122
Mike Frysingerd89e6292005-04-24 05:07:59 +0000123 free(path);
Mike Frysingerc238a972005-04-25 05:24:35 +0000124
Mike Frysingerd89e6292005-04-24 05:07:59 +0000125 return 0;
126}
127
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000128int lsattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000129{
Mike Frysingerd89e6292005-04-24 05:07:59 +0000130 int i;
131
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000132 flags = bb_getopt_ulflags(argc, argv, "Radlv");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000133
Mike Frysingerd89e6292005-04-24 05:07:59 +0000134 if (optind > argc - 1)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000135 lsattr_args(".");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000136 else
137 for (i = optind; i < argc; i++)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000138 lsattr_args(argv[i]);
139
140 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000141}