From c870ac239eca599f2bb3cf76522d4a3591d0cd90 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Mon, 14 Jul 2008 20:54:55 -0700 Subject: add sys.writefile; support for darwin only in this CL SVN=127153 --- src/runtime/runtime.h | 3 ++- src/runtime/sys_amd64_darwin.s | 9 +++++++++ src/runtime/sys_file.c | 31 +++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) (limited to 'src/runtime') diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 0b2e8ed975..dec63eaa76 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -196,8 +196,9 @@ void* mal(uint32); uint32 cmpstring(string, string); void initsig(void); void traceback(uint8 *pc, uint8 *sp, G* gp); -int32 open(byte*, int32); +int32 open(byte*, int32, ...); int32 read(int32, void*, int32); +int32 write(int32, void*, int32); void close(int32); int32 fstat(int32, void*); diff --git a/src/runtime/sys_amd64_darwin.s b/src/runtime/sys_amd64_darwin.s index 56e97690ae..26ceaaf6ea 100644 --- a/src/runtime/sys_amd64_darwin.s +++ b/src/runtime/sys_amd64_darwin.s @@ -26,6 +26,7 @@ TEXT sys·write(SB),1,$-8 TEXT open(SB),1,$-8 MOVQ 8(SP), DI MOVL 16(SP), SI + MOVL 20(SP), DX MOVQ $0, R10 MOVL $(0x2000000+5), AX // syscall entry SYSCALL @@ -52,6 +53,14 @@ TEXT read(SB),1,$-8 SYSCALL RET +TEXT write(SB),1,$-8 + MOVL 8(SP), DI + MOVQ 16(SP), SI + MOVL 24(SP), DX + MOVL $(0x2000000+4), 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/sys_file.c b/src/runtime/sys_file.c index f4d0c98216..70c7fb6521 100644 --- a/src/runtime/sys_file.c +++ b/src/runtime/sys_file.c @@ -37,12 +37,39 @@ sys·readfile(string filein, string fileout, bool okout) fileout = nil; goto close_out; } - okout = 1; + okout = true; close_out: close(fd); out: FLUSH(&fileout); FLUSH(&okout); - return; +} + +void +sys·writefile(string filein, string textin, bool okout) +{ + int32 fd; + byte namebuf[256]; + + okout = false; + + if(filein == nil || filein->len >= sizeof(namebuf)) + goto out; + + mcpy(namebuf, filein->str, filein->len); + namebuf[filein->len] = '\0'; + fd = open(namebuf, 1|0x0200, 0644); // open for write, create if non-existant (sic) + if(fd < 0) + goto out; + + if (write(fd, textin->str, textin->len) != textin->len) { + goto close_out; + } + okout = true; + +close_out: + close(fd); +out: + FLUSH(&okout); } -- cgit v1.3