2012-12-22 02:15:33 +01:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
|
2012-12-22 02:15:33 +01:00
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "array.h"
|
2014-06-07 00:37:27 +02:00
|
|
|
#include "arch.h"
|
|
|
|
#include "malloc.h"
|
2012-12-22 02:15:33 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
extern Slice envs;
|
2012-12-22 02:15:33 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
String
|
2012-12-22 02:15:33 +01:00
|
|
|
runtime_getenv(const char *s)
|
|
|
|
{
|
2013-11-06 20:49:01 +01:00
|
|
|
int32 i, j;
|
|
|
|
intgo len;
|
2012-12-22 02:15:33 +01:00
|
|
|
const byte *v, *bs;
|
|
|
|
String* envv;
|
|
|
|
int32 envc;
|
2015-10-31 01:59:47 +01:00
|
|
|
String ret;
|
2012-12-22 02:15:33 +01:00
|
|
|
|
|
|
|
bs = (const byte*)s;
|
|
|
|
len = runtime_findnull(bs);
|
2015-01-15 01:27:56 +01:00
|
|
|
envv = (String*)envs.__values;
|
|
|
|
envc = envs.__count;
|
2012-12-22 02:15:33 +01:00
|
|
|
for(i=0; i<envc; i++){
|
|
|
|
if(envv[i].len <= len)
|
|
|
|
continue;
|
|
|
|
v = (const byte*)envv[i].str;
|
|
|
|
for(j=0; j<len; j++)
|
|
|
|
if(bs[j] != v[j])
|
|
|
|
goto nomatch;
|
|
|
|
if(v[len] != '=')
|
|
|
|
goto nomatch;
|
2015-10-31 01:59:47 +01:00
|
|
|
ret.str = v+len+1;
|
|
|
|
ret.len = envv[i].len-len-1;
|
|
|
|
return ret;
|
2012-12-22 02:15:33 +01:00
|
|
|
nomatch:;
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
ret.str = nil;
|
|
|
|
ret.len = 0;
|
|
|
|
return ret;
|
2012-12-22 02:15:33 +01:00
|
|
|
}
|