Skip to content

Commit 1996fad

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 7fe996e commit 1996fad

File tree

3 files changed

+84
-50
lines changed

3 files changed

+84
-50
lines changed

src/runtime/env.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//go:build linux || darwin || windows
2+
3+
package runtime
4+
5+
// Update the C environment if cgo is loaded.
6+
// Called from Go 1.20 and above.
7+
//
8+
//go:linkname syscallSetenv syscall.runtimeSetenv
9+
func syscallSetenv(key, value string) {
10+
keydata := cstring(key)
11+
valdata := cstring(value)
12+
setenv(&keydata[0], &valdata[0])
13+
if key == "GODEBUG" && godebugUpdate != nil {
14+
// Starting with Go 1.20, we need to call a callback (set by
15+
// internal/godebug) to notify the GODEBUG environment variable has
16+
// changed. This is necessary to get archive/zip to pass tests.
17+
godebugUpdate(key, value)
18+
}
19+
}
20+
21+
// Update the C environment if cgo is loaded.
22+
// Called from Go 1.20 and above.
23+
//
24+
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
25+
func syscallUnsetenv(key string) {
26+
keydata := cstring(key)
27+
unsetenv(&keydata[0])
28+
}
29+
30+
// Compatibility with Go 1.19 and below.
31+
//
32+
//go:linkname syscall_setenv_c syscall.setenv_c
33+
func syscall_setenv_c(key string, val string) {
34+
syscallSetenv(key, val)
35+
}
36+
37+
// Compatibility with Go 1.19 and below.
38+
//
39+
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
40+
func syscall_unsetenv_c(key string) {
41+
syscallUnsetenv(key)
42+
}
43+
44+
// cstring converts a Go string to a C string.
45+
// borrowed from syscall
46+
func cstring(s string) []byte {
47+
data := make([]byte, len(s)+1)
48+
copy(data, s)
49+
// final byte should be zero from the initial allocation
50+
return data
51+
}

src/runtime/env_unix.go

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,6 @@
22

33
package runtime
44

5-
// Update the C environment if cgo is loaded.
6-
// Called from Go 1.20 and above.
7-
//
8-
//go:linkname syscallSetenv syscall.runtimeSetenv
9-
func syscallSetenv(key, value string) {
10-
keydata := cstring(key)
11-
valdata := cstring(value)
12-
// ignore any errors
13-
libc_setenv(&keydata[0], &valdata[0], 1)
14-
if key == "GODEBUG" && godebugUpdate != nil {
15-
// Starting with Go 1.20, we need to call a callback (set by
16-
// internal/godebug) to notify the GODEBUG environment variable has
17-
// changed. This is necessary to get archive/zip to pass tests.
18-
godebugUpdate(key, value)
19-
}
20-
}
21-
22-
// Update the C environment if cgo is loaded.
23-
// Called from Go 1.20 and above.
24-
//
25-
//go:linkname syscallUnsetenv syscall.runtimeUnsetenv
26-
func syscallUnsetenv(key string) {
27-
keydata := cstring(key)
28-
// ignore any errors
29-
libc_unsetenv(&keydata[0])
30-
}
31-
32-
// Compatibility with Go 1.19 and below.
33-
//
34-
//go:linkname syscall_setenv_c syscall.setenv_c
35-
func syscall_setenv_c(key string, val string) {
36-
syscallSetenv(key, val)
37-
}
38-
39-
// Compatibility with Go 1.19 and below.
40-
//
41-
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
42-
func syscall_unsetenv_c(key string) {
43-
syscallUnsetenv(key)
44-
}
45-
46-
// cstring converts a Go string to a C string.
47-
// borrowed from syscall
48-
func cstring(s string) []byte {
49-
data := make([]byte, len(s)+1)
50-
copy(data, s)
51-
// final byte should be zero from the initial allocation
52-
return data
53-
}
54-
555
// int setenv(const char *name, const char *val, int replace);
566
//
577
//export setenv
@@ -61,3 +11,13 @@ func libc_setenv(name *byte, val *byte, replace int32) int32
6111
//
6212
//export unsetenv
6313
func libc_unsetenv(name *byte) int32
14+
15+
func setenv(key, val *byte) {
16+
// ignore any errors
17+
libc_setenv(key, val, 1)
18+
}
19+
20+
func unsetenv(key *byte) {
21+
// ignore any errors
22+
libc_unsetenv(key)
23+
}

src/runtime/env_windows.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build windows
2+
3+
package runtime
4+
5+
// Set environment variable in Windows:
6+
//
7+
// BOOL SetEnvironmentVariableA(
8+
// [in] LPCSTR lpName,
9+
// [in, optional] LPCSTR lpValue
10+
// );
11+
//
12+
//export SetEnvironmentVariableA
13+
func _SetEnvironmentVariableA(key, val *byte) bool
14+
15+
func setenv(key, val *byte) {
16+
// ignore any errors
17+
_SetEnvironmentVariableA(key, val)
18+
}
19+
20+
func unsetenv(key *byte) {
21+
// ignore any errors
22+
_SetEnvironmentVariableA(key, nil)
23+
}

0 commit comments

Comments
 (0)