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 */
19 #define OPT_FILESYS (1 << 0)
20 #define OPT_TERSE (1 << 1)
21 #define OPT_DEREFERENCE (1 << 2)
22 #define OPT_SELINUX (1 << 3)
24 static const char *file_type(const struct stat *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 const char *human_time(time_t t)
52 str[strlen(str)-1] = '\0';
55 /* coreutils 6.3 compat: */
57 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
58 #define buf bb_common_bufsiz1
60 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
65 /* Return the type of the specified file system.
66 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
67 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
68 * Still others have neither and have to get by with f_type (Linux).
70 static const char *human_fstype(uint32_t f_type)
72 static const struct types {
80 { 0xEF53, "ext2/ext3" },
81 { 0x3153464a, "jfs" },
82 { 0x58465342, "xfs" },
83 { 0xF995E849, "hpfs" },
88 { 0x138F, "minix (30 char.)" },
89 { 0x2468, "minix v2" },
90 { 0x2478, "minix v2 (30 char.)" },
97 { 0x012FF7B4, "xenix" },
98 { 0x012FF7B5, "sysv4" },
99 { 0x012FF7B6, "sysv2" },
100 { 0x012FF7B7, "coh" },
101 { 0x00011954, "ufs" },
102 { 0x012FD16D, "xia" },
103 { 0x5346544e, "ntfs" },
104 { 0x1021994, "tmpfs" },
105 { 0x52654973, "reiserfs" },
106 { 0x28cd3d45, "cramfs" },
108 { 0x858458f6, "romfs" },
109 { 0x73717368, "squashfs" },
110 { 0x62656572, "sysfs" },
116 for (i = 0; humantypes[i].type; ++i)
117 if (humantypes[i].type == f_type)
119 return humantypes[i].fs;
122 static void strcatc(char *str, char c)
124 int len = strlen(str);
129 static void printfs(char *pformat, const char *msg)
131 strcatc(pformat, 's');
132 printf(pformat, msg);
135 #if ENABLE_FEATURE_STAT_FORMAT
136 /* print statfs info */
137 static void print_statfs(char *pformat, const char m,
138 const char *const filename, const void *data
139 USE_SELINUX(, security_context_t scontext))
141 const struct statfs *statfsbuf = data;
143 printfs(pformat, filename);
144 } else if (m == 'i') {
145 strcat(pformat, "Lx");
146 printf(pformat, statfsbuf->f_fsid);
147 } else if (m == 'l') {
148 strcat(pformat, "lu");
149 printf(pformat, statfsbuf->f_namelen);
150 } else if (m == 't') {
151 strcat(pformat, "lx");
152 printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
153 } else if (m == 'T') {
154 printfs(pformat, human_fstype(statfsbuf->f_type));
155 } else if (m == 'b') {
156 strcat(pformat, "jd");
157 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
158 } else if (m == 'f') {
159 strcat(pformat, "jd");
160 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
161 } else if (m == 'a') {
162 strcat(pformat, "jd");
163 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
164 } else if (m == 's' || m == 'S') {
165 strcat(pformat, "lu");
166 printf(pformat, (unsigned long) (statfsbuf->f_bsize));
167 } else if (m == 'c') {
168 strcat(pformat, "jd");
169 printf(pformat, (intmax_t) (statfsbuf->f_files));
170 } else if (m == 'd') {
171 strcat(pformat, "jd");
172 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
174 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
175 printfs(pformat, scontext);
178 strcatc(pformat, 'c');
183 /* print stat info */
184 static void print_stat(char *pformat, const char m,
185 const char *const filename, const void *data
186 USE_SELINUX(, security_context_t scontext))
188 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
189 struct stat *statbuf = (struct stat *) data;
190 struct passwd *pw_ent;
191 struct group *gw_ent;
194 printfs(pformat, filename);
195 } else if (m == 'N') {
196 strcatc(pformat, 's');
197 if (S_ISLNK(statbuf->st_mode)) {
198 char *linkname = xmalloc_readlink_or_warn(filename);
199 if (linkname == NULL)
201 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
202 printf(pformat, filename);
204 printf(pformat, linkname);
207 printf(pformat, filename);
209 } else if (m == 'd') {
210 strcat(pformat, "ju");
211 printf(pformat, (uintmax_t) statbuf->st_dev);
212 } else if (m == 'D') {
213 strcat(pformat, "jx");
214 printf(pformat, (uintmax_t) statbuf->st_dev);
215 } else if (m == 'i') {
216 strcat(pformat, "ju");
217 printf(pformat, (uintmax_t) statbuf->st_ino);
218 } else if (m == 'a') {
219 strcat(pformat, "lo");
220 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
221 } else if (m == 'A') {
222 printfs(pformat, bb_mode_string(statbuf->st_mode));
223 } else if (m == 'f') {
224 strcat(pformat, "lx");
225 printf(pformat, (unsigned long) statbuf->st_mode);
226 } else if (m == 'F') {
227 printfs(pformat, file_type(statbuf));
228 } else if (m == 'h') {
229 strcat(pformat, "lu");
230 printf(pformat, (unsigned long) statbuf->st_nlink);
231 } else if (m == 'u') {
232 strcat(pformat, "lu");
233 printf(pformat, (unsigned long) statbuf->st_uid);
234 } else if (m == 'U') {
236 pw_ent = getpwuid(statbuf->st_uid);
237 printfs(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
238 } else if (m == 'g') {
239 strcat(pformat, "lu");
240 printf(pformat, (unsigned long) statbuf->st_gid);
241 } else if (m == 'G') {
243 gw_ent = getgrgid(statbuf->st_gid);
244 printfs(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
245 } else if (m == 't') {
246 strcat(pformat, "lx");
247 printf(pformat, (unsigned long) major(statbuf->st_rdev));
248 } else if (m == 'T') {
249 strcat(pformat, "lx");
250 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
251 } else if (m == 's') {
252 strcat(pformat, "ju");
253 printf(pformat, (uintmax_t) (statbuf->st_size));
254 } else if (m == 'B') {
255 strcat(pformat, "lu");
256 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
257 } else if (m == 'b') {
258 strcat(pformat, "ju");
259 printf(pformat, (uintmax_t) statbuf->st_blocks);
260 } else if (m == 'o') {
261 strcat(pformat, "lu");
262 printf(pformat, (unsigned long) statbuf->st_blksize);
263 } else if (m == 'x') {
264 printfs(pformat, human_time(statbuf->st_atime));
265 } else if (m == 'X') {
266 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
267 printf(pformat, (unsigned long) statbuf->st_atime);
268 } else if (m == 'y') {
269 printfs(pformat, human_time(statbuf->st_mtime));
270 } else if (m == 'Y') {
271 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
272 printf(pformat, (unsigned long) statbuf->st_mtime);
273 } else if (m == 'z') {
274 printfs(pformat, human_time(statbuf->st_ctime));
275 } else if (m == 'Z') {
276 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
277 printf(pformat, (unsigned long) statbuf->st_ctime);
279 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
280 printfs(pformat, scontext);
283 strcatc(pformat, 'c');
288 static void print_it(const char *masterformat, const char *filename,
289 void (*print_func) (char*, char, const char*, const void* USE_SELINUX(, security_context_t scontext)),
291 USE_SELINUX(, security_context_t scontext) )
293 /* Create a working copy of the format string */
294 char *format = xstrdup(masterformat);
295 /* Add 2 to accomodate our conversion of the stat '%s' format string
296 * to the printf '%llu' one. */
297 char *dest = xmalloc(strlen(format) + 2 + 1);
303 char *p = strchr(b, '%');
305 /* coreutils 6.3 always prints <cr> at the end */
306 /*fputs(b, stdout);*/
313 /* dest = "%<modifiers>" */
314 len = strspn(p, "#-+.I 0123456789");
316 memcpy(dest + 1, p, len);
317 dest[1 + len] = '\0';
329 /* Completes "%<modifiers>" with specifier and printfs */
330 print_func(dest, *p, filename, data USE_SELINUX(,scontext));
340 /* Stat the file system and print what we find. */
341 static bool do_statfs(const char *filename, const char *format)
343 struct statfs statfsbuf;
345 security_context_t scontext = NULL;
347 if (option_mask32 & OPT_SELINUX) {
348 if ((option_mask32 & OPT_DEREFERENCE
349 ? lgetfilecon(filename, &scontext)
350 : getfilecon(filename, &scontext)
353 bb_perror_msg(filename);
358 if (statfs(filename, &statfsbuf) != 0) {
359 bb_perror_msg("cannot read file system information for '%s'", filename);
363 #if ENABLE_FEATURE_STAT_FORMAT
364 if (format == NULL) {
366 format = (option_mask32 & OPT_TERSE
367 ? "%n %i %l %t %s %b %f %a %c %d\n"
369 " ID: %-8i Namelen: %-7l Type: %T\n"
370 "Block size: %-10s\n"
371 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
372 "Inodes: Total: %-10c Free: %d");
374 format = (option_mask32 & OPT_TERSE
375 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
376 "%n %i %l %t %s %b %f %a %c %d\n")
377 : (option_mask32 & OPT_SELINUX ?
379 " ID: %-8i Namelen: %-7l Type: %T\n"
380 "Block size: %-10s\n"
381 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
382 "Inodes: Total: %-10c Free: %d"
385 " ID: %-8i Namelen: %-7l Type: %T\n"
386 "Block size: %-10s\n"
387 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
388 "Inodes: Total: %-10c Free: %d\n")
392 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
393 #else /* FEATURE_STAT_FORMAT */
394 format = (option_mask32 & OPT_TERSE
397 " ID: %-8Lx Namelen: %-7lu ");
401 statfsbuf.f_namelen);
403 if (option_mask32 & OPT_TERSE)
404 printf("%lx ", (unsigned long) (statfsbuf.f_type));
406 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
409 format = (option_mask32 & OPT_TERSE
410 ? "%lu %ld %ld %ld %ld %ld\n"
411 : "Block size: %-10lu\n"
412 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
413 "Inodes: Total: %-10jd Free: %jd\n");
415 (unsigned long) (statfsbuf.f_bsize),
416 (intmax_t) (statfsbuf.f_blocks),
417 (intmax_t) (statfsbuf.f_bfree),
418 (intmax_t) (statfsbuf.f_bavail),
419 (intmax_t) (statfsbuf.f_files),
420 (intmax_t) (statfsbuf.f_ffree));
422 format = (option_mask32 & OPT_TERSE
423 ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
424 "%lu %ld %ld %ld %ld %ld\n")
425 : (option_mask32 & OPT_SELINUX ?
426 "Block size: %-10lu\n"
427 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
428 "Inodes: Total: %-10jd Free: %jd"
430 "Block size: %-10lu\n"
431 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
432 "Inodes: Total: %-10jd Free: %jd\n"));
434 (unsigned long) (statfsbuf.f_bsize),
435 (intmax_t) (statfsbuf.f_blocks),
436 (intmax_t) (statfsbuf.f_bfree),
437 (intmax_t) (statfsbuf.f_bavail),
438 (intmax_t) (statfsbuf.f_files),
439 (intmax_t) (statfsbuf.f_ffree),
445 #endif /* FEATURE_STAT_FORMAT */
449 /* stat the file and print what we find */
450 static bool do_stat(const char *filename, const char *format)
454 security_context_t scontext = NULL;
456 if (option_mask32 & OPT_SELINUX) {
457 if ((option_mask32 & OPT_DEREFERENCE
458 ? lgetfilecon(filename, &scontext)
459 : getfilecon(filename, &scontext)
462 bb_perror_msg(filename);
467 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
468 bb_perror_msg("cannot stat '%s'", filename);
472 #if ENABLE_FEATURE_STAT_FORMAT
473 if (format == NULL) {
475 if (option_mask32 & OPT_TERSE) {
476 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
478 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
481 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
482 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
483 " Device type: %t,%T\n"
484 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
485 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
489 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
490 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
491 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
492 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
496 if (option_mask32 & OPT_TERSE) {
497 format = (option_mask32 & OPT_SELINUX ?
498 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
499 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
501 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
502 format = (option_mask32 & OPT_SELINUX ?
504 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
505 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
506 " Device type: %t,%T\n"
507 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
509 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
511 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
512 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
513 " Device type: %t,%T\n"
514 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
515 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
517 format = (option_mask32 & OPT_SELINUX ?
519 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
520 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
521 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
523 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
525 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
526 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
527 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
528 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
533 print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
534 #else /* FEATURE_STAT_FORMAT */
535 if (option_mask32 & OPT_TERSE) {
536 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
539 (uintmax_t) (statbuf.st_size),
540 (uintmax_t) statbuf.st_blocks,
541 (unsigned long) statbuf.st_mode,
542 (unsigned long) statbuf.st_uid,
543 (unsigned long) statbuf.st_gid,
544 (uintmax_t) statbuf.st_dev,
545 (uintmax_t) statbuf.st_ino,
546 (unsigned long) statbuf.st_nlink,
547 (unsigned long) major(statbuf.st_rdev),
548 (unsigned long) minor(statbuf.st_rdev),
549 (unsigned long) statbuf.st_atime,
550 (unsigned long) statbuf.st_mtime,
551 (unsigned long) statbuf.st_ctime,
552 (unsigned long) statbuf.st_blksize
555 if (option_mask32 & OPT_SELINUX)
556 printf(" %lc\n", *scontext);
561 char *linkname = NULL;
563 struct passwd *pw_ent;
564 struct group *gw_ent;
566 gw_ent = getgrgid(statbuf.st_gid);
568 pw_ent = getpwuid(statbuf.st_uid);
570 if (S_ISLNK(statbuf.st_mode))
571 linkname = xmalloc_readlink_or_warn(filename);
573 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
575 printf(" File: \"%s\"\n", filename);
577 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
578 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
579 (uintmax_t) (statbuf.st_size),
580 (uintmax_t) statbuf.st_blocks,
581 (unsigned long) statbuf.st_blksize,
583 (uintmax_t) statbuf.st_dev,
584 (uintmax_t) statbuf.st_dev,
585 (uintmax_t) statbuf.st_ino,
586 (unsigned long) statbuf.st_nlink);
587 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
588 printf(" Device type: %lx,%lx\n",
589 (unsigned long) major(statbuf.st_rdev),
590 (unsigned long) minor(statbuf.st_rdev));
593 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
594 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
595 bb_mode_string(statbuf.st_mode),
596 (unsigned long) statbuf.st_uid,
597 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
598 (unsigned long) statbuf.st_gid,
599 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
601 printf(" S_Context: %lc\n", *scontext);
603 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
604 human_time(statbuf.st_atime),
605 human_time(statbuf.st_mtime),
606 human_time(statbuf.st_ctime));
608 #endif /* FEATURE_STAT_FORMAT */
612 int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
613 int stat_main(int argc, char **argv)
618 bool (*statfunc)(const char *, const char *) = do_stat;
622 USE_FEATURE_STAT_FORMAT("c:", &format)
625 if (option_mask32 & OPT_FILESYS) /* -f */
626 statfunc = do_statfs;
627 if (argc == optind) /* files */
631 if (option_mask32 & OPT_SELINUX) {
634 #endif /* ENABLE_SELINUX */
635 for (i = optind; i < argc; ++i)
636 ok &= statfunc(argv[i], format);
638 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);