22
33package 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
6313func 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+ }
0 commit comments