xref: /csrg-svn/lib/libc/stdio/funopen.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[] = "@(#)funopen.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <stdio.h>
16*46111Sbostic #include <errno.h>
17*46111Sbostic #include "local.h"
18*46111Sbostic 
19*46111Sbostic FILE *
20*46111Sbostic funopen(cookie, readfn, writefn, seekfn, closefn)
21*46111Sbostic 	char *cookie;
22*46111Sbostic 	int (*readfn)(), (*writefn)();
23*46111Sbostic #if __STDC__
24*46111Sbostic 	fpos_t (*seekfn)(char *cookie, fpos_t off, int whence);
25*46111Sbostic #else
26*46111Sbostic 	fpos_t (*seekfn)();
27*46111Sbostic #endif
28*46111Sbostic 	int (*closefn)();
29*46111Sbostic {
30*46111Sbostic 	register FILE *fp;
31*46111Sbostic 	int flags;
32*46111Sbostic 
33*46111Sbostic 	if (readfn == NULL) {
34*46111Sbostic 		if (writefn == NULL) {		/* illegal */
35*46111Sbostic 			errno = EINVAL;
36*46111Sbostic 			return (NULL);
37*46111Sbostic 		} else
38*46111Sbostic 			flags = __SWR;		/* write only */
39*46111Sbostic 	} else {
40*46111Sbostic 		if (writefn == NULL)
41*46111Sbostic 			flags = __SRD;		/* read only */
42*46111Sbostic 		else
43*46111Sbostic 			flags = __SRW;		/* read-write */
44*46111Sbostic 	}
45*46111Sbostic 	if ((fp = __sfp()) == NULL)
46*46111Sbostic 		return (NULL);
47*46111Sbostic 	fp->_flags = flags;
48*46111Sbostic 	fp->_file = -1;
49*46111Sbostic 	fp->_cookie = cookie;
50*46111Sbostic 	fp->_read = readfn;
51*46111Sbostic 	fp->_write = writefn;
52*46111Sbostic 	fp->_seek = seekfn;
53*46111Sbostic 	fp->_close = closefn;
54*46111Sbostic 	return (fp);
55*46111Sbostic }
56