xref: /netbsd-src/external/gpl2/xcvs/dist/lib/dup2.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1a7c91847Schristos /* Duplicate an open file descriptor to a specified file descriptor.
2a7c91847Schristos    Copyright (C) 1999, 2004 Free Software Foundation, Inc.
3a7c91847Schristos 
4a7c91847Schristos    This program is free software; you can redistribute it and/or modify
5a7c91847Schristos    it under the terms of the GNU General Public License as published by
6a7c91847Schristos    the Free Software Foundation; either version 2, or (at your option)
7a7c91847Schristos    any later version.
8a7c91847Schristos 
9a7c91847Schristos    This program is distributed in the hope that it will be useful,
10a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12a7c91847Schristos    GNU General Public License for more details.
13a7c91847Schristos 
14a7c91847Schristos    You should have received a copy of the GNU General Public License
15a7c91847Schristos    along with this program; if not, write to the Free Software Foundation,
16a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*5a6c14c8Schristos #include <sys/cdefs.h>
18*5a6c14c8Schristos __RCSID("$NetBSD: dup2.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
19*5a6c14c8Schristos 
20a7c91847Schristos 
21a7c91847Schristos /* written by Paul Eggert */
22a7c91847Schristos 
23a7c91847Schristos #ifdef HAVE_CONFIG_H
24a7c91847Schristos # include <config.h>
25a7c91847Schristos #endif
26a7c91847Schristos 
27a7c91847Schristos #include <errno.h>
28a7c91847Schristos 
29a7c91847Schristos #include <fcntl.h>
30a7c91847Schristos 
31a7c91847Schristos #if HAVE_UNISTD_H
32a7c91847Schristos # include <unistd.h>
33a7c91847Schristos #endif
34a7c91847Schristos 
35a7c91847Schristos #ifndef F_DUPFD
36a7c91847Schristos static int
dupfd(int fd,int desired_fd)37a7c91847Schristos dupfd (int fd, int desired_fd)
38a7c91847Schristos {
39a7c91847Schristos   int duplicated_fd = dup (fd);
40a7c91847Schristos   if (duplicated_fd < 0 || duplicated_fd == desired_fd)
41a7c91847Schristos     return duplicated_fd;
42a7c91847Schristos   else
43a7c91847Schristos     {
44a7c91847Schristos       int r = dupfd (fd, desired_fd);
45a7c91847Schristos       int e = errno;
46a7c91847Schristos       close (duplicated_fd);
47a7c91847Schristos       errno = e;
48a7c91847Schristos       return r;
49a7c91847Schristos     }
50a7c91847Schristos }
51a7c91847Schristos #endif
52a7c91847Schristos 
53a7c91847Schristos int
dup2(int fd,int desired_fd)54a7c91847Schristos dup2 (int fd, int desired_fd)
55a7c91847Schristos {
56a7c91847Schristos   if (fd == desired_fd)
57a7c91847Schristos     return fd;
58a7c91847Schristos   close (desired_fd);
59a7c91847Schristos #ifdef F_DUPFD
60a7c91847Schristos   return fcntl (fd, F_DUPFD, desired_fd);
61a7c91847Schristos #else
62a7c91847Schristos   return dupfd (fd, desired_fd);
63a7c91847Schristos #endif
64a7c91847Schristos }
65