Code:
#define CHARS_IN_ADVERT 714
struct MemoryStruct {
char *memory;
size_t size;
};
void stripads(char *s2)
{
if (strlen(s2) >CHARS_IN_ADVERT)
{//chunk.memory is bigger than CHARS_IN_ADVERT characters
s2[strlen(s2) - CHARS_IN_ADVERT] = '\0';
}
else
{//Not enough characters, so cut them all off I guess
s2[0] = '\0';
}
return *s2;
}
void *myrealloc(void *ptr, size_t size)
{
// There might be a realloc() out there that doesn't like reallocing NULL pointers, so we take care of it here
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
struct message getstatus(void)
{
char tmp[4],url[255]="",fileline[9000]="",*thedataPtr,thedata[9000]="";
struct MemoryStruct chunk;
struct message messages[100];
int i=0,nlpos=0,error;
chunk.memory=NULL; // we expect realloc(NULL, size) to work
chunk.size = 0; // no data at this point
if(curlhttp) {
sprintf(url,"%s/messages.php?user=%s&hash=%s",SERVERADDR,usrname,hash);
#if DEBUG
printf("Sending data...\n");
#endif
curl_easy_setopt(curlhttp, CURLOPT_URL, url);
#if DEBUG
printf("Opened %s\nSetting up memory...\n",url);
#endif
// send all data to this function
curl_easy_setopt(curlhttp, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
// we pass our 'chunk' struct to the callback function
curl_easy_setopt(curlhttp, CURLOPT_WRITEDATA, (void *)&chunk);
#if DEBUG
printf("Reading into memory...\n",url);
#endif
// get it!
if(0!=(error=curl_easy_perform(curlhttp))) {
fprintf(stderr,"Error: Getstatus: curl: %s.\n",curl_easy_strerror(error));
return *messages;
}
if(strlen(chunk.memory)<6) {
fprintf(stderr,"Error: Getstatus: Too little, or no data returned.\n");
return *messages;
}
memmove((char *)&chunk.memory[0],(char *)&chunk.memory[1],chunk.size-1); // strip initial newline char.
stripads(chunk.memory);
#if DEBUG
printf("Getstatus: Data Received:\n\"%s\"\n",chunk.memory);
printf("Getstatus: Checking for errors\n");
#endif
sscanf(chunk.memory,"%3s",&tmp);
printf("tmp: \"%s\"\n",tmp);
if(0!=strcmp(tmp,"ERR")) {
#if DEBUG
printf("Getstatus: We have results!\n");
#endif
// parse the data
// tokenize it into individual lines and then into the different parts and create the array of filedetails.
// nlpos=chrpos(chunk.memory,10,0);
printf("Getstatus: %p fileline: %s\n",&fileline,fileline);
// filelinePtr=fileline;
// substr(filelinePtr,chunk.memory,0,nlpos);
printf("Getstatus: Data Received:\n\"%s\"\n",chunk.memory);
// thedata=malloc(sizeof(*chunk.memory));
memcpy(thedata,chunk.memory,chunk.size);
strcat(thedata,'\0');
thedataPtr = thedata;
// sscanf(chunk.memory,"%s",thedata);
/ *************** segfault now after output of string here, but before "here" is printed. *************/
printf("Thedata: \"%s\"\n",thedata);
printf("here");
*fileline=strtok(thedataPtr,10);
HTH