xref: /freebsd/lib/libc/stdio/fopencookie.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/fcntl.h>
31 
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "local.h"
37 
38 struct fopencookie_thunk {
39 	void			*foc_cookie;
40 	cookie_io_functions_t	foc_io;
41 };
42 
43 static int _fopencookie_read(void *, char *, int);
44 static int _fopencookie_write(void *, const char *, int);
45 static fpos_t _fopencookie_seek(void *, fpos_t, int);
46 static int _fopencookie_close(void *);
47 
48 FILE *
49 fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs)
50 {
51 	int (*readfn)(void *, char *, int);
52 	int (*writefn)(void *, const char *, int);
53 	struct fopencookie_thunk *thunk;
54 	FILE *fp;
55 	int flags, oflags;
56 
57 	if ((flags = __sflags(mode, &oflags)) == 0)
58 		return (NULL);
59 
60 	thunk = malloc(sizeof(*thunk));
61 	if (thunk == NULL)
62 		return (NULL);
63 
64 	thunk->foc_cookie = cookie;
65 	thunk->foc_io = io_funcs;
66 
67 	readfn = _fopencookie_read;
68 	writefn = _fopencookie_write;
69 	if (flags == __SWR)
70 		readfn = NULL;
71 	else if (flags == __SRD)
72 		writefn = NULL;
73 
74 	fp = funopen(thunk, readfn, writefn, _fopencookie_seek,
75 	    _fopencookie_close);
76 	if (fp == NULL) {
77 		free(thunk);
78 		return (NULL);
79 	}
80 
81 	if ((oflags & O_APPEND) != 0)
82 		fp->_flags |= __SAPP;
83 
84 	return (fp);
85 }
86 
87 static int
88 _fopencookie_read(void *cookie, char *buf, int size)
89 {
90 	struct fopencookie_thunk *thunk;
91 
92 	thunk = cookie;
93 
94 	/* Reads from a stream with NULL read return EOF. */
95 	if (thunk->foc_io.read == NULL)
96 		return (0);
97 
98 	return ((int)thunk->foc_io.read(thunk->foc_cookie, buf, (size_t)size));
99 }
100 
101 static int
102 _fopencookie_write(void *cookie, const char *buf, int size)
103 {
104 	struct fopencookie_thunk *thunk;
105 
106 	thunk = cookie;
107 
108 	/* Writes to a stream with NULL write discard data. */
109 	if (thunk->foc_io.write == NULL)
110 		return (size);
111 
112 	return ((int)thunk->foc_io.write(thunk->foc_cookie, buf,
113 		(size_t)size));
114 }
115 
116 static fpos_t
117 _fopencookie_seek(void *cookie, fpos_t offset, int whence)
118 {
119 	struct fopencookie_thunk *thunk;
120 	off64_t off64;
121 	int res;
122 
123 	switch (whence) {
124 	case SEEK_SET:
125 	case SEEK_CUR:
126 	case SEEK_END:
127 		break;
128 	default:
129 		/* fopencookie(3) only allows these three seek modes. */
130 		errno = EINVAL;
131 		return (-1);
132 	}
133 
134 	thunk = cookie;
135 
136 	/*
137 	 * If seek is NULL, it is not possible to perform seek operations on
138 	 * the stream.
139 	 */
140 	if (thunk->foc_io.seek == NULL) {
141 		errno = ENOTSUP;
142 		return (-1);
143 	}
144 
145 	off64 = (off64_t)offset;
146 	res = thunk->foc_io.seek(thunk->foc_cookie, &off64, whence);
147 	if (res < 0)
148 		return (res);
149 
150 	return ((fpos_t)off64);
151 }
152 
153 static int
154 _fopencookie_close(void *cookie)
155 {
156 	struct fopencookie_thunk *thunk;
157 	int ret, serrno;
158 
159 	ret = 0;
160 	thunk = cookie;
161 	if (thunk->foc_io.close != NULL)
162 		ret = thunk->foc_io.close(thunk->foc_cookie);
163 
164 	serrno = errno;
165 	free(thunk);
166 	errno = serrno;
167 	return (ret);
168 }
169