"Fossies" - the Fresh Open Source Software Archive 
Member "asctime.c" (12 Jul 2022, 3611 Bytes) of package /linux/misc/tzcode2022g.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "asctime.c":
2021e_vs_2022e.
1 /* asctime and asctime_r a la POSIX and ISO C, except pad years before 1000. */
2
3 /*
4 ** This file is in the public domain, so clarified as of
5 ** 1996-06-05 by Arthur David Olson.
6 */
7
8 /*
9 ** Avoid the temptation to punt entirely to strftime;
10 ** the output of strftime is supposed to be locale specific
11 ** whereas the output of asctime is supposed to be constant.
12 */
13
14 /*LINTLIBRARY*/
15
16 #include "private.h"
17 #include <stdio.h>
18
19 /*
20 ** All years associated with 32-bit time_t values are exactly four digits long;
21 ** some years associated with 64-bit time_t values are not.
22 ** Vintage programs are coded for years that are always four digits long
23 ** and may assume that the newline always lands in the same place.
24 ** For years that are less than four digits, we pad the output with
25 ** leading zeroes to get the newline in the traditional place.
26 ** The -4 ensures that we get four characters of output even if
27 ** we call a strftime variant that produces fewer characters for some years.
28 ** The ISO C and POSIX standards prohibit padding the year,
29 ** but many implementations pad anyway; most likely the standards are buggy.
30 */
31 static char const ASCTIME_FMT[] = "%s %s%3d %.2d:%.2d:%.2d %-4s\n";
32 /*
33 ** For years that are more than four digits we put extra spaces before the year
34 ** so that code trying to overwrite the newline won't end up overwriting
35 ** a digit within a year and truncating the year (operating on the assumption
36 ** that no output is better than wrong output).
37 */
38 static char const ASCTIME_FMT_B[] = "%s %s%3d %.2d:%.2d:%.2d %s\n";
39
40 enum { STD_ASCTIME_BUF_SIZE = 26 };
41 /*
42 ** Big enough for something such as
43 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
44 ** (two three-character abbreviations, five strings denoting integers,
45 ** seven explicit spaces, two explicit colons, a newline,
46 ** and a trailing NUL byte).
47 ** The values above are for systems where an int is 32 bits and are provided
48 ** as an example; the size expression below is a bound for the system at
49 ** hand.
50 */
51 static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1];
52
53 char *
54 asctime_r(register const struct tm *timeptr, char *buf)
55 {
56 static const char wday_name[][4] = {
57 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
58 };
59 static const char mon_name[][4] = {
60 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
61 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
62 };
63 register const char * wn;
64 register const char * mn;
65 char year[INT_STRLEN_MAXIMUM(int) + 2];
66 char result[sizeof buf_asctime];
67
68 if (timeptr == NULL) {
69 errno = EINVAL;
70 return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
71 }
72 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
73 wn = "???";
74 else wn = wday_name[timeptr->tm_wday];
75 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
76 mn = "???";
77 else mn = mon_name[timeptr->tm_mon];
78 /*
79 ** Use strftime's %Y to generate the year, to avoid overflow problems
80 ** when computing timeptr->tm_year + TM_YEAR_BASE.
81 ** Assume that strftime is unaffected by other out-of-range members
82 ** (e.g., timeptr->tm_mday) when processing "%Y".
83 */
84 strftime(year, sizeof year, "%Y", timeptr);
85 /*
86 ** We avoid using snprintf since it's not available on all systems.
87 */
88 sprintf(result,
89 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
90 wn, mn,
91 timeptr->tm_mday, timeptr->tm_hour,
92 timeptr->tm_min, timeptr->tm_sec,
93 year);
94 if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
95 return strcpy(buf, result);
96 else {
97 errno = EOVERFLOW;
98 return NULL;
99 }
100 }
101
102 char *
103 asctime(register const struct tm *timeptr)
104 {
105 return asctime_r(timeptr, buf_asctime);
106 }