aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 21:47:17 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 21:47:17 +0000
commitf57674e9a9b0b29102981e1694b76170b78dfb43 (patch)
tree86dd9ea4d8b1bd757609849977321bbef83a2c6f
parent907a240b1c4bbc65f9010f5b2fd31157f9d083c3 (diff)
downloadbusybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.tar.gz
busybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.tar.bz2
busybox-w32-f57674e9a9b0b29102981e1694b76170b78dfb43.zip
Rewrite of xargs by Vladimir N. Oleynik
-rw-r--r--AUTHORS5
-rw-r--r--docs/busybox_footer.pod4
-rw-r--r--findutils/xargs.c146
-rw-r--r--include/usage.h8
4 files changed, 95 insertions, 68 deletions
diff --git a/AUTHORS b/AUTHORS
index b6dadf44a..6cd8ab80f 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -55,7 +55,7 @@ Glenn McGrath <bug1@optushome.com.au>
55 ar, dpkg, dpkg-deb 55 ar, dpkg, dpkg-deb
56 56
57Vladimir Oleynik <dzo@simtreas.ru> 57Vladimir Oleynik <dzo@simtreas.ru>
58 cmdedit; ports: ash, crond, stty, traceroute, telnetd, top; 58 cmdedit; xargs(current); ports: ash, crond, stty, traceroute, telnetd, top;
59 locale, various fixes 59 locale, various fixes
60 and irreconcilable critic of everything not perfect. 60 and irreconcilable critic of everything not perfect.
61 61
@@ -81,7 +81,8 @@ Linus Torvalds <torvalds@transmeta.com>
81 mkswap, fsck.minix, mkfs.minix 81 mkswap, fsck.minix, mkfs.minix
82 82
83Mark Whitley <markw@lineo.com> <markw@codepoet.org> 83Mark Whitley <markw@lineo.com> <markw@codepoet.org>
84 grep, sed, cut, xargs, style-guide, new-applet-HOWTO, bug fixes, etc. 84 grep, sed, cut, xargs(previous),
85 style-guide, new-applet-HOWTO, bug fixes, etc.
85 86
86Charles P. Wright <cpwright@villagenet.com> 87Charles P. Wright <cpwright@villagenet.com>
87 gzip, mini-netcat(nc) 88 gzip, mini-netcat(nc)
diff --git a/docs/busybox_footer.pod b/docs/busybox_footer.pod
index 96d074f7f..1506768c4 100644
--- a/docs/busybox_footer.pod
+++ b/docs/busybox_footer.pod
@@ -113,7 +113,7 @@ Glenn McGrath <bug1@netconnect.com.au>
113 113
114Vladimir Oleynik <dzo@simtreas.ru> 114Vladimir Oleynik <dzo@simtreas.ru>
115 115
116 cmdedit; ports: ash, crond, stty, traceroute, telnetd, top; 116 cmdedit, xargs(current); ports: ash, crond, stty, traceroute, telnetd, top;
117 locale, various fixes 117 locale, various fixes
118 and irreconcilable critic of everything not perfect. 118 and irreconcilable critic of everything not perfect.
119 119
@@ -167,4 +167,4 @@ Enrique Zanardi <ezanardi@ull.es>
167 167
168=cut 168=cut
169 169
170# $Id: busybox_footer.pod,v 1.7 2002/10/22 12:24:56 andersen Exp $ 170# $Id: busybox_footer.pod,v 1.8 2002/11/10 21:47:15 bug1 Exp $
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 471bae45b..677618dc0 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -1,9 +1,10 @@
1/* 1/*
2 * Mini xargs implementation for busybox 2 * Mini xargs implementation for busybox
3 * Only "-prt" options are supported in this version of xargs.
3 * 4 *
4 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen 5 * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> 6 *
6 * Remixed by Mark Whitley <markw@codepoet.org> 7 * Special thanks Mark Whitley for stimul to rewrote :)
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -23,84 +24,105 @@
23 24
24#include <stdio.h> 25#include <stdio.h>
25#include <stdlib.h> 26#include <stdlib.h>
26#include <string.h> 27#include <unistd.h>
28#include <getopt.h>
29#include <errno.h>
30#include <sys/types.h>
31#include <sys/wait.h>
27#include "busybox.h" 32#include "busybox.h"
28 33
34
35/*
36 This function have special algorithm.
37 Don`t use fork and include to main!
38*/
39static void xargs_exec(char * const * args)
40{
41 int p;
42 int common[4]; /* shared vfork stack */
43
44 common[0] = 0;
45 if ((p = vfork()) >= 0) {
46 if (p == 0) {
47 /* vfork -- child */
48 execvp(args[0], args);
49 common[0] = errno; /* set error to shared stack */
50 _exit(1);
51 } else {
52 /* vfork -- parent */
53 wait(NULL);
54 if(common[0]) {
55 errno = common[0];
56 perror_msg_and_die("%s", args[0]);
57 }
58 }
59 } else {
60 perror_msg_and_die("vfork");
61 }
62}
63
29int xargs_main(int argc, char **argv) 64int xargs_main(int argc, char **argv)
30{ 65{
31 char *cmd_to_be_executed;
32 char *file_to_act_on; 66 char *file_to_act_on;
33 int i; 67 char **args;
34 int len; 68 int i, a;
69 char flg_vi = 0; /* verbose |& interactive */
70 char flg_no_empty = 0;
35 71
36 /* 72 while ((a = getopt(argc, argv, "prt")) > 0) {
37 * No options are supported in this version of xargs; no getopt. 73 switch(a) {
38 * 74 case 'p':
39 * Re: The missing -t flag: Most programs that produce output also print 75 flg_vi |= 3;
40 * the filename, so xargs doesn't really need to do it again. Supporting 76 break;
41 * the -t flag =greatly= bloats up the size of this app and the memory it 77 case 't':
42 * uses because you have to buffer all the input file strings in memory. If 78 flg_vi |= 1;
43 * you really want to see the filenames that xargs will act on, just run it 79 break;
44 * once with no args and xargs will echo the filename. Simple. 80 case 'r':
45 */ 81 flg_no_empty = 1;
82 break;
83 default:
84 show_usage();
85 }
86 }
46 87
47 argv++; 88 a = argc - optind;
48 len = argc; /* arg = count for ' ' + trailing '\0' */ 89 argv += optind;
49 /* Store the command to be executed (taken from the command line) */ 90 if(a==0) {
50 if (argc == 1) {
51 /* default behavior is to echo all the filenames */ 91 /* default behavior is to echo all the filenames */
52 argv[0] = "/bin/echo"; 92 *argv = "/bin/echo";
53 len++; /* space for trailing '\0' */ 93 a++;
54 } else {
55 argc--;
56 }
57 /* concatenate all the arguments passed to xargs together */
58 for (i = 0; i < argc; i++)
59 len += strlen(argv[i]);
60 cmd_to_be_executed = xmalloc (len);
61 for (i = len = 0; i < argc; i++) {
62 len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]);
63 } 94 }
95 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
96 args = xcalloc(a + 3, sizeof(char *));
97
98 /* Store the command to be executed (taken from the command line) */
99 for (i = 0; i < a; i++)
100 args[i] = *argv++;
64 101
65 /* Now, read in one line at a time from stdin, and store this 102 /* Now, read in one line at a time from stdin, and store this
66 * line to be used later as an argument to the command */ 103 * line to be used later as an argument to the command */
67 while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { 104 while ((file_to_act_on = get_line_from_file(stdin)) != NULL) {
68
69 FILE *cmd_output;
70 char *output_line;
71 char *execstr;
72
73 /* eat the newline off the filename. */ 105 /* eat the newline off the filename. */
74 chomp(file_to_act_on); 106 chomp(file_to_act_on);
75 107 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
76 /* eat blank lines */ 108 args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
77 if (file_to_act_on[0] == 0) 109 if(flg_vi) {
78 continue; 110 for(i=0; args[i]; i++) {
79 111 if(i)
80 /* assemble the command and execute it */ 112 fputc(' ', stderr);
81 bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on); 113 fputs(args[i], stderr);
82 114 }
83 cmd_output = popen(execstr, "r"); 115 fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n"));
84 if (cmd_output == NULL) 116 }
85 perror_msg_and_die("popen"); 117 if((flg_vi & 2) == 0 || ask_confirmation() != 0 ) {
86 118 xargs_exec(args);
87 /* harvest the output */ 119 }
88 while ((output_line = get_line_from_file(cmd_output)) != NULL) {
89 fputs(output_line, stdout);
90 free(output_line);
91 } 120 }
92
93 /* clean up */ 121 /* clean up */
94 pclose(cmd_output);
95 free(execstr);
96 free(file_to_act_on); 122 free(file_to_act_on);
97 } 123 }
98
99#ifdef CONFIG_FEATURE_CLEAN_UP 124#ifdef CONFIG_FEATURE_CLEAN_UP
100 free(cmd_to_be_executed); 125 free(args);
101#endif 126#endif
102
103 return 0; 127 return 0;
104} 128}
105
106/* vi: set sw=4 ts=4: */
diff --git a/include/usage.h b/include/usage.h
index d0aa3243f..c140a88ba 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2243,9 +2243,13 @@
2243 "Prints the user name associated with the current effective user id." 2243 "Prints the user name associated with the current effective user id."
2244 2244
2245#define xargs_trivial_usage \ 2245#define xargs_trivial_usage \
2246 "[COMMAND] [ARGS...]" 2246 "[COMMAND] [-prt] [ARGS...]"
2247#define xargs_full_usage \ 2247#define xargs_full_usage \
2248 "Executes COMMAND on every item given by standard input." 2248 "Executes COMMAND on every item given by standard input.\n\n" \
2249 "Options:\n" \
2250 "\t-p\tPrompt the user about whether to run each command\n" \
2251 "\t-r\tDo not run command for empty readed lines\n" \
2252 "\t-t\tPrint the command line on stderr before executing it."
2249#define xargs_example_usage \ 2253#define xargs_example_usage \
2250 "$ ls | xargs gzip\n" \ 2254 "$ ls | xargs gzip\n" \
2251 "$ find . -name '*.c' -print | xargs rm\n" 2255 "$ find . -name '*.c' -print | xargs rm\n"