asctime.c revision 1.26 1 1.26 pgoyette /* $NetBSD: asctime.c,v 1.26 2019/01/27 02:08:35 pgoyette Exp $ */
2 1.26 pgoyette
3 1.26 pgoyette /* asctime and asctime_r a la POSIX and ISO C, except pad years before 1000. */
4 1.25 christos
5 1.25 christos /* asctime and asctime_r a la POSIX and ISO C, except pad years before 1000. */
6 1.3 jtc
7 1.3 jtc /*
8 1.3 jtc ** This file is in the public domain, so clarified as of
9 1.13 mlelstv ** 1996-06-05 by Arthur David Olson.
10 1.13 mlelstv */
11 1.13 mlelstv
12 1.13 mlelstv /*
13 1.13 mlelstv ** Avoid the temptation to punt entirely to strftime;
14 1.13 mlelstv ** the output of strftime is supposed to be locale specific
15 1.13 mlelstv ** whereas the output of asctime is supposed to be constant.
16 1.3 jtc */
17 1.2 jtc
18 1.6 christos #include <sys/cdefs.h>
19 1.11 msaitoh #if defined(LIBC_SCCS) && !defined(lint)
20 1.6 christos #if 0
21 1.14 christos static char elsieid[] = "@(#)asctime.c 8.5";
22 1.6 christos #else
23 1.26 pgoyette __RCSID("$NetBSD: asctime.c,v 1.26 2019/01/27 02:08:35 pgoyette Exp $");
24 1.6 christos #endif
25 1.11 msaitoh #endif /* LIBC_SCCS and not lint */
26 1.1 jtc
27 1.1 jtc /*LINTLIBRARY*/
28 1.1 jtc
29 1.9 kleink #include "namespace.h"
30 1.1 jtc #include "private.h"
31 1.22 christos #include <stdio.h>
32 1.1 jtc
33 1.7 kleink #ifdef __weak_alias
34 1.10 mycroft __weak_alias(asctime_r,_asctime_r)
35 1.7 kleink #endif
36 1.7 kleink
37 1.7 kleink /*
38 1.13 mlelstv ** Some systems only handle "%.2d"; others only handle "%02d";
39 1.13 mlelstv ** "%02.2d" makes (most) everybody happy.
40 1.13 mlelstv ** At least some versions of gcc warn about the %02.2d;
41 1.13 mlelstv ** we conditionalize below to avoid the warning.
42 1.13 mlelstv */
43 1.13 mlelstv /*
44 1.13 mlelstv ** All years associated with 32-bit time_t values are exactly four digits long;
45 1.13 mlelstv ** some years associated with 64-bit time_t values are not.
46 1.13 mlelstv ** Vintage programs are coded for years that are always four digits long
47 1.13 mlelstv ** and may assume that the newline always lands in the same place.
48 1.13 mlelstv ** For years that are less than four digits, we pad the output with
49 1.13 mlelstv ** leading zeroes to get the newline in the traditional place.
50 1.13 mlelstv ** The -4 ensures that we get four characters of output even if
51 1.13 mlelstv ** we call a strftime variant that produces fewer characters for some years.
52 1.25 christos ** The ISO C and POSIX standards prohibit padding the year,
53 1.13 mlelstv ** but many implementations pad anyway; most likely the standards are buggy.
54 1.13 mlelstv */
55 1.13 mlelstv #ifdef __GNUC__
56 1.22 christos #define ASCTIME_FMT "%s %s%3d %2.2d:%2.2d:%2.2d %-4s\n"
57 1.13 mlelstv #else /* !defined __GNUC__ */
58 1.22 christos #define ASCTIME_FMT "%s %s%3d %02.2d:%02.2d:%02.2d %-4s\n"
59 1.13 mlelstv #endif /* !defined __GNUC__ */
60 1.13 mlelstv /*
61 1.13 mlelstv ** For years that are more than four digits we put extra spaces before the year
62 1.13 mlelstv ** so that code trying to overwrite the newline won't end up overwriting
63 1.13 mlelstv ** a digit within a year and truncating the year (operating on the assumption
64 1.13 mlelstv ** that no output is better than wrong output).
65 1.13 mlelstv */
66 1.13 mlelstv #ifdef __GNUC__
67 1.22 christos #define ASCTIME_FMT_B "%s %s%3d %2.2d:%2.2d:%2.2d %s\n"
68 1.13 mlelstv #else /* !defined __GNUC__ */
69 1.22 christos #define ASCTIME_FMT_B "%s %s%3d %02.2d:%02.2d:%02.2d %s\n"
70 1.13 mlelstv #endif /* !defined __GNUC__ */
71 1.13 mlelstv
72 1.13 mlelstv #define STD_ASCTIME_BUF_SIZE 26
73 1.13 mlelstv /*
74 1.13 mlelstv ** Big enough for something such as
75 1.13 mlelstv ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
76 1.13 mlelstv ** (two three-character abbreviations, five strings denoting integers,
77 1.13 mlelstv ** seven explicit spaces, two explicit colons, a newline,
78 1.19 christos ** and a trailing NUL byte).
79 1.13 mlelstv ** The values above are for systems where an int is 32 bits and are provided
80 1.13 mlelstv ** as an example; the define below calculates the maximum for the system at
81 1.13 mlelstv ** hand.
82 1.13 mlelstv */
83 1.13 mlelstv #define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
84 1.13 mlelstv
85 1.13 mlelstv static char buf_asctime[MAX_ASCTIME_BUF_SIZE];
86 1.13 mlelstv
87 1.1 jtc char *
88 1.16 christos asctime_r(const struct tm *timeptr, char *buf)
89 1.1 jtc {
90 1.22 christos static const char wday_name[][4] = {
91 1.1 jtc "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
92 1.1 jtc };
93 1.22 christos static const char mon_name[][4] = {
94 1.1 jtc "Jan", "Feb", "Mar", "Apr", "May", "Jun",
95 1.1 jtc "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
96 1.1 jtc };
97 1.18 christos const char * wn;
98 1.18 christos const char * mn;
99 1.13 mlelstv char year[INT_STRLEN_MAXIMUM(int) + 2];
100 1.13 mlelstv char result[MAX_ASCTIME_BUF_SIZE];
101 1.1 jtc
102 1.14 christos if (timeptr == NULL) {
103 1.14 christos errno = EINVAL;
104 1.14 christos return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
105 1.14 christos }
106 1.1 jtc if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
107 1.1 jtc wn = "???";
108 1.1 jtc else wn = wday_name[timeptr->tm_wday];
109 1.1 jtc if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
110 1.1 jtc mn = "???";
111 1.1 jtc else mn = mon_name[timeptr->tm_mon];
112 1.1 jtc /*
113 1.13 mlelstv ** Use strftime's %Y to generate the year, to avoid overflow problems
114 1.13 mlelstv ** when computing timeptr->tm_year + TM_YEAR_BASE.
115 1.13 mlelstv ** Assume that strftime is unaffected by other out-of-range members
116 1.13 mlelstv ** (e.g., timeptr->tm_mday) when processing "%Y".
117 1.1 jtc */
118 1.13 mlelstv (void) strftime(year, sizeof year, "%Y", timeptr);
119 1.13 mlelstv (void) snprintf(result,
120 1.13 mlelstv sizeof(result),
121 1.13 mlelstv ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
122 1.1 jtc wn, mn,
123 1.1 jtc timeptr->tm_mday, timeptr->tm_hour,
124 1.1 jtc timeptr->tm_min, timeptr->tm_sec,
125 1.13 mlelstv year);
126 1.14 christos if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
127 1.14 christos return strcpy(buf, result);
128 1.14 christos else {
129 1.13 mlelstv errno = EOVERFLOW;
130 1.13 mlelstv return NULL;
131 1.13 mlelstv }
132 1.7 kleink }
133 1.7 kleink
134 1.7 kleink char *
135 1.15 abs asctime(const struct tm *timeptr)
136 1.7 kleink {
137 1.13 mlelstv return asctime_r(timeptr, buf_asctime);
138 1.1 jtc }
139