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 char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
26 static char const * file_type(struct stat const *st)
28 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
29 * for some of these formats.
30 * To keep diagnostics grammatical in English, the
31 * returned string must start with a consonant.
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";
44 if (S_TYPEISTMO(st)) return "typed memory object";
49 static char const *human_time(time_t t)
54 str[strlen(str)-1] = '\0';
57 /* coreutils 6.3 compat: */
59 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
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).
68 static char const *human_fstype(long f_type)
71 static const struct types {
73 const char * const fs;
79 { 0xEF53, "ext2/ext3" },
80 { 0x3153464a, "jfs" },
81 { 0x58465342, "xfs" },
82 { 0xF995E849, "hpfs" },
87 { 0x138F, "minix (30 char.)" },
88 { 0x2468, "minix v2" },
89 { 0x2478, "minix v2 (30 char.)" },
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" },
107 { 0x858458f6, "romfs" },
108 { 0x73717368, "squashfs" },
109 { 0x62656572, "sysfs" },
112 for (i = 0; humantypes[i].type; ++i)
113 if (humantypes[i].type == f_type)
115 return humantypes[i].fs;
118 #if ENABLE_FEATURE_STAT_FORMAT
119 /* print statfs info */
120 static 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))
124 struct statfs const *statfsbuf = data;
126 strncat(pformat, "s", buf_len);
127 printf(pformat, filename);
128 } else if (m == 'i') {
129 strncat(pformat, "Lx", buf_len);
130 printf(pformat, statfsbuf->f_fsid);
131 } else if (m == 'l') {
132 strncat(pformat, "lu", buf_len);
133 printf(pformat, statfsbuf->f_namelen);
134 } else if (m == 't') {
135 strncat(pformat, "lx", buf_len);
136 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv */
137 } else if (m == 'T') {
138 strncat(pformat, "s", buf_len);
139 printf(pformat, human_fstype(statfsbuf->f_type));
140 } else if (m == 'b') {
141 strncat(pformat, "jd", buf_len);
142 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
143 } else if (m == 'f') {
144 strncat(pformat, "jd", buf_len);
145 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
146 } else if (m == 'a') {
147 strncat(pformat, "jd", buf_len);
148 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
149 } else if (m == 's' || m == 'S') {
150 strncat(pformat, "lu", buf_len);
151 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
152 } else if (m == 'c') {
153 strncat(pformat, "jd", buf_len);
154 printf(pformat, (intmax_t) (statfsbuf->f_files));
155 } else if (m == 'd') {
156 strncat(pformat, "jd", buf_len);
157 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
159 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
160 strncat(pformat, "s", buf_len);
164 strncat(pformat, "c", buf_len);
169 /* print stat info */
170 static void print_stat(char *pformat, const size_t buf_len, const char m,
171 const char * const filename, void const *data
172 USE_SELINUX(, security_context_t scontext))
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;
180 strncat(pformat, "s", buf_len);
181 printf(pformat, filename);
182 } else if (m == 'N') {
183 strncat(pformat, "s", buf_len);
184 if (S_ISLNK(statbuf->st_mode)) {
185 char *linkname = xmalloc_readlink_or_warn(filename);
186 if (linkname == NULL) {
187 bb_perror_msg("cannot read symbolic link '%s'", filename);
190 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
191 printf(pformat, filename);
193 printf(pformat, linkname);
195 printf(pformat, filename);
197 } else if (m == 'd') {
198 strncat(pformat, "ju", buf_len);
199 printf(pformat, (uintmax_t) statbuf->st_dev);
200 } else if (m == 'D') {
201 strncat(pformat, "jx", buf_len);
202 printf(pformat, (uintmax_t) statbuf->st_dev);
203 } else if (m == 'i') {
204 strncat(pformat, "ju", buf_len);
205 printf(pformat, (uintmax_t) statbuf->st_ino);
206 } else if (m == 'a') {
207 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)));
209 } else if (m == 'A') {
210 strncat(pformat, "s", buf_len);
211 printf(pformat, bb_mode_string(statbuf->st_mode));
212 } else if (m == 'f') {
213 strncat(pformat, "lx", buf_len);
214 printf(pformat, (unsigned long int) statbuf->st_mode);
215 } else if (m == 'F') {
216 strncat(pformat, "s", buf_len);
217 printf(pformat, file_type(statbuf));
218 } else if (m == 'h') {
219 strncat(pformat, "lu", buf_len);
220 printf(pformat, (unsigned long int) statbuf->st_nlink);
221 } else if (m == 'u') {
222 strncat(pformat, "lu", buf_len);
223 printf(pformat, (unsigned long int) statbuf->st_uid);
224 } else if (m == 'U') {
225 strncat(pformat, "s", buf_len);
227 pw_ent = getpwuid(statbuf->st_uid);
228 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
229 } else if (m == 'g') {
230 strncat(pformat, "lu", buf_len);
231 printf(pformat, (unsigned long int) statbuf->st_gid);
232 } else if (m == 'G') {
233 strncat(pformat, "s", buf_len);
235 gw_ent = getgrgid(statbuf->st_gid);
236 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
237 } else if (m == 't') {
238 strncat(pformat, "lx", buf_len);
239 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
240 } else if (m == 'T') {
241 strncat(pformat, "lx", buf_len);
242 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
243 } else if (m == 's') {
244 strncat(pformat, "ju", buf_len);
245 printf(pformat, (uintmax_t) (statbuf->st_size));
246 } else if (m == 'B') {
247 strncat(pformat, "lu", buf_len);
248 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
249 } else if (m == 'b') {
250 strncat(pformat, "ju", buf_len);
251 printf(pformat, (uintmax_t) statbuf->st_blocks);
252 } else if (m == 'o') {
253 strncat(pformat, "lu", buf_len);
254 printf(pformat, (unsigned long int) statbuf->st_blksize);
255 } else if (m == 'x') {
256 strncat(pformat, "s", buf_len);
257 printf(pformat, human_time(statbuf->st_atime));
258 } else if (m == 'X') {
259 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
260 printf(pformat, (unsigned long int) statbuf->st_atime);
261 } else if (m == 'y') {
262 strncat(pformat, "s", buf_len);
263 printf(pformat, human_time(statbuf->st_mtime));
264 } else if (m == 'Y') {
265 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
266 printf(pformat, (unsigned long int) statbuf->st_mtime);
267 } else if (m == 'z') {
268 strncat(pformat, "s", buf_len);
269 printf(pformat, human_time(statbuf->st_ctime));
270 } else if (m == 'Z') {
271 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
272 printf(pformat, (unsigned long int) statbuf->st_ctime);
274 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
275 strncat(pformat, "s", buf_len);
276 printf(pformat, scontext);
279 strncat(pformat, "c", buf_len);
284 static void print_it(char const *masterformat, char const *filename,
285 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) )
291 /* create a working copy of the format string */
292 char *format = xstrdup(masterformat);
294 /* Add 2 to accomodate our conversion of the stat '%s' format string
295 * to the printf '%llu' one. */
296 size_t n_alloc = strlen(format) + 2 + 1;
297 char *dest = xmalloc(n_alloc);
302 char *p = strchr(b, '%');
304 /* coreutils 6.3 always print <cr> at the end */
305 /*fputs(b, stdout);*/
312 len = strspn(p, "#-+.I 0123456789");
314 memcpy(dest + 1, p, len);
327 print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext));
337 /* Stat the file system and print what we find. */
338 static bool do_statfs(char const *filename, char const *format)
340 struct statfs statfsbuf;
342 security_context_t scontext = NULL;
344 if (option_mask32 & OPT_SELINUX) {
345 if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
346 getfilecon(filename, scontext))< 0) {
347 bb_perror_msg(filename);
352 if (statfs(filename, &statfsbuf) != 0) {
353 bb_perror_msg("cannot read file system information for '%s'", filename);
357 #if ENABLE_FEATURE_STAT_FORMAT
360 format = (option_mask32 & OPT_TERSE
361 ? "%n %i %l %t %s %b %f %a %c %d\n"
363 " ID: %-8i Namelen: %-7l Type: %T\n"
364 "Block size: %-10s\n"
365 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
366 "Inodes: Total: %-10c Free: %d");
367 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
369 format = (option_mask32 & OPT_TERSE
370 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
371 "%n %i %l %t %s %b %f %a %c %d\n")
372 : (option_mask32 & OPT_SELINUX ?
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"
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")
385 print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
387 #else /* FEATURE_STAT_FORMAT */
388 format = (option_mask32 & OPT_TERSE
391 " ID: %-8Lx Namelen: %-7lu ");
395 statfsbuf.f_namelen);
397 if (option_mask32 & OPT_TERSE)
398 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
400 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
403 format = (option_mask32 & OPT_TERSE
404 ? "%lu %ld %ld %ld %ld %ld\n"
405 : "Block size: %-10lu\n"
406 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
407 "Inodes: Total: %-10jd Free: %jd\n");
409 (unsigned long int) (statfsbuf.f_bsize),
410 (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));
416 format = (option_mask32 & OPT_TERSE
417 ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
418 "%lu %ld %ld %ld %ld %ld\n")
419 : (option_mask32 & OPT_SELINUX ?
420 "Block size: %-10lu\n"
421 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
422 "Inodes: Total: %-10jd Free: %jd"
424 "Block size: %-10lu\n"
425 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
426 "Inodes: Total: %-10jd Free: %jd\n"));
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),
439 #endif /* FEATURE_STAT_FORMAT */
443 /* stat the file and print what we find */
444 static bool do_stat(char const *filename, char const *format)
448 security_context_t scontext = NULL;
450 if (option_mask32 & OPT_SELINUX) {
451 if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
452 getfilecon(filename, scontext))< 0) {
453 bb_perror_msg (filename);
458 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
459 bb_perror_msg("cannot stat '%s'", filename);
463 #if ENABLE_FEATURE_STAT_FORMAT
464 if (format == NULL) {
466 if (option_mask32 & OPT_TERSE) {
467 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
469 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
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";
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";
487 if (option_mask32 & OPT_TERSE) {
488 format = (option_mask32 & OPT_SELINUX ?
489 "%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");
492 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
493 format = (option_mask32 & OPT_SELINUX ?
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"
500 "Access: %x\n" "Modify: %y\n" "Change: %z\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");
508 format = (option_mask32 & OPT_SELINUX ?
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"
514 "Access: %x\n" "Modify: %y\n" "Change: %z\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");
524 print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
525 #else /* FEATURE_STAT_FORMAT */
526 if (option_mask32 & OPT_TERSE) {
527 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
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
546 if (option_mask32 & OPT_SELINUX)
547 printf(" %lc\n", *scontext);
552 char *linkname = NULL;
554 struct passwd *pw_ent;
555 struct group *gw_ent;
557 gw_ent = getgrgid(statbuf.st_gid);
559 pw_ent = getpwuid(statbuf.st_uid);
561 if (S_ISLNK(statbuf.st_mode))
562 linkname = xmalloc_readlink_or_warn(filename);
564 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
566 printf(" File: \"%s\"\n", filename);
568 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
569 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
570 (uintmax_t) (statbuf.st_size),
571 (uintmax_t) statbuf.st_blocks,
572 (unsigned long int) statbuf.st_blksize,
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));
584 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
585 (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,
590 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
592 printf(" S_Context: %lc\n", *scontext);
594 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
595 human_time(statbuf.st_atime),
596 human_time(statbuf.st_mtime),
597 human_time(statbuf.st_ctime));
599 #endif /* FEATURE_STAT_FORMAT */
603 int stat_main(int argc, char **argv);
604 int stat_main(int argc, char **argv)
609 bool (*statfunc)(char const *, char const *) = do_stat;
611 getopt32(argc, argv, "ftL"
613 USE_FEATURE_STAT_FORMAT("c:", &format)
616 if (option_mask32 & OPT_FILESYS) /* -f */
617 statfunc = do_statfs;
618 if (argc == optind) /* files */
622 if (option_mask32 & OPT_SELINUX) {
625 #endif /* ENABLE_SELINUX */
626 for (i = optind; i < argc; ++i)
627 ok &= statfunc(argv[i], format);
629 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);