]>
Commit | Line | Data |
---|---|---|
1 | #define _POSIX_C_SOURCE 2 | |
2 | #include <errno.h> | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
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 remove_logfile() { | |
19 | if (remove("tl.log") != 0) { | |
20 | if (errno != ENOENT) { | |
21 | die_err("Error removing log file"); | |
22 | } | |
23 | } | |
24 | } | |
25 | ||
26 | static void write_to_tl_append(const char *content) { | |
27 | FILE *p = popen("./tl-append", "w"); | |
28 | if (p == NULL) | |
29 | die_err("Couldn't run tl-append"); | |
30 | if (fputs(content, p) == EOF) | |
31 | die("Couldn't write to pipe"); | |
32 | int status = pclose(p); | |
33 | if (status < 0) | |
34 | die_err("Error closing pipe"); | |
35 | if (status != 0) | |
36 | die("tl-append exited abnormally"); | |
37 | } | |
38 | ||
39 | static void verify_log_contents(const char *contents) { | |
40 | char buf[10]; | |
41 | ||
42 | FILE *f = fopen("tl.log", "r"); | |
43 | if (f == NULL) | |
44 | die_err("Error opening log file"); | |
45 | buf[fread(buf, 1, sizeof(buf), f)] = '\0'; | |
46 | if (ferror(f)) | |
47 | die("Error reading log file"); | |
48 | if (strncmp(contents, buf, sizeof(buf)) != 0) | |
49 | die("Wrong contents in log file"); | |
50 | if (fclose(f) != 0) | |
51 | die_err("Error closing log file"); | |
52 | } | |
53 | ||
54 | static void write_and_read_line() { | |
55 | remove_logfile(); | |
56 | write_to_tl_append("foo\n"); | |
57 | verify_log_contents("foo\n"); | |
58 | } | |
59 | ||
60 | static void write_and_read_two_lines() { | |
61 | remove_logfile(); | |
62 | write_to_tl_append("foo\n"); | |
63 | write_to_tl_append("bar\n"); | |
64 | verify_log_contents("foo\nbar\n"); | |
65 | } | |
66 | ||
67 | int main() { | |
68 | write_and_read_line(); | |
69 | write_and_read_two_lines(); | |
70 | } |