blob: 1f2176600696b29c115a88186494af8c93ccb770 [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
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000032#include "ext2fs/ext2_fs.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +000033#include "e2fsbb.h"
34#include "e2p/e2p.h"
35
Mike Frysingerd2a64d22005-04-25 04:10:35 +000036#define OPT_RECUR 1
37#define OPT_ALL 2
38#define OPT_DIRS_OPT 4
39#define OPT_PF_LONG 8
40#define OPT_GENERATION 16
41static int flags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000042
Mike Frysingerd2a64d22005-04-25 04:10:35 +000043#ifdef CONFIG_LFS
44# define LSTAT lstat64
45# define STRUCT_STAT struct stat64
Mike Frysingerd89e6292005-04-24 05:07:59 +000046#else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000047# define LSTAT lstat
48# define STRUCT_STAT struct stat
Mike Frysingerd89e6292005-04-24 05:07:59 +000049#endif
50
Mike Frysingerd2a64d22005-04-25 04:10:35 +000051static void list_attributes(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000052{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000053 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +000054 unsigned long generation;
55
Mike Frysingerea338ff2005-05-07 07:17:43 +000056 if (fgetflags(name, &fsflags) == -1)
57 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000058 if (flags & OPT_GENERATION) {
Mike Frysingerea338ff2005-05-07 07:17:43 +000059 if (fgetversion(name, &generation) == -1)
60 goto read_err;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000061 printf("%5lu ", generation);
Mike Frysingerd89e6292005-04-24 05:07:59 +000062 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000063
Mike Frysingerd2a64d22005-04-25 04:10:35 +000064 if (flags & OPT_PF_LONG) {
Mike Frysingerd89e6292005-04-24 05:07:59 +000065 printf("%-28s ", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +000066 print_flags(stdout, fsflags, PFOPT_LONG);
Mike Frysingerea338ff2005-05-07 07:17:43 +000067 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +000068 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000069 print_flags(stdout, fsflags, 0);
Mike Frysingerd89e6292005-04-24 05:07:59 +000070 printf(" %s\n", name);
71 }
Mike Frysingerea338ff2005-05-07 07:17:43 +000072
73 return;
74read_err:
75 bb_perror_msg("reading %s", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000076}
77
Mike Frysingerd2a64d22005-04-25 04:10:35 +000078static int lsattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +000079
Mike Frysingerd2a64d22005-04-25 04:10:35 +000080static void lsattr_args(const char *name)
Mike Frysingerd89e6292005-04-24 05:07:59 +000081{
82 STRUCT_STAT st;
83
Mike Frysingerea338ff2005-05-07 07:17:43 +000084 if (LSTAT(name, &st) == -1) {
85 bb_perror_msg("stating %s", name);
86 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000087 if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
88 iterate_on_dir(name, lsattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +000089 else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000090 list_attributes(name);
Mike Frysingerd89e6292005-04-24 05:07:59 +000091 }
92}
93
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000094static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
Rob Landley7c94bed2006-05-03 21:58:45 +000095 void *private)
Mike Frysingerd89e6292005-04-24 05:07:59 +000096{
97 STRUCT_STAT st;
98 char *path;
Mike Frysingerd89e6292005-04-24 05:07:59 +000099
Mike Frysingerea338ff2005-05-07 07:17:43 +0000100 path = concat_path_file(dir_name, de->d_name);
Mike Frysingerc238a972005-04-25 05:24:35 +0000101
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000102 if (LSTAT(path, &st) == -1)
103 bb_perror_msg(path);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000104 else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000105 if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
106 list_attributes(path);
107 if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
Mike Frysingerea338ff2005-05-07 07:17:43 +0000108 (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
109 (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000110 printf("\n%s:\n", path);
111 iterate_on_dir(path, lsattr_dir_proc, NULL);
112 printf("\n");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000113 }
114 }
115 }
Mike Frysingerc238a972005-04-25 05:24:35 +0000116
Mike Frysingerd89e6292005-04-24 05:07:59 +0000117 free(path);
Mike Frysingerc238a972005-04-25 05:24:35 +0000118
Mike Frysingerd89e6292005-04-24 05:07:59 +0000119 return 0;
120}
121
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000122int lsattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000123{
Mike Frysingerd89e6292005-04-24 05:07:59 +0000124 int i;
125
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000126 flags = bb_getopt_ulflags(argc, argv, "Radlv");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000127
Mike Frysingerd89e6292005-04-24 05:07:59 +0000128 if (optind > argc - 1)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000129 lsattr_args(".");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000130 else
131 for (i = optind; i < argc; i++)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000132 lsattr_args(argv[i]);
133
134 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000135}