[macruby-changes] [1022] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Fri Mar 20 13:01:36 PDT 2009


Revision: 1022
          http://trac.macosforge.org/projects/ruby/changeset/1022
Author:   pthomson at apple.com
Date:     2009-03-20 13:01:35 -0700 (Fri, 20 Mar 2009)
Log Message:
-----------
Implemented a preliminary version of popen().

Modified Paths:
--------------
    MacRuby/branches/experimental/include/ruby/io.h
    MacRuby/branches/experimental/io.c

Modified: MacRuby/branches/experimental/include/ruby/io.h
===================================================================
--- MacRuby/branches/experimental/include/ruby/io.h	2009-03-20 18:27:46 UTC (rev 1021)
+++ MacRuby/branches/experimental/include/ruby/io.h	2009-03-20 20:01:35 UTC (rev 1022)
@@ -27,12 +27,15 @@
     // The streams.
     CFReadStreamRef readStream;
     CFWriteStreamRef writeStream;
+    
+    // The Unixy low-level file handles.
+    int fd; // You can expect this to be what the above CFStreams point to.
+    FILE *fp; // NOTE: Only used by #popen. Don't depend on it!
 
     // Additional information.
     CFStringRef path;
     pid_t pid;
     int lineno;
-    int fd;
     bool sync;
 
     // For ungetc.

Modified: MacRuby/branches/experimental/io.c
===================================================================
--- MacRuby/branches/experimental/io.c	2009-03-20 18:27:46 UTC (rev 1021)
+++ MacRuby/branches/experimental/io.c	2009-03-20 20:01:35 UTC (rev 1022)
@@ -342,6 +342,9 @@
     else {
 	io_struct->writeStream = NULL;
     }
+    
+    io_struct->fp = NULL; 
+    io_struct->pid = -1;
 
     // TODO: Eventually make the ungetc_buf a ByteString
     io_struct->fd = fd;
@@ -1834,7 +1837,20 @@
 static VALUE
 rb_io_s_popen(VALUE klass, SEL sel, int argc, VALUE *argv)
 {
-rb_notimplement();
+    VALUE process_name, mode;
+    rb_scan_args(argc, argv, "11", &process_name, &mode);
+    if (NIL_P(mode)) {
+        mode = (VALUE)CFSTR("r");
+    }
+    
+    FILE *process = popen(StringValueCStr(process_name), RSTRING_PTR(mode));
+    if (process) {
+        rb_raise(rb_eIOError, "system call to popen() failed");
+    }
+    
+    VALUE io = prep_io(fileno(process), convert_mode_string_to_fmode(mode), klass);
+    ExtractIOStruct(io)->fp = process;
+    return io;
 }
 
 /*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090320/702b87ab/attachment.html>


More information about the macruby-changes mailing list