"Fossies" - the Fresh Open Source Software Archive

Member "tnftp-20200705/libnetbsd/vasprintf.c" (31 Jan 2016, 2596 Bytes) of package /linux/privat/tnftp-20200705.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "vasprintf.c" see the Fossies "Dox" file reference documentation.

    1 /* $NetBSD: vasprintf.c,v 1.1 2016/01/31 06:10:20 lukem Exp $  */
    2 /* from: NetBSD: asprintf.c,v 1.3 2012/07/02 16:02:53 joerg Exp  */
    3 
    4 /*-
    5  * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  *
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in
   16  *    the documentation and/or other materials provided with the
   17  *    distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
   25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include "tnftp.h"
   34 
   35 int
   36 vasprintf(char **ret, const char *fmt, va_list ap)
   37 {
   38     char *buf, *new_buf;
   39     size_t len;
   40     int retval;
   41     va_list ap2;
   42 
   43     len = 128;
   44     buf = malloc(len);
   45     if (buf == NULL) {
   46         *ret = NULL;
   47         return -1;
   48     }
   49 
   50 #if defined(HAVE_VA_COPY)
   51     va_copy(ap2, ap);
   52 #define my_va_end(ap2)  va_end(ap2)
   53 #elif defined(HAVE___BUILTIN_VA_COPY)
   54     __builtin_va_copy(ap2, ap);
   55 #define my_va_end(ap2)  __builtin_va_end(ap2)
   56 #else
   57     ap2 = ap;
   58 #define my_va_end(ap2)  do {} while (0)
   59 #endif
   60     retval = vsnprintf(buf, len, fmt, ap);
   61     if (retval < 0) {
   62         free(buf);
   63         *ret = NULL;
   64         va_end(ap2);
   65         return -1;
   66     }
   67 
   68     if (retval < len) {
   69         new_buf = realloc(buf, retval + 1);
   70         if (new_buf == NULL)
   71             *ret = buf;
   72         else
   73             *ret = new_buf;
   74         my_va_end(ap2);
   75         return retval;
   76     }
   77 
   78     len = (size_t)retval + 1;
   79     free(buf);
   80     buf = malloc(len);
   81     if (buf == NULL) {
   82         *ret = NULL;
   83         my_va_end(ap2);
   84         return -1;
   85     }
   86     retval = vsnprintf(buf, len, fmt, ap2);
   87     my_va_end(ap2);
   88     if (retval != len - 1) {
   89         free(buf);
   90         *ret = NULL;
   91         return -1;
   92     }
   93     *ret = buf;
   94     return retval;
   95 }