vdprintf.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /* __STDIO_STREAM_RESET_GCS(&f); */
  24. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  25. f.__cookie = &(f.__filedes);
  26. f.__gcs.read = NULL;
  27. f.__gcs.write = _cs_write;
  28. f.__gcs.seek = NULL;
  29. f.__gcs.close = NULL;
  30. #endif
  31. f.__filedes = filedes;
  32. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  33. #ifdef __UCLIBC_HAS_WCHAR__
  34. f.__ungot_width[0] = 0;
  35. #endif
  36. #ifdef __STDIO_MBSTATE
  37. __INIT_MBSTATE(&(f.__state));
  38. #endif
  39. /* _vfprintf_internal doesn't do any locking, locking init is here
  40. * only because of fflush_unlocked. TODO? */
  41. #if (defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__)) && defined(__UCLIBC_HAS_THREADS__)
  42. f.__user_locking = 1; /* Set user locking. */
  43. STDIO_INIT_MUTEX(f.__lock);
  44. #endif
  45. f.__nextopen = NULL;
  46. #ifdef __USE_OLD_VFPRINTF__
  47. rv = vfprintf(&f, format, arg);
  48. #else
  49. rv = _vfprintf_internal(&f, format, arg);
  50. #endif
  51. #ifdef __STDIO_BUFFERS
  52. /* If not buffering, then fflush is unnecessary. */
  53. if ((rv > 0) && fflush_unlocked(&f)) {
  54. rv = -1;
  55. }
  56. #endif
  57. assert(rv >= -1);
  58. return rv;
  59. }
  60. libc_hidden_def(vdprintf)
  61. #endif