k9core

Coreutils for *nix operating systems [fork]
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

stat.c (1591B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

int
main(int argc, char **argv)
{
	int return_value = 0;
	if(argc == 1) {
		fprintf(stderr, "usage: stat file...\n");
		return 1;
	}
	char mod_date[64], acc_date[64], creat_date[64];
	struct stat file_data;
	for(int i = 1; i < argc; i++) {
		if(stat(argv[i], &file_data) == -1) {
			fprintf(stderr, "stat: %s: %s\n", argv[i], strerror(errno));
			return_value = 1;
			continue;
		}
		/* About file size, location... */
		printf("File: %s\nSize: %lu\t blocks: %li\t IO Block: %li\nDevice: "
			  "%lu\t",
			  argv[i],
			  file_data.st_size,
			  file_data.st_blocks,
			  file_data.st_blksize,
			  file_data.st_dev);
		/* File permisions
		 * TODO: Display only the permissions (644) instead of the whole mode
		 * (100644)
		 */
		printf("Inode: %u\t Links: %lu\nAccess: %o\tUid: %u\tGid:%u\n",
			  file_data.st_gid,
			  file_data.st_nlink,
			  file_data.st_mode,
			  file_data.st_uid,
			  file_data.st_gid);
		/* Access, creation and modification date */
		struct tm *timeinfo;
		/* Modification time */
		timeinfo = localtime(&file_data.st_mtim.tv_sec);
		strftime(mod_date, 64, "%F %H:%M:%S", timeinfo);
		/* Creation time */

		timeinfo = localtime(&file_data.st_ctim.tv_sec);
		strftime(creat_date, 64, "%F %H:%M:%S", timeinfo);

		/* Access time */
		timeinfo = localtime(&file_data.st_atim.tv_sec);
		strftime(acc_date, 64, "%F %H:%M:%S", timeinfo);

		printf("Access: %s\nModify: %s\nCreation %s\n",
			  acc_date,
			  mod_date,
			  creat_date);
	}

	return return_value;
}