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