ubase

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

stat.c (2338B)


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