diff options
Diffstat (limited to 'lib/freopen.c')
-rw-r--r-- | lib/freopen.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/freopen.c b/lib/freopen.c index 384eba64..7ceef765 100644 --- a/lib/freopen.c +++ b/lib/freopen.c @@ -1,5 +1,5 @@ /* Open a stream to a file. - Copyright (C) 2007-2014 Free Software Foundation, Inc. + Copyright (C) 2007-2015 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 @@ -26,6 +26,8 @@ #include <stdio.h> #undef __need_FILE +#include <errno.h> + static FILE * orig_freopen (const char *filename, const char *mode, FILE *stream) { @@ -42,10 +44,24 @@ orig_freopen (const char *filename, const char *mode, FILE *stream) FILE * rpl_freopen (const char *filename, const char *mode, FILE *stream) { + FILE *result; + #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ if (filename != NULL && strcmp (filename, "/dev/null") == 0) filename = "NUL"; #endif - return orig_freopen (filename, mode, stream); + /* Clear errno to check the success of freopen() with it */ + errno = 0; + + result = orig_freopen (filename, mode, stream); + +#ifdef __KLIBC__ + /* On OS/2 kLIBC, freopen() returns NULL even if it is successful + if filename is NULL. */ + if (!filename && !result && !errno) + result = stream; +#endif + + return result; } |