]>
Commit | Line | Data |
---|---|---|
b3d5ed96 SW |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | ||
4 | const char *FILENAME = "tl.log"; | |
5 | const size_t BUF_SIZE = 1024; | |
6 | ||
7 | static void die(const char *message) { | |
8 | fputs(message, stderr); | |
9 | fputc('\n', stderr); | |
10 | exit(1); | |
11 | } | |
12 | ||
13 | static void die_err(const char *message) { | |
14 | perror(message); | |
15 | exit(1); | |
16 | } | |
17 | ||
18 | static void read_line(char *buf) { | |
19 | if (fgets(buf, BUF_SIZE, stdin) == NULL) { | |
20 | if (ferror(stdin)) | |
21 | die("I/O error reading line"); | |
22 | if (feof(stdin)) { | |
23 | buf[0] = '\0'; /* Unclear if fgets does this already */ | |
24 | return; | |
25 | } | |
26 | die("Unexpected error reading line"); | |
27 | } | |
28 | } | |
29 | ||
30 | static void write_line(const char *line) { | |
31 | FILE *f = fopen(FILENAME, "a"); | |
32 | if (f == NULL) | |
33 | die_err("Error opening output file"); | |
34 | if (fputs(line, f) == EOF) | |
35 | die("Error writing to output file"); | |
36 | if (fclose(f) != 0) | |
37 | die_err("Error closing output file"); | |
38 | } | |
39 | ||
40 | int main() { | |
41 | char buf[BUF_SIZE]; | |
42 | for (read_line(buf); buf[0]; read_line(buf)) { | |
43 | write_line(buf); | |
44 | } | |
45 | return 0; | |
46 | } |