morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

stat.c (2435B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 #include <sys/types.h>
      4 
      5 #include <inttypes.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <time.h>
      9 #include <unistd.h>
     10 
     11 #include "util.h"
     12 
     13 static void show_stat(const char *file, struct stat *st);
     14 static void show_stat_terse(const char *file, struct stat *st);
     15 
     16 static void
     17 usage(void)
     18 {
     19 	eprintf("usage: %s [-L] [-t] [file...]\n", argv0);
     20 }
     21 
     22 int
     23 main(int argc, char *argv[])
     24 {
     25 	struct stat st;
     26 	int i, ret = 0;
     27 	int (*fn)(const char *, struct stat *) = lstat;
     28 	char *fnname = "lstat";
     29 	void (*showstat)(const char *, struct stat *) = show_stat;
     30 
     31 	ARGBEGIN {
     32 	case 'L':
     33 		fn = stat;
     34 		fnname = "stat";
     35 		break;
     36 	case 't':
     37 		showstat = show_stat_terse;
     38 		break;
     39 	default:
     40 		usage();
     41 	} ARGEND;
     42 
     43 	if (argc == 0) {
     44 		if (fstat(0, &st) < 0)
     45 			eprintf("stat <stdin>:");
     46 		show_stat("<stdin>", &st);
     47 	}
     48 
     49 	for (i = 0; i < argc; i++) {
     50 		if (fn(argv[i], &st) == -1) {
     51 			weprintf("%s %s:", fnname, argv[i]);
     52 			ret = 1;
     53 			continue;
     54 		}
     55 		showstat(argv[i], &st);
     56 	}
     57 
     58 	return ret;
     59 }
     60 
     61 static void
     62 show_stat_terse(const char *file, struct stat *st)
     63 {
     64 	printf("%s ", file);
     65 	printf("%lu %lu ", (unsigned long)st->st_size,
     66 	       (unsigned long)st->st_blocks);
     67 	printf("%04o %u %u ", st->st_mode & 0777, st->st_uid, st->st_gid);
     68 	printf("%llx ", (unsigned long long)st->st_dev);
     69 	printf("%lu %lu ", (unsigned long)st->st_ino, (unsigned long)st->st_nlink);
     70 	printf("%d %d ", major(st->st_rdev), minor(st->st_rdev));
     71 	printf("%ld %ld %ld ", st->st_atime, st->st_mtime, st->st_ctime);
     72 	printf("%lu\n", (unsigned long)st->st_blksize);
     73 }
     74 
     75 static void
     76 show_stat(const char *file, struct stat *st)
     77 {
     78 	char buf[100];
     79 
     80 	printf("  File: ā€˜%sā€™\n", file);
     81 	printf("  Size: %lu\tBlocks: %lu\tIO Block: %lu\n", (unsigned long)st->st_size,
     82 	       (unsigned long)st->st_blocks, (unsigned long)st->st_blksize);
     83 	printf("Device: %xh/%ud\tInode: %lu\tLinks %lu\n", major(st->st_dev),
     84 	       minor(st->st_dev), (unsigned long)st->st_ino, (unsigned long)st->st_nlink);
     85 	printf("Access: %04o\tUid: %u\tGid: %u\n", st->st_mode & 0777, st->st_uid, st->st_gid);
     86 	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_atime));
     87 	printf("Access: %s\n", buf);
     88 	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_mtime));
     89 	printf("Modify: %s\n", buf);
     90 	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_ctime));
     91 	printf("Change: %s\n", buf);
     92 }