stat using S_ISDIR 似乎并不总是有效

stat using S_ISDIR Don't seem to always work

我注意到我的函数(在 c 中)的输出有些奇怪。 此函数检测目录中的元素是文件还是子目录。

// i cant detected properly if an element is a file or a dir 
int RecursiveSearch(char *Dir){
    DIR *Directory;
    //DIR *SubDirectory;
    struct dirent *entry;
    struct stat filestat;

    printf("I am Reading %s Directory\n", Dir);

    Directory = opendir(Dir);
    if(Directory == NULL)
    {
        perror("Unable to read directory.. i'm leaving\n");
        return(1); // leave
    }

    /* Read directory entries */
    while( (entry=readdir(Directory)) )
    {
        stat(entry->d_name,&filestat);
        if( S_ISDIR(filestat.st_mode) ){
            printf("%4s: %s\n","Dir",entry->d_name);
            if (strstr(entry->d_name, ".") == NULL && strstr(entry->d_name, "..") == NULL ) // to not infinit loop
            {
                // Recursion
                printf("\n*Entering a subDirectory*\n");
                RecursiveSearch(entry->d_name);
                printf("\n*Leaving a subDirectory*\n");
            }


        }
        else
            printf("%4s: %s\n","File",entry->d_name);
    }
    closedir(Directory);

    return(0);
}

我主要是这样调用来测试的 RecursiveSearch("./Directories");

这是我的输出

我的问题在"Starting Recursive Research.."

如您所见,SubDir 不是一个文件。 我看不出我在函数中做错了什么。

如果您需要任何其他说明,请询问。

编辑:

int RecursiveSearch(char *Dir){
    DIR *Directory;
    struct dirent *entry;
    struct stat filestat;

    printf("I am Reading %s Directory\n", Dir);

    Directory = opendir(Dir);
    if(Directory == NULL)
    {
        perror("Unable to read directory.. i'm leaving\n");
        return(1); // leave
    }

    /* Read directory entries */
    while( (entry=readdir(Directory)) )
    {
        char fullname[100];
        sprintf(fullname, "%s/%s",Dir,entry->d_name);
        stat(fullname,&filestat);
        if( S_ISDIR(filestat.st_mode) ){
            printf("%4s: %s\n","Dir",fullname);
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) // to not infinite loop
            {
                // Recursion
                printf("\n*Entering a subDirectory*\n");
                RecursiveSearch(fullname);
                printf("\n*Leaving a subDirectory*\n");
            }
        }
        else
            printf("%4s: %s\n","File",fullname);
    }
    closedir(Directory);

    return(0);
}

entry->d_name 只是名称,没有目录前缀。它是相对于进程的工作目录进行解释的,而不是函数递归中的当前目录。

调用其他函数,包括递归,需要在名称前加目录名前缀。

此外,您需要使用 strcmp() 将名称与 ... 进行比较。使用 strstr() 可防止递归到名称中任何位置具有 . 的目录。

int RecursiveSearch(char *Dir){
    DIR *Directory;
    struct dirent *entry;
    struct stat filestat;

    printf("I am Reading %s Directory\n", Dir);

    Directory = opendir(Dir);
    if(Directory == NULL)
    {
        perror("Unable to read directory.. i'm leaving\n");
        return(1); // leave
    }

    /* Read directory entries */
    while( (entry=readdir(Directory)) )
    {
        char fullname[MAXPATH];
        sprintf(fullname, "%s/%s", Dir, entry->d_name);
        stat(fullname,&filestat);
        if( S_ISDIR(filestat.st_mode) ){
            printf("%4s: %s\n","Dir",fullname);
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) // to not infinite loop
            {
                // Recursion
                printf("\n*Entering a subDirectory*\n");
                RecursiveSearch(fullname);
                printf("\n*Leaving a subDirectory*\n");
            }
        }
        else
            printf("%4s: %s\n","File",fullname);
    }
    closedir(Directory);

    return(0);
}