Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay committed May 14, 2020
1 parent 9c67776 commit 9068c77
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ The API is a combination of parts:
- A convenient string formatting function, which takes a custom allocator:
- rcutils_format_string()
- rcutils/format_string.h
- A function to get an environment variable's value:
- Functions for interfacing with process environment variables:
- rcutils_get_env()
- rcutils_get_home_dir()
- rcutils_set_env()
- rcutils/env.h
- rcutils/get_env.h
- Extensible logging macros:
- Some examples (not exhaustive):
Expand Down
14 changes: 13 additions & 1 deletion include/rcutils/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,20 @@ extern "C"
/**
* This function modifies the environment variables for the current process.
*
* \par Thread Safety:
* This function is not thread-safe, particularly when called concurrently with
* ::rcutils_get_env. Take care not to modify the environment variables while
* another thread might be reading or writing environment variables.
*
* \par Platform Consistency:
* The behavior when setting a variable to an empty string (`""`) differs
* between platforms. On Windows, the variable is un-set (as if \p env_value was
* `NULL`), while on other platforms the variable is set to an empty string as
* expected.
*
* \param[in] env_name Name of the environment variable to modify.
* \param[in] env_value Value to set the environment variable to, or NULL to un-set.
* \param[in] env_value Value to set the environment variable to, or `NULL` to
* un-set.
* \return `True` if success
* \return `False` if env_name is invalid or NULL
* \return `False` on failure
Expand Down
3 changes: 3 additions & 0 deletions src/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ rcutils_set_env(const char * env_name, const char * env_value)
env_value = "";
}
if (0 != _putenv_s(env_name, env_value)) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("_putenv_s failed: %d", errno);
return false;
}
#else
if (NULL == env_value) {
if (0 != unsetenv(env_name)) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("unsetenv failed: %d", errno);
return false;
}
} else {
if (0 != setenv(env_name, env_value, 1)) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("setenv failed: %d", errno);
return false;
}
}
Expand Down

0 comments on commit 9068c77

Please sign in to comment.