blob: 073b2c67bda299157856aa6c396eba7b34036cdf [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
Denys Vlasenkob097a842018-12-28 03:20:17 +010016//config: bool "stat (11 kb)"
Denys Vlasenkoace83302015-10-30 22:10:44 +010017//config: default y
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: display file or filesystem status.
Denys Vlasenkoace83302015-10-30 22:10:44 +010020//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//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.
Denys Vlasenkoace83302015-10-30 22:10:44 +010029//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
Denys Vlasenkoace83302015-10-30 22:10:44 +010034//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020035//config: Without this, stat will not support the '-f' option to display
36//config: information about filesystem status.
Denys Vlasenkoace83302015-10-30 22:10:44 +010037
Denys Vlasenko248a67f2017-08-07 18:18:09 +020038//applet:IF_STAT(APPLET_NOEXEC(stat, stat, BB_DIR_BIN, BB_SUID_DROP, stat))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010039
40//kbuild:lib-$(CONFIG_STAT) += stat.o
Pere Orga34425382011-03-31 14:43:25 +020041
42//usage:#define stat_trivial_usage
Denys Vlasenko11f1a252020-12-13 19:04:19 +010043//usage: "[-lt"IF_FEATURE_STAT_FILESYSTEM("f")"] "IF_FEATURE_STAT_FORMAT("[-c FMT] ")"FILE..."
Pere Orga34425382011-03-31 14:43:25 +020044//usage:#define stat_full_usage "\n\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010045//usage: "Display file"
46//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
47//usage: " status\n"
Pere Orga34425382011-03-31 14:43:25 +020048//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010049//usage: "\n -c FMT Use the specified format"
Pere Orga34425382011-03-31 14:43:25 +020050//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +010051//usage: IF_FEATURE_STAT_FILESYSTEM(
Pere Orga34425382011-03-31 14:43:25 +020052//usage: "\n -f Display filesystem status"
Denys Vlasenkoace83302015-10-30 22:10:44 +010053//usage: )
Pere Orga34425382011-03-31 14:43:25 +020054//usage: "\n -L Follow links"
Denys Vlasenkoace83302015-10-30 22:10:44 +010055//usage: "\n -t Terse display"
Pere Orga34425382011-03-31 14:43:25 +020056//usage: IF_SELINUX(
57//usage: "\n -Z Print security context"
58//usage: )
59//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010060//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
Pere Orga34425382011-03-31 14:43:25 +020061//usage: " %a Access rights in octal\n"
62//usage: " %A Access rights in human readable form\n"
63//usage: " %b Number of blocks allocated (see %B)\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010064//usage: " %B Size in bytes of each block reported by %b\n"
Pere Orga34425382011-03-31 14:43:25 +020065//usage: " %d Device number in decimal\n"
66//usage: " %D Device number in hex\n"
67//usage: " %f Raw mode in hex\n"
68//usage: " %F File type\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010069//usage: " %g Group ID\n"
70//usage: " %G Group name\n"
Pere Orga34425382011-03-31 14:43:25 +020071//usage: " %h Number of hard links\n"
72//usage: " %i Inode number\n"
73//usage: " %n File name\n"
74//usage: " %N File name, with -> TARGET if symlink\n"
75//usage: " %o I/O block size\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010076//usage: " %s Total size in bytes\n"
Pere Orga34425382011-03-31 14:43:25 +020077//usage: " %t Major device type in hex\n"
78//usage: " %T Minor device type in hex\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010079//usage: " %u User ID\n"
80//usage: " %U User name\n"
Pere Orga34425382011-03-31 14:43:25 +020081//usage: " %x Time of last access\n"
82//usage: " %X Time of last access as seconds since Epoch\n"
83//usage: " %y Time of last modification\n"
84//usage: " %Y Time of last modification as seconds since Epoch\n"
85//usage: " %z Time of last change\n"
86//usage: " %Z Time of last change as seconds since Epoch\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010087//usage: IF_FEATURE_STAT_FILESYSTEM(
88//usage: "\nFMT sequences for file systems:\n"
Pere Orga34425382011-03-31 14:43:25 +020089//usage: " %a Free blocks available to non-superuser\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010090//usage: " %b Total data blocks\n"
91//usage: " %c Total file nodes\n"
92//usage: " %d Free file nodes\n"
93//usage: " %f Free blocks\n"
Pere Orga34425382011-03-31 14:43:25 +020094//usage: IF_SELINUX(
95//usage: " %C Security context in selinux\n"
96//usage: )
97//usage: " %i File System ID in hex\n"
98//usage: " %l Maximum length of filenames\n"
99//usage: " %n File name\n"
100//usage: " %s Block size (for faster transfer)\n"
101//usage: " %S Fundamental block size (for block counts)\n"
102//usage: " %t Type in hex\n"
103//usage: " %T Type in human readable form"
104//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +0100105//usage: )
Pere Orga34425382011-03-31 14:43:25 +0200106
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000107#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200108#include "common_bufsiz.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000109
Denys Vlasenkoace83302015-10-30 22:10:44 +0100110enum {
111 OPT_TERSE = (1 << 0),
112 OPT_DEREFERENCE = (1 << 1),
113 OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
114 OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
115};
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000116
Denis Vlasenko85c24712008-03-17 09:04:04 +0000117#if ENABLE_FEATURE_STAT_FORMAT
118typedef bool (*statfunc_ptr)(const char *, const char *);
119#else
120typedef bool (*statfunc_ptr)(const char *);
121#endif
122
Denis Vlasenko91e52032007-10-05 20:31:23 +0000123static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000124{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000125 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000126 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000127 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000128 * returned string must start with a consonant.
129 */
130 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
131 if (S_ISDIR(st->st_mode)) return "directory";
132 if (S_ISBLK(st->st_mode)) return "block special file";
133 if (S_ISCHR(st->st_mode)) return "character special file";
134 if (S_ISFIFO(st->st_mode)) return "fifo";
135 if (S_ISLNK(st->st_mode)) return "symbolic link";
136 if (S_ISSOCK(st->st_mode)) return "socket";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200137#ifdef S_TYPEISMQ
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000138 if (S_TYPEISMQ(st)) return "message queue";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200139#endif
140#ifdef S_TYPEISSEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000141 if (S_TYPEISSEM(st)) return "semaphore";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200142#endif
143#ifdef S_TYPEISSHM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000144 if (S_TYPEISSHM(st)) return "shared memory object";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200145#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000146#ifdef S_TYPEISTMO
147 if (S_TYPEISTMO(st)) return "typed memory object";
148#endif
149 return "weird file";
150}
151
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200152static const char *human_time(struct timespec *ts)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000153{
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200154 char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + /*paranoia*/ 8];
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000155
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200156 /* coreutils 6.3 compat */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200157#define buf bb_common_bufsiz1
158 setup_common_bufsiz();
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200159
160 sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
161 strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000162 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000163#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000164}
165
Denys Vlasenkoace83302015-10-30 22:10:44 +0100166#if ENABLE_FEATURE_STAT_FILESYSTEM
Ron Yorstone2026382019-03-18 11:14:52 +0000167#define FS_TYPE_LIST \
168FS_TYPE(0xADFF, "affs") \
169FS_TYPE(0x1CD1, "devpts") \
170FS_TYPE(0x137D, "ext") \
171FS_TYPE(0xEF51, "ext2") \
172FS_TYPE(0xEF53, "ext2/ext3") \
173FS_TYPE(0x3153464a, "jfs") \
174FS_TYPE(0x58465342, "xfs") \
175FS_TYPE(0xF995E849, "hpfs") \
176FS_TYPE(0x9660, "isofs") \
177FS_TYPE(0x4000, "isofs") \
178FS_TYPE(0x4004, "isofs") \
179FS_TYPE(0x137F, "minix") \
180FS_TYPE(0x138F, "minix (30 char.)") \
181FS_TYPE(0x2468, "minix v2") \
182FS_TYPE(0x2478, "minix v2 (30 char.)") \
183FS_TYPE(0x4d44, "msdos") \
184FS_TYPE(0x4006, "fat") \
185FS_TYPE(0x564c, "novell") \
186FS_TYPE(0x6969, "nfs") \
187FS_TYPE(0x9fa0, "proc") \
188FS_TYPE(0x517B, "smb") \
189FS_TYPE(0x012FF7B4, "xenix") \
190FS_TYPE(0x012FF7B5, "sysv4") \
191FS_TYPE(0x012FF7B6, "sysv2") \
192FS_TYPE(0x012FF7B7, "coh") \
193FS_TYPE(0x00011954, "ufs") \
194FS_TYPE(0x012FD16D, "xia") \
195FS_TYPE(0x5346544e, "ntfs") \
196FS_TYPE(0x1021994, "tmpfs") \
197FS_TYPE(0x52654973, "reiserfs") \
198FS_TYPE(0x28cd3d45, "cramfs") \
199FS_TYPE(0x7275, "romfs") \
200FS_TYPE(0x858458f6, "ramfs") \
201FS_TYPE(0x73717368, "squashfs") \
202FS_TYPE(0x62656572, "sysfs")
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000203/* Return the type of the specified file system.
204 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
205 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
206 * Still others have neither and have to get by with f_type (Linux).
207 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000208static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000209{
Ron Yorstone2026382019-03-18 11:14:52 +0000210# define FS_TYPE(type, name) type,
Denys Vlasenko965b7952020-11-30 13:03:03 +0100211 static const uint32_t fstype[] ALIGN4 = {
Ron Yorstone2026382019-03-18 11:14:52 +0000212 FS_TYPE_LIST
Mike Frysinger408ae212005-04-24 04:11:44 +0000213 };
Ron Yorstone2026382019-03-18 11:14:52 +0000214# undef FS_TYPE
215# define FS_TYPE(type, name) name"\0"
216 static const char humanname[] ALIGN1 =
217 FS_TYPE_LIST
218 "UNKNOWN";
219# undef FS_TYPE
Denis Vlasenko91e52032007-10-05 20:31:23 +0000220 int i;
221
Ron Yorstone2026382019-03-18 11:14:52 +0000222 for (i = 0; i < ARRAY_SIZE(fstype); ++i)
223 if (fstype[i] == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000224 break;
Ron Yorstone2026382019-03-18 11:14:52 +0000225 return nth_string(humanname, i);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000226}
227
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000228/* "man statfs" says that statfsbuf->f_fsid is a mess */
229/* coreutils treats it as an array of ints, most significant first */
230static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
231{
232 const unsigned *p = (const void*) &statfsbuf->f_fsid;
233 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
234 unsigned long long r = 0;
235
236 do
237 r = (r << (sizeof(unsigned)*8)) | *p++;
238 while (--sz > 0);
239 return r;
240}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100241#endif /* FEATURE_STAT_FILESYSTEM */
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000242
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000243#if ENABLE_FEATURE_STAT_FORMAT
244static void strcatc(char *str, char c)
245{
246 int len = strlen(str);
247 str[len++] = c;
248 str[len] = '\0';
249}
250
251static void printfs(char *pformat, const char *msg)
252{
253 strcatc(pformat, 's');
254 printf(pformat, msg);
255}
256
Denys Vlasenkoace83302015-10-30 22:10:44 +0100257#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000258/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100259static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000260 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000261 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000262{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000263 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000264 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000265 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000266 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000267 strcat(pformat, "llx");
268 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000269 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000270 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100271 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000272 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000273 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100274 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000275 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000276 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000277 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100278 strcat(pformat, "llu");
279 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000280 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100281 strcat(pformat, "llu");
282 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000283 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100284 strcat(pformat, "llu");
285 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000286 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000287 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100288 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000289 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100290 strcat(pformat, "llu");
291 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000292 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100293 strcat(pformat, "llu");
294 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
295# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000296 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000297 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100298# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000299 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000300 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000301 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000302 }
303}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100304#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000305
306/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100307static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000308 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000309 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000310{
311#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
312 struct stat *statbuf = (struct stat *) data;
313 struct passwd *pw_ent;
314 struct group *gw_ent;
315
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000316 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000317 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000318 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000319 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000320 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000321 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000322 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000323 return;
Sebd2d327d2010-06-12 21:57:50 +0200324 printf("'%s' -> '%s'", filename, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000325 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000326 } else {
327 printf(pformat, filename);
328 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000329 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100330 strcat(pformat, "llu");
331 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000332 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100333 strcat(pformat, "llx");
334 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000335 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100336 strcat(pformat, "llu");
337 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000338 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000339 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000340 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 +0000341 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000342 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000343 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000344 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000345 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000346 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000347 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000348 } else if (m == 'h') {
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_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000351 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000352 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000353 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000354 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000355 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000356 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000357 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000358 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000359 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000360 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000361 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000362 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
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) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000366 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000367 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000368 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000369 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100370 strcat(pformat, "llu");
371 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000372 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000373 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000374 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000375 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100376 strcat(pformat, "llu");
377 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000378 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000379 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000380 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000381 } else if (m == 'x') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200382 printfs(pformat, human_time(&statbuf->st_atim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000383 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000384 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100385 /* note: (unsigned long) would be wrong:
386 * imagine (unsigned long64)int32 */
387 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000388 } else if (m == 'y') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200389 printfs(pformat, human_time(&statbuf->st_mtim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000390 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000391 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100392 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000393 } else if (m == 'z') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200394 printfs(pformat, human_time(&statbuf->st_ctim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000395 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000396 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100397 printf(pformat, (long) statbuf->st_ctime);
398# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000399 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000400 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100401# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000402 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000403 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000404 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000405 }
406}
407
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100408static void print_it(const char *masterformat,
409 const char *filename,
410 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000411 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100412 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000413{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000414 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000415 char *format = xstrdup(masterformat);
Maninder Singh97c64912015-05-25 13:46:36 +0200416 /* Add 2 to accommodate our conversion of the stat '%s' format string
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000417 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000418 char *dest = xmalloc(strlen(format) + 2 + 1);
419 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000420
421 b = format;
422 while (b) {
Sebd2d327d2010-06-12 21:57:50 +0200423 /* Each iteration finds next %spec,
424 * prints preceding string and handles found %spec
425 */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000426 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000427 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000428 if (!p) {
Sebd2d327d2010-06-12 21:57:50 +0200429 /* coreutils 6.3 always prints newline at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000430 /*fputs(b, stdout);*/
431 puts(b);
432 break;
433 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000434
Denis Vlasenko91e52032007-10-05 20:31:23 +0000435 /* dest = "%<modifiers>" */
Sebd2d327d2010-06-12 21:57:50 +0200436 len = 1 + strspn(p + 1, "#-+.I 0123456789");
437 memcpy(dest, p, len);
438 dest[len] = '\0';
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000439
Sebd2d327d2010-06-12 21:57:50 +0200440 /* print preceding string */
441 *p = '\0';
442 fputs(b, stdout);
443
444 p += len;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000445 b = p + 1;
446 switch (*p) {
447 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000448 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000449 /* fall through */
450 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000451 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000452 break;
453 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000454 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000455 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000456 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000457 }
458 }
459
460 free(format);
461 free(dest);
462}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100463#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000464
Denys Vlasenkoace83302015-10-30 22:10:44 +0100465#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000466/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000467#if !ENABLE_FEATURE_STAT_FORMAT
468#define do_statfs(filename, format) do_statfs(filename)
469#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000470static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000471{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000472 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000473#if !ENABLE_FEATURE_STAT_FORMAT
474 const char *format;
475#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000476#if ENABLE_SELINUX
477 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000478
479 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000480 if ((option_mask32 & OPT_DEREFERENCE
481 ? lgetfilecon(filename, &scontext)
482 : getfilecon(filename, &scontext)
483 ) < 0
484 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200485 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000486 return 0;
487 }
488 }
489#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000490 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100491 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000492 return 0;
493 }
494
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000495#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000496 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100497# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000498 format = (option_mask32 & OPT_TERSE
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200499 ? "%n %i %l %t %s %b %f %a %c %d"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000500 : " File: \"%n\"\n"
501 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000502 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000503 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000504 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100505# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000506 format = (option_mask32 & OPT_TERSE
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200507 ? (option_mask32 & OPT_SELINUX
508 ? "%n %i %l %t %s %b %f %a %c %d %C"
509 : "%n %i %l %t %s %b %f %a %c %d")
510 : (option_mask32 & OPT_SELINUX
511 ? " File: \"%n\"\n"
512 " ID: %-8i Namelen: %-7l Type: %T\n"
513 "Block size: %-10s\n"
514 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
515 "Inodes: Total: %-10c Free: %d"
516 " S_context: %C"
517 : " File: \"%n\"\n"
518 " ID: %-8i Namelen: %-7l Type: %T\n"
519 "Block size: %-10s\n"
520 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
521 "Inodes: Total: %-10c Free: %d")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000522 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100523# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000524 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000525 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200526#else /* !FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000527 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000528 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000529 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000530 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000531 printf(format,
532 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000533 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000534 statfsbuf.f_namelen);
535
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000536 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100537 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000538 else
539 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
540
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100541# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000542 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100543 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000544 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100545 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
546 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000547 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100548 (unsigned long) statfsbuf.f_bsize,
549 (unsigned long long) statfsbuf.f_blocks,
550 (unsigned long long) statfsbuf.f_bfree,
551 (unsigned long long) statfsbuf.f_bavail,
552 (unsigned long long) statfsbuf.f_files,
553 (unsigned long long) statfsbuf.f_ffree);
554# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000555 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100556 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
557 : (option_mask32 & OPT_SELINUX
558 ? "Block size: %-10lu\n"
559 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
560 "Inodes: Total: %-10llu Free: %llu"
561 "S_context: %C\n"
562 : "Block size: %-10lu\n"
563 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
564 "Inodes: Total: %-10llu Free: %llu\n"
565 )
566 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000567 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100568 (unsigned long) statfsbuf.f_bsize,
569 (unsigned long long) statfsbuf.f_blocks,
570 (unsigned long long) statfsbuf.f_bfree,
571 (unsigned long long) statfsbuf.f_bavail,
572 (unsigned long long) statfsbuf.f_files,
573 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000574 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000575
Denis Vlasenko49622d72007-03-10 16:58:49 +0000576 if (scontext)
577 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100578# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200579#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000580 return 1;
581}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100582#endif /* FEATURE_STAT_FILESYSTEM */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000583
584/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000585#if !ENABLE_FEATURE_STAT_FORMAT
586#define do_stat(filename, format) do_stat(filename)
587#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000588static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000589{
590 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000591#if ENABLE_SELINUX
592 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000593
594 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000595 if ((option_mask32 & OPT_DEREFERENCE
596 ? lgetfilecon(filename, &scontext)
597 : getfilecon(filename, &scontext)
598 ) < 0
599 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200600 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000601 return 0;
602 }
603 }
604#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000605 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100606 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000607 return 0;
608 }
609
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000610#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000611 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100612# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000613 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000614 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000615 } else {
616 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
617 format =
Sebd2d327d2010-06-12 21:57:50 +0200618 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000619 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
620 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
621 " Device type: %t,%T\n"
622 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200623 "Access: %x\n" "Modify: %y\n" "Change: %z";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000624 } else {
625 format =
Sebd2d327d2010-06-12 21:57:50 +0200626 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000627 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
628 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
629 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200630 "Access: %x\n" "Modify: %y\n" "Change: %z";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000631 }
632 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100633# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000634 if (option_mask32 & OPT_TERSE) {
635 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100636 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
637 :
638 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
639 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000640 } else {
641 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000642 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100643 " File: %N\n"
644 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
645 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
646 " Device type: %t,%T\n"
647 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
648 " S_Context: %C\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200649 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100650 :
651 " File: %N\n"
652 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
653 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
654 " Device type: %t,%T\n"
655 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200656 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100657 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000658 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000659 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100660 " File: %N\n"
661 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
662 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
663 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
664 "S_Context: %C\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200665 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100666 :
667 " File: %N\n"
668 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
669 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
670 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200671 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100672 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000673 }
674 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100675# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000676 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000677 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000678#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000679 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100680 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000681 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000682 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100683 (unsigned long long) statbuf.st_size,
684 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000685 (unsigned long) statbuf.st_mode,
686 (unsigned long) statbuf.st_uid,
687 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100688 (unsigned long long) statbuf.st_dev,
689 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000690 (unsigned long) statbuf.st_nlink,
691 (unsigned long) major(statbuf.st_rdev),
692 (unsigned long) minor(statbuf.st_rdev),
693 (unsigned long) statbuf.st_atime,
694 (unsigned long) statbuf.st_mtime,
695 (unsigned long) statbuf.st_ctime,
696 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000697 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100698# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000699 if (option_mask32 & OPT_SELINUX)
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200700 printf(" %s\n", scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000701 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000702 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100703# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000704 } else {
705 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000706 struct passwd *pw_ent;
707 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200708
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000709 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000710 pw_ent = getpwuid(statbuf.st_uid);
711
712 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000713 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200714 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200715 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200716 free(linkname);
717 } else {
Sebd2d327d2010-06-12 21:57:50 +0200718 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200719 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000720
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100721 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
722 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
723 (unsigned long long) statbuf.st_size,
724 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000725 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000726 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100727 (unsigned long long) statbuf.st_dev,
728 (unsigned long long) statbuf.st_dev,
729 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000730 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000731 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
732 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000733 (unsigned long) major(statbuf.st_rdev),
734 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000735 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000736 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000737 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000738 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000739 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000740 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000741 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000742 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000743 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100744# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200745 if (option_mask32 & OPT_SELINUX)
746 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100747# endif
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200748 printf("Access: %s\n", human_time(&statbuf.st_atim));
749 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
750 printf("Change: %s\n", human_time(&statbuf.st_ctim));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000751 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200752#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000753 return 1;
754}
755
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000756int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100757int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000758{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000759 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000760 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100761 int ok;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000762 statfunc_ptr statfunc = do_stat;
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100763#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
764 unsigned opts;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000765
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100766 opts =
767#endif
768 getopt32(argv, "^"
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200769 "tL"
Denys Vlasenkoace83302015-10-30 22:10:44 +0100770 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000771 IF_SELINUX("Z")
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200772 IF_FEATURE_STAT_FORMAT("c:")
773 "\0" "-1" /* min one arg */
774 IF_FEATURE_STAT_FORMAT(,&format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000775 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100776#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100777 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000778 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100779#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000780#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100781 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000782 selinux_or_die();
783 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100784#endif
785 ok = 1;
786 argv += optind;
787 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000788 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000789
790 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
791}