1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#define FOR_stat
41#include "toys.h"
42
43GLOBALS(
44 char *c;
45
46 union {
47 struct stat st;
48 struct statfs sf;
49 } stat;
50 char *file, *pattern;
51 int patlen;
52)
53
54
55
56static void out(char c, long long val)
57{
58 sprintf(toybuf, "%.*sll%c", TT.patlen, TT.pattern, c);
59 printf(toybuf, val);
60}
61
62
63static void strout(char *val)
64{
65 sprintf(toybuf, "%.*ss", TT.patlen, TT.pattern);
66 printf(toybuf, val);
67}
68
69static void date_stat_format(struct timespec *ts)
70{
71 strout(format_iso_time(toybuf+128, sizeof(toybuf)-128, ts));
72}
73
74static void print_stat(char type)
75{
76 struct stat *stat = (struct stat *)&TT.stat;
77
78 if (type == 'a') out('o', stat->st_mode&~S_IFMT);
79 else if (type == 'A') {
80 char str[11];
81
82 mode_to_string(stat->st_mode, str);
83 strout(str);
84 } else if (type == 'b') out('u', stat->st_blocks);
85 else if (type == 'B') out('d', 512);
86 else if (type == 'C') {
87 char *context = NULL;
88
89 strout(lsm_get_context(TT.file, &context) != -1 ? context : "?");
90 free(context);
91 } else if (type == 'd') out('d', stat->st_dev);
92 else if (type == 'D') out('x', stat->st_dev);
93 else if (type == 'f') out('x', stat->st_mode);
94 else if (type == 'F') {
95 char *t = "character device\0directory\0block device\0" \
96 "regular file\0symbolic link\0socket\0FIFO (named pipe)";
97 int i, filetype = stat->st_mode & S_IFMT;
98
99 for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
100 if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
101 strout(t);
102 } else if (type == 'g') out('u', stat->st_gid);
103 else if (type == 'G') strout(getgroupname(stat->st_gid));
104 else if (type == 'h') out('u', stat->st_nlink);
105 else if (type == 'i') out('u', stat->st_ino);
106 else if (type == 'm') {
107 struct mtab_list *mt = xgetmountlist(0);
108 dev_t dev = stat->st_rdev ? stat->st_rdev : stat->st_dev;
109
110
111 for (dlist_terminate(mt); mt; mt = mt->next) if (mt->stat.st_dev == dev) {
112 strout(mt->dir);
113 break;
114 }
115 llist_traverse(mt, free);
116 } else if (type == 'N') {
117 printf("%s", TT.file);
118 if (S_ISLNK(stat->st_mode))
119 if (readlink0(TT.file, toybuf, sizeof(toybuf)))
120 printf(" -> '%s'", toybuf);
121 } else if (type == 'o') out('u', stat->st_blksize);
122 else if (type == 's') out('u', stat->st_size);
123 else if (type == 't') out('x', dev_major(stat->st_rdev));
124 else if (type == 'T') out('x', dev_minor(stat->st_rdev));
125 else if (type == 'u') out('u', stat->st_uid);
126 else if (type == 'U') strout(getusername(stat->st_uid));
127 else if (type == 'x') date_stat_format(&stat->st_atim);
128 else if (type == 'X') out('u', stat->st_atime);
129 else if (type == 'y') date_stat_format(&stat->st_mtim);
130 else if (type == 'Y') out('u', stat->st_mtime);
131 else if (type == 'z') date_stat_format(&stat->st_ctim);
132 else if (type == 'Z') out('u', stat->st_ctime);
133 else putchar('?');
134}
135
136static void print_statfs(char type) {
137 struct statfs *statfs = (struct statfs *)&TT.stat;
138
139 if (type == 'a') out('u', statfs->f_bavail);
140 else if (type == 'b') out('u', statfs->f_blocks);
141 else if (type == 'c') out('u', statfs->f_files);
142 else if (type == 'd') out('u', statfs->f_ffree);
143 else if (type == 'f') out('u', statfs->f_bfree);
144 else if (type == 'l') out('d', pathconf(TT.file, _PC_NAME_MAX));
145 else if (type == 't') out('x', statfs->f_type);
146 else if (type == 'T') strout(fs_type_name(statfs));
147 else if (type == 'i') {
148 int *val = (int *) &statfs->f_fsid;
149 char buf[32];
150
151 sprintf(buf, "%08x%08x", val[0], val[1]);
152 strout(buf);
153 } else if (type == 's') out('d', statfs_bsize(statfs));
154 else if (type == 'S') out('d', statfs_frsize(statfs));
155 else strout("?");
156}
157
158void stat_main(void)
159{
160 int flagf = FLAG(f), i;
161 char *format, *f;
162
163 if (FLAG(t)) format = flagf
164 ? "%n %i %l %t %s %S %b %f %a %c %d"
165 : "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
166 else format = flagf
167 ? " File: \"%n\"\n ID: %i Namelen: %l Type: %T\n"
168 "Block Size: %s Fundamental block size: %S\n"
169 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
170 "Inodes: Total: %c\tFree: %d"
171 : " File: %N\n Size: %s\t Blocks: %b\t IO Blocks: %B\t %F\n"
172 "Device: %Dh/%dd\t Inode: %i\t Links: %h\t Device type: %t,%T\n"
173 "Access: (%04a/%A)\tUid: (%5u/%8U)\tGid: (%5g/%8G)\n"
174 "Access: %x\nModify: %y\nChange: %z";
175
176 if (FLAG(c)) format = TT.c;
177
178
179 for (i = 0; toys.optargs[i]; i++) {
180
181
182 TT.file = toys.optargs[i];
183 if (flagf && !statfs(TT.file, (void *)&TT.stat));
184 else if (flagf || (FLAG(L) ? stat : lstat)(TT.file, (void *)&TT.stat)) {
185 perror_msg("'%s'", TT.file);
186 continue;
187 }
188
189
190 for (f = format; *f; f++) {
191 if (*f != '%' || !f[1]) putchar(*f);
192 else if (f[1]=='%') putchar(*f++);
193 else {
194 f = next_printf(f, &TT.pattern);
195 TT.patlen = f-TT.pattern;
196 if (!*f || TT.patlen>99) error_exit("bad %s", TT.pattern);
197 if (*f == 'n') strout(TT.file);
198 else if (flagf) print_statfs(*f);
199 else print_stat(*f);
200 }
201 }
202 xputc('\n');
203 }
204}
205