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>
9 * Written by Michael Meskes
10 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 #include <sys/types.h>
22 #include <getopt.h> /* optind */
24 #include <sys/statfs.h>
25 #include <sys/statvfs.h>
29 /* vars to control behavior */
31 #define OPT_DEREFERENCE 4
34 static char const *file_type(struct stat const *st)
36 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
37 * for some of these formats.
38 * To keep diagnostics grammatical in English, the
39 * returned string must start with a consonant.
41 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
42 if (S_ISDIR(st->st_mode)) return "directory";
43 if (S_ISBLK(st->st_mode)) return "block special file";
44 if (S_ISCHR(st->st_mode)) return "character special file";
45 if (S_ISFIFO(st->st_mode)) return "fifo";
46 if (S_ISLNK(st->st_mode)) return "symbolic link";
47 if (S_ISSOCK(st->st_mode)) return "socket";
48 if (S_TYPEISMQ(st)) return "message queue";
49 if (S_TYPEISSEM(st)) return "semaphore";
50 if (S_TYPEISSHM(st)) return "shared memory object";
52 if (S_TYPEISTMO(st)) return "typed memory object";
57 static char const *human_time(time_t t)
61 str[strlen(str)-1] = '\0';
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 char const *human_fstype(long f_type)
73 static const struct types {
81 { 0xEF53, "ext2/ext3" },
82 { 0x3153464a, "jfs" },
83 { 0x58465342, "xfs" },
84 { 0xF995E849, "hpfs" },
89 { 0x138F, "minix (30 char.)" },
90 { 0x2468, "minix v2" },
91 { 0x2478, "minix v2 (30 char.)" },
98 { 0x012FF7B4, "xenix" },
99 { 0x012FF7B5, "sysv4" },
100 { 0x012FF7B6, "sysv2" },
101 { 0x012FF7B7, "coh" },
102 { 0x00011954, "ufs" },
103 { 0x012FD16D, "xia" },
104 { 0x5346544e, "ntfs" },
105 { 0x1021994, "tmpfs" },
106 { 0x52654973, "reiserfs" },
107 { 0x28cd3d45, "cramfs" },
109 { 0x858458f6, "romfs" },
110 { 0x73717368, "squashfs" },
111 { 0x62656572, "sysfs" },
114 for (i=0; humantypes[i].type; ++i)
115 if (humantypes[i].type == f_type)
117 return humantypes[i].fs;
120 #ifdef CONFIG_FEATURE_STAT_FORMAT
121 /* print statfs info */
122 static void print_statfs(char *pformat, size_t buf_len, char m,
123 char const *filename, void const *data)
125 struct statfs const *statfsbuf = data;
129 strncat(pformat, "s", buf_len);
130 printf(pformat, filename);
133 strncat(pformat, "Lx", buf_len);
134 printf(pformat, statfsbuf->f_fsid);
137 strncat(pformat, "lu", buf_len);
138 printf(pformat, statfsbuf->f_namelen);
141 strncat(pformat, "lx", buf_len);
142 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
145 strncat(pformat, "s", buf_len);
146 printf(pformat, human_fstype(statfsbuf->f_type));
149 strncat(pformat, "jd", buf_len);
150 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
153 strncat(pformat, "jd", buf_len);
154 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
157 strncat(pformat, "jd", buf_len);
158 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
162 strncat(pformat, "lu", buf_len);
163 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
166 strncat(pformat, "jd", buf_len);
167 printf(pformat, (intmax_t) (statfsbuf->f_files));
170 strncat(pformat, "jd", buf_len);
171 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
174 strncat(pformat, "c", buf_len);
180 /* print stat info */
181 static void print_stat(char *pformat, size_t buf_len, char m,
182 char const *filename, void const *data)
184 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
185 struct stat *statbuf = (struct stat *) data;
186 struct passwd *pw_ent;
187 struct group *gw_ent;
191 strncat(pformat, "s", buf_len);
192 printf(pformat, filename);
195 strncat(pformat, "s", buf_len);
196 if (S_ISLNK(statbuf->st_mode)) {
197 char *linkname = xreadlink(filename);
198 if (linkname == NULL) {
199 bb_perror_msg("cannot read symbolic link '%s'", filename);
202 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
203 printf(pformat, filename);
205 printf(pformat, linkname);
207 printf(pformat, filename);
211 strncat(pformat, "ju", buf_len);
212 printf(pformat, (uintmax_t) statbuf->st_dev);
215 strncat(pformat, "jx", buf_len);
216 printf(pformat, (uintmax_t) statbuf->st_dev);
219 strncat(pformat, "ju", buf_len);
220 printf(pformat, (uintmax_t) statbuf->st_ino);
223 strncat(pformat, "lo", buf_len);
224 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
227 strncat(pformat, "s", buf_len);
228 printf(pformat, bb_mode_string(statbuf->st_mode));
231 strncat(pformat, "lx", buf_len);
232 printf(pformat, (unsigned long int) statbuf->st_mode);
235 strncat(pformat, "s", buf_len);
236 printf(pformat, file_type(statbuf));
239 strncat(pformat, "lu", buf_len);
240 printf(pformat, (unsigned long int) statbuf->st_nlink);
243 strncat(pformat, "lu", buf_len);
244 printf(pformat, (unsigned long int) statbuf->st_uid);
247 strncat(pformat, "s", buf_len);
249 pw_ent = getpwuid(statbuf->st_uid);
250 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
253 strncat(pformat, "lu", buf_len);
254 printf(pformat, (unsigned long int) statbuf->st_gid);
257 strncat(pformat, "s", buf_len);
259 gw_ent = getgrgid(statbuf->st_gid);
260 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
263 strncat(pformat, "lx", buf_len);
264 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
267 strncat(pformat, "lx", buf_len);
268 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
271 strncat(pformat, "ju", buf_len);
272 printf(pformat, (uintmax_t) (statbuf->st_size));
275 strncat(pformat, "lu", buf_len);
276 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
279 strncat(pformat, "ju", buf_len);
280 printf(pformat, (uintmax_t) statbuf->st_blocks);
283 strncat(pformat, "lu", buf_len);
284 printf(pformat, (unsigned long int) statbuf->st_blksize);
287 strncat(pformat, "s", buf_len);
288 printf(pformat, human_time(statbuf->st_atime));
291 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
292 printf(pformat, (unsigned long int) statbuf->st_atime);
295 strncat(pformat, "s", buf_len);
296 printf(pformat, human_time(statbuf->st_mtime));
299 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
300 printf(pformat, (unsigned long int) statbuf->st_mtime);
303 strncat(pformat, "s", buf_len);
304 printf(pformat, human_time(statbuf->st_ctime));
307 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
308 printf(pformat, (unsigned long int) statbuf->st_ctime);
311 strncat(pformat, "c", buf_len);
317 static void print_it(char const *masterformat, char const *filename,
318 void (*print_func) (char *, size_t, char, char const *, void const *),
323 /* create a working copy of the format string */
324 char *format = bb_xstrdup(masterformat);
326 /* Add 2 to accommodate our conversion of the stat `%s' format string
327 * to the printf `%llu' one. */
328 size_t n_alloc = strlen(format) + 2 + 1;
329 char *dest = xmalloc(n_alloc);
333 char *p = strchr(b, '%');
339 len = strspn(p, "#-+.I 0123456789");
341 memcpy(dest + 1, p, len);
354 print_func(dest, n_alloc, *p, filename, data);
369 /* Stat the file system and print what we find. */
370 static int do_statfs(char const *filename, char const *format)
372 struct statfs statfsbuf;
374 if (statfs(filename, &statfsbuf) != 0) {
375 bb_perror_msg("cannot read file system information for '%s'", filename);
379 #ifdef CONFIG_FEATURE_STAT_FORMAT
381 format = (flags & OPT_TERSE
382 ? "%n %i %l %t %s %b %f %a %c %d\n"
384 " ID: %-8i Namelen: %-7l Type: %T\n"
385 "Block size: %-10s\n"
386 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
387 "Inodes: Total: %-10c Free: %d\n");
388 print_it(format, filename, print_statfs, &statfsbuf);
391 format = (flags & OPT_TERSE
394 " ID: %-8Lx Namelen: %-7lu ");
398 statfsbuf.f_namelen);
400 if (flags & OPT_TERSE)
401 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
403 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
405 format = (flags & OPT_TERSE
406 ? "%lu %ld %ld %ld %ld %ld\n"
407 : "Block size: %-10lu\n"
408 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
409 "Inodes: Total: %-10jd Free: %jd\n");
411 (unsigned long int) (statfsbuf.f_bsize),
412 (intmax_t) (statfsbuf.f_blocks),
413 (intmax_t) (statfsbuf.f_bfree),
414 (intmax_t) (statfsbuf.f_bavail),
415 (intmax_t) (statfsbuf.f_files),
416 (intmax_t) (statfsbuf.f_ffree));
422 /* stat the file and print what we find */
423 static int do_stat(char const *filename, char const *format)
427 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
428 bb_perror_msg("cannot stat '%s'", filename);
432 #ifdef CONFIG_FEATURE_STAT_FORMAT
433 if (format == NULL) {
434 if (flags & OPT_TERSE) {
435 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
437 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
440 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
441 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
442 " Device type: %t,%T\n"
443 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
444 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
448 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
449 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
450 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
451 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
455 print_it(format, filename, print_stat, &statbuf);
457 if (flags & OPT_TERSE) {
458 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
460 (uintmax_t) (statbuf.st_size),
461 (uintmax_t) statbuf.st_blocks,
462 (unsigned long int) statbuf.st_mode,
463 (unsigned long int) statbuf.st_uid,
464 (unsigned long int) statbuf.st_gid,
465 (uintmax_t) statbuf.st_dev,
466 (uintmax_t) statbuf.st_ino,
467 (unsigned long int) statbuf.st_nlink,
468 (unsigned long int) major(statbuf.st_rdev),
469 (unsigned long int) minor(statbuf.st_rdev),
470 (unsigned long int) statbuf.st_atime,
471 (unsigned long int) statbuf.st_mtime,
472 (unsigned long int) statbuf.st_ctime,
473 (unsigned long int) statbuf.st_blksize
476 char *linkname = NULL;
478 struct passwd *pw_ent;
479 struct group *gw_ent;
481 gw_ent = getgrgid(statbuf.st_gid);
483 pw_ent = getpwuid(statbuf.st_uid);
485 if (S_ISLNK(statbuf.st_mode))
486 linkname = xreadlink(filename);
488 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
490 printf(" File: \"%s\"\n", filename);
492 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
493 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
494 (uintmax_t) (statbuf.st_size),
495 (uintmax_t) statbuf.st_blocks,
496 (unsigned long int) statbuf.st_blksize,
498 (uintmax_t) statbuf.st_dev,
499 (uintmax_t) statbuf.st_dev,
500 (uintmax_t) statbuf.st_ino,
501 (unsigned long int) statbuf.st_nlink);
502 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
503 printf(" Device type: %lx,%lx\n",
504 (unsigned long int) major(statbuf.st_rdev),
505 (unsigned long int) minor(statbuf.st_rdev));
508 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
509 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
510 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
511 bb_mode_string(statbuf.st_mode),
512 (unsigned long int) statbuf.st_uid,
513 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
514 (unsigned long int) statbuf.st_gid,
515 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
516 human_time(statbuf.st_atime),
517 human_time(statbuf.st_mtime),
518 human_time(statbuf.st_ctime));
524 int stat_main(int argc, char **argv)
529 int (*statfunc)(char const *, char const *) = do_stat;
531 flags = bb_getopt_ulflags(argc, argv, "ftL"
532 USE_FEATURE_STAT_FORMAT("c:", &format)
535 if (flags & 1) /* -f */
536 statfunc = do_statfs;
537 if (argc == optind) /* files */
540 for (i = optind; i < argc; ++i)
541 ok &= statfunc(argv[i], format);
543 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);