diff options
| author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2023-04-19 10:41:22 +0000 |
|---|---|---|
| committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2023-04-19 10:41:22 +0000 |
| commit | 48847a88f61e41f0d77755dd58f2df9f04642e1d (patch) | |
| tree | 8b4e81c4064e8a350d995c8cd1174c187297165f /asctime.c | |
| parent | 85639444f44f168af982f59143b53efbba37669e (diff) | |
Diffstat (limited to 'asctime.c')
| -rw-r--r-- | asctime.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/asctime.c b/asctime.c index f0159f8324fa..a40661f28976 100644 --- a/asctime.c +++ b/asctime.c @@ -50,8 +50,18 @@ enum { STD_ASCTIME_BUF_SIZE = 26 }; */ static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1]; +/* A similar buffer for ctime. + C89 requires that they be the same buffer. + This requirement was removed in C99, so support it only if requested, + as support is more likely to lead to bugs in badly written programs. */ +#if SUPPORT_C89 +# define buf_ctime buf_asctime +#else +static char buf_ctime[sizeof buf_asctime]; +#endif + char * -asctime_r(register const struct tm *timeptr, char *buf) +asctime_r(struct tm const *restrict timeptr, char *restrict buf) { static const char wday_name[][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -91,7 +101,8 @@ asctime_r(register const struct tm *timeptr, char *buf) timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, year); - if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) + if (strlen(result) < STD_ASCTIME_BUF_SIZE + || buf == buf_ctime || buf == buf_asctime) return strcpy(buf, result); else { errno = EOVERFLOW; @@ -104,3 +115,17 @@ asctime(register const struct tm *timeptr) { return asctime_r(timeptr, buf_asctime); } + +char * +ctime_r(const time_t *timep, char *buf) +{ + struct tm mytm; + struct tm *tmp = localtime_r(timep, &mytm); + return tmp ? asctime_r(tmp, buf) : NULL; +} + +char * +ctime(const time_t *timep) +{ + return ctime_r(timep, buf_ctime); +} |
