vasprintf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*###########################################################################
  29. # #
  30. # vasprintf #
  31. # #
  32. # Copyright (c) 2002-2005 David TAILLANDIER #
  33. # #
  34. ###########################################################################*/
  35. /*
  36. This software is distributed under the "modified BSD licence".
  37. This software is also released with GNU license (GPL) in another file (same
  38. source-code, only license differ).
  39. Redistribution and use in source and binary forms, with or without
  40. modification, are permitted provided that the following conditions are met:
  41. Redistributions of source code must retain the above copyright notice, this
  42. list of conditions and the following disclaimer. Redistributions in binary
  43. form must reproduce the above copyright notice, this list of conditions and
  44. the following disclaimer in the documentation and/or other materials
  45. provided with the distribution. The name of the author may not be used to
  46. endorse or promote products derived from this software without specific
  47. prior written permission.
  48. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  49. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  50. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  51. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  53. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  54. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  55. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  56. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  57. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. ====================
  59. Hacked from xnprintf version of 26th February 2005 to provide only
  60. vasprintf by Reuben Thomas <rrt@sc3d.org>.
  61. ====================
  62. 'printf' function family use the following format string:
  63. %[flag][width][.prec][modifier]type
  64. %% is the escape sequence to print a '%'
  65. % followed by an unknown format will print the characters without
  66. trying to do any interpretation
  67. flag: none + - # (blank)
  68. width: n 0n *
  69. prec: none .0 .n .*
  70. modifier: F N L h l ll z t ('F' and 'N' are ms-dos/16-bit specific)
  71. type: d i o u x X f e g E G c s p n
  72. The function needs to allocate memory to store the full text before to
  73. actually writing it. i.e if you want to fnprintf() 1000 characters, the
  74. functions will allocate 1000 bytes.
  75. This behaviour can be modified: you have to customise the code to flush the
  76. internal buffer (writing to screen or file) when it reach a given size. Then
  77. the buffer can have a shorter length. But what? If you really need to write
  78. HUGE string, don't use printf!
  79. During the process, some other memory is allocated (1024 bytes minimum)
  80. to handle the output of partial sprintf() calls. If you have only 10000 bytes
  81. free in memory, you *may* not be able to nprintf() an 8000 bytes-long text.
  82. note: if a buffer overflow occurs, exit() is called. This situation should
  83. never appear ... but if you want to be *really* sure, you have to modify the
  84. code to handle those situations (only one place to modify).
  85. A buffer overflow can only occur if your sprintf() do strange things or when
  86. you use strange formats.
  87. */
  88. #include "file.h"
  89. #ifndef lint
  90. FILE_RCSID("@(#)$File: vasprintf.c,v 1.20 2022/07/30 17:04:18 christos Exp $")
  91. #endif /* lint */
  92. #include <assert.h>
  93. #include <string.h>
  94. #include <stdlib.h>
  95. #include <stdarg.h>
  96. #include <ctype.h>
  97. #include <limits.h>
  98. #include <stddef.h>
  99. #define ALLOC_CHUNK 2048
  100. #define ALLOC_SECURITY_MARGIN 1024 /* big value because some platforms have very big 'G' exponent */
  101. #if ALLOC_CHUNK < ALLOC_SECURITY_MARGIN
  102. # error !!! ALLOC_CHUNK < ALLOC_SECURITY_MARGIN !!!
  103. #endif
  104. /* note: to have some interest, ALLOC_CHUNK should be much greater than ALLOC_SECURITY_MARGIN */
  105. /*
  106. * To save a lot of push/pop, every variable are stored into this
  107. * structure, which is passed among nearly every sub-functions.
  108. */
  109. typedef struct {
  110. const char * src_string; /* current position into input string */
  111. char * buffer_base; /* output buffer */
  112. char * dest_string; /* current position into output string */
  113. size_t buffer_len; /* length of output buffer */
  114. size_t real_len; /* real current length of output text */
  115. size_t pseudo_len; /* total length of output text if it were not limited in size */
  116. size_t maxlen;
  117. va_list vargs; /* pointer to current position into vargs */
  118. char * sprintf_string;
  119. FILE * fprintf_file;
  120. } xprintf_struct;
  121. /*
  122. * Realloc buffer if needed
  123. * Return value: 0 = ok
  124. * EOF = not enough memory
  125. */
  126. static int realloc_buff(xprintf_struct *s, size_t len)
  127. {
  128. char * ptr;
  129. if (len + ALLOC_SECURITY_MARGIN + s->real_len > s->buffer_len) {
  130. len += s->real_len + ALLOC_CHUNK;
  131. ptr = (char *)realloc((void *)(s->buffer_base), len);
  132. if (ptr == NULL) {
  133. s->buffer_base = NULL;
  134. return EOF;
  135. }
  136. s->dest_string = ptr + (size_t)(s->dest_string - s->buffer_base);
  137. s->buffer_base = ptr;
  138. s->buffer_len = len;
  139. (s->buffer_base)[s->buffer_len - 1] = 1; /* overflow marker */
  140. }
  141. return 0;
  142. }
  143. /*
  144. * Prints 'usual' characters up to next '%'
  145. * or up to end of text
  146. */
  147. static int usual_char(xprintf_struct * s)
  148. {
  149. size_t len;
  150. len = strcspn(s->src_string, "%"); /* reaches the next '%' or end of input string */
  151. /* note: 'len' is never 0 because the presence of '%' */
  152. /* or end-of-line is checked in the calling function */
  153. if (realloc_buff(s,len) == EOF)
  154. return EOF;
  155. memcpy(s->dest_string, s->src_string, len);
  156. s->src_string += len;
  157. s->dest_string += len;
  158. s->real_len += len;
  159. s->pseudo_len += len;
  160. return 0;
  161. }
  162. /*
  163. * Return value: 0 = ok
  164. * EOF = error
  165. */
  166. static int print_it(xprintf_struct *s, size_t approx_len,
  167. const char *format_string, ...)
  168. {
  169. va_list varg;
  170. int vsprintf_len;
  171. size_t len;
  172. if (realloc_buff(s,approx_len) == EOF)
  173. return EOF;
  174. va_start(varg, format_string);
  175. vsprintf_len = vsprintf(s->dest_string, format_string, varg);
  176. va_end(varg);
  177. /* Check for overflow */
  178. assert((s->buffer_base)[s->buffer_len - 1] == 1);
  179. if (vsprintf_len == EOF) /* must be done *after* overflow-check */
  180. return EOF;
  181. s->pseudo_len += vsprintf_len;
  182. len = strlen(s->dest_string);
  183. s->real_len += len;
  184. s->dest_string += len;
  185. return 0;
  186. }
  187. /*
  188. * Prints a string (%s)
  189. * We need special handling because:
  190. * a: the length of the string is unknown
  191. * b: when .prec is used, we must not access any extra byte of the
  192. * string (of course, if the original sprintf() does... what the
  193. * hell, not my problem)
  194. *
  195. * Return value: 0 = ok
  196. * EOF = error
  197. */
  198. static int type_s(xprintf_struct *s, int width, int prec,
  199. const char *format_string, const char *arg_string)
  200. {
  201. size_t string_len;
  202. if (arg_string == NULL)
  203. return print_it(s, (size_t)6, "(null)", 0);
  204. /* hand-made strlen() which stops when 'prec' is reached. */
  205. /* if 'prec' is -1 then it is never reached. */
  206. string_len = 0;
  207. while (arg_string[string_len] != 0 && (size_t)prec != string_len)
  208. string_len++;
  209. if (width != -1 && string_len < (size_t)width)
  210. string_len = (size_t)width;
  211. return print_it(s, string_len, format_string, arg_string);
  212. }
  213. /*
  214. * Read a series of digits. Stop when non-digit is found.
  215. * Return value: the value read (between 0 and 32767).
  216. * Note: no checks are made against overflow. If the string contain a big
  217. * number, then the return value won't be what we want (but, in this case,
  218. * the programmer don't know whatr he wants, then no problem).
  219. */
  220. static int getint(const char **string)
  221. {
  222. int i = 0;
  223. while (isdigit((unsigned char)**string) != 0) {
  224. i = i * 10 + (**string - '0');
  225. (*string)++;
  226. }
  227. if (i < 0 || i > 32767)
  228. i = 32767; /* if we have i==-10 this is not because the number is */
  229. /* negative; this is because the number is big */
  230. return i;
  231. }
  232. /*
  233. * Read a part of the format string. A part is 'usual characters' (ie "blabla")
  234. * or '%%' escape sequence (to print a single '%') or any combination of
  235. * format specifier (ie "%i" or "%10.2d").
  236. * After the current part is managed, the function returns to caller with
  237. * everything ready to manage the following part.
  238. * The caller must ensure than the string is not empty, i.e. the first byte
  239. * is not zero.
  240. *
  241. * Return value: 0 = ok
  242. * EOF = error
  243. */
  244. static int dispatch(xprintf_struct *s)
  245. {
  246. const char *initial_ptr;
  247. char format_string[24]; /* max length may be something like "% +-#032768.32768Ld" */
  248. char *format_ptr;
  249. int flag_plus, flag_minus, flag_space, flag_sharp, flag_zero;
  250. int width, prec, modifier, approx_width;
  251. char type;
  252. /* most of those variables are here to rewrite the format string */
  253. #define SRCTXT (s->src_string)
  254. #define DESTTXT (s->dest_string)
  255. /* incoherent format string. Characters after the '%' will be printed with the next call */
  256. #define INCOHERENT() do {SRCTXT=initial_ptr; return 0;} while (0) /* do/while to avoid */
  257. #define INCOHERENT_TEST() do {if(*SRCTXT==0) INCOHERENT();} while (0) /* a null statement */
  258. /* 'normal' text */
  259. if (*SRCTXT != '%')
  260. return usual_char(s);
  261. /* we then have a '%' */
  262. SRCTXT++;
  263. /* don't check for end-of-string ; this is done later */
  264. /* '%%' escape sequence */
  265. if (*SRCTXT == '%') {
  266. if (realloc_buff(s, (size_t)1) == EOF) /* because we can have "%%%%%%%%..." */
  267. return EOF;
  268. *DESTTXT = '%';
  269. DESTTXT++;
  270. SRCTXT++;
  271. (s->real_len)++;
  272. (s->pseudo_len)++;
  273. return 0;
  274. }
  275. /* '%' managing */
  276. initial_ptr = SRCTXT; /* save current pointer in case of incorrect */
  277. /* 'decoding'. Points just after the '%' so the '%' */
  278. /* won't be printed in any case, as required. */
  279. /* flag */
  280. flag_plus = flag_minus = flag_space = flag_sharp = flag_zero = 0;
  281. for (;; SRCTXT++) {
  282. if (*SRCTXT == ' ')
  283. flag_space = 1;
  284. else if (*SRCTXT == '+')
  285. flag_plus = 1;
  286. else if (*SRCTXT == '-')
  287. flag_minus = 1;
  288. else if (*SRCTXT == '#')
  289. flag_sharp = 1;
  290. else if (*SRCTXT == '0')
  291. flag_zero = 1;
  292. else
  293. break;
  294. }
  295. INCOHERENT_TEST(); /* here is the first test for end of string */
  296. /* width */
  297. if (*SRCTXT == '*') { /* width given by next argument */
  298. SRCTXT++;
  299. width = va_arg(s->vargs, int);
  300. if ((size_t)width > 0x3fffU) /* 'size_t' to check against negative values too */
  301. width = 0x3fff;
  302. } else if (isdigit((unsigned char)*SRCTXT)) /* width given as ASCII number */
  303. width = getint(&SRCTXT);
  304. else
  305. width = -1; /* no width specified */
  306. INCOHERENT_TEST();
  307. /* .prec */
  308. if (*SRCTXT == '.') {
  309. SRCTXT++;
  310. if (*SRCTXT == '*') { /* .prec given by next argument */
  311. SRCTXT++;
  312. prec = va_arg(s->vargs, int);
  313. if ((size_t)prec >= 0x3fffU) /* 'size_t' to check against negative values too */
  314. prec = 0x3fff;
  315. } else { /* .prec given as ASCII number */
  316. if (isdigit((unsigned char)*SRCTXT) == 0)
  317. INCOHERENT();
  318. prec = getint(&SRCTXT);
  319. }
  320. INCOHERENT_TEST();
  321. } else
  322. prec = -1; /* no .prec specified */
  323. /* modifier */
  324. switch (*SRCTXT) {
  325. case 'L':
  326. case 'h':
  327. case 'l':
  328. case 'z':
  329. case 't':
  330. modifier = *SRCTXT;
  331. SRCTXT++;
  332. if (modifier=='l' && *SRCTXT=='l') {
  333. SRCTXT++;
  334. modifier = 'L'; /* 'll' == 'L' long long == long double */
  335. } /* only for compatibility ; not portable */
  336. INCOHERENT_TEST();
  337. break;
  338. default:
  339. modifier = -1; /* no modifier specified */
  340. break;
  341. }
  342. /* type */
  343. type = *SRCTXT;
  344. if (strchr("diouxXfegEGcspn",type) == NULL)
  345. INCOHERENT(); /* unknown type */
  346. SRCTXT++;
  347. /* rewrite format-string */
  348. format_string[0] = '%';
  349. format_ptr = &(format_string[1]);
  350. if (flag_plus) {
  351. *format_ptr = '+';
  352. format_ptr++;
  353. }
  354. if (flag_minus) {
  355. *format_ptr = '-';
  356. format_ptr++;
  357. }
  358. if (flag_space) {
  359. *format_ptr = ' ';
  360. format_ptr++;
  361. }
  362. if (flag_sharp) {
  363. *format_ptr = '#';
  364. format_ptr++;
  365. }
  366. if (flag_zero) {
  367. *format_ptr = '0';
  368. format_ptr++;
  369. } /* '0' *must* be the last one */
  370. if (width != -1) {
  371. sprintf(format_ptr, "%i", width);
  372. format_ptr += strlen(format_ptr);
  373. }
  374. if (prec != -1) {
  375. *format_ptr = '.';
  376. format_ptr++;
  377. sprintf(format_ptr, "%i", prec);
  378. format_ptr += strlen(format_ptr);
  379. }
  380. if (modifier != -1) {
  381. if (modifier == 'L' && strchr("diouxX",type) != NULL) {
  382. *format_ptr = 'l';
  383. format_ptr++;
  384. *format_ptr = 'l';
  385. format_ptr++;
  386. } else {
  387. *format_ptr = modifier;
  388. format_ptr++;
  389. }
  390. }
  391. *format_ptr = type;
  392. format_ptr++;
  393. *format_ptr = 0;
  394. /* vague approximation of minimal length if width or prec are specified */
  395. approx_width = width + prec;
  396. if (approx_width < 0) /* because width == -1 and/or prec == -1 */
  397. approx_width = 0;
  398. switch (type) {
  399. /* int */
  400. case 'd':
  401. case 'i':
  402. case 'o':
  403. case 'u':
  404. case 'x':
  405. case 'X':
  406. switch (modifier) {
  407. case -1 :
  408. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, int));
  409. case 'L':
  410. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, long long int));
  411. case 'l':
  412. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, long int));
  413. case 'h':
  414. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, int));
  415. case 'z':
  416. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, size_t));
  417. case 't':
  418. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, ptrdiff_t));
  419. /* 'int' instead of 'short int' because default promotion is 'int' */
  420. default:
  421. INCOHERENT();
  422. }
  423. /* char */
  424. case 'c':
  425. if (modifier != -1)
  426. INCOHERENT();
  427. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, int));
  428. /* 'int' instead of 'char' because default promotion is 'int' */
  429. /* math */
  430. case 'e':
  431. case 'f':
  432. case 'g':
  433. case 'E':
  434. case 'G':
  435. switch (modifier) {
  436. case -1 : /* because of default promotion, no modifier means 'l' */
  437. case 'l':
  438. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, double));
  439. case 'L':
  440. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, long double));
  441. default:
  442. INCOHERENT();
  443. }
  444. /* string */
  445. case 's':
  446. return type_s(s, width, prec, format_string, va_arg(s->vargs, const char*));
  447. /* pointer */
  448. case 'p':
  449. if (modifier == -1)
  450. return print_it(s, (size_t)approx_width, format_string, va_arg(s->vargs, void *));
  451. INCOHERENT();
  452. /* store */
  453. case 'n':
  454. if (modifier == -1) {
  455. int * p;
  456. p = va_arg(s->vargs, int *);
  457. if (p != NULL) {
  458. *p = s->pseudo_len;
  459. return 0;
  460. }
  461. return EOF;
  462. }
  463. INCOHERENT();
  464. } /* switch */
  465. INCOHERENT(); /* unknown type */
  466. #undef INCOHERENT
  467. #undef INCOHERENT_TEST
  468. #undef SRCTXT
  469. #undef DESTTXT
  470. }
  471. /*
  472. * Return value: number of *virtually* written characters
  473. * EOF = error
  474. */
  475. static int core(xprintf_struct *s)
  476. {
  477. size_t save_len;
  478. char *dummy_base;
  479. /* basic checks */
  480. if ((int)(s->maxlen) <= 0) /* 'int' to check against some conversion */
  481. return EOF; /* error for example if value is (int)-10 */
  482. s->maxlen--; /* because initial maxlen counts final 0 */
  483. /* note: now 'maxlen' _can_ be zero */
  484. if (s->src_string == NULL)
  485. s->src_string = "(null)";
  486. /* struct init and memory allocation */
  487. s->buffer_base = NULL;
  488. s->buffer_len = 0;
  489. s->real_len = 0;
  490. s->pseudo_len = 0;
  491. if (realloc_buff(s, (size_t)0) == EOF)
  492. return EOF;
  493. s->dest_string = s->buffer_base;
  494. /* process source string */
  495. for (;;) {
  496. /* up to end of source string */
  497. if (*(s->src_string) == 0) {
  498. *(s->dest_string) = '\0'; /* final NUL */
  499. break;
  500. }
  501. if (dispatch(s) == EOF)
  502. goto free_EOF;
  503. /* up to end of dest string */
  504. if (s->real_len >= s->maxlen) {
  505. (s->buffer_base)[s->maxlen] = '\0'; /* final NUL */
  506. break;
  507. }
  508. }
  509. /* for (v)asnprintf */
  510. dummy_base = s->buffer_base;
  511. dummy_base = s->buffer_base + s->real_len;
  512. save_len = s->real_len;
  513. /* process the remaining of source string to compute 'pseudo_len'. We
  514. * overwrite again and again, starting at 'dummy_base' because we don't
  515. * need the text, only char count. */
  516. while(*(s->src_string) != 0) { /* up to end of source string */
  517. s->real_len = 0;
  518. s->dest_string = dummy_base;
  519. if (dispatch(s) == EOF)
  520. goto free_EOF;
  521. }
  522. s->buffer_base = (char *)realloc((void *)(s->buffer_base), save_len + 1);
  523. if (s->buffer_base == NULL)
  524. return EOF; /* should rarely happen because we shrink the buffer */
  525. return s->pseudo_len;
  526. free_EOF:
  527. free(s->buffer_base);
  528. return EOF;
  529. }
  530. int vasprintf(char **ptr, const char *format_string, va_list vargs)
  531. {
  532. xprintf_struct s;
  533. int retval;
  534. s.src_string = format_string;
  535. #ifdef va_copy
  536. va_copy (s.vargs, vargs);
  537. #else
  538. # ifdef __va_copy
  539. __va_copy (s.vargs, vargs);
  540. # else
  541. # ifdef WIN32
  542. s.vargs = vargs;
  543. # else
  544. memcpy (&s.vargs, &vargs, sizeof (s.vargs));
  545. # endif /* WIN32 */
  546. # endif /* __va_copy */
  547. #endif /* va_copy */
  548. s.maxlen = (size_t)INT_MAX;
  549. retval = core(&s);
  550. va_end(s.vargs);
  551. if (retval == EOF) {
  552. *ptr = NULL;
  553. return EOF;
  554. }
  555. *ptr = s.buffer_base;
  556. return retval;
  557. }