2017-04-28 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / libgo / runtime / env_posix.c
blob3a60682597219f150bc168713f01f0b9f7d9c0b3
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 runtime_get_envs(void);
14 String
15 runtime_getenv(const char *s)
17 int32 i, j;
18 intgo len;
19 const byte *v, *bs;
20 Slice envs;
21 String* envv;
22 int32 envc;
23 String ret;
25 bs = (const byte*)s;
26 len = runtime_findnull(bs);
27 envs = runtime_get_envs();
28 envv = (String*)envs.__values;
29 envc = envs.__count;
30 for(i=0; i<envc; i++){
31 if(envv[i].len <= len)
32 continue;
33 v = (const byte*)envv[i].str;
34 for(j=0; j<len; j++)
35 if(bs[j] != v[j])
36 goto nomatch;
37 if(v[len] != '=')
38 goto nomatch;
39 ret.str = v+len+1;
40 ret.len = envv[i].len-len-1;
41 return ret;
42 nomatch:;
44 ret.str = nil;
45 ret.len = 0;
46 return ret;