~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/misc/win32/env.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
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
 * 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_arch_misc.h"
 
21
#include "apr_arch_utf8.h"
 
22
#include "apr_env.h"
 
23
#include "apr_errno.h"
 
24
#include "apr_pools.h"
 
25
 
 
26
 
 
27
#if APR_HAS_UNICODE_FS
 
28
static apr_status_t widen_envvar_name (apr_wchar_t *buffer,
 
29
                                       apr_size_t bufflen,
 
30
                                       const char *envvar)
 
31
{
 
32
    apr_size_t inchars;
 
33
    apr_status_t status;
 
34
 
 
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;
 
39
 
 
40
    return status;
 
41
}
 
42
#endif
 
43
 
 
44
 
 
45
APR_DECLARE(apr_status_t) apr_env_get(char **value,
 
46
                                      const char *envvar,
 
47
                                      apr_pool_t *pool)
 
48
{
 
49
    char *val = NULL;
 
50
    DWORD size;
 
51
 
 
52
#if APR_HAS_UNICODE_FS
 
53
    IF_WIN_OS_IS_UNICODE
 
54
    {
 
55
        apr_wchar_t wenvvar[APR_PATH_MAX];
 
56
        apr_size_t inchars, outchars;
 
57
        apr_wchar_t *wvalue, dummy;
 
58
        apr_status_t status;
 
59
 
 
60
        status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
 
61
        if (status)
 
62
            return status;
 
63
 
 
64
        size = GetEnvironmentVariableW(wenvvar, &dummy, 0);
 
65
        if (size == 0)
 
66
            /* The environment variable doesn't exist. */
 
67
            return APR_ENOENT;
 
68
 
 
69
        wvalue = apr_palloc(pool, size * sizeof(*wvalue));
 
70
        size = GetEnvironmentVariableW(wenvvar, wvalue, size);
 
71
        if (size == 0)
 
72
            /* Mid-air collision?. Somebody must've changed the env. var. */
 
73
            return APR_INCOMPLETE;
 
74
 
 
75
        inchars = wcslen(wvalue) + 1;
 
76
        outchars = 3 * inchars; /* Enougn for any UTF-8 representation */
 
77
        val = apr_palloc(pool, outchars);
 
78
        status = apr_conv_ucs2_to_utf8(wvalue, &inchars, val, &outchars);
 
79
        if (status)
 
80
            return status;
 
81
    }
 
82
#endif
 
83
#if APR_HAS_ANSI_FS
 
84
    ELSE_WIN_OS_IS_ANSI
 
85
    {
 
86
        char dummy;
 
87
 
 
88
        size = GetEnvironmentVariableA(envvar, &dummy, 0);
 
89
        if (size == 0)
 
90
            /* The environment variable doesn't exist. */
 
91
            return APR_ENOENT;
 
92
 
 
93
        val = apr_palloc(pool, size);
 
94
        size = GetEnvironmentVariableA(envvar, val, size);
 
95
        if (size == 0)
 
96
            /* Mid-air collision?. Somebody must've changed the env. var. */
 
97
            return APR_INCOMPLETE;
 
98
    }
 
99
#endif
 
100
 
 
101
    *value = val;
 
102
    return APR_SUCCESS;
 
103
}
 
104
 
 
105
 
 
106
APR_DECLARE(apr_status_t) apr_env_set(const char *envvar,
 
107
                                      const char *value,
 
108
                                      apr_pool_t *pool)
 
109
{
 
110
#if APR_HAS_UNICODE_FS
 
111
    IF_WIN_OS_IS_UNICODE
 
112
    {
 
113
        apr_wchar_t wenvvar[APR_PATH_MAX];
 
114
        apr_wchar_t *wvalue;
 
115
        apr_size_t inchars, outchars;
 
116
        apr_status_t status;
 
117
 
 
118
        status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
 
119
        if (status)
 
120
            return status;
 
121
 
 
122
        outchars = inchars = strlen(value) + 1;
 
123
        wvalue = apr_palloc(pool, outchars * sizeof(*wvalue));
 
124
        status = apr_conv_utf8_to_ucs2(value, &inchars, wvalue, &outchars);
 
125
        if (status)
 
126
            return status;
 
127
 
 
128
        if (!SetEnvironmentVariableW(wenvvar, wvalue))
 
129
            return apr_get_os_error();
 
130
    }
 
131
#endif
 
132
#if APR_HAS_ANSI_FS
 
133
    ELSE_WIN_OS_IS_ANSI
 
134
    {
 
135
        if (!SetEnvironmentVariableA(envvar, value))
 
136
            return apr_get_os_error();
 
137
    }
 
138
#endif
 
139
 
 
140
    return APR_SUCCESS;
 
141
}
 
142
 
 
143
 
 
144
APR_DECLARE(apr_status_t) apr_env_delete(const char *envvar, apr_pool_t *pool)
 
145
{
 
146
#if APR_HAS_UNICODE_FS
 
147
    IF_WIN_OS_IS_UNICODE
 
148
    {
 
149
        apr_wchar_t wenvvar[APR_PATH_MAX];
 
150
        apr_status_t status;
 
151
 
 
152
        status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
 
153
        if (status)
 
154
            return status;
 
155
 
 
156
        if (!SetEnvironmentVariableW(wenvvar, NULL))
 
157
            return apr_get_os_error();
 
158
    }
 
159
#endif
 
160
#if APR_HAS_ANSI_FS
 
161
    ELSE_WIN_OS_IS_ANSI
 
162
    {
 
163
        if (!SetEnvironmentVariableA(envvar, NULL))
 
164
            return apr_get_os_error();
 
165
    }
 
166
#endif
 
167
 
 
168
    return APR_SUCCESS;
 
169
}