]> andersk Git - gssapi-openssh.git/blame - openssh/openbsd-compat/bsd-snprintf.c
Import of OpenSSH 4.6p1
[gssapi-openssh.git] / openssh / openbsd-compat / bsd-snprintf.c
CommitLineData
6a9b3198 1/*
2 * Copyright Patrick Powell 1995
3 * This code is based on code written by Patrick Powell (papowell@astart.com)
4 * It may be used for any purpose as long as this notice remains intact
5 * on all source code distributions
6 */
7
3c0ef626 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 <blong@fiction.net> 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 <blong@fiction.net> 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 <roessler@guug.de> 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 <me@cs.hmc.edu> 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 *
2c06c99b 48 * Andrew Tridgell (tridge@samba.org) Oct 1998
49 * fixed handling of %.0f
50 * added test for HAVE_LONG_DOUBLE
3c0ef626 51 *
2c06c99b 52 * tridge@samba.org, idra@samba.org, 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 <ollie.oldham@metro-optix.com> 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 (mbp@samba.org) 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 (mbp@samba.org) 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.
799ae497 88 *
89 * Damien Miller (djm@mindrot.org) Jan 2007
90 * Fix integer overflows in return value.
91 * Make formatting quite a bit faster by inlining dopr_outch()
92 *
3c0ef626 93 **************************************************************/
94
95#include "includes.h"
96
3c0ef626 97#if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
98# undef HAVE_SNPRINTF
99# undef HAVE_VSNPRINTF
100#endif
101
2c06c99b 102#ifndef VA_COPY
103# ifdef HAVE_VA_COPY
104# define VA_COPY(dest, src) va_copy(dest, src)
105# else
106# ifdef HAVE___VA_COPY
107# define VA_COPY(dest, src) __va_copy(dest, src)
108# else
109# define VA_COPY(dest, src) (dest) = (src)
110# endif
111# endif
112#endif
3c0ef626 113
2c06c99b 114#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
3c0ef626 115
9108f8d9 116#include <ctype.h>
117#include <stdarg.h>
118#include <stdlib.h>
119#include <string.h>
799ae497 120#include <limits.h>
121#include <errno.h>
9108f8d9 122
2c06c99b 123#ifdef HAVE_LONG_DOUBLE
124# define LDOUBLE long double
125#else
126# define LDOUBLE double
127#endif
3c0ef626 128
2c06c99b 129#ifdef HAVE_LONG_LONG
130# define LLONG long long
131#else
132# define LLONG long
133#endif
3c0ef626 134
135/*
136 * dopr(): poor man's version of doprintf
137 */
138
139/* format read states */
140#define DP_S_DEFAULT 0
141#define DP_S_FLAGS 1
142#define DP_S_MIN 2
143#define DP_S_DOT 3
144#define DP_S_MAX 4
145#define DP_S_MOD 5
146#define DP_S_CONV 6
147#define DP_S_DONE 7
148
149/* format flags - Bits */
150#define DP_F_MINUS (1 << 0)
151#define DP_F_PLUS (1 << 1)
152#define DP_F_SPACE (1 << 2)
153#define DP_F_NUM (1 << 3)
154#define DP_F_ZERO (1 << 4)
155#define DP_F_UP (1 << 5)
156#define DP_F_UNSIGNED (1 << 6)
157
158/* Conversion Flags */
2c06c99b 159#define DP_C_SHORT 1
160#define DP_C_LONG 2
161#define DP_C_LDOUBLE 3
162#define DP_C_LLONG 4
163
164#define char_to_int(p) ((p)- '0')
165#ifndef MAX
166# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
167#endif
3c0ef626 168
799ae497 169#define DOPR_OUTCH(buf, pos, buflen, thechar) \
170 do { \
171 if (pos + 1 >= INT_MAX) { \
172 errno = ERANGE; \
173 return -1; \
174 } \
175 if (pos < buflen) \
176 buf[pos] = thechar; \
177 (pos)++; \
178 } while (0)
179
180static int dopr(char *buffer, size_t maxlen, const char *format,
181 va_list args_in);
182static int fmtstr(char *buffer, size_t *currlen, size_t maxlen,
183 char *value, int flags, int min, int max);
184static int fmtint(char *buffer, size_t *currlen, size_t maxlen,
185 LLONG value, int base, int min, int max, int flags);
186static int fmtfp(char *buffer, size_t *currlen, size_t maxlen,
187 LDOUBLE fvalue, int min, int max, int flags);
188
189static int
190dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
3c0ef626 191{
2c06c99b 192 char ch;
193 LLONG value;
194 LDOUBLE fvalue;
195 char *strvalue;
196 int min;
197 int max;
198 int state;
199 int flags;
200 int cflags;
201 size_t currlen;
202 va_list args;
203
204 VA_COPY(args, args_in);
205
206 state = DP_S_DEFAULT;
207 currlen = flags = cflags = min = 0;
208 max = -1;
3c0ef626 209 ch = *format++;
2c06c99b 210
3c0ef626 211 while (state != DP_S_DONE) {
2c06c99b 212 if (ch == '\0')
3c0ef626 213 state = DP_S_DONE;
214
215 switch(state) {
0fff78ff 216 case DP_S_DEFAULT:
217 if (ch == '%')
218 state = DP_S_FLAGS;
799ae497 219 else
220 DOPR_OUTCH(buffer, currlen, maxlen, ch);
0fff78ff 221 ch = *format++;
222 break;
223 case DP_S_FLAGS:
224 switch (ch) {
225 case '-':
226 flags |= DP_F_MINUS;
3c0ef626 227 ch = *format++;
228 break;
0fff78ff 229 case '+':
230 flags |= DP_F_PLUS;
231 ch = *format++;
3c0ef626 232 break;
0fff78ff 233 case ' ':
234 flags |= DP_F_SPACE;
235 ch = *format++;
3c0ef626 236 break;
0fff78ff 237 case '#':
238 flags |= DP_F_NUM;
239 ch = *format++;
3c0ef626 240 break;
0fff78ff 241 case '0':
242 flags |= DP_F_ZERO;
243 ch = *format++;
3c0ef626 244 break;
0fff78ff 245 default:
246 state = DP_S_MIN;
3c0ef626 247 break;
0fff78ff 248 }
249 break;
250 case DP_S_MIN:
251 if (isdigit((unsigned char)ch)) {
2c06c99b 252 min = 10*min + char_to_int (ch);
0fff78ff 253 ch = *format++;
254 } else if (ch == '*') {
255 min = va_arg (args, int);
256 ch = *format++;
257 state = DP_S_DOT;
2c06c99b 258 } else {
0fff78ff 259 state = DP_S_DOT;
2c06c99b 260 }
0fff78ff 261 break;
262 case DP_S_DOT:
263 if (ch == '.') {
264 state = DP_S_MAX;
265 ch = *format++;
2c06c99b 266 } else {
0fff78ff 267 state = DP_S_MOD;
2c06c99b 268 }
0fff78ff 269 break;
270 case DP_S_MAX:
271 if (isdigit((unsigned char)ch)) {
272 if (max < 0)
273 max = 0;
2c06c99b 274 max = 10*max + char_to_int (ch);
0fff78ff 275 ch = *format++;
276 } else if (ch == '*') {
277 max = va_arg (args, int);
278 ch = *format++;
279 state = DP_S_MOD;
2c06c99b 280 } else {
0fff78ff 281 state = DP_S_MOD;
2c06c99b 282 }
0fff78ff 283 break;
284 case DP_S_MOD:
285 switch (ch) {
286 case 'h':
287 cflags = DP_C_SHORT;
288 ch = *format++;
289 break;
290 case 'l':
291 cflags = DP_C_LONG;
292 ch = *format++;
2c06c99b 293 if (ch == 'l') { /* It's a long long */
294 cflags = DP_C_LLONG;
0fff78ff 295 ch = *format++;
3c0ef626 296 }
0fff78ff 297 break;
0fff78ff 298 case 'L':
299 cflags = DP_C_LDOUBLE;
3c0ef626 300 ch = *format++;
3c0ef626 301 break;
0fff78ff 302 default:
303 break;
304 }
305 state = DP_S_CONV;
306 break;
307 case DP_S_CONV:
308 switch (ch) {
309 case 'd':
310 case 'i':
311 if (cflags == DP_C_SHORT)
2c06c99b 312 value = va_arg (args, int);
0fff78ff 313 else if (cflags == DP_C_LONG)
2c06c99b 314 value = va_arg (args, long int);
315 else if (cflags == DP_C_LLONG)
316 value = va_arg (args, LLONG);
0fff78ff 317 else
318 value = va_arg (args, int);
799ae497 319 if (fmtint(buffer, &currlen, maxlen,
320 value, 10, min, max, flags) == -1)
321 return -1;
0fff78ff 322 break;
323 case 'o':
324 flags |= DP_F_UNSIGNED;
325 if (cflags == DP_C_SHORT)
2c06c99b 326 value = va_arg (args, unsigned int);
0fff78ff 327 else if (cflags == DP_C_LONG)
2c06c99b 328 value = (long)va_arg (args, unsigned long int);
329 else if (cflags == DP_C_LLONG)
330 value = (long)va_arg (args, unsigned LLONG);
0fff78ff 331 else
2c06c99b 332 value = (long)va_arg (args, unsigned int);
799ae497 333 if (fmtint(buffer, &currlen, maxlen, value,
334 8, min, max, flags) == -1)
335 return -1;
3c0ef626 336 break;
0fff78ff 337 case 'u':
338 flags |= DP_F_UNSIGNED;
339 if (cflags == DP_C_SHORT)
2c06c99b 340 value = va_arg (args, unsigned int);
0fff78ff 341 else if (cflags == DP_C_LONG)
2c06c99b 342 value = (long)va_arg (args, unsigned long int);
343 else if (cflags == DP_C_LLONG)
344 value = (LLONG)va_arg (args, unsigned LLONG);
0fff78ff 345 else
2c06c99b 346 value = (long)va_arg (args, unsigned int);
799ae497 347 if (fmtint(buffer, &currlen, maxlen, value,
348 10, min, max, flags) == -1)
349 return -1;
0fff78ff 350 break;
351 case 'X':
352 flags |= DP_F_UP;
353 case 'x':
354 flags |= DP_F_UNSIGNED;
355 if (cflags == DP_C_SHORT)
2c06c99b 356 value = va_arg (args, unsigned int);
0fff78ff 357 else if (cflags == DP_C_LONG)
2c06c99b 358 value = (long)va_arg (args, unsigned long int);
359 else if (cflags == DP_C_LLONG)
360 value = (LLONG)va_arg (args, unsigned LLONG);
0fff78ff 361 else
2c06c99b 362 value = (long)va_arg (args, unsigned int);
799ae497 363 if (fmtint(buffer, &currlen, maxlen, value,
364 16, min, max, flags) == -1)
365 return -1;
0fff78ff 366 break;
367 case 'f':
368 if (cflags == DP_C_LDOUBLE)
2c06c99b 369 fvalue = va_arg (args, LDOUBLE);
0fff78ff 370 else
2c06c99b 371 fvalue = va_arg (args, double);
799ae497 372 if (fmtfp(buffer, &currlen, maxlen, fvalue,
373 min, max, flags) == -1)
374 return -1;
0fff78ff 375 break;
376 case 'E':
377 flags |= DP_F_UP;
378 case 'e':
379 if (cflags == DP_C_LDOUBLE)
2c06c99b 380 fvalue = va_arg (args, LDOUBLE);
0fff78ff 381 else
2c06c99b 382 fvalue = va_arg (args, double);
799ae497 383 if (fmtfp(buffer, &currlen, maxlen, fvalue,
384 min, max, flags) == -1)
385 return -1;
0fff78ff 386 break;
387 case 'G':
388 flags |= DP_F_UP;
389 case 'g':
390 if (cflags == DP_C_LDOUBLE)
2c06c99b 391 fvalue = va_arg (args, LDOUBLE);
0fff78ff 392 else
2c06c99b 393 fvalue = va_arg (args, double);
799ae497 394 if (fmtfp(buffer, &currlen, maxlen, fvalue,
395 min, max, flags) == -1)
396 return -1;
0fff78ff 397 break;
398 case 'c':
799ae497 399 DOPR_OUTCH(buffer, currlen, maxlen,
400 va_arg (args, int));
0fff78ff 401 break;
402 case 's':
2c06c99b 403 strvalue = va_arg (args, char *);
404 if (!strvalue) strvalue = "(NULL)";
405 if (max == -1) {
406 max = strlen(strvalue);
407 }
408 if (min > 0 && max >= 0 && min > max) max = min;
799ae497 409 if (fmtstr(buffer, &currlen, maxlen,
410 strvalue, flags, min, max) == -1)
411 return -1;
0fff78ff 412 break;
413 case 'p':
2c06c99b 414 strvalue = va_arg (args, void *);
799ae497 415 if (fmtint(buffer, &currlen, maxlen,
416 (long) strvalue, 16, min, max, flags) == -1)
417 return -1;
0fff78ff 418 break;
419 case 'n':
420 if (cflags == DP_C_SHORT) {
421 short int *num;
2c06c99b 422 num = va_arg (args, short int *);
0fff78ff 423 *num = currlen;
424 } else if (cflags == DP_C_LONG) {
425 long int *num;
2c06c99b 426 num = va_arg (args, long int *);
427 *num = (long int)currlen;
428 } else if (cflags == DP_C_LLONG) {
429 LLONG *num;
430 num = va_arg (args, LLONG *);
431 *num = (LLONG)currlen;
0fff78ff 432 } else {
433 int *num;
2c06c99b 434 num = va_arg (args, int *);
0fff78ff 435 *num = currlen;
436 }
437 break;
438 case '%':
799ae497 439 DOPR_OUTCH(buffer, currlen, maxlen, ch);
0fff78ff 440 break;
2c06c99b 441 case 'w':
442 /* not supported yet, treat as next char */
0fff78ff 443 ch = *format++;
444 break;
2c06c99b 445 default:
446 /* Unknown, skip */
447 break;
0fff78ff 448 }
449 ch = *format++;
450 state = DP_S_DEFAULT;
451 flags = cflags = min = 0;
452 max = -1;
453 break;
454 case DP_S_DONE:
455 break;
2c06c99b 456 default:
457 /* hmm? */
0fff78ff 458 break; /* some picky compilers need this */
3c0ef626 459 }
460 }
2c06c99b 461 if (maxlen != 0) {
462 if (currlen < maxlen - 1)
463 buffer[currlen] = '\0';
464 else if (maxlen > 0)
465 buffer[maxlen - 1] = '\0';
466 }
467
799ae497 468 return currlen < INT_MAX ? (int)currlen : -1;
3c0ef626 469}
470
799ae497 471static int
472fmtstr(char *buffer, size_t *currlen, size_t maxlen,
473 char *value, int flags, int min, int max)
3c0ef626 474{
2c06c99b 475 int padlen, strln; /* amount to pad */
476 int cnt = 0;
477
478#ifdef DEBUG_SNPRINTF
479 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
480#endif
481 if (value == 0) {
3c0ef626 482 value = "<NULL>";
2c06c99b 483 }
3c0ef626 484
996d5e62 485 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
3c0ef626 486 padlen = min - strln;
487 if (padlen < 0)
488 padlen = 0;
489 if (flags & DP_F_MINUS)
490 padlen = -padlen; /* Left Justify */
2c06c99b 491
3c0ef626 492 while ((padlen > 0) && (cnt < max)) {
799ae497 493 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 494 --padlen;
495 ++cnt;
496 }
497 while (*value && (cnt < max)) {
799ae497 498 DOPR_OUTCH(buffer, *currlen, maxlen, *value);
499 *value++;
3c0ef626 500 ++cnt;
501 }
502 while ((padlen < 0) && (cnt < max)) {
799ae497 503 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 504 ++padlen;
505 ++cnt;
506 }
799ae497 507 return 0;
3c0ef626 508}
509
510/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
511
799ae497 512static int
513fmtint(char *buffer, size_t *currlen, size_t maxlen,
9108f8d9 514 LLONG value, int base, int min, int max, int flags)
3c0ef626 515{
2c06c99b 516 int signvalue = 0;
9108f8d9 517 unsigned LLONG uvalue;
3c0ef626 518 char convert[20];
2c06c99b 519 int place = 0;
3c0ef626 520 int spadlen = 0; /* amount to space pad */
521 int zpadlen = 0; /* amount to zero pad */
2c06c99b 522 int caps = 0;
523
3c0ef626 524 if (max < 0)
525 max = 0;
2c06c99b 526
3c0ef626 527 uvalue = value;
2c06c99b 528
529 if(!(flags & DP_F_UNSIGNED)) {
530 if( value < 0 ) {
3c0ef626 531 signvalue = '-';
532 uvalue = -value;
2c06c99b 533 } else {
534 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
535 signvalue = '+';
536 else if (flags & DP_F_SPACE)
537 signvalue = ' ';
538 }
3c0ef626 539 }
540
2c06c99b 541 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
542
3c0ef626 543 do {
544 convert[place++] =
2c06c99b 545 (caps? "0123456789ABCDEF":"0123456789abcdef")
546 [uvalue % (unsigned)base ];
3c0ef626 547 uvalue = (uvalue / (unsigned)base );
2c06c99b 548 } while(uvalue && (place < 20));
549 if (place == 20) place--;
3c0ef626 550 convert[place] = 0;
551
552 zpadlen = max - place;
553 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
2c06c99b 554 if (zpadlen < 0) zpadlen = 0;
555 if (spadlen < 0) spadlen = 0;
3c0ef626 556 if (flags & DP_F_ZERO) {
557 zpadlen = MAX(zpadlen, spadlen);
558 spadlen = 0;
559 }
560 if (flags & DP_F_MINUS)
561 spadlen = -spadlen; /* Left Justifty */
562
2c06c99b 563#ifdef DEBUG_SNPRINTF
564 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
565 zpadlen, spadlen, min, max, place);
566#endif
567
3c0ef626 568 /* Spaces */
569 while (spadlen > 0) {
799ae497 570 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 571 --spadlen;
572 }
573
574 /* Sign */
575 if (signvalue)
799ae497 576 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
3c0ef626 577
578 /* Zeros */
579 if (zpadlen > 0) {
580 while (zpadlen > 0) {
799ae497 581 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
3c0ef626 582 --zpadlen;
583 }
584 }
585
586 /* Digits */
799ae497 587 while (place > 0) {
588 --place;
589 DOPR_OUTCH(buffer, *currlen, maxlen, convert[place]);
590 }
3c0ef626 591
592 /* Left Justified spaces */
593 while (spadlen < 0) {
799ae497 594 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 595 ++spadlen;
596 }
799ae497 597 return 0;
3c0ef626 598}
599
2c06c99b 600static LDOUBLE abs_val(LDOUBLE value)
3c0ef626 601{
2c06c99b 602 LDOUBLE result = value;
603
604 if (value < 0)
605 result = -value;
606
607 return result;
608}
3c0ef626 609
799ae497 610static LDOUBLE POW10(int val)
2c06c99b 611{
612 LDOUBLE result = 1;
613
799ae497 614 while (val) {
3c0ef626 615 result *= 10;
799ae497 616 val--;
3c0ef626 617 }
618
619 return result;
620}
621
2c06c99b 622static LLONG ROUND(LDOUBLE value)
3c0ef626 623{
2c06c99b 624 LLONG intpart;
3c0ef626 625
2c06c99b 626 intpart = (LLONG)value;
627 value = value - intpart;
628 if (value >= 0.5) intpart++;
629
3c0ef626 630 return intpart;
631}
632
2c06c99b 633/* a replacement for modf that doesn't need the math library. Should
634 be portable, but slow */
635static double my_modf(double x0, double *iptr)
3c0ef626 636{
2c06c99b 637 int i;
638 long l;
639 double x = x0;
640 double f = 1.0;
641
642 for (i=0;i<100;i++) {
643 l = (long)x;
644 if (l <= (x+1) && l >= (x-1)) break;
645 x *= 0.1;
646 f *= 10.0;
647 }
648
649 if (i == 100) {
799ae497 650 /*
651 * yikes! the number is beyond what we can handle.
652 * What do we do?
653 */
2c06c99b 654 (*iptr) = 0;
655 return 0;
656 }
657
658 if (i != 0) {
659 double i2;
660 double ret;
661
662 ret = my_modf(x0-l*f, &i2);
663 (*iptr) = l*f + i2;
664 return ret;
665 }
666
667 (*iptr) = l;
668 return x - (*iptr);
669}
670
671
799ae497 672static int
673fmtfp (char *buffer, size_t *currlen, size_t maxlen,
674 LDOUBLE fvalue, int min, int max, int flags)
2c06c99b 675{
676 int signvalue = 0;
677 double ufvalue;
678 char iconvert[311];
679 char fconvert[311];
680 int iplace = 0;
681 int fplace = 0;
3c0ef626 682 int padlen = 0; /* amount to pad */
2c06c99b 683 int zpadlen = 0;
684 int caps = 0;
685 int idx;
686 double intpart;
687 double fracpart;
688 double temp;
3c0ef626 689
690 /*
691 * AIX manpage says the default is 0, but Solaris says the default
692 * is 6, and sprintf on AIX defaults to 6
693 */
694 if (max < 0)
695 max = 6;
696
2c06c99b 697 ufvalue = abs_val (fvalue);
3c0ef626 698
2c06c99b 699 if (fvalue < 0) {
3c0ef626 700 signvalue = '-';
2c06c99b 701 } else {
702 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
703 signvalue = '+';
704 } else {
705 if (flags & DP_F_SPACE)
706 signvalue = ' ';
707 }
708 }
3c0ef626 709
2c06c99b 710#if 0
711 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
712#endif
713
714#if 0
715 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
716#endif
3c0ef626 717
718 /*
2c06c99b 719 * Sorry, we only support 16 digits past the decimal because of our
3c0ef626 720 * conversion method
721 */
2c06c99b 722 if (max > 16)
723 max = 16;
3c0ef626 724
725 /* We "cheat" by converting the fractional part to integer by
726 * multiplying by a factor of 10
727 */
3c0ef626 728
2c06c99b 729 temp = ufvalue;
730 my_modf(temp, &intpart);
731
732 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
733
734 if (fracpart >= POW10(max)) {
3c0ef626 735 intpart++;
2c06c99b 736 fracpart -= POW10(max);
3c0ef626 737 }
738
739 /* Convert integer part */
740 do {
2c06c99b 741 temp = intpart*0.1;
742 my_modf(temp, &intpart);
743 idx = (int) ((temp -intpart +0.05)* 10.0);
744 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
745 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
3c0ef626 746 iconvert[iplace++] =
2c06c99b 747 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
748 } while (intpart && (iplace < 311));
749 if (iplace == 311) iplace--;
3c0ef626 750 iconvert[iplace] = 0;
751
752 /* Convert fractional part */
2c06c99b 753 if (fracpart)
754 {
755 do {
756 temp = fracpart*0.1;
757 my_modf(temp, &fracpart);
758 idx = (int) ((temp -fracpart +0.05)* 10.0);
759 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
760 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
761 fconvert[fplace++] =
762 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
763 } while(fracpart && (fplace < 311));
764 if (fplace == 311) fplace--;
765 }
3c0ef626 766 fconvert[fplace] = 0;
2c06c99b 767
3c0ef626 768 /* -1 for decimal point, another -1 if we are printing a sign */
769 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
770 zpadlen = max - fplace;
2c06c99b 771 if (zpadlen < 0) zpadlen = 0;
3c0ef626 772 if (padlen < 0)
773 padlen = 0;
774 if (flags & DP_F_MINUS)
775 padlen = -padlen; /* Left Justifty */
2c06c99b 776
3c0ef626 777 if ((flags & DP_F_ZERO) && (padlen > 0)) {
778 if (signvalue) {
799ae497 779 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
3c0ef626 780 --padlen;
781 signvalue = 0;
782 }
783 while (padlen > 0) {
799ae497 784 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
3c0ef626 785 --padlen;
786 }
787 }
788 while (padlen > 0) {
799ae497 789 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 790 --padlen;
791 }
792 if (signvalue)
799ae497 793 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
2c06c99b 794
799ae497 795 while (iplace > 0) {
796 --iplace;
797 DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[iplace]);
798 }
2c06c99b 799
800#ifdef DEBUG_SNPRINTF
801 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
802#endif
3c0ef626 803
804 /*
2c06c99b 805 * Decimal point. This should probably use locale to find the correct
806 * char to print out.
3c0ef626 807 */
2c06c99b 808 if (max > 0) {
799ae497 809 DOPR_OUTCH(buffer, *currlen, maxlen, '.');
2c06c99b 810
811 while (zpadlen > 0) {
799ae497 812 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
2c06c99b 813 --zpadlen;
814 }
3c0ef626 815
799ae497 816 while (fplace > 0) {
817 --fplace;
818 DOPR_OUTCH(buffer, *currlen, maxlen, fconvert[fplace]);
819 }
3c0ef626 820 }
821
822 while (padlen < 0) {
799ae497 823 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
3c0ef626 824 ++padlen;
825 }
799ae497 826 return 0;
3c0ef626 827}
828#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
829
2c06c99b 830#if !defined(HAVE_VSNPRINTF)
799ae497 831int
832vsnprintf (char *str, size_t count, const char *fmt, va_list args)
3c0ef626 833{
2c06c99b 834 return dopr(str, count, fmt, args);
3c0ef626 835}
2c06c99b 836#endif
3c0ef626 837
2c06c99b 838#if !defined(HAVE_SNPRINTF)
799ae497 839int
840snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
3c0ef626 841{
2c06c99b 842 size_t ret;
3c0ef626 843 va_list ap;
844
845 va_start(ap, fmt);
2c06c99b 846 ret = vsnprintf(str, count, fmt, ap);
3c0ef626 847 va_end(ap);
2c06c99b 848 return ret;
3c0ef626 849}
2c06c99b 850#endif
This page took 0.385014 seconds and 5 git commands to generate.