blob: fa9ac7b10c87e664b74377437b8d5bf778aa5fdb [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
34//config: select PLATFORM_LINUX # statfs()
35//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: Without this, stat will not support the '-f' option to display
37//config: information about filesystem status.
Denys Vlasenkoace83302015-10-30 22:10:44 +010038
Denys Vlasenko248a67f2017-08-07 18:18:09 +020039//applet:IF_STAT(APPLET_NOEXEC(stat, stat, BB_DIR_BIN, BB_SUID_DROP, stat))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010040
41//kbuild:lib-$(CONFIG_STAT) += stat.o
Pere Orga34425382011-03-31 14:43:25 +020042
43//usage:#define stat_trivial_usage
44//usage: "[OPTIONS] FILE..."
45//usage:#define stat_full_usage "\n\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010046//usage: "Display file"
47//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
48//usage: " status\n"
Pere Orga34425382011-03-31 14:43:25 +020049//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010050//usage: "\n -c FMT Use the specified format"
Pere Orga34425382011-03-31 14:43:25 +020051//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +010052//usage: IF_FEATURE_STAT_FILESYSTEM(
Pere Orga34425382011-03-31 14:43:25 +020053//usage: "\n -f Display filesystem status"
Denys Vlasenkoace83302015-10-30 22:10:44 +010054//usage: )
Pere Orga34425382011-03-31 14:43:25 +020055//usage: "\n -L Follow links"
Denys Vlasenkoace83302015-10-30 22:10:44 +010056//usage: "\n -t Terse display"
Pere Orga34425382011-03-31 14:43:25 +020057//usage: IF_SELINUX(
58//usage: "\n -Z Print security context"
59//usage: )
60//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010061//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
Pere Orga34425382011-03-31 14:43:25 +020062//usage: " %a Access rights in octal\n"
63//usage: " %A Access rights in human readable form\n"
64//usage: " %b Number of blocks allocated (see %B)\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010065//usage: " %B Size in bytes of each block reported by %b\n"
Pere Orga34425382011-03-31 14:43:25 +020066//usage: " %d Device number in decimal\n"
67//usage: " %D Device number in hex\n"
68//usage: " %f Raw mode in hex\n"
69//usage: " %F File type\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010070//usage: " %g Group ID\n"
71//usage: " %G Group name\n"
Pere Orga34425382011-03-31 14:43:25 +020072//usage: " %h Number of hard links\n"
73//usage: " %i Inode number\n"
74//usage: " %n File name\n"
75//usage: " %N File name, with -> TARGET if symlink\n"
76//usage: " %o I/O block size\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010077//usage: " %s Total size in bytes\n"
Pere Orga34425382011-03-31 14:43:25 +020078//usage: " %t Major device type in hex\n"
79//usage: " %T Minor device type in hex\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010080//usage: " %u User ID\n"
81//usage: " %U User name\n"
Pere Orga34425382011-03-31 14:43:25 +020082//usage: " %x Time of last access\n"
83//usage: " %X Time of last access as seconds since Epoch\n"
84//usage: " %y Time of last modification\n"
85//usage: " %Y Time of last modification as seconds since Epoch\n"
86//usage: " %z Time of last change\n"
87//usage: " %Z Time of last change as seconds since Epoch\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010088//usage: IF_FEATURE_STAT_FILESYSTEM(
89//usage: "\nFMT sequences for file systems:\n"
Pere Orga34425382011-03-31 14:43:25 +020090//usage: " %a Free blocks available to non-superuser\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010091//usage: " %b Total data blocks\n"
92//usage: " %c Total file nodes\n"
93//usage: " %d Free file nodes\n"
94//usage: " %f Free blocks\n"
Pere Orga34425382011-03-31 14:43:25 +020095//usage: IF_SELINUX(
96//usage: " %C Security context in selinux\n"
97//usage: )
98//usage: " %i File System ID in hex\n"
99//usage: " %l Maximum length of filenames\n"
100//usage: " %n File name\n"
101//usage: " %s Block size (for faster transfer)\n"
102//usage: " %S Fundamental block size (for block counts)\n"
103//usage: " %t Type in hex\n"
104//usage: " %T Type in human readable form"
105//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +0100106//usage: )
Pere Orga34425382011-03-31 14:43:25 +0200107
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000108#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200109#include "common_bufsiz.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000110
Denys Vlasenkoace83302015-10-30 22:10:44 +0100111enum {
112 OPT_TERSE = (1 << 0),
113 OPT_DEREFERENCE = (1 << 1),
114 OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
115 OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
116};
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000117
Denis Vlasenko85c24712008-03-17 09:04:04 +0000118#if ENABLE_FEATURE_STAT_FORMAT
119typedef bool (*statfunc_ptr)(const char *, const char *);
120#else
121typedef bool (*statfunc_ptr)(const char *);
122#endif
123
Denis Vlasenko91e52032007-10-05 20:31:23 +0000124static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000125{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000126 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000127 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000128 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000129 * returned string must start with a consonant.
130 */
131 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
132 if (S_ISDIR(st->st_mode)) return "directory";
133 if (S_ISBLK(st->st_mode)) return "block special file";
134 if (S_ISCHR(st->st_mode)) return "character special file";
135 if (S_ISFIFO(st->st_mode)) return "fifo";
136 if (S_ISLNK(st->st_mode)) return "symbolic link";
137 if (S_ISSOCK(st->st_mode)) return "socket";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200138#ifdef S_TYPEISMQ
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000139 if (S_TYPEISMQ(st)) return "message queue";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200140#endif
141#ifdef S_TYPEISSEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000142 if (S_TYPEISSEM(st)) return "semaphore";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200143#endif
144#ifdef S_TYPEISSHM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000145 if (S_TYPEISSHM(st)) return "shared memory object";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200146#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000147#ifdef S_TYPEISTMO
148 if (S_TYPEISTMO(st)) return "typed memory object";
149#endif
150 return "weird file";
151}
152
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200153static const char *human_time(struct timespec *ts)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000154{
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200155 char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + /*paranoia*/ 8];
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000156
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200157 /* coreutils 6.3 compat */
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200158#define buf bb_common_bufsiz1
159 setup_common_bufsiz();
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200160
161 sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
162 strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000163 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000164#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000165}
166
Denys Vlasenkoace83302015-10-30 22:10:44 +0100167#if ENABLE_FEATURE_STAT_FILESYSTEM
Ron Yorstone2026382019-03-18 11:14:52 +0000168#define FS_TYPE_LIST \
169FS_TYPE(0xADFF, "affs") \
170FS_TYPE(0x1CD1, "devpts") \
171FS_TYPE(0x137D, "ext") \
172FS_TYPE(0xEF51, "ext2") \
173FS_TYPE(0xEF53, "ext2/ext3") \
174FS_TYPE(0x3153464a, "jfs") \
175FS_TYPE(0x58465342, "xfs") \
176FS_TYPE(0xF995E849, "hpfs") \
177FS_TYPE(0x9660, "isofs") \
178FS_TYPE(0x4000, "isofs") \
179FS_TYPE(0x4004, "isofs") \
180FS_TYPE(0x137F, "minix") \
181FS_TYPE(0x138F, "minix (30 char.)") \
182FS_TYPE(0x2468, "minix v2") \
183FS_TYPE(0x2478, "minix v2 (30 char.)") \
184FS_TYPE(0x4d44, "msdos") \
185FS_TYPE(0x4006, "fat") \
186FS_TYPE(0x564c, "novell") \
187FS_TYPE(0x6969, "nfs") \
188FS_TYPE(0x9fa0, "proc") \
189FS_TYPE(0x517B, "smb") \
190FS_TYPE(0x012FF7B4, "xenix") \
191FS_TYPE(0x012FF7B5, "sysv4") \
192FS_TYPE(0x012FF7B6, "sysv2") \
193FS_TYPE(0x012FF7B7, "coh") \
194FS_TYPE(0x00011954, "ufs") \
195FS_TYPE(0x012FD16D, "xia") \
196FS_TYPE(0x5346544e, "ntfs") \
197FS_TYPE(0x1021994, "tmpfs") \
198FS_TYPE(0x52654973, "reiserfs") \
199FS_TYPE(0x28cd3d45, "cramfs") \
200FS_TYPE(0x7275, "romfs") \
201FS_TYPE(0x858458f6, "ramfs") \
202FS_TYPE(0x73717368, "squashfs") \
203FS_TYPE(0x62656572, "sysfs")
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000204/* Return the type of the specified file system.
205 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
206 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
207 * Still others have neither and have to get by with f_type (Linux).
208 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000209static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000210{
Ron Yorstone2026382019-03-18 11:14:52 +0000211# define FS_TYPE(type, name) type,
212 static const uint32_t fstype[] = {
213 FS_TYPE_LIST
Mike Frysinger408ae212005-04-24 04:11:44 +0000214 };
Ron Yorstone2026382019-03-18 11:14:52 +0000215# undef FS_TYPE
216# define FS_TYPE(type, name) name"\0"
217 static const char humanname[] ALIGN1 =
218 FS_TYPE_LIST
219 "UNKNOWN";
220# undef FS_TYPE
Denis Vlasenko91e52032007-10-05 20:31:23 +0000221 int i;
222
Ron Yorstone2026382019-03-18 11:14:52 +0000223 for (i = 0; i < ARRAY_SIZE(fstype); ++i)
224 if (fstype[i] == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000225 break;
Ron Yorstone2026382019-03-18 11:14:52 +0000226 return nth_string(humanname, i);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000227}
228
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000229/* "man statfs" says that statfsbuf->f_fsid is a mess */
230/* coreutils treats it as an array of ints, most significant first */
231static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
232{
233 const unsigned *p = (const void*) &statfsbuf->f_fsid;
234 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
235 unsigned long long r = 0;
236
237 do
238 r = (r << (sizeof(unsigned)*8)) | *p++;
239 while (--sz > 0);
240 return r;
241}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100242#endif /* FEATURE_STAT_FILESYSTEM */
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000243
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000244#if ENABLE_FEATURE_STAT_FORMAT
245static void strcatc(char *str, char c)
246{
247 int len = strlen(str);
248 str[len++] = c;
249 str[len] = '\0';
250}
251
252static void printfs(char *pformat, const char *msg)
253{
254 strcatc(pformat, 's');
255 printf(pformat, msg);
256}
257
Denys Vlasenkoace83302015-10-30 22:10:44 +0100258#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000259/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100260static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000261 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000262 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000263{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000264 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000265 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000266 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000267 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000268 strcat(pformat, "llx");
269 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000270 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000271 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100272 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000273 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000274 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100275 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000276 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000277 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000278 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100279 strcat(pformat, "llu");
280 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000281 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100282 strcat(pformat, "llu");
283 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000284 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100285 strcat(pformat, "llu");
286 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000287 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000288 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100289 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000290 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100291 strcat(pformat, "llu");
292 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000293 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100294 strcat(pformat, "llu");
295 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
296# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000297 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000298 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100299# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000300 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000301 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000302 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000303 }
304}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100305#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000306
307/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100308static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000309 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000310 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000311{
312#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
313 struct stat *statbuf = (struct stat *) data;
314 struct passwd *pw_ent;
315 struct group *gw_ent;
316
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000317 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000318 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000319 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000320 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000321 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000322 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000323 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000324 return;
Sebd2d327d2010-06-12 21:57:50 +0200325 printf("'%s' -> '%s'", filename, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000326 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000327 } else {
328 printf(pformat, filename);
329 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000330 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100331 strcat(pformat, "llu");
332 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000333 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100334 strcat(pformat, "llx");
335 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000336 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100337 strcat(pformat, "llu");
338 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000339 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000340 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000341 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 +0000342 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000343 printfs(pformat, bb_mode_string(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';
443 fputs(b, stdout);
444
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 {
706 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000707 struct passwd *pw_ent;
708 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200709
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000710 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000711 pw_ent = getpwuid(statbuf.st_uid);
712
713 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000714 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200715 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200716 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200717 free(linkname);
718 } else {
Sebd2d327d2010-06-12 21:57:50 +0200719 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200720 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000721
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100722 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
723 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
724 (unsigned long long) statbuf.st_size,
725 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000726 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000727 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100728 (unsigned long long) statbuf.st_dev,
729 (unsigned long long) statbuf.st_dev,
730 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000731 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000732 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
733 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000734 (unsigned long) major(statbuf.st_rdev),
735 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000736 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000737 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000738 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000739 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000740 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000741 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000742 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000743 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000744 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100745# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200746 if (option_mask32 & OPT_SELINUX)
747 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100748# endif
Denys Vlasenkob81f8052019-08-03 18:08:27 +0200749 printf("Access: %s\n", human_time(&statbuf.st_atim));
750 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
751 printf("Change: %s\n", human_time(&statbuf.st_ctim));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000752 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200753#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000754 return 1;
755}
756
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000757int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100758int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000759{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000760 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000761 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100762 int ok;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000763 statfunc_ptr statfunc = do_stat;
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100764#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
765 unsigned opts;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000766
Denys Vlasenko82d1c1f2017-12-31 17:30:02 +0100767 opts =
768#endif
769 getopt32(argv, "^"
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200770 "tL"
Denys Vlasenkoace83302015-10-30 22:10:44 +0100771 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000772 IF_SELINUX("Z")
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200773 IF_FEATURE_STAT_FORMAT("c:")
774 "\0" "-1" /* min one arg */
775 IF_FEATURE_STAT_FORMAT(,&format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000776 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100777#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100778 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000779 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100780#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000781#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100782 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000783 selinux_or_die();
784 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100785#endif
786 ok = 1;
787 argv += optind;
788 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000789 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000790
791 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
792}