From b67c7fd7d3b959c5b56f905985aae0ebfa292a64 Mon Sep 17 00:00:00 2001 From: gwojda Date: Wed, 15 Feb 2017 15:46:43 +0100 Subject: [PATCH] ajout d'env avec opt -i -u / export / unset --- 42sh/Makefile | 2 + 42sh/includes/builtin.h | 4 +- 42sh/includes/minishell.h | 2 +- 42sh/src/builtin/builtin_env.c | 146 ++++++++++++++++++++++++------ 42sh/src/builtin/builtin_export.c | 20 ++++ 42sh/src/builtin/builtin_unset.c | 18 ++++ 42sh/src/builtin/is_builtin.c | 4 +- 42sh/src/line-editing/ft_prompt.c | 8 +- 42sh/test | 75 ++++----------- 42sh/testmake | 3 + 10 files changed, 190 insertions(+), 92 deletions(-) create mode 100644 42sh/src/builtin/builtin_export.c create mode 100644 42sh/src/builtin/builtin_unset.c create mode 100644 42sh/testmake diff --git a/42sh/Makefile b/42sh/Makefile index 3ada95e4..31d30f64 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -36,9 +36,11 @@ builtin/builtin_cd.c\ builtin/builtin_echo.c\ builtin/builtin_env.c\ builtin/builtin_exit.c\ +builtin/builtin_export.c\ builtin/builtin_history.c\ builtin/builtin_read.c\ builtin/builtin_setenv.c\ +builtin/builtin_unset.c\ builtin/builtin_unsetenv.c\ builtin/is_builtin.c\ completion/c_binary.c\ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 45db3c5a..bb3c2637 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:07:01 by gwojda ### ########.fr */ +/* Updated: 2017/02/15 11:45:15 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ # include "builtin_read.h" t_execf *is_builtin(t_process *p); +int builtin_export(const char *path, char *const av[], char *const envp[]); +int builtin_unset(const char *path, char *const av[], char *const envp[]); int builtin_env(const char *path, char *const argv[], char *const envp[]); int builtin_echo(const char *path, char *const argv[], char *const envp[]); int builtin_cd(const char *path, char *const argv[], char *const envp[]); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index d1e3fa06..5ba7c179 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/02/10 00:26:10 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 14:20:59 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 1262dd6e..1a8c71b3 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -3,43 +3,131 @@ /* ::: :::::::: */ /* builtin_env.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/01/10 13:08:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 15:45:54 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int builtin_env(const char *path, char *const argv[], char *const envp[]) +/* +** a ajouter dans la lib ? +*/ + +int ft_sstr_found(char **sstr, char *name) { - (void)argv; - (void)envp; - (void)path; + int size; + + size = 0; + if (sstr) + while (sstr[size] && ft_strncmp(name, sstr[size], ft_strlen(name))) + ++size; + return (size); +} + +void ft_sstr_freeone(char **sstr, int index) +{ + char *tmp; + + tmp = sstr[index]; + while (sstr[index]) + { + sstr[index] = sstr[index + 1]; + index++; + } + free(tmp); +} + +/* +** +*/ + +static void ft_env_execute(char *const argv[], char **env) +{ + pid_t soon; + char *path; + char *path_exe; + + path = ft_getenv(env, "PATH"); + path_exe = ft_findexec(path, *argv); + if (!path || !path_exe) + { + ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n", + SHELL_NAME, *argv); + return ; + } + if ((soon = fork())) + wait(&soon); + else + set_exitstatus(execve(path_exe, argv, env), 1); + free(path_exe); +} + +static void ft_illegal_opt_env(char c) +{ + ft_dprintf(2, "{red}env: option requires an argument -- %c\n", c); + ft_dprintf(2, "usage: env\t[-iv] [-P utilpath] [-S string]"); + ft_dprintf(2, " [-u name]\n\t\t[name=value ...] "); + ft_dprintf(2, "[utility [argument ...]]{eoc}\n"); +} + +static int ft_check_env_opt(char ***argv, char ***env) +{ + if (!ft_strcmp(**argv, "-i")) + { + ft_sstrfree(*env); + *env = NULL; + ++(*argv); + } + else if (!ft_strcmp(**argv, "-u")) + { + ++(*argv); + if (!**argv) + { + ft_illegal_opt_env('u'); + return (1); + } + ft_sstr_freeone(*env, ft_sstr_found(*env, **argv)); + ++(*argv); + } + else if (***argv == '-') + { + ft_illegal_opt_env(*(**argv + 1)); + return (1); + } return (0); } -/* int builtin_env(char **av, t_data *data) */ -/* { */ -/* int i; */ -/* char **env; */ -/* i = 1; */ -/* env = NULL; */ -/* if (!av[1]) */ -/* { */ -/* ft_sstrprint(data->env, '\n'); */ -/* ft_putchar('\n'); */ -/* } */ -/* else */ -/* { */ -/* while (av[i] && ft_strchr(av[i], '=')) */ -/* { */ -/* env = ft_sstradd(env, av[i]); */ -/* i++; */ -/* } */ -/* if (av[i]) */ -/* ft_cmd_process(av + i); */ -/* } */ -/* return (0); */ -/* } */ +int builtin_env(const char *path, char *const argv[], char *const envp[]) +{ + char **env; + + (void)path; + env = ft_sstrdup((char **)envp); + while (*argv) + { + if (ft_check_env_opt((char ***)&argv, (char ***)&env)) + break ; + while (*argv && ft_strrchr(*argv, '=')) + { + env = ft_sstradd(env, *argv); + ++argv; + } + if (env && (!*argv || (!ft_strcmp(*argv, "env") && !*(argv + 1)))) + { + ft_sstrprint(env, '\n'); + ft_putchar('\n'); + return (0); + } + if (*argv && ft_strcmp(*argv, "env")) + { + ft_env_execute(argv, env); + return (0); + } + if (*argv) + ++argv; + } + return (0); +} diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c new file mode 100644 index 00000000..b5fd1b4d --- /dev/null +++ b/42sh/src/builtin/builtin_export.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_export.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ +/* Updated: 2017/02/15 11:46:20 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_export(const char *path, char *const av[], char *const envp[]) +{ + if (ft_strcmp(av[0], "export") == 0) + av++; + return (builtin_setenv(path, av, envp)); +} diff --git a/42sh/src/builtin/builtin_unset.c b/42sh/src/builtin/builtin_unset.c new file mode 100644 index 00000000..c3afa192 --- /dev/null +++ b/42sh/src/builtin/builtin_unset.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/15 11:43:34 by gwojda #+# #+# */ +/* Updated: 2017/02/15 11:58:22 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_unset(const char *path, char *const av[], char *const envp[]) +{ + return (builtin_unsetenv(path, av, envp)); +} diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index eba6e445..8dd86660 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/01/26 14:58:02 by gwojda ### ########.fr */ +/* Updated: 2017/02/15 11:44:31 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ t_stof g_builtin[] = { {"echo", &builtin_echo}, {"cd", &builtin_cd}, + {"export", &builtin_export}, + {"unset", &builtin_unset}, {"setenv", &builtin_setenv}, {"unsetenv", &builtin_unsetenv}, {"env", &builtin_env}, diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 5e6e21b1..264e9237 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ -/* Updated: 2017/02/14 14:13:05 by gwojda ### ########.fr */ +/* Updated: 2017/02/15 11:52:24 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,12 @@ static int ft_git_status(void) ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line); free(tmp); } + else + { + line = ft_strdup(line + 3); + ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line); + free(tmp); + } if (!get_next_line(pip[0], &tmp)) printf("\x1b[38;5;83m %C ", L'✓'); else diff --git a/42sh/test b/42sh/test index 4e559b24..40044643 100644 --- a/42sh/test +++ b/42sh/test @@ -1,62 +1,19 @@  shell_init.c 28 interactive shell settings - main.c 90 start of shell pid=44206 pgrp=44206 job_control is ON - lexer_end.c 7 check - main.c 65 [history] stack=[0] state=[4] - token_print.c 29 13:[history] - token_print.c 29 13:[history] - lexer_end.c 7 check - main.c 65 [history] stack=[0] state=[4] - token_print.c 29 13:[history] - token_print.c 29 13:[history] - lexer_end.c 7 check - main.c 65 [aaaaaaa] stack=[0] state=[4] - token_print.c 29 13:[aaaaaaa] - token_print.c 29 13:[aaaaaaa] - lexer_end.c 7 check - main.c 65 [ssssss] stack=[0] state=[4] - token_print.c 29 13:[ssssss] - token_print.c 29 13:[ssssss] - lexer_end.c 7 check - main.c 65 [ddddddddd] stack=[0] state=[4] - token_print.c 29 13:[ddddddddd] - token_print.c 29 13:[ddddddddd] - lexer_end.c 7 check - main.c 65 [ffffffffffffff] stack=[0] state=[4] - token_print.c 29 13:[ffffffffffffff] - token_print.c 29 13:[ffffffffffffff] - lexer_end.c 7 check - main.c 65 [ffffffffffffff] stack=[0] state=[4] - token_print.c 29 13:[ffffffffffffff] - token_print.c 29 13:[ffffffffffffff] - lexer_end.c 7 check - main.c 65 [history] stack=[0] state=[4] - token_print.c 29 13:[history] - token_print.c 29 13:[history] - lexer_end.c 7 check - main.c 65 [history] stack=[0] state=[4] - token_print.c 29 13:[history] - token_print.c 29 13:[history] - lexer_end.c 7 check - main.c 65 [ls] stack=[0] state=[4] - token_print.c 29 13:[ls] - token_print.c 29 13:[ls] - job_wait.c 20 gonna wait [1] - lexer_end.c 7 check - main.c 65 [ls -la] stack=[0] state=[4] - token_print.c 29 13:[ls] - token_print.c 29 13:[-la] - token_print.c 29 13:[ls] - token_print.c 29 13:[-la] - job_wait.c 20 gonna wait [1] - lexer_end.c 7 check - main.c 65 [ls -la] stack=[0] state=[4] - token_print.c 29 13:[ls] - token_print.c 29 13:[-la] - token_print.c 29 13:[ls] - token_print.c 29 13:[-la] - job_wait.c 20 gonna wait [1] - lexer_end.c 7 check - main.c 65 [history] stack=[0] state=[4] - token_print.c 29 13:[history] + main.c 88 start of shell JOBC is ON + main.c 64 [env -o] stack=[0] state=[4] + token_print.c 29 13:[env] + token_print.c 29 13:[-o] + main.c 64 [env -u] stack=[0] state=[4] + token_print.c 29 13:[env] + token_print.c 29 13:[-u] + main.c 64 [histor] stack=[0] state=[4] + token_print.c 29 13:[histor] + main.c 64 [history] stack=[0] state=[4]  token_print.c 29 13:[history] + main.c 64 [env -u LESS] stack=[0] state=[4] + token_print.c 29 13:[env] + token_print.c 29 13:[-u] + token_print.c 29 13:[LESS] + main.c 64 [env ] stack=[0] state=[0] + token_print.c 29 13:[env]  shell_exit.c 17 shell_exit() diff --git a/42sh/testmake b/42sh/testmake new file mode 100644 index 00000000..e0995542 --- /dev/null +++ b/42sh/testmake @@ -0,0 +1,3 @@ + shell_init.c 28 interactive shell settings + main.c 88 start of shell JOBC is ON + shell_exit.c 17 shell_exit()