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>
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 /* vars to control behavior */
19 #define OPT_DEREFERENCE 4
22 static char const *file_type(struct stat const *st)
24 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
25 * for some of these formats.
26 * To keep diagnostics grammatical in English, the
27 * returned string must start with a consonant.
29 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
30 if (S_ISDIR(st->st_mode)) return "directory";
31 if (S_ISBLK(st->st_mode)) return "block special file";
32 if (S_ISCHR(st->st_mode)) return "character special file";
33 if (S_ISFIFO(st->st_mode)) return "fifo";
34 if (S_ISLNK(st->st_mode)) return "symbolic link";
35 if (S_ISSOCK(st->st_mode)) return "socket";
36 if (S_TYPEISMQ(st)) return "message queue";
37 if (S_TYPEISSEM(st)) return "semaphore";
38 if (S_TYPEISSHM(st)) return "shared memory object";
40 if (S_TYPEISTMO(st)) return "typed memory object";
45 static char const *human_time(time_t t)
50 str[strlen(str)-1] = '\0';
53 /* coreutils 6.3 compat: */
54 static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
55 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
59 /* Return the type of the specified file system.
60 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
61 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
62 * Still others have neither and have to get by with f_type (Linux).
64 static char const *human_fstype(long f_type)
67 static const struct types {
75 { 0xEF53, "ext2/ext3" },
76 { 0x3153464a, "jfs" },
77 { 0x58465342, "xfs" },
78 { 0xF995E849, "hpfs" },
83 { 0x138F, "minix (30 char.)" },
84 { 0x2468, "minix v2" },
85 { 0x2478, "minix v2 (30 char.)" },
92 { 0x012FF7B4, "xenix" },
93 { 0x012FF7B5, "sysv4" },
94 { 0x012FF7B6, "sysv2" },
95 { 0x012FF7B7, "coh" },
96 { 0x00011954, "ufs" },
97 { 0x012FD16D, "xia" },
98 { 0x5346544e, "ntfs" },
99 { 0x1021994, "tmpfs" },
100 { 0x52654973, "reiserfs" },
101 { 0x28cd3d45, "cramfs" },
103 { 0x858458f6, "romfs" },
104 { 0x73717368, "squashfs" },
105 { 0x62656572, "sysfs" },
108 for (i=0; humantypes[i].type; ++i)
109 if (humantypes[i].type == f_type)
111 return humantypes[i].fs;
114 #ifdef CONFIG_FEATURE_STAT_FORMAT
115 /* print statfs info */
116 static void print_statfs(char *pformat, size_t buf_len, char m,
117 char const *filename, void const *data)
119 struct statfs const *statfsbuf = data;
123 strncat(pformat, "s", buf_len);
124 printf(pformat, filename);
127 strncat(pformat, "Lx", buf_len);
128 printf(pformat, statfsbuf->f_fsid);
131 strncat(pformat, "lu", buf_len);
132 printf(pformat, statfsbuf->f_namelen);
135 strncat(pformat, "lx", buf_len);
136 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
139 strncat(pformat, "s", buf_len);
140 printf(pformat, human_fstype(statfsbuf->f_type));
143 strncat(pformat, "jd", buf_len);
144 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
147 strncat(pformat, "jd", buf_len);
148 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
151 strncat(pformat, "jd", buf_len);
152 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
156 strncat(pformat, "lu", buf_len);
157 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
160 strncat(pformat, "jd", buf_len);
161 printf(pformat, (intmax_t) (statfsbuf->f_files));
164 strncat(pformat, "jd", buf_len);
165 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
168 strncat(pformat, "c", buf_len);
174 /* print stat info */
175 static void print_stat(char *pformat, size_t buf_len, char m,
176 char const *filename, void const *data)
178 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
179 struct stat *statbuf = (struct stat *) data;
180 struct passwd *pw_ent;
181 struct group *gw_ent;
185 strncat(pformat, "s", buf_len);
186 printf(pformat, filename);
189 strncat(pformat, "s", buf_len);
190 if (S_ISLNK(statbuf->st_mode)) {
191 char *linkname = xreadlink(filename);
192 if (linkname == NULL) {
193 bb_perror_msg("cannot read symbolic link '%s'", filename);
196 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
197 printf(pformat, filename);
199 printf(pformat, linkname);
201 printf(pformat, filename);
205 strncat(pformat, "ju", buf_len);
206 printf(pformat, (uintmax_t) statbuf->st_dev);
209 strncat(pformat, "jx", buf_len);
210 printf(pformat, (uintmax_t) statbuf->st_dev);
213 strncat(pformat, "ju", buf_len);
214 printf(pformat, (uintmax_t) statbuf->st_ino);
217 strncat(pformat, "lo", buf_len);
218 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
221 strncat(pformat, "s", buf_len);
222 printf(pformat, bb_mode_string(statbuf->st_mode));
225 strncat(pformat, "lx", buf_len);
226 printf(pformat, (unsigned long int) statbuf->st_mode);
229 strncat(pformat, "s", buf_len);
230 printf(pformat, file_type(statbuf));
233 strncat(pformat, "lu", buf_len);
234 printf(pformat, (unsigned long int) statbuf->st_nlink);
237 strncat(pformat, "lu", buf_len);
238 printf(pformat, (unsigned long int) statbuf->st_uid);
241 strncat(pformat, "s", buf_len);
243 pw_ent = getpwuid(statbuf->st_uid);
244 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
247 strncat(pformat, "lu", buf_len);
248 printf(pformat, (unsigned long int) statbuf->st_gid);
251 strncat(pformat, "s", buf_len);
253 gw_ent = getgrgid(statbuf->st_gid);
254 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
257 strncat(pformat, "lx", buf_len);
258 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
261 strncat(pformat, "lx", buf_len);
262 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
265 strncat(pformat, "ju", buf_len);
266 printf(pformat, (uintmax_t) (statbuf->st_size));
269 strncat(pformat, "lu", buf_len);
270 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
273 strncat(pformat, "ju", buf_len);
274 printf(pformat, (uintmax_t) statbuf->st_blocks);
277 strncat(pformat, "lu", buf_len);
278 printf(pformat, (unsigned long int) statbuf->st_blksize);
281 strncat(pformat, "s", buf_len);
282 printf(pformat, human_time(statbuf->st_atime));
285 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
286 printf(pformat, (unsigned long int) statbuf->st_atime);
289 strncat(pformat, "s", buf_len);
290 printf(pformat, human_time(statbuf->st_mtime));
293 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
294 printf(pformat, (unsigned long int) statbuf->st_mtime);
297 strncat(pformat, "s", buf_len);
298 printf(pformat, human_time(statbuf->st_ctime));
301 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
302 printf(pformat, (unsigned long int) statbuf->st_ctime);
305 strncat(pformat, "c", buf_len);
311 static void print_it(char const *masterformat, char const *filename,
312 void (*print_func) (char *, size_t, char, char const *, void const *),
317 /* create a working copy of the format string */
318 char *format = xstrdup(masterformat);
320 /* Add 2 to accomodate our conversion of the stat '%s' format string
321 * to the printf '%llu' one. */
322 size_t n_alloc = strlen(format) + 2 + 1;
323 char *dest = xmalloc(n_alloc);
328 char *p = strchr(b, '%');
330 /* coreutils 6.3 always print <cr> at the end */
331 /*fputs(b, stdout);*/
338 len = strspn(p, "#-+.I 0123456789");
340 memcpy(dest + 1, p, len);
353 print_func(dest, n_alloc, *p, filename, data);
363 /* Stat the file system and print what we find. */
364 static int do_statfs(char const *filename, char const *format)
366 struct statfs statfsbuf;
368 if (statfs(filename, &statfsbuf) != 0) {
369 bb_perror_msg("cannot read file system information for '%s'", filename);
373 #ifdef CONFIG_FEATURE_STAT_FORMAT
375 format = (flags & OPT_TERSE
376 ? "%n %i %l %t %s %b %f %a %c %d\n"
378 " ID: %-8i Namelen: %-7l Type: %T\n"
379 "Block size: %-10s\n"
380 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
381 "Inodes: Total: %-10c Free: %d");
382 print_it(format, filename, print_statfs, &statfsbuf);
385 format = (flags & OPT_TERSE
388 " ID: %-8Lx Namelen: %-7lu ");
392 statfsbuf.f_namelen);
394 if (flags & OPT_TERSE)
395 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
397 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
399 format = (flags & OPT_TERSE
400 ? "%lu %ld %ld %ld %ld %ld\n"
401 : "Block size: %-10lu\n"
402 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
403 "Inodes: Total: %-10jd Free: %jd\n");
405 (unsigned long int) (statfsbuf.f_bsize),
406 (intmax_t) (statfsbuf.f_blocks),
407 (intmax_t) (statfsbuf.f_bfree),
408 (intmax_t) (statfsbuf.f_bavail),
409 (intmax_t) (statfsbuf.f_files),
410 (intmax_t) (statfsbuf.f_ffree));
416 /* stat the file and print what we find */
417 static int do_stat(char const *filename, char const *format)
421 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
422 bb_perror_msg("cannot stat '%s'", filename);
426 #ifdef CONFIG_FEATURE_STAT_FORMAT
427 if (format == NULL) {
428 if (flags & OPT_TERSE) {
429 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
431 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
434 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
435 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
436 " Device type: %t,%T\n"
437 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
438 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
442 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
443 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
444 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
445 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
449 print_it(format, filename, print_stat, &statbuf);
451 if (flags & OPT_TERSE) {
452 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
454 (uintmax_t) (statbuf.st_size),
455 (uintmax_t) statbuf.st_blocks,
456 (unsigned long int) statbuf.st_mode,
457 (unsigned long int) statbuf.st_uid,
458 (unsigned long int) statbuf.st_gid,
459 (uintmax_t) statbuf.st_dev,
460 (uintmax_t) statbuf.st_ino,
461 (unsigned long int) statbuf.st_nlink,
462 (unsigned long int) major(statbuf.st_rdev),
463 (unsigned long int) minor(statbuf.st_rdev),
464 (unsigned long int) statbuf.st_atime,
465 (unsigned long int) statbuf.st_mtime,
466 (unsigned long int) statbuf.st_ctime,
467 (unsigned long int) statbuf.st_blksize
470 char *linkname = NULL;
472 struct passwd *pw_ent;
473 struct group *gw_ent;
475 gw_ent = getgrgid(statbuf.st_gid);
477 pw_ent = getpwuid(statbuf.st_uid);
479 if (S_ISLNK(statbuf.st_mode))
480 linkname = xreadlink(filename);
482 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
484 printf(" File: \"%s\"\n", filename);
486 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
487 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
488 (uintmax_t) (statbuf.st_size),
489 (uintmax_t) statbuf.st_blocks,
490 (unsigned long int) statbuf.st_blksize,
492 (uintmax_t) statbuf.st_dev,
493 (uintmax_t) statbuf.st_dev,
494 (uintmax_t) statbuf.st_ino,
495 (unsigned long int) statbuf.st_nlink);
496 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
497 printf(" Device type: %lx,%lx\n",
498 (unsigned long int) major(statbuf.st_rdev),
499 (unsigned long int) minor(statbuf.st_rdev));
502 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
503 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
504 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
505 bb_mode_string(statbuf.st_mode),
506 (unsigned long int) statbuf.st_uid,
507 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
508 (unsigned long int) statbuf.st_gid,
509 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
510 human_time(statbuf.st_atime),
511 human_time(statbuf.st_mtime),
512 human_time(statbuf.st_ctime));
518 int stat_main(int argc, char **argv)
523 int (*statfunc)(char const *, char const *) = do_stat;
525 flags = getopt32(argc, argv, "ftL"
526 USE_FEATURE_STAT_FORMAT("c:", &format)
529 if (flags & 1) /* -f */
530 statfunc = do_statfs;
531 if (argc == optind) /* files */
534 for (i = optind; i < argc; ++i)
535 ok &= statfunc(argv[i], format);
537 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);