Merge pull request #1 from tvdu29/dev2

corrected env -i crash & SHLVL
This commit is contained in:
tvdu29 2020-01-28 13:42:43 +01:00 committed by GitHub
commit 64bd25ae09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 12 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/25 18:03:28 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: 2020/01/25 15:06:00 by tmaze ### ########.fr */
/* Updated: 2020/01/26 22:19:25 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,4 +36,6 @@ char **res_ext(char **argv, t_env *env);
int exec_cmd(char **argv, t_env **env);
void *ft_strdel_null(char **s);
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,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
/* Updated: 2020/01/25 17:00:23 by tmaze ### ########.fr */
/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */

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,15 +50,21 @@ t_env *env2lst(char **env)
while (env[i])
{
if ((new = ft_envnew(env[i])) != NULL)
ft_envaddend(&ret, new);
{
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);
ft_envdel(&ret);
return (NULL);
ft_envdelelem(&new);
ft_envdel(&ret);
return (NULL);
}
i++;
}
if (i == 0 && ret == NULL && (ret = def_env()) == NULL)
ft_printf("Env: Memory Error\n");
return (ret);
}

View File

@ -157,9 +157,9 @@ void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env)
int exec_cmd(char **argv, t_env **env)
{
int ret;
char **env_tab;
char *path;
int ret;
char **env_tab;
char *path;
if ((env_tab = ft_envtotab(*env)) == NULL)
return (-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);
}