gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:36 25 4
gpt4 key购买 nike

所以我循环调用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com