xref: /csrg-svn/lib/libc/stdio/stdio.c (revision 46111)
1*46111Sbostic /*-
2*46111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46111Sbostic  * All rights reserved.
4*46111Sbostic  *
5*46111Sbostic  * This code is derived from software contributed to Berkeley by
6*46111Sbostic  * Chris Torek.
7*46111Sbostic  *
8*46111Sbostic  * %sccs.include.redist.c%
9*46111Sbostic  */
10*46111Sbostic 
11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46111Sbostic static char sccsid[] = "@(#)stdio.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <sys/types.h>
16*46111Sbostic #include <sys/file.h>
17*46111Sbostic #include <sys/stdc.h>
18*46111Sbostic #include <stdio.h>
19*46111Sbostic #include "local.h"
20*46111Sbostic 
21*46111Sbostic /*
22*46111Sbostic  * Small standard I/O/seek/close functions.
23*46111Sbostic  * These maintain the `known seek offset' for seek optimisation.
24*46111Sbostic  */
25*46111Sbostic __sread(cookie, buf, n)
26*46111Sbostic 	void *cookie;
27*46111Sbostic 	char *buf;
28*46111Sbostic 	int n;
29*46111Sbostic {
30*46111Sbostic 	register FILE *fp = cookie;
31*46111Sbostic 	register int ret;
32*46111Sbostic 
33*46111Sbostic 	ret = read(fp->_file, buf, n);
34*46111Sbostic 	/* if the read succeeded, update the current offset */
35*46111Sbostic 	if (ret >= 0)
36*46111Sbostic 		fp->_offset += ret;
37*46111Sbostic 	else
38*46111Sbostic 		fp->_flags &= ~__SOFF;	/* paranoia */
39*46111Sbostic 	return (ret);
40*46111Sbostic }
41*46111Sbostic 
42*46111Sbostic __swrite(cookie, buf, n)
43*46111Sbostic 	void *cookie;
44*46111Sbostic 	char const *buf;
45*46111Sbostic 	int n;
46*46111Sbostic {
47*46111Sbostic 	register FILE *fp = cookie;
48*46111Sbostic 
49*46111Sbostic 	if (fp->_flags & __SAPP)
50*46111Sbostic 		(void) lseek(fp->_file, (off_t)0, SEEK_END);
51*46111Sbostic 	fp->_flags &= ~__SOFF;	/* in case FAPPEND mode is set */
52*46111Sbostic 	return (write(fp->_file, buf, n));
53*46111Sbostic }
54*46111Sbostic 
55*46111Sbostic fpos_t
56*46111Sbostic __sseek(cookie, offset, whence)
57*46111Sbostic 	void *cookie;
58*46111Sbostic 	fpos_t offset;
59*46111Sbostic 	int whence;
60*46111Sbostic {
61*46111Sbostic 	register FILE *fp = cookie;
62*46111Sbostic 	register off_t ret;
63*46111Sbostic 
64*46111Sbostic 	ret = lseek(fp->_file, (off_t)offset, whence);
65*46111Sbostic 	if (ret == -1L)
66*46111Sbostic 		fp->_flags &= ~__SOFF;
67*46111Sbostic 	else {
68*46111Sbostic 		fp->_flags |= __SOFF;
69*46111Sbostic 		fp->_offset = ret;
70*46111Sbostic 	}
71*46111Sbostic 	return (ret);
72*46111Sbostic }
73*46111Sbostic 
74*46111Sbostic __sclose(cookie)
75*46111Sbostic 	void *cookie;
76*46111Sbostic {
77*46111Sbostic 
78*46111Sbostic 	return (close(((FILE *)cookie)->_file));
79*46111Sbostic }
80