Annotation of src/usr.bin/apply/apply.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: apply.c,v 1.13 2003/04/06 23:22:43 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: apply.c,v 1.3 1995/03/25 03:38:23 glass Exp $ */
3:
4: /*-
5: * Copyright (c) 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Jan-Simon Pendry.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.14 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)apply.c 8.4 (Berkeley) 4/4/94";
39: #else
1.14 ! millert 40: static char rcsid[] = "$OpenBSD: apply.c,v 1.13 2003/04/06 23:22:43 deraadt Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include <sys/wait.h>
45:
46: #include <ctype.h>
47: #include <err.h>
48: #include <paths.h>
49: #include <signal.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <unistd.h>
54:
1.11 millert 55: void usage(void);
56: int system(const char *);
1.1 deraadt 57:
58: int
59: main(argc, argv)
60: int argc;
61: char *argv[];
62: {
63: int ch, clen, debug, i, l, magic, n, nargs, rval;
64: char *c, *cmd, *p, *q;
1.12 deraadt 65: size_t len;
1.1 deraadt 66:
67: debug = 0;
68: magic = '%'; /* Default magic char is `%'. */
69: nargs = -1;
1.3 millert 70: while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
1.1 deraadt 71: switch (ch) {
72: case 'a':
73: if (optarg[1] != '\0')
74: errx(1,
75: "illegal magic character specification.");
76: magic = optarg[0];
77: break;
78: case 'd':
79: debug = 1;
80: break;
81: case '0': case '1': case '2': case '3': case '4':
82: case '5': case '6': case '7': case '8': case '9':
83: if (nargs != -1)
84: errx(1,
85: "only one -# argument may be specified.");
86: nargs = optopt - '0';
87: break;
88: default:
89: usage();
90: }
91: argc -= optind;
92: argv += optind;
93:
94: if (argc < 2)
95: usage();
96:
97: /*
98: * The command to run is argv[0], and the args are argv[1..].
99: * Look for %digit references in the command, remembering the
100: * largest one.
101: */
102: for (n = 0, p = argv[0]; *p != '\0'; ++p)
103: if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
104: ++p;
105: if (p[0] - '0' > n)
106: n = p[0] - '0';
107: }
108:
109: /*
110: * If there were any %digit references, then use those, otherwise
111: * build a new command string with sufficient %digit references at
112: * the end to consume (nargs) arguments each time round the loop.
113: * Allocate enough space to hold the maximum command.
114: */
1.12 deraadt 115: len = sizeof("exec ") - 1 +
116: strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1;
117: if ((cmd = malloc(len)) == NULL)
1.1 deraadt 118: err(1, NULL);
119:
120: if (n == 0) {
1.12 deraadt 121: size_t l;
122:
1.1 deraadt 123: /* If nargs not set, default to a single argument. */
124: if (nargs == -1)
125: nargs = 1;
126:
1.12 deraadt 127: l = snprintf(cmd, len, "exec %s", argv[0]);
128: if (l >= len)
129: err(1, "snprintf");
130: len -= l;
131: p = cmd + l;
132:
133: for (i = 1; i <= nargs; i++) {
134: l = snprintf(p, len, " %c%d", magic, i);
135: if (l >= len)
136: err(1, "snprintf");
137: len -= l;
138: p += l;
139: }
1.1 deraadt 140:
141: /*
142: * If nargs set to the special value 0, eat a single
143: * argument for each command execution.
144: */
145: if (nargs == 0)
146: nargs = 1;
147: } else {
1.12 deraadt 148: (void)snprintf(cmd, len, "exec %s", argv[0]);
1.1 deraadt 149: nargs = n;
150: }
151:
152: /*
153: * Grab some space in which to build the command. Allocate
154: * as necessary later, but no reason to build it up slowly
155: * for the normal case.
156: */
157: if ((c = malloc(clen = 1024)) == NULL)
158: err(1, NULL);
159:
160: /*
161: * (argc) and (argv) are still offset by one to make it simpler to
162: * expand %digit references. At the end of the loop check for (argc)
163: * equals 1 means that all the (argv) has been consumed.
164: */
165: for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
166: /*
167: * Find a max value for the command length, and ensure
168: * there's enough space to build it.
169: */
170: for (l = strlen(cmd), i = 0; i < nargs; i++)
1.6 deraadt 171: l += strlen(argv[i+1]);
1.1 deraadt 172: if (l > clen && (c = realloc(c, clen = l)) == NULL)
173: err(1, NULL);
174:
175: /* Expand command argv references. */
176: for (p = cmd, q = c; *p != '\0'; ++p)
1.12 deraadt 177: if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
1.13 deraadt 178: strlcpy(q, argv[(++p)[0] - '0'], c + clen - q);
1.12 deraadt 179: q += strlen(q);
180: } else
1.1 deraadt 181: *q++ = *p;
182:
183: /* Terminate the command string. */
184: *q = '\0';
185:
186: /* Run the command. */
187: if (debug)
188: (void)printf("%s\n", c);
189: else
190: if (system(c))
191: rval = 1;
192: }
193:
194: if (argc != 1)
195: errx(1, "expecting additional argument%s after \"%s\"",
196: (nargs - argc) ? "s" : "", argv[argc - 1]);
197: exit(rval);
198: }
199:
200: /*
201: * system --
202: * Private version of system(3). Use the user's SHELL environment
203: * variable as the shell to execute.
204: */
205: int
206: system(command)
207: const char *command;
208: {
209: static char *name, *shell;
210: pid_t pid;
1.10 millert 211: int pstat;
212: sigset_t mask, omask;
1.1 deraadt 213: sig_t intsave, quitsave;
214:
215: if (shell == NULL) {
216: if ((shell = getenv("SHELL")) == NULL)
217: shell = _PATH_BSHELL;
218: if ((name = strrchr(shell, '/')) == NULL)
219: name = shell;
220: else
221: ++name;
222: }
223: if (!command) /* just checking... */
224: return(1);
225:
1.10 millert 226: sigemptyset(&mask);
227: sigaddset(&mask, SIGCHLD);
228: sigprocmask(SIG_BLOCK, &mask, &omask);
1.4 bitblt 229: switch(pid = fork()) {
1.1 deraadt 230: case -1: /* error */
231: err(1, "fork");
232: case 0: /* child */
1.10 millert 233: sigprocmask(SIG_SETMASK, &omask, NULL);
1.9 deraadt 234: execl(shell, name, "-c", command, (char *)NULL);
1.1 deraadt 235: err(1, "%s", shell);
236: }
237: intsave = signal(SIGINT, SIG_IGN);
238: quitsave = signal(SIGQUIT, SIG_IGN);
1.5 deraadt 239: pid = waitpid(pid, &pstat, 0);
1.10 millert 240: sigprocmask(SIG_SETMASK, &omask, NULL);
1.1 deraadt 241: (void)signal(SIGINT, intsave);
242: (void)signal(SIGQUIT, quitsave);
1.5 deraadt 243: return(pid == -1 ? -1 : pstat);
1.1 deraadt 244: }
245:
246: void
247: usage()
248: {
249: (void)fprintf(stderr,
1.8 aaron 250: "usage: apply [-#] [-a magic] [-d] command argument [...]\n");
1.1 deraadt 251: exit(1);
252: }