class File

Public Class Methods

open_temporary(p1 = v1, p2 = v2) click to toggle source
static VALUE
open_temporary(int argc, VALUE* argv, VALUE klass)
{
    char* str = NULL;
    int   fd  = -1;
    VALUE buf = Qundef;
    VALUE dir = Qundef;
    VALUE suf = Qundef;
    int   mod = -1;

    rb_scan_args(argc, argv, "02", &dir, &suf);

    if (dir == Qnil) dir = tmpdir;
    if (suf == Qnil) suf = tmpsuf;

    SafeStringValue(dir);
    SafeStringValue(suf);

    buf = rb_sprintf("%s/%sXXXXXX", StringValueCStr(dir), StringValueCStr(suf));
    str = StringValueCStr(buf);
    if ((fd = mkstemp(str)) == -1) {
        rb_sys_fail("mkstemp(3)");
    }
    else if (unlink(str) == -1) {
        preserving_errno((void)close(fd));
        /* unlink failed, no way to reclaim */
        rb_sys_fail("unlink(2)");
    }
    else if ((mod = fcntl(fd, F_GETFL)) == -1) {
        preserving_errno((void)close(fd));
        rb_sys_fail("fcntl(2)");
    }
    else {
        int state;
        VALUE io;
        struct fdopen_args args;
        args.fd = fd;
        args.mod = mod;
        args.str = str;
        io = rb_protect(call_fdopen, (VALUE)&args, &state);
        if (state) {
            (void)close(fd);
            rb_jump_tag(state);
        }
        return io;
    }
}