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