Mercurial > hg > toybox
comparison toys/pending/stat.c @ 912:f4f5132d5ac7
Stat cleanup.
From the mailing list:
Ok, first thing: clean up the help text. I realize what's there is copied verbatim from the man page, but that man page sucks. ("modification time" vs "change time"?) Took a bit of finagling to fit it in 80x24, but just made it.
GLOBALS() indent was still tab, change to two spaces. And I tend to put a blank line between options lib/args.c automatically fills out and normal globals.
We never do anything with date_stat_format() but immediately print it, might as well make the function do it.
The types[] array in do_stat() is a rough edge. Hmmm... there's no else case that sets the type in case it was unknown (such as 0). In theory, this never happens. In practice it means I can cheat slightly, given this observation:
$ find linux -name stat.h | xargs grep 'S_IF[A-Z]*[ \t]'
linux/include/uapi/linux/stat.h:#define S_IFMT 00170000
linux/include/uapi/linux/stat.h:#define S_IFSOCK 0140000
linux/include/uapi/linux/stat.h:#define S_IFLNK 0120000
linux/include/uapi/linux/stat.h:#define S_IFREG 0100000
linux/include/uapi/linux/stat.h:#define S_IFBLK 0060000
linux/include/uapi/linux/stat.h:#define S_IFDIR 0040000
linux/include/uapi/linux/stat.h:#define S_IFCHR 0020000
linux/include/uapi/linux/stat.h:#define S_IFIFO 0010000
I.E. the only place the I_IFBLAH constants occur a stat.h header in current linux code is in the generic stuff, it doesn't vary per target. (The access permission bits are actually subtly standardized in posix due to the command line arguments to chmod, although I'm sure cygwin finds a way to break. But the type fields, not so much. But linux has to be binary compatible with itself foreverish, and that's all I really care about.)
So, we have ALMOST have this going by twos, except there's no 8 and there is a 1. so let's make the 1 the default, feed a blank string into the 8... No, duh: octal. So it's actually 2, 4, 6, 8, 10, 12. So make the loop look like:
filetype = statf->st_mode & S_IFMT;
TT.ftname = types;
for (i = 1; filetype != (i*8192) && i < 7; i++)
TT.ftname += strlen(TT.ftname)+1;
Yes that's linux-specific, and I think I'm ok with that.
Printing all zeroes and pretending that's nanosecond resolution... either support it or don't. Let's see, supporting it is stat->st_atim.tv_nsec and similar... no mention of nanoseconds in strftime() (et tu, posix2008?) so pass it as a second argument and append it by hand... (Need to test that against musl...)
When we hit an unknown type in print_it() we print the literal character, which is right for %% but what about an unknown option?
$ stat -c %q /
?
Eh, I guess that's a "don't care". It didn't die with an error, that's the important thing.
I have a horrible idea for compressing the switch/case blocks, but should probably check this in and get some sleep for right now...
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 28 May 2013 00:28:45 -0500 |
parents | accabaaac666 |
children | 91d15ead5602 |
comparison
equal
deleted
inserted
replaced
911:accabaaac666 | 912:f4f5132d5ac7 |
---|---|
8 bool stat | 8 bool stat |
9 default n | 9 default n |
10 help | 10 help |
11 usage: stat [-f] [-c FORMAT] FILE... | 11 usage: stat [-f] [-c FORMAT] FILE... |
12 | 12 |
13 display file or file system status | 13 Display status of files or filesystems. |
14 | 14 |
15 -f display file system status instead of file status | 15 -f display filesystem status instead of file status |
16 -c use the specified FORMAT instead of the default; | 16 -c Output specified FORMAT string instead of default |
17 output a newline after each use of FORMAT | 17 |
18 | 18 The valid format escape sequences for files: |
19 The valid format sequences for files: | 19 %a Access bits (octal) |%A Access bits (flags)|%b Blocks allocated |
20 %a Access rights in octal | 20 %B Bytes per block |%d Device ID (dec) |%D Device ID (hex) |
21 %A Access rights in human readable form | 21 %f All mode bits (hex) |%F File type |%g Group ID |
22 %b Number of blocks allocated | 22 %G Group name |%h Hard links |%i Inode |
23 %B The size in bytes of each block | 23 %n Filename |%N Long filename |%o I/O block size |
24 %d Device number in decimal | 24 %s Size (bytes) |%u User ID |%U User name |
25 %D Device number in hex | 25 %x Access time |%X Access unix time |%y File write time |
26 %f Raw mode in hex | 26 %Y File write unix time|%z Dir change time |%Z Dir change unix time |
27 %F File type | 27 |
28 %g Group ID of owner | 28 The valid format escape sequences for filesystems: |
29 %G Group name of owner | 29 %a Available blocks |%b Total blocks |%c Total inodes |
30 %h Number of hard links | 30 %d Free inodes |%f Free blocks |%i File system ID |
31 %i Inode number | 31 %l Max filename length |%n File name |%s Fragment size |
32 %n File name | 32 %S Best transfer size |%t File system type |
33 %N Quoted file name with dereference if symbolic link | |
34 %o I/O block size | |
35 %s Total size, in bytes | |
36 %u User ID of owner | |
37 %U User name of owner | |
38 %x Time of last access | |
39 %X Time of last access as seconds since Epoch | |
40 %y Time of last modification | |
41 %Y Time of last modification as seconds since Epoch | |
42 %z Time of last change | |
43 %Z Time of last change as seconds since Epoch | |
44 | |
45 The valid format sequences for file systems: | |
46 %a Available blocks for unpriviledges user | |
47 %b Total number of blocks | |
48 %c Total number of inodes | |
49 %d Number of free inodes | |
50 %f Number of free blocks | |
51 %i File system ID | |
52 %l Maximum length of file names | |
53 %n File name | |
54 %s Fragment size | |
55 %S Optimal transfer block size | |
56 %t File system type | |
57 */ | 33 */ |
58 | 34 |
59 #define FOR_stat | 35 #define FOR_stat |
60 #include "toys.h" | 36 #include "toys.h" |
61 | 37 |
62 GLOBALS( | 38 GLOBALS( |
63 char *fmt; | 39 char *fmt; |
64 void *stat; | 40 |
65 char *file_type; | 41 void *stat; |
66 struct passwd *user_name; | 42 struct passwd *user_name; |
67 struct group *group_name; | 43 struct group *group_name; |
68 char access_str[11]; | 44 char *ftname, access_str[11]; |
69 ) | 45 ) |
70 | 46 |
71 | 47 |
72 static char * date_stat_format(time_t time) | 48 static void date_stat_format(time_t time, int nano) |
73 { | 49 { |
74 static char buf[36]; | 50 static char buf[36]; |
75 | 51 int len; |
76 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&time)); | 52 |
77 return buf; | 53 len = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.", localtime(&time)); |
78 } | 54 sprintf(buf+len, "%09d", nano); |
79 | 55 xprintf("%s", buf); |
80 static int print_stat(char type) { | 56 } |
57 | |
58 static int print_stat(char type) | |
59 { | |
81 struct stat *stat = (struct stat*)TT.stat; | 60 struct stat *stat = (struct stat*)TT.stat; |
61 | |
82 switch (type) { | 62 switch (type) { |
83 case 'a': | 63 case 'a': |
84 xprintf("%04lo", stat->st_mode & ~S_IFMT); | 64 xprintf("%04lo", stat->st_mode & ~S_IFMT); |
85 break; | 65 break; |
86 case 'A': | 66 case 'A': |
100 break; | 80 break; |
101 case 'f': | 81 case 'f': |
102 xprintf("%lx", stat->st_mode); | 82 xprintf("%lx", stat->st_mode); |
103 break; | 83 break; |
104 case 'F': | 84 case 'F': |
105 xprintf("%s", TT.file_type); | 85 xprintf("%s", TT.ftname); |
106 break; | 86 break; |
107 case 'g': | 87 case 'g': |
108 xprintf("%lu", stat->st_gid); | 88 xprintf("%lu", stat->st_gid); |
109 break; | 89 break; |
110 case 'G': | 90 case 'G': |
130 break; | 110 break; |
131 case 'U': | 111 case 'U': |
132 xprintf("%8s", TT.user_name->pw_name); | 112 xprintf("%8s", TT.user_name->pw_name); |
133 break; | 113 break; |
134 case 'x': | 114 case 'x': |
135 xprintf("%s", date_stat_format(stat->st_atime)); | 115 date_stat_format(stat->st_atime, stat->st_atim.tv_nsec); |
136 break; | 116 break; |
137 case 'X': | 117 case 'X': |
138 xprintf("%llu", stat->st_atime); | 118 xprintf("%llu", stat->st_atime); |
139 break; | 119 break; |
140 case 'y': | 120 case 'y': |
141 xprintf("%s", date_stat_format(stat->st_mtime)); | 121 date_stat_format(stat->st_mtime, stat->st_mtim.tv_nsec); |
142 break; | 122 break; |
143 case 'Y': | 123 case 'Y': |
144 xprintf("%llu", stat->st_mtime); | 124 xprintf("%llu", stat->st_mtime); |
145 break; | 125 break; |
146 case 'z': | 126 case 'z': |
147 xprintf("%s", date_stat_format(stat->st_ctime)); | 127 date_stat_format(stat->st_ctime, stat->st_ctim.tv_nsec); |
148 break; | 128 break; |
149 case 'Z': | 129 case 'Z': |
150 xprintf("%llu", stat->st_ctime); | 130 xprintf("%llu", stat->st_ctime); |
151 break; | 131 break; |
152 default: | 132 default: |
155 return 0; | 135 return 0; |
156 } | 136 } |
157 | 137 |
158 static int print_statfs(char type) { | 138 static int print_statfs(char type) { |
159 struct statfs *statfs = (struct statfs*)TT.stat; | 139 struct statfs *statfs = (struct statfs*)TT.stat; |
140 | |
160 switch (type) { | 141 switch (type) { |
161 case 'a': | 142 case 'a': |
162 xprintf("%lu", statfs->f_bavail); | 143 xprintf("%lu", statfs->f_bavail); |
163 break; | 144 break; |
164 case 'b': | 145 case 'b': |
195 } | 176 } |
196 | 177 |
197 static int do_stat(char *path) | 178 static int do_stat(char *path) |
198 { | 179 { |
199 struct stat *statf = (struct stat*)TT.stat; | 180 struct stat *statf = (struct stat*)TT.stat; |
200 size_t i; | 181 char *types = "character device\0directory\0block device\0" \ |
201 struct { | 182 "regular file\0symbolic link\0socket\0FIFO (named pipe)"; |
202 mode_t mode; | 183 int i, filetype; |
203 char *str; | |
204 } types[] = { | |
205 {S_IFDIR, "directory"}, | |
206 {S_IFCHR, "character device"}, | |
207 {S_IFBLK, "block device"}, | |
208 {S_IFREG, "regular file"}, | |
209 {S_IFIFO, "FIFO (named pipe)"}, | |
210 {S_IFLNK, "symbolic link"}, | |
211 {S_IFSOCK, "socket"} | |
212 }; | |
213 | 184 |
214 if (stat(path, statf) < 0) return 1; | 185 if (stat(path, statf) < 0) return 1; |
215 | 186 |
216 for (i = 0; i < sizeof(types)/sizeof(*types); i++) | 187 filetype = statf->st_mode & S_IFMT; |
217 if ((statf->st_mode & S_IFMT) == types[i].mode) TT.file_type = types[i].str; | 188 TT.ftname = types; |
218 if (!statf->st_size && (statf->st_mode & S_IFMT) == S_IFREG) | 189 for (i = 1; filetype != (i*8192) && i < 7; i++) |
219 TT.file_type = "regular empty file"; | 190 TT.ftname += strlen(TT.ftname)+1; |
191 if (!statf->st_size && filetype == S_IFREG) | |
192 TT.ftname = "regular empty file"; | |
220 | 193 |
221 // check user and group name | 194 // check user and group name |
222 TT.user_name = getpwuid(statf->st_uid); | 195 TT.user_name = getpwuid(statf->st_uid); |
223 TT.group_name = getgrgid(statf->st_gid); | 196 TT.group_name = getgrgid(statf->st_gid); |
224 // function to get access in human readable format | 197 // function to get access in human readable format |
225 format_mode(&TT.access_str, statf->st_mode); | 198 format_mode(&TT.access_str, statf->st_mode); |
199 | |
226 return 0; | 200 return 0; |
227 } | 201 } |
228 | 202 |
229 static int do_statfs(char *path) | 203 static int do_statfs(char *path) |
230 { | 204 { |