Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include <curl/curl.h> |
28 | | |
29 | | static char *GetEnv(const char *variable) |
30 | 1 | { |
31 | | #if defined(CURL_WINDOWS_UWP) || \ |
32 | | defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */ |
33 | | (void)variable; |
34 | | return NULL; |
35 | | #elif defined(_WIN32) |
36 | | /* This uses Windows API instead of C runtime getenv() to get the environment |
37 | | variable since some changes are not always visible to the latter. #4774 */ |
38 | | char *buf = NULL; |
39 | | char *tmp; |
40 | | DWORD bufsize; |
41 | | DWORD rc = 1; |
42 | | const DWORD max = 32768; /* max env var size from MSCRT source */ |
43 | | |
44 | | for(;;) { |
45 | | tmp = curlx_realloc(buf, rc); |
46 | | if(!tmp) { |
47 | | curlx_free(buf); |
48 | | return NULL; |
49 | | } |
50 | | |
51 | | buf = tmp; |
52 | | bufsize = rc; |
53 | | |
54 | | /* it is possible for rc to be 0 if the variable was found but empty. |
55 | | Since getenv does not make that distinction we ignore it as well. */ |
56 | | rc = GetEnvironmentVariableA(variable, buf, bufsize); |
57 | | if(!rc || rc == bufsize || rc > max) { |
58 | | curlx_free(buf); |
59 | | return NULL; |
60 | | } |
61 | | |
62 | | /* if rc < bufsize then rc is bytes written not including null */ |
63 | | if(rc < bufsize) |
64 | | return buf; |
65 | | |
66 | | /* else rc is bytes needed, try again */ |
67 | | } |
68 | | #else |
69 | 1 | char *env = getenv(variable); |
70 | 1 | return (env && env[0]) ? curlx_strdup(env) : NULL; |
71 | 1 | #endif |
72 | 1 | } |
73 | | |
74 | | char *curl_getenv(const char *v) |
75 | 1 | { |
76 | 1 | return GetEnv(v); |
77 | 1 | } |