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