add sys.readfile()
add args to linux runtime

SVN=124961
diff --git a/src/runtime/Makefile b/src/runtime/Makefile
index 11e39cc..cc1e8c2 100644
--- a/src/runtime/Makefile
+++ b/src/runtime/Makefile
@@ -16,9 +16,10 @@
 	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 031cc05..ad2e708 100644
--- a/src/runtime/rt0_amd64_darwin.s
+++ b/src/runtime/rt0_amd64_darwin.s
@@ -7,8 +7,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 @@
 	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 30f7272..d89dc92 100644
--- a/src/runtime/rt0_amd64_linux.s
+++ b/src/runtime/rt0_amd64_linux.s
@@ -7,8 +7,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 @@
 
 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 @@
 	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 @@
 	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 4d185a5..c645992 100644
--- a/src/runtime/runtime.h
+++ b/src/runtime/runtime.h
@@ -90,6 +90,10 @@
 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·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 0000000..80dec3d
--- /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;
+}