#include <err.h>
+#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
static struct tm displayed;
+static int statefd = -1;
+
static void
dummy_out(bool state)
{
fflush(stdout);
}
}
-
static void
-pulse()
+record_tick()
{
- struct timespec const ts = { 0, pulsewidth };
+ ssize_t ret;
+ char buf[10];
- dummy_out(true);
- clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
- dummy_out(false);
displayed.tm_sec += 30;
while (displayed.tm_sec >= 60) {
displayed.tm_min++;
displayed.tm_min -= 60;
}
displayed.tm_hour %= 12;
- printf(" (%d:%02d:%02d)\n",
- displayed.tm_hour, displayed.tm_min, displayed.tm_sec);
- fflush(stdout);
+ sprintf(buf, "%2d:%02d:%02d\n",
+ displayed.tm_hour, displayed.tm_min, displayed.tm_sec);
+ if (statefd != -1) {
+ ret = pwrite(statefd, buf, 9, 0);
+ if (ret == -1)
+ err(1, "write to state file");
+ if (ret != 9)
+ errx(1, "short write to state file");
+ }
+}
+
+static void
+pulse()
+{
+ struct timespec const ts = { 0, pulsewidth };
+
+ dummy_out(true);
+ record_tick();
+ clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
+ dummy_out(false);
}
static void
if (localtime_r(&ts.tv_sec, &displayed) == NULL)
err(1, "localtime_r");
displayed.tm_sec = (displayed.tm_sec >= 30) ? 30 : 0;
- while ((opt = getopt(argc, argv, "s:")) != -1) {
+ while ((opt = getopt(argc, argv, "f:s:")) != -1) {
switch (opt) {
+ case 'f':
+ statefd =
+ open(optarg, O_RDWR | O_CREAT | O_DSYNC, 0666);
+ if (statefd == -1)
+ err(1, "%s", optarg);
+ break;
case 's':
if (sscanf(optarg, "%d:%d:%d", &displayed.tm_hour,
&displayed.tm_min, &displayed.tm_sec) != 3)