small fixes

This commit is contained in:
Loic Deridder
2025-02-19 10:17:27 +01:00
parent e673be640c
commit ba28816858
4 changed files with 24 additions and 16 deletions

View File

@@ -27,7 +27,10 @@ int exec_cd(char *path, t_ast_n *head)
src = getcwd(NULL, 0);
if (chdir(path) == -1)
{
ft_free(&src);
return (err_msg_cmd("cd", path, strerror(errno), EXIT_FAILURE));
}
dest = getcwd(NULL, 0);
pwd_update(head, src, dest);
return (0);

View File

@@ -46,16 +46,6 @@ char *ft_getenv(char *str, char **envp)
return (&envp[j][len]);
}
int print_exit(char **arg, t_ast_n *node)
{
if (*arg && ft_strncmp(*arg, "$?", 2) == 0)
{
ft_fprintf(1, "%d", node->msh->ex_code);
return (1);
}
return (0);
}
static void echo_print(t_ast_n *node, int j, char **envp)
{
int i;
@@ -66,10 +56,7 @@ static void echo_print(t_ast_n *node, int j, char **envp)
i = 0;
while (node->args[j][i])
{
if (print_exit(&node->args[j], node))
i += 2;
else
ft_put_c(node->args[j][i++]);
ft_put_c(node->args[j][i++]);
}
j++;
if (node->args[j])

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 10:00:08 by lderidde #+# #+# */
/* Updated: 2025/02/12 13:31:03 by lderidde ### ########.fr */
/* Updated: 2025/02/19 10:07:55 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -84,8 +84,8 @@ int exec(t_ast_n *node)
if (execve(path, node->args, node->msh->env) == -1)
{
free_child(node->msh);
ft_fprintf(2, "minishell: failed to execute %s\n", path);
ft_free(&path);
perror("execve");
exit(1);
}
return (0);

View File

@@ -27,6 +27,23 @@ static char **ft_setnewenv(void)
return (envp);
}
void set_shellenv(t_msh *msh)
{
char *src;
char *tmp;
int sh;
src = getcwd(NULL, 0);
tmp = ft_strjoin(src, "/minishell");
set_var_env("SHELL", tmp, msh);
ft_free(&src);
ft_free(&tmp);
sh = ft_atoi(get_var_value("SHLVL", msh->env));
tmp = ft_itoa(sh + 1);
set_var_env("SHLVL", tmp, msh);
ft_free(&tmp);
}
t_msh *init_msh(char **envp)
{
t_msh *msh;
@@ -46,6 +63,7 @@ t_msh *init_msh(char **envp)
msh->env = ft_setnewenv();
else
msh->env = init_env(envp);
set_shellenv(msh);
return (msh);
}