diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index adb0c8f4..c2323c21 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_cd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:29:14 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 13:51:18 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ static char *builtin_cd_special(char *const av[], char *const env[]) return (NULL); } else if (ft_strcmp(*av, "-") == 0) - target = ft_getenv((char**)env, "OLDPWD"); + target = ft_strdup(ft_getenv((char**)env, "OLDPWD")); else target = *av; return (target); @@ -66,12 +66,15 @@ int builtin_cd(const char *path, char *const av[], char *const envp[]) int i; int opts; char *target; + char *cwd; opts = 0; i = builtin_cd_opts(av, &opts); if (!(target = builtin_cd_special(av + i, envp))) return (0); - builtin_setenv(path, (char*[3]){"OLDPWD", getcwd(NULL, 0)}, envp); + cwd = getcwd(NULL, 0); + builtin_setenv(path, (char*[3]){"OLDPWD", cwd, NULL}, envp); + free(cwd); if (chdir(target)) { ft_printf(CDERR_1, target); @@ -79,6 +82,10 @@ int builtin_cd(const char *path, char *const av[], char *const envp[]) } else if (target != av[i]) ft_printf("%s\n", target); - builtin_setenv(path, (char*[3]){"PWD", getcwd(NULL, 0)}, envp); + cwd = getcwd(NULL, 0); + builtin_setenv(path, (char*[3]){"PWD", cwd, NULL}, envp); + free(cwd); + if (!ft_strcmp(*(av + i), "-")) + free(target); return (0); } diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index f65e7357..192ca25b 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/02/17 10:27:05 by gwojda ### ########.fr */ +/* Updated: 2017/02/17 11:06:40 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 80359846..3c803bd5 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_setenv.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/02/06 14:05:23 by jhalford ### ########.fr */ +/* Updated: 2017/02/17 13:18:25 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */