vdprintf.c 1.5 KB

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