From 14c7e194422e7a459189ed6d631a15835a2cbbb8 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 1 May 2023 18:53:44 -0400 Subject: [PATCH] use n flag in xargs --- src/commands/xargs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/commands/xargs.c b/src/commands/xargs.c index 3b96bb6..01fb6ce 100644 --- a/src/commands/xargs.c +++ b/src/commands/xargs.c @@ -47,6 +47,7 @@ static int short_arg(char c, char* next) { if (n < 1) { error("error: max arg count must be at least 1"); } + flags.max_args = n; return ARG_USED; } return ARG_UNUSED; @@ -54,7 +55,7 @@ static int short_arg(char c, char* next) { char* read_next(FILE* file, int arg_count) { - if (arg_count == flags.max_args) return NULL; + if (flags.max_args != -1 && arg_count == flags.max_args) return NULL; int size = 0; int capacity = 8; @@ -83,12 +84,14 @@ char* read_next(FILE* file, int arg_count) { void read_args(FILE* file, char*** args, int* size, int* capacity) { char* arg; - while (arg = read_next(file, *size), true) { + static int read = 0; + while (arg = read_next(file, read), true) { if (*size == *capacity) { *capacity *= 2; *args = realloc(*args, sizeof(char*) * *capacity); } (*args)[(*size)++] = arg; + read++; if (arg == NULL) break; } }