1 /* funopen.c - Replacement for funopen.
2  * Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3  *
4  * This file is part of Assuan.
5  *
6  * Assuan is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * Assuan is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 
27 
28 /* Replacement for the *BSD function:
29 
30   FILE *funopen (void *cookie,
31                  int (*readfn)(void *, char *, int),
32                  int (*writefn)(void *, const char *, int),
33                  fpos_t (*seekfn)(void *, fpos_t, int),
34                  int (*closefn)(void *));
35 
36   The functions to provide my either be NULL if not required or
37   similar to the unistd function with the exception of using the
38   cookie instead of the file descriptor.
39 */
40 
41 
42 #ifdef HAVE_FOPENCOOKIE
43 FILE *
_assuan_funopen(void * cookie,cookie_read_function_t * readfn,cookie_write_function_t * writefn,cookie_seek_function_t * seekfn,cookie_close_function_t * closefn)44 _assuan_funopen(void *cookie,
45                 cookie_read_function_t *readfn,
46                 cookie_write_function_t *writefn,
47                 cookie_seek_function_t *seekfn,
48                 cookie_close_function_t *closefn)
49 {
50   cookie_io_functions_t io;
51 
52   io.read = readfn;
53   io.write = writefn;
54   io.seek = seekfn;
55   io.close = closefn;
56 
57   return fopencookie (cookie,
58 		      readfn ? ( writefn ? "rw" : "r" )
59 		      : ( writefn ? "w" : ""), io);
60 }
61 #else
62 #error No known way to implement funopen.
63 #endif
64