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>
35 #include <sys/statfs.h>
36 #include <sys/statvfs.h>
41 # include <linux/version.h>
44 /* vars to control behavior */
46 #define OPT_DEREFERNCE 4
49 static char const *file_type(struct stat const *st)
51 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
52 * for some of these formats.
53 * To keep diagnostics grammatical in English, the
54 * returned string must start with a consonant.
56 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
57 if (S_ISDIR(st->st_mode)) return "directory";
58 if (S_ISBLK(st->st_mode)) return "block special file";
59 if (S_ISCHR(st->st_mode)) return "character special file";
60 if (S_ISFIFO(st->st_mode)) return "fifo";
61 if (S_ISLNK(st->st_mode)) return "symbolic link";
62 if (S_ISSOCK(st->st_mode)) return "socket";
63 if (S_TYPEISMQ(st)) return "message queue";
64 if (S_TYPEISSEM(st)) return "semaphore";
65 if (S_TYPEISSHM(st)) return "shared memory object";
67 if (S_TYPEISTMO(st)) return "typed memory object";
72 static char const *human_time(time_t t)
76 str[strlen(str)-1] = '\0';
80 /* Return the type of the specified file system.
81 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
82 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
83 * Still others have neither and have to get by with f_type (Linux).
85 static char const *human_fstype(long f_type)
96 { 0xEF53, "ext2/ext3" },
97 { 0x3153464a, "jfs" },
98 { 0x58465342, "xfs" },
99 { 0xF995E849, "hpfs" },
104 { 0x138F, "minix (30 char.)" },
105 { 0x2468, "minix v2" },
106 { 0x2478, "minix v2 (30 char.)" },
109 { 0x564c, "novell" },
113 { 0x012FF7B4, "xenix" },
114 { 0x012FF7B5, "sysv4" },
115 { 0x012FF7B6, "sysv2" },
116 { 0x012FF7B7, "coh" },
117 { 0x00011954, "ufs" },
118 { 0x012FD16D, "xia" },
119 { 0x5346544e, "ntfs" },
120 { 0x1021994, "tmpfs" },
121 { 0x52654973, "reiserfs" },
122 { 0x28cd3d45, "cramfs" },
124 { 0x858458f6, "romfs" },
125 { 0x73717368, "squashfs" },
126 { 0x62656572, "sysfs" },
130 for (i=0; humantypes[i].type; ++i)
131 if (humantypes[i].type == f_type)
132 return humantypes[i].fs;
133 return humantypes[i].fs;
136 #ifdef CONFIG_FEATURE_STAT_FORMAT
137 /* print statfs info */
138 static void print_statfs(char *pformat, size_t buf_len, char m,
139 char const *filename, void const *data)
141 struct statfs const *statfsbuf = data;
145 strncat(pformat, "s", buf_len);
146 printf(pformat, filename);
149 strncat(pformat, "Lx", buf_len);
150 printf(pformat, statfsbuf->f_fsid);
153 strncat(pformat, "lu", buf_len);
154 printf(pformat, statfsbuf->f_namelen);
157 strncat(pformat, "lx", buf_len);
158 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
161 strncat(pformat, "s", buf_len);
162 printf(pformat, human_fstype(statfsbuf->f_type));
165 strncat(pformat, "ld", buf_len);
166 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
169 strncat(pformat, "ld", buf_len);
170 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
173 strncat(pformat, "ld", buf_len);
174 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
177 strncat(pformat, "lu", buf_len);
178 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
181 unsigned long int frsize;
182 #if defined(__linux__) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
183 frsize = statfsbuf->f_frsize;
185 frsize = statfsbuf->f_bsize;
187 frsize = statfsbuf->f_bsize;
189 strncat(pformat, "lu", buf_len);
190 printf(pformat, frsize);
194 strncat(pformat, "ld", buf_len);
195 printf(pformat, (intmax_t) (statfsbuf->f_files));
198 strncat(pformat, "ld", buf_len);
199 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
202 strncat(pformat, "c", buf_len);
208 /* print stat info */
209 static void print_stat(char *pformat, size_t buf_len, char m,
210 char const *filename, void const *data)
212 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
213 struct stat *statbuf = (struct stat *) data;
214 struct passwd *pw_ent;
215 struct group *gw_ent;
219 strncat(pformat, "s", buf_len);
220 printf(pformat, filename);
223 strncat(pformat, "s", buf_len);
224 if (S_ISLNK(statbuf->st_mode)) {
225 char *linkname = xreadlink(filename);
226 if (linkname == NULL) {
227 bb_perror_msg("cannot read symbolic link '%s'", filename);
230 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
231 printf(pformat, filename);
233 printf(pformat, linkname);
235 printf(pformat, filename);
239 strncat(pformat, "lu", buf_len);
240 printf(pformat, (uintmax_t) statbuf->st_dev);
243 strncat(pformat, "lx", buf_len);
244 printf(pformat, (uintmax_t) statbuf->st_dev);
247 strncat(pformat, "lu", buf_len);
248 printf(pformat, (uintmax_t) statbuf->st_ino);
251 strncat(pformat, "lo", buf_len);
252 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
255 strncat(pformat, "s", buf_len);
256 printf(pformat, bb_mode_string(statbuf->st_mode));
259 strncat(pformat, "lx", buf_len);
260 printf(pformat, (unsigned long int) statbuf->st_mode);
263 strncat(pformat, "s", buf_len);
264 printf(pformat, file_type(statbuf));
267 strncat(pformat, "lu", buf_len);
268 printf(pformat, (unsigned long int) statbuf->st_nlink);
271 strncat(pformat, "lu", buf_len);
272 printf(pformat, (unsigned long int) statbuf->st_uid);
275 strncat(pformat, "s", buf_len);
277 pw_ent = getpwuid(statbuf->st_uid);
278 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
281 strncat(pformat, "lu", buf_len);
282 printf(pformat, (unsigned long int) statbuf->st_gid);
285 strncat(pformat, "s", buf_len);
287 gw_ent = getgrgid(statbuf->st_gid);
288 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
291 strncat(pformat, "lx", buf_len);
292 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
295 strncat(pformat, "lx", buf_len);
296 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
299 strncat(pformat, "lu", buf_len);
300 printf(pformat, (uintmax_t) (statbuf->st_size));
303 strncat(pformat, "lu", buf_len);
304 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
307 strncat(pformat, "lu", buf_len);
308 printf(pformat, (uintmax_t) statbuf->st_blocks);
311 strncat(pformat, "lu", buf_len);
312 printf(pformat, (unsigned long int) statbuf->st_blksize);
315 strncat(pformat, "s", buf_len);
316 printf(pformat, human_time(statbuf->st_atime));
319 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
320 printf(pformat, (unsigned long int) statbuf->st_atime);
323 strncat(pformat, "s", buf_len);
324 printf(pformat, human_time(statbuf->st_mtime));
327 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
328 printf(pformat, (unsigned long int) statbuf->st_mtime);
331 strncat(pformat, "s", buf_len);
332 printf(pformat, human_time(statbuf->st_ctime));
335 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
336 printf(pformat, (unsigned long int) statbuf->st_ctime);
339 strncat(pformat, "c", buf_len);
345 static void print_it(char const *masterformat, char const *filename,
346 void (*print_func) (char *, size_t, char, char const *, void const *),
351 /* create a working copy of the format string */
352 char *format = bb_xstrdup(masterformat);
354 /* Add 2 to accommodate our conversion of the stat `%s' format string
355 * to the printf `%llu' one. */
356 size_t n_alloc = strlen(format) + 2 + 1;
357 char *dest = xmalloc(n_alloc);
361 char *p = strchr(b, '%');
367 len = strspn(p, "#-+.I 0123456789");
369 memcpy(dest + 1, p, len);
382 print_func(dest, n_alloc, *p, filename, data);
397 /* Stat the file system and print what we find. */
398 static int do_statfs(char const *filename, char const *format)
400 struct statfs statfsbuf;
402 if (statfs(filename, &statfsbuf) != 0) {
403 bb_perror_msg("cannot read file system information for '%s'", filename);
407 #ifdef CONFIG_FEATURE_STAT_FORMAT
409 format = (flags & OPT_TERSE
410 ? "%n %i %l %t %s %S %b %f %a %c %d\n"
412 " ID: %-8i Namelen: %-7l Type: %T\n"
413 "Block size: %-10s Fundamental block size: %S\n"
414 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
415 "Inodes: Total: %-10c Free: %d\n");
416 print_it(format, filename, print_statfs, &statfsbuf);
419 format = (flags & OPT_TERSE
422 " ID: %-8Lx Namelen: %-7lu ");
426 statfsbuf.f_namelen);
428 if (flags & OPT_TERSE)
429 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
431 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
433 format = (flags & OPT_TERSE
434 ? "%lu %lu %ld %ld %ld %ld %ld\n"
435 : "Block size: %-10lu Fundamental block size: %lu\n"
436 "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
437 "Inodes: Total: %-10ld Free: %ld\n");
439 (unsigned long int) (statfsbuf.f_bsize),
440 statfsbuf.f_frsize ? statfsbuf.f_frsize : statfsbuf.f_bsize,
441 (intmax_t) (statfsbuf.f_blocks),
442 (intmax_t) (statfsbuf.f_bfree),
443 (intmax_t) (statfsbuf.f_bavail),
444 (intmax_t) (statfsbuf.f_files),
445 (intmax_t) (statfsbuf.f_ffree));
451 /* stat the file and print what we find */
452 static int do_stat(char const *filename, char const *format)
456 if ((flags & OPT_DEREFERNCE ? stat : lstat) (filename, &statbuf) != 0) {
457 bb_perror_msg("cannot stat '%s'", filename);
461 #ifdef CONFIG_FEATURE_STAT_FORMAT
462 if (format == NULL) {
463 if (flags & OPT_TERSE) {
464 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
466 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
469 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
470 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
471 " Device type: %t,%T\n"
472 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
473 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
477 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
478 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
479 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
480 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
484 print_it(format, filename, print_stat, &statbuf);
486 if (flags & OPT_TERSE) {
487 printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n",
489 (uintmax_t) (statbuf.st_size),
490 (uintmax_t) statbuf.st_blocks,
491 (unsigned long int) statbuf.st_mode,
492 (unsigned long int) statbuf.st_uid,
493 (unsigned long int) statbuf.st_gid,
494 (uintmax_t) statbuf.st_dev,
495 (uintmax_t) statbuf.st_ino,
496 (unsigned long int) statbuf.st_nlink,
497 (unsigned long int) major(statbuf.st_rdev),
498 (unsigned long int) minor(statbuf.st_rdev),
499 (unsigned long int) statbuf.st_atime,
500 (unsigned long int) statbuf.st_mtime,
501 (unsigned long int) statbuf.st_ctime,
502 (unsigned long int) statbuf.st_blksize
505 char *linkname = NULL;
507 struct passwd *pw_ent;
508 struct group *gw_ent;
510 gw_ent = getgrgid(statbuf.st_gid);
512 pw_ent = getpwuid(statbuf.st_uid);
514 if (S_ISLNK(statbuf.st_mode))
515 linkname = xreadlink(filename);
517 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
519 printf(" File: \"%s\"\n", filename);
521 printf(" Size: %-10lu\tBlocks: %-10lu IO Block: %-6lu %s\n"
522 "Device: %lxh/%lud\tInode: %-10lu Links: %-5lu",
523 (uintmax_t) (statbuf.st_size),
524 (uintmax_t) statbuf.st_blocks,
525 (unsigned long int) statbuf.st_blksize,
527 (uintmax_t) statbuf.st_dev,
528 (uintmax_t) statbuf.st_dev,
529 (uintmax_t) statbuf.st_ino,
530 (unsigned long int) statbuf.st_nlink);
531 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
532 printf(" Device type: %lx,%lx\n",
533 (unsigned long int) major(statbuf.st_rdev),
534 (unsigned long int) minor(statbuf.st_rdev));
537 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
538 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
539 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
540 bb_mode_string(statbuf.st_mode),
541 (unsigned long int) statbuf.st_uid,
542 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
543 (unsigned long int) statbuf.st_gid,
544 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
545 human_time(statbuf.st_atime),
546 human_time(statbuf.st_mtime),
547 human_time(statbuf.st_ctime));
553 int stat_main(int argc, char **argv)
558 int (*statfunc)(char const *, char const *) = do_stat;
560 flags = bb_getopt_ulflags(argc, argv, "ftL"
561 #ifdef CONFIG_FEATURE_STAT_FORMAT
566 if (flags & 1) /* -f */
567 statfunc = do_statfs;
568 if (argc == optind) /* files */
571 for (i = optind; i < argc; ++i)
572 ok &= statfunc(argv[i], format);
574 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);