]> Savannah Git Hosting - gnulib.git/commitdiff
freopen: workaround freopen() on OS/2 kLIBC
authorKO Myung-Hun <komh78@gmail.com>
Fri, 5 Dec 2014 06:01:36 +0000 (15:01 +0900)
committerEric Blake <eblake@redhat.com>
Mon, 8 Dec 2014 20:56:55 +0000 (13:56 -0700)
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.

ChangeLog
lib/freopen.c
m4/freopen.m4

index 1c61959a9fbe7265fed83f1bebed8d1aba1c88b7..31ee2400254f3939f500c4189be65f525c6b9318 100644 (file)
--- 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.
index 384eba64dd7f12756c55e83e6dbbac1ab38df520..0d582bf833a906b5935ae639340a99551e1b9254 100644 (file)
@@ -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;
 }
index 69e4523c57d6cc5bd3b865d26abd712f633ece2a..cc53f9c94424c94d1d8d108f5ef23130517f03ce 100644 (file)
@@ -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