source: MondoRescue/branches/3.3/mindi-busybox/coreutils/stat.c@ 3684

Last change on this file since 3684 was 3621, checked in by Bruno Cornec, 7 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 23.9 KB
RevLine 
[1765]1/* vi: set sw=4 ts=4: */
[821]2/*
3 * stat -- display file or file system status
4 *
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>
[1765]8 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
[821]9 *
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 *
[2725]13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]14 */
[3621]15//config:config STAT
16//config: bool "stat"
17//config: default y
18//config: help
19//config: display file or filesystem status.
20//config:
21//config:config FEATURE_STAT_FORMAT
22//config: bool "Enable custom formats (-c)"
23//config: default y
24//config: depends on STAT
25//config: help
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.
29//config:
30//config:config FEATURE_STAT_FILESYSTEM
31//config: bool "Enable display of filesystem status (-f)"
32//config: default y
33//config: depends on STAT
34//config: select PLATFORM_LINUX # statfs()
35//config: help
36//config: Without this, stat will not support the '-f' option to display
37//config: information about filesystem status.
[3232]38
[3621]39
[3232]40//usage:#define stat_trivial_usage
41//usage: "[OPTIONS] FILE..."
42//usage:#define stat_full_usage "\n\n"
[3621]43//usage: "Display file"
44//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
45//usage: " status\n"
[3232]46//usage: IF_FEATURE_STAT_FORMAT(
[3621]47//usage: "\n -c FMT Use the specified format"
[3232]48//usage: )
[3621]49//usage: IF_FEATURE_STAT_FILESYSTEM(
[3232]50//usage: "\n -f Display filesystem status"
[3621]51//usage: )
[3232]52//usage: "\n -L Follow links"
[3621]53//usage: "\n -t Terse display"
[3232]54//usage: IF_SELINUX(
55//usage: "\n -Z Print security context"
56//usage: )
57//usage: IF_FEATURE_STAT_FORMAT(
[3621]58//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
[3232]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"
[3621]62//usage: " %B Size in bytes of each block reported by %b\n"
[3232]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"
[3621]67//usage: " %g Group ID\n"
68//usage: " %G Group name\n"
[3232]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"
[3621]74//usage: " %s Total size in bytes\n"
[3232]75//usage: " %t Major device type in hex\n"
76//usage: " %T Minor device type in hex\n"
[3621]77//usage: " %u User ID\n"
78//usage: " %U User name\n"
[3232]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"
[3621]85//usage: IF_FEATURE_STAT_FILESYSTEM(
86//usage: "\nFMT sequences for file systems:\n"
[3232]87//usage: " %a Free blocks available to non-superuser\n"
[3621]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"
[3232]92//usage: IF_SELINUX(
93//usage: " %C Security context in selinux\n"
94//usage: )
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"
102//usage: )
[3621]103//usage: )
[3232]104
[1765]105#include "libbb.h"
[3621]106#include "common_bufsiz.h"
[821]107
[3621]108enum {
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,
113};
[821]114
[2725]115#if ENABLE_FEATURE_STAT_FORMAT
116typedef bool (*statfunc_ptr)(const char *, const char *);
117#else
118typedef bool (*statfunc_ptr)(const char *);
119#endif
[1765]120
[2725]121static const char *file_type(const struct stat *st)
[821]122{
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.
127 */
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";
[3232]135#ifdef S_TYPEISMQ
[821]136 if (S_TYPEISMQ(st)) return "message queue";
[3232]137#endif
138#ifdef S_TYPEISSEM
[821]139 if (S_TYPEISSEM(st)) return "semaphore";
[3232]140#endif
141#ifdef S_TYPEISSHM
[821]142 if (S_TYPEISSHM(st)) return "shared memory object";
[3232]143#endif
[821]144#ifdef S_TYPEISTMO
145 if (S_TYPEISTMO(st)) return "typed memory object";
146#endif
147 return "weird file";
148}
149
[2725]150static const char *human_time(time_t t)
[821]151{
[1765]152 /* Old
[821]153 static char *str;
154 str = ctime(&t);
155 str[strlen(str)-1] = '\0';
156 return str;
[1765]157 */
158 /* coreutils 6.3 compat: */
159
[2725]160 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
161#define buf bb_common_bufsiz1
[3621]162 setup_common_bufsiz();
163 strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
[1765]164 return buf;
[2725]165#undef buf
[821]166}
167
[3621]168#if ENABLE_FEATURE_STAT_FILESYSTEM
[821]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).
173 */
[2725]174static const char *human_fstype(uint32_t f_type)
[821]175{
176 static const struct types {
[2725]177 uint32_t type;
[1765]178 const char *const fs;
[821]179 } humantypes[] = {
180 { 0xADFF, "affs" },
181 { 0x1Cd1, "devpts" },
182 { 0x137D, "ext" },
183 { 0xEF51, "ext2" },
184 { 0xEF53, "ext2/ext3" },
185 { 0x3153464a, "jfs" },
186 { 0x58465342, "xfs" },
187 { 0xF995E849, "hpfs" },
188 { 0x9660, "isofs" },
189 { 0x4000, "isofs" },
190 { 0x4004, "isofs" },
191 { 0x137F, "minix" },
192 { 0x138F, "minix (30 char.)" },
193 { 0x2468, "minix v2" },
194 { 0x2478, "minix v2 (30 char.)" },
195 { 0x4d44, "msdos" },
196 { 0x4006, "fat" },
197 { 0x564c, "novell" },
198 { 0x6969, "nfs" },
199 { 0x9fa0, "proc" },
200 { 0x517B, "smb" },
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" },
211 { 0x7275, "romfs" },
212 { 0x858458f6, "romfs" },
213 { 0x73717368, "squashfs" },
214 { 0x62656572, "sysfs" },
215 { 0, "UNKNOWN" }
216 };
[2725]217
218 int i;
219
[1765]220 for (i = 0; humantypes[i].type; ++i)
[821]221 if (humantypes[i].type == f_type)
222 break;
223 return humantypes[i].fs;
224}
225
[2725]226/* "man statfs" says that statfsbuf->f_fsid is a mess */
227/* coreutils treats it as an array of ints, most significant first */
228static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
229{
230 const unsigned *p = (const void*) &statfsbuf->f_fsid;
231 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
232 unsigned long long r = 0;
233
234 do
235 r = (r << (sizeof(unsigned)*8)) | *p++;
236 while (--sz > 0);
237 return r;
238}
[3621]239#endif /* FEATURE_STAT_FILESYSTEM */
[2725]240
[1765]241#if ENABLE_FEATURE_STAT_FORMAT
[2725]242static void strcatc(char *str, char c)
243{
244 int len = strlen(str);
245 str[len++] = c;
246 str[len] = '\0';
247}
248
249static void printfs(char *pformat, const char *msg)
250{
251 strcatc(pformat, 's');
252 printf(pformat, msg);
253}
254
[3621]255#if ENABLE_FEATURE_STAT_FILESYSTEM
[821]256/* print statfs info */
[2725]257static 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))
[821]260{
[2725]261 const struct statfs *statfsbuf = data;
[1765]262 if (m == 'n') {
[2725]263 printfs(pformat, filename);
[1765]264 } else if (m == 'i') {
[2725]265 strcat(pformat, "llx");
266 printf(pformat, get_f_fsid(statfsbuf));
[1765]267 } else if (m == 'l') {
[2725]268 strcat(pformat, "lu");
269 printf(pformat, (unsigned long) statfsbuf->f_namelen);
[1765]270 } else if (m == 't') {
[2725]271 strcat(pformat, "lx");
272 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
[1765]273 } else if (m == 'T') {
[2725]274 printfs(pformat, human_fstype(statfsbuf->f_type));
[1765]275 } else if (m == 'b') {
[2725]276 strcat(pformat, "llu");
277 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
[1765]278 } else if (m == 'f') {
[2725]279 strcat(pformat, "llu");
280 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
[1765]281 } else if (m == 'a') {
[2725]282 strcat(pformat, "llu");
283 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
[1765]284 } else if (m == 's' || m == 'S') {
[2725]285 strcat(pformat, "lu");
286 printf(pformat, (unsigned long) statfsbuf->f_bsize);
[1765]287 } else if (m == 'c') {
[2725]288 strcat(pformat, "llu");
289 printf(pformat, (unsigned long long) statfsbuf->f_files);
[1765]290 } else if (m == 'd') {
[2725]291 strcat(pformat, "llu");
292 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
293# if ENABLE_SELINUX
[1765]294 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
[2725]295 printfs(pformat, scontext);
296# endif
[1765]297 } else {
[2725]298 strcatc(pformat, 'c');
[821]299 printf(pformat, m);
300 }
301}
[3621]302#endif
[821]303
304/* print stat info */
[2725]305static 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))
[821]308{
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;
313
[1765]314 if (m == 'n') {
[2725]315 printfs(pformat, filename);
[1765]316 } else if (m == 'N') {
[2725]317 strcatc(pformat, 's');
[821]318 if (S_ISLNK(statbuf->st_mode)) {
[1765]319 char *linkname = xmalloc_readlink_or_warn(filename);
[2725]320 if (linkname == NULL)
[821]321 return;
[2725]322 printf("'%s' -> '%s'", filename, linkname);
323 free(linkname);
[821]324 } else {
325 printf(pformat, filename);
326 }
[1765]327 } else if (m == 'd') {
[2725]328 strcat(pformat, "llu");
329 printf(pformat, (unsigned long long) statbuf->st_dev);
[1765]330 } else if (m == 'D') {
[2725]331 strcat(pformat, "llx");
332 printf(pformat, (unsigned long long) statbuf->st_dev);
[1765]333 } else if (m == 'i') {
[2725]334 strcat(pformat, "llu");
335 printf(pformat, (unsigned long long) statbuf->st_ino);
[1765]336 } else if (m == 'a') {
[2725]337 strcat(pformat, "lo");
[1765]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') {
[2725]340 printfs(pformat, bb_mode_string(statbuf->st_mode));
[1765]341 } else if (m == 'f') {
[2725]342 strcat(pformat, "lx");
[1765]343 printf(pformat, (unsigned long) statbuf->st_mode);
344 } else if (m == 'F') {
[2725]345 printfs(pformat, file_type(statbuf));
[1765]346 } else if (m == 'h') {
[2725]347 strcat(pformat, "lu");
[1765]348 printf(pformat, (unsigned long) statbuf->st_nlink);
349 } else if (m == 'u') {
[2725]350 strcat(pformat, "lu");
[1765]351 printf(pformat, (unsigned long) statbuf->st_uid);
352 } else if (m == 'U') {
[821]353 pw_ent = getpwuid(statbuf->st_uid);
[2725]354 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
[1765]355 } else if (m == 'g') {
[2725]356 strcat(pformat, "lu");
[1765]357 printf(pformat, (unsigned long) statbuf->st_gid);
358 } else if (m == 'G') {
[821]359 gw_ent = getgrgid(statbuf->st_gid);
[2725]360 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
[1765]361 } else if (m == 't') {
[2725]362 strcat(pformat, "lx");
[1765]363 printf(pformat, (unsigned long) major(statbuf->st_rdev));
364 } else if (m == 'T') {
[2725]365 strcat(pformat, "lx");
[1765]366 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
367 } else if (m == 's') {
[2725]368 strcat(pformat, "llu");
369 printf(pformat, (unsigned long long) statbuf->st_size);
[1765]370 } else if (m == 'B') {
[2725]371 strcat(pformat, "lu");
[1765]372 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
373 } else if (m == 'b') {
[2725]374 strcat(pformat, "llu");
375 printf(pformat, (unsigned long long) statbuf->st_blocks);
[1765]376 } else if (m == 'o') {
[2725]377 strcat(pformat, "lu");
[1765]378 printf(pformat, (unsigned long) statbuf->st_blksize);
379 } else if (m == 'x') {
[2725]380 printfs(pformat, human_time(statbuf->st_atime));
[1765]381 } else if (m == 'X') {
[2725]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);
[1765]386 } else if (m == 'y') {
[2725]387 printfs(pformat, human_time(statbuf->st_mtime));
[1765]388 } else if (m == 'Y') {
[2725]389 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
390 printf(pformat, (long) statbuf->st_mtime);
[1765]391 } else if (m == 'z') {
[2725]392 printfs(pformat, human_time(statbuf->st_ctime));
[1765]393 } else if (m == 'Z') {
[2725]394 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
395 printf(pformat, (long) statbuf->st_ctime);
396# if ENABLE_SELINUX
[1765]397 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
[2725]398 printfs(pformat, scontext);
399# endif
[1765]400 } else {
[2725]401 strcatc(pformat, 'c');
[821]402 printf(pformat, m);
403 }
404}
405
[2725]406static 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)),
409 const void *data
410 IF_SELINUX(, security_context_t scontext))
[821]411{
[2725]412 /* Create a working copy of the format string */
[1765]413 char *format = xstrdup(masterformat);
[3621]414 /* Add 2 to accommodate our conversion of the stat '%s' format string
[1765]415 * to the printf '%llu' one. */
[2725]416 char *dest = xmalloc(strlen(format) + 2 + 1);
417 char *b;
[821]418
419 b = format;
420 while (b) {
[2725]421 /* Each iteration finds next %spec,
422 * prints preceding string and handles found %spec
423 */
[1765]424 size_t len;
[821]425 char *p = strchr(b, '%');
[1765]426 if (!p) {
[2725]427 /* coreutils 6.3 always prints newline at the end */
[1765]428 /*fputs(b, stdout);*/
429 puts(b);
430 break;
431 }
[2725]432
433 /* dest = "%<modifiers>" */
434 len = 1 + strspn(p + 1, "#-+.I 0123456789");
435 memcpy(dest, p, len);
436 dest[len] = '\0';
437
438 /* print preceding string */
439 *p = '\0';
[1765]440 fputs(b, stdout);
[821]441
[1765]442 p += len;
443 b = p + 1;
444 switch (*p) {
445 case '\0':
[821]446 b = NULL;
[1765]447 /* fall through */
448 case '%':
[2725]449 bb_putchar('%');
[1765]450 break;
451 default:
[2725]452 /* Completes "%<modifiers>" with specifier and printfs */
453 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
[1765]454 break;
[821]455 }
456 }
457
458 free(format);
459 free(dest);
460}
[2725]461#endif /* FEATURE_STAT_FORMAT */
[821]462
[3621]463#if ENABLE_FEATURE_STAT_FILESYSTEM
[821]464/* Stat the file system and print what we find. */
[2725]465#if !ENABLE_FEATURE_STAT_FORMAT
466#define do_statfs(filename, format) do_statfs(filename)
467#endif
468static bool do_statfs(const char *filename, const char *format)
[821]469{
470 struct statfs statfsbuf;
[2725]471#if !ENABLE_FEATURE_STAT_FORMAT
472 const char *format;
473#endif
[1765]474#if ENABLE_SELINUX
475 security_context_t scontext = NULL;
[821]476
[1765]477 if (option_mask32 & OPT_SELINUX) {
478 if ((option_mask32 & OPT_DEREFERENCE
479 ? lgetfilecon(filename, &scontext)
480 : getfilecon(filename, &scontext)
481 ) < 0
482 ) {
[3232]483 bb_simple_perror_msg(filename);
[1765]484 return 0;
485 }
486 }
487#endif
[821]488 if (statfs(filename, &statfsbuf) != 0) {
[2725]489 bb_perror_msg("can't read file system information for '%s'", filename);
[821]490 return 0;
491 }
492
[1765]493#if ENABLE_FEATURE_STAT_FORMAT
[2725]494 if (format == NULL) {
495# if !ENABLE_SELINUX
[1765]496 format = (option_mask32 & OPT_TERSE
[821]497 ? "%n %i %l %t %s %b %f %a %c %d\n"
498 : " File: \"%n\"\n"
499 " ID: %-8i Namelen: %-7l Type: %T\n"
500 "Block size: %-10s\n"
501 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
[1765]502 "Inodes: Total: %-10c Free: %d");
[2725]503# else
504 format = (option_mask32 & OPT_TERSE
[1765]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 ?
508 " File: \"%n\"\n"
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"
513 " S_context: %C\n":
514 " File: \"%n\"\n"
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")
519 );
[2725]520# endif /* SELINUX */
521 }
522 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
[1765]523#else /* FEATURE_STAT_FORMAT */
524 format = (option_mask32 & OPT_TERSE
[821]525 ? "%s %llx %lu "
526 : " File: \"%s\"\n"
[2725]527 " ID: %-8llx Namelen: %-7lu ");
[821]528 printf(format,
529 filename,
[2725]530 get_f_fsid(&statfsbuf),
[821]531 statfsbuf.f_namelen);
532
[1765]533 if (option_mask32 & OPT_TERSE)
[2725]534 printf("%lx ", (unsigned long) statfsbuf.f_type);
[821]535 else
536 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
537
[2725]538# if !ENABLE_SELINUX
[1765]539 format = (option_mask32 & OPT_TERSE
[2725]540 ? "%lu %llu %llu %llu %llu %llu\n"
[821]541 : "Block size: %-10lu\n"
[2725]542 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
543 "Inodes: Total: %-10llu Free: %llu\n");
[821]544 printf(format,
[2725]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);
551# else
[1765]552 format = (option_mask32 & OPT_TERSE
[2725]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"
558 "S_context: %C\n"
559 : "Block size: %-10lu\n"
560 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
561 "Inodes: Total: %-10llu Free: %llu\n"
562 )
563 );
[1765]564 printf(format,
[2725]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,
[1765]571 scontext);
572
573 if (scontext)
574 freecon(scontext);
[2725]575# endif
576#endif /* FEATURE_STAT_FORMAT */
[821]577 return 1;
578}
[3621]579#endif /* FEATURE_STAT_FILESYSTEM */
[821]580
581/* stat the file and print what we find */
[2725]582#if !ENABLE_FEATURE_STAT_FORMAT
583#define do_stat(filename, format) do_stat(filename)
584#endif
585static bool do_stat(const char *filename, const char *format)
[821]586{
587 struct stat statbuf;
[1765]588#if ENABLE_SELINUX
589 security_context_t scontext = NULL;
[821]590
[1765]591 if (option_mask32 & OPT_SELINUX) {
592 if ((option_mask32 & OPT_DEREFERENCE
593 ? lgetfilecon(filename, &scontext)
594 : getfilecon(filename, &scontext)
595 ) < 0
596 ) {
[3232]597 bb_simple_perror_msg(filename);
[1765]598 return 0;
599 }
600 }
601#endif
602 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
[2725]603 bb_perror_msg("can't stat '%s'", filename);
[821]604 return 0;
605 }
606
[1765]607#if ENABLE_FEATURE_STAT_FORMAT
[821]608 if (format == NULL) {
[2725]609# if !ENABLE_SELINUX
[1765]610 if (option_mask32 & OPT_TERSE) {
611 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
[821]612 } else {
613 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
614 format =
[2725]615 " File: %N\n"
[821]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";
621 } else {
622 format =
[2725]623 " File: %N\n"
[821]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";
628 }
629 }
[2725]630# else
[1765]631 if (option_mask32 & OPT_TERSE) {
632 format = (option_mask32 & OPT_SELINUX ?
[3232]633 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
634 :
635 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
636 );
[1765]637 } else {
638 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
639 format = (option_mask32 & OPT_SELINUX ?
[3232]640 " File: %N\n"
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"
645 " S_Context: %C\n"
646 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
647 :
648 " File: %N\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"
654 );
[1765]655 } else {
656 format = (option_mask32 & OPT_SELINUX ?
[3232]657 " File: %N\n"
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"
661 "S_Context: %C\n"
662 "Access: %x\n" "Modify: %y\n" "Change: %z\n"
663 :
664 " File: %N\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"
669 );
[1765]670 }
671 }
[2725]672# endif
[821]673 }
[2725]674 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
[1765]675#else /* FEATURE_STAT_FORMAT */
676 if (option_mask32 & OPT_TERSE) {
[2725]677 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
678 IF_NOT_SELINUX("\n"),
[821]679 filename,
[2725]680 (unsigned long long) statbuf.st_size,
681 (unsigned long long) statbuf.st_blocks,
[1765]682 (unsigned long) statbuf.st_mode,
683 (unsigned long) statbuf.st_uid,
684 (unsigned long) statbuf.st_gid,
[2725]685 (unsigned long long) statbuf.st_dev,
686 (unsigned long long) statbuf.st_ino,
[1765]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
[821]694 );
[2725]695# if ENABLE_SELINUX
[1765]696 if (option_mask32 & OPT_SELINUX)
[3621]697 printf(" %s\n", scontext);
[1765]698 else
[2725]699 bb_putchar('\n');
700# endif
[821]701 } else {
702 char *linkname = NULL;
703 struct passwd *pw_ent;
704 struct group *gw_ent;
[2725]705
[821]706 gw_ent = getgrgid(statbuf.st_gid);
707 pw_ent = getpwuid(statbuf.st_uid);
708
709 if (S_ISLNK(statbuf.st_mode))
[1765]710 linkname = xmalloc_readlink_or_warn(filename);
[2725]711 if (linkname) {
712 printf(" File: '%s' -> '%s'\n", filename, linkname);
713 free(linkname);
714 } else {
715 printf(" File: '%s'\n", filename);
716 }
[821]717
[2725]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,
[1765]722 (unsigned long) statbuf.st_blksize,
[821]723 file_type(&statbuf),
[2725]724 (unsigned long long) statbuf.st_dev,
725 (unsigned long long) statbuf.st_dev,
726 (unsigned long long) statbuf.st_ino,
[1765]727 (unsigned long) statbuf.st_nlink);
[821]728 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
729 printf(" Device type: %lx,%lx\n",
[1765]730 (unsigned long) major(statbuf.st_rdev),
731 (unsigned long) minor(statbuf.st_rdev));
[821]732 else
[2725]733 bb_putchar('\n');
[1765]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)),
[821]736 bb_mode_string(statbuf.st_mode),
[1765]737 (unsigned long) statbuf.st_uid,
[2725]738 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
[1765]739 (unsigned long) statbuf.st_gid,
[2725]740 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
741# if ENABLE_SELINUX
[3621]742 if (option_mask32 & OPT_SELINUX)
743 printf(" S_Context: %s\n", scontext);
[2725]744# endif
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));
[821]748 }
[2725]749#endif /* FEATURE_STAT_FORMAT */
[821]750 return 1;
751}
752
[2725]753int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
754int stat_main(int argc UNUSED_PARAM, char **argv)
[821]755{
[2725]756 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
[821]757 int i;
[2725]758 int ok;
759 unsigned opts;
760 statfunc_ptr statfunc = do_stat;
[821]761
[2725]762 opt_complementary = "-1"; /* min one arg */
[3621]763 opts = getopt32(argv, "tL"
764 IF_FEATURE_STAT_FILESYSTEM("f")
[2725]765 IF_SELINUX("Z")
766 IF_FEATURE_STAT_FORMAT("c:", &format)
[821]767 );
[3621]768#if ENABLE_FEATURE_STAT_FILESYSTEM
[2725]769 if (opts & OPT_FILESYS) /* -f */
[821]770 statfunc = do_statfs;
[3621]771#endif
[1765]772#if ENABLE_SELINUX
[2725]773 if (opts & OPT_SELINUX) {
[1765]774 selinux_or_die();
775 }
[2725]776#endif
777 ok = 1;
778 argv += optind;
779 for (i = 0; argv[i]; ++i)
780 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
[821]781
782 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
783}
Note: See TracBrowser for help on using the repository browser.