vasprintf.c (102227) | vasprintf.c (103996) |
---|---|
1/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */ | 1/* $OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $ */ |
2 3/* 4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: --- 13 unchanged lines hidden (view full) --- 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> | 2 3/* 4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: --- 13 unchanged lines hidden (view full) --- 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> |
31__FBSDID("$FreeBSD: head/lib/libc/stdio/vasprintf.c 102227 2002-08-21 16:20:02Z mike $"); | 31__FBSDID("$FreeBSD: head/lib/libc/stdio/vasprintf.c 103996 2002-09-26 07:55:18Z tjr $"); |
32 33#include <stdio.h> 34#include <stdlib.h> 35#include <errno.h> 36#include "local.h" 37 38int 39vasprintf(str, fmt, ap) 40 char **str; 41 const char *fmt; 42 __va_list ap; 43{ 44 int ret; 45 FILE f; 46 struct __sFILEX ext; | 32 33#include <stdio.h> 34#include <stdlib.h> 35#include <errno.h> 36#include "local.h" 37 38int 39vasprintf(str, fmt, ap) 40 char **str; 41 const char *fmt; 42 __va_list ap; 43{ 44 int ret; 45 FILE f; 46 struct __sFILEX ext; |
47 unsigned char *_base; |
|
47 48 f._file = -1; 49 f._flags = __SWR | __SSTR | __SALC; 50 f._bf._base = f._p = (unsigned char *)malloc(128); | 48 49 f._file = -1; 50 f._flags = __SWR | __SSTR | __SALC; 51 f._bf._base = f._p = (unsigned char *)malloc(128); |
51 if (f._bf._base == NULL) { 52 *str = NULL; 53 errno = ENOMEM; 54 return (-1); 55 } | 52 if (f._bf._base == NULL) 53 goto err; |
56 f._bf._size = f._w = 127; /* Leave room for the NUL */ 57 f._extra = &ext; 58 INITEXTRA(&f); 59 ret = __vfprintf(&f, fmt, ap); | 54 f._bf._size = f._w = 127; /* Leave room for the NUL */ 55 f._extra = &ext; 56 INITEXTRA(&f); 57 ret = __vfprintf(&f, fmt, ap); |
58 if (ret == -1) 59 goto err; |
|
60 *f._p = '\0'; | 60 *f._p = '\0'; |
61 f._bf._base = reallocf(f._bf._base, f._bf._size + 1); 62 if (f._bf._base == NULL) { 63 errno = ENOMEM; 64 ret = -1; 65 } 66 *str = (char *)f._bf._base; | 61 _base = realloc(f._bf._base, ret + 1); 62 if (_base == NULL) 63 goto err; 64 *str = (char *)_base; |
67 return (ret); | 65 return (ret); |
66 67err: 68 if (f._bf._base != NULL) { 69 free(f._bf._base); 70 f._bf._base = NULL; 71 } 72 *str = NULL; 73 errno = ENOMEM; 74 return (-1); |
|
68} | 75} |