malloc: add glibc compat symbols
[uclibc-ng.git] / libc / stdio / fopencookie.c
blob216dac39d346e7f3b46e7be8febf2048be017829
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.
6 */
8 #include <features.h>
10 #ifdef __USE_GNU
11 #include "_stdio.h"
13 #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
14 #error no custom streams!
15 #endif
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
28 * instead.
31 /* Currently no real reentrancy issues other than a possible double close(). */
33 #ifndef __BCC__
34 FILE *fopencookie(void * __restrict cookie, const char * __restrict mode,
35 cookie_io_functions_t io_functions)
36 #else
37 FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
38 register cookie_io_functions_t *io_functions)
39 #endif
41 FILE *stream;
42 _IO_cookie_file_t *new_f;
44 new_f = malloc(sizeof(_IO_cookie_file_t));
45 if (new_f == NULL) {
46 return NULL;
48 new_f->__fp.__modeflags = __FLAG_FREEFILE;
49 #ifdef __STDIO_BUFFERS
50 new_f->__fp.__bufstart = NULL; /* We allocate a buffer below. */
51 #endif
52 #ifdef __UCLIBC_HAS_THREADS__
53 /* We only initialize the mutex in the non-freopen case. */
54 STDIO_INIT_MUTEX(new_f->__fp.__lock);
55 #endif
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);
59 if (stream) {
60 stream->__filedes = __STDIO_STREAM_GLIBC_CUSTOM_FILEDES;
61 #ifndef __BCC__
62 new_f->__gcs = io_functions;
63 #else
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;
68 #endif
69 new_f->__cookie = cookie;
71 __STDIO_STREAM_VALIDATE(stream);
74 return stream;
76 #ifndef __BCC__
77 libc_hidden_def(fopencookie)
78 #endif
79 #endif