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