Skip to content

Instantly share code, notes, and snippets.

@lshifr
Created June 19, 2015 15:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lshifr/ad66c03e39882af3bade to your computer and use it in GitHub Desktop.
List file modification times
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
int main()
{
printf("Hello, World! Test\n");
char cwd[1024];
char fullpath[1024];
struct stat attr;
if (getcwd(cwd, sizeof(cwd)) != NULL){
fprintf(stdout, "Current working dir: %s\n", cwd);
DIR *dir;
struct dirent *ent;
time_t seconds;
if ((dir = opendir (cwd)) != NULL) {
fprintf(stdout, "Files in the dir:\n");
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
strcpy(fullpath,cwd);
strcat(fullpath, "/");
strcat(fullpath,ent->d_name);
stat(fullpath, &attr);
seconds = time(&attr.st_mtime);
printf ("File: %s , modified: %ld \n", fullpath, seconds);
}
closedir (dir);
}
else {
/* could not open directory */
perror ("");
return 1;
}
}
else
perror("getcwd() error");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment