summaryrefslogtreecommitdiff
path: root/src/predate.c
blob: 6512d20b6cc59a113b94582caaed7f973a747b7e (plain)
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
#include <sys/types.h>
#include <unistd.h>

#include <time.h>

#include "buffer.h"
#include "exit.h"
#include "fd.h"
#include "fmt.h"
#include "logmsg.h"
#include "sig.h"
#include "wait.h"

#include "datetime.h"

#define WHO "predate"

static char *montab[12] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

char num[FMT_ULONG];
char outbuf[1024];

int main(int argc, char **argv)
{
  time_t now;
  struct tm *tm;
  struct datetime dt;
  datetime_sec utc;
  datetime_sec local;
  int minutes;
  int pi[2];
  buffer bo;
  int wstat;
  int pid;

  sig_pipeignore();

  if (!argv[1]) logmsg(WHO, 100, USAGE, "predate child");

  if (pipe(pi) == -1) logmsg(WHO, 111, FATAL, "unable to create pipe");

  switch (pid = fork()) {
    case -1: logmsg(WHO, 111, FATAL, "unable to fork");
    case 0:
      close(pi[1]);
      if (fd_move(0, pi[0]) == -1) logmsg(WHO, 111, FATAL, "unable to set up fds");
      sig_pipedefault();
      execvp(argv[1], argv + 1);
      logmsg(WHO, 111, FATAL, B("unable to run: ", argv[1]));
  }
  close(pi[0]);
  buffer_init(&bo, write, pi[1], outbuf, sizeof(outbuf));

  time(&now);

  tm = gmtime(&now);
  dt.year = tm->tm_year;
  dt.mon = tm->tm_mon;
  dt.mday = tm->tm_mday;
  dt.hour = tm->tm_hour;
  dt.min = tm->tm_min;
  dt.sec = tm->tm_sec;
  utc = datetime_untai(&dt); /* utc == now, if gmtime ignores leap seconds */

  tm = localtime(&now);
  dt.year = tm->tm_year;
  dt.mon = tm->tm_mon;
  dt.mday = tm->tm_mday;
  dt.hour = tm->tm_hour;
  dt.min = tm->tm_min;
  dt.sec = tm->tm_sec;
  local = datetime_untai(&dt);

  buffer_puts(&bo, "Date: ");
  buffer_put(&bo, num, fmt_uint(num, dt.mday));
  buffer_puts(&bo, " ");
  buffer_puts(&bo, montab[dt.mon]);
  buffer_puts(&bo, " ");
  buffer_put(&bo, num, fmt_uint(num, dt.year + 1900));
  buffer_puts(&bo, " ");
  buffer_put(&bo, num, fmt_uint0(num, dt.hour, 2));
  buffer_puts(&bo, ":");
  buffer_put(&bo, num, fmt_uint0(num, dt.min, 2));
  buffer_puts(&bo, ":");
  buffer_put(&bo, num, fmt_uint0(num, dt.sec, 2));

  if (local < utc) {
    minutes = (utc - local + 30) / 60;
    buffer_puts(&bo, " -");
    buffer_put(&bo, num, fmt_uint0(num, minutes / 60, 2));
    buffer_put(&bo, num, fmt_uint0(num, minutes % 60, 2));
  } else {
    minutes = (local - utc + 30) / 60;
    buffer_puts(&bo, " +");
    buffer_put(&bo, num, fmt_uint0(num, minutes / 60, 2));
    buffer_put(&bo, num, fmt_uint0(num, minutes % 60, 2));
  }

  buffer_puts(&bo, "\n");
  buffer_copy(&bo, buffer_0);
  buffer_flush(&bo);
  close(pi[1]);

  if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed");
  if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed");
  _exit(wait_exitcode(wstat));
}