From baabb97c464480745a7cba5b4ac0ec04469ef340 Mon Sep 17 00:00:00 2001 From: Michael Weiss Date: Wed, 7 Nov 2018 12:26:47 +0100 Subject: [PATCH] Support $SOURCE_DATE_EPOCH for reproducible man pages The environment variable SOURCE_DATE_EPOCH [0] is standardized and can be used to produce reproducible output. Distributions like Debian will set this variable before the build and scdoc should use it (instead of the current date) for any timestamps within the man pages. [0]: https://reproducible-builds.org/docs/source-date-epoch/ --- src/main.c | 21 +++++++++++++++++---- test/preamble | 11 +++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 08957bf..72173c6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE #include #include #include @@ -67,10 +68,22 @@ static void parse_preamble(struct parser *p) { int section = -1; uint32_t ch; char date[256]; - time_t now; - time(&now); - struct tm *now_tm = localtime(&now); - strftime(date, sizeof(date), "%F", now_tm); + char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); + if (source_date_epoch != NULL) { + struct tm source_date_epoch_tm; + char *ret = strptime(source_date_epoch, "%s", &source_date_epoch_tm); + if (ret == NULL || *ret != '\0') { + fprintf(stderr, + "Error: $SOURCE_DATE_EPOCH is set but malformed.\n"); + exit(1); + } + strftime(date, sizeof(date), "%F", &source_date_epoch_tm); + } else { + time_t now; + time(&now); + struct tm *now_tm = localtime(&now); + strftime(date, sizeof(date), "%F", now_tm); + } while ((ch = parser_getch(p)) != UTF8_INVALID) { if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') { int ret = str_append_ch(name, ch); diff --git a/test/preamble b/test/preamble index 03e2d0c..e46dc32 100755 --- a/test/preamble +++ b/test/preamble @@ -37,6 +37,9 @@ test(8) EOF end 0 +# Make sure SOURCE_DATE_EPOCH is not set for the next tests +unset SOURCE_DATE_EPOCH + begin "Writes the appropriate header" scdoc </dev/null test(8) @@ -66,3 +69,11 @@ scdoc </d test-manual(8) "" "Header" EOF end 0 + +export SOURCE_DATE_EPOCH=$(date --date="2017-12-09 23:18:57 -0500" +'%s') + +begin "Supports \$SOURCE_DATE_EPOCH" +scdoc </dev/null +reproducible-manual(8) +EOF +end 0