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

1.1       bertrand    1: /*
                      2: ================================================================================
1.36    ! bertrand    3:   RPL/2 (R) version 4.1.9
1.32      bertrand    4:   Copyright (C) 1989-2012 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"
                     24: 
1.1       bertrand   25: 
                     26: int
1.2       bertrand   27: valsprintf(unsigned char **strp, const char *fmt, va_list ap)
1.1       bertrand   28: {
1.3       bertrand   29:    size_t          bs;
                     30:    size_t          s;
1.5       bertrand   31: 
1.3       bertrand   32:    unsigned char   *b = NULL;
1.1       bertrand   33: 
1.5       bertrand   34:    va_list         cap;
                     35: 
1.3       bertrand   36:    for(bs = 1024;; bs *= 2)
1.1       bertrand   37:    {
1.5       bertrand   38:        va_copy(cap, ap);
                     39: 
1.3       bertrand   40:        if (b != NULL)
                     41:        {
                     42:            free(b);
                     43:        }
                     44: 
1.5       bertrand   45:        if ((b = malloc(sizeof(*b) * bs)) == NULL)
1.3       bertrand   46:        {
                     47:            return -1;
                     48:        }
                     49: 
1.5       bertrand   50:        if (((int) (s = vsnprintf(b, bs, fmt, cap))) < 0)
1.3       bertrand   51:        {
                     52:            free(b);
                     53:            return -1;
                     54:        }
                     55: 
1.5       bertrand   56:        if (s < bs)
1.3       bertrand   57:        {
                     58:            break;
                     59:        }
1.1       bertrand   60:    }
                     61: 
1.3       bertrand   62:    if (((*strp) = realloc(b, ((s = strlen(b)) + 1) * sizeof(*b))) == NULL)
1.1       bertrand   63:    {
                     64:        free(b);
                     65:        return -1;
                     66:    }
                     67: 
1.3       bertrand   68:    return s;
1.1       bertrand   69: }
                     70: 
                     71: 
                     72: int
1.2       bertrand   73: alsprintf(unsigned char **strp, const char *fmt, ...)
1.1       bertrand   74: {
1.3       bertrand   75:    int         done;
                     76:    va_list     arg;
1.1       bertrand   77: 
1.3       bertrand   78:    va_start(arg, fmt);
                     79:    done = valsprintf(strp, fmt, arg);
                     80:    va_end(arg);
1.1       bertrand   81: 
1.3       bertrand   82:    return(done);
1.1       bertrand   83: }
                     84: 
                     85: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>