C目录扫描进入死循环

标签 c

int ScanDirectories(const char *dirname, struct images *imagesHeadPtr, struct filesToParse *filesHeadPtr)
{
    // scan the directory and store the entries in a buffer 


DIR *dir;
struct dirent *ent;
int jpgs = 0, pngs = 0;
int totalFiles = 0;
int filesToScan = 0;
char name[256];
char *tmp = malloc(sizeof(char));

if((dir = opendir(dirname)) == NULL)
{
    perror("Unable to open directory");
    DisplayFolderNotFound();
    return(0);
}

while((ent = readdir(dir)) != NULL)
{
    strcpy(name, ent->d_name);
    strncpy(tmp, name, 1);
    if(strcmp(tmp, ".") == 0)   //Not valid directories. These are dir's created by system and are hidden.
    continue;

    char dirCopy[strlen(dirname)+ strlen(name) + 1 /* for slash */  + 1 /*for null character*/];

    strcpy(dirCopy, dirname);        
    strcat(dirCopy, "/");
    strcat(dirCopy, name);

    struct stat s;

    if( stat(dirCopy, &s) == 0 )
    {
        if( s.st_mode & S_IFDIR )
        {
            //              it's a directory
            //              printf("Directory [%s]\n", dirCopy);
            ScanDirectories(dirCopy, imagesHeadPtr, filesHeadPtr); //Already inside a dir, recursively traverse it.
        }
        else if( s.st_mode & S_IFREG )
        {
            //it's a file
            //printf("File [%s]\n", name);

            ++totalFiles;
        }
        else
        {
            //something else
        }
    }
    else
    {
        //error
        return(0);
    }

    int extensionLength = 0;
    char *endP = name + strlen(name) - 1; //pointer to the last char of filename
    char *temp = endP;

    while (*temp != '.')
    {
        ++extensionLength;
        --temp;
    }

    char *extension = (char *)malloc(sizeof(char)*(extensionLength+1 /* for . */ + 1 /* for null char */));
    strncpy(extension, name+strlen(name)-extensionLength-1, extensionLength+2);

    if(strcmp(extension, ".abc")==0)
    {                
        ++pngs;

        struct images *nextPtr = imagesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->fileName = (char *)malloc(sizeof(char)*(strlen(name)+1));
        strcpy(nextPtr->fileName, name);
        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->isUsed = 0;
        nextPtr->fileSize = GetFileSize(dirCopy)/1000.0;
        nextPtr->next = (struct images *)malloc(sizeof(struct images));
        nextPtr = nextPtr->next;
        nextPtr->fileName = NULL;
        nextPtr->filePath = NULL;
        nextPtr->isUsed = 0;
        nextPtr->fileSize = 0;
        nextPtr->next = NULL;
    }

    else if(strcmp(extension, ".rst")==0)
    {                
        ++jpgs;

        struct images *nextPtr = imagesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->fileName = (char *)malloc(sizeof(char)*(strlen(name)+1));
        strcpy(nextPtr->fileName, name);
        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->isUsed = 0;
        nextPtr->fileSize = GetFileSize(dirCopy)/1000.0;
        nextPtr->next = (struct images *)malloc(sizeof(struct images));
        nextPtr = nextPtr->next;
        nextPtr->fileName = NULL;
        nextPtr->filePath = NULL;
        nextPtr->isUsed = 0;
        nextPtr->fileSize = 0;
        nextPtr->next = NULL;
    }

    else if(strcmp(extension, ".dig") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;        
    }

    else if(strcmp(extension, ".x") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;        
    }

    else if(strcmp(extension, ".cderf") == 0)
    {
        ++filesToScan;

        struct filesToParse *nextPtr = filesHeadPtr;

        while(nextPtr->next != NULL)
        nextPtr = nextPtr ->next;

        nextPtr->filePath = (char *)malloc(sizeof(char)*(strlen(dirCopy)+1));
        strcpy(nextPtr->filePath, dirCopy);
        nextPtr->next = (struct filesToParse *)malloc(sizeof(struct filesToParse));
        nextPtr = nextPtr->next;
        nextPtr->filePath = NULL;
        nextPtr->next = NULL;                        
    } 

        free(extension); //because of this line, my code runs into infinite loop saying
//unable to open dir : too many files open. If I comment this out, my code works fine but the concepts of memory management say that I should be freeing it. 

}

free(tmp);

if (closedir(dir) != 0)
perror("Unable to close directory");

return(1);

最佳答案

更新:为了完整起见,添加另一个观察结果:

tmp处理也坏了;它被分配为 1 个字符的缓冲区,除了空字符串之外,它永远不能容纳任何有效的 C 字符串,因为字符串终止字符需要 1 个字符。

删除 tmp , 并与 ent->d_name 进行比较直接:

if(strcmp(ent->d_name, ".") == 0)
  continue;

然后,这个:

char *endP = name + strlen(name) - 1; //pointer to the last char of filename
char *temp = endP;

while (*temp != '.')
{
    ++extensionLength;
    --temp;
}

对于不包含句点的文件名会严重中断。

更理智的方法是寻找最后一个周期,并处理它不存在的情况:

const char *ldot = strrchr(name, '.');
if(ldot != NULL)
{
  /* Extension found. */
}
else
{
  /* No extension. */
}

当然是用<a href="http://linux.die.net/man/3/strrchr" rel="noreferrer noopener nofollow">strrchr()</a>之类的库函数如果文件名采用多字节编码,则文件名不安全;你需要意识到这一点。

关于C目录扫描进入死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12036969/

相关文章:

c - C语言如何将字符数组转换为二进制数组

c - C中3个点之间的角度

c - 如何在C中找到文本文件最长行的长度

c++ reinterpret cast和c style cast之间的区别

c - 从不兼容的 void * 分配 int*

python - 导入错误 : No module name libstdcxx

c - 为什么这个程序没有打印出想要的输出?

c - 分配新节点后根节点的值(空值)没有改变

c++ - Pi 计算器程序每次运行时都会给出不同的输出

c++ - 找不到 PortAudio 符号 : _PaMacCore_SetupChannelMap on Mountain Lion