diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-04 16:10:37 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-04 16:10:37 -0400 |
commit | b1364be7e271c5a080e29efcda209a190a82d6d9 (patch) | |
tree | fc64d1546e59b5ed1c2c204612b6181bc401c27f /src/commands/tee.c | |
parent | grep (diff) | |
download | lazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.tar.gz lazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.tar.bz2 lazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.zip |
ansii c
Diffstat (limited to '')
-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; } |