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

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

CVSweb interface <joel.bertrand@systella.fr>