aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Prager <splinterofchaos@gmail.com>2015-05-03 09:25:53 -0400
committerScott Prager <splinterofchaos@gmail.com>2015-05-29 13:12:12 -0400
commit412d246be71bd99cb4edde4e6f984b0b0d91bcd9 (patch)
tree01307051583b9cf2faf56778a9bd88e00d2a2122 /src
parentfa0f1222212704c93ab828b876bda5e9e1cb507e (diff)
downloadrneovim-412d246be71bd99cb4edde4e6f984b0b0d91bcd9.tar.gz
rneovim-412d246be71bd99cb4edde4e6f984b0b0d91bcd9.tar.bz2
rneovim-412d246be71bd99cb4edde4e6f984b0b0d91bcd9.zip
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().
Diffstat (limited to 'src')
-rw-r--r--src/nvim/diff.c2
-rw-r--r--src/nvim/ex_cmds.c6
-rw-r--r--src/nvim/ex_cmds2.c15
-rw-r--r--src/nvim/main.c6
-rw-r--r--src/nvim/mbyte.c30
-rw-r--r--src/nvim/message.c2
-rw-r--r--src/nvim/msgpack_rpc/server.c5
-rw-r--r--src/nvim/option.c7
-rw-r--r--src/nvim/os/env.c45
-rw-r--r--src/nvim/tui/tui.c1
10 files changed, 63 insertions, 56 deletions
diff --git a/src/nvim/diff.c b/src/nvim/diff.c
index 7c1a9de623..6e2b3056e4 100644
--- a/src/nvim/diff.c
+++ b/src/nvim/diff.c
@@ -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
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 824cc125f4..b4adef9235 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -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;
diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c
index 3fe4acc471..5c1f487d5e 100644
--- a/src/nvim/ex_cmds2.c
+++ b/src/nvim/ex_cmds2.c
@@ -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
}
}
diff --git a/src/nvim/main.c b/src/nvim/main.c
index fc72039b5f..ddc39caa6f 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -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;;
diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c
index b6e08ab277..368ae8e773 100644
--- a/src/nvim/mbyte.c
+++ b/src/nvim/mbyte.c
@@ -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];
+
+ const char *s;
# ifdef HAVE_NL_LANGINFO_CODESET
- if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
+ if (!(s = nl_langinfo(CODESET)) || *s == NUL)
# endif
+ {
# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
- if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
+ if (!(s = setlocale(LC_CTYPE, 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 = os_getenv("LC_ALL"))) {
+ if ((s = os_getenv("LC_CTYPE"))) {
+ s = os_getenv("LANG");
+ }
+ }
+ }
+ }
- if (s == NULL || *s == NUL)
+ 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;
diff --git a/src/nvim/message.c b/src/nvim/message.c
index 9bb92f8928..5b4c90cc8f 100644
--- a/src/nvim/message.c
+++ b/src/nvim/message.c
@@ -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));
diff --git a/src/nvim/msgpack_rpc/server.c b/src/nvim/msgpack_rpc/server.c
index ab1b04d73f..388b5a04cf 100644
--- a/src/nvim/msgpack_rpc/server.c
+++ b/src/nvim/msgpack_rpc/server.c
@@ -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);
}
diff --git a/src/nvim/option.c b/src/nvim/option.c
index b3789e0e5b..6fb661b6e2 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -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'. */
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
index fd115ab2b3..7be8a868bd 100644
--- a/src/nvim/os/env.c
+++ b/src/nvim/os/env.c
@@ -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.
diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c
index 1a8e4523b7..7df1c4f381 100644
--- a/src/nvim/tui/tui.c
+++ b/src/nvim/tui/tui.c
@@ -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.