Originally Posted by
cerr
Nevertheless I changed it to
Code:
(*filelist[items]) = malloc(strlen(filename)+1);
Actually, I have another problem there, let's say that there's more than 1 file in this directory.
Code snippet:
Code:
(*filelist) = temp;
printf("DEBUG: filename:%s, items:%d\n",filename,items);
(*filelist[items]) = malloc(strlen(filename)+1);
printf("DEBUG: After malloc %p\n",(*filelist[items]));
if ((*filelist[items]) == NULL) {
syslog(LOG_NOTICE, "nlog: Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;
}
returns me following:
DEBUG: filename:/usr/share/NovaxTSP/log_record20091229, items:0
DEBUG: After malloc 0x8f0b3a0
DEBUG: filename:/usr/share/NovaxTSP/log_record20091230, items:1
Segmentation fault
Why can't i malloc space for the 2nd element in where items = 1. above I did reallocate filellist so that there should be enough room for it:
Code:
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL){
sprintf(filename,"%s%s",path,dp->d_name);
syslog(LOG_NOTICE, "nlog: %s\n",filename);
temp = realloc((*filelist), (items+1) * sizeof(*temp));
if (temp == NULL) {
while (--items >= 0)
free ((*filelist[items]));
free ((*filelist));
syslog(LOG_NOTICE, "nlog: Error reallocating memory for filelist\n");
printf("Error reallocating memory for filelist\n");
return -1;
}
(*filelist) = temp;
printf("DEBUG: filename:%s, items:%d\n",filename,items);
(*filelist[items]) = malloc(strlen(filename)+1);
printf("DEBUG: After malloc %p\n",(*filelist[items]));
if ((*filelist[items]) == NULL) {
syslog(LOG_NOTICE, "nlog: Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;
}
strcpy ((*filelist[items]), filename);
++items;
}
}