diff --git a/games/snake.cpp b/games/snake.cpp index c1ac17e1..bea1690e 100644 --- a/games/snake.cpp +++ b/games/snake.cpp @@ -56,8 +56,8 @@ void Reset() case 3: velx = 0; vely = -1; break; } - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); taillen = 0; tailmax = 3; @@ -141,8 +141,10 @@ void Update() if ( newx == animalx && newy == animaly ) { tailmax++; - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); + ASSERT(0 <= animalx && animalx < width); + ASSERT(0 <= animaly && animaly < height); if ( maxspeed < speed ) { speed += speedincrease; } } diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index 3a1a7eac..bf96f74a 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -83,7 +83,6 @@ int chown(const char*, uid_t, gid_t); size_t confstr(int, char*, size_t); char* crypt(const char*, const char*); char* ctermid(char*); -int dup(int); int dup2(int, int); void encrypt(char [64], int); int execl(const char*, const char*, ...); @@ -159,6 +158,7 @@ extern int opterr, optind, optopt; #endif int close(int); +int dup(int); void _exit(int); pid_t fork(void); pid_t getpid(void); diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index dfde5839..be89feab 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -34,6 +34,7 @@ namespace Maxsi DEFN_SYSCALL3(ssize_t, SysWrite, 19, int, const void*, size_t); DEFN_SYSCALL1(int, SysPipe, 20, int*); DEFN_SYSCALL1(int, SysClose, 21, int); + DEFN_SYSCALL1(int, SysDup, 22, int); size_t Print(const char* Message) { @@ -83,6 +84,11 @@ namespace Maxsi { return SysClose(fd); } + + extern "C" int dup(int fd) + { + return SysDup(fd); + } #endif } diff --git a/libmaxsi/random.cpp b/libmaxsi/random.cpp index 63daa8a4..9bbef8de 100644 --- a/libmaxsi/random.cpp +++ b/libmaxsi/random.cpp @@ -28,11 +28,11 @@ namespace Maxsi { namespace Random { - int random_seed=1337; + unsigned random_seed = 1337; extern "C" int rand() { random_seed = random_seed + 37 * 1103515245 + 12345; - return random_seed / 65536; + return random_seed >> 16; } } } diff --git a/sortix/io.cpp b/sortix/io.cpp index b7f362d3..e98df387 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -126,11 +126,21 @@ namespace Sortix return 0; } + int SysDup(int fd) + { + Process* process = CurrentProcess(); + Device* dev = process->descriptors.Get(fd); + if ( !dev ) { return -1; /* TODO: EBADF */ } + process->descriptors.Free(fd); + return process->descriptors.Allocate(dev); + } + void Init() { Syscall::Register(SYSCALL_WRITE, (void*) SysWrite); Syscall::Register(SYSCALL_READ, (void*) SysRead); Syscall::Register(SYSCALL_CLOSE, (void*) SysClose); + Syscall::Register(SYSCALL_DUP, (void*) SysDup); } } } diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index f79ba6d0..a9678856 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -47,7 +47,8 @@ #define SYSCALL_WRITE 19 #define SYSCALL_PIPE 20 #define SYSCALL_CLOSE 21 -#define SYSCALL_MAX_NUM 22 /* index of highest constant + 1 */ +#define SYSCALL_DUP 22 +#define SYSCALL_MAX_NUM 23 /* index of highest constant + 1 */ #endif