~arivigo/schain

ref: 61e569b7f312a460a607650f4c87ec6b93b89ebb schain/schain.c -rw-r--r-- 3.4 KiB
61e569b7 — Ariadna Vigo New -r option: reset to zero 27 days ago
                                                                                
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* See LICENSE file for copyright and license details. */

#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "config.h"
#include "date.h"
#include "strlcpy.h"

#define BUF_MAX_SIZE 64
#define DATE_SIZE 11 /* 'YYYY-MM-DD\0' */

/* Execution modes */
enum {
	DEF_MODE,
	CHECKIN_MODE,
	CREATE_MODE
};

/* Output formats */
enum {
	LONG_OUT,
	SHORT_OUT
};

/* Auxiliary procedures */
static void die(const char *fmt, ...);
static void usage(void);
static void read_file(char *date, int64_t *num, const char *path);
static void write_file(const char *path, const char *date, int64_t num);

static void
die(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);

	va_end(ap);

	exit(1);
}

static void
usage(void)
{
	die("usage: schain [-krsv] [-cK num] [-w date] [file]");
}

static void
read_file(char *date, int64_t *num, const char *path)
{
	FILE *fp;
	char *ptr;
	char buf[BUF_MAX_SIZE];

	if ((fp = fopen(path, "r")) == NULL)
		die("%s: %s.", path, strerror(errno));
	fgets(buf, BUF_MAX_SIZE, fp);
	fclose(fp);

	ptr = strtok(buf, "\t");
	if (ptr == NULL)
		goto parsedie;

	if (date != NULL)
		strlcpy(date, ptr, DATE_SIZE);

	ptr = strtok(NULL, "\n");
	if (ptr == NULL)
		goto parsedie;

	if (sscanf(ptr, "%" SCNd64, num) == 0 || *num < 0)
		goto parsedie;

	return;

parsedie:
	die("%s: Not an schain file.", path);
}

static void
write_file(const char *path, const char *date, int64_t num)
{
	FILE *fp;

	if ((fp = fopen(path, "w")) == NULL)
		die("%s: %s.", path, strerror(errno));

	fprintf(fp, "%s\t%" PRId64 "\n", date, num);

	fclose(fp);
}

int
main(int argc, char *argv[])
{
	int opt, mode, out_fmt;
	int64_t num, check_diff;
	const char *filepath;
	char *datearg;
	char date[DATE_SIZE];

	num = 0;
	datearg = NULL;
	check_diff = 1;
	mode = DEF_MODE;
	out_fmt = LONG_OUT;

	while ((opt = getopt(argc, argv, ":krsvc:K:w:")) != -1) {
		switch (opt) {
		case 'k':
			mode = CHECKIN_MODE;
			break;
		case 'r':
			mode = CREATE_MODE;
			num = 0;
			break;
		case 's':
			out_fmt = SHORT_OUT;
			break;
		case 'v':
			die("schain %s", VERSION);
			break; /* UNREACHABLE */
		case 'c':
			mode = CREATE_MODE;
			sscanf(optarg, "%" SCNd64, &num);
			break;
		case 'K':
			mode = CHECKIN_MODE;
			sscanf(optarg, "%" SCNd64, &check_diff);
			break;
		case 'w':
			datearg = optarg;
			break;
		default:
			usage();
			break; /* UNREACHABLE */
		}
	}

	if (optind < argc) {
		filepath = argv[optind];
	} else {
		if ((filepath = getenv("SCHAIN_DEF_FILE")) == NULL)
			die("SCHAIN_DEF_FILE environment variable not set.");
	}

	if (num < 0 || check_diff < 0)
		die("Error: not a positive number.");

	if (datearg != NULL)
		strlcpy(date, datearg, DATE_SIZE);
	else
		strlcpy(date, date_str(time(NULL)), DATE_SIZE);

	if (is_date(date) < 0)
		die("Error: not a valid YYYY-MM-DD date.");

	switch (mode) {
	case CREATE_MODE:
		write_file(filepath, date, num);
		break;
	case CHECKIN_MODE:
		read_file(NULL, &num, filepath);
		if (num + check_diff < 0)
			die("Check-in error: count in file too big. Abort.");

		num += check_diff;

		write_file(filepath, date, num);
		break;
	default:
		read_file(date, &num, filepath);
		break;
	}

	if (out_fmt == SHORT_OUT)
		printf("%" PRId64 "\n", num);
	else
		printf("%s: %" PRId64 " %s.\n", date, num, unit);

	return 0;
}