xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/time.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: time.c,v 1.6 2024/08/18 20:47:25 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-2018 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             size_t sz = sizeof(fmt) + strlen(opts->pzPkgDataDir);
96             envptr = AGALOC(sz, fmt);
97             if (snprintf(envptr, sz, fmt, opts->pzPkgDataDir) >= (int)sz)
98                 option_exits(EXIT_FAILURE);
99 
100             putenv(envptr);
101         }
102 
103         if (access(envptr+8, R_OK) != 0)
104             goto default_action;
105     }
106 
107     /*
108      *  Convert the date to a time since the epoch and stash it in a long int.
109      */
110     {
111         struct tm stm;
112         time_t tm;
113 
114         if (getdate_r(od->optArg.argString, &stm) != 0) {
115             fprintf(stderr, zNotDate, opts->pzProgName,
116                     od->optArg.argString);
117             if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
118                 (*(opts->pUsageProc))(opts, EXIT_FAILURE);
119             return;
120         }
121 
122         tm = mktime(&stm);
123 
124         if (od->fOptState & OPTST_ALLOC_ARG) {
125             AGFREE(od->optArg.argString);
126             od->fOptState &= ~OPTST_ALLOC_ARG;
127         }
128 
129         od->optArg.argInt = tm;
130     }
131     return;
132 
133  default_action:
134 
135 #endif
136     optionTimeVal(opts, od);
137     if (od->optArg.argInt != BAD_TIME)
138         od->optArg.argInt += (long)time(NULL);
139 }
140 /** @}
141  *
142  * Local Variables:
143  * mode: C
144  * c-file-style: "stroustrup"
145  * indent-tabs-mode: nil
146  * End:
147  * end of autoopts/time.c */
148