File: vfprintf.c

package info (click to toggle)
avr-libc 1%3A1.2.3-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,816 kB
  • ctags: 14,018
  • sloc: ansic: 17,998; asm: 5,024; sh: 2,778; makefile: 712; pascal: 441
file content (738 lines) | stat: -rw-r--r-- 18,640 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/* Copyright (c) 2002, Alexander Popov (sasho@vip.bg)
   Copyright (c) 2002,2004 Joerg Wunsch
   Copyright (c) 2005, Helmut Wallner
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

/* From: Id: printf_p_new.c,v 1.1.1.9 2002/10/15 20:10:28 joerg_wunsch Exp */
/* $Id: vfprintf.c,v 1.11.2.1 2005/01/18 16:28:05 joerg_wunsch Exp $ */

#include <avr/pgmspace.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stdio_private.h"

/*
 * This file can be compiled into more than one flavour.  The default
 * is to offer the usual modifiers and integer formatting support
 * (level 2).  Level 1 maintains a minimal version that just offers
 * integer formatting, but no modifier support whatsoever.  Level 3 is
 * intented for floating point support.
 */

#ifndef PRINTF_LEVEL
#  define PRINTF_LEVEL PRINTF_STD
#endif

#if PRINTF_LEVEL == PRINTF_MIN || PRINTF_LEVEL == PRINTF_STD \
|| PRINTF_LEVEL == PRINTF_FLT
/* OK */
#else
#  error "Not a known printf level."
#endif

#if PRINTF_LEVEL >= PRINTF_FLT
#include <math.h>
#endif

#if PRINTF_LEVEL >= PRINTF_MIN
#define FLHASPERCENT	0x01	/* first % found */
#define FLNEGATIVE	0x02	/* arg is negative int */
#define FLLONG		0x04	/* arg is long int */
#endif

#if PRINTF_LEVEL >= PRINTF_STD
#define FLPREC		0x08	/* .prec given */
#define FLSIGNPLUS	0x10	/* always print '+' sign */
#define FLSIGN		0x20	/* always print sign */
#define FLALT		0x40	/* # alternate specification given */
#define FLLPAD		0x80	/* left-align result */
#define FLZFILL		0x100	/* zero-fill up to width */
#endif

#if PRINTF_LEVEL >= PRINTF_FLT
#define FLFLOAT		0x200	/* floating-point conversion */
#define FLFCVT		0x400	/* do style `f' if set, style `e' if clear */
#define FLGCVT		0x800	/* %g format, decide about %e or %f */

/* With 4-byte floats, we can get up to 39 digits. */
#define FLTBUFLEN 40

static int8_t	flcvt(char *buf, double arg, int8_t ndigits, uint16_t *flags);
#endif /* PRINTF_LEVEL >= PRINTF_FLT */

/* Integer conversion buffer length. */
#define BUFLEN 12

int
vfprintf(FILE *stream, const char *fmt, va_list ap) {
	union {
		char c;
		uint8_t u8;
		int8_t i8;
		long l;
		unsigned long ul;
		char *pc;
#if PRINTF_LEVEL >= PRINTF_FLT
		double d;
		div_t dv;
#endif
	} a;
	char	c;	/* holds a char from the format string */
	uint8_t	base;
	char	b[BUFLEN]; /*
			    * Contains character representation of the
			    * number and its prefix, written from back
			    * to front.
			    */
	char	*pb;	/* used as pointer to an element in b */
#if PRINTF_LEVEL > PRINTF_MIN
	int8_t	width, prec;
#endif
#if PRINTF_LEVEL >= PRINTF_FLT
	int8_t	decpt;
	char	*fb;	/* floating point buffer, malloc'ed */
#endif

#if PRINTF_LEVEL < PRINTF_STD
	uint8_t flags;
#else
	uint16_t flags;
#endif

	flags = 0;
	stream->len = 0;
#if PRINTF_LEVEL >= PRINTF_FLT
	fb = 0;
#endif

	if ((stream->flags & __SWR) == 0)
		return EOF;

	/*
	 * Do not use fmt++ in the next line.  pgm_read_byte() is a
	 * macro, so it could evaluate its argument more than once.
	 */
	while ((c = ((stream->flags & __SPGM)? pgm_read_byte(fmt): *fmt))) {

		fmt++;

		if (flags & FLHASPERCENT) {
			if (c >= '0' && c <= '9') {
#if PRINTF_LEVEL > PRINTF_MIN
				if (c == '0' && prec == 0 &&
				    (flags & FLPREC) == 0) {
					flags |= FLZFILL;
				} else {
					/* digit */
					prec *= 10;
					prec += c - '0';
					if (prec < 0)
						prec = SCHAR_MAX;
					if (!(flags & FLPREC))
						width = prec;
				}
#endif
#if PRINTF_LEVEL >= PRINTF_FLT
			} else if ((c >= 'e' && c <= 'g') ||
				   (c >= 'E' && c <= 'G')) {
				a.d = va_arg(ap, double);
				if (fb == 0) {
					if ((fb = malloc(FLTBUFLEN)) == 0)
						/*
						 * No buffer for floating point
						 * conversion, punt.
						 */
						goto clearflags;
				}
				if (!(flags & FLPREC))
					prec = 6;
				/* FLPREC is used for integer formats only */
				flags &= ~(FLALT | FLPREC);
				if (isnan(a.d)) {
					b[0] = b[2] = 'N';
					b[1] = 'A';
					pb = b + 3;
				  dofltexcpt:
					/*
					 * XXX: this makes assumptions about
					 * ASCII being used.  Should probably
					 * use ctype, but would cause more
					 * bloat.
					 */
					if (c >= 'e' && c <= 'g') {
						b[0] += 'a' - 'A';
						b[1] += 'a' - 'A';
						b[2] += 'a' - 'A';
					}
					goto nextitem;
				} else if (isinf(a.d)) {
					b[2] = 'I';
					b[1] = 'N';
					b[0] = 'F';
					pb = b + 3;
					if (a.d < 0.0)
						*pb++ = '-';
					goto dofltexcpt;
				}
				flags |= FLFLOAT;
				switch (c) {
				case 'e':
				case 'E':
					decpt = flcvt(fb, a.d, prec, &flags);
				  dostyle_e:
					width -= 4; /* E+00 */
					if (prec > 0)
						width--; /* decimal sign */
					a.i8 = strlen(pb = fb);
					goto dofloat;

				case 'f':
				case 'F':
					flags |= FLFCVT;
					decpt = flcvt(fb, a.d, prec, &flags);
				  dostyle_f:
					if (prec > 0)
						width--; /* decimal sign */
					pb = fb;
					if (decpt > 0)
						a.i8 = strlen(pb);
					else
						a.i8 = prec + 1;
					goto dofloat;
				case 'g':
				case 'G':
					flags |= FLGCVT;
					decpt = flcvt(fb, a.d,
						      prec == 0? 1: prec,
						      &flags);
					/*
					 * Remove trailing zeros from
					 * fractional result.
					 */
					if (flags & FLFCVT) {
						for (a.i8 = decpt + prec - 1;
						     a.i8 >= 0 && a.i8 >= decpt;
						     a.i8--)
							if (fb[a.i8] != '0')
								break;
						if (a.i8 < 0 || a.i8 < decpt)
							/*
							 * No fractional
							 * digits left,
							 * don't display
							 * `.' either.
							 */
							prec = 0;
						else
							prec = a.i8 - decpt + 1;
					} else {
						/* `e' style */
						for (a.i8 = prec;
						     a.i8 > 0;
						     a.i8--)
							if (fb[a.i8] != '0')
								break;
						if (a.i8 == 0)
							/*
							 * No fractional
							 * digits left,
							 * don't display
							 * `.' either.
							 */
							prec = 0;
						else
							prec = a.i8;
					}
					fb[a.i8 + 1] = '\0';
					/*
					 * Continue depending on the %e/%f
					 * decision flcvt() has done.
					 */
					if (flags & FLFCVT)
						goto dostyle_f;
					c -= 'G' - 'E';
					goto dostyle_e;
				}
#endif /* PRINTF_LEVEL >= PRINTF_FLT */
			} else {
#if PRINTF_LEVEL > PRINTF_MIN
				if ((flags & FLPREC) && prec == 0)
					prec = 1;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
				switch (c) {
				case '+':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLSIGNPLUS;
					/* FALLTHROUGH */
#endif /* PRINTF_LEVEL > PRINTF_MIN */
				case ' ':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLSIGN;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
					break;
				case '-':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLLPAD;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
					break;
				case '#':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLALT;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
					break;
				case '.':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLPREC;
					prec = 0;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
					break;
				case 'l':
					flags |= FLLONG;
					break;
#if PRINTF_LEVEL < PRINTF_FLT
				case 'e':
				case 'f':
				case 'g':
					a.c = '?';
#endif /* PRINTF_LEVEL < PRINTF_FLT */
#if PRINTF_LEVEL > PRINTF_MIN
					width--;
#endif /* PRINTF_LEVEL > PRINTF_MIN */
					c = 'c';
					goto nextitem;
				case 'c':
					/* char is promoted to int via va_arg */
					a.c = (char)va_arg(ap, int);
#if PRINTF_LEVEL > PRINTF_MIN
					width--;
#endif
					goto nextitem;
				case 's':
					a.pc = va_arg(ap, char *);
#if PRINTF_LEVEL > PRINTF_MIN
					if (flags & FLPREC)
						base = strnlen(a.pc, prec);
					else
						base = strlen(a.pc);
					width -= base;
#else
					base = strlen(a.pc);
#endif
					goto nextitem;
				case 'S':
					a.pc = va_arg(ap, char *);
#if PRINTF_LEVEL > PRINTF_MIN
					if (flags & FLPREC)
						base = strnlen_P(a.pc, prec);
					else
						base = strlen_P(a.pc);
					width -= base;
#else
					base = strlen_P(a.pc);
#endif
					goto nextitem;
				case 'd':
				case 'i':
					a.l = flags & FLLONG ?
						va_arg(ap, long):
						va_arg(ap, int);
					if (a.l < 0) {
						flags |= FLNEGATIVE;
						a.l = -a.l;
					}
#if PRINTF_LEVEL > PRINTF_MIN
					flags &= ~FLALT;
#endif
					goto processnum;
				case 'o': /* octal number */
					base = 8;
					goto getulong;
				case 'p':
#if PRINTF_LEVEL > PRINTF_MIN
					flags |= FLALT;
#endif
					c = 'x';
					/* FALLTHROUGH */
				case 'x':
				case 'X':
					base = 16;
					/* FALLTHROUGH */
				case 'u':
				  getulong:
					a.ul = flags & FLLONG?
						va_arg(ap, unsigned long):
						va_arg(ap, unsigned int);
#if PRINTF_LEVEL > PRINTF_MIN
					flags &= ~(FLSIGNPLUS | FLSIGN);
#endif
				  processnum:
					pb = b;
					do {
						*pb = a.ul % base;
						*pb = *pb > 9?
							*pb + c - 'X' + 'A' - 10:
							*pb + '0';
						*pb++;
						a.ul /= base;
					} while (a.ul);
#if PRINTF_LEVEL > PRINTF_MIN
					/* length of converted string */
					a.i8 = pb - b;
#endif
#if PRINTF_LEVEL >= PRINTF_FLT
				  dofloat:
#endif
#if PRINTF_LEVEL > PRINTF_MIN
					/* compute length of padding */
					if (flags & (FLNEGATIVE | FLSIGN |
						     FLSIGNPLUS))
						width--;
					if (flags & FLALT)
						width -= base == 16? 2: 1;
					if (flags & FLPREC) {
						if (prec <= (uint8_t)a.i8)
							/*
							 * No space left for
							 * 0-padding
							 */
							flags &= ~FLPREC;
						else
							prec -= (uint8_t)a.i8;
					}
					if (flags & FLPREC)
						width -= prec;
					else
						width -= a.i8;

					/* do left-side padding if necessary */
					if (!(flags & (FLLPAD | FLZFILL)))
						while (width-- > 0)
							putc(' ', stream);
#endif
					if (flags & FLNEGATIVE)
						putc('-', stream);
#if PRINTF_LEVEL > PRINTF_MIN
					else if (flags & FLSIGNPLUS)
						putc('+', stream);
					else if (flags & FLSIGN)
						putc(' ', stream);
					if (flags & FLALT) {
						putc('0', stream);
						if (base == 16)
							putc(c, stream);
					}
					if ((flags & FLZFILL))
						while (width-- > 0)
							putc('0', stream);
					/*
					 * If there was no space left for
					 * precision-caused 0-padding, the
					 * flag has already been cleared
					 * above.
					 */
					if (flags & FLPREC)
						while (prec-- > 0)
							putc('0', stream);
#endif
				  nextitem:
#if PRINTF_LEVEL > PRINTF_MIN
					/*
					 * This is for %s and %c only.
					 * For numeric arguments,
					 * we've already done the
					 * padding above, and width
					 * will be negative here.
					 */
					if (!(flags & FLLPAD))
						while (width-- > 0)
							putc(' ', stream);
#endif
					if (c == 'c')
						putc(a.c, stream);
					else if (c == 's')
						while (base--)
							putc(*a.pc++, stream);
					else if (c == 'S')
						while (base--) {
							putc(pgm_read_byte(a.pc), stream);
							a.pc++;
						}
#if PRINTF_LEVEL >= PRINTF_FLT
					else if (flags & FLFLOAT) {
						if (flags & FLFCVT) {
							if (decpt > 0) {
								while (decpt--)
									putc(*pb++,
									     stream);
								decpt = 0;
							} else
								putc('0',
								     stream);
						} else
							/* 'e' format */
							putc(*pb++, stream);
						if (prec > 0)
							putc('.', stream);
						if (flags & FLFCVT)
							while (decpt++ < 0 &&
							       prec-- > 0)
								putc('0',
								     stream);
						while ((a.c = *pb++) &&
						       prec-- > 0)
							putc(a.c, stream);
						if (!(flags & FLFCVT)) {
							/* 'e' format */
							putc(c, stream);
							decpt--;
							if (decpt < 0) {
								putc('-',
								     stream);
								decpt = -decpt;
							} else
								putc('+',
								     stream);
							a.dv = div(decpt, 10);
							putc(a.dv.quot + '0',
							     stream);
							putc(a.dv.rem + '0',
							     stream);
						}
					}
#endif /* PRINTF_LEVEL >= PRINTF_FLT */
					else
						while (pb != b)
							putc(*--pb, stream);
#if PRINTF_LEVEL > PRINTF_MIN
					if (flags & FLLPAD)
						while (width-- > 0)
							putc(' ', stream);
#endif
					goto clearflags;

				default:
					putc(c, stream);
				  clearflags:
					flags = 0;
					break;
				}
			}
		} else
			if (c == '%') {
				flags = FLHASPERCENT;
#if PRINTF_LEVEL > PRINTF_MIN
				prec = width = 0;
#endif
				base = 10;
			} else
				putc(c, stream);
	}

#if PRINTF_LEVEL >= PRINTF_FLT
	free(fb);
#endif
	return stream->len;
}

#if PRINTF_LEVEL >= PRINTF_FLT

/*
 * Floating point to string conversion.
 *
 * Featured after the Unix fcvt()/ecvt() functions.  The argument is
 * converted into a digit string.
 *
 * For style `f' conversion (*flags & FLFCVT is set), the position of
 * the decimal point is returned, where a negative position means the
 * decimal point is outside our buffer to the left.  The buffer will
 * then start with the first non-zero digit.  For numbers greater then
 * 1, trailing zeros will be added into the buffer though.  prec
 * determines the number of digits after the decimal point.  (Note
 * that the decimal point itself will not be added into the buffer.)
 *
 * For style `e' conversion (*flags & FLFCVT is clear), decpt is
 * effectively the exponent of our conversion minus 1, and the
 * conversion is always done so that the first digit in the conversion
 * buffer is considered to be the digit before the decimal point, with
 * prec digits following.
 *
 * For style `g' conversion (*flags & FLGCVT is set), after
 * determining the exponent of the conversion, it will be decided
 * whether an `e' style or an `f' style conversion needs to be done.
 * In the former case, *flags will have FLFCVT set upon return.
 *
 * For negative numbers, *flags will get FLNEGATIVE set upon return,
 * while the conversion itself will be handled as if a positive number
 * was given.
 */

#include <math.h>

static int8_t
flcvt(char *buf, double arg, int8_t prec, uint16_t *flags)
{
	int8_t decpt;
	double dnum, dnum2;
	char *p, *p1, *pend;

	/*
	 * Limit prec to what we can handle.  The -3 below is since
	 * style `e' will further increase prec by 1, and there needs
	 * to be space for one insignificant digit used for rounding,
	 * as well as for the trailing '\0'.
	 */
	if (prec < 0)
		prec = 0;
	if (prec > FLTBUFLEN - 3)
		prec = FLTBUFLEN - 3;
	decpt = 0;
	p = buf;
	pend = buf + FLTBUFLEN;
	/*
	 * First, make number positive, and split into integer and
	 * fractional part.
	 */
	if (arg < 0) {
		*flags |= FLNEGATIVE;
		arg = -arg;
	}
	arg = modf(arg, &dnum);
	/*
	 * Handle integer part.  Digits are written in the conversion
	 * buffer back to front, until the integer part becomes 0.
	 * Then, the conversion result is copied to the beginning of
	 * our buffer.
	 */
	if (dnum != 0) {
		for (p1 = pend; dnum != 0 && p1 > p; decpt++) {
			dnum2 = modf(dnum * 0.1, &dnum);
			*--p1 = (int)(dnum2 * 10.1) + '0';
		}
		while (p1 < pend)
			*p++ = *p1++;
	}
	/*
	 * If the number was less than 0, just shift it up into the
	 * range of [0.1 .. 1.0), and remember the number of digits
	 * we needed to shift.
	 */
	else if (arg > 0) {
		while ((dnum = arg * 10) < 1) {
			arg = dnum;
			decpt--;
		}
	}
	if (*flags & FLGCVT) {
		/*
		 * Now decide whether we're going to use style `e' or
		 * style `f' for the conversion.  "Style `e' is used
		 * if the exponent is less than -4 or greater than or
		 * equal to the precision."  Note that we can only
		 * estimate the exponent here since the rounding below
		 * could cause the exponent to further increase; in
		 * order to keep the code small, we simply accept this
		 * possible error which could lead to an `e' conversion
		 * being done where an `f' conversion would still had
		 * sufficed.
		 *
		 * The exponent is equal to decpt - 1 at this point.
		 */
		if (decpt >= -3 && decpt < prec + 1)
			*flags |= FLFCVT;
	}
	/*
	 * For an `e' style conversion, we increase prec so to account
	 * for the digit before the decimal point internally as well.
	 */
	if (!(*flags & FLFCVT))
		prec++;
	p1 = buf + prec;
	if (*flags & FLFCVT)
		p1 += decpt;
	/*
	 * For an `f' style conversion, if prec is too small to hold
	 * any significant digit, the conversion result will consist
	 * solely of 0 digits (which need to be printed by the caller
	 * based on the decpt we return).
	 */
	if (p1 < buf) {
		buf[0] = '\0';
		return decpt;
	}
	/*
	 * Now convert trailing digits from the fractional part, until
	 * we either got the desired precision (p1 points to the last
	 * desired character) or the end of our buffer.
	 */
	while (p <= p1 && p < pend) {
		arg *= 10;
		arg = modf(arg, &dnum);
		*p++ = (int)dnum + '0';
	}
	/*
	 * If we hit the end of the buffer, just terminate the string
	 * and return.  We might have lost precision in that case.
	 */
	if (p1 >= pend) {
		buf[FLTBUFLEN-1] = '\0';
		return decpt;
	}
	/*
	 * If we got here, round the conversion result, beginning from
	 * the last (insignificant) digit, possibly carrying the
	 * overflow to the previous digit(s).  Note that the
	 * insignificant digit will be blanked out below, so we can
	 * just gratitously add a `5' here.
	 */
	p = p1;
	*p1 += 5;
	while (*p1 > '9') {
		*p1 = '0';
		if (p1 > buf)
			++*--p1;
		else {
			*p1 = '1';
			decpt++;
			if (*flags & FLFCVT) {
				if (p > buf)
					*p = '0';
				p++;
			}
		}
	}
	*p = '\0';
	return decpt;
}

#endif /* PRINTF_LEVEL >= PRINTF_FLT */