#include "io.h" #include "minilib.h" #include "arena.h" #include "string.h" /* TODO: this is the Linux hosted environment */ #define _GNU_SOURCE #include #include #include 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 ); buf = allocate( size + 1 ); if( size == 0 ) { buf[size] = '\0'; fclose( f ); return buf; } if( fread( buf, size, 1, f ) != 1 ) { return NULL; } buf[size] = '\0'; fclose( f ); return buf; }