blob: 31dd6624eccdfb4ba9192f00dc691d7602ab7751 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00002/*
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 *
9 * Written by Michael Meskes
10 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
11 *
Bernhard Reutner-Fischere11a01c2006-04-05 17:19:37 +000012 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000013 */
14
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000015#include "busybox.h"
16
17/* vars to control behavior */
Mike Frysingerf06c4942005-04-24 03:53:12 +000018#define OPT_TERSE 2
Bernhard Reutner-Fischer18260d52006-04-18 14:17:49 +000019#define OPT_DEREFERENCE 4
Mike Frysingerf06c4942005-04-24 03:53:12 +000020static long flags;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000021
22static char const *file_type(struct stat const *st)
23{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000024 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000025 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000027 * returned string must start with a consonant.
28 */
29 if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
30 if (S_ISDIR(st->st_mode)) return "directory";
31 if (S_ISBLK(st->st_mode)) return "block special file";
32 if (S_ISCHR(st->st_mode)) return "character special file";
33 if (S_ISFIFO(st->st_mode)) return "fifo";
34 if (S_ISLNK(st->st_mode)) return "symbolic link";
35 if (S_ISSOCK(st->st_mode)) return "socket";
36 if (S_TYPEISMQ(st)) return "message queue";
37 if (S_TYPEISSEM(st)) return "semaphore";
38 if (S_TYPEISSHM(st)) return "shared memory object";
39#ifdef S_TYPEISTMO
40 if (S_TYPEISTMO(st)) return "typed memory object";
41#endif
42 return "weird file";
43}
44
45static char const *human_time(time_t t)
46{
Denis Vlasenko5d148e22006-11-21 00:12:09 +000047 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000048 static char *str;
49 str = ctime(&t);
50 str[strlen(str)-1] = '\0';
51 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +000052 */
53 /* coreutils 6.3 compat: */
54 static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
55 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
56 return buf;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000057}
58
59/* Return the type of the specified file system.
60 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
61 * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
62 * Still others have neither and have to get by with f_type (Linux).
63 */
64static char const *human_fstype(long f_type)
65{
Mike Frysinger408ae212005-04-24 04:11:44 +000066 int i;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000067 static const struct types {
Mike Frysinger408ae212005-04-24 04:11:44 +000068 long type;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000069 const char *fs;
Mike Frysinger408ae212005-04-24 04:11:44 +000070 } humantypes[] = {
71 { 0xADFF, "affs" },
72 { 0x1Cd1, "devpts" },
73 { 0x137D, "ext" },
74 { 0xEF51, "ext2" },
75 { 0xEF53, "ext2/ext3" },
76 { 0x3153464a, "jfs" },
77 { 0x58465342, "xfs" },
78 { 0xF995E849, "hpfs" },
79 { 0x9660, "isofs" },
80 { 0x4000, "isofs" },
81 { 0x4004, "isofs" },
82 { 0x137F, "minix" },
83 { 0x138F, "minix (30 char.)" },
84 { 0x2468, "minix v2" },
85 { 0x2478, "minix v2 (30 char.)" },
86 { 0x4d44, "msdos" },
87 { 0x4006, "fat" },
88 { 0x564c, "novell" },
89 { 0x6969, "nfs" },
90 { 0x9fa0, "proc" },
91 { 0x517B, "smb" },
92 { 0x012FF7B4, "xenix" },
93 { 0x012FF7B5, "sysv4" },
94 { 0x012FF7B6, "sysv2" },
95 { 0x012FF7B7, "coh" },
96 { 0x00011954, "ufs" },
97 { 0x012FD16D, "xia" },
98 { 0x5346544e, "ntfs" },
99 { 0x1021994, "tmpfs" },
100 { 0x52654973, "reiserfs" },
101 { 0x28cd3d45, "cramfs" },
102 { 0x7275, "romfs" },
103 { 0x858458f6, "romfs" },
104 { 0x73717368, "squashfs" },
105 { 0x62656572, "sysfs" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000106 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000107 };
108 for (i=0; humantypes[i].type; ++i)
109 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000110 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000111 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000112}
113
114#ifdef CONFIG_FEATURE_STAT_FORMAT
115/* print statfs info */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000116static void print_statfs(char *pformat, size_t buf_len, char m,
117 char const *filename, void const *data)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000118{
119 struct statfs const *statfsbuf = data;
120
121 switch (m) {
122 case 'n':
123 strncat(pformat, "s", buf_len);
124 printf(pformat, filename);
125 break;
126 case 'i':
127 strncat(pformat, "Lx", buf_len);
128 printf(pformat, statfsbuf->f_fsid);
129 break;
130 case 'l':
131 strncat(pformat, "lu", buf_len);
132 printf(pformat, statfsbuf->f_namelen);
133 break;
134 case 't':
135 strncat(pformat, "lx", buf_len);
136 printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */
137 break;
138 case 'T':
139 strncat(pformat, "s", buf_len);
140 printf(pformat, human_fstype(statfsbuf->f_type));
141 break;
142 case 'b':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000143 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000144 printf(pformat, (intmax_t) (statfsbuf->f_blocks));
145 break;
146 case 'f':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000147 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000148 printf(pformat, (intmax_t) (statfsbuf->f_bfree));
149 break;
150 case 'a':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000151 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000152 printf(pformat, (intmax_t) (statfsbuf->f_bavail));
153 break;
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000154 case 'S':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000155 case 's':
156 strncat(pformat, "lu", buf_len);
157 printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
158 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000159 case 'c':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000160 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000161 printf(pformat, (intmax_t) (statfsbuf->f_files));
162 break;
163 case 'd':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000164 strncat(pformat, "jd", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000165 printf(pformat, (intmax_t) (statfsbuf->f_ffree));
166 break;
167 default:
168 strncat(pformat, "c", buf_len);
169 printf(pformat, m);
170 break;
171 }
172}
173
174/* print stat info */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000175static void print_stat(char *pformat, size_t buf_len, char m,
176 char const *filename, void const *data)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000177{
178#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
179 struct stat *statbuf = (struct stat *) data;
180 struct passwd *pw_ent;
181 struct group *gw_ent;
182
183 switch (m) {
184 case 'n':
185 strncat(pformat, "s", buf_len);
186 printf(pformat, filename);
187 break;
188 case 'N':
189 strncat(pformat, "s", buf_len);
190 if (S_ISLNK(statbuf->st_mode)) {
191 char *linkname = xreadlink(filename);
192 if (linkname == NULL) {
193 bb_perror_msg("cannot read symbolic link '%s'", filename);
194 return;
195 }
196 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
197 printf(pformat, filename);
198 printf(" -> ");
199 printf(pformat, linkname);
200 } else {
201 printf(pformat, filename);
202 }
203 break;
204 case 'd':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000205 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000206 printf(pformat, (uintmax_t) statbuf->st_dev);
207 break;
208 case 'D':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000209 strncat(pformat, "jx", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000210 printf(pformat, (uintmax_t) statbuf->st_dev);
211 break;
212 case 'i':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000213 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000214 printf(pformat, (uintmax_t) statbuf->st_ino);
215 break;
216 case 'a':
217 strncat(pformat, "lo", buf_len);
218 printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
219 break;
220 case 'A':
221 strncat(pformat, "s", buf_len);
222 printf(pformat, bb_mode_string(statbuf->st_mode));
223 break;
224 case 'f':
225 strncat(pformat, "lx", buf_len);
226 printf(pformat, (unsigned long int) statbuf->st_mode);
227 break;
228 case 'F':
229 strncat(pformat, "s", buf_len);
230 printf(pformat, file_type(statbuf));
231 break;
232 case 'h':
233 strncat(pformat, "lu", buf_len);
234 printf(pformat, (unsigned long int) statbuf->st_nlink);
235 break;
236 case 'u':
237 strncat(pformat, "lu", buf_len);
238 printf(pformat, (unsigned long int) statbuf->st_uid);
239 break;
240 case 'U':
241 strncat(pformat, "s", buf_len);
242 setpwent();
243 pw_ent = getpwuid(statbuf->st_uid);
244 printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
245 break;
246 case 'g':
247 strncat(pformat, "lu", buf_len);
248 printf(pformat, (unsigned long int) statbuf->st_gid);
249 break;
250 case 'G':
251 strncat(pformat, "s", buf_len);
252 setgrent();
253 gw_ent = getgrgid(statbuf->st_gid);
254 printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
255 break;
256 case 't':
257 strncat(pformat, "lx", buf_len);
258 printf(pformat, (unsigned long int) major(statbuf->st_rdev));
259 break;
260 case 'T':
261 strncat(pformat, "lx", buf_len);
262 printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
263 break;
264 case 's':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000265 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000266 printf(pformat, (uintmax_t) (statbuf->st_size));
267 break;
268 case 'B':
269 strncat(pformat, "lu", buf_len);
270 printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
271 break;
272 case 'b':
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000273 strncat(pformat, "ju", buf_len);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000274 printf(pformat, (uintmax_t) statbuf->st_blocks);
275 break;
276 case 'o':
277 strncat(pformat, "lu", buf_len);
278 printf(pformat, (unsigned long int) statbuf->st_blksize);
279 break;
280 case 'x':
281 strncat(pformat, "s", buf_len);
282 printf(pformat, human_time(statbuf->st_atime));
283 break;
284 case 'X':
285 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
286 printf(pformat, (unsigned long int) statbuf->st_atime);
287 break;
288 case 'y':
289 strncat(pformat, "s", buf_len);
290 printf(pformat, human_time(statbuf->st_mtime));
291 break;
292 case 'Y':
293 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
294 printf(pformat, (unsigned long int) statbuf->st_mtime);
295 break;
296 case 'z':
297 strncat(pformat, "s", buf_len);
298 printf(pformat, human_time(statbuf->st_ctime));
299 break;
300 case 'Z':
301 strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
302 printf(pformat, (unsigned long int) statbuf->st_ctime);
303 break;
304 default:
305 strncat(pformat, "c", buf_len);
306 printf(pformat, m);
307 break;
308 }
309}
310
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000311static void print_it(char const *masterformat, char const *filename,
312 void (*print_func) (char *, size_t, char, char const *, void const *),
313 void const *data)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000314{
315 char *b;
316
317 /* create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000318 char *format = xstrdup(masterformat);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000319
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000320 /* Add 2 to accomodate our conversion of the stat '%s' format string
321 * to the printf '%llu' one. */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000322 size_t n_alloc = strlen(format) + 2 + 1;
323 char *dest = xmalloc(n_alloc);
324
325 b = format;
326 while (b) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000327 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000328 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000329 if (!p) {
330 /* coreutils 6.3 always print <cr> at the end */
331 /*fputs(b, stdout);*/
332 puts(b);
333 break;
334 }
335 *p++ = '\0';
336 fputs(b, stdout);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000337
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000338 len = strspn(p, "#-+.I 0123456789");
339 dest[0] = '%';
340 memcpy(dest + 1, p, len);
341 dest[1 + len] = 0;
342 p += len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000343
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000344 b = p + 1;
345 switch (*p) {
346 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000347 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000348 /* fall through */
349 case '%':
350 putchar('%');
351 break;
352 default:
353 print_func(dest, n_alloc, *p, filename, data);
354 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000355 }
356 }
357
358 free(format);
359 free(dest);
360}
361#endif
362
363/* Stat the file system and print what we find. */
364static int do_statfs(char const *filename, char const *format)
365{
366 struct statfs statfsbuf;
367
368 if (statfs(filename, &statfsbuf) != 0) {
369 bb_perror_msg("cannot read file system information for '%s'", filename);
370 return 0;
371 }
372
373#ifdef CONFIG_FEATURE_STAT_FORMAT
374 if (format == NULL)
Mike Frysingerf06c4942005-04-24 03:53:12 +0000375 format = (flags & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000376 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000377 : " File: \"%n\"\n"
378 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000379 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000380 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000381 "Inodes: Total: %-10c Free: %d");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000382 print_it(format, filename, print_statfs, &statfsbuf);
383#else
384
Mike Frysingerf06c4942005-04-24 03:53:12 +0000385 format = (flags & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000386 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000387 : " File: \"%s\"\n"
388 " ID: %-8Lx Namelen: %-7lu ");
389 printf(format,
390 filename,
391 statfsbuf.f_fsid,
392 statfsbuf.f_namelen);
393
Mike Frysingerf06c4942005-04-24 03:53:12 +0000394 if (flags & OPT_TERSE)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000395 printf("%lx ", (unsigned long int) (statfsbuf.f_type));
396 else
397 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
398
Mike Frysingerf06c4942005-04-24 03:53:12 +0000399 format = (flags & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000400 ? "%lu %ld %ld %ld %ld %ld\n"
401 : "Block size: %-10lu\n"
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000402 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
403 "Inodes: Total: %-10jd Free: %jd\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000404 printf(format,
405 (unsigned long int) (statfsbuf.f_bsize),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000406 (intmax_t) (statfsbuf.f_blocks),
407 (intmax_t) (statfsbuf.f_bfree),
408 (intmax_t) (statfsbuf.f_bavail),
409 (intmax_t) (statfsbuf.f_files),
410 (intmax_t) (statfsbuf.f_ffree));
411#endif
412
413 return 1;
414}
415
416/* stat the file and print what we find */
417static int do_stat(char const *filename, char const *format)
418{
419 struct stat statbuf;
420
Bernhard Reutner-Fischer18260d52006-04-18 14:17:49 +0000421 if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000422 bb_perror_msg("cannot stat '%s'", filename);
423 return 0;
424 }
425
426#ifdef CONFIG_FEATURE_STAT_FORMAT
427 if (format == NULL) {
Mike Frysingerf06c4942005-04-24 03:53:12 +0000428 if (flags & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000429 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000430 } else {
431 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
432 format =
433 " File: \"%N\"\n"
434 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
435 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
436 " Device type: %t,%T\n"
437 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
438 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
439 } else {
440 format =
441 " File: \"%N\"\n"
442 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
443 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
444 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
445 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
446 }
447 }
448 }
449 print_it(format, filename, print_stat, &statbuf);
450#else
Mike Frysingerf06c4942005-04-24 03:53:12 +0000451 if (flags & OPT_TERSE) {
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000452 printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000453 filename,
454 (uintmax_t) (statbuf.st_size),
455 (uintmax_t) statbuf.st_blocks,
456 (unsigned long int) statbuf.st_mode,
457 (unsigned long int) statbuf.st_uid,
458 (unsigned long int) statbuf.st_gid,
459 (uintmax_t) statbuf.st_dev,
460 (uintmax_t) statbuf.st_ino,
461 (unsigned long int) statbuf.st_nlink,
462 (unsigned long int) major(statbuf.st_rdev),
463 (unsigned long int) minor(statbuf.st_rdev),
464 (unsigned long int) statbuf.st_atime,
465 (unsigned long int) statbuf.st_mtime,
466 (unsigned long int) statbuf.st_ctime,
467 (unsigned long int) statbuf.st_blksize
468 );
469 } else {
470 char *linkname = NULL;
471
472 struct passwd *pw_ent;
473 struct group *gw_ent;
474 setgrent();
475 gw_ent = getgrgid(statbuf.st_gid);
476 setpwent();
477 pw_ent = getpwuid(statbuf.st_uid);
478
479 if (S_ISLNK(statbuf.st_mode))
480 linkname = xreadlink(filename);
481 if (linkname)
482 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
483 else
484 printf(" File: \"%s\"\n", filename);
485
Bernhard Reutner-Fischerfc5f3182006-04-12 08:03:11 +0000486 printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
487 "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000488 (uintmax_t) (statbuf.st_size),
489 (uintmax_t) statbuf.st_blocks,
490 (unsigned long int) statbuf.st_blksize,
491 file_type(&statbuf),
492 (uintmax_t) statbuf.st_dev,
493 (uintmax_t) statbuf.st_dev,
494 (uintmax_t) statbuf.st_ino,
495 (unsigned long int) statbuf.st_nlink);
496 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
497 printf(" Device type: %lx,%lx\n",
498 (unsigned long int) major(statbuf.st_rdev),
499 (unsigned long int) minor(statbuf.st_rdev));
500 else
501 putchar('\n');
502 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n"
503 "Access: %s\n" "Modify: %s\n" "Change: %s\n",
504 (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
505 bb_mode_string(statbuf.st_mode),
506 (unsigned long int) statbuf.st_uid,
507 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
508 (unsigned long int) statbuf.st_gid,
509 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
510 human_time(statbuf.st_atime),
511 human_time(statbuf.st_mtime),
512 human_time(statbuf.st_ctime));
513 }
514#endif
515 return 1;
516}
517
518int stat_main(int argc, char **argv)
519{
520 int i;
521 char *format = NULL;
522 int ok = 1;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000523 int (*statfunc)(char const *, char const *) = do_stat;
524
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000525 flags = getopt32(argc, argv, "ftL"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000526 USE_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000527 );
528
529 if (flags & 1) /* -f */
530 statfunc = do_statfs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000531 if (argc == optind) /* files */
532 bb_show_usage();
533
534 for (i = optind; i < argc; ++i)
535 ok &= statfunc(argv[i], format);
536
537 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
538}