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