root/lib/dup2.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dup2_nothrow
  2. ms_windows_dup2
  3. klibc_dup2dirfd
  4. klibc_dup2
  5. rpl_dup2

     1 /* Duplicate an open file descriptor to a specified file descriptor.
     2 
     3    Copyright (C) 1999, 2004-2007, 2009-2023 Free Software Foundation,
     4    Inc.
     5 
     6    This file is free software: you can redistribute it and/or modify
     7    it under the terms of the GNU Lesser General Public License as
     8    published by the Free Software Foundation; either version 2.1 of the
     9    License, or (at your option) any later version.
    10 
    11    This file is distributed in the hope that it will be useful,
    12    but WITHOUT ANY WARRANTY; without even the implied warranty of
    13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14    GNU Lesser General Public License for more details.
    15 
    16    You should have received a copy of the GNU Lesser General Public License
    17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    18 
    19 /* written by Paul Eggert */
    20 
    21 #include <config.h>
    22 
    23 /* Specification.  */
    24 #include <unistd.h>
    25 
    26 #include <errno.h>
    27 #include <fcntl.h>
    28 
    29 #undef dup2
    30 
    31 #if defined _WIN32 && ! defined __CYGWIN__
    32 
    33 /* Get declarations of the native Windows API functions.  */
    34 # define WIN32_LEAN_AND_MEAN
    35 # include <windows.h>
    36 
    37 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
    38 #  include "msvc-inval.h"
    39 # endif
    40 
    41 /* Get _get_osfhandle.  */
    42 # if GNULIB_MSVC_NOTHROW
    43 #  include "msvc-nothrow.h"
    44 # else
    45 #  include <io.h>
    46 # endif
    47 
    48 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
    49 static int
    50 dup2_nothrow (int fd, int desired_fd)
    51 {
    52   int result;
    53 
    54   TRY_MSVC_INVAL
    55     {
    56       result = _dup2 (fd, desired_fd);
    57     }
    58   CATCH_MSVC_INVAL
    59     {
    60       errno = EBADF;
    61       result = -1;
    62     }
    63   DONE_MSVC_INVAL;
    64 
    65   return result;
    66 }
    67 # else
    68 #  define dup2_nothrow _dup2
    69 # endif
    70 
    71 static int
    72 ms_windows_dup2 (int fd, int desired_fd)
    73 {
    74   int result;
    75 
    76   /* If fd is closed, mingw hangs on dup2 (fd, fd).  If fd is open,
    77      dup2 (fd, fd) returns 0, but all further attempts to use fd in
    78      future dup2 calls will hang.  */
    79   if (fd == desired_fd)
    80     {
    81       if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
    82         {
    83           errno = EBADF;
    84           return -1;
    85         }
    86       return fd;
    87     }
    88 
    89   /* Wine 1.0.1 return 0 when desired_fd is negative but not -1:
    90      https://bugs.winehq.org/show_bug.cgi?id=21289 */
    91   if (desired_fd < 0)
    92     {
    93       errno = EBADF;
    94       return -1;
    95     }
    96 
    97   result = dup2_nothrow (fd, desired_fd);
    98 
    99   if (result == 0)
   100     result = desired_fd;
   101 
   102   return result;
   103 }
   104 
   105 # define dup2 ms_windows_dup2
   106 
   107 #elif defined __KLIBC__
   108 
   109 # include <InnoTekLIBC/backend.h>
   110 
   111 static int
   112 klibc_dup2dirfd (int fd, int desired_fd)
   113 {
   114   int tempfd;
   115   int dupfd;
   116 
   117   tempfd = open ("NUL", O_RDONLY);
   118   if (tempfd == -1)
   119     return -1;
   120 
   121   if (tempfd == desired_fd)
   122     {
   123       close (tempfd);
   124 
   125       char path[_MAX_PATH];
   126       if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
   127         return -1;
   128 
   129       return open(path, O_RDONLY);
   130     }
   131 
   132   dupfd = klibc_dup2dirfd (fd, desired_fd);
   133 
   134   close (tempfd);
   135 
   136   return dupfd;
   137 }
   138 
   139 static int
   140 klibc_dup2 (int fd, int desired_fd)
   141 {
   142   int dupfd;
   143   struct stat sbuf;
   144 
   145   dupfd = dup2 (fd, desired_fd);
   146   if (dupfd == -1 && errno == ENOTSUP \
   147       && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
   148     {
   149       close (desired_fd);
   150 
   151       return klibc_dup2dirfd (fd, desired_fd);
   152     }
   153 
   154   return dupfd;
   155 }
   156 
   157 # define dup2 klibc_dup2
   158 #endif
   159 
   160 int
   161 rpl_dup2 (int fd, int desired_fd)
   162 {
   163   int result;
   164 
   165 #ifdef F_GETFL
   166   /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
   167      On Cygwin 1.5.x, dup2 (1, 1) returns 0.
   168      On Cygwin 1.7.17, dup2 (1, -1) dumps core.
   169      On Cygwin 1.7.25, dup2 (1, 256) can dump core.
   170      On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC.  */
   171 # if HAVE_SETDTABLESIZE
   172   setdtablesize (desired_fd + 1);
   173 # endif
   174   if (desired_fd < 0)
   175     fd = desired_fd;
   176   if (fd == desired_fd)
   177     return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
   178 #endif
   179 
   180   result = dup2 (fd, desired_fd);
   181 
   182   /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x.  */
   183   if (result == -1 && errno == EMFILE)
   184     errno = EBADF;
   185 #if REPLACE_FCHDIR
   186   if (fd != desired_fd && result != -1)
   187     result = _gl_register_dup (fd, result);
   188 #endif
   189   return result;
   190 }

/* [<][>][^][v][top][bottom][index][help] */