From cb445d3b32eee3d9d501a77eaf145d6895391c6e Mon Sep 17 00:00:00 2001 From: "Bernhard M. Wiedemann" Date: Wed, 5 Apr 2017 14:57:25 +0200 Subject: [PATCH] allow to override man-page date in order to allow for reproducible builds. See https://reproducible-builds.org/ for why this is good and https://reproducible-builds.org/specs/source-date-epoch/ for the definition of this variable. --- mxmldoc.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/mxmldoc.c b/mxmldoc.c index 6916dbd..fa0828e 100644 --- a/mxmldoc.c +++ b/mxmldoc.c @@ -23,6 +23,7 @@ #include "zipc.h" #include #include +#include #ifndef WIN32 # include # include @@ -5086,6 +5087,7 @@ write_man(const char *man_name, /* I - Name of manpage */ char prefix; /* Prefix character */ time_t curtime; /* Current time */ struct tm *curdate; /* Current date */ + char *source_date_epoch; /* ENV string */ char buffer[1024]; /* String buffer */ static const char * const scopes[] = /* Scope strings */ { @@ -5099,7 +5101,33 @@ write_man(const char *man_name, /* I - Name of manpage */ * Standard man page... */ - curtime = time(NULL); + source_date_epoch = getenv("SOURCE_DATE_EPOCH"); + if (source_date_epoch) { + char *endptr; + unsigned long long epoch; + errno = 0; + epoch = strtoull(source_date_epoch, &endptr, 10); + if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0)) + || (errno != 0 && epoch == 0)) { + fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + if (endptr == source_date_epoch) { + fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n", endptr); + exit(EXIT_FAILURE); + } + if (*endptr != '\0') { + fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n", endptr); + exit(EXIT_FAILURE); + } + if (epoch > ULONG_MAX) { + fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to %lu but was found to be: %llu \n", ULONG_MAX, epoch); + exit(EXIT_FAILURE); + } + curtime = epoch; + } else { + curtime = time(NULL); + } curdate = localtime(&curtime); strftime(buffer, sizeof(buffer), "%x", curdate);