diff options
Diffstat (limited to 'contrib/bmake/util.c')
-rw-r--r-- | contrib/bmake/util.c | 137 |
1 files changed, 104 insertions, 33 deletions
diff --git a/contrib/bmake/util.c b/contrib/bmake/util.c index dab18e2ece53..f660c21a228a 100644 --- a/contrib/bmake/util.c +++ b/contrib/bmake/util.c @@ -1,9 +1,9 @@ -/* $NetBSD: util.c,v 1.76 2021/02/03 08:00:36 rillig Exp $ */ +/* $NetBSD: util.c,v 1.78 2021/12/15 12:58:01 rillig Exp $ */ /* * Missing stuff from OS's * - * $Id: util.c,v 1.49 2021/10/14 19:26:52 sjg Exp $ + * $Id: util.c,v 1.52 2024/01/04 00:27:30 sjg Exp $ */ #include <sys/param.h> @@ -13,7 +13,7 @@ #include "make.h" -MAKE_RCSID("$NetBSD: util.c,v 1.76 2021/02/03 08:00:36 rillig Exp $"); +MAKE_RCSID("$NetBSD: util.c,v 1.78 2021/12/15 12:58:01 rillig Exp $"); #if !defined(MAKE_NATIVE) && !defined(HAVE_STRERROR) extern int errno, sys_nerr; @@ -22,12 +22,12 @@ extern char *sys_errlist[]; char * strerror(int e) { - static char buf[100]; - if (e < 0 || e >= sys_nerr) { - snprintf(buf, sizeof buf, "Unknown error %d", e); - return buf; - } else - return sys_errlist[e]; + static char buf[100]; + if (e < 0 || e >= sys_nerr) { + snprintf(buf, sizeof buf, "Unknown error %d", e); + return buf; + } else + return sys_errlist[e]; } #endif @@ -57,9 +57,9 @@ findenv(const char *name, int *offset) char * getenv(const char *name) { - int offset; + int offset; - return findenv(name, &offset); + return findenv(name, &offset); } int @@ -73,7 +73,7 @@ unsetenv(const char *name) return -1; } - while (findenv(name, &offset)) { /* if set multiple times */ + while (findenv(name, &offset)) { /* if set multiple times */ for (p = &environ[offset];; p++) if (!(*p = *(p + 1))) break; @@ -94,7 +94,7 @@ setenv(const char *name, const char *value, int rewrite) return -1; } - if (*value == '=') /* no `=' in value */ + if (*value == '=') /* no `=' in value */ value++; l_value = strlen(value); @@ -160,16 +160,15 @@ main(int argc, char *argv[]) static char * strrcpy(char *ptr, char *str) { - int len = strlen(str); - - while (len != 0) - *--ptr = str[--len]; + int len = strlen(str); - return ptr; -} /* end strrcpy */ + while (len != 0) + *--ptr = str[--len]; + return ptr; +} -char *sys_siglist[] = { +char *sys_siglist[] = { "Signal 0", "Hangup", /* SIGHUP */ "Interrupt", /* SIGINT */ @@ -218,7 +217,7 @@ char *sys_siglist[] = { int killpg(int pid, int sig) { - return kill(-pid, sig); + return kill(-pid, sig); } #if !defined(BSD) && !defined(d_fileno) @@ -393,7 +392,7 @@ vsnprintf(char *s, size_t n, const char *fmt, va_list args) fakebuf._cnt++; putc('\0', &fakebuf); if (fakebuf._cnt < 0) - fakebuf._cnt = 0; + fakebuf._cnt = 0; return n - fakebuf._cnt - 1; #else #ifndef _PATH_DEVNULL @@ -432,18 +431,28 @@ snprintf(char *s, size_t n, const char *fmt, ...) } #endif -#if !defined(HAVE_STRFTIME) +#if !defined(HAVE_STRFTIME) || defined(FORCE_BMAKE_STRFTIME) +/* we only implement enough to pass our unit-tests */ size_t strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) { - static char months[][4] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + static const char *months[] = { + "January", "February", "March", + "April", "May", "June", + "July", "August", "September", + "October", "November", "December" }; - + static const char *days[] = { + "Sunday", "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday" + }; + int i; size_t s; char *b = buf; + char *cp; + if (fmt == NULL || *fmt == '\0') + fmt = "%c"; while (*fmt) { if (len == 0) return buf - b; @@ -452,6 +461,7 @@ strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) len--; continue; } + fmt++; switch (*fmt++) { case '%': *buf++ = '%'; @@ -462,26 +472,87 @@ strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) *buf = '%'; s = 1; break; + case 'A': + s = snprintf(buf, len, "%s", days[tm->tm_wday]); + break; + case 'a': + s = snprintf(buf, len, "%.3s", days[tm->tm_wday]); + break; + case 'B': + if (tm->tm_mon >= 12) + return buf - b; + s = snprintf(buf, len, "%s", months[tm->tm_mon]); + break; + case 'b': + if (tm->tm_mon >= 12) + return buf - b; + s = snprintf(buf, len, "%.3s", months[tm->tm_mon]); + break; + case 'c': + s = strftime(buf, len, "%a %b %e %H:%M:%S %Y", tm); + break; + case 'd': + s = snprintf(buf, len, "%02d", tm->tm_mday); + break; + case 'e': + s = snprintf(buf, len, "%2d", tm->tm_mday); + break; + case 'F': + s = strftime(buf, len, "%y-%m-%d", tm); + break; + case 'H': + s = snprintf(buf, len, "%02d", tm->tm_hour); + break; + case 'I': + if ((i = tm->tm_hour) == 0) + i = 24; + s = snprintf(buf, len, "%02d", (i > 12) ? (i - 12) : i); + break; + case 'j': + s = snprintf(buf, len, "%03d", tm->tm_yday + 1); + break; case 'k': s = snprintf(buf, len, "%d", tm->tm_hour); break; case 'M': s = snprintf(buf, len, "%02d", tm->tm_min); break; + case 'm': + s = snprintf(buf, len, "%02d", 1 + tm->tm_mon); + break; case 'S': s = snprintf(buf, len, "%02d", tm->tm_sec); break; - case 'b': - if (tm->tm_mon >= 12) - return buf - b; - s = snprintf(buf, len, "%s", months[tm->tm_mon]); + case 's': + s = snprintf(buf, len, "%ld", (long)time(NULL)); break; - case 'd': - s = snprintf(buf, len, "%02d", tm->tm_mday); + case 'T': + s = strftime(buf, len, "%H:%M:%S", tm); + break; + case 'w': + s = snprintf(buf, len, "%02d", tm->tm_wday); break; case 'Y': s = snprintf(buf, len, "%d", 1900 + tm->tm_year); break; + case 'y': + s = snprintf(buf, len, "%02d", tm->tm_year % 100); + break; + case 'Z': + if ((cp = getenv("TZ")) != NULL) { + char tz[20]; + + i = snprintf(tz, sizeof(tz), "%s", cp); + if (i > 5) { + cp = &tz[i - 3]; + tz[3] = '\0'; + } else + cp = tz; + s = snprintf(buf, len, "%s", + tm->tm_isdst ? cp : tz); + } else + s = 0; + break; default: s = snprintf(buf, len, "Unsupported format %c", fmt[-1]); |