void rb_check_safe_str(VALUE x) { rb_check_safe_obj(x); if (TYPE(x) != T_STRING) { rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", rb_obj_classname(x)); } }
static void prompt(int argc, VALUE *argv, VALUE io) { if (argc > 0 && !NIL_P(argv[0])) { VALUE str = argv[0]; StringValueCStr(str); rb_check_safe_obj(str); rb_io_write(io, str); } }
/* call-seq: * getgrnam(name) -> Group * * Returns information about the group with specified +name+, as found in * /etc/group. * * The information is returned as a Group struct. * * See the unix manpage for <code>getgrnam(3)</code> for more detail. * * === Example: * * Etc.getgrnam('users') * #=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]> * */ static VALUE etc_getgrnam(VALUE obj, VALUE nam) { #ifdef HAVE_GETGRENT struct group *grp; const char *p = StringValueCStr(nam); rb_check_safe_obj(nam); grp = getgrnam(p); if (grp == 0) rb_raise(rb_eArgError, "can't find group for %"PRIsVALUE, nam); return setup_group(grp); #else return Qnil; #endif }
/* call-seq: * getpwnam(name) -> Passwd * * Returns the /etc/passwd information for the user with specified login * +name+. * * The information is returned as a Passwd struct. * * See the unix manpage for <code>getpwnam(3)</code> for more detail. * * === Example: * * Etc.getpwnam('root') * #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"> */ static VALUE etc_getpwnam(VALUE obj, VALUE nam) { #ifdef HAVE_GETPWENT struct passwd *pwd; const char *p = StringValueCStr(nam); rb_check_safe_obj(nam); pwd = getpwnam(p); if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %"PRIsVALUE, nam); return setup_passwd(pwd); #else return Qnil; #endif }
/* call-seq: * open(ident, options, facility) => syslog * * :yields: syslog * * Open the syslog facility. * Raises a runtime exception if it is already open. * * Can be called with or without a code block. If called with a block, the * Syslog object created is passed to the block. * * If the syslog is already open, raises a RuntimeError. * * +ident+ is a String which identifies the calling program. * * +options+ is the logical OR of any of the following: * * LOG_CONS:: If there is an error while sending to the system logger, * write directly to the console instead. * * LOG_NDELAY:: Open the connection now, rather than waiting for the first * message to be written. * * LOG_NOWAIT:: Don't wait for any child processes created while logging * messages. (Has no effect on Linux.) * * LOG_ODELAY:: Opposite of LOG_NDELAY; wait until a message is sent before * opening the connection. (This is the default.) * * LOG_PERROR:: Print the message to stderr as well as sending it to syslog. * (Not in POSIX.1-2001.) * * LOG_PID:: Include the current process ID with each message. * * +facility+ describes the type of program opening the syslog, and is * the logical OR of any of the following which are defined for the host OS: * * LOG_AUTH:: Security or authorization. Deprecated, use LOG_AUTHPRIV * instead. * * LOG_AUTHPRIV:: Security or authorization messages which should be kept * private. * * LOG_CONSOLE:: System console message. * * LOG_CRON:: System task scheduler (cron or at). * * LOG_DAEMON:: A system daemon which has no facility value of its own. * * LOG_FTP:: An FTP server. * * LOG_KERN:: A kernel message (not sendable by user processes, so not of * much use to Ruby, but listed here for completeness). * * LOG_LPR:: Line printer subsystem. * * LOG_MAIL:: Mail delivery or transport subsystem. * * LOG_NEWS:: Usenet news system. * * LOG_NTP:: Network Time Protocol server. * * LOG_SECURITY:: General security message. * * LOG_SYSLOG:: Messages generated internally by syslog. * * LOG_USER:: Generic user-level message. * * LOG_UUCP:: UUCP subsystem. * * LOG_LOCAL0 to LOG_LOCAL7:: Locally-defined facilities. * * Example: * * Syslog.open("webrick", Syslog::LOG_PID, * Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3) * */ static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self) { VALUE ident, opt, fac; const char *ident_ptr; if (syslog_opened) { rb_raise(rb_eRuntimeError, "syslog already open"); } rb_scan_args(argc, argv, "03", &ident, &opt, &fac); if (NIL_P(ident)) { ident = rb_gv_get("$0"); } ident_ptr = StringValueCStr(ident); rb_check_safe_obj(ident); syslog_ident = strdup(ident_ptr); if (NIL_P(opt)) { syslog_options = LOG_PID | LOG_CONS; } else { syslog_options = NUM2INT(opt); } if (NIL_P(fac)) { syslog_facility = LOG_USER; } else { syslog_facility = NUM2INT(fac); } openlog(syslog_ident, syslog_options, syslog_facility); syslog_opened = 1; setlogmask(syslog_mask = setlogmask(0)); /* be like File.new.open {...} */ if (rb_block_given_p()) { rb_ensure(rb_yield, self, mSyslog_close, self); } return self; }
/* call-seq: SQLite3::Database.new(file, options = {}) * * Create a new Database object that opens the given file. If utf16 * is +true+, the filename is interpreted as a UTF-16 encoded string. * * By default, the new database will return result rows as arrays * (#results_as_hash) and has type translation disabled (#type_translation=). */ static VALUE initialize(int argc, VALUE *argv, VALUE self) { sqlite3RubyPtr ctx; VALUE file; VALUE opts; VALUE zvfs; #ifdef HAVE_SQLITE3_OPEN_V2 int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; #endif int status; Data_Get_Struct(self, sqlite3Ruby, ctx); rb_scan_args(argc, argv, "12", &file, &opts, &zvfs); #if defined StringValueCStr StringValuePtr(file); rb_check_safe_obj(file); #else Check_SafeStr(file); #endif if(NIL_P(opts)) opts = rb_hash_new(); else Check_Type(opts, T_HASH); #ifdef HAVE_RUBY_ENCODING_H if(UTF16_LE_P(file) || UTF16_BE_P(file)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #endif if(Qtrue == rb_hash_aref(opts, sym_utf16)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #ifdef HAVE_RUBY_ENCODING_H if(!UTF8_P(file)) { file = rb_str_export_to_enc(file, rb_utf8_encoding()); } #endif if (Qtrue == rb_hash_aref(opts, ID2SYM(rb_intern("readonly")))) { #ifdef HAVE_SQLITE3_OPEN_V2 mode = SQLITE_OPEN_READONLY; #else rb_raise(rb_eNotImpError, "sqlite3-ruby was compiled against a version of sqlite that does not support readonly databases"); #endif } #ifdef HAVE_SQLITE3_OPEN_V2 status = sqlite3_open_v2( StringValuePtr(file), &ctx->db, mode, NIL_P(zvfs) ? NULL : StringValuePtr(zvfs) ); #else status = sqlite3_open( StringValuePtr(file), &ctx->db ); #endif } #ifdef HAVE_RUBY_ENCODING_H } #endif CHECK(ctx->db, status) rb_iv_set(self, "@tracefunc", Qnil); rb_iv_set(self, "@authorizer", Qnil); rb_iv_set(self, "@encoding", Qnil); rb_iv_set(self, "@busy_handler", Qnil); rb_iv_set(self, "@collations", rb_hash_new()); rb_iv_set(self, "@functions", rb_hash_new()); rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash)); rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation)); #ifdef HAVE_SQLITE3_OPEN_V2 rb_iv_set(self, "@readonly", mode == SQLITE_OPEN_READONLY ? Qtrue : Qfalse); #else rb_iv_set(self, "@readonly", Qfalse); #endif if(rb_block_given_p()) { rb_ensure(rb_yield, self, sqlite3_rb_close, self); } return self; }