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