Skip to content

Instantly share code, notes, and snippets.

@jtn
Created September 14, 2010 14:13
Show Gist options
  • Save jtn/579102 to your computer and use it in GitHub Desktop.
Save jtn/579102 to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
extern statPrint(char*, struct stat*);
extern strmode(mode_t mode, char*);
//Hittade lösningen för att formatera datumet på:
//http://stackoverflow.com/questions/3053999/c-convert-time-t-to-string-with-format-yyyy-mm-dd-hhmmss
char *timeString(time_t time, int utc)
{
static char buff[20];
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", utc ? gmtime(&time) : localtime(&time));
return buff;
}
void printTime(time_t time, char *timeType)
{
printf("%s: %s ( %s UTC)\n",
timeType,
timeString(time,0),
timeString(time,1));
}
void myStatPrint(char* fileName, struct stat* info)
{
printf(" File: '%s'\n", fileName);
printf(" Size: %11-i\tBlocks: %11-iIO Block: %i\n",
info->st_size,
info->st_blocks,
info->st_blksize);
printf("Device: 0x%xh\tInode: %12-iLinks: %i\n",
info->st_dev,
info->st_ino,
info->st_nlink);
char accessString[20];
//Strmode found at: http://www.opensource.apple.com/source/Libc/Libc-262/string/strmode.c
strmode(info->st_mode, accessString);
struct passwd *passwd;
passwd = getpwuid(info->st_uid);
struct group *group;
group = getgrgid(info->st_gid);
printf("Access: (%04o/%.10s) Uid: (%5i/%9s) Gid: (%5i/%9s)\n",
info->st_mode & 07777,
accessString,
info->st_uid,
passwd->pw_name,
info->st_gid,
group->gr_name);
printTime(info->st_atime, "Access");
printTime(info->st_mtime, "Modify");
printTime(info->st_ctime, "Change");
printf("\n");
}
int main(int argc, char* argv[])
{
if(argc != 2)
{
puts("You have to suply a filepath");
return 2;
}
struct stat stat_struct;
stat(argv[1], &stat_struct);
statPrint(argv[1], &stat_struct);
printf("######################################################\n");
myStatPrint(argv[1], &stat_struct);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment