#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <errno.h>
+#include <limits.h>
#ifdef WIN32
#include "gen-win32.h"
struct cc *cc;
struct ttnam *ttn, *ttn2;
unsigned int hash;
- struct tm *tm;
time_t now;
char year[11];
int lasttype;
char *prefix = NULL;
char *suffix = NULL;
char *file = NULL;
+ char *source_date_epoch;
+ unsigned long long epoch;
+ char *endptr;
isc_dir_t dir;
for (i = 0; i < TYPENAMES; i++)
INSIST(n > 0 && (unsigned)n < sizeof(srcdir));
sd(0, "", buf, filetype);
- if (time(&now) != -1) {
- if ((tm = localtime(&now)) != NULL && tm->tm_year > 104) {
+ source_date_epoch = getenv("SOURCE_DATE_EPOCH");
+ if (source_date_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);
+ }
+ now = epoch;
+ } else {
+ time(&now);
+ }
+
+ if (now != -1) {
+ struct tm *tm = gmtime(&now);
+ if (tm != NULL && tm->tm_year > 104) {
n = snprintf(year, sizeof(year), "-%d",
tm->tm_year + 1900);
INSIST(n > 0 && (unsigned)n < sizeof(year));