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