This commit is contained in:
Russ Cox 2005-11-08 01:35:49 +00:00
parent d6701f9376
commit ee5fc884bd
16 changed files with 210 additions and 80 deletions

View File

@ -5,7 +5,7 @@ AR=ar
AS=as AS=as
RANLIB=ranlib RANLIB=ranlib
CC=gcc CC=gcc
CFLAGS=-Wall -Wno-missing-braces -ggdb -I$(ROOT) -I$(ROOT)/include -I$(ROOT)/kern -c -D_THREAD_SAFE $(PTHREAD) CFLAGS=-Wall -Wno-missing-braces -ggdb -I$(ROOT) -I$(ROOT)/include -I$(ROOT)/kern -c -D_THREAD_SAFE $(PTHREAD) -O2
O=o O=o
OS=posix OS=posix
GUI=x11 GUI=x11

View File

@ -3,20 +3,20 @@
# on another platform. Otherwise the binaries are just # on another platform. Otherwise the binaries are just
# named gcc, etc. # named gcc, etc.
MING=mingw32- MING=i586-mingw32msvc-
#MING= #MING=
AR=$(MING)ar AR=$(MING)ar
CC=$(MING)gcc CC=$(MING)gcc
AS=$(MING)as AS=$(MING)as
RANLIB=$(MING)ranlib RANLIB=$(MING)ranlib
CFLAGS=-Wall -Wno-missing-braces -I$(ROOT)/include -I$(ROOT) -I$(ROOT)/kern -c -D_X86_ -DIS_32 -DWINDOWS CFLAGS=-Wall -Wno-missing-braces -I$(ROOT)/include -I$(ROOT) -I$(ROOT)/kern -c -D_X86_ -DIS_32 -DWINDOWS -O2
O=o O=o
FS=fs-win32 FS=fs-win32
IP=win32 IP=win32
OS=win32 OS=win32
GUI=win32 GUI=win32
LDFLAGS=-mwindows LDFLAGS=-mwindows
LDADD=-lkernel32 -ladvapi32 -lgdi32 -lmpr -lwsock32 LDADD=-lkernel32 -ladvapi32 -lgdi32 -lmpr -lwsock32 -lmsvcrt -lmingw32
TARG=drawterm.exe TARG=drawterm.exe
# Windows via MSVC # Windows via MSVC

3
cpu.c
View File

@ -380,9 +380,8 @@ p9auth(int fd)
return fd; return fd;
/* exchange random numbers */ /* exchange random numbers */
srand(truerand());
for(i = 0; i < 4; i++) for(i = 0; i < 4; i++)
key[i] = rand(); key[i] = fastrand();
if(write(fd, key, 4) != 4) if(write(fd, key, 4) != 4)
return -1; return -1;
if(readn(fd, key+12, 4) != 4) if(readn(fd, key+12, 4) != 4)

View File

@ -7,8 +7,7 @@ OFILES=\
cload.$O\ cload.$O\
draw.$O\ draw.$O\
load.$O\ load.$O\
screen.$O\ screen.$O
wstrtoutf.$O
default: $(LIB) default: $(LIB)
$(LIB): $(OFILES) $(LIB): $(OFILES)

View File

@ -5,6 +5,7 @@
# endif # endif
#elif defined(WINDOWS) #elif defined(WINDOWS)
# include "9windows.h" # include "9windows.h"
# define main mymain
#else #else
# error "Define an OS" # error "Define an OS"
#endif #endif

View File

@ -15,11 +15,6 @@
#define log2 liblog2 #define log2 liblog2
#define log liblog #define log liblog
#define reboot libreboot #define reboot libreboot
#define srand dtsrand
#define rand dtrand
#define nrand dtnrand
#define lrand dtlrand
#define lnrand dtlnrand
#undef timeradd #undef timeradd
#define timeradd xtimeradd #define timeradd xtimeradd
@ -240,13 +235,6 @@ extern int fmtprint(Fmt*, char*, ...);
extern int fmtvprint(Fmt*, char*, va_list); extern int fmtvprint(Fmt*, char*, va_list);
extern void* mallocz(ulong, int); extern void* mallocz(ulong, int);
extern void srand(long);
extern int rand(void);
extern int nrand(int);
extern long lrand(void);
extern long lnrand(long);
extern double frand(void);
extern ulong getcallerpc(void*); extern ulong getcallerpc(void*);
extern char* cleanname(char*); extern char* cleanname(char*);
extern void sysfatal(char*, ...); extern void sysfatal(char*, ...);

View File

@ -60,7 +60,6 @@ static struct
char *sysname; char *sysname;
vlong fasthz; vlong fasthz;
static void seedrand(void);
static int readtime(ulong, char*, int); static int readtime(ulong, char*, int);
static int readbintime(char*, int); static int readbintime(char*, int);
static int writetime(char*, int); static int writetime(char*, int);
@ -986,30 +985,6 @@ Dev consdevtab = {
devwstat, devwstat,
}; };
static ulong randn;
static void
seedrand(void)
{
randomread((void*)&randn, sizeof(randn));
}
int
xnrand(int n)
{
if(randn == 0)
seedrand();
randn = randn*1103515245 + 12345 + fastticks(0);
return (randn>>16) % n;
}
int
rand(void)
{
xnrand(1);
return randn;
}
static uvlong uvorder = (uvlong) 0x0001020304050607ULL; static uvlong uvorder = (uvlong) 0x0001020304050607ULL;
static uchar* static uchar*

View File

@ -46,12 +46,13 @@ so_socket(int type)
fd = socket(AF_INET, type, 0); fd = socket(AF_INET, type, 0);
if(fd < 0) if(fd < 0)
error(sys_errlist[errno]); oserror();
one = 1; one = 1;
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one)) > 0) if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one)) > 0){
print("setsockopt: %s", sys_errlist[errno]); oserrstr();
print("setsockopt: %s\n", up->errstr);
}
return fd; return fd;
} }
@ -68,7 +69,7 @@ so_connect(int fd, unsigned long raddr, unsigned short rport)
hnputl(&sin.sin_addr.s_addr, raddr); hnputl(&sin.sin_addr.s_addr, raddr);
if(connect(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) if(connect(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
error(sys_errlist[errno]); oserror();
} }
void void
@ -79,7 +80,7 @@ so_getsockname(int fd, unsigned long *laddr, unsigned short *lport)
len = sizeof(sin); len = sizeof(sin);
if(getsockname(fd, (struct sockaddr*)&sin, &len) < 0) if(getsockname(fd, (struct sockaddr*)&sin, &len) < 0)
error(sys_errlist[errno]); oserror();
if(sin.sin_family != AF_INET || len != sizeof(sin)) if(sin.sin_family != AF_INET || len != sizeof(sin))
error("not AF_INET"); error("not AF_INET");
@ -92,7 +93,7 @@ void
so_listen(int fd) so_listen(int fd)
{ {
if(listen(fd, 5) < 0) if(listen(fd, 5) < 0)
error(sys_errlist[errno]); oserror();
} }
int int
@ -104,7 +105,7 @@ so_accept(int fd, unsigned long *raddr, unsigned short *rport)
len = sizeof(sin); len = sizeof(sin);
nfd = accept(fd, (struct sockaddr*)&sin, &len); nfd = accept(fd, (struct sockaddr*)&sin, &len);
if(nfd < 0) if(nfd < 0)
error(sys_errlist[errno]); oserror();
if(sin.sin_family != AF_INET || len != sizeof(sin)) if(sin.sin_family != AF_INET || len != sizeof(sin))
error("not AF_INET"); error("not AF_INET");
@ -121,8 +122,10 @@ so_bind(int fd, int su, unsigned short port)
struct sockaddr_in sin; struct sockaddr_in sin;
one = 1; one = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0) if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0){
print("setsockopt: %s", sys_errlist[errno]); oserrstr();
print("setsockopt: %s", up->errstr);
}
if(su) { if(su) {
for(i = 600; i < 1024; i++) { for(i = 600; i < 1024; i++) {
@ -133,7 +136,7 @@ so_bind(int fd, int su, unsigned short port)
if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) >= 0) if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) >= 0)
return; return;
} }
error(sys_errlist[errno]); oserror();
} }
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
@ -141,7 +144,7 @@ so_bind(int fd, int su, unsigned short port)
hnputs(&sin.sin_port, port); hnputs(&sin.sin_port, port);
if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) if(bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
error(sys_errlist[errno]); oserror();
} }
int int

View File

@ -228,6 +228,7 @@ ipopen(Chan *c, int omode)
Conv *cv, *lcv; Conv *cv, *lcv;
omode &= 3; omode &= 3;
perm = 0;
switch(omode) { switch(omode) {
case OREAD: case OREAD:
perm = 4; perm = 4;

View File

@ -181,6 +181,8 @@ rootgen(Chan *c, char *name, Dirtab *dirt, int ndirt, int s, Dir *dp)
t = c->qid.path - Qmnt - 1; t = c->qid.path - Qmnt - 1;
l = &mntlist; l = &mntlist;
break; break;
default:
return -1;
} }
if(t >= l->ndir) if(t >= l->ndir)
return -1; return -1;

View File

@ -717,7 +717,7 @@ static void
randfill(uchar *buf, int len) randfill(uchar *buf, int len)
{ {
while(len-- > 0) while(len-- > 0)
*buf++ = nrand(256); *buf++ = fastrand();
} }
static long static long

View File

@ -3,6 +3,7 @@
#include "lib.h" #include "lib.h"
#include "dat.h" #include "dat.h"
#include "fns.h" #include "fns.h"
#include <libsec.h>
typedef struct Oproc Oproc; typedef struct Oproc Oproc;
struct Oproc { struct Oproc {
@ -136,24 +137,49 @@ procwakeup(Proc *p)
ReleaseSemaphore(op->sema, 1, 0); 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 void
randominit(void) randominit(void)
{ {
srand(seconds());
} }
ulong ulong
randomread(void *v, ulong n) randomread(void *v, ulong n)
{ {
int m, i, *r; int i;
uchar p[20];
m = (n / sizeof(int)) * sizeof(int);
for (i = 0, r = (int*)v; i < m; i += sizeof(int)) { for(i=0; i<n; i+=20){
*r = rand(); random20(p);
r += sizeof(int); if(i+20 <= n)
memmove((char*)v+i, p, 20);
else
memmove((char*)v+i, p, n-i);
} }
return n;
return m;
} }
long long
@ -183,14 +209,151 @@ fastticks(uvlong *v)
extern int main(int, char*[]); extern int main(int, char*[]);
int APIENTRY
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR arg, int nshow) int
wstrutflen(Rune *s)
{ {
main(__argc, __argv); int n;
for(n=0; *s; n+=runelen(*s),s++)
;
return n;
}
int
wstrtoutf(char *s, Rune *t, int n)
{
int i;
char *s0;
s0 = s;
if(n <= 0)
return wstrutflen(t)+1;
while(*t) {
if(n < UTFmax+1 && n < runelen(*t)+1) {
*s = 0;
return s-s0+wstrutflen(t)+1;
}
i = runetochar(s, t);
s += i;
n -= i;
t++;
}
*s = 0;
return s-s0;
}
int
win_hasunicode(void)
{
OSVERSIONINFOA osinfo;
int r;
osinfo.dwOSVersionInfoSize = sizeof(osinfo);
if(!GetVersionExA(&osinfo))
panic("GetVersionEx failed");
switch(osinfo.dwPlatformId) {
default:
panic("unknown PlatformId");
case VER_PLATFORM_WIN32s:
panic("Win32s not supported");
case VER_PLATFORM_WIN32_WINDOWS:
r = 0;
break;
case VER_PLATFORM_WIN32_NT:
r = 1;
break;
}
return r;
}
int
wstrlen(Rune *s)
{
int n;
for(n=0; *s; s++,n++)
;
return n;
}
static int args(char *argv[], int n, char *p);
int APIENTRY
WinMain(HINSTANCE x, HINSTANCE y, LPSTR z, int w)
{
int argc, n;
char *arg, *p, **argv;
Rune *warg;
if(0 && win_hasunicode()){
warg = GetCommandLineW();
n = (wstrlen(warg)+1)*UTFmax;
arg = malloc(n);
wstrtoutf(arg, warg, n);
}else
arg = GetCommandLineA();
/* conservative guess at the number of args */
for(argc=4,p=arg; *p; p++)
if(*p == ' ' || *p == '\t')
argc++;
argv = malloc(argc*sizeof(char*));
argc = args(argv, argc, arg);
mymain(argc, argv);
ExitThread(0); ExitThread(0);
return 0; return 0;
} }
/*
* Break the command line into arguments
* The rules for this are not documented but appear to be the following
* according to the source for the microsoft C library.
* Words are seperated by space or tab
* Words containing a space or tab can be quoted using "
* 2N backslashes + " ==> 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<n-1; i++) {
while(*p == ' ' || *p == '\t')
p++;
quote = 0;
argv[i] = p2 = p;
for(;*p; p++) {
if(!quote && (*p == ' ' || *p == '\t'))
break;
for(nbs=0; *p == '\\'; p++,nbs++)
;
if(*p == '"') {
for(j=0; j<(nbs>>1); j++)
*p2++ = '\\';
if(nbs&1)
*p2++ = *p;
else
quote = !quote;
} else {
for(j=0; j<nbs; j++)
*p2++ = '\\';
*p2++ = *p;
}
}
/* move p up one to avoid pointing to null at end of p2 */
if(*p)
p++;
*p2 = 0;
}
argv[i] = 0;
return i;
}
/* /*
* Windows socket error messages * Windows socket error messages
* There must be a way to get these strings out of the library. * There must be a way to get these strings out of the library.

View File

@ -29,16 +29,12 @@ OFILES=\
fmtstr.$O\ fmtstr.$O\
fmtvprint.$O\ fmtvprint.$O\
fprint.$O\ fprint.$O\
frand.$O\
getfields.$O\ getfields.$O\
getpid.$O\ getpid.$O\
lnrand.$O\
lock.$O\ lock.$O\
lrand.$O\
mallocz.$O\ mallocz.$O\
nan64.$O\ nan64.$O\
netmkaddr.$O\ netmkaddr.$O\
nrand.$O\
nsec.$O\ nsec.$O\
pow10.$O\ pow10.$O\
pushssl.$O\ pushssl.$O\

View File

@ -19,13 +19,13 @@ static uvlong uvneginf = 0xFFF0000000000000ULL;
double double
__NaN(void) __NaN(void)
{ {
return *(double*)&uvnan; return *(double*)(void*)&uvnan;
} }
int int
__isNaN(double d) __isNaN(double d)
{ {
uvlong x = *(uvlong*)&d; uvlong x = *(uvlong*)(void*)&d;
return (ulong)(x>>32)==0x7FF00000 && !__isInf(d, 0); return (ulong)(x>>32)==0x7FF00000 && !__isInf(d, 0);
} }
@ -33,9 +33,9 @@ double
__Inf(int sign) __Inf(int sign)
{ {
if(sign < 0) if(sign < 0)
return *(double*)&uvinf; return *(double*)(void*)&uvinf;
else else
return *(double*)&uvneginf; return *(double*)(void*)&uvneginf;
} }
int int
@ -43,7 +43,7 @@ __isInf(double d, int sign)
{ {
uvlong x; uvlong x;
x = *(uvlong*)&d; x = *(uvlong*)(void*)&d;
if(sign == 0) if(sign == 0)
return x==uvinf || x==uvneginf; return x==uvinf || x==uvneginf;
else if(sign > 0) else if(sign > 0)

View File

@ -2368,7 +2368,7 @@ DBG print("bsh %d\n", bsh);
bx = -bsh-1; bx = -bsh-1;
ex = -bsh-1-dx; ex = -bsh-1-dx;
SET(bits); bits = 0;
v = par->sdval; v = par->sdval;
/* make little endian */ /* make little endian */

3
main.c
View File

@ -32,6 +32,8 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
eve = getuser(); eve = getuser();
if(eve == nil)
eve = "drawterm";
sizebug(); sizebug();
fmtinstall('r', errfmt); fmtinstall('r', errfmt);
@ -101,6 +103,7 @@ findkey(char **puser, char *dom)
continue; continue;
pass = nil; pass = nil;
haveproto = havedom = 0; haveproto = havedom = 0;
user = nil;
for(i=1; i<nf; i++){ for(i=1; i<nf; i++){
if(strncmp(f[i], "user=", 5) == 0) if(strncmp(f[i], "user=", 5) == 0)
user = f[i]+5; user = f[i]+5;