]>
git.saurik.com Git - apple/libc.git/blob - stdtime/getdate.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/types.h>
32 DATEMSK_NOT_DEFINED
= 1,
42 #define DATEMSK "DATEMSK"
43 #define TM_SEC_SET 0x01
44 #define TM_MIN_SET 0x02
45 #define TM_HOUR_SET 0x04
46 #define TM_MDAY_SET 0x01
47 #define TM_MON_SET 0x02
48 #define TM_YEAR_SET 0x04
51 static struct tm tmundef
= {
66 * Call strptime() on each line of the DATEMSK file, and manipulate the
67 * resulting tm structure.
70 getdate(const char *str
)
72 static struct tm tm
, *now
, *result
= NULL
;
75 int bufsiz
, offset
, len
, dateset
, timeset
, saveerrno
, wday_set
;
78 char *file
= getenv(DATEMSK
);
81 getdate_err
= DATEMSK_NOT_DEFINED
;
85 if((fp
= fopen(file
, "r")) == NULL
) {
86 getdate_err
= CANT_OPEN
;
91 if(fstat(fileno(fp
), &st
) < 0) {
92 getdate_err
= CANT_FSTAT
;
95 if((st
.st_mode
& S_IFMT
) != S_IFREG
) {
96 getdate_err
= NOT_FILE
;
99 if((buf
= malloc(bufsiz
= BUFSIZ
)) == NULL
) {
100 getdate_err
= OUT_OF_MEMORY
;
106 if(fgets(buf
+ offset
, bufsiz
- offset
, fp
) == NULL
) {
108 getdate_err
= IO_ERROR
;
112 getdate_err
= NO_MATCH
;
116 } else if((len
= strlen(buf
)) == bufsiz
- 1
117 && buf
[len
- 1] != '\n') {
118 char *newptr
= realloc(buf
, (bufsiz
+= BUFSIZ
));
120 getdate_err
= OUT_OF_MEMORY
;
127 if(buf
[len
- 1] == '\n')
130 if(strptime(str
, buf
, &tm
) == NULL
) {
136 dateset
= timeset
= 0;
137 if(tm
.tm_sec
!= UNDEFINED
)
138 timeset
|= TM_SEC_SET
;
139 if(tm
.tm_min
!= UNDEFINED
)
140 timeset
|= TM_MIN_SET
;
141 if(tm
.tm_hour
!= UNDEFINED
)
142 timeset
|= TM_HOUR_SET
;
143 if(tm
.tm_mday
!= UNDEFINED
)
144 dateset
|= TM_MDAY_SET
;
145 if(tm
.tm_mon
!= UNDEFINED
)
146 dateset
|= TM_MON_SET
;
147 if(tm
.tm_year
!= UNDEFINED
)
148 dateset
|= TM_YEAR_SET
;
149 wday_set
= tm
.tm_wday
;
153 tm
.tm_sec
= now
->tm_sec
;
154 tm
.tm_min
= now
->tm_min
;
155 tm
.tm_hour
= now
->tm_hour
;
159 tm
.tm_hour
= now
->tm_hour
;
160 tm
.tm_min
= now
->tm_min
;
161 if(tm
.tm_sec
< now
->tm_sec
)
166 tm
.tm_hour
= now
->tm_hour
;
167 if(tm
.tm_min
< now
->tm_min
)
172 case TM_MIN_SET
| TM_SEC_SET
:
173 tm
.tm_hour
= now
->tm_hour
;
174 if((60 * tm
.tm_min
+ tm
.tm_sec
)
175 < (60 * now
->tm_min
+ now
->tm_sec
))
184 case TM_HOUR_SET
| TM_SEC_SET
:
188 case TM_HOUR_SET
| TM_MIN_SET
:
195 tm
.tm_mday
= now
->tm_mday
;
196 if(tm
.tm_hour
< now
->tm_hour
)
198 tm
.tm_mon
= now
->tm_mon
;
199 tm
.tm_year
= now
->tm_year
;
203 tm
.tm_year
= now
->tm_year
;
204 tm
.tm_mon
= now
->tm_mon
;
205 if(tm
.tm_mday
< now
->tm_mday
)
210 case TM_MON_SET
| TM_MDAY_SET
:
211 tm
.tm_year
= now
->tm_year
;
212 if(tm
.tm_mon
< now
->tm_mon
)
214 if(!(dateset
& TM_MDAY_SET
))
219 case TM_YEAR_SET
| TM_MON_SET
:
220 if(!(dateset
& TM_MON_SET
))
225 case TM_YEAR_SET
| TM_MDAY_SET
:
226 tm
.tm_mon
= now
->tm_mon
;
227 if(tm
.tm_mday
< now
->tm_mday
)
232 tm
.tm_wday
= now
->tm_wday
;
233 tm
.tm_gmtoff
= now
->tm_gmtoff
; /* XXX: can't grok timezones */
236 if(mktime(&tm
) == (time_t)-1) {
237 getdate_err
= INVALID_DATE
;
240 if(wday_set
!= UNDEFINED
) {
241 int delta
= wday_set
- tm
.tm_wday
;
242 if(delta
&& (dateset
& TM_MDAY_SET
)) {
243 getdate_err
= INVALID_DATE
;
249 if(mktime(&tm
) == (time_t)-1) {
250 getdate_err
= INVALID_DATE
;