ubase/stat.c

74 lines
1.7 KiB
C
Raw Normal View History

2013-08-11 20:17:19 -04:00
/* 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>
2013-08-11 20:17:19 -04:00
#include "util.h"
static void show_stat(const char *file, struct stat *st);
2013-08-11 20:17:19 -04:00
static void
usage(void)
{
2013-08-14 09:32:22 -04:00
eprintf("usage: %s [-L] file...\n", argv0);
2013-08-11 20:17:19 -04:00
}
int
main(int argc, char *argv[])
{
struct stat st;
int i, ret = 0;
2013-08-12 05:11:55 -04:00
int Lflag = 0;
int (*fn)(const char *, struct stat *);
2013-08-11 20:17:19 -04:00
ARGBEGIN {
2013-08-12 05:11:55 -04:00
case 'L':
Lflag = 1;
break;
2013-08-11 20:17:19 -04:00
default:
usage();
} ARGEND;
if (argc == 0) {
if (fstat(STDIN_FILENO, &st) < 0)
eprintf("stat <stdin>:");
show_stat("<stdin>", &st);
}
2013-08-11 20:17:19 -04:00
for (i = 0; i < argc; i++) {
2013-08-12 05:11:55 -04:00
fn = Lflag ? stat : lstat;
if (fn(argv[i], &st) == -1) {
fprintf(stderr, "%s %s: ", Lflag ? "stat" : "lstat",
argv[i]);
2013-08-11 20:17:19 -04:00
perror(NULL);
ret = 1;
continue;
}
show_stat(argv[i], &st);
2013-08-11 20:17:19 -04:00
}
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);
}