xref: /csrg-svn/lib/libc/stdio/funopen.c (revision 61180)
146111Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)funopen.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <stdio.h>
1646111Sbostic #include <errno.h>
1746111Sbostic #include "local.h"
1846111Sbostic 
1946111Sbostic FILE *
funopen(cookie,readfn,writefn,seekfn,closefn)2046111Sbostic funopen(cookie, readfn, writefn, seekfn, closefn)
2146272Storek 	const void *cookie;
2246111Sbostic 	int (*readfn)(), (*writefn)();
2346111Sbostic #if __STDC__
2446272Storek 	fpos_t (*seekfn)(void *cookie, fpos_t off, int whence);
2546111Sbostic #else
2646111Sbostic 	fpos_t (*seekfn)();
2746111Sbostic #endif
2846111Sbostic 	int (*closefn)();
2946111Sbostic {
3046111Sbostic 	register FILE *fp;
3146111Sbostic 	int flags;
3246111Sbostic 
3346111Sbostic 	if (readfn == NULL) {
3446111Sbostic 		if (writefn == NULL) {		/* illegal */
3546111Sbostic 			errno = EINVAL;
3646111Sbostic 			return (NULL);
3746111Sbostic 		} else
3846111Sbostic 			flags = __SWR;		/* write only */
3946111Sbostic 	} else {
4046111Sbostic 		if (writefn == NULL)
4146111Sbostic 			flags = __SRD;		/* read only */
4246111Sbostic 		else
4346111Sbostic 			flags = __SRW;		/* read-write */
4446111Sbostic 	}
4546111Sbostic 	if ((fp = __sfp()) == NULL)
4646111Sbostic 		return (NULL);
4746111Sbostic 	fp->_flags = flags;
4846111Sbostic 	fp->_file = -1;
4946272Storek 	fp->_cookie = (void *)cookie;
5046111Sbostic 	fp->_read = readfn;
5146111Sbostic 	fp->_write = writefn;
5246111Sbostic 	fp->_seek = seekfn;
5346111Sbostic 	fp->_close = closefn;
5446111Sbostic 	return (fp);
5546111Sbostic }
56