尝试读取linux中的隐藏文件时出现分段错误

时间:2013-10-12 17:18:19

标签: c linux pointers malloc realloc

我的程序读取命令行指定的目录的内容。它以递归方式读取目录,即如果我们正在读取目录“test”的内容,并且在其中我们有另一个目录“inside”,那么它还将读取名为“inside”的目录的内容。问题是,如果我不读取隐藏目录,即以“。”开头的目录,它可以正常工作。 。但是如果我也读了隐藏的目录,它会说分段错误。

代码如下:

主文件:

#include "helper.h"

/*
 * Display's content of String array passed to it,
 * that should conatin full path to files.
 */
void display(char **);

/*
 * Free's the memory utilized by the program
 */
void cleanup(char **);

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: %s [dir]\n\n", argv[0]);
        return 1;
    }

    char **files = calloc(1, sizeof(char *));

    // get files from the directory specified
    getFiles(&files, argv[1]);

    // display files
    display(files);

    // free memory utilized by files array
    cleanup(files);

// that's all folks
return 0;
}


/*
 * Display's content of String array passed to it,
 * that should conatin full path to files.
 */
void display(char **files)
{
    // Color Red
//  printf("[0;31;40m");

    // display files
    for (int i = 0; files[i]; i++)
    {
        printf("%s\n", files[i]);
    }

    // turn off color
//  printf("[0;37;40m");
}


/*
 * Free's the memory utilized by the program
 */
void cleanup(char **files)
{
    // free memory utilized by files array
    for (int i = 0; files[i]; i++)
        free(files[i]);
    free(files);
}

getFiles函数在helpers.c文件中定义,其中包含以下代码:

#include "helper.h"
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>

/*
 * Stores the list of files present in direectory pointed by 'dir' 
 * in array of strings pointed by 'files'
 */
void getFiles(char ***files, const char* dir)
{
    static int i;

    // ensure directory is valid
    if (dir == NULL)
    {
        printf("Error: Invalid Directory\n\n");
        exit(1);
    }

    // declare and initialize directory handler
    DIR *dd = opendir(dir);
    if (dd == NULL)
    {
        printf("Error: Directory Not Found\n\n");
        exit(2);
    }

    // structure that store file attributes read
    struct dirent *content;

    // read directory until all files are scanned
    while ((content = readdir(dd)) != NULL)
    {
        // ignore '.' and '..' directories
        if (strcmp(content->d_name, ".") == 0 || 
            strcmp(content->d_name, "..") == 0)
            continue;
        /*if (content->d_name[0] == '.')
            continue;*/

        //store full file path from current directory
        char temp[1024] = {0};

        // make full path
        makepath(temp, dir, content->d_name);

        // recall itself if another directory found
        if (isdir(temp))
        {
            // read this new directory found
            getFiles(files, temp);
            continue;
        }

        // allocate memory to store locations of char *
        *files = realloc(*files, (i + 2)*(sizeof(char *)));

        // allocate heap memory and store location
        *(*(files + 0) + i) = (char *)strdup(temp);

        // move to next location
        i++;
    }

    // free directory handler
    closedir(dd);

    // set NULL after last file name
    *(*(files + 0) + i) = '\0';
}


/*
 * returns true if 'dir' refers to a directory, false otherwise
 */
bool isdir(const char * dir)
{
    DIR *temp;
    temp = opendir(dir);

    if (temp != NULL)
    {
        closedir(temp);
        return true;
    }

return false;
}


/*
 * appends dir and file/directory name to src, 
 * thus makes a full file/directory path, from current directory
 */
void makepath(char src[], const char *dir, const char *file)
{   
    // prepend directory name
    strcat(src, dir);
    strcat(src, "/");

    // append file/directory name
    strcat(src, file);
}

我在helper.h文件中包含了必要的头文件。

我也想知道我在内存分配方面犯了错误。 (在getFiles函数中重新分配)。

此时忽略隐藏文件行已被评论。

/*if (content->d_name[0] == '.')
                continue;*/

如果我取消注释上述行,那么程序运行正常。

如果您正在考虑为什么我将文件名存储为readdir函数读取,因为稍后这些名称对我来说是必要的,这就是我不能立即显示文件名的原因。

任何建议我如何更好地实现此程序,以及如何解决当我读取隐藏目录时发生的问题。

1 个答案:

答案 0 :(得分:1)

我不知道这是不是问题,但在这里:

// set NULL after last file name
*(*(files + 0) + i) == '\0';

                     ^ you are not setting to NULL, you are comparing
相关问题