-
Notifications
You must be signed in to change notification settings - Fork 3
/
builddate.c
106 lines (94 loc) · 2.62 KB
/
builddate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
void current_ymd (int *y, int *m, int *d)
{
time_t t = time(NULL);
struct tm *lt = localtime(&t);
#ifndef _WIN32
char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
if (source_date_epoch != NULL) {
unsigned long long epoch;
char *endptr;
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);
}
t = epoch;
lt = gmtime(&t);
}
#endif /* ! _WIN32 */
*y = lt->tm_year + 1900;
*m = lt->tm_mon + 1;
*d = lt->tm_mday;
}
static const char *compiler_defs =
"\n#if (defined __GNUC__) && !(defined __clang__)\n"
"# define COMPILER_IDENT \"GCC \" __VERSION__\n"
"#else\n"
"# define COMPILER_IDENT __VERSION__\n"
"#endif\n";
/* See if build.h exists and is up to date, and if not,
create/update it. */
int main (void)
{
int yb = 0, mb = 0, db = 0;
int y = 0, m = 0, d = 0;
int n, update = 1;
char *s, line[128];
FILE *fp;
current_ymd(&y, &m, &d);
fp = fopen("build.h", "r");
if (fp != NULL) {
if (fgets(line, sizeof line, fp) != NULL) {
s = strstr(line, "20");
if (s != NULL) {
n = sscanf(s, "%d-%d-%d", &yb, &mb, &db);
if (n == 3 && y == yb && m == mb && d == db) {
/* dates agree */
update = 0;
}
}
}
fclose(fp);
}
if (update) {
fp = fopen("build.h", "w");
if (fp == NULL) {
fprintf(stderr, "Can't write to build.h\n");
exit(EXIT_FAILURE);
} else {
printf("Updating build.h\n");
fprintf(fp, "#define BUILD_DATE \"%d-%02d-%02d\"\n",
y, m, d);
fputs(compiler_defs, fp);
fclose(fp);
}
} else {
printf("build.h is current\n");
}
return 0;
}