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