2 * stat -- display file or file system status
4 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
5 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
8 * Written by Michael Meskes
9 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
33 #include <getopt.h> /* optind */
35 #include <sys/statfs.h>
36 #include <sys/statvfs.h>
40 /* vars to control behavior */
42 #define OPT_DEREFERNCE 4
45 static char const *file_type(struct stat const *st)
47 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
48 * for some of these formats.
49 * To keep diagnostics grammatical in English, the
50 * returned string must start with a consonant.
52 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
53 if (S_ISDIR(st->st_mode)) return "directory";
54 if (S_ISBLK(st->st_mode)) return "block special file";
55 if (S_ISCHR(st->st_mode)) return "character special file";
56 if (S_ISFIFO(st->st_mode)) return "fifo";
57 if (S_ISLNK(st->st_mode)) return "symbolic link";
58 if (S_ISSOCK(st->st_mode)) return "socket";
59 if (S_TYPEISMQ(st)) return "message queue";
60 if (S_TYPEISSEM(st)) return "semaphore";
61 if (S_TYPEISSHM(st)) return "shared memory object";
63 if (S_TYPEISTMO(st)) return "typed memory object";
68 static char const *human_time(time_t t)
72 str[strlen(str)-1] = '\0';
76 /* Return the type of the specified file system.
77 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
78 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
79 * Still others have neither and have to get by with f_type (Linux).
81 static char const *human_fstype(long f_type)
84 static const struct types {
92 { 0xEF53, "ext2/ext3" },
93 { 0x3153464a, "jfs" },
94 { 0x58465342, "xfs" },
95 { 0xF995E849, "hpfs" },
100 { 0x138F, "minix (30 char.)" },
101 { 0x2468, "minix v2" },
102 { 0x2478, "minix v2 (30 char.)" },
105 { 0x564c, "novell" },
109 { 0x012FF7B4, "xenix" },
110 { 0x012FF7B5, "sysv4" },
111 { 0x012FF7B6, "sysv2" },
112 { 0x012FF7B7, "coh" },
113 { 0x00011954, "ufs" },
114 { 0x012FD16D, "xia" },
115 { 0x5346544e, "ntfs" },
116 { 0x1021994, "tmpfs" },
117 { 0x52654973, "reiserfs" },
118 { 0x28cd3d45, "cramfs" },
120 { 0x858458f6, "romfs" },
121 { 0x73717368, "squashfs" },
122 { 0x62656572, "sysfs" },
125 for (i=0; humantypes[i].type; ++i)
126 if (humantypes[i].type == f_type)
128 return humantypes[i].fs;
131 #ifdef CONFIG_FEATURE_STAT_FORMAT
132 /* print statfs info */
133 static void print_statfs(char *pformat, size_t buf_len, char m,
134 char const *filename, void const *data)
136 struct statfs const *statfsbuf = data;
140 strncat(pformat, "s", buf_len);
141 printf(pformat, filename);
144 strncat(pformat, "Lx", buf_len);
145 printf(pformat, statfsbuf->f_fsid);
148 strncat(pformat, "lu", buf_len);
149 printf(pformat, statfsbuf->f_namelen);
152 strncat(pformat, "lx", buf_len);
153 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
156 strncat(pformat, "s", buf_len);
157 printf(pformat, human_fstype(statfsbuf->f_type));
160 strncat(pformat, "ld", buf_len);
161 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
164 strncat(pformat, "ld", buf_len);
165 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
168 strncat(pformat, "ld", buf_len);
169 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
173 strncat(pformat, "lu", buf_len);
174 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
177 strncat(pformat, "ld", buf_len);
178 printf(pformat, (intmax_t) (statfsbuf->f_files));
181 strncat(pformat, "ld", buf_len);
182 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
185 strncat(pformat, "c", buf_len);
191 /* print stat info */
192 static void print_stat(char *pformat, size_t buf_len, char m,
193 char const *filename, void const *data)
195 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
196 struct stat *statbuf = (struct stat *) data;
197 struct passwd *pw_ent;
198 struct group *gw_ent;
202 strncat(pformat, "s", buf_len);
203 printf(pformat, filename);
206 strncat(pformat, "s", buf_len);
207 if (S_ISLNK(statbuf->st_mode)) {
208 char *linkname = xreadlink(filename);
209 if (linkname == NULL) {
210 bb_perror_msg("cannot read symbolic link '%s'", filename);
213 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
214 printf(pformat, filename);
216 printf(pformat, linkname);
218 printf(pformat, filename);
222 strncat(pformat, "lu", buf_len);
223 printf(pformat, (uintmax_t) statbuf->st_dev);
226 strncat(pformat, "lx", buf_len);
227 printf(pformat, (uintmax_t) statbuf->st_dev);
230 strncat(pformat, "lu", buf_len);
231 printf(pformat, (uintmax_t) statbuf->st_ino);
234 strncat(pformat, "lo", buf_len);
235 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
238 strncat(pformat, "s", buf_len);
239 printf(pformat, bb_mode_string(statbuf->st_mode));
242 strncat(pformat, "lx", buf_len);
243 printf(pformat, (unsigned long int) statbuf->st_mode);
246 strncat(pformat, "s", buf_len);
247 printf(pformat, file_type(statbuf));
250 strncat(pformat, "lu", buf_len);
251 printf(pformat, (unsigned long int) statbuf->st_nlink);
254 strncat(pformat, "lu", buf_len);
255 printf(pformat, (unsigned long int) statbuf->st_uid);
258 strncat(pformat, "s", buf_len);
260 pw_ent = getpwuid(statbuf->st_uid);
261 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
264 strncat(pformat, "lu", buf_len);
265 printf(pformat, (unsigned long int) statbuf->st_gid);
268 strncat(pformat, "s", buf_len);
270 gw_ent = getgrgid(statbuf->st_gid);
271 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
274 strncat(pformat, "lx", buf_len);
275 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
278 strncat(pformat, "lx", buf_len);
279 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
282 strncat(pformat, "lu", buf_len);
283 printf(pformat, (uintmax_t) (statbuf->st_size));
286 strncat(pformat, "lu", buf_len);
287 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
290 strncat(pformat, "lu", buf_len);
291 printf(pformat, (uintmax_t) statbuf->st_blocks);
294 strncat(pformat, "lu", buf_len);
295 printf(pformat, (unsigned long int) statbuf->st_blksize);
298 strncat(pformat, "s", buf_len);
299 printf(pformat, human_time(statbuf->st_atime));
302 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
303 printf(pformat, (unsigned long int) statbuf->st_atime);
306 strncat(pformat, "s", buf_len);
307 printf(pformat, human_time(statbuf->st_mtime));
310 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
311 printf(pformat, (unsigned long int) statbuf->st_mtime);
314 strncat(pformat, "s", buf_len);
315 printf(pformat, human_time(statbuf->st_ctime));
318 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
319 printf(pformat, (unsigned long int) statbuf->st_ctime);
322 strncat(pformat, "c", buf_len);
328 static void print_it(char const *masterformat, char const *filename,
329 void (*print_func) (char *, size_t, char, char const *, void const *),
334 /* create a working copy of the format string */
335 char *format = bb_xstrdup(masterformat);
337 /* Add 2 to accommodate our conversion of the stat `%s' format string
338 * to the printf `%llu' one. */
339 size_t n_alloc = strlen(format) + 2 + 1;
340 char *dest = xmalloc(n_alloc);
344 char *p = strchr(b, '%');
350 len = strspn(p, "#-+.I 0123456789");
352 memcpy(dest + 1, p, len);
365 print_func(dest, n_alloc, *p, filename, data);
380 /* Stat the file system and print what we find. */
381 static int do_statfs(char const *filename, char const *format)
383 struct statfs statfsbuf;
385 if (statfs(filename, &statfsbuf) != 0) {
386 bb_perror_msg("cannot read file system information for '%s'", filename);
390 #ifdef CONFIG_FEATURE_STAT_FORMAT
392 format = (flags & OPT_TERSE
393 ? "%n %i %l %t %s %b %f %a %c %d\n"
395 " ID: %-8i Namelen: %-7l Type: %T\n"
396 "Block size: %-10s\n"
397 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
398 "Inodes: Total: %-10c Free: %d\n");
399 print_it(format, filename, print_statfs, &statfsbuf);
402 format = (flags & OPT_TERSE
405 " ID: %-8Lx Namelen: %-7lu ");
409 statfsbuf.f_namelen);
411 if (flags & OPT_TERSE)
412 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
414 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
416 format = (flags & OPT_TERSE
417 ? "%lu %ld %ld %ld %ld %ld\n"
418 : "Block size: %-10lu\n"
419 "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
420 "Inodes: Total: %-10ld Free: %ld\n");
422 (unsigned long int) (statfsbuf.f_bsize),
423 (intmax_t) (statfsbuf.f_blocks),
424 (intmax_t) (statfsbuf.f_bfree),
425 (intmax_t) (statfsbuf.f_bavail),
426 (intmax_t) (statfsbuf.f_files),
427 (intmax_t) (statfsbuf.f_ffree));
433 /* stat the file and print what we find */
434 static int do_stat(char const *filename, char const *format)
438 if ((flags & OPT_DEREFERNCE ? stat : lstat) (filename, &statbuf) != 0) {
439 bb_perror_msg("cannot stat '%s'", filename);
443 #ifdef CONFIG_FEATURE_STAT_FORMAT
444 if (format == NULL) {
445 if (flags & OPT_TERSE) {
446 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
448 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
451 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
452 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
453 " Device type: %t,%T\n"
454 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
455 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
459 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
460 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
461 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
462 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
466 print_it(format, filename, print_stat, &statbuf);
468 if (flags & OPT_TERSE) {
469 printf("%s %"PRIuMAX" %"PRIuMAX" %lx %lu %lu %"PRIxMAX" %"PRIuMAX" %lu %lx %lx %lu %lu %lu %lu\n",
471 (uintmax_t) (statbuf.st_size),
472 (uintmax_t) statbuf.st_blocks,
473 (unsigned long int) statbuf.st_mode,
474 (unsigned long int) statbuf.st_uid,
475 (unsigned long int) statbuf.st_gid,
476 (uintmax_t) statbuf.st_dev,
477 (uintmax_t) statbuf.st_ino,
478 (unsigned long int) statbuf.st_nlink,
479 (unsigned long int) major(statbuf.st_rdev),
480 (unsigned long int) minor(statbuf.st_rdev),
481 (unsigned long int) statbuf.st_atime,
482 (unsigned long int) statbuf.st_mtime,
483 (unsigned long int) statbuf.st_ctime,
484 (unsigned long int) statbuf.st_blksize
487 char *linkname = NULL;
489 struct passwd *pw_ent;
490 struct group *gw_ent;
492 gw_ent = getgrgid(statbuf.st_gid);
494 pw_ent = getpwuid(statbuf.st_uid);
496 if (S_ISLNK(statbuf.st_mode))
497 linkname = xreadlink(filename);
499 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
501 printf(" File: \"%s\"\n", filename);
503 printf(" Size: %-10"PRIuMAX"\tBlocks: %-10"PRIuMAX" IO Block: %-6lu %s\n"
504 "Device: %"PRIxMAX"h/%"PRIuMAX"d\tInode: %-10"PRIuMAX" Links: %-5lu",
505 (uintmax_t) (statbuf.st_size),
506 (uintmax_t) statbuf.st_blocks,
507 (unsigned long int) statbuf.st_blksize,
509 (uintmax_t) statbuf.st_dev,
510 (uintmax_t) statbuf.st_dev,
511 (uintmax_t) statbuf.st_ino,
512 (unsigned long int) statbuf.st_nlink);
513 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
514 printf(" Device type: %lx,%lx\n",
515 (unsigned long int) major(statbuf.st_rdev),
516 (unsigned long int) minor(statbuf.st_rdev));
519 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
520 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
521 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
522 bb_mode_string(statbuf.st_mode),
523 (unsigned long int) statbuf.st_uid,
524 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
525 (unsigned long int) statbuf.st_gid,
526 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
527 human_time(statbuf.st_atime),
528 human_time(statbuf.st_mtime),
529 human_time(statbuf.st_ctime));
535 int stat_main(int argc, char **argv)
540 int (*statfunc)(char const *, char const *) = do_stat;
542 flags = bb_getopt_ulflags(argc, argv, "ftL"
543 #ifdef CONFIG_FEATURE_STAT_FORMAT
548 if (flags & 1) /* -f */
549 statfunc = do_statfs;
550 if (argc == optind) /* files */
553 for (i = optind; i < argc; ++i)
554 ok &= statfunc(argv[i], format);
556 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);