Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c中模拟cd的错误处理_C_Bash - Fatal编程技术网

如何在c中模拟cd的错误处理

如何在c中模拟cd的错误处理,c,bash,C,Bash,我正在从事一个C项目,其目标是使用C创建一个模拟bash 现在我试图模拟cd的错误,我遇到了这个错误 注意:我不允许使用exec函数 我为什么问这个问题: 我在处理问题时遇到了一个问题这就是问题: 当我创建一个目录并将cd放入该目录并在其中时将其删除时,我在bash中收到以下错误消息: cd:检索当前目录时出错:getcwd:无法访问父目录:没有这样的文件或目录 这是BASH的hole脚本的屏幕截图: 有什么方法可以处理这个错误吗 cd的代码: int ft_cd(t_args *args,

我正在从事一个C项目,其目标是使用C创建一个模拟bash

现在我试图模拟cd的错误,我遇到了这个错误

注意:我不允许使用exec函数

我为什么问这个问题:

我在处理问题时遇到了一个问题这就是问题: 当我创建一个目录并将cd放入该目录并在其中时将其删除时,我在bash中收到以下错误消息:

cd:检索当前目录时出错:getcwd:无法访问父目录:没有这样的文件或目录

这是BASH的hole脚本的屏幕截图:

有什么方法可以处理这个错误吗

cd的代码:

int ft_cd(t_args *args, t_env **head)
{
    char *buff;
    char *dir;
    t_env *temp;
    
    buff = NULL;
    if (args == NULL)
    {
        temp = ft_search_in_list(head, "HOME");
        if (temp == NULL)
            return (ft_put_err("cd:", " HOME not set", 1));
        dir = temp->value;
    }
    else if (args->next != NULL)
        return (ft_put_err("cd:", " too many arguments", 1));
    else if (!(ft_strcmp(args->value, "-")))
    {
        temp = ft_search_in_list(head, "OLDPWD");
        if (temp == NULL)
            return (ft_put_err("cd:", " OLDPWD not set", 1));
        dir = temp->value;
    }
    else
        dir = args->value;
    if (!getcwd(buff, 100))
        ft_put_err("getcwd: ", strerror(errno), 1);
    temp = ft_search_in_list(head, "PWD");
        if (temp == NULL)
            ft_add_to_list(head, ft_create_node("PWD", ""));
    temp = ft_search_in_list(head, "OLDPWD");
        if (temp == NULL)
            ft_add_to_list(head, ft_create_node("OLDPWD", ""));
    if (chdir(dir) == -1)
        return (ft_put_err("cd: ", ft_strjoin(dir, ft_strjoin(": ", strerror(errno))), 1));
    // ft_strjoin(dir, ": No such file or directory")
    temp = ft_search_in_list(head, "PWD");
    ft_replaceit(head, "OLDPWD", temp->value);
    ft_replaceit(head, "PWD", getcwd(buff, 100));
    return (0);
}
解释代码:
t_env**head
:这是一个包含环境的链接列表。
t_args*arg
:这是一个包含所有命令args的链接列表。

检索当前目录时出现的消息
错误:
来自哪里?我在您的代码中看不到这一点。消息不是来自我的代码此消息来自bash(屏幕截图不是来自我的代码它来自bash)
getcwd()
如果当前目录已被删除,则无法工作。它通过跟踪每个目录中的父目录链接来工作,但是删除一个目录会将其从父目录中删除。@Barmar那么我如何处理这个错误呢?更改
ft\u put\u err(“getcwd:,strerror(errno),1)
to
ft\u put\u err(“getcwd:error检索当前目录:something seomthing”,strerror(errno),1)?请不要张贴代码的图片。Bash是开源的。