blob: ddcfcf2d78bd319e7984fdb0c4077ee6a97e2fc1 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00002/*
3 * stat -- display file or file system status
4 *
5 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
6 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
Denis Vlasenko49622d72007-03-10 16:58:49 +00008 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00009 *
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020013 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
Denys Vlasenkoace83302015-10-30 22:10:44 +010015//config:config STAT
16//config: bool "stat"
17//config: default y
18//config: help
19//config: display file or filesystem status.
20//config:
21//config:config FEATURE_STAT_FORMAT
22//config: bool "Enable custom formats (-c)"
23//config: default y
24//config: depends on STAT
25//config: help
26//config: Without this, stat will not support the '-c format' option where
27//config: users can pass a custom format string for output. This adds about
28//config: 7k to a nonstatic build on amd64.
29//config:
30//config:config FEATURE_STAT_FILESYSTEM
31//config: bool "Enable display of filesystem status (-f)"
32//config: default y
33//config: depends on STAT
34//config: select PLATFORM_LINUX # statfs()
35//config: help
36//config: Without this, stat will not support the '-f' option to display
37//config: information about filesystem status.
38
Pere Orga34425382011-03-31 14:43:25 +020039
40//usage:#define stat_trivial_usage
41//usage: "[OPTIONS] FILE..."
42//usage:#define stat_full_usage "\n\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010043//usage: "Display file"
44//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
45//usage: " status\n"
Pere Orga34425382011-03-31 14:43:25 +020046//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010047//usage: "\n -c FMT Use the specified format"
Pere Orga34425382011-03-31 14:43:25 +020048//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +010049//usage: IF_FEATURE_STAT_FILESYSTEM(
Pere Orga34425382011-03-31 14:43:25 +020050//usage: "\n -f Display filesystem status"
Denys Vlasenkoace83302015-10-30 22:10:44 +010051//usage: )
Pere Orga34425382011-03-31 14:43:25 +020052//usage: "\n -L Follow links"
Denys Vlasenkoace83302015-10-30 22:10:44 +010053//usage: "\n -t Terse display"
Pere Orga34425382011-03-31 14:43:25 +020054//usage: IF_SELINUX(
55//usage: "\n -Z Print security context"
56//usage: )
57//usage: IF_FEATURE_STAT_FORMAT(
Denys Vlasenkoace83302015-10-30 22:10:44 +010058//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
Pere Orga34425382011-03-31 14:43:25 +020059//usage: " %a Access rights in octal\n"
60//usage: " %A Access rights in human readable form\n"
61//usage: " %b Number of blocks allocated (see %B)\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010062//usage: " %B Size in bytes of each block reported by %b\n"
Pere Orga34425382011-03-31 14:43:25 +020063//usage: " %d Device number in decimal\n"
64//usage: " %D Device number in hex\n"
65//usage: " %f Raw mode in hex\n"
66//usage: " %F File type\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010067//usage: " %g Group ID\n"
68//usage: " %G Group name\n"
Pere Orga34425382011-03-31 14:43:25 +020069//usage: " %h Number of hard links\n"
70//usage: " %i Inode number\n"
71//usage: " %n File name\n"
72//usage: " %N File name, with -> TARGET if symlink\n"
73//usage: " %o I/O block size\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010074//usage: " %s Total size in bytes\n"
Pere Orga34425382011-03-31 14:43:25 +020075//usage: " %t Major device type in hex\n"
76//usage: " %T Minor device type in hex\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010077//usage: " %u User ID\n"
78//usage: " %U User name\n"
Pere Orga34425382011-03-31 14:43:25 +020079//usage: " %x Time of last access\n"
80//usage: " %X Time of last access as seconds since Epoch\n"
81//usage: " %y Time of last modification\n"
82//usage: " %Y Time of last modification as seconds since Epoch\n"
83//usage: " %z Time of last change\n"
84//usage: " %Z Time of last change as seconds since Epoch\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010085//usage: IF_FEATURE_STAT_FILESYSTEM(
86//usage: "\nFMT sequences for file systems:\n"
Pere Orga34425382011-03-31 14:43:25 +020087//usage: " %a Free blocks available to non-superuser\n"
Denys Vlasenkoace83302015-10-30 22:10:44 +010088//usage: " %b Total data blocks\n"
89//usage: " %c Total file nodes\n"
90//usage: " %d Free file nodes\n"
91//usage: " %f Free blocks\n"
Pere Orga34425382011-03-31 14:43:25 +020092//usage: IF_SELINUX(
93//usage: " %C Security context in selinux\n"
94//usage: )
95//usage: " %i File System ID in hex\n"
96//usage: " %l Maximum length of filenames\n"
97//usage: " %n File name\n"
98//usage: " %s Block size (for faster transfer)\n"
99//usage: " %S Fundamental block size (for block counts)\n"
100//usage: " %t Type in hex\n"
101//usage: " %T Type in human readable form"
102//usage: )
Denys Vlasenkoace83302015-10-30 22:10:44 +0100103//usage: )
Pere Orga34425382011-03-31 14:43:25 +0200104
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000105#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200106#include "common_bufsiz.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000107
Denys Vlasenkoace83302015-10-30 22:10:44 +0100108enum {
109 OPT_TERSE = (1 << 0),
110 OPT_DEREFERENCE = (1 << 1),
111 OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
112 OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
113};
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000114
Denis Vlasenko85c24712008-03-17 09:04:04 +0000115#if ENABLE_FEATURE_STAT_FORMAT
116typedef bool (*statfunc_ptr)(const char *, const char *);
117#else
118typedef bool (*statfunc_ptr)(const char *);
119#endif
120
Denis Vlasenko91e52032007-10-05 20:31:23 +0000121static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000122{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000123 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000124 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000125 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000126 * returned string must start with a consonant.
127 */
128 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
129 if (S_ISDIR(st->st_mode)) return "directory";
130 if (S_ISBLK(st->st_mode)) return "block special file";
131 if (S_ISCHR(st->st_mode)) return "character special file";
132 if (S_ISFIFO(st->st_mode)) return "fifo";
133 if (S_ISLNK(st->st_mode)) return "symbolic link";
134 if (S_ISSOCK(st->st_mode)) return "socket";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200135#ifdef S_TYPEISMQ
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000136 if (S_TYPEISMQ(st)) return "message queue";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200137#endif
138#ifdef S_TYPEISSEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000139 if (S_TYPEISSEM(st)) return "semaphore";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200140#endif
141#ifdef S_TYPEISSHM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000142 if (S_TYPEISSHM(st)) return "shared memory object";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200143#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000144#ifdef S_TYPEISTMO
145 if (S_TYPEISTMO(st)) return "typed memory object";
146#endif
147 return "weird file";
148}
149
Denis Vlasenko91e52032007-10-05 20:31:23 +0000150static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000151{
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000152 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000153 static char *str;
154 str = ctime(&t);
155 str[strlen(str)-1] = '\0';
156 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000157 */
158 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000159
Denis Vlasenko91e52032007-10-05 20:31:23 +0000160 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200161#define buf bb_common_bufsiz1
162 setup_common_bufsiz();
163 strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000164 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000165#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000166}
167
Denys Vlasenkoace83302015-10-30 22:10:44 +0100168#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000169/* Return the type of the specified file system.
170 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
171 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
172 * Still others have neither and have to get by with f_type (Linux).
173 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000174static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000175{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000176 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000177 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000178 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +0000179 } humantypes[] = {
180 { 0xADFF, "affs" },
181 { 0x1Cd1, "devpts" },
182 { 0x137D, "ext" },
183 { 0xEF51, "ext2" },
184 { 0xEF53, "ext2/ext3" },
185 { 0x3153464a, "jfs" },
186 { 0x58465342, "xfs" },
187 { 0xF995E849, "hpfs" },
188 { 0x9660, "isofs" },
189 { 0x4000, "isofs" },
190 { 0x4004, "isofs" },
191 { 0x137F, "minix" },
192 { 0x138F, "minix (30 char.)" },
193 { 0x2468, "minix v2" },
194 { 0x2478, "minix v2 (30 char.)" },
195 { 0x4d44, "msdos" },
196 { 0x4006, "fat" },
197 { 0x564c, "novell" },
198 { 0x6969, "nfs" },
199 { 0x9fa0, "proc" },
200 { 0x517B, "smb" },
201 { 0x012FF7B4, "xenix" },
202 { 0x012FF7B5, "sysv4" },
203 { 0x012FF7B6, "sysv2" },
204 { 0x012FF7B7, "coh" },
205 { 0x00011954, "ufs" },
206 { 0x012FD16D, "xia" },
207 { 0x5346544e, "ntfs" },
208 { 0x1021994, "tmpfs" },
209 { 0x52654973, "reiserfs" },
210 { 0x28cd3d45, "cramfs" },
211 { 0x7275, "romfs" },
212 { 0x858458f6, "romfs" },
213 { 0x73717368, "squashfs" },
214 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000215 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000216 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000217
218 int i;
219
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000220 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000221 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000222 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000223 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000224}
225
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000226/* "man statfs" says that statfsbuf->f_fsid is a mess */
227/* coreutils treats it as an array of ints, most significant first */
228static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
229{
230 const unsigned *p = (const void*) &statfsbuf->f_fsid;
231 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
232 unsigned long long r = 0;
233
234 do
235 r = (r << (sizeof(unsigned)*8)) | *p++;
236 while (--sz > 0);
237 return r;
238}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100239#endif /* FEATURE_STAT_FILESYSTEM */
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000240
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000241#if ENABLE_FEATURE_STAT_FORMAT
242static void strcatc(char *str, char c)
243{
244 int len = strlen(str);
245 str[len++] = c;
246 str[len] = '\0';
247}
248
249static void printfs(char *pformat, const char *msg)
250{
251 strcatc(pformat, 's');
252 printf(pformat, msg);
253}
254
Denys Vlasenkoace83302015-10-30 22:10:44 +0100255#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000256/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100257static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000258 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000259 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000260{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000261 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000262 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000263 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000264 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000265 strcat(pformat, "llx");
266 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000267 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000268 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100269 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000270 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000271 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100272 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000273 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000274 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000275 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100276 strcat(pformat, "llu");
277 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000278 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100279 strcat(pformat, "llu");
280 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000281 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100282 strcat(pformat, "llu");
283 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000284 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000285 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100286 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000287 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100288 strcat(pformat, "llu");
289 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000290 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100291 strcat(pformat, "llu");
292 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
293# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000294 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000295 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100296# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000297 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000298 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000299 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000300 }
301}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100302#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000303
304/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100305static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000306 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000307 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000308{
309#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
310 struct stat *statbuf = (struct stat *) data;
311 struct passwd *pw_ent;
312 struct group *gw_ent;
313
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000314 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000315 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000316 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000317 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000318 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000319 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000320 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000321 return;
Sebd2d327d2010-06-12 21:57:50 +0200322 printf("'%s' -> '%s'", filename, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000323 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000324 } else {
325 printf(pformat, filename);
326 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000327 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100328 strcat(pformat, "llu");
329 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000330 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100331 strcat(pformat, "llx");
332 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000333 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100334 strcat(pformat, "llu");
335 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000336 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000337 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000338 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 +0000339 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000340 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000341 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000342 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000343 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000344 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000345 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000346 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000347 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000348 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000349 } else if (m == 'u') {
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_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000352 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000353 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000354 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000355 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000356 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000357 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000358 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000359 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000360 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000361 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000362 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000363 printf(pformat, (unsigned long) major(statbuf->st_rdev));
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) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000367 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100368 strcat(pformat, "llu");
369 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000370 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000371 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000372 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000373 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100374 strcat(pformat, "llu");
375 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000376 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000377 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000378 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000379 } else if (m == 'x') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000380 printfs(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000381 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000382 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100383 /* note: (unsigned long) would be wrong:
384 * imagine (unsigned long64)int32 */
385 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000386 } else if (m == 'y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000387 printfs(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000388 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000389 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100390 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000391 } else if (m == 'z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000392 printfs(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000393 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000394 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100395 printf(pformat, (long) statbuf->st_ctime);
396# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000397 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000398 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100399# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000400 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000401 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000402 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000403 }
404}
405
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100406static void print_it(const char *masterformat,
407 const char *filename,
408 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000409 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100410 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000411{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000412 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000413 char *format = xstrdup(masterformat);
Maninder Singh97c64912015-05-25 13:46:36 +0200414 /* Add 2 to accommodate our conversion of the stat '%s' format string
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000415 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000416 char *dest = xmalloc(strlen(format) + 2 + 1);
417 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000418
419 b = format;
420 while (b) {
Sebd2d327d2010-06-12 21:57:50 +0200421 /* Each iteration finds next %spec,
422 * prints preceding string and handles found %spec
423 */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000424 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000425 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000426 if (!p) {
Sebd2d327d2010-06-12 21:57:50 +0200427 /* coreutils 6.3 always prints newline at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000428 /*fputs(b, stdout);*/
429 puts(b);
430 break;
431 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000432
Denis Vlasenko91e52032007-10-05 20:31:23 +0000433 /* dest = "%<modifiers>" */
Sebd2d327d2010-06-12 21:57:50 +0200434 len = 1 + strspn(p + 1, "#-+.I 0123456789");
435 memcpy(dest, p, len);
436 dest[len] = '\0';
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000437
Sebd2d327d2010-06-12 21:57:50 +0200438 /* print preceding string */
439 *p = '\0';
440 fputs(b, stdout);
441
442 p += len;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000443 b = p + 1;
444 switch (*p) {
445 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000446 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000447 /* fall through */
448 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000449 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000450 break;
451 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000452 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000453 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000454 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000455 }
456 }
457
458 free(format);
459 free(dest);
460}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100461#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000462
Denys Vlasenkoace83302015-10-30 22:10:44 +0100463#if ENABLE_FEATURE_STAT_FILESYSTEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000464/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000465#if !ENABLE_FEATURE_STAT_FORMAT
466#define do_statfs(filename, format) do_statfs(filename)
467#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000468static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000469{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000470 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000471#if !ENABLE_FEATURE_STAT_FORMAT
472 const char *format;
473#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000474#if ENABLE_SELINUX
475 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000476
477 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000478 if ((option_mask32 & OPT_DEREFERENCE
479 ? lgetfilecon(filename, &scontext)
480 : getfilecon(filename, &scontext)
481 ) < 0
482 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200483 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000484 return 0;
485 }
486 }
487#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000488 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100489 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000490 return 0;
491 }
492
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000493#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000494 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100495# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000496 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000497 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000498 : " File: \"%n\"\n"
499 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000500 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000501 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000502 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100503# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000504 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000505 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000506 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000507 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000508 " File: \"%n\"\n"
509 " ID: %-8i Namelen: %-7l Type: %T\n"
510 "Block size: %-10s\n"
511 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
512 "Inodes: Total: %-10c Free: %d"
513 " S_context: %C\n":
514 " File: \"%n\"\n"
515 " ID: %-8i Namelen: %-7l Type: %T\n"
516 "Block size: %-10s\n"
517 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
518 "Inodes: Total: %-10c Free: %d\n")
519 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100520# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000521 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000522 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000523#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000524 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000525 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000526 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000527 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000528 printf(format,
529 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000530 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000531 statfsbuf.f_namelen);
532
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000533 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100534 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000535 else
536 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
537
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100538# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000539 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100540 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000541 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100542 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
543 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000544 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100545 (unsigned long) statfsbuf.f_bsize,
546 (unsigned long long) statfsbuf.f_blocks,
547 (unsigned long long) statfsbuf.f_bfree,
548 (unsigned long long) statfsbuf.f_bavail,
549 (unsigned long long) statfsbuf.f_files,
550 (unsigned long long) statfsbuf.f_ffree);
551# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000552 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100553 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
554 : (option_mask32 & OPT_SELINUX
555 ? "Block size: %-10lu\n"
556 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
557 "Inodes: Total: %-10llu Free: %llu"
558 "S_context: %C\n"
559 : "Block size: %-10lu\n"
560 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
561 "Inodes: Total: %-10llu Free: %llu\n"
562 )
563 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000564 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100565 (unsigned long) statfsbuf.f_bsize,
566 (unsigned long long) statfsbuf.f_blocks,
567 (unsigned long long) statfsbuf.f_bfree,
568 (unsigned long long) statfsbuf.f_bavail,
569 (unsigned long long) statfsbuf.f_files,
570 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000571 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000572
Denis Vlasenko49622d72007-03-10 16:58:49 +0000573 if (scontext)
574 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100575# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200576#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000577 return 1;
578}
Denys Vlasenkoace83302015-10-30 22:10:44 +0100579#endif /* FEATURE_STAT_FILESYSTEM */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000580
581/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000582#if !ENABLE_FEATURE_STAT_FORMAT
583#define do_stat(filename, format) do_stat(filename)
584#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000585static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000586{
587 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000588#if ENABLE_SELINUX
589 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000590
591 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000592 if ((option_mask32 & OPT_DEREFERENCE
593 ? lgetfilecon(filename, &scontext)
594 : getfilecon(filename, &scontext)
595 ) < 0
596 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200597 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000598 return 0;
599 }
600 }
601#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000602 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100603 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000604 return 0;
605 }
606
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000607#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000608 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100609# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000610 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000611 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000612 } else {
613 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
614 format =
Sebd2d327d2010-06-12 21:57:50 +0200615 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000616 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
617 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
618 " Device type: %t,%T\n"
619 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
620 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
621 } else {
622 format =
Sebd2d327d2010-06-12 21:57:50 +0200623 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000624 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
625 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
626 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
627 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
628 }
629 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100630# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000631 if (option_mask32 & OPT_TERSE) {
632 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100633 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
634 :
635 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
636 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000637 } else {
638 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000639 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100640 " File: %N\n"
641 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
642 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
643 " Device type: %t,%T\n"
644 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
645 " S_Context: %C\n"
646 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
647 :
648 " File: %N\n"
649 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
650 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
651 " Device type: %t,%T\n"
652 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
653 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
654 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000655 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000656 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100657 " File: %N\n"
658 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
659 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
660 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
661 "S_Context: %C\n"
662 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
663 :
664 " File: %N\n"
665 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
666 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
667 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
668 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
669 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000670 }
671 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100672# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000673 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000674 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000675#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000676 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100677 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000678 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000679 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100680 (unsigned long long) statbuf.st_size,
681 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000682 (unsigned long) statbuf.st_mode,
683 (unsigned long) statbuf.st_uid,
684 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100685 (unsigned long long) statbuf.st_dev,
686 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000687 (unsigned long) statbuf.st_nlink,
688 (unsigned long) major(statbuf.st_rdev),
689 (unsigned long) minor(statbuf.st_rdev),
690 (unsigned long) statbuf.st_atime,
691 (unsigned long) statbuf.st_mtime,
692 (unsigned long) statbuf.st_ctime,
693 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000694 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100695# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000696 if (option_mask32 & OPT_SELINUX)
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200697 printf(" %s\n", scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000698 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000699 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100700# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000701 } else {
702 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000703 struct passwd *pw_ent;
704 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200705
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000706 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000707 pw_ent = getpwuid(statbuf.st_uid);
708
709 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000710 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200711 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200712 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200713 free(linkname);
714 } else {
Sebd2d327d2010-06-12 21:57:50 +0200715 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200716 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000717
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100718 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
719 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
720 (unsigned long long) statbuf.st_size,
721 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000722 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000723 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100724 (unsigned long long) statbuf.st_dev,
725 (unsigned long long) statbuf.st_dev,
726 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000727 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000728 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
729 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000730 (unsigned long) major(statbuf.st_rdev),
731 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000732 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000733 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000734 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000735 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000736 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000737 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000738 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000739 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000740 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100741# if ENABLE_SELINUX
Michael Gernoth1b487ea2014-06-27 14:08:29 +0200742 if (option_mask32 & OPT_SELINUX)
743 printf(" S_Context: %s\n", scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100744# endif
Eric Lammerts66be9192010-10-30 02:48:20 +0200745 printf("Access: %s\n", human_time(statbuf.st_atime));
746 printf("Modify: %s\n", human_time(statbuf.st_mtime));
747 printf("Change: %s\n", human_time(statbuf.st_ctime));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000748 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200749#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000750 return 1;
751}
752
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000753int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100754int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000755{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000756 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000757 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100758 int ok;
759 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000760 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000761
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100762 opt_complementary = "-1"; /* min one arg */
Denys Vlasenkoace83302015-10-30 22:10:44 +0100763 opts = getopt32(argv, "tL"
764 IF_FEATURE_STAT_FILESYSTEM("f")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000765 IF_SELINUX("Z")
766 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000767 );
Denys Vlasenkoace83302015-10-30 22:10:44 +0100768#if ENABLE_FEATURE_STAT_FILESYSTEM
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100769 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000770 statfunc = do_statfs;
Denys Vlasenkoace83302015-10-30 22:10:44 +0100771#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000772#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100773 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000774 selinux_or_die();
775 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100776#endif
777 ok = 1;
778 argv += optind;
779 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000780 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000781
782 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
783}