| 1 |
/* |
| 2 |
* Copyright (c) 1995-2003 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <ctype.h> |
| 40 |
#include "roken.h" |
| 41 |
#include <assert.h> |
| 42 |
|
| 43 |
enum format_flags { |
| 44 |
minus_flag = 1, |
| 45 |
plus_flag = 2, |
| 46 |
space_flag = 4, |
| 47 |
alternate_flag = 8, |
| 48 |
zero_flag = 16 |
| 49 |
}; |
| 50 |
|
| 51 |
/* |
| 52 |
* Common state |
| 53 |
*/ |
| 54 |
|
| 55 |
struct snprintf_state { |
| 56 |
unsigned char *str; |
| 57 |
unsigned char *s; |
| 58 |
unsigned char *theend; |
| 59 |
size_t sz; |
| 60 |
size_t max_sz; |
| 61 |
void (*append_char)(struct snprintf_state *, unsigned char); |
| 62 |
/* XXX - methods */ |
| 63 |
}; |
| 64 |
|
| 65 |
#if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF) |
| 66 |
static int |
| 67 |
sn_reserve (struct snprintf_state *state, size_t n) |
| 68 |
{ |
| 69 |
return state->s + n > state->theend; |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
sn_append_char (struct snprintf_state *state, unsigned char c) |
| 74 |
{ |
| 75 |
if (!sn_reserve (state, 1)) |
| 76 |
*state->s++ = c; |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
static int |
| 81 |
as_reserve (struct snprintf_state *state, size_t n) |
| 82 |
{ |
| 83 |
if (state->s + n > state->theend) { |
| 84 |
int off = state->s - state->str; |
| 85 |
unsigned char *tmp; |
| 86 |
|
| 87 |
if (state->max_sz && state->sz >= state->max_sz) |
| 88 |
return 1; |
| 89 |
|
| 90 |
state->sz = max(state->sz * 2, state->sz + n); |
| 91 |
if (state->max_sz) |
| 92 |
state->sz = min(state->sz, state->max_sz); |
| 93 |
tmp = realloc (state->str, state->sz); |
| 94 |
if (tmp == NULL) |
| 95 |
return 1; |
| 96 |
state->str = tmp; |
| 97 |
state->s = state->str + off; |
| 98 |
state->theend = state->str + state->sz - 1; |
| 99 |
} |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
as_append_char (struct snprintf_state *state, unsigned char c) |
| 105 |
{ |
| 106 |
if(!as_reserve (state, 1)) |
| 107 |
*state->s++ = c; |
| 108 |
} |
| 109 |
|
| 110 |
/* longest integer types */ |
| 111 |
|
| 112 |
#ifdef HAVE_LONG_LONG |
| 113 |
typedef unsigned long long u_longest; |
| 114 |
typedef long long longest; |
| 115 |
#else |
| 116 |
typedef unsigned long u_longest; |
| 117 |
typedef long longest; |
| 118 |
#endif |
| 119 |
|
| 120 |
#ifndef HAVE_UINTPTR_T |
| 121 |
typedef u_longest uintptr_t; |
| 122 |
#endif |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static size_t |
| 127 |
pad(struct snprintf_state *state, int width, char c) |
| 128 |
{ |
| 129 |
size_t len = 0; |
| 130 |
while(width-- > 0){ |
| 131 |
(*state->append_char)(state, c); |
| 132 |
++len; |
| 133 |
} |
| 134 |
return len; |
| 135 |
} |
| 136 |
|
| 137 |
/* return true if we should use alternatve hex form */ |
| 138 |
static int |
| 139 |
use_alternative (int flags, u_longest num, unsigned base) |
| 140 |
{ |
| 141 |
return (flags & alternate_flag) && base == 16 && num != 0; |
| 142 |
} |
| 143 |
|
| 144 |
static int |
| 145 |
append_number(struct snprintf_state *state, |
| 146 |
u_longest num, unsigned base, const char *rep, |
| 147 |
int width, int prec, int flags, int minusp) |
| 148 |
{ |
| 149 |
int len = 0; |
| 150 |
u_longest n = num; |
| 151 |
char nstr[64]; /* enough for <192 bit octal integers */ |
| 152 |
int nstart, nlen; |
| 153 |
char signchar; |
| 154 |
|
| 155 |
/* given precision, ignore zero flag */ |
| 156 |
if(prec != -1) |
| 157 |
flags &= ~zero_flag; |
| 158 |
else |
| 159 |
prec = 1; |
| 160 |
|
| 161 |
/* format number as string */ |
| 162 |
nstart = sizeof(nstr); |
| 163 |
nlen = 0; |
| 164 |
nstr[--nstart] = '\0'; |
| 165 |
do { |
| 166 |
assert(nstart > 0); |
| 167 |
nstr[--nstart] = rep[n % base]; |
| 168 |
++nlen; |
| 169 |
n /= base; |
| 170 |
} while(n); |
| 171 |
|
| 172 |
/* zero value with zero precision should produce no digits */ |
| 173 |
if(prec == 0 && num == 0) { |
| 174 |
nlen--; |
| 175 |
nstart++; |
| 176 |
} |
| 177 |
|
| 178 |
/* figure out what char to use for sign */ |
| 179 |
if(minusp) |
| 180 |
signchar = '-'; |
| 181 |
else if((flags & plus_flag)) |
| 182 |
signchar = '+'; |
| 183 |
else if((flags & space_flag)) |
| 184 |
signchar = ' '; |
| 185 |
else |
| 186 |
signchar = '\0'; |
| 187 |
|
| 188 |
if((flags & alternate_flag) && base == 8) { |
| 189 |
/* if necessary, increase the precision to |
| 190 |
make first digit a zero */ |
| 191 |
|
| 192 |
/* XXX C99 claims (regarding # and %o) that "if the value and |
| 193 |
precision are both 0, a single 0 is printed", but there is |
| 194 |
no such wording for %x. This would mean that %#.o would |
| 195 |
output "0", but %#.x "". This does not make sense, and is |
| 196 |
also not what other printf implementations are doing. */ |
| 197 |
|
| 198 |
if(prec <= nlen && nstr[nstart] != '0' && nstr[nstart] != '\0') |
| 199 |
prec = nlen + 1; |
| 200 |
} |
| 201 |
|
| 202 |
/* possible formats: |
| 203 |
pad | sign | alt | zero | digits |
| 204 |
sign | alt | zero | digits | pad minus_flag |
| 205 |
sign | alt | zero | digits zero_flag */ |
| 206 |
|
| 207 |
/* if not right justifying or padding with zeros, we need to |
| 208 |
compute the length of the rest of the string, and then pad with |
| 209 |
spaces */ |
| 210 |
if(!(flags & (minus_flag | zero_flag))) { |
| 211 |
if(prec > nlen) |
| 212 |
width -= prec; |
| 213 |
else |
| 214 |
width -= nlen; |
| 215 |
|
| 216 |
if(use_alternative(flags, num, base)) |
| 217 |
width -= 2; |
| 218 |
|
| 219 |
if(signchar != '\0') |
| 220 |
width--; |
| 221 |
|
| 222 |
/* pad to width */ |
| 223 |
len += pad(state, width, ' '); |
| 224 |
} |
| 225 |
if(signchar != '\0') { |
| 226 |
(*state->append_char)(state, signchar); |
| 227 |
++len; |
| 228 |
} |
| 229 |
if(use_alternative(flags, num, base)) { |
| 230 |
(*state->append_char)(state, '0'); |
| 231 |
(*state->append_char)(state, rep[10] + 23); /* XXX */ |
| 232 |
len += 2; |
| 233 |
} |
| 234 |
if(flags & zero_flag) { |
| 235 |
/* pad to width with zeros */ |
| 236 |
if(prec - nlen > width - len - nlen) |
| 237 |
len += pad(state, prec - nlen, '0'); |
| 238 |
else |
| 239 |
len += pad(state, width - len - nlen, '0'); |
| 240 |
} else |
| 241 |
/* pad to prec with zeros */ |
| 242 |
len += pad(state, prec - nlen, '0'); |
| 243 |
|
| 244 |
while(nstr[nstart] != '\0') { |
| 245 |
(*state->append_char)(state, nstr[nstart++]); |
| 246 |
++len; |
| 247 |
} |
| 248 |
|
| 249 |
if(flags & minus_flag) |
| 250 |
len += pad(state, width - len, ' '); |
| 251 |
|
| 252 |
return len; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* return length |
| 257 |
*/ |
| 258 |
|
| 259 |
static size_t |
| 260 |
append_string (struct snprintf_state *state, |
| 261 |
const unsigned char *arg, |
| 262 |
int width, |
| 263 |
int prec, |
| 264 |
int flags) |
| 265 |
{ |
| 266 |
size_t len = 0; |
| 267 |
|
| 268 |
if(arg == NULL) |
| 269 |
arg = (const unsigned char*)"(null)"; |
| 270 |
|
| 271 |
if(prec != -1) |
| 272 |
width -= prec; |
| 273 |
else |
| 274 |
width -= strlen((const char *)arg); |
| 275 |
if(!(flags & minus_flag)) |
| 276 |
len += pad(state, width, ' '); |
| 277 |
|
| 278 |
if (prec != -1) { |
| 279 |
while (prec-- && *arg) { |
| 280 |
(*state->append_char) (state, *arg++); |
| 281 |
++len; |
| 282 |
} |
| 283 |
} else { |
| 284 |
while (*arg) { |
| 285 |
(*state->append_char) (state, *arg++); |
| 286 |
++len; |
| 287 |
} |
| 288 |
} |
| 289 |
if(flags & minus_flag) |
| 290 |
len += pad(state, width, ' '); |
| 291 |
return len; |
| 292 |
} |
| 293 |
|
| 294 |
static int |
| 295 |
append_char(struct snprintf_state *state, |
| 296 |
unsigned char arg, |
| 297 |
int width, |
| 298 |
int flags) |
| 299 |
{ |
| 300 |
int len = 0; |
| 301 |
|
| 302 |
while(!(flags & minus_flag) && --width > 0) { |
| 303 |
(*state->append_char) (state, ' ') ; |
| 304 |
++len; |
| 305 |
} |
| 306 |
(*state->append_char) (state, arg); |
| 307 |
++len; |
| 308 |
while((flags & minus_flag) && --width > 0) { |
| 309 |
(*state->append_char) (state, ' '); |
| 310 |
++len; |
| 311 |
} |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
/* |
| 316 |
* This can't be made into a function... |
| 317 |
*/ |
| 318 |
|
| 319 |
#ifdef HAVE_LONG_LONG |
| 320 |
|
| 321 |
#define PARSE_INT_FORMAT(res, arg, unsig) \ |
| 322 |
if (long_long_flag) \ |
| 323 |
res = (unsig long long)va_arg(arg, unsig long long); \ |
| 324 |
else if (long_flag) \ |
| 325 |
res = (unsig long)va_arg(arg, unsig long); \ |
| 326 |
else if (size_t_flag) \ |
| 327 |
res = (unsig long)va_arg(arg, size_t); \ |
| 328 |
else if (short_flag) \ |
| 329 |
res = (unsig short)va_arg(arg, unsig int); \ |
| 330 |
else \ |
| 331 |
res = (unsig int)va_arg(arg, unsig int) |
| 332 |
|
| 333 |
#else |
| 334 |
|
| 335 |
#define PARSE_INT_FORMAT(res, arg, unsig) \ |
| 336 |
if (long_flag) \ |
| 337 |
res = (unsig long)va_arg(arg, unsig long); \ |
| 338 |
else if (size_t_flag) \ |
| 339 |
res = (unsig long)va_arg(arg, size_t); \ |
| 340 |
else if (short_flag) \ |
| 341 |
res = (unsig short)va_arg(arg, unsig int); \ |
| 342 |
else \ |
| 343 |
res = (unsig int)va_arg(arg, unsig int) |
| 344 |
|
| 345 |
#endif |
| 346 |
|
| 347 |
/* |
| 348 |
* zyxprintf - return length, as snprintf |
| 349 |
*/ |
| 350 |
|
| 351 |
static size_t |
| 352 |
xyzprintf (struct snprintf_state *state, const char *char_format, va_list ap) |
| 353 |
{ |
| 354 |
const unsigned char *format = (const unsigned char *)char_format; |
| 355 |
unsigned char c; |
| 356 |
size_t len = 0; |
| 357 |
|
| 358 |
while((c = *format++)) { |
| 359 |
if (c == '%') { |
| 360 |
int flags = 0; |
| 361 |
int width = 0; |
| 362 |
int prec = -1; |
| 363 |
int size_t_flag = 0; |
| 364 |
int long_long_flag = 0; |
| 365 |
int long_flag = 0; |
| 366 |
int short_flag = 0; |
| 367 |
|
| 368 |
/* flags */ |
| 369 |
while((c = *format++)){ |
| 370 |
if(c == '-') |
| 371 |
flags |= minus_flag; |
| 372 |
else if(c == '+') |
| 373 |
flags |= plus_flag; |
| 374 |
else if(c == ' ') |
| 375 |
flags |= space_flag; |
| 376 |
else if(c == '#') |
| 377 |
flags |= alternate_flag; |
| 378 |
else if(c == '0') |
| 379 |
flags |= zero_flag; |
| 380 |
else if(c == '\'') |
| 381 |
; /* just ignore */ |
| 382 |
else |
| 383 |
break; |
| 384 |
} |
| 385 |
|
| 386 |
if((flags & space_flag) && (flags & plus_flag)) |
| 387 |
flags ^= space_flag; |
| 388 |
|
| 389 |
if((flags & minus_flag) && (flags & zero_flag)) |
| 390 |
flags ^= zero_flag; |
| 391 |
|
| 392 |
/* width */ |
| 393 |
if (isdigit(c)) |
| 394 |
do { |
| 395 |
width = width * 10 + c - '0'; |
| 396 |
c = *format++; |
| 397 |
} while(isdigit(c)); |
| 398 |
else if(c == '*') { |
| 399 |
width = va_arg(ap, int); |
| 400 |
c = *format++; |
| 401 |
} |
| 402 |
|
| 403 |
/* precision */ |
| 404 |
if (c == '.') { |
| 405 |
prec = 0; |
| 406 |
c = *format++; |
| 407 |
if (isdigit(c)) |
| 408 |
do { |
| 409 |
prec = prec * 10 + c - '0'; |
| 410 |
c = *format++; |
| 411 |
} while(isdigit(c)); |
| 412 |
else if (c == '*') { |
| 413 |
prec = va_arg(ap, int); |
| 414 |
c = *format++; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/* size */ |
| 419 |
|
| 420 |
if (c == 'h') { |
| 421 |
short_flag = 1; |
| 422 |
c = *format++; |
| 423 |
} else if (c == 'z') { |
| 424 |
size_t_flag = 1; |
| 425 |
c = *format++; |
| 426 |
} else if (c == 'l') { |
| 427 |
long_flag = 1; |
| 428 |
c = *format++; |
| 429 |
if (c == 'l') { |
| 430 |
long_long_flag = 1; |
| 431 |
c = *format++; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if(c != 'd' && c != 'i') |
| 436 |
flags &= ~(plus_flag | space_flag); |
| 437 |
|
| 438 |
switch (c) { |
| 439 |
case 'c' : |
| 440 |
append_char(state, va_arg(ap, int), width, flags); |
| 441 |
++len; |
| 442 |
break; |
| 443 |
case 's' : |
| 444 |
len += append_string(state, |
| 445 |
va_arg(ap, unsigned char*), |
| 446 |
width, |
| 447 |
prec, |
| 448 |
flags); |
| 449 |
break; |
| 450 |
case 'd' : |
| 451 |
case 'i' : { |
| 452 |
int64_t arg; |
| 453 |
uint64_t num; |
| 454 |
int minusp = 0; |
| 455 |
|
| 456 |
PARSE_INT_FORMAT(arg, ap, signed); |
| 457 |
|
| 458 |
if (arg == INT64_MIN) { |
| 459 |
minusp = 1; |
| 460 |
num = (uint64_t)INT64_MAX + 1; |
| 461 |
} else if (arg < 0) { |
| 462 |
minusp = 1; |
| 463 |
num = -arg; |
| 464 |
} else |
| 465 |
num = arg; |
| 466 |
|
| 467 |
len += append_number (state, num, 10, "0123456789", |
| 468 |
width, prec, flags, minusp); |
| 469 |
break; |
| 470 |
} |
| 471 |
case 'u' : { |
| 472 |
u_longest arg; |
| 473 |
|
| 474 |
PARSE_INT_FORMAT(arg, ap, unsigned); |
| 475 |
|
| 476 |
len += append_number (state, arg, 10, "0123456789", |
| 477 |
width, prec, flags, 0); |
| 478 |
break; |
| 479 |
} |
| 480 |
case 'o' : { |
| 481 |
u_longest arg; |
| 482 |
|
| 483 |
PARSE_INT_FORMAT(arg, ap, unsigned); |
| 484 |
|
| 485 |
len += append_number (state, arg, 010, "01234567", |
| 486 |
width, prec, flags, 0); |
| 487 |
break; |
| 488 |
} |
| 489 |
case 'x' : { |
| 490 |
u_longest arg; |
| 491 |
|
| 492 |
PARSE_INT_FORMAT(arg, ap, unsigned); |
| 493 |
|
| 494 |
len += append_number (state, arg, 0x10, "0123456789abcdef", |
| 495 |
width, prec, flags, 0); |
| 496 |
break; |
| 497 |
} |
| 498 |
case 'X' :{ |
| 499 |
u_longest arg; |
| 500 |
|
| 501 |
PARSE_INT_FORMAT(arg, ap, unsigned); |
| 502 |
|
| 503 |
len += append_number (state, arg, 0x10, "0123456789ABCDEF", |
| 504 |
width, prec, flags, 0); |
| 505 |
break; |
| 506 |
} |
| 507 |
case 'p' : { |
| 508 |
uintptr_t arg = (uintptr_t)va_arg(ap, void*); |
| 509 |
|
| 510 |
len += append_number (state, arg, 0x10, "0123456789ABCDEF", |
| 511 |
width, prec, flags, 0); |
| 512 |
break; |
| 513 |
} |
| 514 |
case 'n' : { |
| 515 |
int *arg = va_arg(ap, int*); |
| 516 |
*arg = state->s - state->str; |
| 517 |
break; |
| 518 |
} |
| 519 |
case '\0' : |
| 520 |
--format; |
| 521 |
/* FALLTHROUGH */ |
| 522 |
case '%' : |
| 523 |
(*state->append_char)(state, c); |
| 524 |
++len; |
| 525 |
break; |
| 526 |
default : |
| 527 |
(*state->append_char)(state, '%'); |
| 528 |
(*state->append_char)(state, c); |
| 529 |
len += 2; |
| 530 |
break; |
| 531 |
} |
| 532 |
} else { |
| 533 |
(*state->append_char) (state, c); |
| 534 |
++len; |
| 535 |
} |
| 536 |
} |
| 537 |
return len; |
| 538 |
} |
| 539 |
|
| 540 |
#if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF) |
| 541 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 542 |
rk_snprintf (char *str, size_t sz, const char *format, ...) |
| 543 |
{ |
| 544 |
va_list args; |
| 545 |
int ret; |
| 546 |
|
| 547 |
va_start(args, format); |
| 548 |
ret = vsnprintf (str, sz, format, args); |
| 549 |
va_end(args); |
| 550 |
|
| 551 |
#ifdef PARANOIA |
| 552 |
{ |
| 553 |
int ret2; |
| 554 |
char *tmp; |
| 555 |
|
| 556 |
tmp = malloc (sz); |
| 557 |
if (tmp == NULL) |
| 558 |
abort (); |
| 559 |
|
| 560 |
va_start(args, format); |
| 561 |
ret2 = vsprintf (tmp, format, args); |
| 562 |
va_end(args); |
| 563 |
if (ret != ret2 || strcmp(str, tmp)) |
| 564 |
abort (); |
| 565 |
free (tmp); |
| 566 |
} |
| 567 |
#endif |
| 568 |
|
| 569 |
return ret; |
| 570 |
} |
| 571 |
#endif |
| 572 |
|
| 573 |
#if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF) |
| 574 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 575 |
rk_asprintf (char **ret, const char *format, ...) |
| 576 |
{ |
| 577 |
va_list args; |
| 578 |
int val; |
| 579 |
|
| 580 |
va_start(args, format); |
| 581 |
val = vasprintf (ret, format, args); |
| 582 |
va_end(args); |
| 583 |
|
| 584 |
#ifdef PARANOIA |
| 585 |
{ |
| 586 |
int ret2; |
| 587 |
char *tmp; |
| 588 |
tmp = malloc (val + 1); |
| 589 |
if (tmp == NULL) |
| 590 |
abort (); |
| 591 |
|
| 592 |
va_start(args, format); |
| 593 |
ret2 = vsprintf (tmp, format, args); |
| 594 |
va_end(args); |
| 595 |
if (val != ret2 || strcmp(*ret, tmp)) |
| 596 |
abort (); |
| 597 |
free (tmp); |
| 598 |
} |
| 599 |
#endif |
| 600 |
|
| 601 |
return val; |
| 602 |
} |
| 603 |
#endif |
| 604 |
|
| 605 |
#if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF) |
| 606 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 607 |
rk_asnprintf (char **ret, size_t max_sz, const char *format, ...) |
| 608 |
{ |
| 609 |
va_list args; |
| 610 |
int val; |
| 611 |
|
| 612 |
va_start(args, format); |
| 613 |
val = vasnprintf (ret, max_sz, format, args); |
| 614 |
|
| 615 |
#ifdef PARANOIA |
| 616 |
{ |
| 617 |
int ret2; |
| 618 |
char *tmp; |
| 619 |
tmp = malloc (val + 1); |
| 620 |
if (tmp == NULL) |
| 621 |
abort (); |
| 622 |
|
| 623 |
ret2 = vsprintf (tmp, format, args); |
| 624 |
if (val != ret2 || strcmp(*ret, tmp)) |
| 625 |
abort (); |
| 626 |
free (tmp); |
| 627 |
} |
| 628 |
#endif |
| 629 |
|
| 630 |
va_end(args); |
| 631 |
return val; |
| 632 |
} |
| 633 |
#endif |
| 634 |
|
| 635 |
#if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF) |
| 636 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 637 |
rk_vasprintf (char **ret, const char *format, va_list args) |
| 638 |
{ |
| 639 |
return vasnprintf (ret, 0, format, args); |
| 640 |
} |
| 641 |
#endif |
| 642 |
|
| 643 |
|
| 644 |
#if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF) |
| 645 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 646 |
rk_vasnprintf (char **ret, size_t max_sz, const char *format, va_list args) |
| 647 |
{ |
| 648 |
size_t st; |
| 649 |
struct snprintf_state state; |
| 650 |
|
| 651 |
state.max_sz = max_sz; |
| 652 |
state.sz = 1; |
| 653 |
state.str = malloc(state.sz); |
| 654 |
if (state.str == NULL) { |
| 655 |
*ret = NULL; |
| 656 |
return -1; |
| 657 |
} |
| 658 |
state.s = state.str; |
| 659 |
state.theend = state.s + state.sz - 1; |
| 660 |
state.append_char = as_append_char; |
| 661 |
|
| 662 |
st = xyzprintf (&state, format, args); |
| 663 |
if (st > state.sz) { |
| 664 |
free (state.str); |
| 665 |
*ret = NULL; |
| 666 |
return -1; |
| 667 |
} else { |
| 668 |
char *tmp; |
| 669 |
|
| 670 |
*state.s = '\0'; |
| 671 |
tmp = realloc (state.str, st+1); |
| 672 |
if (tmp == NULL) { |
| 673 |
free (state.str); |
| 674 |
*ret = NULL; |
| 675 |
return -1; |
| 676 |
} |
| 677 |
*ret = tmp; |
| 678 |
return st; |
| 679 |
} |
| 680 |
} |
| 681 |
#endif |
| 682 |
|
| 683 |
#if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF) |
| 684 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 685 |
rk_vsnprintf (char *str, size_t sz, const char *format, va_list args) |
| 686 |
{ |
| 687 |
struct snprintf_state state; |
| 688 |
int ret; |
| 689 |
unsigned char *ustr = (unsigned char *)str; |
| 690 |
|
| 691 |
state.max_sz = 0; |
| 692 |
state.sz = sz; |
| 693 |
state.str = ustr; |
| 694 |
state.s = ustr; |
| 695 |
state.theend = ustr + sz - (sz > 0); |
| 696 |
state.append_char = sn_append_char; |
| 697 |
|
| 698 |
ret = xyzprintf (&state, format, args); |
| 699 |
if (state.s != NULL && sz != 0) |
| 700 |
*state.s = '\0'; |
| 701 |
return ret; |
| 702 |
} |
| 703 |
#endif |