diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2017-01-01 19:31:12 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2017-01-01 19:31:12 +0100 |
commit | 69fe7b182a1eedfb75c611f7dd35fa60200426f4 (patch) | |
tree | 329a0c6cc9b06c23d8782ece09f0f7dfa9b16b13 /minilib/io.c | |
download | compilertests-69fe7b182a1eedfb75c611f7dd35fa60200426f4.tar.gz compilertests-69fe7b182a1eedfb75c611f7dd35fa60200426f4.tar.bz2 |
initial checkin
Diffstat (limited to 'minilib/io.c')
-rw-r--r-- | minilib/io.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/minilib/io.c b/minilib/io.c new file mode 100644 index 0000000..006f5df --- /dev/null +++ b/minilib/io.c @@ -0,0 +1,81 @@ +#include "io.h" +#include "minilib.h" +#include "arena.h" +#include "string.h" + +/* TODO: this is the Linux hosted environment */ +#define _GNU_SOURCE +#include <unistd.h> +#include <sys/syscall.h> + +#include <stdio.h> + +void print( char *s ) +{ + syscall( __NR_write, STDOUT_FILENO, s, strlen( s ) ); + syscall( __NR_write, STDOUT_FILENO, "\n", 1 ); +} + +int readfile( char *filename, char *s, int size ) +{ + /* TODO: this is host-only */ + FILE *f = fopen( filename, "rb" ); + + if( !f ) { + return -1; + } + + fread( s, size, 1, f ); + + fclose( f ); + + return 0; +} + +int writefile( char *filename, char *s, int size ) +{ + /* TODO: this is host-only */ + FILE *f = fopen( filename, "wb" ); + + if( !f ) { + return -1; + } + + fwrite( s, size, 1, f ); + + fclose( f ); + + return 0; +} + +char *readallfile( char *filename ) +{ + /* TODO: this is host-only */ + FILE *f = fopen( filename, "rb" ); + int size; + char *buf; + + if( !f ) { + return NULL; + } + + fseek( f, 0, SEEK_END ); + size = ftell( f ); + rewind( f ); + + if( size == 0 ) { + return NULL; + } + + buf = allocate( size + 1 ); + + if( fread( buf, size, 1, f ) != 1 ) { + return NULL; + } + + buf[size] = '\0'; + + fclose( f ); + + return buf; +} |