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