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