diff --git a/42sh/11 b/42sh/11 new file mode 100644 index 00000000..367e9eae --- /dev/null +++ b/42sh/11 @@ -0,0 +1,16 @@ +11 +42sh +Makefile +Session.vim +a.out +debug +donovan_segaults_06-02 +file +includes +libft +objs +out +pdf +script.sh +src +update_makefile.sh diff --git a/42sh/9- b/42sh/9- new file mode 100644 index 00000000..62cc79f8 --- /dev/null +++ b/42sh/9- @@ -0,0 +1,17 @@ +11 +42sh +9- +Makefile +Session.vim +a.out +debug +donovan_segaults_06-02 +file +includes +libft +objs +out +pdf +script.sh +src +update_makefile.sh diff --git a/42sh/Makefile b/42sh/Makefile index dc62070f..9628207c 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/06 15:47:04 by wescande ### ########.fr # +# Updated: 2017/02/06 21:49:51 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -29,18 +29,6 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ -completion/c_output.c\ -completion/c_binary.c\ -completion/c_clear.c\ -completion/c_files.c\ -completion/c_init.c\ -completion/c_matching.c\ -completion/c_parser.c\ -completion/c_pathsolver.c\ -completion/c_printer.c\ -completion/c_sizing.c\ -completion/c_terminal.c\ -completion/completion.c\ builtin/bt_read_get.c\ builtin/bt_read_parse.c\ builtin/bt_read_term.c\ @@ -52,7 +40,20 @@ builtin/builtin_read.c\ builtin/builtin_setenv.c\ builtin/builtin_unsetenv.c\ builtin/is_builtin.c\ +completion/c_binary.c\ +completion/c_clear.c\ +completion/c_files.c\ +completion/c_init.c\ +completion/c_matching.c\ +completion/c_output.c\ +completion/c_parser.c\ +completion/c_pathsolver.c\ +completion/c_printer.c\ +completion/c_sizing.c\ +completion/c_terminal.c\ +completion/completion.c\ exec/ast_free.c\ +exec/bad_fd.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_command.c\ @@ -69,6 +70,11 @@ exec/process_reset.c\ exec/process_setexec.c\ exec/process_setgroup.c\ exec/process_setsig.c\ +exec/redirect_dgreat.c\ +exec/redirect_great.c\ +exec/redirect_greatand.c\ +exec/redirect_less.c\ +exec/redirect_lessand.c\ exec/set_exitstatus.c\ glob/dir_glob.c\ glob/expand_brace.c\ diff --git a/42sh/donovan_segaults_06-02 b/42sh/donovan_segaults_06-02 new file mode 100644 index 00000000..f8217daf --- /dev/null +++ b/42sh/donovan_segaults_06-02 @@ -0,0 +1,533 @@ +ls +cat +`ls` +` +`ls` +`ls` +`ls` +cat * +ls * +echo "*" +echo * +echo */* +ls +make +make +ls +ls | cat +ls | cat -e +`ls` +`ls` +`ls` +`ls` +`ls` +`ls` +`ls` +`ls` +ls +`ls` +echo `ls -l` +(ls) +`ls` +` +`ls` +` +` +` +` +` +` +(ls)) +(ls)) +(ls)) +` +( +`ls` +`ls` +`ls` +ls +` +ls +`ls` +` +) +(vim)) +ls +`ls` +`l` +ls +ls +ls +`ls` +ls +`ls` +ls m +ls m 2>/dev/null +ls +ls >/dev/null +ls m +ls m 2>&1 +ls m +ls m >/dev/null +ls m 2>/dev/null +ls m 2>&- +ls m +ls m 2>&- +ls +ls 1>/dev/null +ls merde +ls merde 2>/dev/null +ls merde 1>/dev/null +ls merde 3>/dev/null +ls merde 2>/dev/null +ls 2>/dev/null +ls +ls >/dev/null +ls >/dev/null +ls >/dev/null +ls >/dev/null +ls >/dev/null +ls >/dev/null +ls 1>/dev/null +ls 1>/dev/null +ls merde +ls merde >/dev/null +ls merde 2>/dev/null +ls merde 2>&- +ls merde 2<&- +ls merde 2<&- +ls merde 2<&- +ls +ls >&- +ls >&- +ls 2>&- +ls m 2>&- +ls +ls 1>&2 2>&- +ls 2>&- 1>&2 +ls 2>&- 1>&2 +ls 1>&2 2>&- +ls 1>&2 2>&- +ls 2>&- 1>&2 +ls 1>&2 +ls 1>&2 1>&- +ls 1>&- 1>&2 +ls 2>&- 1>&2 +ls 1>&- 2>&- +ls 1>&2- 2>&- +ls 1>&2 2>&- +ls 2>&- 1>&2 +ls 1>&2- +ls 1>&2 +ls 1>&2 1>&- +ls 1>&- 1>&2 +ls 1>&- 1>&2 +ls 2>&- +ls 2>&- 1>&- +ls 2>&- 1>&2 +ls 2>&- 1>&2 +ls 2>&- 1>&2 +ls 2>&- 1>&2 +ls 2>&- 1>&- +ls 2>&- 1>&- +ls 2>&- 1>&2 +ls 1>&2 2>&- + +ls +ls 2>&- +ls 2>&- 1>&2 +ls + +ls + +ls + +ls + ls +ls + ls + ls + + + ls +ls + +ls + + + + + + ls + +`ls` +` ` +`` `` +echo `ls``ls` +echo `ls` `ls` +`ls``ls` +a`ls` +`ls`` +`ls``ls` +`` +``` +```` +`` ``` +`` `` +```` +`` +`` +` +`` +ls +`` +` +make +`` +```` +`` `` +```` +`` `` +` +`` +```` +`` `` +```` +`` +`` +```` +```` +```` +```` +a`ls` echo `ls` +echo `ls` +echo `ls` +a`ls` +a`ls` +a`ls` +a`ls` +a`ls` +a`ls | wc` +ls + ls + ls +ls | cat + + +ls + +`` +` ` +```` +`` `` +cd +ls +alalalalal`ls` + + + 1 + ` + `` + ` ` +(vim)& `ls` +jobs +fg +fg +jobs -l +kill -9 80401 +jobs +jobs +fg +vim& +jobs +ls +vim& +ls& +jobs +fg +jobs +ls& +jobs +jobs -l +ls +ls& +jobs +vim +jobs +jobs +ls +ls& +jobs +ls& +jobs +vim& +fg +ls +a`la` +ls + +ls +ls + + +```` ```` `` +ls + +ls +ls >&- +ls >&waf- +ls >&24- +ls +{} +{()} +({}) +{(ls)} +() +ls +read -n 4 +read -n 5 +echo $REPLY +read -n 4 +l +read -n 4 +read - n5 +read -n 5 +echo $REPLY | cat -e +echo $REPLY +read -n 5 +echo $REPLY +read -n 3 +echo $REPLY +read -n 3 +echo $REPLY +read -n -s +read -n 2 -s +read -s +ls +read -n 3 +echo $REPLY +echo $REPLY | cat -e +echo $PATH +read -n 10 +echo $REPLY +read -n 10 +echo $REPLY +(ls) +(ls)l +(ls) +(ls)l +(ls) +(ls)l +{()} +{} +42sh_cpy > wc -l < Makefile +42sh_cpy > wc -l > Makefile +wc -l < Makefile +cat out1 +ls out +ls < out +ls > out +ls < out +ls>out +lsout +lsout +lsout +lsout +lsout +cat out +lsout +lsout +lsout +cat out +ls>out +lsout +rm out +ls >out +ls -l +ls out +ls +lsout +lsout +cat out +ls>out +make +ls>out +ls&- +ls +ls >&2 2>&- +ls > out +rm out +ls > out +ls -l +ls >out +; +l +ls +ls>out +cat out +wc -l out +ls -l >out +catout +cat out +cat out +lsout +lsout +wc -l file1 +cat file1 +cat file1 +cat file1 +cat out +wc -l &- 1>&2 +cat +cat /dev/random|base64|head -c 8 & +cat /dev/random|base64|head -c 8 +ls +stty +stty -a +ls +cat +ls 'abc +def' +ls 'abc +' +ls >&- +ls 2>&- +ls mm 2>&- +ls mm 2>&- +cd >&- +./a.out +ls +./a.out +./42sh +pwd +/Users/jhalford/minishell/a.out +ls | /Users/jhalford/minishell/a.out +/a | cat /dev/random | base64 > /dev/null +./42sh +sleep 66666 & +jobs +echo '\ + +' +ls \ + +ls '\' +ls "\" +\ + +\ +\ +\ +\ +\\ +\\ +abc\ +def +l\ +s +ls 3>&- +ls >&3 +ls -- -1>file3 +ls -- 9>file3 +ls -- 10>file3 +ls -- 9999999999999999999999999999999999999999999999999999999>file3 +ls -- 2147483648>file3 +ls -- 2147483647>file3 +ls -- >&2147483648 +ls -- >&2147483649 +ulimit -a +& +\\ \ \\\ +${$(})} ${$((}))} ${`}`} ${${a}} +( ls # ) +ls&&ls;ls||ls; +ls&&ls;ls||ls +echo a && echo b; +echo a || echo b; +echo a ||; echo b; +echo a; || echo b; +(ls;) ; +ls ;; ls +` ( echo "(ls" )` +` ( echo "(ls(" )` +` ( echo "(ls((" )` +`echo "ls -l"` +` echo "ls `echo .`"` diff --git a/42sh/file b/42sh/file new file mode 100644 index 00000000..62cc79f8 --- /dev/null +++ b/42sh/file @@ -0,0 +1,17 @@ +11 +42sh +9- +Makefile +Session.vim +a.out +debug +donovan_segaults_06-02 +file +includes +libft +objs +out +pdf +script.sh +src +update_makefile.sh diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 8519f2af..609c5936 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/02/06 15:54:00 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 22:34:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,14 +84,19 @@ int exec_command(t_btree **ast); int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); int process_setgroup(t_process *p, pid_t pid); -int process_redirect(t_process *p); -int process_do_redirection(t_redir *redir); void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); -void fd_redirect(void); -void fd_reset(void); +int process_redirect(t_process *p); +int process_do_redirection(t_redir *redir); +void bad_fd(int fd); +int redirect_great(t_redir *redir, int *fdold, int *fdnew); +int redirect_less(t_redir *redir, int *fdold, int *fdnew); +int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew); +int redirect_dless(t_redir *redir, int *fdold, int *fdnew); +int redirect_greatand(t_redir *redir, int *fdold, int *fdnew); +int redirect_lessand(t_redir *redir, int *fdold, int *fdnew); char *ft_findexec(char *path, char *file); diff --git a/42sh/src/exec/bad_fd.c b/42sh/src/exec/bad_fd.c new file mode 100644 index 00000000..4d5399d3 --- /dev/null +++ b/42sh/src/exec/bad_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bad_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:32:43 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:34:21 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void bad_fd(int fd) +{ + ft_dprintf(2, "{red}%s: %i: Bad file descriptor{eoc}\n", + SHELL_NAME, fd); + exit(1); +} diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c index 93726aec..cb5f618b 100644 --- a/42sh/src/exec/process_do_redirection.c +++ b/42sh/src/exec/process_do_redirection.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/02/06 16:51:17 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 22:54:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,46 +22,44 @@ int process_do_redirection(t_redir *redir) int fdold; int fdnew; - if (redir->type & (TK_GREAT | TK_DGREAT)) - { - fdold = redir->n; - if ((fdnew = open(redir->word.word, O_WRONLY | O_CREAT - | ((redir->type & TK_GREAT) ? O_TRUNC : O_APPEND), - 0644)) < 0) - { - DG("open errno=%i", errno); - exit(1); - } - } + if (redir->n > 9) + bad_fd(redir->n); + if (redir->type & TK_GREAT) + redirect_great(redir, &fdold, &fdnew); + else if (redir->type & TK_GREAT) + redirect_dgreat(redir, &fdold, &fdnew); else if (redir->type & TK_LESS) + redirect_less(redir, &fdold, &fdnew); + else if (redir->type & TK_GREATAND) { - fdold = 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)) - { - if (redir->close) - { - close(redir->n); + if (redirect_greatand(redir, &fdold, &fdnew)) return (0); + } + else if (redir->type & TK_LESSAND) + { + if (redirect_lessand(redir, &fdold, &fdnew)) + return (0); + } + else + exit(42); + DG("gonna redirect dup2(%i,%i)", fdold, fdnew); + if (fd_is_valid(fdnew)) + { + if (fd_is_valid(fdold)) + { + dup2(fdold, fdnew); + close(fdold); } else - { - fdold = redir->type & TK_LESSAND ? redir->word.fd : redir->n; - fdnew = redir->type & TK_LESSAND ? redir->n : redir->word.fd; - } + bad_fd(fdold); } else { - ft_dprintf(2, "{red}%s: redirection error.{eoc}\n", SHELL_NAME); - return (-1); + DG("[%i] is not a valid fd", fdnew); + if (fdnew <= 2) + close(fdnew); + else + bad_fd(fdnew); } - fd_is_valid(fdold) ? dup2(fdnew, fdold) : close(fdnew); - close(fdnew); return (0); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 36c3c577..9574008b 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/02/06 16:46:06 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 22:23:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c new file mode 100644 index 00000000..485fbda9 --- /dev/null +++ b/42sh/src/exec/redirect_dgreat.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_dgreat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:07:37 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:27:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew) +{ + *fdold = redir->n; + if ((*fdnew = open(redir->word.word, + O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) + { + DG("open errno=%i", errno); + exit(1); + } + return (0); +} diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c new file mode 100644 index 00000000..67aaa003 --- /dev/null +++ b/42sh/src/exec/redirect_great.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_great.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:42:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int redirect_great(t_redir* redir, int *fdold, int *fdnew) +{ + *fdnew = redir->n; + if ((*fdold = open(redir->word.word, + O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) + { + DG("open errno=%i", errno); + exit(1); + } + return (0); + dup2(fdold, fdnew); +} diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c new file mode 100644 index 00000000..162b78b2 --- /dev/null +++ b/42sh/src/exec/redirect_greatand.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_greatand.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:54:20 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "exec.h" + +int redirect_greatand(t_redir *redir, int *fdold, int *fdnew) +{ + if (redir->word.fd > 9) + bad_fd(redir->word.fd); + if (redir->close) + { + close(redir->n); + return (1); + } + else + { + *fdnew = redir->n; + *fdold = redir->word.fd; + } + if (fd_is_valid(fdold)) + { + dup2(fdold, fdnew); + close(fdold); + } + else + bad_fd(fdold); + return (0); +} diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c new file mode 100644 index 00000000..5e404390 --- /dev/null +++ b/42sh/src/exec/redirect_less.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_less.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:38:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int redirect_less(t_redir *redir, int *fdold, int *fdnew) +{ + *fdnew = redir->n; + if ((*fdold = 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); + } + return (0); + dup2(fdold, fdnew); +} diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c new file mode 100644 index 00000000..52a4b497 --- /dev/null +++ b/42sh/src/exec/redirect_lessand.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirect_lessand.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ +/* Updated: 2017/02/06 22:55:03 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int redirect_lessand(t_redir *redir, int *fdold, int *fdnew) +{ + if (redir->word.fd > 9) + bad_fd(redir->word.fd); + if (redir->close) + { + close(redir->n); + return (1); + } + else + { + *fdnew = redir->word.fd; + *fdold = redir->n; + } + if (fd_is_valid(fdnew)) + { + dup2(fdold, fdnew); + close(fdold); + } + else + bad_fd(fdold); + return (0); +} diff --git a/42sh/src/lexer/ft_lexer.c b/42sh/src/lexer/ft_lexer.c index 69c40fee..71b033f1 100644 --- a/42sh/src/lexer/ft_lexer.c +++ b/42sh/src/lexer/ft_lexer.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 15:30:59 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:40:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 21:49:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 82b9e4b4..8f0e2ada 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/01/27 15:54:53 by wescande ### ########.fr */ +/* Updated: 2017/02/06 22:29:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index ea7c8398..2fbc72d2 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ -/* Updated: 2017/01/27 15:55:16 by wescande ### ########.fr */ +/* Updated: 2017/02/06 22:30:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lessand.c b/42sh/src/lexer/lexer_lessand.c index fc6eef56..bb5b900c 100644 --- a/42sh/src/lexer/lexer_lessand.c +++ b/42sh/src/lexer/lexer_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:51 by jhalford #+# #+# */ -/* Updated: 2017/01/27 15:55:27 by wescande ### ########.fr */ +/* Updated: 2017/02/06 21:58:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */