blob: b918ec62e80f7bdafeabefa411d118b90f3c18e1 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00002/*
3 * stat -- display file or file system status
4 *
5 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
6 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
Denis Vlasenko49622d72007-03-10 16:58:49 +00008 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00009 *
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020013 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
Denys Vlasenkoace83302015-10-30 22:10:44 +010015//config:config STAT
16//config: bool "stat"
17//config: default y
18//config: help
19//config: display file or filesystem status.
20//config:
21//config:config FEATURE_STAT_FORMAT
22//config: bool "Enable custom formats (-c)"
23//config: default y
24//config: depends on STAT
25//config: help
26//config: Without this, stat will not support the '-c format' option where
27//config: users can pass a custom format string for output. This adds about
28//config: 7k to a nonstatic build on amd64.
29//config:
30//config:config FEATURE_STAT_FILESYSTEM
31//config: bool "Enable display of filesystem status (-f)"
32//config: default y
33//config: depends on STAT
34//config: select PLATFORM_LINUX # statfs()
35//config: help
36//config: Without this, stat will not support the '-f' option to display
37//config: information about filesystem status.
38
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010039//applet:IF_STAT(APPLET(stat, BB_DIR_BIN, BB_SUID_DROP))
40
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
Denis Vlasenko91e52032007-10-05 20:31:23 +0000153static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000154{
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000155 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000156 static char *str;
157 str = ctime(&t);
158 str[strlen(str)-1] = '\0';
159 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000160 */
161 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000162
Denis Vlasenko91e52032007-10-05 20:31:23 +0000163 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200164#define buf bb_common_bufsiz1
165 setup_common_bufsiz();
166 strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000167 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000168#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000169}
170
Denys Vlasenkoace83302015-10-30 22:10:44 +0100171#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000172/* Return the type of the specified file system.
173 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
174 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
175 * Still others have neither and have to get by with f_type (Linux).
176 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000177static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000178{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000179 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000180 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000181 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +0000182 } humantypes[] = {
183 { 0xADFF, "affs" },
184 { 0x1Cd1, "devpts" },
185 { 0x137D, "ext" },
186 { 0xEF51, "ext2" },
187 { 0xEF53, "ext2/ext3" },
188 { 0x3153464a, "jfs" },
189 { 0x58465342, "xfs" },
190 { 0xF995E849, "hpfs" },
191 { 0x9660, "isofs" },
192 { 0x4000, "isofs" },
193 { 0x4004, "isofs" },
194 { 0x137F, "minix" },
195 { 0x138F, "minix (30 char.)" },
196 { 0x2468, "minix v2" },
197 { 0x2478, "minix v2 (30 char.)" },
198 { 0x4d44, "msdos" },
199 { 0x4006, "fat" },
200 { 0x564c, "novell" },
201 { 0x6969, "nfs" },
202 { 0x9fa0, "proc" },
203 { 0x517B, "smb" },
204 { 0x012FF7B4, "xenix" },
205 { 0x012FF7B5, "sysv4" },
206 { 0x012FF7B6, "sysv2" },
207 { 0x012FF7B7, "coh" },
208 { 0x00011954, "ufs" },
209 { 0x012FD16D, "xia" },
210 { 0x5346544e, "ntfs" },
211 { 0x1021994, "tmpfs" },
212 { 0x52654973, "reiserfs" },
213 { 0x28cd3d45, "cramfs" },
214 { 0x7275, "romfs" },
215 { 0x858458f6, "romfs" },
216 { 0x73717368, "squashfs" },
217 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000218 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000219 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000220
221 int i;
222
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000223 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000224 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000225 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000226 return humantypes[i].fs;
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') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000383 printfs(pformat, human_time(statbuf->st_atime));
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') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000390 printfs(pformat, human_time(statbuf->st_mtime));
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') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000395 printfs(pformat, human_time(statbuf->st_ctime));
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
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000500 ? "%n %i %l %t %s %b %f %a %c %d\n"
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
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000508 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000509 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000510 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000511 " File: \"%n\"\n"
512 " ID: %-8i Namelen: %-7l Type: %T\n"
513 "Block size: %-10s\n"
514 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
515 "Inodes: Total: %-10c Free: %d"
516 " S_context: %C\n":
517 " File: \"%n\"\n"
518 " ID: %-8i Namelen: %-7l Type: %T\n"
519 "Block size: %-10s\n"
520 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
521 "Inodes: Total: %-10c Free: %d\n")
522 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100523# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000524 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000525 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000526#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000527 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000528 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000529 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000530 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000531 printf(format,
532 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000533 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000534 statfsbuf.f_namelen);
535
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000536 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100537 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000538 else
539 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
540
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100541# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000542 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100543 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000544 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100545 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
546 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000547 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100548 (unsigned long) statfsbuf.f_bsize,
549 (unsigned long long) statfsbuf.f_blocks,
550 (unsigned long long) statfsbuf.f_bfree,
551 (unsigned long long) statfsbuf.f_bavail,
552 (unsigned long long) statfsbuf.f_files,
553 (unsigned long long) statfsbuf.f_ffree);
554# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000555 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100556 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
557 : (option_mask32 & OPT_SELINUX
558 ? "Block size: %-10lu\n"
559 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
560 "Inodes: Total: %-10llu Free: %llu"
561 "S_context: %C\n"
562 : "Block size: %-10lu\n"
563 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
564 "Inodes: Total: %-10llu Free: %llu\n"
565 )
566 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000567 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100568 (unsigned long) statfsbuf.f_bsize,
569 (unsigned long long) statfsbuf.f_blocks,
570 (unsigned long long) statfsbuf.f_bfree,
571 (unsigned long long) statfsbuf.f_bavail,
572 (unsigned long long) statfsbuf.f_files,
573 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000574 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000575
Denis Vlasenko49622d72007-03-10 16:58:49 +0000576 if (scontext)
577 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100578# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200579#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000580 return 1;
581}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100582#endif /* FEATURE_STAT_FILESYSTEM */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000583
584/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000585#if !ENABLE_FEATURE_STAT_FORMAT
586#define do_stat(filename, format) do_stat(filename)
587#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000588static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000589{
590 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000591#if ENABLE_SELINUX
592 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000593
594 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000595 if ((option_mask32 & OPT_DEREFERENCE
596 ? lgetfilecon(filename, &scontext)
597 : getfilecon(filename, &scontext)
598 ) < 0
599 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200600 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000601 return 0;
602 }
603 }
604#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000605 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100606 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000607 return 0;
608 }
609
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000610#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000611 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100612# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000613 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000614 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000615 } else {
616 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
617 format =
Sebd2d327d2010-06-12 21:57:50 +0200618 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000619 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
620 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
621 " Device type: %t,%T\n"
622 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
623 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
624 } else {
625 format =
Sebd2d327d2010-06-12 21:57:50 +0200626 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000627 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
628 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
629 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
630 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
631 }
632 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100633# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000634 if (option_mask32 & OPT_TERSE) {
635 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100636 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
637 :
638 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
639 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000640 } else {
641 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000642 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100643 " File: %N\n"
644 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
645 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
646 " Device type: %t,%T\n"
647 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
648 " S_Context: %C\n"
649 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
650 :
651 " File: %N\n"
652 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
653 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
654 " Device type: %t,%T\n"
655 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
656 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
657 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000658 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000659 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100660 " File: %N\n"
661 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
662 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
663 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
664 "S_Context: %C\n"
665 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
666 :
667 " File: %N\n"
668 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
669 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
670 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
671 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
672 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000673 }
674 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100675# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000676 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000677 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000678#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000679 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100680 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000681 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000682 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100683 (unsigned long long) statbuf.st_size,
684 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000685 (unsigned long) statbuf.st_mode,
686 (unsigned long) statbuf.st_uid,
687 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100688 (unsigned long long) statbuf.st_dev,
689 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000690 (unsigned long) statbuf.st_nlink,
691 (unsigned long) major(statbuf.st_rdev),
692 (unsigned long) minor(statbuf.st_rdev),
693 (unsigned long) statbuf.st_atime,
694 (unsigned long) statbuf.st_mtime,
695 (unsigned long) statbuf.st_ctime,
696 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000697 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100698# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000699 if (option_mask32 & OPT_SELINUX)
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200700 printf(" %s\n", scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000701 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000702 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100703# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000704 } else {
705 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000706 struct passwd *pw_ent;
707 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200708
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000709 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000710 pw_ent = getpwuid(statbuf.st_uid);
711
712 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000713 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200714 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200715 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200716 free(linkname);
717 } else {
Sebd2d327d2010-06-12 21:57:50 +0200718 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200719 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000720
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100721 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
722 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
723 (unsigned long long) statbuf.st_size,
724 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000725 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000726 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100727 (unsigned long long) statbuf.st_dev,
728 (unsigned long long) statbuf.st_dev,
729 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000730 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000731 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
732 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000733 (unsigned long) major(statbuf.st_rdev),
734 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000735 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000736 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000737 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000738 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000739 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000740 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000741 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000742 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000743 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100744# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200745 if (option_mask32 & OPT_SELINUX)
746 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100747# endif
Eric Lammerts66be9192010-10-30 02:48:20 +0200748 printf("Access: %s\n", human_time(statbuf.st_atime));
749 printf("Modify: %s\n", human_time(statbuf.st_mtime));
750 printf("Change: %s\n", human_time(statbuf.st_ctime));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000751 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200752#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000753 return 1;
754}
755
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000756int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100757int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000758{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000759 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000760 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100761 int ok;
762 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000763 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000764
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100765 opt_complementary = "-1"; /* min one arg */
Denys Vlasenkoace83302015-10-30 22:10:44 +0100766 opts = getopt32(argv, "tL"
767 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000768 IF_SELINUX("Z")
769 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000770 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100771#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100772 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000773 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100774#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000775#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100776 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000777 selinux_or_die();
778 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100779#endif
780 ok = 1;
781 argv += optind;
782 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000783 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000784
785 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
786}