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>
8 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
19 //config: display file or filesystem status.
21 //config:config FEATURE_STAT_FORMAT
22 //config: bool "Enable custom formats (-c)"
24 //config: depends on STAT
26 //config: Without this, stat will not support the '-c format' option where
27 //config: users can pass a custom format string for output. This adds about
28 //config: 7k to a nonstatic build on amd64.
30 //config:config FEATURE_STAT_FILESYSTEM
31 //config: bool "Enable display of filesystem status (-f)"
33 //config: depends on STAT
34 //config: select PLATFORM_LINUX # statfs()
36 //config: Without this, stat will not support the '-f' option to display
37 //config: information about filesystem status.
40 //usage:#define stat_trivial_usage
41 //usage: "[OPTIONS] FILE..."
42 //usage:#define stat_full_usage "\n\n"
43 //usage: "Display file"
44 //usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
46 //usage: IF_FEATURE_STAT_FORMAT(
47 //usage: "\n -c FMT Use the specified format"
49 //usage: IF_FEATURE_STAT_FILESYSTEM(
50 //usage: "\n -f Display filesystem status"
52 //usage: "\n -L Follow links"
53 //usage: "\n -t Terse display"
55 //usage: "\n -Z Print security context"
57 //usage: IF_FEATURE_STAT_FORMAT(
58 //usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
59 //usage: " %a Access rights in octal\n"
60 //usage: " %A Access rights in human readable form\n"
61 //usage: " %b Number of blocks allocated (see %B)\n"
62 //usage: " %B Size in bytes of each block reported by %b\n"
63 //usage: " %d Device number in decimal\n"
64 //usage: " %D Device number in hex\n"
65 //usage: " %f Raw mode in hex\n"
66 //usage: " %F File type\n"
67 //usage: " %g Group ID\n"
68 //usage: " %G Group name\n"
69 //usage: " %h Number of hard links\n"
70 //usage: " %i Inode number\n"
71 //usage: " %n File name\n"
72 //usage: " %N File name, with -> TARGET if symlink\n"
73 //usage: " %o I/O block size\n"
74 //usage: " %s Total size in bytes\n"
75 //usage: " %t Major device type in hex\n"
76 //usage: " %T Minor device type in hex\n"
77 //usage: " %u User ID\n"
78 //usage: " %U User name\n"
79 //usage: " %x Time of last access\n"
80 //usage: " %X Time of last access as seconds since Epoch\n"
81 //usage: " %y Time of last modification\n"
82 //usage: " %Y Time of last modification as seconds since Epoch\n"
83 //usage: " %z Time of last change\n"
84 //usage: " %Z Time of last change as seconds since Epoch\n"
85 //usage: IF_FEATURE_STAT_FILESYSTEM(
86 //usage: "\nFMT sequences for file systems:\n"
87 //usage: " %a Free blocks available to non-superuser\n"
88 //usage: " %b Total data blocks\n"
89 //usage: " %c Total file nodes\n"
90 //usage: " %d Free file nodes\n"
91 //usage: " %f Free blocks\n"
93 //usage: " %C Security context in selinux\n"
95 //usage: " %i File System ID in hex\n"
96 //usage: " %l Maximum length of filenames\n"
97 //usage: " %n File name\n"
98 //usage: " %s Block size (for faster transfer)\n"
99 //usage: " %S Fundamental block size (for block counts)\n"
100 //usage: " %t Type in hex\n"
101 //usage: " %T Type in human readable form"
106 #include "common_bufsiz.h"
109 OPT_TERSE
= (1 << 0),
110 OPT_DEREFERENCE
= (1 << 1),
111 OPT_FILESYS
= (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM
,
112 OPT_SELINUX
= (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM
)) * ENABLE_SELINUX
,
115 #if ENABLE_FEATURE_STAT_FORMAT
116 typedef bool (*statfunc_ptr
)(const char *, const char *);
118 typedef bool (*statfunc_ptr
)(const char *);
121 static const char *file_type(const struct stat
*st
)
123 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
124 * for some of these formats.
125 * To keep diagnostics grammatical in English, the
126 * returned string must start with a consonant.
128 if (S_ISREG(st
->st_mode
)) return st
->st_size
== 0 ? "regular empty file" : "regular file";
129 if (S_ISDIR(st
->st_mode
)) return "directory";
130 if (S_ISBLK(st
->st_mode
)) return "block special file";
131 if (S_ISCHR(st
->st_mode
)) return "character special file";
132 if (S_ISFIFO(st
->st_mode
)) return "fifo";
133 if (S_ISLNK(st
->st_mode
)) return "symbolic link";
134 if (S_ISSOCK(st
->st_mode
)) return "socket";
136 if (S_TYPEISMQ(st
)) return "message queue";
139 if (S_TYPEISSEM(st
)) return "semaphore";
142 if (S_TYPEISSHM(st
)) return "shared memory object";
145 if (S_TYPEISTMO(st
)) return "typed memory object";
150 static const char *human_time(time_t t
)
155 str[strlen(str)-1] = '\0';
158 /* coreutils 6.3 compat: */
160 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
161 #define buf bb_common_bufsiz1
162 setup_common_bufsiz();
163 strcpy(strftime_YYYYMMDDHHMMSS(buf
, COMMON_BUFSIZE
, &t
), ".000000000");
168 #if ENABLE_FEATURE_STAT_FILESYSTEM
169 /* Return the type of the specified file system.
170 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
171 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
172 * Still others have neither and have to get by with f_type (Linux).
174 static const char *human_fstype(uint32_t f_type
)
176 static const struct types
{
178 const char *const fs
;
181 { 0x1Cd1, "devpts" },
184 { 0xEF53, "ext2/ext3" },
185 { 0x3153464a, "jfs" },
186 { 0x58465342, "xfs" },
187 { 0xF995E849, "hpfs" },
192 { 0x138F, "minix (30 char.)" },
193 { 0x2468, "minix v2" },
194 { 0x2478, "minix v2 (30 char.)" },
197 { 0x564c, "novell" },
201 { 0x012FF7B4, "xenix" },
202 { 0x012FF7B5, "sysv4" },
203 { 0x012FF7B6, "sysv2" },
204 { 0x012FF7B7, "coh" },
205 { 0x00011954, "ufs" },
206 { 0x012FD16D, "xia" },
207 { 0x5346544e, "ntfs" },
208 { 0x1021994, "tmpfs" },
209 { 0x52654973, "reiserfs" },
210 { 0x28cd3d45, "cramfs" },
212 { 0x858458f6, "romfs" },
213 { 0x73717368, "squashfs" },
214 { 0x62656572, "sysfs" },
220 for (i
= 0; humantypes
[i
].type
; ++i
)
221 if (humantypes
[i
].type
== f_type
)
223 return humantypes
[i
].fs
;
226 /* "man statfs" says that statfsbuf->f_fsid is a mess */
227 /* coreutils treats it as an array of ints, most significant first */
228 static unsigned long long get_f_fsid(const struct statfs
*statfsbuf
)
230 const unsigned *p
= (const void*) &statfsbuf
->f_fsid
;
231 unsigned sz
= sizeof(statfsbuf
->f_fsid
) / sizeof(unsigned);
232 unsigned long long r
= 0;
235 r
= (r
<< (sizeof(unsigned)*8)) | *p
++;
239 #endif /* FEATURE_STAT_FILESYSTEM */
241 #if ENABLE_FEATURE_STAT_FORMAT
242 static void strcatc(char *str
, char c
)
244 int len
= strlen(str
);
249 static void printfs(char *pformat
, const char *msg
)
251 strcatc(pformat
, 's');
252 printf(pformat
, msg
);
255 #if ENABLE_FEATURE_STAT_FILESYSTEM
256 /* print statfs info */
257 static void FAST_FUNC
print_statfs(char *pformat
, const char m
,
258 const char *const filename
, const void *data
259 IF_SELINUX(, security_context_t scontext
))
261 const struct statfs
*statfsbuf
= data
;
263 printfs(pformat
, filename
);
264 } else if (m
== 'i') {
265 strcat(pformat
, "llx");
266 printf(pformat
, get_f_fsid(statfsbuf
));
267 } else if (m
== 'l') {
268 strcat(pformat
, "lu");
269 printf(pformat
, (unsigned long) statfsbuf
->f_namelen
);
270 } else if (m
== 't') {
271 strcat(pformat
, "lx");
272 printf(pformat
, (unsigned long) statfsbuf
->f_type
); /* no equiv */
273 } else if (m
== 'T') {
274 printfs(pformat
, human_fstype(statfsbuf
->f_type
));
275 } else if (m
== 'b') {
276 strcat(pformat
, "llu");
277 printf(pformat
, (unsigned long long) statfsbuf
->f_blocks
);
278 } else if (m
== 'f') {
279 strcat(pformat
, "llu");
280 printf(pformat
, (unsigned long long) statfsbuf
->f_bfree
);
281 } else if (m
== 'a') {
282 strcat(pformat
, "llu");
283 printf(pformat
, (unsigned long long) statfsbuf
->f_bavail
);
284 } else if (m
== 's' || m
== 'S') {
285 strcat(pformat
, "lu");
286 printf(pformat
, (unsigned long) statfsbuf
->f_bsize
);
287 } else if (m
== 'c') {
288 strcat(pformat
, "llu");
289 printf(pformat
, (unsigned long long) statfsbuf
->f_files
);
290 } else if (m
== 'd') {
291 strcat(pformat
, "llu");
292 printf(pformat
, (unsigned long long) statfsbuf
->f_ffree
);
294 } else if (m
== 'C' && (option_mask32
& OPT_SELINUX
)) {
295 printfs(pformat
, scontext
);
298 strcatc(pformat
, 'c');
304 /* print stat info */
305 static void FAST_FUNC
print_stat(char *pformat
, const char m
,
306 const char *const filename
, const void *data
307 IF_SELINUX(, security_context_t scontext
))
309 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
310 struct stat
*statbuf
= (struct stat
*) data
;
311 struct passwd
*pw_ent
;
312 struct group
*gw_ent
;
315 printfs(pformat
, filename
);
316 } else if (m
== 'N') {
317 strcatc(pformat
, 's');
318 if (S_ISLNK(statbuf
->st_mode
)) {
319 char *linkname
= xmalloc_readlink_or_warn(filename
);
320 if (linkname
== NULL
)
322 printf("'%s' -> '%s'", filename
, linkname
);
325 printf(pformat
, filename
);
327 } else if (m
== 'd') {
328 strcat(pformat
, "llu");
329 printf(pformat
, (unsigned long long) statbuf
->st_dev
);
330 } else if (m
== 'D') {
331 strcat(pformat
, "llx");
332 printf(pformat
, (unsigned long long) statbuf
->st_dev
);
333 } else if (m
== 'i') {
334 strcat(pformat
, "llu");
335 printf(pformat
, (unsigned long long) statbuf
->st_ino
);
336 } else if (m
== 'a') {
337 strcat(pformat
, "lo");
338 printf(pformat
, (unsigned long) (statbuf
->st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXU
|S_IRWXG
|S_IRWXO
)));
339 } else if (m
== 'A') {
340 printfs(pformat
, bb_mode_string(statbuf
->st_mode
));
341 } else if (m
== 'f') {
342 strcat(pformat
, "lx");
343 printf(pformat
, (unsigned long) statbuf
->st_mode
);
344 } else if (m
== 'F') {
345 printfs(pformat
, file_type(statbuf
));
346 } else if (m
== 'h') {
347 strcat(pformat
, "lu");
348 printf(pformat
, (unsigned long) statbuf
->st_nlink
);
349 } else if (m
== 'u') {
350 strcat(pformat
, "lu");
351 printf(pformat
, (unsigned long) statbuf
->st_uid
);
352 } else if (m
== 'U') {
353 pw_ent
= getpwuid(statbuf
->st_uid
);
354 printfs(pformat
, (pw_ent
!= NULL
) ? pw_ent
->pw_name
: "UNKNOWN");
355 } else if (m
== 'g') {
356 strcat(pformat
, "lu");
357 printf(pformat
, (unsigned long) statbuf
->st_gid
);
358 } else if (m
== 'G') {
359 gw_ent
= getgrgid(statbuf
->st_gid
);
360 printfs(pformat
, (gw_ent
!= NULL
) ? gw_ent
->gr_name
: "UNKNOWN");
361 } else if (m
== 't') {
362 strcat(pformat
, "lx");
363 printf(pformat
, (unsigned long) major(statbuf
->st_rdev
));
364 } else if (m
== 'T') {
365 strcat(pformat
, "lx");
366 printf(pformat
, (unsigned long) minor(statbuf
->st_rdev
));
367 } else if (m
== 's') {
368 strcat(pformat
, "llu");
369 printf(pformat
, (unsigned long long) statbuf
->st_size
);
370 } else if (m
== 'B') {
371 strcat(pformat
, "lu");
372 printf(pformat
, (unsigned long) 512); //ST_NBLOCKSIZE
373 } else if (m
== 'b') {
374 strcat(pformat
, "llu");
375 printf(pformat
, (unsigned long long) statbuf
->st_blocks
);
376 } else if (m
== 'o') {
377 strcat(pformat
, "lu");
378 printf(pformat
, (unsigned long) statbuf
->st_blksize
);
379 } else if (m
== 'x') {
380 printfs(pformat
, human_time(statbuf
->st_atime
));
381 } else if (m
== 'X') {
382 strcat(pformat
, TYPE_SIGNED(time_t) ? "ld" : "lu");
383 /* note: (unsigned long) would be wrong:
384 * imagine (unsigned long64)int32 */
385 printf(pformat
, (long) statbuf
->st_atime
);
386 } else if (m
== 'y') {
387 printfs(pformat
, human_time(statbuf
->st_mtime
));
388 } else if (m
== 'Y') {
389 strcat(pformat
, TYPE_SIGNED(time_t) ? "ld" : "lu");
390 printf(pformat
, (long) statbuf
->st_mtime
);
391 } else if (m
== 'z') {
392 printfs(pformat
, human_time(statbuf
->st_ctime
));
393 } else if (m
== 'Z') {
394 strcat(pformat
, TYPE_SIGNED(time_t) ? "ld" : "lu");
395 printf(pformat
, (long) statbuf
->st_ctime
);
397 } else if (m
== 'C' && (option_mask32
& OPT_SELINUX
)) {
398 printfs(pformat
, scontext
);
401 strcatc(pformat
, 'c');
406 static void print_it(const char *masterformat
,
407 const char *filename
,
408 void FAST_FUNC (*print_func
)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext
)),
410 IF_SELINUX(, security_context_t scontext
))
412 /* Create a working copy of the format string */
413 char *format
= xstrdup(masterformat
);
414 /* Add 2 to accommodate our conversion of the stat '%s' format string
415 * to the printf '%llu' one. */
416 char *dest
= xmalloc(strlen(format
) + 2 + 1);
421 /* Each iteration finds next %spec,
422 * prints preceding string and handles found %spec
425 char *p
= strchr(b
, '%');
427 /* coreutils 6.3 always prints newline at the end */
428 /*fputs(b, stdout);*/
433 /* dest = "%<modifiers>" */
434 len
= 1 + strspn(p
+ 1, "#-+.I 0123456789");
435 memcpy(dest
, p
, len
);
438 /* print preceding string */
452 /* Completes "%<modifiers>" with specifier and printfs */
453 print_func(dest
, *p
, filename
, data
IF_SELINUX(,scontext
));
461 #endif /* FEATURE_STAT_FORMAT */
463 #if ENABLE_FEATURE_STAT_FILESYSTEM
464 /* Stat the file system and print what we find. */
465 #if !ENABLE_FEATURE_STAT_FORMAT
466 #define do_statfs(filename, format) do_statfs(filename)
468 static bool do_statfs(const char *filename
, const char *format
)
470 struct statfs statfsbuf
;
471 #if !ENABLE_FEATURE_STAT_FORMAT
475 security_context_t scontext
= NULL
;
477 if (option_mask32
& OPT_SELINUX
) {
478 if ((option_mask32
& OPT_DEREFERENCE
479 ? lgetfilecon(filename
, &scontext
)
480 : getfilecon(filename
, &scontext
)
483 bb_simple_perror_msg(filename
);
488 if (statfs(filename
, &statfsbuf
) != 0) {
489 bb_perror_msg("can't read file system information for '%s'", filename
);
493 #if ENABLE_FEATURE_STAT_FORMAT
494 if (format
== NULL
) {
496 format
= (option_mask32
& OPT_TERSE
497 ? "%n %i %l %t %s %b %f %a %c %d\n"
499 " ID: %-8i Namelen: %-7l Type: %T\n"
500 "Block size: %-10s\n"
501 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
502 "Inodes: Total: %-10c Free: %d");
504 format
= (option_mask32
& OPT_TERSE
505 ? (option_mask32
& OPT_SELINUX
? "%n %i %l %t %s %b %f %a %c %d %C\n":
506 "%n %i %l %t %s %b %f %a %c %d\n")
507 : (option_mask32
& OPT_SELINUX
?
509 " ID: %-8i Namelen: %-7l Type: %T\n"
510 "Block size: %-10s\n"
511 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
512 "Inodes: Total: %-10c Free: %d"
515 " ID: %-8i Namelen: %-7l Type: %T\n"
516 "Block size: %-10s\n"
517 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
518 "Inodes: Total: %-10c Free: %d\n")
520 # endif /* SELINUX */
522 print_it(format
, filename
, print_statfs
, &statfsbuf
IF_SELINUX(, scontext
));
523 #else /* FEATURE_STAT_FORMAT */
524 format
= (option_mask32
& OPT_TERSE
527 " ID: %-8llx Namelen: %-7lu ");
530 get_f_fsid(&statfsbuf
),
531 statfsbuf
.f_namelen
);
533 if (option_mask32
& OPT_TERSE
)
534 printf("%lx ", (unsigned long) statfsbuf
.f_type
);
536 printf("Type: %s\n", human_fstype(statfsbuf
.f_type
));
539 format
= (option_mask32
& OPT_TERSE
540 ? "%lu %llu %llu %llu %llu %llu\n"
541 : "Block size: %-10lu\n"
542 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
543 "Inodes: Total: %-10llu Free: %llu\n");
545 (unsigned long) statfsbuf
.f_bsize
,
546 (unsigned long long) statfsbuf
.f_blocks
,
547 (unsigned long long) statfsbuf
.f_bfree
,
548 (unsigned long long) statfsbuf
.f_bavail
,
549 (unsigned long long) statfsbuf
.f_files
,
550 (unsigned long long) statfsbuf
.f_ffree
);
552 format
= (option_mask32
& OPT_TERSE
553 ? (option_mask32
& OPT_SELINUX
? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
554 : (option_mask32
& OPT_SELINUX
555 ? "Block size: %-10lu\n"
556 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
557 "Inodes: Total: %-10llu Free: %llu"
559 : "Block size: %-10lu\n"
560 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
561 "Inodes: Total: %-10llu Free: %llu\n"
565 (unsigned long) statfsbuf
.f_bsize
,
566 (unsigned long long) statfsbuf
.f_blocks
,
567 (unsigned long long) statfsbuf
.f_bfree
,
568 (unsigned long long) statfsbuf
.f_bavail
,
569 (unsigned long long) statfsbuf
.f_files
,
570 (unsigned long long) statfsbuf
.f_ffree
,
576 #endif /* FEATURE_STAT_FORMAT */
579 #endif /* FEATURE_STAT_FILESYSTEM */
581 /* stat the file and print what we find */
582 #if !ENABLE_FEATURE_STAT_FORMAT
583 #define do_stat(filename, format) do_stat(filename)
585 static bool do_stat(const char *filename
, const char *format
)
589 security_context_t scontext
= NULL
;
591 if (option_mask32
& OPT_SELINUX
) {
592 if ((option_mask32
& OPT_DEREFERENCE
593 ? lgetfilecon(filename
, &scontext
)
594 : getfilecon(filename
, &scontext
)
597 bb_simple_perror_msg(filename
);
602 if ((option_mask32
& OPT_DEREFERENCE
? stat
: lstat
) (filename
, &statbuf
) != 0) {
603 bb_perror_msg("can't stat '%s'", filename
);
607 #if ENABLE_FEATURE_STAT_FORMAT
608 if (format
== NULL
) {
610 if (option_mask32
& OPT_TERSE
) {
611 format
= "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
613 if (S_ISBLK(statbuf
.st_mode
) || S_ISCHR(statbuf
.st_mode
)) {
616 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
617 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
618 " Device type: %t,%T\n"
619 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
620 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
624 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
625 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
626 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
627 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
631 if (option_mask32
& OPT_TERSE
) {
632 format
= (option_mask32
& OPT_SELINUX
?
633 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
635 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
638 if (S_ISBLK(statbuf
.st_mode
) || S_ISCHR(statbuf
.st_mode
)) {
639 format
= (option_mask32
& OPT_SELINUX
?
641 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
642 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
643 " Device type: %t,%T\n"
644 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
646 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
649 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
650 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
651 " Device type: %t,%T\n"
652 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
653 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
656 format
= (option_mask32
& OPT_SELINUX
?
658 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
659 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
660 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
662 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
665 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
666 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
667 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
668 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
674 print_it(format
, filename
, print_stat
, &statbuf
IF_SELINUX(, scontext
));
675 #else /* FEATURE_STAT_FORMAT */
676 if (option_mask32
& OPT_TERSE
) {
677 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
678 IF_NOT_SELINUX("\n"),
680 (unsigned long long) statbuf
.st_size
,
681 (unsigned long long) statbuf
.st_blocks
,
682 (unsigned long) statbuf
.st_mode
,
683 (unsigned long) statbuf
.st_uid
,
684 (unsigned long) statbuf
.st_gid
,
685 (unsigned long long) statbuf
.st_dev
,
686 (unsigned long long) statbuf
.st_ino
,
687 (unsigned long) statbuf
.st_nlink
,
688 (unsigned long) major(statbuf
.st_rdev
),
689 (unsigned long) minor(statbuf
.st_rdev
),
690 (unsigned long) statbuf
.st_atime
,
691 (unsigned long) statbuf
.st_mtime
,
692 (unsigned long) statbuf
.st_ctime
,
693 (unsigned long) statbuf
.st_blksize
696 if (option_mask32
& OPT_SELINUX
)
697 printf(" %s\n", scontext
);
702 char *linkname
= NULL
;
703 struct passwd
*pw_ent
;
704 struct group
*gw_ent
;
706 gw_ent
= getgrgid(statbuf
.st_gid
);
707 pw_ent
= getpwuid(statbuf
.st_uid
);
709 if (S_ISLNK(statbuf
.st_mode
))
710 linkname
= xmalloc_readlink_or_warn(filename
);
712 printf(" File: '%s' -> '%s'\n", filename
, linkname
);
715 printf(" File: '%s'\n", filename
);
718 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
719 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
720 (unsigned long long) statbuf
.st_size
,
721 (unsigned long long) statbuf
.st_blocks
,
722 (unsigned long) statbuf
.st_blksize
,
724 (unsigned long long) statbuf
.st_dev
,
725 (unsigned long long) statbuf
.st_dev
,
726 (unsigned long long) statbuf
.st_ino
,
727 (unsigned long) statbuf
.st_nlink
);
728 if (S_ISBLK(statbuf
.st_mode
) || S_ISCHR(statbuf
.st_mode
))
729 printf(" Device type: %lx,%lx\n",
730 (unsigned long) major(statbuf
.st_rdev
),
731 (unsigned long) minor(statbuf
.st_rdev
));
734 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
735 (unsigned long) (statbuf
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
|S_IRWXU
|S_IRWXG
|S_IRWXO
)),
736 bb_mode_string(statbuf
.st_mode
),
737 (unsigned long) statbuf
.st_uid
,
738 (pw_ent
!= NULL
) ? pw_ent
->pw_name
: "UNKNOWN",
739 (unsigned long) statbuf
.st_gid
,
740 (gw_ent
!= NULL
) ? gw_ent
->gr_name
: "UNKNOWN");
742 if (option_mask32
& OPT_SELINUX
)
743 printf(" S_Context: %s\n", scontext
);
745 printf("Access: %s\n", human_time(statbuf
.st_atime
));
746 printf("Modify: %s\n", human_time(statbuf
.st_mtime
));
747 printf("Change: %s\n", human_time(statbuf
.st_ctime
));
749 #endif /* FEATURE_STAT_FORMAT */
753 int stat_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
754 int stat_main(int argc UNUSED_PARAM
, char **argv
)
756 IF_FEATURE_STAT_FORMAT(char *format
= NULL
;)
760 statfunc_ptr statfunc
= do_stat
;
762 opt_complementary
= "-1"; /* min one arg */
763 opts
= getopt32(argv
, "tL"
764 IF_FEATURE_STAT_FILESYSTEM("f")
766 IF_FEATURE_STAT_FORMAT("c:", &format
)
768 #if ENABLE_FEATURE_STAT_FILESYSTEM
769 if (opts
& OPT_FILESYS
) /* -f */
770 statfunc
= do_statfs
;
773 if (opts
& OPT_SELINUX
) {
779 for (i
= 0; argv
[i
]; ++i
)
780 ok
&= statfunc(argv
[i
] IF_FEATURE_STAT_FORMAT(, format
));
782 return (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);