diff options
author | Christopher Faylor <me@cgf.cx> | 2000-09-03 04:16:35 +0000 |
---|---|---|
committer | Christopher Faylor <me@cgf.cx> | 2000-09-03 04:16:35 +0000 |
commit | b0e82b74fbdfa2dee89505aba03f2827480cc8ca (patch) | |
tree | 42b2a83a749596de6cb3a075e379a4e30ecd1796 /winsup/cygwin/environ.cc | |
parent | 39630fe3a1b6106001a9f5f65a9d52bf48583739 (diff) | |
download | cygnal-b0e82b74fbdfa2dee89505aba03f2827480cc8ca.tar.gz cygnal-b0e82b74fbdfa2dee89505aba03f2827480cc8ca.tar.bz2 cygnal-b0e82b74fbdfa2dee89505aba03f2827480cc8ca.zip |
* Makefile.in: Add cygheap.o.
* child_info.h: Add specific exec class.
* cygheap.h: New file. Contains declarations for cygwin heap.
* cygheap.cc: New file. Implements cygwin heap functions.
* dcrt0.cc (quoted): Simplify due to new method for passing arguments between
cygwin programs.
(alloc_stack_hard_way): Attempt to handle overlapped stack.
(dll_crt0_1): Move child_info processing here. Accomodate new method for
passing arguments between cygwin programs. Initialize cygwin heap. Establish
__argc and __argv variables.
(_dll_crt0): Move most of child_info processing to dll_crt0_1.
(cygwin_dll_init): Remove duplication.
* dtable.cc (dtable::extend): Allocate dtable using cygwin heap.
(dtable::build_fhandler): Ditto for fhandler type being constructed.
(dtable::dup_worker): Free new fhandler from cygwin heap on error.
(dtable::select_*): Don't assume that this == fdtab.
(dtable::linearize_fd_array): Delete.
(dtable::delinearize_fd_array): Delete.
(dtable::fixup_after_exec): New file.
(dtable::vfork_child_dup): Use cygwin heap.
(dtable::vfork_parent_restore): Ditto.
* dtable.h: Remove obsolete methods. Add new method.
* environ.cc (posify): Eliminate already_posix parameter and logic.
(envsize): New function.
(_addenv): Use envsize.
(environ_init): Accept an argument pointing to an existing environment list.
If supplied, allocate space for this in the the program's heap.
* fhandler.cc (fhandler_base::operator =): Move here from fhandler.h. Use
cygwin heap to allocate filenames.
(fhandler_base::set_name): Allocate/free names from cygwin heap.
(fhandler_base::linearize): Delete.
(fhandler_base::de_linearize): Delete.
(fhandler_base::operator delete): Free from cygwin heap.
(fhandler_base::~fhandler_base): Ditto.
* fhandler.h: Accomodate elimination of *linearize and other changes above.
* fhandler_console.cc (fhandler_console::fixup_after_exec): Rename from
de_linearize.
* heap.h: New file.
* fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Use cygwin heap for
name. fhandler_tty::fixup_after_exec): Rename from de_linearize.
* fork.cc (fork): Call cygheap_fixup_in_child.
* heap.cc: Use declarations in heap.h.
* malloc.cc: Sprinkle assertions throughout to catch attempts to free/realloc
something from the cygwin heap.
* path.cc: Throughout, eliminate use of per-thread cache for cwd. Use cwd_*
functions rather than cwd_* variables to access cwd_win32 and cwd_posix.
(cwd_win32): New function.
(cwd_posix): New function.
(cwd_hash): New function.
(cwd_fixup_after_exec): New function.
* path.h: Accomodate path.cc changes.
* pinfo.cc (pinfo_init): Accept a pointer to an environment table. Pass this
to environ_init. Eliminate old 'title' tests.
* pinfo.h: Accomodate above change in argument.
* spawn.cc (struct av): New method for building argv list.
(av::unshift): New method.
(spawn_guts): Allocate everything that the child process needs in the cygwin
heap and pass a pointer to this to the child. Build argv list using new
method. Eliminate delinearize stuff.
* thread.h: Eliminate _cwd_win32 and _cwd_posix buffers.
* winsup.h: Eliminate obsolete functions. Add envsize() declaration.
Diffstat (limited to 'winsup/cygwin/environ.cc')
-rw-r--r-- | winsup/cygwin/environ.cc | 92 |
1 files changed, 57 insertions, 35 deletions
diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc index 1af364697..89ad0ab13 100644 --- a/winsup/cygwin/environ.cc +++ b/winsup/cygwin/environ.cc @@ -27,6 +27,8 @@ extern DWORD chunksize; BOOL reset_com = TRUE; static BOOL envcache = TRUE; +static char **lastenviron = NULL; + /* List of names which are converted from dos to unix * on the way in and back again on the way out. * @@ -102,7 +104,7 @@ getwinenv (const char *env, const char *in_posix) /* Convert windows path specs to POSIX, if appropriate. */ static void __stdcall -posify (int already_posix, char **here, const char *value) +posify (char **here, const char *value) { char *src = *here; win_env *conv; @@ -111,22 +113,17 @@ posify (int already_posix, char **here, const char *value) if (!(conv = getwinenv (src))) return; - if (already_posix) - conv->add_cache (value, NULL); - else - { - /* Turn all the items from c:<foo>;<bar> into their - mounted equivalents - if there is one. */ + /* Turn all the items from c:<foo>;<bar> into their + mounted equivalents - if there is one. */ - char *outenv = (char *) malloc (1 + len + conv->posix_len (value)); - memcpy (outenv, src, len); - conv->toposix (value, outenv + len); - conv->add_cache (outenv + len, value); + char *outenv = (char *) malloc (1 + len + conv->posix_len (value)); + memcpy (outenv, src, len); + conv->toposix (value, outenv + len); + conv->add_cache (outenv + len, value); - debug_printf ("env var converted to %s", outenv); - *here = outenv; - free (src); - } + debug_printf ("env var converted to %s", outenv); + *here = outenv; + free (src); } /* @@ -175,6 +172,16 @@ getenv (const char *name) return my_findenv (name, &offset); } +extern int __stdcall +envsize (const char * const *in_envp, int debug_print) +{ + const char * const *envp; + for (envp = in_envp; *envp; envp++) + if (debug_print) + debug_printf ("%s", *envp); + return (1 + envp - in_envp) * sizeof (const char *); +} + /* Takes similar arguments to setenv except that overwrite is either -1, 0, or 1. 0 or 1 signify that the function should perform similarly to setenv. Otherwise putenv is assumed. */ @@ -202,17 +209,19 @@ _addenv (const char *name, const char *value, int overwrite) } else { /* Create new slot. */ - char **env; - - /* Search for the end of the environment. */ - for (env = cur_environ (); *env; env++) - continue; + int sz = envsize (cur_environ ()); + int allocsz = sz + sizeof (char *); - offset = env - cur_environ (); /* Number of elements currently in environ. */ + offset = (sz - 1) / sizeof (char *); /* Allocate space for additional element plus terminating NULL. */ - __cygwin_environ = (char **) realloc (cur_environ (), (sizeof (char *) * - (offset + 2))); + if (__cygwin_environ == lastenviron) + lastenviron = __cygwin_environ = (char **) realloc (cur_environ (), + allocsz); + else if ((lastenviron = (char **) malloc (allocsz)) != NULL) + __cygwin_environ = (char **) memcpy ((char **) lastenviron, + __cygwin_environ, sz); + if (!__cygwin_environ) return -1; /* Oops. No more memory. */ @@ -261,7 +270,7 @@ putenv (const char *str) if ((res = check_null_empty_path (str))) { if (res == ENOENT) - return 0; + return 0; set_errno (res); return -1; } @@ -292,7 +301,7 @@ setenv (const char *name, const char *value, int overwrite) if ((res = check_null_empty_path (name))) { if (res == ENOENT) - return 0; + return 0; set_errno (res); return -1; } @@ -503,18 +512,15 @@ regopt (const char *name) * environment variable and set appropriate options from it. */ void -environ_init (int already_posix) +environ_init (char **envp) { - char *rawenv = GetEnvironmentStrings (); - int envsize, i; + char *rawenv; + int sz, i; char *p; - char *newp, **envp; + char *newp; int sawTERM = 0; static char cygterm[] = "TERM=cygwin"; - /* Allocate space for environment + trailing NULL + CYGWIN env. */ - envp = (char **) malloc ((4 + (envsize = 100)) * sizeof (char *)); - regopt ("default"); if (myself->progname[0]) regopt (myself->progname); @@ -525,6 +531,19 @@ environ_init (int already_posix) allow_ntsec = TRUE; #endif + if (envp) + { + sz = envsize (envp, 1); + char **newenv = (char **) malloc (sz); + envp = (char **) memcpy (newenv, envp, sz); + cfree (envp); + goto out; + } + + /* Allocate space for environment + trailing NULL + CYGWIN env. */ + lastenviron = envp = (char **) malloc ((4 + (sz = 100)) * sizeof (char *)); + rawenv = GetEnvironmentStrings (); + /* Current directory information is recorded as variables of the form "=X:=X:\foo\bar; these must be changed into something legal (we could just ignore them but maybe an application will @@ -532,8 +551,8 @@ environ_init (int already_posix) for (i = 0, p = rawenv; *p != '\0'; p = strchr (p, '\0') + 1, i++) { newp = strdup (p); - if (i >= envsize) - envp = (char **) realloc (envp, (4 + (envsize += 100)) * + if (i >= sz) + envp = (char **) realloc (envp, (4 + (sz += 100)) * sizeof (char *)); envp[i] = newp; if (*newp == '=') @@ -548,13 +567,16 @@ environ_init (int already_posix) if (strncmp (newp, "CYGWIN=", sizeof("CYGWIN=") - 1) == 0) parse_options (newp + sizeof("CYGWIN=") - 1); if (*eq) - posify (already_posix, envp + i, *++eq ? eq : --eq); + posify (envp + i, *++eq ? eq : --eq); debug_printf ("%s", envp[i]); } if (!sawTERM) envp[i++] = cygterm; envp[i] = NULL; + FreeEnvironmentStrings (rawenv); + +out: __cygwin_environ = envp; update_envptrs (); parse_options (NULL); |