Coverage Report

Created: 2026-02-26 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntp-dev/sntp/libopts/time.c
Line
Count
Source
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-2018 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
0
{
43
0
    time_t val;
44
45
0
    if (INQUERY_CALL(opts, od))
46
0
        return;
47
48
0
    val = parse_duration(od->optArg.argString);
49
0
    if (val == BAD_TIME) {
50
0
        fprintf(stderr, zNotDuration, opts->pzProgName, od->optArg.argString);
51
0
        if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
52
0
            (*(opts->pUsageProc))(opts, EXIT_FAILURE);
53
0
    }
54
55
0
    if (od->fOptState & OPTST_ALLOC_ARG) {
56
0
        AGFREE(od->optArg.argString);
57
0
        od->fOptState &= ~OPTST_ALLOC_ARG;
58
0
    }
59
60
0
    od->optArg.argInt = (long)val;
61
0
}
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
0
{
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
            size_t sz = sizeof(fmt) + strlen(opts->pzPkgDataDir);
94
            envptr = AGALOC(sz, fmt);
95
            if (snprintf(envptr, sz, fmt, opts->pzPkgDataDir) >= (int)sz)
96
                option_exits(EXIT_FAILURE);
97
98
            putenv(envptr);
99
        }
100
101
        if (access(envptr+8, R_OK) != 0)
102
            goto default_action;
103
    }
104
105
    /*
106
     *  Convert the date to a time since the epoch and stash it in a long int.
107
     */
108
    {
109
        struct tm stm;
110
        time_t tm;
111
112
        if (getdate_r(od->optArg.argString, &stm) != 0) {
113
            fprintf(stderr, zNotDate, opts->pzProgName,
114
                    od->optArg.argString);
115
            if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
116
                (*(opts->pUsageProc))(opts, EXIT_FAILURE);
117
            return;
118
        }
119
120
        tm = mktime(&stm);
121
122
        if (od->fOptState & OPTST_ALLOC_ARG) {
123
            AGFREE(od->optArg.argString);
124
            od->fOptState &= ~OPTST_ALLOC_ARG;
125
        }
126
127
        od->optArg.argInt = tm;
128
    }
129
    return;
130
131
 default_action:
132
133
#endif
134
0
    optionTimeVal(opts, od);
135
0
    if (od->optArg.argInt != BAD_TIME)
136
0
        od->optArg.argInt += (long)time(NULL);
137
0
}
138
/** @}
139
 *
140
 * Local Variables:
141
 * mode: C
142
 * c-file-style: "stroustrup"
143
 * indent-tabs-mode: nil
144
 * End:
145
 * end of autoopts/time.c */