genericopenlibs/cstdlib/LSTDIO/VFPRINTF.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * FUNCTION
       
     3 * <<vprintf>>, <<vfprintf>>, <<vsprintf>>---format argument list
       
     4 * INDEX
       
     5 * vprintf
       
     6 * INDEX
       
     7 * vfprintf
       
     8 * INDEX
       
     9 * vsprintf
       
    10 * ANSI_SYNOPSIS
       
    11 * #include <stdio.h>
       
    12 * #include <stdarg.h>
       
    13 * int vprintf(const char *<[fmt]>, va_list <[list]>);
       
    14 * int vfprintf(FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>);
       
    15 * int vsprintf(char *<[str]>, const char *<[fmt]>, va_list <[list]>);
       
    16 * int _vprintf_r(void *<[reent]>, const char *<[fmt]>,
       
    17 * va_list <[list]>);
       
    18 * int _vfprintf_r(void *<[reent]>, FILE *<[fp]>, const char *<[fmt]>,
       
    19 * va_list <[list]>);
       
    20 * int _vsprintf_r(void *<[reent]>, char *<[str]>, const char *<[fmt]>,
       
    21 * va_list <[list]>);
       
    22 * TRAD_SYNOPSIS
       
    23 * #include <stdio.h>
       
    24 * #include <varargs.h>
       
    25 * int vprintf( <[fmt]>, <[list]>)
       
    26 * char *<[fmt]>;
       
    27 * va_list <[list]>;
       
    28 * int vfprintf(<[fp]>, <[fmt]>, <[list]>)
       
    29 * FILE *<[fp]>;
       
    30 * char *<[fmt]>;
       
    31 * va_list <[list]>;
       
    32 * int vsprintf(<[str]>, <[fmt]>, <[list]>)
       
    33 * char *<[str]>;
       
    34 * char *<[fmt]>;
       
    35 * va_list <[list]>;
       
    36 * int _vprintf_r(<[reent]>, <[fmt]>, <[list]>)
       
    37 * char *<[reent]>;
       
    38 * char *<[fmt]>;
       
    39 * va_list <[list]>;
       
    40 * int _vfprintf_r(<[reent]>, <[fp]>, <[fmt]>, <[list]>)
       
    41 * char *<[reent]>;
       
    42 * FILE *<[fp]>;
       
    43 * char *<[fmt]>;
       
    44 * va_list <[list]>;
       
    45 * int _vsprintf_r(<[reent]>, <[str]>, <[fmt]>, <[list]>)
       
    46 * char *<[reent]>;
       
    47 * char *<[str]>;
       
    48 * char *<[fmt]>;
       
    49 * va_list <[list]>;
       
    50 * <<vprintf>>, <<vfprintf>>, and <<vsprintf>> are (respectively)
       
    51 * variants of <<printf>>, <<fprintf>>, and <<sprintf>>.  They differ
       
    52 * only in allowing their caller to pass the variable argument list as a
       
    53 * <<va_list>> object (initialized by <<va_start>>) rather than directly
       
    54 * accepting a variable number of arguments.
       
    55 * RETURNS
       
    56 * The return values are consistent with the corresponding functions:
       
    57 * <<vsprintf>> returns the number of bytes in the output string,
       
    58 * save that the concluding <<NULL>> is not counted.
       
    59 * <<vprintf>> and <<vfprintf>> return the number of characters transmitted.
       
    60 * If an error occurs, <<vprintf>> and <<vfprintf>> return <<EOF>>. No
       
    61 * error returns occur for <<vsprintf>>.
       
    62 * PORTABILITY
       
    63 * ANSI C requires all three functions.
       
    64 * Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
       
    65 * <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
       
    66 * 
       
    67 *
       
    68 */
       
    69 
       
    70 
       
    71 
       
    72 /*-
       
    73  * Copyright (c) 1990 The Regents of the University of California.
       
    74  * All rights reserved.
       
    75  *
       
    76  * This code is derived from software contributed to Berkeley by
       
    77  * Chris Torek.
       
    78  *
       
    79  * Redistribution and use in source and binary forms, with or without
       
    80  * modification, are permitted provided that the following conditions
       
    81  * are met:
       
    82  * 1. Redistributions of source code must retain the above copyright
       
    83  *    notice, this list of conditions and the following disclaimer.
       
    84  * 2. Redistributions in binary form must reproduce the above copyright
       
    85  *    notice, this list of conditions and the following disclaimer in the
       
    86  *    documentation and/or other materials provided with the distribution.
       
    87  * 3. All advertising materials mentioning features or use of this software
       
    88  *    must display the following acknowledgement:
       
    89  *	This product includes software developed by the University of
       
    90  *	California, Berkeley and its contributors.
       
    91  * 4. Neither the name of the University nor the names of its contributors
       
    92  *    may be used to endorse or promote products derived from this software
       
    93  *    without specific prior written permission.
       
    94  *
       
    95  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
       
    96  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    97  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    98  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
       
    99  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
   100  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
   101  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
   102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
   103  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
   104  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
   105  * SUCH DAMAGE.
       
   106  */
       
   107 
       
   108 /*
       
   109  * Actual printf innards.
       
   110  *
       
   111  * This code is large and complicated...
       
   112  */
       
   113 
       
   114 #if 1
       
   115 #define _NO_LONGLONG
       
   116 #endif
       
   117 
       
   118 #include <_ansi.h>
       
   119 #include <stdio_r.h>
       
   120 #include <stdlib_r.h>
       
   121 #include <string.h>
       
   122 #include <reent.h>
       
   123 
       
   124 #include <stdarg.h>
       
   125 
       
   126 #include "LOCAL.H"
       
   127 #include "FVWRITE.H"
       
   128 
       
   129 
       
   130 //cm
       
   131 #include <stdlib.h>
       
   132 
       
   133 
       
   134 /*
       
   135  * Flush out all the vectors defined by the given uio,
       
   136  * then reset it so that it can be reused.
       
   137  */
       
   138 static int
       
   139 __sprint(FILE *fp,register struct __suio *uio)
       
   140 {
       
   141 	register int err;
       
   142 
       
   143 	if (uio->uio_resid == 0) {
       
   144 		uio->uio_iovcnt = 0;
       
   145 		return (0);
       
   146 	}
       
   147 	err = __sfvwrite(fp, uio);
       
   148 	uio->uio_resid = 0;
       
   149 	uio->uio_iovcnt = 0;
       
   150 	return (err);
       
   151 }
       
   152 
       
   153 /*
       
   154  * Helper function for `fprintf to unbuffered unix file': creates a
       
   155  * temporary buffer.  We only work on write-only files; this avoids
       
   156  * worries about ungetc buffers and so forth.
       
   157  */
       
   158 static int
       
   159 __sbprintf(register FILE *fp,const char *fmt,va_list ap)
       
   160 {
       
   161 	int ret;
       
   162 	FILE fake;
       
   163 	unsigned char buf[BUFSIZ];
       
   164 
       
   165 	/* copy the important variables */
       
   166 	fake._data = fp->_data;
       
   167 	fake._flags = (short)(fp->_flags & ~__SNBF);
       
   168 	fake._file = fp->_file;
       
   169 	fake._cookie = fp->_cookie;
       
   170 	fake._write = fp->_write;
       
   171 
       
   172 	/* set up the buffer */
       
   173 	fake._bf._base = fake._p = buf;
       
   174 	fake._bf._size = fake._w = sizeof(buf);
       
   175 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
       
   176 
       
   177 	/* do the work, then copy any error status */
       
   178 	ret = vfprintf(&fake, fmt, ap);
       
   179 	if (ret >= 0 && fflush(&fake))
       
   180 		ret = EOF;
       
   181 	if (fake._flags & __SERR)
       
   182 		fp->_flags |= __SERR;
       
   183 	return (ret);
       
   184 }
       
   185 
       
   186 
       
   187 /* #include <locale.h>   for decimal_point */
       
   188 #include <math.h>
       
   189 #include "FLOATIO.H"
       
   190 
       
   191 #define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point */
       
   192 #define	DEFPREC		6
       
   193 
       
   194 extern int e32cvt (char *buf, int buflen, double, int width, int prec, int flags, int fmt);
       
   195 
       
   196 
       
   197 /*
       
   198  * Macros for converting digits to letters and vice versa
       
   199  */
       
   200 #define	to_digit(c)	((c) - '0')
       
   201 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
       
   202 #define	to_char(n)	((n) + '0')
       
   203 
       
   204 /*
       
   205  * Flags used during conversion.
       
   206  */
       
   207 #define	ALT		0x001		/* alternate form */
       
   208 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
       
   209 #define	LADJUST		0x004		/* left adjustment */
       
   210 #define	LONGDBL		0x008		/* long double; unimplemented */
       
   211 #define	LONGINT		0x010		/* long integer */
       
   212 #define	QUADINT		0x020		/* quad integer */
       
   213 #define	SHORTINT	0x040		/* short integer */
       
   214 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
       
   215 #define FPT		0x100		/* Floating point number */
       
   216 
       
   217 EXPORT_C int 
       
   218 vfprintf (FILE * fp, const char *fmt0, va_list ap)
       
   219 {
       
   220   return _vfprintf_r (fp->_data, fp, fmt0, ap);
       
   221 }
       
   222 
       
   223 /**
       
   224 A reentrant version of vfprintf().
       
   225 */
       
   226 EXPORT_C int 
       
   227 _vfprintf_r (struct _reent *data, FILE * fp, const char *fmt0, va_list ap)
       
   228 {
       
   229 	register char *fmt;	/* format string */
       
   230 	register int ch;	/* character from fmt */
       
   231 	register int n, m;	/* handy integers (short term usage) */
       
   232 	register char *cp;	/* handy char pointer (short term usage) */
       
   233 	register struct __siov *iovp;/* for PRINT macro */
       
   234 	register int flags;	/* flags as above */
       
   235 	int ret;		/* return value accumulator */
       
   236 	int width;		/* width from format (%8d), or 0 */
       
   237 	int prec;		/* precision from format (%.3d), or -1 */
       
   238 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
       
   239 	wchar_t wc;
       
   240 #if 0
       
   241 	char *decimal_point = "."; /* localeconv()->decimal_point; */
       
   242 #endif
       
   243 //	char softsign;		/* temporary negative sign for floats */
       
   244 	double _double;		/* double precision arguments %[eEfgG] */
       
   245 //	int expt;		/* integer value of exponent */
       
   246 //	int expsize;		/* character count for expstr */
       
   247 //	int ndig;		/* actual number of digits returned by cvt */
       
   248 //	char expstr[7];		/* buffer for exponent string */
       
   249 
       
   250 #ifndef _NO_LONGLONG
       
   251 #define	quad_t	  long long
       
   252 #define	u_quad_t  unsigned long long
       
   253 #endif
       
   254 
       
   255 #ifndef _NO_LONGLONG
       
   256 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
       
   257 #else
       
   258 	u_long _uquad;
       
   259 #endif
       
   260 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
       
   261 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
       
   262 	int realsz;		/* field size expanded by dprec */
       
   263 	int size;		/* size of converted field or string */
       
   264 	char *xdigs = NULL;	/* digits for [xX] conversion */
       
   265 #define NIOV 8
       
   266 	struct __suio uio;	/* output information: summary */
       
   267 	struct __siov iov[NIOV];/* ... and individual io vectors */
       
   268 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
       
   269 	char ox[2];		/* space for 0x hex-prefix */
       
   270 
       
   271 	/*
       
   272 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
       
   273 	 * fields occur frequently, increase PADSIZE and make the initialisers
       
   274 	 * below longer.
       
   275 	 */
       
   276 #define	PADSIZE	16		/* pad chunk size */
       
   277 	static const char blanks[PADSIZE] =
       
   278 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
       
   279 	static const char zeroes[PADSIZE] =
       
   280 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
       
   281 
       
   282 	/*
       
   283 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
       
   284 	 */
       
   285 #define	PRINT(ptr, len) { \
       
   286 	iovp->iov_base = (ptr); \
       
   287 	iovp->iov_len = (len); \
       
   288 	uio.uio_resid += (len); \
       
   289 	iovp++; \
       
   290 	if (++uio.uio_iovcnt >= NIOV) { \
       
   291 		if (__sprint(fp, &uio)) \
       
   292 			goto error; \
       
   293 		iovp = iov; \
       
   294 	} \
       
   295 }
       
   296 #define	PAD(howmany, with) { \
       
   297 	if ((n = (howmany)) > 0) { \
       
   298 		while (n > PADSIZE) { \
       
   299 			PRINT(with, PADSIZE); \
       
   300 			n -= PADSIZE; \
       
   301 		} \
       
   302 		PRINT(with, n); \
       
   303 	} \
       
   304 }
       
   305 #define	FLUSH() { \
       
   306 	if (uio.uio_resid && __sprint(fp, &uio)) \
       
   307 		goto error; \
       
   308 	uio.uio_iovcnt = 0; \
       
   309 	iovp = iov; \
       
   310 }
       
   311 
       
   312 	/*
       
   313 	 * To extend shorts properly, we need both signed and unsigned
       
   314 	 * argument extraction methods.
       
   315 	 */
       
   316 #ifndef _NO_LONGLONG
       
   317 #define	SARG() \
       
   318 	(flags&QUADINT ? va_arg(ap, quad_t) : \
       
   319 	    flags&LONGINT ? va_arg(ap, long) : \
       
   320 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
       
   321 	    (long)va_arg(ap, int))
       
   322 #define	UARG() \
       
   323 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
       
   324 	    flags&LONGINT ? va_arg(ap, u_long) : \
       
   325 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
       
   326 	    (u_long)va_arg(ap, u_int))
       
   327 #else
       
   328 #define	SARG() \
       
   329 	(flags&LONGINT ? va_arg(ap, long) : \
       
   330 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
       
   331 	    (long)va_arg(ap, int))
       
   332 #define	UARG() \
       
   333 	(flags&LONGINT ? va_arg(ap, u_long) : \
       
   334 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
       
   335 	    (u_long)va_arg(ap, u_int))
       
   336 #endif
       
   337 
       
   338 	CHECK_INIT (fp);
       
   339 
       
   340 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
       
   341 	if (cantwrite(fp))
       
   342 		return (EOF);
       
   343 
       
   344 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
       
   345 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
       
   346 	    fp->_file >= 0)
       
   347 		return (__sbprintf(fp, fmt0, ap));
       
   348 
       
   349 	fmt = (char *)fmt0;
       
   350 	uio.uio_iov = iovp = iov;
       
   351 	uio.uio_resid = 0;
       
   352 	uio.uio_iovcnt = 0;
       
   353 	ret = 0;
       
   354 
       
   355 	/*
       
   356 	 * Scan the format for conversions (`%' character).
       
   357 	 */
       
   358 	for (;;) {
       
   359 		cp = fmt;
       
   360 		while ((n = mbtowc(&wc, fmt, MB_CUR_MAX)) > 0) {
       
   361 			fmt += n;
       
   362 			if (wc == L'%') {
       
   363 				fmt--;
       
   364 				break;
       
   365 			}
       
   366 		}
       
   367 		if ((m = fmt - cp) != 0) {
       
   368 			PRINT(cp, m);
       
   369 			ret += m;
       
   370 		}
       
   371 		if (n <= 0)
       
   372 			goto done;
       
   373 		fmt++;		/* skip over '%' */
       
   374 
       
   375 		flags = 0;
       
   376 		dprec = 0;
       
   377 		width = 0;
       
   378 		prec = -1;
       
   379 		sign = '\0';
       
   380 
       
   381 rflag:		ch = *fmt++;
       
   382 reswitch:	switch (ch) {
       
   383 		case ' ':
       
   384 			/*
       
   385 			 * ``If the space and + flags both appear, the space
       
   386 			 * flag will be ignored.''
       
   387 			 *	-- ANSI X3J11
       
   388 			 */
       
   389 			if (!sign)
       
   390 				sign = ' ';
       
   391 			goto rflag;
       
   392 		case '#':
       
   393 			flags |= ALT;
       
   394 			goto rflag;
       
   395 		case '*':
       
   396 			/*
       
   397 			 * ``A negative field width argument is taken as a
       
   398 			 * - flag followed by a positive field width.''
       
   399 			 *	-- ANSI X3J11
       
   400 			 * They don't exclude field widths read from args.
       
   401 			 */
       
   402 			if ((width = va_arg(ap, int)) >= 0)
       
   403 				goto rflag;
       
   404 			width = -width;
       
   405 			/* FALLTHROUGH */
       
   406 		case '-':
       
   407 			flags |= LADJUST;
       
   408 			goto rflag;
       
   409 		case '+':
       
   410 			sign = '+';
       
   411 			goto rflag;
       
   412 		case '.':
       
   413 			if ((ch = *fmt++) == '*') {
       
   414 				n = va_arg(ap, int);
       
   415 				prec = n < 0 ? -1 : n;
       
   416 				goto rflag;
       
   417 			}
       
   418 			n = 0;
       
   419 			while (is_digit(ch)) {
       
   420 				n = 10 * n + to_digit(ch);
       
   421 				ch = *fmt++;
       
   422 			}
       
   423 			prec = n < 0 ? -1 : n;
       
   424 			goto reswitch;
       
   425 		case '0':
       
   426 			/*
       
   427 			 * ``Note that 0 is taken as a flag, not as the
       
   428 			 * beginning of a field width.''
       
   429 			 *	-- ANSI X3J11
       
   430 			 */
       
   431 			flags |= ZEROPAD;
       
   432 			goto rflag;
       
   433 		case '1': case '2': case '3': case '4':
       
   434 		case '5': case '6': case '7': case '8': case '9':
       
   435 			n = 0;
       
   436 			do {
       
   437 				n = 10 * n + to_digit(ch);
       
   438 				ch = *fmt++;
       
   439 			} while (is_digit(ch));
       
   440 			width = n;
       
   441 			goto reswitch;
       
   442 		case 'L':
       
   443 			flags |= LONGDBL;
       
   444 			goto rflag;
       
   445 		case 'h':
       
   446 			flags |= SHORTINT;
       
   447 			goto rflag;
       
   448 		case 'l':
       
   449 			if (*fmt == 'l') {
       
   450 				fmt++;
       
   451 				flags |= QUADINT;
       
   452 			} else {
       
   453 				flags |= LONGINT;
       
   454 			}
       
   455 			goto rflag;
       
   456 		case 'q':
       
   457 			flags |= QUADINT;
       
   458 			goto rflag;
       
   459 		case 'c':
       
   460 			*(cp = buf) = (char)va_arg(ap, int);
       
   461 			size = 1;
       
   462 			sign = '\0';
       
   463 			break;
       
   464 		case 'D':
       
   465 			flags |= LONGINT;
       
   466 			/*FALLTHROUGH*/
       
   467 		case 'd':
       
   468 		case 'i':
       
   469 			_uquad = SARG();
       
   470 #ifndef _NO_LONGLONG
       
   471 			if ((quad_t)_uquad < 0)
       
   472 #else
       
   473 			if ((long) _uquad < 0)
       
   474 #endif
       
   475 			{
       
   476 
       
   477 #ifndef _NO_LONGLONG
       
   478 				_uquad = -_uquad;
       
   479 #else
       
   480 				_uquad = (u_long)(-(long)_uquad);
       
   481 #endif
       
   482 				sign = '-';
       
   483 			}
       
   484 			base = DEC;
       
   485 			goto number;
       
   486 
       
   487 		case 'e':
       
   488 		case 'E':
       
   489 		case 'f':
       
   490 		case 'g':
       
   491 		case 'G':
       
   492 			if (prec == -1) {
       
   493 				prec = DEFPREC;
       
   494 			} else if ((ch == 'g' || ch == 'G') && prec == 0) {
       
   495 				prec = 1;
       
   496 			}
       
   497 
       
   498 			if (flags & LONGDBL) {
       
   499 				_double = (double) va_arg(ap, long double);
       
   500 			} else {
       
   501 				_double = va_arg(ap, double);
       
   502 			}
       
   503 
       
   504 			/* do this before tricky precision changes */
       
   505 			if (isinf(_double)) {
       
   506 				if (_double < 0)
       
   507 					sign = '-';
       
   508 				cp = "Inf";
       
   509 				size = 3;
       
   510 				break;
       
   511 			}
       
   512 			if (isnan(_double)) {
       
   513 				cp = "NaN";
       
   514 				size = 3;
       
   515 				break;
       
   516 			}
       
   517 			flags |= FPT;
       
   518 #if 0
       
   519 			cp = cvt(data, _double, prec, flags, &softsign,
       
   520 				&expt, ch, &ndig);
       
   521 			if (ch == 'g' || ch == 'G') {
       
   522 				if (expt <= -4 || expt > prec)
       
   523 					ch = (ch == 'g') ? 'e' : 'E';
       
   524 				else
       
   525 					ch = 'g';
       
   526 			} 
       
   527 			if (ch <= 'e') {	/* 'e' or 'E' fmt */
       
   528 				--expt;
       
   529 				expsize = exponent(expstr, expt, ch);
       
   530 				size = expsize + ndig;
       
   531 				if (ndig > 1 || flags & ALT)
       
   532 					++size;
       
   533 			} else if (ch == 'f') {		/* f fmt */
       
   534 				if (expt > 0) {
       
   535 					size = expt;
       
   536 					if (prec || flags & ALT)
       
   537 						size += prec + 1;
       
   538 				} else	/* "0.X" */
       
   539 					size = prec + 2;
       
   540 			} else if (expt >= ndig) {	/* fixed g fmt */
       
   541 				size = expt;
       
   542 				if (flags & ALT)
       
   543 					++size;
       
   544 			} else
       
   545 				size = ndig + (expt > 0 ?
       
   546 					1 : 2 - expt);
       
   547 
       
   548 			if (softsign)
       
   549 				sign = '-';
       
   550 #else
       
   551 			if (_double < 0) {
       
   552 				_double = -_double;
       
   553 				sign = '-';
       
   554 			}
       
   555 			size = e32cvt(buf, BUF, _double, width, prec, flags, ch);
       
   556 			cp=buf;
       
   557 #endif
       
   558 			break;
       
   559 
       
   560 		case 'n':
       
   561 #ifndef _NO_LONGLONG
       
   562 			if (flags & QUADINT)
       
   563 				*va_arg(ap, quad_t *) = ret;
       
   564 			else 
       
   565 #endif
       
   566 			if (flags & LONGINT)
       
   567 				*va_arg(ap, long *) = ret;
       
   568 			else if (flags & SHORTINT)
       
   569 				*va_arg(ap, short *) = (short)ret;
       
   570 			else
       
   571 				*va_arg(ap, int *) = ret;
       
   572 			continue;	/* no output */
       
   573 		case 'O':
       
   574 			flags |= LONGINT;
       
   575 			/*FALLTHROUGH*/
       
   576 		case 'o':
       
   577 			_uquad = UARG();
       
   578 			base = OCT;
       
   579 			goto nosign;
       
   580 		case 'p':
       
   581 			/*
       
   582 			 * ``The argument shall be a pointer to void.  The
       
   583 			 * value of the pointer is converted to a sequence
       
   584 			 * of printable characters, in an implementation-
       
   585 			 * defined manner.''
       
   586 			 *	-- ANSI X3J11
       
   587 			 */
       
   588 			/* NOSTRICT */
       
   589 			_uquad = (u_long)va_arg(ap, void *);
       
   590 			base = HEX;
       
   591 			xdigs = "0123456789abcdef";
       
   592 			flags |= HEXPREFIX;
       
   593 			ch = 'x';
       
   594 			goto nosign;
       
   595 		case 's':
       
   596 			if ((cp = va_arg(ap, char *)) == NULL)
       
   597 				cp = "(null)";
       
   598 			if (prec >= 0) {
       
   599 				/*
       
   600 				 * can't use strlen; can only look for the
       
   601 				 * NUL in the first `prec' characters, and
       
   602 				 * strlen() will go further.
       
   603 				 */
       
   604 				char *p = (char *)memchr(cp, 0, prec);
       
   605 
       
   606 				if (p != NULL) {
       
   607 					size = p - cp;
       
   608 					if (size > prec)
       
   609 						size = prec;
       
   610 				} else
       
   611 					size = prec;
       
   612 			} else
       
   613 				size = strlen(cp);
       
   614 			sign = '\0';
       
   615 			break;
       
   616 		case 'U':
       
   617 			flags |= LONGINT;
       
   618 			/*FALLTHROUGH*/
       
   619 		case 'u':
       
   620 			_uquad = UARG();
       
   621 			base = DEC;
       
   622 			goto nosign;
       
   623 		case 'X':
       
   624 			xdigs = "0123456789ABCDEF";
       
   625 			goto hex;
       
   626 		case 'x':
       
   627 			xdigs = "0123456789abcdef";
       
   628 hex:			_uquad = UARG();
       
   629 			base = HEX;
       
   630 			/* leading 0x/X only if non-zero */
       
   631 			if (flags & ALT && _uquad != 0)
       
   632 				flags |= HEXPREFIX;
       
   633 
       
   634 			/* unsigned conversions */
       
   635 nosign:			sign = '\0';
       
   636 			/*
       
   637 			 * ``... diouXx conversions ... if a precision is
       
   638 			 * specified, the 0 flag will be ignored.''
       
   639 			 *	-- ANSI X3J11
       
   640 			 */
       
   641 number:			if ((dprec = prec) >= 0)
       
   642 				flags &= ~ZEROPAD;
       
   643 
       
   644 			/*
       
   645 			 * ``The result of converting a zero value with an
       
   646 			 * explicit precision of zero is no characters.''
       
   647 			 *	-- ANSI X3J11
       
   648 			 */
       
   649 			cp = buf + BUF;
       
   650 			if (_uquad != 0 || prec != 0) {
       
   651 				/*
       
   652 				 * Unsigned mod is hard, and unsigned mod
       
   653 				 * by a constant is easier than that by
       
   654 				 * a variable; hence this switch.
       
   655 				 */
       
   656 				switch (base) {
       
   657 				case OCT:
       
   658 					do {
       
   659 						*--cp = (char)to_char(_uquad & 7);
       
   660 						_uquad >>= 3;
       
   661 					} while (_uquad);
       
   662 					/* handle octal leading 0 */
       
   663 					if (flags & ALT && *cp != '0')
       
   664 						*--cp = '0';
       
   665 					break;
       
   666 
       
   667 				case DEC:
       
   668 					/* many numbers are 1 digit */
       
   669 					while (_uquad >= 10) {
       
   670 						*--cp = (char)to_char(_uquad % 10);
       
   671 						_uquad /= 10;
       
   672 					}
       
   673 					*--cp = (char)to_char(_uquad);
       
   674 					break;
       
   675 
       
   676 				case HEX:
       
   677 					do {
       
   678 						*--cp = xdigs[_uquad & 15];
       
   679 						_uquad >>= 4;
       
   680 					} while (_uquad);
       
   681 					break;
       
   682 
       
   683 				default:
       
   684 					cp = "bug in vfprintf: bad base";
       
   685 					size = strlen(cp);
       
   686 					goto skipsize;
       
   687 				}
       
   688 			}
       
   689 			/* Workaround GCC compiler bug: size = buf + BUF - cp */
       
   690 			{
       
   691 				char *temp = buf+BUF;
       
   692 				size = temp - cp;
       
   693 			}
       
   694 		skipsize:
       
   695 			break;
       
   696 		default:	/* "%?" prints ?, unless ? is NUL */
       
   697 			if (ch == '\0')
       
   698 				goto done;
       
   699 			/* pretend it was %c with argument ch */
       
   700 			cp = buf;
       
   701 			*cp = (char)ch;
       
   702 			size = 1;
       
   703 			sign = '\0';
       
   704 			break;
       
   705 		}
       
   706 
       
   707 		/*
       
   708 		 * All reasonable formats wind up here.  At this point, `cp'
       
   709 		 * points to a string which (if not flags&LADJUST) should be
       
   710 		 * padded out to `width' places.  If flags&ZEROPAD, it should
       
   711 		 * first be prefixed by any sign or other prefix; otherwise,
       
   712 		 * it should be blank padded before the prefix is emitted.
       
   713 		 * After any left-hand padding and prefixing, emit zeroes
       
   714 		 * required by a decimal [diouxX] precision, then print the
       
   715 		 * string proper, then emit zeroes required by any leftover
       
   716 		 * floating precision; finally, if LADJUST, pad with blanks.
       
   717 		 *
       
   718 		 * Compute actual size, so we know how much to pad.
       
   719 		 * size excludes decimal prec; realsz includes it.
       
   720 		 */
       
   721 		realsz = dprec > size ? dprec : size;
       
   722 		if (sign)
       
   723 			realsz++;
       
   724 		else if (flags & HEXPREFIX)
       
   725 			realsz+= 2;
       
   726 
       
   727 		/* right-adjusting blank padding */
       
   728 		if ((flags & (LADJUST|ZEROPAD)) == 0)
       
   729 			PAD(width - realsz, blanks);
       
   730 
       
   731 		/* prefix */
       
   732 		if (sign) {
       
   733 			PRINT(&sign, 1);
       
   734 		} else if (flags & HEXPREFIX) {
       
   735 			ox[0] = '0';
       
   736 			ox[1] = (char)ch;
       
   737 			PRINT(ox, 2);
       
   738 		}
       
   739 
       
   740 		/* right-adjusting zero padding */
       
   741 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
       
   742 			PAD(width - realsz, zeroes);
       
   743 
       
   744 		/* leading zeroes from decimal precision */
       
   745 		PAD(dprec - size, zeroes);
       
   746 
       
   747 		/* the string or number proper */
       
   748 		PRINT(cp, size);
       
   749 #if 0
       
   750 		} else {	/* glue together f_p fragments */
       
   751 			if (ch >= 'f') {	/* 'f' or 'g' */
       
   752 				if (_double == 0) {
       
   753 					/* kludge for __dtoa irregularity */
       
   754 					PRINT("0", 1);
       
   755 					if (expt < ndig || (flags & ALT) != 0) {
       
   756 						PRINT(decimal_point, 1);
       
   757 						PAD(ndig - 1, zeroes);
       
   758 					}
       
   759 				} else if (expt <= 0) {
       
   760 					PRINT("0", 1);
       
   761 					PRINT(decimal_point, 1);
       
   762 					PAD(-expt, zeroes);
       
   763 					PRINT(cp, ndig);
       
   764 				} else if (expt >= ndig) {
       
   765 					PRINT(cp, ndig);
       
   766 					PAD(expt - ndig, zeroes);
       
   767 					if (flags & ALT)
       
   768 						PRINT(".", 1);
       
   769 				} else {
       
   770 					PRINT(cp, expt);
       
   771 					cp += expt;
       
   772 					PRINT(".", 1);
       
   773 					PRINT(cp, ndig-expt);
       
   774 				}
       
   775 			} else {	/* 'e' or 'E' */
       
   776 				if (ndig > 1 || flags & ALT) {
       
   777 					ox[0] = *cp++;
       
   778 					ox[1] = '.';
       
   779 					PRINT(ox, 2);
       
   780 					if (_double || (flags & ALT) == 0) {
       
   781 						PRINT(cp, ndig-1);
       
   782 					} else	/* 0.[0..] */
       
   783 						/* __dtoa irregularity */
       
   784 						PAD(ndig - 1, zeroes);
       
   785 				} else	/* XeYYY */
       
   786 					PRINT(cp, 1);
       
   787 				PRINT(expstr, expsize);
       
   788 			}
       
   789 		}
       
   790 #endif
       
   791 		/* left-adjusting padding (always blank) */
       
   792 		if (flags & LADJUST)
       
   793 			PAD(width - realsz, blanks);
       
   794 
       
   795 		/* finally, adjust ret */
       
   796 		ret += width > realsz ? width : realsz;
       
   797 
       
   798 		FLUSH();	/* copy out the I/O vectors */
       
   799 	}
       
   800 done:
       
   801 	FLUSH();
       
   802 error:
       
   803 	return (__sferror(fp) ? EOF : ret);
       
   804 	/* NOTREACHED */
       
   805 }
       
   806 
       
   807 #if 0
       
   808 static char *
       
   809 cvt(struct _reent *data,double value,int ndigits, int flags, char *sign, int *decpt, int ch, int *length)
       
   810 {
       
   811 	int mode, dsgn;
       
   812 	char *digits, *bp, *rve;
       
   813 
       
   814 	if (ch == 'f') {
       
   815 		mode = 3;		/* ndigits after the decimal point */
       
   816 	} else {
       
   817 		/* To obtain ndigits after the decimal point for the 'e' 
       
   818 		 * and 'E' formats, round to ndigits + 1 significant 
       
   819 		 * figures.
       
   820 		 */
       
   821 		if (ch == 'e' || ch == 'E') {
       
   822 			ndigits++;
       
   823 		}
       
   824 		mode = 2;		/* ndigits significant digits */
       
   825 	}
       
   826 
       
   827 	if (value < 0) {
       
   828 		value = -value;
       
   829 		*sign = '-';
       
   830 	} else
       
   831 		*sign = '\000';
       
   832 	digits = _dtoa_r(data, value, mode, ndigits, decpt, &dsgn, &rve);
       
   833 	if ((ch != 'g' && ch != 'G') || flags & ALT) {	/* Print trailing zeros */
       
   834 		bp = digits + ndigits;
       
   835 		if (ch == 'f') {
       
   836 			if (*digits == '0' && value)
       
   837 				*decpt = -ndigits + 1;
       
   838 			bp += *decpt;
       
   839 		}
       
   840 		if (value == 0)	/* kludge for __dtoa irregularity */
       
   841 			rve = bp;
       
   842 		while (rve < bp)
       
   843 			*rve++ = '0';
       
   844 	}
       
   845 	*length = rve - digits;
       
   846 	return (digits);
       
   847 }
       
   848 
       
   849 static int
       
   850 exponent(char *p0, int exp, int fmtch)
       
   851 {
       
   852 	register char *p, *t;
       
   853 	char expbuf[MAXEXP];
       
   854 
       
   855 	p = p0;
       
   856 	*p++ = (char)fmtch;
       
   857 	if (exp < 0) {
       
   858 		exp = -exp;
       
   859 		*p++ = '-';
       
   860 	}
       
   861 	else
       
   862 		*p++ = '+';
       
   863 	t = expbuf + MAXEXP;
       
   864 	if (exp > 9) {
       
   865 		do {
       
   866 			*--t = (char)to_char(exp % 10);
       
   867 		} while ((exp /= 10) > 9);
       
   868 		*--t = (char)to_char(exp);
       
   869 		for (; t < expbuf + MAXEXP; *p++ = *t++);
       
   870 	}
       
   871 	else {
       
   872 		*p++ = '0';
       
   873 		*p++ = (char)to_char(exp);
       
   874 	}
       
   875 	return (p - p0);
       
   876 }
       
   877 #endif