1 /* Copyright (C) 2007 Eric Blake
2 * Permission to use, copy, modify, and distribute this software
3 * is freely granted, provided that this notice is preserved.
8 <<fopencookie>>---open a stream with custom callbacks
15 FILE *fopencookie(const void *<[cookie]>, const char *<[mode]>,
16 cookie_io_functions_t <[functions]>);
19 <<fopencookie>> creates a <<FILE>> stream where I/O is performed using
20 custom callbacks. The callbacks are registered via the structure:
22 typedef ssize_t (*cookie_read_function_t)(void *_cookie, char *_buf,
24 typedef ssize_t (*cookie_write_function_t)(void *_cookie,
25 const char *_buf, size_t _n);
26 typedef int (*cookie_seek_function_t)(void *_cookie, off_t *_off,
28 typedef int (*cookie_close_function_t)(void *_cookie);
32 . cookie_read_function_t *read;
33 . cookie_write_function_t *write;
34 . cookie_seek_function_t *seek;
35 . cookie_close_function_t *close;
36 . } cookie_io_functions_t;
38 The stream is opened with <[mode]> treated as in <<fopen>>. The
39 callbacks <[functions.read]> and <[functions.write]> may only be NULL
40 when <[mode]> does not require them.
42 <[functions.read]> should return -1 on failure, or else the number of
43 bytes read (0 on EOF). It is similar to <<read>>, except that
44 <[cookie]> will be passed as the first argument.
46 <[functions.write]> should return -1 on failure, or else the number of
47 bytes written. It is similar to <<write>>, except that <[cookie]>
48 will be passed as the first argument.
50 <[functions.seek]> should return -1 on failure, and 0 on success, with
51 *<[_off]> set to the current file position. It is a cross between
52 <<lseek>> and <<fseek>>, with the <[_whence]> argument interpreted in
53 the same manner. A NULL <[functions.seek]> makes the stream behave
54 similarly to a pipe in relation to stdio functions that require
57 <[functions.close]> should return -1 on failure, or 0 on success. It
58 is similar to <<close>>, except that <[cookie]> will be passed as the
59 first argument. A NULL <[functions.close]> merely flushes all data
60 then lets <<fclose>> succeed. A failed close will still invalidate
63 Read and write I/O functions are allowed to change the underlying
64 buffer on fully buffered or line buffered streams by calling
65 <<setvbuf>>. They are also not required to completely fill or empty
66 the buffer. They are not, however, allowed to change streams from
67 unbuffered to buffered or to change the state of the line buffering
68 flag. They must also be prepared to have read or write calls occur on
69 buffers other than the one most recently specified.
72 The return value is an open FILE pointer on success. On error,
73 <<NULL>> is returned, and <<errno>> will be set to EINVAL if a
74 function pointer is missing or <[mode]> is invalid, ENOMEM if the
75 stream cannot be created, or EMFILE if too many streams are already
79 This function is a newlib extension, copying the prototype from Linux.
80 It is not portable. See also the <<funopen>> interface from BSD.
82 Supporting OS subroutines required: <<sbrk>>.
91 typedef struct fccookie
{
94 cookie_read_function_t
*readfn
;
95 cookie_write_function_t
*writefn
;
96 cookie_seek_function_t
*seekfn
;
97 cookie_close_function_t
*closefn
;
100 static _READ_WRITE_RETURN_TYPE
101 fcreader (struct _reent
*ptr
,
104 _READ_WRITE_BUFSIZE_TYPE n
)
107 fccookie
*c
= (fccookie
*) cookie
;
109 if ((result
= c
->readfn (c
->cookie
, buf
, n
)) < 0 && errno
)
110 _REENT_ERRNO(ptr
) = errno
;
114 static _READ_WRITE_RETURN_TYPE
115 fcwriter (struct _reent
*ptr
,
118 _READ_WRITE_BUFSIZE_TYPE n
)
121 fccookie
*c
= (fccookie
*) cookie
;
122 if (c
->fp
->_flags
& __SAPP
&& c
->fp
->_seek
)
124 #ifdef __LARGE64_FILES
125 c
->fp
->_seek64 (ptr
, cookie
, 0, SEEK_END
);
127 c
->fp
->_seek (ptr
, cookie
, 0, SEEK_END
);
131 if ((result
= c
->writefn (c
->cookie
, buf
, n
)) < 0 && errno
)
132 _REENT_ERRNO(ptr
) = errno
;
137 fcseeker (struct _reent
*ptr
,
142 fccookie
*c
= (fccookie
*) cookie
;
143 #ifndef __LARGE64_FILES
144 off_t offset
= (off_t
) pos
;
145 #else /* __LARGE64_FILES */
146 _off64_t offset
= (_off64_t
) pos
;
147 #endif /* __LARGE64_FILES */
150 if (c
->seekfn (c
->cookie
, &offset
, whence
) < 0 && errno
)
151 _REENT_ERRNO(ptr
) = errno
;
152 #ifdef __LARGE64_FILES
153 else if ((_fpos_t
)offset
!= offset
)
155 _REENT_ERRNO(ptr
) = EOVERFLOW
;
158 #endif /* __LARGE64_FILES */
159 return (_fpos_t
) offset
;
162 #ifdef __LARGE64_FILES
164 fcseeker64 (struct _reent
*ptr
,
170 fccookie
*c
= (fccookie
*) cookie
;
172 if (c
->seekfn (c
->cookie
, &offset
, whence
) < 0 && errno
)
173 _REENT_ERRNO(ptr
) = errno
;
174 return (_fpos64_t
) offset
;
176 #endif /* __LARGE64_FILES */
179 fccloser (struct _reent
*ptr
,
183 fccookie
*c
= (fccookie
*) cookie
;
187 if ((result
= c
->closefn (c
->cookie
)) < 0 && errno
)
188 _REENT_ERRNO(ptr
) = errno
;
195 _fopencookie_r (struct _reent
*ptr
,
198 cookie_io_functions_t functions
)
205 if ((flags
= __sflags (ptr
, mode
, &dummy
)) == 0)
207 if (((flags
& (__SRD
| __SRW
)) && !functions
.read
)
208 || ((flags
& (__SWR
| __SRW
)) && !functions
.write
))
210 _REENT_ERRNO(ptr
) = EINVAL
;
213 if ((fp
= __sfp (ptr
)) == NULL
)
215 if ((c
= (fccookie
*) _malloc_r (ptr
, sizeof *c
)) == NULL
)
217 _newlib_sfp_lock_start ();
218 fp
->_flags
= 0; /* release */
219 #ifndef __SINGLE_THREAD__
220 __lock_close_recursive (fp
->_lock
);
222 _newlib_sfp_lock_end ();
226 _newlib_flockfile_start (fp
);
232 c
->readfn
= functions
.read
;
233 fp
->_read
= fcreader
;
234 c
->writefn
= functions
.write
;
235 fp
->_write
= fcwriter
;
236 c
->seekfn
= functions
.seek
;
237 fp
->_seek
= functions
.seek
? fcseeker
: NULL
;
238 #ifdef __LARGE64_FILES
239 fp
->_seek64
= functions
.seek
? fcseeker64
: NULL
;
240 fp
->_flags
|= __SL64
;
242 c
->closefn
= functions
.close
;
243 fp
->_close
= fccloser
;
244 _newlib_flockfile_end (fp
);
250 fopencookie (void *cookie
,
252 cookie_io_functions_t functions
)
254 return _fopencookie_r (_REENT
, cookie
, mode
, functions
);
256 #endif /* !_REENT_ONLY */