lshifr
6/19/2015 - 3:41 PM

List file modification times

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;
}