kmkbuiltin/expr.c: file revision 1.17 from OpenBSD
[kbuild-mirror.git] / src / ash / redir.c
blob825c9b9d0f25528f0dadeb113f606430a06b7731
1 /* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifdef HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
37 #endif
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
41 #else
42 __RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/types.h>
47 #include <sys/param.h> /* PIPE_BUF */
48 #include <signal.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdlib.h>
56 * Code for dealing with input/output redirection.
59 #include "main.h"
60 #include "shell.h"
61 #include "nodes.h"
62 #include "jobs.h"
63 #include "options.h"
64 #include "expand.h"
65 #include "redir.h"
66 #include "output.h"
67 #include "memalloc.h"
68 #include "error.h"
71 #define EMPTY -2 /* marks an unused slot in redirtab */
72 #ifndef PIPE_BUF
73 # define PIPESIZE 4096 /* amount of buffering in a pipe */
74 #else
75 # define PIPESIZE PIPE_BUF
76 #endif
79 MKINIT
80 struct redirtab {
81 struct redirtab *next;
82 short renamed[10];
86 MKINIT struct redirtab *redirlist;
89 * We keep track of whether or not fd0 has been redirected. This is for
90 * background commands, where we want to redirect fd0 to /dev/null only
91 * if it hasn't already been redirected.
93 int fd0_redirected = 0;
95 STATIC void openredirect(union node *, char[10], int);
96 STATIC int openhere(union node *);
100 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
101 * old file descriptors are stashed away so that the redirection can be
102 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
103 * standard output, and the standard error if it becomes a duplicate of
104 * stdout, is saved in memory.
107 void
108 redirect(union node *redir, int flags)
110 union node *n;
111 struct redirtab *sv = NULL;
112 int i;
113 int fd;
114 int try;
115 char memory[10]; /* file descriptors to write to memory */
117 for (i = 10 ; --i >= 0 ; )
118 memory[i] = 0;
119 memory[1] = flags & REDIR_BACKQ;
120 if (flags & REDIR_PUSH) {
121 /* We don't have to worry about REDIR_VFORK here, as
122 * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
124 sv = ckmalloc(sizeof (struct redirtab));
125 for (i = 0 ; i < 10 ; i++)
126 sv->renamed[i] = EMPTY;
127 sv->next = redirlist;
128 redirlist = sv;
130 for (n = redir ; n ; n = n->nfile.next) {
131 fd = n->nfile.fd;
132 try = 0;
133 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
134 n->ndup.dupfd == fd)
135 continue; /* redirect from/to same file descriptor */
137 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
138 INTOFF;
139 again:
140 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
141 switch (errno) {
142 case EBADF:
143 if (!try) {
144 openredirect(n, memory, flags);
145 try++;
146 goto again;
148 /* FALLTHROUGH*/
149 default:
150 INTON;
151 error("%d: %s", fd, strerror(errno));
152 /* NOTREACHED */
155 if (!try) {
156 sv->renamed[fd] = i;
157 close(fd);
159 INTON;
160 } else {
161 close(fd);
163 if (fd == 0)
164 fd0_redirected++;
165 if (!try)
166 openredirect(n, memory, flags);
168 if (memory[1])
169 out1 = &memout;
170 if (memory[2])
171 out2 = &memout;
175 STATIC void
176 openredirect(union node *redir, char memory[10], int flags)
178 int fd = redir->nfile.fd;
179 char *fname;
180 int f;
181 int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags;
184 * We suppress interrupts so that we won't leave open file
185 * descriptors around. This may not be such a good idea because
186 * an open of a device or a fifo can block indefinitely.
188 INTOFF;
189 memory[fd] = 0;
190 switch (redir->nfile.type) {
191 case NFROM:
192 fname = redir->nfile.expfname;
193 if (flags & REDIR_VFORK)
194 eflags = O_NONBLOCK;
195 else
196 eflags = 0;
197 if ((f = open(fname, O_RDONLY|eflags)) < 0)
198 goto eopen;
199 if (eflags)
200 (void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
201 break;
202 case NFROMTO:
203 fname = redir->nfile.expfname;
204 if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
205 goto ecreate;
206 break;
207 case NTO:
208 if (Cflag)
209 oflags |= O_EXCL;
210 /* FALLTHROUGH */
211 case NCLOBBER:
212 fname = redir->nfile.expfname;
213 if ((f = open(fname, oflags, 0666)) < 0)
214 goto ecreate;
215 break;
216 case NAPPEND:
217 fname = redir->nfile.expfname;
218 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
219 goto ecreate;
220 break;
221 case NTOFD:
222 case NFROMFD:
223 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
224 if (memory[redir->ndup.dupfd])
225 memory[fd] = 1;
226 else
227 copyfd(redir->ndup.dupfd, fd);
229 INTON;
230 return;
231 case NHERE:
232 case NXHERE:
233 f = openhere(redir);
234 break;
235 default:
236 abort();
239 if (f != fd) {
240 copyfd(f, fd);
241 close(f);
243 INTON;
244 return;
245 ecreate:
246 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
247 eopen:
248 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
253 * Handle here documents. Normally we fork off a process to write the
254 * data to a pipe. If the document is short, we can stuff the data in
255 * the pipe without forking.
258 STATIC int
259 openhere(union node *redir)
261 int pip[2];
262 int len = 0;
264 if (pipe(pip) < 0)
265 error("Pipe call failed");
266 if (redir->type == NHERE) {
267 len = strlen(redir->nhere.doc->narg.text);
268 if (len <= PIPESIZE) {
269 xwrite(pip[1], redir->nhere.doc->narg.text, len);
270 goto out;
273 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
274 close(pip[0]);
275 signal(SIGINT, SIG_IGN);
276 signal(SIGQUIT, SIG_IGN);
277 signal(SIGHUP, SIG_IGN);
278 #ifdef SIGTSTP
279 signal(SIGTSTP, SIG_IGN);
280 #endif
281 signal(SIGPIPE, SIG_DFL);
282 if (redir->type == NHERE)
283 xwrite(pip[1], redir->nhere.doc->narg.text, len);
284 else
285 expandhere(redir->nhere.doc, pip[1]);
286 _exit(0);
288 out:
289 close(pip[1]);
290 return pip[0];
296 * Undo the effects of the last redirection.
299 void
300 popredir(void)
302 struct redirtab *rp = redirlist;
303 int i;
305 for (i = 0 ; i < 10 ; i++) {
306 if (rp->renamed[i] != EMPTY) {
307 if (i == 0)
308 fd0_redirected--;
309 close(i);
310 if (rp->renamed[i] >= 0) {
311 copyfd(rp->renamed[i], i);
312 close(rp->renamed[i]);
316 INTOFF;
317 redirlist = rp->next;
318 ckfree(rp);
319 INTON;
323 * Undo all redirections. Called on error or interrupt.
326 #ifdef mkinit
328 INCLUDE "redir.h"
330 RESET {
331 while (redirlist)
332 popredir();
335 SHELLPROC {
336 clearredir(0);
339 #endif
341 /* Return true if fd 0 has already been redirected at least once. */
343 fd0_redirected_p () {
344 return fd0_redirected != 0;
348 * Discard all saved file descriptors.
351 void
352 clearredir(vforked)
353 int vforked;
355 struct redirtab *rp;
356 int i;
358 for (rp = redirlist ; rp ; rp = rp->next) {
359 for (i = 0 ; i < 10 ; i++) {
360 if (rp->renamed[i] >= 0) {
361 close(rp->renamed[i]);
363 if (!vforked)
364 rp->renamed[i] = EMPTY;
372 * Copy a file descriptor to be >= to. Returns -1
373 * if the source file descriptor is closed, EMPTY if there are no unused
374 * file descriptors left.
378 copyfd(int from, int to)
380 int newfd;
382 newfd = fcntl(from, F_DUPFD, to);
383 if (newfd < 0) {
384 if (errno == EMFILE)
385 return EMPTY;
386 else
387 error("%d: %s", from, strerror(errno));
389 return newfd;