blob: 7c56317649a0ef6554639c594fae9e1b08481f54 [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 *
Bernhard Reutner-Fischere11a01c2006-04-05 17:19:37 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
15
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000016#include "busybox.h"
17
18/* vars to control behavior */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000019#define OPT_FILESYS (1<<0)
20#define OPT_TERSE (1<<1)
21#define OPT_DEREFERENCE (1<<2)
22#define OPT_SELINUX (1<<3)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000023
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000024static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
25
26static char const * file_type(struct stat const *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000027{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000029 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000031 * returned string must start with a consonant.
32 */
33 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
34 if (S_ISDIR(st->st_mode)) return "directory";
35 if (S_ISBLK(st->st_mode)) return "block special file";
36 if (S_ISCHR(st->st_mode)) return "character special file";
37 if (S_ISFIFO(st->st_mode)) return "fifo";
38 if (S_ISLNK(st->st_mode)) return "symbolic link";
39 if (S_ISSOCK(st->st_mode)) return "socket";
40 if (S_TYPEISMQ(st)) return "message queue";
41 if (S_TYPEISSEM(st)) return "semaphore";
42 if (S_TYPEISSHM(st)) return "shared memory object";
43#ifdef S_TYPEISTMO
44 if (S_TYPEISTMO(st)) return "typed memory object";
45#endif
46 return "weird file";
47}
48
49static char const *human_time(time_t t)
50{
Denis Vlasenko5d148e22006-11-21 00:12:09 +000051 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000052 static char *str;
53 str = ctime(&t);
54 str[strlen(str)-1] = '\0';
55 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +000056 */
57 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000058
Denis Vlasenko5d148e22006-11-21 00:12:09 +000059 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
60 return buf;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000061}
62
63/* Return the type of the specified file system.
64 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
65 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
66 * Still others have neither and have to get by with f_type (Linux).
67 */
68static char const *human_fstype(long f_type)
69{
Mike Frysinger408ae212005-04-24 04:11:44 +000070 int i;
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000071 static const struct types {
Mike Frysinger408ae212005-04-24 04:11:44 +000072 long type;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000073 const char * const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +000074 } humantypes[] = {
75 { 0xADFF, "affs" },
76 { 0x1Cd1, "devpts" },
77 { 0x137D, "ext" },
78 { 0xEF51, "ext2" },
79 { 0xEF53, "ext2/ext3" },
80 { 0x3153464a, "jfs" },
81 { 0x58465342, "xfs" },
82 { 0xF995E849, "hpfs" },
83 { 0x9660, "isofs" },
84 { 0x4000, "isofs" },
85 { 0x4004, "isofs" },
86 { 0x137F, "minix" },
87 { 0x138F, "minix (30 char.)" },
88 { 0x2468, "minix v2" },
89 { 0x2478, "minix v2 (30 char.)" },
90 { 0x4d44, "msdos" },
91 { 0x4006, "fat" },
92 { 0x564c, "novell" },
93 { 0x6969, "nfs" },
94 { 0x9fa0, "proc" },
95 { 0x517B, "smb" },
96 { 0x012FF7B4, "xenix" },
97 { 0x012FF7B5, "sysv4" },
98 { 0x012FF7B6, "sysv2" },
99 { 0x012FF7B7, "coh" },
100 { 0x00011954, "ufs" },
101 { 0x012FD16D, "xia" },
102 { 0x5346544e, "ntfs" },
103 { 0x1021994, "tmpfs" },
104 { 0x52654973, "reiserfs" },
105 { 0x28cd3d45, "cramfs" },
106 { 0x7275, "romfs" },
107 { 0x858458f6, "romfs" },
108 { 0x73717368, "squashfs" },
109 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000110 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000111 };
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000112 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000113 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000114 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000115 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000116}
117
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000118#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000119/* print statfs info */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000120static void print_statfs(char *pformat, const size_t buf_len, const char m,
121 const char * const filename, void const *data
122 USE_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000123{
124 struct statfs const *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000125 if (m == 'n') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000126 strncat(pformat, "s", buf_len);
127 printf(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000128 } else if (m == 'i') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000129 strncat(pformat, "Lx", buf_len);
130 printf(pformat, statfsbuf->f_fsid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000131 } else if (m == 'l') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000132 strncat(pformat, "lu", buf_len);
133 printf(pformat, statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000134 } else if (m == 't') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000135 strncat(pformat, "lx", buf_len);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000136 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv */
137 } else if (m == 'T') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000138 strncat(pformat, "s", buf_len);
139 printf(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000140 } else if (m == 'b') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000141 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000142 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000143 } else if (m == 'f') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000144 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000145 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000146 } else if (m == 'a') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000147 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000148 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000149 } else if (m == 's' || m == 'S') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000150 strncat(pformat, "lu", buf_len);
151 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000152 } else if (m == 'c') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000153 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000154 printf(pformat, (intmax_t) (statfsbuf->f_files));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000155 } else if (m == 'd') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000156 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000157 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000158#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000159 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
160 strncat(pformat, "s", buf_len);
161 printf(scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000162#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000163 } else {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000164 strncat(pformat, "c", buf_len);
165 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000166 }
167}
168
169/* print stat info */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000170static void print_stat(char *pformat, const size_t buf_len, const char m,
171 const char * const filename, void const *data
Denis Vlasenko49622d72007-03-10 16:58:49 +0000172 USE_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000173{
174#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
175 struct stat *statbuf = (struct stat *) data;
176 struct passwd *pw_ent;
177 struct group *gw_ent;
178
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000179 if (m == 'n') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000180 strncat(pformat, "s", buf_len);
181 printf(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000182 } else if (m == 'N') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000183 strncat(pformat, "s", buf_len);
184 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000185 char *linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000186 if (linkname == NULL) {
187 bb_perror_msg("cannot read symbolic link '%s'", filename);
188 return;
189 }
190 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
191 printf(pformat, filename);
192 printf(" -> ");
193 printf(pformat, linkname);
194 } else {
195 printf(pformat, filename);
196 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000197 } else if (m == 'd') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000198 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000199 printf(pformat, (uintmax_t) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000200 } else if (m == 'D') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000201 strncat(pformat, "jx", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000202 printf(pformat, (uintmax_t) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000203 } else if (m == 'i') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000204 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000205 printf(pformat, (uintmax_t) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000206 } else if (m == 'a') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000207 strncat(pformat, "lo", buf_len);
208 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000209 } else if (m == 'A') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000210 strncat(pformat, "s", buf_len);
211 printf(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000212 } else if (m == 'f') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000213 strncat(pformat, "lx", buf_len);
214 printf(pformat, (unsigned long int) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000215 } else if (m == 'F') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000216 strncat(pformat, "s", buf_len);
217 printf(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000218 } else if (m == 'h') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000219 strncat(pformat, "lu", buf_len);
220 printf(pformat, (unsigned long int) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000221 } else if (m == 'u') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000222 strncat(pformat, "lu", buf_len);
223 printf(pformat, (unsigned long int) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000224 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000225 strncat(pformat, "s", buf_len);
226 setpwent();
227 pw_ent = getpwuid(statbuf->st_uid);
228 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000229 } else if (m == 'g') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000230 strncat(pformat, "lu", buf_len);
231 printf(pformat, (unsigned long int) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000232 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000233 strncat(pformat, "s", buf_len);
234 setgrent();
235 gw_ent = getgrgid(statbuf->st_gid);
236 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000237 } else if (m == 't') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000238 strncat(pformat, "lx", buf_len);
239 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000240 } else if (m == 'T') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000241 strncat(pformat, "lx", buf_len);
242 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000243 } else if (m == 's') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000244 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000245 printf(pformat, (uintmax_t) (statbuf->st_size));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000246 } else if (m == 'B') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000247 strncat(pformat, "lu", buf_len);
248 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000249 } else if (m == 'b') {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000250 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000251 printf(pformat, (uintmax_t) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000252 } else if (m == 'o') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000253 strncat(pformat, "lu", buf_len);
254 printf(pformat, (unsigned long int) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000255 } else if (m == 'x') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000256 strncat(pformat, "s", buf_len);
257 printf(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000258 } else if (m == 'X') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000259 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
260 printf(pformat, (unsigned long int) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000261 } else if (m == 'y') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000262 strncat(pformat, "s", buf_len);
263 printf(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000264 } else if (m == 'Y') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000265 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
266 printf(pformat, (unsigned long int) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000267 } else if (m == 'z') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000268 strncat(pformat, "s", buf_len);
269 printf(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000270 } else if (m == 'Z') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000271 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
272 printf(pformat, (unsigned long int) statbuf->st_ctime);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000273#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000274 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
275 strncat(pformat, "s", buf_len);
276 printf(pformat, scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000277#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000278 } else {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000279 strncat(pformat, "c", buf_len);
280 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000281 }
282}
283
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000284static void print_it(char const *masterformat, char const *filename,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000285 void (*print_func) (char *, size_t, char, char const *, void const *
286 USE_SELINUX(, security_context_t scontext)),
287 void const *data USE_SELINUX(, security_context_t scontext) )
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000288{
289 char *b;
290
291 /* create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000292 char *format = xstrdup(masterformat);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000293
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000294 /* Add 2 to accomodate our conversion of the stat '%s' format string
295 * to the printf '%llu' one. */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000296 size_t n_alloc = strlen(format) + 2 + 1;
297 char *dest = xmalloc(n_alloc);
298
299 b = format;
300 while (b) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000301 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000302 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000303 if (!p) {
304 /* coreutils 6.3 always print <cr> at the end */
305 /*fputs(b, stdout);*/
306 puts(b);
307 break;
308 }
309 *p++ = '\0';
310 fputs(b, stdout);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000311
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000312 len = strspn(p, "#-+.I 0123456789");
313 dest[0] = '%';
314 memcpy(dest + 1, p, len);
315 dest[1 + len] = 0;
316 p += len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000317
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000318 b = p + 1;
319 switch (*p) {
320 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000321 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000322 /* fall through */
323 case '%':
324 putchar('%');
325 break;
326 default:
Denis Vlasenko49622d72007-03-10 16:58:49 +0000327 print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000328 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000329 }
330 }
331
332 free(format);
333 free(dest);
334}
335#endif
336
337/* Stat the file system and print what we find. */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000338static bool do_statfs(char const *filename, char const *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000339{
340 struct statfs statfsbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000341#if ENABLE_SELINUX
342 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000343
344 if (option_mask32 & OPT_SELINUX) {
345 if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
Denis Vlasenko49622d72007-03-10 16:58:49 +0000346 getfilecon(filename, scontext))< 0) {
347 bb_perror_msg(filename);
348 return 0;
349 }
350 }
351#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000352 if (statfs(filename, &statfsbuf) != 0) {
353 bb_perror_msg("cannot read file system information for '%s'", filename);
354 return 0;
355 }
356
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000357#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000358 if (format == NULL)
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000359#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000360 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000361 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000362 : " File: \"%n\"\n"
363 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000364 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000365 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000366 "Inodes: Total: %-10c Free: %d");
Denis Vlasenko49622d72007-03-10 16:58:49 +0000367 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000368#else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000369 format = (option_mask32 & OPT_TERSE
370 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000371 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000372 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000373 " File: \"%n\"\n"
374 " ID: %-8i Namelen: %-7l Type: %T\n"
375 "Block size: %-10s\n"
376 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
377 "Inodes: Total: %-10c Free: %d"
378 " S_context: %C\n":
379 " File: \"%n\"\n"
380 " ID: %-8i Namelen: %-7l Type: %T\n"
381 "Block size: %-10s\n"
382 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
383 "Inodes: Total: %-10c Free: %d\n")
384 );
385 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
386#endif /* SELINUX */
387#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000388 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000389 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000390 : " File: \"%s\"\n"
391 " ID: %-8Lx Namelen: %-7lu ");
392 printf(format,
393 filename,
394 statfsbuf.f_fsid,
395 statfsbuf.f_namelen);
396
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000397 if (option_mask32 & OPT_TERSE)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000398 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
399 else
400 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
401
Denis Vlasenko49622d72007-03-10 16:58:49 +0000402#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000403 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000404 ? "%lu %ld %ld %ld %ld %ld\n"
405 : "Block size: %-10lu\n"
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000406 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
407 "Inodes: Total: %-10jd Free: %jd\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000408 printf(format,
409 (unsigned long int) (statfsbuf.f_bsize),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000410 (intmax_t) (statfsbuf.f_blocks),
411 (intmax_t) (statfsbuf.f_bfree),
412 (intmax_t) (statfsbuf.f_bavail),
413 (intmax_t) (statfsbuf.f_files),
414 (intmax_t) (statfsbuf.f_ffree));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000415#else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000416 format = (option_mask32 & OPT_TERSE
417 ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000418 "%lu %ld %ld %ld %ld %ld\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000419 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000420 "Block size: %-10lu\n"
421 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
422 "Inodes: Total: %-10jd Free: %jd"
423 "S_context: %C\n":
424 "Block size: %-10lu\n"
425 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
426 "Inodes: Total: %-10jd Free: %jd\n"));
427 printf(format,
428 (unsigned long int) (statfsbuf.f_bsize),
429 (intmax_t) (statfsbuf.f_blocks),
430 (intmax_t) (statfsbuf.f_bfree),
431 (intmax_t) (statfsbuf.f_bavail),
432 (intmax_t) (statfsbuf.f_files),
433 (intmax_t) (statfsbuf.f_ffree),
434 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000435
Denis Vlasenko49622d72007-03-10 16:58:49 +0000436 if (scontext)
437 freecon(scontext);
438#endif
439#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000440 return 1;
441}
442
443/* stat the file and print what we find */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000444static bool do_stat(char const *filename, char const *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000445{
446 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000447#if ENABLE_SELINUX
448 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000449
450 if (option_mask32 & OPT_SELINUX) {
451 if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
Denis Vlasenko49622d72007-03-10 16:58:49 +0000452 getfilecon(filename, scontext))< 0) {
453 bb_perror_msg (filename);
454 return 0;
455 }
456 }
457#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000458 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000459 bb_perror_msg("cannot stat '%s'", filename);
460 return 0;
461 }
462
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000463#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000464 if (format == NULL) {
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000465#if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000466 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000467 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000468 } else {
469 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
470 format =
471 " File: \"%N\"\n"
472 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
473 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
474 " Device type: %t,%T\n"
475 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
476 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
477 } else {
478 format =
479 " File: \"%N\"\n"
480 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
481 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
482 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
483 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
484 }
485 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000486#else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000487 if (option_mask32 & OPT_TERSE) {
488 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000489 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
490 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
491 } else {
492 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000493 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000494 " File: \"%N\"\n"
495 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
496 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
497 " Device type: %t,%T\n"
498 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
499 " S_Context: %C\n"
500 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
501 " File: \"%N\"\n"
502 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
503 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
504 " Device type: %t,%T\n"
505 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
506 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
507 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000508 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000509 " File: \"%N\"\n"
510 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
511 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
512 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
513 "S_Context: %C\n"
514 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
515 " File: \"%N\"\n"
516 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
517 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
518 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
519 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
520 }
521 }
522#endif
523 }
524 print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
525#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000526 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000527 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
528 SKIP_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000529 filename,
530 (uintmax_t) (statbuf.st_size),
531 (uintmax_t) statbuf.st_blocks,
532 (unsigned long int) statbuf.st_mode,
533 (unsigned long int) statbuf.st_uid,
534 (unsigned long int) statbuf.st_gid,
535 (uintmax_t) statbuf.st_dev,
536 (uintmax_t) statbuf.st_ino,
537 (unsigned long int) statbuf.st_nlink,
538 (unsigned long int) major(statbuf.st_rdev),
539 (unsigned long int) minor(statbuf.st_rdev),
540 (unsigned long int) statbuf.st_atime,
541 (unsigned long int) statbuf.st_mtime,
542 (unsigned long int) statbuf.st_ctime,
543 (unsigned long int) statbuf.st_blksize
544 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000545#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000546 if (option_mask32 & OPT_SELINUX)
Denis Vlasenko49622d72007-03-10 16:58:49 +0000547 printf(" %lc\n", *scontext);
548 else
549 putchar('\n');
550#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000551 } else {
552 char *linkname = NULL;
553
554 struct passwd *pw_ent;
555 struct group *gw_ent;
556 setgrent();
557 gw_ent = getgrgid(statbuf.st_gid);
558 setpwent();
559 pw_ent = getpwuid(statbuf.st_uid);
560
561 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000562 linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000563 if (linkname)
564 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
565 else
566 printf(" File: \"%s\"\n", filename);
567
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000568 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
569 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000570 (uintmax_t) (statbuf.st_size),
571 (uintmax_t) statbuf.st_blocks,
572 (unsigned long int) statbuf.st_blksize,
573 file_type(&statbuf),
574 (uintmax_t) statbuf.st_dev,
575 (uintmax_t) statbuf.st_dev,
576 (uintmax_t) statbuf.st_ino,
577 (unsigned long int) statbuf.st_nlink);
578 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
579 printf(" Device type: %lx,%lx\n",
580 (unsigned long int) major(statbuf.st_rdev),
581 (unsigned long int) minor(statbuf.st_rdev));
582 else
583 putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000584 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000585 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
586 bb_mode_string(statbuf.st_mode),
587 (unsigned long int) statbuf.st_uid,
588 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
589 (unsigned long int) statbuf.st_gid,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000590 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
591#if ENABLE_SELINUX
592 printf(" S_Context: %lc\n", *scontext);
593#endif
594 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000595 human_time(statbuf.st_atime),
596 human_time(statbuf.st_mtime),
597 human_time(statbuf.st_ctime));
598 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000599#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000600 return 1;
601}
602
Denis Vlasenko06af2162007-02-03 17:28:39 +0000603int stat_main(int argc, char **argv);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000604int stat_main(int argc, char **argv)
605{
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000606 char *format = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000607 int i;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000608 int ok = 1;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000609 bool (*statfunc)(char const *, char const *) = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000610
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000611 getopt32(argc, argv, "ftL"
Denis Vlasenko49622d72007-03-10 16:58:49 +0000612 USE_SELINUX("Z")
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000613 USE_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000614 );
615
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000616 if (option_mask32 & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000617 statfunc = do_statfs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000618 if (argc == optind) /* files */
619 bb_show_usage();
620
Denis Vlasenko49622d72007-03-10 16:58:49 +0000621#if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000622 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000623 selinux_or_die();
624 }
625#endif /* ENABLE_SELINUX */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000626 for (i = optind; i < argc; ++i)
627 ok &= statfunc(argv[i], format);
628
629 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
630}