Annotation of rpl/src/asprintf.c, revision 1.3

1.1       bertrand    1: /*
                      2: ================================================================================
                      3:   RPL/2 (R) version 4.0.10
1.3     ! bertrand    4:   Copyright (C) 1989-2010 Dr. BERTRAND Joël
1.1       bertrand    5: 
                      6:   This file is part of RPL/2.
                      7: 
                      8:   RPL/2 is free software; you can redistribute it and/or modify it
                      9:   under the terms of the CeCILL V2 License as published by the french
                     10:   CEA, CNRS and INRIA.
                     11:  
                     12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
                     13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                     14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
                     15:   for more details.
                     16:  
                     17:   You should have received a copy of the CeCILL License
                     18:   along with RPL/2. If not, write to info@cecill.info.
                     19: ================================================================================
                     20: */
                     21: 
                     22: 
1.2       bertrand   23: #include "rpl.h"
1.1       bertrand   24: #include <stdarg.h>
1.2       bertrand   25: 
1.1       bertrand   26: 
                     27: int
1.2       bertrand   28: valsprintf(unsigned char **strp, const char *fmt, va_list ap)
1.1       bertrand   29: {
1.3     ! bertrand   30:    size_t          bs;
        !            31:    size_t          s;
        !            32:    unsigned char   *b = NULL;
1.1       bertrand   33: 
1.3     ! bertrand   34:    for(bs = 1024;; bs *= 2)
1.1       bertrand   35:    {
1.3     ! bertrand   36:        if (b != NULL)
        !            37:        {
        !            38:            free(b);
        !            39:        }
        !            40: 
        !            41:            if ((b = malloc(sizeof(*b) * bs)) == NULL)
        !            42:        {
        !            43:            return -1;
        !            44:        }
        !            45: 
        !            46:            if (((int) (s = vsnprintf(b, bs, fmt, ap))) < 0)
        !            47:        {
        !            48:            free(b);
        !            49:            return -1;
        !            50:        }
        !            51: 
        !            52:            if (s < bs)
        !            53:        {
        !            54:            break;
        !            55:        }
1.1       bertrand   56:    }
                     57: 
1.3     ! bertrand   58:    if (((*strp) = realloc(b, ((s = strlen(b)) + 1) * sizeof(*b))) == NULL)
1.1       bertrand   59:    {
                     60:        free(b);
                     61:        return -1;
                     62:    }
                     63: 
1.3     ! bertrand   64:    return s;
1.1       bertrand   65: }
                     66: 
                     67: 
                     68: int
1.2       bertrand   69: alsprintf(unsigned char **strp, const char *fmt, ...)
1.1       bertrand   70: {
1.3     ! bertrand   71:    int         done;
        !            72:    va_list     arg;
1.1       bertrand   73: 
1.3     ! bertrand   74:    va_start(arg, fmt);
        !            75:    done = valsprintf(strp, fmt, arg);
        !            76:    va_end(arg);
1.1       bertrand   77: 
1.3     ! bertrand   78:    return(done);
1.1       bertrand   79: }
                     80: 
                     81: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>