blob: 37a924057e1e4d53b562fc59eddb0dd0eea3f303 [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 */
Mike Frysingerf06c4942005-04-24 03:53:12 +000019#define OPT_TERSE 2
Bernhard Reutner-Fischer18260d52006-04-18 14:17:49 +000020#define OPT_DEREFERENCE 4
Denis Vlasenko49622d72007-03-10 16:58:49 +000021#define OPT_SELINUX 8
Mike Frysingerf06c4942005-04-24 03:53:12 +000022static long flags;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000023
24static char const *file_type(struct stat const *st)
25{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000027 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000029 * returned string must start with a consonant.
30 */
31 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
32 if (S_ISDIR(st->st_mode)) return "directory";
33 if (S_ISBLK(st->st_mode)) return "block special file";
34 if (S_ISCHR(st->st_mode)) return "character special file";
35 if (S_ISFIFO(st->st_mode)) return "fifo";
36 if (S_ISLNK(st->st_mode)) return "symbolic link";
37 if (S_ISSOCK(st->st_mode)) return "socket";
38 if (S_TYPEISMQ(st)) return "message queue";
39 if (S_TYPEISSEM(st)) return "semaphore";
40 if (S_TYPEISSHM(st)) return "shared memory object";
41#ifdef S_TYPEISTMO
42 if (S_TYPEISTMO(st)) return "typed memory object";
43#endif
44 return "weird file";
45}
46
47static char const *human_time(time_t t)
48{
Denis Vlasenko5d148e22006-11-21 00:12:09 +000049 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000050 static char *str;
51 str = ctime(&t);
52 str[strlen(str)-1] = '\0';
53 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +000054 */
55 /* coreutils 6.3 compat: */
56 static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
57 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
58 return buf;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000059}
60
61/* Return the type of the specified file system.
62 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
63 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
64 * Still others have neither and have to get by with f_type (Linux).
65 */
66static char const *human_fstype(long f_type)
67{
Mike Frysinger408ae212005-04-24 04:11:44 +000068 int i;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000069 static const struct types {
Mike Frysinger408ae212005-04-24 04:11:44 +000070 long type;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000071 const char *fs;
Mike Frysinger408ae212005-04-24 04:11:44 +000072 } humantypes[] = {
73 { 0xADFF, "affs" },
74 { 0x1Cd1, "devpts" },
75 { 0x137D, "ext" },
76 { 0xEF51, "ext2" },
77 { 0xEF53, "ext2/ext3" },
78 { 0x3153464a, "jfs" },
79 { 0x58465342, "xfs" },
80 { 0xF995E849, "hpfs" },
81 { 0x9660, "isofs" },
82 { 0x4000, "isofs" },
83 { 0x4004, "isofs" },
84 { 0x137F, "minix" },
85 { 0x138F, "minix (30 char.)" },
86 { 0x2468, "minix v2" },
87 { 0x2478, "minix v2 (30 char.)" },
88 { 0x4d44, "msdos" },
89 { 0x4006, "fat" },
90 { 0x564c, "novell" },
91 { 0x6969, "nfs" },
92 { 0x9fa0, "proc" },
93 { 0x517B, "smb" },
94 { 0x012FF7B4, "xenix" },
95 { 0x012FF7B5, "sysv4" },
96 { 0x012FF7B6, "sysv2" },
97 { 0x012FF7B7, "coh" },
98 { 0x00011954, "ufs" },
99 { 0x012FD16D, "xia" },
100 { 0x5346544e, "ntfs" },
101 { 0x1021994, "tmpfs" },
102 { 0x52654973, "reiserfs" },
103 { 0x28cd3d45, "cramfs" },
104 { 0x7275, "romfs" },
105 { 0x858458f6, "romfs" },
106 { 0x73717368, "squashfs" },
107 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000108 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000109 };
110 for (i=0; humantypes[i].type; ++i)
111 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000112 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000113 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000114}
115
116#ifdef CONFIG_FEATURE_STAT_FORMAT
117/* print statfs info */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000118static void print_statfs(char *pformat, size_t buf_len, char m,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000119 char const *filename, void const *data
120 USE_SELINUX(,security_context_t scontext) )
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000121{
122 struct statfs const *statfsbuf = data;
123
124 switch (m) {
125 case 'n':
126 strncat(pformat, "s", buf_len);
127 printf(pformat, filename);
128 break;
129 case 'i':
130 strncat(pformat, "Lx", buf_len);
131 printf(pformat, statfsbuf->f_fsid);
132 break;
133 case 'l':
134 strncat(pformat, "lu", buf_len);
135 printf(pformat, statfsbuf->f_namelen);
136 break;
137 case 't':
138 strncat(pformat, "lx", buf_len);
139 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
140 break;
141 case 'T':
142 strncat(pformat, "s", buf_len);
143 printf(pformat, human_fstype(statfsbuf->f_type));
144 break;
145 case 'b':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000146 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000147 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
148 break;
149 case 'f':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000150 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000151 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
152 break;
153 case 'a':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000154 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000155 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
156 break;
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000157 case 'S':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000158 case 's':
159 strncat(pformat, "lu", buf_len);
160 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
161 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000162 case 'c':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000163 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000164 printf(pformat, (intmax_t) (statfsbuf->f_files));
165 break;
166 case 'd':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000167 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000168 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
169 break;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000170#if ENABLE_SELINUX
171 case 'C':
172 if (flags & OPT_SELINUX) {
173 strncat(pformat, "s", buf_len);
174 printf(scontext);
175 }
176 break;
177#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000178 default:
179 strncat(pformat, "c", buf_len);
180 printf(pformat, m);
181 break;
182 }
183}
184
185/* print stat info */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000186static void print_stat(char *pformat, size_t buf_len, char m,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000187 char const *filename, void const *data
188 USE_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000189{
190#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
191 struct stat *statbuf = (struct stat *) data;
192 struct passwd *pw_ent;
193 struct group *gw_ent;
194
195 switch (m) {
196 case 'n':
197 strncat(pformat, "s", buf_len);
198 printf(pformat, filename);
199 break;
200 case 'N':
201 strncat(pformat, "s", buf_len);
202 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000203 char *linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000204 if (linkname == NULL) {
205 bb_perror_msg("cannot read symbolic link '%s'", filename);
206 return;
207 }
208 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
209 printf(pformat, filename);
210 printf(" -> ");
211 printf(pformat, linkname);
212 } else {
213 printf(pformat, filename);
214 }
215 break;
216 case 'd':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000217 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000218 printf(pformat, (uintmax_t) statbuf->st_dev);
219 break;
220 case 'D':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000221 strncat(pformat, "jx", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000222 printf(pformat, (uintmax_t) statbuf->st_dev);
223 break;
224 case 'i':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000225 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000226 printf(pformat, (uintmax_t) statbuf->st_ino);
227 break;
228 case 'a':
229 strncat(pformat, "lo", buf_len);
230 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
231 break;
232 case 'A':
233 strncat(pformat, "s", buf_len);
234 printf(pformat, bb_mode_string(statbuf->st_mode));
235 break;
236 case 'f':
237 strncat(pformat, "lx", buf_len);
238 printf(pformat, (unsigned long int) statbuf->st_mode);
239 break;
240 case 'F':
241 strncat(pformat, "s", buf_len);
242 printf(pformat, file_type(statbuf));
243 break;
244 case 'h':
245 strncat(pformat, "lu", buf_len);
246 printf(pformat, (unsigned long int) statbuf->st_nlink);
247 break;
248 case 'u':
249 strncat(pformat, "lu", buf_len);
250 printf(pformat, (unsigned long int) statbuf->st_uid);
251 break;
252 case 'U':
253 strncat(pformat, "s", buf_len);
254 setpwent();
255 pw_ent = getpwuid(statbuf->st_uid);
256 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
257 break;
258 case 'g':
259 strncat(pformat, "lu", buf_len);
260 printf(pformat, (unsigned long int) statbuf->st_gid);
261 break;
262 case 'G':
263 strncat(pformat, "s", buf_len);
264 setgrent();
265 gw_ent = getgrgid(statbuf->st_gid);
266 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
267 break;
268 case 't':
269 strncat(pformat, "lx", buf_len);
270 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
271 break;
272 case 'T':
273 strncat(pformat, "lx", buf_len);
274 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
275 break;
276 case 's':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000277 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000278 printf(pformat, (uintmax_t) (statbuf->st_size));
279 break;
280 case 'B':
281 strncat(pformat, "lu", buf_len);
282 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
283 break;
284 case 'b':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000285 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000286 printf(pformat, (uintmax_t) statbuf->st_blocks);
287 break;
288 case 'o':
289 strncat(pformat, "lu", buf_len);
290 printf(pformat, (unsigned long int) statbuf->st_blksize);
291 break;
292 case 'x':
293 strncat(pformat, "s", buf_len);
294 printf(pformat, human_time(statbuf->st_atime));
295 break;
296 case 'X':
297 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
298 printf(pformat, (unsigned long int) statbuf->st_atime);
299 break;
300 case 'y':
301 strncat(pformat, "s", buf_len);
302 printf(pformat, human_time(statbuf->st_mtime));
303 break;
304 case 'Y':
305 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
306 printf(pformat, (unsigned long int) statbuf->st_mtime);
307 break;
308 case 'z':
309 strncat(pformat, "s", buf_len);
310 printf(pformat, human_time(statbuf->st_ctime));
311 break;
312 case 'Z':
313 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
314 printf(pformat, (unsigned long int) statbuf->st_ctime);
315 break;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000316#if ENABLE_SELINUX
317 case 'C':
318 if (flags & OPT_SELINUX) {
319 strncat(pformat, "s", buf_len);
320 printf(pformat, scontext);
321 }
322 break;
323#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000324 default:
325 strncat(pformat, "c", buf_len);
326 printf(pformat, m);
327 break;
328 }
329}
330
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000331static void print_it(char const *masterformat, char const *filename,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000332 void (*print_func) (char *, size_t, char, char const *, void const *
333 USE_SELINUX(, security_context_t scontext)),
334 void const *data USE_SELINUX(, security_context_t scontext) )
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000335{
336 char *b;
337
338 /* create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000339 char *format = xstrdup(masterformat);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000340
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000341 /* Add 2 to accomodate our conversion of the stat '%s' format string
342 * to the printf '%llu' one. */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000343 size_t n_alloc = strlen(format) + 2 + 1;
344 char *dest = xmalloc(n_alloc);
345
346 b = format;
347 while (b) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000348 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000349 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000350 if (!p) {
351 /* coreutils 6.3 always print <cr> at the end */
352 /*fputs(b, stdout);*/
353 puts(b);
354 break;
355 }
356 *p++ = '\0';
357 fputs(b, stdout);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000358
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000359 len = strspn(p, "#-+.I 0123456789");
360 dest[0] = '%';
361 memcpy(dest + 1, p, len);
362 dest[1 + len] = 0;
363 p += len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000364
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000365 b = p + 1;
366 switch (*p) {
367 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000368 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000369 /* fall through */
370 case '%':
371 putchar('%');
372 break;
373 default:
Denis Vlasenko49622d72007-03-10 16:58:49 +0000374 print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000375 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000376 }
377 }
378
379 free(format);
380 free(dest);
381}
382#endif
383
384/* Stat the file system and print what we find. */
385static int do_statfs(char const *filename, char const *format)
386{
387 struct statfs statfsbuf;
388
Denis Vlasenko49622d72007-03-10 16:58:49 +0000389#if ENABLE_SELINUX
390 security_context_t scontext = NULL;
391 if (flags & OPT_SELINUX) {
392 if ((flags & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
393 getfilecon(filename, scontext))< 0) {
394 bb_perror_msg(filename);
395 return 0;
396 }
397 }
398#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000399 if (statfs(filename, &statfsbuf) != 0) {
400 bb_perror_msg("cannot read file system information for '%s'", filename);
401 return 0;
402 }
403
404#ifdef CONFIG_FEATURE_STAT_FORMAT
405 if (format == NULL)
Denis Vlasenko49622d72007-03-10 16:58:49 +0000406#ifndef ENABLE_SELINUX
Mike Frysingerf06c4942005-04-24 03:53:12 +0000407 format = (flags & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000408 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000409 : " File: \"%n\"\n"
410 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000411 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000412 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000413 "Inodes: Total: %-10c Free: %d");
Denis Vlasenko49622d72007-03-10 16:58:49 +0000414 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000415#else
Denis Vlasenko49622d72007-03-10 16:58:49 +0000416 format = (flags & OPT_TERSE
417 ? (flags & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
418 "%n %i %l %t %s %b %f %a %c %d\n")
419 : (flags & OPT_SELINUX ?
420 " File: \"%n\"\n"
421 " ID: %-8i Namelen: %-7l Type: %T\n"
422 "Block size: %-10s\n"
423 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
424 "Inodes: Total: %-10c Free: %d"
425 " S_context: %C\n":
426 " File: \"%n\"\n"
427 " ID: %-8i Namelen: %-7l Type: %T\n"
428 "Block size: %-10s\n"
429 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
430 "Inodes: Total: %-10c Free: %d\n")
431 );
432 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
433#endif /* SELINUX */
434#else /* FEATURE_STAT_FORMAT */
Mike Frysingerf06c4942005-04-24 03:53:12 +0000435 format = (flags & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000436 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000437 : " File: \"%s\"\n"
438 " ID: %-8Lx Namelen: %-7lu ");
439 printf(format,
440 filename,
441 statfsbuf.f_fsid,
442 statfsbuf.f_namelen);
443
Mike Frysingerf06c4942005-04-24 03:53:12 +0000444 if (flags & OPT_TERSE)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000445 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
446 else
447 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
448
Denis Vlasenko49622d72007-03-10 16:58:49 +0000449#if !ENABLE_SELINUX
Mike Frysingerf06c4942005-04-24 03:53:12 +0000450 format = (flags & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000451 ? "%lu %ld %ld %ld %ld %ld\n"
452 : "Block size: %-10lu\n"
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000453 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
454 "Inodes: Total: %-10jd Free: %jd\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000455 printf(format,
456 (unsigned long int) (statfsbuf.f_bsize),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000457 (intmax_t) (statfsbuf.f_blocks),
458 (intmax_t) (statfsbuf.f_bfree),
459 (intmax_t) (statfsbuf.f_bavail),
460 (intmax_t) (statfsbuf.f_files),
461 (intmax_t) (statfsbuf.f_ffree));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000462#else
463 format = (flags & OPT_TERSE
464 ? (flags & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
465 "%lu %ld %ld %ld %ld %ld\n")
466 : (flags & OPT_SELINUX ?
467 "Block size: %-10lu\n"
468 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
469 "Inodes: Total: %-10jd Free: %jd"
470 "S_context: %C\n":
471 "Block size: %-10lu\n"
472 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
473 "Inodes: Total: %-10jd Free: %jd\n"));
474 printf(format,
475 (unsigned long int) (statfsbuf.f_bsize),
476 (intmax_t) (statfsbuf.f_blocks),
477 (intmax_t) (statfsbuf.f_bfree),
478 (intmax_t) (statfsbuf.f_bavail),
479 (intmax_t) (statfsbuf.f_files),
480 (intmax_t) (statfsbuf.f_ffree),
481 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000482
Denis Vlasenko49622d72007-03-10 16:58:49 +0000483 if (scontext)
484 freecon(scontext);
485#endif
486#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000487 return 1;
488}
489
490/* stat the file and print what we find */
491static int do_stat(char const *filename, char const *format)
492{
493 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000494#if ENABLE_SELINUX
495 security_context_t scontext = NULL;
496 if (flags & OPT_SELINUX) {
497 if ((flags & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
498 getfilecon(filename, scontext))< 0) {
499 bb_perror_msg (filename);
500 return 0;
501 }
502 }
503#endif
Bernhard Reutner-Fischer18260d52006-04-18 14:17:49 +0000504 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000505 bb_perror_msg("cannot stat '%s'", filename);
506 return 0;
507 }
508
509#ifdef CONFIG_FEATURE_STAT_FORMAT
510 if (format == NULL) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000511#ifndef ENABLE_SELINUX
Mike Frysingerf06c4942005-04-24 03:53:12 +0000512 if (flags & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000513 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000514 } else {
515 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
516 format =
517 " File: \"%N\"\n"
518 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
519 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
520 " Device type: %t,%T\n"
521 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
522 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
523 } else {
524 format =
525 " File: \"%N\"\n"
526 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
527 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
528 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
529 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
530 }
531 }
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000532#else
Denis Vlasenko49622d72007-03-10 16:58:49 +0000533 if (flags & OPT_TERSE) {
534 format = (flags & OPT_SELINUX ?
535 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
536 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
537 } else {
538 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
539 format = (flags & OPT_SELINUX ?
540 " File: \"%N\"\n"
541 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
542 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
543 " Device type: %t,%T\n"
544 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
545 " S_Context: %C\n"
546 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
547 " File: \"%N\"\n"
548 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
549 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
550 " Device type: %t,%T\n"
551 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
552 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
553 } else {
554 format = (flags & OPT_SELINUX ?
555 " File: \"%N\"\n"
556 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
557 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
558 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
559 "S_Context: %C\n"
560 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
561 " File: \"%N\"\n"
562 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
563 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
564 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
565 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
566 }
567 }
568#endif
569 }
570 print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
571#else /* FEATURE_STAT_FORMAT */
Mike Frysingerf06c4942005-04-24 03:53:12 +0000572 if (flags & OPT_TERSE) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000573 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
574 SKIP_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000575 filename,
576 (uintmax_t) (statbuf.st_size),
577 (uintmax_t) statbuf.st_blocks,
578 (unsigned long int) statbuf.st_mode,
579 (unsigned long int) statbuf.st_uid,
580 (unsigned long int) statbuf.st_gid,
581 (uintmax_t) statbuf.st_dev,
582 (uintmax_t) statbuf.st_ino,
583 (unsigned long int) statbuf.st_nlink,
584 (unsigned long int) major(statbuf.st_rdev),
585 (unsigned long int) minor(statbuf.st_rdev),
586 (unsigned long int) statbuf.st_atime,
587 (unsigned long int) statbuf.st_mtime,
588 (unsigned long int) statbuf.st_ctime,
589 (unsigned long int) statbuf.st_blksize
590 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000591#if ENABLE_SELINUX
592 if (flags & OPT_SELINUX)
593 printf(" %lc\n", *scontext);
594 else
595 putchar('\n');
596#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000597 } else {
598 char *linkname = NULL;
599
600 struct passwd *pw_ent;
601 struct group *gw_ent;
602 setgrent();
603 gw_ent = getgrgid(statbuf.st_gid);
604 setpwent();
605 pw_ent = getpwuid(statbuf.st_uid);
606
607 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000608 linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000609 if (linkname)
610 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
611 else
612 printf(" File: \"%s\"\n", filename);
613
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000614 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
615 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000616 (uintmax_t) (statbuf.st_size),
617 (uintmax_t) statbuf.st_blocks,
618 (unsigned long int) statbuf.st_blksize,
619 file_type(&statbuf),
620 (uintmax_t) statbuf.st_dev,
621 (uintmax_t) statbuf.st_dev,
622 (uintmax_t) statbuf.st_ino,
623 (unsigned long int) statbuf.st_nlink);
624 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
625 printf(" Device type: %lx,%lx\n",
626 (unsigned long int) major(statbuf.st_rdev),
627 (unsigned long int) minor(statbuf.st_rdev));
628 else
629 putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000630 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000631 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
632 bb_mode_string(statbuf.st_mode),
633 (unsigned long int) statbuf.st_uid,
634 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
635 (unsigned long int) statbuf.st_gid,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000636 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
637#if ENABLE_SELINUX
638 printf(" S_Context: %lc\n", *scontext);
639#endif
640 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000641 human_time(statbuf.st_atime),
642 human_time(statbuf.st_mtime),
643 human_time(statbuf.st_ctime));
644 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000645#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000646 return 1;
647}
648
Denis Vlasenko06af2162007-02-03 17:28:39 +0000649int stat_main(int argc, char **argv);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000650int stat_main(int argc, char **argv)
651{
652 int i;
653 char *format = NULL;
654 int ok = 1;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000655 int (*statfunc)(char const *, char const *) = do_stat;
656
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000657 flags = getopt32(argc, argv, "ftL"
Denis Vlasenko49622d72007-03-10 16:58:49 +0000658 USE_SELINUX("Z")
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000659 USE_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000660 );
661
662 if (flags & 1) /* -f */
663 statfunc = do_statfs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000664 if (argc == optind) /* files */
665 bb_show_usage();
666
Denis Vlasenko49622d72007-03-10 16:58:49 +0000667#if ENABLE_SELINUX
668 if (flags & OPT_SELINUX) {
669 selinux_or_die();
670 }
671#endif /* ENABLE_SELINUX */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000672 for (i = optind; i < argc; ++i)
673 ok &= statfunc(argv[i], format);
674
675 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
676}