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