Remove advertising header from all userland binaries.
[dragonfly.git] / bin / sh / redir.c
blob0010abffbf05a050781f34999c8f4bd580777430
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.
32 * @(#)redir.c 8.2 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/redir.c 246288 2013-02-03 15:54:57Z jilles $
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <signal.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <stdlib.h>
46 * Code for dealing with input/output redirection.
49 #include "shell.h"
50 #include "nodes.h"
51 #include "jobs.h"
52 #include "expand.h"
53 #include "redir.h"
54 #include "output.h"
55 #include "memalloc.h"
56 #include "error.h"
57 #include "options.h"
60 #define EMPTY -2 /* marks an unused slot in redirtab */
61 #define CLOSED -1 /* fd was not open before redir */
62 #define PIPESIZE 4096 /* amount of buffering in a pipe */
65 MKINIT
66 struct redirtab {
67 struct redirtab *next;
68 int renamed[10];
72 MKINIT struct redirtab *redirlist;
75 * We keep track of whether or not fd0 has been redirected. This is for
76 * background commands, where we want to redirect fd0 to /dev/null only
77 * if it hasn't already been redirected.
79 static int fd0_redirected = 0;
81 static void openredirect(union node *, char[10 ]);
82 static int openhere(union node *);
86 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
87 * old file descriptors are stashed away so that the redirection can be
88 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
89 * standard output, and the standard error if it becomes a duplicate of
90 * stdout, is saved in memory.
93 void
94 redirect(union node *redir, int flags)
96 union node *n;
97 struct redirtab *sv = NULL;
98 int i;
99 int fd;
100 char memory[10]; /* file descriptors to write to memory */
102 for (i = 10 ; --i >= 0 ; )
103 memory[i] = 0;
104 memory[1] = flags & REDIR_BACKQ;
105 if (flags & REDIR_PUSH) {
106 sv = ckmalloc(sizeof (struct redirtab));
107 for (i = 0 ; i < 10 ; i++)
108 sv->renamed[i] = EMPTY;
109 sv->next = redirlist;
110 redirlist = sv;
112 for (n = redir ; n ; n = n->nfile.next) {
113 fd = n->nfile.fd;
114 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
115 n->ndup.dupfd == fd)
116 continue; /* redirect from/to same file descriptor */
118 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
119 INTOFF;
120 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
121 switch (errno) {
122 case EBADF:
123 i = CLOSED;
124 break;
125 default:
126 INTON;
127 error("%d: %s", fd, strerror(errno));
128 break;
130 } else
131 (void)fcntl(i, F_SETFD, FD_CLOEXEC);
132 sv->renamed[fd] = i;
133 INTON;
135 if (fd == 0)
136 fd0_redirected++;
137 openredirect(n, memory);
139 if (memory[1])
140 out1 = &memout;
141 if (memory[2])
142 out2 = &memout;
146 static void
147 openredirect(union node *redir, char memory[10])
149 struct stat sb;
150 int fd = redir->nfile.fd;
151 char *fname;
152 int f = 0;
153 int e;
156 * We suppress interrupts so that we won't leave open file
157 * descriptors around. Because the signal handler remains
158 * installed and we do not use system call restart, interrupts
159 * will still abort blocking opens such as fifos (they will fail
160 * with EINTR). There is, however, a race condition if an interrupt
161 * arrives after INTOFF and before open blocks.
163 INTOFF;
164 memory[fd] = 0;
165 switch (redir->nfile.type) {
166 case NFROM:
167 fname = redir->nfile.expfname;
168 if ((f = open(fname, O_RDONLY)) < 0)
169 error("cannot open %s: %s", fname, strerror(errno));
170 movefd:
171 if (f != fd) {
172 if (dup2(f, fd) == -1) {
173 e = errno;
174 close(f);
175 error("%d: %s", fd, strerror(e));
177 close(f);
179 break;
180 case NFROMTO:
181 fname = redir->nfile.expfname;
182 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
183 error("cannot create %s: %s", fname, strerror(errno));
184 goto movefd;
185 case NTO:
186 if (Cflag) {
187 fname = redir->nfile.expfname;
188 if (stat(fname, &sb) == -1) {
189 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
190 error("cannot create %s: %s", fname, strerror(errno));
191 } else if (!S_ISREG(sb.st_mode)) {
192 if ((f = open(fname, O_WRONLY, 0666)) < 0)
193 error("cannot create %s: %s", fname, strerror(errno));
194 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
195 close(f);
196 error("cannot create %s: %s", fname,
197 strerror(EEXIST));
199 } else
200 error("cannot create %s: %s", fname,
201 strerror(EEXIST));
202 goto movefd;
204 /* FALLTHROUGH */
205 case NCLOBBER:
206 fname = redir->nfile.expfname;
207 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
208 error("cannot create %s: %s", fname, strerror(errno));
209 goto movefd;
210 case NAPPEND:
211 fname = redir->nfile.expfname;
212 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
213 error("cannot create %s: %s", fname, strerror(errno));
214 goto movefd;
215 case NTOFD:
216 case NFROMFD:
217 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
218 if (memory[redir->ndup.dupfd])
219 memory[fd] = 1;
220 else {
221 if (dup2(redir->ndup.dupfd, fd) < 0)
222 error("%d: %s", redir->ndup.dupfd,
223 strerror(errno));
225 } else {
226 close(fd);
228 break;
229 case NHERE:
230 case NXHERE:
231 f = openhere(redir);
232 goto movefd;
233 default:
234 abort();
236 INTON;
241 * Handle here documents. Normally we fork off a process to write the
242 * data to a pipe. If the document is short, we can stuff the data in
243 * the pipe without forking.
246 static int
247 openhere(union node *redir)
249 char *p;
250 int pip[2];
251 int len = 0;
253 if (pipe(pip) < 0)
254 error("Pipe call failed: %s", strerror(errno));
256 if (redir->type == NXHERE)
257 p = redir->nhere.expdoc;
258 else
259 p = redir->nhere.doc->narg.text;
260 len = strlen(p);
261 if (len <= PIPESIZE) {
262 xwrite(pip[1], p, len);
263 goto out;
266 if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
267 close(pip[0]);
268 signal(SIGINT, SIG_IGN);
269 signal(SIGQUIT, SIG_IGN);
270 signal(SIGHUP, SIG_IGN);
271 signal(SIGTSTP, SIG_IGN);
272 signal(SIGPIPE, SIG_DFL);
273 xwrite(pip[1], p, len);
274 _exit(0);
276 out:
277 close(pip[1]);
278 return pip[0];
284 * Undo the effects of the last redirection.
287 void
288 popredir(void)
290 struct redirtab *rp = redirlist;
291 int i;
293 for (i = 0 ; i < 10 ; i++) {
294 if (rp->renamed[i] != EMPTY) {
295 if (i == 0)
296 fd0_redirected--;
297 if (rp->renamed[i] >= 0) {
298 dup2(rp->renamed[i], i);
299 close(rp->renamed[i]);
300 } else {
301 close(i);
305 INTOFF;
306 redirlist = rp->next;
307 ckfree(rp);
308 INTON;
312 * Undo all redirections. Called on error or interrupt.
315 #ifdef mkinit
317 INCLUDE "redir.h"
319 RESET {
320 while (redirlist)
321 popredir();
324 #endif
326 /* Return true if fd 0 has already been redirected at least once. */
328 fd0_redirected_p(void)
330 return fd0_redirected != 0;
334 * Discard all saved file descriptors.
337 void
338 clearredir(void)
340 struct redirtab *rp;
341 int i;
343 for (rp = redirlist ; rp ; rp = rp->next) {
344 for (i = 0 ; i < 10 ; i++) {
345 if (rp->renamed[i] >= 0) {
346 close(rp->renamed[i]);
348 rp->renamed[i] = EMPTY;