📄 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 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(void *<[reent]>, const char *<[fmt]>, va_list <[list]>); int _vfprintf_r(void *<[reent]>, FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>); int _vasprintf_r(void *<[reent]>, char **<[str]>, 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 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]>) char *<[reent]>; char *<[fmt]>; va_list <[list]>; int _vfprintf_r(<[reent]>, <[fp]>, <[fmt]>, <[list]>) char *<[reent]>; FILE *<[fp]>; char *<[fmt]>; va_list <[list]>; int _vasprintf_r(<[reent]>, <[strp]>, <[fmt]>, <[list]>) char *<[reent]>; char **<[strp]>; 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>>, <<vasprintf>>, <<vsprintf>> and <<vsnprintf>> are (respectively) variants of <<printf>>, <<fprintf>>, <<saprintf>>, <<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>>.*//* * 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.43 2002/08/13 02:40:06 fitzsim 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#define _NO_LONGLONG#if defined WANT_PRINTF_LONG_LONG && defined __GNUC__# undef _NO_LONGLONG#endif#define _NO_POS_ARGS #if defined WANT_IO_POS_ARGS# undef _NO_POS_ARGS#endif#include <_ansi.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <reent.h>#include <wchar.h>#include <string.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_DBL && (LDBL_MANT_DIG > DBL_MANT_DIG)#undef _NO_LONGDBL#endif/* * Flush out all the vectors defined by the given uio, * then reset it so that it can be reused. */static int__sprint(fp, uio) FILE *fp; register struct __suio *uio;{ register int err; if (uio->uio_resid == 0) { uio->uio_iovcnt = 0; return (0); } err = __sfvwrite(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(fp, fmt, ap) register FILE *fp; const char *fmt; va_list ap;{ int ret; FILE fake; unsigned char buf[BUFSIZ]; /* copy the important variables */ fake._data = fp->_data; 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(&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));#else /* no FLOATING_POINT */#define BUF 40#endif /* FLOATING_POINT */#ifndef _NO_LONG_LONG#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 void * 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;};static union arg_val *get_arg (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 */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; _flockfile(fp); CHECK_INIT (fp); result = _VFPRINTF_R (fp->_data, fp, fmt0, ap); _funlockfile(fp); 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) */ 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 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 */ 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'}; memset (&state, '\0', sizeof (state)); /* * 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(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(fp, &uio)) \ goto error; \ uio.uio_iovcnt = 0; \ iovp = iov; \} /* Macros to support positional arguments */#ifndef _NO_POS_ARGS#define GET_ARG(n, ap, type) \ ( is_pos_arg \ ? n < numargs \ ? args[n].val_##type \ : get_arg (n, fmt_anchor, &ap, &numargs, args, arg_type, &saved_fmt)->val_##type \ : arg_index++ < numargs \ ? args[n].val_##type \ : numargs < MAX_POS_ARGS \ ? args[numargs++].val_##type = va_arg(ap, type) \ : va_arg(ap, type) \ )#else#define GET_ARG(n, ap, type) (va_arg(ap, type))#endif /* * To extend shorts properly, we need both signed and unsigned * argument extraction methods. */#ifndef _NO_LONGLONG#define SARG() \ (flags&QUADINT ? GET_ARG(N, ap, quad_t) : \ flags&LONGINT ? GET_ARG(N, ap, long) : \ flags&SHORTINT ? (long)(short)GET_ARG(N, ap, int) : \ (long)GET_ARG(N, ap, int))#define UARG() \ (flags&QUADINT ? GET_ARG(N, ap, u_quad_t) : \ flags&LONGINT ? GET_ARG(N, ap, u_long) : \ flags&SHORTINT ? (u_long)(u_short)GET_ARG(N, ap, int) : \ (u_long)GET_ARG(N, ap, u_int))#else#define SARG() \ (flags&LONGINT ? GET_ARG(N, ap, long) : \ flags&SHORTINT ? (long)(short)GET_ARG(N, ap, int) : \ (long)GET_ARG(N, ap, int))#define UARG() \ (flags&LONGINT ? GET_ARG(N, ap, u_long) : \ flags&SHORTINT ? (u_long)(u_short)GET_ARG(N, ap, int) : \ (u_long)GET_ARG(N, ap, u_int))#endif /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ if (cantwrite(fp)) return (EOF); /* optimise fprintf(stderr) (and other unbuffered Unix files) */ if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && fp->_file >= 0) return (__sbprintf(fp, fmt0, ap)); fmt = (char *)fmt0; uio.uio_iov = iovp = iov; uio.uio_resid = 0; uio.uio_iovcnt = 0; ret = 0; arg_index = 0;#ifndef _NO_POS_ARGS saved_fmt = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -