Skip to content

Commit

Permalink
Deprecate get_env.h and move content to env.{h,c} (#340)
Browse files Browse the repository at this point in the history
* Deprecate get_env.h and move content to env.{h,c}

* Update documentation
* Merge test_get_env.cpp into test_env.cpp
* Use push/pop for Windows warning
* Remove rcutils/get_env.h includes
* Fix rcutils_get_home_dir doc

Signed-off-by: Christophe Bedard <[email protected]>
  • Loading branch information
christophebedard authored May 4, 2021
1 parent ca42bc9 commit 1570091
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 236 deletions.
10 changes: 1 addition & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ set(rcutils_sources
src/filesystem.c
src/find.c
src/format_string.c
src/get_env.c
src/hash_map.c
src/logging.c
src/process.c
Expand Down Expand Up @@ -311,19 +310,12 @@ if(BUILD_TESTING)
target_link_libraries(test_string_array ${PROJECT_NAME})
endif()

rcutils_custom_add_gtest(test_get_env test/test_get_env.cpp
rcutils_custom_add_gtest(test_env test/test_env.cpp
ENV
EMPTY_TEST=
NORMAL_TEST=foo
APPEND_LIBRARY_DIRS "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
)
if(TARGET test_get_env)
target_link_libraries(test_get_env ${PROJECT_NAME})
endif()

rcutils_custom_add_gtest(test_env test/test_env.cpp
APPEND_LIBRARY_DIRS "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
)
if(TARGET test_env)
target_link_libraries(test_env ${PROJECT_NAME})
endif()
Expand Down
63 changes: 60 additions & 3 deletions include/rcutils/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ extern "C"
#include "rcutils/macros.h"
#include "rcutils/visibility_control.h"

// TODO(cottsay): Deprecate get_env.h and eventually merge it here
#include "rcutils/get_env.h"

/// Set or un-set a process-scoped environment variable.
/**
* This function modifies the environment variables for the current process by
Expand Down Expand Up @@ -58,6 +55,66 @@ RCUTILS_WARN_UNUSED
bool
rcutils_set_env(const char * env_name, const char * env_value);

/// Retrieve the value of the given environment variable if it exists, or "".
/** The c-string which is returned in the env_value output parameter is only
* valid until the next time this function is called, because it is a direct
* pointer to the static storage.
* The variable env_value populated by this function should never have free()
* called on it.
* If the environment variable is not set, an empty string will be returned.
*
* In both cases, environment variable set or unset, NULL is returned unless
* an exception has occurred, in which case the error string is returned.
* For example:
*
* ```c
* #include <stdio.h>
* #include <rcutils/env.h>
* const char * env_value;
* const char * error_str;
* error_str = rcutils_get_env("SOME_ENV_VAR", &env_value);
* if (error_str != NULL) {
* fprintf(stderr, "Error getting env var: %s\n", error_str);
* }
* printf("Valued of 'SOME_ENV_VAR': %s\n", env_value);
* ```
*
* This function cannot be concurrently called together with rcutils_set_env (or any platform specific equivalent)
* on different threads, but multiple concurrent calls to this function are thread safe.
*
* \param[in] env_name the name of the environment variable
* \param[out] env_value pointer to the value cstring, or "" if unset
* \return NULL on success (success can be returning an empty string), or
* \return an error string on failure.
*/
RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
const char *
rcutils_get_env(const char * env_name, const char ** env_value);

/// Retrieve the full path to the home directory.
/**
* The c-string which is returned is only valid until the next time this
* function is called, because it is a direct pointer to the static storage.
* Also note that the string returned here should *not* be freed.
*
* The function first tries to get the HOME environment variable.
* If that variable exists and is non-empty, that will be returned.
* Otherwise, on Windows, the function tries to get the USERPROFILE environment variable.
* If that variable exists and is non-empty, that will be returned.
* Otherwise, NULL will be returned.
*
* This function cannot be thread-safely called together with rcutils_set_env
* (or any platform specific equivalent), but multiple calls to this function are thread safe.
*
* \return The home directory on success, or
* \return `NULL` on failure.
*/
RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
const char *
rcutils_get_home_dir(void);

#ifdef __cplusplus
}
#endif
Expand Down
77 changes: 7 additions & 70 deletions include/rcutils/get_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,14 @@
#ifndef RCUTILS__GET_ENV_H_
#define RCUTILS__GET_ENV_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "rcutils/macros.h"
#include "rcutils/visibility_control.h"

/// Retrieve the value of the given environment variable if it exists, or "".
/** The c-string which is returned in the env_value output parameter is only
* valid until the next time this function is called, because it is a direct
* pointer to the static storage.
* The variable env_value populated by this function should never have free()
* called on it.
* If the environment variable is not set, an empty string will be returned.
*
* In both cases, environment variable set or unset, NULL is returned unless
* an exception has occurred, in which case the error string is returned.
* For example:
*
* ```c
* #include <stdio.h>
* #include <rcutils/get_env.h>
* const char * env_value;
* const char * error_str;
* error_str = rcutils_get_env("SOME_ENV_VAR", &env_value);
* if (error_str != NULL) {
* fprintf(stderr, "Error getting env var: %s\n", error_str);
* }
* printf("Valued of 'SOME_ENV_VAR': %s\n", env_value);
* ```
*
* This function cannot be concurrently called together with rcutils_set_env (or any platform specific equivalent)
* on different threads, but multiple concurrent calls to this function are thread safe.
*
* \param[in] env_name the name of the environment variable
* \param[out] env_value pointer to the value cstring, or "" if unset
* \return NULL on success (success can be returning an empty string), or
* \return an error string on failure.
*/
RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
const char *
rcutils_get_env(const char * env_name, const char ** env_value);
// TODO(christophebedard) remove this header completely in I-turtle

/// Retrieve the full path to the home directory.
/**
* The c-string which is returned is only valid until the next time this
* function is called, because it is a direct pointer to the static storage.
* Also note that the string returned here should *not* be freed.
*
* The function first tries to get the HOME environment variable.
* If that variable exists and is non-empty, that will be returned.
* Otherwise, the function tries to get the USERPROFILE environment variable.
* If that variable exists and is non-empty, that will be returned.
* If neither exists, NULL will be returned.
* The above algorithm is portable across both Unix and Windows.
*
* This function cannot be thread-safely called together with rcutils_set_env
* (or any platform specific equivalent), but multiple calls to this function are thread safe.
*
* \return The home directory on success, or
* \return `NULL` on failure.
*/
RCUTILS_PUBLIC
RCUTILS_WARN_UNUSED
const char *
rcutils_get_home_dir(void);

#ifdef __cplusplus
}
#ifdef _MSC_VER
#pragma message ("rcutils/get_env.h has been deprecated, please include rcutils/env.h instead")
#else
#warning rcutils/get_env.h has been deprecated, please include rcutils/env.h instead
#endif

#include "rcutils/env.h"

#endif // RCUTILS__GET_ENV_H_
55 changes: 53 additions & 2 deletions src/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ extern "C"
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include "rcutils/env.h"
#include "rcutils/error_handling.h"

// TODO(cottsay): Move the stuff in get_env.c in here

bool
rcutils_set_env(const char * env_name, const char * env_value)
{
Expand Down Expand Up @@ -58,6 +57,58 @@ rcutils_set_env(const char * env_name, const char * env_value)
return true;
}

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4996)
#endif

const char *
rcutils_get_env(const char * env_name, const char ** env_value)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF("some string error");

if (NULL == env_name) {
return "argument env_name is null";
}
if (NULL == env_value) {
return "argument env_value is null";
}

// TODO(Suyash458): getenv is deprecated on Windows; consider using getenv_s instead
*env_value = getenv(env_name);

if (NULL == *env_value) {
*env_value = "";
}
return NULL;
}

#ifdef _WIN32
#pragma warning(pop)
#endif

const char *
rcutils_get_home_dir(void)
{
const char * homedir;

if (rcutils_get_env("HOME", &homedir) == NULL && *homedir != '\0') {
// The HOME environment variable was set and is non-empty, return it.
return homedir;
}

#ifdef _WIN32
// We didn't find a HOME variable, try USERPROFILE on Windows.
if (rcutils_get_env("USERPROFILE", &homedir) == NULL && *homedir != '\0') {
// The USERPROFILE environment variable was set and is non-empty, return it.
return homedir;
}
#endif

// Couldn't get the home directory, return NULL.
return NULL;
}

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion src/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ extern "C"
#include <direct.h>
#endif // _WIN32

#include "rcutils/env.h"
#include "rcutils/error_handling.h"
#include "rcutils/format_string.h"
#include "rcutils/get_env.h"
#include "rcutils/repl_str.h"
#include "rcutils/strdup.h"

Expand Down
74 changes: 0 additions & 74 deletions src/get_env.c

This file was deleted.

2 changes: 1 addition & 1 deletion src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ extern "C"
#endif

#include "rcutils/allocator.h"
#include "rcutils/env.h"
#include "rcutils/error_handling.h"
#include "rcutils/find.h"
#include "rcutils/format_string.h"
#include "rcutils/get_env.h"
#include "rcutils/logging.h"
#include "rcutils/snprintf.h"
#include "rcutils/strdup.h"
Expand Down
Loading

0 comments on commit 1570091

Please sign in to comment.