| 1 | /* This is taken from cvslock. The same code is used in several |
| 2 | * freeware and GPLd applications. I contacted the email addresses |
| 3 | * listed in the header and have been told that the code is probably |
| 4 | * public domain, but I am still seeking confirmation from Patrick |
| 5 | * Powell. The original code was posted to BugTraq by Patrick in 1995 |
| 6 | * but without any notice as to the copyright or license status. |
| 7 | * |
| 8 | * October 1998, Andrew Tridgell (tridge@samba.anu.edu.au) |
| 9 | */ |
| 10 | |
| 11 | /************************************************************** |
| 12 | * Original: |
| 13 | * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 |
| 14 | * A bombproof version of doprnt (dopr) included. |
| 15 | * Sigh. This sort of thing is always nasty do deal with. Note that |
| 16 | * the version here does not include floating point... |
| 17 | * |
| 18 | * snprintf() is used instead of sprintf() as it does limit checks |
| 19 | * for string length. This covers a nasty loophole. |
| 20 | * |
| 21 | * The other functions are there to prevent NULL pointers from |
| 22 | * causing nast effects. |
| 23 | * |
| 24 | * More Recently: |
| 25 | * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 |
| 26 | * This was ugly. It is still ugly. I opted out of floating point |
| 27 | * numbers, but the formatter understands just about everything |
| 28 | * from the normal C string format, at least as far as I can tell from |
| 29 | * the Solaris 2.5 printf(3S) man page. |
| 30 | * |
| 31 | * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 |
| 32 | * Ok, added some minimal floating point support, which means this |
| 33 | * probably requires libm on most operating systems. Don't yet |
| 34 | * support the exponent (e,E) and sigfig (g,G). Also, fmtint() |
| 35 | * was pretty badly broken, it just wasn't being exercised in ways |
| 36 | * which showed it, so that's been fixed. Also, formated the code |
| 37 | * to mutt conventions, and removed dead code left over from the |
| 38 | * original. Also, there is now a builtin-test, just compile with: |
| 39 | * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm |
| 40 | * and run snprintf for results. |
| 41 | * |
| 42 | * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i |
| 43 | * The PGP code was using unsigned hexadecimal formats. |
| 44 | * Unfortunately, unsigned formats simply didn't work. |
| 45 | * |
| 46 | * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 |
| 47 | * The original code assumed that both snprintf() and vsnprintf() were |
| 48 | * missing. Some systems only have snprintf() but not vsnprintf(), so |
| 49 | * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. |
| 50 | * |
| 51 | * Andrew Tridgell (tridge@samba.anu.edu.au) Oct 1998 |
| 52 | * fixed handling of %.0f |
| 53 | * added test for HAVE_LONG_DOUBLE |
| 54 | * |
| 55 | **************************************************************/ |
| 56 | |
| 57 | #include "config.h" |
| 58 | |
| 59 | #include <string.h> |
| 60 | # include <ctype.h> |
| 61 | #include <sys/types.h> |
| 62 | |
| 63 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
| 64 | |
| 65 | /* Define this as a fall through, HAVE_STDARG_H is probably already set */ |
| 66 | |
| 67 | #define HAVE_VARARGS_H |
| 68 | |
| 69 | /* varargs declarations: */ |
| 70 | |
| 71 | #if defined(HAVE_STDARG_H) |
| 72 | # include <stdarg.h> |
| 73 | # define HAVE_STDARGS /* let's hope that works everywhere (mj) */ |
| 74 | # define VA_LOCAL_DECL va_list ap |
| 75 | # define VA_START(f) va_start(ap, f) |
| 76 | # define VA_SHIFT(v,t) ; /* no-op for ANSI */ |
| 77 | # define VA_END va_end(ap) |
| 78 | #else |
| 79 | # if defined(HAVE_VARARGS_H) |
| 80 | # include <varargs.h> |
| 81 | # undef HAVE_STDARGS |
| 82 | # define VA_LOCAL_DECL va_list ap |
| 83 | # define VA_START(f) va_start(ap) /* f is ignored! */ |
| 84 | # define VA_SHIFT(v,t) v = va_arg(ap,t) |
| 85 | # define VA_END va_end(ap) |
| 86 | # else |
| 87 | /*XX ** NO VARARGS ** XX*/ |
| 88 | # endif |
| 89 | #endif |
| 90 | |
| 91 | #ifdef HAVE_LONG_DOUBLE |
| 92 | #define LDOUBLE long double |
| 93 | #else |
| 94 | #define LDOUBLE double |
| 95 | #endif |
| 96 | |
| 97 | /*int snprintf (char *str, size_t count, const char *fmt, ...);*/ |
| 98 | /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/ |
| 99 | |
| 100 | static void dopr (char *buffer, size_t maxlen, const char *format, |
| 101 | va_list args); |
| 102 | static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
| 103 | char *value, int flags, int min, int max); |
| 104 | static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
| 105 | long value, int base, int min, int max, int flags); |
| 106 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 107 | LDOUBLE fvalue, int min, int max, int flags); |
| 108 | static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); |
| 109 | |
| 110 | /* |
| 111 | * dopr(): poor man's version of doprintf |
| 112 | */ |
| 113 | |
| 114 | /* format read states */ |
| 115 | #define DP_S_DEFAULT 0 |
| 116 | #define DP_S_FLAGS 1 |
| 117 | #define DP_S_MIN 2 |
| 118 | #define DP_S_DOT 3 |
| 119 | #define DP_S_MAX 4 |
| 120 | #define DP_S_MOD 5 |
| 121 | #define DP_S_CONV 6 |
| 122 | #define DP_S_DONE 7 |
| 123 | |
| 124 | /* format flags - Bits */ |
| 125 | #define DP_F_MINUS (1 << 0) |
| 126 | #define DP_F_PLUS (1 << 1) |
| 127 | #define DP_F_SPACE (1 << 2) |
| 128 | #define DP_F_NUM (1 << 3) |
| 129 | #define DP_F_ZERO (1 << 4) |
| 130 | #define DP_F_UP (1 << 5) |
| 131 | #define DP_F_UNSIGNED (1 << 6) |
| 132 | |
| 133 | /* Conversion Flags */ |
| 134 | #define DP_C_SHORT 1 |
| 135 | #define DP_C_LONG 2 |
| 136 | #define DP_C_LDOUBLE 3 |
| 137 | |
| 138 | #define char_to_int(p) (p - '0') |
| 139 | #define MAX(p,q) ((p >= q) ? p : q) |
| 140 | |
| 141 | static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) |
| 142 | { |
| 143 | char ch; |
| 144 | long value; |
| 145 | LDOUBLE fvalue; |
| 146 | char *strvalue; |
| 147 | int min; |
| 148 | int max; |
| 149 | int state; |
| 150 | int flags; |
| 151 | int cflags; |
| 152 | size_t currlen; |
| 153 | |
| 154 | state = DP_S_DEFAULT; |
| 155 | currlen = flags = cflags = min = 0; |
| 156 | max = -1; |
| 157 | ch = *format++; |
| 158 | |
| 159 | while (state != DP_S_DONE) |
| 160 | { |
| 161 | if ((ch == '\0') || (currlen >= maxlen)) |
| 162 | state = DP_S_DONE; |
| 163 | |
| 164 | switch(state) |
| 165 | { |
| 166 | case DP_S_DEFAULT: |
| 167 | if (ch == '%') |
| 168 | state = DP_S_FLAGS; |
| 169 | else |
| 170 | dopr_outch (buffer, &currlen, maxlen, ch); |
| 171 | ch = *format++; |
| 172 | break; |
| 173 | case DP_S_FLAGS: |
| 174 | switch (ch) |
| 175 | { |
| 176 | case '-': |
| 177 | flags |= DP_F_MINUS; |
| 178 | ch = *format++; |
| 179 | break; |
| 180 | case '+': |
| 181 | flags |= DP_F_PLUS; |
| 182 | ch = *format++; |
| 183 | break; |
| 184 | case ' ': |
| 185 | flags |= DP_F_SPACE; |
| 186 | ch = *format++; |
| 187 | break; |
| 188 | case '#': |
| 189 | flags |= DP_F_NUM; |
| 190 | ch = *format++; |
| 191 | break; |
| 192 | case '0': |
| 193 | flags |= DP_F_ZERO; |
| 194 | ch = *format++; |
| 195 | break; |
| 196 | default: |
| 197 | state = DP_S_MIN; |
| 198 | break; |
| 199 | } |
| 200 | break; |
| 201 | case DP_S_MIN: |
| 202 | if (isdigit((unsigned char)ch)) |
| 203 | { |
| 204 | min = 10*min + char_to_int (ch); |
| 205 | ch = *format++; |
| 206 | } |
| 207 | else if (ch == '*') |
| 208 | { |
| 209 | min = va_arg (args, int); |
| 210 | ch = *format++; |
| 211 | state = DP_S_DOT; |
| 212 | } |
| 213 | else |
| 214 | state = DP_S_DOT; |
| 215 | break; |
| 216 | case DP_S_DOT: |
| 217 | if (ch == '.') |
| 218 | { |
| 219 | state = DP_S_MAX; |
| 220 | ch = *format++; |
| 221 | } |
| 222 | else |
| 223 | state = DP_S_MOD; |
| 224 | break; |
| 225 | case DP_S_MAX: |
| 226 | if (isdigit((unsigned char)ch)) |
| 227 | { |
| 228 | if (max < 0) |
| 229 | max = 0; |
| 230 | max = 10*max + char_to_int (ch); |
| 231 | ch = *format++; |
| 232 | } |
| 233 | else if (ch == '*') |
| 234 | { |
| 235 | max = va_arg (args, int); |
| 236 | ch = *format++; |
| 237 | state = DP_S_MOD; |
| 238 | } |
| 239 | else |
| 240 | state = DP_S_MOD; |
| 241 | break; |
| 242 | case DP_S_MOD: |
| 243 | /* Currently, we don't support Long Long, bummer */ |
| 244 | switch (ch) |
| 245 | { |
| 246 | case 'h': |
| 247 | cflags = DP_C_SHORT; |
| 248 | ch = *format++; |
| 249 | break; |
| 250 | case 'l': |
| 251 | cflags = DP_C_LONG; |
| 252 | ch = *format++; |
| 253 | break; |
| 254 | case 'L': |
| 255 | cflags = DP_C_LDOUBLE; |
| 256 | ch = *format++; |
| 257 | break; |
| 258 | default: |
| 259 | break; |
| 260 | } |
| 261 | state = DP_S_CONV; |
| 262 | break; |
| 263 | case DP_S_CONV: |
| 264 | switch (ch) |
| 265 | { |
| 266 | case 'd': |
| 267 | case 'i': |
| 268 | if (cflags == DP_C_SHORT) |
| 269 | value = va_arg (args, short int); |
| 270 | else if (cflags == DP_C_LONG) |
| 271 | value = va_arg (args, long int); |
| 272 | else |
| 273 | value = va_arg (args, int); |
| 274 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 275 | break; |
| 276 | case 'o': |
| 277 | flags |= DP_F_UNSIGNED; |
| 278 | if (cflags == DP_C_SHORT) |
| 279 | value = va_arg (args, unsigned short int); |
| 280 | else if (cflags == DP_C_LONG) |
| 281 | value = va_arg (args, unsigned long int); |
| 282 | else |
| 283 | value = va_arg (args, unsigned int); |
| 284 | fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
| 285 | break; |
| 286 | case 'u': |
| 287 | flags |= DP_F_UNSIGNED; |
| 288 | if (cflags == DP_C_SHORT) |
| 289 | value = va_arg (args, unsigned short int); |
| 290 | else if (cflags == DP_C_LONG) |
| 291 | value = va_arg (args, unsigned long int); |
| 292 | else |
| 293 | value = va_arg (args, unsigned int); |
| 294 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 295 | break; |
| 296 | case 'X': |
| 297 | flags |= DP_F_UP; |
| 298 | case 'x': |
| 299 | flags |= DP_F_UNSIGNED; |
| 300 | if (cflags == DP_C_SHORT) |
| 301 | value = va_arg (args, unsigned short int); |
| 302 | else if (cflags == DP_C_LONG) |
| 303 | value = va_arg (args, unsigned long int); |
| 304 | else |
| 305 | value = va_arg (args, unsigned int); |
| 306 | fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
| 307 | break; |
| 308 | case 'f': |
| 309 | if (cflags == DP_C_LDOUBLE) |
| 310 | fvalue = va_arg (args, LDOUBLE); |
| 311 | else |
| 312 | fvalue = va_arg (args, double); |
| 313 | /* um, floating point? */ |
| 314 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
| 315 | break; |
| 316 | case 'E': |
| 317 | flags |= DP_F_UP; |
| 318 | case 'e': |
| 319 | if (cflags == DP_C_LDOUBLE) |
| 320 | fvalue = va_arg (args, LDOUBLE); |
| 321 | else |
| 322 | fvalue = va_arg (args, double); |
| 323 | break; |
| 324 | case 'G': |
| 325 | flags |= DP_F_UP; |
| 326 | case 'g': |
| 327 | if (cflags == DP_C_LDOUBLE) |
| 328 | fvalue = va_arg (args, LDOUBLE); |
| 329 | else |
| 330 | fvalue = va_arg (args, double); |
| 331 | break; |
| 332 | case 'c': |
| 333 | dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
| 334 | break; |
| 335 | case 's': |
| 336 | strvalue = va_arg (args, char *); |
| 337 | if (max < 0) |
| 338 | max = maxlen; /* ie, no max */ |
| 339 | fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
| 340 | break; |
| 341 | case 'p': |
| 342 | strvalue = va_arg (args, void *); |
| 343 | fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
| 344 | break; |
| 345 | case 'n': |
| 346 | if (cflags == DP_C_SHORT) |
| 347 | { |
| 348 | short int *num; |
| 349 | num = va_arg (args, short int *); |
| 350 | *num = currlen; |
| 351 | } |
| 352 | else if (cflags == DP_C_LONG) |
| 353 | { |
| 354 | long int *num; |
| 355 | num = va_arg (args, long int *); |
| 356 | *num = currlen; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | int *num; |
| 361 | num = va_arg (args, int *); |
| 362 | *num = currlen; |
| 363 | } |
| 364 | break; |
| 365 | case '%': |
| 366 | dopr_outch (buffer, &currlen, maxlen, ch); |
| 367 | break; |
| 368 | case 'w': |
| 369 | /* not supported yet, treat as next char */ |
| 370 | ch = *format++; |
| 371 | break; |
| 372 | default: |
| 373 | /* Unknown, skip */ |
| 374 | break; |
| 375 | } |
| 376 | ch = *format++; |
| 377 | state = DP_S_DEFAULT; |
| 378 | flags = cflags = min = 0; |
| 379 | max = -1; |
| 380 | break; |
| 381 | case DP_S_DONE: |
| 382 | break; |
| 383 | default: |
| 384 | /* hmm? */ |
| 385 | break; /* some picky compilers need this */ |
| 386 | } |
| 387 | } |
| 388 | if (currlen < maxlen - 1) |
| 389 | buffer[currlen] = '\0'; |
| 390 | else |
| 391 | buffer[maxlen - 1] = '\0'; |
| 392 | } |
| 393 | |
| 394 | static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
| 395 | char *value, int flags, int min, int max) |
| 396 | { |
| 397 | int padlen, strln; /* amount to pad */ |
| 398 | int cnt = 0; |
| 399 | |
| 400 | if (value == 0) |
| 401 | { |
| 402 | value = "<NULL>"; |
| 403 | } |
| 404 | |
| 405 | for (strln = 0; value[strln]; ++strln); /* strlen */ |
| 406 | padlen = min - strln; |
| 407 | if (padlen < 0) |
| 408 | padlen = 0; |
| 409 | if (flags & DP_F_MINUS) |
| 410 | padlen = -padlen; /* Left Justify */ |
| 411 | |
| 412 | while ((padlen > 0) && (cnt < max)) |
| 413 | { |
| 414 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 415 | --padlen; |
| 416 | ++cnt; |
| 417 | } |
| 418 | while (*value && (cnt < max)) |
| 419 | { |
| 420 | dopr_outch (buffer, currlen, maxlen, *value++); |
| 421 | ++cnt; |
| 422 | } |
| 423 | while ((padlen < 0) && (cnt < max)) |
| 424 | { |
| 425 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 426 | ++padlen; |
| 427 | ++cnt; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 432 | |
| 433 | static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
| 434 | long value, int base, int min, int max, int flags) |
| 435 | { |
| 436 | int signvalue = 0; |
| 437 | unsigned long uvalue; |
| 438 | char convert[20]; |
| 439 | int place = 0; |
| 440 | int spadlen = 0; /* amount to space pad */ |
| 441 | int zpadlen = 0; /* amount to zero pad */ |
| 442 | int caps = 0; |
| 443 | |
| 444 | if (max < 0) |
| 445 | max = 0; |
| 446 | |
| 447 | uvalue = value; |
| 448 | |
| 449 | if(!(flags & DP_F_UNSIGNED)) |
| 450 | { |
| 451 | if( value < 0 ) { |
| 452 | signvalue = '-'; |
| 453 | uvalue = -value; |
| 454 | } |
| 455 | else |
| 456 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 457 | signvalue = '+'; |
| 458 | else |
| 459 | if (flags & DP_F_SPACE) |
| 460 | signvalue = ' '; |
| 461 | } |
| 462 | |
| 463 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 464 | |
| 465 | do { |
| 466 | convert[place++] = |
| 467 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 468 | [uvalue % (unsigned)base ]; |
| 469 | uvalue = (uvalue / (unsigned)base ); |
| 470 | } while(uvalue && (place < 20)); |
| 471 | if (place == 20) place--; |
| 472 | convert[place] = 0; |
| 473 | |
| 474 | zpadlen = max - place; |
| 475 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
| 476 | if (zpadlen < 0) zpadlen = 0; |
| 477 | if (spadlen < 0) spadlen = 0; |
| 478 | if (flags & DP_F_ZERO) |
| 479 | { |
| 480 | zpadlen = MAX(zpadlen, spadlen); |
| 481 | spadlen = 0; |
| 482 | } |
| 483 | if (flags & DP_F_MINUS) |
| 484 | spadlen = -spadlen; /* Left Justifty */ |
| 485 | |
| 486 | #ifdef DEBUG_SNPRINTF |
| 487 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 488 | zpadlen, spadlen, min, max, place); |
| 489 | #endif |
| 490 | |
| 491 | /* Spaces */ |
| 492 | while (spadlen > 0) |
| 493 | { |
| 494 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 495 | --spadlen; |
| 496 | } |
| 497 | |
| 498 | /* Sign */ |
| 499 | if (signvalue) |
| 500 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 501 | |
| 502 | /* Zeros */ |
| 503 | if (zpadlen > 0) |
| 504 | { |
| 505 | while (zpadlen > 0) |
| 506 | { |
| 507 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 508 | --zpadlen; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* Digits */ |
| 513 | while (place > 0) |
| 514 | dopr_outch (buffer, currlen, maxlen, convert[--place]); |
| 515 | |
| 516 | /* Left Justified spaces */ |
| 517 | while (spadlen < 0) { |
| 518 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 519 | ++spadlen; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static LDOUBLE abs_val (LDOUBLE value) |
| 524 | { |
| 525 | LDOUBLE result = value; |
| 526 | |
| 527 | if (value < 0) |
| 528 | result = -value; |
| 529 | |
| 530 | return result; |
| 531 | } |
| 532 | |
| 533 | static LDOUBLE pow10 (int exp) |
| 534 | { |
| 535 | LDOUBLE result = 1; |
| 536 | |
| 537 | while (exp) |
| 538 | { |
| 539 | result *= 10; |
| 540 | exp--; |
| 541 | } |
| 542 | |
| 543 | return result; |
| 544 | } |
| 545 | |
| 546 | static long round (LDOUBLE value) |
| 547 | { |
| 548 | long intpart; |
| 549 | |
| 550 | intpart = value; |
| 551 | value = value - intpart; |
| 552 | if (value >= 0.5) |
| 553 | intpart++; |
| 554 | |
| 555 | return intpart; |
| 556 | } |
| 557 | |
| 558 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 559 | LDOUBLE fvalue, int min, int max, int flags) |
| 560 | { |
| 561 | int signvalue = 0; |
| 562 | LDOUBLE ufvalue; |
| 563 | char iconvert[20]; |
| 564 | char fconvert[20]; |
| 565 | int iplace = 0; |
| 566 | int fplace = 0; |
| 567 | int padlen = 0; /* amount to pad */ |
| 568 | int zpadlen = 0; |
| 569 | int caps = 0; |
| 570 | long intpart; |
| 571 | long fracpart; |
| 572 | |
| 573 | /* |
| 574 | * AIX manpage says the default is 0, but Solaris says the default |
| 575 | * is 6, and sprintf on AIX defaults to 6 |
| 576 | */ |
| 577 | if (max < 0) |
| 578 | max = 6; |
| 579 | |
| 580 | ufvalue = abs_val (fvalue); |
| 581 | |
| 582 | if (fvalue < 0) |
| 583 | signvalue = '-'; |
| 584 | else |
| 585 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 586 | signvalue = '+'; |
| 587 | else |
| 588 | if (flags & DP_F_SPACE) |
| 589 | signvalue = ' '; |
| 590 | |
| 591 | #if 0 |
| 592 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 593 | #endif |
| 594 | |
| 595 | intpart = ufvalue; |
| 596 | |
| 597 | /* |
| 598 | * Sorry, we only support 9 digits past the decimal because of our |
| 599 | * conversion method |
| 600 | */ |
| 601 | if (max > 9) |
| 602 | max = 9; |
| 603 | |
| 604 | /* We "cheat" by converting the fractional part to integer by |
| 605 | * multiplying by a factor of 10 |
| 606 | */ |
| 607 | fracpart = round ((pow10 (max)) * (ufvalue - intpart)); |
| 608 | |
| 609 | if (fracpart >= pow10 (max)) |
| 610 | { |
| 611 | intpart++; |
| 612 | fracpart -= pow10 (max); |
| 613 | } |
| 614 | |
| 615 | #ifdef DEBUG_SNPRINTF |
| 616 | printf("fmtfp: %g %d.%d min=%d max=%d\n", |
| 617 | (double)fvalue, intpart, fracpart, min, max); |
| 618 | #endif |
| 619 | |
| 620 | /* Convert integer part */ |
| 621 | do { |
| 622 | iconvert[iplace++] = |
| 623 | (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; |
| 624 | intpart = (intpart / 10); |
| 625 | } while(intpart && (iplace < 20)); |
| 626 | if (iplace == 20) iplace--; |
| 627 | iconvert[iplace] = 0; |
| 628 | |
| 629 | /* Convert fractional part */ |
| 630 | do { |
| 631 | fconvert[fplace++] = |
| 632 | (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; |
| 633 | fracpart = (fracpart / 10); |
| 634 | } while(fracpart && (fplace < 20)); |
| 635 | if (fplace == 20) fplace--; |
| 636 | fconvert[fplace] = 0; |
| 637 | |
| 638 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 639 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 640 | zpadlen = max - fplace; |
| 641 | if (zpadlen < 0) |
| 642 | zpadlen = 0; |
| 643 | if (padlen < 0) |
| 644 | padlen = 0; |
| 645 | if (flags & DP_F_MINUS) |
| 646 | padlen = -padlen; /* Left Justifty */ |
| 647 | |
| 648 | if ((flags & DP_F_ZERO) && (padlen > 0)) |
| 649 | { |
| 650 | if (signvalue) |
| 651 | { |
| 652 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 653 | --padlen; |
| 654 | signvalue = 0; |
| 655 | } |
| 656 | while (padlen > 0) |
| 657 | { |
| 658 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 659 | --padlen; |
| 660 | } |
| 661 | } |
| 662 | while (padlen > 0) |
| 663 | { |
| 664 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 665 | --padlen; |
| 666 | } |
| 667 | if (signvalue) |
| 668 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 669 | |
| 670 | while (iplace > 0) |
| 671 | dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
| 672 | |
| 673 | |
| 674 | #ifdef DEBUG_SNPRINTF |
| 675 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 676 | #endif |
| 677 | |
| 678 | /* |
| 679 | * Decimal point. This should probably use locale to find the correct |
| 680 | * char to print out. |
| 681 | */ |
| 682 | if (max > 0) { |
| 683 | dopr_outch (buffer, currlen, maxlen, '.'); |
| 684 | |
| 685 | while (fplace > 0) |
| 686 | dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
| 687 | } |
| 688 | |
| 689 | while (zpadlen > 0) |
| 690 | { |
| 691 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 692 | --zpadlen; |
| 693 | } |
| 694 | |
| 695 | while (padlen < 0) |
| 696 | { |
| 697 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 698 | ++padlen; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) |
| 703 | { |
| 704 | if (*currlen < maxlen) |
| 705 | buffer[(*currlen)++] = c; |
| 706 | } |
| 707 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 708 | |
| 709 | #ifndef HAVE_VSNPRINTF |
| 710 | int vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
| 711 | { |
| 712 | str[0] = 0; |
| 713 | dopr(str, count, fmt, args); |
| 714 | return(strlen(str)); |
| 715 | } |
| 716 | #endif /* !HAVE_VSNPRINTF */ |
| 717 | |
| 718 | #ifndef HAVE_SNPRINTF |
| 719 | /* VARARGS3 */ |
| 720 | #ifdef HAVE_STDARGS |
| 721 | int snprintf (char *str,size_t count,const char *fmt,...) |
| 722 | #else |
| 723 | int snprintf (va_alist) va_dcl |
| 724 | #endif |
| 725 | { |
| 726 | #ifndef HAVE_STDARGS |
| 727 | char *str; |
| 728 | size_t count; |
| 729 | char *fmt; |
| 730 | #endif |
| 731 | VA_LOCAL_DECL; |
| 732 | |
| 733 | VA_START (fmt); |
| 734 | VA_SHIFT (str, char *); |
| 735 | VA_SHIFT (count, size_t ); |
| 736 | VA_SHIFT (fmt, char *); |
| 737 | (void) vsnprintf(str, count, fmt, ap); |
| 738 | VA_END; |
| 739 | return(strlen(str)); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | #else |
| 744 | /* keep compilers happy about empty files */ |
| 745 | void dummy_snprintf(void) {} |
| 746 | #endif /* !HAVE_SNPRINTF */ |
| 747 | |
| 748 | #ifdef TEST_SNPRINTF |
| 749 | #ifndef LONG_STRING |
| 750 | #define LONG_STRING 1024 |
| 751 | #endif |
| 752 | int main (void) |
| 753 | { |
| 754 | char buf1[LONG_STRING]; |
| 755 | char buf2[LONG_STRING]; |
| 756 | char *fp_fmt[] = { |
| 757 | "%-1.5f", |
| 758 | "%1.5f", |
| 759 | "%123.9f", |
| 760 | "%10.5f", |
| 761 | "% 10.5f", |
| 762 | "%+22.9f", |
| 763 | "%+4.9f", |
| 764 | "%01.3f", |
| 765 | "%4f", |
| 766 | "%3.1f", |
| 767 | "%3.2f", |
| 768 | "%.0f", |
| 769 | "%.1f", |
| 770 | NULL |
| 771 | }; |
| 772 | double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, |
| 773 | 0.9996, 1.996, 4.136, 0}; |
| 774 | char *int_fmt[] = { |
| 775 | "%-1.5d", |
| 776 | "%1.5d", |
| 777 | "%123.9d", |
| 778 | "%5.5d", |
| 779 | "%10.5d", |
| 780 | "% 10.5d", |
| 781 | "%+22.33d", |
| 782 | "%01.3d", |
| 783 | "%4d", |
| 784 | NULL |
| 785 | }; |
| 786 | long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; |
| 787 | int x, y; |
| 788 | int fail = 0; |
| 789 | int num = 0; |
| 790 | |
| 791 | printf ("Testing snprintf format codes against system sprintf...\n"); |
| 792 | |
| 793 | for (x = 0; fp_fmt[x] != NULL ; x++) |
| 794 | for (y = 0; fp_nums[y] != 0 ; y++) |
| 795 | { |
| 796 | snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); |
| 797 | sprintf (buf2, fp_fmt[x], fp_nums[y]); |
| 798 | if (strcmp (buf1, buf2)) |
| 799 | { |
| 800 | printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", |
| 801 | fp_fmt[x], buf1, buf2); |
| 802 | fail++; |
| 803 | } |
| 804 | num++; |
| 805 | } |
| 806 | |
| 807 | for (x = 0; int_fmt[x] != NULL ; x++) |
| 808 | for (y = 0; int_nums[y] != 0 ; y++) |
| 809 | { |
| 810 | snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); |
| 811 | sprintf (buf2, int_fmt[x], int_nums[y]); |
| 812 | if (strcmp (buf1, buf2)) |
| 813 | { |
| 814 | printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", |
| 815 | int_fmt[x], buf1, buf2); |
| 816 | fail++; |
| 817 | } |
| 818 | num++; |
| 819 | } |
| 820 | printf ("%d tests failed out of %d.\n", fail, num); |
| 821 | } |
| 822 | #endif /* SNPRINTF_TEST */ |
| 823 | |