2014-01-30 11:20:27 -05:00
|
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
|
2014-01-30 11:20:27 -05:00
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2014-01-30 11:20:27 -05:00
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
static void show_stat(const char *file, struct stat *st);
|
2014-04-04 13:00:47 -04:00
|
|
|
|
static void show_stat_terse(const char *file, struct stat *st);
|
2014-01-30 11:20:27 -05:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage(void)
|
|
|
|
|
{
|
2014-04-04 13:00:47 -04:00
|
|
|
|
eprintf("usage: %s [-L] [-t] [file...]\n", argv0);
|
2014-01-30 11:20:27 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
2014-10-02 18:45:25 -04:00
|
|
|
|
int i, ret = 0;
|
2014-04-04 11:05:16 -04:00
|
|
|
|
int (*fn)(const char *, struct stat *) = lstat;
|
|
|
|
|
char *fnname = "lstat";
|
|
|
|
|
void (*showstat)(const char *, struct stat *) = show_stat;
|
2014-01-30 11:20:27 -05:00
|
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
|
case 'L':
|
2014-04-04 11:05:16 -04:00
|
|
|
|
fn = stat;
|
|
|
|
|
fnname = "stat";
|
2014-01-30 11:20:27 -05:00
|
|
|
|
break;
|
2014-04-04 13:00:47 -04:00
|
|
|
|
case 't':
|
|
|
|
|
showstat = show_stat_terse;
|
|
|
|
|
break;
|
2014-01-30 11:20:27 -05:00
|
|
|
|
default:
|
|
|
|
|
usage();
|
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
|
|
if (argc == 0) {
|
2014-11-30 08:12:15 -05:00
|
|
|
|
if (fstat(0, &st) < 0)
|
2014-01-30 11:20:27 -05:00
|
|
|
|
eprintf("stat <stdin>:");
|
|
|
|
|
show_stat("<stdin>", &st);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
|
if (fn(argv[i], &st) == -1) {
|
2014-06-30 17:42:56 -04:00
|
|
|
|
weprintf("%s %s:", fnname, argv[i]);
|
2014-10-02 18:45:25 -04:00
|
|
|
|
ret = 1;
|
2014-01-30 11:20:27 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-04-04 11:05:16 -04:00
|
|
|
|
showstat(argv[i], &st);
|
2014-01-30 11:20:27 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 13:00:47 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 11:20:27 -05:00
|
|
|
|
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);
|
|
|
|
|
}
|