ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit a7dc139c3585f822384b9b7d24b821ff55f30e2f
parent 211f13ae95ce8a0990e162636afbaba7d831c32c
Author: sin <sin@2f30.org>
Date:   Mon Aug 12 09:52:43 +0100

Make stat(1) work with stdin as well.

Implement show_stat().

Diffstat:
stat.c | 59+++++++++++++++++++++++++++++++++++------------------------
1 file changed, 35 insertions(+), 24 deletions(-)
diff --git a/stat.c b/stat.c @@ -1,11 +1,15 @@ /* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <inttypes.h> -#include <time.h> #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) { @@ -16,40 +20,47 @@ int main(int argc, char *argv[]) { struct stat st; - char buf[100]; - int i, r, ret = 0; - + int i, ret = 0; ARGBEGIN { default: usage(); } ARGEND; - if (argc < 1) - usage(); + if (argc == 0) { + if (fstat(STDIN_FILENO, &st) < 0) + eprintf("stat <stdin>:"); + show_stat("<stdin>", &st); + } for (i = 0; i < argc; i++) { - r = stat(argv[i], &st); - if (r == -1) { - fprintf(stderr, "stat '%s': ", argv[i]); + if (stat(argv[i], &st) == -1) { + fprintf(stderr, "stat %s: ", argv[i]); perror(NULL); ret = 1; continue; } - - printf(" File: ā€˜%sā€™\n", argv[i]); - printf(" Size: %ju\tBlocks: %ju\tIO Block: %ju\n", (uintmax_t)st.st_size, - (uintmax_t)st.st_blocks, (uintmax_t)st.st_blksize); - printf("Device: %xh/%ud\tInode: %ju\tLinks %ju\n", major(st.st_dev), - minor(st.st_dev), (uintmax_t)st.st_ino, (uintmax_t)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), "%F %T %z", localtime(&st.st_atime)); - printf("Access: %s\n", buf); - strftime(buf, sizeof(buf), "%F %T %z", localtime(&st.st_mtime)); - printf("Modify: %s\n", buf); - strftime(buf, sizeof(buf), "%F %T %z", localtime(&st.st_ctime)); - printf("Change: %s\n", buf); + 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: %ju\tBlocks: %ju\tIO Block: %ju\n", (uintmax_t)st->st_size, + (uintmax_t)st->st_blocks, (uintmax_t)st->st_blksize); + printf("Device: %xh/%ud\tInode: %ju\tLinks %ju\n", major(st->st_dev), + minor(st->st_dev), (uintmax_t)st->st_ino, (uintmax_t)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), "%F %T %z", localtime(&st->st_atime)); + printf("Access: %s\n", buf); + strftime(buf, sizeof(buf), "%F %T %z", localtime(&st->st_mtime)); + printf("Modify: %s\n", buf); + strftime(buf, sizeof(buf), "%F %T %z", localtime(&st->st_ctime)); + printf("Change: %s\n", buf); +}