openat: test for fstatat (AT_FDCWD, ..., 0) bug
[gnulib.git] / lib / dup3.c
1 /* Copy a file descriptor, applying specific flags.
2    Copyright (C) 2009-2011 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include <unistd.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26
27 #include "binary-io.h"
28
29 int
30 dup3 (int oldfd, int newfd, int flags)
31 {
32 #if HAVE_DUP3
33 # undef dup3
34   /* Try the system call first, if it exists.  (We may be running with a glibc
35      that has the function but with an older kernel that lacks it.)  */
36   {
37     /* Cache the information whether the system call really exists.  */
38     static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
39     if (have_dup3_really >= 0)
40       {
41         int result = dup3 (oldfd, newfd, flags);
42         if (!(result < 0 && errno == ENOSYS))
43           {
44             have_dup3_really = 1;
45 # if REPLACE_FCHDIR
46             if (0 <= result)
47               result = _gl_register_dup (oldfd, newfd);
48 # endif
49             return result;
50           }
51         have_dup3_really = -1;
52       }
53   }
54 #endif
55
56   if (newfd < 0 || newfd >= getdtablesize () || fcntl (oldfd, F_GETFD) == -1)
57     {
58       errno = EBADF;
59       return -1;
60     }
61
62   if (newfd == oldfd)
63     {
64       errno = EINVAL;
65       return -1;
66     }
67
68   /* Check the supported flags.
69      Note that O_NONBLOCK is not supported, because setting it on newfd
70      would implicitly also set it on oldfd.  */
71   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
72     {
73       errno = EINVAL;
74       return -1;
75     }
76
77   if (flags & O_CLOEXEC)
78     {
79       int result;
80       close (newfd);
81       result = fcntl (oldfd, F_DUPFD_CLOEXEC, newfd);
82       if (newfd < result)
83         {
84           close (result);
85           errno = EIO;
86           result = -1;
87         }
88       if (result < 0)
89         return -1;
90     }
91   else if (dup2 (oldfd, newfd) < 0)
92     return -1;
93
94 #if O_BINARY
95   if (flags & O_BINARY)
96     setmode (newfd, O_BINARY);
97   else if (flags & O_TEXT)
98     setmode (newfd, O_TEXT);
99 #endif
100
101   return newfd;
102 }