environment.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/environment.h>
  6. #include <aws/common/string.h>
  7. #include <stdlib.h>
  8. int aws_get_environment_value(
  9. struct aws_allocator *allocator,
  10. const struct aws_string *variable_name,
  11. struct aws_string **value_out) {
  12. const char *value = getenv(aws_string_c_str(variable_name));
  13. if (value == NULL) {
  14. *value_out = NULL;
  15. return AWS_OP_SUCCESS;
  16. }
  17. *value_out = aws_string_new_from_c_str(allocator, value);
  18. if (*value_out == NULL) {
  19. return aws_raise_error(AWS_ERROR_ENVIRONMENT_GET);
  20. }
  21. return AWS_OP_SUCCESS;
  22. }
  23. int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) {
  24. if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) {
  25. return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET);
  26. }
  27. return AWS_OP_SUCCESS;
  28. }
  29. int aws_unset_environment_value(const struct aws_string *variable_name) {
  30. if (unsetenv(aws_string_c_str(variable_name)) != 0) {
  31. return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET);
  32. }
  33. return AWS_OP_SUCCESS;
  34. }