2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
42 #define TMSENTINEL (-1)
46 * getdate_err is set to one of the following values on error.
48 * 1 The DATEMSK environment variable is null or undefined.
49 * 2 The template file cannot be opened for reading.
50 * 3 Failed to get file status information.
51 * 4 Template file is not a regular file.
52 * 5 Encountered an error while reading the template file.
53 * 6 Cannot allocate memory.
54 * 7 Input string does not match any line in the template.
55 * 8 Input string is invalid (for example, February 31) or could not
56 * be represented in a time_t.
58 * Note that on Solaris, getdate_err is possibly a function, to account
59 * for reentrancy. See the code for getdate_err.c for details.
63 #pragma weak _getdate = getdate
65 getdate(const char *str
)
67 char *datemsk
, *line
, *rp
;
70 static struct tm rtm
, tmnow
;
71 struct tm
*tmp
, *rtmp
= &rtm
;
75 if (((datemsk
= getenv("DATEMSK")) == NULL
) || *datemsk
== '\0') {
80 if (stat(datemsk
, &sb
) < 0) {
85 if ((sb
.st_mode
& S_IFMT
) != S_IFREG
) {
90 if ((fp
= fopen(datemsk
, "r")) == NULL
) {
95 /* loop through datemsk file */
100 * The NetBSD implementation supports a rich flexible file format
101 * with embedded escapes, etc. We don't need any of that. Solaris
102 * just reads the template file and (undocumented!) requires that
103 * each line not exceed 512 bytes, using a fixed buffer. We could
104 * improve on that, but this may grow the stack unreasonably, so
105 * we keep it to the same 512 limit. Some day we can be smarter.
106 * (Note FreeBSD doesn't even have getdate(), and IMO nobody sane
107 * should be using this crufty API. strptime is better.)
110 (void) memset(buf
, 0, sizeof (buf
));
111 while ((line
= fgets(buf
, sizeof (buf
), fp
)) != NULL
) {
113 * If the buffer consumed the entire string, then
114 * the input line was too long. We just check to
115 * see if the 2nd to last byte is set. If it isn't,
116 * then we hit a null byte first, and the line is
119 if (buf
[sizeof (buf
) - 2] != 0) {
124 /* initialize tmp with sentinels */
125 rtm
.tm_sec
= rtm
.tm_min
= rtm
.tm_hour
= TMSENTINEL
;
126 rtm
.tm_mday
= rtm
.tm_mon
= rtm
.tm_year
= TMSENTINEL
;
127 rtm
.tm_wday
= rtm
.tm_yday
= rtm
.tm_isdst
= TMSENTINEL
;
128 rp
= strptime(str
, line
, rtmp
);
133 if (errno
!= 0 || ferror(fp
)) {
141 if (feof(fp
) || (rp
!= NULL
&& *rp
!= '\0')) {
148 tmp
= localtime(&now
);
152 * This implementation does not accept setting the broken-down time
153 * to anything other than the localtime(). It is not possible to
154 * change the scanned timezone with %Z.
156 * Note IRIX and Solaris accept only the current zone for %Z.
157 * XXX Is there any implementation that matches the standard?
158 * XXX (Or am I reading the standard wrong?)
160 * Note: Neither XPG 6 (POSIX 2004) nor XPG 7 (POSIX 2008)
161 * requires strptime(3) support for %Z.
165 * Given only a weekday find the first matching weekday starting
166 * with the current weekday and moving into the future.
168 if (rtm
.tm_wday
!= TMSENTINEL
&& rtm
.tm_year
== TMSENTINEL
&&
169 rtm
.tm_mon
== TMSENTINEL
&& rtm
.tm_mday
== TMSENTINEL
) {
170 rtm
.tm_year
= tmnow
.tm_year
;
171 rtm
.tm_mon
= tmnow
.tm_mon
;
172 rtm
.tm_mday
= tmnow
.tm_mday
+
173 (rtm
.tm_wday
- tmnow
.tm_wday
+ 7) % 7;
177 * Given only a month (and no year) find the first matching month
178 * starting with the current month and moving into the future.
180 if (rtm
.tm_mon
!= TMSENTINEL
) {
181 if (rtm
.tm_year
== TMSENTINEL
) {
182 rtm
.tm_year
= tmnow
.tm_year
+
183 ((rtm
.tm_mon
< tmnow
.tm_mon
)? 1 : 0);
185 if (rtm
.tm_mday
== TMSENTINEL
) {
186 /* assume the first of the month */
189 * XXX This isn't documented! Just observed behavior.
191 * Given the weekday find the first matching weekday
192 * starting with the weekday of the first day of the
193 * the month and moving into the future.
195 if (rtm
.tm_wday
!= TMSENTINEL
) {
198 (void) memset(&tm
, 0, sizeof (struct tm
));
199 tm
.tm_year
= rtm
.tm_year
;
200 tm
.tm_mon
= rtm
.tm_mon
;
204 (rtm
.tm_wday
- tm
.tm_wday
+ 7) % 7;
210 * Given no time of day assume the current time of day.
212 if (rtm
.tm_hour
== TMSENTINEL
&&
213 rtm
.tm_min
== TMSENTINEL
&& rtm
.tm_sec
== TMSENTINEL
) {
214 rtm
.tm_hour
= tmnow
.tm_hour
;
215 rtm
.tm_min
= tmnow
.tm_min
;
216 rtm
.tm_sec
= tmnow
.tm_sec
;
219 * Given an hour and no date, find the first matching hour starting
220 * with the current hour and moving into the future
222 if (rtm
.tm_hour
!= TMSENTINEL
&&
223 rtm
.tm_year
== TMSENTINEL
&& rtm
.tm_mon
== TMSENTINEL
&&
224 rtm
.tm_mday
== TMSENTINEL
) {
225 rtm
.tm_year
= tmnow
.tm_year
;
226 rtm
.tm_mon
= tmnow
.tm_mon
;
227 rtm
.tm_mday
= tmnow
.tm_mday
;
228 if (rtm
.tm_hour
< tmnow
.tm_hour
)
233 * Set to 'sane' values; mktime(3) does funny things otherwise.
234 * No hours, no minutes, no seconds, no service.
236 if (rtm
.tm_hour
== TMSENTINEL
)
238 if (rtm
.tm_min
== TMSENTINEL
)
240 if (rtm
.tm_sec
== TMSENTINEL
)
244 * Given only a year the values of month, day of month, day of year,
245 * week day and is daylight (summer) time are unspecified.
246 * (Specified on the Solaris man page not POSIX.)
248 if (rtm
.tm_year
!= TMSENTINEL
&&
249 rtm
.tm_mon
== TMSENTINEL
&& rtm
.tm_mday
== TMSENTINEL
) {
253 * XXX More undocumented functionality but observed.
255 * Given the weekday find the first matching weekday
256 * starting with the weekday of the first day of the
257 * month and moving into the future.
259 if (rtm
.tm_wday
!= TMSENTINEL
) {
262 (void) memset(&tm
, 0, sizeof (struct tm
));
263 tm
.tm_year
= rtm
.tm_year
;
264 tm
.tm_mon
= rtm
.tm_mon
;
267 rtm
.tm_mday
+= (rtm
.tm_wday
- tm
.tm_wday
+ 7) % 7;
272 * Given only the century but no year within, the current year
273 * is assumed. (Specified on the Solaris man page not POSIX.)
275 * Warning ugly end case
277 * This is more work since strptime(3) doesn't "do the right thing".
279 if (rtm
.tm_year
!= TMSENTINEL
&& (rtm
.tm_year
- 1900) >= 0) {
281 rtm
.tm_year
+= (tmnow
.tm_year
% 100);
285 * mktime() will normalize all values and also check that the
286 * value will fit into a time_t.
288 * This is only for POSIX correctness. A date >= 1900 is
289 * really ok, but using a time_t limits things.
291 if (mktime(rtmp
) < 0) {