1 /* vi: set sw=4 ts=4: */
3 * stat -- display file or file system status
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>
8 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 /* vars to control behavior */
20 #define OPT_DEREFERENCE 4
24 static char const *file_type(struct stat const *st)
26 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
27 * for some of these formats.
28 * To keep diagnostics grammatical in English, the
29 * returned string must start with a consonant.
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";
42 if (S_TYPEISTMO(st)) return "typed memory object";
47 static char const *human_time(time_t t)
52 str[strlen(str)-1] = '\0';
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));
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).
66 static char const *human_fstype(long f_type)
69 static const struct types {
77 { 0xEF53, "ext2/ext3" },
78 { 0x3153464a, "jfs" },
79 { 0x58465342, "xfs" },
80 { 0xF995E849, "hpfs" },
85 { 0x138F, "minix (30 char.)" },
86 { 0x2468, "minix v2" },
87 { 0x2478, "minix v2 (30 char.)" },
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" },
105 { 0x858458f6, "romfs" },
106 { 0x73717368, "squashfs" },
107 { 0x62656572, "sysfs" },
110 for (i=0; humantypes[i].type; ++i)
111 if (humantypes[i].type == f_type)
113 return humantypes[i].fs;
116 #ifdef CONFIG_FEATURE_STAT_FORMAT
117 /* print statfs info */
118 static void print_statfs(char *pformat, size_t buf_len, char m,
119 char const *filename, void const *data
120 USE_SELINUX(,security_context_t scontext) )
122 struct statfs const *statfsbuf = data;
126 strncat(pformat, "s", buf_len);
127 printf(pformat, filename);
130 strncat(pformat, "Lx", buf_len);
131 printf(pformat, statfsbuf->f_fsid);
134 strncat(pformat, "lu", buf_len);
135 printf(pformat, statfsbuf->f_namelen);
138 strncat(pformat, "lx", buf_len);
139 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
142 strncat(pformat, "s", buf_len);
143 printf(pformat, human_fstype(statfsbuf->f_type));
146 strncat(pformat, "jd", buf_len);
147 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
150 strncat(pformat, "jd", buf_len);
151 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
154 strncat(pformat, "jd", buf_len);
155 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
159 strncat(pformat, "lu", buf_len);
160 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
163 strncat(pformat, "jd", buf_len);
164 printf(pformat, (intmax_t) (statfsbuf->f_files));
167 strncat(pformat, "jd", buf_len);
168 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
172 if (flags & OPT_SELINUX) {
173 strncat(pformat, "s", buf_len);
179 strncat(pformat, "c", buf_len);
185 /* print stat info */
186 static void print_stat(char *pformat, size_t buf_len, char m,
187 char const *filename, void const *data
188 USE_SELINUX(, security_context_t scontext))
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;
197 strncat(pformat, "s", buf_len);
198 printf(pformat, filename);
201 strncat(pformat, "s", buf_len);
202 if (S_ISLNK(statbuf->st_mode)) {
203 char *linkname = xmalloc_readlink_or_warn(filename);
204 if (linkname == NULL) {
205 bb_perror_msg("cannot read symbolic link '%s'", filename);
208 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
209 printf(pformat, filename);
211 printf(pformat, linkname);
213 printf(pformat, filename);
217 strncat(pformat, "ju", buf_len);
218 printf(pformat, (uintmax_t) statbuf->st_dev);
221 strncat(pformat, "jx", buf_len);
222 printf(pformat, (uintmax_t) statbuf->st_dev);
225 strncat(pformat, "ju", buf_len);
226 printf(pformat, (uintmax_t) statbuf->st_ino);
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)));
233 strncat(pformat, "s", buf_len);
234 printf(pformat, bb_mode_string(statbuf->st_mode));
237 strncat(pformat, "lx", buf_len);
238 printf(pformat, (unsigned long int) statbuf->st_mode);
241 strncat(pformat, "s", buf_len);
242 printf(pformat, file_type(statbuf));
245 strncat(pformat, "lu", buf_len);
246 printf(pformat, (unsigned long int) statbuf->st_nlink);
249 strncat(pformat, "lu", buf_len);
250 printf(pformat, (unsigned long int) statbuf->st_uid);
253 strncat(pformat, "s", buf_len);
255 pw_ent = getpwuid(statbuf->st_uid);
256 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
259 strncat(pformat, "lu", buf_len);
260 printf(pformat, (unsigned long int) statbuf->st_gid);
263 strncat(pformat, "s", buf_len);
265 gw_ent = getgrgid(statbuf->st_gid);
266 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
269 strncat(pformat, "lx", buf_len);
270 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
273 strncat(pformat, "lx", buf_len);
274 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
277 strncat(pformat, "ju", buf_len);
278 printf(pformat, (uintmax_t) (statbuf->st_size));
281 strncat(pformat, "lu", buf_len);
282 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
285 strncat(pformat, "ju", buf_len);
286 printf(pformat, (uintmax_t) statbuf->st_blocks);
289 strncat(pformat, "lu", buf_len);
290 printf(pformat, (unsigned long int) statbuf->st_blksize);
293 strncat(pformat, "s", buf_len);
294 printf(pformat, human_time(statbuf->st_atime));
297 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
298 printf(pformat, (unsigned long int) statbuf->st_atime);
301 strncat(pformat, "s", buf_len);
302 printf(pformat, human_time(statbuf->st_mtime));
305 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
306 printf(pformat, (unsigned long int) statbuf->st_mtime);
309 strncat(pformat, "s", buf_len);
310 printf(pformat, human_time(statbuf->st_ctime));
313 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
314 printf(pformat, (unsigned long int) statbuf->st_ctime);
318 if (flags & OPT_SELINUX) {
319 strncat(pformat, "s", buf_len);
320 printf(pformat, scontext);
325 strncat(pformat, "c", buf_len);
331 static void print_it(char const *masterformat, char const *filename,
332 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) )
338 /* create a working copy of the format string */
339 char *format = xstrdup(masterformat);
341 /* Add 2 to accomodate our conversion of the stat '%s' format string
342 * to the printf '%llu' one. */
343 size_t n_alloc = strlen(format) + 2 + 1;
344 char *dest = xmalloc(n_alloc);
349 char *p = strchr(b, '%');
351 /* coreutils 6.3 always print <cr> at the end */
352 /*fputs(b, stdout);*/
359 len = strspn(p, "#-+.I 0123456789");
361 memcpy(dest + 1, p, len);
374 print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext));
384 /* Stat the file system and print what we find. */
385 static int do_statfs(char const *filename, char const *format)
387 struct statfs statfsbuf;
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);
399 if (statfs(filename, &statfsbuf) != 0) {
400 bb_perror_msg("cannot read file system information for '%s'", filename);
404 #ifdef CONFIG_FEATURE_STAT_FORMAT
406 #ifndef ENABLE_SELINUX
407 format = (flags & OPT_TERSE
408 ? "%n %i %l %t %s %b %f %a %c %d\n"
410 " ID: %-8i Namelen: %-7l Type: %T\n"
411 "Block size: %-10s\n"
412 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
413 "Inodes: Total: %-10c Free: %d");
414 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
416 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 ?
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"
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")
432 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
434 #else /* FEATURE_STAT_FORMAT */
435 format = (flags & OPT_TERSE
438 " ID: %-8Lx Namelen: %-7lu ");
442 statfsbuf.f_namelen);
444 if (flags & OPT_TERSE)
445 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
447 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
450 format = (flags & OPT_TERSE
451 ? "%lu %ld %ld %ld %ld %ld\n"
452 : "Block size: %-10lu\n"
453 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
454 "Inodes: Total: %-10jd Free: %jd\n");
456 (unsigned long int) (statfsbuf.f_bsize),
457 (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));
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"
471 "Block size: %-10lu\n"
472 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
473 "Inodes: Total: %-10jd Free: %jd\n"));
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),
486 #endif /* FEATURE_STAT_FORMAT */
490 /* stat the file and print what we find */
491 static int do_stat(char const *filename, char const *format)
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);
504 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
505 bb_perror_msg("cannot stat '%s'", filename);
509 #ifdef CONFIG_FEATURE_STAT_FORMAT
510 if (format == NULL) {
511 #ifndef ENABLE_SELINUX
512 if (flags & OPT_TERSE) {
513 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
515 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
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";
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";
533 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");
538 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
539 format = (flags & OPT_SELINUX ?
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"
546 "Access: %x\n" "Modify: %y\n" "Change: %z\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");
554 format = (flags & OPT_SELINUX ?
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"
560 "Access: %x\n" "Modify: %y\n" "Change: %z\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");
570 print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
571 #else /* FEATURE_STAT_FORMAT */
572 if (flags & OPT_TERSE) {
573 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
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
592 if (flags & OPT_SELINUX)
593 printf(" %lc\n", *scontext);
598 char *linkname = NULL;
600 struct passwd *pw_ent;
601 struct group *gw_ent;
603 gw_ent = getgrgid(statbuf.st_gid);
605 pw_ent = getpwuid(statbuf.st_uid);
607 if (S_ISLNK(statbuf.st_mode))
608 linkname = xmalloc_readlink_or_warn(filename);
610 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
612 printf(" File: \"%s\"\n", filename);
614 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
615 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
616 (uintmax_t) (statbuf.st_size),
617 (uintmax_t) statbuf.st_blocks,
618 (unsigned long int) statbuf.st_blksize,
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));
630 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
631 (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,
636 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
638 printf(" S_Context: %lc\n", *scontext);
640 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
641 human_time(statbuf.st_atime),
642 human_time(statbuf.st_mtime),
643 human_time(statbuf.st_ctime));
645 #endif /* FEATURE_STAT_FORMAT */
649 int stat_main(int argc, char **argv);
650 int stat_main(int argc, char **argv)
655 int (*statfunc)(char const *, char const *) = do_stat;
657 flags = getopt32(argc, argv, "ftL"
659 USE_FEATURE_STAT_FORMAT("c:", &format)
662 if (flags & 1) /* -f */
663 statfunc = do_statfs;
664 if (argc == optind) /* files */
668 if (flags & OPT_SELINUX) {
671 #endif /* ENABLE_SELINUX */
672 for (i = optind; i < argc; ++i)
673 ok &= statfunc(argv[i], format);
675 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);