Bruno Haible wrote: > KO Myung-Hun wrote: >> --- a/lib/freopen.c >> +++ b/lib/freopen.c >> @@ -26,10 +26,26 @@ >> #include <stdio.h> >> #undef __need_FILE >> >> +#include <errno.h> >> + >> static FILE * >> orig_freopen (const char *filename, const char *mode, FILE *stream) >> { >> - return freopen (filename, mode, stream); >> + FILE *result; >> + >> + /* Clear errno to check the success of freopen() with it */ >> + errno = 0; >> + >> + result = 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; >> } >> >> /* Specification. */ > > Just a question of coding style: We have a chain of invocations > rpl_freopen -> orig_freopen -> freopen > While, in theory, you can put workarounds in either rpl_freopen or > orig_freopen, > the function name 'orig_freopen' and the comment > /* Get the original definition of freopen. > indicate that 'orig_freopen' should remain a simple invocation of freopen, > and the workaround should go into rpl_freopen. This is the canonical way we've > been using in gnulib (see fopen.c, fstat.c, lstat.c, open.c, openat.c etc.). > I see no reason to abandon this convention. >
No problem. I also don't want to break the convention. Fixed. -- KO Myung-Hun Using Mozilla SeaMonkey 2.7.2 Under OS/2 Warp 4 for Korean with FixPak #15 In VirtualBox v4.1.32 on Intel Core i7-3615QM 2.30GHz with 8GB RAM Korean OS/2 User Community : http://www.ecomstation.co.kr
>From 12eb804c672742f9b92d5204116e198919a8a585 Mon Sep 17 00:00:00 2001 From: KO Myung-Hun <k...@chollian.net> Date: Wed, 3 Sep 2014 20:47:02 +0900 Subject: [PATCH] freopen: workaround freopen() on OS/2 kLIBC On OS/2 kLIBC, freopen() returns NULL even if it is successful if filename is NULL. * lib/freopen.c (rpl_freopen): Workaround. * m4/freopen.m4: Add os2* case. --- lib/freopen.c | 18 +++++++++++++++++- m4/freopen.m4 | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/freopen.c b/lib/freopen.c index 384eba6..0d582bf 100644 --- a/lib/freopen.c +++ b/lib/freopen.c @@ -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; } diff --git a/m4/freopen.m4 b/m4/freopen.m4 index 69e4523..cc53f9c 100644 --- a/m4/freopen.m4 +++ b/m4/freopen.m4 @@ -1,4 +1,4 @@ -# freopen.m4 serial 4 +# freopen.m4 serial 5 dnl Copyright (C) 2007-2014 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -9,7 +9,7 @@ AC_DEFUN([gl_FUNC_FREOPEN], AC_REQUIRE([gl_STDIO_H_DEFAULTS]) AC_REQUIRE([AC_CANONICAL_HOST]) case "$host_os" in - mingw* | pw*) + mingw* | pw* | os2*) REPLACE_FREOPEN=1 ;; esac -- 1.8.5.2