diff options
Diffstat (limited to 'lib/fd-reopen.c')
-rw-r--r-- | lib/fd-reopen.c | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/lib/fd-reopen.c b/lib/fd-reopen.c index 99376e3..1c3a0f6 100644 --- a/lib/fd-reopen.c +++ b/lib/fd-reopen.c @@ -1,11 +1,11 @@ /* Invoke open, but return either a desired file descriptor or -1. - Copyright (C) 2005, 2006 Free Software Foundation, Inc. + Copyright (C) 2005-2016 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + 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 - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,8 +13,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* Written by Paul Eggert. */ @@ -27,20 +26,18 @@ #include <unistd.h> /* Open a file to a particular file descriptor. This is like standard - `open', except it always returns DESIRED_FD if successful. */ + 'open', except it always returns DESIRED_FD if successful. */ int fd_reopen (int desired_fd, char const *file, int flags, mode_t mode) { - int fd; + int fd = open (file, flags, mode); - close (desired_fd); - fd = open (file, flags, mode); if (fd == desired_fd || fd < 0) return fd; else { - int fd2 = fcntl (fd, F_DUPFD, desired_fd); + int fd2 = dup2 (fd, desired_fd); int saved_errno = errno; close (fd); errno = saved_errno; |