/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* s_init_next.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #ifdef __linux__ # include #else # include #endif #include #include #include #include #include "b_export_next.h" #include "f_fail.h" #include "s_destroy.h" #include "s_struct.h" #include "u_vars.h" #include "u_vars_next.h" static void s_set_cwd_next(char *cwd, char *fmt, t_msh *msh) { if (getcwd(cwd, PATH_MAX) != NULL) { ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); } else { ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); } } void s_set_cwd(char cwd[], t_msh *msh) { char fmt[PATH_MAX]; DIR *dir; u_get_var_value(cwd, "$PWD", PATH_MAX, msh); if (cwd[0] == C_NUL) { if (getcwd(cwd, PATH_MAX) != NULL) { ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); } else { ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); } return ; } if ((dir = opendir(cwd)) != NULL) { closedir(dir); } else if (errno == ENOENT) { s_set_cwd_next(cwd, fmt, msh); } }