C Dir扫描进入无限循环

时间:2012-08-20 11:39:22

标签: 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);

}

3 个答案:

答案 0 :(得分:3)

UPDATE :为了完整起见,添加另一个观察:

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. */
}

当然,如果文件名采用多字节编码,则在文件名上使用strrchr()等库函数是不安全的。你需要意识到这一点。

答案 1 :(得分:2)

C库函数strcmp()在字符串不同或达到\0(字符串结尾)时停止比较:http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

if(strcmp(tmp, ".") == 0)   //Not valid directories. These are dir's created by system and are hidden.
    continue;

但是tmp字符串没有空终止符\0。因此比较产生无限循环。

为tmp string保留2个字节而不是1个字节:

char *tmp = malloc(2*sizeof(char));

将终止符\0添加到字符串:

tmp[1]='\0';

答案 2 :(得分:0)

我还没有阅读所有代码,但我可能在这里发生错误:

if(strcmp(tmp, ".") == 0)   //Not valid directories. These are dir's created by system and are hidden.
continue;

使用这种结构你只是排除。 (当前)目录。所以你不排除..(父)目录,这会导致循环。除了你想要排除隐藏的dirrectories,所以最好的想法是使用这种结构:

if (tmp[0] == '.')