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 char modestr[12];
343 printfs(pformat, bb_mode_string(modestr, statbuf->st_mode));
344 } else if (m == 'f') {
345 strcat(pformat, "lx");
346 printf(pformat, (unsigned long) statbuf->st_mode);
347 } else if (m == 'F') {
348 printfs(pformat, file_type(statbuf));
349 } else if (m == 'h') {
350 strcat(pformat, "lu");
351 printf(pformat, (unsigned long) statbuf->st_nlink);
352 } else if (m == 'u') {
353 strcat(pformat, "lu");
354 printf(pformat, (unsigned long) statbuf->st_uid);
355 } else if (m == 'U') {
356 pw_ent = getpwuid(statbuf->st_uid);
357 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
358 } else if (m == 'g') {
359 strcat(pformat, "lu");
360 printf(pformat, (unsigned long) statbuf->st_gid);
361 } else if (m == 'G') {
362 gw_ent = getgrgid(statbuf->st_gid);
363 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
364 } else if (m == 't') {
365 strcat(pformat, "lx");
366 printf(pformat, (unsigned long) major(statbuf->st_rdev));
367 } else if (m == 'T') {
368 strcat(pformat, "lx");
369 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
370 } else if (m == 's') {
371 strcat(pformat, "llu");
372 printf(pformat, (unsigned long long) statbuf->st_size);
373 } else if (m == 'B') {
374 strcat(pformat, "lu");
375 printf(pformat, (unsigned long) 512);
376 } else if (m == 'b') {
377 strcat(pformat, "llu");
378 printf(pformat, (unsigned long long) statbuf->st_blocks);
379 } else if (m == 'o') {
380 strcat(pformat, "lu");
381 printf(pformat, (unsigned long) statbuf->st_blksize);
382 } else if (m == 'x') {
383 printfs(pformat, human_time(&statbuf->st_atim));
384 } else if (m == 'X') {
385 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
386
387
388 printf(pformat, (long) statbuf->st_atime);
389 } else if (m == 'y') {
390 printfs(pformat, human_time(&statbuf->st_mtim));
391 } else if (m == 'Y') {
392 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
393 printf(pformat, (long) statbuf->st_mtime);
394 } else if (m == 'z') {
395 printfs(pformat, human_time(&statbuf->st_ctim));
396 } else if (m == 'Z') {
397 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
398 printf(pformat, (long) statbuf->st_ctime);
399# if ENABLE_SELINUX
400 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
401 printfs(pformat, scontext);
402# endif
403 } else {
404 strcatc(pformat, 'c');
405 printf(pformat, m);
406 }
407}
408
409static void print_it(const char *masterformat,
410 const char *filename,
411 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
412 const void *data
413 IF_SELINUX(, security_context_t scontext))
414{
415
416 char *format = xstrdup(masterformat);
417
418
419 char *dest = xmalloc(strlen(format) + 2 + 1);
420 char *b;
421
422 b = format;
423 while (b) {
424
425
426
427 size_t len;
428 char *p = strchr(b, '%');
429 if (!p) {
430
431
432 puts(b);
433 break;
434 }
435
436
437 len = 1 + strspn(p + 1, "#-+.I 0123456789");
438 memcpy(dest, p, len);
439 dest[len] = '\0';
440
441
442 *p = '\0';
443 fputs_stdout(b);
444
445 p += len;
446 b = p + 1;
447 switch (*p) {
448 case '\0':
449 b = NULL;
450
451 case '%':
452 bb_putchar('%');
453 break;
454 default:
455
456 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
457 break;
458 }
459 }
460
461 free(format);
462 free(dest);
463}
464#endif
465
466#if ENABLE_FEATURE_STAT_FILESYSTEM
467
468#if !ENABLE_FEATURE_STAT_FORMAT
469#define do_statfs(filename, format) do_statfs(filename)
470#endif
471static bool do_statfs(const char *filename, const char *format)
472{
473 struct statfs statfsbuf;
474#if !ENABLE_FEATURE_STAT_FORMAT
475 const char *format;
476#endif
477#if ENABLE_SELINUX
478 security_context_t scontext = NULL;
479
480 if (option_mask32 & OPT_SELINUX) {
481 if ((option_mask32 & OPT_DEREFERENCE
482 ? lgetfilecon(filename, &scontext)
483 : getfilecon(filename, &scontext)
484 ) < 0
485 ) {
486 bb_simple_perror_msg(filename);
487 return 0;
488 }
489 }
490#endif
491 if (statfs(filename, &statfsbuf) != 0) {
492 bb_perror_msg("can't read file system information for '%s'", filename);
493 return 0;
494 }
495
496#if ENABLE_FEATURE_STAT_FORMAT
497 if (format == NULL) {
498# if !ENABLE_SELINUX
499 format = (option_mask32 & OPT_TERSE
500 ? "%n %i %l %t %s %b %f %a %c %d"
501 : " File: \"%n\"\n"
502 " ID: %-8i Namelen: %-7l Type: %T\n"
503 "Block size: %-10s\n"
504 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
505 "Inodes: Total: %-10c Free: %d");
506# else
507 format = (option_mask32 & OPT_TERSE
508 ? (option_mask32 & OPT_SELINUX
509 ? "%n %i %l %t %s %b %f %a %c %d %C"
510 : "%n %i %l %t %s %b %f %a %c %d")
511 : (option_mask32 & OPT_SELINUX
512 ? " File: \"%n\"\n"
513 " ID: %-8i Namelen: %-7l Type: %T\n"
514 "Block size: %-10s\n"
515 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
516 "Inodes: Total: %-10c Free: %d"
517 " S_context: %C"
518 : " File: \"%n\"\n"
519 " ID: %-8i Namelen: %-7l Type: %T\n"
520 "Block size: %-10s\n"
521 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
522 "Inodes: Total: %-10c Free: %d")
523 );
524# endif
525 }
526 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
527#else
528 format = (option_mask32 & OPT_TERSE
529 ? "%s %llx %lu "
530 : " File: \"%s\"\n"
531 " ID: %-8llx Namelen: %-7lu ");
532 printf(format,
533 filename,
534 get_f_fsid(&statfsbuf),
535 statfsbuf.f_namelen);
536
537 if (option_mask32 & OPT_TERSE)
538 printf("%lx ", (unsigned long) statfsbuf.f_type);
539 else
540 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
541
542# if !ENABLE_SELINUX
543 format = (option_mask32 & OPT_TERSE
544 ? "%lu %llu %llu %llu %llu %llu\n"
545 : "Block size: %-10lu\n"
546 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
547 "Inodes: Total: %-10llu Free: %llu\n");
548 printf(format,
549 (unsigned long) statfsbuf.f_bsize,
550 (unsigned long long) statfsbuf.f_blocks,
551 (unsigned long long) statfsbuf.f_bfree,
552 (unsigned long long) statfsbuf.f_bavail,
553 (unsigned long long) statfsbuf.f_files,
554 (unsigned long long) statfsbuf.f_ffree);
555# else
556 format = (option_mask32 & OPT_TERSE
557 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
558 : (option_mask32 & OPT_SELINUX
559 ? "Block size: %-10lu\n"
560 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
561 "Inodes: Total: %-10llu Free: %llu"
562 "S_context: %C\n"
563 : "Block size: %-10lu\n"
564 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
565 "Inodes: Total: %-10llu Free: %llu\n"
566 )
567 );
568 printf(format,
569 (unsigned long) statfsbuf.f_bsize,
570 (unsigned long long) statfsbuf.f_blocks,
571 (unsigned long long) statfsbuf.f_bfree,
572 (unsigned long long) statfsbuf.f_bavail,
573 (unsigned long long) statfsbuf.f_files,
574 (unsigned long long) statfsbuf.f_ffree,
575 scontext);
576
577 if (scontext)
578 freecon(scontext);
579# endif
580#endif
581 return 1;
582}
583#endif
584
585
586#if !ENABLE_FEATURE_STAT_FORMAT
587#define do_stat(filename, format) do_stat(filename)
588#endif
589static bool do_stat(const char *filename, const char *format)
590{
591 struct stat statbuf;
592#if ENABLE_SELINUX
593 security_context_t scontext = NULL;
594
595 if (option_mask32 & OPT_SELINUX) {
596 if ((option_mask32 & OPT_DEREFERENCE
597 ? lgetfilecon(filename, &scontext)
598 : getfilecon(filename, &scontext)
599 ) < 0
600 ) {
601 bb_simple_perror_msg(filename);
602 return 0;
603 }
604 }
605#endif
606 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
607 bb_perror_msg("can't stat '%s'", filename);
608 return 0;
609 }
610
611#if ENABLE_FEATURE_STAT_FORMAT
612 if (format == NULL) {
613# if !ENABLE_SELINUX
614 if (option_mask32 & OPT_TERSE) {
615 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
616 } else {
617 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
618 format =
619 " File: %N\n"
620 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
621 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
622 " Device type: %t,%T\n"
623 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
624 "Access: %x\n" "Modify: %y\n" "Change: %z";
625 } else {
626 format =
627 " File: %N\n"
628 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
629 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
630 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
631 "Access: %x\n" "Modify: %y\n" "Change: %z";
632 }
633 }
634# else
635 if (option_mask32 & OPT_TERSE) {
636 format = (option_mask32 & OPT_SELINUX ?
637 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
638 :
639 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
640 );
641 } else {
642 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
643 format = (option_mask32 & OPT_SELINUX ?
644 " File: %N\n"
645 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
646 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
647 " Device type: %t,%T\n"
648 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
649 " S_Context: %C\n"
650 "Access: %x\n" "Modify: %y\n" "Change: %z"
651 :
652 " File: %N\n"
653 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
654 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
655 " Device type: %t,%T\n"
656 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
657 "Access: %x\n" "Modify: %y\n" "Change: %z"
658 );
659 } else {
660 format = (option_mask32 & OPT_SELINUX ?
661 " File: %N\n"
662 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
663 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
664 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
665 "S_Context: %C\n"
666 "Access: %x\n" "Modify: %y\n" "Change: %z"
667 :
668 " File: %N\n"
669 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
670 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
671 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
672 "Access: %x\n" "Modify: %y\n" "Change: %z"
673 );
674 }
675 }
676# endif
677 }
678 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
679#else
680 if (option_mask32 & OPT_TERSE) {
681 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
682 IF_NOT_SELINUX("\n"),
683 filename,
684 (unsigned long long) statbuf.st_size,
685 (unsigned long long) statbuf.st_blocks,
686 (unsigned long) statbuf.st_mode,
687 (unsigned long) statbuf.st_uid,
688 (unsigned long) statbuf.st_gid,
689 (unsigned long long) statbuf.st_dev,
690 (unsigned long long) statbuf.st_ino,
691 (unsigned long) statbuf.st_nlink,
692 (unsigned long) major(statbuf.st_rdev),
693 (unsigned long) minor(statbuf.st_rdev),
694 (unsigned long) statbuf.st_atime,
695 (unsigned long) statbuf.st_mtime,
696 (unsigned long) statbuf.st_ctime,
697 (unsigned long) statbuf.st_blksize
698 );
699# if ENABLE_SELINUX
700 if (option_mask32 & OPT_SELINUX)
701 printf(" %s\n", scontext);
702 else
703 bb_putchar('\n');
704# endif
705 } else {
706 char modestr[12];
707 char *linkname = NULL;
708 struct passwd *pw_ent;
709 struct group *gw_ent;
710
711 gw_ent = getgrgid(statbuf.st_gid);
712 pw_ent = getpwuid(statbuf.st_uid);
713
714 if (S_ISLNK(statbuf.st_mode))
715 linkname = xmalloc_readlink_or_warn(filename);
716 if (linkname) {
717 printf(" File: '%s' -> '%s'\n", filename, linkname);
718 free(linkname);
719 } else {
720 printf(" File: '%s'\n", filename);
721 }
722
723 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
724 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
725 (unsigned long long) statbuf.st_size,
726 (unsigned long long) statbuf.st_blocks,
727 (unsigned long) statbuf.st_blksize,
728 file_type(&statbuf),
729 (unsigned long long) statbuf.st_dev,
730 (unsigned long long) statbuf.st_dev,
731 (unsigned long long) statbuf.st_ino,
732 (unsigned long) statbuf.st_nlink);
733 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
734 printf(" Device type: %lx,%lx\n",
735 (unsigned long) major(statbuf.st_rdev),
736 (unsigned long) minor(statbuf.st_rdev));
737 else
738 bb_putchar('\n');
739 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
740 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
741 bb_mode_string(modestr, statbuf.st_mode),
742 (unsigned long) statbuf.st_uid,
743 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
744 (unsigned long) statbuf.st_gid,
745 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
746# if ENABLE_SELINUX
747 if (option_mask32 & OPT_SELINUX)
748 printf(" S_Context: %s\n", scontext);
749# endif
750 printf("Access: %s\n", human_time(&statbuf.st_atim));
751 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
752 printf("Change: %s\n", human_time(&statbuf.st_ctim));
753 }
754#endif
755 return 1;
756}
757
758int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
759int stat_main(int argc UNUSED_PARAM, char **argv)
760{
761 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
762 int i;
763 int ok;
764 statfunc_ptr statfunc = do_stat;
765#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
766 unsigned opts;
767
768 opts =
769#endif
770 getopt32(argv, "^"
771 "tL"
772 IF_FEATURE_STAT_FILESYSTEM("f")
773 IF_SELINUX("Z")
774 IF_FEATURE_STAT_FORMAT("c:")
775 "\0" "-1"
776 IF_FEATURE_STAT_FORMAT(,&format)
777 );
778#if ENABLE_FEATURE_STAT_FILESYSTEM
779 if (opts & OPT_FILESYS)
780 statfunc = do_statfs;
781#endif
782#if ENABLE_SELINUX
783 if (opts & OPT_SELINUX) {
784 selinux_or_die();
785 }
786#endif
787 ok = 1;
788 argv += optind;
789 for (i = 0; argv[i]; ++i)
790 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
791
792 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
793}
794