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