Blame src/libopts/time.c

Packit aea12f
Packit aea12f
/**
Packit aea12f
 * \file time.c
Packit aea12f
 *
Packit aea12f
 * @addtogroup autoopts
Packit aea12f
 * @{
Packit aea12f
 */
Packit aea12f
/*
Packit aea12f
 *  This file is part of AutoOpts, a companion to AutoGen.
Packit aea12f
 *  AutoOpts is free software.
Packit Service 991b93
 *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
Packit aea12f
 *
Packit aea12f
 *  AutoOpts is available under any one of two licenses.  The license
Packit aea12f
 *  in use must be one of these two and the choice is under the control
Packit aea12f
 *  of the user of the license.
Packit aea12f
 *
Packit aea12f
 *   The GNU Lesser General Public License, version 3 or later
Packit aea12f
 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
Packit aea12f
 *
Packit aea12f
 *   The Modified Berkeley Software Distribution License
Packit aea12f
 *      See the file "COPYING.mbsd"
Packit aea12f
 *
Packit aea12f
 *  These files have the following sha256 sums:
Packit aea12f
 *
Packit aea12f
 *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
Packit aea12f
 *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
Packit aea12f
 *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
Packit aea12f
 */
Packit aea12f
Packit aea12f
/*=export_func  optionTimeVal
Packit aea12f
 * private:
Packit aea12f
 *
Packit aea12f
 * what:  process an option with a time duration.
Packit aea12f
 * arg:   + tOptions * + opts + program options descriptor +
Packit aea12f
 * arg:   + tOptDesc * + od   + the descriptor for this arg +
Packit aea12f
 *
Packit aea12f
 * doc:
Packit aea12f
 *  Decipher a time duration value.
Packit aea12f
=*/
Packit aea12f
void
Packit aea12f
optionTimeVal(tOptions * opts, tOptDesc * od)
Packit aea12f
{
Packit aea12f
    time_t val;
Packit aea12f
Packit aea12f
    if (INQUERY_CALL(opts, od))
Packit aea12f
        return;
Packit aea12f
Packit aea12f
    val = parse_duration(od->optArg.argString);
Packit aea12f
    if (val == BAD_TIME) {
Packit aea12f
        fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString);
Packit aea12f
        if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
Packit aea12f
            (*(opts->pUsageProc))(opts, EXIT_FAILURE);
Packit aea12f
    }
Packit aea12f
Packit aea12f
    if (od->fOptState & OPTST_ALLOC_ARG) {
Packit aea12f
        AGFREE(od->optArg.argString);
Packit aea12f
        od->fOptState &= ~OPTST_ALLOC_ARG;
Packit aea12f
    }
Packit aea12f
Packit aea12f
    od->optArg.argInt = (long)val;
Packit aea12f
}
Packit aea12f
Packit aea12f
/*=export_func  optionTimeDate
Packit aea12f
 * private:
Packit aea12f
 *
Packit aea12f
 * what:  process an option with a time and date.
Packit aea12f
 * arg:   + tOptions * + opts + program options descriptor +
Packit aea12f
 * arg:   + tOptDesc * + od   + the descriptor for this arg +
Packit aea12f
 *
Packit aea12f
 * doc:
Packit aea12f
 *  Decipher a time and date value.
Packit aea12f
=*/
Packit aea12f
void
Packit aea12f
optionTimeDate(tOptions * opts, tOptDesc * od)
Packit aea12f
{
Packit aea12f
#if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
Packit aea12f
    if (INQUERY_CALL(opts, od))
Packit aea12f
        return;
Packit aea12f
Packit aea12f
    if ((! HAS_pzPkgDataDir(opts)) || (opts->pzPkgDataDir == NULL))
Packit aea12f
        goto default_action;
Packit aea12f
Packit aea12f
    /*
Packit aea12f
     *  Export the DATEMSK environment variable.  getdate_r() uses it to
Packit aea12f
     *  find the file with the strptime formats.  If we cannot find the file
Packit aea12f
     *  we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
Packit aea12f
     */
Packit aea12f
    {
Packit aea12f
        static char * envptr = NULL;
Packit aea12f
Packit aea12f
        if (envptr == NULL) {
Packit aea12f
            static char const fmt[] = "DATEMSK=%s/datemsk";
Packit aea12f
            size_t sz = sizeof(fmt) + strlen(opts->pzPkgDataDir);
Packit aea12f
            envptr = AGALOC(sz, fmt);
Packit Service 991b93
            if (snprintf(envptr, sz, fmt, opts->pzPkgDataDir) >= (int)sz)
Packit aea12f
                option_exits(EXIT_FAILURE);
Packit aea12f
Packit aea12f
            putenv(envptr);
Packit aea12f
        }
Packit aea12f
Packit aea12f
        if (access(envptr+8, R_OK) != 0)
Packit aea12f
            goto default_action;
Packit aea12f
    }
Packit aea12f
Packit aea12f
    /*
Packit aea12f
     *  Convert the date to a time since the epoch and stash it in a long int.
Packit aea12f
     */
Packit aea12f
    {
Packit aea12f
        struct tm stm;
Packit aea12f
        time_t tm;
Packit aea12f
Packit aea12f
        if (getdate_r(od->optArg.argString, &stm) != 0) {
Packit aea12f
            fprintf(stderr, zNotDate, opts->pzProgName,
Packit aea12f
                    od->optArg.argString);
Packit aea12f
            if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
Packit aea12f
                (*(opts->pUsageProc))(opts, EXIT_FAILURE);
Packit aea12f
            return;
Packit aea12f
        }
Packit aea12f
Packit aea12f
        tm = mktime(&stm;;
Packit aea12f
Packit aea12f
        if (od->fOptState & OPTST_ALLOC_ARG) {
Packit aea12f
            AGFREE(od->optArg.argString);
Packit aea12f
            od->fOptState &= ~OPTST_ALLOC_ARG;
Packit aea12f
        }
Packit aea12f
Packit aea12f
        od->optArg.argInt = tm;
Packit aea12f
    }
Packit aea12f
    return;
Packit aea12f
Packit aea12f
 default_action:
Packit aea12f
Packit aea12f
#endif
Packit aea12f
    optionTimeVal(opts, od);
Packit aea12f
    if (od->optArg.argInt != BAD_TIME)
Packit aea12f
        od->optArg.argInt += (long)time(NULL);
Packit aea12f
}
Packit aea12f
/** @}
Packit aea12f
 *
Packit aea12f
 * Local Variables:
Packit aea12f
 * mode: C
Packit aea12f
 * c-file-style: "stroustrup"
Packit aea12f
 * indent-tabs-mode: nil
Packit aea12f
 * End:
Packit aea12f
 * end of autoopts/time.c */