• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* stat.c : display file or file system status
2  * Copyright 2012 <warior.linux@gmail.com>
3  * Copyright 2013 <anand.sinha85@gmail.com>
4 
5 USE_STAT(NEWTOY(stat, "<1c:(format)fLt", TOYFLAG_BIN))
6 
7 config STAT
8   bool stat
9   default y
10   help
11     usage: stat [-tfL] [-c FORMAT] FILE...
12 
13     Display status of files or filesystems.
14 
15     -c	Output specified FORMAT string instead of default
16     -f	Display filesystem status instead of file status
17     -L	Follow symlinks
18     -t	terse (-c "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o")
19     	      (with -f = -c "%n %i %l %t %s %S %b %f %a %c %d")
20 
21     The valid format escape sequences for files:
22     %a  Access bits (octal) |%A  Access bits (flags)|%b  Size/512
23     %B  Bytes per %b (512)  |%d  Device ID (dec)    |%D  Device ID (hex)
24     %f  All mode bits (hex) |%F  File type          |%g  Group ID
25     %G  Group name          |%h  Hard links         |%i  Inode
26     %m  Mount point         |%n  Filename           |%N  Long filename
27     %o  I/O block size      |%s  Size (bytes)       |%t  Devtype major (hex)
28     %T  Devtype minor (hex) |%u  User ID            |%U  User name
29     %x  Access time         |%X  Access unix time   |%y  Modification time
30     %Y  Mod unix time       |%z  Creation time      |%Z  Creation unix time
31 
32     The valid format escape sequences for filesystems:
33     %a  Available blocks    |%b  Total blocks       |%c  Total inodes
34     %d  Free inodes         |%f  Free blocks        |%i  File system ID
35     %l  Max filename length |%n  File name          |%s  Fragment size
36     %S  Best transfer size  |%t  FS type (hex)      |%T  FS type (driver name)
37 */
38 
39 #define FOR_stat
40 #include "toys.h"
41 
42 GLOBALS(
43   char *c;
44 
45   union {
46     struct stat st;
47     struct statfs sf;
48   } stat;
49   char *file, *pattern;
50   int patlen;
51 )
52 
53 // Force numeric output to long long instead of manually typecasting everything
54 // and safely parse length prefix
out(char c,long long val)55 static void out(char c, long long val)
56 {
57   sprintf(toybuf, "%.*sll%c", TT.patlen, TT.pattern, c);
58   printf(toybuf, val);
59 }
60 
61 // Output string with parsed length prefix
strout(char * val)62 static void strout(char *val)
63 {
64   sprintf(toybuf, "%.*ss", TT.patlen, TT.pattern);
65   printf(toybuf, val);
66 }
67 
date_stat_format(struct timespec * ts)68 static void date_stat_format(struct timespec *ts)
69 {
70   strout(format_iso_time(toybuf+128, sizeof(toybuf)-128, ts));
71 }
72 
print_stat(char type)73 static void print_stat(char type)
74 {
75   struct stat *stat = (struct stat *)&TT.stat;
76 
77   if (type == 'a') out('o', stat->st_mode&~S_IFMT);
78   else if (type == 'A') {
79     char str[11];
80 
81     mode_to_string(stat->st_mode, str);
82     strout(str);
83   } else if (type == 'b') out('u', stat->st_blocks);
84   else if (type == 'B') out('d', 512);
85   else if (type == 'd') out('d', stat->st_dev);
86   else if (type == 'D') out('x', stat->st_dev);
87   else if (type == 'f') out('x', stat->st_mode);
88   else if (type == 'F') {
89     char *t = "character device\0directory\0block device\0" \
90               "regular file\0symbolic link\0socket\0FIFO (named pipe)";
91     int i, filetype = stat->st_mode & S_IFMT;
92 
93     for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
94     if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
95     strout(t);
96   } else if (type == 'g') out('u', stat->st_gid);
97   else if (type == 'G') strout(getgroupname(stat->st_gid));
98   else if (type == 'h') out('u', stat->st_nlink);
99   else if (type == 'i') out('u', stat->st_ino);
100   else if (type == 'm') {
101     struct mtab_list *mt = xgetmountlist(0);
102     dev_t dev = stat->st_rdev ? stat->st_rdev : stat->st_dev;
103 
104     // This mount point could exist multiple times, so show oldest.
105     for (dlist_terminate(mt); mt; mt = mt->next) if (mt->stat.st_dev == dev) {
106       strout(mt->dir);
107       break;
108     }
109     llist_traverse(mt, free);
110   } else if (type == 'N') {
111     xprintf("%s", TT.file);
112     if (S_ISLNK(stat->st_mode))
113       if (readlink0(TT.file, toybuf, sizeof(toybuf)))
114         xprintf(" -> `%s'", toybuf);
115   } else if (type == 'o') out('u', stat->st_blksize);
116   else if (type == 's') out('u', stat->st_size);
117   else if (type == 't') out('x', dev_major(stat->st_rdev));
118   else if (type == 'T') out('x', dev_minor(stat->st_rdev));
119   else if (type == 'u') out('u', stat->st_uid);
120   else if (type == 'U') strout(getusername(stat->st_uid));
121   else if (type == 'x') date_stat_format(&stat->st_atim);
122   else if (type == 'X') out('u', stat->st_atime);
123   else if (type == 'y') date_stat_format(&stat->st_mtim);
124   else if (type == 'Y') out('u', stat->st_mtime);
125   else if (type == 'z') date_stat_format(&stat->st_ctim);
126   else if (type == 'Z') out('u', stat->st_ctime);
127   else xprintf("?");
128 }
129 
print_statfs(char type)130 static void print_statfs(char type) {
131   struct statfs *statfs = (struct statfs *)&TT.stat;
132 
133   if (type == 'a') out('u', statfs->f_bavail);
134   else if (type == 'b') out('u', statfs->f_blocks);
135   else if (type == 'c') out('u', statfs->f_files);
136   else if (type == 'd') out('u', statfs->f_ffree);
137   else if (type == 'f') out('u', statfs->f_bfree);
138   else if (type == 'l') out('d', statfs->f_namelen);
139   else if (type == 't') out('x', statfs->f_type);
140   else if (type == 'T') {
141     char *s = "unknown";
142     struct {unsigned num; char *name;} nn[] = {
143       {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
144       {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
145       {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
146       {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
147       {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
148       {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
149       {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
150       {0x73717368, "squashfs"}
151     };
152     int i;
153 
154     for (i=0; i<ARRAY_LEN(nn); i++)
155       if (nn[i].num == statfs->f_type) s = nn[i].name;
156     strout(s);
157   } else if (type == 'i') {
158     char buf[32];
159 
160     sprintf(buf, "%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
161     strout(buf);
162   } else if (type == 's') out('d', statfs->f_frsize);
163   else if (type == 'S') out('d', statfs->f_bsize);
164   else strout("?");
165 }
166 
stat_main(void)167 void stat_main(void)
168 {
169   int flagf = toys.optflags & FLAG_f, i;
170   char *format, *f;
171 
172   if (toys.optflags&FLAG_t) {
173     format = flagf ? "%n %i %l %t %s %S %b %f %a %c %d" :
174                      "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
175   } else format = flagf
176     ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %T\n"
177       "Block Size: %s    Fundamental block size: %S\n"
178       "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
179       "Inodes: Total: %c\tFree: %d"
180     : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
181       "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
182       "Access: (0%a/%A)\tUid: (%5u/%8U)\tGid: (%5g/%8G)\n"
183       "Access: %x\nModify: %y\nChange: %z";
184 
185   if (toys.optflags & FLAG_c) format = TT.c;
186 
187   for (i = 0; toys.optargs[i]; i++) {
188     int L = toys.optflags & FLAG_L;
189 
190     TT.file = toys.optargs[i];
191     if (flagf && !statfs(TT.file, (void *)&TT.stat));
192     else if (flagf || (L ? stat : lstat)(TT.file, (void *)&TT.stat)) {
193       perror_msg("'%s'", TT.file);
194       continue;
195     }
196 
197     for (f = format; *f; f++) {
198       if (*f != '%') putchar(*f);
199       else {
200         f = next_printf(f, &TT.pattern);
201         TT.patlen = f-TT.pattern;
202         if (TT.patlen>99) error_exit("bad %s", TT.pattern);
203         if (*f == 'n') strout(TT.file);
204         else if (flagf) print_statfs(*f);
205         else print_stat(*f);
206       }
207     }
208     xputc('\n');
209   }
210 }
211