diff --git a/cstdlib/stdlib.c b/cstdlib/stdlib.c index c472fb6..4c403f9 100644 --- a/cstdlib/stdlib.c +++ b/cstdlib/stdlib.c @@ -6,6 +6,36 @@ static int ZeroValue = 0; static int TRUEValue = 1; +void StdlibAtof(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->FP = atof(Param[0]->Val->NativePointer); +} + +void StdlibAtoi(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = atoi(Param[0]->Val->NativePointer); +} + +void StdlibAtol(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = atol(Param[0]->Val->NativePointer); +} + +void StdlibStrtod(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->FP = strtod(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer); +} + +void StdlibStrtol(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = strtol(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer); +} + +void StdlibStrtoul(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = strtoul(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer, Param[2]->Val->Integer); +} + void StdlibMalloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { ReturnValue->Val->NativePointer = malloc(Param[0]->Val->Integer); @@ -26,14 +56,56 @@ void StdlibFree(struct ParseState *Parser, struct Value *ReturnValue, struct Val free(Param[0]->Val->NativePointer); } +void StdlibRand(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = rand(); +} + +void StdlibSrand(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + srand(Param[0]->Val->Integer); +} + +void StdlibAbort(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ProgramFail(Parser, "abort"); +} + +void StdlibExit(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + PlatformExit(Param[0]->Val->Integer); +} + +void StdlibGetenv(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->NativePointer = getenv(Param[0]->Val->NativePointer); +} + +void StdlibSystem(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->Integer = system(Param[0]->Val->NativePointer); +} + /* all stdlib.h functions */ struct LibraryFunction StdlibFunctions[] = { - { StdlibMalloc, "void *malloc(int);" }, - { StdlibCalloc, "void *calloc(int,int);" }, - { StdlibRealloc, "void *realloc(void *,int);" }, - { StdlibFree, "void free(void *);" }, - { NULL, NULL } + { StdlibAtof, "float atof(char *);" }, + { StdlibAtoi, "int atoi(char *);" }, + { StdlibAtol, "int atol(char *);" }, + { StdlibStrtod, "float strtod(char *,char **);" }, + { StdlibStrtol, "int strtol(char *,char **,int);" }, + { StdlibStrtoul, "int strtoul(char *,char **,int);" }, + { StdlibMalloc, "void *malloc(int);" }, + { StdlibCalloc, "void *calloc(int,int);" }, + { StdlibRealloc, "void *realloc(void *,int);" }, + { StdlibFree, "void free(void *);" }, + { StdlibRand, "int rand();" }, + { StdlibSrand, "void srand(int);" }, + { StdlibAbort, "void abort();" }, + { StdlibExit, "void exit(int);" }, + { StdlibGetenv, "char *getenv(char *);" }, + { StdlibSystem, "int system(char *);" }, + { NULL, NULL } }; /* creates various system-dependent definitions */