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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define APR_WANT_STRFUNC
20 #include "apr_arch_misc.h"
21 #include "apr_arch_utf8.h"
23 #include "apr_errno.h"
24 #include "apr_pools.h"
25 #include "apr_strings.h"
27 #if APR_HAS_UNICODE_FS && !defined(_WIN32_WCE)
28 static apr_status_t widen_envvar_name (apr_wchar_t *buffer,
35 inchars = strlen(envvar) + 1;
36 status = apr_conv_utf8_to_ucs2(envvar, &inchars, buffer, &bufflen);
37 if (status == APR_INCOMPLETE)
38 status = APR_ENAMETOOLONG;
45 APR_DECLARE(apr_status_t) apr_env_get(char **value,
49 #if defined(_WIN32_WCE)
55 #if APR_HAS_UNICODE_FS
58 apr_wchar_t wenvvar[APR_PATH_MAX];
59 apr_size_t inchars, outchars;
60 apr_wchar_t *wvalue, dummy;
63 status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
68 size = GetEnvironmentVariableW(wenvvar, &dummy, 0);
69 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
70 /* The environment variable doesn't exist. */
74 /* The environment value exists, but is zero-length. */
75 *value = apr_pstrdup(pool, "");
79 wvalue = apr_palloc(pool, size * sizeof(*wvalue));
80 size = GetEnvironmentVariableW(wenvvar, wvalue, size);
82 inchars = wcslen(wvalue) + 1;
83 outchars = 3 * inchars; /* Enough for any UTF-8 representation */
84 val = apr_palloc(pool, outchars);
85 status = apr_conv_ucs2_to_utf8(wvalue, &inchars, val, &outchars);
96 size = GetEnvironmentVariableA(envvar, &dummy, 0);
97 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
98 /* The environment variable doesn't exist. */
102 /* The environment value exists, but is zero-length. */
103 *value = apr_pstrdup(pool, "");
107 val = apr_palloc(pool, size);
108 size = GetEnvironmentVariableA(envvar, val, size);
110 /* Mid-air collision?. Somebody must've changed the env. var. */
111 return APR_INCOMPLETE;
121 APR_DECLARE(apr_status_t) apr_env_set(const char *envvar,
125 #if defined(_WIN32_WCE)
128 #if APR_HAS_UNICODE_FS
131 apr_wchar_t wenvvar[APR_PATH_MAX];
133 apr_size_t inchars, outchars;
136 status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
140 outchars = inchars = strlen(value) + 1;
141 wvalue = apr_palloc(pool, outchars * sizeof(*wvalue));
142 status = apr_conv_utf8_to_ucs2(value, &inchars, wvalue, &outchars);
146 if (!SetEnvironmentVariableW(wenvvar, wvalue))
147 return apr_get_os_error();
153 if (!SetEnvironmentVariableA(envvar, value))
154 return apr_get_os_error();
163 APR_DECLARE(apr_status_t) apr_env_delete(const char *envvar, apr_pool_t *pool)
165 #if defined(_WIN32_WCE)
168 #if APR_HAS_UNICODE_FS
171 apr_wchar_t wenvvar[APR_PATH_MAX];
174 status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
178 if (!SetEnvironmentVariableW(wenvvar, NULL))
179 return apr_get_os_error();
185 if (!SetEnvironmentVariableA(envvar, NULL))
186 return apr_get_os_error();