sl@0: /* REFILL.C sl@0: * sl@0: * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* No user fns here. Pesch 15apr92. */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: #include sl@0: #include "LOCAL.H" sl@0: #include sl@0: sl@0: static int sl@0: lflush (FILE *fp) sl@0: { sl@0: if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR)) sl@0: return fflush (fp); sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: * Refill a stdio buffer. sl@0: * Return EOF on eof or error, 0 otherwise. sl@0: */ sl@0: sl@0: int sl@0: __srefill (register FILE * fp) sl@0: { sl@0: /* make sure stdio is set up */ sl@0: sl@0: CHECK_INIT (fp); sl@0: sl@0: fp->_r = 0; /* largely a convenience for callers */ sl@0: sl@0: /* SysV does not make this test; take it out for compatibility */ sl@0: if (fp->_flags & __SEOF) sl@0: return EOF; sl@0: sl@0: /* if not already reading, have to be reading and writing */ sl@0: if ((fp->_flags & __SRD) == 0) sl@0: { sl@0: if ((fp->_flags & __SRW) == 0) sl@0: return EOF; sl@0: /* switch to reading */ sl@0: if (fp->_flags & __SWR) sl@0: { sl@0: if (fflush (fp)) sl@0: return EOF; sl@0: fp->_flags &= ~__SWR; sl@0: fp->_w = 0; sl@0: fp->_lbfsize = 0; sl@0: } sl@0: fp->_flags |= __SRD; sl@0: } sl@0: else sl@0: { sl@0: /* sl@0: * We were reading. If there is an ungetc buffer, sl@0: * we must have been reading from that. Drop it, sl@0: * restoring the previous buffer (if any). If there sl@0: * is anything in that buffer, return. sl@0: */ sl@0: if (HASUB (fp)) sl@0: { sl@0: FREEUB (fp); sl@0: if ((fp->_r = fp->_ur) != 0) sl@0: { sl@0: fp->_p = fp->_up; sl@0: return 0; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (fp->_bf._base == NULL) sl@0: __smakebuf (fp); sl@0: sl@0: /* sl@0: * Before reading from a line buffered or unbuffered file, sl@0: * flush all line buffered output files, per the ANSI C sl@0: * standard. sl@0: */ sl@0: sl@0: if (fp->_flags & (__SLBF | __SNBF)) sl@0: (void) _fwalk (fp->_data, lflush); sl@0: fp->_p = fp->_bf._base; sl@0: fp->_r = (*fp->_read) (fp->_cookie, (char *) fp->_p, fp->_bf._size); sl@0: fp->_flags &= ~__SMOD; /* buffer contents are again pristine */ sl@0: if (fp->_r <= 0) sl@0: { sl@0: if (fp->_r == 0) sl@0: fp->_flags |= __SEOF; sl@0: else sl@0: { sl@0: fp->_r = 0; sl@0: fp->_flags |= __SERR; sl@0: } sl@0: return EOF; sl@0: } sl@0: return 0; sl@0: }