strftime.c revision 1.22.2.3 1 /* $NetBSD: strftime.c,v 1.22.2.3 2014/08/20 00:02:16 tls Exp $ */
2
3 #include <sys/cdefs.h>
4 #if defined(LIBC_SCCS) && !defined(lint)
5 #if 0
6 static char elsieid[] = "@(#)strftime.c 7.64";
7 static char elsieid[] = "@(#)strftime.c 8.3";
8 #else
9 __RCSID("$NetBSD: strftime.c,v 1.22.2.3 2014/08/20 00:02:16 tls Exp $");
10 #endif
11 #endif /* LIBC_SCCS and not lint */
12
13 #include "namespace.h"
14
15 #include <stddef.h>
16 #include <assert.h>
17 #include <locale.h>
18 #include "setlocale_local.h"
19
20 /*
21 ** Based on the UCB version with the copyright notice and sccsid
22 ** appearing below.
23 **
24 ** This is ANSIish only when "multibyte character == plain character".
25 */
26
27 #include "private.h"
28
29 /*
30 ** We don't use these extensions in strftime operation even when
31 ** supported by the local tzcode configuration. A strictly
32 ** conforming C application may leave them in undefined state.
33 */
34
35 #ifdef _LIBC
36 #undef TM_ZONE
37 #undef TM_GMTOFF
38 #endif
39
40 /*
41 ** Copyright (c) 1989, 1993
42 ** The Regents of the University of California. All rights reserved.
43 **
44 ** Redistribution and use in source and binary forms, with or without
45 ** modification, are permitted provided that the following conditions
46 ** are met:
47 ** 1. Redistributions of source code must retain the above copyright
48 ** notice, this list of conditions and the following disclaimer.
49 ** 2. Redistributions in binary form must reproduce the above copyright
50 ** notice, this list of conditions and the following disclaimer in the
51 ** documentation and/or other materials provided with the distribution.
52 ** 3. All advertising materials mentioning features or use of this software
53 ** must display the following acknowledgement:
54 ** This product includes software developed by the University of
55 ** California, Berkeley and its contributors.
56 ** 4. Neither the name of the University nor the names of its contributors
57 ** may be used to endorse or promote products derived from this software
58 ** without specific prior written permission.
59 **
60 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 ** SUCH DAMAGE.
71 */
72
73 #ifndef LIBC_SCCS
74 #ifndef lint
75 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
76 #endif /* !defined lint */
77 #endif /* !defined LIBC_SCCS */
78
79 #include "tzfile.h"
80 #include "fcntl.h"
81 #include "locale.h"
82
83 #ifdef __weak_alias
84 __weak_alias(strftime_l, _strftime_l)
85 __weak_alias(strftime_lz, _strftime_lz)
86 __weak_alias(strftime_z, _strftime_z)
87 #endif
88
89 #include "sys/localedef.h"
90 #define _TIME_LOCALE(loc) \
91 ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
92 #define c_fmt d_t_fmt
93
94 static char * _add(const char *, char *, const char *);
95 static char * _conv(int, const char *, char *, const char *);
96 static char * _fmt(const timezone_t, const char *, const struct tm *, char *,
97 const char *, int *, locale_t);
98 static char * _yconv(int, int, int, int, char *, const char *);
99
100 extern char * tzname[];
101
102 #ifndef YEAR_2000_NAME
103 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
104 #endif /* !defined YEAR_2000_NAME */
105
106 #define IN_NONE 0
107 #define IN_SOME 1
108 #define IN_THIS 2
109 #define IN_ALL 3
110
111 size_t
112 strftime_z(const timezone_t sp, char * __restrict s, size_t maxsize,
113 const char * __restrict format, const struct tm * __restrict t)
114 {
115 return strftime_lz(sp, s, maxsize, format, t, _current_locale());
116 }
117
118 size_t
119 strftime_lz(const timezone_t sp, char *const s, const size_t maxsize,
120 const char *const format, const struct tm *const t, locale_t loc)
121 {
122 char * p;
123 int warn;
124
125 warn = IN_NONE;
126 p = _fmt(sp, ((format == NULL) ? "%c" : format), t, s, s + maxsize,
127 &warn, loc);
128 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
129 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
130 (void) fprintf(stderr, "\n");
131 if (format == NULL)
132 (void) fprintf(stderr, "NULL strftime format ");
133 else (void) fprintf(stderr, "strftime format \"%s\" ",
134 format);
135 (void) fprintf(stderr, "yields only two digits of years in ");
136 if (warn == IN_SOME)
137 (void) fprintf(stderr, "some locales");
138 else if (warn == IN_THIS)
139 (void) fprintf(stderr, "the current locale");
140 else (void) fprintf(stderr, "all locales");
141 (void) fprintf(stderr, "\n");
142 }
143 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
144 if (p == s + maxsize)
145 return 0;
146 *p = '\0';
147 return p - s;
148 }
149
150 static char *
151 _fmt(const timezone_t sp, const char *format, const struct tm *const t,
152 char *pt, const char *const ptlim, int *warnp, locale_t loc)
153 {
154 for ( ; *format; ++format) {
155 if (*format == '%') {
156 label:
157 switch (*++format) {
158 case '\0':
159 --format;
160 break;
161 case 'A':
162 pt = _add((t->tm_wday < 0 ||
163 t->tm_wday >= DAYSPERWEEK) ?
164 "?" : _TIME_LOCALE(loc)->day[t->tm_wday],
165 pt, ptlim);
166 continue;
167 case 'a':
168 pt = _add((t->tm_wday < 0 ||
169 t->tm_wday >= DAYSPERWEEK) ?
170 "?" : _TIME_LOCALE(loc)->abday[t->tm_wday],
171 pt, ptlim);
172 continue;
173 case 'B':
174 pt = _add((t->tm_mon < 0 ||
175 t->tm_mon >= MONSPERYEAR) ?
176 "?" : _TIME_LOCALE(loc)->mon[t->tm_mon],
177 pt, ptlim);
178 continue;
179 case 'b':
180 case 'h':
181 pt = _add((t->tm_mon < 0 ||
182 t->tm_mon >= MONSPERYEAR) ?
183 "?" : _TIME_LOCALE(loc)->abmon[t->tm_mon],
184 pt, ptlim);
185 continue;
186 case 'C':
187 /*
188 ** %C used to do a...
189 ** _fmt("%a %b %e %X %Y", t);
190 ** ...whereas now POSIX 1003.2 calls for
191 ** something completely different.
192 ** (ado, 1993-05-24)
193 */
194 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
195 pt, ptlim);
196 continue;
197 case 'c':
198 {
199 int warn2 = IN_SOME;
200
201 pt = _fmt(sp, _TIME_LOCALE(loc)->c_fmt, t, pt,
202 ptlim, &warn2, loc);
203 if (warn2 == IN_ALL)
204 warn2 = IN_THIS;
205 if (warn2 > *warnp)
206 *warnp = warn2;
207 }
208 continue;
209 case 'D':
210 pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
211 loc);
212 continue;
213 case 'd':
214 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
215 continue;
216 case 'E':
217 case 'O':
218 /*
219 ** C99 locale modifiers.
220 ** The sequences
221 ** %Ec %EC %Ex %EX %Ey %EY
222 ** %Od %oe %OH %OI %Om %OM
223 ** %OS %Ou %OU %OV %Ow %OW %Oy
224 ** are supposed to provide alternate
225 ** representations.
226 */
227 goto label;
228 case 'e':
229 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
230 continue;
231 case 'F':
232 pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
233 loc);
234 continue;
235 case 'H':
236 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
237 continue;
238 case 'I':
239 pt = _conv((t->tm_hour % 12) ?
240 (t->tm_hour % 12) : 12,
241 "%02d", pt, ptlim);
242 continue;
243 case 'j':
244 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
245 continue;
246 case 'k':
247 /*
248 ** This used to be...
249 ** _conv(t->tm_hour % 12 ?
250 ** t->tm_hour % 12 : 12, 2, ' ');
251 ** ...and has been changed to the below to
252 ** match SunOS 4.1.1 and Arnold Robbins'
253 ** strftime version 3.0. That is, "%k" and
254 ** "%l" have been swapped.
255 ** (ado, 1993-05-24)
256 */
257 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
258 continue;
259 #ifdef KITCHEN_SINK
260 case 'K':
261 /*
262 ** After all this time, still unclaimed!
263 */
264 pt = _add("kitchen sink", pt, ptlim);
265 continue;
266 #endif /* defined KITCHEN_SINK */
267 case 'l':
268 /*
269 ** This used to be...
270 ** _conv(t->tm_hour, 2, ' ');
271 ** ...and has been changed to the below to
272 ** match SunOS 4.1.1 and Arnold Robbin's
273 ** strftime version 3.0. That is, "%k" and
274 ** "%l" have been swapped.
275 ** (ado, 1993-05-24)
276 */
277 pt = _conv((t->tm_hour % 12) ?
278 (t->tm_hour % 12) : 12,
279 "%2d", pt, ptlim);
280 continue;
281 case 'M':
282 pt = _conv(t->tm_min, "%02d", pt, ptlim);
283 continue;
284 case 'm':
285 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
286 continue;
287 case 'n':
288 pt = _add("\n", pt, ptlim);
289 continue;
290 case 'p':
291 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
292 _TIME_LOCALE(loc)->am_pm[1] :
293 _TIME_LOCALE(loc)->am_pm[0],
294 pt, ptlim);
295 continue;
296 case 'R':
297 pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
298 loc);
299 continue;
300 case 'r':
301 pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt_ampm, t,
302 pt, ptlim, warnp, loc);
303 continue;
304 case 'S':
305 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
306 continue;
307 case 's':
308 {
309 struct tm tm;
310 char buf[INT_STRLEN_MAXIMUM(
311 time_t) + 1];
312 time_t mkt;
313
314 tm = *t;
315 mkt = mktime(&tm);
316 /* CONSTCOND */
317 if (TYPE_SIGNED(time_t))
318 (void)snprintf(buf, sizeof(buf),
319 "%jd", (intmax_t) mkt);
320 else (void)snprintf(buf, sizeof(buf),
321 "%ju", (uintmax_t) mkt);
322 pt = _add(buf, pt, ptlim);
323 }
324 continue;
325 case 'T':
326 pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
327 loc);
328 continue;
329 case 't':
330 pt = _add("\t", pt, ptlim);
331 continue;
332 case 'U':
333 pt = _conv((t->tm_yday + DAYSPERWEEK -
334 t->tm_wday) / DAYSPERWEEK,
335 "%02d", pt, ptlim);
336 continue;
337 case 'u':
338 /*
339 ** From Arnold Robbins' strftime version 3.0:
340 ** "ISO 8601: Weekday as a decimal number
341 ** [1 (Monday) - 7]"
342 ** (ado, 1993-05-24)
343 */
344 pt = _conv((t->tm_wday == 0) ?
345 DAYSPERWEEK : t->tm_wday,
346 "%d", pt, ptlim);
347 continue;
348 case 'V': /* ISO 8601 week number */
349 case 'G': /* ISO 8601 year (four digits) */
350 case 'g': /* ISO 8601 year (two digits) */
351 /*
352 ** From Arnold Robbins' strftime version 3.0: "the week number of the
353 ** year (the first Monday as the first day of week 1) as a decimal number
354 ** (01-53)."
355 ** (ado, 1993-05-24)
356 **
357 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
358 ** "Week 01 of a year is per definition the first week which has the
359 ** Thursday in this year, which is equivalent to the week which contains
360 ** the fourth day of January. In other words, the first week of a new year
361 ** is the week which has the majority of its days in the new year. Week 01
362 ** might also contain days from the previous year and the week before week
363 ** 01 of a year is the last week (52 or 53) of the previous year even if
364 ** it contains days from the new year. A week starts with Monday (day 1)
365 ** and ends with Sunday (day 7). For example, the first week of the year
366 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
367 ** (ado, 1996-01-02)
368 */
369 {
370 int year;
371 int base;
372 int yday;
373 int wday;
374 int w;
375
376 year = t->tm_year;
377 base = TM_YEAR_BASE;
378 yday = t->tm_yday;
379 wday = t->tm_wday;
380 for ( ; ; ) {
381 int len;
382 int bot;
383 int top;
384
385 len = isleap_sum(year, base) ?
386 DAYSPERLYEAR :
387 DAYSPERNYEAR;
388 /*
389 ** What yday (-3 ... 3) does
390 ** the ISO year begin on?
391 */
392 bot = ((yday + 11 - wday) %
393 DAYSPERWEEK) - 3;
394 /*
395 ** What yday does the NEXT
396 ** ISO year begin on?
397 */
398 top = bot -
399 (len % DAYSPERWEEK);
400 if (top < -3)
401 top += DAYSPERWEEK;
402 top += len;
403 if (yday >= top) {
404 ++base;
405 w = 1;
406 break;
407 }
408 if (yday >= bot) {
409 w = 1 + ((yday - bot) /
410 DAYSPERWEEK);
411 break;
412 }
413 --base;
414 yday += isleap_sum(year, base) ?
415 DAYSPERLYEAR :
416 DAYSPERNYEAR;
417 }
418 #ifdef XPG4_1994_04_09
419 if ((w == 52 &&
420 t->tm_mon == TM_JANUARY) ||
421 (w == 1 &&
422 t->tm_mon == TM_DECEMBER))
423 w = 53;
424 #endif /* defined XPG4_1994_04_09 */
425 if (*format == 'V')
426 pt = _conv(w, "%02d",
427 pt, ptlim);
428 else if (*format == 'g') {
429 *warnp = IN_ALL;
430 pt = _yconv(year, base, 0, 1,
431 pt, ptlim);
432 } else pt = _yconv(year, base, 1, 1,
433 pt, ptlim);
434 }
435 continue;
436 case 'v':
437 /*
438 ** From Arnold Robbins' strftime version 3.0:
439 ** "date as dd-bbb-YYYY"
440 ** (ado, 1993-05-24)
441 */
442 pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
443 loc);
444 continue;
445 case 'W':
446 pt = _conv((t->tm_yday + DAYSPERWEEK -
447 (t->tm_wday ?
448 (t->tm_wday - 1) :
449 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
450 "%02d", pt, ptlim);
451 continue;
452 case 'w':
453 pt = _conv(t->tm_wday, "%d", pt, ptlim);
454 continue;
455 case 'X':
456 pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt, t, pt,
457 ptlim, warnp, loc);
458 continue;
459 case 'x':
460 {
461 int warn2 = IN_SOME;
462
463 pt = _fmt(sp, _TIME_LOCALE(loc)->d_fmt, t, pt,
464 ptlim, &warn2, loc);
465 if (warn2 == IN_ALL)
466 warn2 = IN_THIS;
467 if (warn2 > *warnp)
468 *warnp = warn2;
469 }
470 continue;
471 case 'y':
472 *warnp = IN_ALL;
473 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
474 pt, ptlim);
475 continue;
476 case 'Y':
477 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
478 pt, ptlim);
479 continue;
480 case 'Z':
481 #ifdef TM_ZONE
482 if (t->TM_ZONE != NULL)
483 pt = _add(t->TM_ZONE, pt, ptlim);
484 else
485 #endif /* defined TM_ZONE */
486 if (t->tm_isdst >= 0)
487 pt = _add((sp ?
488 tzgetname(sp, t->tm_isdst) :
489 tzname[t->tm_isdst != 0]),
490 pt, ptlim);
491 /*
492 ** C99 says that %Z must be replaced by the
493 ** empty string if the time zone is not
494 ** determinable.
495 */
496 continue;
497 case 'z':
498 {
499 long diff;
500 char const * sign;
501
502 if (t->tm_isdst < 0)
503 continue;
504 #ifdef TM_GMTOFF
505 diff = (int)t->TM_GMTOFF;
506 #else /* !defined TM_GMTOFF */
507 /*
508 ** C99 says that the UT offset must
509 ** be computed by looking only at
510 ** tm_isdst. This requirement is
511 ** incorrect, since it means the code
512 ** must rely on magic (in this case
513 ** altzone and timezone), and the
514 ** magic might not have the correct
515 ** offset. Doing things correctly is
516 ** tricky and requires disobeying C99;
517 ** see GNU C strftime for details.
518 ** For now, punt and conform to the
519 ** standard, even though it's incorrect.
520 **
521 ** C99 says that %z must be replaced by the
522 ** empty string if the time zone is not
523 ** determinable, so output nothing if the
524 ** appropriate variables are not available.
525 */
526 #ifndef STD_INSPIRED
527 if (t->tm_isdst == 0)
528 #ifdef USG_COMPAT
529 diff = -timezone;
530 #else /* !defined USG_COMPAT */
531 continue;
532 #endif /* !defined USG_COMPAT */
533 else
534 #ifdef ALTZONE
535 diff = -altzone;
536 #else /* !defined ALTZONE */
537 continue;
538 #endif /* !defined ALTZONE */
539 #else /* defined STD_INSPIRED */
540 {
541 struct tm tmp;
542 time_t lct, gct;
543
544 /*
545 ** Get calendar time from t
546 ** being treated as local.
547 */
548 tmp = *t; /* mktime discards const */
549 lct = mktime(&tmp);
550
551 if (lct == (time_t)-1)
552 continue;
553
554 /*
555 ** Get calendar time from t
556 ** being treated as GMT.
557 **/
558 tmp = *t; /* mktime discards const */
559 gct = timegm(&tmp);
560
561 if (gct == (time_t)-1)
562 continue;
563
564 /* LINTED difference will fit int */
565 diff = (intmax_t)gct - (intmax_t)lct;
566 }
567 #endif /* defined STD_INSPIRED */
568 #endif /* !defined TM_GMTOFF */
569 if (diff < 0) {
570 sign = "-";
571 diff = -diff;
572 } else sign = "+";
573 pt = _add(sign, pt, ptlim);
574 diff /= SECSPERMIN;
575 diff = (diff / MINSPERHOUR) * 100 +
576 (diff % MINSPERHOUR);
577 _DIAGASSERT(__type_fit(int, diff));
578 pt = _conv((int)diff, "%04d", pt, ptlim);
579 }
580 continue;
581 #if 0
582 case '+':
583 pt = _fmt(sp, _TIME_LOCALE(loc)->date_fmt, t,
584 pt, ptlim, warnp, loc);
585 continue;
586 #endif
587 case '%':
588 /*
589 ** X311J/88-090 (4.12.3.5): if conversion char is
590 ** undefined, behavior is undefined. Print out the
591 ** character itself as printf(3) also does.
592 */
593 default:
594 break;
595 }
596 }
597 if (pt == ptlim)
598 break;
599 *pt++ = *format;
600 }
601 return pt;
602 }
603
604 size_t
605 strftime(char * const s, const size_t maxsize,
606 const char * const format, const struct tm * const t)
607 {
608 tzset();
609 return strftime_z(NULL, s, maxsize, format, t);
610 }
611
612 size_t
613 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
614 const struct tm * __restrict t, locale_t loc)
615 {
616 tzset();
617 return strftime_lz(NULL, s, maxsize, format, t, loc);
618 }
619
620 static char *
621 _conv(const int n, const char *const format, char *const pt,
622 const char *const ptlim)
623 {
624 char buf[INT_STRLEN_MAXIMUM(int) + 1];
625
626 (void) snprintf(buf, sizeof(buf), format, n);
627 return _add(buf, pt, ptlim);
628 }
629
630 static char *
631 _add(const char *str, char *pt, const char *const ptlim)
632 {
633 while (pt < ptlim && (*pt = *str++) != '\0')
634 ++pt;
635 return pt;
636 }
637
638 /*
639 ** POSIX and the C Standard are unclear or inconsistent about
640 ** what %C and %y do if the year is negative or exceeds 9999.
641 ** Use the convention that %C concatenated with %y yields the
642 ** same output as %Y, and that %Y contains at least 4 bytes,
643 ** with more only if necessary.
644 */
645
646 static char *
647 _yconv(const int a, const int b, const int convert_top, const int convert_yy,
648 char *pt, const char *const ptlim)
649 {
650 int lead;
651 int trail;
652
653 #define DIVISOR 100
654 trail = a % DIVISOR + b % DIVISOR;
655 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
656 trail %= DIVISOR;
657 if (trail < 0 && lead > 0) {
658 trail += DIVISOR;
659 --lead;
660 } else if (lead < 0 && trail > 0) {
661 trail -= DIVISOR;
662 ++lead;
663 }
664 if (convert_top) {
665 if (lead == 0 && trail < 0)
666 pt = _add("-0", pt, ptlim);
667 else pt = _conv(lead, "%02d", pt, ptlim);
668 }
669 if (convert_yy)
670 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
671 return pt;
672 }
673
674 #ifdef LOCALE_HOME
675 static struct lc_time_T *
676 _loc(void)
677 {
678 static const char locale_home[] = LOCALE_HOME;
679 static const char lc_time[] = "LC_TIME";
680 static char * locale_buf;
681
682 int fd;
683 int oldsun; /* "...ain't got nothin' to do..." */
684 char * lbuf;
685 char * name;
686 char * p;
687 const char ** ap;
688 const char * plim;
689 char filename[FILENAME_MAX];
690 struct stat st;
691 size_t namesize;
692 size_t bufsize;
693
694 /*
695 ** Use localebuf.mon[0] to signal whether locale is already set up.
696 */
697 if (localebuf.mon[0])
698 return &localebuf;
699 name = setlocale(LC_TIME, NULL);
700 if (name == NULL || *name == '\0')
701 goto no_locale;
702 /*
703 ** If the locale name is the same as our cache, use the cache.
704 */
705 lbuf = locale_buf;
706 if (lbuf != NULL && strcmp(name, lbuf) == 0) {
707 p = lbuf;
708 for (ap = (const char **) &localebuf;
709 ap < (const char **) (&localebuf + 1);
710 ++ap)
711 *ap = p += strlen(p) + 1;
712 return &localebuf;
713 }
714 /*
715 ** Slurp the locale file into the cache.
716 */
717 namesize = strlen(name) + 1;
718 if (sizeof filename <
719 ((sizeof locale_home) + namesize + (sizeof lc_time)))
720 goto no_locale;
721 oldsun = 0;
722 (void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
723 fd = open(filename, O_RDONLY);
724 if (fd < 0) {
725 /*
726 ** Old Sun systems have a different naming and data convention.
727 */
728 oldsun = 1;
729 (void) sprintf(filename, "%s/%s/%s", locale_home,
730 lc_time, name);
731 fd = open(filename, O_RDONLY);
732 if (fd < 0)
733 goto no_locale;
734 }
735 if (fstat(fd, &st) != 0)
736 goto bad_locale;
737 if (st.st_size <= 0)
738 goto bad_locale;
739 bufsize = namesize + st.st_size;
740 locale_buf = NULL;
741 lbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
742 if (lbuf == NULL)
743 goto bad_locale;
744 (void) strcpy(lbuf, name);
745 p = lbuf + namesize;
746 plim = p + st.st_size;
747 if (read(fd, p, (size_t) st.st_size) != st.st_size)
748 goto bad_lbuf;
749 if (close(fd) != 0)
750 goto bad_lbuf;
751 /*
752 ** Parse the locale file into localebuf.
753 */
754 if (plim[-1] != '\n')
755 goto bad_lbuf;
756 for (ap = (const char **) &localebuf;
757 ap < (const char **) (&localebuf + 1);
758 ++ap) {
759 if (p == plim)
760 goto bad_lbuf;
761 *ap = p;
762 while (*p != '\n')
763 ++p;
764 *p++ = '\0';
765 }
766 if (oldsun) {
767 /*
768 ** SunOS 4 used an obsolescent format; see localdtconv(3).
769 ** c_fmt had the ``short format for dates and times together''
770 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
771 ** date_fmt had the ``long format for dates''
772 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
773 ** Discard the latter in favor of the former.
774 */
775 localebuf.date_fmt = localebuf.c_fmt;
776 }
777 /*
778 ** Record the successful parse in the cache.
779 */
780 locale_buf = lbuf;
781
782 return &localebuf;
783
784 bad_lbuf:
785 free(lbuf);
786 bad_locale:
787 (void) close(fd);
788 no_locale:
789 localebuf = C_time_locale;
790 locale_buf = NULL;
791 return &localebuf;
792 }
793 #endif /* defined LOCALE_HOME */
794