Debian Codegraph @

crt/aws-c-common/source/posix/environment.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/environment.h>
7
8 #include <aws/common/string.h>
9 #include <stdlib.h>
10
11 struct aws_string *aws_get_env(struct aws_allocator *allocator, const char *name) {
12
13 const char *value = getenv(name);
14 if (value == NULL) {
15 return NULL;
16 }
17
18 return aws_string_new_from_c_str(allocator, value);
19 }
20
21 struct aws_string *aws_get_env_nonempty(struct aws_allocator *allocator, const char *name) {
22
23 const char *value = getenv(name);
24 if (value == NULL || value[0] == '\0') {
25 return NULL;
26 }
27
28 return aws_string_new_from_c_str(allocator, value);
29 }
30
31 int aws_get_environment_value(
32 struct aws_allocator *allocator,
33 const struct aws_string *variable_name,
34 struct aws_string **value_out) {
35
36 const char *value = getenv(aws_string_c_str(variable_name));
37 if (value == NULL) {
38 *value_out = NULL;
39 return AWS_OP_SUCCESS;
40 }
41
42 *value_out = aws_string_new_from_c_str(allocator, value);
43 if (*value_out == NULL) {
44 return aws_raise_error(AWS_ERROR_ENVIRONMENT_GET);
45 }
46
47 return AWS_OP_SUCCESS;
48 }
49
50 int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) {
51
52 if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) {
53 return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET);
54 }
55
56 return AWS_OP_SUCCESS;
57 }
58
59 int aws_unset_environment_value(const struct aws_string *variable_name) {
60 if (unsetenv(aws_string_c_str(variable_name)) != 0) {
61 return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET);
62 }
63
64 return AWS_OP_SUCCESS;
65 }