📄 environ.cc
字号:
/* environ.cc: Cygwin-adopted functions from newlib to manipulate process's environment. Copyright 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.This software is a copyrighted work licensed under the terms of theCygwin license. Please consult the file "CYGWIN_LICENSE" fordetails. */#include "winsup.h"#include <errno.h>#include <stdlib.h>#include <stddef.h>#include <ctype.h>#include <assert.h>#include <sys/cygwin.h>#include <cygwin/version.h>#include "pinfo.h"#include "perprocess.h"#include "security.h"#include "fhandler.h"#include "path.h"#include "cygerrno.h"#include "dtable.h"#include "cygheap.h"#include "registry.h"#include "environ.h"#include "child_info.h"extern BOOL allow_daemon;extern BOOL allow_glob;extern bool ignore_case_with_glob;extern BOOL allow_ntea;extern BOOL allow_smbntsec;extern BOOL allow_winsymlinks;extern BOOL strip_title_path;extern int pcheck_case;extern int subauth_id;BOOL reset_com = FALSE;static BOOL envcache = TRUE;static char **lastenviron;#define ENVMALLOC \ (CYGWIN_VERSION_DLL_MAKE_COMBINED (user_data->api_major, user_data->api_minor) \ <= CYGWIN_VERSION_DLL_MALLOC_ENV)#define NL(x) x, (sizeof (x) - 1)/* List of names which are converted from dos to unix on the way in and back again on the way out. PATH needs to be here because CreateProcess uses it and gdb uses CreateProcess. HOME is here because most shells use it and would be confused by Windows style path names. */static int return_MAX_PATH (const char *) {return MAX_PATH;}static NO_COPY win_env conv_envvars[] = { {NL ("PATH="), NULL, NULL, cygwin_win32_to_posix_path_list, cygwin_posix_to_win32_path_list, cygwin_win32_to_posix_path_list_buf_size, cygwin_posix_to_win32_path_list_buf_size}, {NL ("HOME="), NULL, NULL, cygwin_conv_to_full_posix_path, cygwin_conv_to_full_win32_path, return_MAX_PATH, return_MAX_PATH}, {NL ("LD_LIBRARY_PATH="), NULL, NULL, cygwin_conv_to_full_posix_path, cygwin_conv_to_full_win32_path, return_MAX_PATH, return_MAX_PATH}, {NL ("TMPDIR="), NULL, NULL, cygwin_conv_to_full_posix_path, cygwin_conv_to_full_win32_path, return_MAX_PATH, return_MAX_PATH}, {NL ("TMP="), NULL, NULL, cygwin_conv_to_full_posix_path, cygwin_conv_to_full_win32_path, return_MAX_PATH, return_MAX_PATH}, {NL ("TEMP="), NULL, NULL, cygwin_conv_to_full_posix_path, cygwin_conv_to_full_win32_path, return_MAX_PATH, return_MAX_PATH}, {NULL, 0, NULL, NULL, NULL, NULL, 0, 0} };static unsigned char conv_start_chars[256] = {0};voidwin_env::add_cache (const char *in_posix, const char *in_native){ MALLOC_CHECK; posix = (char *) realloc (posix, strlen (in_posix) + 1); strcpy (posix, in_posix); if (in_native) { native = (char *) realloc (native, namelen + 1 + strlen (in_native)); (void) strcpy (native, name); (void) strcpy (native + namelen, in_native); } else { native = (char *) realloc (native, namelen + 1 + win32_len (in_posix)); (void) strcpy (native, name); towin32 (in_posix, native + namelen); } MALLOC_CHECK; debug_printf ("posix %s", posix); debug_printf ("native %s", native);}/* Check for a "special" environment variable name. *env is the pointer to the beginning of the environment variable name. *in_posix is any known posix value for the environment variable. Returns a pointer to the appropriate conversion structure. */win_env * __stdcallgetwinenv (const char *env, const char *in_posix){ if (!conv_start_chars[(unsigned char)*env]) return NULL; for (int i = 0; conv_envvars[i].name != NULL; i++) if (strncmp (env, conv_envvars[i].name, conv_envvars[i].namelen) == 0) { win_env * const we = conv_envvars + i; const char *val; if (!cur_environ () || !(val = in_posix ?: getenv (we->name))) debug_printf ("can't set native for %s since no environ yet", we->name); else if (!envcache || !we->posix || strcmp (val, we->posix) != 0) we->add_cache (val); return we; } return NULL;}/* Convert windows path specs to POSIX, if appropriate. */static void __stdcallposify (char **here, const char *value){ char *src = *here; win_env *conv; if (!(conv = getwinenv (src))) return; int len = strcspn (src, "=") + 1; /* 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 != '/' ? value : NULL); debug_printf ("env var converted to %s", outenv); *here = outenv; free (src); MALLOC_CHECK;}/* * my_findenv -- * Returns pointer to value associated with name, if any, else NULL. * Sets offset to be the offset of the name/value combination in the * environment array, for use by setenv(3) and unsetenv(3). * Explicitly removes '=' in argument name. */static char * __stdcallmy_findenv (const char *name, int *offset){ register int len; register char **p; const char *c; c = name; len = 0; while (*c && *c != '=') { c++; len++; } for (p = cur_environ (); *p; ++p) if (!strncmp (*p, name, len)) if (*(c = *p + len) == '=') { *offset = p - cur_environ (); return (char *) (++c); } MALLOC_CHECK; return NULL;}/* * getenv -- * Returns ptr to value associated with name, if any, else NULL. */extern "C" char *getenv (const char *name){ int offset; return my_findenv (name, &offset);}static int __stdcallenvsize (const char * const *in_envp){ const char * const *envp; for (envp = in_envp; *envp; envp++) continue; 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. */static int __stdcall_addenv (const char *name, const char *value, int overwrite){ int issetenv = overwrite >= 0; int offset; char *p; unsigned int valuelen = strlen (value); if ((p = my_findenv (name, &offset))) { /* Already exists. */ if (!overwrite) /* Ok to overwrite? */ return 0; /* No. Wanted to add new value. FIXME: Right return value? */ /* We've found the offset into environ. If this is a setenv call and there is room in the current environment entry then just overwrite it. Otherwise handle this case below. */ if (issetenv && strlen (p) >= valuelen) { strcpy (p, value); return 0; } } else { /* Create new slot. */ int sz = envsize (cur_environ ()); int allocsz = sz + (2 * sizeof (char *)); offset = (sz - 1) / sizeof (char *); /* Allocate space for additional element plus terminating NULL. */ if (cur_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) {#ifdef DEBUGGING try_to_debug ();#endif return -1; /* Oops. No more memory. */ } __cygwin_environ[offset + 1] = NULL; /* NULL terminate. */ update_envptrs (); /* Update any local copies of 'environ'. */ } char *envhere; if (!issetenv) /* Not setenv. Just overwrite existing. */ envhere = cur_environ ()[offset] = (char *) (ENVMALLOC ? strdup (name) : name); else { /* setenv */ /* Look for an '=' in the name and ignore anything after that if found. */ for (p = (char *) name; *p && *p != '='; p++) continue; int namelen = p - name; /* Length of name. */ /* Allocate enough space for name + '=' + value + '\0' */ envhere = cur_environ ()[offset] = (char *) malloc (namelen + valuelen + 2); if (!envhere) return -1; /* Oops. No more memory. */ /* Put name '=' value into current slot. */ strncpy (envhere, name, namelen); envhere[namelen] = '='; strcpy (envhere + namelen + 1, value); } /* Update cygwin's cache, if appropriate */ win_env *spenv; if ((spenv = getwinenv (envhere))) spenv->add_cache (value); MALLOC_CHECK; return 0;}/* putenv Sets an environment variable */extern "C" intputenv (const char *str){ int res; if ((res = check_null_empty_str (str))) { if (res == ENOENT) return 0; set_errno (res); return -1; } char *eq = strchr (str, '='); if (eq) return _addenv (str, eq + 1, -1); /* Remove str from the environment. */ unsetenv (str); return 0;}/* setenv -- Set the value of the environment variable "name" to be "value". If overwrite is set, replace any current value. */extern "C" intsetenv (const char *name, const char *value, int overwrite){ int res; if ((res = check_null_empty_str (value)) == EFAULT) { set_errno (res); return -1; } if ((res = check_null_empty_str (name))) { if (res == ENOENT) return 0; set_errno (res); return -1; } if (*value == '=') value++; return _addenv (name, value, !!overwrite);}/* unsetenv(name) -- Delete environment variable "name". */extern "C" voidunsetenv (const char *name){ register char **e; int offset; while (my_findenv (name, &offset)) /* if set multiple times */ /* Move up the rest of the array */ for (e = cur_environ () + offset; ; e++) if (!(*e = *(e + 1))) break;}/* Turn environment variable part of a=b string into uppercase. */static __inline__ voiducenv (char *p, char *eq){ /* Amazingly, NT has a case sensitive environment name list, but only sometimes. It's normal to have NT set your "Path" to something. Later, you set "PATH" to something else. This alters "Path". But if you try and do a naive getenv on "PATH" you'll get nothing. So we upper case the labels here to prevent confusion later but we only do it for the first process in a session group. */ for (; p < eq; p++) if (islower (*p)) *p = cyg_toupper (*p);}/* Parse CYGWIN options */static NO_COPY BOOL export_settings = false;enum settings { justset, isfunc, setbit, set_process_state, };/* When BUF is: null or empty: disables globbing "ignorecase": enables case-insensitive globbing anything else: enables case-sensitive globbing */static voidglob_init (const char *buf){ if (!buf || !*buf) { allow_glob = FALSE; ignore_case_with_glob = FALSE; } else if (strncasematch (buf, "ignorecase", 10)) { allow_glob = TRUE; ignore_case_with_glob = TRUE; } else { allow_glob = TRUE; ignore_case_with_glob = FALSE; }}static voidcheck_case_init (const char *buf){ if (!buf || !*buf) return; if (strncmp (buf, "relax", 5)== 0) { pcheck_case = PCHECK_RELAXED; debug_printf ("File case checking set to RELAXED"); } else if (strcasematch (buf, "adjust")) { pcheck_case = PCHECK_ADJUST; debug_printf ("File case checking set to ADJUST"); } else if (strcasematch (buf, "strict")) { pcheck_case = PCHECK_STRICT; debug_printf ("File case checking set to STRICT"); } else { debug_printf ("Wrong case checking name: %s", buf); }}voidset_file_api_mode (codepage_type cp){ if (cp == oem_cp) { SetFileApisToOEM (); debug_printf ("File APIs set to OEM"); } else if (cp == ansi_cp) { SetFileApisToANSI (); debug_printf ("File APIs set to ANSI"); }}static voidcodepage_init (const char *buf){ if (!buf || !*buf) return; if (strcasematch (buf, "oem")) { current_codepage = oem_cp; set_file_api_mode (current_codepage); } else if (strcasematch (buf, "ansi")) { current_codepage = ansi_cp; set_file_api_mode (current_codepage); } else debug_printf ("Wrong codepage name: %s", buf);}static voidsubauth_id_init (const char *buf){ if (!buf || !*buf) return; int i = strtol (buf, NULL, 0); /* 0..127 are reserved by Microsoft, 132 is IIS subauthentication. */ if (i > 127 && i != 132 && i <= 255) subauth_id = i;}static voidset_chunksize (const char *buf){ wincap.set_chunksize (strtol (buf, NULL, 0));}/* The structure below is used to set up an array which is used to parse the CYGWIN environment variable or, if enabled, options from the registry. */static struct parse_thing { const char *name; union parse_setting { BOOL *b; DWORD *x; int *i; void (*func)(const char *); } setting; enum settings disposition; char *remember; union parse_values { DWORD i; const char *s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -