diff --git a/ChangeLog b/ChangeLog index 42e7eb8e5f..b398c61bec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Feb 27 10:50:23 2012 Nobuyoshi Nakada + + * io.c, process.c, time.c, ext: use rb_sys_fail_str instead of + rb_sys_fail. + Mon Feb 27 10:48:49 2012 Nobuyoshi Nakada * ext/openssl/extconf.rb: suppress useless deprecation warnings diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 2c72143168..f9b08cb622 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -6587,7 +6587,7 @@ d_lite_hash(VALUE self) #include "date_tmx.h" static void set_tmx(VALUE, struct tmx *); -static VALUE strftimev(const char *, VALUE, +static VALUE strftimev(VALUE, const char *, VALUE, void (*)(VALUE, struct tmx *)); /* @@ -6604,7 +6604,7 @@ static VALUE strftimev(const char *, VALUE, static VALUE d_lite_to_s(VALUE self) { - return strftimev("%Y-%m-%d", self, set_tmx); + return strftimev(Qnil, "%Y-%m-%d", self, set_tmx); } #ifndef NDEBUG @@ -6729,7 +6729,7 @@ size_t date_strftime(char *s, size_t maxsize, const char *format, #define SMALLBUF 100 static size_t -date_strftime_alloc(char **buf, const char *format, +date_strftime_alloc(char **buf, VALUE formatv, const char *format, struct tmx *tmx) { size_t size, len, flen; @@ -6756,6 +6756,7 @@ date_strftime_alloc(char **buf, const char *format, if (len > 0) break; xfree(*buf); if (size >= 1024 * flen) { + if (!NIL_P(formatv)) rb_sys_fail_str(formatv); rb_sys_fail(format); break; } @@ -6868,7 +6869,7 @@ date_strftime_internal(int argc, VALUE *argv, VALUE self, str = rb_str_new(0, 0); while (p < pe) { - len = date_strftime_alloc(&buf, p, &tmx); + len = date_strftime_alloc(&buf, vfmt, p, &tmx); rb_str_cat(str, buf, len); p += strlen(p); if (buf != buffer) { @@ -6883,7 +6884,7 @@ date_strftime_internal(int argc, VALUE *argv, VALUE self, return str; } else - len = date_strftime_alloc(&buf, fmt, &tmx); + len = date_strftime_alloc(&buf, vfmt, fmt, &tmx); str = rb_str_new(buf, len); if (buf != buffer) xfree(buf); @@ -7077,7 +7078,7 @@ d_lite_strftime(int argc, VALUE *argv, VALUE self) } static VALUE -strftimev(const char *fmt, VALUE self, +strftimev(VALUE vfmt, const char *fmt, VALUE self, void (*func)(VALUE, struct tmx *)) { char buffer[SMALLBUF], *buf = buffer; @@ -7086,7 +7087,7 @@ strftimev(const char *fmt, VALUE self, VALUE str; (*func)(self, &tmx); - len = date_strftime_alloc(&buf, fmt, &tmx); + len = date_strftime_alloc(&buf, vfmt, fmt, &tmx); str = rb_usascii_str_new(buf, len); if (buf != buffer) xfree(buf); return str; @@ -7105,7 +7106,7 @@ strftimev(const char *fmt, VALUE self, static VALUE d_lite_asctime(VALUE self) { - return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx); + return strftimev(Qnil, "%a %b %e %H:%M:%S %Y", self, set_tmx); } /* @@ -7118,7 +7119,7 @@ d_lite_asctime(VALUE self) static VALUE d_lite_iso8601(VALUE self) { - return strftimev("%Y-%m-%d", self, set_tmx); + return strftimev(Qnil, "%Y-%m-%d", self, set_tmx); } /* @@ -7130,7 +7131,7 @@ d_lite_iso8601(VALUE self) static VALUE d_lite_rfc3339(VALUE self) { - return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx); + return strftimev(Qnil, "%Y-%m-%dT%H:%M:%S%:z", self, set_tmx); } /* @@ -7143,7 +7144,7 @@ d_lite_rfc3339(VALUE self) static VALUE d_lite_rfc2822(VALUE self) { - return strftimev("%a, %-d %b %Y %T %z", self, set_tmx); + return strftimev(Qnil, "%a, %-d %b %Y %T %z", self, set_tmx); } /* @@ -7157,7 +7158,7 @@ static VALUE d_lite_httpdate(VALUE self) { volatile VALUE dup = dup_obj_with_new_offset(self, 0); - return strftimev("%a, %d %b %Y %T GMT", dup, set_tmx); + return strftimev(Qnil, "%a, %d %b %Y %T GMT", dup, set_tmx); } static VALUE @@ -7204,7 +7205,7 @@ d_lite_jisx0301(VALUE self) get_d1(self); s = jisx0301_date(m_real_local_jd(dat), m_real_year(dat)); - return strftimev(RSTRING_PTR(s), self, set_tmx); + return strftimev(s, RSTRING_PTR(s), self, set_tmx); } #ifndef NDEBUG @@ -8294,7 +8295,7 @@ datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass) static VALUE dt_lite_to_s(VALUE self) { - return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx); + return strftimev(Qnil, "%Y-%m-%dT%H:%M:%S%:z", self, set_tmx); } /* @@ -8500,7 +8501,7 @@ iso8601_timediv(VALUE self, VALUE n) rb_str_append(fmt, rb_f_sprintf(3, argv)); } rb_str_append(fmt, rb_usascii_str_new2("%:z")); - return strftimev(RSTRING_PTR(fmt), self, set_tmx); + return strftimev(fmt, RSTRING_PTR(fmt), self, set_tmx); } /* @@ -8526,7 +8527,7 @@ dt_lite_iso8601(int argc, VALUE *argv, VALUE self) if (argc < 1) n = INT2FIX(0); - return f_add(strftimev("%Y-%m-%d", self, set_tmx), + return f_add(strftimev(Qnil, "%Y-%m-%d", self, set_tmx), iso8601_timediv(self, n)); } @@ -8574,7 +8575,7 @@ dt_lite_jisx0301(int argc, VALUE *argv, VALUE self) get_d1(self); s = jisx0301_date(m_real_local_jd(dat), m_real_year(dat)); - return rb_str_append(strftimev(RSTRING_PTR(s), self, set_tmx), + return rb_str_append(strftimev(s, RSTRING_PTR(s), self, set_tmx), iso8601_timediv(self, n)); } } diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c index dbf1024181..4bc1ce3e40 100644 --- a/ext/dbm/dbm.c +++ b/ext/dbm/dbm.c @@ -206,7 +206,7 @@ fdbm_initialize(int argc, VALUE *argv, VALUE obj) if (!dbm) { if (mode == -1) return Qnil; - rb_sys_fail(RSTRING_PTR(file)); + rb_sys_fail_str(file); } dbmp = ALLOC(struct dbmdata); diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c index 328feef0e9..9d958d54a2 100644 --- a/ext/gdbm/gdbm.c +++ b/ext/gdbm/gdbm.c @@ -243,7 +243,7 @@ fgdbm_initialize(int argc, VALUE *argv, VALUE obj) if (gdbm_errno == GDBM_FILE_OPEN_ERROR || gdbm_errno == GDBM_CANT_BE_READER || gdbm_errno == GDBM_CANT_BE_WRITER) - rb_sys_fail(RSTRING_PTR(file)); + rb_sys_fail_str(file); else rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); } diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 125e82d624..32d7f92011 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -105,8 +105,8 @@ static VALUE rb_eIconvOutOfRange; static VALUE rb_eIconvBrokenLibrary; static ID rb_success, rb_failed; -static VALUE iconv_fail _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)); -static VALUE iconv_fail_retry _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg)); +static VALUE iconv_fail _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, VALUE mesg)); +static VALUE iconv_fail_retry _((VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, VALUE mesg)); static VALUE iconv_failure_initialize _((VALUE error, VALUE mesg, VALUE success, VALUE failed)); static VALUE iconv_failure_success _((VALUE self)); static VALUE iconv_failure_failed _((VALUE self)); @@ -174,14 +174,23 @@ map_charset(VALUE *code) return StringValuePtr(*code); } +NORETURN(static void rb_iconv_sys_fail_str(VALUE msg)); +static void +rb_iconv_sys_fail_str(VALUE msg) +{ + if (errno == 0) { + rb_exc_raise(iconv_fail(rb_eIconvBrokenLibrary, Qnil, Qnil, NULL, msg)); + } + rb_sys_fail_str(msg); +} + +#define rb_sys_fail_str(s) rb_iconv_sys_fail_str(s) + NORETURN(static void rb_iconv_sys_fail(const char *s)); static void rb_iconv_sys_fail(const char *s) { - if (errno == 0) { - rb_exc_raise(iconv_fail(rb_eIconvBrokenLibrary, Qnil, Qnil, NULL, s)); - } - rb_sys_fail(s); + rb_iconv_sys_fail_str(rb_str_new_cstr(s)); } #define rb_sys_fail(s) rb_iconv_sys_fail(s) @@ -237,16 +246,11 @@ iconv_create(VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx) } { const char *s = inval ? "invalid encoding " : "iconv"; - volatile VALUE msg = rb_str_new(0, strlen(s) + RSTRING_LEN(to) + - RSTRING_LEN(from) + 8); - - sprintf(RSTRING_PTR(msg), "%s(\"%s\", \"%s\")", - s, RSTRING_PTR(to), RSTRING_PTR(from)); - s = RSTRING_PTR(msg); - rb_str_set_len(msg, strlen(s)); - if (!inval) rb_sys_fail(s); + VALUE msg = rb_sprintf("%s(\"%s\", \"%s\")", + s, RSTRING_PTR(to), RSTRING_PTR(from)); + if (!inval) rb_sys_fail_str(msg); rb_exc_raise(iconv_fail(rb_eIconvInvalidEncoding, Qnil, - rb_ary_new3(2, to, from), NULL, s)); + rb_ary_new3(2, to, from), NULL, msg)); } } @@ -363,12 +367,12 @@ iconv_failure_initialize(VALUE error, VALUE mesg, VALUE success, VALUE failed) } static VALUE -iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg) +iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, VALUE mesg) { VALUE args[3]; - if (mesg && *mesg) { - args[0] = rb_str_new2(mesg); + if (!NIL_P(mesg)) { + args[0] = mesg; } else if (TYPE(failed) != T_STRING || RSTRING_LEN(failed) < FAILED_MAXLEN) { args[0] = rb_inspect(failed); @@ -390,7 +394,7 @@ iconv_fail(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, co } static VALUE -iconv_fail_retry(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, const char *mesg) +iconv_fail_retry(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env, VALUE mesg) { error = iconv_fail(error, success, failed, env, mesg); if (!rb_block_given_p()) rb_exc_raise(error); @@ -438,7 +442,7 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen); if (RTEST(error)) { unsigned int i; - rescue = iconv_fail_retry(error, Qnil, Qnil, env, 0); + rescue = iconv_fail_retry(error, Qnil, Qnil, env, Qnil); if (TYPE(rescue) == T_ARRAY) { str = RARRAY_LEN(rescue) > 0 ? RARRAY_PTR(rescue)[0] : Qnil; } @@ -469,12 +473,11 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct inlen = length; do { - char errmsg[50]; + VALUE errmsg = Qnil; const char *tmpstart = inptr; outptr = buffer; outlen = sizeof(buffer); - errmsg[0] = 0; error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen); if ( @@ -511,7 +514,7 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct } else { /* Some iconv() have a bug, return *outlen out of range */ - sprintf(errmsg, "bug?(output length = %ld)", (long)(sizeof(buffer) - outlen)); + errmsg = rb_sprintf("bug?(output length = %ld)", (long)(sizeof(buffer) - outlen)); error = rb_eIconvOutOfRange; } diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c index 7ba59c4e5c..f138d24e23 100644 --- a/ext/sdbm/init.c +++ b/ext/sdbm/init.c @@ -109,7 +109,7 @@ fsdbm_initialize(int argc, VALUE *argv, VALUE obj) if (!dbm) { if (mode == -1) return Qnil; - rb_sys_fail(RSTRING_PTR(file)); + rb_sys_fail_str(file); } dbmp = ALLOC(struct dbmdata); diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c index b997043620..c1ae88d80c 100644 --- a/ext/socket/basicsocket.c +++ b/ext/socket/basicsocket.c @@ -247,7 +247,7 @@ bsock_setsockopt(int argc, VALUE *argv, VALUE sock) break; } -#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path)) +#define rb_sys_fail_path(path) rb_sys_fail_str(path) rb_io_check_closed(fptr); if (setsockopt(fptr->fd, level, option, v, vlen) < 0) diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c index 2954cef433..afa47d54f6 100644 --- a/ext/socket/unixsocket.c +++ b/ext/socket/unixsocket.c @@ -62,7 +62,7 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server) if (status < 0) { close(fd); - rb_sys_fail(sockaddr.sun_path); + rb_sys_fail_str(path); } if (server) { diff --git a/include/ruby/intern.h b/include/ruby/intern.h index e0c4710132..b42e17ca65 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -585,6 +585,7 @@ struct rb_exec_arg { const char *prog; VALUE options; VALUE redirect_fds; + VALUE progname; }; int rb_proc_exec_n(int, VALUE*, const char*); int rb_proc_exec(const char*); diff --git a/io.c b/io.c index e7116cd824..ab1e6e1e1c 100644 --- a/io.c +++ b/io.c @@ -5454,13 +5454,13 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, switch (fmode & (FMODE_READABLE|FMODE_WRITABLE)) { case FMODE_READABLE|FMODE_WRITABLE: if (rb_pipe(arg.write_pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (rb_pipe(arg.pair) < 0) { int e = errno; close(arg.write_pair[0]); close(arg.write_pair[1]); errno = e; - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } if (eargp) { rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.write_pair[0])); @@ -5469,18 +5469,18 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, break; case FMODE_READABLE: if (rb_pipe(arg.pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (eargp) rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(arg.pair[1])); break; case FMODE_WRITABLE: if (rb_pipe(arg.pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (eargp) rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(arg.pair[0])); break; default: - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } if (eargp) { rb_exec_arg_fixup(arg.execp); @@ -5509,7 +5509,7 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, errno = e; if (errmsg[0]) rb_sys_fail(errmsg); - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } if ((fmode & FMODE_READABLE) && (fmode & FMODE_WRITABLE)) { close(arg.pair[1]); @@ -5542,13 +5542,13 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, switch (fmode & (FMODE_READABLE|FMODE_WRITABLE)) { case FMODE_READABLE|FMODE_WRITABLE: if (rb_pipe(write_pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (rb_pipe(pair) < 0) { int e = errno; close(write_pair[0]); close(write_pair[1]); errno = e; - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } if (eargp) { rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(write_pair[0])); @@ -5557,18 +5557,18 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, break; case FMODE_READABLE: if (rb_pipe(pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (eargp) rb_exec_arg_addopt(eargp, INT2FIX(1), INT2FIX(pair[1])); break; case FMODE_WRITABLE: if (rb_pipe(pair) < 0) - rb_sys_fail(cmd); + rb_sys_fail_str(prog); if (eargp) rb_exec_arg_addopt(eargp, INT2FIX(0), INT2FIX(pair[0])); break; default: - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } if (eargp) { rb_exec_arg_fixup(eargp); @@ -5597,7 +5597,7 @@ pipe_open(struct rb_exec_arg *eargp, VALUE prog, const char *modestr, int fmode, close(write_pair[1]); } errno = e; - rb_sys_fail(cmd); + rb_sys_fail_str(prog); } break; } @@ -7967,7 +7967,7 @@ do_io_advise(rb_io_t *fptr, VALUE advice, off_t offset, off_t len) if (rv) { /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise it returns the error code. */ - rb_syserr_fail(rv, RSTRING_PTR(fptr->pathv)); + rb_syserr_fail_str(rv, fptr->pathv); } return Qnil; diff --git a/process.c b/process.c index 027e4e6ed4..ba7bee19f0 100644 --- a/process.c +++ b/process.c @@ -1798,6 +1798,7 @@ rb_exec_fillarg(VALUE prog, int argc, VALUE *argv, VALUE env, VALUE opthash, str e->argc = argc; e->argv = argv; e->prog = prog ? RSTRING_PTR(prog) : 0; + e->progname = prog; } VALUE @@ -1875,7 +1876,7 @@ rb_f_exec(int argc, VALUE *argv) rb_exec_err(&earg, errmsg, sizeof(errmsg)); if (errmsg[0]) rb_sys_fail(errmsg); - rb_sys_fail(earg.prog); + rb_sys_fail_str(earg.progname); return Qnil; /* dummy */ } @@ -2326,6 +2327,7 @@ rb_run_exec_options_err(const struct rb_exec_arg *e, struct rb_exec_arg *s, char s->prog = NULL; s->options = soptions = hide_obj(rb_ary_new()); s->redirect_fds = Qnil; + s->progname = Qnil; } #ifdef HAVE_SETPGID @@ -3345,8 +3347,8 @@ rb_f_spawn(int argc, VALUE *argv) pid = rb_spawn_process(&earg, rb_exec_arg_prepare(&earg, argc, argv, TRUE), errmsg, sizeof(errmsg)); if (pid == -1) { const char *prog = errmsg; - if (!prog[0] && !(prog = earg.prog) && earg.argc) { - prog = RSTRING_PTR(earg.argv[0]); + if (!prog[0]) { + rb_sys_fail_str(earg.progname); } rb_sys_fail(prog); } diff --git a/time.c b/time.c index d9e446b45d..5748c142a1 100644 --- a/time.c +++ b/time.c @@ -4290,7 +4290,7 @@ time_to_a(VALUE time) #define SMALLBUF 100 static size_t -rb_strftime_alloc(char **buf, const char *format, rb_encoding *enc, +rb_strftime_alloc(char **buf, VALUE formatv, const char *format, rb_encoding *enc, struct vtm *vtm, wideval_t timew, int gmt) { size_t size, len, flen; @@ -4328,6 +4328,7 @@ rb_strftime_alloc(char **buf, const char *format, rb_encoding *enc, if (len > 0) break; xfree(*buf); if (size >= 1024 * flen) { + if (!NIL_P(formatv)) rb_sys_fail_str(formatv); rb_sys_fail(format); break; } @@ -4345,7 +4346,7 @@ strftimev(const char *fmt, VALUE time, rb_encoding *enc) GetTimeval(time, tobj); MAKE_TM(time, tobj); - len = rb_strftime_alloc(&buf, fmt, enc, &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); + len = rb_strftime_alloc(&buf, Qnil, fmt, enc, &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); str = rb_enc_str_new(buf, len, enc); if (buf != buffer) xfree(buf); return str; @@ -4563,7 +4564,8 @@ time_strftime(VALUE time, VALUE format) str = rb_str_new(0, 0); while (p < pe) { - len = rb_strftime_alloc(&buf, p, enc, &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); + len = rb_strftime_alloc(&buf, format, p, enc, + &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); rb_str_cat(str, buf, len); p += strlen(p); if (buf != buffer) { @@ -4576,7 +4578,7 @@ time_strftime(VALUE time, VALUE format) return str; } else { - len = rb_strftime_alloc(&buf, RSTRING_PTR(format), enc, + len = rb_strftime_alloc(&buf, format, RSTRING_PTR(format), enc, &tobj->vtm, tobj->timew, TIME_UTC_P(tobj)); } str = rb_enc_str_new(buf, len, enc);