From b3d5ed960aa9a43df2b48894867ec3481cee77fc Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Wed, 30 Aug 2023 22:25:58 -0700 Subject: [PATCH] Begin --- .gitignore | 2 ++ Makefile | 17 +++++++++++++++++ tl-append-test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ tl-append.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 tl-append-test.c create mode 100644 tl-append.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dad8cbc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +tl-append +tl.log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8209ef8 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +prefix = /usr/local +bindir = $(prefix)/bin +INSTALL = install + +tl-append: + +%: %.c + $(CC) -Wall -Wextra -pedantic -o $@ $^ + +.PHONY: check +check: tl-append tl-append-test + ./tl-append-test + +.PHONY: install +install: tl-append + $(INSTALL) -d $(DESTDIR)$(bindir) + $(INSTALL) -m 755 $^ $(DESTDIR)$(bindir) diff --git a/tl-append-test.c b/tl-append-test.c new file mode 100644 index 0000000..be2adcb --- /dev/null +++ b/tl-append-test.c @@ -0,0 +1,42 @@ +#define _POSIX_C_SOURCE 2 +#include +#include +#include + +static void die(const char *message) { + fputs(message, stderr); + fputc('\n', stderr); + exit(1); +} + +static void die_err(const char *message) { + perror(message); + exit(1); +} + +static void write_and_read_line() { + char buf[10]; + + FILE *p = popen("./tl-append", "w"); + if (p == NULL) + die_err("Couldn't run tl-append"); + if (fputs("foo\n", p) == EOF) + die("Couldn't write to pipe"); + int status = pclose(p); + if (status < 0) + die_err("Error closing pipe"); + if (status != 0) + die("tl-append exited abnormally"); + + FILE *f = fopen("tl.log", "r"); + if (f == NULL) + die_err("Error opening log file"); + if (fgets(buf, sizeof(buf), f) == NULL) + die("Error reading log file"); + if (strncmp("foo\n", buf, sizeof(buf)) != 0) + die("Wrong contents in log file"); + if (fclose(f) != 0) + die_err("Error closing log file"); +} + +int main() { write_and_read_line(); } diff --git a/tl-append.c b/tl-append.c new file mode 100644 index 0000000..21e23a6 --- /dev/null +++ b/tl-append.c @@ -0,0 +1,46 @@ +#include +#include + +const char *FILENAME = "tl.log"; +const size_t BUF_SIZE = 1024; + +static void die(const char *message) { + fputs(message, stderr); + fputc('\n', stderr); + exit(1); +} + +static void die_err(const char *message) { + perror(message); + exit(1); +} + +static void read_line(char *buf) { + if (fgets(buf, BUF_SIZE, stdin) == NULL) { + if (ferror(stdin)) + die("I/O error reading line"); + if (feof(stdin)) { + buf[0] = '\0'; /* Unclear if fgets does this already */ + return; + } + die("Unexpected error reading line"); + } +} + +static void write_line(const char *line) { + FILE *f = fopen(FILENAME, "a"); + if (f == NULL) + die_err("Error opening output file"); + if (fputs(line, f) == EOF) + die("Error writing to output file"); + if (fclose(f) != 0) + die_err("Error closing output file"); +} + +int main() { + char buf[BUF_SIZE]; + for (read_line(buf); buf[0]; read_line(buf)) { + write_line(buf); + } + return 0; +} -- 2.44.1