1
0
mirror of git://git.suckless.org/ubase synced 2025-02-17 19:17:13 +00:00
ubase/stat.c
Markus Rudy a015607af0 Explicitly include sys/sysmacros.h for makedev etc
This header used to be included by sys/types.h in glibc, and musl
adopted the behaviour. However, this dependency was never desired, so
glibc deprecated it in 2016 and finally removed it in 2019, and so did
musl. Explicitly including the header should be a no-op on older libc
versions and fixes the build on newer versions.

https://sourceware.org/bugzilla/show_bug.cgi?id=19239
https://git.musl-libc.org/cgit/musl/commit/?id=f552c79
2023-09-26 09:22:32 +02:00

90 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "util.h"
static void
show_stat_terse(const char *file, struct stat *st)
{
printf("%s ", file);
printf("%lu %lu ", (unsigned long)st->st_size,
(unsigned long)st->st_blocks);
printf("%04o %u %u ", st->st_mode & 0777, st->st_uid, st->st_gid);
printf("%llx ", (unsigned long long)st->st_dev);
printf("%lu %lu ", (unsigned long)st->st_ino, (unsigned long)st->st_nlink);
printf("%d %d ", major(st->st_rdev), minor(st->st_rdev));
printf("%ld %ld %ld ", st->st_atime, st->st_mtime, st->st_ctime);
printf("%lu\n", (unsigned long)st->st_blksize);
}
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);
}
static void
usage(void)
{
eprintf("usage: %s [-L] [-t] [file...]\n", argv0);
}
int
main(int argc, char *argv[])
{
struct stat st;
int i, ret = 0;
int (*fn)(const char *, struct stat *) = lstat;
char *fnname = "lstat";
void (*showstat)(const char *, struct stat *) = show_stat;
ARGBEGIN {
case 'L':
fn = stat;
fnname = "stat";
break;
case 't':
showstat = show_stat_terse;
break;
default:
usage();
} ARGEND;
if (argc == 0) {
if (fstat(0, &st) < 0)
eprintf("stat <stdin>:");
show_stat("<stdin>", &st);
}
for (i = 0; i < argc; i++) {
if (fn(argv[i], &st) == -1) {
weprintf("%s %s:", fnname, argv[i]);
ret = 1;
continue;
}
showstat(argv[i], &st);
}
return ret;
}