- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / coreutils / stat.c
index 33f01deb06dc0d4d3f9497230c35d7bec1cfef00..7c56317649a0ef6554639c594fae9e1b08481f54 100644 (file)
@@ -1,50 +1,33 @@
+/* vi: set sw=4 ts=4: */
 /*
  * 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>
+ * Copyright (C) 2006 by Yoshinori Sato <ysato@users.sourceforge.jp>
  *
  * Written by Michael Meskes
  * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <pwd.h>
-#include <grp.h>
-#include <sys/vfs.h>
-#include <time.h>
-#include <getopt.h>
-#include <sys/stat.h>
-#include <string.h>
 #include "busybox.h"
 
 /* vars to control behavior */
-#define OPT_TERSE 2
-#define OPT_DEREFERNCE 4
-static long flags;
+#define OPT_FILESYS            (1<<0)
+#define OPT_TERSE              (1<<1)
+#define OPT_DEREFERENCE        (1<<2)
+#define OPT_SELINUX            (1<<3)
+
+static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")];
 
-static char const *file_type(struct stat const *st)
+static char const * file_type(struct stat const *st)
 {
-       /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 
+       /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
         * for some of these formats.
-        * To keep diagnostics grammatical in English, the 
+        * To keep diagnostics grammatical in English, the
         * returned string must start with a consonant.
         */
        if (S_ISREG(st->st_mode))  return st->st_size == 0 ? "regular empty file" : "regular file";
@@ -65,10 +48,16 @@ static char const *file_type(struct stat const *st)
 
 static char const *human_time(time_t t)
 {
+       /* Old
        static char *str;
        str = ctime(&t);
        str[strlen(str)-1] = '\0';
        return str;
+       */
+       /* coreutils 6.3 compat: */
+
+       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
+       return buf;
 }
 
 /* Return the type of the specified file system.
@@ -79,9 +68,9 @@ static char const *human_time(time_t t)
 static char const *human_fstype(long f_type)
 {
        int i;
-       static struct types {
+       static const struct types {
                long type;
-               char *fs;
+               const char * const fs;
        } humantypes[] = {
                { 0xADFF,     "affs" },
                { 0x1Cd1,     "devpts" },
@@ -118,100 +107,82 @@ static char const *human_fstype(long f_type)
                { 0x858458f6, "romfs" },
                { 0x73717368, "squashfs" },
                { 0x62656572, "sysfs" },
-               { 0, "UNKNOWN" },
-               { 0, NULL }
+               { 0, "UNKNOWN" }
        };
-       for (i=0; humantypes[i].type; ++i)
+       for (i = 0; humantypes[i].type; ++i)
                if (humantypes[i].type == f_type)
-                       return humantypes[i].fs;
+                       break;
        return humantypes[i].fs;
 }
 
-#ifdef CONFIG_FEATURE_STAT_FORMAT
+#if ENABLE_FEATURE_STAT_FORMAT
 /* print statfs info */
-static void print_statfs(char *pformat, size_t buf_len, char m, 
-                         char const *filename, void const *data)
+static void print_statfs(char *pformat, const size_t buf_len, const char m,
+                        const char * const filename, void const *data
+                        USE_SELINUX(, security_context_t scontext))
 {
        struct statfs const *statfsbuf = data;
-
-       switch (m) {
-       case 'n':
+       if (m == 'n') {
                strncat(pformat, "s", buf_len);
                printf(pformat, filename);
-               break;
-       case 'i':
+       } else if (m == 'i') {
                strncat(pformat, "Lx", buf_len);
                printf(pformat, statfsbuf->f_fsid);
-               break;
-       case 'l':
+       } else if (m == 'l') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, statfsbuf->f_namelen);
-               break;
-       case 't':
+       } else if (m == 't') {
                strncat(pformat, "lx", buf_len);
-               printf(pformat, (unsigned long int) (statfsbuf->f_type));  /* no equiv. */
-               break;
-       case 'T':
+               printf(pformat, (unsigned long int) (statfsbuf->f_type)); /* no equiv */
+       } else if (m == 'T') {
                strncat(pformat, "s", buf_len);
                printf(pformat, human_fstype(statfsbuf->f_type));
-               break;
-       case 'b':
-               strncat(pformat, "ld", buf_len);
+       } else if (m == 'b') {
+               strncat(pformat, "jd", buf_len);
                printf(pformat, (intmax_t) (statfsbuf->f_blocks));
-               break;
-       case 'f':
-               strncat(pformat, "ld", buf_len);
+       } else if (m == 'f') {
+               strncat(pformat, "jd", buf_len);
                printf(pformat, (intmax_t) (statfsbuf->f_bfree));
-               break;
-       case 'a':
-               strncat(pformat, "ld", buf_len);
+       } else if (m == 'a') {
+               strncat(pformat, "jd", buf_len);
                printf(pformat, (intmax_t) (statfsbuf->f_bavail));
-               break;
-       case 's':
+       } else if (m == 's' || m == 'S') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) (statfsbuf->f_bsize));
-               break;
-       case 'S': {
-               unsigned long int frsize = statfsbuf->f_frsize;
-               if (!frsize)
-                       frsize = statfsbuf->f_bsize;
-               strncat(pformat, "lu", buf_len);
-               printf(pformat, frsize);
-               break;
-       }
-       case 'c':
-               strncat(pformat, "ld", buf_len);
+       } else if (m == 'c') {
+               strncat(pformat, "jd", buf_len);
                printf(pformat, (intmax_t) (statfsbuf->f_files));
-               break;
-       case 'd':
-               strncat(pformat, "ld", buf_len);
+       } else if (m == 'd') {
+               strncat(pformat, "jd", buf_len);
                printf(pformat, (intmax_t) (statfsbuf->f_ffree));
-               break;
-       default:
+#if ENABLE_SELINUX
+       } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
+               strncat(pformat, "s", buf_len);
+               printf(scontext);
+#endif
+       } else {
                strncat(pformat, "c", buf_len);
                printf(pformat, m);
-               break;
        }
 }
 
 /* print stat info */
-static void print_stat(char *pformat, size_t buf_len, char m, 
-                       char const *filename, void const *data)
+static void print_stat(char *pformat, const size_t buf_len, const char m,
+                      const char * const filename, void const *data
+                          USE_SELINUX(, security_context_t scontext))
 {
 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
        struct stat *statbuf = (struct stat *) data;
        struct passwd *pw_ent;
        struct group *gw_ent;
 
-       switch (m) {
-       case 'n':
+       if (m == 'n') {
                strncat(pformat, "s", buf_len);
                printf(pformat, filename);
-               break;
-       case 'N':
+       } else if (m == 'N') {
                strncat(pformat, "s", buf_len);
                if (S_ISLNK(statbuf->st_mode)) {
-                       char *linkname = xreadlink(filename);
+                       char *linkname = xmalloc_readlink_or_warn(filename);
                        if (linkname == NULL) {
                                bb_perror_msg("cannot read symbolic link '%s'", filename);
                                return;
@@ -223,158 +194,138 @@ static void print_stat(char *pformat, size_t buf_len, char m,
                } else {
                        printf(pformat, filename);
                }
-               break;
-       case 'd':
-               strncat(pformat, "lu", buf_len);
+       } else if (m == 'd') {
+               strncat(pformat, "ju", buf_len);
                printf(pformat, (uintmax_t) statbuf->st_dev);
-               break;
-       case 'D':
-               strncat(pformat, "lx", buf_len);
+       } else if (m == 'D') {
+               strncat(pformat, "jx", buf_len);
                printf(pformat, (uintmax_t) statbuf->st_dev);
-               break;
-       case 'i':
-               strncat(pformat, "lu", buf_len);
+       } else if (m == 'i') {
+               strncat(pformat, "ju", buf_len);
                printf(pformat, (uintmax_t) statbuf->st_ino);
-               break;
-       case 'a':
+       } else if (m == 'a') {
                strncat(pformat, "lo", buf_len);
                printf(pformat, (unsigned long int) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
-               break;
-       case 'A':
+       } else if (m == 'A') {
                strncat(pformat, "s", buf_len);
                printf(pformat, bb_mode_string(statbuf->st_mode));
-               break;
-       case 'f':
+       } else if (m == 'f') {
                strncat(pformat, "lx", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_mode);
-               break;
-       case 'F':
+       } else if (m == 'F') {
                strncat(pformat, "s", buf_len);
                printf(pformat, file_type(statbuf));
-               break;
-       case 'h':
+       } else if (m == 'h') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_nlink);
-               break;
-       case 'u':
+       } else if (m == 'u') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_uid);
-               break;
-       case 'U':
+       } else if (m == 'U') {
                strncat(pformat, "s", buf_len);
                setpwent();
                pw_ent = getpwuid(statbuf->st_uid);
                printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
-               break;
-       case 'g':
+       } else if (m == 'g') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_gid);
-               break;
-       case 'G':
+       } else if (m == 'G') {
                strncat(pformat, "s", buf_len);
                setgrent();
                gw_ent = getgrgid(statbuf->st_gid);
                printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
-               break;
-       case 't':
+       } else if (m == 't') {
                strncat(pformat, "lx", buf_len);
                printf(pformat, (unsigned long int) major(statbuf->st_rdev));
-               break;
-       case 'T':
+       } else if (m == 'T') {
                strncat(pformat, "lx", buf_len);
                printf(pformat, (unsigned long int) minor(statbuf->st_rdev));
-               break;
-       case 's':
-               strncat(pformat, "lu", buf_len);
+       } else if (m == 's') {
+               strncat(pformat, "ju", buf_len);
                printf(pformat, (uintmax_t) (statbuf->st_size));
-               break;
-       case 'B':
+       } else if (m == 'B') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) 512); //ST_NBLOCKSIZE
-               break;
-       case 'b':
-               strncat(pformat, "lu", buf_len);
+       } else if (m == 'b') {
+               strncat(pformat, "ju", buf_len);
                printf(pformat, (uintmax_t) statbuf->st_blocks);
-               break;
-       case 'o':
+       } else if (m == 'o') {
                strncat(pformat, "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_blksize);
-               break;
-       case 'x':
+       } else if (m == 'x') {
                strncat(pformat, "s", buf_len);
                printf(pformat, human_time(statbuf->st_atime));
-               break;
-       case 'X':
+       } else if (m == 'X') {
                strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_atime);
-               break;
-       case 'y':
+       } else if (m == 'y') {
                strncat(pformat, "s", buf_len);
                printf(pformat, human_time(statbuf->st_mtime));
-               break;
-       case 'Y':
+       } else if (m == 'Y') {
                strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_mtime);
-               break;
-       case 'z':
+       } else if (m == 'z') {
                strncat(pformat, "s", buf_len);
                printf(pformat, human_time(statbuf->st_ctime));
-               break;
-       case 'Z':
+       } else if (m == 'Z') {
                strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len);
                printf(pformat, (unsigned long int) statbuf->st_ctime);
-               break;
-       default:
+#if ENABLE_SELINUX
+       } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
+               strncat(pformat, "s", buf_len);
+               printf(pformat, scontext);
+#endif
+       } else {
                strncat(pformat, "c", buf_len);
                printf(pformat, m);
-               break;
        }
 }
 
-static void print_it(char const *masterformat, char const *filename, 
-                     void (*print_func) (char *, size_t, char, char const *, void const *), 
-                     void const *data)
+static void print_it(char const *masterformat, char const *filename,
+                    void (*print_func) (char *, size_t, char, char const *, void const *
+                                                                USE_SELINUX(, security_context_t scontext)),
+                                        void const *data USE_SELINUX(, security_context_t scontext) )
 {
        char *b;
 
        /* create a working copy of the format string */
-       char *format = bb_xstrdup(masterformat);
+       char *format = xstrdup(masterformat);
 
-       /* Add 2 to accommodate our conversion of the stat `%s' format string
-        * to the printf `%llu' one.  */
+       /* Add 2 to accomodate our conversion of the stat '%s' format string
+        * to the printf '%llu' one.  */
        size_t n_alloc = strlen(format) + 2 + 1;
        char *dest = xmalloc(n_alloc);
 
        b = format;
        while (b) {
+               size_t len;
                char *p = strchr(b, '%');
-               if (p != NULL) {
-                       size_t len;
-                       *p++ = '\0';
-                       fputs(b, stdout);
-
-                       len = strspn(p, "#-+.I 0123456789");
-                       dest[0] = '%';
-                       memcpy(dest + 1, p, len);
-                       dest[1 + len] = 0;
-                       p += len;
-
-                       b = p + 1;
-                       switch (*p) {
-                               case '\0':
-                                       b = NULL;
-                                       /* fall through */
-                               case '%':
-                                       putchar('%');
-                                       break;
-                               default:
-                                       print_func(dest, n_alloc, *p, filename, data);
-                                       break;
-                       }
-
-               } else {
-                       fputs(b, stdout);
+               if (!p) {
+                       /* coreutils 6.3 always print <cr> at the end */
+                       /*fputs(b, stdout);*/
+                       puts(b);
+                       break;
+               }
+               *p++ = '\0';
+               fputs(b, stdout);
+
+               len = strspn(p, "#-+.I 0123456789");
+               dest[0] = '%';
+               memcpy(dest + 1, p, len);
+               dest[1 + len] = 0;
+               p += len;
+
+               b = p + 1;
+               switch (*p) {
+               case '\0':
                        b = NULL;
+                       /* fall through */
+               case '%':
+                       putchar('%');
+                       break;
+               default:
+                       print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext));
+                       break;
                }
        }
 
@@ -384,29 +335,58 @@ static void print_it(char const *masterformat, char const *filename,
 #endif
 
 /* Stat the file system and print what we find.  */
-static int do_statfs(char const *filename, char const *format)
+static bool do_statfs(char const *filename, char const *format)
 {
        struct statfs statfsbuf;
-
+#if ENABLE_SELINUX
+       security_context_t scontext = NULL;
+
+       if (option_mask32 & OPT_SELINUX) {
+               if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
+                    getfilecon(filename, scontext))< 0) {
+                       bb_perror_msg(filename);
+                       return 0;
+               }
+       }
+#endif
        if (statfs(filename, &statfsbuf) != 0) {
                bb_perror_msg("cannot read file system information for '%s'", filename);
                return 0;
        }
 
-#ifdef CONFIG_FEATURE_STAT_FORMAT
+#if ENABLE_FEATURE_STAT_FORMAT
        if (format == NULL)
-               format = (flags & OPT_TERSE
-                       ? "%n %i %l %t %s %S %b %f %a %c %d\n"
+#if !ENABLE_SELINUX
+               format = (option_mask32 & OPT_TERSE
+                       ? "%n %i %l %t %s %b %f %a %c %d\n"
                        : "  File: \"%n\"\n"
                          "    ID: %-8i Namelen: %-7l Type: %T\n"
-                         "Block size: %-10s Fundamental block size: %S\n"
+                         "Block size: %-10s\n"
                          "Blocks: Total: %-10b Free: %-10f Available: %a\n"
-                         "Inodes: Total: %-10c Free: %d\n");
-       print_it(format, filename, print_statfs, &statfsbuf);
+                         "Inodes: Total: %-10c Free: %d");
+       print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
 #else
-
-       format = (flags & OPT_TERSE
-               ? "%s %Lx %lu "
+       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")
+                       : (option_mask32 & OPT_SELINUX ?
+                       "  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")
+                       );
+       print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
+#endif /* SELINUX */
+#else /* FEATURE_STAT_FORMAT */
+       format = (option_mask32 & OPT_TERSE
+               ? "%s %llx %lu "
                : "  File: \"%s\"\n"
                  "    ID: %-8Lx Namelen: %-7lu ");
        printf(format,
@@ -414,43 +394,77 @@ static int do_statfs(char const *filename, char const *format)
               statfsbuf.f_fsid,
               statfsbuf.f_namelen);
 
-       if (flags & OPT_TERSE)
+       if (option_mask32 & OPT_TERSE)
                printf("%lx ", (unsigned long int) (statfsbuf.f_type));
        else
                printf("Type: %s\n", human_fstype(statfsbuf.f_type));
 
-       format = (flags & OPT_TERSE
-               ? "%lu %lu %ld %ld %ld %ld %ld\n"
-               : "Block size: %-10lu Fundamental block size: %lu\n"
-                 "Blocks: Total: %-10ld Free: %-10ld Available: %ld\n"
-                 "Inodes: Total: %-10ld Free: %ld\n");
+#if !ENABLE_SELINUX
+       format = (option_mask32 & OPT_TERSE
+               ? "%lu %ld %ld %ld %ld %ld\n"
+               : "Block size: %-10lu\n"
+                 "Blocks: Total: %-10jd Free: %-10jd Available: %jd\n"
+                 "Inodes: Total: %-10jd Free: %jd\n");
        printf(format,
               (unsigned long int) (statfsbuf.f_bsize),
-              statfsbuf.f_frsize ? statfsbuf.f_frsize : 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));
-#endif
+#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"));
+       printf(format,
+              (unsigned long int) (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),
+               scontext);
 
+       if (scontext)
+               freecon(scontext);
+#endif
+#endif /* FEATURE_STAT_FORMAT */
        return 1;
 }
 
 /* stat the file and print what we find */
-static int do_stat(char const *filename, char const *format)
+static bool do_stat(char const *filename, char const *format)
 {
        struct stat statbuf;
-
-       if ((flags & OPT_DEREFERNCE ? stat : lstat) (filename, &statbuf) != 0) {
+#if ENABLE_SELINUX
+       security_context_t scontext = NULL;
+
+       if (option_mask32 & OPT_SELINUX) {
+               if ((option_mask32 & OPT_DEREFERENCE ? lgetfilecon(filename, scontext):
+                    getfilecon(filename, scontext))< 0) {
+                       bb_perror_msg (filename);
+                       return 0;
+               }
+       }
+#endif
+       if ((option_mask32 & OPT_DEREFERENCE ? stat : lstat) (filename, &statbuf) != 0) {
                bb_perror_msg("cannot stat '%s'", filename);
                return 0;
        }
 
-#ifdef CONFIG_FEATURE_STAT_FORMAT
+#if ENABLE_FEATURE_STAT_FORMAT
        if (format == NULL) {
-               if (flags & OPT_TERSE) {
-                       format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
+#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 =
@@ -469,11 +483,49 @@ static int do_stat(char const *filename, char const *format)
                                        "Access: %x\n" "Modify: %y\n" "Change: %z\n";
                        }
                }
-       }
-       print_it(format, filename, print_stat, &statbuf);
 #else
-       if (flags & OPT_TERSE) {
-               printf("%s %lu %lu %lx %lu %lu %lx %lu %lu %lx %lx %lu %lu %lu %lu\n",
+               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");
+               } 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");
+                       } 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");
+                       }
+               }
+#endif
+       }
+       print_it(format, filename, print_stat, &statbuf USE_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"),
                       filename,
                       (uintmax_t) (statbuf.st_size),
                       (uintmax_t) statbuf.st_blocks,
@@ -490,6 +542,12 @@ static int do_stat(char const *filename, char const *format)
                       (unsigned long int) statbuf.st_ctime,
                       (unsigned long int) statbuf.st_blksize
                );
+#if ENABLE_SELINUX
+               if (option_mask32 & OPT_SELINUX)
+                       printf(" %lc\n", *scontext);
+               else
+                       putchar('\n');
+#endif
        } else {
                char *linkname = NULL;
 
@@ -501,14 +559,14 @@ static int do_stat(char const *filename, char const *format)
                pw_ent = getpwuid(statbuf.st_uid);
 
                if (S_ISLNK(statbuf.st_mode))
-                       linkname = xreadlink(filename);
+                       linkname = xmalloc_readlink_or_warn(filename);
                if (linkname)
                        printf("  File: \"%s\" -> \"%s\"\n", filename, linkname);
                else
                        printf("  File: \"%s\"\n", filename);
 
-               printf("  Size: %-10lu\tBlocks: %-10lu IO Block: %-6lu %s\n"
-                      "Device: %lxh/%lud\tInode: %-10lu  Links: %-5lu",
+               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,
                       (unsigned long int) statbuf.st_blksize,
@@ -523,40 +581,48 @@ static int do_stat(char const *filename, char const *format)
                               (unsigned long int) minor(statbuf.st_rdev));
                else
                        putchar('\n');
-               printf("Access: (%04lo/%10.10s)  Uid: (%5lu/%8s)   Gid: (%5lu/%8s)\n"
-                      "Access: %s\n" "Modify: %s\n" "Change: %s\n",
+               printf("Access: (%04lo/%10.10s)  Uid: (%5lu/%8s)   Gid: (%5lu/%8s)\n",
                       (unsigned long int) (statbuf.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)),
                       bb_mode_string(statbuf.st_mode),
                       (unsigned long int) statbuf.st_uid,
                       (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
                       (unsigned long int) statbuf.st_gid,
-                      (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN",
+                      (gw_ent != 0L) ? 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));
        }
-#endif
+#endif /* FEATURE_STAT_FORMAT */
        return 1;
 }
 
+int stat_main(int argc, char **argv);
 int stat_main(int argc, char **argv)
 {
-       int i;
        char *format = NULL;
+       int i;
        int ok = 1;
-       int (*statfunc)(char const *, char const *) = do_stat;
+       bool (*statfunc)(char const *, char const *) = do_stat;
 
-       flags = bb_getopt_ulflags(argc, argv, "ftL"
-#ifdef CONFIG_FEATURE_STAT_FORMAT
-       "c:", &format
-#endif
+       getopt32(argc, argv, "ftL"
+               USE_SELINUX("Z")
+               USE_FEATURE_STAT_FORMAT("c:", &format)
        );
 
-       if (flags & 1)                /* -f */
+       if (option_mask32 & OPT_FILESYS) /* -f */
                statfunc = do_statfs;
        if (argc == optind)           /* files */
                bb_show_usage();
 
+#if ENABLE_SELINUX
+       if (option_mask32 & OPT_SELINUX) {
+               selinux_or_die();
+       }
+#endif /* ENABLE_SELINUX */
        for (i = optind; i < argc; ++i)
                ok &= statfunc(argv[i], format);