From b1364be7e271c5a080e29efcda209a190a82d6d9 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Thu, 4 May 2023 16:10:37 -0400 Subject: ansii c --- src/commands/tee.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/commands/tee.c') diff --git a/src/commands/tee.c b/src/commands/tee.c index 652e369..b9b31be 100644 --- a/src/commands/tee.c +++ b/src/commands/tee.c @@ -17,16 +17,20 @@ static void help(void) { static void handle(int dummy){UNUSED(dummy);} -static void run_tee(int file_count, FILE* files[file_count]) { +static void run_tee(int file_count, FILE** files) { char c; + int i; + while((c = getchar()) != EOF) { - for (int i = 0; i < file_count; i++) { + int i; + for (i = 0; i < file_count; i++) { fwrite(&c, 1, 1, files[i]); fflush(files[i]); } putchar(c); } - for (int i = 0; i < file_count; i++) { + + for (i = 0; i < file_count; i++) { fclose(files[i]); } } @@ -48,10 +52,13 @@ static int short_arg(char c, char* next) { COMMAND(tee) { + int start, i; + FILE** files; + flags.append = false; flags.handle_sigint = false; - int start = parse_args(argc, argv, help, short_arg, NULL); + start = parse_args(argc, argv, help, short_arg, NULL); if (flags.handle_sigint) { signal(SIGINT, handle); @@ -62,8 +69,9 @@ COMMAND(tee) { return EXIT_SUCCESS; } - FILE* files[argc - start]; - for (int i = start; i < argc; i++) { + 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; } -- cgit v1.2.3-freya