| 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 |
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 |
if (count == 0) |
| 186 |
{ |
| 187 |
str = onebyte; |
| 188 |
count = 1; |
| 189 |
} |
| 190 |
target.bufstart = target.bufptr = str; |
| 191 |
target.bufend = str + count - 1; |
| 192 |
target.stream = NULL; |
| 193 |
target.nchars = 0; |
| 194 |
target.failed = false; |
| 195 |
dopr(&target, fmt, args); |
| 196 |
*(target.bufptr) = '\0'; |
| 197 |
return target.failed ? -1 : (target.bufptr - target.bufstart |
| 198 |
+ target.nchars); |
| 199 |
} |
| 200 |
|
| 201 |
int |
| 202 |
pg_snprintf(char *str, size_t count, const char *fmt,...) |
| 203 |
{ |
| 204 |
int len; |
| 205 |
va_list args; |
| 206 |
|
| 207 |
va_start(args, fmt); |
| 208 |
len = pg_vsnprintf(str, count, fmt, args); |
| 209 |
va_end(args); |
| 210 |
return len; |
| 211 |
} |
| 212 |
|
| 213 |
int |
| 214 |
pg_vsprintf(char *str, const char *fmt, va_list args) |
| 215 |
{ |
| 216 |
PrintfTarget target; |
| 217 |
|
| 218 |
target.bufstart = target.bufptr = str; |
| 219 |
target.bufend = NULL; |
| 220 |
target.stream = NULL; |
| 221 |
target.nchars = 0; /* not really used in this case */ |
| 222 |
target.failed = false; |
| 223 |
dopr(&target, fmt, args); |
| 224 |
*(target.bufptr) = '\0'; |
| 225 |
return target.failed ? -1 : (target.bufptr - target.bufstart |
| 226 |
+ target.nchars); |
| 227 |
} |
| 228 |
|
| 229 |
int |
| 230 |
pg_sprintf(char *str, const char *fmt,...) |
| 231 |
{ |
| 232 |
int len; |
| 233 |
va_list args; |
| 234 |
|
| 235 |
va_start(args, fmt); |
| 236 |
len = pg_vsprintf(str, fmt, args); |
| 237 |
va_end(args); |
| 238 |
return len; |
| 239 |
} |
| 240 |
|
| 241 |
int |
| 242 |
pg_vfprintf(FILE *stream, const char *fmt, va_list args) |
| 243 |
{ |
| 244 |
PrintfTarget target; |
| 245 |
char buffer[1024]; /* size is arbitrary */ |
| 246 |
|
| 247 |
if (stream == NULL) |
| 248 |
{ |
| 249 |
errno = EINVAL; |
| 250 |
return -1; |
| 251 |
} |
| 252 |
target.bufstart = target.bufptr = buffer; |
| 253 |
target.bufend = buffer + sizeof(buffer); /* use the whole buffer */ |
| 254 |
target.stream = stream; |
| 255 |
target.nchars = 0; |
| 256 |
target.failed = false; |
| 257 |
dopr(&target, fmt, args); |
| 258 |
/* dump any remaining buffer contents */ |
| 259 |
flushbuffer(&target); |
| 260 |
return target.failed ? -1 : target.nchars; |
| 261 |
} |
| 262 |
|
| 263 |
int |
| 264 |
pg_fprintf(FILE *stream, const char *fmt,...) |
| 265 |
{ |
| 266 |
int len; |
| 267 |
va_list args; |
| 268 |
|
| 269 |
va_start(args, fmt); |
| 270 |
len = pg_vfprintf(stream, fmt, args); |
| 271 |
va_end(args); |
| 272 |
return len; |
| 273 |
} |
| 274 |
|
| 275 |
int |
| 276 |
pg_vprintf(const char *fmt, va_list args) |
| 277 |
{ |
| 278 |
return pg_vfprintf(stdout, fmt, args); |
| 279 |
} |
| 280 |
|
| 281 |
int |
| 282 |
pg_printf(const char *fmt,...) |
| 283 |
{ |
| 284 |
int len; |
| 285 |
va_list args; |
| 286 |
|
| 287 |
va_start(args, fmt); |
| 288 |
len = pg_vfprintf(stdout, fmt, args); |
| 289 |
va_end(args); |
| 290 |
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 |
flushbuffer(PrintfTarget *target) |
| 299 |
{ |
| 300 |
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 |
if (!target->failed && nc > 0) |
| 307 |
{ |
| 308 |
size_t written; |
| 309 |
|
| 310 |
written = fwrite(target->bufstart, 1, nc, target->stream); |
| 311 |
target->nchars += written; |
| 312 |
if (written != nc) |
| 313 |
target->failed = true; |
| 314 |
} |
| 315 |
target->bufptr = target->bufstart; |
| 316 |
} |
| 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, but since adopted by some other |
| 342 |
* platforms), it's a good bit faster than the equivalent manual loop. |
| 343 |
* Use it if possible, and if it doesn't exist, use this replacement. |
| 344 |
* |
| 345 |
* Note: glibc declares this as returning "char *", but that would require |
| 346 |
* casting away const internally, so we don't follow that detail. |
| 347 |
* |
| 348 |
* Note: macOS has this too as of Sequoia 15.4, but it's hidden behind |
| 349 |
* a deployment-target check that causes compile errors if the deployment |
| 350 |
* target isn't high enough. So !HAVE_DECL_STRCHRNUL may mean "yes it's |
| 351 |
* declared, but it doesn't compile". To avoid failing in that scenario, |
| 352 |
* use a macro to avoid matching <string.h>'s name. |
| 353 |
*/ |
| 354 |
#if !HAVE_DECL_STRCHRNUL |
| 355 |
|
| 356 |
#define strchrnul pg_strchrnul |
| 357 |
|
| 358 |
static inline const char * |
| 359 |
strchrnul(const char *s, int c) |
| 360 |
{ |
| 361 |
while (*s != '\0' && *s != c) |
| 362 |
s++; |
| 363 |
return s; |
| 364 |
} |
| 365 |
|
| 366 |
#endif /* !HAVE_DECL_STRCHRNUL */ |
| 367 |
|
| 368 |
|
| 369 |
/* |
| 370 |
* dopr(): the guts of *printf for all cases. |
| 371 |
*/ |
| 372 |
static void |
| 373 |
dopr(PrintfTarget *target, const char *format, va_list args) |
| 374 |
{ |
| 375 |
int save_errno = errno; |
| 376 |
const char *first_pct = NULL; |
| 377 |
int ch; |
| 378 |
bool have_dollar; |
| 379 |
bool have_star; |
| 380 |
bool afterstar; |
| 381 |
int accum; |
| 382 |
int longlongflag; |
| 383 |
int longflag; |
| 384 |
int pointflag; |
| 385 |
int leftjust; |
| 386 |
int fieldwidth; |
| 387 |
int precision; |
| 388 |
int zpad; |
| 389 |
int forcesign; |
| 390 |
int fmtpos; |
| 391 |
int cvalue; |
| 392 |
long long numvalue; |
| 393 |
double fvalue; |
| 394 |
const char *strvalue; |
| 395 |
PrintfArgValue argvalues[PG_NL_ARGMAX + 1]; |
| 396 |
|
| 397 |
/* |
| 398 |
* Initially, we suppose the format string does not use %n$. The first |
| 399 |
* time we come to a conversion spec that has that, we'll call |
| 400 |
* find_arguments() to check for consistent use of %n$ and fill the |
| 401 |
* argvalues array with the argument values in the correct order. |
| 402 |
*/ |
| 403 |
have_dollar = false; |
| 404 |
|
| 405 |
while (*format != '\0') |
| 406 |
{ |
| 407 |
/* Locate next conversion specifier */ |
| 408 |
if (*format != '%') |
| 409 |
{ |
| 410 |
/* Scan to next '%' or end of string */ |
| 411 |
const char *next_pct = strchrnul(format + 1, '%'); |
| 412 |
|
| 413 |
/* Dump literal data we just scanned over */ |
| 414 |
dostr(format, next_pct - format, target); |
| 415 |
if (target->failed) |
| 416 |
break; |
| 417 |
|
| 418 |
if (*next_pct == '\0') |
| 419 |
break; |
| 420 |
format = next_pct; |
| 421 |
} |
| 422 |
|
| 423 |
/* |
| 424 |
* Remember start of first conversion spec; if we find %n$, then it's |
| 425 |
* sufficient for find_arguments() to start here, without rescanning |
| 426 |
* earlier literal text. |
| 427 |
*/ |
| 428 |
if (first_pct == NULL) |
| 429 |
first_pct = format; |
| 430 |
|
| 431 |
/* Process conversion spec starting at *format */ |
| 432 |
format++; |
| 433 |
|
| 434 |
/* Fast path for conversion spec that is exactly %s */ |
| 435 |
if (*format == 's') |
| 436 |
{ |
| 437 |
format++; |
| 438 |
strvalue = va_arg(args, char *); |
| 439 |
if (strvalue == NULL) |
| 440 |
strvalue = "(null)"; |
| 441 |
dostr(strvalue, strlen(strvalue), target); |
| 442 |
if (target->failed) |
| 443 |
break; |
| 444 |
continue; |
| 445 |
} |
| 446 |
|
| 447 |
fieldwidth = precision = zpad = leftjust = forcesign = 0; |
| 448 |
longflag = longlongflag = pointflag = 0; |
| 449 |
fmtpos = accum = 0; |
| 450 |
have_star = afterstar = false; |
| 451 |
nextch2: |
| 452 |
ch = *format++; |
| 453 |
switch (ch) |
| 454 |
{ |
| 455 |
case '-': |
| 456 |
leftjust = 1; |
| 457 |
goto nextch2; |
| 458 |
case '+': |
| 459 |
forcesign = 1; |
| 460 |
goto nextch2; |
| 461 |
case '0': |
| 462 |
/* set zero padding if no nonzero digits yet */ |
| 463 |
if (accum == 0 && !pointflag) |
| 464 |
zpad = '0'; |
| 465 |
/* FALL THRU */ |
| 466 |
case '1': |
| 467 |
case '2': |
| 468 |
case '3': |
| 469 |
case '4': |
| 470 |
case '5': |
| 471 |
case '6': |
| 472 |
case '7': |
| 473 |
case '8': |
| 474 |
case '9': |
| 475 |
accum = accum * 10 + (ch - '0'); |
| 476 |
goto nextch2; |
| 477 |
case '.': |
| 478 |
if (have_star) |
| 479 |
have_star = false; |
| 480 |
else |
| 481 |
fieldwidth = accum; |
| 482 |
pointflag = 1; |
| 483 |
accum = 0; |
| 484 |
goto nextch2; |
| 485 |
case '*': |
| 486 |
if (have_dollar) |
| 487 |
{ |
| 488 |
/* |
| 489 |
* We'll process value after reading n$. Note it's OK to |
| 490 |
* assume have_dollar is set correctly, because in a valid |
| 491 |
* format string the initial % must have had n$ if * does. |
| 492 |
*/ |
| 493 |
afterstar = true; |
| 494 |
} |
| 495 |
else |
| 496 |
{ |
| 497 |
/* fetch and process value now */ |
| 498 |
int starval = va_arg(args, int); |
| 499 |
|
| 500 |
if (pointflag) |
| 501 |
{ |
| 502 |
precision = starval; |
| 503 |
if (precision < 0) |
| 504 |
{ |
| 505 |
precision = 0; |
| 506 |
pointflag = 0; |
| 507 |
} |
| 508 |
} |
| 509 |
else |
| 510 |
{ |
| 511 |
fieldwidth = starval; |
| 512 |
if (fieldwidth < 0) |
| 513 |
{ |
| 514 |
leftjust = 1; |
| 515 |
fieldwidth = -fieldwidth; |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
have_star = true; |
| 520 |
accum = 0; |
| 521 |
goto nextch2; |
| 522 |
case '$': |
| 523 |
/* First dollar sign? */ |
| 524 |
if (!have_dollar) |
| 525 |
{ |
| 526 |
/* Yup, so examine all conversion specs in format */ |
| 527 |
if (!find_arguments(first_pct, args, argvalues)) |
| 528 |
goto bad_format; |
| 529 |
have_dollar = true; |
| 530 |
} |
| 531 |
if (afterstar) |
| 532 |
{ |
| 533 |
/* fetch and process star value */ |
| 534 |
int starval = argvalues[accum].i; |
| 535 |
|
| 536 |
if (pointflag) |
| 537 |
{ |
| 538 |
precision = starval; |
| 539 |
if (precision < 0) |
| 540 |
{ |
| 541 |
precision = 0; |
| 542 |
pointflag = 0; |
| 543 |
} |
| 544 |
} |
| 545 |
else |
| 546 |
{ |
| 547 |
fieldwidth = starval; |
| 548 |
if (fieldwidth < 0) |
| 549 |
{ |
| 550 |
leftjust = 1; |
| 551 |
fieldwidth = -fieldwidth; |
| 552 |
} |
| 553 |
} |
| 554 |
afterstar = false; |
| 555 |
} |
| 556 |
else |
| 557 |
fmtpos = accum; |
| 558 |
accum = 0; |
| 559 |
goto nextch2; |
| 560 |
#ifdef WIN32 |
| 561 |
case 'I': |
| 562 |
/* Windows PRI*{32,64,PTR} size */ |
| 563 |
if (format[0] == '3' && format[1] == '2') |
| 564 |
format += 2; |
| 565 |
else if (format[0] == '6' && format[1] == '4') |
| 566 |
{ |
| 567 |
format += 2; |
| 568 |
longlongflag = 1; |
| 569 |
} |
| 570 |
else |
| 571 |
{ |
| 572 |
#if SIZEOF_VOID_P == SIZEOF_LONG |
| 573 |
longflag = 1; |
| 574 |
#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG |
| 575 |
longlongflag = 1; |
| 576 |
#else |
| 577 |
#error "cannot find integer type of the same size as intptr_t" |
| 578 |
#endif |
| 579 |
} |
| 580 |
goto nextch2; |
| 581 |
#endif |
| 582 |
case 'l': |
| 583 |
if (longflag) |
| 584 |
longlongflag = 1; |
| 585 |
else |
| 586 |
longflag = 1; |
| 587 |
goto nextch2; |
| 588 |
case 'z': |
| 589 |
#if SIZEOF_SIZE_T == SIZEOF_LONG |
| 590 |
longflag = 1; |
| 591 |
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG |
| 592 |
longlongflag = 1; |
| 593 |
#else |
| 594 |
#error "cannot find integer type of the same size as size_t" |
| 595 |
#endif |
| 596 |
goto nextch2; |
| 597 |
case 'h': |
| 598 |
case '\'': |
| 599 |
/* ignore these */ |
| 600 |
goto nextch2; |
| 601 |
case 'd': |
| 602 |
case 'i': |
| 603 |
if (!have_star) |
| 604 |
{ |
| 605 |
if (pointflag) |
| 606 |
precision = accum; |
| 607 |
else |
| 608 |
fieldwidth = accum; |
| 609 |
} |
| 610 |
if (have_dollar) |
| 611 |
{ |
| 612 |
if (longlongflag) |
| 613 |
numvalue = argvalues[fmtpos].ll; |
| 614 |
else if (longflag) |
| 615 |
numvalue = argvalues[fmtpos].l; |
| 616 |
else |
| 617 |
numvalue = argvalues[fmtpos].i; |
| 618 |
} |
| 619 |
else |
| 620 |
{ |
| 621 |
if (longlongflag) |
| 622 |
numvalue = va_arg(args, long long); |
| 623 |
else if (longflag) |
| 624 |
numvalue = va_arg(args, long); |
| 625 |
else |
| 626 |
numvalue = va_arg(args, int); |
| 627 |
} |
| 628 |
fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad, |
| 629 |
precision, pointflag, target); |
| 630 |
break; |
| 631 |
case 'o': |
| 632 |
case 'u': |
| 633 |
case 'x': |
| 634 |
case 'X': |
| 635 |
if (!have_star) |
| 636 |
{ |
| 637 |
if (pointflag) |
| 638 |
precision = accum; |
| 639 |
else |
| 640 |
fieldwidth = accum; |
| 641 |
} |
| 642 |
if (have_dollar) |
| 643 |
{ |
| 644 |
if (longlongflag) |
| 645 |
numvalue = (unsigned long long) argvalues[fmtpos].ll; |
| 646 |
else if (longflag) |
| 647 |
numvalue = (unsigned long) argvalues[fmtpos].l; |
| 648 |
else |
| 649 |
numvalue = (unsigned int) argvalues[fmtpos].i; |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
if (longlongflag) |
| 654 |
numvalue = (unsigned long long) va_arg(args, long long); |
| 655 |
else if (longflag) |
| 656 |
numvalue = (unsigned long) va_arg(args, long); |
| 657 |
else |
| 658 |
numvalue = (unsigned int) va_arg(args, int); |
| 659 |
} |
| 660 |
fmtint(numvalue, ch, forcesign, leftjust, fieldwidth, zpad, |
| 661 |
precision, pointflag, target); |
| 662 |
break; |
| 663 |
case 'c': |
| 664 |
if (!have_star) |
| 665 |
{ |
| 666 |
if (pointflag) |
| 667 |
precision = accum; |
| 668 |
else |
| 669 |
fieldwidth = accum; |
| 670 |
} |
| 671 |
if (have_dollar) |
| 672 |
cvalue = (unsigned char) argvalues[fmtpos].i; |
| 673 |
else |
| 674 |
cvalue = (unsigned char) va_arg(args, int); |
| 675 |
fmtchar(cvalue, leftjust, fieldwidth, target); |
| 676 |
break; |
| 677 |
case 's': |
| 678 |
if (!have_star) |
| 679 |
{ |
| 680 |
if (pointflag) |
| 681 |
precision = accum; |
| 682 |
else |
| 683 |
fieldwidth = accum; |
| 684 |
} |
| 685 |
if (have_dollar) |
| 686 |
strvalue = argvalues[fmtpos].cptr; |
| 687 |
else |
| 688 |
strvalue = va_arg(args, char *); |
| 689 |
/* If string is NULL, silently substitute "(null)" */ |
| 690 |
if (strvalue == NULL) |
| 691 |
strvalue = "(null)"; |
| 692 |
fmtstr(strvalue, leftjust, fieldwidth, precision, pointflag, |
| 693 |
target); |
| 694 |
break; |
| 695 |
case 'p': |
| 696 |
/* fieldwidth/leftjust are ignored ... */ |
| 697 |
if (have_dollar) |
| 698 |
strvalue = argvalues[fmtpos].cptr; |
| 699 |
else |
| 700 |
strvalue = va_arg(args, char *); |
| 701 |
fmtptr((const void *) strvalue, target); |
| 702 |
break; |
| 703 |
case 'e': |
| 704 |
case 'E': |
| 705 |
case 'f': |
| 706 |
case 'g': |
| 707 |
case 'G': |
| 708 |
if (!have_star) |
| 709 |
{ |
| 710 |
if (pointflag) |
| 711 |
precision = accum; |
| 712 |
else |
| 713 |
fieldwidth = accum; |
| 714 |
} |
| 715 |
if (have_dollar) |
| 716 |
fvalue = argvalues[fmtpos].d; |
| 717 |
else |
| 718 |
fvalue = va_arg(args, double); |
| 719 |
fmtfloat(fvalue, ch, forcesign, leftjust, |
| 720 |
fieldwidth, zpad, |
| 721 |
precision, pointflag, |
| 722 |
target); |
| 723 |
break; |
| 724 |
case 'm': |
| 725 |
{ |
| 726 |
char errbuf[PG_STRERROR_R_BUFLEN]; |
| 727 |
const char *errm = strerror_r(save_errno, |
| 728 |
errbuf, sizeof(errbuf)); |
| 729 |
|
| 730 |
dostr(errm, strlen(errm), target); |
| 731 |
} |
| 732 |
break; |
| 733 |
case '%': |
| 734 |
dopr_outch('%', target); |
| 735 |
break; |
| 736 |
default: |
| 737 |
|
| 738 |
/* |
| 739 |
* Anything else --- in particular, '\0' indicating end of |
| 740 |
* format string --- is bogus. |
| 741 |
*/ |
| 742 |
goto bad_format; |
| 743 |
} |
| 744 |
|
| 745 |
/* Check for failure after each conversion spec */ |
| 746 |
if (target->failed) |
| 747 |
break; |
| 748 |
} |
| 749 |
|
| 750 |
return; |
| 751 |
|
| 752 |
bad_format: |
| 753 |
errno = EINVAL; |
| 754 |
target->failed = true; |
| 755 |
} |
| 756 |
|
| 757 |
/* |
| 758 |
* find_arguments(): sort out the arguments for a format spec with %n$ |
| 759 |
* |
| 760 |
* If format is valid, return true and fill argvalues[i] with the value |
| 761 |
* for the conversion spec that has %i$ or *i$. Else return false. |
| 762 |
*/ |
| 763 |
static bool |
| 764 |
find_arguments(const char *format, va_list args, |
| 765 |
PrintfArgValue *argvalues) |
| 766 |
{ |
| 767 |
int ch; |
| 768 |
bool afterstar; |
| 769 |
int accum; |
| 770 |
int longlongflag; |
| 771 |
int longflag; |
| 772 |
int fmtpos; |
| 773 |
int i; |
| 774 |
int last_dollar = 0; /* Init to "no dollar arguments known" */ |
| 775 |
PrintfArgType argtypes[PG_NL_ARGMAX + 1] = {0}; |
| 776 |
|
| 777 |
/* |
| 778 |
* This loop must accept the same format strings as the one in dopr(). |
| 779 |
* However, we don't need to analyze them to the same level of detail. |
| 780 |
* |
| 781 |
* Since we're only called if there's a dollar-type spec somewhere, we can |
| 782 |
* fail immediately if we find a non-dollar spec. Per the C99 standard, |
| 783 |
* all argument references in the format string must be one or the other. |
| 784 |
*/ |
| 785 |
while (*format != '\0') |
| 786 |
{ |
| 787 |
/* Locate next conversion specifier */ |
| 788 |
if (*format != '%') |
| 789 |
{ |
| 790 |
/* Unlike dopr, we can just quit if there's no more specifiers */ |
| 791 |
format = strchr(format + 1, '%'); |
| 792 |
if (format == NULL) |
| 793 |
break; |
| 794 |
} |
| 795 |
|
| 796 |
/* Process conversion spec starting at *format */ |
| 797 |
format++; |
| 798 |
longflag = longlongflag = 0; |
| 799 |
fmtpos = accum = 0; |
| 800 |
afterstar = false; |
| 801 |
nextch1: |
| 802 |
ch = *format++; |
| 803 |
switch (ch) |
| 804 |
{ |
| 805 |
case '-': |
| 806 |
case '+': |
| 807 |
goto nextch1; |
| 808 |
case '0': |
| 809 |
case '1': |
| 810 |
case '2': |
| 811 |
case '3': |
| 812 |
case '4': |
| 813 |
case '5': |
| 814 |
case '6': |
| 815 |
case '7': |
| 816 |
case '8': |
| 817 |
case '9': |
| 818 |
accum = accum * 10 + (ch - '0'); |
| 819 |
goto nextch1; |
| 820 |
case '.': |
| 821 |
accum = 0; |
| 822 |
goto nextch1; |
| 823 |
case '*': |
| 824 |
if (afterstar) |
| 825 |
return false; /* previous star missing dollar */ |
| 826 |
afterstar = true; |
| 827 |
accum = 0; |
| 828 |
goto nextch1; |
| 829 |
case '$': |
| 830 |
if (accum <= 0 || accum > PG_NL_ARGMAX) |
| 831 |
return false; |
| 832 |
if (afterstar) |
| 833 |
{ |
| 834 |
if (argtypes[accum] && |
| 835 |
argtypes[accum] != ATYPE_INT) |
| 836 |
return false; |
| 837 |
argtypes[accum] = ATYPE_INT; |
| 838 |
last_dollar = Max(last_dollar, accum); |
| 839 |
afterstar = false; |
| 840 |
} |
| 841 |
else |
| 842 |
fmtpos = accum; |
| 843 |
accum = 0; |
| 844 |
goto nextch1; |
| 845 |
#ifdef WIN32 |
| 846 |
case 'I': |
| 847 |
/* Windows PRI*{32,64,PTR} size */ |
| 848 |
if (format[0] == '3' && format[1] == '2') |
| 849 |
format += 2; |
| 850 |
else if (format[0] == '6' && format[1] == '4') |
| 851 |
{ |
| 852 |
format += 2; |
| 853 |
longlongflag = 1; |
| 854 |
} |
| 855 |
else |
| 856 |
{ |
| 857 |
#if SIZEOF_VOID_P == SIZEOF_LONG |
| 858 |
longflag = 1; |
| 859 |
#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG |
| 860 |
longlongflag = 1; |
| 861 |
#else |
| 862 |
#error "cannot find integer type of the same size as intptr_t" |
| 863 |
#endif |
| 864 |
} |
| 865 |
goto nextch1; |
| 866 |
#endif |
| 867 |
case 'l': |
| 868 |
if (longflag) |
| 869 |
longlongflag = 1; |
| 870 |
else |
| 871 |
longflag = 1; |
| 872 |
goto nextch1; |
| 873 |
case 'z': |
| 874 |
#if SIZEOF_SIZE_T == SIZEOF_LONG |
| 875 |
longflag = 1; |
| 876 |
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG |
| 877 |
longlongflag = 1; |
| 878 |
#else |
| 879 |
#error "cannot find integer type of the same size as size_t" |
| 880 |
#endif |
| 881 |
goto nextch1; |
| 882 |
case 'h': |
| 883 |
case '\'': |
| 884 |
/* ignore these */ |
| 885 |
goto nextch1; |
| 886 |
case 'd': |
| 887 |
case 'i': |
| 888 |
case 'o': |
| 889 |
case 'u': |
| 890 |
case 'x': |
| 891 |
case 'X': |
| 892 |
if (fmtpos) |
| 893 |
{ |
| 894 |
PrintfArgType atype; |
| 895 |
|
| 896 |
if (longlongflag) |
| 897 |
atype = ATYPE_LONGLONG; |
| 898 |
else if (longflag) |
| 899 |
atype = ATYPE_LONG; |
| 900 |
else |
| 901 |
atype = ATYPE_INT; |
| 902 |
if (argtypes[fmtpos] && |
| 903 |
argtypes[fmtpos] != atype) |
| 904 |
return false; |
| 905 |
argtypes[fmtpos] = atype; |
| 906 |
last_dollar = Max(last_dollar, fmtpos); |
| 907 |
} |
| 908 |
else |
| 909 |
return false; /* non-dollar conversion spec */ |
| 910 |
break; |
| 911 |
case 'c': |
| 912 |
if (fmtpos) |
| 913 |
{ |
| 914 |
if (argtypes[fmtpos] && |
| 915 |
argtypes[fmtpos] != ATYPE_INT) |
| 916 |
return false; |
| 917 |
argtypes[fmtpos] = ATYPE_INT; |
| 918 |
last_dollar = Max(last_dollar, fmtpos); |
| 919 |
} |
| 920 |
else |
| 921 |
return false; /* non-dollar conversion spec */ |
| 922 |
break; |
| 923 |
case 's': |
| 924 |
case 'p': |
| 925 |
if (fmtpos) |
| 926 |
{ |
| 927 |
if (argtypes[fmtpos] && |
| 928 |
argtypes[fmtpos] != ATYPE_CHARPTR) |
| 929 |
return false; |
| 930 |
argtypes[fmtpos] = ATYPE_CHARPTR; |
| 931 |
last_dollar = Max(last_dollar, fmtpos); |
| 932 |
} |
| 933 |
else |
| 934 |
return false; /* non-dollar conversion spec */ |
| 935 |
break; |
| 936 |
case 'e': |
| 937 |
case 'E': |
| 938 |
case 'f': |
| 939 |
case 'g': |
| 940 |
case 'G': |
| 941 |
if (fmtpos) |
| 942 |
{ |
| 943 |
if (argtypes[fmtpos] && |
| 944 |
argtypes[fmtpos] != ATYPE_DOUBLE) |
| 945 |
return false; |
| 946 |
argtypes[fmtpos] = ATYPE_DOUBLE; |
| 947 |
last_dollar = Max(last_dollar, fmtpos); |
| 948 |
} |
| 949 |
else |
| 950 |
return false; /* non-dollar conversion spec */ |
| 951 |
break; |
| 952 |
case 'm': |
| 953 |
case '%': |
| 954 |
break; |
| 955 |
default: |
| 956 |
return false; /* bogus format string */ |
| 957 |
} |
| 958 |
|
| 959 |
/* |
| 960 |
* If we finish the spec with afterstar still set, there's a |
| 961 |
* non-dollar star in there. |
| 962 |
*/ |
| 963 |
if (afterstar) |
| 964 |
return false; /* non-dollar conversion spec */ |
| 965 |
} |
| 966 |
|
| 967 |
/* |
| 968 |
* Format appears valid so far, so collect the arguments in physical |
| 969 |
* order. (Since we rejected any non-dollar specs that would have |
| 970 |
* collected arguments, we know that dopr() hasn't collected any yet.) |
| 971 |
*/ |
| 972 |
for (i = 1; i <= last_dollar; i++) |
| 973 |
{ |
| 974 |
switch (argtypes[i]) |
| 975 |
{ |
| 976 |
case ATYPE_NONE: |
| 977 |
return false; |
| 978 |
case ATYPE_INT: |
| 979 |
argvalues[i].i = va_arg(args, int); |
| 980 |
break; |
| 981 |
case ATYPE_LONG: |
| 982 |
argvalues[i].l = va_arg(args, long); |
| 983 |
break; |
| 984 |
case ATYPE_LONGLONG: |
| 985 |
argvalues[i].ll = va_arg(args, long long); |
| 986 |
break; |
| 987 |
case ATYPE_DOUBLE: |
| 988 |
argvalues[i].d = va_arg(args, double); |
| 989 |
break; |
| 990 |
case ATYPE_CHARPTR: |
| 991 |
argvalues[i].cptr = va_arg(args, char *); |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
return true; |
| 997 |
} |
| 998 |
|
| 999 |
static void |
| 1000 |
fmtstr(const char *value, int leftjust, int minlen, int maxwidth, |
| 1001 |
int pointflag, PrintfTarget *target) |
| 1002 |
{ |
| 1003 |
int padlen, |
| 1004 |
vallen; /* amount to pad */ |
| 1005 |
|
| 1006 |
/* |
| 1007 |
* If a maxwidth (precision) is specified, we must not fetch more bytes |
| 1008 |
* than that. |
| 1009 |
*/ |
| 1010 |
if (pointflag) |
| 1011 |
vallen = strnlen(value, maxwidth); |
| 1012 |
else |
| 1013 |
vallen = strlen(value); |
| 1014 |
|
| 1015 |
padlen = compute_padlen(minlen, vallen, leftjust); |
| 1016 |
|
| 1017 |
if (padlen > 0) |
| 1018 |
{ |
| 1019 |
dopr_outchmulti(' ', padlen, target); |
| 1020 |
padlen = 0; |
| 1021 |
} |
| 1022 |
|
| 1023 |
dostr(value, vallen, target); |
| 1024 |
|
| 1025 |
trailing_pad(padlen, target); |
| 1026 |
} |
| 1027 |
|
| 1028 |
static void |
| 1029 |
fmtptr(const void *value, PrintfTarget *target) |
| 1030 |
{ |
| 1031 |
int vallen; |
| 1032 |
char convert[64]; |
| 1033 |
|
| 1034 |
/* we rely on regular C library's snprintf to do the basic conversion */ |
| 1035 |
vallen = snprintf(convert, sizeof(convert), "%p", value); |
| 1036 |
if (vallen < 0) |
| 1037 |
target->failed = true; |
| 1038 |
else |
| 1039 |
dostr(convert, vallen, target); |
| 1040 |
} |
| 1041 |
|
| 1042 |
static void |
| 1043 |
fmtint(long long value, char type, int forcesign, int leftjust, |
| 1044 |
int minlen, int zpad, int precision, int pointflag, |
| 1045 |
PrintfTarget *target) |
| 1046 |
{ |
| 1047 |
unsigned long long uvalue; |
| 1048 |
int base; |
| 1049 |
int dosign; |
| 1050 |
const char *cvt = "0123456789abcdef"; |
| 1051 |
int signvalue = 0; |
| 1052 |
char convert[64]; |
| 1053 |
int vallen = 0; |
| 1054 |
int padlen; /* amount to pad */ |
| 1055 |
int zeropad; /* extra leading zeroes */ |
| 1056 |
|
| 1057 |
switch (type) |
| 1058 |
{ |
| 1059 |
case 'd': |
| 1060 |
case 'i': |
| 1061 |
base = 10; |
| 1062 |
dosign = 1; |
| 1063 |
break; |
| 1064 |
case 'o': |
| 1065 |
base = 8; |
| 1066 |
dosign = 0; |
| 1067 |
break; |
| 1068 |
case 'u': |
| 1069 |
base = 10; |
| 1070 |
dosign = 0; |
| 1071 |
break; |
| 1072 |
case 'x': |
| 1073 |
base = 16; |
| 1074 |
dosign = 0; |
| 1075 |
break; |
| 1076 |
case 'X': |
| 1077 |
cvt = "0123456789ABCDEF"; |
| 1078 |
base = 16; |
| 1079 |
dosign = 0; |
| 1080 |
break; |
| 1081 |
default: |
| 1082 |
return; /* keep compiler quiet */ |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* disable MSVC warning about applying unary minus to an unsigned value */ |
| 1086 |
#ifdef _MSC_VER |
| 1087 |
#pragma warning(push) |
| 1088 |
#pragma warning(disable: 4146) |
| 1089 |
#endif |
| 1090 |
/* Handle +/- */ |
| 1091 |
if (dosign && adjust_sign((value < 0), forcesign, &signvalue)) |
| 1092 |
uvalue = -(unsigned long long) value; |
| 1093 |
else |
| 1094 |
uvalue = (unsigned long long) value; |
| 1095 |
#ifdef _MSC_VER |
| 1096 |
#pragma warning(pop) |
| 1097 |
#endif |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* SUS: the result of converting 0 with an explicit precision of 0 is no |
| 1101 |
* characters |
| 1102 |
*/ |
| 1103 |
if (value == 0 && pointflag && precision == 0) |
| 1104 |
vallen = 0; |
| 1105 |
else |
| 1106 |
{ |
| 1107 |
/* |
| 1108 |
* Convert integer to string. We special-case each of the possible |
| 1109 |
* base values so as to avoid general-purpose divisions. On most |
| 1110 |
* machines, division by a fixed constant can be done much more |
| 1111 |
* cheaply than a general divide. |
| 1112 |
*/ |
| 1113 |
if (base == 10) |
| 1114 |
{ |
| 1115 |
do |
| 1116 |
{ |
| 1117 |
convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 10]; |
| 1118 |
uvalue = uvalue / 10; |
| 1119 |
} while (uvalue); |
| 1120 |
} |
| 1121 |
else if (base == 16) |
| 1122 |
{ |
| 1123 |
do |
| 1124 |
{ |
| 1125 |
convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 16]; |
| 1126 |
uvalue = uvalue / 16; |
| 1127 |
} while (uvalue); |
| 1128 |
} |
| 1129 |
else /* base == 8 */ |
| 1130 |
{ |
| 1131 |
do |
| 1132 |
{ |
| 1133 |
convert[sizeof(convert) - (++vallen)] = cvt[uvalue % 8]; |
| 1134 |
uvalue = uvalue / 8; |
| 1135 |
} while (uvalue); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
zeropad = Max(0, precision - vallen); |
| 1140 |
|
| 1141 |
padlen = compute_padlen(minlen, vallen + zeropad, leftjust); |
| 1142 |
|
| 1143 |
leading_pad(zpad, signvalue, &padlen, target); |
| 1144 |
|
| 1145 |
if (zeropad > 0) |
| 1146 |
dopr_outchmulti('0', zeropad, target); |
| 1147 |
|
| 1148 |
dostr(convert + sizeof(convert) - vallen, vallen, target); |
| 1149 |
|
| 1150 |
trailing_pad(padlen, target); |
| 1151 |
} |
| 1152 |
|
| 1153 |
static void |
| 1154 |
fmtchar(int value, int leftjust, int minlen, PrintfTarget *target) |
| 1155 |
{ |
| 1156 |
int padlen; /* amount to pad */ |
| 1157 |
|
| 1158 |
padlen = compute_padlen(minlen, 1, leftjust); |
| 1159 |
|
| 1160 |
if (padlen > 0) |
| 1161 |
{ |
| 1162 |
dopr_outchmulti(' ', padlen, target); |
| 1163 |
padlen = 0; |
| 1164 |
} |
| 1165 |
|
| 1166 |
dopr_outch(value, target); |
| 1167 |
|
| 1168 |
trailing_pad(padlen, target); |
| 1169 |
} |
| 1170 |
|
| 1171 |
static void |
| 1172 |
fmtfloat(double value, char type, int forcesign, int leftjust, |
| 1173 |
int minlen, int zpad, int precision, int pointflag, |
| 1174 |
PrintfTarget *target) |
| 1175 |
{ |
| 1176 |
int signvalue = 0; |
| 1177 |
int prec; |
| 1178 |
int vallen; |
| 1179 |
char fmt[8]; |
| 1180 |
char convert[1024]; |
| 1181 |
int zeropadlen = 0; /* amount to pad with zeroes */ |
| 1182 |
int padlen; /* amount to pad with spaces */ |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* We rely on the regular C library's snprintf to do the basic conversion, |
| 1186 |
* then handle padding considerations here. |
| 1187 |
* |
| 1188 |
* The dynamic range of "double" is about 1E+-308 for IEEE math, and not |
| 1189 |
* too wildly more than that with other hardware. In "f" format, snprintf |
| 1190 |
* could therefore generate at most 308 characters to the left of the |
| 1191 |
* decimal point; while we need to allow the precision to get as high as |
| 1192 |
* 308+17 to ensure that we don't truncate significant digits from very |
| 1193 |
* small values. To handle both these extremes, we use a buffer of 1024 |
| 1194 |
* bytes and limit requested precision to 350 digits; this should prevent |
| 1195 |
* buffer overrun even with non-IEEE math. If the original precision |
| 1196 |
* request was more than 350, separately pad with zeroes. |
| 1197 |
* |
| 1198 |
* We handle infinities and NaNs specially to ensure platform-independent |
| 1199 |
* output. |
| 1200 |
*/ |
| 1201 |
if (precision < 0) /* cover possible overflow of "accum" */ |
| 1202 |
precision = 0; |
| 1203 |
prec = Min(precision, 350); |
| 1204 |
|
| 1205 |
if (isnan(value)) |
| 1206 |
{ |
| 1207 |
strcpy(convert, "NaN"); |
| 1208 |
vallen = 3; |
| 1209 |
/* no zero padding, regardless of precision spec */ |
| 1210 |
} |
| 1211 |
else |
| 1212 |
{ |
| 1213 |
/* |
| 1214 |
* Handle sign (NaNs have no sign, so we don't do this in the case |
| 1215 |
* above). "value < 0.0" will not be true for IEEE minus zero, so we |
| 1216 |
* detect that by looking for the case where value equals 0.0 |
| 1217 |
* according to == but not according to memcmp. |
| 1218 |
*/ |
| 1219 |
static const double dzero = 0.0; |
| 1220 |
|
| 1221 |
if (adjust_sign((value < 0.0 || |
| 1222 |
(value == 0.0 && |
| 1223 |
memcmp(&value, &dzero, sizeof(double)) != 0)), |
| 1224 |
forcesign, &signvalue)) |
| 1225 |
value = -value; |
| 1226 |
|
| 1227 |
if (isinf(value)) |
| 1228 |
{ |
| 1229 |
strcpy(convert, "Infinity"); |
| 1230 |
vallen = 8; |
| 1231 |
/* no zero padding, regardless of precision spec */ |
| 1232 |
} |
| 1233 |
else if (pointflag) |
| 1234 |
{ |
| 1235 |
zeropadlen = precision - prec; |
| 1236 |
fmt[0] = '%'; |
| 1237 |
fmt[1] = '.'; |
| 1238 |
fmt[2] = '*'; |
| 1239 |
fmt[3] = type; |
| 1240 |
fmt[4] = '\0'; |
| 1241 |
vallen = snprintf(convert, sizeof(convert), fmt, prec, value); |
| 1242 |
} |
| 1243 |
else |
| 1244 |
{ |
| 1245 |
fmt[0] = '%'; |
| 1246 |
fmt[1] = type; |
| 1247 |
fmt[2] = '\0'; |
| 1248 |
vallen = snprintf(convert, sizeof(convert), fmt, value); |
| 1249 |
} |
| 1250 |
if (vallen < 0) |
| 1251 |
goto fail; |
| 1252 |
|
| 1253 |
/* |
| 1254 |
* Windows, alone among our supported platforms, likes to emit |
| 1255 |
* three-digit exponent fields even when two digits would do. Hack |
| 1256 |
* such results to look like the way everyone else does it. |
| 1257 |
*/ |
| 1258 |
#ifdef WIN32 |
| 1259 |
if (vallen >= 6 && |
| 1260 |
convert[vallen - 5] == 'e' && |
| 1261 |
convert[vallen - 3] == '0') |
| 1262 |
{ |
| 1263 |
convert[vallen - 3] = convert[vallen - 2]; |
| 1264 |
convert[vallen - 2] = convert[vallen - 1]; |
| 1265 |
vallen--; |
| 1266 |
} |
| 1267 |
#endif |
| 1268 |
} |
| 1269 |
|
| 1270 |
padlen = compute_padlen(minlen, vallen + zeropadlen, leftjust); |
| 1271 |
|
| 1272 |
leading_pad(zpad, signvalue, &padlen, target); |
| 1273 |
|
| 1274 |
if (zeropadlen > 0) |
| 1275 |
{ |
| 1276 |
/* If 'e' or 'E' format, inject zeroes before the exponent */ |
| 1277 |
char *epos = strrchr(convert, 'e'); |
| 1278 |
|
| 1279 |
if (!epos) |
| 1280 |
epos = strrchr(convert, 'E'); |
| 1281 |
if (epos) |
| 1282 |
{ |
| 1283 |
/* pad before exponent */ |
| 1284 |
dostr(convert, epos - convert, target); |
| 1285 |
dopr_outchmulti('0', zeropadlen, target); |
| 1286 |
dostr(epos, vallen - (epos - convert), target); |
| 1287 |
} |
| 1288 |
else |
| 1289 |
{ |
| 1290 |
/* no exponent, pad after the digits */ |
| 1291 |
dostr(convert, vallen, target); |
| 1292 |
dopr_outchmulti('0', zeropadlen, target); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
else |
| 1296 |
{ |
| 1297 |
/* no zero padding, just emit the number as-is */ |
| 1298 |
dostr(convert, vallen, target); |
| 1299 |
} |
| 1300 |
|
| 1301 |
trailing_pad(padlen, target); |
| 1302 |
return; |
| 1303 |
|
| 1304 |
fail: |
| 1305 |
target->failed = true; |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* |
| 1309 |
* Nonstandard entry point to print a double value efficiently. |
| 1310 |
* |
| 1311 |
* This is approximately equivalent to strfromd(), but has an API more |
| 1312 |
* adapted to what float8out() wants. The behavior is like snprintf() |
| 1313 |
* with a format of "%.ng", where n is the specified precision. |
| 1314 |
* However, the target buffer must be nonempty (i.e. count > 0), and |
| 1315 |
* the precision is silently bounded to a sane range. |
| 1316 |
*/ |
| 1317 |
int |
| 1318 |
pg_strfromd(char *str, size_t count, int precision, double value) |
| 1319 |
{ |
| 1320 |
PrintfTarget target; |
| 1321 |
int signvalue = 0; |
| 1322 |
int vallen; |
| 1323 |
char fmt[8]; |
| 1324 |
char convert[64]; |
| 1325 |
|
| 1326 |
/* Set up the target like pg_snprintf, but require nonempty buffer */ |
| 1327 |
Assert(count > 0); |
| 1328 |
target.bufstart = target.bufptr = str; |
| 1329 |
target.bufend = str + count - 1; |
| 1330 |
target.stream = NULL; |
| 1331 |
target.nchars = 0; |
| 1332 |
target.failed = false; |
| 1333 |
|
| 1334 |
/* |
| 1335 |
* We bound precision to a reasonable range; the combination of this and |
| 1336 |
* the knowledge that we're using "g" format without padding allows the |
| 1337 |
* convert[] buffer to be reasonably small. |
| 1338 |
*/ |
| 1339 |
if (precision < 1) |
| 1340 |
precision = 1; |
| 1341 |
else if (precision > 32) |
| 1342 |
precision = 32; |
| 1343 |
|
| 1344 |
/* |
| 1345 |
* The rest is just an inlined version of the fmtfloat() logic above, |
| 1346 |
* simplified using the knowledge that no padding is wanted. |
| 1347 |
*/ |
| 1348 |
if (isnan(value)) |
| 1349 |
{ |
| 1350 |
strcpy(convert, "NaN"); |
| 1351 |
vallen = 3; |
| 1352 |
} |
| 1353 |
else |
| 1354 |
{ |
| 1355 |
static const double dzero = 0.0; |
| 1356 |
|
| 1357 |
if (value < 0.0 || |
| 1358 |
(value == 0.0 && |
| 1359 |
memcmp(&value, &dzero, sizeof(double)) != 0)) |
| 1360 |
{ |
| 1361 |
signvalue = '-'; |
| 1362 |
value = -value; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (isinf(value)) |
| 1366 |
{ |
| 1367 |
strcpy(convert, "Infinity"); |
| 1368 |
vallen = 8; |
| 1369 |
} |
| 1370 |
else |
| 1371 |
{ |
| 1372 |
fmt[0] = '%'; |
| 1373 |
fmt[1] = '.'; |
| 1374 |
fmt[2] = '*'; |
| 1375 |
fmt[3] = 'g'; |
| 1376 |
fmt[4] = '\0'; |
| 1377 |
vallen = snprintf(convert, sizeof(convert), fmt, precision, value); |
| 1378 |
if (vallen < 0) |
| 1379 |
{ |
| 1380 |
target.failed = true; |
| 1381 |
goto fail; |
| 1382 |
} |
| 1383 |
|
| 1384 |
#ifdef WIN32 |
| 1385 |
if (vallen >= 6 && |
| 1386 |
convert[vallen - 5] == 'e' && |
| 1387 |
convert[vallen - 3] == '0') |
| 1388 |
{ |
| 1389 |
convert[vallen - 3] = convert[vallen - 2]; |
| 1390 |
convert[vallen - 2] = convert[vallen - 1]; |
| 1391 |
vallen--; |
| 1392 |
} |
| 1393 |
#endif |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
if (signvalue) |
| 1398 |
dopr_outch(signvalue, &target); |
| 1399 |
|
| 1400 |
dostr(convert, vallen, &target); |
| 1401 |
|
| 1402 |
fail: |
| 1403 |
*(target.bufptr) = '\0'; |
| 1404 |
return target.failed ? -1 : (target.bufptr - target.bufstart |
| 1405 |
+ target.nchars); |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
static void |
| 1410 |
dostr(const char *str, int slen, PrintfTarget *target) |
| 1411 |
{ |
| 1412 |
/* fast path for common case of slen == 1 */ |
| 1413 |
if (slen == 1) |
| 1414 |
{ |
| 1415 |
dopr_outch(*str, target); |
| 1416 |
return; |
| 1417 |
} |
| 1418 |
|
| 1419 |
while (slen > 0) |
| 1420 |
{ |
| 1421 |
int avail; |
| 1422 |
|
| 1423 |
if (target->bufend != NULL) |
| 1424 |
avail = target->bufend - target->bufptr; |
| 1425 |
else |
| 1426 |
avail = slen; |
| 1427 |
if (avail <= 0) |
| 1428 |
{ |
| 1429 |
/* buffer full, can we dump to stream? */ |
| 1430 |
if (target->stream == NULL) |
| 1431 |
{ |
| 1432 |
target->nchars += slen; /* no, lose the data */ |
| 1433 |
return; |
| 1434 |
} |
| 1435 |
flushbuffer(target); |
| 1436 |
continue; |
| 1437 |
} |
| 1438 |
avail = Min(avail, slen); |
| 1439 |
memmove(target->bufptr, str, avail); |
| 1440 |
target->bufptr += avail; |
| 1441 |
str += avail; |
| 1442 |
slen -= avail; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
static void |
| 1447 |
dopr_outch(int c, PrintfTarget *target) |
| 1448 |
{ |
| 1449 |
if (target->bufend != NULL && target->bufptr >= target->bufend) |
| 1450 |
{ |
| 1451 |
/* buffer full, can we dump to stream? */ |
| 1452 |
if (target->stream == NULL) |
| 1453 |
{ |
| 1454 |
target->nchars++; /* no, lose the data */ |
| 1455 |
return; |
| 1456 |
} |
| 1457 |
flushbuffer(target); |
| 1458 |
} |
| 1459 |
*(target->bufptr++) = c; |
| 1460 |
} |
| 1461 |
|
| 1462 |
static void |
| 1463 |
dopr_outchmulti(int c, int slen, PrintfTarget *target) |
| 1464 |
{ |
| 1465 |
/* fast path for common case of slen == 1 */ |
| 1466 |
if (slen == 1) |
| 1467 |
{ |
| 1468 |
dopr_outch(c, target); |
| 1469 |
return; |
| 1470 |
} |
| 1471 |
|
| 1472 |
while (slen > 0) |
| 1473 |
{ |
| 1474 |
int avail; |
| 1475 |
|
| 1476 |
if (target->bufend != NULL) |
| 1477 |
avail = target->bufend - target->bufptr; |
| 1478 |
else |
| 1479 |
avail = slen; |
| 1480 |
if (avail <= 0) |
| 1481 |
{ |
| 1482 |
/* buffer full, can we dump to stream? */ |
| 1483 |
if (target->stream == NULL) |
| 1484 |
{ |
| 1485 |
target->nchars += slen; /* no, lose the data */ |
| 1486 |
return; |
| 1487 |
} |
| 1488 |
flushbuffer(target); |
| 1489 |
continue; |
| 1490 |
} |
| 1491 |
avail = Min(avail, slen); |
| 1492 |
memset(target->bufptr, c, avail); |
| 1493 |
target->bufptr += avail; |
| 1494 |
slen -= avail; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
static int |
| 1500 |
adjust_sign(int is_negative, int forcesign, int *signvalue) |
| 1501 |
{ |
| 1502 |
if (is_negative) |
| 1503 |
{ |
| 1504 |
*signvalue = '-'; |
| 1505 |
return true; |
| 1506 |
} |
| 1507 |
else if (forcesign) |
| 1508 |
*signvalue = '+'; |
| 1509 |
return false; |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
static int |
| 1514 |
compute_padlen(int minlen, int vallen, int leftjust) |
| 1515 |
{ |
| 1516 |
int padlen; |
| 1517 |
|
| 1518 |
padlen = minlen - vallen; |
| 1519 |
if (padlen < 0) |
| 1520 |
padlen = 0; |
| 1521 |
if (leftjust) |
| 1522 |
padlen = -padlen; |
| 1523 |
return padlen; |
| 1524 |
} |
| 1525 |
|
| 1526 |
|
| 1527 |
static void |
| 1528 |
leading_pad(int zpad, int signvalue, int *padlen, PrintfTarget *target) |
| 1529 |
{ |
| 1530 |
int maxpad; |
| 1531 |
|
| 1532 |
if (*padlen > 0 && zpad) |
| 1533 |
{ |
| 1534 |
if (signvalue) |
| 1535 |
{ |
| 1536 |
dopr_outch(signvalue, target); |
| 1537 |
--(*padlen); |
| 1538 |
signvalue = 0; |
| 1539 |
} |
| 1540 |
if (*padlen > 0) |
| 1541 |
{ |
| 1542 |
dopr_outchmulti(zpad, *padlen, target); |
| 1543 |
*padlen = 0; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
maxpad = (signvalue != 0); |
| 1547 |
if (*padlen > maxpad) |
| 1548 |
{ |
| 1549 |
dopr_outchmulti(' ', *padlen - maxpad, target); |
| 1550 |
*padlen = maxpad; |
| 1551 |
} |
| 1552 |
if (signvalue) |
| 1553 |
{ |
| 1554 |
dopr_outch(signvalue, target); |
| 1555 |
if (*padlen > 0) |
| 1556 |
--(*padlen); |
| 1557 |
else if (*padlen < 0) |
| 1558 |
++(*padlen); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
static void |
| 1564 |
trailing_pad(int padlen, PrintfTarget *target) |
| 1565 |
{ |
| 1566 |
if (padlen < 0) |
| 1567 |
dopr_outchmulti(' ', -padlen, target); |
| 1568 |
} |