2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2005-04-23 11:56:38 +05:30
|
|
|
/*
|
|
|
|
* stat -- display file or file system status
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
|
|
|
|
* Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
|
|
|
|
* Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
|
2007-03-10 22:28:49 +05:30
|
|
|
* Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
|
2005-04-23 11:56:38 +05:30
|
|
|
*
|
|
|
|
* Written by Michael Meskes
|
|
|
|
* Taken from coreutils and turned into a busybox applet by Mike Frysinger
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2005-04-23 11:56:38 +05:30
|
|
|
*/
|
2015-10-31 02:40:44 +05:30
|
|
|
//config:config STAT
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "stat (11 kb)"
|
2015-10-31 02:40:44 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: display file or filesystem status.
|
2015-10-31 02:40:44 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_STAT_FORMAT
|
|
|
|
//config: bool "Enable custom formats (-c)"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on STAT
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Without this, stat will not support the '-c format' option where
|
|
|
|
//config: users can pass a custom format string for output. This adds about
|
|
|
|
//config: 7k to a nonstatic build on amd64.
|
2015-10-31 02:40:44 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_STAT_FILESYSTEM
|
|
|
|
//config: bool "Enable display of filesystem status (-f)"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on STAT
|
|
|
|
//config: select PLATFORM_LINUX # statfs()
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Without this, stat will not support the '-f' option to display
|
|
|
|
//config: information about filesystem status.
|
2015-10-31 02:40:44 +05:30
|
|
|
|
2017-08-07 21:48:09 +05:30
|
|
|
//applet:IF_STAT(APPLET_NOEXEC(stat, stat, BB_DIR_BIN, BB_SUID_DROP, stat))
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_STAT) += stat.o
|
2011-03-31 18:13:25 +05:30
|
|
|
|
|
|
|
//usage:#define stat_trivial_usage
|
|
|
|
//usage: "[OPTIONS] FILE..."
|
|
|
|
//usage:#define stat_full_usage "\n\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: "Display file"
|
|
|
|
//usage: IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
|
|
|
|
//usage: " status\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: IF_FEATURE_STAT_FORMAT(
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: "\n -c FMT Use the specified format"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: )
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: IF_FEATURE_STAT_FILESYSTEM(
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: "\n -f Display filesystem status"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: )
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: "\n -L Follow links"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: "\n -t Terse display"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: IF_SELINUX(
|
|
|
|
//usage: "\n -Z Print security context"
|
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_STAT_FORMAT(
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %a Access rights in octal\n"
|
|
|
|
//usage: " %A Access rights in human readable form\n"
|
|
|
|
//usage: " %b Number of blocks allocated (see %B)\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: " %B Size in bytes of each block reported by %b\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %d Device number in decimal\n"
|
|
|
|
//usage: " %D Device number in hex\n"
|
|
|
|
//usage: " %f Raw mode in hex\n"
|
|
|
|
//usage: " %F File type\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: " %g Group ID\n"
|
|
|
|
//usage: " %G Group name\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %h Number of hard links\n"
|
|
|
|
//usage: " %i Inode number\n"
|
|
|
|
//usage: " %n File name\n"
|
|
|
|
//usage: " %N File name, with -> TARGET if symlink\n"
|
|
|
|
//usage: " %o I/O block size\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: " %s Total size in bytes\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %t Major device type in hex\n"
|
|
|
|
//usage: " %T Minor device type in hex\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: " %u User ID\n"
|
|
|
|
//usage: " %U User name\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %x Time of last access\n"
|
|
|
|
//usage: " %X Time of last access as seconds since Epoch\n"
|
|
|
|
//usage: " %y Time of last modification\n"
|
|
|
|
//usage: " %Y Time of last modification as seconds since Epoch\n"
|
|
|
|
//usage: " %z Time of last change\n"
|
|
|
|
//usage: " %Z Time of last change as seconds since Epoch\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: IF_FEATURE_STAT_FILESYSTEM(
|
|
|
|
//usage: "\nFMT sequences for file systems:\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: " %a Free blocks available to non-superuser\n"
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: " %b Total data blocks\n"
|
|
|
|
//usage: " %c Total file nodes\n"
|
|
|
|
//usage: " %d Free file nodes\n"
|
|
|
|
//usage: " %f Free blocks\n"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: IF_SELINUX(
|
|
|
|
//usage: " %C Security context in selinux\n"
|
|
|
|
//usage: )
|
|
|
|
//usage: " %i File System ID in hex\n"
|
|
|
|
//usage: " %l Maximum length of filenames\n"
|
|
|
|
//usage: " %n File name\n"
|
|
|
|
//usage: " %s Block size (for faster transfer)\n"
|
|
|
|
//usage: " %S Fundamental block size (for block counts)\n"
|
|
|
|
//usage: " %t Type in hex\n"
|
|
|
|
//usage: " %T Type in human readable form"
|
|
|
|
//usage: )
|
2015-10-31 02:40:44 +05:30
|
|
|
//usage: )
|
2011-03-31 18:13:25 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2015-10-31 02:40:44 +05:30
|
|
|
enum {
|
|
|
|
OPT_TERSE = (1 << 0),
|
|
|
|
OPT_DEREFERENCE = (1 << 1),
|
|
|
|
OPT_FILESYS = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
|
|
|
|
OPT_SELINUX = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
|
|
|
|
};
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2008-03-17 14:34:04 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FORMAT
|
|
|
|
typedef bool (*statfunc_ptr)(const char *, const char *);
|
|
|
|
#else
|
|
|
|
typedef bool (*statfunc_ptr)(const char *);
|
|
|
|
#endif
|
|
|
|
|
2007-10-06 02:01:23 +05:30
|
|
|
static const char *file_type(const struct stat *st)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2006-01-25 05:38:53 +05:30
|
|
|
/* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
|
2005-04-23 11:56:38 +05:30
|
|
|
* for some of these formats.
|
2006-01-25 05:38:53 +05:30
|
|
|
* To keep diagnostics grammatical in English, the
|
2005-04-23 11:56:38 +05:30
|
|
|
* returned string must start with a consonant.
|
|
|
|
*/
|
|
|
|
if (S_ISREG(st->st_mode)) return st->st_size == 0 ? "regular empty file" : "regular file";
|
|
|
|
if (S_ISDIR(st->st_mode)) return "directory";
|
|
|
|
if (S_ISBLK(st->st_mode)) return "block special file";
|
|
|
|
if (S_ISCHR(st->st_mode)) return "character special file";
|
|
|
|
if (S_ISFIFO(st->st_mode)) return "fifo";
|
|
|
|
if (S_ISLNK(st->st_mode)) return "symbolic link";
|
|
|
|
if (S_ISSOCK(st->st_mode)) return "socket";
|
2012-06-03 20:13:06 +05:30
|
|
|
#ifdef S_TYPEISMQ
|
2005-04-23 11:56:38 +05:30
|
|
|
if (S_TYPEISMQ(st)) return "message queue";
|
2012-06-03 20:13:06 +05:30
|
|
|
#endif
|
|
|
|
#ifdef S_TYPEISSEM
|
2005-04-23 11:56:38 +05:30
|
|
|
if (S_TYPEISSEM(st)) return "semaphore";
|
2012-06-03 20:13:06 +05:30
|
|
|
#endif
|
|
|
|
#ifdef S_TYPEISSHM
|
2005-04-23 11:56:38 +05:30
|
|
|
if (S_TYPEISSHM(st)) return "shared memory object";
|
2012-06-03 20:13:06 +05:30
|
|
|
#endif
|
2005-04-23 11:56:38 +05:30
|
|
|
#ifdef S_TYPEISTMO
|
|
|
|
if (S_TYPEISTMO(st)) return "typed memory object";
|
|
|
|
#endif
|
|
|
|
return "weird file";
|
|
|
|
}
|
|
|
|
|
2007-10-06 02:01:23 +05:30
|
|
|
static const char *human_time(time_t t)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2006-11-21 05:42:09 +05:30
|
|
|
/* Old
|
2005-04-23 11:56:38 +05:30
|
|
|
static char *str;
|
|
|
|
str = ctime(&t);
|
|
|
|
str[strlen(str)-1] = '\0';
|
|
|
|
return str;
|
2006-11-21 05:42:09 +05:30
|
|
|
*/
|
|
|
|
/* coreutils 6.3 compat: */
|
2007-04-04 19:28:33 +05:30
|
|
|
|
2007-10-06 02:01:23 +05:30
|
|
|
/*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
|
2016-04-21 22:08:51 +05:30
|
|
|
#define buf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
|
|
|
strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
|
2006-11-21 05:42:09 +05:30
|
|
|
return buf;
|
2007-10-06 02:01:23 +05:30
|
|
|
#undef buf
|
2005-04-23 11:56:38 +05:30
|
|
|
}
|
|
|
|
|
2015-10-31 02:40:44 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FILESYSTEM
|
2019-03-18 16:44:52 +05:30
|
|
|
#define FS_TYPE_LIST \
|
|
|
|
FS_TYPE(0xADFF, "affs") \
|
|
|
|
FS_TYPE(0x1CD1, "devpts") \
|
|
|
|
FS_TYPE(0x137D, "ext") \
|
|
|
|
FS_TYPE(0xEF51, "ext2") \
|
|
|
|
FS_TYPE(0xEF53, "ext2/ext3") \
|
|
|
|
FS_TYPE(0x3153464a, "jfs") \
|
|
|
|
FS_TYPE(0x58465342, "xfs") \
|
|
|
|
FS_TYPE(0xF995E849, "hpfs") \
|
|
|
|
FS_TYPE(0x9660, "isofs") \
|
|
|
|
FS_TYPE(0x4000, "isofs") \
|
|
|
|
FS_TYPE(0x4004, "isofs") \
|
|
|
|
FS_TYPE(0x137F, "minix") \
|
|
|
|
FS_TYPE(0x138F, "minix (30 char.)") \
|
|
|
|
FS_TYPE(0x2468, "minix v2") \
|
|
|
|
FS_TYPE(0x2478, "minix v2 (30 char.)") \
|
|
|
|
FS_TYPE(0x4d44, "msdos") \
|
|
|
|
FS_TYPE(0x4006, "fat") \
|
|
|
|
FS_TYPE(0x564c, "novell") \
|
|
|
|
FS_TYPE(0x6969, "nfs") \
|
|
|
|
FS_TYPE(0x9fa0, "proc") \
|
|
|
|
FS_TYPE(0x517B, "smb") \
|
|
|
|
FS_TYPE(0x012FF7B4, "xenix") \
|
|
|
|
FS_TYPE(0x012FF7B5, "sysv4") \
|
|
|
|
FS_TYPE(0x012FF7B6, "sysv2") \
|
|
|
|
FS_TYPE(0x012FF7B7, "coh") \
|
|
|
|
FS_TYPE(0x00011954, "ufs") \
|
|
|
|
FS_TYPE(0x012FD16D, "xia") \
|
|
|
|
FS_TYPE(0x5346544e, "ntfs") \
|
|
|
|
FS_TYPE(0x1021994, "tmpfs") \
|
|
|
|
FS_TYPE(0x52654973, "reiserfs") \
|
|
|
|
FS_TYPE(0x28cd3d45, "cramfs") \
|
|
|
|
FS_TYPE(0x7275, "romfs") \
|
|
|
|
FS_TYPE(0x858458f6, "ramfs") \
|
|
|
|
FS_TYPE(0x73717368, "squashfs") \
|
|
|
|
FS_TYPE(0x62656572, "sysfs")
|
2005-04-23 11:56:38 +05:30
|
|
|
/* Return the type of the specified file system.
|
|
|
|
* Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
|
|
|
|
* Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
|
|
|
|
* Still others have neither and have to get by with f_type (Linux).
|
|
|
|
*/
|
2007-10-06 02:01:23 +05:30
|
|
|
static const char *human_fstype(uint32_t f_type)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2019-03-18 16:44:52 +05:30
|
|
|
# define FS_TYPE(type, name) type,
|
|
|
|
static const uint32_t fstype[] = {
|
|
|
|
FS_TYPE_LIST
|
2005-04-24 09:41:44 +05:30
|
|
|
};
|
2019-03-18 16:44:52 +05:30
|
|
|
# undef FS_TYPE
|
|
|
|
# define FS_TYPE(type, name) name"\0"
|
|
|
|
static const char humanname[] ALIGN1 =
|
|
|
|
FS_TYPE_LIST
|
|
|
|
"UNKNOWN";
|
|
|
|
# undef FS_TYPE
|
2007-10-06 02:01:23 +05:30
|
|
|
int i;
|
|
|
|
|
2019-03-18 16:44:52 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(fstype); ++i)
|
|
|
|
if (fstype[i] == f_type)
|
2006-02-22 22:31:00 +05:30
|
|
|
break;
|
2019-03-18 16:44:52 +05:30
|
|
|
return nth_string(humanname, i);
|
2005-04-23 11:56:38 +05:30
|
|
|
}
|
|
|
|
|
2008-11-12 03:55:34 +05:30
|
|
|
/* "man statfs" says that statfsbuf->f_fsid is a mess */
|
|
|
|
/* coreutils treats it as an array of ints, most significant first */
|
|
|
|
static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
|
|
|
|
{
|
|
|
|
const unsigned *p = (const void*) &statfsbuf->f_fsid;
|
|
|
|
unsigned sz = sizeof(statfsbuf->f_fsid) / sizeof(unsigned);
|
|
|
|
unsigned long long r = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
r = (r << (sizeof(unsigned)*8)) | *p++;
|
|
|
|
while (--sz > 0);
|
|
|
|
return r;
|
|
|
|
}
|
2015-10-31 02:40:44 +05:30
|
|
|
#endif /* FEATURE_STAT_FILESYSTEM */
|
2008-11-12 03:55:34 +05:30
|
|
|
|
2008-11-14 03:23:32 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FORMAT
|
|
|
|
static void strcatc(char *str, char c)
|
|
|
|
{
|
|
|
|
int len = strlen(str);
|
|
|
|
str[len++] = c;
|
|
|
|
str[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printfs(char *pformat, const char *msg)
|
|
|
|
{
|
|
|
|
strcatc(pformat, 's');
|
|
|
|
printf(pformat, msg);
|
|
|
|
}
|
|
|
|
|
2015-10-31 02:40:44 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FILESYSTEM
|
2005-04-23 11:56:38 +05:30
|
|
|
/* print statfs info */
|
2010-02-02 07:38:57 +05:30
|
|
|
static void FAST_FUNC print_statfs(char *pformat, const char m,
|
2007-10-06 02:01:23 +05:30
|
|
|
const char *const filename, const void *data
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_SELINUX(, security_context_t scontext))
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2007-10-06 02:01:23 +05:30
|
|
|
const struct statfs *statfsbuf = data;
|
2007-04-04 19:28:33 +05:30
|
|
|
if (m == 'n') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, filename);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'i') {
|
2008-11-12 03:55:34 +05:30
|
|
|
strcat(pformat, "llx");
|
|
|
|
printf(pformat, get_f_fsid(statfsbuf));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'l') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(pformat, (unsigned long) statfsbuf->f_namelen);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 't') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lx");
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'T') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, human_fstype(statfsbuf->f_type));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'b') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statfsbuf->f_blocks);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'f') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statfsbuf->f_bfree);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'a') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statfsbuf->f_bavail);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 's' || m == 'S') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(pformat, (unsigned long) statfsbuf->f_bsize);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'c') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statfsbuf->f_files);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'd') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statfsbuf->f_ffree);
|
|
|
|
# if ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, scontext);
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2007-04-04 19:28:33 +05:30
|
|
|
} else {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcatc(pformat, 'c');
|
2005-04-23 11:56:38 +05:30
|
|
|
printf(pformat, m);
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 02:40:44 +05:30
|
|
|
#endif
|
2005-04-23 11:56:38 +05:30
|
|
|
|
|
|
|
/* print stat info */
|
2010-02-02 07:38:57 +05:30
|
|
|
static void FAST_FUNC print_stat(char *pformat, const char m,
|
2007-10-06 02:01:23 +05:30
|
|
|
const char *const filename, const void *data
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_SELINUX(, security_context_t scontext))
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
|
|
|
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
|
|
|
|
struct stat *statbuf = (struct stat *) data;
|
|
|
|
struct passwd *pw_ent;
|
|
|
|
struct group *gw_ent;
|
|
|
|
|
2007-04-04 19:28:33 +05:30
|
|
|
if (m == 'n') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, filename);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'N') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcatc(pformat, 's');
|
2005-04-23 11:56:38 +05:30
|
|
|
if (S_ISLNK(statbuf->st_mode)) {
|
2007-02-11 21:49:28 +05:30
|
|
|
char *linkname = xmalloc_readlink_or_warn(filename);
|
2007-10-06 02:01:23 +05:30
|
|
|
if (linkname == NULL)
|
2005-04-23 11:56:38 +05:30
|
|
|
return;
|
2010-06-13 01:27:50 +05:30
|
|
|
printf("'%s' -> '%s'", filename, linkname);
|
2007-10-06 02:01:23 +05:30
|
|
|
free(linkname);
|
2005-04-23 11:56:38 +05:30
|
|
|
} else {
|
|
|
|
printf(pformat, filename);
|
|
|
|
}
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'd') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statbuf->st_dev);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'D') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llx");
|
|
|
|
printf(pformat, (unsigned long long) statbuf->st_dev);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'i') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statbuf->st_ino);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'a') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lo");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'A') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, bb_mode_string(statbuf->st_mode));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'f') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lx");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) statbuf->st_mode);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'F') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, file_type(statbuf));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'h') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) statbuf->st_nlink);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'u') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) statbuf->st_uid);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'U') {
|
2005-04-23 11:56:38 +05:30
|
|
|
pw_ent = getpwuid(statbuf->st_uid);
|
2008-06-28 04:01:07 +05:30
|
|
|
printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'g') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) statbuf->st_gid);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'G') {
|
2005-04-23 11:56:38 +05:30
|
|
|
gw_ent = getgrgid(statbuf->st_gid);
|
2008-06-28 04:01:07 +05:30
|
|
|
printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 't') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lx");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) major(statbuf->st_rdev));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'T') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lx");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) minor(statbuf->st_rdev));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 's') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statbuf->st_size);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'B') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'b') {
|
2010-02-02 07:38:57 +05:30
|
|
|
strcat(pformat, "llu");
|
|
|
|
printf(pformat, (unsigned long long) statbuf->st_blocks);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'o') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, "lu");
|
2007-04-14 04:52:00 +05:30
|
|
|
printf(pformat, (unsigned long) statbuf->st_blksize);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'x') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, human_time(statbuf->st_atime));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'X') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
|
2010-02-02 07:38:57 +05:30
|
|
|
/* note: (unsigned long) would be wrong:
|
|
|
|
* imagine (unsigned long64)int32 */
|
|
|
|
printf(pformat, (long) statbuf->st_atime);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'y') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, human_time(statbuf->st_mtime));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'Y') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(pformat, (long) statbuf->st_mtime);
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'z') {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, human_time(statbuf->st_ctime));
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'Z') {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(pformat, (long) statbuf->st_ctime);
|
|
|
|
# if ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
} else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
|
2007-10-06 02:01:23 +05:30
|
|
|
printfs(pformat, scontext);
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2007-04-04 19:28:33 +05:30
|
|
|
} else {
|
2007-10-06 02:01:23 +05:30
|
|
|
strcatc(pformat, 'c');
|
2005-04-23 11:56:38 +05:30
|
|
|
printf(pformat, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-02 07:38:57 +05:30
|
|
|
static void print_it(const char *masterformat,
|
|
|
|
const char *filename,
|
|
|
|
void FAST_FUNC (*print_func)(char*, char, const char*, const void* IF_SELINUX(, security_context_t scontext)),
|
2007-10-06 02:01:23 +05:30
|
|
|
const void *data
|
2010-02-02 07:38:57 +05:30
|
|
|
IF_SELINUX(, security_context_t scontext))
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2007-10-06 02:01:23 +05:30
|
|
|
/* Create a working copy of the format string */
|
2006-08-03 21:11:12 +05:30
|
|
|
char *format = xstrdup(masterformat);
|
2015-05-25 17:16:36 +05:30
|
|
|
/* Add 2 to accommodate our conversion of the stat '%s' format string
|
2006-11-21 05:42:09 +05:30
|
|
|
* to the printf '%llu' one. */
|
2007-10-06 02:01:23 +05:30
|
|
|
char *dest = xmalloc(strlen(format) + 2 + 1);
|
|
|
|
char *b;
|
2005-04-23 11:56:38 +05:30
|
|
|
|
|
|
|
b = format;
|
|
|
|
while (b) {
|
2010-06-13 01:27:50 +05:30
|
|
|
/* Each iteration finds next %spec,
|
|
|
|
* prints preceding string and handles found %spec
|
|
|
|
*/
|
2006-11-21 05:42:09 +05:30
|
|
|
size_t len;
|
2005-04-23 11:56:38 +05:30
|
|
|
char *p = strchr(b, '%');
|
2006-11-21 05:42:09 +05:30
|
|
|
if (!p) {
|
2010-06-13 01:27:50 +05:30
|
|
|
/* coreutils 6.3 always prints newline at the end */
|
2006-11-21 05:42:09 +05:30
|
|
|
/*fputs(b, stdout);*/
|
|
|
|
puts(b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-10-06 02:01:23 +05:30
|
|
|
/* dest = "%<modifiers>" */
|
2010-06-13 01:27:50 +05:30
|
|
|
len = 1 + strspn(p + 1, "#-+.I 0123456789");
|
|
|
|
memcpy(dest, p, len);
|
|
|
|
dest[len] = '\0';
|
2006-11-21 05:42:09 +05:30
|
|
|
|
2010-06-13 01:27:50 +05:30
|
|
|
/* print preceding string */
|
|
|
|
*p = '\0';
|
|
|
|
fputs(b, stdout);
|
|
|
|
|
|
|
|
p += len;
|
2006-11-21 05:42:09 +05:30
|
|
|
b = p + 1;
|
|
|
|
switch (*p) {
|
|
|
|
case '\0':
|
2005-04-23 11:56:38 +05:30
|
|
|
b = NULL;
|
2006-11-21 05:42:09 +05:30
|
|
|
/* fall through */
|
|
|
|
case '%':
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('%');
|
2006-11-21 05:42:09 +05:30
|
|
|
break;
|
|
|
|
default:
|
2007-10-06 02:01:23 +05:30
|
|
|
/* Completes "%<modifiers>" with specifier and printfs */
|
2009-04-21 16:39:40 +05:30
|
|
|
print_func(dest, *p, filename, data IF_SELINUX(,scontext));
|
2006-11-21 05:42:09 +05:30
|
|
|
break;
|
2005-04-23 11:56:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(format);
|
|
|
|
free(dest);
|
|
|
|
}
|
2010-02-02 07:38:57 +05:30
|
|
|
#endif /* FEATURE_STAT_FORMAT */
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2015-10-31 02:40:44 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FILESYSTEM
|
2005-04-23 11:56:38 +05:30
|
|
|
/* Stat the file system and print what we find. */
|
2008-03-17 14:34:04 +05:30
|
|
|
#if !ENABLE_FEATURE_STAT_FORMAT
|
|
|
|
#define do_statfs(filename, format) do_statfs(filename)
|
|
|
|
#endif
|
2007-10-06 02:01:23 +05:30
|
|
|
static bool do_statfs(const char *filename, const char *format)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2008-11-12 03:55:34 +05:30
|
|
|
struct statfs statfsbuf;
|
2008-03-17 14:34:04 +05:30
|
|
|
#if !ENABLE_FEATURE_STAT_FORMAT
|
|
|
|
const char *format;
|
|
|
|
#endif
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
|
|
|
security_context_t scontext = NULL;
|
2007-04-04 19:28:33 +05:30
|
|
|
|
|
|
|
if (option_mask32 & OPT_SELINUX) {
|
2007-08-09 13:40:13 +05:30
|
|
|
if ((option_mask32 & OPT_DEREFERENCE
|
|
|
|
? lgetfilecon(filename, &scontext)
|
|
|
|
: getfilecon(filename, &scontext)
|
|
|
|
) < 0
|
|
|
|
) {
|
2012-09-27 19:05:10 +05:30
|
|
|
bb_simple_perror_msg(filename);
|
2007-03-10 22:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-23 11:56:38 +05:30
|
|
|
if (statfs(filename, &statfsbuf) != 0) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg("can't read file system information for '%s'", filename);
|
2005-04-23 11:56:38 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-08 21:37:02 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FORMAT
|
2007-10-06 02:01:23 +05:30
|
|
|
if (format == NULL) {
|
2010-02-02 07:38:57 +05:30
|
|
|
# if !ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_TERSE
|
2005-07-27 04:09:56 +05:30
|
|
|
? "%n %i %l %t %s %b %f %a %c %d\n"
|
2005-04-23 11:56:38 +05:30
|
|
|
: " File: \"%n\"\n"
|
|
|
|
" ID: %-8i Namelen: %-7l Type: %T\n"
|
2005-07-27 04:09:56 +05:30
|
|
|
"Block size: %-10s\n"
|
2005-04-23 11:56:38 +05:30
|
|
|
"Blocks: Total: %-10b Free: %-10f Available: %a\n"
|
2006-11-21 05:42:09 +05:30
|
|
|
"Inodes: Total: %-10c Free: %d");
|
2010-02-02 07:38:57 +05:30
|
|
|
# else
|
2007-10-06 02:01:23 +05:30
|
|
|
format = (option_mask32 & OPT_TERSE
|
2007-04-04 19:28:33 +05:30
|
|
|
? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
|
2007-03-10 22:28:49 +05:30
|
|
|
"%n %i %l %t %s %b %f %a %c %d\n")
|
2007-04-04 19:28:33 +05:30
|
|
|
: (option_mask32 & OPT_SELINUX ?
|
2007-03-10 22:28:49 +05:30
|
|
|
" File: \"%n\"\n"
|
|
|
|
" ID: %-8i Namelen: %-7l Type: %T\n"
|
|
|
|
"Block size: %-10s\n"
|
|
|
|
"Blocks: Total: %-10b Free: %-10f Available: %a\n"
|
|
|
|
"Inodes: Total: %-10c Free: %d"
|
|
|
|
" S_context: %C\n":
|
|
|
|
" File: \"%n\"\n"
|
|
|
|
" ID: %-8i Namelen: %-7l Type: %T\n"
|
|
|
|
"Block size: %-10s\n"
|
|
|
|
"Blocks: Total: %-10b Free: %-10f Available: %a\n"
|
|
|
|
"Inodes: Total: %-10c Free: %d\n")
|
|
|
|
);
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif /* SELINUX */
|
2007-10-06 02:01:23 +05:30
|
|
|
}
|
2009-04-21 16:39:40 +05:30
|
|
|
print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
|
2007-03-10 22:28:49 +05:30
|
|
|
#else /* FEATURE_STAT_FORMAT */
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_TERSE
|
2006-03-30 04:04:47 +05:30
|
|
|
? "%s %llx %lu "
|
2005-04-23 11:56:38 +05:30
|
|
|
: " File: \"%s\"\n"
|
2008-11-12 03:55:34 +05:30
|
|
|
" ID: %-8llx Namelen: %-7lu ");
|
2005-04-23 11:56:38 +05:30
|
|
|
printf(format,
|
|
|
|
filename,
|
2008-11-12 03:55:34 +05:30
|
|
|
get_f_fsid(&statfsbuf),
|
2005-04-23 11:56:38 +05:30
|
|
|
statfsbuf.f_namelen);
|
|
|
|
|
2007-04-04 19:28:33 +05:30
|
|
|
if (option_mask32 & OPT_TERSE)
|
2010-02-02 07:38:57 +05:30
|
|
|
printf("%lx ", (unsigned long) statfsbuf.f_type);
|
2005-04-23 11:56:38 +05:30
|
|
|
else
|
|
|
|
printf("Type: %s\n", human_fstype(statfsbuf.f_type));
|
|
|
|
|
2010-02-02 07:38:57 +05:30
|
|
|
# if !ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_TERSE
|
2010-02-02 07:38:57 +05:30
|
|
|
? "%lu %llu %llu %llu %llu %llu\n"
|
2005-07-27 04:09:56 +05:30
|
|
|
: "Block size: %-10lu\n"
|
2010-02-02 07:38:57 +05:30
|
|
|
"Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
|
|
|
|
"Inodes: Total: %-10llu Free: %llu\n");
|
2005-04-23 11:56:38 +05:30
|
|
|
printf(format,
|
2010-02-02 07:38:57 +05:30
|
|
|
(unsigned long) statfsbuf.f_bsize,
|
|
|
|
(unsigned long long) statfsbuf.f_blocks,
|
|
|
|
(unsigned long long) statfsbuf.f_bfree,
|
|
|
|
(unsigned long long) statfsbuf.f_bavail,
|
|
|
|
(unsigned long long) statfsbuf.f_files,
|
|
|
|
(unsigned long long) statfsbuf.f_ffree);
|
|
|
|
# else
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_TERSE
|
2010-02-02 07:38:57 +05:30
|
|
|
? (option_mask32 & OPT_SELINUX ? "%lu %llu %llu %llu %llu %llu %C\n" : "%lu %llu %llu %llu %llu %llu\n")
|
|
|
|
: (option_mask32 & OPT_SELINUX
|
|
|
|
? "Block size: %-10lu\n"
|
|
|
|
"Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
|
|
|
|
"Inodes: Total: %-10llu Free: %llu"
|
|
|
|
"S_context: %C\n"
|
|
|
|
: "Block size: %-10lu\n"
|
|
|
|
"Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
|
|
|
|
"Inodes: Total: %-10llu Free: %llu\n"
|
|
|
|
)
|
|
|
|
);
|
2007-03-10 22:28:49 +05:30
|
|
|
printf(format,
|
2010-02-02 07:38:57 +05:30
|
|
|
(unsigned long) statfsbuf.f_bsize,
|
|
|
|
(unsigned long long) statfsbuf.f_blocks,
|
|
|
|
(unsigned long long) statfsbuf.f_bfree,
|
|
|
|
(unsigned long long) statfsbuf.f_bavail,
|
|
|
|
(unsigned long long) statfsbuf.f_files,
|
|
|
|
(unsigned long long) statfsbuf.f_ffree,
|
2007-03-10 22:28:49 +05:30
|
|
|
scontext);
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2007-03-10 22:28:49 +05:30
|
|
|
if (scontext)
|
|
|
|
freecon(scontext);
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2010-10-28 22:27:19 +05:30
|
|
|
#endif /* FEATURE_STAT_FORMAT */
|
2005-04-23 11:56:38 +05:30
|
|
|
return 1;
|
|
|
|
}
|
2015-10-31 02:40:44 +05:30
|
|
|
#endif /* FEATURE_STAT_FILESYSTEM */
|
2005-04-23 11:56:38 +05:30
|
|
|
|
|
|
|
/* stat the file and print what we find */
|
2008-03-17 14:34:04 +05:30
|
|
|
#if !ENABLE_FEATURE_STAT_FORMAT
|
|
|
|
#define do_stat(filename, format) do_stat(filename)
|
|
|
|
#endif
|
2007-10-06 02:01:23 +05:30
|
|
|
static bool do_stat(const char *filename, const char *format)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
|
|
|
security_context_t scontext = NULL;
|
2007-04-04 19:28:33 +05:30
|
|
|
|
|
|
|
if (option_mask32 & OPT_SELINUX) {
|
2007-08-09 13:40:13 +05:30
|
|
|
if ((option_mask32 & OPT_DEREFERENCE
|
|
|
|
? lgetfilecon(filename, &scontext)
|
|
|
|
: getfilecon(filename, &scontext)
|
|
|
|
) < 0
|
|
|
|
) {
|
2012-09-27 19:05:10 +05:30
|
|
|
bb_simple_perror_msg(filename);
|
2007-03-10 22:28:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-04 19:28:33 +05:30
|
|
|
if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg("can't stat '%s'", filename);
|
2005-04-23 11:56:38 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-08 21:37:02 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FORMAT
|
2005-04-23 11:56:38 +05:30
|
|
|
if (format == NULL) {
|
2010-02-02 07:38:57 +05:30
|
|
|
# if !ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
if (option_mask32 & OPT_TERSE) {
|
2006-11-21 05:42:09 +05:30
|
|
|
format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
|
2005-04-23 11:56:38 +05:30
|
|
|
} else {
|
|
|
|
if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
|
|
|
|
format =
|
2010-06-13 01:27:50 +05:30
|
|
|
" File: %N\n"
|
2005-04-23 11:56:38 +05:30
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %-5h"
|
|
|
|
" Device type: %t,%T\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n";
|
|
|
|
} else {
|
|
|
|
format =
|
2010-06-13 01:27:50 +05:30
|
|
|
" File: %N\n"
|
2005-04-23 11:56:38 +05:30
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %h\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n";
|
|
|
|
}
|
|
|
|
}
|
2010-02-02 07:38:57 +05:30
|
|
|
# else
|
2007-04-04 19:28:33 +05:30
|
|
|
if (option_mask32 & OPT_TERSE) {
|
|
|
|
format = (option_mask32 & OPT_SELINUX ?
|
2013-01-14 20:27:44 +05:30
|
|
|
"%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n"
|
|
|
|
:
|
|
|
|
"%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n"
|
|
|
|
);
|
2007-03-10 22:28:49 +05:30
|
|
|
} else {
|
|
|
|
if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_SELINUX ?
|
2013-01-14 20:27:44 +05:30
|
|
|
" File: %N\n"
|
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %-5h"
|
|
|
|
" Device type: %t,%T\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
" S_Context: %C\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n"
|
|
|
|
:
|
|
|
|
" File: %N\n"
|
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %-5h"
|
|
|
|
" Device type: %t,%T\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n"
|
|
|
|
);
|
2007-03-10 22:28:49 +05:30
|
|
|
} else {
|
2007-04-04 19:28:33 +05:30
|
|
|
format = (option_mask32 & OPT_SELINUX ?
|
2013-01-14 20:27:44 +05:30
|
|
|
" File: %N\n"
|
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %h\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
"S_Context: %C\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n"
|
|
|
|
:
|
|
|
|
" File: %N\n"
|
|
|
|
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
|
|
|
|
"Device: %Dh/%dd\tInode: %-10i Links: %h\n"
|
|
|
|
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
|
|
|
|
"Access: %x\n" "Modify: %y\n" "Change: %z\n"
|
|
|
|
);
|
2007-03-10 22:28:49 +05:30
|
|
|
}
|
|
|
|
}
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2007-03-10 22:28:49 +05:30
|
|
|
}
|
2009-04-21 16:39:40 +05:30
|
|
|
print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
|
2007-03-10 22:28:49 +05:30
|
|
|
#else /* FEATURE_STAT_FORMAT */
|
2007-04-04 19:28:33 +05:30
|
|
|
if (option_mask32 & OPT_TERSE) {
|
2010-02-02 07:38:57 +05:30
|
|
|
printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NOT_SELINUX("\n"),
|
2005-04-23 11:56:38 +05:30
|
|
|
filename,
|
2010-02-02 07:38:57 +05:30
|
|
|
(unsigned long long) statbuf.st_size,
|
|
|
|
(unsigned long long) statbuf.st_blocks,
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_mode,
|
|
|
|
(unsigned long) statbuf.st_uid,
|
|
|
|
(unsigned long) statbuf.st_gid,
|
2010-02-02 07:38:57 +05:30
|
|
|
(unsigned long long) statbuf.st_dev,
|
|
|
|
(unsigned long long) statbuf.st_ino,
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_nlink,
|
|
|
|
(unsigned long) major(statbuf.st_rdev),
|
|
|
|
(unsigned long) minor(statbuf.st_rdev),
|
|
|
|
(unsigned long) statbuf.st_atime,
|
|
|
|
(unsigned long) statbuf.st_mtime,
|
|
|
|
(unsigned long) statbuf.st_ctime,
|
|
|
|
(unsigned long) statbuf.st_blksize
|
2005-04-23 11:56:38 +05:30
|
|
|
);
|
2010-02-02 07:38:57 +05:30
|
|
|
# if ENABLE_SELINUX
|
2007-04-04 19:28:33 +05:30
|
|
|
if (option_mask32 & OPT_SELINUX)
|
2014-06-27 17:38:29 +05:30
|
|
|
printf(" %s\n", scontext);
|
2007-03-10 22:28:49 +05:30
|
|
|
else
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2005-04-23 11:56:38 +05:30
|
|
|
} else {
|
|
|
|
char *linkname = NULL;
|
|
|
|
struct passwd *pw_ent;
|
|
|
|
struct group *gw_ent;
|
2010-10-21 03:54:05 +05:30
|
|
|
|
2005-04-23 11:56:38 +05:30
|
|
|
gw_ent = getgrgid(statbuf.st_gid);
|
|
|
|
pw_ent = getpwuid(statbuf.st_uid);
|
|
|
|
|
|
|
|
if (S_ISLNK(statbuf.st_mode))
|
2007-02-11 21:49:28 +05:30
|
|
|
linkname = xmalloc_readlink_or_warn(filename);
|
2010-10-21 03:54:05 +05:30
|
|
|
if (linkname) {
|
2010-06-13 01:27:50 +05:30
|
|
|
printf(" File: '%s' -> '%s'\n", filename, linkname);
|
2010-10-21 03:54:05 +05:30
|
|
|
free(linkname);
|
|
|
|
} else {
|
2010-06-13 01:27:50 +05:30
|
|
|
printf(" File: '%s'\n", filename);
|
2010-10-21 03:54:05 +05:30
|
|
|
}
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2010-02-02 07:38:57 +05:30
|
|
|
printf(" Size: %-10llu\tBlocks: %-10llu IO Block: %-6lu %s\n"
|
|
|
|
"Device: %llxh/%llud\tInode: %-10llu Links: %-5lu",
|
|
|
|
(unsigned long long) statbuf.st_size,
|
|
|
|
(unsigned long long) statbuf.st_blocks,
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_blksize,
|
2005-04-23 11:56:38 +05:30
|
|
|
file_type(&statbuf),
|
2010-02-02 07:38:57 +05:30
|
|
|
(unsigned long long) statbuf.st_dev,
|
|
|
|
(unsigned long long) statbuf.st_dev,
|
|
|
|
(unsigned long long) statbuf.st_ino,
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_nlink);
|
2005-04-23 11:56:38 +05:30
|
|
|
if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
|
|
|
|
printf(" Device type: %lx,%lx\n",
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) major(statbuf.st_rdev),
|
|
|
|
(unsigned long) minor(statbuf.st_rdev));
|
2005-04-23 11:56:38 +05:30
|
|
|
else
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2007-03-10 22:28:49 +05:30
|
|
|
printf("Access: (%04lo/%10.10s) Uid: (%5lu/%8s) Gid: (%5lu/%8s)\n",
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
|
2005-04-23 11:56:38 +05:30
|
|
|
bb_mode_string(statbuf.st_mode),
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_uid,
|
2008-06-28 04:01:07 +05:30
|
|
|
(pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
|
2007-04-14 04:52:00 +05:30
|
|
|
(unsigned long) statbuf.st_gid,
|
2008-06-28 04:01:07 +05:30
|
|
|
(gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
|
2010-02-02 07:38:57 +05:30
|
|
|
# if ENABLE_SELINUX
|
2014-06-27 17:38:29 +05:30
|
|
|
if (option_mask32 & OPT_SELINUX)
|
|
|
|
printf(" S_Context: %s\n", scontext);
|
2010-02-02 07:38:57 +05:30
|
|
|
# endif
|
2010-10-30 06:18:20 +05:30
|
|
|
printf("Access: %s\n", human_time(statbuf.st_atime));
|
|
|
|
printf("Modify: %s\n", human_time(statbuf.st_mtime));
|
|
|
|
printf("Change: %s\n", human_time(statbuf.st_ctime));
|
2005-04-23 11:56:38 +05:30
|
|
|
}
|
2010-10-28 22:27:19 +05:30
|
|
|
#endif /* FEATURE_STAT_FORMAT */
|
2005-04-23 11:56:38 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2009-11-28 19:48:53 +05:30
|
|
|
int stat_main(int argc UNUSED_PARAM, char **argv)
|
2005-04-23 11:56:38 +05:30
|
|
|
{
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_STAT_FORMAT(char *format = NULL;)
|
2007-04-04 19:28:33 +05:30
|
|
|
int i;
|
2009-11-28 19:48:53 +05:30
|
|
|
int ok;
|
2008-03-17 14:34:04 +05:30
|
|
|
statfunc_ptr statfunc = do_stat;
|
2017-12-31 22:00:02 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
|
|
|
|
unsigned opts;
|
2005-04-23 11:56:38 +05:30
|
|
|
|
2017-12-31 22:00:02 +05:30
|
|
|
opts =
|
|
|
|
#endif
|
|
|
|
getopt32(argv, "^"
|
2017-08-09 01:25:02 +05:30
|
|
|
"tL"
|
2015-10-31 02:40:44 +05:30
|
|
|
IF_FEATURE_STAT_FILESYSTEM("f")
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_SELINUX("Z")
|
2017-08-09 01:25:02 +05:30
|
|
|
IF_FEATURE_STAT_FORMAT("c:")
|
|
|
|
"\0" "-1" /* min one arg */
|
|
|
|
IF_FEATURE_STAT_FORMAT(,&format)
|
2005-04-23 11:56:38 +05:30
|
|
|
);
|
2015-10-31 02:40:44 +05:30
|
|
|
#if ENABLE_FEATURE_STAT_FILESYSTEM
|
2009-11-28 19:48:53 +05:30
|
|
|
if (opts & OPT_FILESYS) /* -f */
|
2005-04-23 11:56:38 +05:30
|
|
|
statfunc = do_statfs;
|
2015-10-31 02:40:44 +05:30
|
|
|
#endif
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
2009-11-28 19:48:53 +05:30
|
|
|
if (opts & OPT_SELINUX) {
|
2007-03-10 22:28:49 +05:30
|
|
|
selinux_or_die();
|
|
|
|
}
|
2009-11-28 19:48:53 +05:30
|
|
|
#endif
|
|
|
|
ok = 1;
|
|
|
|
argv += optind;
|
|
|
|
for (i = 0; argv[i]; ++i)
|
2009-04-21 16:39:40 +05:30
|
|
|
ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
|
2005-04-23 11:56:38 +05:30
|
|
|
|
|
|
|
return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|