home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / libio-4.6.26 / ldouble / iovfprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-27  |  21.6 KB  |  901 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*
  26.  * Copyright (c) 1990 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42.  
  43. #if defined(LIBC_SCCS) && !defined(lint)
  44. static char sccsid[] = "%W% (Berkeley) %G%";
  45. #endif /* LIBC_SCCS and not lint */
  46.  
  47. /*
  48.  * Actual printf innards.
  49.  *
  50.  * This code is large and complicated...
  51.  */
  52.  
  53. #include <sys/types.h>
  54. #include "libioP.h"
  55. #include <string.h>
  56. #ifdef __STDC__
  57. #include <stdarg.h>
  58. #else
  59. #include <varargs.h>
  60. #endif
  61.  
  62. /*
  63.  * Define FLOATING_POINT to get floating point.
  64.  */
  65. #ifndef    NO_FLOATING_POINT
  66. #define    FLOATING_POINT
  67. #endif
  68. static int _IO_helper_overflow (_IO_FILE *, int);
  69. static int helper_vfprintf (_IO_FILE*, char const *, _IO_va_list);
  70. /* end of configuration stuff */
  71.  
  72.  
  73. /*
  74.  * Helper "class" for `fprintf to unbuffered': creates a
  75.  * temporary buffer. */
  76.  
  77. struct helper_file
  78. {
  79.   struct _IO_FILE_plus _f;
  80.   _IO_FILE *_put_stream;
  81. };
  82.  
  83. static int
  84. _IO_helper_overflow (fp, c)
  85.      _IO_FILE *fp;
  86.      int c;
  87. {
  88.   _IO_FILE *target = ((struct helper_file*)fp)->_put_stream;
  89.   int used = fp->_IO_write_ptr - fp->_IO_write_base;
  90.   if (used)
  91.     {
  92.       _IO_sputn(target, fp->_IO_write_base, used);
  93.       fp->_IO_write_ptr -= used;
  94.     }
  95.   return _IO_putc (c, fp);
  96. }
  97.  
  98. static struct _IO_jump_t _IO_helper_jumps = {
  99.   _IO_helper_overflow,
  100.   _IO_default_underflow,
  101.   _IO_default_xsputn,
  102.   _IO_default_xsgetn,
  103.   _IO_default_read,
  104.   _IO_default_write,
  105.   _IO_default_doallocate,
  106.   _IO_default_pbackfail,
  107.   _IO_default_setbuf,
  108.   _IO_default_sync,
  109.   _IO_default_finish,
  110.   _IO_default_close,
  111.   _IO_default_stat,
  112.   _IO_default_seek,
  113.   _IO_default_seekoff,
  114.   _IO_default_seekpos,
  115.   _IO_default_uflow
  116. };
  117.  
  118. static int
  119. helper_vfprintf(fp, fmt0, ap)
  120.      register _IO_FILE* fp;
  121.      char const *fmt0;
  122.      _IO_va_list ap;
  123. {
  124.   char buf[_IO_BUFSIZ];
  125.   struct helper_file helper;
  126.   register _IO_FILE *hp = (_IO_FILE*)&helper;
  127.   int result, to_flush;
  128.  
  129.   /* initialize helper */
  130.   helper._put_stream = fp;
  131.   hp->_IO_write_base = buf;
  132.   hp->_IO_write_ptr = buf;
  133.   hp->_IO_write_end = buf+_IO_BUFSIZ;
  134.   hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
  135.   hp->_jumps = &_IO_helper_jumps;
  136.   
  137.   /* Now print to helper instead. */
  138.   result = _IO_vfprintf(hp, fmt0, ap);
  139.  
  140.   /* Now flush anything from the helper to the fp. */
  141.   if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
  142.     {
  143.       if (_IO_sputn(fp, hp->_IO_write_base, to_flush) != to_flush)
  144.     return EOF;
  145.     }
  146.   return result;
  147. }
  148.  
  149. #ifdef FLOATING_POINT
  150.  
  151. #include "floatio.h"
  152. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  153. #define    DEFPREC        6
  154. extern double modf __P((double, double*));
  155.  
  156. #else /* no FLOATING_POINT */
  157.  
  158. #define    BUF        40
  159.  
  160. #endif /* FLOATING_POINT */
  161.  
  162.  
  163. /*
  164.  * Macros for converting digits to letters and vice versa
  165.  */
  166. #define    to_digit(c)    ((c) - '0')
  167. #define is_digit(c)    ((unsigned)to_digit(c) <= 9)
  168. #define    to_char(n)    ((n) + '0')
  169.  
  170. /*
  171.  * Flags used during conversion.
  172.  */
  173. #define    LONGINT        0x01        /* long integer */
  174. #define    LONGDBL        0x02        /* long double; unimplemented */
  175. #define    SHORTINT    0x04        /* short integer */
  176. #define    ALT        0x08        /* alternate form */
  177. #define    LADJUST        0x10        /* left adjustment */
  178. #define    ZEROPAD        0x20        /* zero (as opposed to blank) pad */
  179. #define    HEXPREFIX    0x40        /* add 0x or 0X prefix */
  180.  
  181. int
  182. _IO_vfprintf(fp, fmt0, ap)
  183.      register _IO_FILE* fp;
  184.      char const *fmt0;
  185.      _IO_va_list ap;
  186. {
  187.     register const char *fmt; /* format string */
  188.     register int ch;    /* character from fmt */
  189.     register int n;        /* handy integer (short term usage) */
  190.     register char *cp;    /* handy char pointer (short term usage) */
  191.     const char *fmark;    /* for remembering a place in fmt */
  192.     register int flags;    /* flags as above */
  193.     int ret;        /* return value accumulator */
  194.     int width;        /* width from format (%8d), or 0 */
  195.     int prec;        /* precision from format (%.3d), or -1 */
  196.     char sign;        /* sign prefix (' ', '+', '-', or \0) */
  197. #ifdef FLOATING_POINT
  198. #ifndef _IO_USE_DTOA
  199.     int softsign;        /* temporary negative sign for floats */
  200. #endif
  201.     long double _double;        /* double precision arguments %[eEfgG] */
  202. #ifndef _IO_USE_DTOA
  203.     int fpprec;        /* `extra' floating precision in [eEfgG] */
  204. #endif
  205. #endif
  206.     unsigned long _ulong;    /* integer arguments %[diouxX] */
  207.     enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
  208.     int dprec;        /* a copy of prec if [diouxX], 0 otherwise */
  209.     int dpad;        /* extra 0 padding needed for integers */
  210.     int fieldsz;        /* field size expanded by sign, dpad etc */
  211.     /* The initialization of 'size' is to suppress a warning that
  212.        'size' might be used unitialized.  It seems gcc can't
  213.        quite grok this spaghetti code ... */
  214.     int size = 0;        /* size of converted field or string */
  215.     char buf[BUF];        /* space for %c, %[diouxX], %[eEfgG] */
  216.     char ox[2];        /* space for 0x hex-prefix */
  217.  
  218.     /*
  219.      * BEWARE, these `goto error' on error, and PAD uses `n'.
  220.      */
  221. #define    PRINT(ptr, len) \
  222.   do { if (_IO_sputn(fp,ptr, len) != len) goto error; } while (0)
  223. #define PAD_SP(howmany) if (_IO_padn(fp, ' ', howmany) < (howmany)) goto error;
  224. #define PAD_0(howmany) if (_IO_padn(fp, '0', howmany) < (howmany)) goto error;
  225.  
  226.     /*
  227.      * To extend shorts properly, we need both signed and unsigned
  228.      * argument extraction methods.
  229.      */
  230. #define    SARG() \
  231.     (flags&LONGINT ? va_arg(ap, long) : \
  232.         flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
  233.         (long)va_arg(ap, int))
  234. #define    UARG() \
  235.     (flags&LONGINT ? va_arg(ap, unsigned long) : \
  236.         flags&SHORTINT ? (unsigned long)(unsigned short)va_arg(ap, int) : \
  237.         (unsigned long)va_arg(ap, unsigned int))
  238.  
  239.     /* optimise stderr (and other unbuffered Unix files) */
  240.     if (fp->_IO_file_flags & _IO_UNBUFFERED)
  241.         return helper_vfprintf(fp, fmt0, ap);
  242.  
  243.     fmt = fmt0;
  244.     ret = 0;
  245.  
  246.     /*
  247.      * Scan the format for conversions (`%' character).
  248.      */
  249.     for (;;) {
  250.         for (fmark = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
  251.             /* void */;
  252.         if ((n = fmt - fmark) != 0) {
  253.             PRINT(fmark, n);
  254.             ret += n;
  255.         }
  256.         if (ch == '\0')
  257.             goto done;
  258.         fmt++;        /* skip over '%' */
  259.  
  260.         flags = 0;
  261.         dprec = 0;
  262. #if defined(FLOATING_POINT) && !defined (_IO_USE_DTOA)
  263.         fpprec = 0;
  264. #endif
  265.         width = 0;
  266.         prec = -1;
  267.         sign = '\0';
  268.  
  269. rflag:        ch = *fmt++;
  270. reswitch:    switch (ch) {
  271.         case ' ':
  272.             /*
  273.              * ``If the space and + flags both appear, the space
  274.              * flag will be ignored.''
  275.              *    -- ANSI X3J11
  276.              */
  277.             if (!sign)
  278.                 sign = ' ';
  279.             goto rflag;
  280.         case '#':
  281.             flags |= ALT;
  282.             goto rflag;
  283.         case '*':
  284.             /*
  285.              * ``A negative field width argument is taken as a
  286.              * - flag followed by a positive field width.''
  287.              *    -- ANSI X3J11
  288.              * They don't exclude field widths read from args.
  289.              */
  290.             if ((width = va_arg(ap, int)) >= 0)
  291.                 goto rflag;
  292.             width = -width;
  293.             /* FALLTHROUGH */
  294.         case '-':
  295.             flags |= LADJUST;
  296.             flags &= ~ZEROPAD; /* '-' disables '0' */
  297.             goto rflag;
  298.         case '+':
  299.             sign = '+';
  300.             goto rflag;
  301.         case '.':
  302.             if ((ch = *fmt++) == '*') {
  303.                 n = va_arg(ap, int);
  304.                 prec = n < 0 ? -1 : n;
  305.                 goto rflag;
  306.             }
  307.             n = 0;
  308.             while (is_digit(ch)) {
  309.                 n = 10 * n + to_digit(ch);
  310.                 ch = *fmt++;
  311.             }
  312.             prec = n < 0 ? -1 : n;
  313.             goto reswitch;
  314.         case '0':
  315.             /*
  316.              * ``Note that 0 is taken as a flag, not as the
  317.              * beginning of a field width.''
  318.              *    -- ANSI X3J11
  319.              */
  320.             if (!(flags & LADJUST))
  321.                 flags |= ZEROPAD; /* '-' disables '0' */
  322.             goto rflag;
  323.         case '1': case '2': case '3': case '4':
  324.         case '5': case '6': case '7': case '8': case '9':
  325.             n = 0;
  326.             do {
  327.                 n = 10 * n + to_digit(ch);
  328.                 ch = *fmt++;
  329.             } while (is_digit(ch));
  330.             width = n;
  331.             goto reswitch;
  332. #ifdef FLOATING_POINT
  333.         case 'L':
  334.             flags |= LONGDBL;
  335.             goto rflag;
  336. #endif
  337.         case 'h':
  338.             flags |= SHORTINT;
  339.             goto rflag;
  340.         case 'l':
  341.             flags |= LONGINT;
  342.             goto rflag;
  343.         case 'c':
  344.             *(cp = buf) = va_arg(ap, int);
  345.             size = 1;
  346.             sign = '\0';
  347.             break;
  348.         case 'D':
  349.             flags |= LONGINT;
  350.             /*FALLTHROUGH*/
  351.         case 'd':
  352.         case 'i':
  353.             _ulong = SARG();
  354.             if ((long)_ulong < 0) {
  355.                 _ulong = -_ulong;
  356.                 sign = '-';
  357.             }
  358.             base = DEC;
  359.             goto number;
  360. #ifdef FLOATING_POINT
  361.         case 'e':
  362.         case 'E':
  363.         case 'f':
  364.         case 'F':
  365.         case 'g':
  366.         case 'G':
  367.                       if (flags & LONGDBL)
  368.                         _double = va_arg(ap, long double);
  369.                       else
  370.                         _double = (long double) (va_arg(ap, double));
  371. #ifdef _IO_USE_DTOA
  372.             {
  373.                 int fmt_flags = 0;
  374.                 int fill = ' ';
  375.                 if (flags & ALT)
  376.                 fmt_flags |= _IO_SHOWPOINT;
  377.                 if (flags & LADJUST)
  378.                 fmt_flags |= _IO_LEFT;
  379.                 else if (flags & ZEROPAD)
  380.                 fmt_flags |= _IO_INTERNAL, fill = '0';
  381.                 if (flags & LONGDBL)
  382.                   fmt_flags |= _IO_LDOUBLE;
  383.                 n = _IO_outfloat(_double, fp, ch, width,
  384.                          prec < 0 ? DEFPREC : prec,
  385.                          fmt_flags, sign, fill);
  386.                 if (n < 0)
  387.                 goto error;
  388.                 ret += n;
  389.             }
  390.             /* CHECK ERROR! */
  391.             continue;
  392. #else
  393.             /*
  394.              * don't do unrealistic precision; just pad it with
  395.              * zeroes later, so buffer size stays rational.
  396.              */
  397.             if (prec > MAXFRACT) {
  398.                 if ((ch != 'g' && ch != 'G') || (flags&ALT))
  399.                     fpprec = prec - MAXFRACT;
  400.                 prec = MAXFRACT;
  401.             } else if (prec == -1)
  402.                 prec = DEFPREC;
  403.             /* __cvt_double may have to round up before the
  404.                "start" of its buffer, i.e.
  405.                ``intf("%.2f", (double)9.999);'';
  406.                if the first character is still NUL, it did.
  407.                softsign avoids negative 0 if _double < 0 but
  408.                no significant digits will be shown. */
  409.             cp = buf;
  410.             *cp = '\0';
  411.             size = __cvt_double(_double, prec, flags, &softsign,
  412.                         ch, cp, buf + sizeof(buf));
  413.             if (softsign)
  414.                 sign = '-';
  415.             if (*cp == '\0')
  416.                 cp++;
  417.             break;
  418. #endif
  419. #endif /* FLOATING_POINT */
  420.         case 'n':
  421.             if (flags & LONGINT)
  422.                 *va_arg(ap, long *) = ret;
  423.             else if (flags & SHORTINT)
  424.                 *va_arg(ap, short *) = ret;
  425.             else
  426.                 *va_arg(ap, int *) = ret;
  427.             continue;    /* no output */
  428.         case 'O':
  429.             flags |= LONGINT;
  430.             /*FALLTHROUGH*/
  431.         case 'o':
  432.             _ulong = UARG();
  433.             base = OCT;
  434.             goto nosign;
  435.         case 'p':
  436.             /*
  437.              * ``The argument shall be a pointer to void.  The
  438.              * value of the pointer is converted to a sequence
  439.              * of printable characters, in an implementation-
  440.              * defined manner.''
  441.              *    -- ANSI X3J11
  442.              */
  443.             /* NOSTRICT */
  444.             _ulong = (unsigned long)va_arg(ap, void *);
  445.             base = HEX;
  446.             flags |= HEXPREFIX;
  447.             ch = 'x';
  448.             goto nosign;
  449.         case 's':
  450.             if ((cp = va_arg(ap, char *)) == NULL)
  451.                 cp = "(null)";
  452.             if (prec >= 0) {
  453.                 /*
  454.                  * can't use strlen; can only look for the
  455.                  * NUL in the first `prec' characters, and
  456.                  * strlen() will go further.
  457.                  */
  458.                 char *p = (char*)memchr(cp, 0, prec);
  459.  
  460.                 if (p != NULL) {
  461.                     size = p - cp;
  462.                     if (size > prec)
  463.                         size = prec;
  464.                 } else
  465.                     size = prec;
  466.             } else
  467.                 size = strlen(cp);
  468.             sign = '\0';
  469.             break;
  470.         case 'U':
  471.             flags |= LONGINT;
  472.             /*FALLTHROUGH*/
  473.         case 'u':
  474.             _ulong = UARG();
  475.             base = DEC;
  476.             goto nosign;
  477.         case 'X':
  478.         case 'x':
  479.             _ulong = UARG();
  480.             base = HEX;
  481.             /* leading 0x/X only if non-zero */
  482.             if (flags & ALT && _ulong != 0)
  483.                 flags |= HEXPREFIX;
  484.  
  485.             /* unsigned conversions */
  486. nosign:            sign = '\0';
  487.             /*
  488.              * ``... diouXx conversions ... if a precision is
  489.              * specified, the 0 flag will be ignored.''
  490.              *    -- ANSI X3J11
  491.              */
  492. number:            if ((dprec = prec) >= 0)
  493.                 flags &= ~ZEROPAD;
  494.  
  495.             /*
  496.              * ``The result of converting a zero value with an
  497.              * explicit precision of zero is no characters.''
  498.              *    -- ANSI X3J11
  499.              */
  500.             cp = buf + BUF;
  501.             if (_ulong != 0 || prec != 0) {
  502.                     char *xdigs; /* digits for [xX] conversion */
  503.                 /*
  504.                  * unsigned mod is hard, and unsigned mod
  505.                  * by a constant is easier than that by
  506.                  * a variable; hence this switch.
  507.                  */
  508.                 switch (base) {
  509.                 case OCT:
  510.                     do {
  511.                         *--cp = to_char(_ulong & 7);
  512.                         _ulong >>= 3;
  513.                     } while (_ulong);
  514.                     /* handle octal leading 0 */
  515.                     if (flags & ALT && *cp != '0')
  516.                         *--cp = '0';
  517.                     break;
  518.  
  519.                 case DEC:
  520.                     /* many numbers are 1 digit */
  521.                     while (_ulong >= 10) {
  522.                         *--cp = to_char(_ulong % 10);
  523.                         _ulong /= 10;
  524.                     }
  525.                     *--cp = to_char(_ulong);
  526.                     break;
  527.  
  528.                 case HEX:
  529.                     if (ch == 'X')
  530.                         xdigs = "0123456789ABCDEF";
  531.                     else /* ch == 'x' || ch == 'p' */
  532.                         xdigs = "0123456789abcdef";
  533.                     do {
  534.                         *--cp = xdigs[_ulong & 15];
  535.                         _ulong >>= 4;
  536.                     } while (_ulong);
  537.                     break;
  538.  
  539.                 default:
  540.                     cp = "bug in vform: bad base";
  541.                     goto skipsize;
  542.                 }
  543.             }
  544.             size = buf + BUF - cp;
  545.         skipsize:
  546.             break;
  547.         default:    /* "%?" prints ?, unless ? is NUL */
  548.             if (ch == '\0')
  549.                 goto done;
  550.             /* pretend it was %c with argument ch */
  551.             cp = buf;
  552.             *cp = ch;
  553.             size = 1;
  554.             sign = '\0';
  555.             break;
  556.         }
  557.  
  558.         /*
  559.          * All reasonable formats wind up here.  At this point,
  560.          * `cp' points to a string which (if not flags&LADJUST)
  561.          * should be padded out to `width' places.  If
  562.          * flags&ZEROPAD, it should first be prefixed by any
  563.          * sign or other prefix; otherwise, it should be blank
  564.          * padded before the prefix is emitted.  After any
  565.          * left-hand padding and prefixing, emit zeroes
  566.          * required by a decimal [diouxX] precision, then print
  567.          * the string proper, then emit zeroes required by any
  568.          * leftover floating precision; finally, if LADJUST,
  569.          * pad with blanks.
  570.          */
  571.  
  572.         /*
  573.          * compute actual size, so we know how much to pad.
  574.          */
  575. #if defined(FLOATING_POINT) && !defined (_IO_USE_DTOA)
  576.         fieldsz = size + fpprec;
  577. #else
  578.         fieldsz = size;
  579. #endif
  580.         dpad = dprec - size;
  581.         if (dpad < 0)
  582.             dpad = 0;
  583.  
  584.         if (sign)
  585.             fieldsz++;
  586.         else if (flags & HEXPREFIX)
  587.             fieldsz += 2;
  588.         fieldsz += dpad;
  589.  
  590.         /* right-adjusting blank padding */
  591.         if ((flags & (LADJUST|ZEROPAD)) == 0)
  592.             PAD_SP(width - fieldsz);
  593.  
  594.         /* prefix */
  595.         if (sign) {
  596.             PRINT(&sign, 1);
  597.         } else if (flags & HEXPREFIX) {
  598.             ox[0] = '0';
  599.             ox[1] = ch;
  600.             PRINT(ox, 2);
  601.         }
  602.  
  603.         /* right-adjusting zero padding */
  604.         if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
  605.             PAD_0(width - fieldsz);
  606.  
  607.         /* leading zeroes from decimal precision */
  608.         PAD_0(dpad);
  609.  
  610.         /* the string or number proper */
  611.         PRINT(cp, size);
  612.  
  613. #if defined(FLOATING_POINT) && !defined (_IO_USE_DTOA)
  614.         /* trailing f.p. zeroes */
  615.         PAD_0(fpprec);
  616. #endif
  617.  
  618.         /* left-adjusting padding (always blank) */
  619.         if (flags & LADJUST)
  620.             PAD_SP(width - fieldsz);
  621.  
  622.         /* finally, adjust ret */
  623.         ret += width > fieldsz ? width : fieldsz;
  624.  
  625.     }
  626. done:
  627.     return ret;
  628. error:
  629.     return EOF;
  630.     /* NOTREACHED */
  631. }
  632.  
  633. #if defined(FLOATING_POINT) && !defined(_IO_USE_DTOA)
  634.  
  635. static char *exponent(register char *p, register int exp, int fmtch)
  636. {
  637.     register char *t;
  638.     char expbuf[MAXEXP];
  639.  
  640.     *p++ = fmtch;
  641.     if (exp < 0) {
  642.         exp = -exp;
  643.         *p++ = '-';
  644.     }
  645.     else
  646.         *p++ = '+';
  647.     t = expbuf + MAXEXP;
  648.     if (exp > 9) {
  649.         do {
  650.             *--t = to_char(exp % 10);
  651.         } while ((exp /= 10) > 9);
  652.         *--t = to_char(exp);
  653.         for (; t < expbuf + MAXEXP; *p++ = *t++);
  654.     }
  655.     else {
  656.         *p++ = '0';
  657.         *p++ = to_char(exp);
  658.     }
  659.     return (p);
  660. }
  661.  
  662. static char * round(double fract, int *exp,
  663.             register char *start, register char *end,
  664.             char ch, int *signp)
  665. {
  666.     double tmp;
  667.  
  668.     if (fract)
  669.     (void)modf(fract * 10, &tmp);
  670.     else
  671.         tmp = to_digit(ch);
  672.     if (tmp > 4)
  673.         for (;; --end) {
  674.             if (*end == '.')
  675.                 --end;
  676.             if (++*end <= '9')
  677.                 break;
  678.             *end = '0';
  679.             if (end == start) {
  680.                 if (exp) {    /* e/E; increment exponent */
  681.                     *end = '1';
  682.                     ++*exp;
  683.                 }
  684.                 else {        /* f; add extra digit */
  685.                 *--end = '1';
  686.                 --start;
  687.                 }
  688.                 break;
  689.             }
  690.         }
  691.     /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
  692.     else if (*signp == '-')
  693.         for (;; --end) {
  694.             if (*end == '.')
  695.                 --end;
  696.             if (*end != '0')
  697.                 break;
  698.             if (end == start)
  699.                 *signp = 0;
  700.         }
  701.     return (start);
  702. }
  703.  
  704. int __cvt_double(double number, register int prec, int flags, int *signp,
  705.          int fmtch, char *startp, char *endp)
  706. {
  707.     register char *p, *t;
  708.     register double fract;
  709.     int dotrim = 0, expcnt, gformat = 0;
  710.     double integer, tmp;
  711.  
  712.     expcnt = 0;
  713.     if (number < 0) {
  714.         number = -number;
  715.         *signp = '-';
  716.     } else
  717.         *signp = 0;
  718.  
  719.     fract = modf(number, &integer);
  720.  
  721.     /* get an extra slot for rounding. */
  722.     t = ++startp;
  723.  
  724.     /*
  725.      * get integer portion of number; put into the end of the buffer; the
  726.      * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
  727.      */
  728.     for (p = endp - 1; integer; ++expcnt) {
  729.         tmp = modf(integer / 10, &integer);
  730.         *p-- = to_char((int)((tmp + .01) * 10));
  731.     }
  732.     switch (fmtch) {
  733.     case 'f':
  734.     case 'F':
  735.         /* reverse integer into beginning of buffer */
  736.         if (expcnt)
  737.             for (; ++p < endp; *t++ = *p);
  738.         else
  739.             *t++ = '0';
  740.         /*
  741.          * if precision required or alternate flag set, add in a
  742.          * decimal point.
  743.          */
  744.         if (prec || flags&ALT)
  745.             *t++ = '.';
  746.         /* if requires more precision and some fraction left */
  747.         if (fract) {
  748.             if (prec)
  749.                 do {
  750.                     fract = modf(fract * 10, &tmp);
  751.                     *t++ = to_char((int)tmp);
  752.                 } while (--prec && fract);
  753.             if (fract)
  754.                 startp = round(fract, (int *)NULL, startp,
  755.                     t - 1, (char)0, signp);
  756.         }
  757.         for (; prec--; *t++ = '0');
  758.         break;
  759.     case 'e':
  760.     case 'E':
  761. eformat:    if (expcnt) {
  762.             *t++ = *++p;
  763.             if (prec || flags&ALT)
  764.                 *t++ = '.';
  765.             /* if requires more precision and some integer left */
  766.             for (; prec && ++p < endp; --prec)
  767.                 *t++ = *p;
  768.             /*
  769.              * if done precision and more of the integer component,
  770.              * round using it; adjust fract so we don't re-round
  771.              * later.
  772.              */
  773.             if (!prec && ++p < endp) {
  774.                 fract = 0;
  775.                 startp = round((double)0, &expcnt, startp,
  776.                     t - 1, *p, signp);
  777.             }
  778.             /* adjust expcnt for digit in front of decimal */
  779.             --expcnt;
  780.         }
  781.         /* until first fractional digit, decrement exponent */
  782.         else if (fract) {
  783.             /* adjust expcnt for digit in front of decimal */
  784.             for (expcnt = -1;; --expcnt) {
  785.                 fract = modf(fract * 10, &tmp);
  786.                 if (tmp)
  787.                     break;
  788.             }
  789.             *t++ = to_char((int)tmp);
  790.             if (prec || flags&ALT)
  791.                 *t++ = '.';
  792.         }
  793.         else {
  794.             *t++ = '0';
  795.             if (prec || flags&ALT)
  796.                 *t++ = '.';
  797.         }
  798.         /* if requires more precision and some fraction left */
  799.         if (fract) {
  800.             if (prec)
  801.                 do {
  802.                     fract = modf(fract * 10, &tmp);
  803.                     *t++ = to_char((int)tmp);
  804.                 } while (--prec && fract);
  805.             if (fract)
  806.                 startp = round(fract, &expcnt, startp,
  807.                     t - 1, (char)0, signp);
  808.         }
  809.         /* if requires more precision */
  810.         for (; prec--; *t++ = '0');
  811.  
  812.         /* unless alternate flag, trim any g/G format trailing 0's */
  813.         if (gformat && !(flags&ALT)) {
  814.             while (t > startp && *--t == '0');
  815.             if (*t == '.')
  816.                 --t;
  817.             ++t;
  818.         }
  819.         t = exponent(t, expcnt, fmtch);
  820.         break;
  821.     case 'g':
  822.     case 'G':
  823.         /* a precision of 0 is treated as a precision of 1. */
  824.         if (!prec)
  825.             ++prec;
  826.         /*
  827.          * ``The style used depends on the value converted; style e
  828.          * will be used only if the exponent resulting from the
  829.          * conversion is less than -4 or greater than the precision.''
  830.          *    -- ANSI X3J11
  831.          */
  832.         if (expcnt > prec || (!expcnt && fract && fract < .0001)) {
  833.             /*
  834.              * g/G format counts "significant digits, not digits of
  835.              * precision; for the e/E format, this just causes an
  836.              * off-by-one problem, i.e. g/G considers the digit
  837.              * before the decimal point significant and e/E doesn't
  838.              * count it as precision.
  839.              */
  840.             --prec;
  841.             fmtch -= 2;        /* G->E, g->e */
  842.             gformat = 1;
  843.             goto eformat;
  844.         }
  845.         /*
  846.          * reverse integer into beginning of buffer,
  847.          * note, decrement precision
  848.          */
  849.         if (expcnt)
  850.             for (; ++p < endp; *t++ = *p, --prec);
  851.         else
  852.             *t++ = '0';
  853.         /*
  854.          * if precision required or alternate flag set, add in a
  855.          * decimal point.  If no digits yet, add in leading 0.
  856.          */
  857.         if (prec || flags&ALT) {
  858.             dotrim = 1;
  859.             *t++ = '.';
  860.         }
  861.         else
  862.             dotrim = 0;
  863.         /* if requires more precision and some fraction left */
  864.         if (fract) {
  865.             if (prec) {
  866.                 /* If no integer part, don't count initial
  867.                  * zeros as significant digits. */
  868.                 do {
  869.                     fract = modf(fract * 10, &tmp);
  870.                     *t++ = to_char((int)tmp);
  871.                 } while(!tmp && !expcnt);
  872.                 while (--prec && fract) {
  873.                     fract = modf(fract * 10, &tmp);
  874.                     *t++ = to_char((int)tmp);
  875.                 }
  876.             }
  877.             if (fract)
  878.                 startp = round(fract, (int *)NULL, startp,
  879.                     t - 1, (char)0, signp);
  880.         }
  881.         /* alternate format, adds 0's for precision, else trim 0's */
  882.         if (flags&ALT)
  883.             for (; prec--; *t++ = '0');
  884.         else if (dotrim) {
  885.             while (t > startp && *--t == '0');
  886.             if (*t != '.')
  887.                 ++t;
  888.         }
  889.     }
  890.     return (t - startp);
  891. }
  892.  
  893. #endif /* defined(FLOATING_POINT) && !defined(_IO_USE_DTOA) */
  894.  
  895. #if defined(__ELF__) || defined(__GNU_LIBRARY__)
  896. #include <gnu-stabs.h>
  897. #ifdef weak_alias
  898. weak_alias (_IO_vfprintf, vfprintf);
  899. #endif
  900. #endif
  901.