| 274 | | #if !USE_SNPRINTF |
|---|
| 275 | | size_t tmp_length; |
|---|
| 276 | | CHAR_T tmpbuf[700]; |
|---|
| 277 | | CHAR_T *tmp; |
|---|
| 278 | | |
|---|
| 279 | | /* Allocate a temporary buffer of sufficient size for calling |
|---|
| 280 | | sprintf. */ |
|---|
| 281 | | { |
|---|
| 282 | | size_t width; |
|---|
| 283 | | size_t precision; |
|---|
| 284 | | |
|---|
| 285 | | width = 0; |
|---|
| 286 | | if (dp->width_start != dp->width_end) |
|---|
| 287 | | { |
|---|
| 288 | | if (dp->width_arg_index != ARG_NONE) |
|---|
| 289 | | { |
|---|
| 290 | | int arg; |
|---|
| 291 | | |
|---|
| 292 | | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) |
|---|
| 293 | | abort (); |
|---|
| 294 | | arg = a.arg[dp->width_arg_index].a.a_int; |
|---|
| 295 | | width = (arg < 0 ? (unsigned int) (-arg) : arg); |
|---|
| 296 | | } |
|---|
| 297 | | else |
|---|
| 298 | | { |
|---|
| 299 | | const CHAR_T *digitp = dp->width_start; |
|---|
| 300 | | |
|---|
| 301 | | do |
|---|
| 302 | | width = xsum (xtimes (width, 10), *digitp++ - '0'); |
|---|
| 303 | | while (digitp != dp->width_end); |
|---|
| 304 | | } |
|---|
| 305 | | } |
|---|
| 306 | | |
|---|
| 307 | | precision = 6; |
|---|
| 308 | | if (dp->precision_start != dp->precision_end) |
|---|
| 309 | | { |
|---|
| 310 | | if (dp->precision_arg_index != ARG_NONE) |
|---|
| 311 | | { |
|---|
| 312 | | int arg; |
|---|
| 313 | | |
|---|
| 314 | | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) |
|---|
| 315 | | abort (); |
|---|
| 316 | | arg = a.arg[dp->precision_arg_index].a.a_int; |
|---|
| 317 | | precision = (arg < 0 ? 0 : arg); |
|---|
| 318 | | } |
|---|
| 319 | | else |
|---|
| 320 | | { |
|---|
| 321 | | const CHAR_T *digitp = dp->precision_start + 1; |
|---|
| 322 | | |
|---|
| 323 | | precision = 0; |
|---|
| 324 | | while (digitp != dp->precision_end) |
|---|
| 325 | | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); |
|---|
| 326 | | } |
|---|
| 327 | | } |
|---|
| 328 | | |
|---|
| 329 | | switch (dp->conversion) |
|---|
| 330 | | { |
|---|
| 331 | | |
|---|
| 332 | | case 'd': case 'i': case 'u': |
|---|
| 333 | | # ifdef HAVE_LONG_LONG |
|---|
| 334 | | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
|---|
| 335 | | tmp_length = |
|---|
| 336 | | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
|---|
| 337 | | * 0.30103 /* binary -> decimal */ |
|---|
| 338 | | * 2 /* estimate for FLAG_GROUP */ |
|---|
| 339 | | ) |
|---|
| 340 | | + 1 /* turn floor into ceil */ |
|---|
| 341 | | + 1; /* account for leading sign */ |
|---|
| 342 | | else |
|---|
| 343 | | # endif |
|---|
| 344 | | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
|---|
| 345 | | tmp_length = |
|---|
| 346 | | (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
|---|
| 347 | | * 0.30103 /* binary -> decimal */ |
|---|
| 348 | | * 2 /* estimate for FLAG_GROUP */ |
|---|
| 349 | | ) |
|---|
| 350 | | + 1 /* turn floor into ceil */ |
|---|
| 351 | | + 1; /* account for leading sign */ |
|---|
| 352 | | else |
|---|
| 353 | | tmp_length = |
|---|
| 354 | | (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
|---|
| 355 | | * 0.30103 /* binary -> decimal */ |
|---|
| 356 | | * 2 /* estimate for FLAG_GROUP */ |
|---|
| 357 | | ) |
|---|
| 358 | | + 1 /* turn floor into ceil */ |
|---|
| 359 | | + 1; /* account for leading sign */ |
|---|
| 360 | | break; |
|---|
| 361 | | |
|---|
| 362 | | case 'o': |
|---|
| 363 | | # ifdef HAVE_LONG_LONG |
|---|
| 364 | | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
|---|
| 365 | | tmp_length = |
|---|
| 366 | | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
|---|
| 367 | | * 0.333334 /* binary -> octal */ |
|---|
| 368 | | ) |
|---|
| 369 | | + 1 /* turn floor into ceil */ |
|---|
| 370 | | + 1; /* account for leading sign */ |
|---|
| 371 | | else |
|---|
| 372 | | # endif |
|---|
| 373 | | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
|---|
| 374 | | tmp_length = |
|---|
| 375 | | (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
|---|
| 376 | | * 0.333334 /* binary -> octal */ |
|---|
| 377 | | ) |
|---|
| 378 | | + 1 /* turn floor into ceil */ |
|---|
| 379 | | + 1; /* account for leading sign */ |
|---|
| 380 | | else |
|---|
| 381 | | tmp_length = |
|---|
| 382 | | (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
|---|
| 383 | | * 0.333334 /* binary -> octal */ |
|---|
| 384 | | ) |
|---|
| 385 | | + 1 /* turn floor into ceil */ |
|---|
| 386 | | + 1; /* account for leading sign */ |
|---|
| 387 | | break; |
|---|
| 388 | | |
|---|
| 389 | | case 'x': case 'X': |
|---|
| 390 | | # ifdef HAVE_LONG_LONG |
|---|
| 391 | | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
|---|
| 392 | | tmp_length = |
|---|
| 393 | | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
|---|
| 394 | | * 0.25 /* binary -> hexadecimal */ |
|---|
| 395 | | ) |
|---|
| 396 | | + 1 /* turn floor into ceil */ |
|---|
| 397 | | + 2; /* account for leading sign or alternate form */ |
|---|
| 398 | | else |
|---|
| 399 | | # endif |
|---|
| 400 | | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
|---|
| 401 | | tmp_length = |
|---|
| 402 | | (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
|---|
| 403 | | * 0.25 /* binary -> hexadecimal */ |
|---|
| 404 | | ) |
|---|
| 405 | | + 1 /* turn floor into ceil */ |
|---|
| 406 | | + 2; /* account for leading sign or alternate form */ |
|---|
| 407 | | else |
|---|
| 408 | | tmp_length = |
|---|
| 409 | | (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
|---|
| 410 | | * 0.25 /* binary -> hexadecimal */ |
|---|
| 411 | | ) |
|---|
| 412 | | + 1 /* turn floor into ceil */ |
|---|
| 413 | | + 2; /* account for leading sign or alternate form */ |
|---|
| 414 | | break; |
|---|
| 415 | | |
|---|
| 416 | | case 'f': case 'F': |
|---|
| 417 | | # ifdef HAVE_LONG_DOUBLE |
|---|
| 418 | | if (type == TYPE_LONGDOUBLE) |
|---|
| 419 | | tmp_length = |
|---|
| 420 | | (unsigned int) (LDBL_MAX_EXP |
|---|
| 421 | | * 0.30103 /* binary -> decimal */ |
|---|
| 422 | | * 2 /* estimate for FLAG_GROUP */ |
|---|
| 423 | | ) |
|---|
| 424 | | + 1 /* turn floor into ceil */ |
|---|
| 425 | | + 10; /* sign, decimal point etc. */ |
|---|
| 426 | | else |
|---|
| 427 | | # endif |
|---|
| 428 | | tmp_length = |
|---|
| 429 | | (unsigned int) (DBL_MAX_EXP |
|---|
| 430 | | * 0.30103 /* binary -> decimal */ |
|---|
| 431 | | * 2 /* estimate for FLAG_GROUP */ |
|---|
| 432 | | ) |
|---|
| 433 | | + 1 /* turn floor into ceil */ |
|---|
| 434 | | + 10; /* sign, decimal point etc. */ |
|---|
| 435 | | tmp_length = xsum (tmp_length, precision); |
|---|
| 436 | | break; |
|---|
| 437 | | |
|---|
| 438 | | case 'e': case 'E': case 'g': case 'G': |
|---|
| 439 | | case 'a': case 'A': |
|---|
| 440 | | tmp_length = |
|---|
| 441 | | 12; /* sign, decimal point, exponent etc. */ |
|---|
| 442 | | tmp_length = xsum (tmp_length, precision); |
|---|
| 443 | | break; |
|---|
| 444 | | |
|---|
| 445 | | case 'c': |
|---|
| 446 | | # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION |
|---|
| 447 | | if (type == TYPE_WIDE_CHAR) |
|---|
| 448 | | tmp_length = MB_CUR_MAX; |
|---|
| 449 | | else |
|---|
| 450 | | # endif |
|---|
| 451 | | tmp_length = 1; |
|---|
| 452 | | break; |
|---|
| 453 | | |
|---|
| 454 | | case 's': |
|---|
| 455 | | # ifdef HAVE_WCHAR_T |
|---|
| 456 | | if (type == TYPE_WIDE_STRING) |
|---|
| 457 | | { |
|---|
| 458 | | tmp_length = |
|---|
| 459 | | local_wcslen (a.arg[dp->arg_index].a.a_wide_string); |
|---|
| 460 | | |
|---|
| 461 | | # if !WIDE_CHAR_VERSION |
|---|
| 462 | | tmp_length = xtimes (tmp_length, MB_CUR_MAX); |
|---|
| 463 | | # endif |
|---|
| 464 | | } |
|---|
| 465 | | else |
|---|
| 466 | | # endif |
|---|
| 467 | | tmp_length = strlen (a.arg[dp->arg_index].a.a_string); |
|---|
| 468 | | break; |
|---|
| 469 | | |
|---|
| 470 | | case 'p': |
|---|
| 471 | | tmp_length = |
|---|
| 472 | | (unsigned int) (sizeof (void *) * CHAR_BIT |
|---|
| 473 | | * 0.25 /* binary -> hexadecimal */ |
|---|
| 474 | | ) |
|---|
| 475 | | + 1 /* turn floor into ceil */ |
|---|
| 476 | | + 2; /* account for leading 0x */ |
|---|
| 477 | | break; |
|---|
| 478 | | |
|---|
| 479 | | default: |
|---|
| 480 | | abort (); |
|---|
| 481 | | } |
|---|
| 482 | | |
|---|
| 483 | | if (tmp_length < width) |
|---|
| 484 | | tmp_length = width; |
|---|
| 485 | | |
|---|
| 486 | | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ |
|---|
| 487 | | } |
|---|
| 488 | | |
|---|
| 489 | | if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T)) |
|---|
| 490 | | tmp = tmpbuf; |
|---|
| 491 | | else |
|---|
| 492 | | { |
|---|
| 493 | | size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T)); |
|---|
| 494 | | |
|---|
| 495 | | if (size_overflow_p (tmp_memsize)) |
|---|
| 496 | | /* Overflow, would lead to out of memory. */ |
|---|
| 497 | | goto out_of_memory; |
|---|
| 498 | | tmp = (CHAR_T *) malloc (tmp_memsize); |
|---|
| 499 | | if (tmp == NULL) |
|---|
| 500 | | /* Out of memory. */ |
|---|
| 501 | | goto out_of_memory; |
|---|
| 502 | | } |
|---|
| 503 | | #endif |
|---|
| 504 | | |
|---|
| 505 | | /* Construct the format string for calling snprintf or |
|---|
| 506 | | sprintf. */ |
|---|
| | 274 | |
|---|
| | 275 | /* Construct the format string for calling snprintf. */ |
|---|