vdprintf.c 1.5 KB

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