【发布时间】:2015-04-16 13:26:57
【问题描述】:
我正在制作一个横穿系统所有子文件的函数,如果它是常规文件,则打印出文件目录。如果文件在目录中,它会打印出一个文件,但是一旦它尝试遍历子目录,它就会给我一个malloc() memory corruption error,尽管我没有分配任何东西。
void Traverse(char* dir)
{
struct stat buffer;
struct stat *s=&buffer;
struct dirent* file;
DIR* currentDir;
// printf("%s \n",dir);
if((strstr(dir,"./")!=NULL)||(strstr(dir,"../")!=NULL))
return;
currentDir = opendir(dir);
if (lstat(dir,s)==0)
{
if(S_ISREG(s->st_mode))
printf("%s \n", dir);
else if (S_ISDIR (s->st_mode))
{
while((file= readdir(currentDir))!=NULL)
{
char* path = strdup(dir);
strcat(path,"/");
strcat(path,file->d_name);
Traverse(path);
}
closedir(currentDir);
}
}
else
return;
}
【问题讨论】:
-
我认为 strcat 可能会在这里产生问题
标签: c ubuntu directory filesystems