xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/misc/unix/env.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define APR_WANT_STRFUNC
18 #include "apr_want.h"
19 #include "apr.h"
20 #include "apr_private.h"
21 #include "apr_env.h"
22 #include "apr_strings.h"
23 
24 #if APR_HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #if APR_HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 
apr_env_get(char ** value,const char * envvar,apr_pool_t * pool)31 APR_DECLARE(apr_status_t) apr_env_get(char **value,
32                                       const char *envvar,
33                                       apr_pool_t *pool)
34 {
35 #ifdef HAVE_GETENV
36 
37     char *val = getenv(envvar);
38     if (!val)
39         return APR_ENOENT;
40     *value = val;
41     return APR_SUCCESS;
42 
43 #else
44     return APR_ENOTIMPL;
45 #endif
46 }
47 
48 
apr_env_set(const char * envvar,const char * value,apr_pool_t * pool)49 APR_DECLARE(apr_status_t) apr_env_set(const char *envvar,
50                                       const char *value,
51                                       apr_pool_t *pool)
52 {
53 #if defined(HAVE_SETENV)
54 
55     if (0 > setenv(envvar, value, 1))
56         return APR_ENOMEM;
57     return APR_SUCCESS;
58 
59 #elif defined(HAVE_PUTENV)
60 
61     if (0 > putenv(apr_pstrcat(pool, envvar, "=", value, NULL)))
62         return APR_ENOMEM;
63     return APR_SUCCESS;
64 
65 #else
66     return APR_ENOTIMPL;
67 #endif
68 }
69 
70 
apr_env_delete(const char * envvar,apr_pool_t * pool)71 APR_DECLARE(apr_status_t) apr_env_delete(const char *envvar, apr_pool_t *pool)
72 {
73 #ifdef HAVE_UNSETENV
74 
75     unsetenv(envvar);
76     return APR_SUCCESS;
77 
78 #else
79     /* hint: some platforms allow envvars to be unset via
80      *       putenv("varname")...  that isn't Single Unix spec,
81      *       but if your platform doesn't have unsetenv() it is
82      *       worth investigating and potentially adding a
83      *       configure check to decide when to use that form of
84      *       putenv() here
85      */
86     return APR_ENOTIMPL;
87 #endif
88 }
89