1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
13 #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
14 #error no custom streams!
17 /* NOTE: GLIBC difference!!! -- fopencookie
18 * According to the info pages, glibc allows seeking within buffers even if
19 * no seek function is supplied. We don't. */
21 /* NOTE: GLIBC difference!!! -- fopencookie
22 * When compiled without large file support, the offset pointer for the
23 * cookie_seek function is off_t * and not off64_t * as for glibc. */
25 /* NOTE: GLIBC difference!!! -- fopencookie (bcc only)
26 * Since bcc doesn't support passing of structs, we define fopencookie as a
27 * macro in terms of _fopencookie which takes a struct * for the io functions
31 /* Currently no real reentrancy issues other than a possible double close(). */
34 FILE *fopencookie(void * __restrict cookie
, const char * __restrict mode
,
35 cookie_io_functions_t io_functions
)
37 FILE *_fopencookie(void * __restrict cookie
, const char * __restrict mode
,
38 register cookie_io_functions_t
*io_functions
)
42 _IO_cookie_file_t
*new_f
;
44 new_f
= malloc(sizeof(_IO_cookie_file_t
));
48 new_f
->__fp
.__modeflags
= __FLAG_FREEFILE
;
49 #ifdef __STDIO_BUFFERS
50 new_f
->__fp
.__bufstart
= NULL
; /* We allocate a buffer below. */
52 #ifdef __UCLIBC_HAS_THREADS__
53 /* We only initialize the mutex in the non-freopen case. */
54 STDIO_INIT_MUTEX(new_f
->__fp
.__lock
);
56 /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
57 * check without an fcntl call. */
58 stream
= _stdio_fopen(((intptr_t)(INT_MAX
-1)), mode
, &new_f
->__fp
, INT_MAX
);
60 stream
->__filedes
= __STDIO_STREAM_GLIBC_CUSTOM_FILEDES
;
62 new_f
->__gcs
= io_functions
;
64 new_f
->__gcs
.read
= io_functions
->read
;
65 new_f
->__gcs
.write
= io_functions
->write
;
66 new_f
->__gcs
.seek
= io_functions
->seek
;
67 new_f
->__gcs
.close
= io_functions
->close
;
69 new_f
->__cookie
= cookie
;
71 __STDIO_STREAM_VALIDATE(stream
);
77 libc_hidden_def(fopencookie
)