vdprintf.c 1.9 KB

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