home *** CD-ROM | disk | FTP | other *** search
- /* :set tabstops=4 */
- static char *RCSid = "$Header: iobuf.c,v 1.1 86/04/20 16:16:43 sysad Exp $";
-
- /*
- * $Log: iobuf.c,v $
- * Revision 1.1 86/04/20 16:16:43 sysad
- * Initial distribution version
- *
- *
- */
-
- /* An utterly simple buffering scheme; it's all we need. */
-
- /* It is the intent of the author that this software may be distributed
- * and used freely, without restriction. If you make improvements or
- * enhancements, I would appreciate a copy.
- *
- * Duane H. Hesser Teltone Corporation
- * ....uw-beaver!tikal!sysad
- * ....uw-beaver!tikal!dhh
- */
-
- #include "io.h"
-
- bufopen(file,io)
- char *file;
- struct inbuf *io;
- {
-
- if((io->fd = open(file,0)) < 0)
- return(-1);
- io->nleft = 0;
- io->eof = 0;
- return(io->fd);
- }
-
- bufclose(io)
- struct inbuf *io;
- {
- if(io->fd >= 0) close(io->fd);
- io->eof = 1;
- io->nleft = 0;
- io->fd = -1;
- }
-
- char
- getbyte(io)
- struct inbuf *io;
- {
- char next,ebuf[32];
-
- if(io->nleft <= 0)
- {
- if(io->fd < 0)
- {
- sprintf(ebuf,"%s: unopen file error in getbyte()\n");
- write(2,ebuf,strlen(ebuf));
- exit(69);
- }
- io->nleft = read(io->fd,io->buff,INBUFSIZE);
- if(io->nleft < 0)
- {
- sprintf(ebuf,"%s: read error in getbyte()\n");
- write(2,ebuf,strlen(ebuf));
- exit(69);
- }
- io->nextp = io->buff;
- }
- if(io->nleft-- <= 0)
- {
- io->eof++;
- return('\0');
- }
- next = *(io->nextp++) & '\377';
- return(next);
- }
-
-
-
- getstr(io,buf)
- struct inbuf *io;
- char *buf;
- {
- char *ptr = buf;
- char getbyte();
-
- while( *ptr = getbyte(io) )
- {
- if(*ptr++ == '\n')
- {
- *--ptr = '\0';
- break;
- }
- }
- return(strlen(buf));
- }
-
-
- #ifdef BIGENDIAN
- union {
- short sh_word;
- struct {
- char hi_byte;
- char lo_byte;
- } sh_bytes;
- } sh;
- #else
- union {
- short sh_word;
- struct {
- char lo_byte;
- char hi_byte;
- } sh_bytes;
- } sh;
- #endif
- short
- getshort(io)
- struct inbuf *io;
- {
- char c,ebuf[32];
-
- c = getbyte(io);
- if(eof(io))
- {
- sprintf(ebuf,"unexpected EOF in getshort()\n");
- write(2,ebuf,strlen(ebuf));
- exit(69);
- }
- else sh.sh_bytes.lo_byte = c;
-
- c = getbyte(io);
- if(eof(io))
- {
- sprintf(ebuf,"unexpected EOF in getshort()\n");
- write(2,ebuf,strlen(ebuf));
- exit(69);
- }
- else sh.sh_bytes.hi_byte = c;
- return(sh.sh_word);
- }
-
- eof(source)
- struct inbuf *source;
- {
- return(source->eof);
- }
-
-
- /* Note that we don't even bother to "open" for output; just use
- * file descriptor 1 (normally stdout)
- */
- bufwrite(from,len,outbuf)
- char *from;
- int len;
- struct outbuf *outbuf;
- {
- if(len <= 0) return;
- if((len + outbuf->datalen) > OUTBUFSIZE) buf_flush(outbuf);
- bcopy(from,outbuf->nextp,len);
- outbuf->datalen += len;
- outbuf->nextp += len;
- if((len + outbuf->datalen) >= OUTBUFSIZE) buf_flush(outbuf);
- }
-
- buf_flush(buf)
- struct outbuf *buf;
- {
- if(buf->datalen > 0) write(1,buf->buffer,buf->datalen);
- buf->nextp = buf->buffer;
- buf->datalen = 0;
- }
-