From d5a99a7acc0bb37b204a121552dd6cfd249dc7a2 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 18:41:29 +0100 Subject: [PATCH 1/2] gonna start work, cliopts first --- 42sh/libft/src/sstr/ft_sstrdel.c | 3 ++- 42sh/src/builtin/builtin_env.c | 8 ++++---- 42sh/src/builtin/builtin_unsetenv.c | 2 +- 42sh/src/exec/plaunch_builtin.c | 2 +- 42sh/src/exec/plaunch_if.c | 2 +- 42sh/src/exec/process_launch.c | 4 ++-- 42sh/src/exec/redirect_greatand.c | 2 +- 42sh/src/glob/command_getoutput.c | 2 +- 8 files changed, 13 insertions(+), 12 deletions(-) diff --git a/42sh/libft/src/sstr/ft_sstrdel.c b/42sh/libft/src/sstr/ft_sstrdel.c index c0d618ac..fd044783 100644 --- a/42sh/libft/src/sstr/ft_sstrdel.c +++ b/42sh/libft/src/sstr/ft_sstrdel.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:09:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 17:52:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,4 +22,5 @@ void ft_sstrdel(char **sstr, int index) sstr[i] = sstr[i + 1]; i++; } + ft_strdel(sstr + index); } diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 13a606a0..15ef430c 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/03/21 01:14:41 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:41:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,10 @@ static int env_usage(int arg_miss, char c) { if (arg_miss) - ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n"); + SH_ERR("env: option requires an argument -- u"); else if (c) - ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c); - ft_dprintf(2, "usage: %s\n", US_ENV); + SH_ERR("env: illegal option -- %c", c); + SH_ERR("usage: %s", US_ENV); return (1); } diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index 61069a81..d014efd9 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/21 17:40:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 17:51:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_builtin.c b/42sh/src/exec/plaunch_builtin.c index 00215451..774171d4 100644 --- a/42sh/src/exec/plaunch_builtin.c +++ b/42sh/src/exec/plaunch_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:50:12 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 18:10:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/plaunch_if.c b/42sh/src/exec/plaunch_if.c index 64044393..1e4cb74d 100644 --- a/42sh/src/exec/plaunch_if.c +++ b/42sh/src/exec/plaunch_if.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 17:26:53 by wescande #+# #+# */ -/* Updated: 2017/03/21 00:53:01 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:10:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 95f7575d..63b85bbe 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/21 01:26:45 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:11:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ int process_launch(t_process *p) process_free(p, 0); return (1); } - DG("FORK"); + DG("FORK"); p->pid = pid; process_setgroup(p, pid); if (p->fdin != STDIN) diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index 09beb928..d5e2e573 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/03/21 17:40:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 17:41:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index fdab45d0..18da3edc 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ -/* Updated: 2017/03/21 01:21:38 by wescande ### ########.fr */ +/* Updated: 2017/03/21 18:15:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 36040d152aff9e40d18d7359431d41c37cf179e8 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Mar 2017 22:08:27 +0100 Subject: [PATCH 2/2] builtin_env seems stable, gonna merge --- 42sh/includes/builtin.h | 13 +++ 42sh/src/builtin/builtin_env.c | 156 ++++++++++---------------------- 42sh/src/builtin/builtin_read.c | 8 +- 3 files changed, 63 insertions(+), 114 deletions(-) diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 17a22131..094bc086 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -19,6 +19,18 @@ # define BT_EXPORT_LP (1 << 0) +# define BT_ENV_LI (1 << 0) +# define BT_ENV_LU (1 << 1) + +struct s_env_data +{ + t_flag flag; + char **av_data; + char **custom_env; +}; + +typedef struct s_env_data t_env_data; + t_execf *is_builtin(t_process *p); int builtin_return_status(int ret, int status); int builtin_export(const char *path, char *const av[], char *const envp[]); @@ -38,6 +50,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[]); int builtin_hash(const char *path, char *const av[], char *const envp[]); int builtin_math(const char *path, char *const av[], char *const envp[]); +int bt_env_geti(char ***av, t_env_data *data); int error_msg(char *msg); #endif diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 15ef430c..f52a8080 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -1,124 +1,64 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* builtin_env.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ -/* Updated: 2017/03/21 18:41:06 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - #include "minishell.h" -#define US_ENV "env [-i] [-u name] [name=value]... [utility [argument...]]" -static int env_usage(int arg_miss, char c) +#define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]" +#define ENV_NOFILE "env: %s: No such file or directory" +#define ENV_NOPERM "env: %s: Permission denied" + +t_cliopts g_env_opts[] = { - if (arg_miss) - SH_ERR("env: option requires an argument -- u"); - else if (c) - SH_ERR("env: illegal option -- %c", c); - SH_ERR("usage: %s", US_ENV); - return (1); + {'i', NULL, BT_ENV_LI, 0, bt_env_geti}, + {0, 0, 0, 0, 0}, +}; + +int bt_env_geti(char ***av, t_env_data *data) +{ + if (!av || !*av || !data) + return (1); + while (**av && ft_strchr(**av, '=')) + { + data->custom_env = ft_sstradd(data->custom_env, **av); + ++(*av); + } + --(*av); + return (0); } -static void env_freeone(char **env, char *arg) +static int bt_env_parse(t_env_data *data, char **av) { - int i; - char *tmp; - - while (env && *env && (i = -1)) - { - if (ft_strcmp(*env, arg) == '=' - && ft_strlen(arg) == ft_strlenchr(*env, '=')) - { - tmp = *env; - while (*env) - { - *env = *(env + 1); - ++env; - } - ft_strdel(&tmp); - return ; - } - ++env; - } -} - -static void env_replace(char ***custom_env, char *arg) -{ - char **arg_split; - - if ((arg_split = ft_strsplit(arg, '='))) - { - env_freeone(*custom_env, *arg_split); - ft_tabdel(&arg_split); - } - *custom_env = ft_sstradd(*custom_env, arg); -} - -static int env_treat_flag(char ***custom_env, char *const *arg[]) -{ - char *tmp; - - while (*(++*arg)) - { - if (!ft_strcmp(**arg, "-i")) - { - tmp = ft_strjoin("PATH=", ft_getenv(*custom_env, "PATH")); - ft_tabdel(custom_env); - *custom_env = ft_sstradd(NULL, tmp); - ft_strdel(&tmp); - } - else if (!ft_strcmp(**arg, "-u")) - { - ++*arg; - if (**arg) - env_freeone(*custom_env, **arg); - else - return (env_usage(1, 0)); - } - else if (ft_strchr(**arg, '=')) - env_replace(custom_env, **arg); - else if (!ft_strcmp(**arg, "--")) - { - ++*arg; - return (0); - } - else if ((**arg)[0] == '-') - return (env_usage(0, (**arg)[1])); - else - return (0); - } + data->flag = 0; + data->av_data = NULL; + data->custom_env = NULL; + if (cliopts_get(av, g_env_opts, data)) + return (1); return (0); } int builtin_env(const char *path, char *const argv[], char *const envp[]) { - char **env; - pid_t pid; + t_env_data data; + int status; + pid_t pid; + struct stat buf; - (void)path; - pid = 0; - if (!argv || ft_strcmp(*argv, "env")) - return (builtin_return_status(0, env_usage(0, 0))); - env = ft_sstrdup((char **)envp); - if (env_treat_flag(&env, &argv)) + (void)envp; + if (bt_env_parse(&data, (char**)argv)) + return (ft_perror() && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0); + else if (!*data.av_data) + return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL)); + else if ((pid = fork()) == 0) { - ft_sstrfree(env); - return (builtin_return_status(0, 1)); + if (!(path = ft_strchr(data.av_data[0], '/') ? + ft_strdup(data.av_data[0]) : ft_hash(data.av_data[0])) + || access(path, F_OK) != 0) + exit (SH_ERR(ENV_NOFILE, data.av_data[0])); + stat(path, &buf); + if (S_ISDIR(buf.st_mode) || access(path, X_OK) != 0) + exit (SH_ERR(ENV_NOPERM, data.av_data[0])); + execve(path, data.av_data, data.custom_env); } - if (!*argv) - { - ft_sstrprint(env, '\n'); - if (env) - ft_putchar('\n'); - } - else - pid = command_setoutput(argv, env); - ft_tabdel(&env); - DG("%d", pid); - return (builtin_return_status(pid, 0)); + waitpid(pid, &status, 0); + ft_sstrfree(data.custom_env); + return (0); + /* return (builtin_return_status(pid, 0)); */ } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index a7a4edf1..d5edb32c 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "minishell.h" + #define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]" #define US_READ_1 "[-n nchars] [-d delim] [name ...]" @@ -26,11 +27,6 @@ t_cliopts g_read_opts[] = {0, 0, 0, 0, 0}, }; -void bt_read_usage(void) -{ - SH_ERR("usage: read %s %s\n", US_READ, US_READ_1); -} - int bt_read_init(t_read *data, char **av) { data->opts = 0; @@ -115,7 +111,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) if (ret == -1) exit(1); if (ret != 0) - bt_read_usage(); + SH_ERR("usage: read %s %s\n", US_READ, US_READ_1); if (ret != 2) bt_read_exit(&data); return (builtin_return_status(0, ret));