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