/* mp5demo.c * a Standard I/O replacement (input only) * by Douglas Jones */ /************************************************************** * NOTE THAT THIS CODE DOES NOT SOLVE MP5 * * anyone using this code without changing MYFILE from a * * simple integer to an appropriate structure will not get * * any credit. In a good solution, mygetc() must only call * * read() when the buffer is empty, and it must read blocks * * of characters into the buffer that always keep the OS * * file position an aligned multiple of the buffer size. * **************************************************************/ #include #include #include #include "myio.h" /************************************************************** * global variables * **************************************************************/ struct myfile { int fd; /* for now, a file is just a file descriptor */ }; MYFILE * myfdopen( int fd, const char * mode ) { /* identical specification to fdopen() */ MYFILE * f = malloc( sizeof(MYFILE) ); f->fd = fd; return f; } int mygetc( MYFILE * f ) { /* identical specification to fgetc */ char c; read( f->fd, &c, 1 ); return c; } long int myseek( MYFILE * f, long int pos ) { /* equivalent to fseek( ... SEEK_SET ) */ return lseek( f->fd, pos, SEEK_SET ); } long int mytell( MYFILE * f ) { /* identical specification to ftell */ return lseek( f->fd, 0, SEEK_CUR ); }