getenv: return NULL if empty #2574

Making an environment variable empty can be a way of unsetting it for
platforms that don't support unsetenv(). In most cases, we treat empty
variables as having been unset. For all others, use os_env_exists().
This commit is contained in:
Scott Prager 2015-05-03 09:25:53 -04:00
parent fa0f122221
commit 412d246be7
10 changed files with 67 additions and 60 deletions

View File

@ -808,7 +808,7 @@ static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff)
/* We don't want $DIFF_OPTIONS to get in the way. */
if (os_getenv("DIFF_OPTIONS")) {
vim_setenv("DIFF_OPTIONS", "");
os_unsetenv("DIFF_OPTIONS");
}
/* Build the diff command and execute it. Always use -a, binary

View File

@ -1658,9 +1658,9 @@ static char_u *viminfo_filename(char_u *file)
file = use_viminfo;
else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) {
#ifdef VIMINFO_FILE2
/* don't use $HOME when not defined (turned into "c:/"!). */
if (os_getenv((char_u *)"HOME") == NULL) {
/* don't use $VIM when not available. */
// don't use $HOME when not defined (turned into "c:/"!).
if (!os_env_exists("HOME")) {
// don't use $VIM when not available.
expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */
file = (char_u *)VIMINFO_FILE2;

View File

@ -3025,10 +3025,11 @@ char *get_mess_lang(void)
# endif
# else
p = os_getenv("LC_ALL");
if (p == NULL || *p == NUL) {
if (p == NULL) {
p = os_getenv("LC_MESSAGES");
if (p == NULL || *p == NUL)
if (p == NULL) {
p = os_getenv("LANG");
}
}
# endif
return p;
@ -3044,15 +3045,17 @@ static char_u *get_mess_env(void)
char_u *p;
p = (char_u *)os_getenv("LC_ALL");
if (p == NULL || *p == NUL) {
if (p == NULL) {
p = (char_u *)os_getenv("LC_MESSAGES");
if (p == NULL || *p == NUL) {
if (p == NULL) {
p = (char_u *)os_getenv("LANG");
if (p != NULL && ascii_isdigit(*p))
if (p != NULL && ascii_isdigit(*p)) {
p = NULL; /* ignore something like "1043" */
}
# ifdef HAVE_GET_LOCALE_VAL
if (p == NULL || *p == NUL)
if (p == NULL) {
p = (char_u *)get_locale_val(LC_CTYPE);
}
# endif
}
}

View File

@ -1889,8 +1889,8 @@ static void main_start_gui(void)
/// OK otherwise.
static int process_env(char *env, bool is_viminit)
{
char *initstr = (char *)os_getenv(env);
if (initstr != NULL && *initstr != NUL) {
const char *initstr = os_getenv(env);
if (initstr != NULL) {
if (is_viminit) {
vimrc_found(NULL, NULL);
}
@ -1900,7 +1900,7 @@ static int process_env(char *env, bool is_viminit)
sourcing_lnum = 0;
scid_T save_sid = current_SID;
current_SID = SID_ENV;
do_cmdline_cmd(initstr);
do_cmdline_cmd((char *)initstr);
sourcing_name = save_sourcing_name;
sourcing_lnum = save_sourcing_lnum;
current_SID = save_sid;;

View File

@ -3398,22 +3398,29 @@ static int enc_alias_search(char_u *name)
*/
char_u * enc_locale(void)
{
char *s;
char *p;
int i;
char buf[50];
# ifdef HAVE_NL_LANGINFO_CODESET
if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
# endif
# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
# endif
if ((s = (char *)os_getenv("LC_ALL")) == NULL || *s == NUL)
if ((s = (char *)os_getenv("LC_CTYPE")) == NULL || *s == NUL)
s = (char *)os_getenv("LANG");
if (s == NULL || *s == NUL)
const char *s;
# ifdef HAVE_NL_LANGINFO_CODESET
if (!(s = nl_langinfo(CODESET)) || *s == NUL)
# endif
{
# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
if (!(s = setlocale(LC_CTYPE, NULL)) || *s == NUL)
# endif
{
if ((s = os_getenv("LC_ALL"))) {
if ((s = os_getenv("LC_CTYPE"))) {
s = os_getenv("LANG");
}
}
}
}
if (!s) {
return NULL;
}
/* The most generic locale format is:
* language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
@ -3423,11 +3430,12 @@ char_u * enc_locale(void)
* Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
* "ko_KR.EUC" == "euc-kr"
*/
if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL) {
if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
const char *p = (char *)vim_strchr((char_u *)s, '.');
if (p != NULL) {
if (p > s + 2 && !STRNICMP(p + 1, "EUC", 3)
&& !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_') {
/* copy "XY.EUC" to "euc-XY" to buf[10] */
STRCPY(buf + 10, "euc-");
strcpy(buf + 10, "euc-");
buf[14] = p[-2];
buf[15] = p[-1];
buf[16] = 0;

View File

@ -743,7 +743,7 @@ void ex_messages(exarg_T *eap)
msg_hist_off = TRUE;
s = os_getenv("LANG");
if (s != NULL && *s != NUL)
if (s)
msg_attr((char_u *)
_("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
hl_attr(HLF_T));

View File

@ -16,6 +16,7 @@
#include "nvim/log.h"
#include "nvim/tempfile.h"
#include "nvim/path.h"
#include "nvim/strings.h"
#define MAX_CONNECTIONS 32
#define ADDRESS_MAX_SIZE 256
@ -59,7 +60,7 @@ bool server_init(void)
bool must_free = false;
const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
if (listen_address == NULL || *listen_address == NUL) {
if (listen_address == NULL) {
must_free = true;
listen_address = (char *)vim_tempname();
}
@ -216,7 +217,7 @@ int server_start(const char *endpoint)
// Update $NVIM_LISTEN_ADDRESS, if not set.
const char *listen_address = os_getenv(LISTEN_ADDRESS_ENV_VAR);
if (listen_address == NULL || *listen_address == NUL) {
if (listen_address == NULL) {
os_setenv(LISTEN_ADDRESS_ENV_VAR, addr, 1);
}

View File

@ -1727,7 +1727,7 @@ void set_init_1(void)
p_cp = FALSE;
/* Use POSIX compatibility when $VIM_POSIX is set. */
if (os_getenv("VIM_POSIX") != NULL) {
if (os_env_exists("VIM_POSIX")) {
set_string_default("cpo", (char_u *)CPO_ALL);
set_string_default("shm", (char_u *)"A");
}
@ -1736,8 +1736,7 @@ void set_init_1(void)
* Find default value for 'shell' option.
* Don't use it if it is empty.
*/
if (((p = (char_u *)os_getenv("SHELL")) != NULL && *p != NUL)
)
if ((p = (char_u *)os_getenv("SHELL")) != NULL)
set_string_default("sh", p);
/*
@ -1926,7 +1925,7 @@ void set_init_1(void)
* NOTE: mlterm's author is being asked to 'set' a variable
* instead of an environment variable due to inheritance.
*/
if (os_getenv("MLTERM") != NULL)
if (os_env_exists("MLTERM"))
set_option_value((char_u *)"tbidi", 1L, NULL, 0);
/* Parse default for 'fillchars'. */

View File

@ -27,12 +27,24 @@
#include <sys/utsname.h>
#endif
/// Like getenv(), but returns NULL if the variable is empty.
const char *os_getenv(const char *name)
FUNC_ATTR_NONNULL_ALL
{
return getenv(name);
const char *e = getenv(name);
return e == NULL || *e == NUL ? NULL : e;
}
/// Returns `true` if the environment variable, `name`, has been defined
/// (even if empty).
bool os_env_exists(const char *name)
FUNC_ATTR_NONNULL_ALL
{
return getenv(name) != NULL;
}
int os_setenv(const char *name, const char *value, int overwrite)
FUNC_ATTR_NONNULL_ALL
{
return setenv(name, value, overwrite);
}
@ -123,17 +135,11 @@ static char_u *homedir = NULL;
void init_homedir(void)
{
char_u *var;
/* In case we are called a second time (when 'encoding' changes). */
xfree(homedir);
homedir = NULL;
var = (char_u *)os_getenv("HOME");
if (var != NULL && *var == NUL) /* empty is same as not set */
var = NULL;
char_u *var = (char_u *)os_getenv("HOME");
if (var != NULL) {
#ifdef UNIX
@ -417,11 +423,6 @@ static char *remove_tail(char *p, char *pend, char *name)
char *vim_getenv(const char *name)
{
const char *kos_env_path = os_getenv(name);
if (kos_env_path != NULL
&& *kos_env_path == NUL) { // empty is the same as not set
kos_env_path = NULL;
}
if (kos_env_path != NULL) {
return xstrdup(kos_env_path);
}
@ -440,10 +441,6 @@ char *vim_getenv(const char *name)
#endif
) {
kos_env_path = os_getenv("VIM");
if (kos_env_path != NULL
&& *kos_env_path == NUL) { // empty is the same as not set
kos_env_path = NULL;
}
if (kos_env_path != NULL) {
vim_path = vim_version_dir(kos_env_path);
if (vim_path == NULL) {
@ -533,8 +530,6 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one)
{
size_t dirlen = 0, envlen = 0;
size_t len;
char_u *homedir_env, *homedir_env_orig;
char_u *p;
if (src == NULL) {
*dst = NUL;
@ -556,12 +551,11 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one)
if (homedir != NULL)
dirlen = STRLEN(homedir);
homedir_env_orig = homedir_env = (char_u *)os_getenv("HOME");
/* Empty is the same as not set. */
if (homedir_env != NULL && *homedir_env == NUL)
homedir_env = NULL;
char_u *homedir_env = (char_u *)os_getenv("HOME");
bool must_free = false;
if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) {
must_free = true;
size_t usedlen = 0;
size_t flen = STRLEN(homedir_env);
char_u *fbuf = NULL;
@ -587,7 +581,7 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one)
* as "~er/bla" (which would seem to indicate the file "bla" in user
* er's home directory)).
*/
p = homedir;
char_u *p = homedir;
len = dirlen;
for (;; ) {
if ( len
@ -623,8 +617,9 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one)
*dst = NUL;
if (homedir_env != homedir_env_orig)
if (must_free) {
xfree(homedir_env);
}
}
/// Like home_replace, store the replaced string in allocated memory.

View File

@ -15,6 +15,7 @@
#include "nvim/api/private/helpers.h"
#include "nvim/os/event.h"
#include "nvim/tui/tui.h"
#include "nvim/strings.h"
// Space reserved in the output buffer to restore the cursor to normal when
// flushing. No existing terminal will require 32 bytes to do that.