vdprintf.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <stdarg.h>
  11. #ifdef __USE_OLD_VFPRINTF__
  12. #endif
  13. int vdprintf(int filedes, const char * __restrict format, va_list arg)
  14. {
  15. FILE f;
  16. int rv;
  17. #ifdef __STDIO_BUFFERS
  18. char buf[64]; /* TODO: provide _optional_ buffering? */
  19. f.__bufend = (unsigned char *) buf + sizeof(buf);
  20. f.__bufstart = (unsigned char *) buf;
  21. __STDIO_STREAM_DISABLE_GETC(&f);
  22. __STDIO_STREAM_DISABLE_PUTC(&f);
  23. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  24. #endif
  25. /* __STDIO_STREAM_RESET_GCS(&f); */
  26. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  27. f.__cookie = &(f.__filedes);
  28. f.__gcs.read = NULL;
  29. f.__gcs.write = _cs_write;
  30. f.__gcs.seek = NULL;
  31. f.__gcs.close = NULL;
  32. #endif
  33. f.__filedes = filedes;
  34. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  35. #ifdef __UCLIBC_HAS_WCHAR__
  36. f.__ungot_width[0] = 0;
  37. #endif /* __UCLIBC_HAS_WCHAR__ */
  38. #ifdef __STDIO_MBSTATE
  39. __INIT_MBSTATE(&(f.__state));
  40. #endif /* __STDIO_MBSTATE */
  41. /* _vfprintf_internal doesn't do any locking, locking init is here
  42. * only because of fflush_unlocked. TODO? */
  43. #if (defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__)) && defined(__UCLIBC_HAS_THREADS__)
  44. f.__user_locking = 1; /* Set user locking. */
  45. __stdio_init_mutex(&f.__lock);
  46. #endif
  47. f.__nextopen = NULL;
  48. #ifdef __USE_OLD_VFPRINTF__
  49. rv = vfprintf(&f, format, arg);
  50. #else
  51. rv = _vfprintf_internal(&f, format, arg);
  52. #endif
  53. #ifdef __STDIO_BUFFERS
  54. /* If not buffering, then fflush is unnecessary. */
  55. if ((rv > 0) && fflush_unlocked(&f)) {
  56. rv = -1;
  57. }
  58. #endif
  59. assert(rv >= -1);
  60. return rv;
  61. }
  62. libc_hidden_def(vdprintf)
  63. #endif