]> git.scottworley.com Git - tl-append/blame - tl-append.c
Write timestamps
[tl-append] / tl-append.c
CommitLineData
b3d5ed96
SW
1#include <stdio.h>
2#include <stdlib.h>
3
d522116b 4#include "common.h"
b3d5ed96 5
d522116b 6const size_t BUF_SIZE = 1024;
b3d5ed96
SW
7
8static 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
20static void write_line(const char *line) {
2998af81 21 const char *now = encode_time(time(NULL));
b3d5ed96
SW
22 FILE *f = fopen(FILENAME, "a");
23 if (f == NULL)
24 die_err("Error opening output file");
2998af81
SW
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");
b3d5ed96
SW
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
35int 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}