int vasprintf_l(char **str, locale_t locale, const char *fmt, __va_list ap) { FILE f = FAKE_FILE; int ret; FIX_LOCALE(locale); f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = malloc(128); if (f._bf._base == NULL) { *str = NULL; errno = ENOMEM; return (-1); } f._bf._size = f._w = 127; /* Leave room for the NUL */ ret = __vfprintf(&f, locale, fmt, ap); if (ret < 0) { free(f._bf._base); *str = NULL; errno = ENOMEM; return (-1); } *f._p = '\0'; *str = (char *)f._bf._base; return (ret); }
EXPORT_C int vasprintf(char **str, const char *fmt, va_list ap) { int ret; FILE f; struct __sFILEX ext; f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) { *str = NULL; errno = ENOMEM; return (-1); } f._bf._size = f._w = 127; /* Leave room for the NUL */ f._extra = &ext; INITEXTRA(&f); ret = __vfprintf(&f, fmt, ap); if (ret < 0) { free(f._bf._base); *str = NULL; errno = ENOMEM; return (-1); } *f._p = '\0'; *str = (char *)f._bf._base; return (ret); }
/* * 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(FILE *fp, const char *fmt, va_list ap) { int ret; FILE fake; struct __sfileext fakeext; unsigned char buf[BUFSIZ]; _FILEEXT_SETUP(&fake, &fakeext); /* 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(&fake, fmt, ap); if (ret >= 0 && __sflush(&fake)) ret = EOF; if (fake._flags & __SERR) fp->_flags |= __SERR; return (ret); }
/* * 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(FILE *fp, const char *fmt, va_list ap) { int ret; FILE fake = FAKE_FILE; unsigned char buf[BUFSIZ]; /* XXX This is probably not needed. */ if (prepwrite(fp) != 0) return (EOF); /* copy the important variables */ fake._flags = fp->_flags & ~__SNBF; fake._file = fp->_file; fake._cookie = fp->_cookie; fake._write = fp->_write; fake._orientation = fp->_orientation; fake._mbstate = fp->_mbstate; /* 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); }
int snprintf(char *str, size_t n, const char *fmt, ...) { va_list ap; int ret; char dummy; FILE f; struct __sfileext fext; /* While snprintf(3) specifies size_t stdio uses an int internally */ if (n > INT_MAX) n = INT_MAX; /* Stdio internals do not deal correctly with zero length buffer */ if (n == 0) { str = &dummy; n = 1; } _FILEEXT_SETUP(&f, &fext); f._file = -1; f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *)str; f._bf._size = f._w = n - 1; va_start(ap, fmt); ret = __vfprintf(&f, fmt, ap); va_end(ap); *f._p = '\0'; return (ret); }
int vasprintf_l(char **str, locale_t locale, const char *fmt, __va_list ap) { int ret; FILE f; struct __sfileext fext; unsigned char *_base; FIX_LOCALE(locale); _FILEEXT_SETUP(&f, &fext); f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) goto err; f._bf._size = f._w = 127; /* Leave room for the NUL */ ret = __vfprintf(&f, locale, fmt, ap); if (ret == -1) goto err; *f._p = '\0'; _base = realloc(f._bf._base, ret + 1); if (_base == NULL) goto err; *str = (char *)_base; return (ret); err: if (f._bf._base) { free(f._bf._base); f._bf._base = NULL; } *str = NULL; errno = ENOMEM; return (-1); }
int asprintf(char **str, const char *fmt, ...) { int ret; va_list ap; FILE f; unsigned char *_base; f._file = -1; f._flags = __SWR | __SSTR | __SALC; f._bf._base = f._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) goto err; f._bf._size = f._w = 127; /* Leave room for the NUL */ va_start(ap, fmt); ret = __vfprintf(&f, fmt, ap); va_end(ap); if (ret == -1) goto err; *f._p = '\0'; _base = realloc(f._bf._base, ret + 1); if (_base == NULL) goto err; *str = (char *)_base; return (ret); err: free(f._bf._base); *str = NULL; errno = ENOMEM; return (-1); }
int vasprintf(char **str, const char *fmt, __va_list ap) { int ret; FILE f; f.pub._fileno = -1; f.pub._flags = __SWR | __SSTR | __SALC; f._bf._base = f.pub._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) { *str = NULL; errno = ENOMEM; return (-1); } f._bf._size = f.pub._w = 127; /* Leave room for the NUL */ memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data)); ret = __vfprintf(&f, fmt, ap); if (ret < 0) { free(f._bf._base); *str = NULL; errno = ENOMEM; return (-1); } *f.pub._p = '\0'; *str = (char *)f._bf._base; return (ret); }
int __printf (const char* format, ...) { va_list args; va_start(args, format); int res = __vfprintf(stdout, format, args); va_end(args); return res; }
int vfprintf(FILE *stream,const char *format,va_list args) { size_t outcount=0; /* optimize unbuffered write-only files */ if((stream->flags&(__SWO|__SNBF))==(__SWO|__SNBF)) return __vfprintf(stream,format,args); while(*format) { if(*format=='%') { static const char flagc[]= { '#','0','-',' ','+' }; static const char lowertabel[]= { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; static const char uppertabel[]= { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; size_t width=0,preci=ULONG_MAX,flags=0; /* Specifications */ char type,subtype='i'; char buffer1[2]; /* Signs and that like */ char buffer[REQUIREDBUFFER]; /* The body */ char *buffer2=buffer; /* So we can set this to any other strings */ size_t size1=0,size2=0; /* How many chars in buffer? */ const char *ptr=format+1; /* pointer to format string */ size_t i,pad; /* Some temporary variables */ do /* read flags */ for(i=0;i<sizeof(flagc);i++) if(flagc[i]==*ptr) { flags|=1<<i; ptr++; break; } while(i<sizeof(flagc)); if(*ptr=='*') /* read width from arguments */ { signed int a; ptr++; a=va_arg(args,signed int); if(a<0) { flags|=LALIGNFLAG; width=-a; } else width=a; }else while(isdigit(*ptr)) width=width*10+(*ptr++-'0'); if(*ptr=='.') { ptr++; if(*ptr=='*') /* read precision from arguments */ { signed int a; ptr++; a=va_arg(args,signed int); if(a>=0) preci=a; }else
int vfprintf(FILE *fp, const char *fmt0, __va_list ap) { int ret; FLOCKFILE(fp); ret = __vfprintf(fp, fmt0, ap); FUNLOCKFILE(fp); return (ret); }
/* * MT-safe version */ int vfprintf_l(FILE *fp, locale_t locale, const char *fmt0, __va_list ap) { int ret; FIX_LOCALE(locale); FLOCKFILE(fp); /* optimise fprintf(stderr) (and other unbuffered Unix files) */ if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && fp->_file >= 0) ret = __sbprintf(fp, locale, fmt0, ap); else ret = __vfprintf(fp, locale, fmt0, ap); FUNLOCKFILE(fp); return (ret); }
static char sccsid[] = "@(#)vsprintf.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include <sys/cdefs.h> __FBSDID("$FreeBSD: releng/10.2/lib/libc/stdio/vsprintf.c 249808 2013-04-23 13:33:13Z emaste $"); #include <stdio.h> #include <limits.h> #include "local.h" #include "xlocale_private.h" int vsprintf_l(char * __restrict str, locale_t locale, const char * __restrict fmt, __va_list ap) { int ret; FILE f = FAKE_FILE; FIX_LOCALE(locale); f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *)str; f._bf._size = f._w = INT_MAX; ret = __vfprintf(&f, locale, fmt, ap); *f._p = 0; return (ret); } int vsprintf(char * __restrict str, const char * __restrict fmt, __va_list ap) { return vsprintf_l(str, __get_locale(), fmt, ap); }
* * @(#)sprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/sprintf.c,v 1.16 2008/04/17 22:17:54 jhb Exp $ * $DragonFly: src/lib/libc/stdio/sprintf.c,v 1.7 2006/03/02 18:05:30 joerg Exp $ */ #include <stdio.h> #include <stdarg.h> #include <limits.h> #include "local.h" #include "priv_stdio.h" int sprintf(char * __restrict str, const char * __restrict fmt, ...) { int ret; va_list ap; FILE f; f.pub._fileno = -1; f.pub._flags = __SWR | __SSTR; f._bf._base = f.pub._p = (unsigned char *)str; f._bf._size = f.pub._w = INT_MAX; memset(WCIO_GET(&f), 0, sizeof(struct wchar_io_data)); va_start(ap, fmt); ret = __vfprintf(&f, fmt, ap); va_end(ap); *f.pub._p = 0; return (ret); }
int __vprintf (const char* format, va_list args) { return __vfprintf(stdout, format, args); }