getdate.c revision 1.4 1 1.4 kamil /* $NetBSD: getdate.c,v 1.4 2018/01/04 20:57:29 kamil Exp $ */
2 1.1 ginsbach /*
3 1.1 ginsbach * Copyright (c) 2009 The NetBSD Foundation, Inc.
4 1.1 ginsbach * All rights reserved.
5 1.1 ginsbach *
6 1.1 ginsbach * This code is derived from software contributed to The NetBSD Foundation
7 1.1 ginsbach * by Brian Ginsbach.
8 1.1 ginsbach *
9 1.1 ginsbach * Redistribution and use in source and binary forms, with or without
10 1.1 ginsbach * modification, are permitted provided that the following conditions
11 1.1 ginsbach * are met:
12 1.1 ginsbach * 1. Redistributions of source code must retain the above copyright
13 1.1 ginsbach * notice, this list of conditions and the following disclaimer.
14 1.1 ginsbach * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 ginsbach * notice, this list of conditions and the following disclaimer in the
16 1.1 ginsbach * documentation and/or other materials provided with the distribution.
17 1.1 ginsbach *
18 1.1 ginsbach * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 ginsbach * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 ginsbach * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 ginsbach * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 ginsbach * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 ginsbach * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 ginsbach * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 ginsbach * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 ginsbach * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 ginsbach * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 ginsbach * POSSIBILITY OF SUCH DAMAGE.
29 1.1 ginsbach */
30 1.1 ginsbach
31 1.4 kamil #include "namespace.h"
32 1.4 kamil
33 1.1 ginsbach #include <sys/stat.h>
34 1.1 ginsbach
35 1.1 ginsbach #include <errno.h>
36 1.1 ginsbach #include <stdio.h>
37 1.1 ginsbach #include <stdlib.h>
38 1.1 ginsbach #include <string.h>
39 1.1 ginsbach #include <time.h>
40 1.1 ginsbach #include <unistd.h>
41 1.1 ginsbach #include <util.h>
42 1.1 ginsbach
43 1.1 ginsbach #define TMSENTINEL (-1)
44 1.1 ginsbach
45 1.1 ginsbach /*
46 1.1 ginsbach * getdate_err is set to one of the following values on error.
47 1.1 ginsbach *
48 1.1 ginsbach * 1 The DATEMSK environment variable is null or undefined.
49 1.1 ginsbach * 2 The template file cannot be opened for reading.
50 1.1 ginsbach * 3 Failed to get file status information.
51 1.1 ginsbach * 4 Template file is not a regular file.
52 1.1 ginsbach * 5 Encountered an error while reading the template file.
53 1.1 ginsbach * 6 Cannot allocate memory.
54 1.1 ginsbach * 7 Input string does not match any line in the template.
55 1.1 ginsbach * 8 Input string is invalid (for example, February 31) or could not
56 1.1 ginsbach * be represented in a time_t.
57 1.1 ginsbach */
58 1.1 ginsbach
59 1.1 ginsbach int getdate_err;
60 1.1 ginsbach
61 1.1 ginsbach struct tm *
62 1.1 ginsbach getdate(const char *str)
63 1.1 ginsbach {
64 1.1 ginsbach char *datemsk, *line, *rp;
65 1.1 ginsbach FILE *fp;
66 1.1 ginsbach struct stat sb;
67 1.1 ginsbach static struct tm rtm, tmnow;
68 1.1 ginsbach struct tm *tmp, *rtmp = &rtm;
69 1.1 ginsbach size_t lineno = 0;
70 1.1 ginsbach time_t now;
71 1.1 ginsbach
72 1.1 ginsbach if (((datemsk = getenv("DATEMSK")) == NULL) || *datemsk == '\0') {
73 1.1 ginsbach getdate_err = 1;
74 1.1 ginsbach return (NULL);
75 1.1 ginsbach }
76 1.1 ginsbach
77 1.1 ginsbach if (stat(datemsk, &sb) < 0) {
78 1.1 ginsbach getdate_err = 3;
79 1.1 ginsbach return (NULL);
80 1.1 ginsbach }
81 1.1 ginsbach
82 1.1 ginsbach if ((sb.st_mode & S_IFMT) != S_IFREG) {
83 1.1 ginsbach getdate_err = 4;
84 1.1 ginsbach return (NULL);
85 1.1 ginsbach }
86 1.1 ginsbach
87 1.3 christos if ((fp = fopen(datemsk, "re")) == NULL) {
88 1.1 ginsbach getdate_err = 2;
89 1.1 ginsbach return (NULL);
90 1.1 ginsbach }
91 1.1 ginsbach
92 1.1 ginsbach /* loop through datemsk file */
93 1.1 ginsbach errno = 0;
94 1.1 ginsbach rp = NULL;
95 1.1 ginsbach while ((line = fparseln(fp, NULL, &lineno, NULL, 0)) != NULL) {
96 1.1 ginsbach /* initialize tmp with sentinels */
97 1.1 ginsbach rtm.tm_sec = rtm.tm_min = rtm.tm_hour = TMSENTINEL;
98 1.1 ginsbach rtm.tm_mday = rtm.tm_mon = rtm.tm_year = TMSENTINEL;
99 1.1 ginsbach rtm.tm_wday = rtm.tm_yday = rtm.tm_isdst = TMSENTINEL;
100 1.1 ginsbach rtm.tm_gmtoff = 0;
101 1.1 ginsbach rtm.tm_zone = NULL;
102 1.1 ginsbach rp = strptime(str, line, rtmp);
103 1.1 ginsbach free(line);
104 1.1 ginsbach if (rp != NULL)
105 1.1 ginsbach break;
106 1.1 ginsbach errno = 0;
107 1.1 ginsbach }
108 1.1 ginsbach if (errno != 0 || ferror(fp)) {
109 1.1 ginsbach if (errno == ENOMEM)
110 1.1 ginsbach getdate_err = 6;
111 1.1 ginsbach else
112 1.1 ginsbach getdate_err = 5;
113 1.1 ginsbach fclose(fp);
114 1.1 ginsbach return (NULL);
115 1.1 ginsbach }
116 1.1 ginsbach if (feof(fp) || (rp != NULL && *rp != '\0')) {
117 1.1 ginsbach getdate_err = 7;
118 1.1 ginsbach return (NULL);
119 1.1 ginsbach }
120 1.1 ginsbach fclose(fp);
121 1.1 ginsbach
122 1.1 ginsbach time(&now);
123 1.1 ginsbach tmp = localtime(&now);
124 1.1 ginsbach tmnow = *tmp;
125 1.1 ginsbach
126 1.1 ginsbach /*
127 1.1 ginsbach * This implementation does not accept setting the broken-down time
128 1.1 ginsbach * to anything other than the localtime(). It is not possible to
129 1.1 ginsbach * change the scanned timezone with %Z.
130 1.1 ginsbach *
131 1.1 ginsbach * Note IRIX and Solaris accept only the current zone for %Z.
132 1.1 ginsbach * XXX Is there any implementation that matches the standard?
133 1.1 ginsbach * XXX (Or am I reading the standard wrong?)
134 1.1 ginsbach *
135 1.1 ginsbach * Note: Neither XPG 6 (POSIX 2004) nor XPG 7 (POSIX 2008)
136 1.1 ginsbach * requires strptime(3) support for %Z.
137 1.1 ginsbach */
138 1.1 ginsbach
139 1.1 ginsbach /*
140 1.1 ginsbach * Given only a weekday find the first matching weekday starting
141 1.1 ginsbach * with the current weekday and moving into the future.
142 1.1 ginsbach */
143 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL && rtm.tm_year == TMSENTINEL &&
144 1.1 ginsbach rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
145 1.1 ginsbach rtm.tm_year = tmnow.tm_year;
146 1.1 ginsbach rtm.tm_mon = tmnow.tm_mon;
147 1.1 ginsbach rtm.tm_mday = tmnow.tm_mday +
148 1.1 ginsbach (rtm.tm_wday - tmnow.tm_wday + 7) % 7;
149 1.1 ginsbach }
150 1.1 ginsbach
151 1.1 ginsbach /*
152 1.1 ginsbach * Given only a month (and no year) find the first matching month
153 1.1 ginsbach * starting with the current month and moving into the future.
154 1.1 ginsbach */
155 1.1 ginsbach if (rtm.tm_mon != TMSENTINEL) {
156 1.1 ginsbach if (rtm.tm_year == TMSENTINEL) {
157 1.1 ginsbach rtm.tm_year = tmnow.tm_year +
158 1.1 ginsbach ((rtm.tm_mon < tmnow.tm_mon)? 1 : 0);
159 1.1 ginsbach }
160 1.1 ginsbach if (rtm.tm_mday == TMSENTINEL) {
161 1.1 ginsbach /* assume the first of the month */
162 1.1 ginsbach rtm.tm_mday = 1;
163 1.1 ginsbach /*
164 1.1 ginsbach * XXX This isn't documented! Just observed behavior.
165 1.1 ginsbach *
166 1.1 ginsbach * Given the weekday find the first matching weekday
167 1.1 ginsbach * starting with the weekday of the first day of the
168 1.1 ginsbach * the month and moving into the future.
169 1.1 ginsbach */
170 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL) {
171 1.1 ginsbach struct tm tm;
172 1.1 ginsbach
173 1.1 ginsbach memset(&tm, 0, sizeof(struct tm));
174 1.1 ginsbach tm.tm_year = rtm.tm_year;
175 1.1 ginsbach tm.tm_mon = rtm.tm_mon;
176 1.1 ginsbach tm.tm_mday = 1;
177 1.1 ginsbach mktime(&tm);
178 1.1 ginsbach rtm.tm_mday +=
179 1.1 ginsbach (rtm.tm_wday - tm.tm_wday + 7) % 7;
180 1.1 ginsbach }
181 1.1 ginsbach }
182 1.1 ginsbach }
183 1.1 ginsbach
184 1.1 ginsbach /*
185 1.1 ginsbach * Given no time of day assume the current time of day.
186 1.1 ginsbach */
187 1.1 ginsbach if (rtm.tm_hour == TMSENTINEL &&
188 1.1 ginsbach rtm.tm_min == TMSENTINEL && rtm.tm_sec == TMSENTINEL) {
189 1.1 ginsbach rtm.tm_hour = tmnow.tm_hour;
190 1.1 ginsbach rtm.tm_min = tmnow.tm_min;
191 1.1 ginsbach rtm.tm_sec = tmnow.tm_sec;
192 1.1 ginsbach }
193 1.1 ginsbach /*
194 1.1 ginsbach * Given an hour and no date, find the first matching hour starting
195 1.1 ginsbach * with the current hour and moving into the future
196 1.1 ginsbach */
197 1.1 ginsbach if (rtm.tm_hour != TMSENTINEL &&
198 1.1 ginsbach rtm.tm_year == TMSENTINEL && rtm.tm_mon == TMSENTINEL &&
199 1.1 ginsbach rtm.tm_mday == TMSENTINEL) {
200 1.1 ginsbach rtm.tm_year = tmnow.tm_year;
201 1.1 ginsbach rtm.tm_mon = tmnow.tm_mon;
202 1.1 ginsbach rtm.tm_mday = tmnow.tm_mday;
203 1.1 ginsbach if (rtm.tm_hour < tmnow.tm_hour)
204 1.1 ginsbach rtm.tm_hour += 24;
205 1.1 ginsbach }
206 1.1 ginsbach
207 1.1 ginsbach /*
208 1.1 ginsbach * Set to 'sane' values; mktime(3) does funny things otherwise.
209 1.1 ginsbach * No hours, no minutes, no seconds, no service.
210 1.1 ginsbach */
211 1.1 ginsbach if (rtm.tm_hour == TMSENTINEL)
212 1.1 ginsbach rtm.tm_hour = 0;
213 1.1 ginsbach if (rtm.tm_min == TMSENTINEL)
214 1.1 ginsbach rtm.tm_min = 0;
215 1.1 ginsbach if (rtm.tm_sec == TMSENTINEL)
216 1.1 ginsbach rtm.tm_sec = 0;
217 1.1 ginsbach
218 1.1 ginsbach /*
219 1.1 ginsbach * Given only a year the values of month, day of month, day of year,
220 1.1 ginsbach * week day and is daylight (summer) time are unspecified.
221 1.1 ginsbach * (Specified on the Solaris man page not POSIX.)
222 1.1 ginsbach */
223 1.1 ginsbach if (rtm.tm_year != TMSENTINEL &&
224 1.1 ginsbach rtm.tm_mon == TMSENTINEL && rtm.tm_mday == TMSENTINEL) {
225 1.1 ginsbach rtm.tm_mon = 0;
226 1.1 ginsbach rtm.tm_mday = 1;
227 1.1 ginsbach /*
228 1.1 ginsbach * XXX More undocumented functionality but observed.
229 1.1 ginsbach *
230 1.1 ginsbach * Given the weekday find the first matching weekday
231 1.2 mbalmer * starting with the weekday of the first day of the
232 1.1 ginsbach * month and moving into the future.
233 1.1 ginsbach */
234 1.1 ginsbach if (rtm.tm_wday != TMSENTINEL) {
235 1.1 ginsbach struct tm tm;
236 1.1 ginsbach
237 1.1 ginsbach memset(&tm, 0, sizeof(struct tm));
238 1.1 ginsbach tm.tm_year = rtm.tm_year;
239 1.1 ginsbach tm.tm_mon = rtm.tm_mon;
240 1.1 ginsbach tm.tm_mday = 1;
241 1.1 ginsbach mktime(&tm);
242 1.1 ginsbach rtm.tm_mday += (rtm.tm_wday - tm.tm_wday + 7) % 7;
243 1.1 ginsbach }
244 1.1 ginsbach }
245 1.1 ginsbach
246 1.1 ginsbach /*
247 1.1 ginsbach * Given only the century but no year within, the current year
248 1.1 ginsbach * is assumed. (Specified on the Solaris man page not POSIX.)
249 1.1 ginsbach *
250 1.1 ginsbach * Warning ugly end case
251 1.1 ginsbach *
252 1.1 ginsbach * This is more work since strptime(3) doesn't "do the right thing".
253 1.1 ginsbach */
254 1.1 ginsbach if (rtm.tm_year != TMSENTINEL && (rtm.tm_year - 1900) >= 0) {
255 1.1 ginsbach rtm.tm_year -= 1900;
256 1.1 ginsbach rtm.tm_year += (tmnow.tm_year % 100);
257 1.1 ginsbach }
258 1.1 ginsbach
259 1.1 ginsbach /*
260 1.1 ginsbach * mktime() will normalize all values and also check that the
261 1.1 ginsbach * value will fit into a time_t.
262 1.1 ginsbach *
263 1.1 ginsbach * This is only for POSIX correctness. A date >= 1900 is
264 1.1 ginsbach * really ok, but using a time_t limits things.
265 1.1 ginsbach */
266 1.1 ginsbach if (mktime(rtmp) < 0) {
267 1.1 ginsbach getdate_err = 8;
268 1.1 ginsbach return (NULL);
269 1.1 ginsbach }
270 1.1 ginsbach
271 1.1 ginsbach return (rtmp);
272 1.1 ginsbach }
273