Mercurial > hg > toybox
annotate toys/posix/xargs.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
rev | line source |
---|---|
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * xargs.c - Run command with arguments taken from stdin. |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * Copyright 2011 Rob Landley <rob@landley.net> |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 * |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/xargs.html |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
416
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
9 USE_XARGS(NEWTOY(xargs, "^I:E:L#ptxrn#<1s#0", TOYFLAG_USR|TOYFLAG_BIN)) |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 config XARGS |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 bool "xargs" |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 default y |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 help |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 usage: xargs [-ptxr0] [-s NUM] [-n NUM] [-L NUM] [-E STR] COMMAND... |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 Run command line one or more times, appending arguments from stdin. |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 |
416
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
19 If command exits with 255, don't launch another even if arguments remain. |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
20 |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 -s Size in bytes per command line |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 -n Max number of arguments per command |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 -0 Each argument is NULL terminated, no whitespace or quote processing |
416
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
24 #-p Prompt for y/n from tty before running each command |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
25 #-t Trace, print command line to stderr |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
26 #-x Exit if can't fit everything in one command |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
27 #-r Don't run command with empty input |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
28 #-L Max number of lines of input per command |
423 | 29 -E stop at line matching string |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 */ |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
32 #define FOR_xargs |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 #include "toys.h" |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
35 GLOBALS( |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 long max_bytes; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 long max_entries; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 long L; |
423 | 39 char *eofstr; |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 char *I; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 long entries, bytes; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 char delim; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 ) |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 // If out==NULL count TT.bytes and TT.entries, stopping at max. |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 // Otherwise, fill out out[] |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 // Returning NULL means need more data. |
423 | 50 // Returning char * means hit data limits, start of data left over |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 // Returning 1 means hit data limits, but consumed all data |
423 | 52 // Returning 2 means hit -E eofstr |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 static char *handle_entries(char *data, char **entry) |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 if (TT.delim) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 char *s = data; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 // Chop up whitespace delimited string into args |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 while (*s) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 char *save; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 while (isspace(*s)) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 if (entry) *s = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 s++; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 |
525 | 68 if (TT.max_entries && TT.entries >= TT.max_entries) |
416
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
69 return *s ? s : (char *)1; |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 if (!*s) break; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 save = s; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 for (;;) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 if (++TT.bytes >= TT.max_bytes && TT.max_bytes) return save; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 if (!*s || isspace(*s)) break; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 s++; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 } |
423 | 79 if (TT.eofstr) { |
80 int len = s-save; | |
81 if (len == strlen(TT.eofstr) && !strncmp(save, TT.eofstr, len)) | |
82 return (char *)2; | |
83 } | |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 if (entry) entry[TT.entries] = save; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 ++TT.entries; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 } |
525 | 87 |
88 // -0 support | |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 } else { |
525 | 90 TT.bytes += strlen(data)+1; |
91 if (TT.max_bytes && TT.bytes >= TT.max_bytes) return data; | |
92 if (TT.max_entries && TT.entries >= TT.max_entries) | |
93 return (char *)1; | |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 if (entry) entry[TT.entries] = data; |
525 | 95 TT.entries++; |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 return NULL; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 void xargs_main(void) |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 struct double_list *dlist = NULL; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 int entries, bytes, done = 0, status; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 char *data = NULL; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
107 if (!(toys.optflags & FLAG_0)) TT.delim = '\n'; |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 // If no optargs, call echo. |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 if (!toys.optc) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 free(toys.optargs); |
525 | 112 *(toys.optargs = xzalloc(2*sizeof(char *)))="echo"; |
113 toys.optc = 1; | |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 for (entries = 0, bytes = -1; entries < toys.optc; entries++, bytes++) |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 bytes += strlen(toys.optargs[entries]); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 // Loop through exec chunks. |
416
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
120 while (data || !done) { |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
121 char **out; |
e9f6e7f25854
More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents:
414
diff
changeset
|
122 |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 TT.entries = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 TT.bytes = bytes; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 // Loop reading input |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 for (;;) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 // Read line |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 if (!data) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 ssize_t l = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 l = getdelim(&data, (size_t *)&l, TT.delim, stdin); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 if (l<0) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 data = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 done++; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 break; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 dlist_add(&dlist, data); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 // Count data used |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 data = handle_entries(data, NULL); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 if (!data) continue; |
423 | 145 if (data == (char *)2) done++; |
146 if ((long)data <= 2) data = 0; | |
414
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 else data = xstrdup(data); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 break; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 // Accumulate cally thing |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 if (data && !TT.entries) error_exit("argument too long"); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 out = xzalloc((entries+TT.entries+1)*sizeof(char *)); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 if (dlist) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 struct double_list *dtemp; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 // Fill out command line to exec |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 memcpy(out, toys.optargs, entries*sizeof(char *)); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 TT.entries = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 TT.bytes = bytes; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 dlist->prev->next = 0; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 for (dtemp = dlist; dtemp; dtemp = dtemp->next) |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 handle_entries(dtemp->data, out+entries); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 pid_t pid=fork(); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 if (!pid) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 xclose(0); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 open("/dev/null", O_RDONLY); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
172 xexec(out); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
173 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
174 waitpid(pid, &status, 0); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
175 status = WEXITSTATUS(status); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
176 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
177 // Abritrary number of execs, can't just leak memory each time... |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
178 while (dlist) { |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
179 struct double_list *dtemp = dlist->next; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
180 |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
181 free(dlist->data); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
182 free(dlist); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
183 dlist = dtemp; |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
184 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
185 free(out); |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 } |
9204821e6b95
Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
187 } |