From f26f75bb32fa1474ee83fcff787b6423c44189cf Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Thu, 22 Oct 2009 18:55:32 +0000 Subject: Fixed maddening mismatch between groff and strftime mismatch of day ("%e"). Noted by Ulrich Sporlein. --- man_html.c | 8 ++------ man_term.c | 8 ++------ mdoc_html.c | 10 +++------- mdoc_term.c | 12 +++--------- out.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ out.h | 3 +++ 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/man_html.c b/man_html.c index f6939321..82170e9f 100644 --- a/man_html.c +++ b/man_html.c @@ -281,15 +281,11 @@ man_root_pre(MAN_ARGS) static void man_root_post(MAN_ARGS) { - struct tm tm; struct htmlpair tag[2]; struct tag *t, *tt; - char b[BUFSIZ]; + char b[DATESIZ]; - (void)localtime_r(&m->date, &tm); - - if (0 == strftime(b, BUFSIZ - 1, "%B %e, %Y", &tm)) - err(EXIT_FAILURE, "strftime"); + time2a(m->date, b, DATESIZ); PAIR_CLASS_INIT(&tag[0], "footer"); bufcat_style(h, "width", "100%"); diff --git a/man_term.c b/man_term.c index 188c9f1c..76e07ecc 100644 --- a/man_term.c +++ b/man_term.c @@ -909,13 +909,9 @@ print_body(DECL_ARGS) static void print_foot(struct termp *p, const struct man_meta *meta) { - struct tm *tm; - char buf[BUFSIZ]; + char buf[DATESIZ]; - tm = localtime(&meta->date); - - if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm)) - (void)strlcpy(buf, "(invalid date)", BUFSIZ); + time2a(meta->date, buf, DATESIZ); term_vspace(p); diff --git a/mdoc_html.c b/mdoc_html.c index dc7b1200..d0c2ff7c 100644 --- a/mdoc_html.c +++ b/mdoc_html.c @@ -456,10 +456,11 @@ print_mdoc_node(MDOC_ARGS) static void mdoc_root_post(MDOC_ARGS) { - struct tm tm; struct htmlpair tag[2]; struct tag *t, *tt; - char b[BUFSIZ]; + char b[DATESIZ]; + + time2a(m->date, b, DATESIZ); /* * XXX: this should use divs, but in Firefox, divs with nested @@ -467,11 +468,6 @@ mdoc_root_post(MDOC_ARGS) * below. So I use tables, instead. */ - (void)localtime_r(&m->date, &tm); - - if (0 == strftime(b, BUFSIZ - 1, "%B %e, %Y", &tm)) - err(EXIT_FAILURE, "strftime"); - PAIR_CLASS_INIT(&tag[0], "footer"); bufcat_style(h, "width", "100%"); PAIR_STYLE_INIT(&tag[1], h); diff --git a/mdoc_term.c b/mdoc_term.c index 789d3454..c59c42b6 100644 --- a/mdoc_term.c +++ b/mdoc_term.c @@ -347,8 +347,8 @@ print_node(DECL_ARGS) static void print_foot(DECL_ARGS) { - struct tm *tm; - char *buf, *os; + char buf[DATESIZ]; + char *os; /* * Output the footer in new-groff style, that is, three columns @@ -358,15 +358,10 @@ print_foot(DECL_ARGS) * SYSTEM DATE SYSTEM */ - if (NULL == (buf = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); if (NULL == (os = malloc(p->rmargin))) err(EXIT_FAILURE, "malloc"); - tm = localtime(&m->date); - - if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm)) - err(EXIT_FAILURE, "strftime"); + time2a(m->date, buf, DATESIZ); (void)strlcpy(os, m->os, p->rmargin); @@ -398,7 +393,6 @@ print_foot(DECL_ARGS) p->rmargin = p->maxrmargin; p->flags = 0; - free(buf); free(os); } diff --git a/out.c b/out.c index ce09af1c..aac9d78e 100644 --- a/out.c +++ b/out.c @@ -16,9 +16,11 @@ */ #include +#include #include #include #include +#include #include "out.h" @@ -117,3 +119,46 @@ a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) return(1); } + + +/* + * Correctly writes the time in nroff form, which differs from standard + * form in that a space isn't printed in lieu of the extra %e field for + * single-digit dates. + */ +void +time2a(time_t t, char *dst, size_t sz) +{ + struct tm tm; + char buf[5]; + char *p; + size_t nsz; + + assert(sz > 1); + localtime_r(&t, &tm); + + p = dst; + nsz = 0; + + dst[0] = '\0'; + + if (0 == (nsz = strftime(p, sz, "%B ", &tm))) + return; + + p += (int)nsz; + sz -= nsz; + + if (0 == strftime(buf, sizeof(buf), "%e, ", &tm)) + return; + + nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz); + + if (nsz >= sz) + return; + + p += (int)nsz; + sz -= nsz; + + (void)strftime(p, sz, "%Y", &tm); +} + diff --git a/out.h b/out.h index 6a38059f..92ebbba9 100644 --- a/out.h +++ b/out.h @@ -17,6 +17,8 @@ #ifndef OUT_H #define OUT_H +#define DATESIZ 24 + __BEGIN_DECLS enum roffscale { @@ -52,6 +54,7 @@ struct roffsu { int a2roffsu(const char *, struct roffsu *, enum roffscale); +void time2a(time_t, char *, size_t); __END_DECLS -- cgit