#define NOCCARG #include stdio.h #include clib.def extern int *_auxsz, *_auxef, _auxrd, _auxwt, _auxfl, _status[]; /* ** Loaded only if auxbuf() is called. Longer explanation in DDJ. */ int _xsize[MAXFILES], /*size of buffer*/ _xaddr[MAXFILES], /*aux buffer address*/ _xnext[MAXFILES], /*address of next byte in buffer*/ _xend[MAXFILES], /*address of end-of-data in buffer*/ _xeof[MAXFILES]; /*true if current buffer ends file*/ /* ** auxbuf(fd, size) -- allocate an auxiliary buffer of size size for ** fd fd. Longer explanation in DDJ. */ auxbuf(fd, size) int fd; char *size; { /*fake unsigned*/ if(!_mode(fd) || size || avail(NO) < size || _xsize[fd]) return (ERR); _xaddr[fd] = _xnext[fd] = _xend[fd] = malloc(size); _auxef = _xeof; /*pass locations to i/o routines*/ _auxrd = _xread; _auxwt = _xwrite; _auxsz = _xsize; _auxfl = _xflush; _xsize[fd] = size; return (NULL); } /* ** Fill buffer if necessary, and return next byte. */ _xread(fd) int fd; { char *ptr; while(YES) { ptr = _xnext[fd]; if(ptr < _xend[fd]) {++_xnext[fd]; return (*ptr);} if(_xeof[fd]) {_seteof(fd); return (EOF);} _auxsz = NULL; /*avoid recursive loop*/ _xend[fd] = _xaddr[fd] + read(fd, _xnext[fd]=_xaddr[fd], _xsize[fd]); _auxsz = _xsize; /*restore _auxsz*/ if(feof(fd)) {_xeof[fd] = YES; _clreof(fd);} } } /* ** Empty buffer if necessary, and store ch in buffer. */ _xwrite(ch, fd) int ch, fd; { char *ptr; while(YES) { ptr = _xnext[fd]; if(ptr < (_xaddr[fd] + _xsize[fd])) {*ptr = ch; ++_xnext[fd]; return (ch);} if(_xflush(fd)) return (EOF); } } /* ** Flush aux buffer to file. */ _xflush(fd) int fd; { int i, j; i = _xnext[fd] - _xaddr[fd]; _auxsz = NULL; /*avoid recursive loop*/ j = write(fd, _xnext[fd]=_xaddr[fd], i); _auxsz = _xsize; /*restore _auxsz*/ if(i != j) return (EOF); return (NULL); }