vdprintf.c 1.5 KB

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