]> git.scottworley.com Git - tl-append/blob - tl-append.c
Write timestamps
[tl-append] / tl-append.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "common.h"
5
6 const size_t BUF_SIZE = 1024;
7
8 static void read_line(char *buf) {
9 if (fgets(buf, BUF_SIZE, stdin) == NULL) {
10 if (ferror(stdin))
11 die("I/O error reading line");
12 if (feof(stdin)) {
13 buf[0] = '\0'; /* Unclear if fgets does this already */
14 return;
15 }
16 die("Unexpected error reading line");
17 }
18 }
19
20 static void write_line(const char *line) {
21 const char *now = encode_time(time(NULL));
22 FILE *f = fopen(FILENAME, "a");
23 if (f == NULL)
24 die_err("Error opening output file");
25 if (fputs(now, f) == EOF)
26 die("Error writing to output file");
27 if (fputc(' ', f) == EOF)
28 die("Error writing to output file");
29 if (fputs(line, f) == EOF)
30 die("Error writing to output file");
31 if (fclose(f) != 0)
32 die_err("Error closing output file");
33 }
34
35 int main() {
36 char buf[BUF_SIZE];
37 for (read_line(buf); buf[0]; read_line(buf)) {
38 write_line(buf);
39 }
40 return 0;
41 }