]> git.scottworley.com Git - tl-append/blame - tl-append.c
A space for test & implementation to share code
[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) {
21 FILE *f = fopen(FILENAME, "a");
22 if (f == NULL)
23 die_err("Error opening output file");
24 if (fputs(line, f) == EOF)
25 die("Error writing to output file");
26 if (fclose(f) != 0)
27 die_err("Error closing output file");
28}
29
30int main() {
31 char buf[BUF_SIZE];
32 for (read_line(buf); buf[0]; read_line(buf)) {
33 write_line(buf);
34 }
35 return 0;
36}