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.
17 #define OPT_FILESYS (1 << 0)
18 #define OPT_TERSE (1 << 1)
19 #define OPT_DEREFERENCE (1 << 2)
20 #define OPT_SELINUX (1 << 3)
22 #if ENABLE_FEATURE_STAT_FORMAT
23 typedef bool (*statfunc_ptr)(const char *, const char *);
25 typedef bool (*statfunc_ptr)(const char *);
28 static const char *file_type(const struct stat *st)
30 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
31 * for some of these formats.
32 * To keep diagnostics grammatical in English, the
33 * returned string must start with a consonant.
35 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
36 if (S_ISDIR(st->st_mode)) return "directory";
37 if (S_ISBLK(st->st_mode)) return "block special file";
38 if (S_ISCHR(st->st_mode)) return "character special file";
39 if (S_ISFIFO(st->st_mode)) return "fifo";
40 if (S_ISLNK(st->st_mode)) return "symbolic link";
41 if (S_ISSOCK(st->st_mode)) return "socket";
42 if (S_TYPEISMQ(st)) return "message queue";
43 if (S_TYPEISSEM(st)) return "semaphore";
44 if (S_TYPEISSHM(st)) return "shared memory object";
46 if (S_TYPEISTMO(st)) return "typed memory object";
51 static const char *human_time(time_t t)
56 str[strlen(str)-1] = '\0';
59 /* coreutils 6.3 compat: */
61 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
62 #define buf bb_common_bufsiz1
64 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
69 /* Return the type of the specified file system.
70 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
71 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
72 * Still others have neither and have to get by with f_type (Linux).
74 static const char *human_fstype(uint32_t f_type)
76 static const struct types {
84 { 0xEF53, "ext2/ext3" },
85 { 0x3153464a, "jfs" },
86 { 0x58465342, "xfs" },
87 { 0xF995E849, "hpfs" },
92 { 0x138F, "minix (30 char.)" },
93 { 0x2468, "minix v2" },
94 { 0x2478, "minix v2 (30 char.)" },
101 { 0x012FF7B4, "xenix" },
102 { 0x012FF7B5, "sysv4" },
103 { 0x012FF7B6, "sysv2" },
104 { 0x012FF7B7, "coh" },
105 { 0x00011954, "ufs" },
106 { 0x012FD16D, "xia" },
107 { 0x5346544e, "ntfs" },
108 { 0x1021994, "tmpfs" },
109 { 0x52654973, "reiserfs" },
110 { 0x28cd3d45, "cramfs" },
112 { 0x858458f6, "romfs" },
113 { 0x73717368, "squashfs" },
114 { 0x62656572, "sysfs" },
120 for (i = 0; humantypes[i].type; ++i)
121 if (humantypes[i].type == f_type)
123 return humantypes[i].fs;
126 /* "man statfs" says that statfsbuf->f_fsid is a mess */
127 /* coreutils treats it as an array of ints, most significant first */
128 static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
130 const unsigned *p = (const void*) &statfsbuf->f_fsid;
131 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
132 unsigned long long r = 0;
135 r = (r << (sizeof(unsigned)*8)) | *p++;
140 #if ENABLE_FEATURE_STAT_FORMAT
141 static void strcatc(char *str, char c)
143 int len = strlen(str);
148 static void printfs(char *pformat, const char *msg)
150 strcatc(pformat, 's');
151 printf(pformat, msg);
154 /* print statfs info */
155 static void FAST_FUNC print_statfs(char *pformat, const char m,
156 const char *const filename, const void *data
157 IF_SELINUX(, security_context_t scontext))
159 const struct statfs *statfsbuf = data;
161 printfs(pformat, filename);
162 } else if (m == 'i') {
163 strcat(pformat, "llx");
164 printf(pformat, get_f_fsid(statfsbuf));
165 } else if (m == 'l') {
166 strcat(pformat, "lu");
167 printf(pformat, (unsigned long) statfsbuf->f_namelen);
168 } else if (m == 't') {
169 strcat(pformat, "lx");
170 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
171 } else if (m == 'T') {
172 printfs(pformat, human_fstype(statfsbuf->f_type));
173 } else if (m == 'b') {
174 strcat(pformat, "llu");
175 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
176 } else if (m == 'f') {
177 strcat(pformat, "llu");
178 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
179 } else if (m == 'a') {
180 strcat(pformat, "llu");
181 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
182 } else if (m == 's' || m == 'S') {
183 strcat(pformat, "lu");
184 printf(pformat, (unsigned long) statfsbuf->f_bsize);
185 } else if (m == 'c') {
186 strcat(pformat, "llu");
187 printf(pformat, (unsigned long long) statfsbuf->f_files);
188 } else if (m == 'd') {
189 strcat(pformat, "llu");
190 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
192 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
193 printfs(pformat, scontext);
196 strcatc(pformat, 'c');
201 /* print stat info */
202 static void FAST_FUNC print_stat(char *pformat, const char m,
203 const char *const filename, const void *data
204 IF_SELINUX(, security_context_t scontext))
206 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
207 struct stat *statbuf = (struct stat *) data;
208 struct passwd *pw_ent;
209 struct group *gw_ent;
212 printfs(pformat, filename);
213 } else if (m == 'N') {
214 strcatc(pformat, 's');
215 if (S_ISLNK(statbuf->st_mode)) {
216 char *linkname = xmalloc_readlink_or_warn(filename);
217 if (linkname == NULL)
219 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
220 printf(pformat, filename);
222 printf(pformat, linkname);
225 printf(pformat, filename);
227 } else if (m == 'd') {
228 strcat(pformat, "llu");
229 printf(pformat, (unsigned long long) statbuf->st_dev);
230 } else if (m == 'D') {
231 strcat(pformat, "llx");
232 printf(pformat, (unsigned long long) statbuf->st_dev);
233 } else if (m == 'i') {
234 strcat(pformat, "llu");
235 printf(pformat, (unsigned long long) statbuf->st_ino);
236 } else if (m == 'a') {
237 strcat(pformat, "lo");
238 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
239 } else if (m == 'A') {
240 printfs(pformat, bb_mode_string(statbuf->st_mode));
241 } else if (m == 'f') {
242 strcat(pformat, "lx");
243 printf(pformat, (unsigned long) statbuf->st_mode);
244 } else if (m == 'F') {
245 printfs(pformat, file_type(statbuf));
246 } else if (m == 'h') {
247 strcat(pformat, "lu");
248 printf(pformat, (unsigned long) statbuf->st_nlink);
249 } else if (m == 'u') {
250 strcat(pformat, "lu");
251 printf(pformat, (unsigned long) statbuf->st_uid);
252 } else if (m == 'U') {
254 pw_ent = getpwuid(statbuf->st_uid);
255 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
256 } else if (m == 'g') {
257 strcat(pformat, "lu");
258 printf(pformat, (unsigned long) statbuf->st_gid);
259 } else if (m == 'G') {
261 gw_ent = getgrgid(statbuf->st_gid);
262 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
263 } else if (m == 't') {
264 strcat(pformat, "lx");
265 printf(pformat, (unsigned long) major(statbuf->st_rdev));
266 } else if (m == 'T') {
267 strcat(pformat, "lx");
268 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
269 } else if (m == 's') {
270 strcat(pformat, "llu");
271 printf(pformat, (unsigned long long) statbuf->st_size);
272 } else if (m == 'B') {
273 strcat(pformat, "lu");
274 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
275 } else if (m == 'b') {
276 strcat(pformat, "llu");
277 printf(pformat, (unsigned long long) statbuf->st_blocks);
278 } else if (m == 'o') {
279 strcat(pformat, "lu");
280 printf(pformat, (unsigned long) statbuf->st_blksize);
281 } else if (m == 'x') {
282 printfs(pformat, human_time(statbuf->st_atime));
283 } else if (m == 'X') {
284 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
285 /* note: (unsigned long) would be wrong:
286 * imagine (unsigned long64)int32 */
287 printf(pformat, (long) statbuf->st_atime);
288 } else if (m == 'y') {
289 printfs(pformat, human_time(statbuf->st_mtime));
290 } else if (m == 'Y') {
291 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
292 printf(pformat, (long) statbuf->st_mtime);
293 } else if (m == 'z') {
294 printfs(pformat, human_time(statbuf->st_ctime));
295 } else if (m == 'Z') {
296 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
297 printf(pformat, (long) statbuf->st_ctime);
299 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
300 printfs(pformat, scontext);
303 strcatc(pformat, 'c');
308 static void print_it(const char *masterformat,
309 const char *filename,
310 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
312 IF_SELINUX(, security_context_t scontext))
314 /* Create a working copy of the format string */
315 char *format = xstrdup(masterformat);
316 /* Add 2 to accomodate our conversion of the stat '%s' format string
317 * to the printf '%llu' one. */
318 char *dest = xmalloc(strlen(format) + 2 + 1);
324 char *p = strchr(b, '%');
326 /* coreutils 6.3 always prints <cr> at the end */
327 /*fputs(b, stdout);*/
334 /* dest = "%<modifiers>" */
335 len = strspn(p, "#-+.I 0123456789");
337 memcpy(dest + 1, p, len);
338 dest[1 + len] = '\0';
350 /* Completes "%<modifiers>" with specifier and printfs */
351 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
359 #endif /* FEATURE_STAT_FORMAT */
361 /* Stat the file system and print what we find. */
362 #if !ENABLE_FEATURE_STAT_FORMAT
363 #define do_statfs(filename, format) do_statfs(filename)
365 static bool do_statfs(const char *filename, const char *format)
367 struct statfs statfsbuf;
368 #if !ENABLE_FEATURE_STAT_FORMAT
372 security_context_t scontext = NULL;
374 if (option_mask32 & OPT_SELINUX) {
375 if ((option_mask32 & OPT_DEREFERENCE
376 ? lgetfilecon(filename, &scontext)
377 : getfilecon(filename, &scontext)
380 bb_perror_msg(filename);
385 if (statfs(filename, &statfsbuf) != 0) {
386 bb_perror_msg("can't read file system information for '%s'", filename);
390 #if ENABLE_FEATURE_STAT_FORMAT
391 if (format == NULL) {
393 format = (option_mask32 & OPT_TERSE
394 ? "%n %i %l %t %s %b %f %a %c %d\n"
396 " ID: %-8i Namelen: %-7l Type: %T\n"
397 "Block size: %-10s\n"
398 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
399 "Inodes: Total: %-10c Free: %d");
401 format = (option_mask32 & OPT_TERSE
402 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
403 "%n %i %l %t %s %b %f %a %c %d\n")
404 : (option_mask32 & OPT_SELINUX ?
406 " ID: %-8i Namelen: %-7l Type: %T\n"
407 "Block size: %-10s\n"
408 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
409 "Inodes: Total: %-10c Free: %d"
412 " ID: %-8i Namelen: %-7l Type: %T\n"
413 "Block size: %-10s\n"
414 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
415 "Inodes: Total: %-10c Free: %d\n")
417 # endif /* SELINUX */
419 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
420 #else /* FEATURE_STAT_FORMAT */
421 format = (option_mask32 & OPT_TERSE
424 " ID: %-8llx Namelen: %-7lu ");
427 get_f_fsid(&statfsbuf),
428 statfsbuf.f_namelen);
430 if (option_mask32 & OPT_TERSE)
431 printf("%lx ", (unsigned long) statfsbuf.f_type);
433 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
436 format = (option_mask32 & OPT_TERSE
437 ? "%lu %llu %llu %llu %llu %llu\n"
438 : "Block size: %-10lu\n"
439 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
440 "Inodes: Total: %-10llu Free: %llu\n");
442 (unsigned long) statfsbuf.f_bsize,
443 (unsigned long long) statfsbuf.f_blocks,
444 (unsigned long long) statfsbuf.f_bfree,
445 (unsigned long long) statfsbuf.f_bavail,
446 (unsigned long long) statfsbuf.f_files,
447 (unsigned long long) statfsbuf.f_ffree);
449 format = (option_mask32 & OPT_TERSE
450 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
451 : (option_mask32 & OPT_SELINUX
452 ? "Block size: %-10lu\n"
453 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
454 "Inodes: Total: %-10llu Free: %llu"
456 : "Block size: %-10lu\n"
457 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
458 "Inodes: Total: %-10llu Free: %llu\n"
462 (unsigned long) statfsbuf.f_bsize,
463 (unsigned long long) statfsbuf.f_blocks,
464 (unsigned long long) statfsbuf.f_bfree,
465 (unsigned long long) statfsbuf.f_bavail,
466 (unsigned long long) statfsbuf.f_files,
467 (unsigned long long) statfsbuf.f_ffree,
473 #endif /* FEATURE_STAT_FORMAT */
477 /* stat the file and print what we find */
478 #if !ENABLE_FEATURE_STAT_FORMAT
479 #define do_stat(filename, format) do_stat(filename)
481 static bool do_stat(const char *filename, const char *format)
485 security_context_t scontext = NULL;
487 if (option_mask32 & OPT_SELINUX) {
488 if ((option_mask32 & OPT_DEREFERENCE
489 ? lgetfilecon(filename, &scontext)
490 : getfilecon(filename, &scontext)
493 bb_perror_msg(filename);
498 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
499 bb_perror_msg("can't stat '%s'", filename);
503 #if ENABLE_FEATURE_STAT_FORMAT
504 if (format == NULL) {
506 if (option_mask32 & OPT_TERSE) {
507 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
509 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
512 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
513 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
514 " Device type: %t,%T\n"
515 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
516 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
520 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
521 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
522 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
523 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
527 if (option_mask32 & OPT_TERSE) {
528 format = (option_mask32 & OPT_SELINUX ?
529 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
530 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
532 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
533 format = (option_mask32 & OPT_SELINUX ?
535 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
536 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
537 " Device type: %t,%T\n"
538 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
540 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
542 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
543 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
544 " Device type: %t,%T\n"
545 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
546 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
548 format = (option_mask32 & OPT_SELINUX ?
550 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
551 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
552 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
554 "Access: %x\n" "Modify: %y\n" "Change: %z\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 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
564 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
565 #else /* FEATURE_STAT_FORMAT */
566 if (option_mask32 & OPT_TERSE) {
567 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
568 IF_NOT_SELINUX("\n"),
570 (unsigned long long) statbuf.st_size,
571 (unsigned long long) statbuf.st_blocks,
572 (unsigned long) statbuf.st_mode,
573 (unsigned long) statbuf.st_uid,
574 (unsigned long) statbuf.st_gid,
575 (unsigned long long) statbuf.st_dev,
576 (unsigned long long) statbuf.st_ino,
577 (unsigned long) statbuf.st_nlink,
578 (unsigned long) major(statbuf.st_rdev),
579 (unsigned long) minor(statbuf.st_rdev),
580 (unsigned long) statbuf.st_atime,
581 (unsigned long) statbuf.st_mtime,
582 (unsigned long) statbuf.st_ctime,
583 (unsigned long) statbuf.st_blksize
586 if (option_mask32 & OPT_SELINUX)
587 printf(" %lc\n", *scontext);
592 char *linkname = NULL;
594 struct passwd *pw_ent;
595 struct group *gw_ent;
597 gw_ent = getgrgid(statbuf.st_gid);
599 pw_ent = getpwuid(statbuf.st_uid);
601 if (S_ISLNK(statbuf.st_mode))
602 linkname = xmalloc_readlink_or_warn(filename);
604 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
606 printf(" File: \"%s\"\n", filename);
608 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
609 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
610 (unsigned long long) statbuf.st_size,
611 (unsigned long long) statbuf.st_blocks,
612 (unsigned long) statbuf.st_blksize,
614 (unsigned long long) statbuf.st_dev,
615 (unsigned long long) statbuf.st_dev,
616 (unsigned long long) statbuf.st_ino,
617 (unsigned long) statbuf.st_nlink);
618 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
619 printf(" Device type: %lx,%lx\n",
620 (unsigned long) major(statbuf.st_rdev),
621 (unsigned long) minor(statbuf.st_rdev));
624 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
625 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
626 bb_mode_string(statbuf.st_mode),
627 (unsigned long) statbuf.st_uid,
628 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
629 (unsigned long) statbuf.st_gid,
630 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
632 printf(" S_Context: %lc\n", *scontext);
634 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
635 human_time(statbuf.st_atime),
636 human_time(statbuf.st_mtime),
637 human_time(statbuf.st_ctime));
639 #endif /* FEATURE_STAT_FORMAT */
643 int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
644 int stat_main(int argc UNUSED_PARAM, char **argv)
646 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
650 statfunc_ptr statfunc = do_stat;
652 opt_complementary = "-1"; /* min one arg */
653 opts = getopt32(argv, "ftL"
655 IF_FEATURE_STAT_FORMAT("c:", &format)
657 if (opts & OPT_FILESYS) /* -f */
658 statfunc = do_statfs;
660 if (opts & OPT_SELINUX) {
666 for (i = 0; argv[i]; ++i)
667 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
669 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);