diff options
| author | Russ Cox <rsc@golang.org> | 2009-01-16 14:58:14 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2009-01-16 14:58:14 -0800 |
| commit | 360962420c484427bbc16e1f8699ee5cccc4f012 (patch) | |
| tree | 321251a8cd180ad7856af54e8e531850d0735fb0 /src/runtime | |
| parent | ec9f2b0cd46300b9da3a82aa3604c98fce017baa (diff) | |
| download | go-360962420c484427bbc16e1f8699ee5cccc4f012.tar.xz | |
casify, cleanup sys
R=r
OCL=22978
CL=22984
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/Makefile | 1 | ||||
| -rw-r--r-- | src/runtime/chan.c | 10 | ||||
| -rw-r--r-- | src/runtime/iface.c | 8 | ||||
| -rw-r--r-- | src/runtime/proc.c | 15 | ||||
| -rw-r--r-- | src/runtime/rt0_amd64.s | 4 | ||||
| -rw-r--r-- | src/runtime/rt1_amd64_darwin.c | 4 | ||||
| -rw-r--r-- | src/runtime/rt1_amd64_linux.c | 6 | ||||
| -rw-r--r-- | src/runtime/rt2_amd64.c | 2 | ||||
| -rw-r--r-- | src/runtime/rune.c | 133 | ||||
| -rw-r--r-- | src/runtime/runtime.c | 101 | ||||
| -rw-r--r-- | src/runtime/runtime.h | 22 | ||||
| -rw-r--r-- | src/runtime/sema.c | 10 | ||||
| -rw-r--r-- | src/runtime/sys_amd64_darwin.s | 2 | ||||
| -rw-r--r-- | src/runtime/sys_amd64_linux.s | 6 | ||||
| -rw-r--r-- | src/runtime/sys_file.c | 75 |
15 files changed, 84 insertions, 315 deletions
diff --git a/src/runtime/Makefile b/src/runtime/Makefile index 409925328b..f62229fd2c 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -36,7 +36,6 @@ LIBOFILES=\ sema.$O\ string.$O\ symtab.$O\ - sys_file.$O\ OFILES=$(RT0OFILES) $(LIBOFILES) OS_H=$(GOARCH)_$(GOOS).h diff --git a/src/runtime/chan.c b/src/runtime/chan.c index 8296cdc023..f6e245320f 100644 --- a/src/runtime/chan.c +++ b/src/runtime/chan.c @@ -195,7 +195,7 @@ sendchan(Hchan *c, byte *ep, bool *pres) g->status = Gwaiting; enqueue(&c->sendq, sg); unlock(&chanlock); - sys·gosched(); + sys·Gosched(); lock(&chanlock); sg = g->param; @@ -217,7 +217,7 @@ asynch: g->status = Gwaiting; enqueue(&c->sendq, sg); unlock(&chanlock); - sys·gosched(); + sys·Gosched(); lock(&chanlock); } @@ -281,7 +281,7 @@ chanrecv(Hchan* c, byte *ep, bool* pres) g->status = Gwaiting; enqueue(&c->recvq, sg); unlock(&chanlock); - sys·gosched(); + sys·Gosched(); lock(&chanlock); sg = g->param; @@ -303,7 +303,7 @@ asynch: g->status = Gwaiting; enqueue(&c->recvq, sg); unlock(&chanlock); - sys·gosched(); + sys·Gosched(); lock(&chanlock); } c->elemalg->copy(c->elemsize, ep, c->recvdataq->elem); @@ -690,7 +690,7 @@ sys·selectgo(Select *sel) g->status = Gwaiting; unlock(&chanlock); - sys·gosched(); + sys·Gosched(); lock(&chanlock); sg = g->param; diff --git a/src/runtime/iface.c b/src/runtime/iface.c index 6dfba09283..832f157263 100644 --- a/src/runtime/iface.c +++ b/src/runtime/iface.c @@ -457,7 +457,7 @@ sys·printinter(Iface i) } void -sys·reflect(Iface i, uint64 retit, string rettype, bool retindir) +sys·Reflect(Iface i, uint64 retit, string rettype, bool retindir) { int32 wid; @@ -492,14 +492,14 @@ extern int32 ngotypesigs; // print("first field is string"); // } // -// vv.Interface() returns the result of sys.unreflect with +// vv.Interface() returns the result of sys.Unreflect with // a typestring of "[]int". If []int is not used with interfaces // in the rest of the program, there will be no signature in gotypesigs // for "[]int", so we have to invent one. The only requirements // on the fake signature are: // // (1) any interface conversion using the signature will fail -// (2) calling sys.reflect() returns the args to unreflect +// (2) calling sys.Reflect() returns the args to unreflect // // (1) is ensured by the fact that we allocate a new Sigt, // so it will necessarily be != any Sigt in gotypesigs. @@ -561,7 +561,7 @@ findtype(string type, bool indir) void -sys·unreflect(uint64 it, string type, bool indir, Iface ret) +sys·Unreflect(uint64 it, string type, bool indir, Iface ret) { Sigt *sigt; diff --git a/src/runtime/proc.c b/src/runtime/proc.c index 01581569f7..943792f494 100644 --- a/src/runtime/proc.c +++ b/src/runtime/proc.c @@ -97,7 +97,8 @@ schedinit(void) byte *p; mallocinit(); - + goargs(); + // Allocate internal symbol table representation now, // so that we don't need to call malloc when we crash. findfunc(0); @@ -127,7 +128,7 @@ initdone(void) } void -sys·goexit(void) +sys·Goexit(void) { if(debug > 1){ lock(&debuglock); @@ -135,7 +136,7 @@ sys·goexit(void) unlock(&debuglock); } g->status = Gmoribund; - sys·gosched(); + sys·Gosched(); } G* @@ -186,7 +187,7 @@ sys·newproc(int32 siz, byte* fn, byte* arg0) mcpy(sp, (byte*)&arg0, siz); sp -= 8; - *(byte**)sp = (byte*)sys·goexit; + *(byte**)sp = (byte*)sys·Goexit; sp -= 8; // retpc used by gogo newg->sched.SP = sp; @@ -493,7 +494,7 @@ scheduler(void) case Gmoribund: gp->status = Gdead; if(--sched.gcount == 0) - sys·exit(0); + sys_Exit(0); break; } if(gp->readyonstop){ @@ -522,7 +523,7 @@ scheduler(void) // before running g again. If g->status is Gmoribund, // kills off g. void -sys·gosched(void) +sys·Gosched(void) { if(gosave(&g->sched) == 0){ g = m->g0; @@ -585,7 +586,7 @@ sys·exitsyscall(void) // The scheduler will ready g and put this m to sleep. // When the scheduler takes g awa from m, // it will undo the sched.mcpu++ above. - sys·gosched(); + sys·Gosched(); } diff --git a/src/runtime/rt0_amd64.s b/src/runtime/rt0_amd64.s index 61f9255a51..f1fbcebbf7 100644 --- a/src/runtime/rt0_amd64.s +++ b/src/runtime/rt0_amd64.s @@ -55,12 +55,12 @@ TEXT mainstart(SB),7,$0 CALL initdone(SB) CALL main·main(SB) PUSHQ $0 - CALL sys·exit(SB) + CALL sys·Exit(SB) POPQ AX CALL notok(SB) RET -TEXT sys·breakpoint(SB),7,$0 +TEXT sys·Breakpoint(SB),7,$0 BYTE $0xcc RET diff --git a/src/runtime/rt1_amd64_darwin.c b/src/runtime/rt1_amd64_darwin.c index 7bd7c78fdc..f1ef946646 100644 --- a/src/runtime/rt1_amd64_darwin.c +++ b/src/runtime/rt1_amd64_darwin.c @@ -129,7 +129,7 @@ void sighandler(int32 sig, struct siginfo *info, void *context) { if(panicking) // traceback already printed - sys·exit(2); + sys_Exit(2); _STRUCT_MCONTEXT64 *uc_mcontext = get_uc_mcontext(context); _STRUCT_X86_THREAD_STATE64 *ss = get___ss(uc_mcontext); @@ -151,7 +151,7 @@ sighandler(int32 sig, struct siginfo *info, void *context) print_thread_state(ss); } - sys·exit(2); + sys_Exit(2); } void diff --git a/src/runtime/rt1_amd64_linux.c b/src/runtime/rt1_amd64_linux.c index 30a0410667..74032e4c14 100644 --- a/src/runtime/rt1_amd64_linux.c +++ b/src/runtime/rt1_amd64_linux.c @@ -135,7 +135,7 @@ void sighandler(int32 sig, struct siginfo* info, void** context) { if(panicking) // traceback already printed - sys·exit(2); + sys_Exit(2); struct sigcontext *sc = &(((struct ucontext *)context)->uc_mcontext); @@ -156,8 +156,8 @@ sighandler(int32 sig, struct siginfo* info, void** context) print_sigcontext(sc); } - sys·breakpoint(); - sys·exit(2); + sys·Breakpoint(); + sys_Exit(2); } struct stack_t { diff --git a/src/runtime/rt2_amd64.c b/src/runtime/rt2_amd64.c index 62c74bf51b..762eaad581 100644 --- a/src/runtime/rt2_amd64.c +++ b/src/runtime/rt2_amd64.c @@ -72,7 +72,7 @@ traceback(byte *pc0, byte *sp, G *g) // func caller(n int) (pc uint64, file string, line int, ok bool) void -sys·caller(int32 n, uint64 retpc, string retfile, int32 retline, bool retbool) +sys·Caller(int32 n, uint64 retpc, string retfile, int32 retline, bool retbool) { uint64 pc; byte *sp; diff --git a/src/runtime/rune.c b/src/runtime/rune.c index 5738ca3646..9e731ad219 100644 --- a/src/runtime/rune.c +++ b/src/runtime/rune.c @@ -52,120 +52,6 @@ enum Runemax = 0x10FFFF, /* maximum rune value */ }; -/* - * Modified by Wei-Hwa Huang, Google Inc., on 2004-09-24 - * This is a slower but "safe" version of the old chartorune - * that works on strings that are not necessarily null-terminated. - * - * If you know for sure that your string is null-terminated, - * chartorune will be a bit faster. - * - * It is guaranteed not to attempt to access "length" - * past the incoming pointer. This is to avoid - * possible access violations. If the string appears to be - * well-formed but incomplete (i.e., to get the whole Rune - * we'd need to read past str+length) then we'll set the Rune - * to Bad and return 0. - * - * Note that if we have decoding problems for other - * reasons, we return 1 instead of 0. - */ -int32 -charntorune(int32 *rune, byte *str, int32 length) -{ - int32 c, c1, c2, c3; - int32 l; - - /* When we're not allowed to read anything */ - if(length <= 0) { - goto badlen; - } - - /* - * one character sequence (7-bit value) - * 00000-0007F => T1 - */ - c = *(byte*)str; /* cast not necessary, but kept for safety */ - if(c < Tx) { - *rune = c; - return 1; - } - - // If we can't read more than one character we must stop - if(length <= 1) { - goto badlen; - } - - /* - * two character sequence (11-bit value) - * 0080-07FF => T2 Tx - */ - c1 = *(byte*)(str+1) ^ Tx; - if(c1 & Testx) - goto bad; - if(c < T3) { - if(c < T2) - goto bad; - l = ((c << Bitx) | c1) & Rune2; - if(l <= Rune1) - goto bad; - *rune = l; - return 2; - } - - // If we can't read more than two characters we must stop - if(length <= 2) { - goto badlen; - } - - /* - * three character sequence (16-bit value) - * 0800-FFFF => T3 Tx Tx - */ - c2 = *(byte*)(str+2) ^ Tx; - if(c2 & Testx) - goto bad; - if(c < T4) { - l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3; - if(l <= Rune2) - goto bad; - *rune = l; - return 3; - } - - if (length <= 3) - goto badlen; - - /* - * four character sequence (21-bit value) - * 10000-1FFFFF => T4 Tx Tx Tx - */ - c3 = *(byte*)(str+3) ^ Tx; - if (c3 & Testx) - goto bad; - if (c < T5) { - l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4; - if (l <= Rune3) - goto bad; - *rune = l; - return 4; - } - - // Support for 5-byte or longer UTF-8 would go here, but - // since we don't have that, we'll just fall through to bad. - - /* - * bad decoding - */ -bad: - *rune = Bad; - return 1; -badlen: - *rune = Bad; - return 0; - -} - int32 runetochar(byte *str, int32 rune) /* note: in original, arg2 was pointer */ { @@ -222,22 +108,3 @@ runetochar(byte *str, int32 rune) /* note: in original, arg2 was pointer */ str[3] = Tx | (c & Maskx); return 4; } - -/* - * Wrappers for calling from go - */ -void -sys·bytestorune(byte *str, int32 off, int32 length, int32 outrune, int32 outcount) -{ - outcount = charntorune(&outrune, str + off, length); - FLUSH(&outrune); - FLUSH(&outcount); -} - -void -sys·stringtorune(string str, int32 off, int32 outrune, int32 outcount) -{ - outcount = charntorune(&outrune, str->str + off, str->len - off); - FLUSH(&outrune); - FLUSH(&outcount); -} diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 708abd4cab..31bd1ed868 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -32,8 +32,8 @@ sys·panicl(int32 lno) tracebackothers(g); } panicking = 1; - sys·breakpoint(); // so we can grab it in a debugger - sys·exit(2); + sys·Breakpoint(); // so we can grab it in a debugger + sys_Exit(2); } void @@ -55,7 +55,7 @@ throw(int8 *s) prints(s); prints("\n"); *(int32*)0 = 0; - sys·exit(1); + sys_Exit(1); } void @@ -280,9 +280,9 @@ modf(float64 d, float64 *ip) return d - dd; } -// func frexp(float64) (float64, int32); // break fp into exp,frac +// func Frexp(float64) (float64, int32); // break fp into exp,frac void -sys·frexp(float64 din, float64 dou, int32 iou) +sys·Frexp(float64 din, float64 dou, int32 iou) { dou = frexp(din, &iou); FLUSH(&dou); @@ -290,7 +290,7 @@ sys·frexp(float64 din, float64 dou, int32 iou) //func ldexp(int32, float64) float64; // make fp from exp,frac void -sys·ldexp(float64 din, int32 ein, float64 dou) +sys·Ldexp(float64 din, int32 ein, float64 dou) { dou = ldexp(din, ein); FLUSH(&dou); @@ -298,7 +298,7 @@ sys·ldexp(float64 din, int32 ein, float64 dou) //func modf(float64) (float64, float64); // break fp into double+double void -sys·modf(float64 din, float64 integer, float64 fraction) +sys·Modf(float64 din, float64 integer, float64 fraction) { fraction = modf(din, &integer); FLUSH(&fraction); @@ -306,7 +306,7 @@ sys·modf(float64 din, float64 integer, float64 fraction) //func isinf(float64, int32 sign) bool; // test for infinity void -sys·isInf(float64 din, int32 signin, bool out) +sys·IsInf(float64 din, int32 signin, bool out) { out = isInf(din, signin); FLUSH(&out); @@ -314,7 +314,7 @@ sys·isInf(float64 din, int32 signin, bool out) //func isnan(float64) bool; // test for NaN void -sys·isNaN(float64 din, bool out) +sys·IsNaN(float64 din, bool out) { out = isNaN(din); FLUSH(&out); @@ -338,7 +338,7 @@ sys·NaN(float64 out) // func float32bits(float32) uint32; // raw bits of float32 void -sys·float32bits(float32 din, uint32 iou) +sys·Float32bits(float32 din, uint32 iou) { iou = float32tobits(din); FLUSH(&iou); @@ -346,7 +346,7 @@ sys·float32bits(float32 din, uint32 iou) // func float64bits(float64) uint64; // raw bits of float64 void -sys·float64bits(float64 din, uint64 iou) +sys·Float64bits(float64 din, uint64 iou) { iou = float64tobits(din); FLUSH(&iou); @@ -354,7 +354,7 @@ sys·float64bits(float64 din, uint64 iou) // func float32frombits(uint32) float32; // raw bits to float32 void -sys·float32frombits(uint32 uin, float32 dou) +sys·Float32frombits(uint32 uin, float32 dou) { dou = float32frombits(uin); FLUSH(&dou); @@ -362,7 +362,7 @@ sys·float32frombits(uint32 uin, float32 dou) // func float64frombits(uint64) float64; // raw bits to float64 void -sys·float64frombits(uint64 uin, float64 dou) +sys·Float64frombits(uint64 uin, float64 dou) { dou = float64frombits(uin); FLUSH(&dou); @@ -370,23 +370,37 @@ sys·float64frombits(uint64 uin, float64 dou) static int32 argc; static uint8** argv; -static int32 envc; -static uint8** envv; + +Array sys·Args; +Array sys·Envs; void args(int32 c, uint8 **v) { argc = c; argv = v; - envv = v + argc + 1; // skip 0 at end of argv - for (envc = 0; envv[envc] != 0; envc++) - ; } -int32 -getenvc(void) +void +goargs(void) { - return envc; + string* goargv; + string* envv; + int32 i, envc; + + goargv = (string*)argv; + for (i=0; i<argc; i++) + goargv[i] = gostring(argv[i]); + sys·Args.array = (byte*)argv; + sys·Args.nel = argc; + sys·Args.cap = argc; + + envv = goargv + argc + 1; // skip 0 at end of argv + for (envc = 0; envv[envc] != 0; envc++) + envv[envc] = gostring((uint8*)envv[envc]); + sys·Envs.array = (byte*)envv; + sys·Envs.nel = envc; + sys·Envs.cap = envc; } byte* @@ -394,11 +408,15 @@ getenv(int8 *s) { int32 i, j, len; byte *v, *bs; + string* envv; + int32 envc; bs = (byte*)s; len = findnull(bs); + envv = (string*)sys·Envs.array; + envc = sys·Envs.nel; for(i=0; i<envc; i++){ - v = envv[i]; + v = envv[i]->str; for(j=0; j<len; j++) if(bs[j] != v[j]) goto nomatch; @@ -410,6 +428,7 @@ getenv(int8 *s) return nil; } + int32 atoi(byte *p) { @@ -421,44 +440,6 @@ atoi(byte *p) return n; } -//func argc() int32; // return number of arguments -void -sys·argc(int32 v) -{ - v = argc; - FLUSH(&v); -} - -//func envc() int32; // return number of environment variables -void -sys·envc(int32 v) -{ - v = envc; - FLUSH(&v); -} - -//func argv(i) string; // return argument i -void -sys·argv(int32 i, string s) -{ - if(i >= 0 && i < argc) - s = gostring(argv[i]); - else - s = emptystring; - FLUSH(&s); -} - -//func envv(i) string; // return environment variable i -void -sys·envv(int32 i, string s) -{ - if(i >= 0 && i < envc) - s = gostring(envv[i]); - else - s = emptystring; - FLUSH(&s); -} - void check(void) { diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index becf498228..735f1aa9ce 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -252,7 +252,6 @@ int32 strcmp(byte*, byte*); int32 findnull(byte*); void dump(byte*, int32); int32 runetochar(byte*, int32); -int32 chartorune(uint32*, byte*); /* * very low level c-called @@ -261,6 +260,7 @@ int32 gogo(Gobuf*); int32 gosave(Gobuf*); int32 gogoret(Gobuf*, uint64); void retfromnewstack(void); +void goargs(void); void setspgoto(byte*, void(*)(void), void(*)(void)); void FLUSH(void*); void* getu(void); @@ -348,16 +348,15 @@ void notewakeup(Note*); * UTF-8 characters in identifiers. */ #ifndef __GNUC__ -#define sys_exit sys·exit -#define sys_gosched sys·gosched +#define sys_Exit sys·Exit +#define sys_Gosched sys·Gosched #define sys_memclr sys·memclr #define sys_write sys·write -#define sys_breakpoint sys·breakpoint -#define sys_bytestorune sys·bytestorune +#define sys_Breakpoint sys·Breakpoint #define sys_catstring sys·catstring #define sys_cmpstring sys·cmpstring #define sys_getcallerpc sys·getcallerpc -#define sys_goexit sys·goexit +#define sys_Goexit sys·Goexit #define sys_indexstring sys·indexstring #define sys_intstring sys·intstring #define sys_mal sys·mal @@ -376,17 +375,16 @@ void notewakeup(Note*); #define sys_semrelease sys·semrelease #define sys_setcallerpc sys·setcallerpc #define sys_slicestring sys·slicestring -#define sys_stringtorune sys·stringtorune #endif /* * low level go -called */ -void sys_goexit(void); -void sys_gosched(void); -void sys_exit(int32); +void sys_Goexit(void); +void sys_Gosched(void); +void sys_Exit(int32); void sys_write(int32, void*, int32); -void sys_breakpoint(void); +void sys_Breakpoint(void); uint8* sys_mmap(byte*, uint32, int32, int32, int32, uint32); void sys_memclr(byte*, uint32); void sys_setcallerpc(void*, void*); @@ -416,7 +414,5 @@ bool isNaN(float64); * User go-called */ void sys_readfile(string, string, bool); -void sys_bytestorune(byte*, int32, int32, int32, int32); -void sys_stringtorune(string, int32, int32, int32); void sys_semacquire(uint32*); void sys_semrelease(uint32*); diff --git a/src/runtime/sema.c b/src/runtime/sema.c index 117b0797a7..e4309f079b 100644 --- a/src/runtime/sema.c +++ b/src/runtime/sema.c @@ -119,7 +119,7 @@ semsleep2(Sema *s) { USED(s); g->status = Gwaiting; - sys·gosched(); + sys·Gosched(); } static int32 @@ -133,11 +133,11 @@ cansemacquire(uint32 *addr) return 0; } -// func sys.semacquire(addr *uint32) +// func sync.semacquire(addr *uint32) // For now has no return value. // Might return an ok (not interrupted) bool in the future? void -sys·semacquire(uint32 *addr) +sync·semacquire(uint32 *addr) { Sema s; @@ -163,9 +163,9 @@ sys·semacquire(uint32 *addr) semwakeup(addr); } -// func sys.semrelease(addr *uint32) +// func sync.semrelease(addr *uint32) void -sys·semrelease(uint32 *addr) +sync·semrelease(uint32 *addr) { uint32 v; diff --git a/src/runtime/sys_amd64_darwin.s b/src/runtime/sys_amd64_darwin.s index 92af21c935..e16983346d 100644 --- a/src/runtime/sys_amd64_darwin.s +++ b/src/runtime/sys_amd64_darwin.s @@ -9,7 +9,7 @@ // // Exit the entire program (like C exit) -TEXT sys·exit(SB),7,$-8 +TEXT sys·Exit(SB),7,$-8 MOVL 8(SP), DI // arg 1 exit status MOVL $(0x2000000+1), AX // syscall entry SYSCALL diff --git a/src/runtime/sys_amd64_linux.s b/src/runtime/sys_amd64_linux.s index d385bb43ec..49349bb48b 100644 --- a/src/runtime/sys_amd64_linux.s +++ b/src/runtime/sys_amd64_linux.s @@ -6,7 +6,7 @@ // System calls and other sys.stuff for AMD64, Linux // -TEXT sys·exit(SB),7,$0-8 +TEXT sys·Exit(SB),7,$0-8 MOVL 8(SP), DI MOVL $231, AX // exitgroup - force all os threads to exi SYSCALL @@ -175,12 +175,12 @@ TEXT clone(SB),7,$0 MOVQ SI, SP MOVQ R8, R14 // m MOVQ R9, R15 // g - + // Initialize m->procid to Linux tid MOVL $186, AX // gettid SYSCALL MOVQ AX, 24(R14) - + // Call fn CALL R12 diff --git a/src/runtime/sys_file.c b/src/runtime/sys_file.c deleted file mode 100644 index 1fc1e55e65..0000000000 --- a/src/runtime/sys_file.c +++ /dev/null @@ -1,75 +0,0 @@ -// 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" -#include "sys_types.h" - -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 = true; - -close_out: - close(fd); -out: - FLUSH(&fileout); - FLUSH(&okout); -} - -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|O_CREAT, 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); -} |
