Project

General

Profile

Feature #4046 ยป OS_ARGV_and_STARTUP_DIR.patch

Save C's **argv and cwd as RubyVM::OS_ARGV and RubyVM::OS_STARTUP_DIR - rocky (Rocky Bernstein), 11/12/2010 12:32 PM

View differences:

include/ruby/ruby.h (working copy)
1454 1454
#include "ruby/subst.h"
1455 1455
#endif
1456 1456

  
1457
struct origarg {
1458
    int argc;
1459
    char **argv;
1460
    char *os_startup_dir;
1461
};
1462

  
1457 1463
#if defined(__cplusplus)
1458 1464
#if 0
1459 1465
{ /* satisfy cc-mode */
vm.c (working copy)
1975 1975
    return ary;
1976 1976
}
1977 1977

  
1978
extern struct origarg origarg;
1979

  
1978 1980
void
1979 1981
Init_VM(void)
1980 1982
{
......
1987 1990
    rb_undef_alloc_func(rb_cRubyVM);
1988 1991
    rb_undef_method(CLASS_OF(rb_cRubyVM), "new");
1989 1992

  
1993
    /** ::VM::OS_ARGV and OS_STARTUP_DIR */
1994
    rb_define_const(rb_cRubyVM, "OS_STARTUP_DIR", 
1995
		    strlen(origarg.os_startup_dir) > 0 ? 
1996
		    rb_str_new2(origarg.os_startup_dir) : Qnil);
1997
    {
1998
	int argc;
1999
	char **argv;
2000
	VALUE ary = rb_ary_new();
2001
	rb_define_const(rb_cRubyVM, "OS_ARGV", ary);
2002
	for (argc = origarg.argc, argv = origarg.argv; 
2003
	     argc > 0; argc--, argv++) {
2004
	    rb_ary_push(ary, rb_str_new2(*argv));
2005
	}
2006
    }
2007

  
1990 2008
    /* ::VM::FrozenCore */
1991 2009
    fcore = rb_class_new(rb_cBasicObject);
1992 2010
    RBASIC(fcore)->flags = T_ICLASS;
ruby.c (working copy)
118 118
static void forbid_setid(const char *, struct cmdline_options *);
119 119
#define forbid_setid(s) forbid_setid(s, opt)
120 120

  
121
static struct {
122
    int argc;
123
    char **argv;
121
struct origarg origarg;
122

  
124 123
#if !defined(PSTAT_SETCMD) && !defined(HAVE_SETPROCTITLE)
125
    size_t len;
124
    size_t origarg_len;
126 125
#endif
127
} origarg;
128 126

  
129 127
static void
130 128
usage(const char *name)
......
1752 1750
    setproctitle("%.*s", (int)i, s);
1753 1751
#else
1754 1752

  
1755
    if ((size_t)i > origarg.len - origarg.argc) {
1756
	i = (long)(origarg.len - origarg.argc);
1753
    if ((size_t)i > origarg_len - origarg.argc) {
1754
	i = (long)(origarg_len - origarg.argc);
1757 1755
    }
1758 1756

  
1759 1757
    memcpy(origarg.argv[0], s, i);
......
1763 1761
	char *t = origarg.argv[0] + i;
1764 1762
	*t = '\0';
1765 1763

  
1766
	if ((size_t)(i + 1) < origarg.len) {
1767
	    memset(t + 1, '\0', origarg.len - i - 1);
1764
	if ((size_t)(i + 1) < origarg_len) {
1765
	    memset(t + 1, '\0', origarg_len - i - 1);
1768 1766
	}
1769 1767
	for (j = 1; j < origarg.argc; j++) {
1770 1768
	    origarg.argv[j] = t;
......
1891 1889
#endif
1892 1890
    origarg.argc = *argc;
1893 1891
    origarg.argv = *argv;
1892
    origarg.os_startup_dir = malloc(MAXPATHLEN);
1893
#ifdef HAVE_GETCWD
1894
    if (!getcwd(origarg.os_startup_dir, MAXPATHLEN)) 
1895
	origarg.os_startup_dir[0] = '\0';
1896
#else
1897
    if (!getwd(origarg.os_startup_dir)) 
1898
	origarg.os_startup_dir[0] = '\0';
1899
#endif
1900
    origarg.os_startup_dir = realloc(origarg.os_startup_dir, 
1901
				     strlen(origarg.os_startup_dir)+1);
1902

  
1894 1903
#if !defined(PSTAT_SETCMD) && !defined(HAVE_SETPROCTITLE)
1895
    origarg.len = get_arglen(origarg.argc, origarg.argv);
1904
    origarg_len = get_arglen(origarg.argc, origarg.argv);
1896 1905
#endif
1897 1906
#if defined(USE_DLN_A_OUT)
1898 1907
    dln_argv0 = origarg.argv[0];