bc: convert to "G trick" - this returns bc to zero bss increase
[oweals/busybox.git] / coreutils / stat.c
index 4c729e0716f06b35ab30045ce3c097fa01f98943..3d527ae63d5b1ccf2a7094c97e5c5ac3d97d71e0 100644 (file)
  * Written by Michael Meskes
  * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//config:config STAT
+//config:      bool "stat (10 kb)"
+//config:      default y
+//config:      help
+//config:      display file or filesystem status.
+//config:
+//config:config FEATURE_STAT_FORMAT
+//config:      bool "Enable custom formats (-c)"
+//config:      default y
+//config:      depends on STAT
+//config:      help
+//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.
+//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
+//config:      Without this, stat will not support the '-f' option to display
+//config:      information about filesystem status.
+
+//applet:IF_STAT(APPLET_NOEXEC(stat, stat, BB_DIR_BIN, BB_SUID_DROP, stat))
+
+//kbuild:lib-$(CONFIG_STAT) += stat.o
+
+//usage:#define stat_trivial_usage
+//usage:       "[OPTIONS] FILE..."
+//usage:#define stat_full_usage "\n\n"
+//usage:       "Display file"
+//usage:            IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
+//usage:            " status\n"
+//usage:       IF_FEATURE_STAT_FORMAT(
+//usage:     "\n       -c FMT  Use the specified format"
+//usage:       )
+//usage:       IF_FEATURE_STAT_FILESYSTEM(
+//usage:     "\n       -f      Display filesystem status"
+//usage:       )
+//usage:     "\n       -L      Follow links"
+//usage:     "\n       -t      Terse display"
+//usage:       IF_SELINUX(
+//usage:     "\n       -Z      Print security context"
+//usage:       )
+//usage:       IF_FEATURE_STAT_FORMAT(
+//usage:       "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
+//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"
+//usage:       " %B    Size in bytes of each block reported by %b\n"
+//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"
+//usage:       " %g    Group ID\n"
+//usage:       " %G    Group name\n"
+//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"
+//usage:       " %s    Total size in bytes\n"
+//usage:       " %t    Major device type in hex\n"
+//usage:       " %T    Minor device type in hex\n"
+//usage:       " %u    User ID\n"
+//usage:       " %U    User name\n"
+//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"
+//usage:       IF_FEATURE_STAT_FILESYSTEM(
+//usage:       "\nFMT sequences for file systems:\n"
+//usage:       " %a    Free blocks available to non-superuser\n"
+//usage:       " %b    Total data blocks\n"
+//usage:       " %c    Total file nodes\n"
+//usage:       " %d    Free file nodes\n"
+//usage:       " %f    Free blocks\n"
+//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:       )
+//usage:       )
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
-/* vars to control behavior */
-#define OPT_FILESYS     (1 << 0)
-#define OPT_TERSE       (1 << 1)
-#define OPT_DEREFERENCE (1 << 2)
-#define OPT_SELINUX     (1 << 3)
+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,
+};
 
 #if ENABLE_FEATURE_STAT_FORMAT
 typedef bool (*statfunc_ptr)(const char *, const char *);
@@ -41,9 +135,15 @@ static const char *file_type(const struct stat *st)
        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";
+#ifdef S_TYPEISMQ
        if (S_TYPEISMQ(st))        return "message queue";
+#endif
+#ifdef S_TYPEISSEM
        if (S_TYPEISSEM(st))       return "semaphore";
+#endif
+#ifdef S_TYPEISSHM
        if (S_TYPEISSHM(st))       return "shared memory object";
+#endif
 #ifdef S_TYPEISTMO
        if (S_TYPEISTMO(st))       return "typed memory object";
 #endif
@@ -62,12 +162,13 @@ static const char *human_time(time_t t)
 
        /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
 #define buf bb_common_bufsiz1
-
-       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
+       setup_common_bufsiz();
+       strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
        return buf;
 #undef buf
 }
 
+#if ENABLE_FEATURE_STAT_FILESYSTEM
 /* 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)
@@ -111,7 +212,7 @@ static const char *human_fstype(uint32_t f_type)
                { 0x52654973, "reiserfs" },
                { 0x28cd3d45, "cramfs" },
                { 0x7275,     "romfs" },
-               { 0x858458f6, "romfs" },
+               { 0x858458f6, "ramfs" },
                { 0x73717368, "squashfs" },
                { 0x62656572, "sysfs" },
                { 0, "UNKNOWN" }
@@ -125,20 +226,6 @@ static const char *human_fstype(uint32_t f_type)
        return humantypes[i].fs;
 }
 
-#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);
-}
-
 /* "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)
@@ -152,11 +239,27 @@ static unsigned long long get_f_fsid(const struct statfs *statfsbuf)
        while (--sz > 0);
        return r;
 }
+#endif  /* FEATURE_STAT_FILESYSTEM */
+
+#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);
+}
 
+#if ENABLE_FEATURE_STAT_FILESYSTEM
 /* print statfs info */
-static void print_statfs(char *pformat, const char m,
+static void FAST_FUNC print_statfs(char *pformat, const char m,
                const char *const filename, const void *data
-               USE_SELINUX(, security_context_t scontext))
+               IF_SELINUX(, security_context_t scontext))
 {
        const struct statfs *statfsbuf = data;
        if (m == 'n') {
@@ -166,44 +269,45 @@ static void print_statfs(char *pformat, const char m,
                printf(pformat, get_f_fsid(statfsbuf));
        } else if (m == 'l') {
                strcat(pformat, "lu");
-               printf(pformat, (unsigned long) (statfsbuf->f_namelen));
+               printf(pformat, (unsigned long) statfsbuf->f_namelen);
        } else if (m == 't') {
                strcat(pformat, "lx");
-               printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
+               printf(pformat, (unsigned long) statfsbuf->f_type); /* no equiv */
        } else if (m == 'T') {
                printfs(pformat, human_fstype(statfsbuf->f_type));
        } else if (m == 'b') {
-               strcat(pformat, "jd");
-               printf(pformat, (intmax_t) (statfsbuf->f_blocks));
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statfsbuf->f_blocks);
        } else if (m == 'f') {
-               strcat(pformat, "jd");
-               printf(pformat, (intmax_t) (statfsbuf->f_bfree));
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statfsbuf->f_bfree);
        } else if (m == 'a') {
-               strcat(pformat, "jd");
-               printf(pformat, (intmax_t) (statfsbuf->f_bavail));
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statfsbuf->f_bavail);
        } else if (m == 's' || m == 'S') {
                strcat(pformat, "lu");
-               printf(pformat, (unsigned long) (statfsbuf->f_bsize));
+               printf(pformat, (unsigned long) statfsbuf->f_bsize);
        } else if (m == 'c') {
-               strcat(pformat, "jd");
-               printf(pformat, (intmax_t) (statfsbuf->f_files));
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statfsbuf->f_files);
        } else if (m == 'd') {
-               strcat(pformat, "jd");
-               printf(pformat, (intmax_t) (statfsbuf->f_ffree));
-#if ENABLE_SELINUX
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statfsbuf->f_ffree);
+# if ENABLE_SELINUX
        } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
                printfs(pformat, scontext);
-#endif
+# endif
        } else {
                strcatc(pformat, 'c');
                printf(pformat, m);
        }
 }
+#endif
 
 /* print stat info */
-static void print_stat(char *pformat, const char m,
+static void FAST_FUNC print_stat(char *pformat, const char m,
                const char *const filename, const void *data
-               USE_SELINUX(, security_context_t scontext))
+               IF_SELINUX(, security_context_t scontext))
 {
 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
        struct stat *statbuf = (struct stat *) data;
@@ -218,23 +322,20 @@ static void print_stat(char *pformat, const char m,
                        char *linkname = xmalloc_readlink_or_warn(filename);
                        if (linkname == NULL)
                                return;
-                       /*printf("\"%s\" -> \"%s\"", filename, linkname); */
-                       printf(pformat, filename);
-                       printf(" -> ");
-                       printf(pformat, linkname);
+                       printf("'%s' -> '%s'", filename, linkname);
                        free(linkname);
                } else {
                        printf(pformat, filename);
                }
        } else if (m == 'd') {
-               strcat(pformat, "ju");
-               printf(pformat, (uintmax_t) statbuf->st_dev);
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statbuf->st_dev);
        } else if (m == 'D') {
-               strcat(pformat, "jx");
-               printf(pformat, (uintmax_t) statbuf->st_dev);
+               strcat(pformat, "llx");
+               printf(pformat, (unsigned long long) statbuf->st_dev);
        } else if (m == 'i') {
-               strcat(pformat, "ju");
-               printf(pformat, (uintmax_t) statbuf->st_ino);
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statbuf->st_ino);
        } else if (m == 'a') {
                strcat(pformat, "lo");
                printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
@@ -252,14 +353,12 @@ static void print_stat(char *pformat, const char m,
                strcat(pformat, "lu");
                printf(pformat, (unsigned long) statbuf->st_uid);
        } else if (m == 'U') {
-               setpwent();
                pw_ent = getpwuid(statbuf->st_uid);
                printfs(pformat, (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN");
        } else if (m == 'g') {
                strcat(pformat, "lu");
                printf(pformat, (unsigned long) statbuf->st_gid);
        } else if (m == 'G') {
-               setgrent();
                gw_ent = getgrgid(statbuf->st_gid);
                printfs(pformat, (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
        } else if (m == 't') {
@@ -269,14 +368,14 @@ static void print_stat(char *pformat, const char m,
                strcat(pformat, "lx");
                printf(pformat, (unsigned long) minor(statbuf->st_rdev));
        } else if (m == 's') {
-               strcat(pformat, "ju");
-               printf(pformat, (uintmax_t) (statbuf->st_size));
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statbuf->st_size);
        } else if (m == 'B') {
                strcat(pformat, "lu");
                printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
        } else if (m == 'b') {
-               strcat(pformat, "ju");
-               printf(pformat, (uintmax_t) statbuf->st_blocks);
+               strcat(pformat, "llu");
+               printf(pformat, (unsigned long long) statbuf->st_blocks);
        } else if (m == 'o') {
                strcat(pformat, "lu");
                printf(pformat, (unsigned long) statbuf->st_blksize);
@@ -284,59 +383,66 @@ static void print_stat(char *pformat, const char m,
                printfs(pformat, human_time(statbuf->st_atime));
        } else if (m == 'X') {
                strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
-               printf(pformat, (unsigned long) statbuf->st_atime);
+               /* note: (unsigned long) would be wrong:
+                * imagine (unsigned long64)int32 */
+               printf(pformat, (long) statbuf->st_atime);
        } else if (m == 'y') {
                printfs(pformat, human_time(statbuf->st_mtime));
        } else if (m == 'Y') {
                strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
-               printf(pformat, (unsigned long) statbuf->st_mtime);
+               printf(pformat, (long) statbuf->st_mtime);
        } else if (m == 'z') {
                printfs(pformat, human_time(statbuf->st_ctime));
        } else if (m == 'Z') {
                strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
-               printf(pformat, (unsigned long) statbuf->st_ctime);
-#if ENABLE_SELINUX
+               printf(pformat, (long) statbuf->st_ctime);
+# if ENABLE_SELINUX
        } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
                printfs(pformat, scontext);
-#endif
+# endif
        } else {
                strcatc(pformat, 'c');
                printf(pformat, m);
        }
 }
 
-static void print_it(const char *masterformat, const char *filename,
-               void (*print_func) (char*, char, const char*, const void* USE_SELINUX(, security_context_t scontext)),
+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)),
                const void *data
-               USE_SELINUX(, security_context_t scontext) )
+               IF_SELINUX(, security_context_t scontext))
 {
        /* Create a working copy of the format string */
        char *format = xstrdup(masterformat);
-       /* Add 2 to accomodate our conversion of the stat '%s' format string
+       /* Add 2 to accommodate our conversion of the stat '%s' format string
         * to the printf '%llu' one.  */
        char *dest = xmalloc(strlen(format) + 2 + 1);
        char *b;
 
        b = format;
        while (b) {
+               /* Each iteration finds next %spec,
+                * prints preceding string and handles found %spec
+                */
                size_t len;
                char *p = strchr(b, '%');
                if (!p) {
-                       /* coreutils 6.3 always prints <cr> at the end */
+                       /* coreutils 6.3 always prints newline at the end */
                        /*fputs(b, stdout);*/
                        puts(b);
                        break;
                }
-               *p++ = '\0';
-               fputs(b, stdout);
 
                /* dest = "%<modifiers>" */
-               len = strspn(p, "#-+.I 0123456789");
-               dest[0] = '%';
-               memcpy(dest + 1, p, len);
-               dest[1 + len] = '\0';
-               p += len;
+               len = 1 + strspn(p + 1, "#-+.I 0123456789");
+               memcpy(dest, p, len);
+               dest[len] = '\0';
 
+               /* print preceding string */
+               *p = '\0';
+               fputs(b, stdout);
+
+               p += len;
                b = p + 1;
                switch (*p) {
                case '\0':
@@ -347,7 +453,7 @@ static void print_it(const char *masterformat, const char *filename,
                        break;
                default:
                        /* Completes "%<modifiers>" with specifier and printfs */
-                       print_func(dest, *p, filename, data USE_SELINUX(,scontext));
+                       print_func(dest, *p, filename, data IF_SELINUX(,scontext));
                        break;
                }
        }
@@ -355,8 +461,9 @@ static void print_it(const char *masterformat, const char *filename,
        free(format);
        free(dest);
 }
-#endif
+#endif  /* FEATURE_STAT_FORMAT */
 
+#if ENABLE_FEATURE_STAT_FILESYSTEM
 /* Stat the file system and print what we find.  */
 #if !ENABLE_FEATURE_STAT_FORMAT
 #define do_statfs(filename, format) do_statfs(filename)
@@ -364,7 +471,6 @@ static void print_it(const char *masterformat, const char *filename,
 static bool do_statfs(const char *filename, const char *format)
 {
        struct statfs statfsbuf;
-
 #if !ENABLE_FEATURE_STAT_FORMAT
        const char *format;
 #endif
@@ -377,19 +483,19 @@ static bool do_statfs(const char *filename, const char *format)
                     : getfilecon(filename, &scontext)
                    ) < 0
                ) {
-                       bb_perror_msg(filename);
+                       bb_simple_perror_msg(filename);
                        return 0;
                }
        }
 #endif
        if (statfs(filename, &statfsbuf) != 0) {
-               bb_perror_msg("cannot read file system information for '%s'", filename);
+               bb_perror_msg("can't read file system information for '%s'", filename);
                return 0;
        }
 
 #if ENABLE_FEATURE_STAT_FORMAT
        if (format == NULL) {
-#if !ENABLE_SELINUX
+# if !ENABLE_SELINUX
                format = (option_mask32 & OPT_TERSE
                        ? "%n %i %l %t %s %b %f %a %c %d\n"
                        : "  File: \"%n\"\n"
@@ -397,7 +503,7 @@ static bool do_statfs(const char *filename, const char *format)
                          "Block size: %-10s\n"
                          "Blocks: Total: %-10b Free: %-10f Available: %a\n"
                          "Inodes: Total: %-10c Free: %d");
-#else
+# else
                format = (option_mask32 & OPT_TERSE
                        ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
                        "%n %i %l %t %s %b %f %a %c %d\n")
@@ -414,9 +520,9 @@ static bool do_statfs(const char *filename, const char *format)
                        "Blocks: Total: %-10b Free: %-10f Available: %a\n"
                        "Inodes: Total: %-10c Free: %d\n")
                        );
-#endif /* SELINUX */
+# endif /* SELINUX */
        }
-       print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
+       print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
 #else /* FEATURE_STAT_FORMAT */
        format = (option_mask32 & OPT_TERSE
                ? "%s %llx %lu "
@@ -428,50 +534,52 @@ static bool do_statfs(const char *filename, const char *format)
               statfsbuf.f_namelen);
 
        if (option_mask32 & OPT_TERSE)
-               printf("%lx ", (unsigned long) (statfsbuf.f_type));
+               printf("%lx ", (unsigned long) statfsbuf.f_type);
        else
                printf("Type: %s\n", human_fstype(statfsbuf.f_type));
 
-#if !ENABLE_SELINUX
+# if !ENABLE_SELINUX
        format = (option_mask32 & OPT_TERSE
-               ? "%lu %ld %ld %ld %ld %ld\n"
+               ? "%lu %llu %llu %llu %llu %llu\n"
                : "Block size: %-10lu\n"
-                 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
-                 "Inodes: Total: %-10jd Free: %jd\n");
+                 "Blocks: Total: %-10llu Free: %-10llu Available: %llu\n"
+                 "Inodes: Total: %-10llu Free: %llu\n");
        printf(format,
-              (unsigned long) (statfsbuf.f_bsize),
-              (intmax_t) (statfsbuf.f_blocks),
-              (intmax_t) (statfsbuf.f_bfree),
-              (intmax_t) (statfsbuf.f_bavail),
-              (intmax_t) (statfsbuf.f_files),
-              (intmax_t) (statfsbuf.f_ffree));
-#else
+              (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
        format = (option_mask32 & OPT_TERSE
-               ? (option_mask32 & OPT_SELINUX ? "%lu %ld %ld %ld %ld %ld %C\n":
-               "%lu %ld %ld %ld %ld %ld\n")
-               : (option_mask32 & OPT_SELINUX ?
-               "Block size: %-10lu\n"
-               "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
-               "Inodes: Total: %-10jd Free: %jd"
-               "S_context: %C\n":
-               "Block size: %-10lu\n"
-               "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
-               "Inodes: Total: %-10jd Free: %jd\n"));
+               ? (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"
+                       )
+               );
        printf(format,
-              (unsigned long) (statfsbuf.f_bsize),
-              (intmax_t) (statfsbuf.f_blocks),
-              (intmax_t) (statfsbuf.f_bfree),
-              (intmax_t) (statfsbuf.f_bavail),
-              (intmax_t) (statfsbuf.f_files),
-              (intmax_t) (statfsbuf.f_ffree),
+               (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,
                scontext);
 
        if (scontext)
                freecon(scontext);
-#endif
-#endif /* FEATURE_STAT_FORMAT */
+# endif
+#endif  /* FEATURE_STAT_FORMAT */
        return 1;
 }
+#endif  /* FEATURE_STAT_FILESYSTEM */
 
 /* stat the file and print what we find */
 #if !ENABLE_FEATURE_STAT_FORMAT
@@ -489,25 +597,25 @@ static bool do_stat(const char *filename, const char *format)
                     : getfilecon(filename, &scontext)
                    ) < 0
                ) {
-                       bb_perror_msg(filename);
+                       bb_simple_perror_msg(filename);
                        return 0;
                }
        }
 #endif
        if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
-               bb_perror_msg("cannot stat '%s'", filename);
+               bb_perror_msg("can't stat '%s'", filename);
                return 0;
        }
 
 #if ENABLE_FEATURE_STAT_FORMAT
        if (format == NULL) {
-#if !ENABLE_SELINUX
+# if !ENABLE_SELINUX
                if (option_mask32 & OPT_TERSE) {
                        format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
                } else {
                        if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
                                format =
-                                       "  File: \"%N\"\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"
@@ -515,64 +623,70 @@ static bool do_stat(const char *filename, const char *format)
                                        "Access: %x\n" "Modify: %y\n" "Change: %z\n";
                        } else {
                                format =
-                                       "  File: \"%N\"\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";
                        }
                }
-#else
+# else
                if (option_mask32 & OPT_TERSE) {
                        format = (option_mask32 & OPT_SELINUX ?
-                                 "%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");
+                               "%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"
+                               );
                } else {
                        if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) {
                                format = (option_mask32 & OPT_SELINUX ?
-                                         "  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");
+                                       "  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"
+                                       );
                        } else {
                                format = (option_mask32 & OPT_SELINUX ?
-                                         "  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");
+                                       "  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"
+                                       );
                        }
                }
-#endif
+# endif
        }
-       print_it(format, filename, print_stat, &statbuf USE_SELINUX(, scontext));
+       print_it(format, filename, print_stat, &statbuf IF_SELINUX(, scontext));
 #else  /* FEATURE_STAT_FORMAT */
        if (option_mask32 & OPT_TERSE) {
-               printf("%s %ju %ju %lx %lu %lu %jx %ju %lu %lx %lx %lu %lu %lu %lu"
-                      SKIP_SELINUX("\n"),
+               printf("%s %llu %llu %lx %lu %lu %llx %llu %lu %lx %lx %lu %lu %lu %lu"
+                      IF_NOT_SELINUX("\n"),
                       filename,
-                      (uintmax_t) (statbuf.st_size),
-                      (uintmax_t) statbuf.st_blocks,
+                      (unsigned long long) statbuf.st_size,
+                      (unsigned long long) statbuf.st_blocks,
                       (unsigned long) statbuf.st_mode,
                       (unsigned long) statbuf.st_uid,
                       (unsigned long) statbuf.st_gid,
-                      (uintmax_t) statbuf.st_dev,
-                      (uintmax_t) statbuf.st_ino,
+                      (unsigned long long) statbuf.st_dev,
+                      (unsigned long long) statbuf.st_ino,
                       (unsigned long) statbuf.st_nlink,
                       (unsigned long) major(statbuf.st_rdev),
                       (unsigned long) minor(statbuf.st_rdev),
@@ -581,38 +695,38 @@ static bool do_stat(const char *filename, const char *format)
                       (unsigned long) statbuf.st_ctime,
                       (unsigned long) statbuf.st_blksize
                );
-#if ENABLE_SELINUX
+# if ENABLE_SELINUX
                if (option_mask32 & OPT_SELINUX)
-                       printf(" %lc\n", *scontext);
+                       printf(" %s\n", scontext);
                else
                        bb_putchar('\n');
-#endif
+# endif
        } else {
                char *linkname = NULL;
-
                struct passwd *pw_ent;
                struct group *gw_ent;
-               setgrent();
+
                gw_ent = getgrgid(statbuf.st_gid);
-               setpwent();
                pw_ent = getpwuid(statbuf.st_uid);
 
                if (S_ISLNK(statbuf.st_mode))
                        linkname = xmalloc_readlink_or_warn(filename);
-               if (linkname)
-                       printf("  File: \"%s\" -> \"%s\"\n", filename, linkname);
-               else
-                       printf("  File: \"%s\"\n", filename);
+               if (linkname) {
+                       printf("  File: '%s' -> '%s'\n", filename, linkname);
+                       free(linkname);
+               } else {
+                       printf("  File: '%s'\n", filename);
+               }
 
-               printf("  Size: %-10ju\tBlocks: %-10ju IO Block: %-6lu %s\n"
-                      "Device: %jxh/%jud\tInode: %-10ju  Links: %-5lu",
-                      (uintmax_t) (statbuf.st_size),
-                      (uintmax_t) statbuf.st_blocks,
+               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,
                       (unsigned long) statbuf.st_blksize,
                       file_type(&statbuf),
-                      (uintmax_t) statbuf.st_dev,
-                      (uintmax_t) statbuf.st_dev,
-                      (uintmax_t) statbuf.st_ino,
+                      (unsigned long long) statbuf.st_dev,
+                      (unsigned long long) statbuf.st_dev,
+                      (unsigned long long) statbuf.st_ino,
                       (unsigned long) statbuf.st_nlink);
                if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
                        printf(" Device type: %lx,%lx\n",
@@ -627,43 +741,51 @@ static bool do_stat(const char *filename, const char *format)
                       (pw_ent != NULL) ? pw_ent->pw_name : "UNKNOWN",
                       (unsigned long) statbuf.st_gid,
                       (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
-#if ENABLE_SELINUX
-               printf("   S_Context: %lc\n", *scontext);
-#endif
-               printf("Access: %s\n" "Modify: %s\n" "Change: %s\n",
-                      human_time(statbuf.st_atime),
-                      human_time(statbuf.st_mtime),
-                      human_time(statbuf.st_ctime));
+# if ENABLE_SELINUX
+               if (option_mask32 & OPT_SELINUX)
+                       printf("   S_Context: %s\n", scontext);
+# endif
+               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));
        }
-#endif /* FEATURE_STAT_FORMAT */
+#endif  /* FEATURE_STAT_FORMAT */
        return 1;
 }
 
 int stat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int stat_main(int argc, char **argv)
+int stat_main(int argc UNUSED_PARAM, char **argv)
 {
-       USE_FEATURE_STAT_FORMAT(char *format = NULL;)
+       IF_FEATURE_STAT_FORMAT(char *format = NULL;)
        int i;
-       int ok = 1;
+       int ok;
        statfunc_ptr statfunc = do_stat;
+#if ENABLE_FEATURE_STAT_FILESYSTEM || ENABLE_SELINUX
+       unsigned opts;
 
-       getopt32(argv, "ftL"
-               USE_SELINUX("Z")
-               USE_FEATURE_STAT_FORMAT("c:", &format)
+       opts =
+#endif
+       getopt32(argv, "^"
+               "tL"
+               IF_FEATURE_STAT_FILESYSTEM("f")
+               IF_SELINUX("Z")
+               IF_FEATURE_STAT_FORMAT("c:")
+               "\0" "-1" /* min one arg */
+               IF_FEATURE_STAT_FORMAT(,&format)
        );
-
-       if (option_mask32 & OPT_FILESYS) /* -f */
+#if ENABLE_FEATURE_STAT_FILESYSTEM
+       if (opts & OPT_FILESYS) /* -f */
                statfunc = do_statfs;
-       if (argc == optind)           /* files */
-               bb_show_usage();
-
+#endif
 #if ENABLE_SELINUX
-       if (option_mask32 & OPT_SELINUX) {
+       if (opts & OPT_SELINUX) {
                selinux_or_die();
        }
-#endif /* ENABLE_SELINUX */
-       for (i = optind; i < argc; ++i)
-               ok &= statfunc(argv[i] USE_FEATURE_STAT_FORMAT(, format));
+#endif
+       ok = 1;
+       argv += optind;
+       for (i = 0; argv[i]; ++i)
+               ok &= statfunc(argv[i] IF_FEATURE_STAT_FORMAT(, format));
 
        return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }