blob: c8677ebaa93b71705b510bbff8dd04296c7c4b9d [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 */
Pere Orga34425382011-03-31 14:43:25 +020015
16//usage:#define stat_trivial_usage
17//usage: "[OPTIONS] FILE..."
18//usage:#define stat_full_usage "\n\n"
19//usage: "Display file (default) or filesystem status\n"
Pere Orga34425382011-03-31 14:43:25 +020020//usage: IF_FEATURE_STAT_FORMAT(
21//usage: "\n -c fmt Use the specified format"
22//usage: )
23//usage: "\n -f Display filesystem status"
24//usage: "\n -L Follow links"
25//usage: "\n -t Display info in terse form"
26//usage: IF_SELINUX(
27//usage: "\n -Z Print security context"
28//usage: )
29//usage: IF_FEATURE_STAT_FORMAT(
30//usage: "\n\nValid format sequences for files:\n"
31//usage: " %a Access rights in octal\n"
32//usage: " %A Access rights in human readable form\n"
33//usage: " %b Number of blocks allocated (see %B)\n"
34//usage: " %B The size in bytes of each block reported by %b\n"
35//usage: " %d Device number in decimal\n"
36//usage: " %D Device number in hex\n"
37//usage: " %f Raw mode in hex\n"
38//usage: " %F File type\n"
39//usage: " %g Group ID of owner\n"
40//usage: " %G Group name of owner\n"
41//usage: " %h Number of hard links\n"
42//usage: " %i Inode number\n"
43//usage: " %n File name\n"
44//usage: " %N File name, with -> TARGET if symlink\n"
45//usage: " %o I/O block size\n"
46//usage: " %s Total size, in bytes\n"
47//usage: " %t Major device type in hex\n"
48//usage: " %T Minor device type in hex\n"
49//usage: " %u User ID of owner\n"
50//usage: " %U User name of owner\n"
51//usage: " %x Time of last access\n"
52//usage: " %X Time of last access as seconds since Epoch\n"
53//usage: " %y Time of last modification\n"
54//usage: " %Y Time of last modification as seconds since Epoch\n"
55//usage: " %z Time of last change\n"
56//usage: " %Z Time of last change as seconds since Epoch\n"
57//usage: "\nValid format sequences for file systems:\n"
58//usage: " %a Free blocks available to non-superuser\n"
59//usage: " %b Total data blocks in file system\n"
60//usage: " %c Total file nodes in file system\n"
61//usage: " %d Free file nodes in file system\n"
62//usage: " %f Free blocks in file system\n"
63//usage: IF_SELINUX(
64//usage: " %C Security context in selinux\n"
65//usage: )
66//usage: " %i File System ID in hex\n"
67//usage: " %l Maximum length of filenames\n"
68//usage: " %n File name\n"
69//usage: " %s Block size (for faster transfer)\n"
70//usage: " %S Fundamental block size (for block counts)\n"
71//usage: " %t Type in hex\n"
72//usage: " %T Type in human readable form"
73//usage: )
74
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000075#include "libbb.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000076
Denis Vlasenko91e52032007-10-05 20:31:23 +000077#define OPT_FILESYS (1 << 0)
78#define OPT_TERSE (1 << 1)
79#define OPT_DEREFERENCE (1 << 2)
80#define OPT_SELINUX (1 << 3)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000081
Denis Vlasenko85c24712008-03-17 09:04:04 +000082#if ENABLE_FEATURE_STAT_FORMAT
83typedef bool (*statfunc_ptr)(const char *, const char *);
84#else
85typedef bool (*statfunc_ptr)(const char *);
86#endif
87
Denis Vlasenko91e52032007-10-05 20:31:23 +000088static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000089{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000090 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000091 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000093 * returned string must start with a consonant.
94 */
95 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
96 if (S_ISDIR(st->st_mode)) return "directory";
97 if (S_ISBLK(st->st_mode)) return "block special file";
98 if (S_ISCHR(st->st_mode)) return "character special file";
99 if (S_ISFIFO(st->st_mode)) return "fifo";
100 if (S_ISLNK(st->st_mode)) return "symbolic link";
101 if (S_ISSOCK(st->st_mode)) return "socket";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200102#ifdef S_TYPEISMQ
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000103 if (S_TYPEISMQ(st)) return "message queue";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200104#endif
105#ifdef S_TYPEISSEM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000106 if (S_TYPEISSEM(st)) return "semaphore";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200107#endif
108#ifdef S_TYPEISSHM
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000109 if (S_TYPEISSHM(st)) return "shared memory object";
Tias Gunsa1ec8412012-06-03 16:43:06 +0200110#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000111#ifdef S_TYPEISTMO
112 if (S_TYPEISTMO(st)) return "typed memory object";
113#endif
114 return "weird file";
115}
116
Denis Vlasenko91e52032007-10-05 20:31:23 +0000117static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000118{
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000119 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000120 static char *str;
121 str = ctime(&t);
122 str[strlen(str)-1] = '\0';
123 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000124 */
125 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000126
Denis Vlasenko91e52032007-10-05 20:31:23 +0000127 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
128#define buf bb_common_bufsiz1
129
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000130 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
131 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +0000132#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000133}
134
135/* Return the type of the specified file system.
136 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
137 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
138 * Still others have neither and have to get by with f_type (Linux).
139 */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000140static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000141{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000142 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000143 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000144 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +0000145 } humantypes[] = {
146 { 0xADFF, "affs" },
147 { 0x1Cd1, "devpts" },
148 { 0x137D, "ext" },
149 { 0xEF51, "ext2" },
150 { 0xEF53, "ext2/ext3" },
151 { 0x3153464a, "jfs" },
152 { 0x58465342, "xfs" },
153 { 0xF995E849, "hpfs" },
154 { 0x9660, "isofs" },
155 { 0x4000, "isofs" },
156 { 0x4004, "isofs" },
157 { 0x137F, "minix" },
158 { 0x138F, "minix (30 char.)" },
159 { 0x2468, "minix v2" },
160 { 0x2478, "minix v2 (30 char.)" },
161 { 0x4d44, "msdos" },
162 { 0x4006, "fat" },
163 { 0x564c, "novell" },
164 { 0x6969, "nfs" },
165 { 0x9fa0, "proc" },
166 { 0x517B, "smb" },
167 { 0x012FF7B4, "xenix" },
168 { 0x012FF7B5, "sysv4" },
169 { 0x012FF7B6, "sysv2" },
170 { 0x012FF7B7, "coh" },
171 { 0x00011954, "ufs" },
172 { 0x012FD16D, "xia" },
173 { 0x5346544e, "ntfs" },
174 { 0x1021994, "tmpfs" },
175 { 0x52654973, "reiserfs" },
176 { 0x28cd3d45, "cramfs" },
177 { 0x7275, "romfs" },
178 { 0x858458f6, "romfs" },
179 { 0x73717368, "squashfs" },
180 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000181 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000182 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000183
184 int i;
185
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000186 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000187 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000188 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000189 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000190}
191
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000192/* "man statfs" says that statfsbuf->f_fsid is a mess */
193/* coreutils treats it as an array of ints, most significant first */
194static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
195{
196 const unsigned *p = (const void*) &statfsbuf->f_fsid;
197 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
198 unsigned long long r = 0;
199
200 do
201 r = (r << (sizeof(unsigned)*8)) | *p++;
202 while (--sz > 0);
203 return r;
204}
205
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000206#if ENABLE_FEATURE_STAT_FORMAT
207static void strcatc(char *str, char c)
208{
209 int len = strlen(str);
210 str[len++] = c;
211 str[len] = '\0';
212}
213
214static void printfs(char *pformat, const char *msg)
215{
216 strcatc(pformat, 's');
217 printf(pformat, msg);
218}
219
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000220/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100221static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000222 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000223 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000224{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000225 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000226 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000227 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000228 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000229 strcat(pformat, "llx");
230 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000231 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000232 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100233 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000234 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000235 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100236 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000237 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000238 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000239 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100240 strcat(pformat, "llu");
241 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000242 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100243 strcat(pformat, "llu");
244 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000245 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100246 strcat(pformat, "llu");
247 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000248 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000249 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100250 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000251 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100252 strcat(pformat, "llu");
253 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000254 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100255 strcat(pformat, "llu");
256 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
257# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000258 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000259 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100260# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000261 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000262 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000263 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000264 }
265}
266
267/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100268static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000269 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000270 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000271{
272#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
273 struct stat *statbuf = (struct stat *) data;
274 struct passwd *pw_ent;
275 struct group *gw_ent;
276
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000277 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000278 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000279 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000280 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000281 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000282 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000283 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000284 return;
Sebd2d327d2010-06-12 21:57:50 +0200285 printf("'%s' -> '%s'", filename, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000286 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000287 } else {
288 printf(pformat, filename);
289 }
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) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000293 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100294 strcat(pformat, "llx");
295 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000296 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100297 strcat(pformat, "llu");
298 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000299 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000300 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000301 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 +0000302 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000303 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000304 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000305 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000306 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000307 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000308 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000309 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000310 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000311 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000312 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000313 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000314 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000315 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000316 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000317 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000318 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000319 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000320 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000321 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000322 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000323 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000324 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000325 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000326 printf(pformat, (unsigned long) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000327 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000328 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000329 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000330 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100331 strcat(pformat, "llu");
332 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000333 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000334 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000335 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000336 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100337 strcat(pformat, "llu");
338 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000339 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000340 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000341 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000342 } else if (m == 'x') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000343 printfs(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000344 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000345 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100346 /* note: (unsigned long) would be wrong:
347 * imagine (unsigned long64)int32 */
348 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000349 } else if (m == 'y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000350 printfs(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000351 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000352 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100353 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000354 } else if (m == 'z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000355 printfs(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000356 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000357 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100358 printf(pformat, (long) statbuf->st_ctime);
359# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000360 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000361 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100362# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000363 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000364 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000365 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000366 }
367}
368
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100369static void print_it(const char *masterformat,
370 const char *filename,
371 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000372 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100373 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000374{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000375 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000376 char *format = xstrdup(masterformat);
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000377 /* Add 2 to accomodate our conversion of the stat '%s' format string
378 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000379 char *dest = xmalloc(strlen(format) + 2 + 1);
380 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000381
382 b = format;
383 while (b) {
Sebd2d327d2010-06-12 21:57:50 +0200384 /* Each iteration finds next %spec,
385 * prints preceding string and handles found %spec
386 */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000387 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000388 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000389 if (!p) {
Sebd2d327d2010-06-12 21:57:50 +0200390 /* coreutils 6.3 always prints newline at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000391 /*fputs(b, stdout);*/
392 puts(b);
393 break;
394 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000395
Denis Vlasenko91e52032007-10-05 20:31:23 +0000396 /* dest = "%<modifiers>" */
Sebd2d327d2010-06-12 21:57:50 +0200397 len = 1 + strspn(p + 1, "#-+.I 0123456789");
398 memcpy(dest, p, len);
399 dest[len] = '\0';
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000400
Sebd2d327d2010-06-12 21:57:50 +0200401 /* print preceding string */
402 *p = '\0';
403 fputs(b, stdout);
404
405 p += len;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000406 b = p + 1;
407 switch (*p) {
408 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000409 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000410 /* fall through */
411 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000412 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000413 break;
414 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000415 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000416 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000417 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000418 }
419 }
420
421 free(format);
422 free(dest);
423}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100424#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000425
426/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000427#if !ENABLE_FEATURE_STAT_FORMAT
428#define do_statfs(filename, format) do_statfs(filename)
429#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000430static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000431{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000432 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000433#if !ENABLE_FEATURE_STAT_FORMAT
434 const char *format;
435#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000436#if ENABLE_SELINUX
437 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000438
439 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000440 if ((option_mask32 & OPT_DEREFERENCE
441 ? lgetfilecon(filename, &scontext)
442 : getfilecon(filename, &scontext)
443 ) < 0
444 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200445 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000446 return 0;
447 }
448 }
449#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000450 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100451 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000452 return 0;
453 }
454
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000455#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000456 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100457# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000458 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000459 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000460 : " File: \"%n\"\n"
461 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000462 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000463 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000464 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100465# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000466 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000467 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000468 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000469 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000470 " File: \"%n\"\n"
471 " ID: %-8i Namelen: %-7l Type: %T\n"
472 "Block size: %-10s\n"
473 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
474 "Inodes: Total: %-10c Free: %d"
475 " S_context: %C\n":
476 " File: \"%n\"\n"
477 " ID: %-8i Namelen: %-7l Type: %T\n"
478 "Block size: %-10s\n"
479 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
480 "Inodes: Total: %-10c Free: %d\n")
481 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100482# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000483 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000484 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000485#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000486 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000487 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000488 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000489 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000490 printf(format,
491 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000492 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000493 statfsbuf.f_namelen);
494
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000495 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100496 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000497 else
498 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
499
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100500# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000501 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100502 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000503 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100504 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
505 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000506 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100507 (unsigned long) statfsbuf.f_bsize,
508 (unsigned long long) statfsbuf.f_blocks,
509 (unsigned long long) statfsbuf.f_bfree,
510 (unsigned long long) statfsbuf.f_bavail,
511 (unsigned long long) statfsbuf.f_files,
512 (unsigned long long) statfsbuf.f_ffree);
513# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000514 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100515 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
516 : (option_mask32 & OPT_SELINUX
517 ? "Block size: %-10lu\n"
518 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
519 "Inodes: Total: %-10llu Free: %llu"
520 "S_context: %C\n"
521 : "Block size: %-10lu\n"
522 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
523 "Inodes: Total: %-10llu Free: %llu\n"
524 )
525 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000526 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100527 (unsigned long) statfsbuf.f_bsize,
528 (unsigned long long) statfsbuf.f_blocks,
529 (unsigned long long) statfsbuf.f_bfree,
530 (unsigned long long) statfsbuf.f_bavail,
531 (unsigned long long) statfsbuf.f_files,
532 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000533 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000534
Denis Vlasenko49622d72007-03-10 16:58:49 +0000535 if (scontext)
536 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100537# endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200538#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000539 return 1;
540}
541
542/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000543#if !ENABLE_FEATURE_STAT_FORMAT
544#define do_stat(filename, format) do_stat(filename)
545#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000546static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000547{
548 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000549#if ENABLE_SELINUX
550 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000551
552 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000553 if ((option_mask32 & OPT_DEREFERENCE
554 ? lgetfilecon(filename, &scontext)
555 : getfilecon(filename, &scontext)
556 ) < 0
557 ) {
Denys Vlasenko93710432012-09-27 15:35:10 +0200558 bb_simple_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000559 return 0;
560 }
561 }
562#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000563 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100564 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000565 return 0;
566 }
567
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000568#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000569 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100570# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000571 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000572 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000573 } else {
574 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
575 format =
Sebd2d327d2010-06-12 21:57:50 +0200576 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000577 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
578 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
579 " Device type: %t,%T\n"
580 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
581 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
582 } else {
583 format =
Sebd2d327d2010-06-12 21:57:50 +0200584 " File: %N\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000585 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
586 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
587 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
588 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
589 }
590 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100591# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000592 if (option_mask32 & OPT_TERSE) {
593 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100594 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
595 :
596 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
597 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000598 } else {
599 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000600 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100601 " File: %N\n"
602 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
603 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
604 " Device type: %t,%T\n"
605 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
606 " S_Context: %C\n"
607 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
608 :
609 " File: %N\n"
610 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
611 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
612 " Device type: %t,%T\n"
613 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
614 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
615 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000616 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000617 format = (option_mask32 & OPT_SELINUX ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100618 " File: %N\n"
619 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
620 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
621 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
622 "S_Context: %C\n"
623 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
624 :
625 " File: %N\n"
626 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
627 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
628 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
629 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
630 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000631 }
632 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100633# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000634 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000635 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000636#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000637 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100638 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000639 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000640 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100641 (unsigned long long) statbuf.st_size,
642 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000643 (unsigned long) statbuf.st_mode,
644 (unsigned long) statbuf.st_uid,
645 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100646 (unsigned long long) statbuf.st_dev,
647 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000648 (unsigned long) statbuf.st_nlink,
649 (unsigned long) major(statbuf.st_rdev),
650 (unsigned long) minor(statbuf.st_rdev),
651 (unsigned long) statbuf.st_atime,
652 (unsigned long) statbuf.st_mtime,
653 (unsigned long) statbuf.st_ctime,
654 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000655 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100656# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000657 if (option_mask32 & OPT_SELINUX)
Denis Vlasenko49622d72007-03-10 16:58:49 +0000658 printf(" %lc\n", *scontext);
659 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000660 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100661# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000662 } else {
663 char *linkname = NULL;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000664 struct passwd *pw_ent;
665 struct group *gw_ent;
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200666
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000667 gw_ent = getgrgid(statbuf.st_gid);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000668 pw_ent = getpwuid(statbuf.st_uid);
669
670 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000671 linkname = xmalloc_readlink_or_warn(filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200672 if (linkname) {
Sebd2d327d2010-06-12 21:57:50 +0200673 printf(" File: '%s' -> '%s'\n", filename, linkname);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200674 free(linkname);
675 } else {
Sebd2d327d2010-06-12 21:57:50 +0200676 printf(" File: '%s'\n", filename);
Alexander Shishkina7027bf2010-10-21 00:24:05 +0200677 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000678
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100679 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
680 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
681 (unsigned long long) statbuf.st_size,
682 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000683 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000684 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100685 (unsigned long long) statbuf.st_dev,
686 (unsigned long long) statbuf.st_dev,
687 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000688 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000689 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
690 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000691 (unsigned long) major(statbuf.st_rdev),
692 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000693 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000694 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000695 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000696 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000697 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000698 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000699 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000700 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000701 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100702# if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000703 printf(" S_Context: %lc\n", *scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100704# endif
Eric Lammerts66be9192010-10-30 02:48:20 +0200705 printf("Access: %s\n", human_time(statbuf.st_atime));
706 printf("Modify: %s\n", human_time(statbuf.st_mtime));
707 printf("Change: %s\n", human_time(statbuf.st_ctime));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000708 }
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200709#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000710 return 1;
711}
712
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000713int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100714int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000715{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000716 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000717 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100718 int ok;
719 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000720 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000721
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100722 opt_complementary = "-1"; /* min one arg */
723 opts = getopt32(argv, "ftL"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000724 IF_SELINUX("Z")
725 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000726 );
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100727 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000728 statfunc = do_statfs;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000729#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100730 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000731 selinux_or_die();
732 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100733#endif
734 ok = 1;
735 argv += optind;
736 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000737 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000738
739 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
740}