遍历c中的路径

时间:2014-09-09 01:05:52

标签: c unix path traversal dirent.h

我在c中编写一个程序,它接受一个路径名并遍历该路径,并打印出它遇到的所有文件路径以及块中该文件的大小,然后如果是dir则打印出dir路径名和块大小。

代码以无限循环结束并一直说“无法获取状态:打开的文件太多。”

#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char **argv ){
    struct stat statbuf;
    struct dirent *direntp;
    DIR *dirp;
    if(stat(argv[1], &statbuf) == -1){
        perror("Failed to get file status");
        return 1;
    }
    else if(argc != 2){
        perror("Invalid amount of arguments, showtreesize requires 1 pathname");
        return 1;
    }
    else{
        if(S_ISDIR(statbuf.st_mode) || S_ISREG(statbuf.st_mode)){
            printf("%s      %d", argv[1], depthfirstapply(argv[1],     sizepathfun(argv[1])));
        }
        else{
            if(S_ISCHR(statbuf.st_mode)){
                printf("%s is a character special file.", argv[1]);
            }
            if(S_ISBLK(statbuf.st_mode)){
                printf("%s is a block special file", argv[1]);
            }
            if(S_ISFIFO(statbuf.st_mode)){
                printf("%s is a FIFO special file", argv[1]);
            }
            else{
                printf("%s is not a valid filetype", argv[1]);
            }
        }
        return 0;
    }
}


int sum = 0;
int levelcount = 0;
int isDirectory(char *path){
    struct stat statbuf;
    if(stat(path, &statbuf) == -1)
        return 0;
    else
        return S_ISDIR(statbuf.st_mode);
}

int depthfirstapply(char *path, int pathfun(char *path1)){
    struct dirent *direntp;
    DIR *dirp;
    if(isDirectory(path)){
        printf("%s\n", path);
        if((dirp = opendir(path)) == NULL){
            perror ("Failed to open directory");
            return -1;
        }
        else{
            while((direntp = readdir(dirp)) != NULL) {
                if(isDirectory(direntp->d_name)){
                    int result = depthfirstapply(direntp->d_name, pathfun);
                    if (result > 0){
                        sum += result;
                    }
                }
                else{
                    if(pathfun(direntp->d_name) >= 0){
                        sum += pathfun(direntp->d_name);
                    }
                }
            }
            while ((closedir(dirp) == -1) && (errno == EINTR)) ;
        }
    }
    else{
        sum += pathfun(path);
    }
    return sum;
}



int sizepathfun(char *path){
    struct stat statbuf;
    if(stat(path, &statbuf) == -1){
        perror("Failed to get file status");
        return -1;
    }
    if(S_ISREG(statbuf.st_mode) == 0){
        return -1;
    }
    else{
        printf("%s      %d", path,  statbuf.st_blocks);
        return statbuf.st_blocks;
    }
}

1 个答案:

答案 0 :(得分:0)

几个问题:

  1. 您需要跳过...条目。否则,您将继续循环到同一目录。

  2. lstat()中使用stat()而不是isDirectory。否则,您将递归到目录的符号链接,这可能会导致循环。

  3. 当你走到每个目录级别时,你需要将目录组件连接到名称。

  4. depthfirstapply的第二个参数应该是一个函数。但是在main()中,您使用sizepathfun(argv[1))来调用它,它返回一个整数。论证应该是sizepathfun。由于类型不匹配,您应该收到编译警告。

  5. POSIX为此提供了一组标准函数fts_XXX()

相关问题