/* $Id: filetime.c,v 1.1 2005/11/27 22:01:41 sms Exp $ * www.pccl.demon.co.uk */ #include #include #include #include #include #include static char *sProgName = NULL; enum Format { EPOCH , STRFTIME }; enum Property { ACCESS_TIME , MOD_TIME , STATUS_TIME }; static int displayTime ( char *aFile /* File to show */ , enum Property aProperty /* Which time to show */ , enum Format aFormat /* Display format */ , char *aFormatString /* Format string */ ) { struct stat lStat; /* File status */ int lRet = 0; /* Return code */ if (stat (aFile, &lStat) < 0) { lRet = errno; fprintf(stderr, "Cannot stat %s, %s\n" , aFile , strerror (errno) ); } else { time_t lTime; /* Raw time */ char lDisplayTime[80]; /* Data to display */ /* * Pick up the value we want. */ switch (aProperty) { case ACCESS_TIME: lTime = lStat.st_atime; break; case MOD_TIME: lTime = lStat.st_mtime; break; case STATUS_TIME: lTime = lStat.st_ctime; break; } switch (aFormat) { case EPOCH: (void) sprintf(lDisplayTime, "%ld", lTime); break; case STRFTIME: { (void) strftime (lDisplayTime , sizeof (lDisplayTime) , aFormatString , localtime (&lTime) ); break; } } (void) printf("%s: %s\n", aFile, lDisplayTime); } return (lRet); } /* * Display the usage message */ static void usage ( FILE *aFp /* Stream to write */ ) { fprintf (aFp, "USAGE:\n"); fprintf (aFp, " %s -[a|c|m] -[e|s format] -h file\n", sProgName); fprintf (aFp, " -a display last access time\n"); fprintf (aFp, " -e display seconds since epoch (default)\n"); fprintf (aFp, " -c display last status change time\n"); fprintf (aFp, " -h help\n"); fprintf (aFp, " -m display last modification time (default)\n"); fprintf (aFp, " -s display strftime() format\n"); } /* * This displays the time stamp of the named file. */ int main ( int aArgc /* Argument count */ , char *aArgv[] /* Arguments */ ) { char lOpt; /* Command line option */ int lArgIdx; /* Index through command line */ int lExit = 0; /* Exit code */ enum Property lProperty = STATUS_TIME; /* Which time? */ enum Format lFormat = EPOCH; /* Which format?*/ char *lFormatString = NULL; /* Format string*/ /* * Parse the command line */ sProgName = basename (aArgv[0]); while ((lOpt = getopt (aArgc, aArgv, "acehms:")) != -1) { switch (lOpt) { case 'a': lProperty = ACCESS_TIME; break; case 'c': lProperty = STATUS_TIME; break; case 'e': lFormat = EPOCH; break; case 'h': usage (stdout); exit (EINVAL); break; case 'm': lProperty = MOD_TIME; break; case 's': lFormat = STRFTIME; lFormatString = optarg; break; case '?': fprintf (stderr , "Unrecognised option: -%c\n" , optopt ); usage (stderr); exit (EINVAL); } } /* * Process the file */ for (lArgIdx = optind; lArgIdx < aArgc; lArgIdx++) { lExit = displayTime(aArgv[lArgIdx] , lProperty , lFormat , lFormatString ); } exit (lExit); }