I am writing a function which prints out to as standard output, like how a regular printf function does but instade of taking indicators like %d or %s it takes {i} or {s}. The problem i have is that when the string of the format argument is too long about 23 characters, i get a segmentation fault at the line where i am calling vfprintf function.
int mr_asprintf(const char *format, ...)
{
int i;
char *newFormat = calloc(1,sizeof(char));
char integer[3] = "%d";
char str[3] = "%s";
char tmpStr[2];
va_list args;
newFormat[0] ='\0';
tmpStr[1] = '\0';
for(i=0;format[i]!='\0';i++) // convert to printf syntaxe
{
if(format[i]=='{' && format[i+2]=='}') //check if it's {x}
{
switch(format[i+1])
{
case 'i':
strcat(newFormat,integer);
i += 2;
break;
case 's':
strcat(newFormat,str);
i += 2;
break;
}
}
else
{
tmpStr[0] = format[i];
strcat(newFormat,tmpStr);
}
}
va_start(args,format);
int s = vfprintf(stdout,newFormat,args);
va_end(args);
free(newFormat);
return s;
}
Test example :
int main()
{
char *result = mr_asprintf("bce }edadacba{i}}aa}da{s}fe aeaee d{i}cefaa",55,"XXX",66);
printf("%s\n",result);
return 0;
}