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