static VALUE t_invoke_popen (VALUE self, VALUE cmd)
{
    // 1.8.7+
#ifdef RARRAY_LEN
    int len = RARRAY_LEN(cmd);
#else
    int len = RARRAY (cmd)->len;
#endif
    if (len >= 2048)
        rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
    char *strings [2048];
    for (int i=0; i < len; i++) {
        VALUE ix = INT2FIX (i);
        VALUE s = rb_ary_aref (1, &ix, cmd);
        strings[i] = StringValuePtr (s);
    }
    strings[len] = NULL;

    unsigned long f = 0;
    try {
        f = evma_popen (strings);
    } catch (std::runtime_error e) {
        f = 0; // raise exception below
    }
    if (!f) {
        char *err = strerror (errno);
        char buf[100];
        memset (buf, 0, sizeof(buf));
        snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
        rb_raise (rb_eRuntimeError, "%s", buf);
    }
    return ULONG2NUM (f);
}
Ejemplo n.º 2
0
static VALUE t_invoke_popen (VALUE self UNUSED, VALUE cmd)
{
	#ifdef OS_WIN32
	rb_raise (EM_eUnsupported, "popen is not available on this platform");
	#endif

	int len = RARRAY_LEN(cmd);
	if (len >= 2048)
		rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
	char *strings [2048];
	for (int i=0; i < len; i++) {
		VALUE ix = INT2FIX (i);
		VALUE s = rb_ary_aref (1, &ix, cmd);
		strings[i] = StringValueCStr (s);
	}
	strings[len] = NULL;

	uintptr_t f = 0;
	try {
		f = evma_popen (strings);
	} catch (std::runtime_error e) {
		rb_raise (rb_eRuntimeError, "%s", e.what());
	}
	if (!f) {
		char *err = strerror (errno);
		char buf[100];
		memset (buf, 0, sizeof(buf));
		snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
		rb_raise (rb_eRuntimeError, "%s", buf);
	}
	return BSIG2NUM (f);
}