diff --git a/library_stdio.c b/library_stdio.c index bd392c9..3384f7b 100644 --- a/library_stdio.c +++ b/library_stdio.c @@ -3,6 +3,8 @@ #ifndef NO_HASH_INCLUDE +static int EOFValue = EOF; + void StdioFopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { ReturnValue->Val->NativePointer = fopen(Param[0]->Val->NativePointer, Param[1]->Val->NativePointer); @@ -53,6 +55,9 @@ void StdioSetupFunc(void) { /* make a "struct FILEStruct" which is the same size as a native FILE structure */ TypeCreateOpaqueStruct(NULL, TableStrRegister("FILEStruct"), sizeof(FILE)); + + /* define EOF equal to the system EOF */ + VariableDefinePlatformVar(NULL, "EOF", &IntType, (union AnyValue *)&EOFValue, FALSE); } #endif /* NO_HASH_INCLUDE */ diff --git a/tests/40_stdio.c b/tests/40_stdio.c new file mode 100644 index 0000000..f1d76b9 --- /dev/null +++ b/tests/40_stdio.c @@ -0,0 +1,45 @@ +#include + +FILE *f = fopen("fred.txt", "w"); +fwrite("hello\nhello\n", 1, 12, f); +fclose(f); + +char freddy[7]; +f = fopen("fred.txt", "r"); +if (fread(freddy, 1, 6, f) != 6) + printf("couldn't read fred.txt\n"); + +freddy[6] = '\0'; +fclose(f); + +printf("%s", freddy); + +char InChar; +char ShowChar; +f = fopen("fred.txt", "r"); +while ( (InChar = fgetc(f)) != EOF) +{ + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); +} +fclose(f); + +f = fopen("fred.txt", "r"); +while ( (InChar = getc(f)) != EOF) +{ + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); +} +fclose(f); + +f = fopen("fred.txt", "r"); +while (fgets(freddy, sizeof(freddy), f) != NULL) + printf("x: %s", freddy); + +fclose(f); diff --git a/tests/40_stdio.expect b/tests/40_stdio.expect new file mode 100644 index 0000000..e08167a --- /dev/null +++ b/tests/40_stdio.expect @@ -0,0 +1,27 @@ +hello +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +x: hello +x: hello diff --git a/tests/Makefile b/tests/Makefile index e28faa8..7b8c663 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -32,7 +32,8 @@ TESTS= 00_assignment.test \ 36_array_initialisers.test \ 37_sprintf.test \ 38_multiple_array_index.test \ - 39_typedef.test + 39_typedef.test \ + 40_stdio.test %.test: %.expect %.c @echo Test: $*...