diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index c0902fc6..48456c09 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -141,6 +141,7 @@ sstr/ft_sstrcat.c\ sstr/ft_sstrdel.c\ sstr/ft_sstrdup.c\ sstr/ft_sstrfree.c\ +sstr/ft_sstrmerge.c\ sstr/ft_sstrprint.c\ sstr/ft_sstrprint_fd.c\ sstr/ft_sstrsort.c\ diff --git a/42sh/libft/includes/sstr.h b/42sh/libft/includes/sstr.h index ac65cd02..b1f10bc2 100644 --- a/42sh/libft/includes/sstr.h +++ b/42sh/libft/includes/sstr.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 13:16:25 by jhalford #+# #+# */ -/* Updated: 2017/03/07 11:35:11 by ariard ### ########.fr */ +/* Updated: 2017/03/24 17:46:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ void ft_sstrsort(char **list, int (*cmp)()); void ft_sstrprint(char **list, char sep); void ft_sstrprint_fd(int fd, char **list, char sep); char **ft_sstrdup(char **list); +char **ft_sstrmerge(char **s1, char **s2); void ft_sstrdel(char **sstr, int index); void ft_sstrfree(char **sstr); char *ft_sstrcat(char **sstr, char sep); diff --git a/42sh/libft/src/sstr/ft_sstrmerge.c b/42sh/libft/src/sstr/ft_sstrmerge.c new file mode 100644 index 00000000..2705a285 --- /dev/null +++ b/42sh/libft/src/sstr/ft_sstrmerge.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrmerge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 17:40:50 by jhalford #+# #+# */ +/* Updated: 2017/03/24 18:05:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_sstrmerge(char **s1, char **s2) +{ + char **out; + + out = ft_sstrdup(s1); + if (!s2) + return (out); + while (*s2) + { + out = ft_sstradd(out, *s2); + s2++; + } + return (out); +} diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 838a9c24..51e5936d 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */ -/* Updated: 2017/03/24 15:11:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:59:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,11 @@ t_cliopts g_env_opts[] = { - {'i', NULL, BT_ENV_LI, 0, bt_env_geti}, + {'i', NULL, BT_ENV_LI, 0, NULL}, {0, 0, 0, 0, 0}, }; -int bt_env_geti(char ***av, t_env_data *data) +int bt_env_getcustom(char ***av, t_env_data *data) { if (!av || !*av || !data) return (1); @@ -31,7 +31,6 @@ int bt_env_geti(char ***av, t_env_data *data) data->custom_env = ft_sstradd(data->custom_env, **av); ++(*av); } - --(*av); return (0); } @@ -39,9 +38,19 @@ static int bt_env_parse(t_env_data *data, char **av) { data->flag = 0; data->av_data = NULL; - data->custom_env = NULL; + DG(); if (cliopts_get(av, g_env_opts, data)) return (1); + DG(); + data->custom_env = NULL; + bt_env_getcustom(&data->av_data, data); + DG(); + if (!(data->flag & BT_ENV_LI)) + { + DG("no -i"); + data->custom_env = ft_sstrmerge(data_singleton()->env, data->custom_env); + } + DG(); return (0); } @@ -55,11 +64,18 @@ int builtin_env(const char *path, (void)envp; if (bt_env_parse(&data, (char**)argv)) - return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0); - else if (!*data.av_data) - return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL)); + return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE)); + DG(); + if (!*data.av_data) + { + DG(); + ft_sstrprint(data.custom_env, '\n'); + ft_putchar('\n'); + return (0); + } else if ((pid = fork()) == 0) { + DG(); 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) diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index e1865178..f2fd8909 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:06:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:23:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ int exec_reset_job(t_job *job) job->pgid = 0; job->attrs = 0; job->first_process = NULL; + tcgetattr(STDIN, &job->tmodes); return (0); } @@ -31,12 +32,11 @@ int exec_reset(void) jobc = &data_singleton()->jobc; i = -1; while (++i < 10) - exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10); + exec->fd_save[i] = fcntl(i, F_DUPFD, 10); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; exec_reset_job(&exec->job); - tcgetattr(STDIN, &exec->job.tmodes); jobc->first_job = NULL; jobc->current_id = 1; return (0); diff --git a/42sh/src/exec/mark_process_status.c b/42sh/src/exec/mark_process_status.c index 7c6af2b2..15f1f223 100644 --- a/42sh/src/exec/mark_process_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:05:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:28:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int mark_process_status(pid_t pid, int status) { t_list *plist; t_process *p; + int signo; if ((plist = job_getprocess(pid))) { @@ -26,8 +27,11 @@ int mark_process_status(pid_t pid, int status) else { p->state = PROCESS_COMPLETED; - if (WIFSIGNALED(status) && WTERMSIG(status) != SIGPIPE) - ft_dprintf(2, "%s\n", strsignal((WTERMSIG(status)))); + if (WIFSIGNALED(status) && (signo = WTERMSIG(status))) + { + if (signo != SIGPIPE && signo != SIGINT) + ft_dprintf(2, "%s\n", strsignal(signo)); + } } return (0); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index b044bdbd..eb01f277 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/24 14:56:32 by wescande ### ########.fr */ +/* Updated: 2017/03/24 18:22:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,10 @@ int process_launch(t_process *p) if (process_redirect(p)) set_exitstatus(1, 1); else + { + exec_reset(); p->map.launch(p); + } shell_resetfds(); shell_resetsig(); process_free(p, 0); diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 49f59d8e..4bca5724 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:15:43 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 16:58:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,6 @@ int redirect_great(t_redir *redir) if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) exit(1); - dup2(fdold, fdnew); + dup2_close(fdold, fdnew); return (0); } diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index 22ef92a6..68ae3c86 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 15:39:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:17:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,12 +66,9 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; - do_job_notification(); if (!SH_HAS_JOBC(data_singleton()->opts)) - { - SH_ERR("jobs: %s", SH_MSG_NOJOBC); - return (1); - } + return (SH_ERR("jobs: %s", SH_MSG_NOJOBC)); + do_job_notification(); ft_bzero(&data, sizeof(t_data_template)); if (cliopts_get((char**)av, g_jobs_opts, &data)) return (ft_perror("jobs")); diff --git a/42sh/src/job_control/job_format_head.c b/42sh/src/job_control/job_format_head.c index 450fcdeb..f12526cb 100644 --- a/42sh/src/job_control/job_format_head.c +++ b/42sh/src/job_control/job_format_head.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:10:38 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:55:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 16:55:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,5 @@ void job_format_head(t_job *j) job_getrank(&rank); crank = j->id == rank[0] ? '+' : ' '; crank = j->id == rank[1] ? '-' : crank; - DG("raks [%i:%i]", rank[0], rank[1]); ft_printf("{mag}[%i]%c {eoc}", j->id, crank); } diff --git a/42sh/src/job_control/job_hup_all.c b/42sh/src/job_control/job_hup_all.c index 0933048b..9e38a8c0 100644 --- a/42sh/src/job_control/job_hup_all.c +++ b/42sh/src/job_control/job_hup_all.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 11:37:40 by jhalford #+# #+# */ -/* Updated: 2017/03/24 15:40:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:54:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,8 @@ void job_hup_all(void) while (jlist) { job = jlist->content; - DG("gonna SIGHUP %i", job->pgid); - builtin_jobs(NULL, NULL, NULL); if (job->pgid != 1) kill(-job->pgid, SIGHUP); - DG("after SIGHUP %i", job->pgid); jlist = jlist->next; } } diff --git a/42sh/src/job_control/put_job_in_background.c b/42sh/src/job_control/put_job_in_background.c index b0302223..ae75068d 100644 --- a/42sh/src/job_control/put_job_in_background.c +++ b/42sh/src/job_control/put_job_in_background.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 15:03:29 by jhalford #+# #+# */ -/* Updated: 2017/03/10 15:08:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 17:24:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ int put_job_in_background(t_job *j, int cont) { if (cont) if (kill(-j->pgid, SIGCONT) < 0) - DG("kill(SIGCONT) failed"); + SH_ERR("kill(): %s", strerror(errno)); return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 4f32fbba..e63fdc2c 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/24 15:30:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:23:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,8 @@ static int handle_instruction(t_list **token, t_btree **ast) else if (ret > 0) break ; } + /* btree_print(3, *ast, ft_putast); */ + /* exit(1); */ if (data->parser.state == SUCCESS && ft_exec(ast) < 0) exit(1); else if (data->parser.state != SUCCESS)