gzip: fix a case where tar xzf fails (we use uninitialized fd)
[oweals/busybox.git] / coreutils / stat.c
1 /* vi: set sw=4 ts=4: */
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>
8  * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
9  *
10  * Written by Michael Meskes
11  * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12  *
13  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14  */
15
16 #include "libbb.h"
17
18 /* vars to control behavior */
19 #define OPT_FILESYS     (1 << 0)
20 #define OPT_TERSE       (1 << 1)
21 #define OPT_DEREFERENCE (1 << 2)
22 #define OPT_SELINUX     (1 << 3)
23
24 static const char *file_type(const struct stat *st)
25 {
26         /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
27          * for some of these formats.
28          * To keep diagnostics grammatical in English, the
29          * returned string must start with a consonant.
30          */
31         if (S_ISREG(st->st_mode))  return st->st_size == 0 ? "regular empty file" : "regular file";
32         if (S_ISDIR(st->st_mode))  return "directory";
33         if (S_ISBLK(st->st_mode))  return "block special file";
34         if (S_ISCHR(st->st_mode))  return "character special file";
35         if (S_ISFIFO(st->st_mode)) return "fifo";
36         if (S_ISLNK(st->st_mode))  return "symbolic link";
37         if (S_ISSOCK(st->st_mode)) return "socket";
38         if (S_TYPEISMQ(st))        return "message queue";
39         if (S_TYPEISSEM(st))       return "semaphore";
40         if (S_TYPEISSHM(st))       return "shared memory object";
41 #ifdef S_TYPEISTMO
42         if (S_TYPEISTMO(st))       return "typed memory object";
43 #endif
44         return "weird file";
45 }
46
47 static const char *human_time(time_t t)
48 {
49         /* Old
50         static char *str;
51         str = ctime(&t);
52         str[strlen(str)-1] = '\0';
53         return str;
54         */
55         /* coreutils 6.3 compat: */
56
57         /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
58 #define buf bb_common_bufsiz1
59
60         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
61         return buf;
62 #undef buf
63 }
64
65 /* Return the type of the specified file system.
66  * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
67  * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
68  * Still others have neither and have to get by with f_type (Linux).
69  */
70 static const char *human_fstype(uint32_t f_type)
71 {
72         static const struct types {
73                 uint32_t type;
74                 const char *const fs;
75         } humantypes[] = {
76                 { 0xADFF,     "affs" },
77                 { 0x1Cd1,     "devpts" },
78                 { 0x137D,     "ext" },
79                 { 0xEF51,     "ext2" },
80                 { 0xEF53,     "ext2/ext3" },
81                 { 0x3153464a, "jfs" },
82                 { 0x58465342, "xfs" },
83                 { 0xF995E849, "hpfs" },
84                 { 0x9660,     "isofs" },
85                 { 0x4000,     "isofs" },
86                 { 0x4004,     "isofs" },
87                 { 0x137F,     "minix" },
88                 { 0x138F,     "minix (30 char.)" },
89                 { 0x2468,     "minix v2" },
90                 { 0x2478,     "minix v2 (30 char.)" },
91                 { 0x4d44,     "msdos" },
92                 { 0x4006,     "fat" },
93                 { 0x564c,     "novell" },
94                 { 0x6969,     "nfs" },
95                 { 0x9fa0,     "proc" },
96                 { 0x517B,     "smb" },
97                 { 0x012FF7B4, "xenix" },
98                 { 0x012FF7B5, "sysv4" },
99                 { 0x012FF7B6, "sysv2" },
100                 { 0x012FF7B7, "coh" },
101                 { 0x00011954, "ufs" },
102                 { 0x012FD16D, "xia" },
103                 { 0x5346544e, "ntfs" },
104                 { 0x1021994,  "tmpfs" },
105                 { 0x52654973, "reiserfs" },
106                 { 0x28cd3d45, "cramfs" },
107                 { 0x7275,     "romfs" },
108                 { 0x858458f6, "romfs" },
109                 { 0x73717368, "squashfs" },
110                 { 0x62656572, "sysfs" },
111                 { 0, "UNKNOWN" }
112         };
113
114         int i;
115
116         for (i = 0; humantypes[i].type; ++i)
117                 if (humantypes[i].type == f_type)
118                         break;
119         return humantypes[i].fs;
120 }
121
122 #if ENABLE_FEATURE_STAT_FORMAT
123 static void strcatc(char *str, char c)
124 {
125         int len = strlen(str);
126         str[len++] = c;
127         str[len] = '\0';
128 }
129
130 static void printfs(char *pformat, const char *msg)
131 {
132         strcatc(pformat, 's');
133         printf(pformat, msg);
134 }
135
136 /* print statfs info */
137 static void print_statfs(char *pformat, const char m,
138                 const char *const filename, const void *data
139                 USE_SELINUX(, security_context_t scontext))
140 {
141         const struct statfs *statfsbuf = data;
142         if (m == 'n') {
143                 printfs(pformat, filename);
144         } else if (m == 'i') {
145                 strcat(pformat, "Lx");
146                 printf(pformat, statfsbuf->f_fsid);
147         } else if (m == 'l') {
148                 strcat(pformat, "lu");
149                 printf(pformat, statfsbuf->f_namelen);
150         } else if (m == 't') {
151                 strcat(pformat, "lx");
152                 printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
153         } else if (m == 'T') {
154                 printfs(pformat, human_fstype(statfsbuf->f_type));
155         } else if (m == 'b') {
156                 strcat(pformat, "jd");
157                 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
158         } else if (m == 'f') {
159                 strcat(pformat, "jd");
160                 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
161         } else if (m == 'a') {
162                 strcat(pformat, "jd");
163                 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
164         } else if (m == 's' || m == 'S') {
165                 strcat(pformat, "lu");
166                 printf(pformat, (unsigned long) (statfsbuf->f_bsize));
167         } else if (m == 'c') {
168                 strcat(pformat, "jd");
169                 printf(pformat, (intmax_t) (statfsbuf->f_files));
170         } else if (m == 'd') {
171                 strcat(pformat, "jd");
172                 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
173 #if ENABLE_SELINUX
174         } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
175                 printfs(pformat, scontext);
176 #endif
177         } else {
178                 strcatc(pformat, 'c');
179                 printf(pformat, m);
180         }
181 }
182
183 /* print stat info */
184 static void print_stat(char *pformat, const char m,
185                 const char *const filename, const void *data
186                 USE_SELINUX(, security_context_t scontext))
187 {
188 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
189         struct stat *statbuf = (struct stat *) data;
190         struct passwd *pw_ent;
191         struct group *gw_ent;
192
193         if (m == 'n') {
194                 printfs(pformat, filename);
195         } else if (m == 'N') {
196                 strcatc(pformat, 's');
197                 if (S_ISLNK(statbuf->st_mode)) {
198                         char *linkname = xmalloc_readlink_or_warn(filename);
199                         if (linkname == NULL)
200                                 return;
201                         /*printf("\"%s\" -> \"%s\"", filename, linkname); */
202                         printf(pformat, filename);
203                         printf(" -> ");
204                         printf(pformat, linkname);
205                         free(linkname);
206                 } else {
207                         printf(pformat, filename);
208                 }
209         } else if (m == 'd') {
210                 strcat(pformat, "ju");
211                 printf(pformat, (uintmax_t) statbuf->st_dev);
212         } else if (m == 'D') {
213                 strcat(pformat, "jx");
214                 printf(pformat, (uintmax_t) statbuf->st_dev);
215         } else if (m == 'i') {
216                 strcat(pformat, "ju");
217                 printf(pformat, (uintmax_t) statbuf->st_ino);
218         } else if (m == 'a') {
219                 strcat(pformat, "lo");
220                 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
221         } else if (m == 'A') {
222                 printfs(pformat, bb_mode_string(statbuf->st_mode));
223         } else if (m == 'f') {
224                 strcat(pformat, "lx");
225                 printf(pformat, (unsigned long) statbuf->st_mode);
226         } else if (m == 'F') {
227                 printfs(pformat, file_type(statbuf));
228         } else if (m == 'h') {
229                 strcat(pformat, "lu");
230                 printf(pformat, (unsigned long) statbuf->st_nlink);
231         } else if (m == 'u') {
232                 strcat(pformat, "lu");
233                 printf(pformat, (unsigned long) statbuf->st_uid);
234         } else if (m == 'U') {
235                 setpwent();
236                 pw_ent = getpwuid(statbuf->st_uid);
237                 printfs(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
238         } else if (m == 'g') {
239                 strcat(pformat, "lu");
240                 printf(pformat, (unsigned long) statbuf->st_gid);
241         } else if (m == 'G') {
242                 setgrent();
243                 gw_ent = getgrgid(statbuf->st_gid);
244                 printfs(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
245         } else if (m == 't') {
246                 strcat(pformat, "lx");
247                 printf(pformat, (unsigned long) major(statbuf->st_rdev));
248         } else if (m == 'T') {
249                 strcat(pformat, "lx");
250                 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
251         } else if (m == 's') {
252                 strcat(pformat, "ju");
253                 printf(pformat, (uintmax_t) (statbuf->st_size));
254         } else if (m == 'B') {
255                 strcat(pformat, "lu");
256                 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
257         } else if (m == 'b') {
258                 strcat(pformat, "ju");
259                 printf(pformat, (uintmax_t) statbuf->st_blocks);
260         } else if (m == 'o') {
261                 strcat(pformat, "lu");
262                 printf(pformat, (unsigned long) statbuf->st_blksize);
263         } else if (m == 'x') {
264                 printfs(pformat, human_time(statbuf->st_atime));
265         } else if (m == 'X') {
266                 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
267                 printf(pformat, (unsigned long) statbuf->st_atime);
268         } else if (m == 'y') {
269                 printfs(pformat, human_time(statbuf->st_mtime));
270         } else if (m == 'Y') {
271                 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
272                 printf(pformat, (unsigned long) statbuf->st_mtime);
273         } else if (m == 'z') {
274                 printfs(pformat, human_time(statbuf->st_ctime));
275         } else if (m == 'Z') {
276                 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
277                 printf(pformat, (unsigned long) statbuf->st_ctime);
278 #if ENABLE_SELINUX
279         } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
280                 printfs(pformat, scontext);
281 #endif
282         } else {
283                 strcatc(pformat, 'c');
284                 printf(pformat, m);
285         }
286 }
287
288 static void print_it(const char *masterformat, const char *filename,
289                 void (*print_func) (char*, char, const char*, const void* USE_SELINUX(, security_context_t scontext)),
290                 const void *data
291                 USE_SELINUX(, security_context_t scontext) )
292 {
293         /* Create a working copy of the format string */
294         char *format = xstrdup(masterformat);
295         /* Add 2 to accomodate our conversion of the stat '%s' format string
296          * to the printf '%llu' one.  */
297         char *dest = xmalloc(strlen(format) + 2 + 1);
298         char *b;
299
300         b = format;
301         while (b) {
302                 size_t len;
303                 char *p = strchr(b, '%');
304                 if (!p) {
305                         /* coreutils 6.3 always prints <cr> at the end */
306                         /*fputs(b, stdout);*/
307                         puts(b);
308                         break;
309                 }
310                 *p++ = '\0';
311                 fputs(b, stdout);
312
313                 /* dest = "%<modifiers>" */
314                 len = strspn(p, "#-+.I 0123456789");
315                 dest[0] = '%';
316                 memcpy(dest + 1, p, len);
317                 dest[1 + len] = '\0';
318                 p += len;
319
320                 b = p + 1;
321                 switch (*p) {
322                 case '\0':
323                         b = NULL;
324                         /* fall through */
325                 case '%':
326                         bb_putchar('%');
327                         break;
328                 default:
329                         /* Completes "%<modifiers>" with specifier and printfs */
330                         print_func(dest, *p, filename, data USE_SELINUX(,scontext));
331                         break;
332                 }
333         }
334
335         free(format);
336         free(dest);
337 }
338 #endif
339
340 /* Stat the file system and print what we find.  */
341 static bool do_statfs(const char *filename, const char *format)
342 {
343         struct statfs statfsbuf;
344 #if ENABLE_SELINUX
345         security_context_t scontext = NULL;
346
347         if (option_mask32 & OPT_SELINUX) {
348                 if ((option_mask32 & OPT_DEREFERENCE
349                      ? lgetfilecon(filename, &scontext)
350                      : getfilecon(filename, &scontext)
351                     ) < 0
352                 ) {
353                         bb_perror_msg(filename);
354                         return 0;
355                 }
356         }
357 #endif
358         if (statfs(filename, &statfsbuf) != 0) {
359                 bb_perror_msg("cannot read file system information for '%s'", filename);
360                 return 0;
361         }
362
363 #if ENABLE_FEATURE_STAT_FORMAT
364         if (format == NULL) {
365 #if !ENABLE_SELINUX
366                 format = (option_mask32 & OPT_TERSE
367                         ? "%n %i %l %t %s %b %f %a %c %d\n"
368                         : "  File: \"%n\"\n"
369                           "    ID: %-8i Namelen: %-7l Type: %T\n"
370                           "Block size: %-10s\n"
371                           "Blocks: Total: %-10b Free: %-10f Available: %a\n"
372                           "Inodes: Total: %-10c Free: %d");
373 #else
374                 format = (option_mask32 & OPT_TERSE
375                         ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
376                         "%n %i %l %t %s %b %f %a %c %d\n")
377                         : (option_mask32 & OPT_SELINUX ?
378                         "  File: \"%n\"\n"
379                         "    ID: %-8i Namelen: %-7l Type: %T\n"
380                         "Block size: %-10s\n"
381                         "Blocks: Total: %-10b Free: %-10f Available: %a\n"
382                         "Inodes: Total: %-10c Free: %d"
383                         "  S_context: %C\n":
384                         "  File: \"%n\"\n"
385                         "    ID: %-8i Namelen: %-7l Type: %T\n"
386                         "Block size: %-10s\n"
387                         "Blocks: Total: %-10b Free: %-10f Available: %a\n"
388                         "Inodes: Total: %-10c Free: %d\n")
389                         );
390 #endif /* SELINUX */
391         }
392         print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
393 #else /* FEATURE_STAT_FORMAT */
394         format = (option_mask32 & OPT_TERSE
395                 ? "%s %llx %lu "
396                 : "  File: \"%s\"\n"
397                   "    ID: %-8Lx Namelen: %-7lu ");
398         printf(format,
399                filename,
400                statfsbuf.f_fsid,
401                statfsbuf.f_namelen);
402
403         if (option_mask32 & OPT_TERSE)
404                 printf("%lx ", (unsigned long) (statfsbuf.f_type));
405         else
406                 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
407
408 #if !ENABLE_SELINUX
409         format = (option_mask32 & OPT_TERSE
410                 ? "%lu %ld %ld %ld %ld %ld\n"
411                 : "Block size: %-10lu\n"
412                   "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
413                   "Inodes: Total: %-10jd Free: %jd\n");
414         printf(format,
415                (unsigned long) (statfsbuf.f_bsize),
416                (intmax_t) (statfsbuf.f_blocks),
417                (intmax_t) (statfsbuf.f_bfree),
418                (intmax_t) (statfsbuf.f_bavail),
419                (intmax_t) (statfsbuf.f_files),
420                (intmax_t) (statfsbuf.f_ffree));
421 #else
422         format = (option_mask32 & OPT_TERSE
423                 ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
424                 "%lu %ld %ld %ld %ld %ld\n")
425                 : (option_mask32 & OPT_SELINUX ?
426                 "Block size: %-10lu\n"
427                 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
428                 "Inodes: Total: %-10jd Free: %jd"
429                 "S_context: %C\n":
430                 "Block size: %-10lu\n"
431                 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
432                 "Inodes: Total: %-10jd Free: %jd\n"));
433         printf(format,
434                (unsigned long) (statfsbuf.f_bsize),
435                (intmax_t) (statfsbuf.f_blocks),
436                (intmax_t) (statfsbuf.f_bfree),
437                (intmax_t) (statfsbuf.f_bavail),
438                (intmax_t) (statfsbuf.f_files),
439                (intmax_t) (statfsbuf.f_ffree),
440                 scontext);
441
442         if (scontext)
443                 freecon(scontext);
444 #endif
445 #endif  /* FEATURE_STAT_FORMAT */
446         return 1;
447 }
448
449 /* stat the file and print what we find */
450 static bool do_stat(const char *filename, const char *format)
451 {
452         struct stat statbuf;
453 #if ENABLE_SELINUX
454         security_context_t scontext = NULL;
455
456         if (option_mask32 & OPT_SELINUX) {
457                 if ((option_mask32 & OPT_DEREFERENCE
458                      ? lgetfilecon(filename, &scontext)
459                      : getfilecon(filename, &scontext)
460                     ) < 0
461                 ) {
462                         bb_perror_msg(filename);
463                         return 0;
464                 }
465         }
466 #endif
467         if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
468                 bb_perror_msg("cannot stat '%s'", filename);
469                 return 0;
470         }
471
472 #if ENABLE_FEATURE_STAT_FORMAT
473         if (format == NULL) {
474 #if !ENABLE_SELINUX
475                 if (option_mask32 & OPT_TERSE) {
476                         format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
477                 } else {
478                         if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
479                                 format =
480                                         "  File: \"%N\"\n"
481                                         "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
482                                         "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
483                                         " Device type: %t,%T\n"
484                                         "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
485                                         "Access: %x\n" "Modify: %y\n" "Change: %z\n";
486                         } else {
487                                 format =
488                                         "  File: \"%N\"\n"
489                                         "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
490                                         "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
491                                         "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
492                                         "Access: %x\n" "Modify: %y\n" "Change: %z\n";
493                         }
494                 }
495 #else
496                 if (option_mask32 & OPT_TERSE) {
497                         format = (option_mask32 & OPT_SELINUX ?
498                                   "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
499                                   "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
500                 } else {
501                         if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
502                                 format = (option_mask32 & OPT_SELINUX ?
503                                           "  File: \"%N\"\n"
504                                           "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
505                                           "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
506                                           " Device type: %t,%T\n"
507                                           "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
508                                           "   S_Context: %C\n"
509                                           "Access: %x\n" "Modify: %y\n" "Change: %z\n":
510                                           "  File: \"%N\"\n"
511                                           "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
512                                           "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
513                                           " Device type: %t,%T\n"
514                                           "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
515                                           "Access: %x\n" "Modify: %y\n" "Change: %z\n");
516                         } else {
517                                 format = (option_mask32 & OPT_SELINUX ?
518                                           "  File: \"%N\"\n"
519                                           "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
520                                           "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
521                                           "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
522                                           "S_Context: %C\n"
523                                           "Access: %x\n" "Modify: %y\n" "Change: %z\n":
524                                           "  File: \"%N\"\n"
525                                           "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
526                                           "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
527                                           "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
528                                           "Access: %x\n" "Modify: %y\n" "Change: %z\n");
529                         }
530                 }
531 #endif
532         }
533         print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
534 #else   /* FEATURE_STAT_FORMAT */
535         if (option_mask32 & OPT_TERSE) {
536                 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
537                        SKIP_SELINUX("\n"),
538                        filename,
539                        (uintmax_t) (statbuf.st_size),
540                        (uintmax_t) statbuf.st_blocks,
541                        (unsigned long) statbuf.st_mode,
542                        (unsigned long) statbuf.st_uid,
543                        (unsigned long) statbuf.st_gid,
544                        (uintmax_t) statbuf.st_dev,
545                        (uintmax_t) statbuf.st_ino,
546                        (unsigned long) statbuf.st_nlink,
547                        (unsigned long) major(statbuf.st_rdev),
548                        (unsigned long) minor(statbuf.st_rdev),
549                        (unsigned long) statbuf.st_atime,
550                        (unsigned long) statbuf.st_mtime,
551                        (unsigned long) statbuf.st_ctime,
552                        (unsigned long) statbuf.st_blksize
553                 );
554 #if ENABLE_SELINUX
555                 if (option_mask32 & OPT_SELINUX)
556                         printf(" %lc\n", *scontext);
557                 else
558                         bb_putchar('\n');
559 #endif
560         } else {
561                 char *linkname = NULL;
562
563                 struct passwd *pw_ent;
564                 struct group *gw_ent;
565                 setgrent();
566                 gw_ent = getgrgid(statbuf.st_gid);
567                 setpwent();
568                 pw_ent = getpwuid(statbuf.st_uid);
569
570                 if (S_ISLNK(statbuf.st_mode))
571                         linkname = xmalloc_readlink_or_warn(filename);
572                 if (linkname)
573                         printf("  File: \"%s\" -> \"%s\"\n", filename, linkname);
574                 else
575                         printf("  File: \"%s\"\n", filename);
576
577                 printf("  Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
578                        "Device: %jxh/%jud\tInode: %-10ju  Links: %-5lu",
579                        (uintmax_t) (statbuf.st_size),
580                        (uintmax_t) statbuf.st_blocks,
581                        (unsigned long) statbuf.st_blksize,
582                        file_type(&statbuf),
583                        (uintmax_t) statbuf.st_dev,
584                        (uintmax_t) statbuf.st_dev,
585                        (uintmax_t) statbuf.st_ino,
586                        (unsigned long) statbuf.st_nlink);
587                 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
588                         printf(" Device type: %lx,%lx\n",
589                                (unsigned long) major(statbuf.st_rdev),
590                                (unsigned long) minor(statbuf.st_rdev));
591                 else
592                         bb_putchar('\n');
593                 printf("Access: (%04lo/%10.10s)  Uid: (%5lu/%8s)   Gid: (%5lu/%8s)\n",
594                        (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
595                        bb_mode_string(statbuf.st_mode),
596                        (unsigned long) statbuf.st_uid,
597                        (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
598                        (unsigned long) statbuf.st_gid,
599                        (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
600 #if ENABLE_SELINUX
601                 printf("   S_Context: %lc\n", *scontext);
602 #endif
603                 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
604                        human_time(statbuf.st_atime),
605                        human_time(statbuf.st_mtime),
606                        human_time(statbuf.st_ctime));
607         }
608 #endif  /* FEATURE_STAT_FORMAT */
609         return 1;
610 }
611
612 int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
613 int stat_main(int argc, char **argv)
614 {
615         char *format = NULL;
616         int i;
617         int ok = 1;
618         bool (*statfunc)(const char *, const char *) = do_stat;
619
620         getopt32(argv, "ftL"
621                 USE_SELINUX("Z")
622                 USE_FEATURE_STAT_FORMAT("c:", &format)
623         );
624
625         if (option_mask32 & OPT_FILESYS) /* -f */
626                 statfunc = do_statfs;
627         if (argc == optind)           /* files */
628                 bb_show_usage();
629
630 #if ENABLE_SELINUX
631         if (option_mask32 & OPT_SELINUX) {
632                 selinux_or_die();
633         }
634 #endif  /* ENABLE_SELINUX */
635         for (i = optind; i < argc; ++i)
636                 ok &= statfunc(argv[i], format);
637
638         return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
639 }