diff options
Diffstat (limited to 'src/nvim/os/env.c')
-rw-r--r-- | src/nvim/os/env.c | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 30e44341a9..be4b22de3a 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -37,6 +37,19 @@ int os_setenv(const char *name, const char *value, int overwrite) return setenv(name, value, overwrite); } +/// Unset environment variable +/// +/// For systems where unsetenv() is not available the value will be set as an +/// empty string +int os_unsetenv(const char *name) +{ +#ifdef HAVE_UNSETENV + return unsetenv(name); +#else + return os_setenv(name, "", 1); +#endif +} + char *os_getenvname_at_index(size_t index) { # if defined(HAVE__NSGETENVIRON) @@ -113,7 +126,7 @@ void init_homedir(void) char_u *var; /* In case we are called a second time (when 'encoding' changes). */ - free(homedir); + xfree(homedir); homedir = NULL; var = (char_u *)os_getenv("HOME"); @@ -144,7 +157,7 @@ void init_homedir(void) void free_homedir(void) { - free(homedir); + xfree(homedir); } #endif @@ -304,7 +317,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one, char_u *p = vim_strsave(var); if (mustfree) { - free(var); + xfree(var); } var = p; mustfree = true; @@ -318,7 +331,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one, char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); if (mustfree) - free(var); + xfree(var); var = p; mustfree = true; } @@ -341,7 +354,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one, copy_char = false; } if (mustfree) - free(var); + xfree(var); } if (copy_char) { /* copy at least one char */ @@ -371,33 +384,33 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one, /// Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. /// Return NULL if not, return its name in allocated memory otherwise. /// @param vimdir directory to test -static char_u *vim_version_dir(char_u *vimdir) +static char *vim_version_dir(char *vimdir) { char_u *p; if (vimdir == NULL || *vimdir == NUL) return NULL; - p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, true); + p = concat_fnames((char_u *)vimdir, (char_u *)VIM_VERSION_NODOT, true); if (os_isdir(p)) - return p; - free(p); - p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, true); + return (char *)p; + xfree(p); + p = concat_fnames((char_u *)vimdir, (char_u *)RUNTIME_DIRNAME, true); if (os_isdir(p)) - return p; - free(p); + return (char *)p; + xfree(p); return NULL; } /// If the string between "p" and "pend" ends in "name/", return "pend" minus /// the length of "name/". Otherwise return "pend". -static char_u *remove_tail(char_u *p, char_u *pend, char_u *name) +static char *remove_tail(char *p, char *pend, char *name) { - int len = (int)STRLEN(name) + 1; - char_u *newend = pend - len; + size_t len = STRLEN(name) + 1; + char *newend = pend - len; if (newend >= p - && fnamencmp(newend, name, len - 1) == 0 - && (newend == p || after_pathsep(p, newend))) + && fnamencmp((char_u *)newend, (char_u *)name, len - 1) == 0 + && (newend == p || after_pathsep((char_u *)p, (char_u *)newend))) return newend; return pend; } @@ -442,7 +455,7 @@ char_u *vim_getenv(char_u *name, bool *mustfree) if (p != NULL && *p == NUL) /* empty is the same as not set */ p = NULL; if (p != NULL) { - p = vim_version_dir(p); + p = (char_u *)vim_version_dir((char *)p); if (p != NULL) *mustfree = true; else @@ -464,12 +477,14 @@ char_u *vim_getenv(char_u *name, bool *mustfree) /* remove "doc/" from 'helpfile', if present */ if (p == p_hf) - pend = remove_tail(p, pend, (char_u *)"doc"); + pend = (char_u *)remove_tail((char *)p, (char *)pend, "doc"); /* for $VIM, remove "runtime/" or "vim54/", if present */ if (!vimruntime) { - pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); - pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); + pend = (char_u *)remove_tail((char *)p, (char *)pend, + RUNTIME_DIRNAME); + pend = (char_u *)remove_tail((char *)p, (char *)pend, + VIM_VERSION_NODOT); } /* remove trailing path separator */ @@ -481,7 +496,7 @@ char_u *vim_getenv(char_u *name, bool *mustfree) p = vim_strnsave(p, (size_t)(pend - p)); if (!os_isdir(p)) { - free(p); + xfree(p); p = NULL; } else { *mustfree = true; @@ -495,13 +510,14 @@ char_u *vim_getenv(char_u *name, bool *mustfree) if (p == NULL) { /* Only use default_vimruntime_dir when it is not empty */ if (vimruntime && *default_vimruntime_dir != NUL) { - p = default_vimruntime_dir; + p = (char_u *)default_vimruntime_dir; *mustfree = false; } else if (*default_vim_dir != NUL) { - if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) { + if (vimruntime + && (p = (char_u *)vim_version_dir(default_vim_dir)) != NULL) { *mustfree = true; } else { - p = default_vim_dir; + p = (char_u *)default_vim_dir; *mustfree = false; } } @@ -631,7 +647,7 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one) *dst = NUL; if (homedir_env != homedir_env_orig) - free(homedir_env); + xfree(homedir_env); } /// Like home_replace, store the replaced string in allocated memory. @@ -660,7 +676,7 @@ void vim_setenv(char_u *name, char_u *val) if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) { char_u *buf = concat_str(val, (char_u *)"/lang"); bindtextdomain(VIMPACKAGE, (char *)buf); - free(buf); + xfree(buf); } } @@ -675,7 +691,7 @@ char_u *get_env_name(expand_T *xp, int idx) char *envname = os_getenvname_at_index((size_t)idx); if (envname) { STRLCPY(name, envname, ENVNAMELEN); - free(envname); + xfree(envname); return name; } else { return NULL; |