#include #include #include #include int mprintf (char ** p, const char *format, ...) { va_list arg; int tmp; int len = 0; int add_len = 0; /* count old chars */ if(*p) len = strlen(*p); /* count leng to add */ add_len = strlen(format) + 2; /*printf("Changing size to %i\n", p->string_len);*/ *p=realloc(*p, ((len + add_len) * sizeof(char))); while(1) { va_start (arg, format); tmp = vsnprintf ((*p)+len, add_len, format, arg); va_end (arg); /* check if that was enouth */ if(tmp > -1 && tmp < add_len) break; add_len = ( tmp < 0 ? add_len * 2 : tmp + 1); *p=realloc(*p, ((len + add_len) * sizeof(char))); } return tmp; } 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); }