annotate toys/xargs.c @ 582:b88bc7dcdb48

Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
author Rob Landley <rob@landley.net>
date Sun, 27 May 2012 00:56:17 -0500
parents f188e572acc7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
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
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #include "toys.h"
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
33
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
34 DEFINE_GLOBALS(
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
35 long max_bytes;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
36 long max_entries;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
37 long L;
423
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
38 char *eofstr;
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
39 char *I;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
40
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
41 long entries, bytes;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
42 char delim;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
43 )
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 #define TT this.xargs
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
46
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
47 // 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
48 // Otherwise, fill out out[]
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
49
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
50 // Returning NULL means need more data.
423
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
51 // 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
52 // Returning 1 means hit data limits, but consumed all data
423
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
53 // Returning 2 means hit -E eofstr
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
54
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
55 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
56 {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
57 if (TT.delim) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
58 char *s = data;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
59
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
60 // Chop up whitespace delimited string into args
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
61 while (*s) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
62 char *save;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
63
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
64 while (isspace(*s)) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
65 if (entry) *s = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
66 s++;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
67 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
68
525
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
69 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
70 return *s ? s : (char *)1;
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
71
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
72 if (!*s) break;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
73 save = s;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
74
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
75 for (;;) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
76 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
77 if (!*s || isspace(*s)) break;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
78 s++;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
79 }
423
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
80 if (TT.eofstr) {
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
81 int len = s-save;
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
82 if (len == strlen(TT.eofstr) && !strncmp(save, TT.eofstr, len))
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
83 return (char *)2;
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
84 }
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
85 if (entry) entry[TT.entries] = save;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
86 ++TT.entries;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
87 }
525
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
88
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
89 // -0 support
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
90 } else {
525
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
91 TT.bytes += strlen(data)+1;
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
92 if (TT.max_bytes && TT.bytes >= TT.max_bytes) return data;
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
93 if (TT.max_entries && TT.entries >= TT.max_entries)
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
94 return (char *)1;
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
95 if (entry) entry[TT.entries] = data;
525
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
96 TT.entries++;
414
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
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
99 return NULL;
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
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
102 void xargs_main(void)
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
103 {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
104 struct double_list *dlist = NULL;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
105 int entries, bytes, done = 0, status;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
106 char *data = NULL;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
107
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
108 if (!(toys.optflags&1)) TT.delim = '\n';
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
109
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
110 // If no optargs, call echo.
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
111 if (!toys.optc) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
112 free(toys.optargs);
525
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
113 *(toys.optargs = xzalloc(2*sizeof(char *)))="echo";
f188e572acc7 Fix xargs -0 option.
Rob Landley <rob@landley.net>
parents: 423
diff changeset
114 toys.optc = 1;
414
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
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
117 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
118 bytes += strlen(toys.optargs[entries]);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
119
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
120 // Loop through exec chunks.
416
e9f6e7f25854 More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents: 414
diff changeset
121 while (data || !done) {
e9f6e7f25854 More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents: 414
diff changeset
122 char **out;
e9f6e7f25854 More work on xargs: bugfix and tests.
Rob Landley <rob@landley.net>
parents: 414
diff changeset
123
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
124 TT.entries = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
125 TT.bytes = bytes;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
126
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
127 // Loop reading input
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
128 for (;;) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
129
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
130 // Read line
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
131 if (!data) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
132 ssize_t l = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
133 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
134
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
135 if (l<0) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
136 data = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
137 done++;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
138 break;
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 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
141 dlist_add(&dlist, data);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
142
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
143 // Count data used
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
144 data = handle_entries(data, NULL);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
145 if (!data) continue;
423
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
146 if (data == (char *)2) done++;
2a2b483a4cf9 Implement xargs -E.
Rob Landley <rob@landley.net>
parents: 416
diff changeset
147 if ((long)data <= 2) data = 0;
414
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
148 else data = xstrdup(data);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
149
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
150 break;
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
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
153 // Accumulate cally thing
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
154
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
155 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
156 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
157
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
158 if (dlist) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
159 struct double_list *dtemp;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
160
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
161 // Fill out command line to exec
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
162 memcpy(out, toys.optargs, entries*sizeof(char *));
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
163 TT.entries = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
164 TT.bytes = bytes;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
165 dlist->prev->next = 0;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
166 for (dtemp = dlist; dtemp; dtemp = dtemp->next)
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
167 handle_entries(dtemp->data, out+entries);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
168 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
169 pid_t pid=fork();
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
170 if (!pid) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
171 xclose(0);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
172 open("/dev/null", O_RDONLY);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
173 xexec(out);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
174 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
175 waitpid(pid, &status, 0);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
176 status = WEXITSTATUS(status);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
177
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
178 // 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
179 while (dlist) {
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
180 struct double_list *dtemp = dlist->next;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
181
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
182 free(dlist->data);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
183 free(dlist);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
184 dlist = dtemp;
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
185 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
186 free(out);
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
187 }
9204821e6b95 Implement xargs (first pass, not full SUSv4 yet).
Rob Landley <rob@landley.net>
parents:
diff changeset
188 }