From 79ac0a8edcb88a16ae4bc26d82f9900e840882f6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 23:05:52 +0100 Subject: [PATCH] unsetenv OK --- 42sh/libft/src/sys/fd_replace.c | 5 +-- 42sh/src/builtin/builtin_cd.c | 48 ++++++++++------------------- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/builtin/builtin_unsetenv.c | 4 +-- 42sh/src/exec/plaunch_while.c | 2 +- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index 6af06a9e..495ae484 100644 --- a/42sh/libft/src/sys/fd_replace.c +++ b/42sh/libft/src/sys/fd_replace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:08:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:59:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,6 @@ int fd_replace(int fd1, int fd2) { if (fd1 != fd2) - { - DG("%i ---> %i", fd2, fd1); return (dup2_close(fd1, fd2)); - } return (0); } diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 5e96339f..4d40ae44 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:28:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 23:03:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,13 @@ #define CDERR_2 "cd: HOME not set" #define CDERR_3 "cd: too many arguments{eoc}" +static g_cliopts g_cdotps = +{ + {'P', NULL, CD_OPT_P, CD_OPT_L, NULL}, + {'L', NULL, CD_OPT_L, CD_OPT_P, NULL}, + {0, NULL, 0, 0, NULL}, +} + static char *builtin_cd_special(char *const av[], char *const env[]) { char *target; @@ -44,30 +51,13 @@ static char *builtin_cd_special(char *const av[], char *const env[]) return (target); } -static int builtin_cd_opts(char *const av[], int *opts) +void setwd(char *var) { - int i; - int j; + char *cwd; - i = 1; - if (av) - while (av[i] && av[i][0] == '-' && av[i][1]) - { - j = 0; - while (av[i][++j]) - { - if (av[i][j] == 'P') - *opts = CDOPT_P; - else if (av[i][j] == 'L') - *opts = CDOPT_L; - else if (av[i][j] == '-') - return (i + 1); - else - return (i); - } - ++i; - } - return (i); + cwd = getcwd(NULL, 0); + builtin_setenv(path, (char*[4]){"setenv", var, cwd, NULL}, envp); + free(cwd); } int builtin_cd(const char *path, @@ -76,15 +66,13 @@ int builtin_cd(const char *path, int i; int opts; char *target; - char *cwd; - opts = CDOPT_L; + data->flag = CD_OPT_L; + if (cliopts(av, g_cdopts, &data)) i = builtin_cd_opts(av, &opts); + setwd("OLDPWD"); if (!(target = builtin_cd_special(av + i, envp))) return (1); - cwd = getcwd(NULL, 0); - builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp); - free(cwd); if (chdir(target)) { SH_ERR(CDERR_1, target); @@ -92,9 +80,7 @@ int builtin_cd(const char *path, } else if (target != av[i]) ft_printf("%s\n", target); - cwd = getcwd(NULL, 0); - builtin_setenv(path, (char*[4]){"setenv", "PWD", cwd, NULL}, envp); - free(cwd); + setwd("PWD"); if (!ft_strcmp(*(av + i), "-")) free(target); return (0); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 5dd2bd1f..26852d3d 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/03/23 18:26:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:43:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index a7d2a674..22bea5d2 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ -/* Updated: 2017/03/23 18:24:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:59:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[]) while (*env && (*env)[j]) { if (ft_strcmp((*env)[j], av[i]) == '=' - && ft_strlen(av[1]) == ft_strlenchr((*env)[i], '=')) + && ft_strlen(av[i]) == ft_strlenchr((*env)[j], '=')) ft_sstrdel(*env, j); else j++; diff --git a/42sh/src/exec/plaunch_while.c b/42sh/src/exec/plaunch_while.c index 44c2d554..d4631982 100644 --- a/42sh/src/exec/plaunch_while.c +++ b/42sh/src/exec/plaunch_while.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 17:20:53 by wescande #+# #+# */ -/* Updated: 2017/03/22 19:23:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:43:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */