obprintf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Print output of stream to given obstack.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdlib.h>
  17. #include "libioP.h"
  18. #include "strfile.h"
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <obstack.h>
  23. #include <stdarg.h>
  24. #include <stdio_ext.h>
  25. struct _IO_obstack_file
  26. {
  27. struct _IO_FILE_plus file;
  28. struct obstack *obstack;
  29. };
  30. static int
  31. _IO_obstack_overflow (FILE *fp, int c)
  32. {
  33. struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
  34. int size;
  35. /* Make room for another character. This might as well allocate a
  36. new chunk a memory and moves the old contents over. */
  37. assert (c != EOF);
  38. obstack_1grow (obstack, c);
  39. /* Setup the buffer pointers again. */
  40. fp->_IO_write_base = obstack_base (obstack);
  41. fp->_IO_write_ptr = obstack_next_free (obstack);
  42. size = obstack_room (obstack);
  43. fp->_IO_write_end = fp->_IO_write_ptr + size;
  44. /* Now allocate the rest of the current chunk. */
  45. obstack_blank_fast (obstack, size);
  46. return c;
  47. }
  48. static size_t
  49. _IO_obstack_xsputn (FILE *fp, const void *data, size_t n)
  50. {
  51. struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
  52. if (fp->_IO_write_ptr + n > fp->_IO_write_end)
  53. {
  54. int size;
  55. /* We need some more memory. First shrink the buffer to the
  56. space we really currently need. */
  57. obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
  58. /* Now grow for N bytes, and put the data there. */
  59. obstack_grow (obstack, data, n);
  60. /* Setup the buffer pointers again. */
  61. fp->_IO_write_base = obstack_base (obstack);
  62. fp->_IO_write_ptr = obstack_next_free (obstack);
  63. size = obstack_room (obstack);
  64. fp->_IO_write_end = fp->_IO_write_ptr + size;
  65. /* Now allocate the rest of the current chunk. */
  66. obstack_blank_fast (obstack, size);
  67. }
  68. else
  69. fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
  70. return n;
  71. }
  72. /* the jump table. */
  73. const struct _IO_jump_t _IO_obstack_jumps libio_vtable attribute_hidden =
  74. {
  75. JUMP_INIT_DUMMY,
  76. JUMP_INIT(finish, NULL),
  77. JUMP_INIT(overflow, _IO_obstack_overflow),
  78. JUMP_INIT(underflow, NULL),
  79. JUMP_INIT(uflow, NULL),
  80. JUMP_INIT(pbackfail, NULL),
  81. JUMP_INIT(xsputn, _IO_obstack_xsputn),
  82. JUMP_INIT(xsgetn, NULL),
  83. JUMP_INIT(seekoff, NULL),
  84. JUMP_INIT(seekpos, NULL),
  85. JUMP_INIT(setbuf, NULL),
  86. JUMP_INIT(sync, NULL),
  87. JUMP_INIT(doallocate, NULL),
  88. JUMP_INIT(read, NULL),
  89. JUMP_INIT(write, NULL),
  90. JUMP_INIT(seek, NULL),
  91. JUMP_INIT(close, NULL),
  92. JUMP_INIT(stat, NULL),
  93. JUMP_INIT(showmanyc, NULL),
  94. JUMP_INIT(imbue, NULL)
  95. };
  96. int
  97. __obstack_vprintf_internal (struct obstack *obstack, const char *format,
  98. va_list args, unsigned int mode_flags)
  99. {
  100. struct obstack_FILE
  101. {
  102. struct _IO_obstack_file ofile;
  103. } new_f;
  104. int result;
  105. int size;
  106. int room;
  107. #ifdef _IO_MTSAFE_IO
  108. new_f.ofile.file.file._lock = NULL;
  109. #endif
  110. _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
  111. _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
  112. room = obstack_room (obstack);
  113. size = obstack_object_size (obstack) + room;
  114. if (size == 0)
  115. {
  116. /* We have to handle the allocation a bit different since the
  117. `_IO_str_init_static' function would handle a size of zero
  118. different from what we expect. */
  119. /* Get more memory. */
  120. obstack_make_room (obstack, 64);
  121. /* Recompute how much room we have. */
  122. room = obstack_room (obstack);
  123. size = room;
  124. assert (size != 0);
  125. }
  126. _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
  127. obstack_base (obstack),
  128. size, obstack_next_free (obstack));
  129. /* Now allocate the rest of the current chunk. */
  130. assert (size == (new_f.ofile.file.file._IO_write_end
  131. - new_f.ofile.file.file._IO_write_base));
  132. assert (new_f.ofile.file.file._IO_write_ptr
  133. == (new_f.ofile.file.file._IO_write_base
  134. + obstack_object_size (obstack)));
  135. obstack_blank_fast (obstack, room);
  136. new_f.ofile.obstack = obstack;
  137. result = __vfprintf_internal (&new_f.ofile.file.file, format, args,
  138. mode_flags);
  139. /* Shrink the buffer to the space we really currently need. */
  140. obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
  141. - new_f.ofile.file.file._IO_write_end));
  142. return result;
  143. }
  144. int
  145. __obstack_vprintf (struct obstack *obstack, const char *format, va_list ap)
  146. {
  147. return __obstack_vprintf_internal (obstack, format, ap, 0);
  148. }
  149. ldbl_weak_alias (__obstack_vprintf, obstack_vprintf)
  150. int
  151. __obstack_printf (struct obstack *obstack, const char *format, ...)
  152. {
  153. int result;
  154. va_list ap;
  155. va_start (ap, format);
  156. result = __obstack_vprintf_internal (obstack, format, ap, 0);
  157. va_end (ap);
  158. return result;
  159. }
  160. ldbl_weak_alias (__obstack_printf, obstack_printf)