rewrite, so it should be firly clean now

This commit is contained in:
Eric Andersen 2000-09-23 06:10:14 +00:00
parent 5b17693f0a
commit a37d5b772b
2 changed files with 194 additions and 196 deletions

View File

@ -1,116 +1,115 @@
/* minix xargs - Make and execute commands /* vi: set sw=4 ts=4: */
* Author: Ian Nicholls: 1 Mar 90 */
/* /*
* xargs - Accept words from stdin until, combined with the arguments * Mini xargs implementation for busybox
* given on the command line, just fit into the command line limit.
* Then, execute the result.
* e.g. ls | xargs compress
* find . -name '*.s' -print | xargs ar qv libc.a
* *
* flags: -t Print the command just before it is run * Copyright (C) 2000 by Lineo, inc.
* -l len Use len as maximum line length (default 490, max 1023) * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* -e ending Append ending to the command before executing it.
* *
* Exits with: 0 No errors. * This program is free software; you can redistribute it and/or modify
* 1 If any system(3) call returns a nonzero status. * it under the terms of the GNU General Public License as published by
* 2 Usage error * the Free Software Foundation; either version 2 of the License, or
* 3 Line length too short to contain some single argument. * (at your option) any later version.
* *
* Examples: xargs ar qv libc.a < liborder # Create a new libc.a * This program is distributed in the hope that it will be useful,
* find . -name '*.s' -print | xargs rm # Remove all .s files * but WITHOUT ANY WARRANTY; without even the implied warranty of
* find . -type f ! -name '*.Z' \ # Compress old files. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* -atime +60 -print | xargs compress -v * General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* Bugs: If the command contains unquoted wildflags, then the system(3) call
* call may expand this to larger than the maximum line size.
* The command is not executed if nothing was read from stdin.
* xargs may give up too easily when the command returns nonzero.
*/ */
#define USAGE "usage: xargs [-t] [-l len] [-e endargs] command [args...]\n"
#include <errno.h> #include "internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h> #include <getopt.h>
#ifndef MAX_ARGLINE
# define MAX_ARGLINE 1023
#endif
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
char outlin[MAX_ARGLINE]; int xargs_main(int argc, char **argv)
char inlin[MAX_ARGLINE];
char startlin[MAX_ARGLINE];
char *ending = NULL;
char traceflag = 0;
int xargs_main(int ac, char **av)
{ {
int outlen, inlen, startlen, endlen=0, i; char *in_from_stdin = NULL;
char errflg = 0; char *args_from_cmdline = NULL;
int maxlin = MAX_ARGLINE; char *cmd_to_be_executed = NULL;
char traceflag = 0;
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
while ((i = getopt(ac, av, "tl:e:")) != EOF) while ((opt = getopt(argc, argv, "t")) != EOF) {
switch (i) { switch (opt) {
case 't': traceflag = 1; break; case 't':
case 'l': maxlin = min(MAX_ARGLINE, atoi(optarg)); break; traceflag=1;
case 'e': ending = optarg; break; break;
case '?': errflg++; break; default:
fatalError(xargs_usage);
} }
if (errflg) {
fprintf(stderr, USAGE);
exit(2);
} }
startlin[0] = 0; /* Store the command and arguments to be executed (from the command line) */
if (optind == ac) { if (optind == argc) {
strcat(startlin, "echo "); len_args_from_cmdline = 6;
args_from_cmdline = xmalloc(len_args_from_cmdline);
strcat(args_from_cmdline, "echo ");
} else {
opt=strlen(argv[optind]);
len_args_from_cmdline = (opt > 10)? opt : 10;
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
for (; optind < argc; optind++) {
if (strlen(argv[optind]) + strlen(args_from_cmdline) >
len_args_from_cmdline) {
len_args_from_cmdline += strlen(argv[optind]);
args_from_cmdline =
xrealloc(args_from_cmdline,
len_args_from_cmdline+1);
}
strcat(args_from_cmdline, argv[optind]);
strcat(args_from_cmdline, " ");
} }
else for ( ; optind < ac; optind++) {
strcat(startlin, av[optind]);
strcat(startlin, " ");
} }
startlen = strlen(startlin);
if (ending) endlen = strlen(ending);
maxlin = maxlin - 1 - endlen; /* Pre-compute */
strcpy(outlin, startlin); /* Set up some space for the command to be executed to be held in */
outlen = startlen; len_cmd_to_be_executed=10;
cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
strcpy(cmd_to_be_executed, args_from_cmdline);
strcat(cmd_to_be_executed, " ");
while (gets(inlin) != NULL) { /* Now, read in one line at a time from stdin, and run command+args on it */
inlen = strlen(inlin); in_from_stdin = get_line_from_file(stdin);
if (maxlin <= (outlen + inlen)) { for (;in_from_stdin!=NULL;) {
if (outlen == startlen) { len = strlen(in_from_stdin) + len_args_from_cmdline;
fprintf(stderr, "%s: Line length too short to process '%s'\n", if ( len > len_cmd_to_be_executed ) {
av[0], inlin); len_cmd_to_be_executed=len+3;
exit(3); cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed);
}
if (ending) strcat(outlin, ending);
if (traceflag) fputs(outlin,stderr);
errno = 0;
if (0 != system(outlin)) {
if (errno != 0) perror("xargs");
exit(1);
}
strcpy(outlin, startlin);
outlen = startlen;
}
strcat(outlin, inlin);
strcat(outlin, " ");
outlen = outlen + inlen + 1;
}
if (outlen != startlen) {
if (ending) strcat(outlin, ending);
if (traceflag) fputs(outlin,stderr);
errno = 0;
if (0 != system(outlin)) {
if (errno != 0) perror("xargs");
exit(1);
} }
strcat(cmd_to_be_executed, in_from_stdin);
strcat(cmd_to_be_executed+strlen(cmd_to_be_executed)-2, " ");
strcat(cmd_to_be_executed, " ");
free(in_from_stdin);
in_from_stdin = get_line_from_file(stdin);
} }
if (traceflag==1)
fputs(cmd_to_be_executed, stderr);
if ((system(cmd_to_be_executed) != 0) && (errno != 0))
fatalError("%s", strerror(errno));
#ifdef BB_FEATURE_CLEAN_UP
free(args_from_cmdline);
free(cmd_to_be_executed);
#endif
return 0; return 0;
} }
/*
Local Variables:
c-file-style: "linux"
c-basic-offset: 4
tab-width: 4
End:
*/

181
xargs.c
View File

@ -1,116 +1,115 @@
/* minix xargs - Make and execute commands /* vi: set sw=4 ts=4: */
* Author: Ian Nicholls: 1 Mar 90 */
/* /*
* xargs - Accept words from stdin until, combined with the arguments * Mini xargs implementation for busybox
* given on the command line, just fit into the command line limit.
* Then, execute the result.
* e.g. ls | xargs compress
* find . -name '*.s' -print | xargs ar qv libc.a
* *
* flags: -t Print the command just before it is run * Copyright (C) 2000 by Lineo, inc.
* -l len Use len as maximum line length (default 490, max 1023) * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* -e ending Append ending to the command before executing it.
* *
* Exits with: 0 No errors. * This program is free software; you can redistribute it and/or modify
* 1 If any system(3) call returns a nonzero status. * it under the terms of the GNU General Public License as published by
* 2 Usage error * the Free Software Foundation; either version 2 of the License, or
* 3 Line length too short to contain some single argument. * (at your option) any later version.
* *
* Examples: xargs ar qv libc.a < liborder # Create a new libc.a * This program is distributed in the hope that it will be useful,
* find . -name '*.s' -print | xargs rm # Remove all .s files * but WITHOUT ANY WARRANTY; without even the implied warranty of
* find . -type f ! -name '*.Z' \ # Compress old files. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* -atime +60 -print | xargs compress -v * General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* Bugs: If the command contains unquoted wildflags, then the system(3) call
* call may expand this to larger than the maximum line size.
* The command is not executed if nothing was read from stdin.
* xargs may give up too easily when the command returns nonzero.
*/ */
#define USAGE "usage: xargs [-t] [-l len] [-e endargs] command [args...]\n"
#include <errno.h> #include "internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h> #include <getopt.h>
#ifndef MAX_ARGLINE
# define MAX_ARGLINE 1023
#endif
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
char outlin[MAX_ARGLINE]; int xargs_main(int argc, char **argv)
char inlin[MAX_ARGLINE];
char startlin[MAX_ARGLINE];
char *ending = NULL;
char traceflag = 0;
int xargs_main(int ac, char **av)
{ {
int outlen, inlen, startlen, endlen=0, i; char *in_from_stdin = NULL;
char errflg = 0; char *args_from_cmdline = NULL;
int maxlin = MAX_ARGLINE; char *cmd_to_be_executed = NULL;
char traceflag = 0;
int len_args_from_cmdline, len_cmd_to_be_executed, len, opt;
while ((i = getopt(ac, av, "tl:e:")) != EOF) while ((opt = getopt(argc, argv, "t")) != EOF) {
switch (i) { switch (opt) {
case 't': traceflag = 1; break; case 't':
case 'l': maxlin = min(MAX_ARGLINE, atoi(optarg)); break; traceflag=1;
case 'e': ending = optarg; break; break;
case '?': errflg++; break; default:
fatalError(xargs_usage);
} }
if (errflg) {
fprintf(stderr, USAGE);
exit(2);
} }
startlin[0] = 0; /* Store the command and arguments to be executed (from the command line) */
if (optind == ac) { if (optind == argc) {
strcat(startlin, "echo "); len_args_from_cmdline = 6;
args_from_cmdline = xmalloc(len_args_from_cmdline);
strcat(args_from_cmdline, "echo ");
} else {
opt=strlen(argv[optind]);
len_args_from_cmdline = (opt > 10)? opt : 10;
args_from_cmdline = xcalloc(len_args_from_cmdline, sizeof(char));
for (; optind < argc; optind++) {
if (strlen(argv[optind]) + strlen(args_from_cmdline) >
len_args_from_cmdline) {
len_args_from_cmdline += strlen(argv[optind]);
args_from_cmdline =
xrealloc(args_from_cmdline,
len_args_from_cmdline+1);
}
strcat(args_from_cmdline, argv[optind]);
strcat(args_from_cmdline, " ");
} }
else for ( ; optind < ac; optind++) {
strcat(startlin, av[optind]);
strcat(startlin, " ");
} }
startlen = strlen(startlin);
if (ending) endlen = strlen(ending);
maxlin = maxlin - 1 - endlen; /* Pre-compute */
strcpy(outlin, startlin); /* Set up some space for the command to be executed to be held in */
outlen = startlen; len_cmd_to_be_executed=10;
cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char));
strcpy(cmd_to_be_executed, args_from_cmdline);
strcat(cmd_to_be_executed, " ");
while (gets(inlin) != NULL) { /* Now, read in one line at a time from stdin, and run command+args on it */
inlen = strlen(inlin); in_from_stdin = get_line_from_file(stdin);
if (maxlin <= (outlen + inlen)) { for (;in_from_stdin!=NULL;) {
if (outlen == startlen) { len = strlen(in_from_stdin) + len_args_from_cmdline;
fprintf(stderr, "%s: Line length too short to process '%s'\n", if ( len > len_cmd_to_be_executed ) {
av[0], inlin); len_cmd_to_be_executed=len+3;
exit(3); cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed);
}
if (ending) strcat(outlin, ending);
if (traceflag) fputs(outlin,stderr);
errno = 0;
if (0 != system(outlin)) {
if (errno != 0) perror("xargs");
exit(1);
}
strcpy(outlin, startlin);
outlen = startlen;
}
strcat(outlin, inlin);
strcat(outlin, " ");
outlen = outlen + inlen + 1;
}
if (outlen != startlen) {
if (ending) strcat(outlin, ending);
if (traceflag) fputs(outlin,stderr);
errno = 0;
if (0 != system(outlin)) {
if (errno != 0) perror("xargs");
exit(1);
} }
strcat(cmd_to_be_executed, in_from_stdin);
strcat(cmd_to_be_executed+strlen(cmd_to_be_executed)-2, " ");
strcat(cmd_to_be_executed, " ");
free(in_from_stdin);
in_from_stdin = get_line_from_file(stdin);
} }
if (traceflag==1)
fputs(cmd_to_be_executed, stderr);
if ((system(cmd_to_be_executed) != 0) && (errno != 0))
fatalError("%s", strerror(errno));
#ifdef BB_FEATURE_CLEAN_UP
free(args_from_cmdline);
free(cmd_to_be_executed);
#endif
return 0; return 0;
} }
/*
Local Variables:
c-file-style: "linux"
c-basic-offset: 4
tab-width: 4
End:
*/