diff options
author | Juli Mallett <jmallett@FreeBSD.org> | 2002-05-03 17:41:54 +0000 |
---|---|---|
committer | Juli Mallett <jmallett@FreeBSD.org> | 2002-05-03 17:41:54 +0000 |
commit | 263dc775f2e0a79a7e17bbfeff8e8d5729922b65 (patch) | |
tree | 1b57377730cad0c42c762e677db296f8b4a94fc8 /usr.bin/xargs | |
parent | 0d84b47e9cbf5a6a2ebab6ff9679ffa6eb5e5db2 (diff) | |
download | src-263dc775f2e0a79a7e17bbfeff8e8d5729922b65.tar.gz src-263dc775f2e0a79a7e17bbfeff8e8d5729922b65.zip |
Move substitution out to a function, prerun(), and replace use of run() with
it. It handles everything (right now) that needs done before run(), except
the -J case, because while that would be easy, I don't quite understand -J.
Reviewed by: src/tools/regression/usr.bin/xargs
Notes
Notes:
svn path=/head/; revision=95990
Diffstat (limited to 'usr.bin/xargs')
-rw-r--r-- | usr.bin/xargs/xargs.c | 163 |
1 files changed, 78 insertions, 85 deletions
diff --git a/usr.bin/xargs/xargs.c b/usr.bin/xargs/xargs.c index ea1e3f1c3d29..3cef98a3cb10 100644 --- a/usr.bin/xargs/xargs.c +++ b/usr.bin/xargs/xargs.c @@ -63,6 +63,7 @@ __FBSDID("$FreeBSD$"); #include "pathnames.h" +static void prerun(int, char **, int, const char *, char **, char **); static void run(char **); static void usage(void); void strnsubst(char **, const char *, const char *, size_t); @@ -301,89 +302,7 @@ arg2: for (avj = argv; *avj; avj++) *xp++ = *avj; } - if (Iflag) { - char **tmp, **tmp2; - size_t repls; - int iter; - - /* - * Set up some locals, the number of - * times we may replace replstr with a - * line of input, a modifiable pointer - * to the head of the original argument - * list, and the number of iterations to - * perform -- the number of arguments. - */ - repls = Rflag; - avj = av; - iter = argc; - - /* - * Allocate memory to hold the argument - * list. - */ - tmp = malloc(linelen * sizeof(char **)); - if (tmp == NULL) - err(1, "malloc"); - tmp2 = tmp; - /* - * Just save the first argument, as it - * is the utility name, and we cannot - * be trusted to do strnsubst() to it. - */ - *tmp++ = strdup(*avj++); - /* - * Now for every argument to utility, - * if we have not used up the number of - * replacements we are allowed to do, and - * if the argument contains at least one - * occurance of replstr, call strnsubst(), - * or else just save the string. - * Iterations over elements of avj and tmp - * are done where appropriate. - */ - while (--iter) { - *tmp = *avj++; - if (repls && strstr(*tmp, replstr) != NULL) { - strnsubst(tmp++, replstr, inpline, - (size_t)255); - repls--; - } else { - if ((*tmp = strdup(*tmp)) == NULL) - err(1, "strdup"); - tmp++; - } - } - /* - * NULL terminate the list of arguments, - * for run(). - */ - *tmp = *xp = NULL; - run(tmp2); - /* - * From the tail to the head, free along - * the way. - */ - for (; tmp2 != tmp; tmp--) - free(*tmp); - /* - * Free the list. - */ - free(tmp2); - /* - * Free the input line buffer, and create - * a new dummy. - */ - free(inpline); - inpline = strdup(""); - } else { - /* - * Mark the tail of the argument list with - * a NULL, and run() with it. - */ - *xp = NULL; - run(av); - } + prerun(argc, av, Rflag, replstr, xp, &inpline); if (ch == EOF || foundeof) exit(rval); p = bbp; @@ -429,8 +348,7 @@ addch: if (p < ebp) { for (avj = argv; *avj; avj++) *xp++ = *avj; } - *xp = NULL; - run(av); + prerun(argc, av, Rflag, replstr, xp, &inpline); xp = bxp; cnt = ebp - argp; memcpy(bbp, argp, (size_t)cnt); @@ -441,6 +359,81 @@ addch: if (p < ebp) { /* NOTREACHED */ } +/* + * Do things necessary before run()'ing, such as -I substitution, + * and then call run(). + */ +static void +prerun(int argc, char **argv, int repls, const char *replstr, char **xp, char **inpline) +{ + char **tmp, **tmp2, **avj; + + if (repls == 0) { + *xp = NULL; + run(argv); + return; + } + + avj = argv; + + /* + * Allocate memory to hold the argument list, and + * a NULL at the tail. + */ + tmp = calloc(argc + 1, sizeof(char**)); + if (tmp == NULL) + err(1, "calloc"); + tmp2 = tmp; + + /* + * Save the first argument and iterate over it, we + * cannot do strnsubst() to it. + */ + if ((*tmp++ = strdup(*avj++)) == NULL) + err(1, "strdup"); + + /* + * For each argument to utility, if we have not used up + * the number of replacements we are allowed to do, and + * if the argument contains at least one occurance of + * replstr, call strnsubst(), else just save the string. + * Iterations over elements of avj and tmp are done + * where appropriate. + */ + while (--argc) { + *tmp = *avj++; + if (repls && strstr(*tmp, replstr) != NULL) { + strnsubst(tmp++, replstr, *inpline, (size_t)255); + repls--; + } else { + if ((*tmp = strdup(*tmp)) == NULL) + err(1, "strdup"); + tmp++; + } + } + + /* + * Run it. + */ + run(tmp2); + + /* + * Walk from the tail to the head, free along the way. + */ + for (; tmp2 != tmp; tmp--) + free(*tmp); + /* + * Now free the list itself. + */ + free(tmp2); + + /* + * Free the input line buffer, and create a new dummy. + */ + free(*inpline); + *inpline = strdup(""); +} + static void run(char **argv) { |