递归列表

时间:2016-06-20 05:18:48

标签: c recursion ls

我在尝试在C上实现像ls -R这样的东西时遇到了问题,问题是我需要列表递归列出从给定目录开始的所有内容,然后使用我从列表中获得的那些常规文件。 这就是我到目前为止所做的:

 void ls(char* path){
    DIR *directory;
    struct dirent *filei;
    struct stat stats;
    directory = opendir(path);
    if (directory != NULL)  
    {
        while ((filei=readdir(directory))!=NULL){
            stat(filei->d_name, &stats);    
            printf(" %s\n", filei->d_name); 
            if (S_ISDIR(stats.st_mode)){
                char *buf = malloc(strlen(path)+strlen(filei->d_name)+2);
                strcpy(buf,path);
                strcat(buf,"/");
                strcat(buf,filei->d_name);
                ls(buf);
            }
        }
        closedir(directory);
    }
    else{
        printf("Error.\n");     
    }
}

它完全不起作用,它显示的文件甚至不在我正在使用的文件夹中。 有什么想法吗? 感谢。

2 个答案:

答案 0 :(得分:3)

你不能递归“。”和“..”条目。你至少无限地在同一个dir上进行渲染,或者 up 严重。过滤器:

if (!strcmp(filei->d_name,".") && (!strcmp(filei->d_name,"..")) ls(buf);

您还必须stat进入完整路径:

char *buf = malloc(strlen(path)+strlen(filei->d_name)+2);
strcpy(buf,path);
strcat(buf,"/");
strcat(buf,filei->d_name);
stat(buf, &stats);
if (S_ISDIR(stats.st_mode)) {
  if (!strcmp(filei->d_name,".") && (!strcmp(filei->d_name,"..")) {
    ls(buf);
  }
}

答案 1 :(得分:1)

以下对代码的返工在完整文件路径上调用stat(),跳过“。”和“..”目录,修复内存泄漏并添加一点错误处理:

#define SEPARATOR "/"

void ls(const char *path)
{
    DIR *directory = opendir(path);

    if (directory != NULL)  
    {
        struct dirent *filei;

        while ((filei = readdir(directory)) != NULL)
        {
            if (strcmp(filei->d_name, ".") == 0 || strcmp(filei->d_name, "..") == 0)
            {
                continue;
            }

            char *buffer = malloc(strlen(path) + strlen(filei->d_name) + strlen(SEPARATOR) + 1);
            strcat(strcat(strcpy(buffer, path), SEPARATOR), filei->d_name);

            struct stat stat_buffer;

            if (stat(buffer, &stat_buffer) == 0)
            {   
                printf("%s\n", buffer); 

                if (S_ISDIR(stat_buffer.st_mode))
                {
                    ls(buffer);
                }
            }
            else
            {
                perror(NULL);
            }

            free(buffer);
        }

        closedir(directory);
    }
    else
    {
        perror(NULL);     
    }
}

看看它是否适合你。

相关问题