// (c) 2007 Electronic Arts Inc.
/* $Id: filetime.c,v 1.1 2005/11/27 22:01:41 sms Exp $
 * www.pccl.demon.co.uk
 */
#include    <sys/types.h>
#include    <sys/stat.h>

#include    <errno.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <time.h>
static char *sProgName  = NULL;
int optind=1;
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\n"
                , aFile
//              , strerror (errno)
                );
}
    else
    {
        time_t  cTime;          /* create time      */
        time_t  aTime;          /* access time      */
        time_t  wTime;          /* write time       */
        char    lDisplayTime[80];   /* Data to display  */

        /*
                 * Pick up the value we want.
         */
        cTime   = lStat.st_ctime;
        aTime   = lStat.st_atime;
        wTime   = lStat.st_mtime;
#if 0
        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;
        }
#endif

        switch (aFormat)
        {
            case EPOCH:
                (void) sprintf(lDisplayTime, "%ld %ld %ld", cTime, aTime, wTime);
                break;

            case STRFTIME:
                {
                    (void) strftime (lDisplayTime
                            , sizeof (lDisplayTime)
                            , aFormatString
                            , localtime (&wTime)
                            );
                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   = aArgv[0];

#if 0
    while (
    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);
        }
    }
#endif
    /*
     * Process the file
     */
    for (lArgIdx = optind; lArgIdx < aArgc; lArgIdx++)
    {
        lExit   = displayTime(aArgv[lArgIdx]
                    , lProperty
                    , lFormat
                    , lFormatString
                    );
    }


    exit (lExit);
}

