From 67232b21511423f82ab18db04f941e6925338a7b Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 17 Aug 2004 09:02:40 +0000 Subject: [PATCH] * io.c (rb_io_reopen): should clear allocated OpenFile. pointed out by Guy Decoux. [ruby-core:03288] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6781 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- configure.in | 2 +- io.c | 2 +- marshal.c | 1 + misc/ruby-mode.el | 2 +- process.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ string.c | 11 +++++----- 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/configure.in b/configure.in index e63195319d..453a61e5c4 100644 --- a/configure.in +++ b/configure.in @@ -418,7 +418,7 @@ AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot fsync getcwd getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\ getpriority getrlimit dlopen sigprocmask sigaction _setjmp\ setsid telldir seekdir fchmod mktime timegm cosh sinh tanh\ - setuid setgid) + setuid setgid daemon) AC_ARG_ENABLE(setreuid, [ --enable-setreuid use setreuid()/setregid() according to need even if obsolete.], [use_setreuid=$enableval]) diff --git a/io.c b/io.c index 4bb90f18e8..29839d376c 100644 --- a/io.c +++ b/io.c @@ -3369,6 +3369,7 @@ rb_io_reopen(argc, argv, file) fptr = RFILE(file)->fptr; if (!fptr) { fptr = RFILE(file)->fptr = ALLOC(OpenFile); + MEMZERO(fptr, OpenFile, 1); } if (!NIL_P(nmode)) { @@ -3392,7 +3393,6 @@ rb_io_reopen(argc, argv, file) fclose(fptr->f2); fptr->f2 = 0; } - return file; } diff --git a/marshal.c b/marshal.c index 90c0788b83..473aee9ccd 100644 --- a/marshal.c +++ b/marshal.c @@ -871,6 +871,7 @@ r_bytes0(len, arg) { VALUE str; + if (len == 0) return rb_str_new(0, 0); if (!arg->end) { VALUE src = (VALUE)arg->ptr; VALUE n = LONG2NUM(len); diff --git a/misc/ruby-mode.el b/misc/ruby-mode.el index e4998cc594..462ec609be 100644 --- a/misc/ruby-mode.el +++ b/misc/ruby-mode.el @@ -779,7 +779,7 @@ An end of a defun is found by moving forward from the beginning of one." (if done (save-excursion (back-to-indentation) - (if (looking-at (concat ("\\<\\(" ruby-block-mid-re "\\)\\>"))) + (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>")) (setq done nil)))))) (back-to-indentation)) diff --git a/process.c b/process.c index 67171ba5b5..0132c31606 100644 --- a/process.c +++ b/process.c @@ -2663,6 +2663,60 @@ proc_setmaxgroups(obj, val) return INT2FIX(maxgroups); } +/* + * call-seq: + * Process.daemon() => fixnum + * Process.daemon(nochdir=0,noclose=0) => fixnum + * + * Detach the process from controlling terminal and run in + * the background as system daemon. Unless the argument + * nochdir is true (i.e. non false), it changes the current + * working directory to the root ("/"). Unless the argument + * noclose is true, daemon() will redirect standard input, + * standard output and standard error to /dev/null. + */ + +static VALUE +proc_daemon(argc, argv) + int argc; + VALUE *argv; +{ + VALUE nochdir, noclose; + int n; + + rb_scan_args(argc, argv, "02", &nochdir, &noclose); + +#if defined(HAVE_DAEMON) + n = daemon(RTEST(nochdir), RTEST(noclose)); + if (n < 0) rb_sys_fail("daemon"); + return INT2FIX(n); +#elif defined(HAVE_FORK) + switch (rb_fork(0, 0, 0)) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + proc_setsid(); + + if (!RTEST(nochdir)) + (void)chdir("/"); + + if (!RTEST(noclose) && (n = open("/dev/null", O_RDWR, 0)) != -1) { + (void)dup2(n, 0); + (void)dup2(n, 1); + (void)dup2(n, 2); + if (n > 2) + (void)close (n); + } + return INT2FIX(0); +#else + rb_notimplement(); +#endif +} /******************************************************************** * @@ -3543,6 +3597,8 @@ Init_process() rb_define_module_function(rb_mProcess, "maxgroups", proc_getmaxgroups, 0); rb_define_module_function(rb_mProcess, "maxgroups=", proc_setmaxgroups, 1); + rb_define_module_function(rb_mProcess, "daemon", proc_daemon, -1); + rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0); #if defined(HAVE_TIMES) || defined(_WIN32) diff --git a/string.c b/string.c index e68dfcc20a..0b32ddeb4c 100644 --- a/string.c +++ b/string.c @@ -1079,10 +1079,10 @@ rb_str_index_m(argc, argv, str) { int c = FIX2INT(sub); long len = RSTRING(str)->len; - unsigned char *p = RSTRING(str)->ptr; + char *p = RSTRING(str)->ptr; for (;posptr + pos; - unsigned char *pbeg = RSTRING(str)->ptr; + char *p = RSTRING(str)->ptr + pos; + char *pbeg = RSTRING(str)->ptr; if (pos == RSTRING(str)->len) { if (pos == 0) return Qnil; --p; } while (pbeg <= p) { - if (*p == c) return LONG2NUM((char*)p - RSTRING(str)->ptr); + if ((unsigned char)*p == c) + return LONG2NUM((char*)p - RSTRING(str)->ptr); p--; } return Qnil;