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)
49 str[strlen(str)-1] = '\0';
53 /* Return the type of the specified file system.
54 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
55 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
56 * Still others have neither and have to get by with f_type (Linux).
58 static char const *human_fstype(long f_type)
61 static const struct types {
69 { 0xEF53, "ext2/ext3" },
70 { 0x3153464a, "jfs" },
71 { 0x58465342, "xfs" },
72 { 0xF995E849, "hpfs" },
77 { 0x138F, "minix (30 char.)" },
78 { 0x2468, "minix v2" },
79 { 0x2478, "minix v2 (30 char.)" },
86 { 0x012FF7B4, "xenix" },
87 { 0x012FF7B5, "sysv4" },
88 { 0x012FF7B6, "sysv2" },
89 { 0x012FF7B7, "coh" },
90 { 0x00011954, "ufs" },
91 { 0x012FD16D, "xia" },
92 { 0x5346544e, "ntfs" },
93 { 0x1021994, "tmpfs" },
94 { 0x52654973, "reiserfs" },
95 { 0x28cd3d45, "cramfs" },
97 { 0x858458f6, "romfs" },
98 { 0x73717368, "squashfs" },
99 { 0x62656572, "sysfs" },
102 for (i=0; humantypes[i].type; ++i)
103 if (humantypes[i].type == f_type)
105 return humantypes[i].fs;
108 #ifdef CONFIG_FEATURE_STAT_FORMAT
109 /* print statfs info */
110 static void print_statfs(char *pformat, size_t buf_len, char m,
111 char const *filename, void const *data)
113 struct statfs const *statfsbuf = data;
117 strncat(pformat, "s", buf_len);
118 printf(pformat, filename);
121 strncat(pformat, "Lx", buf_len);
122 printf(pformat, statfsbuf->f_fsid);
125 strncat(pformat, "lu", buf_len);
126 printf(pformat, statfsbuf->f_namelen);
129 strncat(pformat, "lx", buf_len);
130 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
133 strncat(pformat, "s", buf_len);
134 printf(pformat, human_fstype(statfsbuf->f_type));
137 strncat(pformat, "jd", buf_len);
138 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
141 strncat(pformat, "jd", buf_len);
142 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
145 strncat(pformat, "jd", buf_len);
146 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
150 strncat(pformat, "lu", buf_len);
151 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
154 strncat(pformat, "jd", buf_len);
155 printf(pformat, (intmax_t) (statfsbuf->f_files));
158 strncat(pformat, "jd", buf_len);
159 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
162 strncat(pformat, "c", buf_len);
168 /* print stat info */
169 static void print_stat(char *pformat, size_t buf_len, char m,
170 char const *filename, void const *data)
172 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
173 struct stat *statbuf = (struct stat *) data;
174 struct passwd *pw_ent;
175 struct group *gw_ent;
179 strncat(pformat, "s", buf_len);
180 printf(pformat, filename);
183 strncat(pformat, "s", buf_len);
184 if (S_ISLNK(statbuf->st_mode)) {
185 char *linkname = xreadlink(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);
199 strncat(pformat, "ju", buf_len);
200 printf(pformat, (uintmax_t) statbuf->st_dev);
203 strncat(pformat, "jx", buf_len);
204 printf(pformat, (uintmax_t) statbuf->st_dev);
207 strncat(pformat, "ju", buf_len);
208 printf(pformat, (uintmax_t) statbuf->st_ino);
211 strncat(pformat, "lo", buf_len);
212 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
215 strncat(pformat, "s", buf_len);
216 printf(pformat, bb_mode_string(statbuf->st_mode));
219 strncat(pformat, "lx", buf_len);
220 printf(pformat, (unsigned long int) statbuf->st_mode);
223 strncat(pformat, "s", buf_len);
224 printf(pformat, file_type(statbuf));
227 strncat(pformat, "lu", buf_len);
228 printf(pformat, (unsigned long int) statbuf->st_nlink);
231 strncat(pformat, "lu", buf_len);
232 printf(pformat, (unsigned long int) statbuf->st_uid);
235 strncat(pformat, "s", buf_len);
237 pw_ent = getpwuid(statbuf->st_uid);
238 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
241 strncat(pformat, "lu", buf_len);
242 printf(pformat, (unsigned long int) statbuf->st_gid);
245 strncat(pformat, "s", buf_len);
247 gw_ent = getgrgid(statbuf->st_gid);
248 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
251 strncat(pformat, "lx", buf_len);
252 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
255 strncat(pformat, "lx", buf_len);
256 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
259 strncat(pformat, "ju", buf_len);
260 printf(pformat, (uintmax_t) (statbuf->st_size));
263 strncat(pformat, "lu", buf_len);
264 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
267 strncat(pformat, "ju", buf_len);
268 printf(pformat, (uintmax_t) statbuf->st_blocks);
271 strncat(pformat, "lu", buf_len);
272 printf(pformat, (unsigned long int) statbuf->st_blksize);
275 strncat(pformat, "s", buf_len);
276 printf(pformat, human_time(statbuf->st_atime));
279 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
280 printf(pformat, (unsigned long int) statbuf->st_atime);
283 strncat(pformat, "s", buf_len);
284 printf(pformat, human_time(statbuf->st_mtime));
287 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
288 printf(pformat, (unsigned long int) statbuf->st_mtime);
291 strncat(pformat, "s", buf_len);
292 printf(pformat, human_time(statbuf->st_ctime));
295 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
296 printf(pformat, (unsigned long int) statbuf->st_ctime);
299 strncat(pformat, "c", buf_len);
305 static void print_it(char const *masterformat, char const *filename,
306 void (*print_func) (char *, size_t, char, char const *, void const *),
311 /* create a working copy of the format string */
312 char *format = xstrdup(masterformat);
314 /* Add 2 to accommodate our conversion of the stat `%s' format string
315 * to the printf `%llu' one. */
316 size_t n_alloc = strlen(format) + 2 + 1;
317 char *dest = xmalloc(n_alloc);
321 char *p = strchr(b, '%');
327 len = strspn(p, "#-+.I 0123456789");
329 memcpy(dest + 1, p, len);
342 print_func(dest, n_alloc, *p, filename, data);
357 /* Stat the file system and print what we find. */
358 static int do_statfs(char const *filename, char const *format)
360 struct statfs statfsbuf;
362 if (statfs(filename, &statfsbuf) != 0) {
363 bb_perror_msg("cannot read file system information for '%s'", filename);
367 #ifdef CONFIG_FEATURE_STAT_FORMAT
369 format = (flags & OPT_TERSE
370 ? "%n %i %l %t %s %b %f %a %c %d\n"
372 " ID: %-8i Namelen: %-7l Type: %T\n"
373 "Block size: %-10s\n"
374 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
375 "Inodes: Total: %-10c Free: %d\n");
376 print_it(format, filename, print_statfs, &statfsbuf);
379 format = (flags & OPT_TERSE
382 " ID: %-8Lx Namelen: %-7lu ");
386 statfsbuf.f_namelen);
388 if (flags & OPT_TERSE)
389 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
391 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
393 format = (flags & OPT_TERSE
394 ? "%lu %ld %ld %ld %ld %ld\n"
395 : "Block size: %-10lu\n"
396 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
397 "Inodes: Total: %-10jd Free: %jd\n");
399 (unsigned long int) (statfsbuf.f_bsize),
400 (intmax_t) (statfsbuf.f_blocks),
401 (intmax_t) (statfsbuf.f_bfree),
402 (intmax_t) (statfsbuf.f_bavail),
403 (intmax_t) (statfsbuf.f_files),
404 (intmax_t) (statfsbuf.f_ffree));
410 /* stat the file and print what we find */
411 static int do_stat(char const *filename, char const *format)
415 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
416 bb_perror_msg("cannot stat '%s'", filename);
420 #ifdef CONFIG_FEATURE_STAT_FORMAT
421 if (format == NULL) {
422 if (flags & OPT_TERSE) {
423 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
425 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
428 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
429 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
430 " Device type: %t,%T\n"
431 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
432 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
436 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
437 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
438 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
439 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
443 print_it(format, filename, print_stat, &statbuf);
445 if (flags & OPT_TERSE) {
446 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
448 (uintmax_t) (statbuf.st_size),
449 (uintmax_t) statbuf.st_blocks,
450 (unsigned long int) statbuf.st_mode,
451 (unsigned long int) statbuf.st_uid,
452 (unsigned long int) statbuf.st_gid,
453 (uintmax_t) statbuf.st_dev,
454 (uintmax_t) statbuf.st_ino,
455 (unsigned long int) statbuf.st_nlink,
456 (unsigned long int) major(statbuf.st_rdev),
457 (unsigned long int) minor(statbuf.st_rdev),
458 (unsigned long int) statbuf.st_atime,
459 (unsigned long int) statbuf.st_mtime,
460 (unsigned long int) statbuf.st_ctime,
461 (unsigned long int) statbuf.st_blksize
464 char *linkname = NULL;
466 struct passwd *pw_ent;
467 struct group *gw_ent;
469 gw_ent = getgrgid(statbuf.st_gid);
471 pw_ent = getpwuid(statbuf.st_uid);
473 if (S_ISLNK(statbuf.st_mode))
474 linkname = xreadlink(filename);
476 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
478 printf(" File: \"%s\"\n", filename);
480 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
481 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
482 (uintmax_t) (statbuf.st_size),
483 (uintmax_t) statbuf.st_blocks,
484 (unsigned long int) statbuf.st_blksize,
486 (uintmax_t) statbuf.st_dev,
487 (uintmax_t) statbuf.st_dev,
488 (uintmax_t) statbuf.st_ino,
489 (unsigned long int) statbuf.st_nlink);
490 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
491 printf(" Device type: %lx,%lx\n",
492 (unsigned long int) major(statbuf.st_rdev),
493 (unsigned long int) minor(statbuf.st_rdev));
496 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
497 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
498 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
499 bb_mode_string(statbuf.st_mode),
500 (unsigned long int) statbuf.st_uid,
501 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
502 (unsigned long int) statbuf.st_gid,
503 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
504 human_time(statbuf.st_atime),
505 human_time(statbuf.st_mtime),
506 human_time(statbuf.st_ctime));
512 int stat_main(int argc, char **argv)
517 int (*statfunc)(char const *, char const *) = do_stat;
519 flags = bb_getopt_ulflags(argc, argv, "ftL"
520 USE_FEATURE_STAT_FORMAT("c:", &format)
523 if (flags & 1) /* -f */
524 statfunc = do_statfs;
525 if (argc == optind) /* files */
528 for (i = optind; i < argc; ++i)
529 ok &= statfunc(argv[i], format);
531 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);