Readdir()结果添加到链表导致seg错误

时间:2018-01-25 00:05:50

标签: c linux linked-list

所以我循环读取readdir()函数并将生成的文件名添加到链表中的新节点。通过设置#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); } 解决问题后,我遇到并发出dir_list循环在访问目录时遇到问题的位置。

/**
 * 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);
}

代码:

{{1}}

add_file_list():

{{1}}

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

1 个答案:

答案 0 :(得分:0)

这是错误的,正如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