fopencookie.c 1.9 KB

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