diff --git a/Makefile b/Makefile index d21b734..23fb8cd 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# # -# Updated: 2020/02/01 15:54:10 by tmaze ### ########.fr # +# Updated: 2020/02/14 11:56:31 by tmaze ### ########.fr # # # #******************************************************************************# @@ -38,15 +38,17 @@ INCDIR = includes libft/includes # Source files SRC = main.c \ cmd_env.c \ - cmd_setenv.c \ - cmd_unsetenv.c \ + cmd_setenv.c \ + cmd_unsetenv.c \ cmd_cd.c \ + cmd_cd_utils.c \ cmd_echo.c \ ms_exec.c \ ms_ext.c \ - put_error_cd.c \ - ms_exec_utils.c \ + put_error_cd.c \ + ms_exec_utils.c \ def_env.c \ + envnew.c \ check_path_slash_cd.c OBJ = $(SRC:.c=.o) diff --git a/includes/minishell.h b/includes/minishell.h index f23818c..23307de 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -50,4 +50,6 @@ t_env *def_env(void); void *return_null(char *msg); +t_env *cd_updateenv(char *key, char *val, t_env **env); + #endif diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 9204891..0d6af2f 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -374,6 +374,7 @@ int ft_putendl_fd2(char const *s, int fd); void ft_envdelelem(t_env **elem); void ft_envdel(t_env **env); t_env *ft_envnew(char *env); +t_env *ft_envnewinit(char *key, char *val, t_env *env); t_env *ft_envaddend(t_env **alst, t_env *new); t_env *ft_envgetelem(char *key, t_env *env); char *ft_envtochar(t_env *env); diff --git a/libft/srcs/ft_envnew.c b/libft/srcs/ft_envnew.c index 9994459..1d70b74 100644 --- a/libft/srcs/ft_envnew.c +++ b/libft/srcs/ft_envnew.c @@ -27,3 +27,18 @@ t_env *ft_envnew(char *env) ft_envdelelem(&ret); return (ret); } + +t_env *ft_envnewinit(char *key, char *val, t_env *env) +{ + t_env *ret; + int i; + + i = 0; + ret = NULL; + if ((ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL) + if ((ret->key = ft_strdup(key)) != NULL) + if ((ret->val = ft_strdup(val)) != NULL) + return (ret); + ft_envdelelem(&ret); + return (ret); +} diff --git a/srcs/cmd_cd.c b/srcs/cmd_cd.c index dfb0605..b5d6f72 100644 --- a/srcs/cmd_cd.c +++ b/srcs/cmd_cd.c @@ -25,8 +25,8 @@ int cmd_cd_update_env(char *path, t_env **env, char opt) if (getcwd(p_path, 4096) == NULL || (oldpwd = ft_strdup(pwd->val)) == NULL || ft_realpath(path, &c_path) == NULL) return (put_error_cd2(path, "error")); - if (ft_envupdate("PWD", *env, ((opt == 'P') ? p_path : c_path)) == NULL - || ft_envupdate("OLDPWD", *env, oldpwd) == NULL) + if (cd_updateenv("PWD", ((opt == 'P') ? p_path : c_path), env) == NULL + || cd_updateenv("OLDPWD", oldpwd, env) == NULL) { ft_strdel(&oldpwd); ft_strdel(&c_path); diff --git a/srcs/main.c b/srcs/main.c index d4bc335..9ab885d 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -78,14 +78,15 @@ int cleanup(char **argv, t_env *env) return (0); } -int main(void) +int main(int ac , char **av, char **envp) { - extern char **environ; char **argv; t_env *env; char *cmd; - if ((env = env2lst(environ)) == NULL) + (void)ac; + (void)av; + if ((env = env2lst(envp)) == NULL) return (2); while (1) {