Correctly determine whether pow is available in libc on AIX 7 with xlc.
[gnulib.git] / lib / dup3.c
1 /* Copy a file descriptor, applying specific flags.
2    Copyright (C) 2009-2010 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 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* Native Woe32 API.  */
31
32 # include <string.h>
33
34 /* Get declarations of the Win32 API functions.  */
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37
38 /* Upper bound on getdtablesize().  See lib/getdtablesize.c.  */
39 # define OPEN_MAX_MAX 0x10000
40
41 #else
42 /* Unix API.  */
43
44 # ifndef O_CLOEXEC
45 #  define O_CLOEXEC 0
46 # endif
47
48 #endif
49
50 int
51 dup3 (int oldfd, int newfd, int flags)
52 {
53 #if HAVE_DUP3
54 # undef dup3
55   /* Try the system call first, if it exists.  (We may be running with a glibc
56      that has the function but with an older kernel that lacks it.)  */
57   {
58     /* Cache the information whether the system call really exists.  */
59     static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
60     if (have_dup3_really >= 0)
61       {
62         int result = dup3 (oldfd, newfd, flags);
63         if (!(result < 0 && errno == ENOSYS))
64           {
65             have_dup3_really = 1;
66 #if REPLACE_FCHDIR
67             if (0 <= result)
68               result = _gl_register_dup (oldfd, newfd);
69 #endif
70             return result;
71           }
72         have_dup3_really = -1;
73       }
74   }
75 #endif
76
77   if (newfd < 0 || newfd >= getdtablesize () || fcntl (oldfd, F_GETFD) == -1)
78     {
79       errno = EBADF;
80       return -1;
81     }
82
83   if (newfd == oldfd)
84     {
85       errno = EINVAL;
86       return -1;
87     }
88
89   /* Check the supported flags.
90      Note that O_NONBLOCK is not supported, because setting it on newfd
91      would implicitly also set it on oldfd.  */
92   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
93     {
94       errno = EINVAL;
95       return -1;
96     }
97
98   if (flags & O_CLOEXEC)
99     {
100       int result;
101       close (newfd);
102       result = fcntl (oldfd, F_DUPFD_CLOEXEC, newfd);
103       if (newfd < result)
104         {
105           close (result);
106           errno = EIO;
107           result = -1;
108         }
109       if (result < 0)
110         return -1;
111     }
112   else if (dup2 (oldfd, newfd) < 0)
113     return -1;
114
115 #if O_BINARY
116   if (flags & O_BINARY)
117     setmode (newfd, O_BINARY);
118   else if (flags & O_TEXT)
119     setmode (newfd, O_TEXT);
120 #endif
121
122   return newfd;
123 }