void allocateFolderTree(char **tree)
{
int i;
tree = (char **)malloc(sizeof(char*)*MAX_FOLDERS);
for(i=0;i<MAX_FOLDERS;i++)
tree[i] = (char *)malloc(sizeof(char)*MAX_FOLDERS*MAX_FILENAME);
}
void getFolderTree (char **tree, char *path, int i)
{
DIR *dir = opendir(path);
struct dirent *entry;
while (entry = readdir(dir))
{
if( !strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
if (entry->d_type & DT_DIR)//check file type
{
//segfault for the next 3 lines
strcpy(tree[i], path);
strcat(tree[i], "/");
strcat(tree[i], entry->d_name);
i++;
char *new_path = malloc(sizeof(char)*(strlen(path)+MAX_FILENAME));
strcpy(new_path, path);
strcat(new_path, "/");
strcat(new_path, entry->d_name);
getFolderTree(tree, new_path, i);
free(new_path);
}
}
closedir (dir);
}
int main ()
{
char **folderTree;
allocateFolderTree(folderTree);
getFolderTree(folderTree, ROOT, 0);
free(folderTree);
return 0;
}
Why am I getting segfault and how do I solve this?
PS: MAX_FOLDERS=1000 MAX_FILENAME=30 The folders I'm reading are less than 10 and each has a name less than 30!