📄 vfprintf.c
字号:
/* * Copyright (c) 1990, 2006 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. *//*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 vasprintf(char **<[strp]>, const char *<[fmt]>, va_list <[list]>); int vsnprintf(char *<[str]>, size_t <[size]>, const char *<[fmt]>, va_list <[list]>); int _vprintf_r(struct _reent *<[reent]>, const char *<[fmt]>, va_list <[list]>); int _vfprintf_r(struct _reent *<[reent]>, FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>); int _vasprintf_r(struct _reent *<[reent]>, char **<[str]>, const char *<[fmt]>, va_list <[list]>); int _vsprintf_r(struct _reent *<[reent]>, char *<[str]>, const char *<[fmt]>, va_list <[list]>); int _vsnprintf_r(struct _reent *<[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 vasprintf(<[strp]>, <[fmt]>, <[list]>) char **<[strp]>; 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]>) struct _reent *<[reent]>; char *<[fmt]>; va_list <[list]>; int _vfprintf_r(<[reent]>, <[fp]>, <[fmt]>, <[list]>) struct _reent *<[reent]>; FILE *<[fp]>; char *<[fmt]>; va_list <[list]>; int _vasprintf_r(<[reent]>, <[strp]>, <[fmt]>, <[list]>) struct _reent *<[reent]>; char **<[strp]>; char *<[fmt]>; va_list <[list]>; int _vsprintf_r(<[reent]>, <[str]>, <[fmt]>, <[list]>) struct _reent *<[reent]>; char *<[str]>; char *<[fmt]>; va_list <[list]>; int _vsnprintf_r(<[reent]>, <[str]>, <[size]>, <[fmt]>, <[list]>) struct _reent *<[reent]>; char *<[str]>; size_t <[size]>; char *<[fmt]>; va_list <[list]>;DESCRIPTION<<vprintf>>, <<vfprintf>>, <<vasprintf>>, <<vsprintf>> and <<vsnprintf>> are (respectively) variants of <<printf>>, <<fprintf>>, <<asprintf>>, <<sprintf>>,and <<snprintf>>. They differ only in allowing their caller to pass the variable argument list as a <<va_list>> object (initialized by <<va_start>>) rather than directly accepting a variable number of arguments.RETURNSThe return values are consistent with the corresponding functions:<<vasprintf>>/<<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>> and<<vasprintf>> returns -1. No error returns occur for <<vsprintf>>.PORTABILITYANSI C requires all three functions.Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,<<lseek>>, <<read>>, <<sbrk>>, <<write>>.*/#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.43 2002/08/13 02:40:06 fitzsim Exp $";#endif /* LIBC_SCCS and not lint *//* * Actual printf innards. * * This code is large and complicated... */#include <newlib.h>#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#define _NO_POS_ARGS #if defined _WANT_IO_POS_ARGS# undef _NO_POS_ARGS#endif#include <_ansi.h>#include <reent.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <stdint.h>#include <wchar.h>#include <string.h>#include <sys/lock.h>#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/* * Flush out all the vectors defined by the given uio, * then reset it so that it can be reused. */static int_DEFUN(__sprint_r, (ptr, fp, uio), struct _reent *ptr _AND FILE *fp _AND register struct __suio *uio){ register int err; if (uio->uio_resid == 0) { uio->uio_iovcnt = 0; return (0); } err = __sfvwrite_r(ptr, 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_DEFUN(__sbprintf, (rptr, fp, fmt, ap), struct _reent *rptr _AND register FILE *fp _AND _CONST char *fmt _AND 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 */#ifndef __SINGLE_THREAD__ __lock_init_recursive (fake._lock);#endif /* 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;#ifndef __SINGLE_THREAD__ __lock_close_recursive (fake._lock);#endif return (ret);}#ifdef FLOATING_POINT#include <locale.h>#include <math.h>#include "floatio.h"#if ((MAXEXP+MAXFRACT+1) > MB_LEN_MAX)# define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */#else # define BUF MB_LEN_MAX#endif#define DEFPREC 6#ifdef _NO_LONGDBLstatic char *_EXFUN(cvt, (struct _reent *, double, int, int, char *, int *, int, int *));#elsestatic char *_EXFUN(cvt, (struct _reent *, _LONG_DOUBLE, int, int, char *, int *, int, int *));extern int _EXFUN(_ldcheck,(_LONG_DOUBLE *));#endifstatic int _EXFUN(exponent, (char *, int, int));#else /* no FLOATING_POINT */#define BUF 40#endif /* FLOATING_POINT */#ifndef _NO_LONGLONG#define quad_t long long#define u_quad_t unsigned long long#else#define quad_t long#define u_quad_t unsigned long#endiftypedef quad_t * quad_ptr_t;typedef _PTR void_ptr_t;typedef char * char_ptr_t;typedef long * long_ptr_t;typedef int * int_ptr_t;typedef short * short_ptr_t;#ifndef _NO_POS_ARGS#define MAX_POS_ARGS 32union arg_val{ int val_int; u_int val_u_int; long val_long; u_long val_u_long; float val_float; double val_double; _LONG_DOUBLE val__LONG_DOUBLE; int_ptr_t val_int_ptr_t; short_ptr_t val_short_ptr_t; long_ptr_t val_long_ptr_t; char_ptr_t val_char_ptr_t; quad_ptr_t val_quad_ptr_t; void_ptr_t val_void_ptr_t; quad_t val_quad_t; u_quad_t val_u_quad_t; wint_t val_wint_t;};static union arg_val *_EXFUN(get_arg, (struct _reent *data, int n, char *fmt, va_list *ap, int *numargs, union arg_val *args, int *arg_type, char **last_fmt));#endif /* !_NO_POS_ARGS *//* * 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 CHARINT 0x200 /* char as integer */int _EXFUN(_VFPRINTF_R, (struct _reent *, FILE *, _CONST char *, va_list));int _DEFUN(VFPRINTF, (fp, fmt0, ap), FILE * fp _AND _CONST char *fmt0 _AND va_list ap){ int result; result = _VFPRINTF_R (_REENT, fp, fmt0, ap); return result;}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 */ char *fmt_anchor; /* current format spec being processed */ int N; /* arg number */ int arg_index; /* index into args processed directly */#ifndef _NO_POS_ARGS int numargs; /* number of varargs read */ char *saved_fmt; /* saved fmt pointer */ union arg_val args[MAX_POS_ARGS]; int arg_type[MAX_POS_ARGS]; int is_pos_arg; /* is current format positional? */ int old_is_pos_arg; /* is current format positional? */#endif 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) */#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 = 0; /* actual number of digits returned by cvt */ char expstr[7]; /* buffer for exponent string */#endif u_quad_t _uquad; /* integer arguments %[diouxX] */ 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 _MB_CAPABLE wchar_t wc; mbstate_t state; /* mbtowc calls from library must not change state */#endif char *malloc_buf = NULL;/* handy pointer for malloced buffers */ /* * 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'};#ifdef _MB_CAPABLE memset (&state, '\0', sizeof (state));#endif /* * BEWARE, these `goto error' on error, and PAD uses `n'. */#define PRINT(ptr, len) { \ iovp->iov_base = (ptr); \ iovp->iov_len = (len); \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -