2015-12-22 12:55:44 +01:00
|
|
|
/*
|
2016-11-25 17:18:40 +01:00
|
|
|
* Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it>
|
|
|
|
*
|
2015-12-22 12:55:44 +01:00
|
|
|
* This file is part of the UCB release of Plan 9. It is subject to the license
|
|
|
|
* terms in the LICENSE file found in the top-level directory of this
|
|
|
|
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
|
|
|
* part of the UCB release of Plan 9, including this file, may be copied,
|
|
|
|
* modified, propagated, or distributed except according to the terms contained
|
|
|
|
* in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
|
|
|
|
char*
|
|
|
|
getenv(const char *name)
|
|
|
|
{
|
|
|
|
int r, f;
|
|
|
|
int32_t s;
|
|
|
|
char *ans;
|
|
|
|
char *p, *ep, ename[100];
|
|
|
|
|
|
|
|
if(strchr(name, '/') != nil)
|
|
|
|
return nil;
|
|
|
|
snprint(ename, sizeof ename, "/env/%s", name);
|
|
|
|
if(strcmp(ename+5, name) != 0)
|
|
|
|
return nil;
|
|
|
|
f = open(ename, OREAD);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(f < 0){
|
|
|
|
/* try with #e, in case of a previous rfork(RFCNAMEG)
|
|
|
|
*
|
|
|
|
* NOTE: /env is bound to #ec by default, so we
|
|
|
|
* cannot simply use always #e instead of /env. Also
|
|
|
|
* using #ec when the open in #e fails is both
|
|
|
|
* slow and not flexible enough.
|
|
|
|
*/
|
|
|
|
snprint(ename, sizeof ename, "#e/%s", name);
|
|
|
|
f = open(ename, OREAD);
|
|
|
|
if(f < 0)
|
|
|
|
return nil;
|
|
|
|
}
|
2015-12-22 12:55:44 +01:00
|
|
|
s = seek(f, 0, 2);
|
|
|
|
ans = malloc(s+1);
|
|
|
|
if(ans) {
|
|
|
|
setmalloctag(ans, getcallerpc());
|
|
|
|
seek(f, 0, 0);
|
|
|
|
r = read(f, ans, s);
|
|
|
|
if(r >= 0) {
|
|
|
|
ep = ans + s - 1;
|
|
|
|
for(p = ans; p < ep; p++)
|
|
|
|
if(*p == '\0')
|
|
|
|
*p = ' ';
|
|
|
|
ans[s] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(f);
|
|
|
|
return ans;
|
|
|
|
}
|