diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-10-21 10:21:33 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-10-21 10:21:33 +0000 |
commit | 4eca37ecd81a80606e9c2afed5401f15d15e3671 (patch) | |
tree | edb21b983f86981f8ed77704231cbe589bc19bdd /contrib/tools/m4/lib/dup2.c | |
parent | 7f4d37b99e25e931918580a353dba7eed11407ee (diff) | |
parent | d3ed30f2deefe6a5ed0d07a3018c723749ca5d7b (diff) | |
download | ydb-4eca37ecd81a80606e9c2afed5401f15d15e3671.tar.gz |
Merge branch 'rightlib' into mergelibs-241021-1020
Diffstat (limited to 'contrib/tools/m4/lib/dup2.c')
-rw-r--r-- | contrib/tools/m4/lib/dup2.c | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/contrib/tools/m4/lib/dup2.c b/contrib/tools/m4/lib/dup2.c index 9219eb3823..13dda55eef 100644 --- a/contrib/tools/m4/lib/dup2.c +++ b/contrib/tools/m4/lib/dup2.c @@ -1,6 +1,6 @@ /* Duplicate an open file descriptor to a specified file descriptor. - Copyright (C) 1999, 2004-2007, 2009-2013 Free Software Foundation, Inc. + Copyright (C) 1999, 2004-2007, 2009-2016 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -85,6 +85,57 @@ ms_windows_dup2 (int fd, int desired_fd) # define dup2 ms_windows_dup2 +# elif defined __KLIBC__ + +# error #include <InnoTekLIBC/backend.h> + +static int +klibc_dup2dirfd (int fd, int desired_fd) +{ + int tempfd; + int dupfd; + + tempfd = open ("NUL", O_RDONLY); + if (tempfd == -1) + return -1; + + if (tempfd == desired_fd) + { + close (tempfd); + + char path[_MAX_PATH]; + if (__libc_Back_ioFHToPath (fd, path, sizeof (path))) + return -1; + + return open(path, O_RDONLY); + } + + dupfd = klibc_dup2dirfd (fd, desired_fd); + + close (tempfd); + + return dupfd; +} + +static int +klibc_dup2 (int fd, int desired_fd) +{ + int dupfd; + struct stat sbuf; + + dupfd = dup2 (fd, desired_fd); + if (dupfd == -1 && errno == ENOTSUP \ + && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode)) + { + close (desired_fd); + + return klibc_dup2dirfd (fd, desired_fd); + } + + return dupfd; +} + +# define dup2 klibc_dup2 # endif int @@ -96,7 +147,11 @@ rpl_dup2 (int fd, int desired_fd) /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF. On Cygwin 1.5.x, dup2 (1, 1) returns 0. On Cygwin 1.7.17, dup2 (1, -1) dumps core. + On Cygwin 1.7.25, dup2 (1, 256) can dump core. On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */ +# if HAVE_SETDTABLESIZE + setdtablesize (desired_fd + 1); +# endif if (desired_fd < 0) fd = desired_fd; if (fd == desired_fd) |