From a07af81154e42e03968dcf3f4873b9a5ea9fec6e Mon Sep 17 00:00:00 2001 From: "Tim J. Robbins" Date: Sat, 13 Jul 2002 08:08:46 +0000 Subject: [PATCH] Account for space used by environment variables in a similar way to xargs(1) when handling -exec ... {} + constructions. --- usr.bin/find/function.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index 2cd35aec6520..66f92b12b9c8 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -70,6 +70,8 @@ static long long find_parsenum(PLAN *, const char *, char *, char *); static long long find_parsetime(PLAN *, const char *, char *); static char *nextarg(OPTION *, char ***); +extern char **environ; + #define COMPARE(a, b) do { \ switch (plan->flags & F_ELG_MASK) { \ case F_EQUAL: \ @@ -606,7 +608,7 @@ c_exec(option, argvp) PLAN *new; /* node returned */ long argmax; int cnt, i; - char **argv, **ap, *p; + char **argv, **ap, **ep, *p; /* XXX - was in c_execdir, but seems unnecessary!? ftsoptions &= ~FTS_NOSTAT; @@ -638,13 +640,15 @@ c_exec(option, argvp) warn("sysconf(_SC_ARG_MAX)"); argmax = _POSIX_ARG_MAX; } - /* - * Estimate the maximum number of arguments as {ARG_MAX}/10, - * and the maximum number of bytes to use for arguments as - * {ARG_MAX}*(3/4). - */ - new->e_pnummax = argmax / 10; - new->e_psizemax = (argmax / 4) * 3; + argmax -= 1024; + for (ep = environ; *ep != NULL; ep++) + argmax -= strlen(*ep) + 1 + sizeof(*ep); + argmax -= 1 + sizeof(*ep); + new->e_pnummax = argmax / 16; + argmax -= sizeof(char *) * new->e_pnummax; + if (argmax <= 0) + errx(1, "no space for arguments"); + new->e_psizemax = argmax; new->e_pbsize = 0; cnt += new->e_pnummax + 1; }