| 1 | /* |
| 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <aws/common/environment.h> |
| 17 | |
| 18 | #include <aws/common/string.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | int aws_get_environment_value( |
| 22 | struct aws_allocator *allocator, |
| 23 | const struct aws_string *variable_name, |
| 24 | struct aws_string **value_out) { |
| 25 | |
| 26 | const char *value = getenv(aws_string_c_str(variable_name)); |
| 27 | if (value == NULL) { |
| 28 | *value_out = NULL; |
| 29 | return AWS_OP_SUCCESS; |
| 30 | } |
| 31 | |
| 32 | *value_out = aws_string_new_from_c_str(allocator, value); |
| 33 | if (*value_out == NULL) { |
| 34 | return aws_raise_error(AWS_ERROR_ENVIRONMENT_GET); |
| 35 | } |
| 36 | |
| 37 | return AWS_OP_SUCCESS; |
| 38 | } |
| 39 | |
| 40 | int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) { |
| 41 | |
| 42 | if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) { |
| 43 | return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET); |
| 44 | } |
| 45 | |
| 46 | return AWS_OP_SUCCESS; |
| 47 | } |
| 48 | |
| 49 | int aws_unset_environment_value(const struct aws_string *variable_name) { |
| 50 | if (unsetenv(aws_string_c_str(variable_name)) != 0) { |
| 51 | return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET); |
| 52 | } |
| 53 | |
| 54 | return AWS_OP_SUCCESS; |
| 55 | } |
| 56 | |