xref: /freebsd/lib/libc/stdio/fopencookie.c (revision 559a218c)
1877a840cSConrad Meyer /*
2877a840cSConrad Meyer  * Copyright (c) 2016, EMC / Isilon Storage Division
3877a840cSConrad Meyer  * All rights reserved.
4877a840cSConrad Meyer  *
5877a840cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
6877a840cSConrad Meyer  * modification, are permitted provided that the following conditions
7877a840cSConrad Meyer  * are met:
8877a840cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
9877a840cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
10877a840cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
11877a840cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
12877a840cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
13877a840cSConrad Meyer  *
14877a840cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15877a840cSConrad Meyer  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16877a840cSConrad Meyer  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17877a840cSConrad Meyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18877a840cSConrad Meyer  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19877a840cSConrad Meyer  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20877a840cSConrad Meyer  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21877a840cSConrad Meyer  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22877a840cSConrad Meyer  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23877a840cSConrad Meyer  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24877a840cSConrad Meyer  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25877a840cSConrad Meyer  */
26877a840cSConrad Meyer 
27877a840cSConrad Meyer #include <sys/fcntl.h>
28877a840cSConrad Meyer 
29877a840cSConrad Meyer #include <errno.h>
30877a840cSConrad Meyer #include <stdio.h>
31877a840cSConrad Meyer #include <stdlib.h>
32877a840cSConrad Meyer 
33877a840cSConrad Meyer #include "local.h"
34877a840cSConrad Meyer 
35877a840cSConrad Meyer struct fopencookie_thunk {
36877a840cSConrad Meyer 	void			*foc_cookie;
37877a840cSConrad Meyer 	cookie_io_functions_t	foc_io;
38877a840cSConrad Meyer };
39877a840cSConrad Meyer 
40877a840cSConrad Meyer static int _fopencookie_read(void *, char *, int);
41877a840cSConrad Meyer static int _fopencookie_write(void *, const char *, int);
42877a840cSConrad Meyer static fpos_t _fopencookie_seek(void *, fpos_t, int);
43877a840cSConrad Meyer static int _fopencookie_close(void *);
44877a840cSConrad Meyer 
45877a840cSConrad Meyer FILE *
fopencookie(void * cookie,const char * mode,cookie_io_functions_t io_funcs)46877a840cSConrad Meyer fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs)
47877a840cSConrad Meyer {
48877a840cSConrad Meyer 	int (*readfn)(void *, char *, int);
49877a840cSConrad Meyer 	int (*writefn)(void *, const char *, int);
50877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
51877a840cSConrad Meyer 	FILE *fp;
52877a840cSConrad Meyer 	int flags, oflags;
53877a840cSConrad Meyer 
54877a840cSConrad Meyer 	if ((flags = __sflags(mode, &oflags)) == 0)
55877a840cSConrad Meyer 		return (NULL);
56877a840cSConrad Meyer 
57877a840cSConrad Meyer 	thunk = malloc(sizeof(*thunk));
58877a840cSConrad Meyer 	if (thunk == NULL)
59877a840cSConrad Meyer 		return (NULL);
60877a840cSConrad Meyer 
61877a840cSConrad Meyer 	thunk->foc_cookie = cookie;
62877a840cSConrad Meyer 	thunk->foc_io = io_funcs;
63877a840cSConrad Meyer 
64877a840cSConrad Meyer 	readfn = _fopencookie_read;
65877a840cSConrad Meyer 	writefn = _fopencookie_write;
66877a840cSConrad Meyer 	if (flags == __SWR)
67877a840cSConrad Meyer 		readfn = NULL;
68877a840cSConrad Meyer 	else if (flags == __SRD)
69877a840cSConrad Meyer 		writefn = NULL;
70877a840cSConrad Meyer 
71877a840cSConrad Meyer 	fp = funopen(thunk, readfn, writefn, _fopencookie_seek,
72877a840cSConrad Meyer 	    _fopencookie_close);
73877a840cSConrad Meyer 	if (fp == NULL) {
74877a840cSConrad Meyer 		free(thunk);
75877a840cSConrad Meyer 		return (NULL);
76877a840cSConrad Meyer 	}
77877a840cSConrad Meyer 
78877a840cSConrad Meyer 	if ((oflags & O_APPEND) != 0)
79877a840cSConrad Meyer 		fp->_flags |= __SAPP;
80877a840cSConrad Meyer 
81877a840cSConrad Meyer 	return (fp);
82877a840cSConrad Meyer }
83877a840cSConrad Meyer 
84877a840cSConrad Meyer static int
_fopencookie_read(void * cookie,char * buf,int size)85877a840cSConrad Meyer _fopencookie_read(void *cookie, char *buf, int size)
86877a840cSConrad Meyer {
87877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
88877a840cSConrad Meyer 
89877a840cSConrad Meyer 	thunk = cookie;
90877a840cSConrad Meyer 
91877a840cSConrad Meyer 	/* Reads from a stream with NULL read return EOF. */
92877a840cSConrad Meyer 	if (thunk->foc_io.read == NULL)
93877a840cSConrad Meyer 		return (0);
94877a840cSConrad Meyer 
95877a840cSConrad Meyer 	return ((int)thunk->foc_io.read(thunk->foc_cookie, buf, (size_t)size));
96877a840cSConrad Meyer }
97877a840cSConrad Meyer 
98877a840cSConrad Meyer static int
_fopencookie_write(void * cookie,const char * buf,int size)99877a840cSConrad Meyer _fopencookie_write(void *cookie, const char *buf, int size)
100877a840cSConrad Meyer {
101877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
102877a840cSConrad Meyer 
103877a840cSConrad Meyer 	thunk = cookie;
104877a840cSConrad Meyer 
105877a840cSConrad Meyer 	/* Writes to a stream with NULL write discard data. */
106877a840cSConrad Meyer 	if (thunk->foc_io.write == NULL)
107877a840cSConrad Meyer 		return (size);
108877a840cSConrad Meyer 
109877a840cSConrad Meyer 	return ((int)thunk->foc_io.write(thunk->foc_cookie, buf,
110877a840cSConrad Meyer 		(size_t)size));
111877a840cSConrad Meyer }
112877a840cSConrad Meyer 
113877a840cSConrad Meyer static fpos_t
_fopencookie_seek(void * cookie,fpos_t offset,int whence)114877a840cSConrad Meyer _fopencookie_seek(void *cookie, fpos_t offset, int whence)
115877a840cSConrad Meyer {
116877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
117877a840cSConrad Meyer 	off64_t off64;
118877a840cSConrad Meyer 	int res;
119877a840cSConrad Meyer 
120877a840cSConrad Meyer 	switch (whence) {
121877a840cSConrad Meyer 	case SEEK_SET:
122877a840cSConrad Meyer 	case SEEK_CUR:
123877a840cSConrad Meyer 	case SEEK_END:
124877a840cSConrad Meyer 		break;
125877a840cSConrad Meyer 	default:
126877a840cSConrad Meyer 		/* fopencookie(3) only allows these three seek modes. */
127877a840cSConrad Meyer 		errno = EINVAL;
128877a840cSConrad Meyer 		return (-1);
129877a840cSConrad Meyer 	}
130877a840cSConrad Meyer 
131877a840cSConrad Meyer 	thunk = cookie;
132877a840cSConrad Meyer 
133877a840cSConrad Meyer 	/*
134877a840cSConrad Meyer 	 * If seek is NULL, it is not possible to perform seek operations on
135877a840cSConrad Meyer 	 * the stream.
136877a840cSConrad Meyer 	 */
137877a840cSConrad Meyer 	if (thunk->foc_io.seek == NULL) {
138877a840cSConrad Meyer 		errno = ENOTSUP;
139877a840cSConrad Meyer 		return (-1);
140877a840cSConrad Meyer 	}
141877a840cSConrad Meyer 
142877a840cSConrad Meyer 	off64 = (off64_t)offset;
143877a840cSConrad Meyer 	res = thunk->foc_io.seek(thunk->foc_cookie, &off64, whence);
144877a840cSConrad Meyer 	if (res < 0)
145877a840cSConrad Meyer 		return (res);
146877a840cSConrad Meyer 
147877a840cSConrad Meyer 	return ((fpos_t)off64);
148877a840cSConrad Meyer }
149877a840cSConrad Meyer 
150877a840cSConrad Meyer static int
_fopencookie_close(void * cookie)151877a840cSConrad Meyer _fopencookie_close(void *cookie)
152877a840cSConrad Meyer {
153877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
154877a840cSConrad Meyer 	int ret, serrno;
155877a840cSConrad Meyer 
156877a840cSConrad Meyer 	ret = 0;
157877a840cSConrad Meyer 	thunk = cookie;
158877a840cSConrad Meyer 	if (thunk->foc_io.close != NULL)
159877a840cSConrad Meyer 		ret = thunk->foc_io.close(thunk->foc_cookie);
160877a840cSConrad Meyer 
161877a840cSConrad Meyer 	serrno = errno;
162877a840cSConrad Meyer 	free(thunk);
163877a840cSConrad Meyer 	errno = serrno;
164877a840cSConrad Meyer 	return (ret);
165877a840cSConrad Meyer }
166