VALUE t_do(VALUE self, VALUE in, VALUE out, VALUE size) { rb_check_type(in, T_FILE); rb_check_type(out, T_FILE); FILE *source; if (rb_funcall(rb_eval_string("Socket"), rb_intern("=="), 1, rb_class_of(in)) == Qtrue) source= RFILE(in)->fptr->f2; else source= RFILE(in)->fptr->f; FILE *destination; if (rb_funcall(rb_eval_string("Socket"), rb_intern("=="), 1, rb_class_of(out)) == Qtrue) destination= RFILE(out)->fptr->f2; else destination= RFILE(out)->fptr->f; VALUE result= INT2NUM(0); char buffer[1024]; while ((size == Qnil) || (rb_funcall(result, rb_intern("<"), 1, size) == Qtrue)) { size_t done= fread(buffer, 1, sizeof(buffer), source); if (ferror(source)) { rb_sys_fail("io2io_do(read)"); } fwrite(buffer, 1, done, destination); if (ferror(destination)) { rb_sys_fail("io2io_do(write)"); } result= rb_funcall(result, rb_intern("+"), 1, INT2NUM(done)); if (done < sizeof(buffer)) { return result; } } return result; }
static VALUE rb_io_get_write_io(VALUE io) { VALUE write_io; rb_io_check_initialized(RFILE(io)->fptr); write_io = RFILE(io)->fptr->tied_io_for_writing; if (write_io) { return write_io; } return io; }
static VALUE rb_shadow_fgetspent(VALUE self, VALUE file) { struct spwd *entry; VALUE result; if( TYPE(file) != T_FILE ) rb_raise(rb_eTypeError,"argument must be a File."); entry = fgetspent(file_ptr(RFILE(file)->fptr)); if( entry == NULL ) return Qnil; result = rb_struct_new(rb_sPasswdEntry, rb_tainted_str_new2(entry->sp_namp), rb_tainted_str_new2(entry->sp_pwdp), INT2FIX(entry->sp_lstchg), INT2FIX(entry->sp_min), INT2FIX(entry->sp_max), INT2FIX(entry->sp_warn), INT2FIX(entry->sp_inact), INT2FIX(entry->sp_expire), INT2FIX(entry->sp_flag), 0); return result; };
static VALUE rb_shadow_putspent(VALUE self, VALUE entry, VALUE file) { struct spwd centry; FILE* cfile; VALUE val[9]; int i; int result; for(i=0; i<=8; i++) val[i] = RSTRUCT_PTR(entry)[i]; cfile = file_ptr(RFILE(file)->fptr); centry.sp_namp = StringValuePtr(val[0]); centry.sp_pwdp = StringValuePtr(val[1]); centry.sp_lstchg = FIX2INT(val[2]); centry.sp_min = FIX2INT(val[3]); centry.sp_max = FIX2INT(val[4]); centry.sp_warn = FIX2INT(val[5]); centry.sp_inact = FIX2INT(val[6]); centry.sp_expire = FIX2INT(val[7]); centry.sp_flag = FIX2INT(val[8]); result = putspent(¢ry,cfile); if( result == -1 ) rb_raise(rb_eStandardError,"can't change password"); return Qtrue; };
int __stdcall CompareDirectories(TListItem *Item1, TListItem *Item2) { // Because CompareDirectories is called from each other compare functions // it's sufficient to check pointers only here (see below) assert(Item1 && RFILE(1) && Item2 && RFILE(2)); if (RFILE(1)->IsParentDirectory && !RFILE(2)->IsParentDirectory) return -1; else if (!RFILE(1)->IsParentDirectory && RFILE(2)->IsParentDirectory) return 1; else if (RFILE(1)->IsDirectory && !RFILE(2)->IsDirectory) return -1; else if (!RFILE(1)->IsDirectory && RFILE(2)->IsDirectory) return 1; else return 0; }
static VALUE fopen_r(VALUE name, VALUE permissions) { FILE * f; Check_Type(name, T_STRING); Check_Type(permissions, T_STRING); f = fopen(RSTRING_PTR(name), RSTRING_PTR(permissions)); if (0 == f) return Qnil; else return RFILE(f); // return (0 == f) ? Qnil : RFILE(*f); }
static VALUE rb_shadow_fgetspent(VALUE self, VALUE file) { struct spwd *entry; VALUE result; if( TYPE(file) != T_FILE ) rb_raise(rb_eTypeError,"argument must be a File."); entry = fgetspent( file_ptr( (RFILE(file)->fptr) ) ); if( entry == NULL ) return Qnil; result = convert_pw_struct( entry ); return result; };
/* * call-seq: * IO.console -> #<File:/dev/tty> * * Returns an File instance opened console. * * You must require 'io/console' to use this method. */ 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 (RB_TYPE_P(con, T_FILE)) { if ((fptr = RFILE(con)->fptr) && GetReadFD(fptr) != -1) return con; } rb_mod_remove_const(klass, ID2SYM(id_console)); } { VALUE args[2]; #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H # define CONSOLE_DEVICE "/dev/tty" #elif defined _WIN32 # define CONSOLE_DEVICE "con$" # define CONSOLE_DEVICE_FOR_READING "conin$" # define CONSOLE_DEVICE_FOR_WRITING "conout$" #endif #ifndef CONSOLE_DEVICE_FOR_READING # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE #endif #ifdef CONSOLE_DEVICE_FOR_WRITING VALUE out; rb_io_t *ofptr; #endif int fd; #ifdef CONSOLE_DEVICE_FOR_WRITING fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY, 0); 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); #endif fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0); if (fd < 0) { #ifdef CONSOLE_DEVICE_FOR_WRITING rb_io_close(out); #endif 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); #ifdef HAVE_RUBY_IO_H fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE)); #else fptr->path = ruby_strdup(CONSOLE_DEVICE); #endif #ifdef CONSOLE_DEVICE_FOR_WRITING GetOpenFile(out, ofptr); # ifdef HAVE_RB_IO_GET_WRITE_IO ofptr->pathv = fptr->pathv; fptr->tied_io_for_writing = out; # else fptr->f2 = ofptr->f; ofptr->f = 0; # endif ofptr->mode |= FMODE_SYNC; #endif fptr->mode |= FMODE_SYNC; rb_const_set(klass, id_console, con); } return con; }
static size_t memsize_of(VALUE obj) { size_t size = 0; if (SPECIAL_CONST_P(obj)) { return 0; } if (FL_TEST(obj, FL_EXIVAR)) { size += rb_generic_ivar_memsize(obj); } switch (BUILTIN_TYPE(obj)) { case T_OBJECT: if (!(RBASIC(obj)->flags & ROBJECT_EMBED) && ROBJECT(obj)->as.heap.ivptr) { size += ROBJECT(obj)->as.heap.numiv * sizeof(VALUE); } break; case T_MODULE: case T_CLASS: size += st_memsize(RCLASS_M_TBL(obj)); if (RCLASS_IV_TBL(obj)) { size += st_memsize(RCLASS_IV_TBL(obj)); } if (RCLASS_IV_INDEX_TBL(obj)) { size += st_memsize(RCLASS_IV_INDEX_TBL(obj)); } if (RCLASS(obj)->ptr->iv_tbl) { size += st_memsize(RCLASS(obj)->ptr->iv_tbl); } if (RCLASS(obj)->ptr->const_tbl) { size += st_memsize(RCLASS(obj)->ptr->const_tbl); } size += sizeof(rb_classext_t); break; case T_STRING: size += rb_str_memsize(obj); break; case T_ARRAY: size += rb_ary_memsize(obj); break; case T_HASH: if (RHASH(obj)->ntbl) { size += st_memsize(RHASH(obj)->ntbl); } break; case T_REGEXP: if (RREGEXP(obj)->ptr) { size += onig_memsize(RREGEXP(obj)->ptr); } break; case T_DATA: size += rb_objspace_data_type_memsize(obj); break; case T_MATCH: if (RMATCH(obj)->rmatch) { struct rmatch *rm = RMATCH(obj)->rmatch; size += sizeof(struct re_registers); /* TODO: onig_region_memsize(&rm->regs); */ size += sizeof(struct rmatch_offset) * rm->char_offset_num_allocated; size += sizeof(struct rmatch); } break; case T_FILE: if (RFILE(obj)->fptr) { size += rb_io_memsize(RFILE(obj)->fptr); } break; case T_RATIONAL: case T_COMPLEX: break; case T_ICLASS: /* iClass shares table with the module */ break; case T_FLOAT: break; case T_BIGNUM: if (!(RBASIC(obj)->flags & RBIGNUM_EMBED_FLAG) && RBIGNUM_DIGITS(obj)) { size += RBIGNUM_LEN(obj) * sizeof(BDIGIT); } break; case T_NODE: switch (nd_type(obj)) { case NODE_SCOPE: if (RNODE(obj)->u1.tbl) { /* TODO: xfree(RANY(obj)->as.node.u1.tbl); */ } break; case NODE_ALLOCA: /* TODO: xfree(RANY(obj)->as.node.u1.node); */ ; } break; /* no need to free iv_tbl */ case T_STRUCT: if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 && RSTRUCT(obj)->as.heap.ptr) { size += sizeof(VALUE) * RSTRUCT_LEN(obj); } break; case T_ZOMBIE: break; default: rb_bug("objspace/memsize_of(): unknown data type 0x%x(%p)", BUILTIN_TYPE(obj), (void*)obj); } return size; }
FILE *get_io_ptr(VALUE self) { VALUE io; io = rb_iv_get(self,"@io"); return rb_io_stdio_file(RFILE(io)->fptr); }
/* * call-seq: * IO.console -> #<File:/dev/tty> * IO.console(sym, *args) * * Returns an File instance opened console. * * If +sym+ is given, it will be sent to the opened console with * +args+ and the result will be returned instead of the console IO * itself. * * You must require 'io/console' to use this method. */ static VALUE console_dev(int argc, VALUE *argv, VALUE klass) { VALUE con = 0; rb_io_t *fptr; VALUE sym = 0; rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS); if (argc) { Check_Type(sym = argv[0], T_SYMBOL); } if (klass == rb_cIO) klass = rb_cFile; if (rb_const_defined(klass, id_console)) { con = rb_const_get(klass, id_console); if (!RB_TYPE_P(con, T_FILE) || (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1)) { rb_const_remove(klass, id_console); con = 0; } } if (sym) { if (sym == ID2SYM(id_close) && argc == 1) { if (con) { rb_io_close(con); rb_const_remove(klass, id_console); con = 0; } return Qnil; } } if (!con) { VALUE args[2]; #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H # define CONSOLE_DEVICE "/dev/tty" #elif defined _WIN32 # define CONSOLE_DEVICE "con$" # define CONSOLE_DEVICE_FOR_READING "conin$" # define CONSOLE_DEVICE_FOR_WRITING "conout$" #endif #ifndef CONSOLE_DEVICE_FOR_READING # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE #endif #ifdef CONSOLE_DEVICE_FOR_WRITING VALUE out; rb_io_t *ofptr; #endif int fd; #ifdef CONSOLE_DEVICE_FOR_WRITING fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0); 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); #endif fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0); if (fd < 0) { #ifdef CONSOLE_DEVICE_FOR_WRITING rb_io_close(out); #endif 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)); #ifdef CONSOLE_DEVICE_FOR_WRITING GetOpenFile(out, ofptr); ofptr->pathv = fptr->pathv; fptr->tied_io_for_writing = out; ofptr->mode |= FMODE_SYNC; #endif fptr->mode |= FMODE_SYNC; rb_const_set(klass, id_console, con); } if (sym) { return rb_f_send(argc, argv, con); } return con; }