ubase

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

stat.c (2311B)


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