xref: /freebsd/lib/libc/stdio/fopencookie.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*877a840cSConrad Meyer /*
2*877a840cSConrad Meyer  * Copyright (c) 2016, EMC / Isilon Storage Division
3*877a840cSConrad Meyer  * All rights reserved.
4*877a840cSConrad Meyer  *
5*877a840cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
6*877a840cSConrad Meyer  * modification, are permitted provided that the following conditions
7*877a840cSConrad Meyer  * are met:
8*877a840cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
9*877a840cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
10*877a840cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
11*877a840cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
12*877a840cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
13*877a840cSConrad Meyer  *
14*877a840cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15*877a840cSConrad Meyer  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16*877a840cSConrad Meyer  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17*877a840cSConrad Meyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18*877a840cSConrad Meyer  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19*877a840cSConrad Meyer  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20*877a840cSConrad Meyer  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21*877a840cSConrad Meyer  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22*877a840cSConrad Meyer  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23*877a840cSConrad Meyer  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24*877a840cSConrad Meyer  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*877a840cSConrad Meyer  */
26*877a840cSConrad Meyer 
27*877a840cSConrad Meyer #include <sys/fcntl.h>
28*877a840cSConrad Meyer 
29*877a840cSConrad Meyer #include <errno.h>
30*877a840cSConrad Meyer #include <stdio.h>
31*877a840cSConrad Meyer #include <stdlib.h>
32*877a840cSConrad Meyer 
33*877a840cSConrad Meyer #include "local.h"
34*877a840cSConrad Meyer 
35*877a840cSConrad Meyer struct fopencookie_thunk {
36*877a840cSConrad Meyer 	void			*foc_cookie;
37*877a840cSConrad Meyer 	cookie_io_functions_t	foc_io;
38*877a840cSConrad Meyer };
39*877a840cSConrad Meyer 
40*877a840cSConrad Meyer static int _fopencookie_read(void *, char *, int);
41*877a840cSConrad Meyer static int _fopencookie_write(void *, const char *, int);
42*877a840cSConrad Meyer static fpos_t _fopencookie_seek(void *, fpos_t, int);
43*877a840cSConrad Meyer static int _fopencookie_close(void *);
44*877a840cSConrad Meyer 
45*877a840cSConrad Meyer FILE *
fopencookie(void * cookie,const char * mode,cookie_io_functions_t io_funcs)46*877a840cSConrad Meyer fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs)
47*877a840cSConrad Meyer {
48*877a840cSConrad Meyer 	int (*readfn)(void *, char *, int);
49*877a840cSConrad Meyer 	int (*writefn)(void *, const char *, int);
50*877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
51*877a840cSConrad Meyer 	FILE *fp;
52*877a840cSConrad Meyer 	int flags, oflags;
53*877a840cSConrad Meyer 
54*877a840cSConrad Meyer 	if ((flags = __sflags(mode, &oflags)) == 0)
55*877a840cSConrad Meyer 		return (NULL);
56*877a840cSConrad Meyer 
57*877a840cSConrad Meyer 	thunk = malloc(sizeof(*thunk));
58*877a840cSConrad Meyer 	if (thunk == NULL)
59*877a840cSConrad Meyer 		return (NULL);
60*877a840cSConrad Meyer 
61*877a840cSConrad Meyer 	thunk->foc_cookie = cookie;
62*877a840cSConrad Meyer 	thunk->foc_io = io_funcs;
63*877a840cSConrad Meyer 
64*877a840cSConrad Meyer 	readfn = _fopencookie_read;
65*877a840cSConrad Meyer 	writefn = _fopencookie_write;
66*877a840cSConrad Meyer 	if (flags == __SWR)
67*877a840cSConrad Meyer 		readfn = NULL;
68*877a840cSConrad Meyer 	else if (flags == __SRD)
69*877a840cSConrad Meyer 		writefn = NULL;
70*877a840cSConrad Meyer 
71*877a840cSConrad Meyer 	fp = funopen(thunk, readfn, writefn, _fopencookie_seek,
72*877a840cSConrad Meyer 	    _fopencookie_close);
73*877a840cSConrad Meyer 	if (fp == NULL) {
74*877a840cSConrad Meyer 		free(thunk);
75*877a840cSConrad Meyer 		return (NULL);
76*877a840cSConrad Meyer 	}
77*877a840cSConrad Meyer 
78*877a840cSConrad Meyer 	if ((oflags & O_APPEND) != 0)
79*877a840cSConrad Meyer 		fp->_flags |= __SAPP;
80*877a840cSConrad Meyer 
81*877a840cSConrad Meyer 	return (fp);
82*877a840cSConrad Meyer }
83*877a840cSConrad Meyer 
84*877a840cSConrad Meyer static int
_fopencookie_read(void * cookie,char * buf,int size)85*877a840cSConrad Meyer _fopencookie_read(void *cookie, char *buf, int size)
86*877a840cSConrad Meyer {
87*877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
88*877a840cSConrad Meyer 
89*877a840cSConrad Meyer 	thunk = cookie;
90*877a840cSConrad Meyer 
91*877a840cSConrad Meyer 	/* Reads from a stream with NULL read return EOF. */
92*877a840cSConrad Meyer 	if (thunk->foc_io.read == NULL)
93*877a840cSConrad Meyer 		return (0);
94*877a840cSConrad Meyer 
95*877a840cSConrad Meyer 	return ((int)thunk->foc_io.read(thunk->foc_cookie, buf, (size_t)size));
96*877a840cSConrad Meyer }
97*877a840cSConrad Meyer 
98*877a840cSConrad Meyer static int
_fopencookie_write(void * cookie,const char * buf,int size)99*877a840cSConrad Meyer _fopencookie_write(void *cookie, const char *buf, int size)
100*877a840cSConrad Meyer {
101*877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
102*877a840cSConrad Meyer 
103*877a840cSConrad Meyer 	thunk = cookie;
104*877a840cSConrad Meyer 
105*877a840cSConrad Meyer 	/* Writes to a stream with NULL write discard data. */
106*877a840cSConrad Meyer 	if (thunk->foc_io.write == NULL)
107*877a840cSConrad Meyer 		return (size);
108*877a840cSConrad Meyer 
109*877a840cSConrad Meyer 	return ((int)thunk->foc_io.write(thunk->foc_cookie, buf,
110*877a840cSConrad Meyer 		(size_t)size));
111*877a840cSConrad Meyer }
112*877a840cSConrad Meyer 
113*877a840cSConrad Meyer static fpos_t
_fopencookie_seek(void * cookie,fpos_t offset,int whence)114*877a840cSConrad Meyer _fopencookie_seek(void *cookie, fpos_t offset, int whence)
115*877a840cSConrad Meyer {
116*877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
117*877a840cSConrad Meyer 	off64_t off64;
118*877a840cSConrad Meyer 	int res;
119*877a840cSConrad Meyer 
120*877a840cSConrad Meyer 	switch (whence) {
121*877a840cSConrad Meyer 	case SEEK_SET:
122*877a840cSConrad Meyer 	case SEEK_CUR:
123*877a840cSConrad Meyer 	case SEEK_END:
124*877a840cSConrad Meyer 		break;
125*877a840cSConrad Meyer 	default:
126*877a840cSConrad Meyer 		/* fopencookie(3) only allows these three seek modes. */
127*877a840cSConrad Meyer 		errno = EINVAL;
128*877a840cSConrad Meyer 		return (-1);
129*877a840cSConrad Meyer 	}
130*877a840cSConrad Meyer 
131*877a840cSConrad Meyer 	thunk = cookie;
132*877a840cSConrad Meyer 
133*877a840cSConrad Meyer 	/*
134*877a840cSConrad Meyer 	 * If seek is NULL, it is not possible to perform seek operations on
135*877a840cSConrad Meyer 	 * the stream.
136*877a840cSConrad Meyer 	 */
137*877a840cSConrad Meyer 	if (thunk->foc_io.seek == NULL) {
138*877a840cSConrad Meyer 		errno = ENOTSUP;
139*877a840cSConrad Meyer 		return (-1);
140*877a840cSConrad Meyer 	}
141*877a840cSConrad Meyer 
142*877a840cSConrad Meyer 	off64 = (off64_t)offset;
143*877a840cSConrad Meyer 	res = thunk->foc_io.seek(thunk->foc_cookie, &off64, whence);
144*877a840cSConrad Meyer 	if (res < 0)
145*877a840cSConrad Meyer 		return (res);
146*877a840cSConrad Meyer 
147*877a840cSConrad Meyer 	return ((fpos_t)off64);
148*877a840cSConrad Meyer }
149*877a840cSConrad Meyer 
150*877a840cSConrad Meyer static int
_fopencookie_close(void * cookie)151*877a840cSConrad Meyer _fopencookie_close(void *cookie)
152*877a840cSConrad Meyer {
153*877a840cSConrad Meyer 	struct fopencookie_thunk *thunk;
154*877a840cSConrad Meyer 	int ret, serrno;
155*877a840cSConrad Meyer 
156*877a840cSConrad Meyer 	ret = 0;
157*877a840cSConrad Meyer 	thunk = cookie;
158*877a840cSConrad Meyer 	if (thunk->foc_io.close != NULL)
159*877a840cSConrad Meyer 		ret = thunk->foc_io.close(thunk->foc_cookie);
160*877a840cSConrad Meyer 
161*877a840cSConrad Meyer 	serrno = errno;
162*877a840cSConrad Meyer 	free(thunk);
163*877a840cSConrad Meyer 	errno = serrno;
164*877a840cSConrad Meyer 	return (ret);
165*877a840cSConrad Meyer }
166