strtoumax: fix typo in previous commit.
[gnulib.git] / lib / nonblocking.c
index cb103be..95d7736 100644 (file)
@@ -1,5 +1,5 @@
 /* Non-blocking I/O for pipe or socket descriptors.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011-2013 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
 #include <errno.h>
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-/* Native Woe32 API.  */
+/* Native Windows API.  */
 
+# include <sys/ioctl.h>
 # include <sys/socket.h>
 # include <unistd.h>
 
-/* Get declarations of the Win32 API functions.  */
+/* Get declarations of the native Windows API functions.  */
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
 
+# include "msvc-nothrow.h"
+
 int
 get_nonblocking_flag (int desc)
 {
   HANDLE h = (HANDLE) _get_osfhandle (desc);
+  if (h == INVALID_HANDLE_VALUE)
+    {
+      errno = EBADF;
+      return -1;
+    }
   if (GetFileType (h) == FILE_TYPE_PIPE)
     {
       /* h is a pipe or socket.  */
@@ -48,7 +56,8 @@ get_nonblocking_flag (int desc)
         return -1;
     }
   else
-    /* Win32 does not support non-blocking on regular files.  */
+    /* The native Windows API does not support non-blocking on regular
+       files.  */
     return 0;
 }
 
@@ -56,6 +65,11 @@ int
 set_nonblocking_flag (int desc, bool value)
 {
   HANDLE h = (HANDLE) _get_osfhandle (desc);
+  if (h == INVALID_HANDLE_VALUE)
+    {
+      errno = EBADF;
+      return -1;
+    }
   if (GetFileType (h) == FILE_TYPE_PIPE)
     {
       /* h is a pipe or socket.  */
@@ -89,7 +103,10 @@ set_nonblocking_flag (int desc, bool value)
     }
   else
     {
-      /* Win32 does not support non-blocking on regular files.  */
+      /* The native Windows API does not support non-blocking on regular
+         files.  */
+      if (!value)
+        return 0;
       errno = ENOTSUP;
       return -1;
     }
@@ -100,7 +117,7 @@ set_nonblocking_flag (int desc, bool value)
 
 # include <fcntl.h>
 
-# if !O_NONBLOCK
+# if GNULIB_defined_O_NONBLOCK
 #  error Please port nonblocking to your platform
 # endif