Line data Source code
1 : /*
2 : * Copyright (c) 1983, 1995, 1996 Eric P. Allman
3 : * Copyright (c) 1988, 1993
4 : * The Regents of the University of California. All rights reserved.
5 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, 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 : * 3. Neither the name of the University nor the names of its contributors
16 : * may be used to endorse or promote products derived from this software
17 : * without specific prior written permission.
18 : *
19 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 : * SUCH DAMAGE.
30 : *
31 : * src/port/snprintf.c
32 : */
33 :
34 : #include "c.h"
35 :
36 : #include <math.h>
37 :
38 : /*
39 : * We used to use the platform's NL_ARGMAX here, but that's a bad idea,
40 : * first because the point of this module is to remove platform dependencies
41 : * not perpetuate them, and second because some platforms use ridiculously
42 : * large values, leading to excessive stack consumption in dopr().
43 : */
44 : #define PG_NL_ARGMAX 31
45 :
46 :
47 : /*
48 : * SNPRINTF, VSNPRINTF and friends
49 : *
50 : * These versions have been grabbed off the net. They have been
51 : * cleaned up to compile properly and support for most of the C99
52 : * specification has been added. Remaining unimplemented features are:
53 : *
54 : * 1. No locale support: the radix character is always '.' and the '
55 : * (single quote) format flag is ignored.
56 : *
57 : * 2. No support for the "%n" format specification.
58 : *
59 : * 3. No support for wide characters ("lc" and "ls" formats).
60 : *
61 : * 4. No support for "long double" ("Lf" and related formats).
62 : *
63 : * 5. Space and '#' flags are not implemented.
64 : *
65 : * In addition, we support some extensions over C99:
66 : *
67 : * 1. Argument order control through "%n$" and "*n$", as required by POSIX.
68 : *
69 : * 2. "%m" expands to the value of strerror(errno), where errno is the
70 : * value that variable had at the start of the call. This is a glibc
71 : * extension, but a very useful one.
72 : *
73 : *
74 : * Historically the result values of sprintf/snprintf varied across platforms.
75 : * This implementation now follows the C99 standard:
76 : *
77 : * 1. -1 is returned if an error is detected in the format string, or if
78 : * a write to the target stream fails (as reported by fwrite). Note that
79 : * overrunning snprintf's target buffer is *not* an error.
80 : *
81 : * 2. For successful writes to streams, the actual number of bytes written
82 : * to the stream is returned.
83 : *
84 : * 3. For successful sprintf/snprintf, the number of bytes that would have
85 : * been written to an infinite-size buffer (excluding the trailing '\0')
86 : * is returned. snprintf will truncate its output to fit in the buffer
87 : * (ensuring a trailing '\0' unless count == 0), but this is not reflected
88 : * in the function result.
89 : *
90 : * snprintf buffer overrun can be detected by checking for function result
91 : * greater than or equal to the supplied count.
92 : */
93 :
94 : /**************************************************************
95 : * Original:
96 : * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
97 : * A bombproof version of doprnt (dopr) included.
98 : * Sigh. This sort of thing is always nasty do deal with. Note that
99 : * the version here does not include floating point. (now it does ... tgl)
100 : **************************************************************/
101 :
102 : /* Prevent recursion */
103 : #undef vsnprintf
104 : #undef snprintf
105 : #undef vsprintf
106 : #undef sprintf
107 : #undef vfprintf
108 : #undef fprintf
109 : #undef vprintf
110 : #undef printf
111 :
112 : /*
113 : * Info about where the formatted output is going.
114 : *
115 : * dopr and subroutines will not write at/past bufend, but snprintf
116 : * reserves one byte, ensuring it may place the trailing '\0' there.
117 : *
118 : * In snprintf, we use nchars to count the number of bytes dropped on the
119 : * floor due to buffer overrun. The correct result of snprintf is thus
120 : * (bufptr - bufstart) + nchars. (This isn't as inconsistent as it might
121 : * seem: nchars is the number of emitted bytes that are not in the buffer now,
122 : * either because we sent them to the stream or because we couldn't fit them
123 : * into the buffer to begin with.)
124 : */
125 : typedef struct
126 : {
127 : char *bufptr; /* next buffer output position */
128 : char *bufstart; /* first buffer element */
129 : char *bufend; /* last+1 buffer element, or NULL */
130 : /* bufend == NULL is for sprintf, where we assume buf is big enough */
131 : FILE *stream; /* eventual output destination, or NULL */
132 : int nchars; /* # chars sent to stream, or dropped */
133 : bool failed; /* call is a failure; errno is set */
134 : } PrintfTarget;
135 :
136 : /*
137 : * Info about the type and value of a formatting parameter. Note that we
138 : * don't currently support "long double", "wint_t", or "wchar_t *" data,
139 : * nor the '%n' formatting code; else we'd need more types. Also, at this
140 : * level we need not worry about signed vs unsigned values.
141 : */
142 : typedef enum
143 : {
144 : ATYPE_NONE = 0,
145 : ATYPE_INT,
146 : ATYPE_LONG,
147 : ATYPE_LONGLONG,
148 : ATYPE_DOUBLE,
149 : ATYPE_CHARPTR
150 : } PrintfArgType;
151 :
152 : typedef union
153 : {
154 : int i;
155 : long l;
156 : long long ll;
157 : double d;
158 : char *cptr;
159 : } PrintfArgValue;
160 :
161 :
162 : static void flushbuffer(PrintfTarget *target);
163 : static void dopr(PrintfTarget *target, const char *format, va_list args);
164 :
165 :
166 : /*
167 : * Externally visible entry points.
168 : *
169 : * All of these are just wrappers around dopr(). Note it's essential that
170 : * they not change the value of "errno" before reaching dopr().
171 : */
172 :
173 : int
174 456175224 : pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
175 : {
176 : PrintfTarget target;
177 : char onebyte[1];
178 :
179 : /*
180 : * C99 allows the case str == NULL when count == 0. Rather than
181 : * special-casing this situation further down, we substitute a one-byte
182 : * local buffer. Callers cannot tell, since the function result doesn't
183 : * depend on count.
184 : */
185 456175224 : if (count == 0)
186 : {
187 245562 : str = onebyte;
188 245562 : count = 1;
189 : }
190 456175224 : target.bufstart = target.bufptr = str;
191 456175224 : target.bufend = str + count - 1;
192 456175224 : target.stream = NULL;
193 456175224 : target.nchars = 0;
194 456175224 : target.failed = false;
195 456175224 : dopr(&target, fmt, args);
196 456175228 : *(target.bufptr) = '\0';
197 912350456 : return target.failed ? -1 : (target.bufptr - target.bufstart
198 456175228 : + target.nchars);
199 : }
200 :
201 : int
202 39415262 : pg_snprintf(char *str, size_t count, const char *fmt,...)
203 : {
204 : int len;
205 : va_list args;
206 :
207 39415262 : va_start(args, fmt);
208 39415262 : len = pg_vsnprintf(str, count, fmt, args);
209 39415266 : va_end(args);
210 39415266 : return len;
211 : }
212 :
213 : int
214 11771210 : pg_vsprintf(char *str, const char *fmt, va_list args)
215 : {
216 : PrintfTarget target;
217 :
218 11771210 : target.bufstart = target.bufptr = str;
219 11771210 : target.bufend = NULL;
220 11771210 : target.stream = NULL;
221 11771210 : target.nchars = 0; /* not really used in this case */
222 11771210 : target.failed = false;
223 11771210 : dopr(&target, fmt, args);
224 11771214 : *(target.bufptr) = '\0';
225 23542428 : return target.failed ? -1 : (target.bufptr - target.bufstart
226 11771214 : + target.nchars);
227 : }
228 :
229 : int
230 11771210 : pg_sprintf(char *str, const char *fmt,...)
231 : {
232 : int len;
233 : va_list args;
234 :
235 11771210 : va_start(args, fmt);
236 11771210 : len = pg_vsprintf(str, fmt, args);
237 11771214 : va_end(args);
238 11771214 : return len;
239 : }
240 :
241 : int
242 4524630 : pg_vfprintf(FILE *stream, const char *fmt, va_list args)
243 : {
244 : PrintfTarget target;
245 : char buffer[1024]; /* size is arbitrary */
246 :
247 4524630 : if (stream == NULL)
248 : {
249 0 : errno = EINVAL;
250 0 : return -1;
251 : }
252 4524630 : target.bufstart = target.bufptr = buffer;
253 4524630 : target.bufend = buffer + sizeof(buffer); /* use the whole buffer */
254 4524630 : target.stream = stream;
255 4524630 : target.nchars = 0;
256 4524630 : target.failed = false;
257 4524630 : dopr(&target, fmt, args);
258 : /* dump any remaining buffer contents */
259 4524630 : flushbuffer(&target);
260 4524630 : return target.failed ? -1 : target.nchars;
261 : }
262 :
263 : int
264 2302250 : pg_fprintf(FILE *stream, const char *fmt,...)
265 : {
266 : int len;
267 : va_list args;
268 :
269 2302250 : va_start(args, fmt);
270 2302250 : len = pg_vfprintf(stream, fmt, args);
271 2302250 : va_end(args);
272 2302250 : return len;
273 : }
274 :
275 : int
276 0 : pg_vprintf(const char *fmt, va_list args)
277 : {
278 0 : return pg_vfprintf(stdout, fmt, args);
279 : }
280 :
281 : int
282 2199636 : pg_printf(const char *fmt,...)
283 : {
284 : int len;
285 : va_list args;
286 :
287 2199636 : va_start(args, fmt);
288 2199636 : len = pg_vfprintf(stdout, fmt, args);
289 2199636 : va_end(args);
290 2199636 : return len;
291 : }
292 :
293 : /*
294 : * Attempt to write the entire buffer to target->stream; discard the entire
295 : * buffer in any case. Call this only when target->stream is defined.
296 : */
297 : static void
298 4525232 : flushbuffer(PrintfTarget *target)
299 : {
300 4525232 : size_t nc = target->bufptr - target->bufstart;
301 :
302 : /*
303 : * Don't write anything if we already failed; this is to ensure we
304 : * preserve the original failure's errno.
305 : */
306 4525232 : if (!target->failed && nc > 0)
307 : {
308 : size_t written;
309 :
310 4262696 : written = fwrite(target->bufstart, 1, nc, target->stream);
311 4262696 : target->nchars += written;
312 4262696 : if (written != nc)
313 0 : target->failed = true;
314 : }
315 4525232 : target->bufptr = target->bufstart;
316 4525232 : }
317 :
318 :
319 : static bool find_arguments(const char *format, va_list args,
320 : PrintfArgValue *argvalues);
321 : static void fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
322 : int pointflag, PrintfTarget *target);
323 : static void fmtptr(const void *value, PrintfTarget *target);
324 : static void fmtint(long long value, char type, int forcesign,
325 : int leftjust, int minlen, int zpad, int precision, int pointflag,
326 : PrintfTarget *target);
327 : static void fmtchar(int value, int leftjust, int minlen, PrintfTarget *target);
328 : static void fmtfloat(double value, char type, int forcesign,
329 : int leftjust, int minlen, int zpad, int precision, int pointflag,
330 : PrintfTarget *target);
331 : static void dostr(const char *str, int slen, PrintfTarget *target);
332 : static void dopr_outch(int c, PrintfTarget *target);
333 : static void dopr_outchmulti(int c, int slen, PrintfTarget *target);
334 : static int adjust_sign(int is_negative, int forcesign, int *signvalue);
335 : static int compute_padlen(int minlen, int vallen, int leftjust);
336 : static void leading_pad(int zpad, int signvalue, int *padlen,
337 : PrintfTarget *target);
338 : static void trailing_pad(int padlen, PrintfTarget *target);
339 :
340 : /*
341 : * If strchrnul exists (it's a glibc-ism), it's a good bit faster than the
342 : * equivalent manual loop. If it doesn't exist, provide a replacement.
343 : *
344 : * Note: glibc declares this as returning "char *", but that would require
345 : * casting away const internally, so we don't follow that detail.
346 : */
347 : #ifndef HAVE_STRCHRNUL
348 :
349 : static inline const char *
350 : strchrnul(const char *s, int c)
351 : {
352 : while (*s != '\0' && *s != c)
353 : s++;
354 : return s;
355 : }
356 :
357 : #else
358 :
359 : /*
360 : * glibc's <string.h> declares strchrnul only if _GNU_SOURCE is defined.
361 : * While we typically use that on glibc platforms, configure will set
362 : * HAVE_STRCHRNUL whether it's used or not. Fill in the missing declaration
363 : * so that this file will compile cleanly with or without _GNU_SOURCE.
364 : */
365 : #ifndef _GNU_SOURCE
366 : extern char *strchrnul(const char *s, int c);
367 : #endif
368 :
369 : #endif /* HAVE_STRCHRNUL */
370 :
371 :
372 : /*
373 : * dopr(): the guts of *printf for all cases.
374 : */
375 : static void
376 472471064 : dopr(PrintfTarget *target, const char *format, va_list args)
377 : {
378 472471064 : int save_errno = errno;
379 472471064 : const char *first_pct = NULL;
380 : int ch;
381 : bool have_dollar;
382 : bool have_star;
383 : bool afterstar;
384 : int accum;
385 : int longlongflag;
386 : int longflag;
387 : int pointflag;
388 : int leftjust;
389 : int fieldwidth;
390 : int precision;
391 : int zpad;
392 : int forcesign;
393 : int fmtpos;
394 : int cvalue;
395 : long long numvalue;
396 : double fvalue;
397 : const char *strvalue;
398 : PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
399 :
400 : /*
401 : * Initially, we suppose the format string does not use %n$. The first
402 : * time we come to a conversion spec that has that, we'll call
403 : * find_arguments() to check for consistent use of %n$ and fill the
404 : * argvalues array with the argument values in the correct order.
405 : */
406 472471064 : have_dollar = false;
407 :
408 976626418 : while (*format != '\0')
409 : {
410 : /* Locate next conversion specifier */
411 648677234 : if (*format != '%')
412 : {
413 : /* Scan to next '%' or end of string */
414 461893840 : const char *next_pct = strchrnul(format + 1, '%');
415 :
416 : /* Dump literal data we just scanned over */
417 461893840 : dostr(format, next_pct - format, target);
418 461893838 : if (target->failed)
419 0 : break;
420 :
421 461893838 : if (*next_pct == '\0')
422 144521888 : break;
423 317371950 : format = next_pct;
424 : }
425 :
426 : /*
427 : * Remember start of first conversion spec; if we find %n$, then it's
428 : * sufficient for find_arguments() to start here, without rescanning
429 : * earlier literal text.
430 : */
431 504155344 : if (first_pct == NULL)
432 465621758 : first_pct = format;
433 :
434 : /* Process conversion spec starting at *format */
435 504155344 : format++;
436 :
437 : /* Fast path for conversion spec that is exactly %s */
438 504155344 : if (*format == 's')
439 : {
440 84778698 : format++;
441 84778698 : strvalue = va_arg(args, char *);
442 84778700 : if (strvalue == NULL)
443 0 : strvalue = "(null)";
444 84778700 : dostr(strvalue, strlen(strvalue), target);
445 84778704 : if (target->failed)
446 0 : break;
447 84778704 : continue;
448 : }
449 :
450 419376646 : fieldwidth = precision = zpad = leftjust = forcesign = 0;
451 419376646 : longflag = longlongflag = pointflag = 0;
452 419376646 : fmtpos = accum = 0;
453 419376646 : have_star = afterstar = false;
454 490671474 : nextch2:
455 490671474 : ch = *format++;
456 490671474 : switch (ch)
457 : {
458 1010584 : case '-':
459 1010584 : leftjust = 1;
460 1010584 : goto nextch2;
461 282 : case '+':
462 282 : forcesign = 1;
463 282 : goto nextch2;
464 28214064 : case '0':
465 : /* set zero padding if no nonzero digits yet */
466 28214064 : if (accum == 0 && !pointflag)
467 27656586 : zpad = '0';
468 : /* FALL THRU */
469 : case '1':
470 : case '2':
471 : case '3':
472 : case '4':
473 : case '5':
474 : case '6':
475 : case '7':
476 : case '8':
477 : case '9':
478 58948696 : accum = accum * 10 + (ch - '0');
479 58948696 : goto nextch2;
480 629072 : case '.':
481 629072 : if (have_star)
482 0 : have_star = false;
483 : else
484 629072 : fieldwidth = accum;
485 629072 : pointflag = 1;
486 629072 : accum = 0;
487 629072 : goto nextch2;
488 1396808 : case '*':
489 1396808 : if (have_dollar)
490 : {
491 : /*
492 : * We'll process value after reading n$. Note it's OK to
493 : * assume have_dollar is set correctly, because in a valid
494 : * format string the initial % must have had n$ if * does.
495 : */
496 0 : afterstar = true;
497 : }
498 : else
499 : {
500 : /* fetch and process value now */
501 1396808 : int starval = va_arg(args, int);
502 :
503 1396808 : if (pointflag)
504 : {
505 60264 : precision = starval;
506 60264 : if (precision < 0)
507 : {
508 0 : precision = 0;
509 0 : pointflag = 0;
510 : }
511 : }
512 : else
513 : {
514 1336544 : fieldwidth = starval;
515 1336544 : if (fieldwidth < 0)
516 : {
517 5646 : leftjust = 1;
518 5646 : fieldwidth = -fieldwidth;
519 : }
520 : }
521 : }
522 1396808 : have_star = true;
523 1396808 : accum = 0;
524 1396808 : goto nextch2;
525 0 : case '$':
526 : /* First dollar sign? */
527 0 : if (!have_dollar)
528 : {
529 : /* Yup, so examine all conversion specs in format */
530 0 : if (!find_arguments(first_pct, args, argvalues))
531 2 : goto bad_format;
532 0 : have_dollar = true;
533 : }
534 0 : if (afterstar)
535 : {
536 : /* fetch and process star value */
537 0 : int starval = argvalues[accum].i;
538 :
539 0 : if (pointflag)
540 : {
541 0 : precision = starval;
542 0 : if (precision < 0)
543 : {
544 0 : precision = 0;
545 0 : pointflag = 0;
546 : }
547 : }
548 : else
549 : {
550 0 : fieldwidth = starval;
551 0 : if (fieldwidth < 0)
552 : {
553 0 : leftjust = 1;
554 0 : fieldwidth = -fieldwidth;
555 : }
556 : }
557 0 : afterstar = false;
558 : }
559 : else
560 0 : fmtpos = accum;
561 0 : accum = 0;
562 0 : goto nextch2;
563 : #ifdef WIN32
564 : case 'I':
565 : /* Windows PRI*{32,64,PTR} size */
566 : if (format[0] == '3' && format[1] == '2')
567 : format += 2;
568 : else if (format[0] == '6' && format[1] == '4')
569 : {
570 : format += 2;
571 : longlongflag = 1;
572 : }
573 : else
574 : {
575 : #if SIZEOF_VOID_P == SIZEOF_LONG
576 : longflag = 1;
577 : #elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
578 : longlongflag = 1;
579 : #else
580 : #error "cannot find integer type of the same size as intptr_t"
581 : #endif
582 : }
583 : goto nextch2;
584 : #endif
585 8980914 : case 'l':
586 8980914 : if (longflag)
587 1107776 : longlongflag = 1;
588 : else
589 7873138 : longflag = 1;
590 8980914 : goto nextch2;
591 328386 : case 'z':
592 : #if SIZEOF_SIZE_T == SIZEOF_LONG
593 328386 : longflag = 1;
594 : #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
595 : longlongflag = 1;
596 : #else
597 : #error "cannot find integer type of the same size as size_t"
598 : #endif
599 328386 : goto nextch2;
600 88 : case 'h':
601 : case '\'':
602 : /* ignore these */
603 88 : goto nextch2;
604 283422364 : case 'd':
605 : case 'i':
606 283422364 : if (!have_star)
607 : {
608 283383196 : if (pointflag)
609 0 : precision = accum;
610 : else
611 283383196 : fieldwidth = accum;
612 : }
613 283422364 : if (have_dollar)
614 : {
615 0 : if (longlongflag)
616 0 : numvalue = argvalues[fmtpos].ll;
617 0 : else if (longflag)
618 0 : numvalue = argvalues[fmtpos].l;
619 : else
620 0 : numvalue = argvalues[fmtpos].i;
621 : }
622 : else
623 : {
624 283422364 : if (longlongflag)
625 986336 : numvalue = va_arg(args, long long);
626 282436028 : else if (longflag)
627 4767668 : numvalue = va_arg(args, long);
628 : else
629 277668360 : numvalue = va_arg(args, int);
630 : }
631 283422364 : fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
632 : precision, pointflag, target);
633 283422366 : break;
634 132774244 : case 'o':
635 : case 'u':
636 : case 'x':
637 : case 'X':
638 132774244 : if (!have_star)
639 : {
640 132774244 : if (pointflag)
641 0 : precision = accum;
642 : else
643 132774244 : fieldwidth = accum;
644 : }
645 132774244 : if (have_dollar)
646 : {
647 0 : if (longlongflag)
648 0 : numvalue = (unsigned long long) argvalues[fmtpos].ll;
649 0 : else if (longflag)
650 0 : numvalue = (unsigned long) argvalues[fmtpos].l;
651 : else
652 0 : numvalue = (unsigned int) argvalues[fmtpos].i;
653 : }
654 : else
655 : {
656 132774244 : if (longlongflag)
657 121440 : numvalue = (unsigned long long) va_arg(args, long long);
658 132652804 : else if (longflag)
659 2326080 : numvalue = (unsigned long) va_arg(args, long);
660 : else
661 130326724 : numvalue = (unsigned int) va_arg(args, int);
662 : }
663 132774244 : fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad,
664 : precision, pointflag, target);
665 132774244 : break;
666 39050 : case 'c':
667 39050 : if (!have_star)
668 : {
669 39008 : if (pointflag)
670 0 : precision = accum;
671 : else
672 39008 : fieldwidth = accum;
673 : }
674 39050 : if (have_dollar)
675 0 : cvalue = (unsigned char) argvalues[fmtpos].i;
676 : else
677 39050 : cvalue = (unsigned char) va_arg(args, int);
678 39050 : fmtchar(cvalue, leftjust, fieldwidth, target);
679 39050 : break;
680 1837796 : case 's':
681 1837796 : if (!have_star)
682 : {
683 520052 : if (pointflag)
684 0 : precision = accum;
685 : else
686 520052 : fieldwidth = accum;
687 : }
688 1837796 : if (have_dollar)
689 0 : strvalue = argvalues[fmtpos].cptr;
690 : else
691 1837796 : strvalue = va_arg(args, char *);
692 : /* If string is NULL, silently substitute "(null)" */
693 1837796 : if (strvalue == NULL)
694 0 : strvalue = "(null)";
695 1837796 : fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag,
696 : target);
697 1837796 : break;
698 82 : case 'p':
699 : /* fieldwidth/leftjust are ignored ... */
700 82 : if (have_dollar)
701 0 : strvalue = argvalues[fmtpos].cptr;
702 : else
703 82 : strvalue = va_arg(args, char *);
704 82 : fmtptr((const void *) strvalue, target);
705 82 : break;
706 1018664 : case 'e':
707 : case 'E':
708 : case 'f':
709 : case 'g':
710 : case 'G':
711 1018664 : if (!have_star)
712 : {
713 978810 : if (pointflag)
714 568808 : precision = accum;
715 : else
716 410002 : fieldwidth = accum;
717 : }
718 1018664 : if (have_dollar)
719 0 : fvalue = argvalues[fmtpos].d;
720 : else
721 1018664 : fvalue = va_arg(args, double);
722 1018664 : fmtfloat(fvalue, ch, forcesign, leftjust,
723 : fieldwidth, zpad,
724 : precision, pointflag,
725 : target);
726 1018664 : break;
727 296 : case 'm':
728 : {
729 : char errbuf[PG_STRERROR_R_BUFLEN];
730 296 : const char *errm = strerror_r(save_errno,
731 : errbuf, sizeof(errbuf));
732 :
733 296 : dostr(errm, strlen(errm), target);
734 : }
735 300 : break;
736 284148 : case '%':
737 284148 : dopr_outch('%', target);
738 284148 : break;
739 0 : default:
740 :
741 : /*
742 : * Anything else --- in particular, '\0' indicating end of
743 : * format string --- is bogus.
744 : */
745 0 : goto bad_format;
746 : }
747 :
748 : /* Check for failure after each conversion spec */
749 419376650 : if (target->failed)
750 0 : break;
751 : }
752 :
753 472471072 : return;
754 :
755 2 : bad_format:
756 2 : errno = EINVAL;
757 2 : target->failed = true;
758 : }
759 :
760 : /*
761 : * find_arguments(): sort out the arguments for a format spec with %n$
762 : *
763 : * If format is valid, return true and fill argvalues[i] with the value
764 : * for the conversion spec that has %i$ or *i$. Else return false.
765 : */
766 : static bool
767 0 : find_arguments(const char *format, va_list args,
768 : PrintfArgValue *argvalues)
769 : {
770 : int ch;
771 : bool afterstar;
772 : int accum;
773 : int longlongflag;
774 : int longflag;
775 : int fmtpos;
776 : int i;
777 0 : int last_dollar = 0; /* Init to "no dollar arguments known" */
778 0 : PrintfArgType argtypes[PG_NL_ARGMAX + 1] = {0};
779 :
780 : /*
781 : * This loop must accept the same format strings as the one in dopr().
782 : * However, we don't need to analyze them to the same level of detail.
783 : *
784 : * Since we're only called if there's a dollar-type spec somewhere, we can
785 : * fail immediately if we find a non-dollar spec. Per the C99 standard,
786 : * all argument references in the format string must be one or the other.
787 : */
788 0 : while (*format != '\0')
789 : {
790 : /* Locate next conversion specifier */
791 0 : if (*format != '%')
792 : {
793 : /* Unlike dopr, we can just quit if there's no more specifiers */
794 0 : format = strchr(format + 1, '%');
795 0 : if (format == NULL)
796 0 : break;
797 : }
798 :
799 : /* Process conversion spec starting at *format */
800 0 : format++;
801 0 : longflag = longlongflag = 0;
802 0 : fmtpos = accum = 0;
803 0 : afterstar = false;
804 0 : nextch1:
805 0 : ch = *format++;
806 0 : switch (ch)
807 : {
808 0 : case '-':
809 : case '+':
810 0 : goto nextch1;
811 0 : case '0':
812 : case '1':
813 : case '2':
814 : case '3':
815 : case '4':
816 : case '5':
817 : case '6':
818 : case '7':
819 : case '8':
820 : case '9':
821 0 : accum = accum * 10 + (ch - '0');
822 0 : goto nextch1;
823 0 : case '.':
824 0 : accum = 0;
825 0 : goto nextch1;
826 0 : case '*':
827 0 : if (afterstar)
828 0 : return false; /* previous star missing dollar */
829 0 : afterstar = true;
830 0 : accum = 0;
831 0 : goto nextch1;
832 0 : case '$':
833 0 : if (accum <= 0 || accum > PG_NL_ARGMAX)
834 0 : return false;
835 0 : if (afterstar)
836 : {
837 0 : if (argtypes[accum] &&
838 0 : argtypes[accum] != ATYPE_INT)
839 0 : return false;
840 0 : argtypes[accum] = ATYPE_INT;
841 0 : last_dollar = Max(last_dollar, accum);
842 0 : afterstar = false;
843 : }
844 : else
845 0 : fmtpos = accum;
846 0 : accum = 0;
847 0 : goto nextch1;
848 : #ifdef WIN32
849 : case 'I':
850 : /* Windows PRI*{32,64,PTR} size */
851 : if (format[0] == '3' && format[1] == '2')
852 : format += 2;
853 : else if (format[0] == '6' && format[1] == '4')
854 : {
855 : format += 2;
856 : longlongflag = 1;
857 : }
858 : else
859 : {
860 : #if SIZEOF_VOID_P == SIZEOF_LONG
861 : longflag = 1;
862 : #elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
863 : longlongflag = 1;
864 : #else
865 : #error "cannot find integer type of the same size as intptr_t"
866 : #endif
867 : }
868 : goto nextch1;
869 : #endif
870 0 : case 'l':
871 0 : if (longflag)
872 0 : longlongflag = 1;
873 : else
874 0 : longflag = 1;
875 0 : goto nextch1;
876 0 : case 'z':
877 : #if SIZEOF_SIZE_T == SIZEOF_LONG
878 0 : longflag = 1;
879 : #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
880 : longlongflag = 1;
881 : #else
882 : #error "cannot find integer type of the same size as size_t"
883 : #endif
884 0 : goto nextch1;
885 0 : case 'h':
886 : case '\'':
887 : /* ignore these */
888 0 : goto nextch1;
889 0 : case 'd':
890 : case 'i':
891 : case 'o':
892 : case 'u':
893 : case 'x':
894 : case 'X':
895 0 : if (fmtpos)
896 : {
897 : PrintfArgType atype;
898 :
899 0 : if (longlongflag)
900 0 : atype = ATYPE_LONGLONG;
901 0 : else if (longflag)
902 0 : atype = ATYPE_LONG;
903 : else
904 0 : atype = ATYPE_INT;
905 0 : if (argtypes[fmtpos] &&
906 0 : argtypes[fmtpos] != atype)
907 0 : return false;
908 0 : argtypes[fmtpos] = atype;
909 0 : last_dollar = Max(last_dollar, fmtpos);
910 : }
911 : else
912 0 : return false; /* non-dollar conversion spec */
913 0 : break;
914 0 : case 'c':
915 0 : if (fmtpos)
916 : {
917 0 : if (argtypes[fmtpos] &&
918 0 : argtypes[fmtpos] != ATYPE_INT)
919 0 : return false;
920 0 : argtypes[fmtpos] = ATYPE_INT;
921 0 : last_dollar = Max(last_dollar, fmtpos);
922 : }
923 : else
924 0 : return false; /* non-dollar conversion spec */
925 0 : break;
926 0 : case 's':
927 : case 'p':
928 0 : if (fmtpos)
929 : {
930 0 : if (argtypes[fmtpos] &&
931 0 : argtypes[fmtpos] != ATYPE_CHARPTR)
932 0 : return false;
933 0 : argtypes[fmtpos] = ATYPE_CHARPTR;
934 0 : last_dollar = Max(last_dollar, fmtpos);
935 : }
936 : else
937 0 : return false; /* non-dollar conversion spec */
938 0 : break;
939 0 : case 'e':
940 : case 'E':
941 : case 'f':
942 : case 'g':
943 : case 'G':
944 0 : if (fmtpos)
945 : {
946 0 : if (argtypes[fmtpos] &&
947 0 : argtypes[fmtpos] != ATYPE_DOUBLE)
948 0 : return false;
949 0 : argtypes[fmtpos] = ATYPE_DOUBLE;
950 0 : last_dollar = Max(last_dollar, fmtpos);
951 : }
952 : else
953 0 : return false; /* non-dollar conversion spec */
954 0 : break;
955 0 : case 'm':
956 : case '%':
957 0 : break;
958 0 : default:
959 0 : return false; /* bogus format string */
960 : }
961 :
962 : /*
963 : * If we finish the spec with afterstar still set, there's a
964 : * non-dollar star in there.
965 : */
966 0 : if (afterstar)
967 0 : return false; /* non-dollar conversion spec */
968 : }
969 :
970 : /*
971 : * Format appears valid so far, so collect the arguments in physical
972 : * order. (Since we rejected any non-dollar specs that would have
973 : * collected arguments, we know that dopr() hasn't collected any yet.)
974 : */
975 0 : for (i = 1; i <= last_dollar; i++)
976 : {
977 0 : switch (argtypes[i])
978 : {
979 0 : case ATYPE_NONE:
980 0 : return false;
981 0 : case ATYPE_INT:
982 0 : argvalues[i].i = va_arg(args, int);
983 0 : break;
984 0 : case ATYPE_LONG:
985 0 : argvalues[i].l = va_arg(args, long);
986 0 : break;
987 0 : case ATYPE_LONGLONG:
988 0 : argvalues[i].ll = va_arg(args, long long);
989 0 : break;
990 0 : case ATYPE_DOUBLE:
991 0 : argvalues[i].d = va_arg(args, double);
992 0 : break;
993 0 : case ATYPE_CHARPTR:
994 0 : argvalues[i].cptr = va_arg(args, char *);
995 0 : break;
996 : }
997 0 : }
998 :
999 0 : return true;
1000 : }
1001 :
1002 : static void
1003 1837796 : fmtstr(const char *value, int leftjust, int minlen, int maxwidth,
1004 : int pointflag, PrintfTarget *target)
1005 : {
1006 : int padlen,
1007 : vallen; /* amount to pad */
1008 :
1009 : /*
1010 : * If a maxwidth (precision) is specified, we must not fetch more bytes
1011 : * than that.
1012 : */
1013 1837796 : if (pointflag)
1014 20410 : vallen = strnlen(value, maxwidth);
1015 : else
1016 1817386 : vallen = strlen(value);
1017 :
1018 1837796 : padlen = compute_padlen(minlen, vallen, leftjust);
1019 :
1020 1837796 : if (padlen > 0)
1021 : {
1022 549152 : dopr_outchmulti(' ', padlen, target);
1023 549152 : padlen = 0;
1024 : }
1025 :
1026 1837796 : dostr(value, vallen, target);
1027 :
1028 1837796 : trailing_pad(padlen, target);
1029 1837796 : }
1030 :
1031 : static void
1032 82 : fmtptr(const void *value, PrintfTarget *target)
1033 : {
1034 : int vallen;
1035 : char convert[64];
1036 :
1037 : /* we rely on regular C library's snprintf to do the basic conversion */
1038 82 : vallen = snprintf(convert, sizeof(convert), "%p", value);
1039 82 : if (vallen < 0)
1040 0 : target->failed = true;
1041 : else
1042 82 : dostr(convert, vallen, target);
1043 82 : }
1044 :
1045 : static void
1046 416196608 : fmtint(long long value, char type, int forcesign, int leftjust,
1047 : int minlen, int zpad, int precision, int pointflag,
1048 : PrintfTarget *target)
1049 : {
1050 : unsigned long long uvalue;
1051 : int base;
1052 : int dosign;
1053 416196608 : const char *cvt = "0123456789abcdef";
1054 416196608 : int signvalue = 0;
1055 : char convert[64];
1056 416196608 : int vallen = 0;
1057 : int padlen; /* amount to pad */
1058 : int zeropad; /* extra leading zeroes */
1059 :
1060 416196608 : switch (type)
1061 : {
1062 283422364 : case 'd':
1063 : case 'i':
1064 283422364 : base = 10;
1065 283422364 : dosign = 1;
1066 283422364 : break;
1067 10974 : case 'o':
1068 10974 : base = 8;
1069 10974 : dosign = 0;
1070 10974 : break;
1071 114726902 : case 'u':
1072 114726902 : base = 10;
1073 114726902 : dosign = 0;
1074 114726902 : break;
1075 57118 : case 'x':
1076 57118 : base = 16;
1077 57118 : dosign = 0;
1078 57118 : break;
1079 17979250 : case 'X':
1080 17979250 : cvt = "0123456789ABCDEF";
1081 17979250 : base = 16;
1082 17979250 : dosign = 0;
1083 17979250 : break;
1084 0 : default:
1085 0 : return; /* keep compiler quiet */
1086 : }
1087 :
1088 : /* disable MSVC warning about applying unary minus to an unsigned value */
1089 : #ifdef _MSC_VER
1090 : #pragma warning(push)
1091 : #pragma warning(disable: 4146)
1092 : #endif
1093 : /* Handle +/- */
1094 416196608 : if (dosign && adjust_sign((value < 0), forcesign, &signvalue))
1095 28819534 : uvalue = -(unsigned long long) value;
1096 : else
1097 387377074 : uvalue = (unsigned long long) value;
1098 : #ifdef _MSC_VER
1099 : #pragma warning(pop)
1100 : #endif
1101 :
1102 : /*
1103 : * SUS: the result of converting 0 with an explicit precision of 0 is no
1104 : * characters
1105 : */
1106 416196608 : if (value == 0 && pointflag && precision == 0)
1107 0 : vallen = 0;
1108 : else
1109 : {
1110 : /*
1111 : * Convert integer to string. We special-case each of the possible
1112 : * base values so as to avoid general-purpose divisions. On most
1113 : * machines, division by a fixed constant can be done much more
1114 : * cheaply than a general divide.
1115 : */
1116 416196608 : if (base == 10)
1117 : {
1118 : do
1119 : {
1120 688143540 : convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10];
1121 688143540 : uvalue = uvalue / 10;
1122 688143540 : } while (uvalue);
1123 : }
1124 18047342 : else if (base == 16)
1125 : {
1126 : do
1127 : {
1128 68259612 : convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16];
1129 68259612 : uvalue = uvalue / 16;
1130 68259612 : } while (uvalue);
1131 : }
1132 : else /* base == 8 */
1133 : {
1134 : do
1135 : {
1136 32922 : convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8];
1137 32922 : uvalue = uvalue / 8;
1138 32922 : } while (uvalue);
1139 : }
1140 : }
1141 :
1142 416196608 : zeropad = Max(0, precision - vallen);
1143 :
1144 416196608 : padlen = compute_padlen(minlen, vallen + zeropad, leftjust);
1145 :
1146 416196612 : leading_pad(zpad, signvalue, &padlen, target);
1147 :
1148 416196610 : if (zeropad > 0)
1149 0 : dopr_outchmulti('0', zeropad, target);
1150 :
1151 416196610 : dostr(convert + sizeof(convert) - vallen, vallen, target);
1152 :
1153 416196608 : trailing_pad(padlen, target);
1154 : }
1155 :
1156 : static void
1157 39050 : fmtchar(int value, int leftjust, int minlen, PrintfTarget *target)
1158 : {
1159 : int padlen; /* amount to pad */
1160 :
1161 39050 : padlen = compute_padlen(minlen, 1, leftjust);
1162 :
1163 39050 : if (padlen > 0)
1164 : {
1165 42 : dopr_outchmulti(' ', padlen, target);
1166 42 : padlen = 0;
1167 : }
1168 :
1169 39050 : dopr_outch(value, target);
1170 :
1171 39050 : trailing_pad(padlen, target);
1172 39050 : }
1173 :
1174 : static void
1175 1018664 : fmtfloat(double value, char type, int forcesign, int leftjust,
1176 : int minlen, int zpad, int precision, int pointflag,
1177 : PrintfTarget *target)
1178 : {
1179 1018664 : int signvalue = 0;
1180 : int prec;
1181 : int vallen;
1182 : char fmt[8];
1183 : char convert[1024];
1184 1018664 : int zeropadlen = 0; /* amount to pad with zeroes */
1185 : int padlen; /* amount to pad with spaces */
1186 :
1187 : /*
1188 : * We rely on the regular C library's snprintf to do the basic conversion,
1189 : * then handle padding considerations here.
1190 : *
1191 : * The dynamic range of "double" is about 1E+-308 for IEEE math, and not
1192 : * too wildly more than that with other hardware. In "f" format, snprintf
1193 : * could therefore generate at most 308 characters to the left of the
1194 : * decimal point; while we need to allow the precision to get as high as
1195 : * 308+17 to ensure that we don't truncate significant digits from very
1196 : * small values. To handle both these extremes, we use a buffer of 1024
1197 : * bytes and limit requested precision to 350 digits; this should prevent
1198 : * buffer overrun even with non-IEEE math. If the original precision
1199 : * request was more than 350, separately pad with zeroes.
1200 : *
1201 : * We handle infinities and NaNs specially to ensure platform-independent
1202 : * output.
1203 : */
1204 1018664 : if (precision < 0) /* cover possible overflow of "accum" */
1205 0 : precision = 0;
1206 1018664 : prec = Min(precision, 350);
1207 :
1208 1018664 : if (isnan(value))
1209 : {
1210 48 : strcpy(convert, "NaN");
1211 48 : vallen = 3;
1212 : /* no zero padding, regardless of precision spec */
1213 : }
1214 : else
1215 : {
1216 : /*
1217 : * Handle sign (NaNs have no sign, so we don't do this in the case
1218 : * above). "value < 0.0" will not be true for IEEE minus zero, so we
1219 : * detect that by looking for the case where value equals 0.0
1220 : * according to == but not according to memcmp.
1221 : */
1222 : static const double dzero = 0.0;
1223 :
1224 2023180 : if (adjust_sign((value < 0.0 ||
1225 1004564 : (value == 0.0 &&
1226 362842 : memcmp(&value, &dzero, sizeof(double)) != 0)),
1227 : forcesign, &signvalue))
1228 14052 : value = -value;
1229 :
1230 1018616 : if (isinf(value))
1231 : {
1232 96 : strcpy(convert, "Infinity");
1233 96 : vallen = 8;
1234 : /* no zero padding, regardless of precision spec */
1235 : }
1236 1018520 : else if (pointflag)
1237 : {
1238 608518 : zeropadlen = precision - prec;
1239 608518 : fmt[0] = '%';
1240 608518 : fmt[1] = '.';
1241 608518 : fmt[2] = '*';
1242 608518 : fmt[3] = type;
1243 608518 : fmt[4] = '\0';
1244 608518 : vallen = snprintf(convert, sizeof(convert), fmt, prec, value);
1245 : }
1246 : else
1247 : {
1248 410002 : fmt[0] = '%';
1249 410002 : fmt[1] = type;
1250 410002 : fmt[2] = '\0';
1251 410002 : vallen = snprintf(convert, sizeof(convert), fmt, value);
1252 : }
1253 1018616 : if (vallen < 0)
1254 0 : goto fail;
1255 :
1256 : /*
1257 : * Windows, alone among our supported platforms, likes to emit
1258 : * three-digit exponent fields even when two digits would do. Hack
1259 : * such results to look like the way everyone else does it.
1260 : */
1261 : #ifdef WIN32
1262 : if (vallen >= 6 &&
1263 : convert[vallen - 5] == 'e' &&
1264 : convert[vallen - 3] == '0')
1265 : {
1266 : convert[vallen - 3] = convert[vallen - 2];
1267 : convert[vallen - 2] = convert[vallen - 1];
1268 : vallen--;
1269 : }
1270 : #endif
1271 : }
1272 :
1273 1018664 : padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust);
1274 :
1275 1018664 : leading_pad(zpad, signvalue, &padlen, target);
1276 :
1277 1018664 : if (zeropadlen > 0)
1278 : {
1279 : /* If 'e' or 'E' format, inject zeroes before the exponent */
1280 0 : char *epos = strrchr(convert, 'e');
1281 :
1282 0 : if (!epos)
1283 0 : epos = strrchr(convert, 'E');
1284 0 : if (epos)
1285 : {
1286 : /* pad before exponent */
1287 0 : dostr(convert, epos - convert, target);
1288 0 : dopr_outchmulti('0', zeropadlen, target);
1289 0 : dostr(epos, vallen - (epos - convert), target);
1290 : }
1291 : else
1292 : {
1293 : /* no exponent, pad after the digits */
1294 0 : dostr(convert, vallen, target);
1295 0 : dopr_outchmulti('0', zeropadlen, target);
1296 : }
1297 : }
1298 : else
1299 : {
1300 : /* no zero padding, just emit the number as-is */
1301 1018664 : dostr(convert, vallen, target);
1302 : }
1303 :
1304 1018664 : trailing_pad(padlen, target);
1305 1018664 : return;
1306 :
1307 0 : fail:
1308 0 : target->failed = true;
1309 : }
1310 :
1311 : /*
1312 : * Nonstandard entry point to print a double value efficiently.
1313 : *
1314 : * This is approximately equivalent to strfromd(), but has an API more
1315 : * adapted to what float8out() wants. The behavior is like snprintf()
1316 : * with a format of "%.ng", where n is the specified precision.
1317 : * However, the target buffer must be nonempty (i.e. count > 0), and
1318 : * the precision is silently bounded to a sane range.
1319 : */
1320 : int
1321 230054 : pg_strfromd(char *str, size_t count, int precision, double value)
1322 : {
1323 : PrintfTarget target;
1324 230054 : int signvalue = 0;
1325 : int vallen;
1326 : char fmt[8];
1327 : char convert[64];
1328 :
1329 : /* Set up the target like pg_snprintf, but require nonempty buffer */
1330 : Assert(count > 0);
1331 230054 : target.bufstart = target.bufptr = str;
1332 230054 : target.bufend = str + count - 1;
1333 230054 : target.stream = NULL;
1334 230054 : target.nchars = 0;
1335 230054 : target.failed = false;
1336 :
1337 : /*
1338 : * We bound precision to a reasonable range; the combination of this and
1339 : * the knowledge that we're using "g" format without padding allows the
1340 : * convert[] buffer to be reasonably small.
1341 : */
1342 230054 : if (precision < 1)
1343 0 : precision = 1;
1344 230054 : else if (precision > 32)
1345 0 : precision = 32;
1346 :
1347 : /*
1348 : * The rest is just an inlined version of the fmtfloat() logic above,
1349 : * simplified using the knowledge that no padding is wanted.
1350 : */
1351 230054 : if (isnan(value))
1352 : {
1353 12084 : strcpy(convert, "NaN");
1354 12084 : vallen = 3;
1355 : }
1356 : else
1357 : {
1358 : static const double dzero = 0.0;
1359 :
1360 217970 : if (value < 0.0 ||
1361 185576 : (value == 0.0 &&
1362 26866 : memcmp(&value, &dzero, sizeof(double)) != 0))
1363 : {
1364 32460 : signvalue = '-';
1365 32460 : value = -value;
1366 : }
1367 :
1368 217970 : if (isinf(value))
1369 : {
1370 7092 : strcpy(convert, "Infinity");
1371 7092 : vallen = 8;
1372 : }
1373 : else
1374 : {
1375 210878 : fmt[0] = '%';
1376 210878 : fmt[1] = '.';
1377 210878 : fmt[2] = '*';
1378 210878 : fmt[3] = 'g';
1379 210878 : fmt[4] = '\0';
1380 210878 : vallen = snprintf(convert, sizeof(convert), fmt, precision, value);
1381 210878 : if (vallen < 0)
1382 : {
1383 0 : target.failed = true;
1384 0 : goto fail;
1385 : }
1386 :
1387 : #ifdef WIN32
1388 : if (vallen >= 6 &&
1389 : convert[vallen - 5] == 'e' &&
1390 : convert[vallen - 3] == '0')
1391 : {
1392 : convert[vallen - 3] = convert[vallen - 2];
1393 : convert[vallen - 2] = convert[vallen - 1];
1394 : vallen--;
1395 : }
1396 : #endif
1397 : }
1398 : }
1399 :
1400 230054 : if (signvalue)
1401 32460 : dopr_outch(signvalue, &target);
1402 :
1403 230054 : dostr(convert, vallen, &target);
1404 :
1405 230054 : fail:
1406 230054 : *(target.bufptr) = '\0';
1407 460108 : return target.failed ? -1 : (target.bufptr - target.bufstart
1408 230054 : + target.nchars);
1409 : }
1410 :
1411 :
1412 : static void
1413 965956032 : dostr(const char *str, int slen, PrintfTarget *target)
1414 : {
1415 : /* fast path for common case of slen == 1 */
1416 965956032 : if (slen == 1)
1417 : {
1418 413036356 : dopr_outch(*str, target);
1419 413036358 : return;
1420 : }
1421 :
1422 1103057488 : while (slen > 0)
1423 : {
1424 : int avail;
1425 :
1426 550885548 : if (target->bufend != NULL)
1427 543547100 : avail = target->bufend - target->bufptr;
1428 : else
1429 7338448 : avail = slen;
1430 550885548 : if (avail <= 0)
1431 : {
1432 : /* buffer full, can we dump to stream? */
1433 748224 : if (target->stream == NULL)
1434 : {
1435 747736 : target->nchars += slen; /* no, lose the data */
1436 747736 : return;
1437 : }
1438 488 : flushbuffer(target);
1439 488 : continue;
1440 : }
1441 550137324 : avail = Min(avail, slen);
1442 550137324 : memmove(target->bufptr, str, avail);
1443 550137324 : target->bufptr += avail;
1444 550137324 : str += avail;
1445 550137324 : slen -= avail;
1446 : }
1447 : }
1448 :
1449 : static void
1450 450421062 : dopr_outch(int c, PrintfTarget *target)
1451 : {
1452 450421062 : if (target->bufend != NULL && target->bufptr >= target->bufend)
1453 : {
1454 : /* buffer full, can we dump to stream? */
1455 238936 : if (target->stream == NULL)
1456 : {
1457 238936 : target->nchars++; /* no, lose the data */
1458 238936 : return;
1459 : }
1460 0 : flushbuffer(target);
1461 : }
1462 450182128 : *(target->bufptr++) = c;
1463 : }
1464 :
1465 : static void
1466 11242670 : dopr_outchmulti(int c, int slen, PrintfTarget *target)
1467 : {
1468 : /* fast path for common case of slen == 1 */
1469 11242670 : if (slen == 1)
1470 : {
1471 8195258 : dopr_outch(c, target);
1472 8195258 : return;
1473 : }
1474 :
1475 6095046 : while (slen > 0)
1476 : {
1477 : int avail;
1478 :
1479 3047634 : if (target->bufend != NULL)
1480 3031692 : avail = target->bufend - target->bufptr;
1481 : else
1482 15942 : avail = slen;
1483 3047634 : if (avail <= 0)
1484 : {
1485 : /* buffer full, can we dump to stream? */
1486 114 : if (target->stream == NULL)
1487 : {
1488 0 : target->nchars += slen; /* no, lose the data */
1489 0 : return;
1490 : }
1491 114 : flushbuffer(target);
1492 114 : continue;
1493 : }
1494 3047520 : avail = Min(avail, slen);
1495 3047520 : memset(target->bufptr, c, avail);
1496 3047520 : target->bufptr += avail;
1497 3047520 : slen -= avail;
1498 : }
1499 : }
1500 :
1501 :
1502 : static int
1503 284440982 : adjust_sign(int is_negative, int forcesign, int *signvalue)
1504 : {
1505 284440982 : if (is_negative)
1506 : {
1507 28833586 : *signvalue = '-';
1508 28833586 : return true;
1509 : }
1510 255607396 : else if (forcesign)
1511 204 : *signvalue = '+';
1512 255607396 : return false;
1513 : }
1514 :
1515 :
1516 : static int
1517 419092122 : compute_padlen(int minlen, int vallen, int leftjust)
1518 : {
1519 : int padlen;
1520 :
1521 419092122 : padlen = minlen - vallen;
1522 419092122 : if (padlen < 0)
1523 388078220 : padlen = 0;
1524 419092122 : if (leftjust)
1525 1016230 : padlen = -padlen;
1526 419092122 : return padlen;
1527 : }
1528 :
1529 :
1530 : static void
1531 417215274 : leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target)
1532 : {
1533 : int maxpad;
1534 :
1535 417215274 : if (*padlen > 0 && zpad)
1536 : {
1537 8449548 : if (signvalue)
1538 : {
1539 192 : dopr_outch(signvalue, target);
1540 192 : --(*padlen);
1541 192 : signvalue = 0;
1542 : }
1543 8449548 : if (*padlen > 0)
1544 : {
1545 8449530 : dopr_outchmulti(zpad, *padlen, target);
1546 8449530 : *padlen = 0;
1547 : }
1548 : }
1549 417215274 : maxpad = (signvalue != 0);
1550 417215274 : if (*padlen > maxpad)
1551 : {
1552 1551492 : dopr_outchmulti(' ', *padlen - maxpad, target);
1553 1551492 : *padlen = maxpad;
1554 : }
1555 417215274 : if (signvalue)
1556 : {
1557 28833598 : dopr_outch(signvalue, target);
1558 28833598 : if (*padlen > 0)
1559 0 : --(*padlen);
1560 28833598 : else if (*padlen < 0)
1561 0 : ++(*padlen);
1562 : }
1563 417215274 : }
1564 :
1565 :
1566 : static void
1567 419092116 : trailing_pad(int padlen, PrintfTarget *target)
1568 : {
1569 419092116 : if (padlen < 0)
1570 692454 : dopr_outchmulti(' ', -padlen, target);
1571 419092116 : }
|