github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/env_posix.c (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin freebsd linux netbsd openbsd windows 6 7 #include "runtime.h" 8 9 Slice syscall·envs; 10 11 byte* 12 runtime·getenv(int8 *s) 13 { 14 int32 i, j, len; 15 byte *v, *bs; 16 String* envv; 17 int32 envc; 18 19 bs = (byte*)s; 20 len = runtime·findnull(bs); 21 envv = (String*)syscall·envs.array; 22 envc = syscall·envs.len; 23 for(i=0; i<envc; i++){ 24 if(envv[i].len <= len) 25 continue; 26 v = envv[i].str; 27 for(j=0; j<len; j++) 28 if(bs[j] != v[j]) 29 goto nomatch; 30 if(v[len] != '=') 31 goto nomatch; 32 return v+len+1; 33 nomatch:; 34 } 35 return nil; 36 } 37 38 void (*_cgo_setenv)(byte**); 39 40 // Update the C environment if cgo is loaded. 41 // Called from syscall.Setenv. 42 void 43 syscall·setenv_c(String k, String v) 44 { 45 byte *arg[2]; 46 47 if(_cgo_setenv == nil) 48 return; 49 50 arg[0] = runtime·malloc(k.len + 1); 51 runtime·memmove(arg[0], k.str, k.len); 52 arg[0][k.len] = 0; 53 54 arg[1] = runtime·malloc(v.len + 1); 55 runtime·memmove(arg[1], v.str, v.len); 56 arg[1][v.len] = 0; 57 58 runtime·asmcgocall((void*)_cgo_setenv, arg); 59 runtime·free(arg[0]); 60 runtime·free(arg[1]); 61 }