1*eabc0478Schristos /* $NetBSD: time.c,v 1.6 2024/08/18 20:47:25 christos Exp $ */ 2abb0f93cSkardel 3abb0f93cSkardel 4f003fb54Skardel /** 5f003fb54Skardel * \file time.c 6f003fb54Skardel * 78585484eSchristos * @addtogroup autoopts 88585484eSchristos * @{ 98585484eSchristos */ 108585484eSchristos /* 11abb0f93cSkardel * This file is part of AutoOpts, a companion to AutoGen. 12abb0f93cSkardel * AutoOpts is free software. 13*eabc0478Schristos * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved 14abb0f93cSkardel * 15abb0f93cSkardel * AutoOpts is available under any one of two licenses. The license 16abb0f93cSkardel * in use must be one of these two and the choice is under the control 17abb0f93cSkardel * of the user of the license. 18abb0f93cSkardel * 19abb0f93cSkardel * The GNU Lesser General Public License, version 3 or later 20abb0f93cSkardel * See the files "COPYING.lgplv3" and "COPYING.gplv3" 21abb0f93cSkardel * 22abb0f93cSkardel * The Modified Berkeley Software Distribution License 23abb0f93cSkardel * See the file "COPYING.mbsd" 24abb0f93cSkardel * 258585484eSchristos * These files have the following sha256 sums: 26abb0f93cSkardel * 278585484eSchristos * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3 288585484eSchristos * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3 298585484eSchristos * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd 30abb0f93cSkardel */ 31abb0f93cSkardel 32abb0f93cSkardel /*=export_func optionTimeVal 33abb0f93cSkardel * private: 34abb0f93cSkardel * 35f003fb54Skardel * what: process an option with a time duration. 36b8ecfcfeSchristos * arg: + tOptions * + opts + program options descriptor + 37b8ecfcfeSchristos * arg: + tOptDesc * + od + the descriptor for this arg + 38abb0f93cSkardel * 39abb0f93cSkardel * doc: 40abb0f93cSkardel * Decipher a time duration value. 41abb0f93cSkardel =*/ 42abb0f93cSkardel void 43b8ecfcfeSchristos optionTimeVal(tOptions * opts, tOptDesc * od) 44abb0f93cSkardel { 45f003fb54Skardel time_t val; 46abb0f93cSkardel 47b8ecfcfeSchristos if (INQUERY_CALL(opts, od)) 488585484eSchristos return; 498585484eSchristos 50b8ecfcfeSchristos val = parse_duration(od->optArg.argString); 51f003fb54Skardel if (val == BAD_TIME) { 52b8ecfcfeSchristos fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString); 53b8ecfcfeSchristos if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) 54b8ecfcfeSchristos (*(opts->pUsageProc))(opts, EXIT_FAILURE); 55f003fb54Skardel } 56abb0f93cSkardel 57b8ecfcfeSchristos if (od->fOptState & OPTST_ALLOC_ARG) { 58b8ecfcfeSchristos AGFREE(od->optArg.argString); 59b8ecfcfeSchristos od->fOptState &= ~OPTST_ALLOC_ARG; 60abb0f93cSkardel } 61abb0f93cSkardel 62b8ecfcfeSchristos od->optArg.argInt = (long)val; 63f003fb54Skardel } 64abb0f93cSkardel 65f003fb54Skardel /*=export_func optionTimeDate 66f003fb54Skardel * private: 67f003fb54Skardel * 68f003fb54Skardel * what: process an option with a time and date. 69b8ecfcfeSchristos * arg: + tOptions * + opts + program options descriptor + 70b8ecfcfeSchristos * arg: + tOptDesc * + od + the descriptor for this arg + 71f003fb54Skardel * 72f003fb54Skardel * doc: 73f003fb54Skardel * Decipher a time and date value. 74f003fb54Skardel =*/ 75f003fb54Skardel void 76b8ecfcfeSchristos optionTimeDate(tOptions * opts, tOptDesc * od) 77f003fb54Skardel { 78f003fb54Skardel #if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV) 79b8ecfcfeSchristos if (INQUERY_CALL(opts, od)) 808585484eSchristos return; 818585484eSchristos 82b8ecfcfeSchristos if ((! HAS_pzPkgDataDir(opts)) || (opts->pzPkgDataDir == NULL)) 83f003fb54Skardel goto default_action; 84f003fb54Skardel 85f003fb54Skardel /* 86f003fb54Skardel * Export the DATEMSK environment variable. getdate_r() uses it to 87f003fb54Skardel * find the file with the strptime formats. If we cannot find the file 88f003fb54Skardel * we need ($PKGDATADIR/datemsk), then fall back to just a time duration. 89f003fb54Skardel */ 90f003fb54Skardel { 91f003fb54Skardel static char * envptr = NULL; 92f003fb54Skardel 93f003fb54Skardel if (envptr == NULL) { 94f003fb54Skardel static char const fmt[] = "DATEMSK=%s/datemsk"; 95*eabc0478Schristos size_t sz = sizeof(fmt) + strlen(opts->pzPkgDataDir); 96*eabc0478Schristos envptr = AGALOC(sz, fmt); 97*eabc0478Schristos if (snprintf(envptr, sz, fmt, opts->pzPkgDataDir) >= (int)sz) 98*eabc0478Schristos option_exits(EXIT_FAILURE); 99f003fb54Skardel 100f003fb54Skardel putenv(envptr); 101f003fb54Skardel } 102f003fb54Skardel 103f003fb54Skardel if (access(envptr+8, R_OK) != 0) 104f003fb54Skardel goto default_action; 105f003fb54Skardel } 106f003fb54Skardel 107f003fb54Skardel /* 108f003fb54Skardel * Convert the date to a time since the epoch and stash it in a long int. 109f003fb54Skardel */ 110f003fb54Skardel { 111f003fb54Skardel struct tm stm; 112f003fb54Skardel time_t tm; 113f003fb54Skardel 114b8ecfcfeSchristos if (getdate_r(od->optArg.argString, &stm) != 0) { 115b8ecfcfeSchristos fprintf(stderr, zNotDate, opts->pzProgName, 116b8ecfcfeSchristos od->optArg.argString); 117b8ecfcfeSchristos if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) 118b8ecfcfeSchristos (*(opts->pUsageProc))(opts, EXIT_FAILURE); 119f003fb54Skardel return; 120f003fb54Skardel } 121abb0f93cSkardel 122f003fb54Skardel tm = mktime(&stm); 123f003fb54Skardel 124b8ecfcfeSchristos if (od->fOptState & OPTST_ALLOC_ARG) { 125b8ecfcfeSchristos AGFREE(od->optArg.argString); 126b8ecfcfeSchristos od->fOptState &= ~OPTST_ALLOC_ARG; 127f003fb54Skardel } 128f003fb54Skardel 129b8ecfcfeSchristos od->optArg.argInt = tm; 130f003fb54Skardel } 131f003fb54Skardel return; 132f003fb54Skardel 133f003fb54Skardel default_action: 134f003fb54Skardel 135f003fb54Skardel #endif 136b8ecfcfeSchristos optionTimeVal(opts, od); 137b8ecfcfeSchristos if (od->optArg.argInt != BAD_TIME) 138b8ecfcfeSchristos od->optArg.argInt += (long)time(NULL); 139abb0f93cSkardel } 1408585484eSchristos /** @} 1418585484eSchristos * 142abb0f93cSkardel * Local Variables: 143abb0f93cSkardel * mode: C 144abb0f93cSkardel * c-file-style: "stroustrup" 145abb0f93cSkardel * indent-tabs-mode: nil 146abb0f93cSkardel * End: 147f003fb54Skardel * end of autoopts/time.c */ 148