fopencookie.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  9. #error no custom streams!
  10. #endif
  11. /* NOTE: GLIBC difference!!! -- fopencookie
  12. * According to the info pages, glibc allows seeking within buffers even if
  13. * no seek function is supplied. We don't. */
  14. /* NOTE: GLIBC difference!!! -- fopencookie
  15. * When compiled without large file support, the offset pointer for the
  16. * cookie_seek function is off_t * and not off64_t * as for glibc. */
  17. /* NOTE: GLIBC difference!!! -- fopencookie (bcc only)
  18. * Since bcc doesn't support passing of structs, we define fopencookie as a
  19. * macro in terms of _fopencookie which takes a struct * for the io functions
  20. * instead.
  21. */
  22. /* Currently no real reentrancy issues other than a possible double close(). */
  23. #ifndef __BCC__
  24. FILE attribute_hidden *__fopencookie(void * __restrict cookie, const char * __restrict mode,
  25. cookie_io_functions_t io_functions)
  26. #else
  27. FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
  28. register cookie_io_functions_t *io_functions)
  29. #endif
  30. {
  31. FILE *stream;
  32. /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
  33. * check without an fcntl call. */
  34. stream = _stdio_fopen(((intptr_t)(INT_MAX-1)), mode, NULL, INT_MAX);
  35. if (stream) {
  36. stream->__filedes = -1;
  37. #ifndef __BCC__
  38. stream->__gcs = io_functions;
  39. #else
  40. stream->__gcs.read = io_functions->read;
  41. stream->__gcs.write = io_functions->write;
  42. stream->__gcs.seek = io_functions->seek;
  43. stream->__gcs.close = io_functions->close;
  44. #endif
  45. stream->__cookie = cookie;
  46. __STDIO_STREAM_VALIDATE(stream);
  47. }
  48. return stream;
  49. }
  50. #ifndef __BCC__
  51. strong_alias(__fopencookie,fopencookie)
  52. #endif