xref: /csrg-svn/lib/libc/stdio/stdio.c (revision 61180)
146111Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)stdio.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546611Sbostic #include <fcntl.h>
1646611Sbostic #include <unistd.h>
1746111Sbostic #include <stdio.h>
1846111Sbostic #include "local.h"
1946111Sbostic 
2046111Sbostic /*
2146111Sbostic  * Small standard I/O/seek/close functions.
2246111Sbostic  * These maintain the `known seek offset' for seek optimisation.
2346111Sbostic  */
__sread(cookie,buf,n)2446111Sbostic __sread(cookie, buf, n)
2546111Sbostic 	void *cookie;
2646111Sbostic 	char *buf;
2746111Sbostic 	int n;
2846111Sbostic {
2946111Sbostic 	register FILE *fp = cookie;
3046111Sbostic 	register int ret;
3146111Sbostic 
3246111Sbostic 	ret = read(fp->_file, buf, n);
3346111Sbostic 	/* if the read succeeded, update the current offset */
3446111Sbostic 	if (ret >= 0)
3546111Sbostic 		fp->_offset += ret;
3646111Sbostic 	else
3746111Sbostic 		fp->_flags &= ~__SOFF;	/* paranoia */
3846111Sbostic 	return (ret);
3946111Sbostic }
4046111Sbostic 
__swrite(cookie,buf,n)4146111Sbostic __swrite(cookie, buf, n)
4246111Sbostic 	void *cookie;
4346111Sbostic 	char const *buf;
4446111Sbostic 	int n;
4546111Sbostic {
4646111Sbostic 	register FILE *fp = cookie;
4746111Sbostic 
4846111Sbostic 	if (fp->_flags & __SAPP)
4946111Sbostic 		(void) lseek(fp->_file, (off_t)0, SEEK_END);
5046111Sbostic 	fp->_flags &= ~__SOFF;	/* in case FAPPEND mode is set */
5146111Sbostic 	return (write(fp->_file, buf, n));
5246111Sbostic }
5346111Sbostic 
5446111Sbostic fpos_t
__sseek(cookie,offset,whence)5546111Sbostic __sseek(cookie, offset, whence)
5646111Sbostic 	void *cookie;
5746111Sbostic 	fpos_t offset;
5846111Sbostic 	int whence;
5946111Sbostic {
6046111Sbostic 	register FILE *fp = cookie;
6146111Sbostic 	register off_t ret;
6246111Sbostic 
6346111Sbostic 	ret = lseek(fp->_file, (off_t)offset, whence);
6446111Sbostic 	if (ret == -1L)
6546111Sbostic 		fp->_flags &= ~__SOFF;
6646111Sbostic 	else {
6746111Sbostic 		fp->_flags |= __SOFF;
6846111Sbostic 		fp->_offset = ret;
6946111Sbostic 	}
7046111Sbostic 	return (ret);
7146111Sbostic }
7246111Sbostic 
__sclose(cookie)7346111Sbostic __sclose(cookie)
7446111Sbostic 	void *cookie;
7546111Sbostic {
7646111Sbostic 
7746111Sbostic 	return (close(((FILE *)cookie)->_file));
7846111Sbostic }
79