Skip to content

Commit

Permalink
New Get_ENV Functions (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored Mar 4, 2025
1 parent 9fd58f9 commit 568f46b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/aws/common/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ AWS_EXTERN_C_BEGIN
* Not thread-safe
*/
AWS_COMMON_API
struct aws_string *aws_get_env(struct aws_allocator *allocator, const char *name);

/*
* Get the value of an environment variable. If the variable is not set or is empty, the output string will be set to
* NULL. Not thread-safe
*/
AWS_COMMON_API
struct aws_string *aws_get_env_nonempty(struct aws_allocator *allocator, const char *name);

/*
* *DEPRECATED*
* Please use the `aws_get_env` or `aws_get_env_nonempty` instead.
* Get the value of an environment variable. If the variable is not set, the output string will be set to NULL.
* Not thread-safe
*/
AWS_COMMON_API
int aws_get_environment_value(
struct aws_allocator *allocator,
const struct aws_string *variable_name,
Expand Down
20 changes: 20 additions & 0 deletions source/posix/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
#include <aws/common/string.h>
#include <stdlib.h>

struct aws_string *aws_get_env(struct aws_allocator *allocator, const char *name) {

const char *value = getenv(name);
if (value == NULL) {
return NULL;
}

return aws_string_new_from_c_str(allocator, value);
}

struct aws_string *aws_get_env_nonempty(struct aws_allocator *allocator, const char *name) {

const char *value = getenv(name);
if (value == NULL || value[0] == '\0') {
return NULL;
}

return aws_string_new_from_c_str(allocator, value);
}

int aws_get_environment_value(
struct aws_allocator *allocator,
const struct aws_string *variable_name,
Expand Down
34 changes: 34 additions & 0 deletions source/windows/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@

#include <stdlib.h>

struct aws_string *aws_get_env(struct aws_allocator *allocator, const char *name) {
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4996)
#endif
const char *value = getenv(name);
#ifdef _MSC_VER
# pragma warning(pop)
#endif

if (value == NULL) {
return NULL;
}

return aws_string_new_from_c_str(allocator, value);
}

struct aws_string *aws_get_env_nonempty(struct aws_allocator *allocator, const char *name) {
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4996)
#endif
const char *value = getenv(name);
#ifdef _MSC_VER
# pragma warning(pop)
#endif

if (value == NULL || value[0] == '\0') {
return NULL;
}

return aws_string_new_from_c_str(allocator, value);
}

int aws_get_environment_value(
struct aws_allocator *allocator,
const struct aws_string *variable_name,
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ add_test_case(uuid_string_parse_too_short)
add_test_case(uuid_string_parse_malformed)

add_test_case(test_environment_functions)
add_test_case(test_env_functions)

add_test_case(short_argument_parse)
add_test_case(long_argument_parse)
Expand Down
40 changes: 40 additions & 0 deletions tests/environment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@ static int s_test_environment_functions_fn(struct aws_allocator *allocator, void
}

AWS_TEST_CASE(test_environment_functions, s_test_environment_functions_fn)

static int s_test_env_functions_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

const char *env_name = aws_string_c_str(s_test_variable);
struct aws_string *value = aws_get_env(allocator, env_name);
ASSERT_TRUE(value == NULL);

value = aws_get_env_nonempty(allocator, env_name);
ASSERT_TRUE(value == NULL);

int result = aws_set_environment_value(s_test_variable, (struct aws_string *)s_test_value);
ASSERT_TRUE(result == AWS_OP_SUCCESS);

value = aws_get_env(allocator, env_name);
ASSERT_TRUE(aws_string_compare(value, s_test_value) == 0);
aws_string_destroy(value);

value = aws_get_env_nonempty(allocator, env_name);
ASSERT_TRUE(aws_string_compare(value, s_test_value) == 0);
aws_string_destroy(value);

struct aws_string *empty_str = aws_string_new_from_c_str(allocator, "");
result = aws_set_environment_value(s_test_variable, empty_str);
ASSERT_TRUE(result == AWS_OP_SUCCESS);

value = aws_get_env(allocator, env_name);
#ifndef AWS_OS_WINDOWS
ASSERT_TRUE(aws_string_compare(value, empty_str) == 0);
#endif
aws_string_destroy(value);

value = aws_get_env_nonempty(allocator, env_name);
ASSERT_TRUE(value == NULL);

aws_string_destroy(empty_str);
return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_env_functions, s_test_env_functions_fn)

0 comments on commit 568f46b

Please sign in to comment.