[#30722] JSON ライブラリの取り込み — "NARUSE, Yui" <naruse@...>

naruseです。

20 messages 2007/04/21

[ruby-dev:30687] Re: IO.popen("-") with no fork

From: Nobuyoshi Nakada <nobu@...>
Date: 2007-04-04 07:40:08 UTC
List: ruby-dev #30687
なかだです。

At Wed, 4 Apr 2007 11:56:05 +0900,
Yukihiro Matsumoto wrote in [ruby-dev:30686]:
> |それはそれとして、これが直っても
> |  The popen() function is unimplemented on this machine
> |とか出るのはそれはそれで紛らわしいと思うんですが、どうしたも
> |のでしょうかね。
> 
> ここはrb_notimplement()を使わないで直接NotImplementErrorを
> raiseするということにするしかないですかねえ。

ちょっと条件を整理してみました。


Index: trunk/io.c
===================================================================
--- trunk/io.c	(revision 12144)
+++ trunk/io.c	(working copy)
@@ -3057,5 +3057,5 @@ pipe_open(int argc, VALUE *argv, const c
     rb_io_t *fptr;
     VALUE port, prog;
-#if defined(HAVE_FORK) && defined(HAVE_SOCKETPAIR)
+#if defined(HAVE_FORK)
     int status;
     struct popen_arg arg;
@@ -3078,5 +3078,12 @@ pipe_open(int argc, VALUE *argv, const c
     doexec = (strcmp("-", cmd) != 0);
 
-#if defined(HAVE_FORK) && defined(HAVE_SOCKETPAIR)
+#if !defined(HAVE_FORK)
+    if (!doexec) {
+	rb_raise(rb_eNotImpError,
+		 "The fork() function is unimplemented on this machine");
+    }
+#endif
+
+#if defined(HAVE_FORK)
     if (!doexec) {
 	fflush(stdin);		/* is it really needed? */
@@ -3086,17 +3093,20 @@ pipe_open(int argc, VALUE *argv, const c
     arg.modef = modef;
     arg.pair[0] = arg.pair[1] = -1;
-    if ((modef & FMODE_READABLE) && (modef & FMODE_WRITABLE)) {
+    switch (modef & (FMODE_READABLE|FMODE_WRITABLE)) {
+#if defined(HAVE_SOCKETPAIR)
+      case FMODE_READABLE|FMODE_WRITABLE:
         if (socketpair(AF_UNIX, SOCK_STREAM, 0, arg.pair) < 0)
             rb_sys_fail(cmd);
-    }
-    else if (modef & FMODE_READABLE) {
+	break;
+#endif
+      case FMODE_READABLE:
         if (pipe(arg.pair) < 0)
             rb_sys_fail(cmd);
-    }
-    else if (modef & FMODE_WRITABLE) {
+	break;
+      case FMODE_WRITABLE:
         if (pipe(arg.pair) < 0)
             rb_sys_fail(cmd);
-    }
-    else {
+	break;
+      default:
         rb_sys_fail(cmd);
     }
@@ -3138,5 +3148,4 @@ pipe_open(int argc, VALUE *argv, const c
     }
 #elif defined(_WIN32)
-    if (!doexec) rb_notimplement();
     if (argc) {
 	char **args = ALLOCA_N(char *, argc+1);
@@ -3166,5 +3175,4 @@ pipe_open(int argc, VALUE *argv, const c
     }
 #else
-    if (!doexec) rb_notimplement();
     if (argc) {
 	prog = rb_ary_join(rb_ary_new4(argc, argv), rb_str_new2(" "));
Index: stable/io.c
===================================================================
--- stable/io.c	(revision 12144)
+++ stable/io.c	(working copy)
@@ -3027,10 +3027,27 @@ pipe_open(pstr, pname, mode)
     int modef = rb_io_mode_flags(mode);
     OpenFile *fptr;
-
 #if defined(DJGPP) || defined(__human68k__) || defined(__VMS)
     FILE *f;
+#else
+    int pid;
+#ifdef _WIN32
+    FILE *fpr, *fpw;
+#else
+    int pr[2], pw[2];
+#endif
+#endif
+    volatile int doexec;
 
     if (!pname) pname = StringValueCStr(pstr);
-    if (strcmp("-", pname) == 0) rb_notimplement();
+    doexec = (strcmp("-", pname) != 0);
+
+#if defined(DJGPP) || defined(__human68k__) || defined(__VMS) || defined(_WIN32)
+    if (!doexec) {
+	rb_raise(rb_eNotImpError,
+		 "The fork() function is unimplemented on this machine");
+    }
+#endif
+
+#if defined(DJGPP) || defined(__human68k__) || defined(__VMS)
     f = popen(pname, mode);
     
@@ -3054,9 +3071,4 @@ pipe_open(pstr, pname, mode)
 #else
 #ifdef _WIN32
-    int pid;
-    FILE *fpr, *fpw;
-
-    if (!pname) pname = StringValueCStr(pstr);
-    if (strcmp("-", pname) == 0) rb_notimplement();
 retry:
     pid = pipe_exec(pname, rb_io_mode_modenum(mode), &fpr, &fpw);
@@ -3088,14 +3100,8 @@ retry:
     }
 #else
-    int pid, pr[2], pw[2];
-    volatile int doexec;
-
-    if (!pname) pname = StringValueCStr(pstr);
-
     if (((modef & FMODE_READABLE) && pipe(pr) == -1) ||
 	((modef & FMODE_WRITABLE) && pipe(pw) == -1))
 	rb_sys_fail(pname);
 
-    doexec = (strcmp("-", pname) != 0);
     if (!doexec) {
 	fflush(stdin);		/* is it really needed? */


-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread