k9core

Unnamed repository
Log | Files | Refs | LICENSE

commit 6922539f10f584cd856d1810294d0f0bdf57b10d
parent 97e5f8eeef9475bf36b35448e3558c6daf2f7321
Author: qorg11 <qorg@vxempire.xyz>
Date:   Thu, 24 Dec 2020 23:23:36 +0100

Added stat.c

I was really drunk at the moment of writing this, so code can be
*very* better i guess, i'll improve it some other day
have a nice dinner tho

Diffstat:
Asrc/stat.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/src/stat.c b/src/stat.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <sys/stat.h> +#include <string.h> +#include <errno.h> +#include <time.h> + +int +main(int argc, char **argv) +{ + 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) { + printf("Cannot stat '%s': %s\n",argv[i],strerror(errno)); + 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 0; +}