blob: 2c2909e7e45bf2664951bc9bd5941e25333ad207 [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') {
Denys Vlasenko59ac4672021-09-17 01:13:58 +0200342 char modestr[12];
343 printfs(pformat, bb_mode_string(modestr, statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000344 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000345 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000346 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000347 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000348 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000349 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000350 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000351 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000352 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000353 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000354 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000355 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000356 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000357 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000358 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000359 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000360 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000361 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000362 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000363 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000364 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000365 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000366 printf(pformat, (unsigned long) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000367 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000368 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000369 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000370 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100371 strcat(pformat, "llu");
372 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000373 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000374 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000375 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000376 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100377 strcat(pformat, "llu");
378 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000379 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000380 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000381 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000382 } else if (m == 'x') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200383 printfs(pformat, human_time(&statbuf->st_atim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000384 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000385 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100386 /* note: (unsigned long) would be wrong:
387 * imagine (unsigned long64)int32 */
388 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000389 } else if (m == 'y') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200390 printfs(pformat, human_time(&statbuf->st_mtim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000391 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000392 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100393 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000394 } else if (m == 'z') {
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200395 printfs(pformat, human_time(&statbuf->st_ctim));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000396 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000397 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100398 printf(pformat, (long) statbuf->st_ctime);
399# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000400 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000401 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100402# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000403 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000404 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000405 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000406 }
407}
408
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100409static void print_it(const char *masterformat,
410 const char *filename,
411 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000412 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100413 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000414{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000415 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000416 char *format = xstrdup(masterformat);
Maninder Singh97c64912015-05-25 13:46:36 +0200417 /* Add 2 to accommodate our conversion of the stat '%s' format string
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000418 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000419 char *dest = xmalloc(strlen(format) + 2 + 1);
420 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000421
422 b = format;
423 while (b) {
Sebd2d327d2010-06-12 21:57:50 +0200424 /* Each iteration finds next %spec,
425 * prints preceding string and handles found %spec
426 */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000427 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000428 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000429 if (!p) {
Sebd2d327d2010-06-12 21:57:50 +0200430 /* coreutils 6.3 always prints newline at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000431 /*fputs(b, stdout);*/
432 puts(b);
433 break;
434 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000435
Denis Vlasenko91e52032007-10-05 20:31:23 +0000436 /* dest = "%<modifiers>" */
Sebd2d327d2010-06-12 21:57:50 +0200437 len = 1 + strspn(p + 1, "#-+.I 0123456789");
438 memcpy(dest, p, len);
439 dest[len] = '\0';
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000440
Sebd2d327d2010-06-12 21:57:50 +0200441 /* print preceding string */
442 *p = '\0';
Ron Yorstoncad3fc72021-02-03 20:47:14 +0100443 fputs_stdout(b);
Sebd2d327d2010-06-12 21:57:50 +0200444
445 p += len;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000446 b = p + 1;
447 switch (*p) {
448 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000449 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000450 /* fall through */
451 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000452 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000453 break;
454 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000455 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000456 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000457 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000458 }
459 }
460
461 free(format);
462 free(dest);
463}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100464#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000465
Denys Vlasenkoace83302015-10-30 22:10:44 +0100466#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000467/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000468#if !ENABLE_FEATURE_STAT_FORMAT
469#define do_statfs(filename, format) do_statfs(filename)
470#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000471static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000472{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000473 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000474#if !ENABLE_FEATURE_STAT_FORMAT
475 const char *format;
476#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000477#if ENABLE_SELINUX
478 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000479
480 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000481 if ((option_mask32 & OPT_DEREFERENCE
482 ? lgetfilecon(filename, &scontext)
483 : getfilecon(filename, &scontext)
484 ) < 0
485 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200486 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000487 return 0;
488 }
489 }
490#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000491 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100492 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000493 return 0;
494 }
495
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000496#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000497 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100498# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000499 format = (option_mask32 & OPT_TERSE
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200500 ? "%n %i %l %t %s %b %f %a %c %d"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000501 : " File: \"%n\"\n"
502 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000503 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000504 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000505 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100506# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000507 format = (option_mask32 & OPT_TERSE
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200508 ? (option_mask32 & OPT_SELINUX
509 ? "%n %i %l %t %s %b %f %a %c %d %C"
510 : "%n %i %l %t %s %b %f %a %c %d")
511 : (option_mask32 & OPT_SELINUX
512 ? " File: \"%n\"\n"
513 " ID: %-8i Namelen: %-7l Type: %T\n"
514 "Block size: %-10s\n"
515 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
516 "Inodes: Total: %-10c Free: %d"
517 " S_context: %C"
518 : " File: \"%n\"\n"
519 " ID: %-8i Namelen: %-7l Type: %T\n"
520 "Block size: %-10s\n"
521 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
522 "Inodes: Total: %-10c Free: %d")
Denis Vlasenko49622d72007-03-10 16:58:49 +0000523 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100524# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000525 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000526 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200527#else /* !FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000528 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000529 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000530 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000531 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000532 printf(format,
533 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000534 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000535 statfsbuf.f_namelen);
536
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000537 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100538 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000539 else
540 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
541
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100542# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000543 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100544 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000545 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100546 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
547 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000548 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100549 (unsigned long) statfsbuf.f_bsize,
550 (unsigned long long) statfsbuf.f_blocks,
551 (unsigned long long) statfsbuf.f_bfree,
552 (unsigned long long) statfsbuf.f_bavail,
553 (unsigned long long) statfsbuf.f_files,
554 (unsigned long long) statfsbuf.f_ffree);
555# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000556 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100557 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
558 : (option_mask32 & OPT_SELINUX
559 ? "Block size: %-10lu\n"
560 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
561 "Inodes: Total: %-10llu Free: %llu"
562 "S_context: %C\n"
563 : "Block size: %-10lu\n"
564 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
565 "Inodes: Total: %-10llu Free: %llu\n"
566 )
567 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000568 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100569 (unsigned long) statfsbuf.f_bsize,
570 (unsigned long long) statfsbuf.f_blocks,
571 (unsigned long long) statfsbuf.f_bfree,
572 (unsigned long long) statfsbuf.f_bavail,
573 (unsigned long long) statfsbuf.f_files,
574 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000575 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000576
Denis Vlasenko49622d72007-03-10 16:58:49 +0000577 if (scontext)
578 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100579# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200580#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000581 return 1;
582}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100583#endif /* FEATURE_STAT_FILESYSTEM */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000584
585/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000586#if !ENABLE_FEATURE_STAT_FORMAT
587#define do_stat(filename, format) do_stat(filename)
588#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000589static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000590{
591 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000592#if ENABLE_SELINUX
593 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000594
595 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000596 if ((option_mask32 & OPT_DEREFERENCE
597 ? lgetfilecon(filename, &scontext)
598 : getfilecon(filename, &scontext)
599 ) < 0
600 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200601 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000602 return 0;
603 }
604 }
605#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000606 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100607 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000608 return 0;
609 }
610
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000611#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000612 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100613# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000614 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000615 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000616 } else {
617 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
618 format =
Sebd2d327d2010-06-12 21:57:50 +0200619 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000620 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
621 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
622 " Device type: %t,%T\n"
623 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200624 "Access: %x\n" "Modify: %y\n" "Change: %z";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000625 } else {
626 format =
Sebd2d327d2010-06-12 21:57:50 +0200627 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000628 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
629 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
630 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200631 "Access: %x\n" "Modify: %y\n" "Change: %z";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000632 }
633 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100634# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000635 if (option_mask32 & OPT_TERSE) {
636 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100637 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
638 :
639 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
640 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000641 } else {
642 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000643 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100644 " File: %N\n"
645 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
646 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
647 " Device type: %t,%T\n"
648 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
649 " S_Context: %C\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200650 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100651 :
652 " File: %N\n"
653 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
654 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
655 " Device type: %t,%T\n"
656 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200657 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100658 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000659 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000660 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100661 " File: %N\n"
662 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
663 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
664 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
665 "S_Context: %C\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200666 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100667 :
668 " File: %N\n"
669 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
670 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
671 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200672 "Access: %x\n" "Modify: %y\n" "Change: %z"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100673 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000674 }
675 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100676# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000677 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000678 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000679#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000680 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100681 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000682 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000683 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100684 (unsigned long long) statbuf.st_size,
685 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000686 (unsigned long) statbuf.st_mode,
687 (unsigned long) statbuf.st_uid,
688 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100689 (unsigned long long) statbuf.st_dev,
690 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000691 (unsigned long) statbuf.st_nlink,
692 (unsigned long) major(statbuf.st_rdev),
693 (unsigned long) minor(statbuf.st_rdev),
694 (unsigned long) statbuf.st_atime,
695 (unsigned long) statbuf.st_mtime,
696 (unsigned long) statbuf.st_ctime,
697 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000698 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100699# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000700 if (option_mask32 & OPT_SELINUX)
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200701 printf(" %s\n", scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000702 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000703 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100704# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000705 } else {
Denys Vlasenko59ac4672021-09-17 01:13:58 +0200706 char modestr[12];
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000707 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000708 struct passwd *pw_ent;
709 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200710
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000711 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000712 pw_ent = getpwuid(statbuf.st_uid);
713
714 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000715 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200716 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200717 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200718 free(linkname);
719 } else {
Sebd2d327d2010-06-12 21:57:50 +0200720 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200721 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000722
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100723 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
724 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
725 (unsigned long long) statbuf.st_size,
726 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000727 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000728 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100729 (unsigned long long) statbuf.st_dev,
730 (unsigned long long) statbuf.st_dev,
731 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000732 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000733 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
734 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000735 (unsigned long) major(statbuf.st_rdev),
736 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000737 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000738 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000739 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000740 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Denys Vlasenko59ac4672021-09-17 01:13:58 +0200741 bb_mode_string(modestr, statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000742 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000743 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000744 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000745 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100746# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200747 if (option_mask32 & OPT_SELINUX)
748 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100749# endif
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200750 printf("Access: %s\n", human_time(&statbuf.st_atim));
751 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
752 printf("Change: %s\n", human_time(&statbuf.st_ctim));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000753 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200754#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000755 return 1;
756}
757
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000758int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100759int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000760{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000761 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000762 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100763 int ok;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000764 statfunc_ptr statfunc = do_stat;
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100765#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
766 unsigned opts;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000767
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100768 opts =
769#endif
770 getopt32(argv, "^"
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200771 "tL"
Denys Vlasenkoace83302015-10-30 22:10:44 +0100772 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000773 IF_SELINUX("Z")
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200774 IF_FEATURE_STAT_FORMAT("c:")
775 "\0" "-1" /* min one arg */
776 IF_FEATURE_STAT_FORMAT(,&format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000777 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100778#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100779 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000780 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100781#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000782#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100783 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000784 selinux_or_die();
785 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100786#endif
787 ok = 1;
788 argv += optind;
789 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000790 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000791
792 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
793}