changeset 17826:68470530f583

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.
author KO Myung-Hun <komh78@gmail.com>
date Fri, 05 Dec 2014 15:01:36 +0900
parents 37e2854ec84c
children 1e216edfab95
files ChangeLog lib/freopen.c m4/freopen.m4
diffstat 3 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2014-12-08  KO Myung-Hun  <komh78@gmail.com>
 
+	freopen: workaround freopen() on OS/2 kLIBC
+	* lib/freopen.c (rpl_freopen): Workaround.
+	* m4/freopen.m4: Add os2* case.
+
 	get_shared_library_fullname: port to EMX
 	* lib/relocatable.c: Define strcmp and strncmp to stricmp and strnicmp
 	on EMX, respectively.
--- 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 @@
 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;
 }
--- 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_REQUIRE([gl_STDIO_H_DEFAULTS])
   AC_REQUIRE([AC_CANONICAL_HOST])
   case "$host_os" in
-    mingw* | pw*)
+    mingw* | pw* | os2*)
       REPLACE_FREOPEN=1
       ;;
   esac