"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 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> |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp> |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 9 | * |
| 10 | * Written by Michael Meskes |
| 11 | * Taken from coreutils and turned into a busybox applet by Mike Frysinger |
| 12 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 14 | */ |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 15 | //config:config STAT |
| 16 | //config: bool "stat" |
| 17 | //config: default y |
| 18 | //config: help |
| 19 | //config: display file or filesystem status. |
| 20 | //config: |
| 21 | //config:config FEATURE_STAT_FORMAT |
| 22 | //config: bool "Enable custom formats (-c)" |
| 23 | //config: default y |
| 24 | //config: depends on STAT |
| 25 | //config: help |
| 26 | //config: Without this, stat will not support the '-c format' option where |
| 27 | //config: users can pass a custom format string for output. This adds about |
| 28 | //config: 7k to a nonstatic build on amd64. |
| 29 | //config: |
| 30 | //config:config FEATURE_STAT_FILESYSTEM |
| 31 | //config: bool "Enable display of filesystem status (-f)" |
| 32 | //config: default y |
| 33 | //config: depends on STAT |
| 34 | //config: select PLATFORM_LINUX # statfs() |
| 35 | //config: help |
| 36 | //config: Without this, stat will not support the '-f' option to display |
| 37 | //config: information about filesystem status. |
| 38 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 39 | |
| 40 | //usage:#define stat_trivial_usage |
| 41 | //usage: "[OPTIONS] FILE..." |
| 42 | //usage:#define stat_full_usage "\n\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 43 | //usage: "Display file" |
| 44 | //usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem") |
| 45 | //usage: " status\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 46 | //usage: IF_FEATURE_STAT_FORMAT( |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 47 | //usage: "\n -c FMT Use the specified format" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 48 | //usage: ) |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 49 | //usage: IF_FEATURE_STAT_FILESYSTEM( |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 50 | //usage: "\n -f Display filesystem status" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 51 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 52 | //usage: "\n -L Follow links" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 53 | //usage: "\n -t Terse display" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 54 | //usage: IF_SELINUX( |
| 55 | //usage: "\n -Z Print security context" |
| 56 | //usage: ) |
| 57 | //usage: IF_FEATURE_STAT_FORMAT( |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 58 | //usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 59 | //usage: " %a Access rights in octal\n" |
| 60 | //usage: " %A Access rights in human readable form\n" |
| 61 | //usage: " %b Number of blocks allocated (see %B)\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 62 | //usage: " %B Size in bytes of each block reported by %b\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 63 | //usage: " %d Device number in decimal\n" |
| 64 | //usage: " %D Device number in hex\n" |
| 65 | //usage: " %f Raw mode in hex\n" |
| 66 | //usage: " %F File type\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 67 | //usage: " %g Group ID\n" |
| 68 | //usage: " %G Group name\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 69 | //usage: " %h Number of hard links\n" |
| 70 | //usage: " %i Inode number\n" |
| 71 | //usage: " %n File name\n" |
| 72 | //usage: " %N File name, with -> TARGET if symlink\n" |
| 73 | //usage: " %o I/O block size\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 74 | //usage: " %s Total size in bytes\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 75 | //usage: " %t Major device type in hex\n" |
| 76 | //usage: " %T Minor device type in hex\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 77 | //usage: " %u User ID\n" |
| 78 | //usage: " %U User name\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 79 | //usage: " %x Time of last access\n" |
| 80 | //usage: " %X Time of last access as seconds since Epoch\n" |
| 81 | //usage: " %y Time of last modification\n" |
| 82 | //usage: " %Y Time of last modification as seconds since Epoch\n" |
| 83 | //usage: " %z Time of last change\n" |
| 84 | //usage: " %Z Time of last change as seconds since Epoch\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 85 | //usage: IF_FEATURE_STAT_FILESYSTEM( |
| 86 | //usage: "\nFMT sequences for file systems:\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 87 | //usage: " %a Free blocks available to non-superuser\n" |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 88 | //usage: " %b Total data blocks\n" |
| 89 | //usage: " %c Total file nodes\n" |
| 90 | //usage: " %d Free file nodes\n" |
| 91 | //usage: " %f Free blocks\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 92 | //usage: IF_SELINUX( |
| 93 | //usage: " %C Security context in selinux\n" |
| 94 | //usage: ) |
| 95 | //usage: " %i File System ID in hex\n" |
| 96 | //usage: " %l Maximum length of filenames\n" |
| 97 | //usage: " %n File name\n" |
| 98 | //usage: " %s Block size (for faster transfer)\n" |
| 99 | //usage: " %S Fundamental block size (for block counts)\n" |
| 100 | //usage: " %t Type in hex\n" |
| 101 | //usage: " %T Type in human readable form" |
| 102 | //usage: ) |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 103 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 104 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 105 | #include "libbb.h" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 106 | |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 107 | enum { |
| 108 | OPT_TERSE = (1 << 0), |
| 109 | OPT_DEREFERENCE = (1 << 1), |
| 110 | OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM, |
| 111 | OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX, |
| 112 | }; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 113 | |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 114 | #if ENABLE_FEATURE_STAT_FORMAT |
| 115 | typedef bool (*statfunc_ptr)(const char *, const char *); |
| 116 | #else |
| 117 | typedef bool (*statfunc_ptr)(const char *); |
| 118 | #endif |
| 119 | |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 120 | static const char *file_type(const struct stat *st) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 121 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 122 | /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 123 | * for some of these formats. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 124 | * To keep diagnostics grammatical in English, the |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 125 | * returned string must start with a consonant. |
| 126 | */ |
| 127 | if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file"; |
| 128 | if (S_ISDIR(st->st_mode)) return "directory"; |
| 129 | if (S_ISBLK(st->st_mode)) return "block special file"; |
| 130 | if (S_ISCHR(st->st_mode)) return "character special file"; |
| 131 | if (S_ISFIFO(st->st_mode)) return "fifo"; |
| 132 | if (S_ISLNK(st->st_mode)) return "symbolic link"; |
| 133 | if (S_ISSOCK(st->st_mode)) return "socket"; |
Tias Guns | a1ec841 | 2012-06-03 16:43:06 +0200 | [diff] [blame] | 134 | #ifdef S_TYPEISMQ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 135 | if (S_TYPEISMQ(st)) return "message queue"; |
Tias Guns | a1ec841 | 2012-06-03 16:43:06 +0200 | [diff] [blame] | 136 | #endif |
| 137 | #ifdef S_TYPEISSEM |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 138 | if (S_TYPEISSEM(st)) return "semaphore"; |
Tias Guns | a1ec841 | 2012-06-03 16:43:06 +0200 | [diff] [blame] | 139 | #endif |
| 140 | #ifdef S_TYPEISSHM |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 141 | if (S_TYPEISSHM(st)) return "shared memory object"; |
Tias Guns | a1ec841 | 2012-06-03 16:43:06 +0200 | [diff] [blame] | 142 | #endif |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 143 | #ifdef S_TYPEISTMO |
| 144 | if (S_TYPEISTMO(st)) return "typed memory object"; |
| 145 | #endif |
| 146 | return "weird file"; |
| 147 | } |
| 148 | |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 149 | static const char *human_time(time_t t) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 150 | { |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 151 | /* Old |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 152 | static char *str; |
| 153 | str = ctime(&t); |
| 154 | str[strlen(str)-1] = '\0'; |
| 155 | return str; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 156 | */ |
| 157 | /* coreutils 6.3 compat: */ |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 159 | /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ |
| 160 | #define buf bb_common_bufsiz1 |
| 161 | |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 162 | strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000"); |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 163 | return buf; |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 164 | #undef buf |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 167 | #if ENABLE_FEATURE_STAT_FILESYSTEM |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 168 | /* Return the type of the specified file system. |
| 169 | * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris) |
| 170 | * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2) |
| 171 | * Still others have neither and have to get by with f_type (Linux). |
| 172 | */ |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 173 | static const char *human_fstype(uint32_t f_type) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 174 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 175 | static const struct types { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 176 | uint32_t type; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 177 | const char *const fs; |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 178 | } humantypes[] = { |
| 179 | { 0xADFF, "affs" }, |
| 180 | { 0x1Cd1, "devpts" }, |
| 181 | { 0x137D, "ext" }, |
| 182 | { 0xEF51, "ext2" }, |
| 183 | { 0xEF53, "ext2/ext3" }, |
| 184 | { 0x3153464a, "jfs" }, |
| 185 | { 0x58465342, "xfs" }, |
| 186 | { 0xF995E849, "hpfs" }, |
| 187 | { 0x9660, "isofs" }, |
| 188 | { 0x4000, "isofs" }, |
| 189 | { 0x4004, "isofs" }, |
| 190 | { 0x137F, "minix" }, |
| 191 | { 0x138F, "minix (30 char.)" }, |
| 192 | { 0x2468, "minix v2" }, |
| 193 | { 0x2478, "minix v2 (30 char.)" }, |
| 194 | { 0x4d44, "msdos" }, |
| 195 | { 0x4006, "fat" }, |
| 196 | { 0x564c, "novell" }, |
| 197 | { 0x6969, "nfs" }, |
| 198 | { 0x9fa0, "proc" }, |
| 199 | { 0x517B, "smb" }, |
| 200 | { 0x012FF7B4, "xenix" }, |
| 201 | { 0x012FF7B5, "sysv4" }, |
| 202 | { 0x012FF7B6, "sysv2" }, |
| 203 | { 0x012FF7B7, "coh" }, |
| 204 | { 0x00011954, "ufs" }, |
| 205 | { 0x012FD16D, "xia" }, |
| 206 | { 0x5346544e, "ntfs" }, |
| 207 | { 0x1021994, "tmpfs" }, |
| 208 | { 0x52654973, "reiserfs" }, |
| 209 | { 0x28cd3d45, "cramfs" }, |
| 210 | { 0x7275, "romfs" }, |
| 211 | { 0x858458f6, "romfs" }, |
| 212 | { 0x73717368, "squashfs" }, |
| 213 | { 0x62656572, "sysfs" }, |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 214 | { 0, "UNKNOWN" } |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 215 | }; |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 216 | |
| 217 | int i; |
| 218 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 219 | for (i = 0; humantypes[i].type; ++i) |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 220 | if (humantypes[i].type == f_type) |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 221 | break; |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 222 | return humantypes[i].fs; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 225 | /* "man statfs" says that statfsbuf->f_fsid is a mess */ |
| 226 | /* coreutils treats it as an array of ints, most significant first */ |
| 227 | static unsigned long long get_f_fsid(const struct statfs *statfsbuf) |
| 228 | { |
| 229 | const unsigned *p = (const void*) &statfsbuf->f_fsid; |
| 230 | unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned); |
| 231 | unsigned long long r = 0; |
| 232 | |
| 233 | do |
| 234 | r = (r << (sizeof(unsigned)*8)) | *p++; |
| 235 | while (--sz > 0); |
| 236 | return r; |
| 237 | } |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 238 | #endif /* FEATURE_STAT_FILESYSTEM */ |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 239 | |
Denis Vlasenko | 86c285d | 2008-11-13 21:53:32 +0000 | [diff] [blame] | 240 | #if ENABLE_FEATURE_STAT_FORMAT |
| 241 | static void strcatc(char *str, char c) |
| 242 | { |
| 243 | int len = strlen(str); |
| 244 | str[len++] = c; |
| 245 | str[len] = '\0'; |
| 246 | } |
| 247 | |
| 248 | static void printfs(char *pformat, const char *msg) |
| 249 | { |
| 250 | strcatc(pformat, 's'); |
| 251 | printf(pformat, msg); |
| 252 | } |
| 253 | |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 254 | #if ENABLE_FEATURE_STAT_FILESYSTEM |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 255 | /* print statfs info */ |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 256 | static void FAST_FUNC print_statfs(char *pformat, const char m, |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 257 | const char *const filename, const void *data |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 258 | IF_SELINUX(, security_context_t scontext)) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 259 | { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 260 | const struct statfs *statfsbuf = data; |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 261 | if (m == 'n') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 262 | printfs(pformat, filename); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 263 | } else if (m == 'i') { |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 264 | strcat(pformat, "llx"); |
| 265 | printf(pformat, get_f_fsid(statfsbuf)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 266 | } else if (m == 'l') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 267 | strcat(pformat, "lu"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 268 | printf(pformat, (unsigned long) statfsbuf->f_namelen); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 269 | } else if (m == 't') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 270 | strcat(pformat, "lx"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 271 | printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */ |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 272 | } else if (m == 'T') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 273 | printfs(pformat, human_fstype(statfsbuf->f_type)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 274 | } else if (m == 'b') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 275 | strcat(pformat, "llu"); |
| 276 | printf(pformat, (unsigned long long) statfsbuf->f_blocks); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 277 | } else if (m == 'f') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 278 | strcat(pformat, "llu"); |
| 279 | printf(pformat, (unsigned long long) statfsbuf->f_bfree); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 280 | } else if (m == 'a') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 281 | strcat(pformat, "llu"); |
| 282 | printf(pformat, (unsigned long long) statfsbuf->f_bavail); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 283 | } else if (m == 's' || m == 'S') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 284 | strcat(pformat, "lu"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 285 | printf(pformat, (unsigned long) statfsbuf->f_bsize); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 286 | } else if (m == 'c') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 287 | strcat(pformat, "llu"); |
| 288 | printf(pformat, (unsigned long long) statfsbuf->f_files); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 289 | } else if (m == 'd') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 290 | strcat(pformat, "llu"); |
| 291 | printf(pformat, (unsigned long long) statfsbuf->f_ffree); |
| 292 | # if ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 293 | } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 294 | printfs(pformat, scontext); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 295 | # endif |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 296 | } else { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 297 | strcatc(pformat, 'c'); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 298 | printf(pformat, m); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 301 | #endif |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 302 | |
| 303 | /* print stat info */ |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 304 | static void FAST_FUNC print_stat(char *pformat, const char m, |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 305 | const char *const filename, const void *data |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 306 | IF_SELINUX(, security_context_t scontext)) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 307 | { |
| 308 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) |
| 309 | struct stat *statbuf = (struct stat *) data; |
| 310 | struct passwd *pw_ent; |
| 311 | struct group *gw_ent; |
| 312 | |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 313 | if (m == 'n') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 314 | printfs(pformat, filename); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 315 | } else if (m == 'N') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 316 | strcatc(pformat, 's'); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 317 | if (S_ISLNK(statbuf->st_mode)) { |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 318 | char *linkname = xmalloc_readlink_or_warn(filename); |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 319 | if (linkname == NULL) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 320 | return; |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 321 | printf("'%s' -> '%s'", filename, linkname); |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 322 | free(linkname); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 323 | } else { |
| 324 | printf(pformat, filename); |
| 325 | } |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 326 | } else if (m == 'd') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 327 | strcat(pformat, "llu"); |
| 328 | printf(pformat, (unsigned long long) statbuf->st_dev); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 329 | } else if (m == 'D') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 330 | strcat(pformat, "llx"); |
| 331 | printf(pformat, (unsigned long long) statbuf->st_dev); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 332 | } else if (m == 'i') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 333 | strcat(pformat, "llu"); |
| 334 | printf(pformat, (unsigned long long) statbuf->st_ino); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 335 | } else if (m == 'a') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 336 | strcat(pformat, "lo"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 337 | printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO))); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 338 | } else if (m == 'A') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 339 | printfs(pformat, bb_mode_string(statbuf->st_mode)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 340 | } else if (m == 'f') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 341 | strcat(pformat, "lx"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 342 | printf(pformat, (unsigned long) statbuf->st_mode); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 343 | } else if (m == 'F') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 344 | printfs(pformat, file_type(statbuf)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 345 | } else if (m == 'h') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 346 | strcat(pformat, "lu"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 347 | printf(pformat, (unsigned long) statbuf->st_nlink); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 348 | } else if (m == 'u') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 349 | strcat(pformat, "lu"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 350 | printf(pformat, (unsigned long) statbuf->st_uid); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 351 | } else if (m == 'U') { |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 352 | pw_ent = getpwuid(statbuf->st_uid); |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 353 | printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN"); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 354 | } else if (m == 'g') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 355 | strcat(pformat, "lu"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 356 | printf(pformat, (unsigned long) statbuf->st_gid); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 357 | } else if (m == 'G') { |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 358 | gw_ent = getgrgid(statbuf->st_gid); |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 359 | printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN"); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 360 | } else if (m == 't') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 361 | strcat(pformat, "lx"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 362 | printf(pformat, (unsigned long) major(statbuf->st_rdev)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 363 | } else if (m == 'T') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 364 | strcat(pformat, "lx"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 365 | printf(pformat, (unsigned long) minor(statbuf->st_rdev)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 366 | } else if (m == 's') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 367 | strcat(pformat, "llu"); |
| 368 | printf(pformat, (unsigned long long) statbuf->st_size); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 369 | } else if (m == 'B') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 370 | strcat(pformat, "lu"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 371 | printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 372 | } else if (m == 'b') { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 373 | strcat(pformat, "llu"); |
| 374 | printf(pformat, (unsigned long long) statbuf->st_blocks); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 375 | } else if (m == 'o') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 376 | strcat(pformat, "lu"); |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 377 | printf(pformat, (unsigned long) statbuf->st_blksize); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 378 | } else if (m == 'x') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 379 | printfs(pformat, human_time(statbuf->st_atime)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 380 | } else if (m == 'X') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 381 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 382 | /* note: (unsigned long) would be wrong: |
| 383 | * imagine (unsigned long64)int32 */ |
| 384 | printf(pformat, (long) statbuf->st_atime); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 385 | } else if (m == 'y') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 386 | printfs(pformat, human_time(statbuf->st_mtime)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 387 | } else if (m == 'Y') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 388 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 389 | printf(pformat, (long) statbuf->st_mtime); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 390 | } else if (m == 'z') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 391 | printfs(pformat, human_time(statbuf->st_ctime)); |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 392 | } else if (m == 'Z') { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 393 | strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 394 | printf(pformat, (long) statbuf->st_ctime); |
| 395 | # if ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 396 | } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 397 | printfs(pformat, scontext); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 398 | # endif |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 399 | } else { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 400 | strcatc(pformat, 'c'); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 401 | printf(pformat, m); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 405 | static void print_it(const char *masterformat, |
| 406 | const char *filename, |
| 407 | void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)), |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 408 | const void *data |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 409 | IF_SELINUX(, security_context_t scontext)) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 410 | { |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 411 | /* Create a working copy of the format string */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 412 | char *format = xstrdup(masterformat); |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 413 | /* Add 2 to accommodate our conversion of the stat '%s' format string |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 414 | * to the printf '%llu' one. */ |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 415 | char *dest = xmalloc(strlen(format) + 2 + 1); |
| 416 | char *b; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 417 | |
| 418 | b = format; |
| 419 | while (b) { |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 420 | /* Each iteration finds next %spec, |
| 421 | * prints preceding string and handles found %spec |
| 422 | */ |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 423 | size_t len; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 424 | char *p = strchr(b, '%'); |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 425 | if (!p) { |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 426 | /* coreutils 6.3 always prints newline at the end */ |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 427 | /*fputs(b, stdout);*/ |
| 428 | puts(b); |
| 429 | break; |
| 430 | } |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 431 | |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 432 | /* dest = "%<modifiers>" */ |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 433 | len = 1 + strspn(p + 1, "#-+.I 0123456789"); |
| 434 | memcpy(dest, p, len); |
| 435 | dest[len] = '\0'; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 436 | |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 437 | /* print preceding string */ |
| 438 | *p = '\0'; |
| 439 | fputs(b, stdout); |
| 440 | |
| 441 | p += len; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 442 | b = p + 1; |
| 443 | switch (*p) { |
| 444 | case '\0': |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 445 | b = NULL; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 446 | /* fall through */ |
| 447 | case '%': |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 448 | bb_putchar('%'); |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 449 | break; |
| 450 | default: |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 451 | /* Completes "%<modifiers>" with specifier and printfs */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 452 | print_func(dest, *p, filename, data IF_SELINUX(,scontext)); |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 453 | break; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | free(format); |
| 458 | free(dest); |
| 459 | } |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 460 | #endif /* FEATURE_STAT_FORMAT */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 461 | |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 462 | #if ENABLE_FEATURE_STAT_FILESYSTEM |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 463 | /* Stat the file system and print what we find. */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 464 | #if !ENABLE_FEATURE_STAT_FORMAT |
| 465 | #define do_statfs(filename, format) do_statfs(filename) |
| 466 | #endif |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 467 | static bool do_statfs(const char *filename, const char *format) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 468 | { |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 469 | struct statfs statfsbuf; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 470 | #if !ENABLE_FEATURE_STAT_FORMAT |
| 471 | const char *format; |
| 472 | #endif |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 473 | #if ENABLE_SELINUX |
| 474 | security_context_t scontext = NULL; |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 475 | |
| 476 | if (option_mask32 & OPT_SELINUX) { |
Denis Vlasenko | 501bfe2 | 2007-08-09 08:10:13 +0000 | [diff] [blame] | 477 | if ((option_mask32 & OPT_DEREFERENCE |
| 478 | ? lgetfilecon(filename, &scontext) |
| 479 | : getfilecon(filename, &scontext) |
| 480 | ) < 0 |
| 481 | ) { |
Denys Vlasenko | 9371043 | 2012-09-27 15:35:10 +0200 | [diff] [blame] | 482 | bb_simple_perror_msg(filename); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | } |
| 486 | #endif |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 487 | if (statfs(filename, &statfsbuf) != 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 488 | bb_perror_msg("can't read file system information for '%s'", filename); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 492 | #if ENABLE_FEATURE_STAT_FORMAT |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 493 | if (format == NULL) { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 494 | # if !ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 495 | format = (option_mask32 & OPT_TERSE |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 496 | ? "%n %i %l %t %s %b %f %a %c %d\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 497 | : " File: \"%n\"\n" |
| 498 | " ID: %-8i Namelen: %-7l Type: %T\n" |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 499 | "Block size: %-10s\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 500 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 501 | "Inodes: Total: %-10c Free: %d"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 502 | # else |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 503 | format = (option_mask32 & OPT_TERSE |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 504 | ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n": |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 505 | "%n %i %l %t %s %b %f %a %c %d\n") |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 506 | : (option_mask32 & OPT_SELINUX ? |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 507 | " File: \"%n\"\n" |
| 508 | " ID: %-8i Namelen: %-7l Type: %T\n" |
| 509 | "Block size: %-10s\n" |
| 510 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" |
| 511 | "Inodes: Total: %-10c Free: %d" |
| 512 | " S_context: %C\n": |
| 513 | " File: \"%n\"\n" |
| 514 | " ID: %-8i Namelen: %-7l Type: %T\n" |
| 515 | "Block size: %-10s\n" |
| 516 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" |
| 517 | "Inodes: Total: %-10c Free: %d\n") |
| 518 | ); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 519 | # endif /* SELINUX */ |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 520 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 521 | print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext)); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 522 | #else /* FEATURE_STAT_FORMAT */ |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 523 | format = (option_mask32 & OPT_TERSE |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 524 | ? "%s %llx %lu " |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 525 | : " File: \"%s\"\n" |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 526 | " ID: %-8llx Namelen: %-7lu "); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 527 | printf(format, |
| 528 | filename, |
Denis Vlasenko | 98f5cdf | 2008-11-11 22:25:34 +0000 | [diff] [blame] | 529 | get_f_fsid(&statfsbuf), |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 530 | statfsbuf.f_namelen); |
| 531 | |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 532 | if (option_mask32 & OPT_TERSE) |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 533 | printf("%lx ", (unsigned long) statfsbuf.f_type); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 534 | else |
| 535 | printf("Type: %s\n", human_fstype(statfsbuf.f_type)); |
| 536 | |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 537 | # if !ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 538 | format = (option_mask32 & OPT_TERSE |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 539 | ? "%lu %llu %llu %llu %llu %llu\n" |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 540 | : "Block size: %-10lu\n" |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 541 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" |
| 542 | "Inodes: Total: %-10llu Free: %llu\n"); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 543 | printf(format, |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 544 | (unsigned long) statfsbuf.f_bsize, |
| 545 | (unsigned long long) statfsbuf.f_blocks, |
| 546 | (unsigned long long) statfsbuf.f_bfree, |
| 547 | (unsigned long long) statfsbuf.f_bavail, |
| 548 | (unsigned long long) statfsbuf.f_files, |
| 549 | (unsigned long long) statfsbuf.f_ffree); |
| 550 | # else |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 551 | format = (option_mask32 & OPT_TERSE |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 552 | ? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n") |
| 553 | : (option_mask32 & OPT_SELINUX |
| 554 | ? "Block size: %-10lu\n" |
| 555 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" |
| 556 | "Inodes: Total: %-10llu Free: %llu" |
| 557 | "S_context: %C\n" |
| 558 | : "Block size: %-10lu\n" |
| 559 | "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n" |
| 560 | "Inodes: Total: %-10llu Free: %llu\n" |
| 561 | ) |
| 562 | ); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 563 | printf(format, |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 564 | (unsigned long) statfsbuf.f_bsize, |
| 565 | (unsigned long long) statfsbuf.f_blocks, |
| 566 | (unsigned long long) statfsbuf.f_bfree, |
| 567 | (unsigned long long) statfsbuf.f_bavail, |
| 568 | (unsigned long long) statfsbuf.f_files, |
| 569 | (unsigned long long) statfsbuf.f_ffree, |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 570 | scontext); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 571 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 572 | if (scontext) |
| 573 | freecon(scontext); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 574 | # endif |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 575 | #endif /* FEATURE_STAT_FORMAT */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 576 | return 1; |
| 577 | } |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 578 | #endif /* FEATURE_STAT_FILESYSTEM */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 579 | |
| 580 | /* stat the file and print what we find */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 581 | #if !ENABLE_FEATURE_STAT_FORMAT |
| 582 | #define do_stat(filename, format) do_stat(filename) |
| 583 | #endif |
Denis Vlasenko | 91e5203 | 2007-10-05 20:31:23 +0000 | [diff] [blame] | 584 | static bool do_stat(const char *filename, const char *format) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 585 | { |
| 586 | struct stat statbuf; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 587 | #if ENABLE_SELINUX |
| 588 | security_context_t scontext = NULL; |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 589 | |
| 590 | if (option_mask32 & OPT_SELINUX) { |
Denis Vlasenko | 501bfe2 | 2007-08-09 08:10:13 +0000 | [diff] [blame] | 591 | if ((option_mask32 & OPT_DEREFERENCE |
| 592 | ? lgetfilecon(filename, &scontext) |
| 593 | : getfilecon(filename, &scontext) |
| 594 | ) < 0 |
| 595 | ) { |
Denys Vlasenko | 9371043 | 2012-09-27 15:35:10 +0200 | [diff] [blame] | 596 | bb_simple_perror_msg(filename); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | } |
| 600 | #endif |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 601 | if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 602 | bb_perror_msg("can't stat '%s'", filename); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 606 | #if ENABLE_FEATURE_STAT_FORMAT |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 607 | if (format == NULL) { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 608 | # if !ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 609 | if (option_mask32 & OPT_TERSE) { |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 610 | format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o"; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 611 | } else { |
| 612 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { |
| 613 | format = |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 614 | " File: %N\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 615 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 616 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" |
| 617 | " Device type: %t,%T\n" |
| 618 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 619 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; |
| 620 | } else { |
| 621 | format = |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 622 | " File: %N\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 623 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 624 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" |
| 625 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 626 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; |
| 627 | } |
| 628 | } |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 629 | # else |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 630 | if (option_mask32 & OPT_TERSE) { |
| 631 | format = (option_mask32 & OPT_SELINUX ? |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 632 | "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n" |
| 633 | : |
| 634 | "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n" |
| 635 | ); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 636 | } else { |
| 637 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 638 | format = (option_mask32 & OPT_SELINUX ? |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 639 | " File: %N\n" |
| 640 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 641 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" |
| 642 | " Device type: %t,%T\n" |
| 643 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 644 | " S_Context: %C\n" |
| 645 | "Access: %x\n" "Modify: %y\n" "Change: %z\n" |
| 646 | : |
| 647 | " File: %N\n" |
| 648 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 649 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" |
| 650 | " Device type: %t,%T\n" |
| 651 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 652 | "Access: %x\n" "Modify: %y\n" "Change: %z\n" |
| 653 | ); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 654 | } else { |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 655 | format = (option_mask32 & OPT_SELINUX ? |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 656 | " File: %N\n" |
| 657 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 658 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" |
| 659 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 660 | "S_Context: %C\n" |
| 661 | "Access: %x\n" "Modify: %y\n" "Change: %z\n" |
| 662 | : |
| 663 | " File: %N\n" |
| 664 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 665 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" |
| 666 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 667 | "Access: %x\n" "Modify: %y\n" "Change: %z\n" |
| 668 | ); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 671 | # endif |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 672 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 673 | print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext)); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 674 | #else /* FEATURE_STAT_FORMAT */ |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 675 | if (option_mask32 & OPT_TERSE) { |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 676 | printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 677 | IF_NOT_SELINUX("\n"), |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 678 | filename, |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 679 | (unsigned long long) statbuf.st_size, |
| 680 | (unsigned long long) statbuf.st_blocks, |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 681 | (unsigned long) statbuf.st_mode, |
| 682 | (unsigned long) statbuf.st_uid, |
| 683 | (unsigned long) statbuf.st_gid, |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 684 | (unsigned long long) statbuf.st_dev, |
| 685 | (unsigned long long) statbuf.st_ino, |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 686 | (unsigned long) statbuf.st_nlink, |
| 687 | (unsigned long) major(statbuf.st_rdev), |
| 688 | (unsigned long) minor(statbuf.st_rdev), |
| 689 | (unsigned long) statbuf.st_atime, |
| 690 | (unsigned long) statbuf.st_mtime, |
| 691 | (unsigned long) statbuf.st_ctime, |
| 692 | (unsigned long) statbuf.st_blksize |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 693 | ); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 694 | # if ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 695 | if (option_mask32 & OPT_SELINUX) |
Michael Gernoth | 1b487ea | 2014-06-27 14:08:29 +0200 | [diff] [blame] | 696 | printf(" %s\n", scontext); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 697 | else |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 698 | bb_putchar('\n'); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 699 | # endif |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 700 | } else { |
| 701 | char *linkname = NULL; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 702 | struct passwd *pw_ent; |
| 703 | struct group *gw_ent; |
Alexander Shishkin | a7027bf | 2010-10-21 00:24:05 +0200 | [diff] [blame] | 704 | |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 705 | gw_ent = getgrgid(statbuf.st_gid); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 706 | pw_ent = getpwuid(statbuf.st_uid); |
| 707 | |
| 708 | if (S_ISLNK(statbuf.st_mode)) |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 709 | linkname = xmalloc_readlink_or_warn(filename); |
Alexander Shishkin | a7027bf | 2010-10-21 00:24:05 +0200 | [diff] [blame] | 710 | if (linkname) { |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 711 | printf(" File: '%s' -> '%s'\n", filename, linkname); |
Alexander Shishkin | a7027bf | 2010-10-21 00:24:05 +0200 | [diff] [blame] | 712 | free(linkname); |
| 713 | } else { |
Seb | d2d327d | 2010-06-12 21:57:50 +0200 | [diff] [blame] | 714 | printf(" File: '%s'\n", filename); |
Alexander Shishkin | a7027bf | 2010-10-21 00:24:05 +0200 | [diff] [blame] | 715 | } |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 716 | |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 717 | printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n" |
| 718 | "Device: %llxh/%llud\tInode: %-10llu Links: %-5lu", |
| 719 | (unsigned long long) statbuf.st_size, |
| 720 | (unsigned long long) statbuf.st_blocks, |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 721 | (unsigned long) statbuf.st_blksize, |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 722 | file_type(&statbuf), |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 723 | (unsigned long long) statbuf.st_dev, |
| 724 | (unsigned long long) statbuf.st_dev, |
| 725 | (unsigned long long) statbuf.st_ino, |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 726 | (unsigned long) statbuf.st_nlink); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 727 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) |
| 728 | printf(" Device type: %lx,%lx\n", |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 729 | (unsigned long) major(statbuf.st_rdev), |
| 730 | (unsigned long) minor(statbuf.st_rdev)); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 731 | else |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 732 | bb_putchar('\n'); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 733 | printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n", |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 734 | (unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 735 | bb_mode_string(statbuf.st_mode), |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 736 | (unsigned long) statbuf.st_uid, |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 737 | (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN", |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 738 | (unsigned long) statbuf.st_gid, |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 739 | (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN"); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 740 | # if ENABLE_SELINUX |
Michael Gernoth | 1b487ea | 2014-06-27 14:08:29 +0200 | [diff] [blame] | 741 | if (option_mask32 & OPT_SELINUX) |
| 742 | printf(" S_Context: %s\n", scontext); |
Denys Vlasenko | 5b9b136 | 2010-02-02 03:08:57 +0100 | [diff] [blame] | 743 | # endif |
Eric Lammerts | 66be919 | 2010-10-30 02:48:20 +0200 | [diff] [blame] | 744 | printf("Access: %s\n", human_time(statbuf.st_atime)); |
| 745 | printf("Modify: %s\n", human_time(statbuf.st_mtime)); |
| 746 | printf("Change: %s\n", human_time(statbuf.st_ctime)); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 747 | } |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 748 | #endif /* FEATURE_STAT_FORMAT */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 749 | return 1; |
| 750 | } |
| 751 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 752 | int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 753 | int stat_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 754 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 755 | IF_FEATURE_STAT_FORMAT(char *format = NULL;) |
Bernhard Reutner-Fischer | 0e6ab01 | 2007-04-04 13:58:33 +0000 | [diff] [blame] | 756 | int i; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 757 | int ok; |
| 758 | unsigned opts; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 759 | statfunc_ptr statfunc = do_stat; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 760 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 761 | opt_complementary = "-1"; /* min one arg */ |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 762 | opts = getopt32(argv, "tL" |
| 763 | IF_FEATURE_STAT_FILESYSTEM("f") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 764 | IF_SELINUX("Z") |
| 765 | IF_FEATURE_STAT_FORMAT("c:", &format) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 766 | ); |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 767 | #if ENABLE_FEATURE_STAT_FILESYSTEM |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 768 | if (opts & OPT_FILESYS) /* -f */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 769 | statfunc = do_statfs; |
Denys Vlasenko | ace8330 | 2015-10-30 22:10:44 +0100 | [diff] [blame] | 770 | #endif |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 771 | #if ENABLE_SELINUX |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 772 | if (opts & OPT_SELINUX) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 773 | selinux_or_die(); |
| 774 | } |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 775 | #endif |
| 776 | ok = 1; |
| 777 | argv += optind; |
| 778 | for (i = 0; argv[i]; ++i) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 779 | ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format)); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 780 | |
| 781 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 782 | } |