changeset 9953:5605642e7756

Port to native Windows platforms.
author Bruno Haible <bruno@clisp.org>
date Mon, 21 Apr 2008 23:47:45 +0200
parents 08dc00159dba
children 0f61f5040489
files ChangeLog lib/isapipe.c
diffstat 2 files changed, 41 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2008-04-20  Bruno Haible  <bruno@clisp.org>
+
+	* lib/isapipe.c: Port to native Windows platforms.
+
 2008-04-20  Bruno Haible  <bruno@clisp.org>
 
 	* lib/gc-gnulib.c: Include <windows.h> before <wincrypt.h>.
--- a/lib/isapipe.c
+++ b/lib/isapipe.c
@@ -1,6 +1,6 @@
 /* Test whether a file descriptor is a pipe.
 
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2008 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
@@ -22,15 +22,42 @@
 #include "isapipe.h"
 
 #include <errno.h>
-#include <stdbool.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
+
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+/* Windows platforms.  */
+
+/* Get _get_osfhandle.  */
+# include <io.h>
+
+/* Get GetFileType.  */
+# include <windows.h>
+
+int
+isapipe (int fd)
+{
+  HANDLE h = (HANDLE) _get_osfhandle (fd);
+
+  if (h == INVALID_HANDLE_VALUE)
+    {
+      errno = EBADF;
+      return -1;
+    }
+
+  return (GetFileType (h) == FILE_TYPE_PIPE);
+}
+
+#else
+/* Unix platforms.  */
+
+# include <stdbool.h>
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <unistd.h>
 
 /* The maximum link count for pipes; (nlink_t) -1 if not known.  */
-#ifndef PIPE_LINK_COUNT_MAX
-# define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
-#endif
+# ifndef PIPE_LINK_COUNT_MAX
+#  define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
+# endif
 
 /* Return 1 if FD is a pipe, 0 if not, -1 (setting errno) on error.
 
@@ -88,3 +115,5 @@
     (st.st_nlink <= pipe_link_count_max
      && (check_for_fifo ? S_ISFIFO (st.st_mode) : S_ISSOCK (st.st_mode)));
 }
+
+#endif