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