From 4b571b192a221cd05dc140113373acf5aee7ca24 Mon Sep 17 00:00:00 2001 From: Robert Drehmel Date: Sun, 23 Jun 2002 20:41:30 +0000 Subject: [PATCH] Fix a bug which prevented the duplication of the standard i/o file descriptors in programs linked with libc_r with flags other than the default ones. This kept, inter alia, freopen() from working correctly when reopening standard streams. reviewed by: deischen PR: misc/39377 --- lib/libc_r/uthread/uthread_dup2.c | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/libc_r/uthread/uthread_dup2.c b/lib/libc_r/uthread/uthread_dup2.c index af0e32aff5b5..d8cead7f3c62 100644 --- a/lib/libc_r/uthread/uthread_dup2.c +++ b/lib/libc_r/uthread/uthread_dup2.c @@ -58,21 +58,32 @@ _dup2(int fd, int newfd) if (!(newfd_opened = (_thread_fd_table[newfd] != NULL)) || (ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) { /* Perform the 'dup2' syscall: */ - if ((ret = __sys_dup2(fd, newfd)) < 0) { - } - /* Initialise the file descriptor table entry: */ - else if (_thread_fd_table_init(ret) != 0) { - /* Quietly close the file: */ - __sys_close(ret); - - /* Reset the file descriptor: */ - ret = -1; - } else { + ret = __sys_dup2(fd, newfd); + if (ret >= 0) { /* - * Save the file open flags so that they can - * be checked later: + * If we are duplicating one of the standard + * file descriptors, update its flags in the + * table of pthread stdio descriptor flags. */ - _thread_fd_table[ret]->flags = _thread_fd_table[fd]->flags; + if (ret < 3) { + _pthread_stdio_flags[ret] = + _thread_fd_table[fd]->flags; + } + /* Initialise the file descriptor table entry */ + if (_thread_fd_table_init(ret) != 0) { + /* Quietly close the file: */ + __sys_close(ret); + + /* Reset the file descriptor: */ + ret = -1; + } else { + /* + * Save the file open flags so that + * they can be checked later: + */ + _thread_fd_table[ret]->flags = + _thread_fd_table[fd]->flags; + } } /* Unlock the file descriptor: */