summaryrefslogtreecommitdiff
path: root/main.c
blob: 28066d9cb10347cc5a9b05c5b277929bd1fdce19 (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <libintl.h>

#define _(x) gettext(x)

#define NSEC_PER_SEC UINT64_C(1'000'000'000)

#if __APPLE__
#define TIMER_ABSTIME (1)

int clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
#endif

time_t syncs(time_t), syncm(time_t), synch(time_t);

[[noreturn]] static void
usage(const char *argv0)
{
	fprintf(stderr,
	        _("Usage: %s [-i interval] [format ...]\n"
	          "       %s -h\n"),
	        argv0, argv0);
	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	setlocale(LC_ALL, "");
	bindtextdomain("tiktok", PODIR);
	textdomain("tiktok");

	char interval = 's';
	const char *argv0 = basename(argv[0]);
	const char *optstr = "hi:";
	static struct option longopts[] = {
		{"help",     no_argument,       nullptr, 'h'},
		{"interval", required_argument, nullptr, 'i'},
		{nullptr,    0,                 nullptr,  0 },
	};

	for (;;) {
		int opt = getopt_long(argc, argv, optstr, longopts, nullptr);
		if (opt == -1)
			break;

		switch (opt) {
		case 'h':
			execlp("man", "man", "1", argv0, nullptr);
			err(EXIT_FAILURE, "execlp: man");
		case 'i':
			if (strlen(optarg) == 1) {
				interval = *optarg;
				if (interval == 's' || interval == 'm' || interval == 'h')
					break;
			}
			errx(EXIT_FAILURE,
				_("invalid interval ‘%s’\nRead the %s(1) manual page for valid intervals"),
				optarg, argv0);
		default:
			usage(argv0);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
		argc = 1;
		argv = (char *[]){"%c"};
	}

	time_t (*sync)(time_t) =
		  interval == 's' ? syncs
		: interval == 'm' ? syncm
		: interval == 'h' ? synch
		: /* default */     syncs;

	const char *tzorig = getenv("TZ");

	for (;;) {
		struct timespec now, then;
		if (clock_gettime(CLOCK_REALTIME, &now) == -1)
			warn(_("failed to get the time"));

		if (tzorig != nullptr) {
			if (setenv("TZ", tzorig, true) == -1)
				warn(_("failed to set the timezone"));
		} else if (unsetenv("TZ") == -1)
			warn(_("failed to set the timezone"));

		for (int i = 0; i < argc; i++) {
			if (strlen(argv[i]) >= 3 && memcmp("TZ=", argv[i], 3) == 0) {
				if (putenv(argv[i]) == -1)
					warn(_("failed to set the timezone"));
			} else {
				static char *buf;
				static size_t bufsz = 1024;
				if (buf == nullptr && (buf = malloc(bufsz)) == nullptr)
					err(EXIT_FAILURE, "malloc");

				struct tm *tm = localtime(&now.tv_sec);
				size_t n = strftime(buf, bufsz, argv[i], tm);
				while (n == 0) {
					bufsz *= 2;
					if ((buf = realloc(buf, bufsz)) == nullptr)
						err(EXIT_FAILURE, "malloc");
					n = strftime(buf, bufsz, argv[i], tm);
				}
				fputs(buf, stdout);
			}
		}
		putchar('\n');

		if (clock_gettime(CLOCK_REALTIME, &then) == -1)
			warn(_("failed to get the time"));

		/* Duration of the clock formatting and printing */
		struct timespec Δ = {
			then.tv_sec  - now.tv_sec,
			then.tv_nsec - now.tv_nsec,
		};
		if (Δ.tv_nsec < 0) {
			Δ.tv_nsec += NSEC_PER_SEC;
			Δ.tv_sec--;
		}

		struct timespec rqtp = {
			.tv_sec  = sync(then.tv_sec),
			.tv_nsec = 0,
		};

		int rv;
		do
			rv = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &rqtp, nullptr);
		while (rv == -1 && errno == EINTR);
	}

	/* We should never get here */
	return EXIT_FAILURE;
}

time_t
syncs(time_t n)
{
	return n + 1;
}

time_t
syncm(time_t n)
{
	return n + 60 - n%60;
}

time_t
synch(time_t n)
{
	return n + 3600 - n%3600;
}

#if __APPLE__

int
clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *req,
				struct timespec *rem)
{
	/* The rest not implemented */
	assert(flags == TIMER_ABSTIME);

	struct timespec now, Δ;

	if (clock_gettime(clock_id, &now) != 0)
		return errno;

	Δ.tv_sec = req->tv_sec - now.tv_sec;
	Δ.tv_nsec = req->tv_nsec - now.tv_nsec;

	if (Δ.tv_sec < 0) {
		Δ.tv_sec  = 0;
		Δ.tv_nsec = 0;
		goto out;
	}

	if (Δ.tv_nsec < 0) {
		if (Δ.tv_sec == 0) {
			Δ.tv_sec  = 0;
			Δ.tv_nsec = 0;
			goto out;
		}

		Δ.tv_sec--;
		Δ.tv_nsec += NSEC_PER_SEC;
	}

out:
	return nanosleep(&Δ, rem);
}

#endif