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