| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#include <string.h> |
| 62 |
|
| 63 |
#include "config.h" |
| 64 |
#include "compat.h" |
| 65 |
|
| 66 |
#ifndef HAVE_STRLCAT |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
size_t |
| 75 |
strlcat(char *dst, const char *src, size_t siz) |
| 76 |
{ |
| 77 |
char *d = dst; |
| 78 |
const char *s = src; |
| 79 |
size_t n = siz; |
| 80 |
size_t dlen; |
| 81 |
|
| 82 |
|
| 83 |
while (n-- != 0 && *d != '\0') |
| 84 |
d++; |
| 85 |
dlen = d - dst; |
| 86 |
n = siz - dlen; |
| 87 |
|
| 88 |
if (n == 0) |
| 89 |
return(dlen + strlen(s)); |
| 90 |
while (*s != '\0') { |
| 91 |
if (n != 1) { |
| 92 |
*d++ = *s; |
| 93 |
n--; |
| 94 |
} |
| 95 |
s++; |
| 96 |
} |
| 97 |
*d = '\0'; |
| 98 |
|
| 99 |
return(dlen + (s - src)); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
|
| 104 |
#ifndef HAVE_STRLCPY |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
size_t |
| 111 |
strlcpy(char *dst, const char *src, size_t siz) |
| 112 |
{ |
| 113 |
char *d = dst; |
| 114 |
const char *s = src; |
| 115 |
size_t n = siz; |
| 116 |
|
| 117 |
|
| 118 |
if (n != 0 && --n != 0) { |
| 119 |
do { |
| 120 |
if ((*d++ = *s++) == 0) |
| 121 |
break; |
| 122 |
} while (--n != 0); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
if (n == 0) { |
| 127 |
if (siz != 0) |
| 128 |
*d = '\0'; |
| 129 |
while (*s++) |
| 130 |
; |
| 131 |
} |
| 132 |
|
| 133 |
return(s - src - 1); |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifndef HAVE_GETOPT |
| 138 |
char *optarg; |
| 139 |
int optind = 1; |
| 140 |
int |
| 141 |
getopt(int argc, char * const argv[], const char *args) |
| 142 |
{ |
| 143 |
int n; |
| 144 |
int nlen = strlen(args); |
| 145 |
char cmd; |
| 146 |
char rv; |
| 147 |
|
| 148 |
if (argv[optind] && *argv[optind] == '-') { |
| 149 |
cmd = *(argv[optind] + 1); |
| 150 |
|
| 151 |
for (n = 0; n < nlen; n++) { |
| 152 |
if (args[n] == ':') |
| 153 |
continue; |
| 154 |
if (args[n] == cmd) { |
| 155 |
rv = *(argv[optind] + 1); |
| 156 |
if (args[n+1] == ':') { |
| 157 |
if (*(argv[optind] + 2) != '\0') { |
| 158 |
optarg = argv[optind] + 2; |
| 159 |
optind += 1; |
| 160 |
} else { |
| 161 |
optarg = argv[optind + 1]; |
| 162 |
optind += 2; |
| 163 |
} |
| 164 |
if (!optarg) |
| 165 |
optarg=""; |
| 166 |
return rv; |
| 167 |
} else { |
| 168 |
optarg = NULL; |
| 169 |
optind += 1; |
| 170 |
return rv; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return -1; |
| 177 |
} |
| 178 |
#endif |
| 179 |
|
| 180 |
#if !defined(HAVE_MKSTEMP) && !defined(_WIN32) |
| 181 |
#include <fcntl.h> |
| 182 |
#include <unistd.h> |
| 183 |
|
| 184 |
int |
| 185 |
mkstemp(char *path) |
| 186 |
{ |
| 187 |
char *trv; |
| 188 |
unsigned int pid; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static char xtra[2] = "aa"; |
| 194 |
int xcnt = 0; |
| 195 |
|
| 196 |
pid = getpid(); |
| 197 |
|
| 198 |
|
| 199 |
for (trv = path; *trv; ++trv) |
| 200 |
if (*trv == 'X') |
| 201 |
xcnt++; |
| 202 |
else |
| 203 |
xcnt = 0; |
| 204 |
|
| 205 |
|
| 206 |
if (*(trv - 1) == 'X') |
| 207 |
*--trv = xtra[0]; |
| 208 |
if (xcnt > 6 && *(trv - 1) == 'X') |
| 209 |
*--trv = xtra[1]; |
| 210 |
|
| 211 |
|
| 212 |
while (*--trv == 'X') { |
| 213 |
*trv = (pid % 10) + '0'; |
| 214 |
pid /= 10; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if (xtra[0] != 'z') |
| 219 |
xtra[0]++; |
| 220 |
else { |
| 221 |
xtra[0] = 'a'; |
| 222 |
if (xtra[1] != 'z') |
| 223 |
xtra[1]++; |
| 224 |
else |
| 225 |
xtra[1] = 'a'; |
| 226 |
} |
| 227 |
|
| 228 |
return open(path, O_CREAT | O_EXCL | O_RDWR, 0600); |
| 229 |
} |
| 230 |
#endif |
| 231 |
|
| 232 |
#ifndef HAVE_FFS |
| 233 |
int |
| 234 |
ffs(int x) |
| 235 |
{ |
| 236 |
int r = 1; |
| 237 |
if (!x) return 0; |
| 238 |
if (!(x & 0xffff)) { x >>= 16; r += 16; } |
| 239 |
if (!(x & 0xff)) { x >>= 8; r += 8; } |
| 240 |
if (!(x & 0xf)) { x >>= 4; r += 4; } |
| 241 |
if (!(x & 3)) { x >>= 2; r += 2; } |
| 242 |
if (!(x & 1)) { x >>= 1; r += 1; } |
| 243 |
|
| 244 |
return r; |
| 245 |
} |
| 246 |
#endif |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
| 256 |
#include <ctype.h> |
| 257 |
|
| 258 |
static void |
| 259 |
dopr(char *buffer, size_t maxlen, const char *format, va_list args); |
| 260 |
|
| 261 |
static void |
| 262 |
fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags, |
| 263 |
int min, int max); |
| 264 |
|
| 265 |
static void |
| 266 |
fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base, |
| 267 |
int min, int max, int flags); |
| 268 |
|
| 269 |
static void |
| 270 |
fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue, |
| 271 |
int min, int max, int flags); |
| 272 |
|
| 273 |
static void |
| 274 |
dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
#define DP_S_DEFAULT 0 |
| 282 |
#define DP_S_FLAGS 1 |
| 283 |
#define DP_S_MIN 2 |
| 284 |
#define DP_S_DOT 3 |
| 285 |
#define DP_S_MAX 4 |
| 286 |
#define DP_S_MOD 5 |
| 287 |
#define DP_S_CONV 6 |
| 288 |
#define DP_S_DONE 7 |
| 289 |
|
| 290 |
|
| 291 |
#define DP_F_MINUS (1 << 0) |
| 292 |
#define DP_F_PLUS (1 << 1) |
| 293 |
#define DP_F_SPACE (1 << 2) |
| 294 |
#define DP_F_NUM (1 << 3) |
| 295 |
#define DP_F_ZERO (1 << 4) |
| 296 |
#define DP_F_UP (1 << 5) |
| 297 |
#define DP_F_UNSIGNED (1 << 6) |
| 298 |
|
| 299 |
|
| 300 |
#define DP_C_SHORT 1 |
| 301 |
#define DP_C_LONG 2 |
| 302 |
#define DP_C_LDOUBLE 3 |
| 303 |
#define DP_C_LONG_LONG 4 |
| 304 |
|
| 305 |
#define char_to_int(p) (p - '0') |
| 306 |
#define abs_val(p) (p < 0 ? -p : p) |
| 307 |
|
| 308 |
|
| 309 |
static void |
| 310 |
dopr(char *buffer, size_t maxlen, const char *format, va_list args) |
| 311 |
{ |
| 312 |
char *strvalue, ch; |
| 313 |
long value; |
| 314 |
long double fvalue; |
| 315 |
int min = 0, max = -1, state = DP_S_DEFAULT, flags = 0, cflags = 0; |
| 316 |
size_t currlen = 0; |
| 317 |
|
| 318 |
ch = *format++; |
| 319 |
|
| 320 |
while (state != DP_S_DONE) { |
| 321 |
if ((ch == '\0') || (currlen >= maxlen)) |
| 322 |
state = DP_S_DONE; |
| 323 |
|
| 324 |
switch(state) { |
| 325 |
case DP_S_DEFAULT: |
| 326 |
if (ch == '%') |
| 327 |
state = DP_S_FLAGS; |
| 328 |
else |
| 329 |
dopr_outch(buffer, &currlen, maxlen, ch); |
| 330 |
ch = *format++; |
| 331 |
break; |
| 332 |
case DP_S_FLAGS: |
| 333 |
switch (ch) { |
| 334 |
case '-': |
| 335 |
flags |= DP_F_MINUS; |
| 336 |
ch = *format++; |
| 337 |
break; |
| 338 |
case '+': |
| 339 |
flags |= DP_F_PLUS; |
| 340 |
ch = *format++; |
| 341 |
break; |
| 342 |
case ' ': |
| 343 |
flags |= DP_F_SPACE; |
| 344 |
ch = *format++; |
| 345 |
break; |
| 346 |
case '#': |
| 347 |
flags |= DP_F_NUM; |
| 348 |
ch = *format++; |
| 349 |
break; |
| 350 |
case '0': |
| 351 |
flags |= DP_F_ZERO; |
| 352 |
ch = *format++; |
| 353 |
break; |
| 354 |
default: |
| 355 |
state = DP_S_MIN; |
| 356 |
break; |
| 357 |
} |
| 358 |
break; |
| 359 |
case DP_S_MIN: |
| 360 |
if (isdigit((unsigned char)ch)) { |
| 361 |
min = 10 * min + char_to_int (ch); |
| 362 |
ch = *format++; |
| 363 |
} else if (ch == '*') { |
| 364 |
min = va_arg (args, int); |
| 365 |
ch = *format++; |
| 366 |
state = DP_S_DOT; |
| 367 |
} else |
| 368 |
state = DP_S_DOT; |
| 369 |
break; |
| 370 |
case DP_S_DOT: |
| 371 |
if (ch == '.') { |
| 372 |
state = DP_S_MAX; |
| 373 |
ch = *format++; |
| 374 |
} else |
| 375 |
state = DP_S_MOD; |
| 376 |
break; |
| 377 |
case DP_S_MAX: |
| 378 |
if (isdigit((unsigned char)ch)) { |
| 379 |
if (max < 0) |
| 380 |
max = 0; |
| 381 |
max = 10 * max + char_to_int(ch); |
| 382 |
ch = *format++; |
| 383 |
} else if (ch == '*') { |
| 384 |
max = va_arg (args, int); |
| 385 |
ch = *format++; |
| 386 |
state = DP_S_MOD; |
| 387 |
} else |
| 388 |
state = DP_S_MOD; |
| 389 |
break; |
| 390 |
case DP_S_MOD: |
| 391 |
switch (ch) { |
| 392 |
case 'h': |
| 393 |
cflags = DP_C_SHORT; |
| 394 |
ch = *format++; |
| 395 |
break; |
| 396 |
case 'l': |
| 397 |
cflags = DP_C_LONG; |
| 398 |
ch = *format++; |
| 399 |
if (ch == 'l') { |
| 400 |
cflags = DP_C_LONG_LONG; |
| 401 |
ch = *format++; |
| 402 |
} |
| 403 |
break; |
| 404 |
case 'q': |
| 405 |
cflags = DP_C_LONG_LONG; |
| 406 |
ch = *format++; |
| 407 |
break; |
| 408 |
case 'L': |
| 409 |
cflags = DP_C_LDOUBLE; |
| 410 |
ch = *format++; |
| 411 |
break; |
| 412 |
default: |
| 413 |
break; |
| 414 |
} |
| 415 |
state = DP_S_CONV; |
| 416 |
break; |
| 417 |
case DP_S_CONV: |
| 418 |
switch (ch) { |
| 419 |
case 'd': |
| 420 |
case 'i': |
| 421 |
if (cflags == DP_C_SHORT) |
| 422 |
value = va_arg(args, int); |
| 423 |
else if (cflags == DP_C_LONG) |
| 424 |
value = va_arg(args, long int); |
| 425 |
else if (cflags == DP_C_LONG_LONG) |
| 426 |
value = va_arg (args, long long); |
| 427 |
else |
| 428 |
value = va_arg (args, int); |
| 429 |
fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 430 |
break; |
| 431 |
case 'o': |
| 432 |
flags |= DP_F_UNSIGNED; |
| 433 |
if (cflags == DP_C_SHORT) |
| 434 |
value = va_arg(args, unsigned int); |
| 435 |
else if (cflags == DP_C_LONG) |
| 436 |
value = va_arg(args, unsigned long int); |
| 437 |
else if (cflags == DP_C_LONG_LONG) |
| 438 |
value = va_arg(args, unsigned long long); |
| 439 |
else |
| 440 |
value = va_arg(args, unsigned int); |
| 441 |
fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags); |
| 442 |
break; |
| 443 |
case 'u': |
| 444 |
flags |= DP_F_UNSIGNED; |
| 445 |
if (cflags == DP_C_SHORT) |
| 446 |
value = va_arg(args, unsigned int); |
| 447 |
else if (cflags == DP_C_LONG) |
| 448 |
value = va_arg(args, unsigned long int); |
| 449 |
else if (cflags == DP_C_LONG_LONG) |
| 450 |
value = va_arg(args, unsigned long long); |
| 451 |
else |
| 452 |
value = va_arg(args, unsigned int); |
| 453 |
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 454 |
break; |
| 455 |
case 'X': |
| 456 |
flags |= DP_F_UP; |
| 457 |
case 'x': |
| 458 |
flags |= DP_F_UNSIGNED; |
| 459 |
if (cflags == DP_C_SHORT) |
| 460 |
value = va_arg(args, unsigned int); |
| 461 |
else if (cflags == DP_C_LONG) |
| 462 |
value = va_arg(args, unsigned long int); |
| 463 |
else if (cflags == DP_C_LONG_LONG) |
| 464 |
value = va_arg(args, unsigned long long); |
| 465 |
else |
| 466 |
value = va_arg(args, unsigned int); |
| 467 |
fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags); |
| 468 |
break; |
| 469 |
case 'f': |
| 470 |
if (cflags == DP_C_LDOUBLE) |
| 471 |
fvalue = va_arg(args, long double); |
| 472 |
else |
| 473 |
fvalue = va_arg(args, double); |
| 474 |
|
| 475 |
fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags); |
| 476 |
break; |
| 477 |
case 'E': |
| 478 |
flags |= DP_F_UP; |
| 479 |
case 'e': |
| 480 |
if (cflags == DP_C_LDOUBLE) |
| 481 |
fvalue = va_arg(args, long double); |
| 482 |
else |
| 483 |
fvalue = va_arg(args, double); |
| 484 |
break; |
| 485 |
case 'G': |
| 486 |
flags |= DP_F_UP; |
| 487 |
case 'g': |
| 488 |
if (cflags == DP_C_LDOUBLE) |
| 489 |
fvalue = va_arg(args, long double); |
| 490 |
else |
| 491 |
fvalue = va_arg(args, double); |
| 492 |
break; |
| 493 |
case 'c': |
| 494 |
dopr_outch(buffer, &currlen, maxlen, va_arg(args, int)); |
| 495 |
break; |
| 496 |
case 's': |
| 497 |
strvalue = va_arg(args, char *); |
| 498 |
if (max < 0) |
| 499 |
max = maxlen; |
| 500 |
fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max); |
| 501 |
break; |
| 502 |
case 'p': |
| 503 |
strvalue = va_arg(args, void *); |
| 504 |
fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
| 505 |
break; |
| 506 |
case 'n': |
| 507 |
if (cflags == DP_C_SHORT) { |
| 508 |
short int *num; |
| 509 |
num = va_arg(args, short int *); |
| 510 |
*num = currlen; |
| 511 |
} else if (cflags == DP_C_LONG) { |
| 512 |
long int *num; |
| 513 |
num = va_arg(args, long int *); |
| 514 |
*num = currlen; |
| 515 |
} else if (cflags == DP_C_LONG_LONG) { |
| 516 |
long long *num; |
| 517 |
num = va_arg(args, long long *); |
| 518 |
*num = currlen; |
| 519 |
} else { |
| 520 |
int *num; |
| 521 |
num = va_arg(args, int *); |
| 522 |
*num = currlen; |
| 523 |
} |
| 524 |
break; |
| 525 |
case '%': |
| 526 |
dopr_outch(buffer, &currlen, maxlen, ch); |
| 527 |
break; |
| 528 |
case 'w': |
| 529 |
ch = *format++; |
| 530 |
break; |
| 531 |
default: |
| 532 |
break; |
| 533 |
} |
| 534 |
ch = *format++; |
| 535 |
state = DP_S_DEFAULT; |
| 536 |
flags = cflags = min = 0; |
| 537 |
max = -1; |
| 538 |
break; |
| 539 |
case DP_S_DONE: |
| 540 |
break; |
| 541 |
default: |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
if (currlen < maxlen - 1) |
| 546 |
buffer[currlen] = '\0'; |
| 547 |
else |
| 548 |
buffer[maxlen - 1] = '\0'; |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 553 |
char *value, int flags, int min, int max) |
| 554 |
{ |
| 555 |
int cnt = 0, padlen, strln; |
| 556 |
|
| 557 |
if (value == 0) |
| 558 |
value = "<NULL>"; |
| 559 |
|
| 560 |
for (strln = 0; value[strln]; ++strln); |
| 561 |
padlen = min - strln; |
| 562 |
if (padlen < 0) |
| 563 |
padlen = 0; |
| 564 |
if (flags & DP_F_MINUS) |
| 565 |
padlen = -padlen; |
| 566 |
|
| 567 |
while ((padlen > 0) && (cnt < max)) { |
| 568 |
dopr_outch(buffer, currlen, maxlen, ' '); |
| 569 |
--padlen; |
| 570 |
++cnt; |
| 571 |
} |
| 572 |
while (*value && (cnt < max)) { |
| 573 |
dopr_outch(buffer, currlen, maxlen, *value++); |
| 574 |
++cnt; |
| 575 |
} |
| 576 |
while ((padlen < 0) && (cnt < max)) { |
| 577 |
dopr_outch(buffer, currlen, maxlen, ' '); |
| 578 |
++padlen; |
| 579 |
++cnt; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
static void |
| 586 |
fmtint(char *buffer, size_t *currlen, size_t maxlen, |
| 587 |
long value, int base, int min, int max, int flags) |
| 588 |
{ |
| 589 |
unsigned long uvalue; |
| 590 |
char convert[20]; |
| 591 |
int signvalue = 0, place = 0, caps = 0; |
| 592 |
int spadlen = 0; |
| 593 |
int zpadlen = 0; |
| 594 |
|
| 595 |
#define PADMAX(x,y) ((x) > (y) ? (x) : (y)) |
| 596 |
|
| 597 |
if (max < 0) |
| 598 |
max = 0; |
| 599 |
|
| 600 |
uvalue = value; |
| 601 |
|
| 602 |
if (!(flags & DP_F_UNSIGNED)) { |
| 603 |
if (value < 0) { |
| 604 |
signvalue = '-'; |
| 605 |
uvalue = -value; |
| 606 |
} else if (flags & DP_F_PLUS) |
| 607 |
signvalue = '+'; |
| 608 |
else if (flags & DP_F_SPACE) |
| 609 |
signvalue = ' '; |
| 610 |
} |
| 611 |
|
| 612 |
if (flags & DP_F_UP) |
| 613 |
caps = 1; |
| 614 |
do { |
| 615 |
convert[place++] = |
| 616 |
(caps ? "0123456789ABCDEF" : "0123456789abcdef") |
| 617 |
[uvalue % (unsigned)base]; |
| 618 |
uvalue = (uvalue / (unsigned)base ); |
| 619 |
} while (uvalue && (place < 20)); |
| 620 |
if (place == 20) |
| 621 |
place--; |
| 622 |
convert[place] = 0; |
| 623 |
|
| 624 |
zpadlen = max - place; |
| 625 |
spadlen = min - PADMAX(max, place) - (signvalue ? 1 : 0); |
| 626 |
if (zpadlen < 0) |
| 627 |
zpadlen = 0; |
| 628 |
if (spadlen < 0) |
| 629 |
spadlen = 0; |
| 630 |
if (flags & DP_F_ZERO) { |
| 631 |
zpadlen = PADMAX(zpadlen, spadlen); |
| 632 |
spadlen = 0; |
| 633 |
} |
| 634 |
if (flags & DP_F_MINUS) |
| 635 |
spadlen = -spadlen; |
| 636 |
|
| 637 |
|
| 638 |
while (spadlen > 0) { |
| 639 |
dopr_outch(buffer, currlen, maxlen, ' '); |
| 640 |
--spadlen; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
if (signvalue) |
| 645 |
dopr_outch(buffer, currlen, maxlen, signvalue); |
| 646 |
|
| 647 |
|
| 648 |
if (zpadlen > 0) { |
| 649 |
while (zpadlen > 0) { |
| 650 |
dopr_outch(buffer, currlen, maxlen, '0'); |
| 651 |
--zpadlen; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
while (place > 0) |
| 657 |
dopr_outch(buffer, currlen, maxlen, convert[--place]); |
| 658 |
|
| 659 |
|
| 660 |
while (spadlen < 0) { |
| 661 |
dopr_outch (buffer, currlen, maxlen, ' '); |
| 662 |
++spadlen; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
static long double |
| 667 |
ldpow10(int exp) |
| 668 |
{ |
| 669 |
long double result = 1; |
| 670 |
|
| 671 |
while (exp) { |
| 672 |
result *= 10; |
| 673 |
exp--; |
| 674 |
} |
| 675 |
|
| 676 |
return result; |
| 677 |
} |
| 678 |
|
| 679 |
static long |
| 680 |
lroundl(long double value) |
| 681 |
{ |
| 682 |
long intpart = value; |
| 683 |
|
| 684 |
value -= intpart; |
| 685 |
if (value >= 0.5) |
| 686 |
intpart++; |
| 687 |
|
| 688 |
return intpart; |
| 689 |
} |
| 690 |
|
| 691 |
static void |
| 692 |
fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue, |
| 693 |
int min, int max, int flags) |
| 694 |
{ |
| 695 |
char iconvert[20], fconvert[20]; |
| 696 |
int signvalue = 0, iplace = 0, fplace = 0; |
| 697 |
int padlen = 0; |
| 698 |
int zpadlen = 0, caps = 0; |
| 699 |
long intpart, fracpart; |
| 700 |
long double ufvalue; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
if (max < 0) |
| 707 |
max = 6; |
| 708 |
|
| 709 |
ufvalue = abs_val(fvalue); |
| 710 |
|
| 711 |
if (fvalue < 0) |
| 712 |
signvalue = '-'; |
| 713 |
else if (flags & DP_F_PLUS) |
| 714 |
signvalue = '+'; |
| 715 |
else if (flags & DP_F_SPACE) |
| 716 |
signvalue = ' '; |
| 717 |
|
| 718 |
intpart = ufvalue; |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
if (max > 9) |
| 725 |
max = 9; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
fracpart = lroundl((ldpow10 (max)) * (ufvalue - intpart)); |
| 731 |
|
| 732 |
if (fracpart >= ldpow10 (max)) { |
| 733 |
intpart++; |
| 734 |
fracpart -= ldpow10 (max); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
do { |
| 739 |
iconvert[iplace++] = |
| 740 |
(caps ? "0123456789ABCDEF" : "0123456789abcdef") |
| 741 |
[intpart % 10]; |
| 742 |
intpart = (intpart / 10); |
| 743 |
} while(intpart && (iplace < 20)); |
| 744 |
if (iplace == 20) |
| 745 |
iplace--; |
| 746 |
iconvert[iplace] = 0; |
| 747 |
|
| 748 |
|
| 749 |
do { |
| 750 |
fconvert[fplace++] = |
| 751 |
(caps ? "0123456789ABCDEF" : "0123456789abcdef") |
| 752 |
[fracpart % 10]; |
| 753 |
fracpart = (fracpart / 10); |
| 754 |
} while(fracpart && (fplace < 20)); |
| 755 |
if (fplace == 20) |
| 756 |
fplace--; |
| 757 |
fconvert[fplace] = 0; |
| 758 |
|
| 759 |
|
| 760 |
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 761 |
zpadlen = max - fplace; |
| 762 |
if (zpadlen < 0) |
| 763 |
zpadlen = 0; |
| 764 |
if (padlen < 0) |
| 765 |
padlen = 0; |
| 766 |
if (flags & DP_F_MINUS) |
| 767 |
padlen = -padlen; |
| 768 |
|
| 769 |
if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 770 |
if (signvalue) { |
| 771 |
dopr_outch(buffer, currlen, maxlen, signvalue); |
| 772 |
--padlen; |
| 773 |
signvalue = 0; |
| 774 |
} |
| 775 |
while (padlen > 0) { |
| 776 |
dopr_outch(buffer, currlen, maxlen, '0'); |
| 777 |
--padlen; |
| 778 |
} |
| 779 |
} |
| 780 |
while (padlen > 0) { |
| 781 |
dopr_outch(buffer, currlen, maxlen, ' '); |
| 782 |
--padlen; |
| 783 |
} |
| 784 |
if (signvalue) |
| 785 |
dopr_outch(buffer, currlen, maxlen, signvalue); |
| 786 |
|
| 787 |
while (iplace > 0) |
| 788 |
dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
dopr_outch(buffer, currlen, maxlen, '.'); |
| 795 |
|
| 796 |
while (fplace > 0) |
| 797 |
dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]); |
| 798 |
|
| 799 |
while (zpadlen > 0) { |
| 800 |
dopr_outch(buffer, currlen, maxlen, '0'); |
| 801 |
--zpadlen; |
| 802 |
} |
| 803 |
|
| 804 |
while (padlen < 0) { |
| 805 |
dopr_outch(buffer, currlen, maxlen, ' '); |
| 806 |
++padlen; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
static void |
| 811 |
dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) |
| 812 |
{ |
| 813 |
if (*currlen < maxlen) |
| 814 |
buffer[(*currlen)++] = c; |
| 815 |
} |
| 816 |
#endif |
| 817 |
|
| 818 |
#ifndef HAVE_VSNPRINTF |
| 819 |
int |
| 820 |
vsnprintf(char *str, size_t count, const char *fmt, va_list args) |
| 821 |
{ |
| 822 |
str[0] = 0; |
| 823 |
dopr(str, count, fmt, args); |
| 824 |
|
| 825 |
return(strlen(str)); |
| 826 |
} |
| 827 |
#endif |
| 828 |
|
| 829 |
#ifndef HAVE_SNPRINTF |
| 830 |
int |
| 831 |
snprintf(char *str,size_t count,const char *fmt,...) |
| 832 |
{ |
| 833 |
va_list ap; |
| 834 |
|
| 835 |
va_start(ap, fmt); |
| 836 |
(void) vsnprintf(str, count, fmt, ap); |
| 837 |
va_end(ap); |
| 838 |
|
| 839 |
return(strlen(str)); |
| 840 |
} |
| 841 |
|
| 842 |
#endif |