]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/localtime.c.patch
0bdf074403ce994ccf458826ea4b7dccc1faf852
[apple/libc.git] / stdtime / FreeBSD / localtime.c.patch
1 --- localtime.c.orig 2007-05-23 18:18:18.000000000 -0700
2 +++ localtime.c 2007-05-23 18:20:52.000000000 -0700
3 @@ -22,8 +22,22 @@
4 #include "namespace.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 +#include <time.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 +#include <errno.h>
11 +#ifdef NOTIFY_TZ
12 +//#define NOTIFY_TZ_DEBUG
13 +//#define NOTIFY_TZ_DEBUG_FILE "/var/log/localtime.debug"
14 +//#define NOTIFY_TZ_LOG "/var/log/localtime.log"
15 +/* force ALL_STATE if NOTIFY_TZ is set */
16 +#ifndef ALL_STATE
17 +#define ALL_STATE
18 +#endif /* ALL_STATE */
19 +#include <mach/mach_init.h>
20 +#include <notify.h>
21 +#include <alloca.h>
22 +#endif /* NOTIFY_TZ */
23 #include "private.h"
24 #include "un-namespace.h"
25
26 @@ -33,6 +47,7 @@
27
28 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
29 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
30 +extern int __pthread_tsd_first;
31
32 /*
33 ** SunOS 4.1.1 headers lack O_BINARY.
34 @@ -135,40 +150,96 @@
35 #define DAY_OF_YEAR 1 /* n - day of year */
36 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
37
38 +#ifdef NOTIFY_TZ
39 +typedef struct {
40 + int token;
41 + int notify_was_off;
42 + int is_set;
43 +} notify_tz_t;
44 +
45 +#define NOTIFY_TZ_NAME "com.apple.system.timezone"
46 +#endif /* NOTIFY_TZ */
47 +
48 /*
49 ** Prototypes for static functions.
50 */
51 +#define localsub _st_localsub
52 +#define time1 _st_time1
53 +#define tzset_basic _st_tzset_basic
54 +__private_extern__
55 +#ifdef __LP64__
56 +struct tm * localsub(const time_t * timep, long offset,
57 + struct tm * tmp);
58 +#else /* !__LP64__ */
59 +void localsub(const time_t * timep, long offset,
60 + struct tm * tmp);
61 +#endif /* __LP64__ */
62 +__private_extern__
63 +time_t time1(struct tm * tmp,
64 +#ifdef __LP64__
65 + struct tm *(*funcp) (const time_t *,
66 + long, struct tm *),
67 +#else /* !__LP64__ */
68 + void(*funcp) (const time_t *,
69 + long, struct tm *),
70 +#endif /* __LP64__ */
71 + long offset,
72 + int unix03);
73 +__private_extern__
74 +void tzset_basic(void);
75 +
76 +#define lcl_mutex _st_lcl_mutex
77
78 +#if !BUILDING_VARIANT
79 static long detzcode(const char * codep);
80 -static const char * getzname(const char * strp);
81 +static const char * getzname(const char * strp, char **name, size_t *len);
82 static const char * getnum(const char * strp, int * nump, int min,
83 int max);
84 static const char * getsecs(const char * strp, long * secsp);
85 static const char * getoffset(const char * strp, long * offsetp);
86 static const char * getrule(const char * strp, struct rule * rulep);
87 static void gmtload(struct state * sp);
88 -static void gmtsub(const time_t * timep, long offset,
89 +#ifdef __LP64__
90 +static struct tm * gmtsub(const time_t * timep, long offset,
91 struct tm * tmp);
92 -static void localsub(const time_t * timep, long offset,
93 +#else /* !__LP64__ */
94 +static void gmtsub(const time_t * timep, long offset,
95 struct tm * tmp);
96 +#endif /* __LP64__ */
97 static int increment_overflow(int * number, int delta);
98 static int normalize_overflow(int * tensptr, int * unitsptr,
99 int base);
100 +#ifdef NOTIFY_TZ
101 +static void notify_check_tz(notify_tz_t *p);
102 +static void notify_register_tz(char *file, notify_tz_t *p);
103 +#endif /* NOTIFY_TZ */
104 static void settzname(void);
105 -static time_t time1(struct tm * tmp,
106 - void(*funcp) (const time_t *,
107 - long, struct tm *),
108 - long offset);
109 static time_t time2(struct tm *tmp,
110 +#ifdef __LP64__
111 + struct tm *(*funcp) (const time_t *,
112 + long, struct tm*),
113 +#else /* !__LP64__ */
114 void(*funcp) (const time_t *,
115 long, struct tm*),
116 - long offset, int * okayp);
117 +#endif /* __LP64__ */
118 + long offset, int * okayp, int unix03);
119 static time_t time2sub(struct tm *tmp,
120 +#ifdef __LP64__
121 + struct tm *(*funcp) (const time_t *,
122 + long, struct tm*),
123 +#else /* !__LP64__ */
124 void(*funcp) (const time_t *,
125 long, struct tm*),
126 - long offset, int * okayp, int do_norm_secs);
127 +#endif /* __LP64__ */
128 + long offset, int * okayp, int do_norm_secs,
129 + int unix03);
130 +#ifdef __LP64__
131 +static struct tm * timesub(const time_t * timep, long offset,
132 + const struct state * sp, struct tm * tmp);
133 +#else /* !__LP64__ */
134 static void timesub(const time_t * timep, long offset,
135 const struct state * sp, struct tm * tmp);
136 +#endif /* __LP64__ */
137 static int tmcomp(const struct tm * atmp,
138 const struct tm * btmp);
139 static time_t transtime(time_t janfirst, int year,
140 @@ -194,10 +265,15 @@
141 #endif /* !defined TZ_STRLEN_MAX */
142
143 static char lcl_TZname[TZ_STRLEN_MAX + 1];
144 +#ifdef NOTIFY_TZ
145 +#define lcl_is_set (lcl_notify.is_set)
146 +#define gmt_is_set (gmt_notify.is_set)
147 +#else /* ! NOTIFY_TZ */
148 static int lcl_is_set;
149 static int gmt_is_set;
150 -static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
151 -static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
152 +#endif /* NOTIFY_TZ */
153 +__private_extern__ pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
154 +static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
155
156 char * tzname[2] = {
157 wildabbr,
158 @@ -214,15 +290,62 @@
159
160 static struct tm tm;
161
162 +#define USG_COMPAT
163 +#define ALTZONE
164 #ifdef USG_COMPAT
165 -time_t timezone = 0;
166 int daylight = 0;
167 +__private_extern__ void _st_set_timezone(long);
168 #endif /* defined USG_COMPAT */
169
170 #ifdef ALTZONE
171 -time_t altzone = 0;
172 +__private_extern__ long __darwin_altzone = 0;
173 +#define altzone __darwin_altzone
174 #endif /* defined ALTZONE */
175
176 +#ifdef NOTIFY_TZ
177 +#ifdef NOTIFY_TZ_DEBUG
178 +#ifdef NOTIFY_TZ_DEBUG_FILE
179 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
180 +{ \
181 + FILE *_notify_tz_fp_; \
182 + if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
183 + fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
184 + fclose(_notify_tz_fp_); \
185 + } \
186 +}
187 +#else /* ! NOTIFY_TZ_DEBUG_FILE */
188 +#define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
189 +#endif /* NOTIFY_TZ_DEBUG_FILE */
190 +#endif /* NOTIFY_TZ_DEBUG */
191 +#ifdef NOTIFY_TZ_LOG
192 +#define NOTIFY_LOG(fmt, args...) \
193 +{ \
194 + FILE *_notify_log_fp_; \
195 + if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
196 + fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
197 + fclose(_notify_log_fp_); \
198 + } \
199 +}
200 +#endif /* NOTIFY_TZ_LOG */
201 +/*--------------------------------------------------------------------
202 + * __notify_78945668_info__ is a global variable (defined in Libnotify)
203 + * that can be used to disable the notify mechanism. Set to a negative
204 + * value to disable. It can then be set back to zero to re-enable.
205 + *-------------------------------------------------------------------- */
206 +extern int __notify_78945668_info__;
207 +
208 +/*--------------------------------------------------------------------
209 + * fullname is used to pass the actual path of the timezone file to the
210 + * notify routines. If it is a nil string, that means no timezone file
211 + * is being used.
212 + *-------------------------------------------------------------------- */
213 +static char * fullname = NULL;
214 +
215 +static notify_tz_t gmt_notify = {-1, 0, 0};
216 +static notify_tz_t lcl_notify = {-1, 0, 0};
217 +static char notify_tz_name[] = NOTIFY_TZ_NAME;
218 +#endif /* NOTIFY_TZ */
219 +
220 static long
221 detzcode(codep)
222 const char * const codep;
223 @@ -246,14 +369,14 @@
224 tzname[1] = wildabbr;
225 #ifdef USG_COMPAT
226 daylight = 0;
227 - timezone = 0;
228 + _st_set_timezone(0);
229 #endif /* defined USG_COMPAT */
230 #ifdef ALTZONE
231 altzone = 0;
232 #endif /* defined ALTZONE */
233 #ifdef ALL_STATE
234 if (sp == NULL) {
235 - tzname[0] = tzname[1] = gmt;
236 + tzname[0] = tzname[1] = (char *)gmt;
237 return;
238 }
239 #endif /* defined ALL_STATE */
240 @@ -266,7 +389,7 @@
241 if (ttisp->tt_isdst)
242 daylight = 1;
243 if (i == 0 || !ttisp->tt_isdst)
244 - timezone = -(ttisp->tt_gmtoff);
245 + _st_set_timezone(-(ttisp->tt_gmtoff));
246 #endif /* defined USG_COMPAT */
247 #ifdef ALTZONE
248 if (i == 0 || ttisp->tt_isdst)
249 @@ -286,6 +409,119 @@
250 }
251 }
252
253 +#ifdef NOTIFY_TZ
254 +static void
255 +notify_check_tz(notify_tz_t *p)
256 +{
257 + unsigned int nstat;
258 + int ncheck;
259 +
260 + if (__notify_78945668_info__ < 0) {
261 +#ifdef NOTIFY_TZ_DEBUG
262 + if(!p->notify_was_off) NOTIFY_TZ_PRINTF("notify_check_tz: setting %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
263 +#endif /* NOTIFY_TZ_DEBUG */
264 + p->notify_was_off = 1;
265 + return;
266 + }
267 + /* force rereading the timezone file if notify was off */
268 + if (p->notify_was_off) {
269 +#ifdef NOTIFY_TZ_DEBUG
270 + NOTIFY_TZ_PRINTF("notify_check_tz: saw %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
271 +#endif /* NOTIFY_TZ_DEBUG */
272 + p->is_set = 0;
273 + p->notify_was_off = 0;
274 + return;
275 + }
276 + if (p->token < 0)
277 + return;
278 + nstat = notify_check(p->token, &ncheck);
279 + if (nstat || ncheck) {
280 + p->is_set = 0;
281 +#ifdef NOTIFY_TZ_DEBUG
282 + NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p == &lcl_notify) ? "lcl" : "gmt");
283 +#endif /* NOTIFY_TZ_DEBUG */
284 + }
285 +#ifdef NOTIFY_TZ_DEBUG
286 + NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p == &lcl_notify) ? "lcl" : "gmt");
287 +#endif /* NOTIFY_TZ_DEBUG */
288 +}
289 +
290 +extern uint32_t notify_monitor_file(int token, char *path, int flags);
291 +
292 +static void
293 +notify_register_tz(char *file, notify_tz_t *p)
294 +{
295 + char *name;
296 + unsigned int nstat;
297 + int ncheck;
298 +
299 + if (__notify_78945668_info__ < 0)
300 + return;
301 + /*----------------------------------------------------------------
302 + * Since we don't record the last time zone filename, just cancel
303 + * (which should remove the file monitor) and setup from scratch
304 + *----------------------------------------------------------------*/
305 + if (p->token >= 0)
306 + notify_cancel(p->token);
307 + if (!file || *file == 0) {
308 + /* no time zone file to monitor */
309 + p->token = -1;
310 + return;
311 + }
312 + /*----------------------------------------------------------------
313 + * Just use com.apple.system.timezone if the path is /etc/localtime.
314 + * Otherwise use com.apple.system.timezone.<fullpath>
315 + *----------------------------------------------------------------*/
316 + if (TZDEFAULT && strcmp(file, TZDEFAULT) == 0)
317 + name = notify_tz_name;
318 + else {
319 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
320 + if (name == NULL) {
321 + p->token = -1;
322 + return;
323 + }
324 + strcpy(name, notify_tz_name);
325 + strcat(name, ".");
326 + strcat(name, file);
327 + }
328 +#ifdef NOTIFY_TZ_DEBUG
329 + NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file, name);
330 +#endif /* NOTIFY_TZ_DEBUG */
331 + nstat = notify_register_check(name, &p->token);
332 + if (nstat != 0) {
333 + p->token = -1;
334 + p->is_set = 0;
335 +#ifdef NOTIFY_TZ_DEBUG
336 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat);
337 +#endif /* NOTIFY_TZ_DEBUG */
338 +#ifdef NOTIFY_TZ_LOG
339 + NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name, nstat);
340 +#endif /* NOTIFY_TZ_LOG */
341 + return;
342 + }
343 + /* don't need to request monitoring /etc/localtime */
344 + if (name != notify_tz_name) {
345 +#ifdef NOTIFY_TZ_DEBUG
346 + NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file);
347 +#endif /* NOTIFY_TZ_DEBUG */
348 + nstat = notify_monitor_file(p->token, file, 0);
349 + if (nstat != 0) {
350 + notify_cancel(p->token);
351 + p->token = -1;
352 + p->is_set = 0;
353 +#ifdef NOTIFY_TZ_DEBUG
354 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat);
355 +#endif /* NOTIFY_TZ_DEBUG */
356 +#ifdef NOTIFY_TZ_LOG
357 + NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file, nstat);
358 +#endif /* NOTIFY_TZ_LOG */
359 + return;
360 + }
361 + }
362 + notify_check(p->token, &ncheck); /* this always returns true */
363 +}
364 +#endif /* NOTIFY_TZ */
365 +
366 static int
367 tzload(name, sp)
368 const char * name;
369 @@ -295,6 +531,9 @@
370 int i;
371 int fid;
372
373 +#ifdef NOTIFY_TZ_DEBUG
374 + NOTIFY_TZ_PRINTF("tzload: name=%s\n", name);
375 +#endif /* NOTIFY_TZ_DEBUG */
376 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
377 if (name != NULL && issetugid() != 0)
378 if ((name[0] == ':' && name[1] == '/') ||
379 @@ -312,7 +551,15 @@
380 ** to hold the longest file name string that the implementation
381 ** guarantees can be opened."
382 */
383 +#ifdef NOTIFY_TZ
384 + if (!fullname) {
385 + fullname = malloc(FILENAME_MAX + 1);
386 + if (!fullname)
387 + return -1;
388 + }
389 +#else /* ! NOTIFY_TZ */
390 char fullname[FILENAME_MAX + 1];
391 +#endif /* NOTIFY_TZ */
392
393 if (name[0] == ':')
394 ++name;
395 @@ -320,7 +567,11 @@
396 if (!doaccess) {
397 if ((p = TZDIR) == NULL)
398 return -1;
399 +#ifdef NOTIFY_TZ
400 + if ((strlen(p) + 1 + strlen(name) + 1) >= (FILENAME_MAX + 1))
401 +#else /* ! NOTIFY_TZ */
402 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
403 +#endif /* NOTIFY_TZ */
404 return -1;
405 (void) strcpy(fullname, p);
406 (void) strcat(fullname, "/");
407 @@ -332,6 +583,10 @@
408 doaccess = TRUE;
409 name = fullname;
410 }
411 +#ifdef NOTIFY_TZ
412 + else
413 + strcpy(fullname, name);
414 +#endif /* NOTIFY_TZ */
415 if (doaccess && access(name, R_OK) != 0)
416 return -1;
417 if ((fid = _open(name, OPEN_MODE)) == -1)
418 @@ -350,6 +605,9 @@
419 int ttisstdcnt;
420 int ttisgmtcnt;
421
422 +#ifdef NOTIFY_TZ_DEBUG
423 + NOTIFY_TZ_PRINTF("tzload: reading %s\n", name);
424 +#endif /* NOTIFY_TZ_DEBUG */
425 i = _read(fid, u.buf, sizeof u.buf);
426 if (_close(fid) != 0)
427 return -1;
428 @@ -456,14 +714,24 @@
429 */
430
431 static const char *
432 -getzname(strp)
433 +getzname(strp, name, len)
434 const char * strp;
435 +char ** name;
436 +size_t * len;
437 {
438 char c;
439 + char * ket;
440
441 + if (*strp == '<' && (ket = strchr(strp, '>')) != NULL) {
442 + *name = (char *)(strp + 1);
443 + *len = ket - strp - 1;
444 + return ket + 1;
445 + }
446 + *name = (char *)strp;
447 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
448 c != '+')
449 ++strp;
450 + *len = strp - *name;
451 return strp;
452 }
453
454 @@ -743,16 +1011,15 @@
455 int load_result;
456
457 INITIALIZE(dstname);
458 - stdname = name;
459 if (lastditch) {
460 + stdname = name;
461 stdlen = strlen(name); /* length of standard zone name */
462 name += stdlen;
463 if (stdlen >= sizeof sp->chars)
464 stdlen = (sizeof sp->chars) - 1;
465 stdoffset = 0;
466 } else {
467 - name = getzname(name);
468 - stdlen = name - stdname;
469 + name = getzname(name, (char **)&stdname, &stdlen);
470 if (stdlen < 3)
471 return -1;
472 if (*name == '\0')
473 @@ -764,12 +1031,14 @@
474 }
475 }
476 load_result = tzload(TZDEFRULES, sp);
477 +#ifdef NOTIFY_TZ
478 + *fullname = 0; /* mark fullname as invalid */
479 +#endif /* NOTIFY_TZ */
480 if (load_result != 0)
481 sp->leapcnt = 0; /* so, we're off a little */
482 if (*name != '\0') {
483 dstname = name;
484 - name = getzname(name);
485 - dstlen = name - dstname; /* length of DST zone name */
486 + name = getzname(name, (char **)&dstname, &dstlen);
487 if (dstlen < 3)
488 return -1;
489 if (*name != '\0' && *name != ',' && *name != ';') {
490 @@ -951,8 +1220,19 @@
491 static void
492 tzsetwall_basic(void)
493 {
494 +#ifdef NOTIFY_TZ
495 + notify_check_tz(&lcl_notify);
496 +#endif /* NOTIFY_TZ */
497 +#ifdef NOTIFY_TZ_DEBUG
498 + if (lcl_is_set < 0) {
499 + NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
500 + return;
501 + }
502 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
503 +#else /* ! NOTIFY_TZ_DEBUG */
504 if (lcl_is_set < 0)
505 return;
506 +#endif /* NOTIFY_TZ_DEBUG */
507 lcl_is_set = -1;
508
509 #ifdef ALL_STATE
510 @@ -966,18 +1246,24 @@
511 #endif /* defined ALL_STATE */
512 if (tzload((char *) NULL, lclptr) != 0)
513 gmtload(lclptr);
514 +#ifdef NOTIFY_TZ
515 + notify_register_tz(fullname, &lcl_notify);
516 +#endif /* NOTIFY_TZ */
517 settzname();
518 }
519
520 void
521 tzsetwall(void)
522 {
523 +#ifdef NOTIFY_TZ_DEBUG
524 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
525 +#endif /* NOTIFY_TZ_DEBUG */
526 _MUTEX_LOCK(&lcl_mutex);
527 tzsetwall_basic();
528 _MUTEX_UNLOCK(&lcl_mutex);
529 }
530
531 -static void
532 +__private_extern__ void
533 tzset_basic(void)
534 {
535 const char * name;
536 @@ -988,8 +1274,18 @@
537 return;
538 }
539
540 +#ifdef NOTIFY_TZ
541 + notify_check_tz(&lcl_notify);
542 +#endif /* NOTIFY_TZ */
543 +#ifdef NOTIFY_TZ_DEBUG
544 + if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
545 + NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname);
546 + return;
547 + }
548 +#else /* ! NOTIFY_TZ_DEBUG */
549 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
550 return;
551 +#endif /* NOTIFY_TZ_DEBUG */
552 lcl_is_set = strlen(name) < sizeof lcl_TZname;
553 if (lcl_is_set)
554 (void) strcpy(lcl_TZname, name);
555 @@ -1014,15 +1310,25 @@
556 lclptr->ttis[0].tt_gmtoff = 0;
557 lclptr->ttis[0].tt_abbrind = 0;
558 (void) strcpy(lclptr->chars, gmt);
559 +#ifdef NOTIFY_TZ
560 + if (fullname)
561 + *fullname = 0;
562 +#endif /* NOTIFY_TZ */
563 } else if (tzload(name, lclptr) != 0)
564 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
565 (void) gmtload(lclptr);
566 +#ifdef NOTIFY_TZ
567 + notify_register_tz(fullname, &lcl_notify);
568 +#endif /* NOTIFY_TZ */
569 settzname();
570 }
571
572 void
573 tzset(void)
574 {
575 +#ifdef NOTIFY_TZ_DEBUG
576 + NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
577 +#endif /* NOTIFY_TZ_DEBUG */
578 _MUTEX_LOCK(&lcl_mutex);
579 tzset_basic();
580 _MUTEX_UNLOCK(&lcl_mutex);
581 @@ -1038,7 +1344,11 @@
582 */
583
584 /*ARGSUSED*/
585 -static void
586 +#ifdef __LP64__
587 +__private_extern__ struct tm *
588 +#else /* !__LP64__ */
589 +__private_extern__ void
590 +#endif /* __LP64__ */
591 localsub(timep, offset, tmp)
592 const time_t * const timep;
593 const long offset;
594 @@ -1049,11 +1359,18 @@
595 int i;
596 const time_t t = *timep;
597
598 +#ifdef NOTIFY_TZ_DEBUG
599 + NOTIFY_TZ_PRINTF("localsub called\n");
600 +#endif /* NOTIFY_TZ_DEBUG */
601 sp = lclptr;
602 #ifdef ALL_STATE
603 if (sp == NULL) {
604 +#ifdef __LP64__
605 + return gmtsub(timep, offset, tmp);
606 +#else /* !__LP64__ */
607 gmtsub(timep, offset, tmp);
608 return;
609 +#endif /* __LP64__ */
610 }
611 #endif /* defined ALL_STATE */
612 if (sp->timecnt == 0 || t < sp->ats[0]) {
613 @@ -1076,12 +1393,20 @@
614 ** t += ttisp->tt_gmtoff;
615 ** timesub(&t, 0L, sp, tmp);
616 */
617 +#ifdef __LP64__
618 + if (timesub(&t, ttisp->tt_gmtoff, sp, tmp) == NULL)
619 + return NULL;
620 +#else /* !__LP64__ */
621 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
622 +#endif /* __LP64__ */
623 tmp->tm_isdst = ttisp->tt_isdst;
624 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
625 #ifdef TM_ZONE
626 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
627 #endif /* defined TM_ZONE */
628 +#ifdef __LP64__
629 + return tmp;
630 +#endif /* __LP64__ */
631 }
632
633 struct tm *
634 @@ -1094,8 +1419,9 @@
635
636 if (__isthreaded != 0) {
637 _pthread_mutex_lock(&localtime_mutex);
638 - if (localtime_key < 0) {
639 - if (_pthread_key_create(&localtime_key, free) < 0) {
640 + if (localtime_key == (pthread_key_t)-1) {
641 + localtime_key = __pthread_tsd_first + 2;
642 + if (pthread_key_init_np(localtime_key, free) < 0) {
643 _pthread_mutex_unlock(&localtime_mutex);
644 return(NULL);
645 }
646 @@ -1110,13 +1436,21 @@
647 }
648 _pthread_mutex_lock(&lcl_mutex);
649 tzset_basic();
650 +#ifdef __LP64__
651 + p_tm = localsub(timep, 0L, p_tm);
652 +#else /* !__LP64__ */
653 localsub(timep, 0L, p_tm);
654 +#endif /* __LP64__ */
655 _pthread_mutex_unlock(&lcl_mutex);
656 return(p_tm);
657 } else {
658 tzset_basic();
659 +#ifdef __LP64__
660 + return localsub(timep, 0L, &tm);
661 +#else /* !__LP64__ */
662 localsub(timep, 0L, &tm);
663 return(&tm);
664 +#endif /* __LP64__ */
665 }
666 }
667
668 @@ -1125,13 +1459,15 @@
669 */
670
671 struct tm *
672 -localtime_r(timep, tm)
673 -const time_t * const timep;
674 -struct tm * tm;
675 +localtime_r(const time_t * const __restrict timep, struct tm * __restrict tm)
676 {
677 _MUTEX_LOCK(&lcl_mutex);
678 tzset_basic();
679 +#ifdef __LP64__
680 + tm = localsub(timep, 0L, tm);
681 +#else /* !__LP64__ */
682 localsub(timep, 0L, tm);
683 +#endif /* __LP64__ */
684 _MUTEX_UNLOCK(&lcl_mutex);
685 return tm;
686 }
687 @@ -1140,23 +1476,48 @@
688 ** gmtsub is to gmtime as localsub is to localtime.
689 */
690
691 +#ifdef __LP64__
692 +static struct tm *
693 +#else /* !__LP64__ */
694 static void
695 +#endif /* __LP64__ */
696 gmtsub(timep, offset, tmp)
697 const time_t * const timep;
698 const long offset;
699 struct tm * const tmp;
700 {
701 +#ifdef NOTIFY_TZ_DEBUG
702 + NOTIFY_TZ_PRINTF("gmtsub called\n");
703 +#endif /* NOTIFY_TZ_DEBUG */
704 _MUTEX_LOCK(&gmt_mutex);
705 +#ifdef NOTIFY_TZ
706 + notify_check_tz(&gmt_notify);
707 +#endif /* NOTIFY_TZ */
708 if (!gmt_is_set) {
709 gmt_is_set = TRUE;
710 #ifdef ALL_STATE
711 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
712 +#ifdef NOTIFY_TZ
713 + if (gmtptr == NULL)
714 +#endif /* NOTIFY_TZ */
715 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
716 if (gmtptr != NULL)
717 +#ifdef NOTIFY_TZ
718 + {
719 +#endif /* NOTIFY_TZ */
720 #endif /* defined ALL_STATE */
721 gmtload(gmtptr);
722 +#ifdef NOTIFY_TZ
723 + notify_register_tz(fullname, &gmt_notify);
724 + }
725 +#endif /* NOTIFY_TZ */
726 }
727 _MUTEX_UNLOCK(&gmt_mutex);
728 +#ifdef __LP64__
729 + if(timesub(timep, offset, gmtptr, tmp) == NULL)
730 + return NULL;
731 +#else /* !__LP64__ */
732 timesub(timep, offset, gmtptr, tmp);
733 +#endif /* __LP64__ */
734 #ifdef TM_ZONE
735 /*
736 ** Could get fancy here and deliver something such as
737 @@ -1168,7 +1529,7 @@
738 else {
739 #ifdef ALL_STATE
740 if (gmtptr == NULL)
741 - tmp->TM_ZONE = gmt;
742 + tmp->TM_ZONE = (char *)gmt;
743 else tmp->TM_ZONE = gmtptr->chars;
744 #endif /* defined ALL_STATE */
745 #ifndef ALL_STATE
746 @@ -1176,6 +1537,9 @@
747 #endif /* State Farm */
748 }
749 #endif /* defined TM_ZONE */
750 +#ifdef __LP64__
751 + return tmp;
752 +#endif /* __LP64__ */
753 }
754
755 struct tm *
756 @@ -1186,10 +1550,12 @@
757 static pthread_key_t gmtime_key = -1;
758 struct tm *p_tm;
759
760 +
761 if (__isthreaded != 0) {
762 _pthread_mutex_lock(&gmtime_mutex);
763 - if (gmtime_key < 0) {
764 - if (_pthread_key_create(&gmtime_key, free) < 0) {
765 + if (gmtime_key == (pthread_key_t)-1) {
766 + gmtime_key = __pthread_tsd_first + 3;
767 + if (pthread_key_init_np(gmtime_key, free) < 0) {
768 _pthread_mutex_unlock(&gmtime_mutex);
769 return(NULL);
770 }
771 @@ -1206,12 +1572,20 @@
772 }
773 _pthread_setspecific(gmtime_key, p_tm);
774 }
775 +#ifdef __LP64__
776 + return gmtsub(timep, 0L, p_tm);
777 +#else /* !__LP64__ */
778 gmtsub(timep, 0L, p_tm);
779 return(p_tm);
780 +#endif /* __LP64__ */
781 }
782 else {
783 +#ifdef __LP64__
784 + return gmtsub(timep, 0L, &tm);
785 +#else /* !__LP64__ */
786 gmtsub(timep, 0L, &tm);
787 return(&tm);
788 +#endif /* __LP64__ */
789 }
790 }
791
792 @@ -1224,8 +1598,13 @@
793 const time_t * const timep;
794 struct tm * tm;
795 {
796 +
797 +#ifdef __LP64__
798 + return gmtsub(timep, 0L, tm);
799 +#else /* !__LP64__ */
800 gmtsub(timep, 0L, tm);
801 return tm;
802 +#endif /* __LP64__ */
803 }
804
805 #ifdef STD_INSPIRED
806 @@ -1235,13 +1614,21 @@
807 const time_t * const timep;
808 const long offset;
809 {
810 +#ifdef __LP64__
811 + return gmtsub(timep, offset, &tm);
812 +#else /* !__LP64__ */
813 gmtsub(timep, offset, &tm);
814 return &tm;
815 +#endif /* __LP64__ */
816 }
817
818 #endif /* defined STD_INSPIRED */
819
820 +#ifdef __LP64__
821 +static struct tm *
822 +#else /* !__LP64__ */
823 static void
824 +#endif /* __LP64__ */
825 timesub(timep, offset, sp, tmp)
826 const time_t * const timep;
827 const long offset;
828 @@ -1330,7 +1717,16 @@
829 LEAPS_THRU_END_OF(y - 1);
830 y = newy;
831 }
832 +#ifdef __LP64__
833 + y -= TM_YEAR_BASE;
834 + if (y < INT_MIN || y > INT_MAX) {
835 + errno = EOVERFLOW;
836 + return NULL;
837 + }
838 + tmp->tm_year = y;
839 +#else /* !__LP64__ */
840 tmp->tm_year = y - TM_YEAR_BASE;
841 +#endif /* __LP64__ */
842 tmp->tm_yday = (int) days;
843 ip = mon_lengths[yleap];
844 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
845 @@ -1340,6 +1736,9 @@
846 #ifdef TM_GMTOFF
847 tmp->TM_GMTOFF = offset;
848 #endif /* defined TM_GMTOFF */
849 +#ifdef __LP64__
850 + return tmp;
851 +#endif /* __LP64__ */
852 }
853
854 char *
855 @@ -1427,12 +1826,17 @@
856 }
857
858 static time_t
859 -time2sub(tmp, funcp, offset, okayp, do_norm_secs)
860 +time2sub(tmp, funcp, offset, okayp, do_norm_secs, unix03)
861 struct tm * const tmp;
862 +#ifdef __LP64__
863 +struct tm *(* const funcp)(const time_t*, long, struct tm*);
864 +#else /* !__LP64__ */
865 void (* const funcp)(const time_t*, long, struct tm*);
866 +#endif /* __LP64__ */
867 const long offset;
868 int * const okayp;
869 const int do_norm_secs;
870 +int unix03;
871 {
872 const struct state * sp;
873 int dir;
874 @@ -1442,6 +1846,9 @@
875 time_t newt;
876 time_t t;
877 struct tm yourtm, mytm;
878 +#ifdef __LP64__
879 + long year, il;
880 +#endif /* __LP64__ */
881
882 *okayp = FALSE;
883 yourtm = *tmp;
884 @@ -1460,33 +1867,64 @@
885 ** Turn yourtm.tm_year into an actual year number for now.
886 ** It is converted back to an offset from TM_YEAR_BASE later.
887 */
888 +#ifdef __LP64__
889 + year = (long)yourtm.tm_year + TM_YEAR_BASE;
890 +#else /* !__LP64__ */
891 if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
892 return WRONG;
893 +#endif /* __LP64__ */
894 while (yourtm.tm_mday <= 0) {
895 +#ifdef __LP64__
896 + year--;
897 + il = year + (1 < yourtm.tm_mon);
898 + yourtm.tm_mday += year_lengths[isleap(il)];
899 +#else /* !__LP64__ */
900 if (increment_overflow(&yourtm.tm_year, -1))
901 return WRONG;
902 i = yourtm.tm_year + (1 < yourtm.tm_mon);
903 yourtm.tm_mday += year_lengths[isleap(i)];
904 +#endif /* __LP64__ */
905 }
906 while (yourtm.tm_mday > DAYSPERLYEAR) {
907 +#ifdef __LP64__
908 + il = year + (1 < yourtm.tm_mon);
909 + yourtm.tm_mday -= year_lengths[isleap(il)];
910 + year++;
911 +#else /* !__LP64__ */
912 i = yourtm.tm_year + (1 < yourtm.tm_mon);
913 yourtm.tm_mday -= year_lengths[isleap(i)];
914 if (increment_overflow(&yourtm.tm_year, 1))
915 return WRONG;
916 +#endif /* __LP64__ */
917 }
918 for ( ; ; ) {
919 +#ifdef __LP64__
920 + i = mon_lengths[isleap(year)][yourtm.tm_mon];
921 +#else /* !__LP64__ */
922 i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
923 +#endif /* __LP64__ */
924 if (yourtm.tm_mday <= i)
925 break;
926 yourtm.tm_mday -= i;
927 if (++yourtm.tm_mon >= MONSPERYEAR) {
928 yourtm.tm_mon = 0;
929 +#ifdef __LP64__
930 + year++;
931 +#else /* !__LP64__ */
932 if (increment_overflow(&yourtm.tm_year, 1))
933 return WRONG;
934 +#endif /* __LP64__ */
935 }
936 }
937 +#ifdef __LP64__
938 + year -= TM_YEAR_BASE;
939 + if (year > INT_MAX || year < INT_MIN)
940 + return WRONG;
941 + yourtm.tm_year = year;
942 +#else /* !__LP64__ */
943 if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
944 return WRONG;
945 +#endif /* __LP64__ */
946 /* Don't go below 1900 for POLA */
947 if (yourtm.tm_year < 0)
948 return WRONG;
949 @@ -1527,8 +1965,19 @@
950 */
951 t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
952 for ( ; ; ) {
953 +#ifdef __LP64__
954 + if ((*funcp)(&t, offset, &mytm) == NULL) {
955 + /* we overflowed, so t is too big */
956 + dir = 1;
957 + goto skip_tmcomp;
958 + }
959 +#else /* !__LP64__ */
960 (*funcp)(&t, offset, &mytm);
961 +#endif /* __LP64__ */
962 dir = tmcomp(&mytm, &yourtm);
963 +#ifdef __LP64__
964 +skip_tmcomp:
965 +#endif /* __LP64__ */
966 if (dir != 0) {
967 if (bits-- < 0)
968 return WRONG;
969 @@ -1539,6 +1988,9 @@
970 else t += ((time_t) 1) << bits;
971 continue;
972 }
973 + sp = (funcp == localsub) ? lclptr : gmtptr;
974 + if (unix03 && sp->typecnt == 1 && yourtm.tm_isdst > 0)
975 + yourtm.tm_isdst = 0; /* alternative time does not apply */
976 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
977 break;
978 /*
979 @@ -1547,7 +1999,6 @@
980 ** It's okay to guess wrong since the guess
981 ** gets checked.
982 */
983 - sp = (funcp == localsub) ? lclptr : gmtptr;
984 #ifdef ALL_STATE
985 if (sp == NULL)
986 return WRONG;
987 @@ -1560,7 +2011,12 @@
988 continue;
989 newt = t + sp->ttis[j].tt_gmtoff -
990 sp->ttis[i].tt_gmtoff;
991 +#ifdef __LP64__
992 + if ((*funcp)(&newt, offset, &mytm) == NULL)
993 + return WRONG;
994 +#else /* !__LP64__ */
995 (*funcp)(&newt, offset, &mytm);
996 +#endif /* __LP64__ */
997 if (tmcomp(&mytm, &yourtm) != 0)
998 continue;
999 if (mytm.tm_isdst != yourtm.tm_isdst)
1000 @@ -1579,17 +2035,27 @@
1001 if ((newt < t) != (saved_seconds < 0))
1002 return WRONG;
1003 t = newt;
1004 +#ifdef __LP64__
1005 + if ((*funcp)(&t, offset, tmp) == NULL)
1006 + return WRONG;
1007 +#else /* !__LP64__ */
1008 (*funcp)(&t, offset, tmp);
1009 +#endif /* __LP64__ */
1010 *okayp = TRUE;
1011 return t;
1012 }
1013
1014 static time_t
1015 -time2(tmp, funcp, offset, okayp)
1016 +time2(tmp, funcp, offset, okayp, unix03)
1017 struct tm * const tmp;
1018 +#ifdef __LP64__
1019 +struct tm *(* const funcp)(const time_t*, long, struct tm*);
1020 +#else /* !__LP64__ */
1021 void (* const funcp)(const time_t*, long, struct tm*);
1022 +#endif /* __LP64__ */
1023 const long offset;
1024 int * const okayp;
1025 +int unix03;
1026 {
1027 time_t t;
1028
1029 @@ -1598,15 +2064,20 @@
1030 ** (in case tm_sec contains a value associated with a leap second).
1031 ** If that fails, try with normalization of seconds.
1032 */
1033 - t = time2sub(tmp, funcp, offset, okayp, FALSE);
1034 - return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1035 + t = time2sub(tmp, funcp, offset, okayp, FALSE, unix03);
1036 + return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, unix03);
1037 }
1038
1039 -static time_t
1040 -time1(tmp, funcp, offset)
1041 +__private_extern__ time_t
1042 +time1(tmp, funcp, offset, unix03)
1043 struct tm * const tmp;
1044 +#ifdef __LP64__
1045 +struct tm *(* const funcp)(const time_t *, long, struct tm *);
1046 +#else /* !__LP64__ */
1047 void (* const funcp)(const time_t *, long, struct tm *);
1048 +#endif /* __LP64__ */
1049 const long offset;
1050 +int unix03;
1051 {
1052 time_t t;
1053 const struct state * sp;
1054 @@ -1620,7 +2091,7 @@
1055
1056 if (tmp->tm_isdst > 1)
1057 tmp->tm_isdst = 1;
1058 - t = time2(tmp, funcp, offset, &okay);
1059 + t = time2(tmp, funcp, offset, &okay, unix03);
1060 #ifdef PCTS
1061 /*
1062 ** PCTS code courtesy Grant Sullivan (grant@osf.org).
1063 @@ -1664,7 +2135,7 @@
1064 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
1065 sp->ttis[samei].tt_gmtoff;
1066 tmp->tm_isdst = !tmp->tm_isdst;
1067 - t = time2(tmp, funcp, offset, &okay);
1068 + t = time2(tmp, funcp, offset, &okay, unix03);
1069 if (okay)
1070 return t;
1071 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
1072 @@ -1674,19 +2145,25 @@
1073 }
1074 return WRONG;
1075 }
1076 +#else /* BUILDING_VARIANT */
1077 +__private_extern__ pthread_mutex_t lcl_mutex;
1078 +#endif /* BUILDING_VARIANT */
1079
1080 time_t
1081 mktime(tmp)
1082 struct tm * const tmp;
1083 {
1084 time_t mktime_return_value;
1085 + int serrno = errno;
1086 _MUTEX_LOCK(&lcl_mutex);
1087 tzset_basic();
1088 - mktime_return_value = time1(tmp, localsub, 0L);
1089 + mktime_return_value = time1(tmp, localsub, 0L, __DARWIN_UNIX03);
1090 _MUTEX_UNLOCK(&lcl_mutex);
1091 + errno = serrno;
1092 return(mktime_return_value);
1093 }
1094
1095 +#if !BUILDING_VARIANT
1096 #ifdef STD_INSPIRED
1097
1098 time_t
1099 @@ -1702,7 +2179,7 @@
1100 struct tm * const tmp;
1101 {
1102 tmp->tm_isdst = 0;
1103 - return time1(tmp, gmtsub, 0L);
1104 + return time1(tmp, gmtsub, 0L, __DARWIN_UNIX03);
1105 }
1106
1107 time_t
1108 @@ -1711,7 +2188,7 @@
1109 const long offset;
1110 {
1111 tmp->tm_isdst = 0;
1112 - return time1(tmp, gmtsub, offset);
1113 + return time1(tmp, gmtsub, offset, __DARWIN_UNIX03);
1114 }
1115
1116 #endif /* defined STD_INSPIRED */
1117 @@ -1811,3 +2288,4 @@
1118 }
1119
1120 #endif /* defined STD_INSPIRED */
1121 +#endif /* !BUILDING_VARIANT */