[前][次][番号順一覧][スレッド一覧]

ruby-changes:21532

From: usa <ko1@a...>
Date: Mon, 31 Oct 2011 13:16:23 +0900 (JST)
Subject: [ruby-changes:21532] usa:r33581 (trunk): * win32/win32.c (setfl): extract from fcntl().

usa	2011-10-31 13:16:11 +0900 (Mon, 31 Oct 2011)

  New Revision: 33581

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33581

  Log:
    * win32/win32.c (setfl): extract from fcntl().
    
    * win32/win32.c (dupfd): new function to support F_DUPFD. base on a
      patch written by akr.
    
    * win32/win32.c (fcntl): use above functions.
    
    * include/ruby/win32.h (F_DUPFD): define. [experimental]
    
    * include/ruby/win32.h (F_SETFL): change the value to correspond with
      other platforms.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/win32.h
    trunk/win32/win32.c

Index: include/ruby/win32.h
===================================================================
--- include/ruby/win32.h	(revision 33580)
+++ include/ruby/win32.h	(revision 33581)
@@ -569,7 +569,12 @@
 # define EREMOTE		WSAEREMOTE
 #endif
 
-#define F_SETFL 1
+#define F_DUPFD 0
+//#define F_GETFD 1
+//#define F_SETFD 2
+//#define F_GETFL 3
+#define F_SETFL 4
+//#define FD_CLOEXEC 1 /* F_GETFD, F_SETFD */
 #define O_NONBLOCK 1
 
 #undef FD_SET
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 33580)
+++ ChangeLog	(revision 33581)
@@ -1,3 +1,17 @@
+Mon Oct 31 13:10:06 2011  NAKAMURA Usaku  <usa@r...>
+
+	* win32/win32.c (setfl): extract from fcntl().
+
+	* win32/win32.c (dupfd): new function to support F_DUPFD. base on a
+	  patch written by akr.
+
+	* win32/win32.c (fcntl): use above functions.
+
+	* include/ruby/win32.h (F_DUPFD): define. [experimental]
+
+	* include/ruby/win32.h (F_SETFL): change the value to correspond with
+	  other platforms.
+
 Mon Oct 31 12:37:50 2011  Tanaka Akira  <akr@f...>
 
 	* ext/pty/pty.c (get_device_once): use O_CLOEXEC for posix_openpt if
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 33580)
+++ win32/win32.c	(revision 33581)
@@ -3766,28 +3766,13 @@
 void setservent (int stayopen) {}
 
 /* License: Ruby's */
-int
-fcntl(int fd, int cmd, ...)
+static int
+setfl(SOCKET sock, int arg)
 {
-    SOCKET sock = TO_SOCKET(fd);
-    va_list va;
-    int arg;
     int ret;
     int flag = 0;
     u_long ioctlArg;
 
-    if (!is_socket(sock)) {
-	errno = EBADF;
-	return -1;
-    }
-    if (cmd != F_SETFL) {
-	errno = EINVAL;
-	return -1;
-    }
-
-    va_start(va, cmd);
-    arg = va_arg(va, int);
-    va_end(va);
     socklist_lookup(sock, &flag);
     if (arg & O_NONBLOCK) {
 	flag |= O_NONBLOCK;
@@ -3808,6 +3793,82 @@
     return ret;
 }
 
+/* License: Ruby's */
+static int
+dupfd(HANDLE hDup, char flags, int minfd)
+{
+    int save_errno;
+    int ret;
+    int fds[32];
+    int filled = 0;
+
+    do {
+	ret = _open_osfhandle((intptr_t)hDup, flags | FOPEN);
+	if (ret == -1) {
+	    goto close_fds_and_return;
+	}
+	if (ret >= minfd) {
+	    goto close_fds_and_return;
+	}
+	fds[filled++] = ret;
+    } while (filled < (sizeof(fds)/sizeof(fds[0])));
+
+    ret = dupfd(hDup, flags, minfd);
+
+  close_fds_and_return:
+    save_errno = errno;
+    while (filled > 0) {
+	_osfhnd(fds[--filled]) = (intptr_t)INVALID_HANDLE_VALUE;
+	close(fds[filled]);
+    }
+    errno = save_errno;
+
+    return ret;
+}
+
+/* License: Ruby's */
+int
+fcntl(int fd, int cmd, ...)
+{
+    va_list va;
+    int arg;
+
+    if (cmd == F_SETFL) {
+	SOCKET sock = TO_SOCKET(fd);
+	if (!is_socket(sock)) {
+	    errno = EBADF;
+	    return -1;
+	}
+
+	va_start(va, cmd);
+	arg = va_arg(va, int);
+	va_end(va);
+	return setfl(sock, arg);
+    }
+    else if (cmd == F_DUPFD) {
+	int ret;
+	HANDLE hDup;
+	if (!(DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
+			      GetCurrentProcess(), &hDup, 0L, TRUE,
+			      DUPLICATE_SAME_ACCESS))) {
+	    errno = map_errno(GetLastError());
+	    return -1;
+	}
+
+	va_start(va, cmd);
+	arg = va_arg(va, int);
+	va_end(va);
+
+	if ((ret = dupfd(hDup, _osfile(fd), arg)) == -1)
+	    CloseHandle(hDup);
+	return ret;
+    }
+    else {
+	errno = EINVAL;
+	return -1;
+    }
+}
+
 #ifndef WNOHANG
 #define WNOHANG -1
 #endif

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]