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, "c: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  File system type
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.%09d", toybuf, ts->tv_nsec);
59 }
60 
print_stat(char type)61 static void print_stat(char type)
62 {
63   struct stat *stat = (struct stat *)&TT.stat;
64 
65   if (type == 'a') xprintf("%lo", stat->st_mode & ~S_IFMT);
66   else if (type == 'A') {
67     char str[11];
68 
69     mode_to_string(stat->st_mode, str);
70     xprintf("%s", str);
71   } else if (type == 'b') xprintf("%llu", stat->st_blocks);
72   else if (type == 'B') xprintf("%lu", stat->st_blksize);
73   else if (type == 'd') xprintf("%ldd", stat->st_dev);
74   else if (type == 'D') xprintf("%llxh", stat->st_dev);
75   else if (type == 'f') xprintf("%lx", stat->st_mode);
76   else if (type == 'F') {
77     char *t = "character device\0directory\0block device\0" \
78               "regular file\0symbolic link\0socket\0FIFO (named pipe)";
79     int i, filetype = stat->st_mode & S_IFMT;
80 
81     for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
82     if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
83     xprintf("%s", t);
84   } else if (type == 'g') xprintf("%lu", stat->st_gid);
85   else if (type == 'G') xprintf("%8s", TT.user_name->pw_name);
86   else if (type == 'h') xprintf("%lu", stat->st_nlink);
87   else if (type == 'i') xprintf("%llu", stat->st_ino);
88   else if (type == 'N') {
89     xprintf("`%s'", *toys.optargs);
90     if (S_ISLNK(stat->st_mode))
91       if (0<readlink(*toys.optargs, toybuf, sizeof(toybuf)))
92         xprintf(" -> `%s'", toybuf);
93   } else if (type == 'o') xprintf("%lu", stat->st_blksize);
94   else if (type == 's') xprintf("%llu", stat->st_size);
95   else if (type == 'u') xprintf("%lu", stat->st_uid);
96   else if (type == 'U') xprintf("%8s", TT.user_name->pw_name);
97   else if (type == 'x') date_stat_format((void *)&stat->st_atime);
98   else if (type == 'X') xprintf("%llu", (long long)stat->st_atime);
99   else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
100   else if (type == 'Y') xprintf("%llu", (long long)stat->st_mtime);
101   else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
102   else if (type == 'Z') xprintf("%llu", (long long)stat->st_ctime);
103   else xprintf("?");
104 }
105 
print_statfs(char type)106 static void print_statfs(char type) {
107   struct statfs *statfs = (struct statfs *)&TT.stat;
108 
109   if (type == 'a') xprintf("%llu", statfs->f_bavail);
110   else if (type == 'b') xprintf("%llu", statfs->f_blocks);
111   else if (type == 'c') xprintf("%llu", statfs->f_files);
112   else if (type == 'd') xprintf("%llu", statfs->f_ffree);
113   else if (type == 'f') xprintf("%llu", statfs->f_bfree);
114   else if (type == 'l') xprintf("%ld", statfs->f_namelen);
115   else if (type == 't') xprintf("%lx", statfs->f_type);
116   else if (type == 'i')
117     xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
118   else if (type == 's') xprintf("%d", statfs->f_frsize);
119   else if (type == 'S') xprintf("%d", statfs->f_bsize);
120   else xprintf("?");
121 }
122 
stat_main(void)123 void stat_main(void)
124 {
125   int flagf = toys.optflags & FLAG_f;
126   char *format = flagf
127     ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %t\n"
128       "Block Size: %s    Fundamental block size: %S\n"
129       "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
130       "Inodes: Total: %c\tFree: %d"
131     : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
132       "Device: %D\t Inode: %i\t Links: %h\n"
133       "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
134       "Access: %x\nModify: %y\nChange: %z";
135 
136   if (toys.optflags & FLAG_c) format = TT.fmt;
137 
138   for (; *toys.optargs; toys.optargs++) {
139     char *f;
140 
141     if (flagf && !statfs(*toys.optargs, (void *)&TT.stat));
142     else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) {
143       struct stat *stat = (struct stat*)&TT.stat;
144 
145       // check user and group name
146       TT.user_name = getpwuid(stat->st_uid);
147       TT.group_name = getgrgid(stat->st_gid);
148     } else {
149       perror_msg("'%s'", *toys.optargs);
150       continue;
151     }
152 
153     for (f = format; *f; f++) {
154       if (*f != '%') putchar(*f);
155       else {
156         if (*++f == 'n') xprintf("%s", *toys.optargs);
157         else if (flagf) print_statfs(*f);
158         else print_stat(*f);
159       }
160     }
161     xputc('\n');
162   }
163 }
164