File: builddate.c

package info (click to toggle)
gretl 2016d-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 48,620 kB
  • ctags: 22,779
  • sloc: ansic: 345,830; sh: 4,648; makefile: 2,712; xml: 570; perl: 364
file content (94 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download
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
#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);
    char *endptr;
    char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
    unsigned long long epoch;

    if (source_date_epoch != NULL) {
        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);
    }

    *y = lt->tm_year + 1900;
    *m = lt->tm_mon + 1;
    *d = lt->tm_mday;
}

/* 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);
	    fclose(fp);
	}
    } else {
	printf("build.h is current\n");
    }

    return 0;
}