xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/time.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: time.c,v 1.1.1.3 2013/12/27 23:31: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-2013 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* + pOpts    + program options descriptor +
37  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
38  *
39  * doc:
40  *  Decipher a time duration value.
41 =*/
42 void
43 optionTimeVal(tOptions * pOpts, tOptDesc * pOD)
44 {
45     time_t val;
46 
47     if (pOpts <= OPTPROC_EMIT_LIMIT)
48         return;
49 
50     if ((pOD->fOptState & OPTST_RESET) != 0)
51         return;
52 
53     val = parse_duration(pOD->optArg.argString);
54     if (val == BAD_TIME) {
55         fprintf(stderr, zNotDuration, pOpts->pzProgName, pOD->optArg.argString);
56         if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
57             (*(pOpts->pUsageProc))(pOpts, EXIT_FAILURE);
58     }
59 
60     if (pOD->fOptState & OPTST_ALLOC_ARG) {
61         AGFREE(pOD->optArg.argString);
62         pOD->fOptState &= ~OPTST_ALLOC_ARG;
63     }
64 
65     pOD->optArg.argInt = (long)val;
66 }
67 
68 /*=export_func  optionTimeDate
69  * private:
70  *
71  * what:  process an option with a time and date.
72  * arg:   + tOptions* + pOpts    + program options descriptor +
73  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
74  *
75  * doc:
76  *  Decipher a time and date value.
77 =*/
78 void
79 optionTimeDate(tOptions * pOpts, tOptDesc * pOD)
80 {
81 #if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
82     if (pOpts <= OPTPROC_EMIT_LIMIT)
83         return;
84 
85     if ((! HAS_pzPkgDataDir(pOpts)) || (pOpts->pzPkgDataDir == NULL))
86         goto default_action;
87 
88     /*
89      *  Export the DATEMSK environment variable.  getdate_r() uses it to
90      *  find the file with the strptime formats.  If we cannot find the file
91      *  we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
92      */
93     {
94         static char * envptr = NULL;
95 
96         if (envptr == NULL) {
97             static char const fmt[] = "DATEMSK=%s/datemsk";
98             envptr = AGALOC(sizeof(fmt) + strlen(pOpts->pzPkgDataDir), fmt);
99             sprintf(envptr, fmt, pOpts->pzPkgDataDir);
100 
101             putenv(envptr);
102         }
103 
104         if (access(envptr+8, R_OK) != 0)
105             goto default_action;
106     }
107 
108     /*
109      *  Convert the date to a time since the epoch and stash it in a long int.
110      */
111     {
112         struct tm stm;
113         time_t tm;
114 
115         if (getdate_r(pOD->optArg.argString, &stm) != 0) {
116             fprintf(stderr, zNotDate, pOpts->pzProgName,
117                     pOD->optArg.argString);
118             if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
119                 (*(pOpts->pUsageProc))(pOpts, EXIT_FAILURE);
120             return;
121         }
122 
123         tm = mktime(&stm);
124 
125         if (pOD->fOptState & OPTST_ALLOC_ARG) {
126             AGFREE(pOD->optArg.argString);
127             pOD->fOptState &= ~OPTST_ALLOC_ARG;
128         }
129 
130         pOD->optArg.argInt = tm;
131     }
132     return;
133 
134  default_action:
135 
136 #endif
137     optionTimeVal(pOpts, pOD);
138     if (pOD->optArg.argInt != BAD_TIME)
139         pOD->optArg.argInt += (long)time(NULL);
140 }
141 /** @}
142  *
143  * Local Variables:
144  * mode: C
145  * c-file-style: "stroustrup"
146  * indent-tabs-mode: nil
147  * End:
148  * end of autoopts/time.c */
149