diff --git a/42sh/libft b/42sh/libft index 6a2672a1..bc489f86 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 6a2672a19268c6481525d9aaee5bd35722bbd75a +Subproject commit bc489f8664fdc24317c31b3069811f54b1178643 diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e3179079..ebff91f8 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:09:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_builtin.c b/42sh/src/exec/launch_builtin.c new file mode 100644 index 00000000..6f79c917 --- /dev/null +++ b/42sh/src/exec/launch_builtin.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* launch_builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ +/* Updated: 2017/03/07 16:39:52 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int launch_builtin(t_process *p) +{ + pid_t pid; + + if (IS_PIPESINGLE(*p)) + { + if (process_redirect(p)) + return (1); + set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); + return (1); + } + pid = fork(); + if (pid == 0) + { + process_setgroup(p, 0); + process_setsig(); + if (process_redirect(p)) + exit (1); + exit((*p->execf)(p->path, p->av, data_singleton()->env)); + } + else if (pid > 0) + return (0); + else if (pid == -1) + ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); + return (1); +} diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index c482d419..c1e17302 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:54:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,45 +14,33 @@ int launch_file(t_process *p) { int pid; - if (p->attrs & PROCESS_BUILTIN && IS_PIPESINGLE(*p)) - { - if (process_redirect(p)) - return (1); - set_exitstatus((*p->execf)(p->path, p->av, data_singleton()->env), 1); - return (1); - } - p->attrs &= ~PROCESS_STATE_MASK; - p->attrs |= PROCESS_RUNNING; - if (p->attrs & (PROCESS_BINARY | PROCESS_SCRIPT) - && access(p->path, X_OK) == -1) - { - ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); - set_exitstatus(126, 1); - return (1); - } pid = fork(); if (pid == 0) { - if (p->attrs & PROCESS_UNKNOWN) + if (!p->stat) { ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->av[0]); exit(127); } + else if (IS_DIR(p->stat.st_mode)) + { + ft_dprintf(2, "{red}%s: %s: Is a directory{eoc}\n", SHELL_NAME, p->av[0]); + exit(126); + } + else if (access(p->path, X_OK) == -1) + { + ft_dprintf(2, "{red}%s: permission denied: %s{eoc}\n", SHELL_NAME, p->av[0]); + exit(126); + } process_setgroup(p, 0); process_setsig(); if (process_redirect(p)) exit (1); - if (p->attrs & PROCESS_BUILTIN) - exit((*p->execf)(p->path, p->av, data_singleton()->env)); (*p->execf)(p->path, p->av, data_singleton()->env); ft_dprintf(2, "{red}%s: internal execve error on %s{eoc}\n", SHELL_NAME, p->av[0]); } else if (pid > 0) - { - p->pid = pid; - process_setgroup(p, pid); return (0); - } else if (pid == -1) ft_dprintf(2, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); return (1); diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index ebf4c0eb..e9f758e8 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:04:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ t_itof g_launchmap[] = { {PROCESS_FUNCTION, NULL}, - {PROCESS_BUILTIN, NULL}, + {PROCESS_BUILTIN, launch_builtin}, {PROCESS_FILE, launch_file}, {PROCESS_SUBSHELL, NULL}, {PROCESS_WHILE, NULL}, @@ -35,9 +35,18 @@ int launch_process(t_process *p) while (g_launchmap[i].type) { if (p->type == g_launchmap[i].type) + { if (!g_launchmap[i].f) return (-1); - return ((*g_launchmap[i].f)(p)); + p->attrs &= ~PROCESS_STATE_MASK; + p->attrs |= PROCESS_RUNNING; + if (!(*g_launchmap[i].f)(p)) + { + p->pid = pid; + process_setgroup(p, pid); + return (0); + } + } i++; } return (-1); diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 1114f927..1f6c66ab 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/03/07 14:49:45 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:39:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,27 +14,27 @@ int process_setexec(t_process *p) { - p->path = NULL; + p->data.cmd.path = NULL; + p->data.cmd.execf = NULL; /* if ((p->execf = is_function(p))) */ /* p->type = PROCESS_FUNCTION; */ if ((p->data.cmd.execf = is_builtin(p))) p->type = PROCESS_BUILTIN; - else if (ft_hash(p)) - { - p->data.cmd.execf = &execve; - p->type = PROCESS_FILE; - } else if (ft_strchr(p->av[0], '/')) { - p->data.cmd.execf = &execve; p->type = PROCESS_FILE; + p->data.cmd.execf = &execve; p->data.cmd.path = ft_strdup(p->av[0]); + stat(p->data.cmd.path, &p->data.cmd.stat); } else { - p->execf = NULL; - p->attrs |= PROCESS_UNKNOWN; - return (1); + p->type = PROCESS_FILE; + if (ft_hash(p)) + { + p->data.cmd.execf = &execve; + lstat(p->data.cmd.path, p->data.cmd.stat); + } } return (0); } diff --git a/42sh/src/hash_table/ft_add_hash.c b/42sh/src/hash_table/ft_add_hash.c index 403ee64d..53b0992c 100644 --- a/42sh/src/hash_table/ft_add_hash.c +++ b/42sh/src/hash_table/ft_add_hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:20:11 by gwojda #+# #+# */ -/* Updated: 2017/03/07 14:43:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:02:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/hash_table/hash.c b/42sh/src/hash_table/hash.c index b7022ec5..2276a4e1 100644 --- a/42sh/src/hash_table/hash.c +++ b/42sh/src/hash_table/hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:06:19 by gwojda #+# #+# */ -/* Updated: 2017/03/04 18:50:56 by ariard ### ########.fr */ +/* Updated: 2017/03/07 16:02:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 78d413f7..ce7e2a03 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:45:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:38:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,14 +23,6 @@ int data_init(void) data->env = ft_sstrdup(environ); data->comp = NULL; data->opts = 0; - /* data->exec.process.path = NULL; */ - /* data->exec.process.av = NULL; */ - /* data->exec.process.to_close = 0; */ - /* data->exec.process.fdin = STDIN; */ - /* data->exec.process.fdout = STDOUT; */ - /* data->exec.process.pid = 0; */ - /* data->exec.process.attributes = 0; */ - /* data->exec.process.redirs = NULL; */ data->exec.fd_save[0] = fcntl(0, F_DUPFD_CLOEXEC); data->exec.fd_save[1] = fcntl(1, F_DUPFD_CLOEXEC); data->exec.fd_save[2] = fcntl(2, F_DUPFD_CLOEXEC); @@ -38,12 +30,10 @@ int data_init(void) data->exec.fdin = STDIN; data->exec.attrs = 0; - /* data->exec.aol_status = NULL; */ - /* data->exec.aol_search = 0; */ - /* data->exec.job.id = 0; */ - /* data->exec.job.pgid = 0; */ - /* data->exec.job.attributes = 0; */ - /* data->exec.job.first_process = 0; */ + data->exec.job.id = 0; + data->exec.job.pgid = 0; + data->exec.job.attrs = 0; + data->exec.job.first_process = NULL; data->jobc.first_job = NULL; data->jobc.current_id = 1; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index cf6c3dcc..2673700d 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:09:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 15:43:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 272440d7..abf15a8e 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/03 19:45:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/07 16:38:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */