Module: Syscall

Defined in:
ext/syscall/syscall.c

Constant Summary collapse

O_RDONLY =
INT2NUM(O_RDONLY)
O_WRONLY =
INT2NUM(O_WRONLY)
O_RDWR =
INT2NUM(O_RDWR)
O_CREAT =
INT2NUM(O_CREAT)
O_TRUNC =
INT2NUM(O_TRUNC)

Class Method Summary collapse

Class Method Details

.close(fd) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'ext/syscall/syscall.c', line 64

static VALUE rb_syscall_close(VALUE self, VALUE fd)
{
    long ret = syscall(
        SYS_close,
        NUM2LONG(fd)
    );

    return LONG2NUM(ret);
}

.openat(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/syscall/syscall.c', line 39

static VALUE rb_syscall_openat(int argc, VALUE *argv, VALUE self)
{
    VALUE path;
    VALUE flags;
    VALUE mode;

    rb_scan_args(argc, argv, "21",
                 &path,
                 &flags,
                 &mode);

    if (NIL_P(mode))
        mode = INT2NUM(0644);

    long fd = syscall(
        SYS_openat,
        AT_FDCWD,
        StringValueCStr(path),
        NUM2LONG(flags),
        NUM2LONG(mode)
    );

    return LONG2NUM(fd);
}

.read(fd, size) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'ext/syscall/syscall.c', line 18

static VALUE rb_syscall_read(VALUE self, VALUE fd, VALUE size)
{
    long len = NUM2LONG(size);

    VALUE str = rb_str_new(NULL, len);

    long ret = syscall(
        SYS_read,
        NUM2LONG(fd),
        RSTRING_PTR(str),
        len
    );

    if (ret < 0)
        return Qnil;

    rb_str_set_len(str, ret);

    return str;
}

.write(fd, str) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'ext/syscall/syscall.c', line 6

static VALUE rb_syscall_write(VALUE self, VALUE fd, VALUE str)
{
    long ret = syscall(
        SYS_write,
        NUM2LONG(fd),
        RSTRING_PTR(str),
        RSTRING_LEN(str)
    );

    return LONG2NUM(ret);
}