/src/open5gs/lib/core/ogs-env.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "core-config-private.h" |
21 | | |
22 | | #include "ogs-core.h" |
23 | | |
24 | | char *ogs_env_get(const char *envvar) |
25 | 0 | { |
26 | 0 | #ifdef HAVE_GETENV |
27 | 0 | return getenv(envvar); |
28 | | #else |
29 | | return NULL; |
30 | | #endif |
31 | 0 | } |
32 | | |
33 | | |
34 | | int ogs_env_set(const char *envvar, const char *value) |
35 | 0 | { |
36 | 0 | #if defined(HAVE_SETENV) |
37 | |
|
38 | 0 | if (0 > setenv(envvar, value, 1)) { |
39 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_errno, "setenv() failed"); |
40 | 0 | return OGS_ERROR; |
41 | 0 | } |
42 | 0 | return OGS_OK; |
43 | |
|
44 | | #elif defined(HAVE_PUTENV) |
45 | | |
46 | | char buf[OGS_HUGE_LEN]; |
47 | | |
48 | | if (ogs_snprintf(buf, OGS_HUGE_LEN, "%s=%s", envvar, value) < 0) { |
49 | | ogs_error("snprintf() failed"); |
50 | | return OGS_ERROR; |
51 | | } |
52 | | if (0 > putenv(buf)) { |
53 | | ogs_log_message(OGS_LOG_ERROR, ogs_errno, "putenv() failed"); |
54 | | return OGS_ERROR; |
55 | | } |
56 | | return OGS_OK; |
57 | | |
58 | | #else |
59 | | ogs_error("Not implemented"); |
60 | | return OGS_ERROR; |
61 | | #endif |
62 | 0 | } |
63 | | |
64 | | |
65 | | int ogs_env_delete(const char *envvar) |
66 | 0 | { |
67 | 0 | #ifdef HAVE_UNSETENV |
68 | |
|
69 | 0 | if (0 > unsetenv(envvar)) { |
70 | 0 | ogs_error("unsetenv() failed"); |
71 | 0 | return OGS_ERROR; |
72 | 0 | } |
73 | 0 | return OGS_OK; |
74 | |
|
75 | | #else |
76 | | /* hint: some platforms allow envvars to be unset via |
77 | | * putenv("varname")... that isn't Single Unix spec, |
78 | | * but if your platform doesn't have unsetenv() it is |
79 | | * worth investigating and potentially adding a |
80 | | * configure check to decide when to use that form of |
81 | | * putenv() here |
82 | | */ |
83 | | ogs_error("Not implemented"); |
84 | | return OGS_ERROR; |
85 | | #endif |
86 | 0 | } |