]> git.saurik.com Git - apple/shell_cmds.git/blame - sh/redir.c
shell_cmds-203.tar.gz
[apple/shell_cmds.git] / sh / redir.c
CommitLineData
71aad674
A
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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 * 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.
19 *
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.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
deb63bfb 39__FBSDID("$FreeBSD: head/bin/sh/redir.c 295085 2016-01-30 21:21:25Z jilles $");
71aad674
A
40
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <signal.h>
44#include <string.h>
45#include <fcntl.h>
46#include <errno.h>
47#include <unistd.h>
48#include <stdlib.h>
49
50/*
51 * Code for dealing with input/output redirection.
52 */
53
54#include "shell.h"
55#include "nodes.h"
56#include "jobs.h"
57#include "expand.h"
58#include "redir.h"
59#include "output.h"
60#include "memalloc.h"
61#include "error.h"
62#include "options.h"
63
64
65#define EMPTY -2 /* marks an unused slot in redirtab */
66#define CLOSED -1 /* fd was not open before redir */
67
68
69struct redirtab {
70 struct redirtab *next;
71 int renamed[10];
72 int fd0_redirected;
deb63bfb 73 unsigned int empty_redirs;
71aad674
A
74};
75
76
77static struct redirtab *redirlist;
78
79/*
80 * We keep track of whether or not fd0 has been redirected. This is for
81 * background commands, where we want to redirect fd0 to /dev/null only
82 * if it hasn't already been redirected.
83*/
84static int fd0_redirected = 0;
85
deb63bfb
A
86/* Number of redirtabs that have not been allocated. */
87static unsigned int empty_redirs = 0;
88
71aad674
A
89static void openredirect(union node *, char[10 ]);
90static int openhere(union node *);
91
92
93/*
94 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
95 * old file descriptors are stashed away so that the redirection can be
96 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
97 * standard output, and the standard error if it becomes a duplicate of
98 * stdout, is saved in memory.
99*
100 * We suppress interrupts so that we won't leave open file
101 * descriptors around. Because the signal handler remains
102 * installed and we do not use system call restart, interrupts
103 * will still abort blocking opens such as fifos (they will fail
104 * with EINTR). There is, however, a race condition if an interrupt
105 * arrives after INTOFF and before open blocks.
106 */
107
108void
109redirect(union node *redir, int flags)
110{
111 union node *n;
112 struct redirtab *sv = NULL;
113 int i;
114 int fd;
115 char memory[10]; /* file descriptors to write to memory */
116
117 INTOFF;
118 for (i = 10 ; --i >= 0 ; )
119 memory[i] = 0;
120 memory[1] = flags & REDIR_BACKQ;
121 if (flags & REDIR_PUSH) {
deb63bfb
A
122 empty_redirs++;
123 if (redir != NULL) {
124 sv = ckmalloc(sizeof (struct redirtab));
125 for (i = 0 ; i < 10 ; i++)
126 sv->renamed[i] = EMPTY;
127 sv->fd0_redirected = fd0_redirected;
128 sv->empty_redirs = empty_redirs - 1;
129 sv->next = redirlist;
130 redirlist = sv;
131 empty_redirs = 0;
132 }
71aad674
A
133 }
134 for (n = redir ; n ; n = n->nfile.next) {
135 fd = n->nfile.fd;
136 if (fd == 0)
137 fd0_redirected = 1;
138 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
139 n->ndup.dupfd == fd)
140 continue; /* redirect from/to same file descriptor */
141
142 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
143 INTOFF;
144 if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
145 switch (errno) {
146 case EBADF:
147 i = CLOSED;
148 break;
149 default:
150 INTON;
151 error("%d: %s", fd, strerror(errno));
152 break;
153 }
154 }
155 sv->renamed[fd] = i;
156 INTON;
157 }
158 openredirect(n, memory);
159 INTON;
160 INTOFF;
161 }
162 if (memory[1])
163 out1 = &memout;
164 if (memory[2])
165 out2 = &memout;
166 INTON;
167}
168
169
170static void
171openredirect(union node *redir, char memory[10])
172{
173 struct stat sb;
174 int fd = redir->nfile.fd;
175 const char *fname;
176 int f;
177 int e;
178
179 memory[fd] = 0;
180 switch (redir->nfile.type) {
181 case NFROM:
182 fname = redir->nfile.expfname;
183 if ((f = open(fname, O_RDONLY)) < 0)
184 error("cannot open %s: %s", fname, strerror(errno));
185 break;
186 case NFROMTO:
187 fname = redir->nfile.expfname;
188 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
189 error("cannot create %s: %s", fname, strerror(errno));
190 break;
191 case NTO:
192 if (Cflag) {
193 fname = redir->nfile.expfname;
194 if (stat(fname, &sb) == -1) {
195 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
196 error("cannot create %s: %s", fname, strerror(errno));
197 } else if (!S_ISREG(sb.st_mode)) {
198 if ((f = open(fname, O_WRONLY, 0666)) < 0)
199 error("cannot create %s: %s", fname, strerror(errno));
200 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
201 close(f);
202 error("cannot create %s: %s", fname,
203 strerror(EEXIST));
204 }
205 } else
206 error("cannot create %s: %s", fname,
207 strerror(EEXIST));
208 break;
209 }
210 /* FALLTHROUGH */
211 case NCLOBBER:
212 fname = redir->nfile.expfname;
213 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
214 error("cannot create %s: %s", fname, strerror(errno));
215 break;
216 case NAPPEND:
217 fname = redir->nfile.expfname;
218 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
219 error("cannot create %s: %s", fname, strerror(errno));
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 if (dup2(redir->ndup.dupfd, fd) < 0)
228 error("%d: %s", redir->ndup.dupfd,
229 strerror(errno));
230 }
231 } else {
232 close(fd);
233 }
234 return;
235 case NHERE:
236 case NXHERE:
237 f = openhere(redir);
238 break;
239 default:
240 abort();
241 }
242 if (f != fd) {
243 if (dup2(f, fd) == -1) {
244 e = errno;
245 close(f);
246 error("%d: %s", fd, strerror(e));
247 }
248 close(f);
249 }
250}
251
252
253/*
254 * Handle here documents. Normally we fork off a process to write the
255 * data to a pipe. If the document is short, we can stuff the data in
256 * the pipe without forking.
257 */
258
259static int
260openhere(union node *redir)
261{
262 const char *p;
263 int pip[2];
264 size_t len = 0;
265 int flags;
266 ssize_t written = 0;
267
268 if (pipe(pip) < 0)
269 error("Pipe call failed: %s", strerror(errno));
270
271 if (redir->type == NXHERE)
272 p = redir->nhere.expdoc;
273 else
274 p = redir->nhere.doc->narg.text;
275 len = strlen(p);
276 if (len == 0)
277 goto out;
278 flags = fcntl(pip[1], F_GETFL, 0);
279 if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) {
280 written = write(pip[1], p, len);
281 if (written < 0)
282 written = 0;
283 if ((size_t)written == len)
284 goto out;
285 fcntl(pip[1], F_SETFL, flags);
286 }
287
288 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
289 close(pip[0]);
290 signal(SIGINT, SIG_IGN);
291 signal(SIGQUIT, SIG_IGN);
292 signal(SIGHUP, SIG_IGN);
293 signal(SIGTSTP, SIG_IGN);
294 signal(SIGPIPE, SIG_DFL);
295 xwrite(pip[1], p + written, len - written);
296 _exit(0);
297 }
298out:
299 close(pip[1]);
300 return pip[0];
301}
302
303
304
305/*
306 * Undo the effects of the last redirection.
307 */
308
309void
310popredir(void)
311{
312 struct redirtab *rp = redirlist;
313 int i;
314
deb63bfb
A
315 INTOFF;
316 if (empty_redirs > 0) {
317 empty_redirs--;
318 INTON;
319 return;
320 }
71aad674
A
321 for (i = 0 ; i < 10 ; i++) {
322 if (rp->renamed[i] != EMPTY) {
323 if (rp->renamed[i] >= 0) {
324 dup2(rp->renamed[i], i);
325 close(rp->renamed[i]);
326 } else {
327 close(i);
328 }
329 }
330 }
71aad674 331 fd0_redirected = rp->fd0_redirected;
deb63bfb 332 empty_redirs = rp->empty_redirs;
71aad674
A
333 redirlist = rp->next;
334 ckfree(rp);
335 INTON;
336}
337
338/* Return true if fd 0 has already been redirected at least once. */
339int
340fd0_redirected_p(void)
341{
342 return fd0_redirected != 0;
343}
344
345/*
346 * Discard all saved file descriptors.
347 */
348
349void
350clearredir(void)
351{
352 struct redirtab *rp;
353 int i;
354
355 for (rp = redirlist ; rp ; rp = rp->next) {
356 for (i = 0 ; i < 10 ; i++) {
357 if (rp->renamed[i] >= 0) {
358 close(rp->renamed[i]);
359 }
360 rp->renamed[i] = EMPTY;
361 }
362 }
363}