Brand new version of xargs. Tested thoroughly by Kent Robotti. (Domo arigato,

Mr. Robotti...) Closes bug #1065.
This commit is contained in:
Mark Whitley 2000-11-14 22:43:21 +00:00
parent 3c5ee9a379
commit e2e2c29ea1
2 changed files with 116 additions and 218 deletions

View File

@ -1,9 +1,9 @@
/* vi: set sw=4 ts=4: */
/* /*
* Mini xargs implementation for busybox * Mini xargs implementation for busybox
* *
* Copyright (C) 2000 by Lineo, inc. * Copyright (C) 2000 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* Remixed by Mark Whitley <markw@lineo.com>, <markw@enol.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -22,138 +22,87 @@
*/ */
#include "busybox.h" #include "busybox.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
int xargs_main(int argc, char **argv) int xargs_main(int argc, char **argv)
{ {
char *in_from_stdin = NULL;
char *args = NULL;
char *cmd_to_be_executed = NULL; char *cmd_to_be_executed = NULL;
char traceflag = 0; char *file_to_act_on = NULL;
int len_args=0, len_cmd_to_be_executed, opt;
pid_t pid;
int wpid, status;
/* Note that we do not use getopt here, since /*
* we only want to interpret initial options, * No options are supported in this version of xargs; no getopt.
* not options passed to commands */ *
while (--argc && **(++argv) == '-') { * Re: The missing -t flag: Most programs that produce output also print
while (*++(*argv)) { * the filename, so xargs doesn't really need to do it again. Supporting
switch (**argv) { * the -t flag =greatly= bloats up the size of this app and the memory it
case 't': * uses because you have to buffer all the input file strings in memory. If
traceflag=1; * you really want to see the filenames that xargs will act on, just run it
break; * once with no args and xargs will echo the filename. Simple.
default: */
fatalError(xargs_usage);
}
}
}
/* Store the command to be executed (taken from the command line) */ /* Store the command to be executed (taken from the command line) */
if (argc == 0) { if (argc == 1) {
len_cmd_to_be_executed=6; /* default behavior is to echo all the filenames */
cmd_to_be_executed = xmalloc(len_cmd_to_be_executed); cmd_to_be_executed = strdup("/bin/echo ");
strcat(cmd_to_be_executed, "echo");
} else { } else {
opt=strlen(*argv); /* concatenate all the arguments passed to xargs together */
len_cmd_to_be_executed = (opt > 10)? opt : 10; int i;
cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); int len = 1; /* for the '\0' */
strcat(cmd_to_be_executed, *argv); for (i = 1; i < argc; i++) {
len += strlen(argv[i]);
len += 1; /* for the space between the args */
cmd_to_be_executed = xrealloc(cmd_to_be_executed, len);
strcat(cmd_to_be_executed, argv[i]);
strcat(cmd_to_be_executed, " ");
}
} }
//args=xrealloc(args, len_args);
// strcpy(args, " ");
/* Now, read in one line at a time from stdin, and store this /* Now, read in one line at a time from stdin, and store this
* line to be used later as an argument to the command */ * line to be used later as an argument to the command */
in_from_stdin = get_line_from_file(stdin); while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
for (;in_from_stdin!=NULL;) {
char *tmp;
opt = strlen(in_from_stdin);
len_args += opt + 3;
args=xrealloc(args, len_args);
/* Strip out the final \n */ FILE *cmd_output = NULL;
in_from_stdin[opt-1]=' '; char *output_line = NULL;
char *execstr = NULL;
/* Replace any tabs with spaces */ /* eat the newline off the filename. */
while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) if (file_to_act_on[strlen(file_to_act_on)-1] == '\n')
*tmp=' '; file_to_act_on[strlen(file_to_act_on)-1] = '\0';
/* Strip out any extra intra-word spaces */ /* eat blank lines */
while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { if (strlen(file_to_act_on) == 0)
opt = strlen(in_from_stdin); continue;
memmove(tmp, tmp+1, opt-(tmp-in_from_stdin));
/* assemble the command and execute it */
execstr = xcalloc(strlen(cmd_to_be_executed) +
strlen(file_to_act_on) + 1, sizeof(char));
strcat(execstr, cmd_to_be_executed);
strcat(execstr, file_to_act_on);
cmd_output = popen(execstr, "r");
if (cmd_output == NULL) {
perror("popen");
exit(1);
} }
/* trim trailing whitespace */ /* harvest the output */
opt = strlen(in_from_stdin) - 1; while ((output_line = get_line_from_file(cmd_output)) != NULL) {
while (isspace(in_from_stdin[opt])) fputs(output_line, stdout);
opt--; free(output_line);
in_from_stdin[++opt] = 0; }
/* Strip out any leading whitespace */ /* clean up */
tmp=in_from_stdin; pclose(cmd_output);
while(isspace(*tmp)) free(execstr);
tmp++; free(file_to_act_on);
strcat(args, tmp);
strcat(args, " ");
free(in_from_stdin);
in_from_stdin = get_line_from_file(stdin);
} }
if ((pid = fork()) == 0) {
char *cmd[255];
int i=1;
if (traceflag==1) {
fprintf(stderr, "%s ", cmd_to_be_executed);
}
cmd[0] = cmd_to_be_executed;
while (--argc && ++argv && *argv ) {
if (traceflag==1) {
fprintf(stderr, "%s ", *argv);
}
cmd[i++]=*argv;
}
if (traceflag==1) {
fprintf(stderr, "%s\n", args);
}
cmd[i++] = args;
cmd[i] = NULL;
execvp(cmd_to_be_executed, cmd);
/* What? Still here? Exit with an error */
fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno));
}
/* Wait for a child process to exit */
wpid = wait(&status);
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
free(args);
free(cmd_to_be_executed); free(cmd_to_be_executed);
#endif #endif
if (wpid > 0) return 0;
return (WEXITSTATUS(status));
else
return EXIT_FAILURE;
} }
/*
Local Variables: /* vi: set sw=4 ts=4: */
c-file-style: "linux"
c-basic-offset: 4
tab-width: 4
End:
*/

167
xargs.c
View File

@ -1,9 +1,9 @@
/* vi: set sw=4 ts=4: */
/* /*
* Mini xargs implementation for busybox * Mini xargs implementation for busybox
* *
* Copyright (C) 2000 by Lineo, inc. * Copyright (C) 2000 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* Remixed by Mark Whitley <markw@lineo.com>, <markw@enol.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -22,138 +22,87 @@
*/ */
#include "busybox.h" #include "busybox.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
int xargs_main(int argc, char **argv) int xargs_main(int argc, char **argv)
{ {
char *in_from_stdin = NULL;
char *args = NULL;
char *cmd_to_be_executed = NULL; char *cmd_to_be_executed = NULL;
char traceflag = 0; char *file_to_act_on = NULL;
int len_args=0, len_cmd_to_be_executed, opt;
pid_t pid;
int wpid, status;
/* Note that we do not use getopt here, since /*
* we only want to interpret initial options, * No options are supported in this version of xargs; no getopt.
* not options passed to commands */ *
while (--argc && **(++argv) == '-') { * Re: The missing -t flag: Most programs that produce output also print
while (*++(*argv)) { * the filename, so xargs doesn't really need to do it again. Supporting
switch (**argv) { * the -t flag =greatly= bloats up the size of this app and the memory it
case 't': * uses because you have to buffer all the input file strings in memory. If
traceflag=1; * you really want to see the filenames that xargs will act on, just run it
break; * once with no args and xargs will echo the filename. Simple.
default: */
fatalError(xargs_usage);
}
}
}
/* Store the command to be executed (taken from the command line) */ /* Store the command to be executed (taken from the command line) */
if (argc == 0) { if (argc == 1) {
len_cmd_to_be_executed=6; /* default behavior is to echo all the filenames */
cmd_to_be_executed = xmalloc(len_cmd_to_be_executed); cmd_to_be_executed = strdup("/bin/echo ");
strcat(cmd_to_be_executed, "echo");
} else { } else {
opt=strlen(*argv); /* concatenate all the arguments passed to xargs together */
len_cmd_to_be_executed = (opt > 10)? opt : 10; int i;
cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); int len = 1; /* for the '\0' */
strcat(cmd_to_be_executed, *argv); for (i = 1; i < argc; i++) {
len += strlen(argv[i]);
len += 1; /* for the space between the args */
cmd_to_be_executed = xrealloc(cmd_to_be_executed, len);
strcat(cmd_to_be_executed, argv[i]);
strcat(cmd_to_be_executed, " ");
}
} }
//args=xrealloc(args, len_args);
// strcpy(args, " ");
/* Now, read in one line at a time from stdin, and store this /* Now, read in one line at a time from stdin, and store this
* line to be used later as an argument to the command */ * line to be used later as an argument to the command */
in_from_stdin = get_line_from_file(stdin); while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
for (;in_from_stdin!=NULL;) {
char *tmp;
opt = strlen(in_from_stdin);
len_args += opt + 3;
args=xrealloc(args, len_args);
/* Strip out the final \n */ FILE *cmd_output = NULL;
in_from_stdin[opt-1]=' '; char *output_line = NULL;
char *execstr = NULL;
/* Replace any tabs with spaces */ /* eat the newline off the filename. */
while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) if (file_to_act_on[strlen(file_to_act_on)-1] == '\n')
*tmp=' '; file_to_act_on[strlen(file_to_act_on)-1] = '\0';
/* Strip out any extra intra-word spaces */ /* eat blank lines */
while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { if (strlen(file_to_act_on) == 0)
opt = strlen(in_from_stdin); continue;
memmove(tmp, tmp+1, opt-(tmp-in_from_stdin));
/* assemble the command and execute it */
execstr = xcalloc(strlen(cmd_to_be_executed) +
strlen(file_to_act_on) + 1, sizeof(char));
strcat(execstr, cmd_to_be_executed);
strcat(execstr, file_to_act_on);
cmd_output = popen(execstr, "r");
if (cmd_output == NULL) {
perror("popen");
exit(1);
} }
/* trim trailing whitespace */ /* harvest the output */
opt = strlen(in_from_stdin) - 1; while ((output_line = get_line_from_file(cmd_output)) != NULL) {
while (isspace(in_from_stdin[opt])) fputs(output_line, stdout);
opt--; free(output_line);
in_from_stdin[++opt] = 0; }
/* Strip out any leading whitespace */ /* clean up */
tmp=in_from_stdin; pclose(cmd_output);
while(isspace(*tmp)) free(execstr);
tmp++; free(file_to_act_on);
strcat(args, tmp);
strcat(args, " ");
free(in_from_stdin);
in_from_stdin = get_line_from_file(stdin);
} }
if ((pid = fork()) == 0) {
char *cmd[255];
int i=1;
if (traceflag==1) {
fprintf(stderr, "%s ", cmd_to_be_executed);
}
cmd[0] = cmd_to_be_executed;
while (--argc && ++argv && *argv ) {
if (traceflag==1) {
fprintf(stderr, "%s ", *argv);
}
cmd[i++]=*argv;
}
if (traceflag==1) {
fprintf(stderr, "%s\n", args);
}
cmd[i++] = args;
cmd[i] = NULL;
execvp(cmd_to_be_executed, cmd);
/* What? Still here? Exit with an error */
fatalError("%s: %s\n", cmd_to_be_executed, strerror(errno));
}
/* Wait for a child process to exit */
wpid = wait(&status);
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
free(args);
free(cmd_to_be_executed); free(cmd_to_be_executed);
#endif #endif
if (wpid > 0) return 0;
return (WEXITSTATUS(status));
else
return EXIT_FAILURE;
} }
/*
Local Variables: /* vi: set sw=4 ts=4: */
c-file-style: "linux"
c-basic-offset: 4
tab-width: 4
End:
*/