From d8f2c10b7108fff6b7e437291093a1cadc15ab9f Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sat, 6 May 2023 00:39:44 -0400 Subject: refactor --- command/tee.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 command/tee.c (limited to 'command/tee.c') diff --git a/command/tee.c b/command/tee.c new file mode 100644 index 0000000..0462517 --- /dev/null +++ b/command/tee.c @@ -0,0 +1,83 @@ +#include "command.h" +#include "lslib.h" + +#include +#include + +static struct { + bool append; + bool handle_sigint; +} flags; + +static void help(void) { + printf("Usage: tee [-ai] [FILE]...\n\n"); + printf("Copy stdin to each FILE, and also to stdout\n\n"); + printf("\t-a Append to the given FILEs, don't overwrite\n"); + printf("\t-i Ignore interrupt signals (SIGINT)\n"); + exit(EXIT_SUCCESS); +} + +static void handle(int dummy){UNUSED(dummy);} + +static void run_tee(int file_count, FILE** files) { + char c; + int i; + + while((c = getchar()) != EOF) { + int i; + for (i = 0; i < file_count; i++) { + fwrite(&c, 1, 1, files[i]); + fflush(files[i]); + } + putchar(c); + } + + for (i = 0; i < file_count; i++) { + fclose(files[i]); + } +} + +static int short_arg(char c, char* next) { + UNUSED(next); + switch (c) { + case 'a': + flags.append = true; + break; + case 'i': + flags.handle_sigint = true; + break; + default: + return ARG_INVALID; + } + return ARG_UNUSED; +} + +COMMAND(tee) { + + int start, i; + FILE** files; + + flags.append = false; + flags.handle_sigint = false; + + start = parse_args(argc, argv, help, short_arg, NULL); + + if (flags.handle_sigint) { + signal(SIGINT, handle); + } + + if (argc - start < 1) { + run_tee(0, NULL); + return EXIT_SUCCESS; + } + + files = malloc(sizeof(FILE*) * (argc - start)); + + for (i = start; i < argc; i++) { + FILE* file = get_file(argv[i], flags.append ? "a" : "w"); + files[i - start] = file; + } + + run_tee(argc - start, files); + return EXIT_SUCCESS; +} -- cgit v1.2.3-freya