#include #include "u.h" #include "lib.h" #include "dat.h" #include "fns.h" #include typedef struct Oproc Oproc; struct Oproc { int tid; HANDLE *sema; }; static int tlsx = TLS_OUT_OF_INDEXES; char *argv0; Proc* _getproc(void) { if(tlsx == TLS_OUT_OF_INDEXES) return nil; return TlsGetValue(tlsx); } void _setproc(Proc *p) { if(tlsx == TLS_OUT_OF_INDEXES){ tlsx = TlsAlloc(); if(tlsx == TLS_OUT_OF_INDEXES) panic("out of indexes"); } TlsSetValue(tlsx, p); } void oserror(void) { oserrstr(); nexterror(); } void osinit(void) { Oproc *t; static Proc firstprocCTstore; _setproc(&firstprocCTstore); t = (Oproc*)firstprocCTstore.oproc; assert(t != 0); t->tid = GetCurrentThreadId(); t->sema = CreateSemaphore(0, 0, 1000, 0); if(t->sema == 0) { oserror(); panic("could not create semaphore: %r"); } } void osnewproc(Proc *p) { Oproc *op; op = (Oproc*)p->oproc; op->sema = CreateSemaphore(0, 0, 1000, 0); if (op->sema == 0) { oserror(); panic("could not create semaphore: %r"); } } void osmsleep(int ms) { Sleep((DWORD) ms); } void osyield(void) { Sleep(0); } static DWORD WINAPI tramp(LPVOID vp); void osproc(Proc *p) { DWORD tid; if(CreateThread(0, 0, tramp, p, 0, &tid) == 0) { oserror(); panic("osproc: %r"); } Sleep(0); } static DWORD WINAPI tramp(LPVOID vp) { Proc *p = (Proc *) vp; Oproc *op = (Oproc*) p->oproc; _setproc(p); op->tid = GetCurrentThreadId(); op->sema = CreateSemaphore(0, 0, 1000, 0); if(op->sema == 0) { oserror(); panic("could not create semaphore: %r"); } (*p->fn)(p->arg); ExitThread(0); return 0; } void procsleep(void) { Proc *p; Oproc *op; p = up; op = (Oproc*)p->oproc; WaitForSingleObject(op->sema, INFINITE);} void procwakeup(Proc *p) { Oproc *op; op = (Oproc*)p->oproc; ReleaseSemaphore(op->sema, 1, 0); } void random20(uchar *p) { LARGE_INTEGER ti; int i, j; FILETIME ft; DigestState ds; vlong tsc; GetSystemTimeAsFileTime(&ft); memset(&ds, 0, sizeof ds); sha1((uchar*)&ft, sizeof(ft), 0, &ds); for(i=0; i<50; i++) { for(j=0; j<10; j++) { QueryPerformanceCounter(&ti); sha1((uchar*)&ti, sizeof(ti), 0, &ds); tsc = GetTickCount(); sha1((uchar*)&tsc, sizeof(tsc), 0, &ds); } Sleep(10); } sha1(0, 0, p, &ds); } void randominit(void) { } ulong randomread(void *v, ulong n) { int i; uchar p[20]; for(i=0; i N backslashes and end quote * 2N+1 backslashes + " ==> N backslashes + literal " * N backslashes not followed by " ==> N backslashes */ static int args(char *argv[], int n, char *p) { char *p2; int i, j, quote, nbs; for(i=0; *p && i>1); j++) *p2++ = '\\'; if(nbs&1) *p2++ = *p; else quote = !quote; } else { for(j=0; jerrstr, ERRMAX); } long showfilewrite(char *a, int n) { Rune *action, *arg, *cmd; static Rune Lopen[] = { 'o', 'p', 'e', 'n', 0 }; cmd = runesmprint("%.*s", n, a); if(cmd == nil) error("out of memory"); if(cmd[runestrlen(cmd)-1] == '\n') cmd[runestrlen(cmd)] = 0; p = runestrchr(cmd, ' '); if(p){ action = cmd; *p++ = 0; arg = p; }else{ action = Lopen; arg = cmd; } ShellExecute(0, 0, action, arg, 0, SW_SHOWNORMAL); return n; }