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