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