"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> |
| 8 | * |
| 9 | * Written by Michael Meskes |
| 10 | * Taken from coreutils and turned into a busybox applet by Mike Frysinger |
| 11 | * |
Bernhard Reutner-Fischer | e11a01c | 2006-04-05 17:19:37 +0000 | [diff] [blame] | 12 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
| 16 | |
| 17 | /* vars to control behavior */ |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 18 | #define OPT_TERSE 2 |
Bernhard Reutner-Fischer | 18260d5 | 2006-04-18 14:17:49 +0000 | [diff] [blame] | 19 | #define OPT_DEREFERENCE 4 |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 20 | static long flags; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 21 | |
| 22 | static char const *file_type(struct stat const *st) |
| 23 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 24 | /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 25 | * for some of these formats. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 26 | * To keep diagnostics grammatical in English, the |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 27 | * returned string must start with a consonant. |
| 28 | */ |
| 29 | if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file"; |
| 30 | if (S_ISDIR(st->st_mode)) return "directory"; |
| 31 | if (S_ISBLK(st->st_mode)) return "block special file"; |
| 32 | if (S_ISCHR(st->st_mode)) return "character special file"; |
| 33 | if (S_ISFIFO(st->st_mode)) return "fifo"; |
| 34 | if (S_ISLNK(st->st_mode)) return "symbolic link"; |
| 35 | if (S_ISSOCK(st->st_mode)) return "socket"; |
| 36 | if (S_TYPEISMQ(st)) return "message queue"; |
| 37 | if (S_TYPEISSEM(st)) return "semaphore"; |
| 38 | if (S_TYPEISSHM(st)) return "shared memory object"; |
| 39 | #ifdef S_TYPEISTMO |
| 40 | if (S_TYPEISTMO(st)) return "typed memory object"; |
| 41 | #endif |
| 42 | return "weird file"; |
| 43 | } |
| 44 | |
| 45 | static char const *human_time(time_t t) |
| 46 | { |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 47 | /* Old |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 48 | static char *str; |
| 49 | str = ctime(&t); |
| 50 | str[strlen(str)-1] = '\0'; |
| 51 | return str; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 52 | */ |
| 53 | /* coreutils 6.3 compat: */ |
| 54 | static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")]; |
| 55 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); |
| 56 | return buf; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* Return the type of the specified file system. |
| 60 | * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris) |
| 61 | * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2) |
| 62 | * Still others have neither and have to get by with f_type (Linux). |
| 63 | */ |
| 64 | static char const *human_fstype(long f_type) |
| 65 | { |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 66 | int i; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 67 | static const struct types { |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 68 | long type; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 69 | const char *fs; |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 70 | } humantypes[] = { |
| 71 | { 0xADFF, "affs" }, |
| 72 | { 0x1Cd1, "devpts" }, |
| 73 | { 0x137D, "ext" }, |
| 74 | { 0xEF51, "ext2" }, |
| 75 | { 0xEF53, "ext2/ext3" }, |
| 76 | { 0x3153464a, "jfs" }, |
| 77 | { 0x58465342, "xfs" }, |
| 78 | { 0xF995E849, "hpfs" }, |
| 79 | { 0x9660, "isofs" }, |
| 80 | { 0x4000, "isofs" }, |
| 81 | { 0x4004, "isofs" }, |
| 82 | { 0x137F, "minix" }, |
| 83 | { 0x138F, "minix (30 char.)" }, |
| 84 | { 0x2468, "minix v2" }, |
| 85 | { 0x2478, "minix v2 (30 char.)" }, |
| 86 | { 0x4d44, "msdos" }, |
| 87 | { 0x4006, "fat" }, |
| 88 | { 0x564c, "novell" }, |
| 89 | { 0x6969, "nfs" }, |
| 90 | { 0x9fa0, "proc" }, |
| 91 | { 0x517B, "smb" }, |
| 92 | { 0x012FF7B4, "xenix" }, |
| 93 | { 0x012FF7B5, "sysv4" }, |
| 94 | { 0x012FF7B6, "sysv2" }, |
| 95 | { 0x012FF7B7, "coh" }, |
| 96 | { 0x00011954, "ufs" }, |
| 97 | { 0x012FD16D, "xia" }, |
| 98 | { 0x5346544e, "ntfs" }, |
| 99 | { 0x1021994, "tmpfs" }, |
| 100 | { 0x52654973, "reiserfs" }, |
| 101 | { 0x28cd3d45, "cramfs" }, |
| 102 | { 0x7275, "romfs" }, |
| 103 | { 0x858458f6, "romfs" }, |
| 104 | { 0x73717368, "squashfs" }, |
| 105 | { 0x62656572, "sysfs" }, |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 106 | { 0, "UNKNOWN" } |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 107 | }; |
| 108 | for (i=0; humantypes[i].type; ++i) |
| 109 | if (humantypes[i].type == f_type) |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 110 | break; |
Mike Frysinger | 408ae21 | 2005-04-24 04:11:44 +0000 | [diff] [blame] | 111 | return humantypes[i].fs; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #ifdef CONFIG_FEATURE_STAT_FORMAT |
| 115 | /* print statfs info */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 116 | static void print_statfs(char *pformat, size_t buf_len, char m, |
| 117 | char const *filename, void const *data) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 118 | { |
| 119 | struct statfs const *statfsbuf = data; |
| 120 | |
| 121 | switch (m) { |
| 122 | case 'n': |
| 123 | strncat(pformat, "s", buf_len); |
| 124 | printf(pformat, filename); |
| 125 | break; |
| 126 | case 'i': |
| 127 | strncat(pformat, "Lx", buf_len); |
| 128 | printf(pformat, statfsbuf->f_fsid); |
| 129 | break; |
| 130 | case 'l': |
| 131 | strncat(pformat, "lu", buf_len); |
| 132 | printf(pformat, statfsbuf->f_namelen); |
| 133 | break; |
| 134 | case 't': |
| 135 | strncat(pformat, "lx", buf_len); |
| 136 | printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv. */ |
| 137 | break; |
| 138 | case 'T': |
| 139 | strncat(pformat, "s", buf_len); |
| 140 | printf(pformat, human_fstype(statfsbuf->f_type)); |
| 141 | break; |
| 142 | case 'b': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 143 | strncat(pformat, "jd", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 144 | printf(pformat, (intmax_t) (statfsbuf->f_blocks)); |
| 145 | break; |
| 146 | case 'f': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 147 | strncat(pformat, "jd", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 148 | printf(pformat, (intmax_t) (statfsbuf->f_bfree)); |
| 149 | break; |
| 150 | case 'a': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 151 | strncat(pformat, "jd", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 152 | printf(pformat, (intmax_t) (statfsbuf->f_bavail)); |
| 153 | break; |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 154 | case 'S': |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 155 | case 's': |
| 156 | strncat(pformat, "lu", buf_len); |
| 157 | printf(pformat, (unsigned long int) (statfsbuf->f_bsize)); |
| 158 | break; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 159 | case 'c': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 160 | strncat(pformat, "jd", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 161 | printf(pformat, (intmax_t) (statfsbuf->f_files)); |
| 162 | break; |
| 163 | case 'd': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 164 | strncat(pformat, "jd", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 165 | printf(pformat, (intmax_t) (statfsbuf->f_ffree)); |
| 166 | break; |
| 167 | default: |
| 168 | strncat(pformat, "c", buf_len); |
| 169 | printf(pformat, m); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* print stat info */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 175 | static void print_stat(char *pformat, size_t buf_len, char m, |
| 176 | char const *filename, void const *data) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 177 | { |
| 178 | #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) |
| 179 | struct stat *statbuf = (struct stat *) data; |
| 180 | struct passwd *pw_ent; |
| 181 | struct group *gw_ent; |
| 182 | |
| 183 | switch (m) { |
| 184 | case 'n': |
| 185 | strncat(pformat, "s", buf_len); |
| 186 | printf(pformat, filename); |
| 187 | break; |
| 188 | case 'N': |
| 189 | strncat(pformat, "s", buf_len); |
| 190 | if (S_ISLNK(statbuf->st_mode)) { |
| 191 | char *linkname = xreadlink(filename); |
| 192 | if (linkname == NULL) { |
| 193 | bb_perror_msg("cannot read symbolic link '%s'", filename); |
| 194 | return; |
| 195 | } |
| 196 | /*printf("\"%s\" -> \"%s\"", filename, linkname); */ |
| 197 | printf(pformat, filename); |
| 198 | printf(" -> "); |
| 199 | printf(pformat, linkname); |
| 200 | } else { |
| 201 | printf(pformat, filename); |
| 202 | } |
| 203 | break; |
| 204 | case 'd': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 205 | strncat(pformat, "ju", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 206 | printf(pformat, (uintmax_t) statbuf->st_dev); |
| 207 | break; |
| 208 | case 'D': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 209 | strncat(pformat, "jx", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 210 | printf(pformat, (uintmax_t) statbuf->st_dev); |
| 211 | break; |
| 212 | case 'i': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 213 | strncat(pformat, "ju", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 214 | printf(pformat, (uintmax_t) statbuf->st_ino); |
| 215 | break; |
| 216 | case 'a': |
| 217 | strncat(pformat, "lo", buf_len); |
| 218 | printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO))); |
| 219 | break; |
| 220 | case 'A': |
| 221 | strncat(pformat, "s", buf_len); |
| 222 | printf(pformat, bb_mode_string(statbuf->st_mode)); |
| 223 | break; |
| 224 | case 'f': |
| 225 | strncat(pformat, "lx", buf_len); |
| 226 | printf(pformat, (unsigned long int) statbuf->st_mode); |
| 227 | break; |
| 228 | case 'F': |
| 229 | strncat(pformat, "s", buf_len); |
| 230 | printf(pformat, file_type(statbuf)); |
| 231 | break; |
| 232 | case 'h': |
| 233 | strncat(pformat, "lu", buf_len); |
| 234 | printf(pformat, (unsigned long int) statbuf->st_nlink); |
| 235 | break; |
| 236 | case 'u': |
| 237 | strncat(pformat, "lu", buf_len); |
| 238 | printf(pformat, (unsigned long int) statbuf->st_uid); |
| 239 | break; |
| 240 | case 'U': |
| 241 | strncat(pformat, "s", buf_len); |
| 242 | setpwent(); |
| 243 | pw_ent = getpwuid(statbuf->st_uid); |
| 244 | printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN"); |
| 245 | break; |
| 246 | case 'g': |
| 247 | strncat(pformat, "lu", buf_len); |
| 248 | printf(pformat, (unsigned long int) statbuf->st_gid); |
| 249 | break; |
| 250 | case 'G': |
| 251 | strncat(pformat, "s", buf_len); |
| 252 | setgrent(); |
| 253 | gw_ent = getgrgid(statbuf->st_gid); |
| 254 | printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN"); |
| 255 | break; |
| 256 | case 't': |
| 257 | strncat(pformat, "lx", buf_len); |
| 258 | printf(pformat, (unsigned long int) major(statbuf->st_rdev)); |
| 259 | break; |
| 260 | case 'T': |
| 261 | strncat(pformat, "lx", buf_len); |
| 262 | printf(pformat, (unsigned long int) minor(statbuf->st_rdev)); |
| 263 | break; |
| 264 | case 's': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 265 | strncat(pformat, "ju", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 266 | printf(pformat, (uintmax_t) (statbuf->st_size)); |
| 267 | break; |
| 268 | case 'B': |
| 269 | strncat(pformat, "lu", buf_len); |
| 270 | printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE |
| 271 | break; |
| 272 | case 'b': |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 273 | strncat(pformat, "ju", buf_len); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 274 | printf(pformat, (uintmax_t) statbuf->st_blocks); |
| 275 | break; |
| 276 | case 'o': |
| 277 | strncat(pformat, "lu", buf_len); |
| 278 | printf(pformat, (unsigned long int) statbuf->st_blksize); |
| 279 | break; |
| 280 | case 'x': |
| 281 | strncat(pformat, "s", buf_len); |
| 282 | printf(pformat, human_time(statbuf->st_atime)); |
| 283 | break; |
| 284 | case 'X': |
| 285 | strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); |
| 286 | printf(pformat, (unsigned long int) statbuf->st_atime); |
| 287 | break; |
| 288 | case 'y': |
| 289 | strncat(pformat, "s", buf_len); |
| 290 | printf(pformat, human_time(statbuf->st_mtime)); |
| 291 | break; |
| 292 | case 'Y': |
| 293 | strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); |
| 294 | printf(pformat, (unsigned long int) statbuf->st_mtime); |
| 295 | break; |
| 296 | case 'z': |
| 297 | strncat(pformat, "s", buf_len); |
| 298 | printf(pformat, human_time(statbuf->st_ctime)); |
| 299 | break; |
| 300 | case 'Z': |
| 301 | strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); |
| 302 | printf(pformat, (unsigned long int) statbuf->st_ctime); |
| 303 | break; |
| 304 | default: |
| 305 | strncat(pformat, "c", buf_len); |
| 306 | printf(pformat, m); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 311 | static void print_it(char const *masterformat, char const *filename, |
| 312 | void (*print_func) (char *, size_t, char, char const *, void const *), |
| 313 | void const *data) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 314 | { |
| 315 | char *b; |
| 316 | |
| 317 | /* create a working copy of the format string */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 318 | char *format = xstrdup(masterformat); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 319 | |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 320 | /* Add 2 to accomodate our conversion of the stat '%s' format string |
| 321 | * to the printf '%llu' one. */ |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 322 | size_t n_alloc = strlen(format) + 2 + 1; |
| 323 | char *dest = xmalloc(n_alloc); |
| 324 | |
| 325 | b = format; |
| 326 | while (b) { |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 327 | size_t len; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 328 | char *p = strchr(b, '%'); |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 329 | if (!p) { |
| 330 | /* coreutils 6.3 always print <cr> at the end */ |
| 331 | /*fputs(b, stdout);*/ |
| 332 | puts(b); |
| 333 | break; |
| 334 | } |
| 335 | *p++ = '\0'; |
| 336 | fputs(b, stdout); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 338 | len = strspn(p, "#-+.I 0123456789"); |
| 339 | dest[0] = '%'; |
| 340 | memcpy(dest + 1, p, len); |
| 341 | dest[1 + len] = 0; |
| 342 | p += len; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 344 | b = p + 1; |
| 345 | switch (*p) { |
| 346 | case '\0': |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 347 | b = NULL; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 348 | /* fall through */ |
| 349 | case '%': |
| 350 | putchar('%'); |
| 351 | break; |
| 352 | default: |
| 353 | print_func(dest, n_alloc, *p, filename, data); |
| 354 | break; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | free(format); |
| 359 | free(dest); |
| 360 | } |
| 361 | #endif |
| 362 | |
| 363 | /* Stat the file system and print what we find. */ |
| 364 | static int do_statfs(char const *filename, char const *format) |
| 365 | { |
| 366 | struct statfs statfsbuf; |
| 367 | |
| 368 | if (statfs(filename, &statfsbuf) != 0) { |
| 369 | bb_perror_msg("cannot read file system information for '%s'", filename); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | #ifdef CONFIG_FEATURE_STAT_FORMAT |
| 374 | if (format == NULL) |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 375 | format = (flags & OPT_TERSE |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 376 | ? "%n %i %l %t %s %b %f %a %c %d\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 377 | : " File: \"%n\"\n" |
| 378 | " ID: %-8i Namelen: %-7l Type: %T\n" |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 379 | "Block size: %-10s\n" |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 380 | "Blocks: Total: %-10b Free: %-10f Available: %a\n" |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 381 | "Inodes: Total: %-10c Free: %d"); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 382 | print_it(format, filename, print_statfs, &statfsbuf); |
| 383 | #else |
| 384 | |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 385 | format = (flags & OPT_TERSE |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 386 | ? "%s %llx %lu " |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 387 | : " File: \"%s\"\n" |
| 388 | " ID: %-8Lx Namelen: %-7lu "); |
| 389 | printf(format, |
| 390 | filename, |
| 391 | statfsbuf.f_fsid, |
| 392 | statfsbuf.f_namelen); |
| 393 | |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 394 | if (flags & OPT_TERSE) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 395 | printf("%lx ", (unsigned long int) (statfsbuf.f_type)); |
| 396 | else |
| 397 | printf("Type: %s\n", human_fstype(statfsbuf.f_type)); |
| 398 | |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 399 | format = (flags & OPT_TERSE |
Mike Frysinger | 726b2cb | 2005-07-26 22:39:56 +0000 | [diff] [blame] | 400 | ? "%lu %ld %ld %ld %ld %ld\n" |
| 401 | : "Block size: %-10lu\n" |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 402 | "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n" |
| 403 | "Inodes: Total: %-10jd Free: %jd\n"); |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 404 | printf(format, |
| 405 | (unsigned long int) (statfsbuf.f_bsize), |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 406 | (intmax_t) (statfsbuf.f_blocks), |
| 407 | (intmax_t) (statfsbuf.f_bfree), |
| 408 | (intmax_t) (statfsbuf.f_bavail), |
| 409 | (intmax_t) (statfsbuf.f_files), |
| 410 | (intmax_t) (statfsbuf.f_ffree)); |
| 411 | #endif |
| 412 | |
| 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | /* stat the file and print what we find */ |
| 417 | static int do_stat(char const *filename, char const *format) |
| 418 | { |
| 419 | struct stat statbuf; |
| 420 | |
Bernhard Reutner-Fischer | 18260d5 | 2006-04-18 14:17:49 +0000 | [diff] [blame] | 421 | if ((flags & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) { |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 422 | bb_perror_msg("cannot stat '%s'", filename); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | #ifdef CONFIG_FEATURE_STAT_FORMAT |
| 427 | if (format == NULL) { |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 428 | if (flags & OPT_TERSE) { |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 429 | 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] | 430 | } else { |
| 431 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { |
| 432 | format = |
| 433 | " File: \"%N\"\n" |
| 434 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 435 | "Device: %Dh/%dd\tInode: %-10i Links: %-5h" |
| 436 | " Device type: %t,%T\n" |
| 437 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 438 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; |
| 439 | } else { |
| 440 | format = |
| 441 | " File: \"%N\"\n" |
| 442 | " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" |
| 443 | "Device: %Dh/%dd\tInode: %-10i Links: %h\n" |
| 444 | "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" |
| 445 | "Access: %x\n" "Modify: %y\n" "Change: %z\n"; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | print_it(format, filename, print_stat, &statbuf); |
| 450 | #else |
Mike Frysinger | f06c494 | 2005-04-24 03:53:12 +0000 | [diff] [blame] | 451 | if (flags & OPT_TERSE) { |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 452 | printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu\n", |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 453 | filename, |
| 454 | (uintmax_t) (statbuf.st_size), |
| 455 | (uintmax_t) statbuf.st_blocks, |
| 456 | (unsigned long int) statbuf.st_mode, |
| 457 | (unsigned long int) statbuf.st_uid, |
| 458 | (unsigned long int) statbuf.st_gid, |
| 459 | (uintmax_t) statbuf.st_dev, |
| 460 | (uintmax_t) statbuf.st_ino, |
| 461 | (unsigned long int) statbuf.st_nlink, |
| 462 | (unsigned long int) major(statbuf.st_rdev), |
| 463 | (unsigned long int) minor(statbuf.st_rdev), |
| 464 | (unsigned long int) statbuf.st_atime, |
| 465 | (unsigned long int) statbuf.st_mtime, |
| 466 | (unsigned long int) statbuf.st_ctime, |
| 467 | (unsigned long int) statbuf.st_blksize |
| 468 | ); |
| 469 | } else { |
| 470 | char *linkname = NULL; |
| 471 | |
| 472 | struct passwd *pw_ent; |
| 473 | struct group *gw_ent; |
| 474 | setgrent(); |
| 475 | gw_ent = getgrgid(statbuf.st_gid); |
| 476 | setpwent(); |
| 477 | pw_ent = getpwuid(statbuf.st_uid); |
| 478 | |
| 479 | if (S_ISLNK(statbuf.st_mode)) |
| 480 | linkname = xreadlink(filename); |
| 481 | if (linkname) |
| 482 | printf(" File: \"%s\" -> \"%s\"\n", filename, linkname); |
| 483 | else |
| 484 | printf(" File: \"%s\"\n", filename); |
| 485 | |
Bernhard Reutner-Fischer | fc5f318 | 2006-04-12 08:03:11 +0000 | [diff] [blame] | 486 | printf(" Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n" |
| 487 | "Device: %jxh/%jud\tInode: %-10ju Links: %-5lu", |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 488 | (uintmax_t) (statbuf.st_size), |
| 489 | (uintmax_t) statbuf.st_blocks, |
| 490 | (unsigned long int) statbuf.st_blksize, |
| 491 | file_type(&statbuf), |
| 492 | (uintmax_t) statbuf.st_dev, |
| 493 | (uintmax_t) statbuf.st_dev, |
| 494 | (uintmax_t) statbuf.st_ino, |
| 495 | (unsigned long int) statbuf.st_nlink); |
| 496 | if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) |
| 497 | printf(" Device type: %lx,%lx\n", |
| 498 | (unsigned long int) major(statbuf.st_rdev), |
| 499 | (unsigned long int) minor(statbuf.st_rdev)); |
| 500 | else |
| 501 | putchar('\n'); |
| 502 | printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n" |
| 503 | "Access: %s\n" "Modify: %s\n" "Change: %s\n", |
| 504 | (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), |
| 505 | bb_mode_string(statbuf.st_mode), |
| 506 | (unsigned long int) statbuf.st_uid, |
| 507 | (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN", |
| 508 | (unsigned long int) statbuf.st_gid, |
| 509 | (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN", |
| 510 | human_time(statbuf.st_atime), |
| 511 | human_time(statbuf.st_mtime), |
| 512 | human_time(statbuf.st_ctime)); |
| 513 | } |
| 514 | #endif |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | int stat_main(int argc, char **argv) |
| 519 | { |
| 520 | int i; |
| 521 | char *format = NULL; |
| 522 | int ok = 1; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 523 | int (*statfunc)(char const *, char const *) = do_stat; |
| 524 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 525 | flags = getopt32(argc, argv, "ftL" |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame^] | 526 | USE_FEATURE_STAT_FORMAT("c:", &format) |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 527 | ); |
| 528 | |
| 529 | if (flags & 1) /* -f */ |
| 530 | statfunc = do_statfs; |
Mike Frysinger | 9b5f71e | 2005-04-23 06:26:38 +0000 | [diff] [blame] | 531 | if (argc == optind) /* files */ |
| 532 | bb_show_usage(); |
| 533 | |
| 534 | for (i = optind; i < argc; ++i) |
| 535 | ok &= statfunc(argv[i], format); |
| 536 | |
| 537 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 538 | } |