Original Post
Hi,
I''m having difficulty''s implementing a new function in my program, I think the problem is in the use of pointers...
I was hoping someone out here could help me ...
I wrote a function that looks somewhat like this :
int RefreshFileList(FileInfoT *theFiles, char *theDir,
int (*select)(const struct dirent*),
int fileAttr, int dirAttr)
{
struct dirent **f;
int t1,dt,ft;
FileInfoT buf;
int fc = 0;
fc = scandir(theDir, &f, select, alphasort);
if ((theFiles = (FileInfoT *)realloc(theFiles,
fc*sizeof(FileInfoT))) == NULL)
{
endwin();
printf(
"\nnot enough memory to store directory listing...
\n");
exit(1);
};
ft = 0; dt = fc-1;
for (t1=0;t1d_type == DT_DIR)
{
sprintf(theFiles[ft].name,"/%s",f[t1]->d_name);
sprintf(theFiles[ft].path,"%s%s/",theDir,f[t1]->d_name);
theFiles[ft].attr = fileAttr;
ft++;
}
else
{
sprintf(theFiles[dt].name," %s",f[t1]->d_name);
sprintf(theFiles[dt].path,"%s%s",theDir,f[t1]->d_name);
theFiles[dt].attr = dirAttr;
dt--;
}
free(f[t1]);
}
free(f);
return(fc);
}
I cut some parts of the function becouse I did''nt seem to be important to the problem...
But the function is designed to read a directory list, reformat and put it in theFiles...
The function seems to work, or at least... calling it with
fileCount = RefreshFileList(files, currentDir,
FileListFileSelect, MFFILEA, MFDIRECTORYA);
seems to wordt fine...
But the program dies giving me a segmentation fault as soon as
I try to access the files array using :
files.name or anything like that.. (i < fileCount)
I realy can''t figure out what the problem might be ...
I hope someone can help me with this one...