Skip to content

Commit 8ccd43a

Browse files
committed
windows: add support for syscall.runtimeSetenv
I missed this in #3391 (because I didn't test on Windows, my fault).
1 parent 50f8d08 commit 8ccd43a

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/runtime/env_unix.go renamed to src/runtime/env.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build linux || darwin
1+
//go:build linux || darwin || windows
22

33
package runtime
44

@@ -9,8 +9,7 @@ package runtime
99
func syscallSetenv(key, value string) {
1010
keydata := cstring(key)
1111
valdata := cstring(value)
12-
// ignore any errors
13-
libc_setenv(&keydata[0], &valdata[0], 1)
12+
setenv(&keydata[0], &valdata[0])
1413
if key == "GODEBUG" && godebugUpdate != nil {
1514
// Starting with Go 1.20, we need to call a callback (set by
1615
// internal/godebug) to notify the GODEBUG environment variable has
@@ -25,8 +24,7 @@ func syscallSetenv(key, value string) {
2524
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
2625
func syscallUnsetenv(key string) {
2726
keydata := cstring(key)
28-
// ignore any errors
29-
libc_unsetenv(&keydata[0])
27+
unsetenv(&keydata[0])
3028
}
3129

3230
// Compatibility with Go 1.19 and below.
@@ -51,13 +49,3 @@ func cstring(s string) []byte {
5149
// final byte should be zero from the initial allocation
5250
return data
5351
}
54-
55-
// int setenv(const char *name, const char *val, int replace);
56-
//
57-
//export setenv
58-
func libc_setenv(name *byte, val *byte, replace int32) int32
59-
60-
// int unsetenv(const char *name);
61-
//
62-
//export unsetenv
63-
func libc_unsetenv(name *byte) int32

src/runtime/os_windows.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ const GOOS = "windows"
77
//export GetModuleHandleExA
88
func _GetModuleHandleExA(dwFlags uint32, lpModuleName unsafe.Pointer, phModule **exeHeader) bool
99

10+
// Set environment variable in Windows:
11+
//
12+
// BOOL SetEnvironmentVariableA(
13+
// [in] LPCSTR lpName,
14+
// [in, optional] LPCSTR lpValue
15+
// );
16+
//
17+
//export SetEnvironmentVariableA
18+
func _SetEnvironmentVariableA(key, val *byte) bool
19+
1020
// MS-DOS stub with PE header offset:
1121
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#ms-dos-stub-image-only
1222
type exeHeader struct {
@@ -113,3 +123,13 @@ func syscall_Getpagesize() int {
113123
_GetSystemInfo(unsafe.Pointer(&info))
114124
return int(info.dwpagesize)
115125
}
126+
127+
func setenv(key, val *byte) {
128+
// ignore any errors
129+
_SetEnvironmentVariableA(key, val)
130+
}
131+
132+
func unsetenv(key *byte) {
133+
// ignore any errors
134+
_SetEnvironmentVariableA(key, nil)
135+
}

src/runtime/runtime_unix.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ func syscall_runtime_envs() []string {
150150
return envs
151151
}
152152

153+
// int setenv(const char *name, const char *val, int replace);
154+
//
155+
//export setenv
156+
func libc_setenv(name *byte, val *byte, replace int32) int32
157+
158+
// int unsetenv(const char *name);
159+
//
160+
//export unsetenv
161+
func libc_unsetenv(name *byte) int32
162+
163+
func setenv(key, val *byte) {
164+
// ignore any errors
165+
libc_setenv(key, val, 1)
166+
}
167+
168+
func unsetenv(key *byte) {
169+
// ignore any errors
170+
libc_unsetenv(key)
171+
}
172+
153173
func putchar(c byte) {
154174
buf := [1]byte{c}
155175
libc_write(1, unsafe.Pointer(&buf[0]), 1)

0 commit comments

Comments
 (0)