>& and <& check if fd is open for reading/writing specifically

This commit is contained in:
Jack Halford 2017-03-06 16:58:47 +01:00
parent c42357efa3
commit e9633119e5
6 changed files with 13 additions and 10 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 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_reset(t_process *p);
void process_resetfds(void); void process_resetfds(void);
int fd_is_valid(int fd); int fd_is_valid(int fd, int flag);
int bad_fd(int fd); int bad_fd(int fd);
int process_redirect(t_process *p); int process_redirect(t_process *p);
int redirect_great(t_redir *redir); int redirect_great(t_redir *redir);

View file

@ -6,13 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 13:46:40 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" #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);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:12:31 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); return (0);
if (fdold > 9) if (fdold > 9)
return (bad_fd(fdold)); return (bad_fd(fdold));
if (fd_is_valid(fdold)) if (fd_is_valid(fdold, O_RDONLY))
dup2_close(fdold, fdnew); dup2_close(fdold, fdnew);
else else
return (bad_fd(fdold)); return (bad_fd(fdold));

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:11:18 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); return (0);
if (fdold > 9) if (fdold > 9)
return (bad_fd(fdold)); return (bad_fd(fdold));
if (fd_is_valid(fdold)) if (fd_is_valid(fdold, O_WRONLY))
dup2_close(fdold, fdnew); dup2_close(fdold, fdnew);
else else
return (bad_fd(fdold)); return (bad_fd(fdold));

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/11 14:04:48 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file