I need to find out all of the information about a file entered as a command line argument.
I'm using stat() to find out the simpler information such as file size and date last accessed. However, I need to find other info, such as the username of the file owner, not just their uid, as well as their groups, not just there gid. I can find the uid and gid fine with stat(). No idea how to find this stuff. Searched online but there was surprisingly little stuff about stat. Also, with the dates accessed, it gives it in seconds since 1970. How would I convert that to a date? Here is what I have so far:
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <locale.h>
#include <langinfo.h>
#include <stdint.h>
int main(int argc, char *argv[]){
FILE *fp;
struct stat fileAtt;
if(stat(argv[1], &fileAtt) == 0){
printf("File Name: %s\n", argv[1]);
printf("UID : %d\nGID : %d\n", fileAtt.st_uid, fileAtt.st_gid);
printf("File size is: %lu\n", fileAtt.st_size);
printf("Last accessed: %u\n", fileAtt.st_atime);
}
return 0;
}
Thanks in advance,
JB

New Topic/Question
Reply




MultiQuote





|