From 6d737a7607ed7f779618117697e96308357fdfa4 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 14 Nov 2017 11:00:29 +0000 Subject: [PATCH] output: unify time stamp creations Use iso time stamp in both mustach and html outputs. Effectively this is a removal of libc langinfo D_T_FMT format, that pulled a lot of gnulib stuff to project almost unnecessarily. Signed-off-by: Sami Kerola --- bootstrap.conf | 2 -- lib/.gitignore | 16 ---------------- src/dhcpd-pools.h | 1 + src/mustach-dhcpd-pools.c | 31 ------------------------------- src/other.c | 32 ++++++++++++++++++++++++++++++++ src/output.c | 21 +++------------------ 6 files changed, 36 insertions(+), 67 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index 7ab7e49..bc07e7f 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -29,9 +29,7 @@ gnulib_modules=" getopt-gnu inet_pton isnan - langinfo netinet_in - nl_langinfo nstrftime progname quote diff --git a/lib/.gitignore b/lib/.gitignore index e138fda..0de1e31 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -1,11 +1,6 @@ /*.lo -/*.o -/.deps/ -/.gitignore~ /.libs/ -/Makefile /Makefile.am -/Makefile.in /_Noreturn.h /alloca.h /alloca.in.h @@ -71,7 +66,6 @@ /getprogname.c /getprogname.h /gettext.h -/gettimeofday.c /hard-locale.c /hard-locale.h /inet_pton.c @@ -81,16 +75,11 @@ /isnanf.c /isnanl.c /itold.c -/langinfo.h -/langinfo.in.h /libdhcpd_pools.la /limits.h /limits.in.h /localcharset.c /localcharset.h -/locale.h -/locale.in.h -/localeconv.c /localtime-buffer.c /localtime-buffer.h /lseek.c @@ -113,7 +102,6 @@ /msvc-nothrow.c /msvc-nothrow.h /netinet_in.in.h -/nl_langinfo.c /nstrftime.c /pathmax.h /progname.c @@ -133,7 +121,6 @@ /stdalign.in.h /stdbool.in.h /stddef.in.h -/stdint.h /stdint.in.h /stdio-impl.h /stdio.h @@ -147,7 +134,6 @@ /strerror-override.c /strerror-override.h /strerror.c -/strftime.c /strftime.h /string.h /string.in.h @@ -156,13 +142,11 @@ /strtod.c /sys/socket.h /sys/stat.h -/sys/time.h /sys/types.h /sys/uio.h /sys_socket.c /sys_socket.in.h /sys_stat.in.h -/sys_time.in.h /sys_types.in.h /sys_uio.in.h /time-internal.h diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 42e6138..a2715f3 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -277,6 +277,7 @@ extern int parse_color_mode(const char *restrict optarg); extern double strtod_or_err(const char *restrict str, const char *restrict errmesg); extern void __attribute__ ((noreturn)) print_version(void); extern void __attribute__ ((noreturn)) usage(int status); +extern void dp_time_tool(FILE *file, const char *path, int epoch); extern int (*parse_ipaddr) (struct conf_t *state, const char *restrict src, union ipaddr_t *restrict dst); diff --git a/src/mustach-dhcpd-pools.c b/src/mustach-dhcpd-pools.c index a3a7127..d152938 100644 --- a/src/mustach-dhcpd-pools.c +++ b/src/mustach-dhcpd-pools.c @@ -45,14 +45,12 @@ #include #include #include -#include #include #include "close-stream.h" #include "dhcpd-pools.h" #include "error.h" #include "mustach.h" -#include "strftime.h" #include "xalloc.h" /*! \struct expl @@ -69,35 +67,6 @@ struct expl { static int must_enter(void *closure, const char *name); static int must_leave(void *closure); -static void dp_time_tool(FILE *file, const char *path, int epoch) -{ - time_t t; - - /* a file or now */ - if (path) { - struct stat st; - - stat(path, &st); - t = st.st_mtime; - } else - t = time(NULL); - /* epoc or iso time stamp */ - if (epoch) - fprintf(file, "%ld", t); - else { - char time_stamp[64]; - struct tm tm; - int len; - - localtime_r(&t, &tm); - len = snprintf(time_stamp, sizeof(time_stamp), "%4d-%.2d-%.2dT%02d:%02d:%02d", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); - strftime(time_stamp + len, sizeof(time_stamp) - len, "%z", &tm); - fprintf(file, "%s", time_stamp); - } -} - /*! \brief Template base level tag parser and printer. */ static int must_put_base(void *closure, const char *name, int escape __attribute__ ((unused)), FILE *file) diff --git a/src/other.c b/src/other.c index dc207bd..5467fb1 100644 --- a/src/other.c +++ b/src/other.c @@ -47,6 +47,8 @@ #include #include #include +#include +#include #include #include "error.h" @@ -544,6 +546,36 @@ void clean_up(struct conf_t *state) } } +/*! \brief Print a time stamp of a path or now to output file. */ +void dp_time_tool(FILE *file, const char *path, int epoch) +{ + time_t t; + + /* a file or now */ + if (path) { + struct stat st; + + stat(path, &st); + t = st.st_mtime; + } else + t = time(NULL); + /* epoc or iso time stamp */ + if (epoch) + fprintf(file, "%ld", t); + else { + char time_stamp[64]; + struct tm tm; + int len; + + localtime_r(&t, &tm); + len = snprintf(time_stamp, sizeof(time_stamp), "%4d-%.2d-%.2dT%02d:%02d:%02d", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec); + strftime(time_stamp + len, sizeof(time_stamp) - len, "%z", &tm); + fprintf(file, "%s", time_stamp); + } +} + /*! \brief A version printing. */ void __attribute__ ((__noreturn__)) print_version(void) { diff --git a/src/output.c b/src/output.c index 9647f0c..9ee2475 100644 --- a/src/output.c +++ b/src/output.c @@ -609,22 +609,6 @@ static int output_json(struct conf_t *state, const int print_mac_addreses) */ static void html_header(struct conf_t *state, FILE *restrict f) { - char outstr[200]; - struct tm *tmp, result; - - struct stat statbuf; - - stat(state->dhcpdlease_file, &statbuf); - - tmp = localtime_r(&statbuf.st_mtime, &result); - if (tmp == NULL) { - error(EXIT_FAILURE, errno, "html_header: localtime"); - } - setlocale(LC_CTYPE, ""); - setlocale(LC_NUMERIC, ""); - if (strftime(outstr, sizeof(outstr), nl_langinfo(D_T_FMT), &result) == 0) { - error(EXIT_FAILURE, 0, "html_header: strftime returned 0"); - } fprintf(f, "\n"); fprintf(f, "\n"); fprintf(f, "\n"); @@ -641,8 +625,9 @@ static void html_header(struct conf_t *state, FILE *restrict f) fprintf(f, "\n"); fprintf(f, "
\n"); fprintf(f, "

ISC DHCPD status

\n"); - fprintf(f, "File %s was last modified at %s
\n", state->dhcpdlease_file, - outstr); + fprintf(f, "File %s was last modified at ", state->dhcpdlease_file); + dp_time_tool(f, state->dhcpdlease_file, 0); + fprintf(f, "
\n"); } /*! \brief Footer for full html output format.