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