All pastes #46411 Raw Edit

Untitled

public text v1 · immutable
#46411 ·published 2006-03-21 00:52 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>


int mprintf (char ** p, const char *format, ...)
{
    va_list arg;
    char *tmp;
    int done;
  
    int len = 0;
    int add_len = 0;
    
    /* count old chars */
    if(*p)
        len = strlen(*p);


    /* count leng to add */
    add_len = strlen(format);
    
    /*printf("Changing size to %i\n", p->string_len);*/
    *p=realloc(*p, ((len + add_len + 1) * sizeof(char)));

    /* Put pointer to where to start write */
    tmp=(*p)+len;

    va_start (arg, format);
    done = vsprintf (tmp, format, arg);
    va_end (arg);

    return done;
}


int main(int argc, char **argv)
{
    char *test=NULL;

    mprintf(&test, "Hello world\n");
    mprintf(&test, "This is a funny test\n");
    mprintf(&test, "Of an self reallocating function\n");
    /*mprintf(&test, "that shud work with %s\n", "variables");*/


    printf("This is test (%i): \n\"%s\"\n", strlen(test), test);

    free(test);
    exit(0);
}