add opendir alias
[minix.git] / commands / ash / redir.c
blob9fa35de48e6a4b4c4d7329711601b67ef1ba6b09
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/redir.c,v 1.26 2004/04/06 20:06:51 markm Exp $");
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <stdlib.h>
53 * Code for dealing with input/output redirection.
56 #include "shell.h"
57 #include "nodes.h"
58 #include "jobs.h"
59 #include "expand.h"
60 #include "redir.h"
61 #include "output.h"
62 #include "memalloc.h"
63 #include "error.h"
64 #include "options.h"
67 #define EMPTY -2 /* marks an unused slot in redirtab */
68 #define PIPESIZE 4096 /* amount of buffering in a pipe */
71 MKINIT
72 struct redirtab {
73 struct redirtab *next;
74 int renamed[10];
78 MKINIT struct redirtab *redirlist;
81 * We keep track of whether or not fd0 has been redirected. This is for
82 * background commands, where we want to redirect fd0 to /dev/null only
83 * if it hasn't already been redirected.
85 STATIC int fd0_redirected = 0;
87 STATIC void openredirect(union node *, char[10 ]);
88 STATIC int openhere(union node *);
92 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
93 * old file descriptors are stashed away so that the redirection can be
94 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
95 * standard output, and the standard error if it becomes a duplicate of
96 * stdout, is saved in memory.
99 void
100 redirect(union node *redir, int flags)
102 union node *n;
103 struct redirtab *sv = NULL;
104 int i;
105 int fd;
106 int try;
107 char memory[10]; /* file descriptors to write to memory */
109 for (i = 10 ; --i >= 0 ; )
110 memory[i] = 0;
111 memory[1] = flags & REDIR_BACKQ;
112 if (flags & REDIR_PUSH) {
113 sv = ckmalloc(sizeof (struct redirtab));
114 for (i = 0 ; i < 10 ; i++)
115 sv->renamed[i] = EMPTY;
116 sv->next = redirlist;
117 redirlist = sv;
119 for (n = redir ; n ; n = n->nfile.next) {
120 fd = n->nfile.fd;
121 try = 0;
122 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
123 n->ndup.dupfd == fd)
124 continue; /* redirect from/to same file descriptor */
126 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
127 INTOFF;
128 again:
129 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
130 switch (errno) {
131 case EBADF:
132 if (!try) {
133 openredirect(n, memory);
134 try++;
135 goto again;
137 /* FALLTHROUGH*/
138 default:
139 INTON;
140 error("%d: %s", fd, strerror(errno));
141 break;
144 if (!try) {
145 sv->renamed[fd] = i;
147 INTON;
149 if (fd == 0)
150 fd0_redirected++;
151 if (!try)
152 openredirect(n, memory);
154 if (memory[1])
155 out1 = &memout;
156 if (memory[2])
157 out2 = &memout;
161 STATIC void
162 openredirect(union node *redir, char memory[10])
164 struct stat sb;
165 int fd = redir->nfile.fd;
166 char *fname;
167 int f;
169 /* Assume redirection succeeds. */
170 { extern int exitstatus; exitstatus = 0; }
173 * We suppress interrupts so that we won't leave open file
174 * descriptors around. This may not be such a good idea because
175 * an open of a device or a fifo can block indefinitely.
177 INTOFF;
178 memory[fd] = 0;
179 switch (redir->nfile.type) {
180 case NFROM:
181 fname = redir->nfile.expfname;
182 if ((f = open(fname, O_RDONLY)) < 0)
183 error("cannot open %s: %s", fname, strerror(errno));
184 movefd:
185 if (f != fd) {
186 dup2(f, fd);
187 close(f);
189 break;
190 case NFROMTO:
191 fname = redir->nfile.expfname;
192 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
193 error("cannot create %s: %s", fname, strerror(errno));
194 goto movefd;
195 case NTO:
196 fname = redir->nfile.expfname;
197 if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode))
198 error("cannot create %s: %s", fname,
199 strerror(EEXIST));
200 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
201 error("cannot create %s: %s", fname, strerror(errno));
202 goto movefd;
203 case NCLOBBER:
204 fname = redir->nfile.expfname;
205 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
206 error("cannot create %s: %s", fname, strerror(errno));
207 goto movefd;
208 case NAPPEND:
209 fname = redir->nfile.expfname;
210 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
211 error("cannot create %s: %s", fname, strerror(errno));
212 goto movefd;
213 case NTOFD:
214 case NFROMFD:
215 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
216 if (memory[redir->ndup.dupfd])
217 memory[fd] = 1;
218 else
219 dup2(redir->ndup.dupfd, fd);
220 } else {
221 close(fd);
223 break;
224 case NHERE:
225 case NXHERE:
226 f = openhere(redir);
227 goto movefd;
228 default:
229 abort();
231 INTON;
236 * Handle here documents. Normally we fork off a process to write the
237 * data to a pipe. If the document is short, we can stuff the data in
238 * the pipe without forking.
241 STATIC int
242 openhere(union node *redir)
244 int pip[2];
245 int len = 0;
247 if (pipe(pip) < 0)
248 error("Pipe call failed: %s", strerror(errno));
249 if (redir->type == NHERE) {
250 len = strlen(redir->nhere.doc->narg.text);
251 if (len <= PIPESIZE) {
252 xwrite(pip[1], redir->nhere.doc->narg.text, len);
253 goto out;
256 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
257 close(pip[0]);
258 signal(SIGINT, SIG_IGN);
259 signal(SIGQUIT, SIG_IGN);
260 signal(SIGHUP, SIG_IGN);
261 signal(SIGTSTP, SIG_IGN);
262 signal(SIGPIPE, SIG_DFL);
263 if (redir->type == NHERE)
264 xwrite(pip[1], redir->nhere.doc->narg.text, len);
265 else
266 expandhere(redir->nhere.doc, pip[1]);
267 _exit(0);
269 out:
270 close(pip[1]);
271 return pip[0];
277 * Undo the effects of the last redirection.
280 void
281 popredir(void)
283 struct redirtab *rp = redirlist;
284 int i;
286 for (i = 0 ; i < 10 ; i++) {
287 if (rp->renamed[i] != EMPTY) {
288 if (i == 0)
289 fd0_redirected--;
290 if (rp->renamed[i] >= 0) {
291 dup2(rp->renamed[i], i);
292 close(rp->renamed[i]);
293 } else {
294 close(i);
298 INTOFF;
299 redirlist = rp->next;
300 ckfree(rp);
301 INTON;
305 * Undo all redirections. Called on error or interrupt.
308 #ifdef mkinit
310 INCLUDE "redir.h"
312 RESET {
313 while (redirlist)
314 popredir();
317 SHELLPROC {
318 clearredir();
321 #endif
323 /* Return true if fd 0 has already been redirected at least once. */
325 fd0_redirected_p(void)
327 return fd0_redirected != 0;
331 * Discard all saved file descriptors.
334 void
335 clearredir(void)
337 struct redirtab *rp;
338 int i;
340 for (rp = redirlist ; rp ; rp = rp->next) {
341 for (i = 0 ; i < 10 ; i++) {
342 if (rp->renamed[i] >= 0) {
343 close(rp->renamed[i]);
345 rp->renamed[i] = EMPTY;
351 * $PchId: redir.c,v 1.5 2006/05/22 12:27:37 philip Exp $