From: Matthijs Mekking Date: Wed, 17 Jun 2020 12:00:09 +0000 (+0200) Subject: Move dst key printtime in separate function X-Git-Tag: v9.17.3~26^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e03f8e8feb9b794a769612afb0eaf417c35c5e3;p=thirdparty%2Fbind9.git Move dst key printtime in separate function I'd like to use the same functionality (pretty print the datetime of keytime metadata) in the 'rndc dnssec -status' command. So it is better that this logic is done in a separate function. Since the stdtime.c code have differernt files for unix and win32, I think the "#ifdef WIN32" define can be dropped. --- diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c index 65afc771d36..7d7940a8515 100644 --- a/lib/dns/dst_api.c +++ b/lib/dns/dst_api.c @@ -1906,7 +1906,6 @@ printtime(const dst_key_t *key, int type, const char *tag, FILE *stream) { isc_result_t result; char output[26]; /* Minimum buffer as per ctime_r() specification. */ isc_stdtime_t when; - time_t t; char utc[sizeof("YYYYMMDDHHSSMM")]; isc_buffer_t b; isc_region_t r; @@ -1916,18 +1915,7 @@ printtime(const dst_key_t *key, int type, const char *tag, FILE *stream) { return; } - /* time_t and isc_stdtime_t might be different sizes */ - t = when; -#ifdef WIN32 - if (ctime_s(output, sizeof(output), &t) != 0) { - goto error; - } -#else /* ifdef WIN32 */ - if (ctime_r(&t, output) == NULL) { - goto error; - } -#endif /* ifdef WIN32 */ - + isc_stdtime_tostring(when, output, sizeof(output)); isc_buffer_init(&b, utc, sizeof(utc)); result = dns_time32_totext(when, &b); if (result != ISC_R_SUCCESS) { diff --git a/lib/isc/unix/include/isc/stdtime.h b/lib/isc/unix/include/isc/stdtime.h index 0503a28501a..08d737ee8c0 100644 --- a/lib/isc/unix/include/isc/stdtime.h +++ b/lib/isc/unix/include/isc/stdtime.h @@ -15,6 +15,7 @@ /*! \file */ #include +#include #include @@ -37,6 +38,20 @@ isc_stdtime_get(isc_stdtime_t *t); *\li 't' is a valid pointer. */ +void +isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen); +/* + * Convert 't' into a null-terminated string of the form + * "Wed Jun 30 21:49:08 1993". Store the string in the 'out' + * buffer. + * + * Requires: + * + * 't' is a valid time. + * 'out' is a valid pointer. + * 'outlen' is at least 26. + */ + #define isc_stdtime_convert32(t, t32p) (*(t32p) = t) /* * Convert the standard time to its 32-bit version. diff --git a/lib/isc/unix/stdtime.c b/lib/isc/unix/stdtime.c index e3fefad5004..24c071d76c3 100644 --- a/lib/isc/unix/stdtime.c +++ b/lib/isc/unix/stdtime.c @@ -49,3 +49,18 @@ isc_stdtime_get(isc_stdtime_t *t) { *t = (isc_stdtime_t)ts.tv_sec; } + +void +isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) { + time_t when; + + REQUIRE(out != NULL); + REQUIRE(outlen >= 26); + + UNUSED(outlen); + + /* time_t and isc_stdtime_t might be different sizes */ + when = t; + INSIST((ctime_r(&when, out) != NULL)); + *(out + strlen(out) - 1) = '\0'; +} diff --git a/lib/isc/win32/include/isc/stdtime.h b/lib/isc/win32/include/isc/stdtime.h index 5a845bd55f8..74ad0151fc3 100644 --- a/lib/isc/win32/include/isc/stdtime.h +++ b/lib/isc/win32/include/isc/stdtime.h @@ -13,6 +13,7 @@ #define ISC_STDTIME_H 1 #include +#include #include @@ -35,6 +36,20 @@ isc_stdtime_get(isc_stdtime_t *t); * 't' is a valid pointer. */ +void +isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen); +/* + * Convert 't' into a null-terminated string of the form + * "Wed Jun 30 21:49:08 1993". Store the string in the 'out' + * buffer. + * + * Requires: + * + * 't' is a valid time. + * 'out' is a valid pointer. + * 'outlen' is at least 26. + */ + #define isc_stdtime_convert32(t, t32p) (*(t32p) = t) /* * Convert the standard time to its 32-bit version. diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index 4d6705feec0..3f37ede289e 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -605,6 +605,7 @@ isc_stdio_sync isc_stdio_tell isc_stdio_write isc_stdtime_get +isc_stdtime_tostring isc_string_strerror_r isc_symtab_count isc_symtab_create diff --git a/lib/isc/win32/stdtime.c b/lib/isc/win32/stdtime.c index 55bd1239529..23364624ae7 100644 --- a/lib/isc/win32/stdtime.c +++ b/lib/isc/win32/stdtime.c @@ -25,3 +25,17 @@ isc_stdtime_get(isc_stdtime_t *t) { (void)_time32(t); } + +void +isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) { + time_t when; + + REQUIRE(out != NULL); + /* Minimum buffer as per ctime_r() specification. */ + REQUIRE(outlen >= 26); + + /* time_t and isc_stdtime_t might be different sizes */ + when = t; + INSIST((ctime_s(out, outlen, &when) == 0)); + *(out + strlen(out) - 1) = '\0'; +}