Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersEnvironmentVariablesSystem.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3// Helpers for working with environment variables in a portable way. This mainly
4// abstracts the string type differences between Win32 and non-Win32 environment
5// variables.
6
7#include "nuitka/environment_variables_system.h"
8#include "nuitka/safe_string_ops.h"
9
10#if defined(_WIN32)
11
12environment_char_t const *getEnvironmentVariableW(wchar_t const *name) {
13 // Max size for environment variables according to docs.
14 wchar_t buffer[32768];
15 buffer[0] = 0;
16
17 // Size must be in bytes apparently, not in characters. Cannot be larger anyway.
18 DWORD res = GetEnvironmentVariableW(name, buffer, 65536);
19
20 if (res == 0 || res > sizeof(buffer)) {
21 return NULL;
22 }
23
24 return wcsdup(buffer);
25}
26
27environment_char_t const *getEnvironmentVariable(char const *name) {
28 wchar_t name_wide[40];
29 name_wide[0] = 0;
30 appendStringSafeW(name_wide, name, sizeof(name_wide) / sizeof(wchar_t));
31
32 return getEnvironmentVariableW(name_wide);
33}
34
35void setEnvironmentVariable(char const *name, environment_char_t const *value) {
36 assert(name != NULL);
37 assert(value != NULL);
38
39 wchar_t name_wide[40];
40 name_wide[0] = 0;
41 appendStringSafeW(name_wide, name, sizeof(name_wide) / sizeof(wchar_t));
42
43 DWORD res = SetEnvironmentVariableW(name_wide, value);
44 assert(wcscmp(getEnvironmentVariable(name), value) == 0);
45
46 assert(res != 0);
47}
48
49void unsetEnvironmentVariable(char const *name) {
50 wchar_t name_wide[40];
51 name_wide[0] = 0;
52 appendStringSafeW(name_wide, name, sizeof(name_wide) / sizeof(wchar_t));
53
54 DWORD res = SetEnvironmentVariableW(name_wide, NULL);
55
56 assert(res != 0);
57}
58
59#else
60
61environment_char_t const *getEnvironmentVariable(char const *name) { return getenv(name); }
62
63void setEnvironmentVariable(char const *name, environment_char_t const *value) { setenv(name, value, 1); }
64
65void unsetEnvironmentVariable(char const *name) { unsetenv(name); }
66
67#endif
68
69void setEnvironmentVariableFromLong(char const *name, long value) {
70 char buffer[128];
71 snprintf(buffer, sizeof(buffer), "%ld", value);
72
73#if defined(_WIN32)
74 wchar_t buffer2[128];
75 buffer2[0] = 0;
76 appendStringSafeW(buffer2, buffer, 128);
77
78 setEnvironmentVariable(name, buffer2);
79#else
80 setEnvironmentVariable(name, buffer);
81#endif
82}
83
84// Part of "Nuitka", an optimizing Python compiler that is compatible and
85// integrates with CPython, but also works on its own.
86//
87// Licensed under the Apache License, Version 2.0 (the "License");
88// you may not use this file except in compliance with the License.
89// You may obtain a copy of the License at
90//
91// http://www.apache.org/licenses/LICENSE-2.0
92//
93// Unless required by applicable law or agreed to in writing, software
94// distributed under the License is distributed on an "AS IS" BASIS,
95// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
96// See the License for the specific language governing permissions and
97// limitations under the License.