/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libc-port:stdio/ungetc.c	2.7"
/*	3.0 SID #	1.3	*/
/*LINTLIBRARY*/
#include "shlib.h"
#include <stdio.h>

extern _findbuf();

int
ungetc(c, iop)
int	c;
register FILE *iop;
{
	if(c == EOF)   
		return(EOF);
	if((iop->_flag & (_IORW|_IOWRT|_IOREAD)) == _IORW)
		iop->_flag |= _IOREAD;
	if((iop->_flag & _IOREAD) == 0 || iop->_ptr <= iop->_base)
		if(iop->_ptr == iop->_base && iop->_cnt == 0)
			++iop->_ptr;
		else
			return(EOF);
	if (iop->_base == NULL) {
		_findbuf(iop);
		*iop->_ptr = c;
		++iop->_cnt;
	} else {
		*--iop->_ptr = c;
		++iop->_cnt;
	}
	/*
	 * The XPG3 states:
	 * A successful call to the ungetc() function clears the end-of-file
	 * indicatior for the stream.
	 */
	iop->_flag &= ~_IOEOF;
	return(c);
}
