time.c revision 296373
133965Sjdp
233965Sjdp/**
360484Sobrien * \file time.c
460484Sobrien *
533965Sjdp * @addtogroup autoopts
633965Sjdp * @{
733965Sjdp */
833965Sjdp/*
933965Sjdp *  This file is part of AutoOpts, a companion to AutoGen.
1033965Sjdp *  AutoOpts is free software.
1133965Sjdp *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
1233965Sjdp *
1333965Sjdp *  AutoOpts is available under any one of two licenses.  The license
1433965Sjdp *  in use must be one of these two and the choice is under the control
1533965Sjdp *  of the user of the license.
1633965Sjdp *
1733965Sjdp *   The GNU Lesser General Public License, version 3 or later
1833965Sjdp *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
1933965Sjdp *
2033965Sjdp *   The Modified Berkeley Software Distribution License
2133965Sjdp *      See the file "COPYING.mbsd"
2233965Sjdp *
2333965Sjdp *  These files have the following sha256 sums:
2433965Sjdp *
2533965Sjdp *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
2633965Sjdp *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
2733965Sjdp *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
2838889Sjdp */
2938889Sjdp
3038889Sjdp/*=export_func  optionTimeVal
3138889Sjdp * private:
3238889Sjdp *
3338889Sjdp * what:  process an option with a time duration.
3438889Sjdp * arg:   + tOptions * + opts + program options descriptor +
3533965Sjdp * arg:   + tOptDesc * + od   + the descriptor for this arg +
3633965Sjdp *
3733965Sjdp * doc:
3833965Sjdp *  Decipher a time duration value.
3933965Sjdp=*/
4033965Sjdpvoid
4133965SjdpoptionTimeVal(tOptions * opts, tOptDesc * od)
4233965Sjdp{
4333965Sjdp    time_t val;
4433965Sjdp
4533965Sjdp    if (INQUERY_CALL(opts, od))
4633965Sjdp        return;
4733965Sjdp
4833965Sjdp    val = parse_duration(od->optArg.argString);
4933965Sjdp    if (val == BAD_TIME) {
5033965Sjdp        fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString);
5133965Sjdp        if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
5233965Sjdp            (*(opts->pUsageProc))(opts, EXIT_FAILURE);
5333965Sjdp    }
5433965Sjdp
5533965Sjdp    if (od->fOptState & OPTST_ALLOC_ARG) {
5633965Sjdp        AGFREE(od->optArg.argString);
5733965Sjdp        od->fOptState &= ~OPTST_ALLOC_ARG;
5833965Sjdp    }
5933965Sjdp
6033965Sjdp    od->optArg.argInt = (long)val;
6133965Sjdp}
6233965Sjdp
6333965Sjdp/*=export_func  optionTimeDate
6433965Sjdp * private:
6533965Sjdp *
6633965Sjdp * what:  process an option with a time and date.
6733965Sjdp * arg:   + tOptions * + opts + program options descriptor +
6833965Sjdp * arg:   + tOptDesc * + od   + the descriptor for this arg +
6933965Sjdp *
7033965Sjdp * doc:
7133965Sjdp *  Decipher a time and date value.
7233965Sjdp=*/
7333965Sjdpvoid
7433965SjdpoptionTimeDate(tOptions * opts, tOptDesc * od)
7533965Sjdp{
7633965Sjdp#if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
7733965Sjdp    if (INQUERY_CALL(opts, od))
7833965Sjdp        return;
7933965Sjdp
8033965Sjdp    if ((! HAS_pzPkgDataDir(opts)) || (opts->pzPkgDataDir == NULL))
8133965Sjdp        goto default_action;
8233965Sjdp
8333965Sjdp    /*
8433965Sjdp     *  Export the DATEMSK environment variable.  getdate_r() uses it to
8533965Sjdp     *  find the file with the strptime formats.  If we cannot find the file
8633965Sjdp     *  we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
8733965Sjdp     */
8833965Sjdp    {
8933965Sjdp        static char * envptr = NULL;
9033965Sjdp
9133965Sjdp        if (envptr == NULL) {
9233965Sjdp            static char const fmt[] = "DATEMSK=%s/datemsk";
9333965Sjdp            envptr = AGALOC(sizeof(fmt) + strlen(opts->pzPkgDataDir), fmt);
9433965Sjdp            sprintf(envptr, fmt, opts->pzPkgDataDir);
9533965Sjdp
9633965Sjdp            putenv(envptr);
9733965Sjdp        }
9833965Sjdp
9933965Sjdp        if (access(envptr+8, R_OK) != 0)
10033965Sjdp            goto default_action;
10133965Sjdp    }
10233965Sjdp
10333965Sjdp    /*
10433965Sjdp     *  Convert the date to a time since the epoch and stash it in a long int.
10533965Sjdp     */
10633965Sjdp    {
10733965Sjdp        struct tm stm;
10833965Sjdp        time_t tm;
10933965Sjdp
11033965Sjdp        if (getdate_r(od->optArg.argString, &stm) != 0) {
11133965Sjdp            fprintf(stderr, zNotDate, opts->pzProgName,
11233965Sjdp                    od->optArg.argString);
11333965Sjdp            if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
11433965Sjdp                (*(opts->pUsageProc))(opts, EXIT_FAILURE);
11533965Sjdp            return;
11633965Sjdp        }
11733965Sjdp
11833965Sjdp        tm = mktime(&stm);
11933965Sjdp
12033965Sjdp        if (od->fOptState & OPTST_ALLOC_ARG) {
12133965Sjdp            AGFREE(od->optArg.argString);
12233965Sjdp            od->fOptState &= ~OPTST_ALLOC_ARG;
12333965Sjdp        }
12433965Sjdp
12533965Sjdp        od->optArg.argInt = tm;
12633965Sjdp    }
12733965Sjdp    return;
12833965Sjdp
12933965Sjdp default_action:
13033965Sjdp
13133965Sjdp#endif
13233965Sjdp    optionTimeVal(opts, od);
13333965Sjdp    if (od->optArg.argInt != BAD_TIME)
13433965Sjdp        od->optArg.argInt += (long)time(NULL);
13533965Sjdp}
13633965Sjdp/** @}
13733965Sjdp *
13833965Sjdp * Local Variables:
13933965Sjdp * mode: C
14033965Sjdp * c-file-style: "stroustrup"
14133965Sjdp * indent-tabs-mode: nil
14233965Sjdp * End:
14333965Sjdp * end of autoopts/time.c */
14433965Sjdp