mirror of
https://github.com/neovim/neovim.git
synced 2025-10-17 23:31:51 +00:00
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
This commit is contained in:

committed by
Thiago de Arruda

parent
fc86866402
commit
f2433aedc8
148
src/misc2.c
148
src/misc2.c
@@ -2249,154 +2249,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.
|
||||
|
Reference in New Issue
Block a user