From: Scott Worley Date: Thu, 31 Aug 2023 05:25:58 +0000 (-0700) Subject: Begin X-Git-Tag: v1.0.0~26 X-Git-Url: http://git.scottworley.com/tl-append/commitdiff_plain/b3d5ed960aa9a43df2b48894867ec3481cee77fc Begin --- b3d5ed960aa9a43df2b48894867ec3481cee77fc 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; +}