diff options
author | Stefan Hoffmann <stefan991@gmail.com> | 2014-03-03 20:02:32 +0100 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-03-06 13:38:45 -0300 |
commit | f2433aedc86db171d5616410605cf0d398d8fdc2 (patch) | |
tree | 2df40baf1dce2b9e6e6dc175f66cfff41d7bd6ad /src/misc2.c | |
parent | fc8686640250561156913387c62924d2bdb5e1ac (diff) | |
download | rneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.tar.gz rneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.tar.bz2 rneovim-f2433aedc86db171d5616410605cf0d398d8fdc2.zip |
cleanup environment variable handling + unit tests
* removed a putenv() implementation which isn't needed anymore
* mch_getenv() and mch_setenv() are now functions in src/os/env.c
* removes direct calls to getenv() and setenv() outside of src/os/env.c
* refactored the logic of get_env_name into mch_getenvname_at_index
* added unittests for the functions in os/env.c
Diffstat (limited to 'src/misc2.c')
-rw-r--r-- | src/misc2.c | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/src/misc2.c b/src/misc2.c index 1b1693a72a..92a6af598b 100644 --- a/src/misc2.c +++ b/src/misc2.c @@ -2250,154 +2250,6 @@ int pathcmp(const char *p, const char *q, int maxlen) #endif /* - * The putenv() implementation below comes from the "screen" program. - * Included with permission from Juergen Weigert. - * See pty.c for the copyright notice. - */ - -/* - * putenv -- put value into environment - * - * Usage: i = putenv (string) - * int i; - * char *string; - * - * where string is of the form <name>=<value>. - * Putenv returns 0 normally, -1 on error (not enough core for malloc). - * - * Putenv may need to add a new name into the environment, or to - * associate a value longer than the current value with a particular - * name. So, to make life simpler, putenv() copies your entire - * environment into the heap (i.e. malloc()) from the stack - * (i.e. where it resides when your process is initiated) the first - * time you call it. - * - * (history removed, not very interesting. See the "screen" sources.) - */ - -#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) - -#define EXTRASIZE 5 /* increment to add to env. size */ - -static int envsize = -1; /* current size of environment */ -extern -char **environ; /* the global which is your env. */ - -static int findenv(char *name); /* look for a name in the env. */ -static int newenv(void); /* copy env. from stack to heap */ -static int moreenv(void); /* incr. size of env. */ - -int putenv(const char *string) -{ - int i; - char *p; - - if (envsize < 0) { /* first time putenv called */ - if (newenv() < 0) /* copy env. to heap */ - return -1; - } - - i = findenv((char *)string); /* look for name in environment */ - - if (i < 0) { /* name must be added */ - for (i = 0; environ[i]; i++) ; - if (i >= (envsize - 1)) { /* need new slot */ - if (moreenv() < 0) - return -1; - } - p = (char *)alloc((unsigned)(strlen(string) + 1)); - if (p == NULL) /* not enough core */ - return -1; - environ[i + 1] = 0; /* new end of env. */ - } else { /* name already in env. */ - p = vim_realloc(environ[i], strlen(string) + 1); - if (p == NULL) - return -1; - } - sprintf(p, "%s", string); /* copy into env. */ - environ[i] = p; - - return 0; -} - -static int findenv(char *name) -{ - char *namechar, *envchar; - int i, found; - - found = 0; - for (i = 0; environ[i] && !found; i++) { - envchar = environ[i]; - namechar = name; - while (*namechar && *namechar != '=' && (*namechar == *envchar)) { - namechar++; - envchar++; - } - found = ((*namechar == '\0' || *namechar == '=') && *envchar == '='); - } - return found ? i - 1 : -1; -} - -static int newenv(void) { - char **env, *elem; - int i, esize; - - for (i = 0; environ[i]; i++) - ; - esize = i + EXTRASIZE + 1; - env = (char **)alloc((unsigned)(esize * sizeof (elem))); - if (env == NULL) - return -1; - - for (i = 0; environ[i]; i++) { - elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1)); - if (elem == NULL) - return -1; - env[i] = elem; - strcpy(elem, environ[i]); - } - - env[i] = 0; - environ = env; - envsize = esize; - return 0; -} - -static int moreenv(void) { - int esize; - char **env; - - esize = envsize + EXTRASIZE; - env = (char **)vim_realloc((char *)environ, esize * sizeof (*env)); - if (env == 0) - return -1; - environ = env; - envsize = esize; - return 0; -} - -# ifdef USE_VIMPTY_GETENV -char_u *vimpty_getenv(const char_u *string) -{ - int i; - char_u *p; - - if (envsize < 0) - return NULL; - - i = findenv((char *)string); - - if (i < 0) - return NULL; - - p = vim_strchr((char_u *)environ[i], '='); - return p + 1; -} -# endif - -#endif /* !defined(HAVE_SETENV) && !defined(HAVE_PUTENV) */ - -/* * Return 0 for not writable, 1 for writable file, 2 for a dir which we have * rights to write into. */ |