Hi,

Added stat(1), no manpage yet.

Tested on Linux + OpenBSD.

Thanks,
sin
>From b8bd9d28387a37e826b563171c8b92fe770bce67 Mon Sep 17 00:00:00 2001
From: sin <s...@2f30.org>
Date: Thu, 15 Aug 2013 10:04:38 +0100
Subject: [PATCH] Add stat(1)

---
 Makefile |  1 +
 stat.c   | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 stat.c

diff --git a/Makefile b/Makefile
index 6bc091a..010391c 100644
--- a/Makefile
+++ b/Makefile
@@ -86,6 +86,7 @@ SRC = \
        sha1sum.c  \
        sha256sum.c\
        sha512sum.c\
+       stat.c     \
        wc.c       \
        who.c      \
        yes.c
diff --git a/stat.c b/stat.c
new file mode 100644
index 0000000..7ce2cf5
--- /dev/null
+++ b/stat.c
@@ -0,0 +1,73 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <time.h>
+#include "util.h"
+
+static void show_stat(const char *file, struct stat *st);
+
+static void
+usage(void)
+{
+       eprintf("usage: %s [-L] file...\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+       struct stat st;
+       int i, ret = 0;
+       int Lflag = 0;
+       int (*fn)(const char *, struct stat *);
+
+       ARGBEGIN {
+       case 'L':
+               Lflag = 1;
+               break;
+       default:
+               usage();
+       } ARGEND;
+
+       if (argc == 0) {
+               if (fstat(STDIN_FILENO, &st) < 0)
+                       eprintf("stat <stdin>:");
+               show_stat("<stdin>", &st);
+       }
+
+       for (i = 0; i < argc; i++) {
+               fn = Lflag ? stat : lstat;
+               if (fn(argv[i], &st) == -1) {
+                       fprintf(stderr, "%s %s: ", Lflag ? "stat" : "lstat",
+                               argv[i]);
+                       perror(NULL);
+                       ret = 1;
+                       continue;
+               }
+               show_stat(argv[i], &st);
+       }
+
+       return ret;
+}
+
+static void
+show_stat(const char *file, struct stat *st)
+{
+       char buf[100];
+
+       printf("  File: ā€˜%s’\n", file);
+       printf("  Size: %lu\tBlocks: %lu\tIO Block: %lu\n", (unsigned 
long)st->st_size,
+              (unsigned long)st->st_blocks, (unsigned long)st->st_blksize);
+       printf("Device: %xh/%ud\tInode: %lu\tLinks %lu\n", major(st->st_dev),
+              minor(st->st_dev), (unsigned long)st->st_ino, (unsigned 
long)st->st_nlink);
+       printf("Access: %04o\tUid: %u\tGid: %u\n", st->st_mode & 0777, 
st->st_uid, st->st_gid);
+       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", 
localtime(&st->st_atime));
+       printf("Access: %s\n", buf);
+       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", 
localtime(&st->st_mtime));
+       printf("Modify: %s\n", buf);
+       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", 
localtime(&st->st_ctime));
+       printf("Change: %s\n", buf);
+}
-- 
1.8.2.3

Reply via email to