Datareign

Here's a little programme that gets all the access dates associated with a Unix file. The first date (called 'access' here) is the date that the file was last opened, even for a read. The other two dates show the date the file was last written to and the date it had its status changed, respectively.

 /* -------------------------------------------------
    Name: ft
    Purpose: Displays the access, modified and status
             change times for a given filename
    ---------------------------------------------- */
 
 #include <stdio.h>
 #include <dirent.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 main(int argc, char *argv[])
 {
   char *FilePathName;             /* File to interrogate (from command line) */
   struct stat ThisEntryStatus;    /* Buffer for directory status entries */
   time_t AccessSeconds;
   time_t ModifiedSeconds;
   time_t StatusSeconds;
   char AccessBuffer[20];
   char ModifiedBuffer[20];
   char StatusBuffer[20];
 
   if(argc == 2)
      FilePathName = argv[1];
   else
   {
      printf("-----------------------------------------------------n");
      printf("Usage: ft <filename>\n");
      printf("       (where <filename> is any valid file.)\n");
      printf("This will display the access, modified and status\n");
      printf("change times for the file in the format...\n\n");
      printf("CCYYMMDDhhmmss CCYYMMDDhhmmss CCYYMMDDhhmmss\n\n");
      printf("...returning 0 if it succeeds or non-zero if it fails\n");
      printf("-----------------------------------------------------\n\n");
      return(1);
   }
   if (stat(FilePathName, &ThisEntryStatus) == 0)
   {
      /* -------------------------------------------------
         Got a status record: extract the times in seconds
         since 00:00:00 UTC, Jan. 1, 1970
         ---------------------------------------------- */
      AccessSeconds = ThisEntryStatus.st_atim.tv_sec;
      ModifiedSeconds = ThisEntryStatus.st_mtim.tv_sec;
      StatusSeconds = ThisEntryStatus.st_ctim.tv_sec;
 
      /* ---------------------------------------------
         Now convert the time in seconds to strings...
         NOTE that you must pass the address of the
         source variable - not the variable itself!
         ------------------------------------------ */
      cftime(AccessBuffer, "%Y%m%d%H%M%S", &ThisEntryStatus.st_atim.tv_sec);
      cftime(ModifiedBuffer, "%Y%m%d%H%M%S", &ThisEntryStatus.st_mtim.tv_sec);
      cftime(StatusBuffer, "%Y%m%d%H%M%S", &ThisEntryStatus.st_ctim.tv_sec);
 
      /* -------------------------------------------
         Print out the results and exit with zero...
         ---------------------------------------- */
      printf("%s %s %s\n", AccessBuffer, ModifiedBuffer, StatusBuffer);
 
      return(0) ;
   }
   /* -----------------------------------
      If we get here there's a problem...
      -------------------------------- */
   printf("ft: Failed to stat [%s]\n", FilePathName);
   return(2) ;
 }
Last modified: 2009/01/03 19:16