Example #1
0
int ungetc(int c, FILE *f){
	if(c==EOF) return EOF;
	switch(f->state){
	default:	/* WR */
		f->state=ERR;
		return EOF;
	case CLOSED:
	case ERR:
		return EOF;
	case OPEN:
		_IO_setvbuf(f);
	case RDWR:
	case END:
		f->wp=f->buf;
		if(f->bufl==0)
			f->wp += 1;
		else
			f->wp += f->bufl;
		f->rp = f->wp;
		f->state=RD;
	case RD:
		if(f->rp==f->buf) return EOF;
		if(f->flags&STRING)
			f->rp--;
		else
			*--f->rp=c;
		return (char)c;
	}
}
Example #2
0
int _IO_getc(FILE *f){
	int cnt, n;
	switch(f->state){
	default:	/* CLOSED, WR, ERR, EOF */
		return EOF;
	case OPEN:
		_IO_setvbuf(f);
	case RDWR:
	case RD:
		if(f->flags&STRING) return EOF;
		if(f->buf == f->unbuf)
			n = 1;
		else
			n = f->bufl;
		cnt=read(f->fd, f->buf, n);
		switch(cnt){
		case -1: f->state=ERR; return EOF;
		case 0: f->state=END; return EOF;
		default:
			f->state=RD;
			f->rp=f->buf;
			f->wp=f->buf+cnt;
			return (*f->rp++)&_IO_CHMASK;
		}
	}
}
Example #3
0
void
setlinebuf (_IO_FILE *stream)
{
  _IO_setvbuf (stream, NULL, 1, 0);
}
Example #4
0
/*
 * Look this over for simplification
 */
int _IO_putc(int c, FILE *f){
	int cnt;
	static int first=1;
	switch(f->state){
	case RD:
		f->state=ERR;
	case ERR:
	case CLOSED:
		return EOF;
	case OPEN:
		_IO_setvbuf(f);
		/* fall through */
	case RDWR:
	case END:
		f->rp=f->buf+f->bufl;
		if(f->flags&LINEBUF){
			f->wp=f->rp;
			f->lp=f->buf;
		}
		else
			f->wp=f->buf;
		break;
	}
	if(first){
		atexit(_IO_cleanup);
		first=0;
	}
	if(f->flags&STRING){
		f->rp=f->buf+f->bufl;
		if(f->wp==f->rp){
			if(f->flags&BALLOC)
				f->buf=realloc(f->buf, f->bufl+BUFSIZ);
			else{
				f->state=ERR;
				return EOF;
			}
			if(f->buf==NULL){
				f->state=ERR;
				return EOF;
			}
			f->rp=f->buf+f->bufl;
			f->bufl+=BUFSIZ;
		}
		*f->wp++=c;
	}
	else if(f->flags&LINEBUF){
		if(f->lp==f->rp){
			cnt=f->lp-f->buf;
			if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
			if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
				f->state=ERR;
				return EOF;
			}
			f->lp=f->buf;
		}
		*f->lp++=c;
		if(c=='\n'){
			cnt=f->lp-f->buf;
			if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
			if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
				f->state=ERR;
				return EOF;
			}
			f->lp=f->buf;
		}
	}
	else if(f->buf==f->unbuf){
		f->unbuf[0]=c;
		if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
		if(write(f->fd, f->buf, 1)!=1){
			f->state=ERR;
			return EOF;
		}
	}
	else{
		if(f->wp==f->rp){
			cnt=f->wp-f->buf;
			if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
			if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
				f->state=ERR;
				return EOF;
			}
			f->wp=f->buf;
			f->rp=f->buf+f->bufl;
		}
		*f->wp++=c;
	}
	f->state=WR;
	/*
	 * Make sure EOF looks different from putc(-1)
	 * Should be able to cast to unsigned char, but
	 * there's a vc bug preventing that from working
	 */
	return c&0xff;
}