Commit c870ac23 authored by Rob Pike's avatar Rob Pike

add sys.writefile; support for darwin only in this CL

SVN=127153
parent e9a19438
...@@ -53,6 +53,7 @@ func gosched(); ...@@ -53,6 +53,7 @@ func gosched();
func goexit(); func goexit();
func readfile(string) (string, bool); // read file into string; boolean status func readfile(string) (string, bool); // read file into string; boolean status
func writefile(string, string) (bool); // write string into file; boolean status
func bytestorune(*byte, int32, int32) (int32, int32); // convert bytes to runes func bytestorune(*byte, int32, int32) (int32, int32); // convert bytes to runes
func stringtorune(string, int32, int32) (int32, int32); // convert bytes to runes func stringtorune(string, int32, int32) (int32, int32); // convert bytes to runes
...@@ -113,6 +114,7 @@ export ...@@ -113,6 +114,7 @@ export
// files // files
readfile readfile
writefile
// runes and utf-8 // runes and utf-8
bytestorune bytestorune
......
This diff is collapsed.
...@@ -196,8 +196,9 @@ void* mal(uint32); ...@@ -196,8 +196,9 @@ void* mal(uint32);
uint32 cmpstring(string, string); uint32 cmpstring(string, string);
void initsig(void); void initsig(void);
void traceback(uint8 *pc, uint8 *sp, G* gp); void traceback(uint8 *pc, uint8 *sp, G* gp);
int32 open(byte*, int32); int32 open(byte*, int32, ...);
int32 read(int32, void*, int32); int32 read(int32, void*, int32);
int32 write(int32, void*, int32);
void close(int32); void close(int32);
int32 fstat(int32, void*); int32 fstat(int32, void*);
......
...@@ -26,6 +26,7 @@ TEXT sys·write(SB),1,$-8 ...@@ -26,6 +26,7 @@ TEXT sys·write(SB),1,$-8
TEXT open(SB),1,$-8 TEXT open(SB),1,$-8
MOVQ 8(SP), DI MOVQ 8(SP), DI
MOVL 16(SP), SI MOVL 16(SP), SI
MOVL 20(SP), DX
MOVQ $0, R10 MOVQ $0, R10
MOVL $(0x2000000+5), AX // syscall entry MOVL $(0x2000000+5), AX // syscall entry
SYSCALL SYSCALL
...@@ -52,6 +53,14 @@ TEXT read(SB),1,$-8 ...@@ -52,6 +53,14 @@ TEXT read(SB),1,$-8
SYSCALL SYSCALL
RET 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 TEXT sys·sigaction(SB),1,$-8
MOVL 8(SP), DI // arg 1 sig MOVL 8(SP), DI // arg 1 sig
MOVQ 16(SP), SI // arg 2 act MOVQ 16(SP), SI // arg 2 act
......
...@@ -37,12 +37,39 @@ sys·readfile(string filein, string fileout, bool okout) ...@@ -37,12 +37,39 @@ sys·readfile(string filein, string fileout, bool okout)
fileout = nil; fileout = nil;
goto close_out; goto close_out;
} }
okout = 1; okout = true;
close_out: close_out:
close(fd); close(fd);
out: out:
FLUSH(&fileout); FLUSH(&fileout);
FLUSH(&okout); 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);
} }
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