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