console()
public
Returns an File instance opened console.
You must require ‘io/console’ to use this method.
Show source
static VALUE
console_dev(VALUE klass)
{
VALUE con = 0;
rb_io_t *fptr;
if (klass == rb_cIO) klass = rb_cFile;
if (rb_const_defined(klass, id_console)) {
con = rb_const_get(klass, id_console);
if (TYPE(con) == T_FILE) {
if ((fptr = RFILE(con)->fptr) && GetReadFD(fptr) != -1)
return con;
}
rb_mod_remove_const(klass, ID2SYM(id_console));
}
{
VALUE args[2];
VALUE out;
rb_io_t *ofptr;
int fd;
fd = open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY);
if (fd < 0) return Qnil;
rb_update_max_fd(fd);
args[1] = INT2FIX(O_WRONLY);
args[0] = INT2NUM(fd);
out = rb_class_new_instance(2, args, klass);
fd = open(CONSOLE_DEVICE_FOR_READING, O_RDWR);
if (fd < 0) {
rb_io_close(out);
return Qnil;
}
rb_update_max_fd(fd);
args[1] = INT2FIX(O_RDWR);
args[0] = INT2NUM(fd);
con = rb_class_new_instance(2, args, klass);
GetOpenFile(con, fptr);
fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
GetOpenFile(out, ofptr);
ofptr->pathv = fptr->pathv;
fptr->tied_io_for_writing = out;
fptr->f2 = ofptr->f;
ofptr->f = 0;
ofptr->mode |= FMODE_SYNC;
fptr->mode |= FMODE_SYNC;
rb_const_set(klass, id_console, con);
}
return con;
}