12b15cb3dSCy Schubert
22b15cb3dSCy Schubert /**
32b15cb3dSCy Schubert * \file time.c
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * @addtogroup autoopts
62b15cb3dSCy Schubert * @{
72b15cb3dSCy Schubert */
82b15cb3dSCy Schubert /*
92b15cb3dSCy Schubert * This file is part of AutoOpts, a companion to AutoGen.
102b15cb3dSCy Schubert * AutoOpts is free software.
11*a466cc55SCy Schubert * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
122b15cb3dSCy Schubert *
132b15cb3dSCy Schubert * AutoOpts is available under any one of two licenses. The license
142b15cb3dSCy Schubert * in use must be one of these two and the choice is under the control
152b15cb3dSCy Schubert * of the user of the license.
162b15cb3dSCy Schubert *
172b15cb3dSCy Schubert * The GNU Lesser General Public License, version 3 or later
182b15cb3dSCy Schubert * See the files "COPYING.lgplv3" and "COPYING.gplv3"
192b15cb3dSCy Schubert *
202b15cb3dSCy Schubert * The Modified Berkeley Software Distribution License
212b15cb3dSCy Schubert * See the file "COPYING.mbsd"
222b15cb3dSCy Schubert *
232b15cb3dSCy Schubert * These files have the following sha256 sums:
242b15cb3dSCy Schubert *
252b15cb3dSCy Schubert * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
262b15cb3dSCy Schubert * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
272b15cb3dSCy Schubert * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
282b15cb3dSCy Schubert */
292b15cb3dSCy Schubert
302b15cb3dSCy Schubert /*=export_func optionTimeVal
312b15cb3dSCy Schubert * private:
322b15cb3dSCy Schubert *
332b15cb3dSCy Schubert * what: process an option with a time duration.
342b15cb3dSCy Schubert * arg: + tOptions * + opts + program options descriptor +
352b15cb3dSCy Schubert * arg: + tOptDesc * + od + the descriptor for this arg +
362b15cb3dSCy Schubert *
372b15cb3dSCy Schubert * doc:
382b15cb3dSCy Schubert * Decipher a time duration value.
392b15cb3dSCy Schubert =*/
402b15cb3dSCy Schubert void
optionTimeVal(tOptions * opts,tOptDesc * od)412b15cb3dSCy Schubert optionTimeVal(tOptions * opts, tOptDesc * od)
422b15cb3dSCy Schubert {
432b15cb3dSCy Schubert time_t val;
442b15cb3dSCy Schubert
452b15cb3dSCy Schubert if (INQUERY_CALL(opts, od))
462b15cb3dSCy Schubert return;
472b15cb3dSCy Schubert
482b15cb3dSCy Schubert val = parse_duration(od->optArg.argString);
492b15cb3dSCy Schubert if (val == BAD_TIME) {
502b15cb3dSCy Schubert fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString);
512b15cb3dSCy Schubert if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
522b15cb3dSCy Schubert (*(opts->pUsageProc))(opts, EXIT_FAILURE);
532b15cb3dSCy Schubert }
542b15cb3dSCy Schubert
552b15cb3dSCy Schubert if (od->fOptState & OPTST_ALLOC_ARG) {
562b15cb3dSCy Schubert AGFREE(od->optArg.argString);
572b15cb3dSCy Schubert od->fOptState &= ~OPTST_ALLOC_ARG;
582b15cb3dSCy Schubert }
592b15cb3dSCy Schubert
602b15cb3dSCy Schubert od->optArg.argInt = (long)val;
612b15cb3dSCy Schubert }
622b15cb3dSCy Schubert
632b15cb3dSCy Schubert /*=export_func optionTimeDate
642b15cb3dSCy Schubert * private:
652b15cb3dSCy Schubert *
662b15cb3dSCy Schubert * what: process an option with a time and date.
672b15cb3dSCy Schubert * arg: + tOptions * + opts + program options descriptor +
682b15cb3dSCy Schubert * arg: + tOptDesc * + od + the descriptor for this arg +
692b15cb3dSCy Schubert *
702b15cb3dSCy Schubert * doc:
712b15cb3dSCy Schubert * Decipher a time and date value.
722b15cb3dSCy Schubert =*/
732b15cb3dSCy Schubert void
optionTimeDate(tOptions * opts,tOptDesc * od)742b15cb3dSCy Schubert optionTimeDate(tOptions * opts, tOptDesc * od)
752b15cb3dSCy Schubert {
762b15cb3dSCy Schubert #if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
772b15cb3dSCy Schubert if (INQUERY_CALL(opts, od))
782b15cb3dSCy Schubert return;
792b15cb3dSCy Schubert
802b15cb3dSCy Schubert if ((! HAS_pzPkgDataDir(opts)) || (opts->pzPkgDataDir == NULL))
812b15cb3dSCy Schubert goto default_action;
822b15cb3dSCy Schubert
832b15cb3dSCy Schubert /*
842b15cb3dSCy Schubert * Export the DATEMSK environment variable. getdate_r() uses it to
852b15cb3dSCy Schubert * find the file with the strptime formats. If we cannot find the file
862b15cb3dSCy Schubert * we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
872b15cb3dSCy Schubert */
882b15cb3dSCy Schubert {
892b15cb3dSCy Schubert static char * envptr = NULL;
902b15cb3dSCy Schubert
912b15cb3dSCy Schubert if (envptr == NULL) {
922b15cb3dSCy Schubert static char const fmt[] = "DATEMSK=%s/datemsk";
93*a466cc55SCy Schubert size_t sz = sizeof(fmt) + strlen(opts->pzPkgDataDir);
94*a466cc55SCy Schubert envptr = AGALOC(sz, fmt);
95*a466cc55SCy Schubert if (snprintf(envptr, sz, fmt, opts->pzPkgDataDir) >= (int)sz)
96*a466cc55SCy Schubert option_exits(EXIT_FAILURE);
972b15cb3dSCy Schubert
982b15cb3dSCy Schubert putenv(envptr);
992b15cb3dSCy Schubert }
1002b15cb3dSCy Schubert
1012b15cb3dSCy Schubert if (access(envptr+8, R_OK) != 0)
1022b15cb3dSCy Schubert goto default_action;
1032b15cb3dSCy Schubert }
1042b15cb3dSCy Schubert
1052b15cb3dSCy Schubert /*
1062b15cb3dSCy Schubert * Convert the date to a time since the epoch and stash it in a long int.
1072b15cb3dSCy Schubert */
1082b15cb3dSCy Schubert {
1092b15cb3dSCy Schubert struct tm stm;
1102b15cb3dSCy Schubert time_t tm;
1112b15cb3dSCy Schubert
1122b15cb3dSCy Schubert if (getdate_r(od->optArg.argString, &stm) != 0) {
1132b15cb3dSCy Schubert fprintf(stderr, zNotDate, opts->pzProgName,
1142b15cb3dSCy Schubert od->optArg.argString);
1152b15cb3dSCy Schubert if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
1162b15cb3dSCy Schubert (*(opts->pUsageProc))(opts, EXIT_FAILURE);
1172b15cb3dSCy Schubert return;
1182b15cb3dSCy Schubert }
1192b15cb3dSCy Schubert
1202b15cb3dSCy Schubert tm = mktime(&stm);
1212b15cb3dSCy Schubert
1222b15cb3dSCy Schubert if (od->fOptState & OPTST_ALLOC_ARG) {
1232b15cb3dSCy Schubert AGFREE(od->optArg.argString);
1242b15cb3dSCy Schubert od->fOptState &= ~OPTST_ALLOC_ARG;
1252b15cb3dSCy Schubert }
1262b15cb3dSCy Schubert
1272b15cb3dSCy Schubert od->optArg.argInt = tm;
1282b15cb3dSCy Schubert }
1292b15cb3dSCy Schubert return;
1302b15cb3dSCy Schubert
1312b15cb3dSCy Schubert default_action:
1322b15cb3dSCy Schubert
1332b15cb3dSCy Schubert #endif
1342b15cb3dSCy Schubert optionTimeVal(opts, od);
1352b15cb3dSCy Schubert if (od->optArg.argInt != BAD_TIME)
1362b15cb3dSCy Schubert od->optArg.argInt += (long)time(NULL);
1372b15cb3dSCy Schubert }
1382b15cb3dSCy Schubert /** @}
1392b15cb3dSCy Schubert *
1402b15cb3dSCy Schubert * Local Variables:
1412b15cb3dSCy Schubert * mode: C
1422b15cb3dSCy Schubert * c-file-style: "stroustrup"
1432b15cb3dSCy Schubert * indent-tabs-mode: nil
1442b15cb3dSCy Schubert * End:
1452b15cb3dSCy Schubert * end of autoopts/time.c */
146