8 time_t real_seconds(void) {
10 int s = clock_gettime(CLOCK_REALTIME, &t);
15 /* Subtract the `struct timeval' values X and Y,
16 storing the result in RESULT.
17 Return 1 if the difference is negative, otherwise 0. */
18 int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
19 /* Perform the carry for the later subtraction by updating y. */
20 if (x->tv_usec < y->tv_usec) {
21 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
22 y->tv_usec -= 1000000 * nsec;
25 if (x->tv_usec - y->tv_usec > 1000000) {
26 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
27 y->tv_usec += 1000000 * nsec;
31 /* Compute the time remaining to wait.
32 tv_usec is certainly positive. */
33 result->tv_sec = x->tv_sec - y->tv_sec;
34 result->tv_usec = x->tv_usec - y->tv_usec;
36 /* Return 1 if result is negative. */
37 return x->tv_sec < y->tv_sec;
40 void sleep_until(time_t dst_sec) {
44 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &dst, NULL)) /* nothing */;
47 time_t round_up(time_t t, int align) {
48 t = t - 1; /* counter bad +t for aligned things in last step */
49 t = t - t%align; /* round down to align */
50 t = t + align; /* add alignment to make it a round up */
54 void handle_connection(int s) {
59 if (!fgets(line_in, 100, stdin)) return;
60 char *p = line_in + strlen("GET /");
63 printf("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<!--");
65 char *spaaaace = malloc(1024*64);
66 memset(spaaaace, ' ', 1024*64);
69 time_t t = round_up(real_seconds(), 4);
71 if (*p != '0' && *p != '1') return;
74 if (fwrite(spaaaace, 1024, 64, stdout) <= 0) return;
75 t += (*p == '1') ? 2 : 4;
81 int s = netopen_server(NULL, "4422", JH_TCP_HINTS);
83 int s_ = accept(s, NULL, NULL);
86 handle_connection(s_);