blob: 1a490fef74b7276181a2e3d3320f7858b422c735 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020013 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
Denys Vlasenkoace83302015-10-30 22:10:44 +010015//config:config STAT
16//config: bool "stat"
17//config: default y
18//config: help
19//config: display file or filesystem status.
20//config:
21//config:config FEATURE_STAT_FORMAT
22//config: bool "Enable custom formats (-c)"
23//config: default y
24//config: depends on STAT
25//config: help
26//config: Without this, stat will not support the '-c format' option where
27//config: users can pass a custom format string for output. This adds about
28//config: 7k to a nonstatic build on amd64.
29//config:
30//config:config FEATURE_STAT_FILESYSTEM
31//config: bool "Enable display of filesystem status (-f)"
32//config: default y
33//config: depends on STAT
34//config: select PLATFORM_LINUX # statfs()
35//config: help
36//config: Without this, stat will not support the '-f' option to display
37//config: information about filesystem status.
38
Pere Orga34425382011-03-31 14:43:25 +020039
40//usage:#define stat_trivial_usage
41//usage: "[OPTIONS] FILE..."
42//usage:#define stat_full_usage "\n\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010043//usage: "Display file"
44//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
45//usage: " status\n"
Pere Orga34425382011-03-31 14:43:25 +020046//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010047//usage: "\n -c FMT Use the specified format"
Pere Orga34425382011-03-31 14:43:25 +020048//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +010049//usage: IF_FEATURE_STAT_FILESYSTEM(
Pere Orga34425382011-03-31 14:43:25 +020050//usage: "\n -f Display filesystem status"
Denys Vlasenkoace83302015-10-30 22:10:44 +010051//usage: )
Pere Orga34425382011-03-31 14:43:25 +020052//usage: "\n -L Follow links"
Denys Vlasenkoace83302015-10-30 22:10:44 +010053//usage: "\n -t Terse display"
Pere Orga34425382011-03-31 14:43:25 +020054//usage: IF_SELINUX(
55//usage: "\n -Z Print security context"
56//usage: )
57//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010058//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
Pere Orga34425382011-03-31 14:43:25 +020059//usage: " %a Access rights in octal\n"
60//usage: " %A Access rights in human readable form\n"
61//usage: " %b Number of blocks allocated (see %B)\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010062//usage: " %B Size in bytes of each block reported by %b\n"
Pere Orga34425382011-03-31 14:43:25 +020063//usage: " %d Device number in decimal\n"
64//usage: " %D Device number in hex\n"
65//usage: " %f Raw mode in hex\n"
66//usage: " %F File type\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010067//usage: " %g Group ID\n"
68//usage: " %G Group name\n"
Pere Orga34425382011-03-31 14:43:25 +020069//usage: " %h Number of hard links\n"
70//usage: " %i Inode number\n"
71//usage: " %n File name\n"
72//usage: " %N File name, with -> TARGET if symlink\n"
73//usage: " %o I/O block size\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010074//usage: " %s Total size in bytes\n"
Pere Orga34425382011-03-31 14:43:25 +020075//usage: " %t Major device type in hex\n"
76//usage: " %T Minor device type in hex\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010077//usage: " %u User ID\n"
78//usage: " %U User name\n"
Pere Orga34425382011-03-31 14:43:25 +020079//usage: " %x Time of last access\n"
80//usage: " %X Time of last access as seconds since Epoch\n"
81//usage: " %y Time of last modification\n"
82//usage: " %Y Time of last modification as seconds since Epoch\n"
83//usage: " %z Time of last change\n"
84//usage: " %Z Time of last change as seconds since Epoch\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010085//usage: IF_FEATURE_STAT_FILESYSTEM(
86//usage: "\nFMT sequences for file systems:\n"
Pere Orga34425382011-03-31 14:43:25 +020087//usage: " %a Free blocks available to non-superuser\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010088//usage: " %b Total data blocks\n"
89//usage: " %c Total file nodes\n"
90//usage: " %d Free file nodes\n"
91//usage: " %f Free blocks\n"
Pere Orga34425382011-03-31 14:43:25 +020092//usage: IF_SELINUX(
93//usage: " %C Security context in selinux\n"
94//usage: )
95//usage: " %i File System ID in hex\n"
96//usage: " %l Maximum length of filenames\n"
97//usage: " %n File name\n"
98//usage: " %s Block size (for faster transfer)\n"
99//usage: " %S Fundamental block size (for block counts)\n"
100//usage: " %t Type in hex\n"
101//usage: " %T Type in human readable form"
102//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +0100103//usage: )
Pere Orga34425382011-03-31 14:43:25 +0200104
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000105#include "libbb.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000106
Denys Vlasenkoace83302015-10-30 22:10:44 +0100107enum {
108 OPT_TERSE = (1 << 0),
109 OPT_DEREFERENCE = (1 << 1),
110 OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
111 OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
112};
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000113
Denis Vlasenko85c24712008-03-17 09:04:04 +0000114#if ENABLE_FEATURE_STAT_FORMAT
115typedef bool (*statfunc_ptr)(const char *, const char *);
116#else
117typedef bool (*statfunc_ptr)(const char *);
118#endif
119
Denis Vlasenko91e52032007-10-05 20:31:23 +0000120static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000121{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000123 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000124 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000125 * returned string must start with a consonant.
126 */
127 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
128 if (S_ISDIR(st->st_mode)) return "directory";
129 if (S_ISBLK(st->st_mode)) return "block special file";
130 if (S_ISCHR(st->st_mode)) return "character special file";
131 if (S_ISFIFO(st->st_mode)) return "fifo";
132 if (S_ISLNK(st->st_mode)) return "symbolic link";
133 if (S_ISSOCK(st->st_mode)) return "socket";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200134#ifdef S_TYPEISMQ
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000135 if (S_TYPEISMQ(st)) return "message queue";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200136#endif
137#ifdef S_TYPEISSEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000138 if (S_TYPEISSEM(st)) return "semaphore";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200139#endif
140#ifdef S_TYPEISSHM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000141 if (S_TYPEISSHM(st)) return "shared memory object";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200142#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000143#ifdef S_TYPEISTMO
144 if (S_TYPEISTMO(st)) return "typed memory object";
145#endif
146 return "weird file";
147}
148
Denis Vlasenko91e52032007-10-05 20:31:23 +0000149static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000150{
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000151 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000152 static char *str;
153 str = ctime(&t);
154 str[strlen(str)-1] = '\0';
155 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000156 */
157 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000158
Denis Vlasenko91e52032007-10-05 20:31:23 +0000159 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
160#define buf bb_common_bufsiz1
161
Denys Vlasenko8f2cb7a2013-03-29 12:30:33 +0100162 strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000");
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000163 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000164#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000165}
166
Denys Vlasenkoace83302015-10-30 22:10:44 +0100167#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000168/* Return the type of the specified file system.
169 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
170 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
171 * Still others have neither and have to get by with f_type (Linux).
172 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000173static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000174{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000175 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000176 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000177 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +0000178 } humantypes[] = {
179 { 0xADFF, "affs" },
180 { 0x1Cd1, "devpts" },
181 { 0x137D, "ext" },
182 { 0xEF51, "ext2" },
183 { 0xEF53, "ext2/ext3" },
184 { 0x3153464a, "jfs" },
185 { 0x58465342, "xfs" },
186 { 0xF995E849, "hpfs" },
187 { 0x9660, "isofs" },
188 { 0x4000, "isofs" },
189 { 0x4004, "isofs" },
190 { 0x137F, "minix" },
191 { 0x138F, "minix (30 char.)" },
192 { 0x2468, "minix v2" },
193 { 0x2478, "minix v2 (30 char.)" },
194 { 0x4d44, "msdos" },
195 { 0x4006, "fat" },
196 { 0x564c, "novell" },
197 { 0x6969, "nfs" },
198 { 0x9fa0, "proc" },
199 { 0x517B, "smb" },
200 { 0x012FF7B4, "xenix" },
201 { 0x012FF7B5, "sysv4" },
202 { 0x012FF7B6, "sysv2" },
203 { 0x012FF7B7, "coh" },
204 { 0x00011954, "ufs" },
205 { 0x012FD16D, "xia" },
206 { 0x5346544e, "ntfs" },
207 { 0x1021994, "tmpfs" },
208 { 0x52654973, "reiserfs" },
209 { 0x28cd3d45, "cramfs" },
210 { 0x7275, "romfs" },
211 { 0x858458f6, "romfs" },
212 { 0x73717368, "squashfs" },
213 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000214 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000215 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000216
217 int i;
218
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000219 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000220 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000221 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000222 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000223}
224
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000225/* "man statfs" says that statfsbuf->f_fsid is a mess */
226/* coreutils treats it as an array of ints, most significant first */
227static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
228{
229 const unsigned *p = (const void*) &statfsbuf->f_fsid;
230 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
231 unsigned long long r = 0;
232
233 do
234 r = (r << (sizeof(unsigned)*8)) | *p++;
235 while (--sz > 0);
236 return r;
237}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100238#endif /* FEATURE_STAT_FILESYSTEM */
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000239
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000240#if ENABLE_FEATURE_STAT_FORMAT
241static void strcatc(char *str, char c)
242{
243 int len = strlen(str);
244 str[len++] = c;
245 str[len] = '\0';
246}
247
248static void printfs(char *pformat, const char *msg)
249{
250 strcatc(pformat, 's');
251 printf(pformat, msg);
252}
253
Denys Vlasenkoace83302015-10-30 22:10:44 +0100254#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000255/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100256static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000257 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000258 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000259{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000260 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000261 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000262 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000263 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000264 strcat(pformat, "llx");
265 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000266 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000267 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100268 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000269 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000270 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100271 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000272 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000273 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000274 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100275 strcat(pformat, "llu");
276 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000277 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100278 strcat(pformat, "llu");
279 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000280 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100281 strcat(pformat, "llu");
282 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000283 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000284 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100285 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000286 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100287 strcat(pformat, "llu");
288 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000289 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100290 strcat(pformat, "llu");
291 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
292# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000293 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000294 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100295# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000296 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000297 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000298 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000299 }
300}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100301#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000302
303/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100304static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000305 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000306 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000307{
308#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
309 struct stat *statbuf = (struct stat *) data;
310 struct passwd *pw_ent;
311 struct group *gw_ent;
312
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000313 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000314 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000315 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000316 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000317 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000318 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000319 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000320 return;
Sebd2d327d2010-06-12 21:57:50 +0200321 printf("'%s' -> '%s'", filename, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000322 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000323 } else {
324 printf(pformat, filename);
325 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000326 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100327 strcat(pformat, "llu");
328 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000329 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100330 strcat(pformat, "llx");
331 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000332 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100333 strcat(pformat, "llu");
334 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000335 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000336 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000337 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 +0000338 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000339 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000340 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000341 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000342 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000343 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000344 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000345 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000346 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000347 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000348 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000349 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000350 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000351 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000352 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000353 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000354 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000355 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000356 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000357 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000358 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000359 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000360 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000361 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000362 printf(pformat, (unsigned long) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000363 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000364 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000365 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000366 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100367 strcat(pformat, "llu");
368 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000369 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000370 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000371 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000372 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100373 strcat(pformat, "llu");
374 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000375 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000376 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000377 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000378 } else if (m == 'x') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000379 printfs(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000380 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000381 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100382 /* note: (unsigned long) would be wrong:
383 * imagine (unsigned long64)int32 */
384 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000385 } else if (m == 'y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000386 printfs(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000387 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000388 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100389 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000390 } else if (m == 'z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000391 printfs(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000392 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000393 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100394 printf(pformat, (long) statbuf->st_ctime);
395# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000396 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000397 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100398# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000399 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000400 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000401 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000402 }
403}
404
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100405static void print_it(const char *masterformat,
406 const char *filename,
407 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000408 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100409 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000410{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000411 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000412 char *format = xstrdup(masterformat);
Maninder Singh97c64912015-05-25 13:46:36 +0200413 /* Add 2 to accommodate our conversion of the stat '%s' format string
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000414 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000415 char *dest = xmalloc(strlen(format) + 2 + 1);
416 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000417
418 b = format;
419 while (b) {
Sebd2d327d2010-06-12 21:57:50 +0200420 /* Each iteration finds next %spec,
421 * prints preceding string and handles found %spec
422 */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000423 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000424 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000425 if (!p) {
Sebd2d327d2010-06-12 21:57:50 +0200426 /* coreutils 6.3 always prints newline at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000427 /*fputs(b, stdout);*/
428 puts(b);
429 break;
430 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000431
Denis Vlasenko91e52032007-10-05 20:31:23 +0000432 /* dest = "%<modifiers>" */
Sebd2d327d2010-06-12 21:57:50 +0200433 len = 1 + strspn(p + 1, "#-+.I 0123456789");
434 memcpy(dest, p, len);
435 dest[len] = '\0';
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000436
Sebd2d327d2010-06-12 21:57:50 +0200437 /* print preceding string */
438 *p = '\0';
439 fputs(b, stdout);
440
441 p += len;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000442 b = p + 1;
443 switch (*p) {
444 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000445 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000446 /* fall through */
447 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000448 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000449 break;
450 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000451 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000452 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000453 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000454 }
455 }
456
457 free(format);
458 free(dest);
459}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100460#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000461
Denys Vlasenkoace83302015-10-30 22:10:44 +0100462#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000463/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000464#if !ENABLE_FEATURE_STAT_FORMAT
465#define do_statfs(filename, format) do_statfs(filename)
466#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000467static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000468{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000469 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000470#if !ENABLE_FEATURE_STAT_FORMAT
471 const char *format;
472#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000473#if ENABLE_SELINUX
474 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000475
476 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000477 if ((option_mask32 & OPT_DEREFERENCE
478 ? lgetfilecon(filename, &scontext)
479 : getfilecon(filename, &scontext)
480 ) < 0
481 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200482 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000483 return 0;
484 }
485 }
486#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000487 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100488 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000489 return 0;
490 }
491
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000492#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000493 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100494# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000495 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000496 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000497 : " File: \"%n\"\n"
498 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000499 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000500 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000501 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100502# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000503 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000504 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000505 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000506 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000507 " File: \"%n\"\n"
508 " ID: %-8i Namelen: %-7l Type: %T\n"
509 "Block size: %-10s\n"
510 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
511 "Inodes: Total: %-10c Free: %d"
512 " S_context: %C\n":
513 " File: \"%n\"\n"
514 " ID: %-8i Namelen: %-7l Type: %T\n"
515 "Block size: %-10s\n"
516 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
517 "Inodes: Total: %-10c Free: %d\n")
518 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100519# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000520 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000521 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000522#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000523 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000524 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000525 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000526 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000527 printf(format,
528 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000529 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000530 statfsbuf.f_namelen);
531
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000532 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100533 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000534 else
535 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
536
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100537# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000538 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100539 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000540 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100541 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
542 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000543 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100544 (unsigned long) statfsbuf.f_bsize,
545 (unsigned long long) statfsbuf.f_blocks,
546 (unsigned long long) statfsbuf.f_bfree,
547 (unsigned long long) statfsbuf.f_bavail,
548 (unsigned long long) statfsbuf.f_files,
549 (unsigned long long) statfsbuf.f_ffree);
550# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000551 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100552 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
553 : (option_mask32 & OPT_SELINUX
554 ? "Block size: %-10lu\n"
555 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
556 "Inodes: Total: %-10llu Free: %llu"
557 "S_context: %C\n"
558 : "Block size: %-10lu\n"
559 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
560 "Inodes: Total: %-10llu Free: %llu\n"
561 )
562 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000563 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100564 (unsigned long) statfsbuf.f_bsize,
565 (unsigned long long) statfsbuf.f_blocks,
566 (unsigned long long) statfsbuf.f_bfree,
567 (unsigned long long) statfsbuf.f_bavail,
568 (unsigned long long) statfsbuf.f_files,
569 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000570 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000571
Denis Vlasenko49622d72007-03-10 16:58:49 +0000572 if (scontext)
573 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100574# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200575#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000576 return 1;
577}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100578#endif /* FEATURE_STAT_FILESYSTEM */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000579
580/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000581#if !ENABLE_FEATURE_STAT_FORMAT
582#define do_stat(filename, format) do_stat(filename)
583#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000584static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000585{
586 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000587#if ENABLE_SELINUX
588 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000589
590 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000591 if ((option_mask32 & OPT_DEREFERENCE
592 ? lgetfilecon(filename, &scontext)
593 : getfilecon(filename, &scontext)
594 ) < 0
595 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200596 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000597 return 0;
598 }
599 }
600#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000601 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100602 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000603 return 0;
604 }
605
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000606#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000607 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100608# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000609 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000610 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000611 } else {
612 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
613 format =
Sebd2d327d2010-06-12 21:57:50 +0200614 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000615 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
616 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
617 " Device type: %t,%T\n"
618 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
619 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
620 } else {
621 format =
Sebd2d327d2010-06-12 21:57:50 +0200622 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000623 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
624 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
625 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
626 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
627 }
628 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100629# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000630 if (option_mask32 & OPT_TERSE) {
631 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100632 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
633 :
634 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
635 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000636 } else {
637 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000638 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100639 " File: %N\n"
640 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
641 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
642 " Device type: %t,%T\n"
643 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
644 " S_Context: %C\n"
645 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
646 :
647 " File: %N\n"
648 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
649 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
650 " Device type: %t,%T\n"
651 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
652 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
653 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000654 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000655 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100656 " File: %N\n"
657 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
658 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
659 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
660 "S_Context: %C\n"
661 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
662 :
663 " File: %N\n"
664 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
665 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
666 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
667 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
668 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000669 }
670 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100671# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000672 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000673 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000674#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000675 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100676 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000677 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000678 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100679 (unsigned long long) statbuf.st_size,
680 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000681 (unsigned long) statbuf.st_mode,
682 (unsigned long) statbuf.st_uid,
683 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100684 (unsigned long long) statbuf.st_dev,
685 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000686 (unsigned long) statbuf.st_nlink,
687 (unsigned long) major(statbuf.st_rdev),
688 (unsigned long) minor(statbuf.st_rdev),
689 (unsigned long) statbuf.st_atime,
690 (unsigned long) statbuf.st_mtime,
691 (unsigned long) statbuf.st_ctime,
692 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000693 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100694# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000695 if (option_mask32 & OPT_SELINUX)
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200696 printf(" %s\n", scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000697 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000698 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100699# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000700 } else {
701 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000702 struct passwd *pw_ent;
703 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200704
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000705 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000706 pw_ent = getpwuid(statbuf.st_uid);
707
708 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000709 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200710 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200711 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200712 free(linkname);
713 } else {
Sebd2d327d2010-06-12 21:57:50 +0200714 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200715 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000716
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100717 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
718 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
719 (unsigned long long) statbuf.st_size,
720 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000721 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000722 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100723 (unsigned long long) statbuf.st_dev,
724 (unsigned long long) statbuf.st_dev,
725 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000726 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000727 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
728 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000729 (unsigned long) major(statbuf.st_rdev),
730 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000731 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000732 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000733 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000734 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000735 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000736 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000737 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000738 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000739 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100740# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200741 if (option_mask32 & OPT_SELINUX)
742 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100743# endif
Eric Lammerts66be9192010-10-30 02:48:20 +0200744 printf("Access: %s\n", human_time(statbuf.st_atime));
745 printf("Modify: %s\n", human_time(statbuf.st_mtime));
746 printf("Change: %s\n", human_time(statbuf.st_ctime));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000747 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200748#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000749 return 1;
750}
751
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000752int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100753int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000754{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000755 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000756 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100757 int ok;
758 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000759 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000760
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100761 opt_complementary = "-1"; /* min one arg */
Denys Vlasenkoace83302015-10-30 22:10:44 +0100762 opts = getopt32(argv, "tL"
763 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000764 IF_SELINUX("Z")
765 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000766 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100767#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100768 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000769 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100770#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000771#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100772 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000773 selinux_or_die();
774 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100775#endif
776 ok = 1;
777 argv += optind;
778 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000779 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000780
781 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
782}