C
cerr
Hi There,
I wrote a function that reads out all the files in a certain directory
and it's writing them into an array of character arrays that was
passed. The function looks like this:
Code:
int getdir(char *path, char **filelist)
{
int items=0;
char **temp=NULL;
char filename[1024]={0};
struct dirent *dp;
DIR *dir = opendir(path);
if (dir==NULL){
syslog(LOG_ALERT, "Error opening log_record directory %s", path);
printf("Error opening log_record directory %s", path);
return -1;
}
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL){
sprintf(filename,"%s%s",path,dp->d_name);
syslog(LOG_ALERT, "%s\n",filename);
temp = realloc(filelist, (items+1) * sizeof(*temp));
if (temp == NULL) {
while (--items >= 0)
free (filelist[items]);
free (filelist);
syslog(LOG_ALERT, "Error reallocating memory for filelist\n");
printf("Error reallocating memory for filelist\n");
return -1;
}
filelist = temp;
filelist[items] = calloc(strlen(filename)+1,sizeof(char));
if (filelist[items] == NULL) {
syslog(LOG_ALERT, "Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;
}
strncpy (filelist[items], filename, strlen(filename));
++items;
}
}
printf("filelist: %s\n",filelist[items-1]);
return items;
}
and i'm calling it like this:
Code:
files=getdir("/usr/share/NovaxTSP/", list);
printf("files %d - %s\n",files,list[files-1]);
where list is of typer char**
Now my problem is that my application segfaults out at the printf even
tho i know that files value is 1 and if i do the same printf inside
the function, it works just well, i do see the file path. What's going
on here?
Thank you!
Ron
I wrote a function that reads out all the files in a certain directory
and it's writing them into an array of character arrays that was
passed. The function looks like this:
Code:
int getdir(char *path, char **filelist)
{
int items=0;
char **temp=NULL;
char filename[1024]={0};
struct dirent *dp;
DIR *dir = opendir(path);
if (dir==NULL){
syslog(LOG_ALERT, "Error opening log_record directory %s", path);
printf("Error opening log_record directory %s", path);
return -1;
}
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL){
sprintf(filename,"%s%s",path,dp->d_name);
syslog(LOG_ALERT, "%s\n",filename);
temp = realloc(filelist, (items+1) * sizeof(*temp));
if (temp == NULL) {
while (--items >= 0)
free (filelist[items]);
free (filelist);
syslog(LOG_ALERT, "Error reallocating memory for filelist\n");
printf("Error reallocating memory for filelist\n");
return -1;
}
filelist = temp;
filelist[items] = calloc(strlen(filename)+1,sizeof(char));
if (filelist[items] == NULL) {
syslog(LOG_ALERT, "Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;
}
strncpy (filelist[items], filename, strlen(filename));
++items;
}
}
printf("filelist: %s\n",filelist[items-1]);
return items;
}
and i'm calling it like this:
Code:
files=getdir("/usr/share/NovaxTSP/", list);
printf("files %d - %s\n",files,list[files-1]);
where list is of typer char**
Now my problem is that my application segfaults out at the printf even
tho i know that files value is 1 and if i do the same printf inside
the function, it works just well, i do see the file path. What's going
on here?
Thank you!
Ron