1
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);

}

4

3 回答 3

3

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

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()如果文件名采用多字节编码,则使用诸如文件名之类的库函数是不安全的。你需要意识到这一点。

于 2012-08-20T11:41:34.663 回答
2

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

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

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

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

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

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

tmp[1]='\0';
于 2012-08-20T11:44:42.647 回答
0

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

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

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

if (tmp[0] == '.')
于 2012-08-20T11:47:18.207 回答