/* * fgetc * Conforms closely to the standard i/o function used in the Version 7 * U**X C Library but takes careful note of the CP/M console and reader * devices. The structure of the hypothetical getc macro corresponding * to this function is as follows: * #define getc(p) ((p)->_nleft-- ? *(p)->_nextp++ : _filbuf(p)) * Last Edit 7/3/83 */ int fgetc(stream) FILE *stream; { int nsecs; char c; if (stream == stdin) { c = bdos(1,0); return ((c == '\r') ? '\n' : c) ; } else if (stream == DEV_RDR) return bdos(3,0); else if (!stream -> _nleft--) { if((nsecs = read(stream->_fd, stream->_base, NSECTS)) <= 0){ if (stream -> _nleft == EOF) stream -> _flag = _IOEOF ; if (nsecs == ERROR) stream -> _flag |= _IOERR ; return stream -> _nleft++; } stream -> _nleft = nsecs * SECSIZ - 1; stream -> _nextp = stream -> _base; } return *stream->_nextp++; } /* * fputc * This function conforms closely to the function in the Version * 7 U**X C Library with consideraton of the CPM console and reader * devices. The hypothetical corresponding putc macro follows: * #define putc(c,p) ((p)->_nleft--?*(p)->_nextp++ = (c):_flsbuf((x),p)) * Last Edit 7/1/83 */ void fputc(c,stream) char c; FILE *stream; { if (stream==stdout) { if (c == '\n') bdos(2,'\r'); bdos(2,c); return ; } else if (stream==DEV_LST) { bdos(5,c); return ; } else if (stream==DEV_PUN) { bdos(4,c); return ; } else if (stream==stderr) { if (c == '\n') bios(4,'\r'); bios(4,c); return ; } if ((stream -> _flag & _IOWRT) == 0) { /* can't write on stream */ stream -> _flag |= _IOERR ; return ; } if (!stream -> _nleft--) { /* if buffer full then flush it */ if ((write(stream->_fd, stream->_base, NSECTS)) != NSECTS) { stream -> _flag |= _IOERR ; return ; } stream -> _nleft = (BUFSIZ - 1); stream -> _nextp = stream -> _base; } *stream -> _nextp++ = c; return ; } /* * ungetc * This function conforms closely to the macro used in the Version * 7 U**X Standard C Libary. It is a buffered "unget" character * routine. Only ONE char may be "ungotten" between consecutive * getc()/fgetc() calls. Note that any ungotten character is lost * if a call subsquently made to fseek(). The macro corresponding * the function is as follows: * #define ungetc(c,p) ((p)->_nleft==BUFSIZ ? -1 : *--(p)->_nextp=(c)) * Last Edit 7/3/83 */ void ungetc(c, stream) FILE *stream; char c; { if (stream == stdin) { ungetch(c); /* stdin is special */ return ; } else if (stream <= DEV_PUN && stream > stdin) { /* stream -> _flag |= _IOERR ; no pushbacks here */ return ; } if (stream -> _nleft == BUFSIZ) { stream -> _flag |= _IOERR ; /* too many pushed back */ return ; } *--stream -> _nextp = c ; stream -> _nleft++ ; return ; }