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