Annotation of /tags/mkinitrd-6_5_2/busybox/coreutils/stat.c
Parent Directory
|
Revision Log
Revision 1522 -
(hide annotations)
(download)
Wed Sep 7 18:40:16 2011 UTC (12 years, 9 months ago) by niro
File MIME type: text/plain
File size: 20551 byte(s)
Wed Sep 7 18:40:16 2011 UTC (12 years, 9 months ago) by niro
File MIME type: text/plain
File size: 20551 byte(s)
tagged 'mkinitrd-6_5_2'
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * stat -- display file or file system status | ||
4 | * | ||
5 | * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation. | ||
6 | * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org> | ||
7 | * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org> | ||
8 | niro | 816 | * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp> |
9 | niro | 532 | * |
10 | * Written by Michael Meskes | ||
11 | * Taken from coreutils and turned into a busybox applet by Mike Frysinger | ||
12 | * | ||
13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
14 | */ | ||
15 | niro | 816 | #include "libbb.h" |
16 | niro | 532 | |
17 | niro | 816 | #define OPT_FILESYS (1 << 0) |
18 | #define OPT_TERSE (1 << 1) | ||
19 | #define OPT_DEREFERENCE (1 << 2) | ||
20 | #define OPT_SELINUX (1 << 3) | ||
21 | niro | 532 | |
22 | niro | 816 | #if ENABLE_FEATURE_STAT_FORMAT |
23 | typedef bool (*statfunc_ptr)(const char *, const char *); | ||
24 | #else | ||
25 | typedef bool (*statfunc_ptr)(const char *); | ||
26 | #endif | ||
27 | |||
28 | static const char *file_type(const struct stat *st) | ||
29 | niro | 532 | { |
30 | /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 | ||
31 | * for some of these formats. | ||
32 | * To keep diagnostics grammatical in English, the | ||
33 | * returned string must start with a consonant. | ||
34 | */ | ||
35 | if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file"; | ||
36 | if (S_ISDIR(st->st_mode)) return "directory"; | ||
37 | if (S_ISBLK(st->st_mode)) return "block special file"; | ||
38 | if (S_ISCHR(st->st_mode)) return "character special file"; | ||
39 | if (S_ISFIFO(st->st_mode)) return "fifo"; | ||
40 | if (S_ISLNK(st->st_mode)) return "symbolic link"; | ||
41 | if (S_ISSOCK(st->st_mode)) return "socket"; | ||
42 | if (S_TYPEISMQ(st)) return "message queue"; | ||
43 | if (S_TYPEISSEM(st)) return "semaphore"; | ||
44 | if (S_TYPEISSHM(st)) return "shared memory object"; | ||
45 | #ifdef S_TYPEISTMO | ||
46 | if (S_TYPEISTMO(st)) return "typed memory object"; | ||
47 | #endif | ||
48 | return "weird file"; | ||
49 | } | ||
50 | |||
51 | niro | 816 | static const char *human_time(time_t t) |
52 | niro | 532 | { |
53 | /* Old | ||
54 | static char *str; | ||
55 | str = ctime(&t); | ||
56 | str[strlen(str)-1] = '\0'; | ||
57 | return str; | ||
58 | */ | ||
59 | /* coreutils 6.3 compat: */ | ||
60 | niro | 816 | |
61 | /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ | ||
62 | #define buf bb_common_bufsiz1 | ||
63 | |||
64 | niro | 532 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); |
65 | return buf; | ||
66 | niro | 816 | #undef buf |
67 | niro | 532 | } |
68 | |||
69 | /* Return the type of the specified file system. | ||
70 | * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris) | ||
71 | * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2) | ||
72 | * Still others have neither and have to get by with f_type (Linux). | ||
73 | */ | ||
74 | niro | 816 | static const char *human_fstype(uint32_t f_type) |
75 | niro | 532 | { |
76 | static const struct types { | ||
77 | niro | 816 | uint32_t type; |
78 | const char *const fs; | ||
79 | niro | 532 | } humantypes[] = { |
80 | { 0xADFF, "affs" }, | ||
81 | { 0x1Cd1, "devpts" }, | ||
82 | { 0x137D, "ext" }, | ||
83 | { 0xEF51, "ext2" }, | ||
84 | { 0xEF53, "ext2/ext3" }, | ||
85 | { 0x3153464a, "jfs" }, | ||
86 | { 0x58465342, "xfs" }, | ||
87 | { 0xF995E849, "hpfs" }, | ||
88 | { 0x9660, "isofs" }, | ||
89 | { 0x4000, "isofs" }, | ||
90 | { 0x4004, "isofs" }, | ||
91 | { 0x137F, "minix" }, | ||
92 | { 0x138F, "minix (30 char.)" }, | ||
93 | { 0x2468, "minix v2" }, | ||
94 | { 0x2478, "minix v2 (30 char.)" }, | ||
95 | { 0x4d44, "msdos" }, | ||
96 | { 0x4006, "fat" }, | ||
97 | { 0x564c, "novell" }, | ||
98 | { 0x6969, "nfs" }, | ||
99 | { 0x9fa0, "proc" }, | ||
100 | { 0x517B, "smb" }, | ||
101 | { 0x012FF7B4, "xenix" }, | ||
102 | { 0x012FF7B5, "sysv4" }, | ||
103 | { 0x012FF7B6, "sysv2" }, | ||
104 | { 0x012FF7B7, "coh" }, | ||
105 | { 0x00011954, "ufs" }, | ||
106 | { 0x012FD16D, "xia" }, | ||
107 | { 0x5346544e, "ntfs" }, | ||
108 | { 0x1021994, "tmpfs" }, | ||
109 | { 0x52654973, "reiserfs" }, | ||
110 | { 0x28cd3d45, "cramfs" }, | ||
111 | { 0x7275, "romfs" }, | ||
112 | { 0x858458f6, "romfs" }, | ||
113 | { 0x73717368, "squashfs" }, | ||
114 | { 0x62656572, "sysfs" }, | ||
115 | { 0, "UNKNOWN" } | ||
116 | }; | ||
117 | niro | 816 | |
118 | int i; | ||
119 | |||
120 | for (i = 0; humantypes[i].type; ++i) | ||
121 | niro | 532 | if (humantypes[i].type == f_type) |
122 | break; | ||
123 | return humantypes[i].fs; | ||
124 | } | ||
125 | |||
126 | niro | 984 | /* "man statfs" says that statfsbuf->f_fsid is a mess */ |
127 | /* coreutils treats it as an array of ints, most significant first */ | ||
128 | static unsigned long long get_f_fsid(const struct statfs *statfsbuf) | ||
129 | { | ||
130 | const unsigned *p = (const void*) &statfsbuf->f_fsid; | ||
131 | unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned); | ||
132 | unsigned long long r = 0; | ||
133 | |||
134 | do | ||
135 | r = (r << (sizeof(unsigned)*8)) | *p++; | ||
136 | while (--sz > 0); | ||
137 | return r; | ||
138 | } | ||
139 | |||
140 | niro | 816 | #if ENABLE_FEATURE_STAT_FORMAT |
141 | static void strcatc(char *str, char c) | ||
142 | { | ||
143 | int len = strlen(str); | ||
144 | str[len++] = c; | ||
145 | str[len] = '\0'; | ||
146 | } | ||
147 | |||
148 | static void printfs(char *pformat, const char *msg) | ||
149 | { | ||
150 | strcatc(pformat, 's'); | ||
151 | printf(pformat, msg); | ||
152 | } | ||
153 | |||
154 | niro | 532 | /* print statfs info */ |
155 | niro | 1123 | static void FAST_FUNC print_statfs(char *pformat, const char m, |
156 | niro | 816 | const char *const filename, const void *data |
157 | niro | 984 | IF_SELINUX(, security_context_t scontext)) |
158 | niro | 532 | { |
159 | niro | 816 | const struct statfs *statfsbuf = data; |
160 | if (m == 'n') { | ||
161 | printfs(pformat, filename); | ||
162 | } else if (m == 'i') { | ||
163 | niro | 984 | strcat(pformat, "llx"); |
164 | printf(pformat, get_f_fsid(statfsbuf)); | ||
165 | niro | 816 | } else if (m == 'l') { |
166 | strcat(pformat, "lu"); | ||
167 | niro | 1123 | printf(pformat, (unsigned long) statfsbuf->f_namelen); |
168 | niro | 816 | } else if (m == 't') { |
169 | strcat(pformat, "lx"); | ||
170 | niro | 1123 | printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */ |
171 | niro | 816 | } else if (m == 'T') { |
172 | printfs(pformat, human_fstype(statfsbuf->f_type)); | ||
173 | } else if (m == 'b') { | ||
174 | niro | 1123 | strcat(pformat, "llu"); |
175 | printf(pformat, (unsigned long long) statfsbuf->f_blocks); | ||
176 | niro | 816 | } else if (m == 'f') { |
177 | niro | 1123 | strcat(pformat, "llu"); |
178 | printf(pformat, (unsigned long long) statfsbuf->f_bfree); | ||
179 | niro | 816 | } else if (m == 'a') { |
180 | niro | 1123 | strcat(pformat, "llu"); |
181 | printf(pformat, (unsigned long long) statfsbuf->f_bavail); | ||
182 | niro | 816 | } else if (m == 's' || m == 'S') { |
183 | strcat(pformat, "lu"); | ||
184 | niro | 1123 | printf(pformat, (unsigned long) statfsbuf->f_bsize); |
185 | niro | 816 | } else if (m == 'c') { |
186 | niro | 1123 | strcat(pformat, "llu"); |
187 | printf(pformat, (unsigned long long) statfsbuf->f_files); | ||
188 | niro | 816 | } else if (m == 'd') { |
189 | niro | 1123 | strcat(pformat, "llu"); |
190 | printf(pformat, (unsigned long long) statfsbuf->f_ffree); | ||
191 | # if ENABLE_SELINUX | ||
192 | niro | 816 | } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { |
193 | printfs(pformat, scontext); | ||
194 | niro | 1123 | # endif |
195 | niro | 816 | } else { |
196 | strcatc(pformat, 'c'); | ||
197 | niro | 532 | printf(pformat, m); |
198 | } | ||
199 | } | ||
200 | |||
201 | /* print stat info */ | ||
202 | niro | 1123 | static void FAST_FUNC print_stat(char *pformat, const char m, |
203 | niro | 816 | const char *const filename, const void *data |
204 | niro | 984 | IF_SELINUX(, security_context_t scontext)) |
205 | niro | 532 | { |
206 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) | ||
207 | struct stat *statbuf = (struct stat *) data; | ||
208 | struct passwd *pw_ent; | ||
209 | struct group *gw_ent; | ||
210 | |||
211 | niro | 816 | if (m == 'n') { |
212 | printfs(pformat, filename); | ||
213 | } else if (m == 'N') { | ||
214 | strcatc(pformat, 's'); | ||
215 | niro | 532 | if (S_ISLNK(statbuf->st_mode)) { |
216 | niro | 816 | char *linkname = xmalloc_readlink_or_warn(filename); |
217 | if (linkname == NULL) | ||
218 | niro | 532 | return; |
219 | niro | 1123 | printf("'%s' -> '%s'", filename, linkname); |
220 | niro | 816 | free(linkname); |
221 | niro | 532 | } else { |
222 | printf(pformat, filename); | ||
223 | } | ||
224 | niro | 816 | } else if (m == 'd') { |
225 | niro | 1123 | strcat(pformat, "llu"); |
226 | printf(pformat, (unsigned long long) statbuf->st_dev); | ||
227 | niro | 816 | } else if (m == 'D') { |
228 | niro | 1123 | strcat(pformat, "llx"); |
229 | printf(pformat, (unsigned long long) statbuf->st_dev); | ||
230 | niro | 816 | } else if (m == 'i') { |
231 | niro | 1123 | strcat(pformat, "llu"); |
232 | printf(pformat, (unsigned long long) statbuf->st_ino); | ||
233 | niro | 816 | } else if (m == 'a') { |
234 | strcat(pformat, "lo"); | ||
235 | printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO))); | ||
236 | } else if (m == 'A') { | ||
237 | printfs(pformat, bb_mode_string(statbuf->st_mode)); | ||
238 | } else if (m == 'f') { | ||
239 | strcat(pformat, "lx"); | ||
240 | printf(pformat, (unsigned long) statbuf->st_mode); | ||
241 | } else if (m == 'F') { | ||
242 | printfs(pformat, file_type(statbuf)); | ||
243 | } else if (m == 'h') { | ||
244 | strcat(pformat, "lu"); | ||
245 | printf(pformat, (unsigned long) statbuf->st_nlink); | ||
246 | } else if (m == 'u') { | ||
247 | strcat(pformat, "lu"); | ||
248 | printf(pformat, (unsigned long) statbuf->st_uid); | ||
249 | } else if (m == 'U') { | ||
250 | niro | 532 | setpwent(); |
251 | pw_ent = getpwuid(statbuf->st_uid); | ||
252 | niro | 816 | printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN"); |
253 | } else if (m == 'g') { | ||
254 | strcat(pformat, "lu"); | ||
255 | printf(pformat, (unsigned long) statbuf->st_gid); | ||
256 | } else if (m == 'G') { | ||
257 | niro | 532 | setgrent(); |
258 | gw_ent = getgrgid(statbuf->st_gid); | ||
259 | niro | 816 | printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN"); |
260 | } else if (m == 't') { | ||
261 | strcat(pformat, "lx"); | ||
262 | printf(pformat, (unsigned long) major(statbuf->st_rdev)); | ||
263 | } else if (m == 'T') { | ||
264 | strcat(pformat, "lx"); | ||
265 | printf(pformat, (unsigned long) minor(statbuf->st_rdev)); | ||
266 | } else if (m == 's') { | ||
267 | niro | 1123 | strcat(pformat, "llu"); |
268 | printf(pformat, (unsigned long long) statbuf->st_size); | ||
269 | niro | 816 | } else if (m == 'B') { |
270 | strcat(pformat, "lu"); | ||
271 | printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE | ||
272 | } else if (m == 'b') { | ||
273 | niro | 1123 | strcat(pformat, "llu"); |
274 | printf(pformat, (unsigned long long) statbuf->st_blocks); | ||
275 | niro | 816 | } else if (m == 'o') { |
276 | strcat(pformat, "lu"); | ||
277 | printf(pformat, (unsigned long) statbuf->st_blksize); | ||
278 | } else if (m == 'x') { | ||
279 | printfs(pformat, human_time(statbuf->st_atime)); | ||
280 | } else if (m == 'X') { | ||
281 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); | ||
282 | niro | 1123 | /* note: (unsigned long) would be wrong: |
283 | * imagine (unsigned long64)int32 */ | ||
284 | printf(pformat, (long) statbuf->st_atime); | ||
285 | niro | 816 | } else if (m == 'y') { |
286 | printfs(pformat, human_time(statbuf->st_mtime)); | ||
287 | } else if (m == 'Y') { | ||
288 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); | ||
289 | niro | 1123 | printf(pformat, (long) statbuf->st_mtime); |
290 | niro | 816 | } else if (m == 'z') { |
291 | printfs(pformat, human_time(statbuf->st_ctime)); | ||
292 | } else if (m == 'Z') { | ||
293 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); | ||
294 | niro | 1123 | printf(pformat, (long) statbuf->st_ctime); |
295 | # if ENABLE_SELINUX | ||
296 | niro | 816 | } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { |
297 | printfs(pformat, scontext); | ||
298 | niro | 1123 | # endif |
299 | niro | 816 | } else { |
300 | strcatc(pformat, 'c'); | ||
301 | niro | 532 | printf(pformat, m); |
302 | } | ||
303 | } | ||
304 | |||
305 | niro | 1123 | static void print_it(const char *masterformat, |
306 | const char *filename, | ||
307 | void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)), | ||
308 | niro | 816 | const void *data |
309 | niro | 1123 | IF_SELINUX(, security_context_t scontext)) |
310 | niro | 532 | { |
311 | niro | 816 | /* Create a working copy of the format string */ |
312 | niro | 532 | char *format = xstrdup(masterformat); |
313 | /* Add 2 to accomodate our conversion of the stat '%s' format string | ||
314 | * to the printf '%llu' one. */ | ||
315 | niro | 816 | char *dest = xmalloc(strlen(format) + 2 + 1); |
316 | char *b; | ||
317 | niro | 532 | |
318 | b = format; | ||
319 | while (b) { | ||
320 | niro | 1123 | /* Each iteration finds next %spec, |
321 | * prints preceding string and handles found %spec | ||
322 | */ | ||
323 | niro | 532 | size_t len; |
324 | char *p = strchr(b, '%'); | ||
325 | if (!p) { | ||
326 | niro | 1123 | /* coreutils 6.3 always prints newline at the end */ |
327 | niro | 532 | /*fputs(b, stdout);*/ |
328 | puts(b); | ||
329 | break; | ||
330 | } | ||
331 | niro | 1123 | |
332 | /* dest = "%<modifiers>" */ | ||
333 | len = 1 + strspn(p + 1, "#-+.I 0123456789"); | ||
334 | memcpy(dest, p, len); | ||
335 | dest[len] = '\0'; | ||
336 | |||
337 | /* print preceding string */ | ||
338 | *p = '\0'; | ||
339 | niro | 532 | fputs(b, stdout); |
340 | |||
341 | p += len; | ||
342 | b = p + 1; | ||
343 | switch (*p) { | ||
344 | case '\0': | ||
345 | b = NULL; | ||
346 | /* fall through */ | ||
347 | case '%': | ||
348 | niro | 816 | bb_putchar('%'); |
349 | niro | 532 | break; |
350 | default: | ||
351 | niro | 816 | /* Completes "%<modifiers>" with specifier and printfs */ |
352 | niro | 984 | print_func(dest, *p, filename, data IF_SELINUX(,scontext)); |
353 | niro | 532 | break; |
354 | } | ||
355 | } | ||
356 | |||
357 | free(format); | ||
358 | free(dest); | ||
359 | } | ||
360 | niro | 1123 | #endif /* FEATURE_STAT_FORMAT */ |
361 | niro | 532 | |
362 | /* Stat the file system and print what we find. */ | ||
363 | niro | 816 | #if !ENABLE_FEATURE_STAT_FORMAT |
364 | #define do_statfs(filename, format) do_statfs(filename) | ||
365 | #endif | ||
366 | static bool do_statfs(const char *filename, const char *format) | ||
367 | niro | 532 | { |
368 | niro | 984 | struct statfs statfsbuf; |
369 | niro | 816 | #if !ENABLE_FEATURE_STAT_FORMAT |
370 | const char *format; | ||
371 | #endif | ||
372 | #if ENABLE_SELINUX | ||
373 | security_context_t scontext = NULL; | ||
374 | niro | 532 | |
375 | niro | 816 | if (option_mask32 & OPT_SELINUX) { |
376 | if ((option_mask32 & OPT_DEREFERENCE | ||
377 | ? lgetfilecon(filename, &scontext) | ||
378 | : getfilecon(filename, &scontext) | ||
379 | ) < 0 | ||
380 | ) { | ||
381 | bb_perror_msg(filename); | ||
382 | return 0; | ||
383 | } | ||
384 | } | ||
385 | #endif | ||
386 | niro | 532 | if (statfs(filename, &statfsbuf) != 0) { |
387 | niro | 984 | bb_perror_msg("can't read file system information for '%s'", filename); |
388 | niro | 532 | return 0; |
389 | } | ||
390 | |||
391 | niro | 816 | #if ENABLE_FEATURE_STAT_FORMAT |
392 | if (format == NULL) { | ||
393 | niro | 1123 | # if !ENABLE_SELINUX |
394 | niro | 816 | format = (option_mask32 & OPT_TERSE |
395 | niro | 532 | ? "%n %i %l %t %s %b %f %a %c %d\n" |
396 | : " File: \"%n\"\n" | ||
397 | " ID: %-8i Namelen: %-7l Type: %T\n" | ||
398 | "Block size: %-10s\n" | ||
399 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" | ||
400 | "Inodes: Total: %-10c Free: %d"); | ||
401 | niro | 1123 | # else |
402 | niro | 816 | format = (option_mask32 & OPT_TERSE |
403 | ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n": | ||
404 | "%n %i %l %t %s %b %f %a %c %d\n") | ||
405 | : (option_mask32 & OPT_SELINUX ? | ||
406 | " File: \"%n\"\n" | ||
407 | " ID: %-8i Namelen: %-7l Type: %T\n" | ||
408 | "Block size: %-10s\n" | ||
409 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" | ||
410 | "Inodes: Total: %-10c Free: %d" | ||
411 | " S_context: %C\n": | ||
412 | " File: \"%n\"\n" | ||
413 | " ID: %-8i Namelen: %-7l Type: %T\n" | ||
414 | "Block size: %-10s\n" | ||
415 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" | ||
416 | "Inodes: Total: %-10c Free: %d\n") | ||
417 | ); | ||
418 | niro | 1123 | # endif /* SELINUX */ |
419 | niro | 816 | } |
420 | niro | 984 | print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext)); |
421 | niro | 816 | #else /* FEATURE_STAT_FORMAT */ |
422 | format = (option_mask32 & OPT_TERSE | ||
423 | niro | 532 | ? "%s %llx %lu " |
424 | : " File: \"%s\"\n" | ||
425 | niro | 984 | " ID: %-8llx Namelen: %-7lu "); |
426 | niro | 532 | printf(format, |
427 | filename, | ||
428 | niro | 984 | get_f_fsid(&statfsbuf), |
429 | niro | 532 | statfsbuf.f_namelen); |
430 | |||
431 | niro | 816 | if (option_mask32 & OPT_TERSE) |
432 | niro | 1123 | printf("%lx ", (unsigned long) statfsbuf.f_type); |
433 | niro | 532 | else |
434 | printf("Type: %s\n", human_fstype(statfsbuf.f_type)); | ||
435 | |||
436 | niro | 1123 | # if !ENABLE_SELINUX |
437 | niro | 816 | format = (option_mask32 & OPT_TERSE |
438 | niro | 1123 | ? "%lu %llu %llu %llu %llu %llu\n" |
439 | niro | 532 | : "Block size: %-10lu\n" |
440 | niro | 1123 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" |
441 | "Inodes: Total: %-10llu Free: %llu\n"); | ||
442 | niro | 532 | printf(format, |
443 | niro | 1123 | (unsigned long) statfsbuf.f_bsize, |
444 | (unsigned long long) statfsbuf.f_blocks, | ||
445 | (unsigned long long) statfsbuf.f_bfree, | ||
446 | (unsigned long long) statfsbuf.f_bavail, | ||
447 | (unsigned long long) statfsbuf.f_files, | ||
448 | (unsigned long long) statfsbuf.f_ffree); | ||
449 | # else | ||
450 | niro | 816 | format = (option_mask32 & OPT_TERSE |
451 | niro | 1123 | ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n") |
452 | : (option_mask32 & OPT_SELINUX | ||
453 | ? "Block size: %-10lu\n" | ||
454 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" | ||
455 | "Inodes: Total: %-10llu Free: %llu" | ||
456 | "S_context: %C\n" | ||
457 | : "Block size: %-10lu\n" | ||
458 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" | ||
459 | "Inodes: Total: %-10llu Free: %llu\n" | ||
460 | ) | ||
461 | ); | ||
462 | niro | 816 | printf(format, |
463 | niro | 1123 | (unsigned long) statfsbuf.f_bsize, |
464 | (unsigned long long) statfsbuf.f_blocks, | ||
465 | (unsigned long long) statfsbuf.f_bfree, | ||
466 | (unsigned long long) statfsbuf.f_bavail, | ||
467 | (unsigned long long) statfsbuf.f_files, | ||
468 | (unsigned long long) statfsbuf.f_ffree, | ||
469 | niro | 816 | scontext); |
470 | |||
471 | if (scontext) | ||
472 | freecon(scontext); | ||
473 | niro | 1123 | # endif |
474 | niro | 816 | #endif /* FEATURE_STAT_FORMAT */ |
475 | niro | 532 | return 1; |
476 | } | ||
477 | |||
478 | /* stat the file and print what we find */ | ||
479 | niro | 816 | #if !ENABLE_FEATURE_STAT_FORMAT |
480 | #define do_stat(filename, format) do_stat(filename) | ||
481 | #endif | ||
482 | static bool do_stat(const char *filename, const char *format) | ||
483 | niro | 532 | { |
484 | struct stat statbuf; | ||
485 | niro | 816 | #if ENABLE_SELINUX |
486 | security_context_t scontext = NULL; | ||
487 | niro | 532 | |
488 | niro | 816 | if (option_mask32 & OPT_SELINUX) { |
489 | if ((option_mask32 & OPT_DEREFERENCE | ||
490 | ? lgetfilecon(filename, &scontext) | ||
491 | : getfilecon(filename, &scontext) | ||
492 | ) < 0 | ||
493 | ) { | ||
494 | bb_perror_msg(filename); | ||
495 | return 0; | ||
496 | } | ||
497 | } | ||
498 | #endif | ||
499 | if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) { | ||
500 | niro | 984 | bb_perror_msg("can't stat '%s'", filename); |
501 | niro | 532 | return 0; |
502 | } | ||
503 | |||
504 | niro | 816 | #if ENABLE_FEATURE_STAT_FORMAT |
505 | niro | 532 | if (format == NULL) { |
506 | niro | 1123 | # if !ENABLE_SELINUX |
507 | niro | 816 | if (option_mask32 & OPT_TERSE) { |
508 | niro | 532 | format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o"; |
509 | } else { | ||
510 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { | ||
511 | format = | ||
512 | niro | 1123 | " File: %N\n" |
513 | niro | 532 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
514 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" | ||
515 | " Device type: %t,%T\n" | ||
516 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
517 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; | ||
518 | } else { | ||
519 | format = | ||
520 | niro | 1123 | " File: %N\n" |
521 | niro | 532 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
522 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" | ||
523 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
524 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; | ||
525 | } | ||
526 | } | ||
527 | niro | 1123 | # else |
528 | niro | 816 | if (option_mask32 & OPT_TERSE) { |
529 | format = (option_mask32 & OPT_SELINUX ? | ||
530 | "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n": | ||
531 | "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"); | ||
532 | } else { | ||
533 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { | ||
534 | format = (option_mask32 & OPT_SELINUX ? | ||
535 | niro | 1123 | " File: %N\n" |
536 | niro | 816 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
537 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" | ||
538 | " Device type: %t,%T\n" | ||
539 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
540 | " S_Context: %C\n" | ||
541 | "Access: %x\n" "Modify: %y\n" "Change: %z\n": | ||
542 | niro | 1123 | " File: %N\n" |
543 | niro | 816 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
544 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" | ||
545 | " Device type: %t,%T\n" | ||
546 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
547 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"); | ||
548 | } else { | ||
549 | format = (option_mask32 & OPT_SELINUX ? | ||
550 | niro | 1123 | " File: %N\n" |
551 | niro | 816 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
552 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" | ||
553 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
554 | "S_Context: %C\n" | ||
555 | "Access: %x\n" "Modify: %y\n" "Change: %z\n": | ||
556 | niro | 1123 | " File: %N\n" |
557 | niro | 816 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
558 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" | ||
559 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" | ||
560 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"); | ||
561 | } | ||
562 | } | ||
563 | niro | 1123 | # endif |
564 | niro | 532 | } |
565 | niro | 984 | print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext)); |
566 | niro | 816 | #else /* FEATURE_STAT_FORMAT */ |
567 | if (option_mask32 & OPT_TERSE) { | ||
568 | niro | 1123 | printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu" |
569 | niro | 984 | IF_NOT_SELINUX("\n"), |
570 | niro | 532 | filename, |
571 | niro | 1123 | (unsigned long long) statbuf.st_size, |
572 | (unsigned long long) statbuf.st_blocks, | ||
573 | niro | 816 | (unsigned long) statbuf.st_mode, |
574 | (unsigned long) statbuf.st_uid, | ||
575 | (unsigned long) statbuf.st_gid, | ||
576 | niro | 1123 | (unsigned long long) statbuf.st_dev, |
577 | (unsigned long long) statbuf.st_ino, | ||
578 | niro | 816 | (unsigned long) statbuf.st_nlink, |
579 | (unsigned long) major(statbuf.st_rdev), | ||
580 | (unsigned long) minor(statbuf.st_rdev), | ||
581 | (unsigned long) statbuf.st_atime, | ||
582 | (unsigned long) statbuf.st_mtime, | ||
583 | (unsigned long) statbuf.st_ctime, | ||
584 | (unsigned long) statbuf.st_blksize | ||
585 | niro | 532 | ); |
586 | niro | 1123 | # if ENABLE_SELINUX |
587 | niro | 816 | if (option_mask32 & OPT_SELINUX) |
588 | printf(" %lc\n", *scontext); | ||
589 | else | ||
590 | bb_putchar('\n'); | ||
591 | niro | 1123 | # endif |
592 | niro | 532 | } else { |
593 | char *linkname = NULL; | ||
594 | |||
595 | struct passwd *pw_ent; | ||
596 | struct group *gw_ent; | ||
597 | setgrent(); | ||
598 | gw_ent = getgrgid(statbuf.st_gid); | ||
599 | setpwent(); | ||
600 | pw_ent = getpwuid(statbuf.st_uid); | ||
601 | |||
602 | if (S_ISLNK(statbuf.st_mode)) | ||
603 | niro | 816 | linkname = xmalloc_readlink_or_warn(filename); |
604 | niro | 532 | if (linkname) |
605 | niro | 1123 | printf(" File: '%s' -> '%s'\n", filename, linkname); |
606 | niro | 532 | else |
607 | niro | 1123 | printf(" File: '%s'\n", filename); |
608 | niro | 532 | |
609 | niro | 1123 | printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n" |
610 | "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu", | ||
611 | (unsigned long long) statbuf.st_size, | ||
612 | (unsigned long long) statbuf.st_blocks, | ||
613 | niro | 816 | (unsigned long) statbuf.st_blksize, |
614 | niro | 532 | file_type(&statbuf), |
615 | niro | 1123 | (unsigned long long) statbuf.st_dev, |
616 | (unsigned long long) statbuf.st_dev, | ||
617 | (unsigned long long) statbuf.st_ino, | ||
618 | niro | 816 | (unsigned long) statbuf.st_nlink); |
619 | niro | 532 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) |
620 | printf(" Device type: %lx,%lx\n", | ||
621 | niro | 816 | (unsigned long) major(statbuf.st_rdev), |
622 | (unsigned long) minor(statbuf.st_rdev)); | ||
623 | niro | 532 | else |
624 | niro | 816 | bb_putchar('\n'); |
625 | printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n", | ||
626 | (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), | ||
627 | niro | 532 | bb_mode_string(statbuf.st_mode), |
628 | niro | 816 | (unsigned long) statbuf.st_uid, |
629 | (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN", | ||
630 | (unsigned long) statbuf.st_gid, | ||
631 | (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN"); | ||
632 | niro | 1123 | # if ENABLE_SELINUX |
633 | niro | 816 | printf(" S_Context: %lc\n", *scontext); |
634 | niro | 1123 | # endif |
635 | niro | 816 | printf("Access: %s\n" "Modify: %s\n" "Change: %s\n", |
636 | niro | 532 | human_time(statbuf.st_atime), |
637 | human_time(statbuf.st_mtime), | ||
638 | human_time(statbuf.st_ctime)); | ||
639 | } | ||
640 | niro | 816 | #endif /* FEATURE_STAT_FORMAT */ |
641 | niro | 532 | return 1; |
642 | } | ||
643 | |||
644 | niro | 816 | int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
645 | niro | 984 | int stat_main(int argc UNUSED_PARAM, char **argv) |
646 | niro | 532 | { |
647 | niro | 984 | IF_FEATURE_STAT_FORMAT(char *format = NULL;) |
648 | niro | 532 | int i; |
649 | niro | 984 | int ok; |
650 | unsigned opts; | ||
651 | niro | 816 | statfunc_ptr statfunc = do_stat; |
652 | niro | 532 | |
653 | niro | 984 | opt_complementary = "-1"; /* min one arg */ |
654 | opts = getopt32(argv, "ftL" | ||
655 | IF_SELINUX("Z") | ||
656 | IF_FEATURE_STAT_FORMAT("c:", &format) | ||
657 | niro | 532 | ); |
658 | niro | 984 | if (opts & OPT_FILESYS) /* -f */ |
659 | niro | 532 | statfunc = do_statfs; |
660 | niro | 816 | #if ENABLE_SELINUX |
661 | niro | 984 | if (opts & OPT_SELINUX) { |
662 | niro | 816 | selinux_or_die(); |
663 | } | ||
664 | niro | 984 | #endif |
665 | ok = 1; | ||
666 | argv += optind; | ||
667 | for (i = 0; argv[i]; ++i) | ||
668 | ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format)); | ||
669 | niro | 532 | |
670 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); | ||
671 | } |