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