Deleted Added
full compact
redir.c (214290) redir.c (216851)
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

--- 22 unchanged lines hidden (view full) ---

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>
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

--- 22 unchanged lines hidden (view full) ---

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>
39__FBSDID("$FreeBSD: head/bin/sh/redir.c 214290 2010-10-24 20:09:49Z jilles $");
39__FBSDID("$FreeBSD: head/bin/sh/redir.c 216851 2010-12-31 18:20:17Z jilles $");
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>

--- 102 unchanged lines hidden (view full) ---

150
151static void
152openredirect(union node *redir, char memory[10])
153{
154 struct stat sb;
155 int fd = redir->nfile.fd;
156 char *fname;
157 int f;
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>

--- 102 unchanged lines hidden (view full) ---

150
151static void
152openredirect(union node *redir, char memory[10])
153{
154 struct stat sb;
155 int fd = redir->nfile.fd;
156 char *fname;
157 int f;
158 int e;
158
159 /*
160 * We suppress interrupts so that we won't leave open file
161 * descriptors around. Because the signal handler remains
162 * installed and we do not use system call restart, interrupts
163 * will still abort blocking opens such as fifos (they will fail
164 * with EINTR). There is, however, a race condition if an interrupt
165 * arrives after INTOFF and before open blocks.
166 */
167 INTOFF;
168 memory[fd] = 0;
169 switch (redir->nfile.type) {
170 case NFROM:
171 fname = redir->nfile.expfname;
172 if ((f = open(fname, O_RDONLY)) < 0)
173 error("cannot open %s: %s", fname, strerror(errno));
174movefd:
175 if (f != fd) {
159
160 /*
161 * We suppress interrupts so that we won't leave open file
162 * descriptors around. Because the signal handler remains
163 * installed and we do not use system call restart, interrupts
164 * will still abort blocking opens such as fifos (they will fail
165 * with EINTR). There is, however, a race condition if an interrupt
166 * arrives after INTOFF and before open blocks.
167 */
168 INTOFF;
169 memory[fd] = 0;
170 switch (redir->nfile.type) {
171 case NFROM:
172 fname = redir->nfile.expfname;
173 if ((f = open(fname, O_RDONLY)) < 0)
174 error("cannot open %s: %s", fname, strerror(errno));
175movefd:
176 if (f != fd) {
176 dup2(f, fd);
177 if (dup2(f, fd) == -1) {
178 e = errno;
179 close(f);
180 error("%d: %s", fd, strerror(e));
181 }
177 close(f);
178 }
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;

--- 169 unchanged lines hidden ---
182 close(f);
183 }
184 break;
185 case NFROMTO:
186 fname = redir->nfile.expfname;
187 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
188 error("cannot create %s: %s", fname, strerror(errno));
189 goto movefd;

--- 169 unchanged lines hidden ---