blob: 57f1f145a4aa50ea8110fe83cdbaf15a0997f0cd [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>
Denis Vlasenko49622d72007-03-10 16:58:49 +00008 * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
Mike Frysinger9b5f71e2005-04-23 06:26:38 +00009 *
10 * Written by Michael Meskes
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 *
Bernhard Reutner-Fischere11a01c2006-04-05 17:19:37 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000014 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000016
Denis Vlasenko91e52032007-10-05 20:31:23 +000017#define OPT_FILESYS (1 << 0)
18#define OPT_TERSE (1 << 1)
19#define OPT_DEREFERENCE (1 << 2)
20#define OPT_SELINUX (1 << 3)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000021
Denis Vlasenko85c24712008-03-17 09:04:04 +000022#if ENABLE_FEATURE_STAT_FORMAT
23typedef bool (*statfunc_ptr)(const char *, const char *);
24#else
25typedef bool (*statfunc_ptr)(const char *);
26#endif
27
Denis Vlasenko91e52032007-10-05 20:31:23 +000028static const char *file_type(const struct stat *st)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000029{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000031 * for some of these formats.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032 * To keep diagnostics grammatical in English, the
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000033 * 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
Denis Vlasenko91e52032007-10-05 20:31:23 +000051static const char *human_time(time_t t)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000052{
Denis Vlasenko5d148e22006-11-21 00:12:09 +000053 /* Old
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000054 static char *str;
55 str = ctime(&t);
56 str[strlen(str)-1] = '\0';
57 return str;
Denis Vlasenko5d148e22006-11-21 00:12:09 +000058 */
59 /* coreutils 6.3 compat: */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +000060
Denis Vlasenko91e52032007-10-05 20:31:23 +000061 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
62#define buf bb_common_bufsiz1
63
Denis Vlasenko5d148e22006-11-21 00:12:09 +000064 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
65 return buf;
Denis Vlasenko91e52032007-10-05 20:31:23 +000066#undef buf
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000067}
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 */
Denis Vlasenko91e52032007-10-05 20:31:23 +000074static const char *human_fstype(uint32_t f_type)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +000075{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000076 static const struct types {
Denis Vlasenko91e52032007-10-05 20:31:23 +000077 uint32_t type;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000078 const char *const fs;
Mike Frysinger408ae212005-04-24 04:11:44 +000079 } 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" },
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000115 { 0, "UNKNOWN" }
Mike Frysinger408ae212005-04-24 04:11:44 +0000116 };
Denis Vlasenko91e52032007-10-05 20:31:23 +0000117
118 int i;
119
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000120 for (i = 0; humantypes[i].type; ++i)
Mike Frysinger408ae212005-04-24 04:11:44 +0000121 if (humantypes[i].type == f_type)
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000122 break;
Mike Frysinger408ae212005-04-24 04:11:44 +0000123 return humantypes[i].fs;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000124}
125
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000126/* "man statfs" says that statfsbuf->f_fsid is a mess */
127/* coreutils treats it as an array of ints, most significant first */
128static 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
Denis Vlasenko86c285d2008-11-13 21:53:32 +0000140#if ENABLE_FEATURE_STAT_FORMAT
141static void strcatc(char *str, char c)
142{
143 int len = strlen(str);
144 str[len++] = c;
145 str[len] = '\0';
146}
147
148static void printfs(char *pformat, const char *msg)
149{
150 strcatc(pformat, 's');
151 printf(pformat, msg);
152}
153
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000154/* print statfs info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100155static void FAST_FUNC print_statfs(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000156 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000157 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000158{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000159 const struct statfs *statfsbuf = data;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000160 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000161 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000162 } else if (m == 'i') {
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000163 strcat(pformat, "llx");
164 printf(pformat, get_f_fsid(statfsbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000165 } else if (m == 'l') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000166 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100167 printf(pformat, (unsigned long) statfsbuf->f_namelen);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000168 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000169 strcat(pformat, "lx");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100170 printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000171 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000172 printfs(pformat, human_fstype(statfsbuf->f_type));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000173 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100174 strcat(pformat, "llu");
175 printf(pformat, (unsigned long long) statfsbuf->f_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000176 } else if (m == 'f') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100177 strcat(pformat, "llu");
178 printf(pformat, (unsigned long long) statfsbuf->f_bfree);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000179 } else if (m == 'a') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100180 strcat(pformat, "llu");
181 printf(pformat, (unsigned long long) statfsbuf->f_bavail);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000182 } else if (m == 's' || m == 'S') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000183 strcat(pformat, "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100184 printf(pformat, (unsigned long) statfsbuf->f_bsize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000185 } else if (m == 'c') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100186 strcat(pformat, "llu");
187 printf(pformat, (unsigned long long) statfsbuf->f_files);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000188 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100189 strcat(pformat, "llu");
190 printf(pformat, (unsigned long long) statfsbuf->f_ffree);
191# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000192 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000193 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100194# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000195 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000196 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000197 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000198 }
199}
200
201/* print stat info */
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100202static void FAST_FUNC print_stat(char *pformat, const char m,
Denis Vlasenko91e52032007-10-05 20:31:23 +0000203 const char *const filename, const void *data
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000204 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000205{
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
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000211 if (m == 'n') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000212 printfs(pformat, filename);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000213 } else if (m == 'N') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000214 strcatc(pformat, 's');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000215 if (S_ISLNK(statbuf->st_mode)) {
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000216 char *linkname = xmalloc_readlink_or_warn(filename);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000217 if (linkname == NULL)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000218 return;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000219 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
220 printf(pformat, filename);
221 printf(" -> ");
222 printf(pformat, linkname);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000223 free(linkname);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000224 } else {
225 printf(pformat, filename);
226 }
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000227 } else if (m == 'd') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100228 strcat(pformat, "llu");
229 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000230 } else if (m == 'D') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100231 strcat(pformat, "llx");
232 printf(pformat, (unsigned long long) statbuf->st_dev);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000233 } else if (m == 'i') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100234 strcat(pformat, "llu");
235 printf(pformat, (unsigned long long) statbuf->st_ino);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000236 } else if (m == 'a') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000237 strcat(pformat, "lo");
Denis Vlasenko87468852007-04-13 23:22:00 +0000238 printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000239 } else if (m == 'A') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000240 printfs(pformat, bb_mode_string(statbuf->st_mode));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000241 } else if (m == 'f') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000242 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000243 printf(pformat, (unsigned long) statbuf->st_mode);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000244 } else if (m == 'F') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000245 printfs(pformat, file_type(statbuf));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000246 } else if (m == 'h') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000247 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000248 printf(pformat, (unsigned long) statbuf->st_nlink);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000249 } else if (m == 'u') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000250 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000251 printf(pformat, (unsigned long) statbuf->st_uid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000252 } else if (m == 'U') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000253 setpwent();
254 pw_ent = getpwuid(statbuf->st_uid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000255 printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000256 } else if (m == 'g') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000257 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000258 printf(pformat, (unsigned long) statbuf->st_gid);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000259 } else if (m == 'G') {
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000260 setgrent();
261 gw_ent = getgrgid(statbuf->st_gid);
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000262 printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000263 } else if (m == 't') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000264 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000265 printf(pformat, (unsigned long) major(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000266 } else if (m == 'T') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000267 strcat(pformat, "lx");
Denis Vlasenko87468852007-04-13 23:22:00 +0000268 printf(pformat, (unsigned long) minor(statbuf->st_rdev));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000269 } else if (m == 's') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100270 strcat(pformat, "llu");
271 printf(pformat, (unsigned long long) statbuf->st_size);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000272 } else if (m == 'B') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000273 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000274 printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000275 } else if (m == 'b') {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100276 strcat(pformat, "llu");
277 printf(pformat, (unsigned long long) statbuf->st_blocks);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000278 } else if (m == 'o') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000279 strcat(pformat, "lu");
Denis Vlasenko87468852007-04-13 23:22:00 +0000280 printf(pformat, (unsigned long) statbuf->st_blksize);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000281 } else if (m == 'x') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000282 printfs(pformat, human_time(statbuf->st_atime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000283 } else if (m == 'X') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000284 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100285 /* note: (unsigned long) would be wrong:
286 * imagine (unsigned long64)int32 */
287 printf(pformat, (long) statbuf->st_atime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000288 } else if (m == 'y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000289 printfs(pformat, human_time(statbuf->st_mtime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000290 } else if (m == 'Y') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000291 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100292 printf(pformat, (long) statbuf->st_mtime);
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000293 } else if (m == 'z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000294 printfs(pformat, human_time(statbuf->st_ctime));
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000295 } else if (m == 'Z') {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000296 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100297 printf(pformat, (long) statbuf->st_ctime);
298# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000299 } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000300 printfs(pformat, scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100301# endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000302 } else {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000303 strcatc(pformat, 'c');
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000304 printf(pformat, m);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000305 }
306}
307
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100308static void print_it(const char *masterformat,
309 const char *filename,
310 void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
Denis Vlasenko91e52032007-10-05 20:31:23 +0000311 const void *data
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100312 IF_SELINUX(, security_context_t scontext))
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000313{
Denis Vlasenko91e52032007-10-05 20:31:23 +0000314 /* Create a working copy of the format string */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000315 char *format = xstrdup(masterformat);
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000316 /* Add 2 to accomodate our conversion of the stat '%s' format string
317 * to the printf '%llu' one. */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000318 char *dest = xmalloc(strlen(format) + 2 + 1);
319 char *b;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000320
321 b = format;
322 while (b) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000323 size_t len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000324 char *p = strchr(b, '%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000325 if (!p) {
Denis Vlasenko91e52032007-10-05 20:31:23 +0000326 /* coreutils 6.3 always prints <cr> at the end */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000327 /*fputs(b, stdout);*/
328 puts(b);
329 break;
330 }
331 *p++ = '\0';
332 fputs(b, stdout);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000333
Denis Vlasenko91e52032007-10-05 20:31:23 +0000334 /* dest = "%<modifiers>" */
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000335 len = strspn(p, "#-+.I 0123456789");
336 dest[0] = '%';
337 memcpy(dest + 1, p, len);
Denis Vlasenko91e52032007-10-05 20:31:23 +0000338 dest[1 + len] = '\0';
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000339 p += len;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000340
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000341 b = p + 1;
342 switch (*p) {
343 case '\0':
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000344 b = NULL;
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000345 /* fall through */
346 case '%':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000347 bb_putchar('%');
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000348 break;
349 default:
Denis Vlasenko91e52032007-10-05 20:31:23 +0000350 /* Completes "%<modifiers>" with specifier and printfs */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000351 print_func(dest, *p, filename, data IF_SELINUX(,scontext));
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000352 break;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000353 }
354 }
355
356 free(format);
357 free(dest);
358}
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100359#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000360
361/* Stat the file system and print what we find. */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000362#if !ENABLE_FEATURE_STAT_FORMAT
363#define do_statfs(filename, format) do_statfs(filename)
364#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000365static bool do_statfs(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000366{
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000367 struct statfs statfsbuf;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000368#if !ENABLE_FEATURE_STAT_FORMAT
369 const char *format;
370#endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000371#if ENABLE_SELINUX
372 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000373
374 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000375 if ((option_mask32 & OPT_DEREFERENCE
376 ? lgetfilecon(filename, &scontext)
377 : getfilecon(filename, &scontext)
378 ) < 0
379 ) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000380 bb_perror_msg(filename);
381 return 0;
382 }
383 }
384#endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000385 if (statfs(filename, &statfsbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100386 bb_perror_msg("can't read file system information for '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000387 return 0;
388 }
389
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000390#if ENABLE_FEATURE_STAT_FORMAT
Denis Vlasenko91e52032007-10-05 20:31:23 +0000391 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100392# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000393 format = (option_mask32 & OPT_TERSE
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000394 ? "%n %i %l %t %s %b %f %a %c %d\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000395 : " File: \"%n\"\n"
396 " ID: %-8i Namelen: %-7l Type: %T\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000397 "Block size: %-10s\n"
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000398 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000399 "Inodes: Total: %-10c Free: %d");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100400# else
Denis Vlasenko91e52032007-10-05 20:31:23 +0000401 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000402 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
Denis Vlasenko49622d72007-03-10 16:58:49 +0000403 "%n %i %l %t %s %b %f %a %c %d\n")
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000404 : (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000405 " File: \"%n\"\n"
406 " ID: %-8i Namelen: %-7l Type: %T\n"
407 "Block size: %-10s\n"
408 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
409 "Inodes: Total: %-10c Free: %d"
410 " S_context: %C\n":
411 " File: \"%n\"\n"
412 " ID: %-8i Namelen: %-7l Type: %T\n"
413 "Block size: %-10s\n"
414 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
415 "Inodes: Total: %-10c Free: %d\n")
416 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100417# endif /* SELINUX */
Denis Vlasenko91e52032007-10-05 20:31:23 +0000418 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000419 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000420#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000421 format = (option_mask32 & OPT_TERSE
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000422 ? "%s %llx %lu "
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000423 : " File: \"%s\"\n"
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000424 " ID: %-8llx Namelen: %-7lu ");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000425 printf(format,
426 filename,
Denis Vlasenko98f5cdf2008-11-11 22:25:34 +0000427 get_f_fsid(&statfsbuf),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000428 statfsbuf.f_namelen);
429
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000430 if (option_mask32 & OPT_TERSE)
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100431 printf("%lx ", (unsigned long) statfsbuf.f_type);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000432 else
433 printf("Type: %s\n", human_fstype(statfsbuf.f_type));
434
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100435# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000436 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100437 ? "%lu %llu %llu %llu %llu %llu\n"
Mike Frysinger726b2cb2005-07-26 22:39:56 +0000438 : "Block size: %-10lu\n"
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100439 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
440 "Inodes: Total: %-10llu Free: %llu\n");
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000441 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100442 (unsigned long) statfsbuf.f_bsize,
443 (unsigned long long) statfsbuf.f_blocks,
444 (unsigned long long) statfsbuf.f_bfree,
445 (unsigned long long) statfsbuf.f_bavail,
446 (unsigned long long) statfsbuf.f_files,
447 (unsigned long long) statfsbuf.f_ffree);
448# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000449 format = (option_mask32 & OPT_TERSE
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100450 ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
451 : (option_mask32 & OPT_SELINUX
452 ? "Block size: %-10lu\n"
453 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
454 "Inodes: Total: %-10llu Free: %llu"
455 "S_context: %C\n"
456 : "Block size: %-10lu\n"
457 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
458 "Inodes: Total: %-10llu Free: %llu\n"
459 )
460 );
Denis Vlasenko49622d72007-03-10 16:58:49 +0000461 printf(format,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100462 (unsigned long) statfsbuf.f_bsize,
463 (unsigned long long) statfsbuf.f_blocks,
464 (unsigned long long) statfsbuf.f_bfree,
465 (unsigned long long) statfsbuf.f_bavail,
466 (unsigned long long) statfsbuf.f_files,
467 (unsigned long long) statfsbuf.f_ffree,
Denis Vlasenko49622d72007-03-10 16:58:49 +0000468 scontext);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000469
Denis Vlasenko49622d72007-03-10 16:58:49 +0000470 if (scontext)
471 freecon(scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100472# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000473#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000474 return 1;
475}
476
477/* stat the file and print what we find */
Denis Vlasenko85c24712008-03-17 09:04:04 +0000478#if !ENABLE_FEATURE_STAT_FORMAT
479#define do_stat(filename, format) do_stat(filename)
480#endif
Denis Vlasenko91e52032007-10-05 20:31:23 +0000481static bool do_stat(const char *filename, const char *format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000482{
483 struct stat statbuf;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000484#if ENABLE_SELINUX
485 security_context_t scontext = NULL;
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000486
487 if (option_mask32 & OPT_SELINUX) {
Denis Vlasenko501bfe22007-08-09 08:10:13 +0000488 if ((option_mask32 & OPT_DEREFERENCE
489 ? lgetfilecon(filename, &scontext)
490 : getfilecon(filename, &scontext)
491 ) < 0
492 ) {
493 bb_perror_msg(filename);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000494 return 0;
495 }
496 }
497#endif
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000498 if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100499 bb_perror_msg("can't stat '%s'", filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000500 return 0;
501 }
502
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000503#if ENABLE_FEATURE_STAT_FORMAT
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000504 if (format == NULL) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100505# if !ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000506 if (option_mask32 & OPT_TERSE) {
Denis Vlasenko5d148e22006-11-21 00:12:09 +0000507 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000508 } else {
509 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
510 format =
511 " File: \"%N\"\n"
512 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
513 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
514 " Device type: %t,%T\n"
515 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
516 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
517 } else {
518 format =
519 " File: \"%N\"\n"
520 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
521 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
522 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
523 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
524 }
525 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100526# else
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000527 if (option_mask32 & OPT_TERSE) {
528 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000529 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n":
530 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n");
531 } else {
532 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000533 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000534 " File: \"%N\"\n"
535 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
536 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
537 " Device type: %t,%T\n"
538 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
539 " S_Context: %C\n"
540 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
541 " File: \"%N\"\n"
542 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
543 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
544 " Device type: %t,%T\n"
545 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
546 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
547 } else {
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000548 format = (option_mask32 & OPT_SELINUX ?
Denis Vlasenko49622d72007-03-10 16:58:49 +0000549 " File: \"%N\"\n"
550 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
551 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
552 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
553 "S_Context: %C\n"
554 "Access: %x\n" "Modify: %y\n" "Change: %z\n":
555 " File: \"%N\"\n"
556 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
557 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
558 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
559 "Access: %x\n" "Modify: %y\n" "Change: %z\n");
560 }
561 }
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100562# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000563 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000564 print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
Denis Vlasenko49622d72007-03-10 16:58:49 +0000565#else /* FEATURE_STAT_FORMAT */
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000566 if (option_mask32 & OPT_TERSE) {
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100567 printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000568 IF_NOT_SELINUX("\n"),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000569 filename,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100570 (unsigned long long) statbuf.st_size,
571 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000572 (unsigned long) statbuf.st_mode,
573 (unsigned long) statbuf.st_uid,
574 (unsigned long) statbuf.st_gid,
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100575 (unsigned long long) statbuf.st_dev,
576 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000577 (unsigned long) statbuf.st_nlink,
578 (unsigned long) major(statbuf.st_rdev),
579 (unsigned long) minor(statbuf.st_rdev),
580 (unsigned long) statbuf.st_atime,
581 (unsigned long) statbuf.st_mtime,
582 (unsigned long) statbuf.st_ctime,
583 (unsigned long) statbuf.st_blksize
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000584 );
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100585# if ENABLE_SELINUX
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000586 if (option_mask32 & OPT_SELINUX)
Denis Vlasenko49622d72007-03-10 16:58:49 +0000587 printf(" %lc\n", *scontext);
588 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000589 bb_putchar('\n');
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100590# endif
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000591 } else {
592 char *linkname = NULL;
593
594 struct passwd *pw_ent;
595 struct group *gw_ent;
596 setgrent();
597 gw_ent = getgrgid(statbuf.st_gid);
598 setpwent();
599 pw_ent = getpwuid(statbuf.st_uid);
600
601 if (S_ISLNK(statbuf.st_mode))
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000602 linkname = xmalloc_readlink_or_warn(filename);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000603 if (linkname)
604 printf(" File: \"%s\" -> \"%s\"\n", filename, linkname);
605 else
606 printf(" File: \"%s\"\n", filename);
607
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100608 printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
609 "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
610 (unsigned long long) statbuf.st_size,
611 (unsigned long long) statbuf.st_blocks,
Denis Vlasenko87468852007-04-13 23:22:00 +0000612 (unsigned long) statbuf.st_blksize,
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000613 file_type(&statbuf),
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100614 (unsigned long long) statbuf.st_dev,
615 (unsigned long long) statbuf.st_dev,
616 (unsigned long long) statbuf.st_ino,
Denis Vlasenko87468852007-04-13 23:22:00 +0000617 (unsigned long) statbuf.st_nlink);
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000618 if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
619 printf(" Device type: %lx,%lx\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000620 (unsigned long) major(statbuf.st_rdev),
621 (unsigned long) minor(statbuf.st_rdev));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000622 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000623 bb_putchar('\n');
Denis Vlasenko49622d72007-03-10 16:58:49 +0000624 printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
Denis Vlasenko87468852007-04-13 23:22:00 +0000625 (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000626 bb_mode_string(statbuf.st_mode),
Denis Vlasenko87468852007-04-13 23:22:00 +0000627 (unsigned long) statbuf.st_uid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000628 (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
Denis Vlasenko87468852007-04-13 23:22:00 +0000629 (unsigned long) statbuf.st_gid,
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000630 (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100631# if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000632 printf(" S_Context: %lc\n", *scontext);
Denys Vlasenko5b9b1362010-02-02 03:08:57 +0100633# endif
Denis Vlasenko49622d72007-03-10 16:58:49 +0000634 printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000635 human_time(statbuf.st_atime),
636 human_time(statbuf.st_mtime),
637 human_time(statbuf.st_ctime));
638 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000639#endif /* FEATURE_STAT_FORMAT */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000640 return 1;
641}
642
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000643int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100644int stat_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000645{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000646 IF_FEATURE_STAT_FORMAT(char *format = NULL;)
Bernhard Reutner-Fischer0e6ab012007-04-04 13:58:33 +0000647 int i;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100648 int ok;
649 unsigned opts;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000650 statfunc_ptr statfunc = do_stat;
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000651
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100652 opt_complementary = "-1"; /* min one arg */
653 opts = getopt32(argv, "ftL"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000654 IF_SELINUX("Z")
655 IF_FEATURE_STAT_FORMAT("c:", &format)
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000656 );
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100657 if (opts & OPT_FILESYS) /* -f */
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000658 statfunc = do_statfs;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000659#if ENABLE_SELINUX
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100660 if (opts & OPT_SELINUX) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000661 selinux_or_die();
662 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100663#endif
664 ok = 1;
665 argv += optind;
666 for (i = 0; argv[i]; ++i)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000667 ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
Mike Frysinger9b5f71e2005-04-23 06:26:38 +0000668
669 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
670}