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