2016-07-13 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / libgo / runtime / env_posix.c
blobb93edd65a6bb53695895ce9078c0c56e593fd04a
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.
5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
7 #include "runtime.h"
8 #include "array.h"
9 #include "arch.h"
10 #include "malloc.h"
12 extern Slice envs;
14 String
15 runtime_getenv(const char *s)
17 int32 i, j;
18 intgo len;
19 const byte *v, *bs;
20 String* envv;
21 int32 envc;
22 String ret;
24 bs = (const byte*)s;
25 len = runtime_findnull(bs);
26 envv = (String*)envs.__values;
27 envc = envs.__count;
28 for(i=0; i<envc; i++){
29 if(envv[i].len <= len)
30 continue;
31 v = (const byte*)envv[i].str;
32 for(j=0; j<len; j++)
33 if(bs[j] != v[j])
34 goto nomatch;
35 if(v[len] != '=')
36 goto nomatch;
37 ret.str = v+len+1;
38 ret.len = envv[i].len-len-1;
39 return ret;
40 nomatch:;
42 ret.str = nil;
43 ret.len = 0;
44 return ret;