blob: bbd2e6a7cbb81281e3e6fe0a1de74b07f7791969 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00002/*
3 * stat -- display file or file system status
4 *
5 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
6 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
Denis Vlasenko49622d72007-03-10 16:58:49 +00008 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00009 *
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 *
Bernhard Reutner-Fischere11a01c2006-04-05 17:19:37 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000017
18/* vars to control behavior */
Denis Vlasenko91e52032007-10-05 20:31:23 +000019#define OPT_FILESYS (1 << 0)
20#define OPT_TERSE (1 << 1)
21#define OPT_DEREFERENCE (1 << 2)
22#define OPT_SELINUX (1 << 3)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000023
Denis Vlasenko85c24712008-03-17 09:04:04 +000024#if ENABLE_FEATURE_STAT_FORMAT
25typedef bool (*statfunc_ptr)(const char *, const char *);
26#else
27typedef bool (*statfunc_ptr)(const char *);
28#endif
29
Denis Vlasenko91e52032007-10-05 20:31:23 +000030static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000031{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000033 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000035 * returned string must start with a consonant.
36 */
37 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
38 if (S_ISDIR(st->st_mode)) return "directory";
39 if (S_ISBLK(st->st_mode)) return "block special file";
40 if (S_ISCHR(st->st_mode)) return "character special file";
41 if (S_ISFIFO(st->st_mode)) return "fifo";
42 if (S_ISLNK(st->st_mode)) return "symbolic link";
43 if (S_ISSOCK(st->st_mode)) return "socket";
44 if (S_TYPEISMQ(st)) return "message queue";
45 if (S_TYPEISSEM(st)) return "semaphore";
46 if (S_TYPEISSHM(st)) return "shared memory object";
47#ifdef S_TYPEISTMO
48 if (S_TYPEISTMO(st)) return "typed memory object";
49#endif
50 return "weird file";
51}
52
Denis Vlasenko91e52032007-10-05 20:31:23 +000053static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000054{
Denis Vlasenko5d148e22006-11-21 00:12:09 +000055 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000056 static char *str;
57 str = ctime(&t);
58 str[strlen(str)-1] = '\0';
59 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +000060 */
61 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000062
Denis Vlasenko91e52032007-10-05 20:31:23 +000063 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
64#define buf bb_common_bufsiz1
65
Denis Vlasenko5d148e22006-11-21 00:12:09 +000066 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
67 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +000068#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000069}
70
71/* Return the type of the specified file system.
72 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
73 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
74 * Still others have neither and have to get by with f_type (Linux).
75 */
Denis Vlasenko91e52032007-10-05 20:31:23 +000076static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000077{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000078 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +000079 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000080 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +000081 } humantypes[] = {
82 { 0xADFF, "affs" },
83 { 0x1Cd1, "devpts" },
84 { 0x137D, "ext" },
85 { 0xEF51, "ext2" },
86 { 0xEF53, "ext2/ext3" },
87 { 0x3153464a, "jfs" },
88 { 0x58465342, "xfs" },
89 { 0xF995E849, "hpfs" },
90 { 0x9660, "isofs" },
91 { 0x4000, "isofs" },
92 { 0x4004, "isofs" },
93 { 0x137F, "minix" },
94 { 0x138F, "minix (30 char.)" },
95 { 0x2468, "minix v2" },
96 { 0x2478, "minix v2 (30 char.)" },
97 { 0x4d44, "msdos" },
98 { 0x4006, "fat" },
99 { 0x564c, "novell" },
100 { 0x6969, "nfs" },
101 { 0x9fa0, "proc" },
102 { 0x517B, "smb" },
103 { 0x012FF7B4, "xenix" },
104 { 0x012FF7B5, "sysv4" },
105 { 0x012FF7B6, "sysv2" },
106 { 0x012FF7B7, "coh" },
107 { 0x00011954, "ufs" },
108 { 0x012FD16D, "xia" },
109 { 0x5346544e, "ntfs" },
110 { 0x1021994, "tmpfs" },
111 { 0x52654973, "reiserfs" },
112 { 0x28cd3d45, "cramfs" },
113 { 0x7275, "romfs" },
114 { 0x858458f6, "romfs" },
115 { 0x73717368, "squashfs" },
116 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000117 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000118 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000119
120 int i;
121
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000122 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000123 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000124 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000125 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000126}
127
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000128/* "man statfs" says that statfsbuf->f_fsid is a mess */
129/* coreutils treats it as an array of ints, most significant first */
130static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
131{
132 const unsigned *p = (const void*) &statfsbuf->f_fsid;
133 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
134 unsigned long long r = 0;
135
136 do
137 r = (r << (sizeof(unsigned)*8)) | *p++;
138 while (--sz > 0);
139 return r;
140}
141
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000142#if ENABLE_FEATURE_STAT_FORMAT
143static void strcatc(char *str, char c)
144{
145 int len = strlen(str);
146 str[len++] = c;
147 str[len] = '\0';
148}
149
150static void printfs(char *pformat, const char *msg)
151{
152 strcatc(pformat, 's');
153 printf(pformat, msg);
154}
155
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000156/* print statfs info */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000157static void print_statfs(char *pformat, const char m,
158 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000159 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000160{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000161 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000162 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000163 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000164 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000165 strcat(pformat, "llx");
166 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000167 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000168 strcat(pformat, "lu");
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000169 printf(pformat, (unsigned long) (statfsbuf->f_namelen));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000170 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000171 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000172 printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000173 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000174 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000175 } else if (m == 'b') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000176 strcat(pformat, "jd");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000177 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000178 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000179 strcat(pformat, "jd");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000180 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000181 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000182 strcat(pformat, "jd");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000183 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000184 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000185 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000186 printf(pformat, (unsigned long) (statfsbuf->f_bsize));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000187 } else if (m == 'c') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000188 strcat(pformat, "jd");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000189 printf(pformat, (intmax_t) (statfsbuf->f_files));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000190 } else if (m == 'd') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000191 strcat(pformat, "jd");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000192 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000193#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000194 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000195 printfs(pformat, scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000196#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000197 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000198 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000199 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000200 }
201}
202
203/* print stat info */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000204static void print_stat(char *pformat, const char m,
205 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000206 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000207{
208#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
209 struct stat *statbuf = (struct stat *) data;
210 struct passwd *pw_ent;
211 struct group *gw_ent;
212
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000213 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000214 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000215 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000216 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000217 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000218 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000219 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000220 return;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000221 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
222 printf(pformat, filename);
223 printf(" -> ");
224 printf(pformat, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000225 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000226 } else {
227 printf(pformat, filename);
228 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000229 } else if (m == 'd') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000230 strcat(pformat, "ju");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000231 printf(pformat, (uintmax_t) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000232 } else if (m == 'D') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000233 strcat(pformat, "jx");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000234 printf(pformat, (uintmax_t) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000235 } else if (m == 'i') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000236 strcat(pformat, "ju");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000237 printf(pformat, (uintmax_t) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000238 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000239 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000240 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000241 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000242 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000243 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000244 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000245 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000246 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000247 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000248 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000249 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000250 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000251 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000252 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000253 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000254 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000255 setpwent();
256 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000257 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000258 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000259 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000260 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000261 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000262 setgrent();
263 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000264 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000265 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000266 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000267 printf(pformat, (unsigned long) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000268 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000269 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000270 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000271 } else if (m == 's') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000272 strcat(pformat, "ju");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000273 printf(pformat, (uintmax_t) (statbuf->st_size));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000274 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000275 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000276 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000277 } else if (m == 'b') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000278 strcat(pformat, "ju");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000279 printf(pformat, (uintmax_t) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000280 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000281 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000282 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000283 } else if (m == 'x') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000284 printfs(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000285 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000286 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000287 printf(pformat, (unsigned long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000288 } else if (m == 'y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000289 printfs(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000290 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000291 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000292 printf(pformat, (unsigned long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000293 } else if (m == 'z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000294 printfs(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000295 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000296 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000297 printf(pformat, (unsigned long) statbuf->st_ctime);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000298#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000299 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000300 printfs(pformat, scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000301#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000302 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000303 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000304 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000305 }
306}
307
Denis Vlasenko91e52032007-10-05 20:31:23 +0000308static void print_it(const char *masterformat, const char *filename,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000309 void (*print_func) (char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000310 const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000311 IF_SELINUX(, security_context_t scontext) )
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000312{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000313 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000314 char *format = xstrdup(masterformat);
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000315 /* Add 2 to accomodate our conversion of the stat '%s' format string
316 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000317 char *dest = xmalloc(strlen(format) + 2 + 1);
318 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000319
320 b = format;
321 while (b) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000322 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000323 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000324 if (!p) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000325 /* coreutils 6.3 always prints <cr> at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000326 /*fputs(b, stdout);*/
327 puts(b);
328 break;
329 }
330 *p++ = '\0';
331 fputs(b, stdout);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000332
Denis Vlasenko91e52032007-10-05 20:31:23 +0000333 /* dest = "%<modifiers>" */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000334 len = strspn(p, "#-+.I 0123456789");
335 dest[0] = '%';
336 memcpy(dest + 1, p, len);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000337 dest[1 + len] = '\0';
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000338 p += len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000339
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000340 b = p + 1;
341 switch (*p) {
342 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000343 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000344 /* fall through */
345 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000346 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000347 break;
348 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000349 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000350 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000351 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000352 }
353 }
354
355 free(format);
356 free(dest);
357}
358#endif
359
360/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000361#if !ENABLE_FEATURE_STAT_FORMAT
362#define do_statfs(filename, format) do_statfs(filename)
363#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000364static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000365{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000366 struct statfs statfsbuf;
367
Denis Vlasenko85c24712008-03-17 09:04:04 +0000368#if !ENABLE_FEATURE_STAT_FORMAT
369 const char *format;
370#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000371#if ENABLE_SELINUX
372 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000373
374 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000375 if ((option_mask32 & OPT_DEREFERENCE
376 ? lgetfilecon(filename, &scontext)
377 : getfilecon(filename, &scontext)
378 ) < 0
379 ) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000380 bb_perror_msg(filename);
381 return 0;
382 }
383 }
384#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000385 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100386 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000387 return 0;
388 }
389
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000390#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000391 if (format == NULL) {
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000392#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000393 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000394 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000395 : " File: \"%n\"\n"
396 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000397 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000398 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000399 "Inodes: Total: %-10c Free: %d");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000400#else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000401 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000402 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000403 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000404 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000405 " File: \"%n\"\n"
406 " ID: %-8i Namelen: %-7l Type: %T\n"
407 "Block size: %-10s\n"
408 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
409 "Inodes: Total: %-10c Free: %d"
410 " S_context: %C\n":
411 " File: \"%n\"\n"
412 " ID: %-8i Namelen: %-7l Type: %T\n"
413 "Block size: %-10s\n"
414 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
415 "Inodes: Total: %-10c Free: %d\n")
416 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000417#endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000418 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000419 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000420#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000421 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000422 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000423 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000424 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000425 printf(format,
426 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000427 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000428 statfsbuf.f_namelen);
429
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000430 if (option_mask32 & OPT_TERSE)
Denis Vlasenko87468852007-04-13 23:22:00 +0000431 printf("%lx ", (unsigned long) (statfsbuf.f_type));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000432 else
433 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
434
Denis Vlasenko49622d72007-03-10 16:58:49 +0000435#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000436 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000437 ? "%lu %ld %ld %ld %ld %ld\n"
438 : "Block size: %-10lu\n"
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000439 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
440 "Inodes: Total: %-10jd Free: %jd\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000441 printf(format,
Denis Vlasenko87468852007-04-13 23:22:00 +0000442 (unsigned long) (statfsbuf.f_bsize),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000443 (intmax_t) (statfsbuf.f_blocks),
444 (intmax_t) (statfsbuf.f_bfree),
445 (intmax_t) (statfsbuf.f_bavail),
446 (intmax_t) (statfsbuf.f_files),
447 (intmax_t) (statfsbuf.f_ffree));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000448#else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000449 format = (option_mask32 & OPT_TERSE
450 ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000451 "%lu %ld %ld %ld %ld %ld\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000452 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000453 "Block size: %-10lu\n"
454 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
455 "Inodes: Total: %-10jd Free: %jd"
456 "S_context: %C\n":
457 "Block size: %-10lu\n"
458 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
459 "Inodes: Total: %-10jd Free: %jd\n"));
460 printf(format,
Denis Vlasenko87468852007-04-13 23:22:00 +0000461 (unsigned long) (statfsbuf.f_bsize),
Denis Vlasenko49622d72007-03-10 16:58:49 +0000462 (intmax_t) (statfsbuf.f_blocks),
463 (intmax_t) (statfsbuf.f_bfree),
464 (intmax_t) (statfsbuf.f_bavail),
465 (intmax_t) (statfsbuf.f_files),
466 (intmax_t) (statfsbuf.f_ffree),
467 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000468
Denis Vlasenko49622d72007-03-10 16:58:49 +0000469 if (scontext)
470 freecon(scontext);
471#endif
472#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000473 return 1;
474}
475
476/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000477#if !ENABLE_FEATURE_STAT_FORMAT
478#define do_stat(filename, format) do_stat(filename)
479#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000480static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000481{
482 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000483#if ENABLE_SELINUX
484 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000485
486 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000487 if ((option_mask32 & OPT_DEREFERENCE
488 ? lgetfilecon(filename, &scontext)
489 : getfilecon(filename, &scontext)
490 ) < 0
491 ) {
492 bb_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000493 return 0;
494 }
495 }
496#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000497 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100498 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000499 return 0;
500 }
501
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000502#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000503 if (format == NULL) {
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000504#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000505 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000506 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000507 } else {
508 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
509 format =
510 " File: \"%N\"\n"
511 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
512 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
513 " Device type: %t,%T\n"
514 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
515 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
516 } else {
517 format =
518 " File: \"%N\"\n"
519 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
520 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
521 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
522 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
523 }
524 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000525#else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000526 if (option_mask32 & OPT_TERSE) {
527 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000528 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
529 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
530 } else {
531 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000532 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000533 " File: \"%N\"\n"
534 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
535 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
536 " Device type: %t,%T\n"
537 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
538 " S_Context: %C\n"
539 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
540 " File: \"%N\"\n"
541 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
542 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
543 " Device type: %t,%T\n"
544 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
545 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
546 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000547 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000548 " File: \"%N\"\n"
549 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
550 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
551 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
552 "S_Context: %C\n"
553 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
554 " File: \"%N\"\n"
555 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
556 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
557 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
558 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
559 }
560 }
561#endif
562 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000563 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000564#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000565 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000566 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000567 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000568 filename,
569 (uintmax_t) (statbuf.st_size),
570 (uintmax_t) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000571 (unsigned long) statbuf.st_mode,
572 (unsigned long) statbuf.st_uid,
573 (unsigned long) statbuf.st_gid,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000574 (uintmax_t) statbuf.st_dev,
575 (uintmax_t) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000576 (unsigned long) statbuf.st_nlink,
577 (unsigned long) major(statbuf.st_rdev),
578 (unsigned long) minor(statbuf.st_rdev),
579 (unsigned long) statbuf.st_atime,
580 (unsigned long) statbuf.st_mtime,
581 (unsigned long) statbuf.st_ctime,
582 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000583 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000584#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000585 if (option_mask32 & OPT_SELINUX)
Denis Vlasenko49622d72007-03-10 16:58:49 +0000586 printf(" %lc\n", *scontext);
587 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000588 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000589#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000590 } else {
591 char *linkname = NULL;
592
593 struct passwd *pw_ent;
594 struct group *gw_ent;
595 setgrent();
596 gw_ent = getgrgid(statbuf.st_gid);
597 setpwent();
598 pw_ent = getpwuid(statbuf.st_uid);
599
600 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000601 linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000602 if (linkname)
603 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
604 else
605 printf(" File: \"%s\"\n", filename);
606
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000607 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
608 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000609 (uintmax_t) (statbuf.st_size),
610 (uintmax_t) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000611 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000612 file_type(&statbuf),
613 (uintmax_t) statbuf.st_dev,
614 (uintmax_t) statbuf.st_dev,
615 (uintmax_t) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000616 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000617 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
618 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000619 (unsigned long) major(statbuf.st_rdev),
620 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000621 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000622 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000623 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000624 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000625 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000626 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000627 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000628 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000629 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denis Vlasenko49622d72007-03-10 16:58:49 +0000630#if ENABLE_SELINUX
631 printf(" S_Context: %lc\n", *scontext);
632#endif
633 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000634 human_time(statbuf.st_atime),
635 human_time(statbuf.st_mtime),
636 human_time(statbuf.st_ctime));
637 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000638#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000639 return 1;
640}
641
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000642int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100643int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000644{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000645 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000646 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100647 int ok;
648 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000649 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000650
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100651 opt_complementary = "-1"; /* min one arg */
652 opts = getopt32(argv, "ftL"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000653 IF_SELINUX("Z")
654 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000655 );
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100656 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000657 statfunc = do_statfs;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000658#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100659 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000660 selinux_or_die();
661 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100662#endif
663 ok = 1;
664 argv += optind;
665 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000666 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000667
668 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
669}