redirection while works

This commit is contained in:
Jack Halford 2017-03-24 20:11:24 +01:00
parent bb4ffa50e5
commit 1aebf7531a
15 changed files with 98 additions and 44 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/24 14:47:12 by ariard ### ########.fr */
/* Updated: 2017/03/24 20:07:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -131,7 +131,7 @@ struct s_job
struct s_exec
{
t_job job;
int fd_save[10];
t_list *fd_save[10];
t_flag attrs;
int fdin;
t_list *op_stack;
@ -140,6 +140,8 @@ struct s_exec
};
int exec_reset(void);
int exec_pushfds();
int exec_popfds();
int process_fork(t_process *p);
int process_setgroup(t_process *p, pid_t pid);
void process_setsig(void);

View file

@ -108,6 +108,7 @@ lst/ft_lstnew_range.c\
lst/ft_lstsort.c\
lst/pop.c\
lst/push.c\
lst/top.c\
math/ft_addrcmp.c\
math/ft_ilen.c\
math/ft_ilen_base.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2017/03/21 16:32:40 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:08:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */
/* Updated: 2017/03/11 16:18:00 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:09:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,7 @@ typedef struct s_list t_list;
int pop(t_list **lst);
t_list *push(t_list **stack, int elem);
int top(t_list *top);
t_list *ft_lstnew(void const *content, size_t content_size);
void ft_lstdel(t_list **alst, void (*del)(void *, size_t));

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:09:56 by jhalford #+# #+# */
/* Updated: 2017/03/03 16:49:59 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:05:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

18
42sh/libft/src/lst/top.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* top.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/24 20:04:27 by jhalford #+# #+# */
/* Updated: 2017/03/24 20:09:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int top(t_list *top)
{
return (top ? *(int*)top->content : 0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 16:07:28 by jhalford #+# #+# */
/* Updated: 2017/02/07 16:15:52 by jhalford ### ########.fr */
/* Updated: 2017/03/24 19:33:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,9 @@
int dup2_close(int fd1, int fd2)
{
if (dup2(fd1, fd2))
if (dup2(fd1, fd2) < 0)
return (-1);
if (close(fd1))
if (close(fd1) < 0)
return (-1);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */
/* Updated: 2017/03/24 19:09:44 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:08:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,8 +14,10 @@
int fd_replace(int fd1, int fd2)
{
DG("%i ----> %i", fd2, fd1);
if (fd1 != fd2)
{
DG("%i ---> %i", fd2, fd1);
return (dup2_close(fd1, fd2));
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:47:30 by wescande #+# #+# */
/* Updated: 2017/03/24 19:14:08 by jhalford ### ########.fr */
/* Updated: 2017/03/24 19:24:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,7 +43,6 @@ int exec_leaf(t_btree **ast)
job_addprocess(&p);
if (IS_PIPEEND(p))
{
DG("end of pipe");
if (JOB_IS_FG(job->attrs))
put_job_in_foreground(job, 0);
else

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */
/* Updated: 2017/03/24 18:41:14 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:09:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,17 +22,38 @@ int exec_reset_job(t_job *job)
return (0);
}
int exec_pushfds()
{
int i;
t_exec *exec;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
push(&(exec->fd_save[i]), fcntl(i, F_DUPFD_CLOEXEC, 10));
return (0);
}
int exec_popfds()
{
int i;
t_exec *exec;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
pop(&exec->fd_save[i]);
return (0);
}
int exec_reset(void)
{
t_exec *exec;
t_jobc *jobc;
int i;
t_exec *exec;
exec = &data_singleton()->exec;
jobc = &data_singleton()->jobc;
i = -1;
while (++i < 10)
exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10);
exec_pushfds();
exec->op_stack = NULL;
exec->fdin = STDIN;
exec->attrs = 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */
/* Updated: 2017/03/24 19:21:07 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:02:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,18 +35,10 @@ int process_fork(t_process *p)
int process_launch(t_process *p)
{
p->state = PROCESS_RUNNING;
if (IS_PIPESINGLE(*p)
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
if (!IS_PIPESINGLE(*p)
|| p->type == PROCESS_FILE
|| p->type == PROCESS_SUBSHELL)
{
if (process_redirect(p))
set_exitstatus(1, 1);
else
p->map.launch(p);
shell_resetfds();
shell_resetsig();
process_free(p, 0);
return (0);
}
p->pid = process_fork(p);
process_setgroup(p, p->pid);
if (p->fdin != STDIN)
@ -54,4 +46,17 @@ int process_launch(t_process *p)
if (p->fdout != STDOUT)
close(p->fdout);
return (1);
}
if (process_redirect(p))
set_exitstatus(1, 1);
else
{
exec_pushfds();
p->map.launch(p);
}
exec_popfds();
shell_resetfds();
shell_resetsig();
process_free(p, 0);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */
/* Updated: 2017/03/24 18:25:41 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:09:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,10 @@ int redirect_great(t_redir *redir)
fdnew = redir->n;
if ((fdold = open(redir->word,
O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
exit(1);
dup2_close(fdold, fdnew);
{
SH_ERR("open(): %s", strerror(errno));
exit (1);
}
fd_replace(fdold, fdnew);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/03 17:39:45 by jhalford #+# #+# */
/* Updated: 2017/03/16 22:50:16 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:04:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
/* Updated: 2017/03/24 19:23:34 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:02:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */
/* Updated: 2017/03/22 17:49:38 by jhalford ### ########.fr */
/* Updated: 2017/03/24 20:09:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,12 +26,14 @@ void shell_resetfds(void)
{
t_exec *exec;
int i;
int fd;
exec = &data_singleton()->exec;
i = -1;
while (++i < 10)
{
if (exec->fd_save[i] != -1)
dup2(exec->fd_save[i], i);
fd = top(exec->fd_save[i]);
if (fd != -1)
dup2(fd, i);
}
}