fopencookie.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <features.h>
  8. #ifdef __USE_GNU
  9. #include "_stdio.h"
  10. #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  11. #error no custom streams!
  12. #endif
  13. /* NOTE: GLIBC difference!!! -- fopencookie
  14. * According to the info pages, glibc allows seeking within buffers even if
  15. * no seek function is supplied. We don't. */
  16. /* NOTE: GLIBC difference!!! -- fopencookie
  17. * When compiled without large file support, the offset pointer for the
  18. * cookie_seek function is off_t * and not off64_t * as for glibc. */
  19. /* NOTE: GLIBC difference!!! -- fopencookie (bcc only)
  20. * Since bcc doesn't support passing of structs, we define fopencookie as a
  21. * macro in terms of _fopencookie which takes a struct * for the io functions
  22. * instead.
  23. */
  24. /* Currently no real reentrancy issues other than a possible double close(). */
  25. #ifndef __BCC__
  26. FILE *fopencookie(void * __restrict cookie, const char * __restrict mode,
  27. cookie_io_functions_t io_functions)
  28. #else
  29. FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
  30. register cookie_io_functions_t *io_functions)
  31. #endif
  32. {
  33. FILE *stream;
  34. /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
  35. * check without an fcntl call. */
  36. stream = _stdio_fopen(((intptr_t)(INT_MAX-1)), mode, NULL, INT_MAX);
  37. if (stream) {
  38. stream->__filedes = -1;
  39. #ifndef __BCC__
  40. stream->__gcs = io_functions;
  41. #else
  42. stream->__gcs.read = io_functions->read;
  43. stream->__gcs.write = io_functions->write;
  44. stream->__gcs.seek = io_functions->seek;
  45. stream->__gcs.close = io_functions->close;
  46. #endif
  47. stream->__cookie = cookie;
  48. __STDIO_STREAM_VALIDATE(stream);
  49. }
  50. return stream;
  51. }
  52. #ifndef __BCC__
  53. libc_hidden_def(fopencookie)
  54. #endif
  55. #endif