issue #128 part 1

This commit is contained in:
Jack Halford 2017-03-21 16:41:00 +01:00
parent 151b4ec02d
commit fb806bfa90
11 changed files with 38 additions and 29 deletions

View file

@ -191,6 +191,7 @@ str/ft_strsub.c\
str/ft_strtok.c\ str/ft_strtok.c\
str/ft_strtrim.c\ str/ft_strtrim.c\
sys/dup2_close.c\ sys/dup2_close.c\
sys/fd_replace.c\
sys/ft_getenv.c\ sys/ft_getenv.c\
sys/ft_xattr_count.c\ sys/ft_xattr_count.c\
sys/ft_xattr_print.c\ sys/ft_xattr_print.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 17:24:23 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); int ft_xattr_count(char *path);
char *ft_getenv(char **env, char *key); char *ft_getenv(char **env, char *key);
int dup2_close(int fd1, int fd2); int dup2_close(int fd1, int fd2);
int fd_replace(int fd1, int fd2);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 15:52:57 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/21 15:45:00 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fd_replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:32:43 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) int bad_fd(int fd)
{ {
ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n", SH_ERR("%i: Bad file descriptor", fd);
SHELL_NAME, fd);
return (1); return (1);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 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}, {0, NULL},
}; };
static void process_close(int fd1, int fd2)
{
if (fd1 != fd2)
dup2_close(fd1, fd2);
}
int process_redirect(t_process *p) int process_redirect(t_process *p)
{ {
t_list *redirs; t_list *redirs;
@ -38,6 +32,8 @@ int process_redirect(t_process *p)
redirs = p->redirs; redirs = p->redirs;
if (p->to_close != STDIN) if (p->to_close != STDIN)
close(p->to_close); close(p->to_close);
fd_replace(p->fdin, STDIN);
fd_replace(p->fdout, STDOUT);
while (redirs) while (redirs)
{ {
redir = redirs->content; redir = redirs->content;
@ -53,7 +49,5 @@ int process_redirect(t_process *p)
} }
redirs = redirs->next; redirs = redirs->next;
} }
process_close(p->fdin, STDIN);
process_close(p->fdout, STDOUT);
return (0); return (0);
} }

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/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)) if (!ft_stris(redir->word, ft_isdigit))
{ {
ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", SH_ERR("%s: can only be digits{eoc}\n", redir->word);
data_singleton()->argv[0], redir->word);
return (1); return (1);
} }
fdold = ft_atoi(redir->word); fdold = ft_atoi(redir->word);
fdnew = redir->n; fdnew = redir->n;
if (fdold == fdnew)
return (0);
if (fdold > 9) if (fdold > 9)
return (bad_fd(fdold)); return (bad_fd(fdold));
if (fd_is_valid(fdold, O_WRONLY | O_RDWR)) if (fd_is_valid(fdold, O_WRONLY | O_RDWR))
dup2_close(fdold, fdnew); fd_replace(fdold, fdnew);
else else
return (bad_fd(fdold)); return (bad_fd(fdold));
return (0); return (0);

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/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)) if (!ft_stris(redir->word, ft_isdigit))
{ {
ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", SH_ERR("%s: can only be digits{eoc}\n", redir->word);
data_singleton()->argv[0], redir->word);
return (1); return (1);
} }
fdold = ft_atoi(redir->word); fdold = ft_atoi(redir->word);
fdnew = redir->n; fdnew = redir->n;
if (fdold == fdnew)
return (0);
if (fdold > 9) if (fdold > 9)
return (bad_fd(fdold)); return (bad_fd(fdold));
if (fd_is_valid(fdold, O_RDONLY | O_RDWR)) if (fd_is_valid(fdold, O_RDONLY | O_RDWR))
dup2_close(fdold, fdnew); fd_replace(fdold, fdnew);
else else
return (bad_fd(fdold)); return (bad_fd(fdold));
return (0); return (0);