source: trunk/minix/commands/ash/redir.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 8.2 KB
Line 
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char sccsid[] = "@(#)redir.c 5.1 (Berkeley) 3/7/91";
39#endif /* not lint */
40
41/*
42 * Code for dealing with input/output redirection.
43 */
44
45#include "shell.h"
46#include "nodes.h"
47#include "jobs.h"
48#include "expand.h"
49#include "redir.h"
50#include "eval.h"
51#include "output.h"
52#include "memalloc.h"
53#include "error.h"
54#include <sys/types.h>
55#include <signal.h>
56#include <fcntl.h>
57#include <errno.h>
58#include <limits.h>
59
60
61#define EMPTY -2 /* marks an unused slot in redirtab */
62#define PIPESIZE 4096 /* amount of buffering in a pipe */
63
64
65MKINIT
66struct redirtab {
67 struct redirtab *next;
68 short renamed[10];
69};
70
71
72MKINIT struct redirtab *redirlist;
73
74/* We keep track of whether or not fd0 has been redirected. This is for
75 background commands, where we want to redirect fd0 to /dev/null only
76 if it hasn't already been redirected. */
77int fd0_redirected = 0;
78
79#ifdef __STDC__
80STATIC void openredirect(union node *, char *);
81STATIC int openhere(union node *);
82#else
83STATIC void openredirect();
84STATIC int openhere();
85#endif
86
87
88
89/*
90 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
91 * old file descriptors are stashed away so that the redirection can be
92 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
93 * standard output, and the standard error if it becomes a duplicate of
94 * stdout, is saved in memory.
95 */
96
97void
98redirect(redir, flags)
99 union node *redir;
100 int flags;
101 {
102 union node *n;
103 struct redirtab *sv;
104 int i;
105 int fd;
106 char memory[10]; /* file descriptors to write to memory */
107
108 for (i = 10 ; --i >= 0 ; )
109 memory[i] = 0;
110 memory[1] = flags & REDIR_BACKQ;
111 if (flags & REDIR_PUSH) {
112 sv = ckmalloc(sizeof (struct redirtab));
113 for (i = 0 ; i < 10 ; i++)
114 sv->renamed[i] = EMPTY;
115 sv->next = redirlist;
116 redirlist = sv;
117 }
118 for (n = redir ; n ; n = n->nfile.next) {
119 fd = n->nfile.fd;
120 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
121 INTOFF;
122 if ((i = copyfd(fd, 10)) != EMPTY) {
123 sv->renamed[fd] = i;
124 close(fd);
125 }
126 INTON;
127 if (i == EMPTY)
128 error("Out of file descriptors");
129 } else {
130 close(fd);
131 }
132 if (fd == 0)
133 fd0_redirected++;
134 openredirect(n, memory);
135 }
136 if (memory[1])
137 out1 = &memout;
138 if (memory[2])
139 out2 = &memout;
140}
141
142
143STATIC void
144openredirect(redir, memory)
145 union node *redir;
146 char memory[10];
147 {
148 int fd = redir->nfile.fd;
149 char *fname;
150 int f;
151
152 /* Assume redirection succeeds. */
153 exitstatus = 0;
154
155 /*
156 * We suppress interrupts so that we won't leave open file
157 * descriptors around. This may not be such a good idea because
158 * an open of a device or a fifo can block indefinitely.
159 */
160 INTOFF;
161 memory[fd] = 0;
162 switch (redir->nfile.type) {
163 case NFROM:
164 fname = redir->nfile.expfname;
165 if ((f = open(fname, O_RDONLY)) < 0)
166 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
167movefd:
168 if (f != fd) {
169 copyfd(f, fd);
170 close(f);
171 }
172 break;
173 case NTO:
174 fname = redir->nfile.expfname;
175#ifdef O_CREAT
176 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
177 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
178#else
179 if ((f = creat(fname, 0666)) < 0)
180 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
181#endif
182 goto movefd;
183 case NAPPEND:
184 fname = redir->nfile.expfname;
185#ifdef O_APPEND
186 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
187 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
188#else
189 if ((f = open(fname, O_WRONLY)) < 0
190 && (f = creat(fname, 0666)) < 0)
191 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
192 lseek(f, 0L, 2);
193#endif
194 goto movefd;
195 case NTOFD:
196 case NFROMFD:
197 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
198 if (memory[redir->ndup.dupfd])
199 memory[fd] = 1;
200 else
201 copyfd(redir->ndup.dupfd, fd);
202 }
203 break;
204 case NHERE:
205 case NXHERE:
206 f = openhere(redir);
207 goto movefd;
208 default:
209 abort();
210 }
211 INTON;
212}
213
214
215/*
216 * Handle here documents. Normally we fork off a process to write the
217 * data to a pipe. If the document is short, we can stuff the data in
218 * the pipe without forking.
219 */
220
221STATIC int
222openhere(redir)
223 union node *redir;
224 {
225 int pip[2];
226 int len;
227
228 if (pipe(pip) < 0)
229 error("Pipe call failed");
230 if (redir->type == NHERE) {
231 len = strlen(redir->nhere.doc->narg.text);
232 if (len <= PIPESIZE) {
233 xwrite(pip[1], redir->nhere.doc->narg.text, len);
234 goto out;
235 }
236 }
237 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
238 close(pip[0]);
239 signal(SIGINT, SIG_IGN);
240 signal(SIGQUIT, SIG_IGN);
241 signal(SIGHUP, SIG_IGN);
242#ifdef SIGTSTP
243 signal(SIGTSTP, SIG_IGN);
244#endif
245 signal(SIGPIPE, SIG_DFL);
246 if (redir->type == NHERE)
247 xwrite(pip[1], redir->nhere.doc->narg.text, len);
248 else
249 expandhere(redir->nhere.doc, pip[1]);
250 _exit(0);
251 }
252out:
253 close(pip[1]);
254 return pip[0];
255}
256
257
258
259/*
260 * Undo the effects of the last redirection.
261 */
262
263void
264popredir() {
265 register struct redirtab *rp = redirlist;
266 int i;
267
268 for (i = 0 ; i < 10 ; i++) {
269 if (rp->renamed[i] != EMPTY) {
270 if (i == 0)
271 fd0_redirected--;
272 close(i);
273 if (rp->renamed[i] >= 0) {
274 copyfd(rp->renamed[i], i);
275 close(rp->renamed[i]);
276 }
277 }
278 }
279 INTOFF;
280 redirlist = rp->next;
281 ckfree(rp);
282 INTON;
283}
284
285
286
287/*
288 * Undo all redirections. Called on error or interrupt.
289 */
290
291#ifdef mkinit
292
293INCLUDE "redir.h"
294
295RESET {
296 while (redirlist)
297 popredir();
298}
299
300SHELLPROC {
301 clearredir();
302}
303
304#endif
305
306
307/*
308 * Discard all saved file descriptors.
309 */
310
311void
312clearredir() {
313 register struct redirtab *rp;
314 int i;
315
316 for (rp = redirlist ; rp ; rp = rp->next) {
317 for (i = 0 ; i < 10 ; i++) {
318 if (rp->renamed[i] >= 0) {
319 close(rp->renamed[i]);
320 }
321 rp->renamed[i] = EMPTY;
322 }
323 }
324}
325
326
327
328/*
329 * Copy a file descriptor, like the F_DUPFD option of fcntl. Returns -1
330 * if the source file descriptor is closed, EMPTY if there are no unused
331 * file descriptors left.
332 */
333
334int
335copyfd(from, to) {
336#ifdef F_DUPFD
337 int newfd;
338
339 newfd = fcntl(from, F_DUPFD, to);
340 if (newfd < 0 && errno == EMFILE)
341 return EMPTY;
342 return newfd;
343#else
344 char toclose[32];
345 int i;
346 int newfd;
347 int e;
348
349 for (i = 0 ; i < to ; i++)
350 toclose[i] = 0;
351 INTOFF;
352 while ((newfd = dup(from)) >= 0 && newfd < to)
353 toclose[newfd] = 1;
354 e = errno;
355 for (i = 0 ; i < to ; i++) {
356 if (toclose[i])
357 close(i);
358 }
359 INTON;
360 if (newfd < 0 && e == EMFILE)
361 return EMPTY;
362 return newfd;
363#endif
364}
365
366/* Return true if fd 0 has already been redirected at least once. */
367int
368fd0_redirected_p () {
369 return fd0_redirected != 0;
370}
Note: See TracBrowser for help on using the repository browser.