Skip to content

Commit

Permalink
wip impl
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Aug 5, 2024
1 parent 70d97a8 commit 296de34
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
24 changes: 22 additions & 2 deletions include/aws/common/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,28 @@ struct aws_string;
AWS_EXTERN_C_BEGIN

/*
* 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
* 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
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(
Expand Down
27 changes: 26 additions & 1 deletion source/posix/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,38 @@
#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,
struct aws_string **value_out) {

const char *value = getenv(aws_string_c_str(variable_name));
if (value == NULL || value[0] == '\0') {
if (value == NULL) {
*value_out = NULL;
return AWS_OP_SUCCESS;
}
Expand Down
26 changes: 25 additions & 1 deletion source/windows/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@

#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 All @@ -24,7 +48,7 @@ int aws_get_environment_value(
# pragma warning(pop)
#endif

if (value == NULL || value[0] == '\0') {
if (value == NULL) {
*value_out = NULL;
return AWS_OP_SUCCESS;
}
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);
ASSERT_TRUE(aws_string_compare(value, empty_str) == 0);
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 296de34

Please sign in to comment.