// This is really an os package test but here for convenience. func testSetEnv(t *testing.T) { const key = "CGO_OS_TEST_KEY" const val = "CGO_OS_TEST_VALUE" os.Setenv(key, val) keyc := C.CString(key) defer C.free(unsafe.Pointer(keyc)) v := C.getenv(keyc) if v == (*C.char)(unsafe.Pointer(uintptr(0))) { t.Fatal("getenv returned NULL") } vs := C.GoString(v) if vs != val { t.Fatalf("getenv() = %q; want %q", vs, val) } }
//export callMain func callMain(mainPC uintptr) { for _, name := range []string{"TMPDIR", "PATH", "LD_LIBRARY_PATH"} { n := C.CString(name) os.Setenv(name, C.GoString(C.getenv(n))) C.free(unsafe.Pointer(n)) } // Set timezone. // // Note that Android zoneinfo is stored in /system/usr/share/zoneinfo, // but it is in some kind of packed TZiff file that we do not support // yet. As a stopgap, we build a fixed zone using the tm_zone name. var curtime C.time_t var curtm C.struct_tm C.time(&curtime) C.localtime_r(&curtime, &curtm) tzOffset := int(curtm.tm_gmtoff) tz := C.GoString(curtm.tm_zone) time.Local = time.FixedZone(tz, tzOffset) go callfn.CallFn(mainPC) }
// This is really an os package test but here for convenience. func testSetEnv(t *testing.T) { if runtime.GOOS == "windows" { // Go uses SetEnvironmentVariable on windows. Howerver, // C runtime takes a *copy* at process startup of thei // OS environment, and stores it in environ/envp. // It is this copy that getenv/putenv manipulate. t.Logf("skipping test") return } const key = "CGO_OS_TEST_KEY" const val = "CGO_OS_TEST_VALUE" os.Setenv(key, val) keyc := C.CString(key) defer C.free(unsafe.Pointer(keyc)) v := C.getenv(keyc) if uintptr(unsafe.Pointer(v)) == 0 { t.Fatal("getenv returned NULL") } vs := C.GoString(v) if vs != val { t.Fatalf("getenv() = %q; want %q", vs, val) } }