1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#include "libbb.h"
108#include "common_bufsiz.h"
109
110enum {
111 OPT_TERSE = (1 << 0),
112 OPT_DEREFERENCE = (1 << 1),
113 OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
114 OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
115};
116
117#if ENABLE_FEATURE_STAT_FORMAT
118typedef bool (*statfunc_ptr)(const char *, const char *);
119#else
120typedef bool (*statfunc_ptr)(const char *);
121#endif
122
123static const char *file_type(const struct stat *st)
124{
125
126
127
128
129
130 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
131 if (S_ISDIR(st->st_mode)) return "directory";
132 if (S_ISBLK(st->st_mode)) return "block special file";
133 if (S_ISCHR(st->st_mode)) return "character special file";
134 if (S_ISFIFO(st->st_mode)) return "fifo";
135 if (S_ISLNK(st->st_mode)) return "symbolic link";
136 if (S_ISSOCK(st->st_mode)) return "socket";
137#ifdef S_TYPEISMQ
138 if (S_TYPEISMQ(st)) return "message queue";
139#endif
140#ifdef S_TYPEISSEM
141 if (S_TYPEISSEM(st)) return "semaphore";
142#endif
143#ifdef S_TYPEISSHM
144 if (S_TYPEISSHM(st)) return "shared memory object";
145#endif
146#ifdef S_TYPEISTMO
147 if (S_TYPEISTMO(st)) return "typed memory object";
148#endif
149 return "weird file";
150}
151
152static const char *human_time(struct timespec *ts)
153{
154 char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + 8];
155
156
157#define buf bb_common_bufsiz1
158 setup_common_bufsiz();
159
160 sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
161 strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
162 return buf;
163#undef buf
164}
165
166#if ENABLE_FEATURE_STAT_FILESYSTEM
167#define FS_TYPE_LIST \
168FS_TYPE(0xADFF, "affs") \
169FS_TYPE(0x1CD1, "devpts") \
170FS_TYPE(0x137D, "ext") \
171FS_TYPE(0xEF51, "ext2") \
172FS_TYPE(0xEF53, "ext2/ext3") \
173FS_TYPE(0x3153464a, "jfs") \
174FS_TYPE(0x58465342, "xfs") \
175FS_TYPE(0xF995E849, "hpfs") \
176FS_TYPE(0x9660, "isofs") \
177FS_TYPE(0x4000, "isofs") \
178FS_TYPE(0x4004, "isofs") \
179FS_TYPE(0x137F, "minix") \
180FS_TYPE(0x138F, "minix (30 char.)") \
181FS_TYPE(0x2468, "minix v2") \
182FS_TYPE(0x2478, "minix v2 (30 char.)") \
183FS_TYPE(0x4d44, "msdos") \
184FS_TYPE(0x4006, "fat") \
185FS_TYPE(0x564c, "novell") \
186FS_TYPE(0x6969, "nfs") \
187FS_TYPE(0x9fa0, "proc") \
188FS_TYPE(0x517B, "smb") \
189FS_TYPE(0x012FF7B4, "xenix") \
190FS_TYPE(0x012FF7B5, "sysv4") \
191FS_TYPE(0x012FF7B6, "sysv2") \
192FS_TYPE(0x012FF7B7, "coh") \
193FS_TYPE(0x00011954, "ufs") \
194FS_TYPE(0x012FD16D, "xia") \
195FS_TYPE(0x5346544e, "ntfs") \
196FS_TYPE(0x1021994, "tmpfs") \
197FS_TYPE(0x52654973, "reiserfs") \
198FS_TYPE(0x28cd3d45, "cramfs") \
199FS_TYPE(0x7275, "romfs") \
200FS_TYPE(0x858458f6, "ramfs") \
201FS_TYPE(0x73717368, "squashfs") \
202FS_TYPE(0x62656572, "sysfs")
203
204
205
206
207
208static const char *human_fstype(uint32_t f_type)
209{
210# define FS_TYPE(type, name) type,
211 static const uint32_t fstype[] ALIGN4 = {
212 FS_TYPE_LIST
213 };
214# undef FS_TYPE
215# define FS_TYPE(type, name) name"\0"
216 static const char humanname[] ALIGN1 =
217 FS_TYPE_LIST
218 "UNKNOWN";
219# undef FS_TYPE
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(fstype); ++i)
223 if (fstype[i] == f_type)
224 break;
225 return nth_string(humanname, i);
226}
227
228
229
230static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
231{
232 const unsigned *p = (const void*) &statfsbuf->f_fsid;
233 unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
234 unsigned long long r = 0;
235
236 do
237 r = (r << (sizeof(unsigned)*8)) | *p++;
238 while (--sz > 0);
239 return r;
240}
241#endif
242
243#if ENABLE_FEATURE_STAT_FORMAT
244static void strcatc(char *str, char c)
245{
246 int len = strlen(str);
247 str[len++] = c;
248 str[len] = '\0';
249}
250
251static void printfs(char *pformat, const char *msg)
252{
253 strcatc(pformat, 's');
254 printf(pformat, msg);
255}
256
257#if ENABLE_FEATURE_STAT_FILESYSTEM
258
259static void FAST_FUNC print_statfs(char *pformat, const char m,
260 const char *const filename, const void *data
261 IF_SELINUX(, security_context_t scontext))
262{
263 const struct statfs *statfsbuf = data;
264 if (m == 'n') {
265 printfs(pformat, filename);
266 } else if (m == 'i') {
267 strcat(pformat, "llx");
268 printf(pformat, get_f_fsid(statfsbuf));
269 } else if (m == 'l') {
270 strcat(pformat, "lu");
271 printf(pformat, (unsigned long) statfsbuf->f_namelen);
272 } else if (m == 't') {
273 strcat(pformat, "lx");
274 printf(pformat, (unsigned long) statfsbuf->f_type);
275 } else if (m == 'T') {
276 printfs(pformat, human_fstype(statfsbuf->f_type));
277 } else if (m == 'b') {
278 strcat(pformat, "llu");
279 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
280 } else if (m == 'f') {
281 strcat(pformat, "llu");
282 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
283 } else if (m == 'a') {
284 strcat(pformat, "llu");
285 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
286 } else if (m == 's' || m == 'S') {
287 strcat(pformat, "lu");
288 printf(pformat, (unsigned long) statfsbuf->f_bsize);
289 } else if (m == 'c') {
290 strcat(pformat, "llu");
291 printf(pformat, (unsigned long long) statfsbuf->f_files);
292 } else if (m == 'd') {
293 strcat(pformat, "llu");
294 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
295# if ENABLE_SELINUX
296 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
297 printfs(pformat, scontext);
298# endif
299 } else {
300 strcatc(pformat, 'c');
301 printf(pformat, m);
302 }
303}
304#endif
305
306
307static void FAST_FUNC print_stat(char *pformat, const char m,
308 const char *const filename, const void *data
309 IF_SELINUX(, security_context_t scontext))
310{
311#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
312 struct stat *statbuf = (struct stat *) data;
313 struct passwd *pw_ent;
314 struct group *gw_ent;
315
316 if (m == 'n') {
317 printfs(pformat, filename);
318 } else if (m == 'N') {
319 strcatc(pformat, 's');
320 if (S_ISLNK(statbuf->st_mode)) {
321 char *linkname = xmalloc_readlink_or_warn(filename);
322 if (linkname == NULL)
323 return;
324 printf("'%s' -> '%s'", filename, linkname);
325 free(linkname);
326 } else {
327 printf(pformat, filename);
328 }
329 } else if (m == 'd') {
330 strcat(pformat, "llu");
331 printf(pformat, (unsigned long long) statbuf->st_dev);
332 } else if (m == 'D') {
333 strcat(pformat, "llx");
334 printf(pformat, (unsigned long long) statbuf->st_dev);
335 } else if (m == 'i') {
336 strcat(pformat, "llu");
337 printf(pformat, (unsigned long long) statbuf->st_ino);
338 } else if (m == 'a') {
339 strcat(pformat, "lo");
340 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
341 } else if (m == 'A') {
342 printfs(pformat, bb_mode_string(statbuf->st_mode));
343 } else if (m == 'f') {
344 strcat(pformat, "lx");
345 printf(pformat, (unsigned long) statbuf->st_mode);
346 } else if (m == 'F') {
347 printfs(pformat, file_type(statbuf));
348 } else if (m == 'h') {
349 strcat(pformat, "lu");
350 printf(pformat, (unsigned long) statbuf->st_nlink);
351 } else if (m == 'u') {
352 strcat(pformat, "lu");
353 printf(pformat, (unsigned long) statbuf->st_uid);
354 } else if (m == 'U') {
355 pw_ent = getpwuid(statbuf->st_uid);
356 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
357 } else if (m == 'g') {
358 strcat(pformat, "lu");
359 printf(pformat, (unsigned long) statbuf->st_gid);
360 } else if (m == 'G') {
361 gw_ent = getgrgid(statbuf->st_gid);
362 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
363 } else if (m == 't') {
364 strcat(pformat, "lx");
365 printf(pformat, (unsigned long) major(statbuf->st_rdev));
366 } else if (m == 'T') {
367 strcat(pformat, "lx");
368 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
369 } else if (m == 's') {
370 strcat(pformat, "llu");
371 printf(pformat, (unsigned long long) statbuf->st_size);
372 } else if (m == 'B') {
373 strcat(pformat, "lu");
374 printf(pformat, (unsigned long) 512);
375 } else if (m == 'b') {
376 strcat(pformat, "llu");
377 printf(pformat, (unsigned long long) statbuf->st_blocks);
378 } else if (m == 'o') {
379 strcat(pformat, "lu");
380 printf(pformat, (unsigned long) statbuf->st_blksize);
381 } else if (m == 'x') {
382 printfs(pformat, human_time(&statbuf->st_atim));
383 } else if (m == 'X') {
384 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
385
386
387 printf(pformat, (long) statbuf->st_atime);
388 } else if (m == 'y') {
389 printfs(pformat, human_time(&statbuf->st_mtim));
390 } else if (m == 'Y') {
391 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
392 printf(pformat, (long) statbuf->st_mtime);
393 } else if (m == 'z') {
394 printfs(pformat, human_time(&statbuf->st_ctim));
395 } else if (m == 'Z') {
396 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
397 printf(pformat, (long) statbuf->st_ctime);
398# if ENABLE_SELINUX
399 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
400 printfs(pformat, scontext);
401# endif
402 } else {
403 strcatc(pformat, 'c');
404 printf(pformat, m);
405 }
406}
407
408static void print_it(const char *masterformat,
409 const char *filename,
410 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
411 const void *data
412 IF_SELINUX(, security_context_t scontext))
413{
414
415 char *format = xstrdup(masterformat);
416
417
418 char *dest = xmalloc(strlen(format) + 2 + 1);
419 char *b;
420
421 b = format;
422 while (b) {
423
424
425
426 size_t len;
427 char *p = strchr(b, '%');
428 if (!p) {
429
430
431 puts(b);
432 break;
433 }
434
435
436 len = 1 + strspn(p + 1, "#-+.I 0123456789");
437 memcpy(dest, p, len);
438 dest[len] = '\0';
439
440
441 *p = '\0';
442 fputs_stdout(b);
443
444 p += len;
445 b = p + 1;
446 switch (*p) {
447 case '\0':
448 b = NULL;
449
450 case '%':
451 bb_putchar('%');
452 break;
453 default:
454
455 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
456 break;
457 }
458 }
459
460 free(format);
461 free(dest);
462}
463#endif
464
465#if ENABLE_FEATURE_STAT_FILESYSTEM
466
467#if !ENABLE_FEATURE_STAT_FORMAT
468#define do_statfs(filename, format) do_statfs(filename)
469#endif
470static bool do_statfs(const char *filename, const char *format)
471{
472 struct statfs statfsbuf;
473#if !ENABLE_FEATURE_STAT_FORMAT
474 const char *format;
475#endif
476#if ENABLE_SELINUX
477 security_context_t scontext = NULL;
478
479 if (option_mask32 & OPT_SELINUX) {
480 if ((option_mask32 & OPT_DEREFERENCE
481 ? lgetfilecon(filename, &scontext)
482 : getfilecon(filename, &scontext)
483 ) < 0
484 ) {
485 bb_simple_perror_msg(filename);
486 return 0;
487 }
488 }
489#endif
490 if (statfs(filename, &statfsbuf) != 0) {
491 bb_perror_msg("can't read file system information for '%s'", filename);
492 return 0;
493 }
494
495#if ENABLE_FEATURE_STAT_FORMAT
496 if (format == NULL) {
497# if !ENABLE_SELINUX
498 format = (option_mask32 & OPT_TERSE
499 ? "%n %i %l %t %s %b %f %a %c %d"
500 : " File: \"%n\"\n"
501 " ID: %-8i Namelen: %-7l Type: %T\n"
502 "Block size: %-10s\n"
503 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
504 "Inodes: Total: %-10c Free: %d");
505# else
506 format = (option_mask32 & OPT_TERSE
507 ? (option_mask32 & OPT_SELINUX
508 ? "%n %i %l %t %s %b %f %a %c %d %C"
509 : "%n %i %l %t %s %b %f %a %c %d")
510 : (option_mask32 & OPT_SELINUX
511 ? " File: \"%n\"\n"
512 " ID: %-8i Namelen: %-7l Type: %T\n"
513 "Block size: %-10s\n"
514 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
515 "Inodes: Total: %-10c Free: %d"
516 " S_context: %C"
517 : " File: \"%n\"\n"
518 " ID: %-8i Namelen: %-7l Type: %T\n"
519 "Block size: %-10s\n"
520 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
521 "Inodes: Total: %-10c Free: %d")
522 );
523# endif
524 }
525 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
526#else
527 format = (option_mask32 & OPT_TERSE
528 ? "%s %llx %lu "
529 : " File: \"%s\"\n"
530 " ID: %-8llx Namelen: %-7lu ");
531 printf(format,
532 filename,
533 get_f_fsid(&statfsbuf),
534 statfsbuf.f_namelen);
535
536 if (option_mask32 & OPT_TERSE)
537 printf("%lx ", (unsigned long) statfsbuf.f_type);
538 else
539 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
540
541# if !ENABLE_SELINUX
542 format = (option_mask32 & OPT_TERSE
543 ? "%lu %llu %llu %llu %llu %llu\n"
544 : "Block size: %-10lu\n"
545 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
546 "Inodes: Total: %-10llu Free: %llu\n");
547 printf(format,
548 (unsigned long) statfsbuf.f_bsize,
549 (unsigned long long) statfsbuf.f_blocks,
550 (unsigned long long) statfsbuf.f_bfree,
551 (unsigned long long) statfsbuf.f_bavail,
552 (unsigned long long) statfsbuf.f_files,
553 (unsigned long long) statfsbuf.f_ffree);
554# else
555 format = (option_mask32 & OPT_TERSE
556 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
557 : (option_mask32 & OPT_SELINUX
558 ? "Block size: %-10lu\n"
559 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
560 "Inodes: Total: %-10llu Free: %llu"
561 "S_context: %C\n"
562 : "Block size: %-10lu\n"
563 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
564 "Inodes: Total: %-10llu Free: %llu\n"
565 )
566 );
567 printf(format,
568 (unsigned long) statfsbuf.f_bsize,
569 (unsigned long long) statfsbuf.f_blocks,
570 (unsigned long long) statfsbuf.f_bfree,
571 (unsigned long long) statfsbuf.f_bavail,
572 (unsigned long long) statfsbuf.f_files,
573 (unsigned long long) statfsbuf.f_ffree,
574 scontext);
575
576 if (scontext)
577 freecon(scontext);
578# endif
579#endif
580 return 1;
581}
582#endif
583
584
585#if !ENABLE_FEATURE_STAT_FORMAT
586#define do_stat(filename, format) do_stat(filename)
587#endif
588static bool do_stat(const char *filename, const char *format)
589{
590 struct stat statbuf;
591#if ENABLE_SELINUX
592 security_context_t scontext = NULL;
593
594 if (option_mask32 & OPT_SELINUX) {
595 if ((option_mask32 & OPT_DEREFERENCE
596 ? lgetfilecon(filename, &scontext)
597 : getfilecon(filename, &scontext)
598 ) < 0
599 ) {
600 bb_simple_perror_msg(filename);
601 return 0;
602 }
603 }
604#endif
605 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
606 bb_perror_msg("can't stat '%s'", filename);
607 return 0;
608 }
609
610#if ENABLE_FEATURE_STAT_FORMAT
611 if (format == NULL) {
612# if !ENABLE_SELINUX
613 if (option_mask32 & OPT_TERSE) {
614 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
615 } else {
616 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
617 format =
618 " File: %N\n"
619 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
620 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
621 " Device type: %t,%T\n"
622 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
623 "Access: %x\n" "Modify: %y\n" "Change: %z";
624 } else {
625 format =
626 " File: %N\n"
627 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
628 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
629 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
630 "Access: %x\n" "Modify: %y\n" "Change: %z";
631 }
632 }
633# else
634 if (option_mask32 & OPT_TERSE) {
635 format = (option_mask32 & OPT_SELINUX ?
636 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
637 :
638 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
639 );
640 } else {
641 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
642 format = (option_mask32 & OPT_SELINUX ?
643 " File: %N\n"
644 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
645 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
646 " Device type: %t,%T\n"
647 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
648 " S_Context: %C\n"
649 "Access: %x\n" "Modify: %y\n" "Change: %z"
650 :
651 " File: %N\n"
652 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
653 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
654 " Device type: %t,%T\n"
655 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
656 "Access: %x\n" "Modify: %y\n" "Change: %z"
657 );
658 } else {
659 format = (option_mask32 & OPT_SELINUX ?
660 " File: %N\n"
661 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
662 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
663 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
664 "S_Context: %C\n"
665 "Access: %x\n" "Modify: %y\n" "Change: %z"
666 :
667 " File: %N\n"
668 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
669 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
670 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
671 "Access: %x\n" "Modify: %y\n" "Change: %z"
672 );
673 }
674 }
675# endif
676 }
677 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
678#else
679 if (option_mask32 & OPT_TERSE) {
680 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
681 IF_NOT_SELINUX("\n"),
682 filename,
683 (unsigned long long) statbuf.st_size,
684 (unsigned long long) statbuf.st_blocks,
685 (unsigned long) statbuf.st_mode,
686 (unsigned long) statbuf.st_uid,
687 (unsigned long) statbuf.st_gid,
688 (unsigned long long) statbuf.st_dev,
689 (unsigned long long) statbuf.st_ino,
690 (unsigned long) statbuf.st_nlink,
691 (unsigned long) major(statbuf.st_rdev),
692 (unsigned long) minor(statbuf.st_rdev),
693 (unsigned long) statbuf.st_atime,
694 (unsigned long) statbuf.st_mtime,
695 (unsigned long) statbuf.st_ctime,
696 (unsigned long) statbuf.st_blksize
697 );
698# if ENABLE_SELINUX
699 if (option_mask32 & OPT_SELINUX)
700 printf(" %s\n", scontext);
701 else
702 bb_putchar('\n');
703# endif
704 } else {
705 char *linkname = NULL;
706 struct passwd *pw_ent;
707 struct group *gw_ent;
708
709 gw_ent = getgrgid(statbuf.st_gid);
710 pw_ent = getpwuid(statbuf.st_uid);
711
712 if (S_ISLNK(statbuf.st_mode))
713 linkname = xmalloc_readlink_or_warn(filename);
714 if (linkname) {
715 printf(" File: '%s' -> '%s'\n", filename, linkname);
716 free(linkname);
717 } else {
718 printf(" File: '%s'\n", filename);
719 }
720
721 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
722 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
723 (unsigned long long) statbuf.st_size,
724 (unsigned long long) statbuf.st_blocks,
725 (unsigned long) statbuf.st_blksize,
726 file_type(&statbuf),
727 (unsigned long long) statbuf.st_dev,
728 (unsigned long long) statbuf.st_dev,
729 (unsigned long long) statbuf.st_ino,
730 (unsigned long) statbuf.st_nlink);
731 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
732 printf(" Device type: %lx,%lx\n",
733 (unsigned long) major(statbuf.st_rdev),
734 (unsigned long) minor(statbuf.st_rdev));
735 else
736 bb_putchar('\n');
737 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
738 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
739 bb_mode_string(statbuf.st_mode),
740 (unsigned long) statbuf.st_uid,
741 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
742 (unsigned long) statbuf.st_gid,
743 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
744# if ENABLE_SELINUX
745 if (option_mask32 & OPT_SELINUX)
746 printf(" S_Context: %s\n", scontext);
747# endif
748 printf("Access: %s\n", human_time(&statbuf.st_atim));
749 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
750 printf("Change: %s\n", human_time(&statbuf.st_ctim));
751 }
752#endif
753 return 1;
754}
755
756int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
757int stat_main(int argc UNUSED_PARAM, char **argv)
758{
759 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
760 int i;
761 int ok;
762 statfunc_ptr statfunc = do_stat;
763#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
764 unsigned opts;
765
766 opts =
767#endif
768 getopt32(argv, "^"
769 "tL"
770 IF_FEATURE_STAT_FILESYSTEM("f")
771 IF_SELINUX("Z")
772 IF_FEATURE_STAT_FORMAT("c:")
773 "\0" "-1"
774 IF_FEATURE_STAT_FORMAT(,&format)
775 );
776#if ENABLE_FEATURE_STAT_FILESYSTEM
777 if (opts & OPT_FILESYS)
778 statfunc = do_statfs;
779#endif
780#if ENABLE_SELINUX
781 if (opts & OPT_SELINUX) {
782 selinux_or_die();
783 }
784#endif
785 ok = 1;
786 argv += optind;
787 for (i = 0; argv[i]; ++i)
788 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
789
790 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
791}
792