diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 726fddbc..4ce95f9b 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -191,6 +191,7 @@ str/ft_strsub.c\ str/ft_strtok.c\ str/ft_strtrim.c\ sys/dup2_close.c\ +sys/fd_replace.c\ sys/ft_getenv.c\ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index 55cd9537..98477d16 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:44:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:32:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index 9f63db23..972805ae 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/20 09:38:36 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:33:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,5 +29,6 @@ int ft_xattr_print(char *path); int ft_xattr_count(char *path); char *ft_getenv(char **env, char *key); int dup2_close(int fd1, int fd2); +int fd_replace(int fd1, int fd2); #endif diff --git a/42sh/libft/src/ft_printf/ft_vprintf.c b/42sh/libft/src/ft_printf/ft_vprintf.c index 78ffa130..718897e9 100644 --- a/42sh/libft/src/ft_printf/ft_vprintf.c +++ b/42sh/libft/src/ft_printf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:52:57 by jhalford #+# #+# */ -/* Updated: 2017/03/21 16:08:02 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:34:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/mem/ft_malloc.c b/42sh/libft/src/mem/ft_malloc.c index 3caec1b4..4560b1e8 100644 --- a/42sh/libft/src/mem/ft_malloc.c +++ b/42sh/libft/src/mem/ft_malloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 15:45:00 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:45:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:34:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c new file mode 100644 index 00000000..905b340c --- /dev/null +++ b/42sh/libft/src/sys/fd_replace.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fd_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ +/* Updated: 2017/03/21 16:40:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int fd_replace(int fd1, int fd2) +{ + if (fd1 != fd2) + return(dup2_close(fd1, fd2)); + return (0); +} diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 7c2a8769..90a08144 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/03/20 18:14:20 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 16:24:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/bad_fd.c b/42sh/src/exec/bad_fd.c index 8851abcd..4b86b0ae 100644 --- a/42sh/src/exec/bad_fd.c +++ b/42sh/src/exec/bad_fd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:32:43 by jhalford #+# #+# */ -/* Updated: 2017/03/20 10:24:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:24:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ int bad_fd(int fd) { - ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n", - SHELL_NAME, fd); + SH_ERR("%i: Bad file descriptor", fd); return (1); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index ce1025cc..80d54701 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/03/20 18:38:13 by gwojda ### ########.fr */ +/* Updated: 2017/03/21 16:40:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,6 @@ t_itof g_redirmap[] = {0, NULL}, }; -static void process_close(int fd1, int fd2) -{ - if (fd1 != fd2) - dup2_close(fd1, fd2); -} - int process_redirect(t_process *p) { t_list *redirs; @@ -38,6 +32,8 @@ int process_redirect(t_process *p) redirs = p->redirs; if (p->to_close != STDIN) close(p->to_close); + fd_replace(p->fdin, STDIN); + fd_replace(p->fdout, STDOUT); while (redirs) { redir = redirs->content; @@ -53,7 +49,5 @@ int process_redirect(t_process *p) } redirs = redirs->next; } - process_close(p->fdin, STDIN); - process_close(p->fdout, STDOUT); return (0); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index ee798a6b..76724439 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/20 12:36:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:35:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,18 +24,15 @@ int redirect_greatand(t_redir *redir) } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", - data_singleton()->argv[0], redir->word); + SH_ERR("%s: can only be digits{eoc}\n", redir->word); return (1); } fdold = ft_atoi(redir->word); fdnew = redir->n; - if (fdold == fdnew) - return (0); if (fdold > 9) return (bad_fd(fdold)); if (fd_is_valid(fdold, O_WRONLY | O_RDWR)) - dup2_close(fdold, fdnew); + fd_replace(fdold, fdnew); else return (bad_fd(fdold)); return (0); diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index a5c7efe4..0f4f5928 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/03/20 12:36:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/21 16:33:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,18 +24,15 @@ int redirect_lessand(t_redir *redir) } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", - data_singleton()->argv[0], redir->word); + SH_ERR("%s: can only be digits{eoc}\n", redir->word); return (1); } fdold = ft_atoi(redir->word); fdnew = redir->n; - if (fdold == fdnew) - return (0); if (fdold > 9) return (bad_fd(fdold)); if (fd_is_valid(fdold, O_RDONLY | O_RDWR)) - dup2_close(fdold, fdnew); + fd_replace(fdold, fdnew); else return (bad_fd(fdold)); return (0);