use n flag in xargs

This commit is contained in:
Murphy 2023-05-01 18:53:44 -04:00
parent 82e55dde69
commit 14c7e19442

View file

@ -47,6 +47,7 @@ static int short_arg(char c, char* next) {
if (n < 1) { if (n < 1) {
error("error: max arg count must be at least 1"); error("error: max arg count must be at least 1");
} }
flags.max_args = n;
return ARG_USED; return ARG_USED;
} }
return ARG_UNUSED; return ARG_UNUSED;
@ -54,7 +55,7 @@ static int short_arg(char c, char* next) {
char* read_next(FILE* file, int arg_count) { 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 size = 0;
int capacity = 8; 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) { void read_args(FILE* file, char*** args, int* size, int* capacity) {
char* arg; char* arg;
while (arg = read_next(file, *size), true) { static int read = 0;
while (arg = read_next(file, read), true) {
if (*size == *capacity) { if (*size == *capacity) {
*capacity *= 2; *capacity *= 2;
*args = realloc(*args, sizeof(char*) * *capacity); *args = realloc(*args, sizeof(char*) * *capacity);
} }
(*args)[(*size)++] = arg; (*args)[(*size)++] = arg;
read++;
if (arg == NULL) break; if (arg == NULL) break;
} }
} }