Commit 432f1822 authored by Anthony Martin's avatar Anthony Martin Committed by Russ Cox

runtime: implement getenv for Plan 9

With this change the runtime can now read GOMAXPROCS, GOGC, etc.

I'm not quite sure how we missed this.

R=seed, lucio.dere, rsc
CC=golang-dev
https://golang.org/cl/6935062
parent 94430937
// 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.
#include "runtime.h"
#include "os_GOOS.h"
byte*
runtime·getenv(int8 *s)
{
int32 fd, len, n, r;
byte file[128];
byte *p;
len = runtime·findnull((byte*)s);
if(len > sizeof file-6)
return nil;
runtime·memclr(file, sizeof file);
runtime·memmove((void*)file, (void*)"/env/", 5);
runtime·memmove((void*)(file+5), (void*)s, len);
fd = runtime·open(file, OREAD);
if(fd < 0)
return nil;
n = runtime·seek(fd, 0, 2);
p = runtime·malloc(n+1);
r = runtime·pread(fd, p, n, 0);
runtime·close(fd);
if(r < 0)
return nil;
return p;
}
// 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.
// +build darwin freebsd linux netbsd openbsd windows
#include "runtime.h"
Slice syscall·envs;
byte*
runtime·getenv(int8 *s)
{
int32 i, j, len;
byte *v, *bs;
String* envv;
int32 envc;
bs = (byte*)s;
len = runtime·findnull(bs);
envv = (String*)syscall·envs.array;
envc = syscall·envs.len;
for(i=0; i<envc; i++){
if(envv[i].len <= len)
continue;
v = envv[i].str;
for(j=0; j<len; j++)
if(bs[j] != v[j])
goto nomatch;
if(v[len] != '=')
goto nomatch;
return v+len+1;
nomatch:;
}
return nil;
}
void (*libcgo_setenv)(byte**);
// Update the C environment if cgo is loaded.
// Called from syscall.Setenv.
void
syscall·setenv_c(String k, String v)
{
byte *arg[2];
if(libcgo_setenv == nil)
return;
arg[0] = runtime·malloc(k.len + 1);
runtime·memmove(arg[0], k.str, k.len);
arg[0][k.len] = 0;
arg[1] = runtime·malloc(v.len + 1);
runtime·memmove(arg[1], v.str, v.len);
arg[1][v.len] = 0;
runtime·asmcgocall((void*)libcgo_setenv, arg);
runtime·free(arg[0]);
runtime·free(arg[1]);
}
...@@ -7,6 +7,7 @@ int32 runtime·open(uint8 *file, int32 mode); ...@@ -7,6 +7,7 @@ int32 runtime·open(uint8 *file, int32 mode);
int32 runtime·pread(int32 fd, void *buf, int32 nbytes, int64 offset); int32 runtime·pread(int32 fd, void *buf, int32 nbytes, int64 offset);
int32 runtime·pwrite(int32 fd, void *buf, int32 nbytes, int64 offset); int32 runtime·pwrite(int32 fd, void *buf, int32 nbytes, int64 offset);
int32 runtime·read(int32 fd, void *buf, int32 nbytes); int32 runtime·read(int32 fd, void *buf, int32 nbytes);
int64 runtime·seek(int32 fd, int64 offset, int32 whence);
int32 runtime·close(int32 fd); int32 runtime·close(int32 fd);
void runtime·exits(int8* msg); void runtime·exits(int8* msg);
intptr runtime·brk_(void*); intptr runtime·brk_(void*);
......
...@@ -110,58 +110,6 @@ runtime·goenvs_unix(void) ...@@ -110,58 +110,6 @@ runtime·goenvs_unix(void)
syscall·envs.cap = n; syscall·envs.cap = n;
} }
byte*
runtime·getenv(int8 *s)
{
int32 i, j, len;
byte *v, *bs;
String* envv;
int32 envc;
bs = (byte*)s;
len = runtime·findnull(bs);
envv = (String*)syscall·envs.array;
envc = syscall·envs.len;
for(i=0; i<envc; i++){
if(envv[i].len <= len)
continue;
v = envv[i].str;
for(j=0; j<len; j++)
if(bs[j] != v[j])
goto nomatch;
if(v[len] != '=')
goto nomatch;
return v+len+1;
nomatch:;
}
return nil;
}
void (*libcgo_setenv)(byte**);
// Update the C environment if cgo is loaded.
// Called from syscall.Setenv.
void
syscall·setenv_c(String k, String v)
{
byte *arg[2];
if(libcgo_setenv == nil)
return;
arg[0] = runtime·malloc(k.len + 1);
runtime·memmove(arg[0], k.str, k.len);
arg[0][k.len] = 0;
arg[1] = runtime·malloc(v.len + 1);
runtime·memmove(arg[1], v.str, v.len);
arg[1][v.len] = 0;
runtime·asmcgocall((void*)libcgo_setenv, arg);
runtime·free(arg[0]);
runtime·free(arg[1]);
}
void void
runtime·getgoroot(String out) runtime·getgoroot(String out)
{ {
......
...@@ -24,9 +24,19 @@ TEXT runtime·pwrite(SB),7,$0 ...@@ -24,9 +24,19 @@ TEXT runtime·pwrite(SB),7,$0
INT $64 INT $64
RET RET
TEXT runtime·seek(SB),7,$0
MOVL $39, AX
INT $64
CMPL AX, $-1
JNE 4(PC)
MOVL a+0(FP), CX
MOVL AX, 0(CX)
MOVL AX, 4(CX)
RET
TEXT runtime·close(SB),7,$0 TEXT runtime·close(SB),7,$0
MOVL $4, AX MOVL $4, AX
INT $64 INT $64
RET RET
TEXT runtime·exits(SB),7,$0 TEXT runtime·exits(SB),7,$0
......
...@@ -27,6 +27,30 @@ TEXT runtime·pwrite(SB),7,$0 ...@@ -27,6 +27,30 @@ TEXT runtime·pwrite(SB),7,$0
SYSCALL SYSCALL
RET RET
// int32 _seek(int64*, int32, int64, int32)
TEXT _seek<>(SB),7,$0
MOVQ $0x8000, AX
MOVQ $39, BP
SYSCALL
RET
// int64 seek(int32, int64, int32)
TEXT runtime·seek(SB),7,$56
LEAQ new+48(SP), CX
MOVQ CX, 0(SP)
MOVQ fd+0(FP), CX
MOVQ CX, 8(SP)
MOVQ off+8(FP), CX
MOVQ CX, 16(SP)
MOVQ whence+16(FP), CX
MOVQ CX, 24(SP)
CALL _seek<>(SB)
CMPL AX, $0
JGE 2(PC)
MOVQ $-1, new+48(SP)
MOVQ new+48(SP), AX
RET
TEXT runtime·close(SB),7,$0 TEXT runtime·close(SB),7,$0
MOVQ $0x8000, AX MOVQ $0x8000, AX
MOVQ $4, BP MOVQ $4, BP
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment