c - Readdir() 结果添加到链表导致段错误

标签 c linux linked-list

所以我循环调用 readdir() 函数并将生成的文件名添加到链表中的新节点。通过设置 file_list = add_file_node() 解决问题后,我遇到了 dir_list 循环在访问目录时遇到问题的问题。

hls: cannot access hls: cannot access h: No such file or directory

代码:

#include "header.h"


/**
 * main - main ls function
 *
 * @argc: argument count
 * @argv: argument vector
 *
 * Return: 0, or the errno of the error
 */
int main(int argc, char **argv)
{
    struct dirent *read;
    char dir[400], error_message[400], format, hidden;
    int i, j, dir_count, max_src_bytes = 397;
    dir_list_t *dir_list, *dir_node;
    file_list_t *file_list;
    DIR *dirp;
    int errno;

    format = ' ';
    hidden = ' ';
    dir_count = 0;

    strcpy(dir, ".");
    for (i = 1; i < argc; i++)
    {
        if (argv[i][0] == '-')
        {
            for (j = 1; argv[i][j]; j++)
            {
                if (argv[i][j] == '1')
                    format = '1';
                else if (argv[i][j] == 'l')
                    format = 'l';
                if (argv[i][j] == 'a')
                    hidden = 'a';
                else if (argv[i][j] == 'A')
                    hidden = 'A';
            }
        }
        else
        {
            memset(dir, 0, strlen(dir));
            strcpy(dir, argv[i]);
            dir_list = add_dir_list(&dir_list, dir);
            dir_count++;
        }
    }

    if (dir_count == 0)
        dir_list = add_dir_list(&dir_list, dir);


    for (dir_node = dir_list; dir_node != NULL; dir_node = dir_node->next)
    {
        dirp = opendir(dir_node->dir);
        if (dirp == NULL)
        {
            strcpy(error_message, "hls: cannot access ");
            max_src_bytes = 381;
            perror(strncat(error_message, dir_node->dir, max_src_bytes));
            return (errno);
        }
        if (dir_count > 1)
            printf("%s:\n", dir_node->dir);


        while ((read = readdir(dirp)) != NULL)
        {
            file_list = add_file_list(&file_list, read->d_name);
        }


        switch (format)
        {
            case '1':
                print_ls(hidden, '\n', file_list);
                break;
            case 'l':
                print_ls(hidden, '\n', file_list);
                break;
            default:
                print_ls(hidden, '\t', file_list);
        }
        if (dir_node->next != NULL)
            putchar('\n');

        free_file_list(&file_list);
    }

    free_dir_list(&dir_list);
    closedir(dirp);
    return (0);
}


/**
 * print_ls - print contents in the default ls format, i.e. columns
 *
 * @hidden: parameter denoting the option for revealing hidden files
 * @format: printing format parameter
 * @dirp: pointer to the directory data
 *
 * Return: 0 for success, 1 for failure
 */
int print_ls(char hidden, char format, file_list_t *file_list)
{
    file_list_t *file_node;

    for (file_node = file_list; file_node != NULL; file_node = file_node->next)
    {
        if (hidden == 'a')
        {
            printf("%s", file_list->file);
            if (file_list->next != NULL)
                putchar(format);
        }
        else if (hidden == 'A')
        {
            if (strcmp(file_list->file, ".") != 0 &&
                strcmp(file_list->file, "..") != 0)
            {
                printf("%s", file_list->file);
                if (file_list->next != NULL)
                    putchar(format);
            }
        }
        else
        {
            if (file_list->file[0] != '.')
            {
                printf("%s", file_list->file); // (line 139)
                if (file_list->next != NULL)
                    putchar(format);
            }
        }
    }
    if (format == '\t')
        printf("\n");

    return (0);
}

添加文件列表():

/**
 * add_file_list - add a new node at the start of a file_list_t linked list
 *
 * @head: start of linked list
 * @file: file data to add to node
 *
 * Return: address of new node; NULL if failure
 */
file_list_t *add_file_list(file_list_t **head, const char file[256])
{
    file_list_t *node;

    node = malloc(sizeof(file_list_t));
    if (node == NULL)
        return (NULL);
    strcpy(node->file, file);
    node->next = *head;
    node->prev = NULL;
    *head = node;
    return (node);
}

我正在考虑用一组指针来尝试这个,但我不想在获得一些见解之前丢弃我的代码。我没有正确地将数据输入到节点中吗?如果是这样,我该怎么做?

最佳答案

这是错误的,正如valgrind所说

   file_head = file_list; <<<< file_list is not initlaized, file_head = junk
    while ((read = readdir(dirp)) != NULL)
    {
        printf("read: %s\n", read->d_name);
        append_file_list(&file_list, read->d_name);
        printf("file_list: %s\n", file_list->file);
    }
    printf("file_head: %s\n", file_head->file); <<<<<= (line 78) file_head = junk

关于c - Readdir() 结果添加到链表导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433662/

相关文章:

c - 如何打印(C)中链表上的所有节点

c - 如果数组中的每个整数都不同,我该如何编写一个返回 true 的函数?

c - 抢占式调度算法

c - 大型网络中的 XBee 通信

linux - 我在Linux中安装了kibana,但无法正常工作

c++ - 我的单链表中的一个函数(在找到特定节点后添加一个新节点)使我的程序崩溃

c++ - 在编译时不知道返回类型时如何避免向下转换?

c++ - block 级使用LRU方法

linux - 程序集:试图写入文件,但文本附加到文件名

linux - 内核上下文中的进程可以在持有互斥锁时休眠吗?