corrected env -i crash & SHLVL

This commit is contained in:
Tanguy Maze 2020-01-28 13:38:25 +01:00
parent 40718a99ac
commit 25c4b28f4c
7 changed files with 82 additions and 31 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2020/01/22 17:50:24 by tmaze ### ########.fr #
# Updated: 2020/01/26 22:21:26 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -43,7 +43,8 @@ SRC = main.c \
cmd_cd.c \
cmd_echo.c \
ms_exec.c \
ms_ext.c
ms_ext.c \
put_error_cd.c
OBJ = $(SRC:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
/* Updated: 2019/12/06 09:54:47 by tmaze ### ########.fr */
/* Updated: 2020/01/26 22:19:25 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,4 +35,6 @@ char **res_ext(char **argv, t_env *env);
int exec_cmd(char **argv, t_env **env);
void *put_error_cd(char *file, char *msg);
#endif

View File

@ -17,7 +17,7 @@ char *ft_envtochar(t_env *env)
int size;
char *ret;
size = ft_strlen(env->key) + ft_strlen(env->val) + 1;
size = ft_strlen(env->key) + ft_strlen(env->val) + 2;
if ((ret = ft_strnew(size)) != NULL)
{
ft_strlcpy(ret, env->key, size);

View File

@ -6,21 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
/* Updated: 2020/01/23 23:37:53 by tmaze ### ########.fr */
/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void *put_error_cd(char *file, char *msg)
{
ft_putstr("cd: ");
ft_putstr(file);
ft_putstr(": ");
ft_putendl(msg);
return (NULL);
}
char *check_path_slash_cd(char *exec)
{
int i;

View File

@ -12,6 +12,33 @@
#include "minishell.h"
t_env *def_env(void)
{
t_env *ret;
t_env *n;
char p[PATH_MAX];
n = ft_memalloc(sizeof(t_env));
if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX)))
ft_envdelelem(&n);
if (n != NULL)
ft_strcpy(n->key, "PWD");
if (n != NULL && (n->val = ft_strdup(p)) == NULL)
ft_envdelelem(&n);
if (n != NULL)
ft_envaddend(&ret, n);
n = ft_memalloc(sizeof(t_env));
if (n != NULL && (n->key = ft_strnew(5)) == NULL)
ft_envdelelem(&n);
if (n != NULL)
ft_strcpy(n->key, "SHLVL");
if (n != NULL && (n->val = ft_strdup("1")) == NULL)
ft_envdelelem(&n);
if (n != NULL)
ft_envaddend(&ret, n);
return (ret);
}
t_env *env2lst(char **env)
{
t_env *ret;
@ -23,7 +50,11 @@ t_env *env2lst(char **env)
while (env[i])
{
if ((new = ft_envnew(env[i])) != NULL)
{
if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val))
new->val = ft_itoa(ft_atoi(new->val) + 1);
ft_envaddend(&ret, new);
}
else
{
ft_envdelelem(&new);
@ -32,6 +63,8 @@ t_env *env2lst(char **env)
}
i++;
}
if (i == 0 && ret == NULL && (ret = def_env()) == NULL)
ft_printf("Env: Memory Error\n");
return (ret);
}

View File

@ -141,6 +141,17 @@ char *check_path(char *exec, t_env *env)
return (ret);
}
void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env)
{
execve(path, argv, env_tab);
ft_putendl_fd("minishell: error", 1);
if (ft_strcmp(path, argv[0]) != 0)
ft_strdel(&path);
ft_del_words_tables(&env_tab);
ft_envdel(env);
exit(-1);
}
int exec_cmd(char **argv, t_env **env)
{
int ret;
@ -151,19 +162,10 @@ int exec_cmd(char **argv, t_env **env)
return (-1);
if ((path = check_path(argv[0], *env)) == NULL)
ft_del_words_tables(&env_tab);
ft_printf("path after resolve: %s\n", path);
if (path == NULL)
return (-1);
if ((ret = fork()) == 0)
{
execve(path, argv, env_tab);
ft_putendl_fd("minishell: error", 1);
if (ft_strcmp(path, argv[0]) != 0)
ft_strdel(&path);
ft_del_words_tables(&env_tab);
ft_envdel(env);
exit(-1);
}
exec_cmd_child(path, argv, env_tab, env);
else if (ret == -1)
ft_putendl_fd("minishell: error", 1);
if (ret == -1)

22
srcs/put_error_cd.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_error_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/26 22:18:09 by tmaze #+# #+# */
/* Updated: 2020/01/26 22:18:24 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void *put_error_cd(char *file, char *msg)
{
ft_putstr("cd: ");
ft_putstr(file);
ft_putstr(": ");
ft_putendl(msg);
return (NULL);
}