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