From e9633119e5467ebca1580e6597891ea60ef52212 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 6 Mar 2017 16:58:47 +0100 Subject: [PATCH] >& and <& check if fd is open for reading/writing specifically --- 42sh/includes/exec.h | 4 ++-- 42sh/src/exec/fd_is_valid.c | 9 ++++++--- 42sh/src/exec/redirect_greatand.c | 4 ++-- 42sh/src/exec/redirect_lessand.c | 4 ++-- 42sh/src/main/shell_get_opts.c | 2 +- 42sh/yolo | 0 6 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 42sh/yolo diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 4afa61d6..81b783cb 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/06 12:33:24 by wescande ### ########.fr */ +/* Updated: 2017/03/06 16:56:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -126,7 +126,7 @@ void process_free(void *content, size_t content_size); void process_reset(t_process *p); void process_resetfds(void); -int fd_is_valid(int fd); +int fd_is_valid(int fd, int flag); int bad_fd(int fd); int process_redirect(t_process *p); int redirect_great(t_redir *redir); diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index d89e0f49..39fac840 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,13 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/05 19:44:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:58:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int fd_is_valid(int fd) +int fd_is_valid(int fd, int has_flag) { - return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); + int flags; + + flags = fcntl(fd, F_GETFD); + return ((flags != -1 || errno != EBADF) && flags & has_flag); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index c210c647..002375e8 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/05 19:44:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:54:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int redirect_greatand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold)) + if (fd_is_valid(fdold, O_RDONLY)) dup2_close(fdold, fdnew); else return (bad_fd(fdold)); diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index 0c3b5982..bb9987d6 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/05 19:43:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:53:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int redirect_lessand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold)) + if (fd_is_valid(fdold, O_WRONLY)) dup2_close(fdold, fdnew); else return (bad_fd(fdold)); diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index 0eee3eb4..dc75dddd 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/03/05 17:45:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/06 16:52:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/yolo b/42sh/yolo deleted file mode 100644 index e69de29b..00000000