diff options
| author | Rob Pike <r@golang.org> | 2008-06-26 14:09:26 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2008-06-26 14:09:26 -0700 |
| commit | 3e4e83ab1ee4b860a0ed08ccbe81c059982e89e1 (patch) | |
| tree | e446a18d52b4311b7fabada1de0e78b8e72fd773 /src/runtime | |
| parent | 7e21ec300722023af9afb1a243e5bb6a04407dca (diff) | |
| download | go-3e4e83ab1ee4b860a0ed08ccbe81c059982e89e1.tar.xz | |
add sys.readfile()
add args to linux runtime
SVN=124961
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/Makefile | 3 | ||||
| -rw-r--r-- | src/runtime/rt0_amd64_darwin.s | 32 | ||||
| -rw-r--r-- | src/runtime/rt0_amd64_linux.s | 39 | ||||
| -rw-r--r-- | src/runtime/runtime.h | 5 | ||||
| -rw-r--r-- | src/runtime/sys_file.c | 80 |
5 files changed, 153 insertions, 6 deletions
diff --git a/src/runtime/Makefile b/src/runtime/Makefile index 11e39cc6ce..cc1e8c2ea4 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -16,9 +16,10 @@ LIBOFILES=\ rt1_$(GOARCH)_$(GOOS).$O\ rt2_$(GOARCH).$O\ runtime.$O\ + sys_file.$O\ OFILES=$(RT0OFILES) $(LIBOFILES) -HFILES= +HFILES=runtime.h install: rt0 $(LIB) cp $(RT0OFILES) $(GOROOT)/lib diff --git a/src/runtime/rt0_amd64_darwin.s b/src/runtime/rt0_amd64_darwin.s index 031cc059df..ad2e70843b 100644 --- a/src/runtime/rt0_amd64_darwin.s +++ b/src/runtime/rt0_amd64_darwin.s @@ -7,8 +7,8 @@ TEXT _rt0_amd64_darwin(SB),1,$-8 PUSHQ $0 MOVQ SP, BP ANDQ $~15, SP - MOVQ 8(BP), DI - LEAQ 16(BP), SI + MOVQ 8(BP), DI // argc + LEAQ 16(BP), SI // argv MOVL DI, DX ADDL $1, DX SHLL $3, DX @@ -57,6 +57,34 @@ TEXT sys·write(SB),1,$-8 CALL notok(SB) RET +TEXT open(SB),1,$-8 + MOVQ 8(SP), DI + MOVL 16(SP), SI + MOVL $5, AX // syscall entry + SYSCALL + RET + +TEXT close(SB),1,$-8 + MOVL 8(SP), DI + MOVL $6, AX // syscall entry + SYSCALL + RET + +TEXT fstat(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL $189, AX // syscall entry + SYSCALL + RET + +TEXT read(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL 24(SP), DX + MOVL $3, AX // syscall entry + SYSCALL + RET + TEXT sys·sigaction(SB),1,$-8 MOVL 8(SP), DI // arg 1 sig MOVQ 16(SP), SI // arg 2 act diff --git a/src/runtime/rt0_amd64_linux.s b/src/runtime/rt0_amd64_linux.s index 30f72728ad..d89dc927ef 100644 --- a/src/runtime/rt0_amd64_linux.s +++ b/src/runtime/rt0_amd64_linux.s @@ -7,8 +7,8 @@ TEXT _rt0_amd64_linux(SB),1,$-8 PUSHQ $0 MOVQ SP, BP ANDQ $~15, SP - MOVQ 8(BP), DI - LEAQ 16(BP), SI + MOVQ 8(BP), DI // argc + LEAQ 16(BP), SI // argv MOVL DI, DX ADDL $1, DX SHLL $3, DX @@ -24,6 +24,11 @@ loop: done: ADDQ $8, CX + SUBQ $16, SP + MOVL DI, 0(SP) + MOVQ SI, 8(SP) + CALL args(SB) + ADDQ $16, SP CALL check(SB) CALL main·main(SB) CALL sys·exit(SB) @@ -52,6 +57,34 @@ TEXT sys·write(SB),1,$-8 CALL notok(SB) RET +TEXT open(SB),1,$-8 + MOVQ 8(SP), DI + MOVL 16(SP), SI + MOVL $2, AX // syscall entry + SYSCALL + RET + +TEXT close(SB),1,$-8 + MOVL 8(SP), DI + MOVL $3, AX // syscall entry + SYSCALL + RET + +TEXT fstat(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL $5, AX // syscall entry + SYSCALL + RET + +TEXT read(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL 24(SP), DX + MOVL $0, AX // syscall entry + SYSCALL + RET + TEXT sys·rt_sigaction(SB),1,$-8 MOVL 8(SP), DI MOVQ 16(SP), SI @@ -94,7 +127,7 @@ TEXT sys·mmap(SB),1,$-8 MOVL $9, AX // syscall entry SYSCALL CMPQ AX, $0xfffffffffffff001 - JNE 2(PC) + JLS 2(PC) CALL notok(SB) RET diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 4d185a5dc7..c645992d8f 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -90,6 +90,10 @@ void* mal(uint32); uint32 cmpstring(string, string); void initsig(void); void traceback(uint8 *pc, uint8 *sp); +int32 open(byte*, int32); +int32 read(int32, void*, int32); +void close(int32); +int32 fstat(int32, void*); struct SigTab { int32 catch; @@ -124,3 +128,4 @@ void sys·intstring(int64, string); void sys·ifaces2i(Sigi*, Sigs*, Map*, void*); void sys·ifacei2i(Sigi*, Map*, void*); void sys·ifacei2s(Sigs*, Map*, void*); +void sys·readfile(string, string, bool); diff --git a/src/runtime/sys_file.c b/src/runtime/sys_file.c new file mode 100644 index 0000000000..80dec3da80 --- /dev/null +++ b/src/runtime/sys_file.c @@ -0,0 +1,80 @@ +// Copyright 2009 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" + +typedef uint64 dev_t; +typedef uint64 ino_t; +typedef uint32 mode_t; +typedef uint64 nlink_t; +typedef uint32 uid_t; +typedef uint32 gid_t; +typedef int64 off_t; +typedef int64 blksize_t; +typedef int64 blkcnt_t; +typedef int64 time_t; + +struct timespec { + time_t tv_sec; + int64 tv_nsec; +}; + +struct stat { + dev_t st_dev; /* ID of device containing file */ + ino_t st_ino; /* inode number */ + nlink_t st_nlink; /* number of hard links */ + mode_t st_mode; /* protection */ + uid_t st_uid; /* user ID of owner */ + gid_t st_gid; /* group ID of owner */ + int32 pad0; + dev_t st_rdev; /* device ID (if special file) */ + off_t st_size; /* total size, in bytes */ + blksize_t st_blksize; /* blocksize for filesystem I/O */ + blkcnt_t st_blocks; /* number of blocks allocated */ + struct timespec st_atime; /* time of last access */ + struct timespec st_mtime; /* time of last modification */ + struct timespec st_ctime; /* time of last status change */ +}; + +void +sys·readfile(string filein, string fileout, bool okout) +{ + int32 fd; + byte namebuf[256]; + struct stat statbuf; + + fileout = nil; + okout = false; + + if(filein == nil || filein->len >= sizeof(namebuf)) + goto out; + + mcpy(namebuf, filein->str, filein->len); + namebuf[filein->len] = '\0'; + fd = open(namebuf, 0); + if(fd < 0) + goto out; + + if (fstat(fd, &statbuf) < 0) + goto close_out; + + if (statbuf.st_size <= 0) + goto close_out; + + fileout = mal(sizeof(fileout->len)+statbuf.st_size + 1); + fileout->len = statbuf.st_size; + + if (read(fd, fileout->str, statbuf.st_size) != statbuf.st_size) { + fileout = nil; + goto close_out; + } + okout = 1; + +close_out: + close(fd); +out: + FLUSH(&fileout); + FLUSH(&okout); + return; +} |
