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)
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" },
126 for (i=0; humantypes[i].type; ++i)
127 if (humantypes[i].type == f_type)
128 return humantypes[i].fs;
129 return humantypes[i].fs;
132 #ifdef CONFIG_FEATURE_STAT_FORMAT
133 /* print statfs info */
134 static void print_statfs(char *pformat, size_t buf_len, char m,
135 char const *filename, void const *data)
137 struct statfs const *statfsbuf = data;
141 strncat(pformat, "s", buf_len);
142 printf(pformat, filename);
145 strncat(pformat, "Lx", buf_len);
146 printf(pformat, statfsbuf->f_fsid);
149 strncat(pformat, "lu", buf_len);
150 printf(pformat, statfsbuf->f_namelen);
153 strncat(pformat, "lx", buf_len);
154 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
157 strncat(pformat, "s", buf_len);
158 printf(pformat, human_fstype(statfsbuf->f_type));
161 strncat(pformat, "ld", buf_len);
162 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
165 strncat(pformat, "ld", buf_len);
166 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
169 strncat(pformat, "ld", buf_len);
170 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
174 strncat(pformat, "lu", buf_len);
175 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
178 strncat(pformat, "ld", buf_len);
179 printf(pformat, (intmax_t) (statfsbuf->f_files));
182 strncat(pformat, "ld", buf_len);
183 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
186 strncat(pformat, "c", buf_len);
192 /* print stat info */
193 static void print_stat(char *pformat, size_t buf_len, char m,
194 char const *filename, void const *data)
196 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
197 struct stat *statbuf = (struct stat *) data;
198 struct passwd *pw_ent;
199 struct group *gw_ent;
203 strncat(pformat, "s", buf_len);
204 printf(pformat, filename);
207 strncat(pformat, "s", buf_len);
208 if (S_ISLNK(statbuf->st_mode)) {
209 char *linkname = xreadlink(filename);
210 if (linkname == NULL) {
211 bb_perror_msg("cannot read symbolic link '%s'", filename);
214 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
215 printf(pformat, filename);
217 printf(pformat, linkname);
219 printf(pformat, filename);
223 strncat(pformat, "lu", buf_len);
224 printf(pformat, (uintmax_t) statbuf->st_dev);
227 strncat(pformat, "lx", buf_len);
228 printf(pformat, (uintmax_t) statbuf->st_dev);
231 strncat(pformat, "lu", buf_len);
232 printf(pformat, (uintmax_t) statbuf->st_ino);
235 strncat(pformat, "lo", buf_len);
236 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
239 strncat(pformat, "s", buf_len);
240 printf(pformat, bb_mode_string(statbuf->st_mode));
243 strncat(pformat, "lx", buf_len);
244 printf(pformat, (unsigned long int) statbuf->st_mode);
247 strncat(pformat, "s", buf_len);
248 printf(pformat, file_type(statbuf));
251 strncat(pformat, "lu", buf_len);
252 printf(pformat, (unsigned long int) statbuf->st_nlink);
255 strncat(pformat, "lu", buf_len);
256 printf(pformat, (unsigned long int) statbuf->st_uid);
259 strncat(pformat, "s", buf_len);
261 pw_ent = getpwuid(statbuf->st_uid);
262 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
265 strncat(pformat, "lu", buf_len);
266 printf(pformat, (unsigned long int) statbuf->st_gid);
269 strncat(pformat, "s", buf_len);
271 gw_ent = getgrgid(statbuf->st_gid);
272 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
275 strncat(pformat, "lx", buf_len);
276 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
279 strncat(pformat, "lx", buf_len);
280 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
283 strncat(pformat, "lu", buf_len);
284 printf(pformat, (uintmax_t) (statbuf->st_size));
287 strncat(pformat, "lu", buf_len);
288 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
291 strncat(pformat, "lu", buf_len);
292 printf(pformat, (uintmax_t) statbuf->st_blocks);
295 strncat(pformat, "lu", buf_len);
296 printf(pformat, (unsigned long int) statbuf->st_blksize);
299 strncat(pformat, "s", buf_len);
300 printf(pformat, human_time(statbuf->st_atime));
303 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
304 printf(pformat, (unsigned long int) statbuf->st_atime);
307 strncat(pformat, "s", buf_len);
308 printf(pformat, human_time(statbuf->st_mtime));
311 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
312 printf(pformat, (unsigned long int) statbuf->st_mtime);
315 strncat(pformat, "s", buf_len);
316 printf(pformat, human_time(statbuf->st_ctime));
319 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
320 printf(pformat, (unsigned long int) statbuf->st_ctime);
323 strncat(pformat, "c", buf_len);
329 static void print_it(char const *masterformat, char const *filename,
330 void (*print_func) (char *, size_t, char, char const *, void const *),
335 /* create a working copy of the format string */
336 char *format = bb_xstrdup(masterformat);
338 /* Add 2 to accommodate our conversion of the stat `%s' format string
339 * to the printf `%llu' one. */
340 size_t n_alloc = strlen(format) + 2 + 1;
341 char *dest = xmalloc(n_alloc);
345 char *p = strchr(b, '%');
351 len = strspn(p, "#-+.I 0123456789");
353 memcpy(dest + 1, p, len);
366 print_func(dest, n_alloc, *p, filename, data);
381 /* Stat the file system and print what we find. */
382 static int do_statfs(char const *filename, char const *format)
384 struct statfs statfsbuf;
386 if (statfs(filename, &statfsbuf) != 0) {
387 bb_perror_msg("cannot read file system information for '%s'", filename);
391 #ifdef CONFIG_FEATURE_STAT_FORMAT
393 format = (flags & OPT_TERSE
394 ? "%n %i %l %t %s %b %f %a %c %d\n"
396 " ID: %-8i Namelen: %-7l Type: %T\n"
397 "Block size: %-10s\n"
398 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
399 "Inodes: Total: %-10c Free: %d\n");
400 print_it(format, filename, print_statfs, &statfsbuf);
403 format = (flags & OPT_TERSE
406 " ID: %-8Lx Namelen: %-7lu ");
410 statfsbuf.f_namelen);
412 if (flags & OPT_TERSE)
413 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
415 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
417 format = (flags & OPT_TERSE
418 ? "%lu %ld %ld %ld %ld %ld\n"
419 : "Block size: %-10lu\n"
420 "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
421 "Inodes: Total: %-10ld Free: %ld\n");
423 (unsigned long int) (statfsbuf.f_bsize),
424 (intmax_t) (statfsbuf.f_blocks),
425 (intmax_t) (statfsbuf.f_bfree),
426 (intmax_t) (statfsbuf.f_bavail),
427 (intmax_t) (statfsbuf.f_files),
428 (intmax_t) (statfsbuf.f_ffree));
434 /* stat the file and print what we find */
435 static int do_stat(char const *filename, char const *format)
439 if ((flags & OPT_DEREFERNCE ? stat : lstat) (filename, &statbuf) != 0) {
440 bb_perror_msg("cannot stat '%s'", filename);
444 #ifdef CONFIG_FEATURE_STAT_FORMAT
445 if (format == NULL) {
446 if (flags & OPT_TERSE) {
447 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
449 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
452 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
453 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
454 " Device type: %t,%T\n"
455 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
456 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
460 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
461 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
462 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
463 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
467 print_it(format, filename, print_stat, &statbuf);
469 if (flags & OPT_TERSE) {
470 printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n",
472 (uintmax_t) (statbuf.st_size),
473 (uintmax_t) statbuf.st_blocks,
474 (unsigned long int) statbuf.st_mode,
475 (unsigned long int) statbuf.st_uid,
476 (unsigned long int) statbuf.st_gid,
477 (uintmax_t) statbuf.st_dev,
478 (uintmax_t) statbuf.st_ino,
479 (unsigned long int) statbuf.st_nlink,
480 (unsigned long int) major(statbuf.st_rdev),
481 (unsigned long int) minor(statbuf.st_rdev),
482 (unsigned long int) statbuf.st_atime,
483 (unsigned long int) statbuf.st_mtime,
484 (unsigned long int) statbuf.st_ctime,
485 (unsigned long int) statbuf.st_blksize
488 char *linkname = NULL;
490 struct passwd *pw_ent;
491 struct group *gw_ent;
493 gw_ent = getgrgid(statbuf.st_gid);
495 pw_ent = getpwuid(statbuf.st_uid);
497 if (S_ISLNK(statbuf.st_mode))
498 linkname = xreadlink(filename);
500 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
502 printf(" File: \"%s\"\n", filename);
504 printf(" Size: %-10lu\tBlocks: %-10lu IO Block: %-6lu %s\n"
505 "Device: %lxh/%lud\tInode: %-10lu Links: %-5lu",
506 (uintmax_t) (statbuf.st_size),
507 (uintmax_t) statbuf.st_blocks,
508 (unsigned long int) statbuf.st_blksize,
510 (uintmax_t) statbuf.st_dev,
511 (uintmax_t) statbuf.st_dev,
512 (uintmax_t) statbuf.st_ino,
513 (unsigned long int) statbuf.st_nlink);
514 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
515 printf(" Device type: %lx,%lx\n",
516 (unsigned long int) major(statbuf.st_rdev),
517 (unsigned long int) minor(statbuf.st_rdev));
520 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
521 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
522 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
523 bb_mode_string(statbuf.st_mode),
524 (unsigned long int) statbuf.st_uid,
525 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
526 (unsigned long int) statbuf.st_gid,
527 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
528 human_time(statbuf.st_atime),
529 human_time(statbuf.st_mtime),
530 human_time(statbuf.st_ctime));
536 int stat_main(int argc, char **argv)
541 int (*statfunc)(char const *, char const *) = do_stat;
543 flags = bb_getopt_ulflags(argc, argv, "ftL"
544 #ifdef CONFIG_FEATURE_STAT_FORMAT
549 if (flags & 1) /* -f */
550 statfunc = do_statfs;
551 if (argc == optind) /* files */
554 for (i = optind; i < argc; ++i)
555 ok &= statfunc(argv[i], format);
557 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);