file redirections repaired

This commit is contained in:
Jack Halford 2017-02-06 16:51:34 +01:00
parent a919dc53b9
commit 156e1cb29c
15 changed files with 48 additions and 38 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/02/03 14:01:10 by jhalford ### ########.fr */ /* Updated: 2017/02/06 15:54:00 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,7 +85,7 @@ int launch_process(t_process *p);
int process_setexec(t_type type, t_process *p); int process_setexec(t_type type, t_process *p);
int process_setgroup(t_process *p, pid_t pid); int process_setgroup(t_process *p, pid_t pid);
int process_redirect(t_process *p); int process_redirect(t_process *p);
void process_do_redirection(t_redir *redir); int process_do_redirection(t_redir *redir);
void process_setsig(void); void process_setsig(void);
void process_free(void *content, size_t content_size); void process_free(void *content, size_t content_size);
void process_reset(void); void process_reset(void);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/02/03 17:31:08 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:22:01 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2017/02/03 13:58:38 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:12:14 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2017/02/03 13:45:05 by jhalford ### ########.fr */ /* Updated: 2017/02/06 15:27:19 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
/* Updated: 2017/02/02 14:28:55 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:08:09 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,52 +6,62 @@
/* 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/02/03 15:01:18 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:51:17 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int fd_is_valid(int fd) int fd_is_valid(int fd)
{ {
return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); return (fcntl(fd, F_GETFD) != -1 || errno != EBADF);
} }
void process_do_redirection(t_redir *redir) int process_do_redirection(t_redir *redir)
{ {
int fdin; int fdold;
int fdout; int fdnew;
if (redir->type & (TK_GREAT | TK_DGREAT)) if (redir->type & (TK_GREAT | TK_DGREAT))
{ {
fdin = redir->n; fdold = redir->n;
fdout = open(redir->word.word, O_WRONLY | O_CREAT if ((fdnew = open(redir->word.word, O_WRONLY | O_CREAT
| (redir->type & TK_GREAT) ? O_TRUNC : O_APPEND, | ((redir->type & TK_GREAT) ? O_TRUNC : O_APPEND),
0644); 0644)) < 0)
{
DG("open errno=%i", errno);
exit(1);
}
} }
else if (redir->type & TK_LESS) else if (redir->type & TK_LESS)
{ {
fdin = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); fdold = redir->n;
fdout = redir->n; if ((fdnew = open(redir->word.word, O_RDONLY)) < 0)
{
ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n",
SHELL_NAME, redir->word.word);
exit (1);
}
} }
else if (redir->type & (TK_LESSAND | TK_GREATAND)) else if (redir->type & (TK_LESSAND | TK_GREATAND))
{ {
if (redir->close) if (redir->close)
{ {
close(redir->n); close(redir->n);
return ; return (0);
} }
else else
{ {
fdin = redir->type & TK_LESSAND ? redir->word.fd : redir->n; fdold = redir->type & TK_LESSAND ? redir->word.fd : redir->n;
fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; fdnew = redir->type & TK_LESSAND ? redir->n : redir->word.fd;
} }
} }
else else
{ {
ft_dprintf(2, "{red}%s: redirection error.", SHELL_NAME); ft_dprintf(2, "{red}%s: redirection error.{eoc}\n", SHELL_NAME);
return ; return (-1);
} }
fd_is_valid(fdout) ? dup2(fdout, fdin) : close(fdin); fd_is_valid(fdold) ? dup2(fdnew, fdold) : close(fdnew);
close(fdout); close(fdnew);
return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2017/02/03 13:46:30 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:46:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/02/06 14:38:45 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:46:48 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,8 +37,8 @@ int shell_single_command(char *command)
int main(int ac, char **av) int main(int ac, char **av)
{ {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF");
shell_init(ac, av); shell_init(ac, av);
DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF");
if (data_singleton()->opts & SHELL_OPTS_LC) if (data_singleton()->opts & SHELL_OPTS_LC)
{ {
shell_single_command(ft_strdup(shell_get_avdata())); shell_single_command(ft_strdup(shell_get_avdata()));

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/02 19:02:52 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:28:15 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 13:31:40 by jhalford #+# #+# */ /* Created: 2016/12/03 13:31:40 by jhalford #+# #+# */
/* Updated: 2016/12/03 13:31:41 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:28:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:07:03 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:29:54 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst)
next_tok = (*lst)->next->content; next_tok = (*lst)->next->content;
if (!(next_tok->type & TK_WORD)) if (!(next_tok->type & TK_WORD))
return (1); return (1);
node->data.redir.n = ft_atoi(tok->data); node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data);
node->data.redir.word.word = ft_strdup(next_tok->data); node->data.redir.word.word = ft_strdup(next_tok->data);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:09:50 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:30:41 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/12/05 11:58:48 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:31:19 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,9 +24,9 @@ int parse_less(t_btree **ast, t_list **start, t_list **lst)
return (1); return (1);
tok = (*lst)->content; tok = (*lst)->content;
next_tok = (*lst)->next->content; next_tok = (*lst)->next->content;
if (next_tok->type != TK_WORD) if (!(next_tok->type & TK_WORD))
return (1); return (1);
node->data.redir.n = ft_atoi(tok->data); node->data.redir.n = *tok->data == '>' ? 0 : ft_atoi(tok->data);
node->data.redir.word.word = ft_strdup(next_tok->data); node->data.redir.word.word = ft_strdup(next_tok->data);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2017/02/03 14:07:55 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:30:37 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ int parse_lessand(t_btree **ast, t_list **start, t_list **lst)
node->type = TK_LESSAND; node->type = TK_LESSAND;
tok = (*lst)->content; tok = (*lst)->content;
and = ft_strchr(tok->data, '&'); and = ft_strchr(tok->data, '&');
node->data.redir.n = *tok->data == '<' ? 1 : ft_atoi(tok->data); node->data.redir.n = *tok->data == '<' ? 0 : ft_atoi(tok->data);
node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.word.fd = ft_atoi(and + 1);
node->data.redir.close = node->data.redir.close =
tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ /* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */
/* Updated: 2017/02/03 13:58:57 by jhalford ### ########.fr */ /* Updated: 2017/02/06 16:07:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */