// fmemopen
// test_fmemopen.c
// victor00000 (c) 2018.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ~$ gcc -Wall test_fmemopen.c -o test_fmemopen
// ~$ ./test_fmemopen /* 1 2 3
int main (int ia, char *ca[]){
if (ia == 1) return 0;
int i = 0;
char *c = 0;
int l;
for (i=1;i<ia;i++){
if (c == 0){
c = malloc (strlen (ca[i]) + 2 );
c[0] = 0;
}
l = strlen (c);
if (l != 0)
c = realloc (c,l + strlen (ca[i]) + 2 );
sprintf (c+l,"%s\n",ca[i]);
}
l = strlen (c);
char *bf = malloc (1024*4);
char **c2 = malloc (sizeof(char*));
c2[0] = 0;
i = 0;
FILE *f;
f = fmemopen (c,l,"r");
while (fgets (bf,1024*4,f) != 0){
c2[i] = strdup ( bf );
i++;
c2 = realloc (c2,(i+1)*sizeof (char*));
c2[ i ] = 0;
}
fclose (f);
free (bf);
free (c);
i = 0;
while (c2[i] != 0){
printf ("%s",c2[i]);
free (c2[i]);
i++;
}
free (c2);
return 0;
}