fopencookie.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. libc_hidden_proto(fopencookie)
  25. FILE *fopencookie(void * __restrict cookie, const char * __restrict mode,
  26. cookie_io_functions_t io_functions)
  27. #else
  28. FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
  29. register cookie_io_functions_t *io_functions)
  30. #endif
  31. {
  32. FILE *stream;
  33. /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
  34. * check without an fcntl call. */
  35. stream = _stdio_fopen(((intptr_t)(INT_MAX-1)), mode, NULL, INT_MAX);
  36. if (stream) {
  37. stream->__filedes = -1;
  38. #ifndef __BCC__
  39. stream->__gcs = io_functions;
  40. #else
  41. stream->__gcs.read = io_functions->read;
  42. stream->__gcs.write = io_functions->write;
  43. stream->__gcs.seek = io_functions->seek;
  44. stream->__gcs.close = io_functions->close;
  45. #endif
  46. stream->__cookie = cookie;
  47. __STDIO_STREAM_VALIDATE(stream);
  48. }
  49. return stream;
  50. }
  51. #ifndef __BCC__
  52. libc_hidden_def(fopencookie)
  53. #endif