add asprintf and vasprintf, originally written by Todd Miller for OpenBSD
This commit is contained in:
parent
3d5e078fe9
commit
253ef37df2
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: stdio.h,v 1.26 1998/07/30 00:44:16 mycroft Exp $ */
|
||||
/* $NetBSD: stdio.h,v 1.27 1998/08/28 21:37:12 perry Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
|
@ -163,6 +163,7 @@ __END_DECLS
|
|||
#define __SNPT 0x0800 /* do not do fseek() optimisation */
|
||||
#define __SOFF 0x1000 /* set iff _offset is in fact correct */
|
||||
#define __SMOD 0x2000 /* true => fgetln modified _p text */
|
||||
#define __SALC 0x4000 /* allocate string space dynamically */
|
||||
|
||||
/*
|
||||
* The following three definitions are for ANSI C, which took them
|
||||
|
@ -327,10 +328,14 @@ __END_DECLS
|
|||
#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
|
||||
!defined(_XOPEN_SOURCE)
|
||||
__BEGIN_DECLS
|
||||
int asprintf __P((char **, const char *, ...))
|
||||
__attribute__((format (printf, 2, 3)));
|
||||
char *fgetln __P((FILE *, size_t *));
|
||||
int fpurge __P((FILE *));
|
||||
void setbuffer __P((FILE *, char *, int));
|
||||
int setlinebuf __P((FILE *));
|
||||
int vasprintf __P((char **, const char *, _BSD_VA_LIST_))
|
||||
__attribute__((format (printf, 2, 0)));
|
||||
int vscanf __P((const char *, _BSD_VA_LIST_))
|
||||
__attribute__((__format__(__scanf__, 1, 0)));
|
||||
int vsscanf __P((const char *, const char *, _BSD_VA_LIST_))
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
|
||||
# $NetBSD: Makefile.inc,v 1.16 1998/07/27 16:41:40 mycroft Exp $
|
||||
# $NetBSD: Makefile.inc,v 1.17 1998/08/28 21:33:10 perry Exp $
|
||||
|
||||
# stdio sources
|
||||
.PATH: ${.CURDIR}/stdio
|
||||
|
||||
CPPFLAGS+=-DFLOATING_POINT
|
||||
|
||||
SRCS+= clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c fgetln.c \
|
||||
fgetpos.c fgets.c fileno.c findfp.c flags.c fopen.c fprintf.c \
|
||||
fpurge.c fputc.c fputs.c fread.c freopen.c fscanf.c fseek.c fsetpos.c \
|
||||
ftell.c funopen.c fvwrite.c fwalk.c fwrite.c getc.c getchar.c \
|
||||
gettemp.c getw.c makebuf.c mkdtemp.c mkstemp.c perror.c \
|
||||
printf.c putc.c putchar.c puts.c putw.c refill.c remove.c rewind.c \
|
||||
rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c sscanf.c \
|
||||
stdio.c tmpfile.c ungetc.c vfprintf.c vfscanf.c vprintf.c vscanf.c \
|
||||
SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \
|
||||
fgetc.c fgetln.c fgetpos.c fgets.c fileno.c findfp.c flags.c \
|
||||
fopen.c fprintf.c fpurge.c fputc.c fputs.c fread.c freopen.c \
|
||||
fscanf.c fseek.c fsetpos.c ftell.c funopen.c fvwrite.c \
|
||||
fwalk.c fwrite.c getc.c getchar.c gettemp.c getw.c makebuf.c \
|
||||
mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c puts.c \
|
||||
putw.c refill.c remove.c rewind.c rget.c scanf.c setbuf.c \
|
||||
setbuffer.c setvbuf.c snprintf.c sscanf.c stdio.c tmpfile.c \
|
||||
ungetc.c vasprintf.c vfprintf.c vfscanf.c vprintf.c vscanf.c \
|
||||
vsnprintf.c vsscanf.c wbuf.c wsetup.c
|
||||
|
||||
.if !defined(AUDIT)
|
||||
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
|
||||
.endif
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
/* $NetBSD: asprintf.c,v 1.1 1998/08/28 21:33:10 perry Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: asprintf.c,v 1.1 1998/08/28 21:33:10 perry Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
asprintf(char **str, char const *fmt, ...)
|
||||
#else
|
||||
asprintf(str, fmt, va_alist)
|
||||
char **str;
|
||||
const char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
unsigned char *_base;
|
||||
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
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 NULL */
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
*f._p = '\0';
|
||||
va_end(ap);
|
||||
_base = realloc(f._bf._base, f._bf._size + 1);
|
||||
if (_base == NULL) {
|
||||
errno = ENOMEM;
|
||||
ret = -1;
|
||||
}
|
||||
f._bf._base = _base;
|
||||
*str = (char *)f._bf._base;
|
||||
return (ret);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: fvwrite.c,v 1.8 1998/02/03 18:41:15 perry Exp $ */
|
||||
/* $NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
|
@ -41,12 +41,13 @@
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fvwrite.c,v 1.8 1998/02/03 18:41:15 perry Exp $");
|
||||
__RCSID("$NetBSD: fvwrite.c,v 1.9 1998/08/28 21:33:11 perry Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
@ -117,6 +118,23 @@ __sfvwrite(fp, uio)
|
|||
*/
|
||||
do {
|
||||
GETIOV(;);
|
||||
if ((fp->_flags & (__SALC | __SSTR)) ==
|
||||
(__SALC | __SSTR) && fp->_w < len) {
|
||||
size_t blen = fp->_p - fp->_bf._base;
|
||||
unsigned char *_base;
|
||||
|
||||
/*
|
||||
* Alloc an extra 128 bytes (+ 1 for NULL)
|
||||
* so we don't call realloc(3) so often.
|
||||
*/
|
||||
fp->_w = len + 128;
|
||||
fp->_bf._size = blen + len + 128;
|
||||
_base = realloc(fp->_bf._base, fp->_bf._size + 1);
|
||||
if (_base == NULL)
|
||||
goto err;
|
||||
fp->_bf._base = _base;
|
||||
fp->_p = fp->_bf._base + blen;
|
||||
}
|
||||
w = fp->_w;
|
||||
if (fp->_flags & __SSTR) {
|
||||
if (len < w)
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/* $NetBSD: vasprintf.c,v 1.1 1998/08/28 21:33:11 perry Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: vasprintf.c,v 1.1 1998/08/28 21:33:11 perry Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
int
|
||||
vasprintf(str, fmt, ap)
|
||||
char **str;
|
||||
const char *fmt;
|
||||
_BSD_VA_LIST_ ap;
|
||||
{
|
||||
int ret;
|
||||
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) {
|
||||
*str = NULL;
|
||||
errno = ENOMEM;
|
||||
return (-1);
|
||||
}
|
||||
f._bf._size = f._w = 127; /* Leave room for the NULL */
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
*f._p = '\0';
|
||||
_base = realloc(f._bf._base, f._bf._size + 1);
|
||||
if (_base == NULL) {
|
||||
if (f._bf._base)
|
||||
free(f._bf._base);
|
||||
f._bf._base = NULL;
|
||||
errno = ENOMEM;
|
||||
ret = -1;
|
||||
}
|
||||
f._bf._base = _base;
|
||||
*str = (char *)f._bf._base;
|
||||
return (ret);
|
||||
}
|
Loading…
Reference in New Issue