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