diff options
Diffstat (limited to 'src/commands/tee.c')
-rw-r--r-- | src/commands/tee.c | 20 |
1 files changed, 14 insertions, 6 deletions
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; } |