📄 vfprintf.c
字号:
/*FUNCTION<<vprintf>>, <<vfprintf>>, <<vsprintf>>---format argument listINDEX vprintfINDEX vfprintfINDEX vsprintfINDEX vsnprintfANSI_SYNOPSIS #include <stdio.h> #include <stdarg.h> int vprintf(const char *<[fmt]>, va_list <[list]>); int vfprintf(FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>); int vsprintf(char *<[str]>, const char *<[fmt]>, va_list <[list]>); int vsnprintf(char *<[str]>, size_t <[size]>, const char *<[fmt]>, va_list <[list]>); int _vprintf_r(void *<[reent]>, const char *<[fmt]>, va_list <[list]>); int _vfprintf_r(void *<[reent]>, FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>); int _vsprintf_r(void *<[reent]>, char *<[str]>, const char *<[fmt]>, va_list <[list]>); int _vsnprintf_r(void *<[reent]>, char *<[str]>, size_t <[size]>, const char *<[fmt]>, va_list <[list]>);TRAD_SYNOPSIS #include <stdio.h> #include <varargs.h> int vprintf( <[fmt]>, <[list]>) char *<[fmt]>; va_list <[list]>; int vfprintf(<[fp]>, <[fmt]>, <[list]>) FILE *<[fp]>; char *<[fmt]>; va_list <[list]>; int vsprintf(<[str]>, <[fmt]>, <[list]>) char *<[str]>; char *<[fmt]>; va_list <[list]>; int vsnprintf(<[str]>, <[size]>, <[fmt]>, <[list]>) char *<[str]>; size_t <[size]>; char *<[fmt]>; va_list <[list]>; int _vprintf_r(<[reent]>, <[fmt]>, <[list]>) char *<[reent]>; char *<[fmt]>; va_list <[list]>; int _vfprintf_r(<[reent]>, <[fp]>, <[fmt]>, <[list]>) char *<[reent]>; FILE *<[fp]>; char *<[fmt]>; va_list <[list]>; int _vsprintf_r(<[reent]>, <[str]>, <[fmt]>, <[list]>) char *<[reent]>; char *<[str]>; char *<[fmt]>; va_list <[list]>; int _vsnprintf_r(<[reent]>, <[str]>, <[size]>, <[fmt]>, <[list]>) char *<[reent]>; char *<[str]>; size_t <[size]>; char *<[fmt]>; va_list <[list]>;DESCRIPTION<<vprintf>>, <<vfprintf>>, <<vsprintf>> and <<vsnprintf>> are (respectively)variants of <<printf>>, <<fprintf>>, <<sprintf>> and <<snprintf>>. They differonly in allowing their caller to pass the variable argument list as a<<va_list>> object (initialized by <<va_start>>) rather than directlyaccepting a variable number of arguments.RETURNSThe return values are consistent with the corresponding functions:<<vsprintf>> returns the number of bytes in the output string,save that the concluding <<NULL>> is not counted.<<vprintf>> and <<vfprintf>> return the number of characters transmitted.If an error occurs, <<vprintf>> and <<vfprintf>> return <<EOF>>. Noerror returns occur for <<vsprintf>>.PORTABILITYANSI C requires all three functions.Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,<<lseek>>, <<read>>, <<sbrk>>, <<write>>.*//*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#if defined(LIBC_SCCS) && !defined(lint)/*static char *sccsid = "from: @(#)vfprintf.c 5.50 (Berkeley) 12/16/92";*/static char *rcsid = "$Id: vfprintf.c,v 1.13 2007/03/29 06:25:44 nickc Exp $";#endif /* LIBC_SCCS and not lint *//* * Actual printf innards. * * This code is large and complicated... */#ifdef INTEGER_ONLY#define VFPRINTF vfiprintf#define _VFPRINTF_R _vfiprintf_r#else#define VFPRINTF vfprintf#define _VFPRINTF_R _vfprintf_r#ifndef NO_FLOATING_POINT#define FLOATING_POINT#endif#endif#include <_ansi.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <reent.h>#include <wchar.h>#include <string.h>#ifdef __ALTIVEC__#include <altivec.h>#endif#ifdef _HAVE_STDC#include <stdarg.h>#else#include <varargs.h>#endif#include "local.h"#include "fvwrite.h"#include "vfieeefp.h"/* Currently a test is made to see if long double processing is warranted. This could be changed in the future should the _ldtoa_r code be preferred over _dtoa_r. */#define _NO_LONGDBL#if defined _WANT_IO_LONG_DOUBLE && (LDBL_MANT_DIG > DBL_MANT_DIG)#undef _NO_LONGDBL#endif#define _NO_LONGLONG#if defined _WANT_IO_LONG_LONG && defined __GNUC__# undef _NO_LONGLONG#endif#ifdef __ALTIVEC__typedef union{ vector int v; float f[4]; int i[16 / sizeof(int)]; long l[4]; short s[8]; signed char c[16];} vec_16_byte_union;#endif /* __ALTIVEC__ *//* * Flush out all the vectors defined by the given uio, * then reset it so that it can be reused. */static int__sprint_r(rptr, fp, uio) struct _reent *rptr; FILE *fp; register struct __suio *uio;{ register int err; if (uio->uio_resid == 0) { uio->uio_iovcnt = 0; return (0); } err = __sfvwrite_r(rptr, fp, uio); uio->uio_resid = 0; uio->uio_iovcnt = 0; return (err);}/* * Helper function for `fprintf to unbuffered unix file': creates a * temporary buffer. We only work on write-only files; this avoids * worries about ungetc buffers and so forth. */static int__sbprintf_r(rptr, fp, fmt, ap) struct _reent *rptr; register FILE *fp; const char *fmt; va_list ap;{ int ret; FILE fake; unsigned char buf[BUFSIZ]; /* copy the important variables */ fake._flags = fp->_flags & ~__SNBF; fake._file = fp->_file; fake._cookie = fp->_cookie; fake._write = fp->_write; /* set up the buffer */ fake._bf._base = fake._p = buf; fake._bf._size = fake._w = sizeof(buf); fake._lbfsize = 0; /* not actually used, but Just In Case */ /* do the work, then copy any error status */ ret = _VFPRINTF_R(rptr, &fake, fmt, ap); if (ret >= 0 && fflush(&fake)) ret = EOF; if (fake._flags & __SERR) fp->_flags |= __SERR; return (ret);}#ifdef FLOATING_POINT#include <locale.h>#include <math.h>#include "floatio.h"#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */#define DEFPREC 6#ifdef _NO_LONGDBLstatic char *cvt _PARAMS((struct _reent *, double, int, int, char *, int *, int, int *));#elsestatic char *cvt _PARAMS((struct _reent *, _LONG_DOUBLE, int, int, char *, int *, int, int *));extern int _ldcheck _PARAMS((_LONG_DOUBLE *));#endifstatic int exponent _PARAMS((char *, int, int));#ifdef __SPE__static char *cvt_ufix64 _PARAMS((struct _reent *, unsigned long long, int, int *, int *));#endif /* __SPE__ */#else /* no FLOATING_POINT */#define BUF 40#endif /* FLOATING_POINT *//* * Macros for converting digits to letters and vice versa */#define to_digit(c) ((c) - '0')#define is_digit(c) ((unsigned)to_digit(c) <= 9)#define to_char(n) ((n) + '0')/* * Flags used during conversion. */#define ALT 0x001 /* alternate form */#define HEXPREFIX 0x002 /* add 0x or 0X prefix */#define LADJUST 0x004 /* left adjustment */#define LONGDBL 0x008 /* long double */#define LONGINT 0x010 /* long integer */#ifndef _NO_LONGLONG#define QUADINT 0x020 /* quad integer */#else /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so that %lld behaves the same as %ld, not as %d, as expected if: sizeof (long long) = sizeof long > sizeof int */#define QUADINT LONGINT#endif#define SHORTINT 0x040 /* short integer */#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */#define FPT 0x100 /* Floating point number */#define VECTOR 0x200 /* vector */#define FIXEDPOINT 0x400 /* fixed-point */int _DEFUN (VFPRINTF, (fp, fmt0, ap), FILE * fp _AND _CONST char *fmt0 _AND va_list ap){ CHECK_INIT (_REENT, fp); return _VFPRINTF_R (_REENT, fp, fmt0, ap);}int _DEFUN (_VFPRINTF_R, (data, fp, fmt0, ap), struct _reent *data _AND FILE * fp _AND _CONST char *fmt0 _AND va_list ap){ register char *fmt; /* format string */ register int ch; /* character from fmt */ register int n, m; /* handy integers (short term usage) */ register char *cp; /* handy char pointer (short term usage) */ register struct __siov *iovp;/* for PRINT macro */ register int flags; /* flags as above */ int ret; /* return value accumulator */ int width; /* width from format (%8d), or 0 */ int prec; /* precision from format (%.3d), or -1 */ char sign; /* sign prefix (' ', '+', '-', or \0) */ char old_sign; /* saved value of sign when looping for vectors */ int old_ch; /* saved value of ch when looping for vectors */ char *format_anchor; /* start of format to process */ wchar_t wc;#ifdef FLOATING_POINT char *decimal_point = localeconv()->decimal_point; char softsign; /* temporary negative sign for floats */#ifdef _NO_LONGDBL union { int i; double d; } _double_ = {0}; #define _fpvalue (_double_.d)#else union { int i; _LONG_DOUBLE ld; } _long_double_ = {0}; #define _fpvalue (_long_double_.ld) int tmp; #endif int expt; /* integer value of exponent */ int expsize = 0; /* character count for expstr */ int ndig; /* actual number of digits returned by cvt */ char expstr[7]; /* buffer for exponent string */#endif#ifndef _NO_LONGLONG#define quad_t long long#define u_quad_t unsigned long long#endif#ifndef _NO_LONGLONG u_quad_t _uquad; /* integer arguments %[diouxX] */#else u_long _uquad;#endif enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ int dprec; /* a copy of prec if [diouxX], 0 otherwise */ int realsz; /* field size expanded by dprec */ int size; /* size of converted field or string */ char *xdigs = NULL; /* digits for [xX] conversion */#define NIOV 8 struct __suio uio; /* output information: summary */ struct __siov iov[NIOV];/* ... and individual io vectors */ char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ char ox[2]; /* space for 0x hex-prefix */#ifdef __ALTIVEC__ char vec_sep; /* vector separator char */ int vec_print_count; /* number of vector chunks remaining */ vec_16_byte_union vec_tmp;#endif /* __ALTIVEC__ */ mbstate_t state; /* mbtowc calls from library must not change state */ /* * Choose PADSIZE to trade efficiency vs. size. If larger printf * fields occur frequently, increase PADSIZE and make the initialisers * below longer. */#define PADSIZE 16 /* pad chunk size */ static _CONST char blanks[PADSIZE] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; static _CONST char zeroes[PADSIZE] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; /* * BEWARE, these `goto error' on error, and PAD uses `n'. */#define PRINT(ptr, len) { \ iovp->iov_base = (ptr); \ iovp->iov_len = (len); \ uio.uio_resid += (len); \ iovp++; \ if (++uio.uio_iovcnt >= NIOV) { \ if (__sprint_r(data, fp, &uio)) \ goto error; \ iovp = iov; \ } \}#define PAD(howmany, with) { \ if ((n = (howmany)) > 0) { \ while (n > PADSIZE) { \ PRINT(with, PADSIZE); \ n -= PADSIZE; \ } \ PRINT(with, n); \ } \}#define FLUSH() { \ if (uio.uio_resid && __sprint_r(data, fp, &uio)) \ goto error; \ uio.uio_iovcnt = 0; \ iovp = iov; \}#ifdef __ALTIVEC__#define GET_SHORT(ap) \ (flags&VECTOR ? \ (vec_print_count < 8 ? (short)vec_tmp.s[8 - vec_print_count] : \ (vec_tmp.v = va_arg(ap, vector int), (short)vec_tmp.s[0])) : \ (short)va_arg(ap, int))#define GET_USHORT(ap) \ (flags&VECTOR ? \ (vec_print_count < 8 ? (u_short)vec_tmp.s[8 - vec_print_count] : \ (vec_tmp.v = va_arg(ap, vector int), (u_short)vec_tmp.s[0])) : \ (u_short)va_arg(ap, int))#define GET_LONG(ap) \ (flags&VECTOR ? \ (vec_print_count < 4 ? (long)vec_tmp.l[4 - vec_print_count] : \ (vec_tmp.v = va_arg(ap, vector int), vec_tmp.l[0])) : \ va_arg(ap, long int))#define GET_ULONG(ap) \ (flags&VECTOR ? \ (vec_print_count < 4 ? (u_long)vec_tmp.l[4 - vec_print_count] : \ (vec_tmp.v = va_arg(ap, vector int), (u_long)vec_tmp.l[0])) : \ (u_long)va_arg(ap, unsigned long int))#define GET_INT(ap) \ (flags&VECTOR ? \ (vec_print_count < 16 ? \ vec_tmp.c[16 - vec_print_count] : \ (vec_tmp.v = va_arg(ap, vector int), (int)vec_tmp.c[0])) : \ va_arg(ap, int))#define GET_UINT(ap) \ (flags&VECTOR ? \ (vec_print_count < 16 ? \ (u_int)((unsigned char)vec_tmp.c[16 - vec_print_count]) : \ (vec_tmp.v = va_arg(ap, vector int), (u_int)((unsigned char)vec_tmp.c[0]))) : \ (u_int)va_arg(ap, unsigned int))#else /* !__ALTIVEC__ */#define GET_SHORT(ap) ((short)va_arg(ap, int))#define GET_USHORT(ap) ((u_short)va_arg(ap, int))#define GET_LONG(ap) (va_arg(ap, long int))#define GET_ULONG(ap) ((u_long)va_arg(ap, unsigned long int))#define GET_INT(ap) ((int)va_arg(ap, int))#define GET_UINT(ap) ((u_int)va_arg(ap, unsigned int))#endif /* !__ALTIVEC__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -