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

1.1     ! bertrand    1: /*
        !             2: ================================================================================
        !             3:   RPL/2 (R) version 4.0.10
        !             4:   Copyright (C) 1989-2010 Dr. BERTRAND Joël
        !             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: 
        !            23: #include <stdio.h>
        !            24: #include <stdlib.h>
        !            25: #include <stdarg.h>
        !            26: #include <string.h>
        !            27: #include <strings.h>
        !            28: 
        !            29: int
        !            30: vasprintf(char **strp, const char *fmt, va_list ap)
        !            31: {
        !            32:   size_t bs;
        !            33:   size_t s;
        !            34:   char *b = NULL;
        !            35: 
        !            36:   for(bs = 1024;; bs *= 2)
        !            37:   {
        !            38:     if (b != NULL)
        !            39:    {
        !            40:        free(b);
        !            41:    }
        !            42: 
        !            43:     if ((b = malloc(sizeof(*b) * bs)) == NULL)
        !            44:    {
        !            45:        return -1;
        !            46:    }
        !            47: 
        !            48:     if (((int) (s = vsnprintf(b, bs, fmt, ap))) < 0)
        !            49:    {
        !            50:        free(b);
        !            51:        return -1;
        !            52:    }
        !            53: 
        !            54:     if (s < bs)
        !            55:    {
        !            56:        break;
        !            57:    }
        !            58:   }
        !            59: 
        !            60:   if (((*strp) = realloc(b, ((s = strlen(b)) + 1) * sizeof(*b)))
        !            61:       == NULL)
        !            62:   {
        !            63:      free(b);
        !            64:      return -1;
        !            65:   }
        !            66: 
        !            67:   return s;
        !            68: }
        !            69: 
        !            70: 
        !            71: int
        !            72: asprintf(char **strp, const char *fmt, ...)
        !            73: {
        !            74:   int done;
        !            75:   va_list arg;
        !            76: 
        !            77:   va_start(arg, fmt);
        !            78:   done = vasprintf(strp, fmt, arg);
        !            79:   va_end(arg);
        !            80: 
        !            81:   return(done);
        !            82: }
        !            83: 
        !            84: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>