1258945Sroberto
2258945Sroberto/**
3258945Sroberto * \file time.c
4258945Sroberto *
5280849Scy * @addtogroup autoopts
6280849Scy * @{
7280849Scy */
8280849Scy/*
9258945Sroberto *  This file is part of AutoOpts, a companion to AutoGen.
10258945Sroberto *  AutoOpts is free software.
11290000Sglebius *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
12258945Sroberto *
13258945Sroberto *  AutoOpts is available under any one of two licenses.  The license
14258945Sroberto *  in use must be one of these two and the choice is under the control
15258945Sroberto *  of the user of the license.
16258945Sroberto *
17258945Sroberto *   The GNU Lesser General Public License, version 3 or later
18258945Sroberto *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
19258945Sroberto *
20258945Sroberto *   The Modified Berkeley Software Distribution License
21258945Sroberto *      See the file "COPYING.mbsd"
22258945Sroberto *
23280849Scy *  These files have the following sha256 sums:
24258945Sroberto *
25280849Scy *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
26280849Scy *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
27280849Scy *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
28258945Sroberto */
29258945Sroberto
30258945Sroberto/*=export_func  optionTimeVal
31258945Sroberto * private:
32258945Sroberto *
33258945Sroberto * what:  process an option with a time duration.
34290000Sglebius * arg:   + tOptions * + opts + program options descriptor +
35290000Sglebius * arg:   + tOptDesc * + od   + the descriptor for this arg +
36258945Sroberto *
37258945Sroberto * doc:
38258945Sroberto *  Decipher a time duration value.
39258945Sroberto=*/
40258945Srobertovoid
41280849ScyoptionTimeVal(tOptions * opts, tOptDesc * od)
42258945Sroberto{
43258945Sroberto    time_t val;
44258945Sroberto
45280849Scy    if (INQUERY_CALL(opts, od))
46258945Sroberto        return;
47258945Sroberto
48280849Scy    val = parse_duration(od->optArg.argString);
49258945Sroberto    if (val == BAD_TIME) {
50280849Scy        fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString);
51280849Scy        if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
52280849Scy            (*(opts->pUsageProc))(opts, EXIT_FAILURE);
53258945Sroberto    }
54258945Sroberto
55280849Scy    if (od->fOptState & OPTST_ALLOC_ARG) {
56280849Scy        AGFREE(od->optArg.argString);
57280849Scy        od->fOptState &= ~OPTST_ALLOC_ARG;
58258945Sroberto    }
59258945Sroberto
60280849Scy    od->optArg.argInt = (long)val;
61258945Sroberto}
62258945Sroberto
63258945Sroberto/*=export_func  optionTimeDate
64258945Sroberto * private:
65258945Sroberto *
66258945Sroberto * what:  process an option with a time and date.
67290000Sglebius * arg:   + tOptions * + opts + program options descriptor +
68290000Sglebius * arg:   + tOptDesc * + od   + the descriptor for this arg +
69258945Sroberto *
70258945Sroberto * doc:
71258945Sroberto *  Decipher a time and date value.
72258945Sroberto=*/
73258945Srobertovoid
74280849ScyoptionTimeDate(tOptions * opts, tOptDesc * od)
75258945Sroberto{
76258945Sroberto#if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
77280849Scy    if (INQUERY_CALL(opts, od))
78280849Scy        return;
79280849Scy
80280849Scy    if ((! HAS_pzPkgDataDir(opts)) || (opts->pzPkgDataDir == NULL))
81258945Sroberto        goto default_action;
82258945Sroberto
83258945Sroberto    /*
84258945Sroberto     *  Export the DATEMSK environment variable.  getdate_r() uses it to
85258945Sroberto     *  find the file with the strptime formats.  If we cannot find the file
86258945Sroberto     *  we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
87258945Sroberto     */
88258945Sroberto    {
89258945Sroberto        static char * envptr = NULL;
90258945Sroberto
91258945Sroberto        if (envptr == NULL) {
92258945Sroberto            static char const fmt[] = "DATEMSK=%s/datemsk";
93280849Scy            envptr = AGALOC(sizeof(fmt) + strlen(opts->pzPkgDataDir), fmt);
94280849Scy            sprintf(envptr, fmt, opts->pzPkgDataDir);
95258945Sroberto
96258945Sroberto            putenv(envptr);
97258945Sroberto        }
98258945Sroberto
99258945Sroberto        if (access(envptr+8, R_OK) != 0)
100258945Sroberto            goto default_action;
101258945Sroberto    }
102258945Sroberto
103258945Sroberto    /*
104258945Sroberto     *  Convert the date to a time since the epoch and stash it in a long int.
105258945Sroberto     */
106258945Sroberto    {
107258945Sroberto        struct tm stm;
108258945Sroberto        time_t tm;
109258945Sroberto
110280849Scy        if (getdate_r(od->optArg.argString, &stm) != 0) {
111280849Scy            fprintf(stderr, zNotDate, opts->pzProgName,
112280849Scy                    od->optArg.argString);
113280849Scy            if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
114280849Scy                (*(opts->pUsageProc))(opts, EXIT_FAILURE);
115258945Sroberto            return;
116258945Sroberto        }
117258945Sroberto
118258945Sroberto        tm = mktime(&stm);
119258945Sroberto
120280849Scy        if (od->fOptState & OPTST_ALLOC_ARG) {
121280849Scy            AGFREE(od->optArg.argString);
122280849Scy            od->fOptState &= ~OPTST_ALLOC_ARG;
123258945Sroberto        }
124258945Sroberto
125280849Scy        od->optArg.argInt = tm;
126258945Sroberto    }
127258945Sroberto    return;
128258945Sroberto
129280849Scy default_action:
130258945Sroberto
131258945Sroberto#endif
132280849Scy    optionTimeVal(opts, od);
133280849Scy    if (od->optArg.argInt != BAD_TIME)
134280849Scy        od->optArg.argInt += (long)time(NULL);
135258945Sroberto}
136280849Scy/** @}
137280849Scy *
138258945Sroberto * Local Variables:
139258945Sroberto * mode: C
140258945Sroberto * c-file-style: "stroustrup"
141258945Sroberto * indent-tabs-mode: nil
142258945Sroberto * End:
143258945Sroberto * end of autoopts/time.c */
144