|
1 #include <sys/types.h> |
|
2 #include <time.h> |
|
3 #include "datetime.h" |
|
4 #include "fork.h" |
|
5 #include "wait.h" |
|
6 #include "fd.h" |
|
7 #include "fmt.h" |
|
8 #include "strerr.h" |
|
9 #include "substdio.h" |
|
10 #include "subfd.h" |
|
11 #include "readwrite.h" |
|
12 #include "exit.h" |
|
13 |
|
14 #define FATAL "predate: fatal: " |
|
15 |
|
16 static char *montab[12] = { |
|
17 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
|
18 }; |
|
19 |
|
20 char num[FMT_ULONG]; |
|
21 char outbuf[1024]; |
|
22 |
|
23 void main(argc,argv) |
|
24 int argc; |
|
25 char **argv; |
|
26 { |
|
27 time_t now; |
|
28 struct tm *tm; |
|
29 struct datetime dt; |
|
30 datetime_sec utc; |
|
31 datetime_sec local; |
|
32 int minutes; |
|
33 int pi[2]; |
|
34 substdio ss; |
|
35 int wstat; |
|
36 int pid; |
|
37 |
|
38 sig_pipeignore(); |
|
39 |
|
40 if (!argv[1]) |
|
41 strerr_die1x(100,"predate: usage: predate child"); |
|
42 |
|
43 if (pipe(pi) == -1) |
|
44 strerr_die2sys(111,FATAL,"unable to create pipe: "); |
|
45 |
|
46 switch(pid = fork()) { |
|
47 case -1: |
|
48 strerr_die2sys(111,FATAL,"unable to fork: "); |
|
49 case 0: |
|
50 close(pi[1]); |
|
51 if (fd_move(0,pi[0]) == -1) |
|
52 strerr_die2sys(111,FATAL,"unable to set up fds: "); |
|
53 sig_pipedefault(); |
|
54 execvp(argv[1],argv + 1); |
|
55 strerr_die4sys(111,FATAL,"unable to run ",argv[1],": "); |
|
56 } |
|
57 close(pi[0]); |
|
58 substdio_fdbuf(&ss,write,pi[1],outbuf,sizeof(outbuf)); |
|
59 |
|
60 time(&now); |
|
61 |
|
62 tm = gmtime(&now); |
|
63 dt.year = tm->tm_year; |
|
64 dt.mon = tm->tm_mon; |
|
65 dt.mday = tm->tm_mday; |
|
66 dt.hour = tm->tm_hour; |
|
67 dt.min = tm->tm_min; |
|
68 dt.sec = tm->tm_sec; |
|
69 utc = datetime_untai(&dt); /* utc == now, if gmtime ignores leap seconds */ |
|
70 |
|
71 tm = localtime(&now); |
|
72 dt.year = tm->tm_year; |
|
73 dt.mon = tm->tm_mon; |
|
74 dt.mday = tm->tm_mday; |
|
75 dt.hour = tm->tm_hour; |
|
76 dt.min = tm->tm_min; |
|
77 dt.sec = tm->tm_sec; |
|
78 local = datetime_untai(&dt); |
|
79 |
|
80 substdio_puts(&ss,"Date: "); |
|
81 substdio_put(&ss,num,fmt_uint(num,dt.mday)); |
|
82 substdio_puts(&ss," "); |
|
83 substdio_puts(&ss,montab[dt.mon]); |
|
84 substdio_puts(&ss," "); |
|
85 substdio_put(&ss,num,fmt_uint(num,dt.year + 1900)); |
|
86 substdio_puts(&ss," "); |
|
87 substdio_put(&ss,num,fmt_uint0(num,dt.hour,2)); |
|
88 substdio_puts(&ss,":"); |
|
89 substdio_put(&ss,num,fmt_uint0(num,dt.min,2)); |
|
90 substdio_puts(&ss,":"); |
|
91 substdio_put(&ss,num,fmt_uint0(num,dt.sec,2)); |
|
92 |
|
93 if (local < utc) { |
|
94 minutes = (utc - local + 30) / 60; |
|
95 substdio_puts(&ss," -"); |
|
96 substdio_put(&ss,num,fmt_uint0(num,minutes / 60,2)); |
|
97 substdio_put(&ss,num,fmt_uint0(num,minutes % 60,2)); |
|
98 } |
|
99 else { |
|
100 minutes = (local - utc + 30) / 60; |
|
101 substdio_puts(&ss," +"); |
|
102 substdio_put(&ss,num,fmt_uint0(num,minutes / 60,2)); |
|
103 substdio_put(&ss,num,fmt_uint0(num,minutes % 60,2)); |
|
104 } |
|
105 |
|
106 substdio_puts(&ss,"\n"); |
|
107 substdio_copy(&ss,subfdin); |
|
108 substdio_flush(&ss); |
|
109 close(pi[1]); |
|
110 |
|
111 if (wait_pid(&wstat,pid) == -1) |
|
112 strerr_die2sys(111,FATAL,"wait failed: "); |
|
113 if (wait_crashed(wstat)) |
|
114 strerr_die2x(111,FATAL,"child crashed"); |
|
115 _exit(wait_exitcode(wstat)); |
|
116 } |