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 "namespace.h" 17 #include "private.h" 18 #include "un-namespace.h" 19 #include <stdio.h> 20 21 /* 22 ** All years associated with 32-bit time_t values are exactly four digits long; 23 ** some years associated with 64-bit time_t values are not. 24 ** Vintage programs are coded for years that are always four digits long 25 ** and may assume that the newline always lands in the same place. 26 ** For years that are less than four digits, we pad the output with 27 ** leading zeroes to get the newline in the traditional place. 28 ** The -4 ensures that we get four characters of output even if 29 ** we call a strftime variant that produces fewer characters for some years. 30 ** The ISO C and POSIX standards prohibit padding the year, 31 ** but many implementations pad anyway; most likely the standards are buggy. 32 */ 33 static char const ASCTIME_FMT[] = "%s %s%3d %.2d:%.2d:%.2d %-4s\n"; 34 /* 35 ** For years that are more than four digits we put extra spaces before the year 36 ** so that code trying to overwrite the newline won't end up overwriting 37 ** a digit within a year and truncating the year (operating on the assumption 38 ** that no output is better than wrong output). 39 */ 40 static char const ASCTIME_FMT_B[] = "%s %s%3d %.2d:%.2d:%.2d %s\n"; 41 42 enum { STD_ASCTIME_BUF_SIZE = 26 }; 43 /* 44 ** Big enough for something such as 45 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n 46 ** (two three-character abbreviations, five strings denoting integers, 47 ** seven explicit spaces, two explicit colons, a newline, 48 ** and a trailing NUL byte). 49 ** The values above are for systems where an int is 32 bits and are provided 50 ** as an example; the size expression below is a bound for the system at 51 ** hand. 52 */ 53 static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1]; 54 55 char * 56 asctime_r(register const struct tm *timeptr, char *buf) 57 { 58 static const char wday_name[][4] = { 59 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 60 }; 61 static const char mon_name[][4] = { 62 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 63 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 64 }; 65 const char * wn; 66 const char * mn; 67 char year[INT_STRLEN_MAXIMUM(int) + 2]; 68 char result[sizeof buf_asctime]; 69 70 if (timeptr == NULL) { 71 errno = EINVAL; 72 return strcpy(buf, "??? ??? ?? ??:??:?? ????\n"); 73 } 74 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) 75 wn = "???"; 76 else wn = wday_name[timeptr->tm_wday]; 77 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) 78 mn = "???"; 79 else mn = mon_name[timeptr->tm_mon]; 80 /* 81 ** Use strftime's %Y to generate the year, to avoid overflow problems 82 ** when computing timeptr->tm_year + TM_YEAR_BASE. 83 ** Assume that strftime is unaffected by other out-of-range members 84 ** (e.g., timeptr->tm_mday) when processing "%Y". 85 */ 86 strftime(year, sizeof year, "%Y", timeptr); 87 /* 88 ** We avoid using snprintf since it's not available on all systems. 89 */ 90 sprintf(result, 91 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), 92 wn, mn, 93 timeptr->tm_mday, timeptr->tm_hour, 94 timeptr->tm_min, timeptr->tm_sec, 95 year); 96 if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) 97 return strcpy(buf, result); 98 else { 99 errno = EOVERFLOW; 100 return NULL; 101 } 102 } 103 104 char * 105 asctime(register const struct tm *timeptr) 106 { 107 return asctime_r(timeptr, buf_asctime); 108 } 109