]> git.scottworley.com Git - tl-append/blob - tl-append.c
A space for test & implementation to share code
[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 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
30 int 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 }