/** * myio.c - Declarations of simple I/O functions. * Based closely on code taken from section 4.3 of K&R. * Error checking removed because error messages should go to client, * not to user. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For fprintf, getchar #include "myio.h" // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** * The maximum number of characters we can "unread". */ #define BUFSIZE 128 // +---------+-------------------------------------------------------- // | Globals | // +---------+ /** * The buffer in which we store unread characters. */ int buf[BUFSIZE]; /** * The position in the buffer that stores the most-recently unread * character. (When 0, there is no most-recently unread character.) */ int bufp = 0; // +--------------------+--------------------------------------------- // | Exported Functions | // +--------------------+ int getch () { return (bufp > 0) ? buf[--bufp] : getchar (); } // getch void ungetch (int c) { buf[bufp++] = c; } // ungetch