1 1.17 christos /* $NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $ */ 2 1.1 perry 3 1.1 perry /* 4 1.1 perry * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com> 5 1.1 perry * All rights reserved. 6 1.1 perry * 7 1.1 perry * Redistribution and use in source and binary forms, with or without 8 1.1 perry * modification, are permitted provided that the following conditions 9 1.1 perry * are met: 10 1.1 perry * 1. Redistributions of source code must retain the above copyright 11 1.1 perry * notice, this list of conditions and the following disclaimer. 12 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 perry * notice, this list of conditions and the following disclaimer in the 14 1.1 perry * documentation and/or other materials provided with the distribution. 15 1.1 perry * 3. The name of the author may not be used to endorse or promote products 16 1.1 perry * derived from this software without specific prior written permission. 17 1.1 perry * 18 1.1 perry * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 1.1 perry * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20 1.1 perry * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 1.1 perry * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 1.1 perry * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 1.1 perry * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 1.1 perry * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 1.1 perry * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 1.1 perry * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 1.1 perry * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 perry */ 29 1.1 perry 30 1.1 perry #include <sys/cdefs.h> 31 1.1 perry #if defined(LIBC_SCCS) && !defined(lint) 32 1.17 christos __RCSID("$NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $"); 33 1.1 perry #endif /* LIBC_SCCS and not lint */ 34 1.1 perry 35 1.15 joerg #include "namespace.h" 36 1.15 joerg 37 1.5 lukem #include <assert.h> 38 1.5 lukem #include <errno.h> 39 1.15 joerg #include <locale.h> 40 1.1 perry #include <stdio.h> 41 1.1 perry #include <stdlib.h> 42 1.15 joerg 43 1.9 thorpej #include "reentrant.h" 44 1.15 joerg #include "setlocale_local.h" 45 1.8 yamt #include "local.h" 46 1.1 perry 47 1.15 joerg __weak_alias(asprintf, _asprintf) 48 1.15 joerg __weak_alias(asprintf_l, _asprintf_l) 49 1.15 joerg __weak_alias(vasprintf, _vasprintf) 50 1.15 joerg __weak_alias(vasprintf_l, _vasprintf_l) 51 1.15 joerg 52 1.1 perry int 53 1.15 joerg vasprintf_l(char **str, locale_t loc, const char *fmt, va_list ap) 54 1.1 perry { 55 1.1 perry int ret; 56 1.1 perry FILE f; 57 1.8 yamt struct __sfileext fext; 58 1.1 perry unsigned char *_base; 59 1.5 lukem 60 1.5 lukem _DIAGASSERT(str != NULL); 61 1.5 lukem _DIAGASSERT(fmt != NULL); 62 1.1 perry 63 1.8 yamt _FILEEXT_SETUP(&f, &fext); 64 1.7 mycroft f._file = -1; 65 1.1 perry f._flags = __SWR | __SSTR | __SALC; 66 1.17 christos f._bf._base = f._p = malloc(128); 67 1.3 mycroft if (f._bf._base == NULL) 68 1.3 mycroft goto err; 69 1.2 kleink f._bf._size = f._w = 127; /* Leave room for the NUL */ 70 1.15 joerg ret = __vfprintf_unlocked_l(&f, loc, fmt, ap); 71 1.3 mycroft if (ret == -1) 72 1.3 mycroft goto err; 73 1.1 perry *f._p = '\0'; 74 1.12 christos _base = realloc(f._bf._base, (size_t)(ret + 1)); 75 1.3 mycroft if (_base == NULL) 76 1.3 mycroft goto err; 77 1.3 mycroft *str = (char *)_base; 78 1.14 christos return ret; 79 1.3 mycroft 80 1.3 mycroft err: 81 1.3 mycroft if (f._bf._base) 82 1.3 mycroft free(f._bf._base); 83 1.3 mycroft *str = NULL; 84 1.3 mycroft errno = ENOMEM; 85 1.14 christos return -1; 86 1.1 perry } 87 1.15 joerg 88 1.15 joerg int 89 1.15 joerg vasprintf(char **str, const char *fmt, va_list ap) 90 1.15 joerg { 91 1.16 joerg return vasprintf_l(str, _current_locale(), fmt, ap); 92 1.15 joerg } 93 1.15 joerg 94 1.15 joerg int 95 1.15 joerg asprintf_l(char **str, locale_t loc, char const *fmt, ...) 96 1.15 joerg { 97 1.15 joerg va_list ap; 98 1.15 joerg int ret; 99 1.15 joerg 100 1.15 joerg va_start(ap, fmt); 101 1.15 joerg ret = vasprintf_l(str, loc, fmt, ap); 102 1.15 joerg va_end(ap); 103 1.15 joerg return ret; 104 1.15 joerg } 105 1.15 joerg 106 1.15 joerg int 107 1.15 joerg asprintf(char **str, char const *fmt, ...) 108 1.15 joerg { 109 1.15 joerg va_list ap; 110 1.15 joerg int ret; 111 1.15 joerg 112 1.15 joerg va_start(ap, fmt); 113 1.15 joerg ret = vasprintf(str, fmt, ap); 114 1.15 joerg va_end(ap); 115 1.15 joerg return ret; 116 1.15 joerg } 117