]>
Commit | Line | Data |
---|---|---|
b3d5ed96 | 1 | #define _POSIX_C_SOURCE 2 |
27dfbfeb | 2 | #include <errno.h> |
b3d5ed96 SW |
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 | ||
27dfbfeb SW |
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 | ||
aacfb261 | 26 | static void write_to_tl_append(const char *content) { |
b3d5ed96 SW |
27 | FILE *p = popen("./tl-append", "w"); |
28 | if (p == NULL) | |
29 | die_err("Couldn't run tl-append"); | |
aacfb261 | 30 | if (fputs(content, p) == EOF) |
b3d5ed96 SW |
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"); | |
aacfb261 SW |
37 | } |
38 | ||
39 | static void verify_log_contents(const char *contents) { | |
7a92d9bf SW |
40 | size_t len = strlen(contents); |
41 | char *buf = (char *)malloc(len + 2); | |
b3d5ed96 SW |
42 | |
43 | FILE *f = fopen("tl.log", "r"); | |
44 | if (f == NULL) | |
45 | die_err("Error opening log file"); | |
7a92d9bf | 46 | buf[fread(buf, 1, len + 1, f)] = '\0'; |
27dfbfeb | 47 | if (ferror(f)) |
b3d5ed96 | 48 | die("Error reading log file"); |
7a92d9bf | 49 | if (strncmp(contents, buf, len + 1) != 0) |
b3d5ed96 SW |
50 | die("Wrong contents in log file"); |
51 | if (fclose(f) != 0) | |
52 | die_err("Error closing log file"); | |
7a92d9bf | 53 | free(buf); |
b3d5ed96 SW |
54 | } |
55 | ||
aacfb261 | 56 | static void write_and_read_line() { |
27dfbfeb | 57 | remove_logfile(); |
aacfb261 SW |
58 | write_to_tl_append("foo\n"); |
59 | verify_log_contents("foo\n"); | |
60 | } | |
61 | ||
27dfbfeb SW |
62 | static void write_and_read_two_lines() { |
63 | remove_logfile(); | |
64 | write_to_tl_append("foo\n"); | |
65 | write_to_tl_append("bar\n"); | |
66 | verify_log_contents("foo\nbar\n"); | |
67 | } | |
68 | ||
69 | int main() { | |
70 | write_and_read_line(); | |
71 | write_and_read_two_lines(); | |
72 | } |