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:f", TOYFLAG_BIN))
6 
7 config STAT
8   bool stat
9   default y
10   help
11     usage: stat [-f] [-c FORMAT] FILE...
12 
13     Display status of files or filesystems.
14 
15     -f display filesystem status instead of file status
16     -c Output specified FORMAT string instead of default
17 
18     The valid format escape sequences for files:
19     %a  Access bits (octal) |%A  Access bits (flags)|%b  Blocks allocated
20     %B  Bytes per block     |%d  Device ID (dec)    |%D  Device ID (hex)
21     %f  All mode bits (hex) |%F  File type          |%g  Group ID
22     %G  Group name          |%h  Hard links         |%i  Inode
23     %n  Filename            |%N  Long filename      |%o  I/O block size
24     %s  Size (bytes)        |%u  User ID            |%U  User name
25     %x  Access time         |%X  Access unix time   |%y  File write time
26     %Y  File write unix time|%z  Dir change time    |%Z  Dir change unix time
27 
28     The valid format escape sequences for filesystems:
29     %a  Available blocks    |%b  Total blocks       |%c  Total inodes
30     %d  Free inodes         |%f  Free blocks        |%i  File system ID
31     %l  Max filename length |%n  File name          |%s  Fragment size
32     %S  Best transfer size  |%t  Filesystem type    |%T  Filesystem type name
33 */
34 
35 #define FOR_stat
36 #include "toys.h"
37 
GLOBALS(char * fmt;union{ struct stat st; struct statfs sf; } stat;struct passwd * user_name;struct group * group_name;)38 GLOBALS(
39   char *fmt;
40 
41   union {
42     struct stat st;
43     struct statfs sf;
44   } stat;
45   struct passwd *user_name;
46   struct group *group_name;
47 )
48 
49 
50 // Note: the atime, mtime, and ctime fields in struct stat are the start
51 // of embedded struct timespec, but posix won't let them use that
52 // struct definition for legacy/namespace reasons.
53 
54 static void date_stat_format(struct timespec *ts)
55 {
56   strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
57     localtime(&(ts->tv_sec)));
58   xprintf("%s.%09ld", toybuf, ts->tv_nsec);
59 }
60 
61 // Force numeric output to long long instead of manually typecasting everything
out(char c,long long val)62 static void out(char c, long long val)
63 {
64   sprintf(toybuf, "%%ll%c", c);
65   printf(toybuf, val);
66 }
67 
print_stat(char type)68 static void print_stat(char type)
69 {
70   struct stat *stat = (struct stat *)&TT.stat;
71 
72   if (type == 'a') out('o', stat->st_mode&~S_IFMT);
73   else if (type == 'A') {
74     char str[11];
75 
76     mode_to_string(stat->st_mode, str);
77     xprintf("%s", str);
78   } else if (type == 'b') out('u', stat->st_blocks);
79   else if (type == 'B') out('u', stat->st_blksize);
80   else if (type == 'd') out('d', stat->st_dev);
81   else if (type == 'D') out('x', stat->st_dev);
82   else if (type == 'f') out('x', stat->st_mode);
83   else if (type == 'F') {
84     char *t = "character device\0directory\0block device\0" \
85               "regular file\0symbolic link\0socket\0FIFO (named pipe)";
86     int i, filetype = stat->st_mode & S_IFMT;
87 
88     for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
89     if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
90     xprintf("%s", t);
91   } else if (type == 'g') out('u', stat->st_gid);
92   else if (type == 'G') xprintf("%8s", TT.group_name->gr_name);
93   else if (type == 'h') out('u', stat->st_nlink);
94   else if (type == 'i') out('u', stat->st_ino);
95   else if (type == 'N') {
96     xprintf("`%s'", *toys.optargs);
97     if (S_ISLNK(stat->st_mode))
98       if (0<readlink(*toys.optargs, toybuf, sizeof(toybuf)))
99         xprintf(" -> `%s'", toybuf);
100   } else if (type == 'o') out('u', stat->st_blksize);
101   else if (type == 's') out('u', stat->st_size);
102   else if (type == 'u') out('u', stat->st_uid);
103   else if (type == 'U') xprintf("%8s", TT.user_name->pw_name);
104   else if (type == 'x') date_stat_format((void *)&stat->st_atime);
105   else if (type == 'X') out('u', stat->st_atime);
106   else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
107   else if (type == 'Y') out('u', stat->st_mtime);
108   else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
109   else if (type == 'Z') out('u', stat->st_ctime);
110   else xprintf("?");
111 }
112 
print_statfs(char type)113 static void print_statfs(char type) {
114   struct statfs *statfs = (struct statfs *)&TT.stat;
115 
116   if (type == 'a') out('u', statfs->f_bavail);
117   else if (type == 'b') out('u', statfs->f_blocks);
118   else if (type == 'c') out('u', statfs->f_files);
119   else if (type == 'd') out('u', statfs->f_ffree);
120   else if (type == 'f') out('u', statfs->f_bfree);
121   else if (type == 'l') out('d', statfs->f_namelen);
122   else if (type == 't') out('x', statfs->f_type);
123   else if (type == 'T') {
124     char *s = "unknown";
125     struct {unsigned num; char *name;} nn[] = {
126       {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
127       {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
128       {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
129       {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
130       {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
131       {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
132       {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
133       {0x73717368, "squashfs"}
134     };
135     int i;
136 
137     for (i=0; i<ARRAY_LEN(nn); i++)
138       if (nn[i].num == statfs->f_type) s = nn[i].name;
139     fputs(s, stdout);
140   } else if (type == 'i')
141     xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
142   else if (type == 's') out('d', statfs->f_frsize);
143   else if (type == 'S') out('d', statfs->f_bsize);
144   else xprintf("?");
145 }
146 
stat_main(void)147 void stat_main(void)
148 {
149   int flagf = toys.optflags & FLAG_f;
150   char *format = flagf
151     ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %t\n"
152       "Block Size: %s    Fundamental block size: %S\n"
153       "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
154       "Inodes: Total: %c\tFree: %d"
155     : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
156       "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
157       "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
158       "Access: %x\nModify: %y\nChange: %z";
159 
160   if (toys.optflags & FLAG_c) format = TT.fmt;
161 
162   for (; *toys.optargs; toys.optargs++) {
163     char *f;
164 
165     if (flagf && !statfs(*toys.optargs, (void *)&TT.stat));
166     else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) {
167       struct stat *stat = (struct stat*)&TT.stat;
168 
169       // check user and group name
170       TT.user_name = getpwuid(stat->st_uid);
171       TT.group_name = getgrgid(stat->st_gid);
172     } else {
173       perror_msg("'%s'", *toys.optargs);
174       continue;
175     }
176 
177     for (f = format; *f; f++) {
178       if (*f != '%') putchar(*f);
179       else {
180         if (*++f == 'n') xprintf("%s", *toys.optargs);
181         else if (flagf) print_statfs(*f);
182         else print_stat(*f);
183       }
184     }
185     xputc('\n');
186   }
187 }
188