github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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 dragonfly 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; 15 intgo len; 16 byte *v, *bs; 17 String* envv; 18 int32 envc; 19 20 bs = (byte*)s; 21 len = runtime·findnull(bs); 22 envv = (String*)syscall·envs.array; 23 envc = syscall·envs.len; 24 for(i=0; i<envc; i++){ 25 if(envv[i].len <= len) 26 continue; 27 v = envv[i].str; 28 for(j=0; j<len; j++) 29 if(bs[j] != v[j]) 30 goto nomatch; 31 if(v[len] != '=') 32 goto nomatch; 33 return v+len+1; 34 nomatch:; 35 } 36 return nil; 37 } 38 39 void (*_cgo_setenv)(byte**); 40 41 // Update the C environment if cgo is loaded. 42 // Called from syscall.Setenv. 43 void 44 syscall·setenv_c(String k, String v) 45 { 46 byte *arg[2]; 47 48 if(_cgo_setenv == nil) 49 return; 50 51 arg[0] = runtime·malloc(k.len + 1); 52 runtime·memmove(arg[0], k.str, k.len); 53 arg[0][k.len] = 0; 54 55 arg[1] = runtime·malloc(v.len + 1); 56 runtime·memmove(arg[1], v.str, v.len); 57 arg[1][v.len] = 0; 58 59 runtime·asmcgocall((void*)_cgo_setenv, arg); 60 runtime·free(arg[0]); 61 runtime·free(arg[1]); 62 }