diff --git a/42sh/Makefile b/42sh/Makefile index d75d9774..36d1d3b0 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/26 22:13:53 by jhalford ### ########.fr # +# Updated: 2017/03/26 22:15:25 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror +FLAGS = -Wall -Wextra -Werror -fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 7d483cd1..0c0530f9 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -194,12 +194,13 @@ str/ft_strtok.c\ str/ft_strtrim.c\ sys/create_directory.c\ sys/dup2_close.c\ +sys/fd_minor.c\ sys/fd_replace.c\ sys/ft_getenv.c\ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ sys/is_directory.c\ -sys/open_access.c\ +sys/try_access.c\ time/ft_mytime_free.c\ time/ft_mytime_get.c\ time/ft_time_isrecent.c diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index b3c42e22..25f77020 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/25 15:12:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:05:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,11 +33,12 @@ int ft_xattr_count(char *path); char *ft_getenv(char **env, char *key); -int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); +int try_access(char *file, int exists, t_flag a_flag); int is_directory(const char *path); char *create_directory(const char *path, const char *old_pathnames); int dup2_close(int fd1, int fd2); int fd_replace(int fd1, int fd2); +int fd_minor(int fd, int minor); #endif diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index 495ae484..a3502c52 100644 --- a/42sh/libft/src/sys/fd_replace.c +++ b/42sh/libft/src/sys/fd_replace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ -/* Updated: 2017/03/24 22:59:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:18:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,5 @@ int fd_replace(int fd1, int fd2) { if (fd1 != fd2) return (dup2_close(fd1, fd2)); - return (0); + return (-1); } diff --git a/42sh/libft/src/sys/open_access.c b/42sh/libft/src/sys/try_access.c similarity index 64% rename from 42sh/libft/src/sys/open_access.c rename to 42sh/libft/src/sys/try_access.c index bc1ee88e..79335cd4 100644 --- a/42sh/libft/src/sys/open_access.c +++ b/42sh/libft/src/sys/try_access.c @@ -6,23 +6,19 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 01:10:56 by jhalford #+# #+# */ -/* Updated: 2017/03/25 15:07:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 22:36:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm) +int try_access(char *file, int exists, t_flag a_flag) { - int fd; - - if (a_flag & F_OK && access(file, F_OK) != 0) - return (-ERR_SET(E_SYS_NOFILE, file)); - if (is_directory(file)) - return (-ERR_SET(E_SYS_ISDIR, file)); - if (access(file, F_OK) == 0 && access(file, a_flag) != 0) - return (-ERR_SET(E_SYS_NOPERM, file)); - if ((fd = open(file, o_flag, o_perm)) < 0) - exit(1); - return (fd); + if (exists && access(file, F_OK) != 0) + return (ERR_SET(E_SYS_NOFILE, file) * 0 - 1); + else if (is_directory(file)) + return (ERR_SET(E_SYS_ISDIR, file) * 0 - 1); + else if (access(file, F_OK) == 0 && access(file, a_flag) != 0) + return (ERR_SET(E_SYS_NOPERM, file) * 0 - 1); + return (0); } diff --git a/42sh/src/exec/exec_destroy.c b/42sh/src/exec/exec_destroy.c index 28f092f8..e6e2b525 100644 --- a/42sh/src/exec/exec_destroy.c +++ b/42sh/src/exec/exec_destroy.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/26 22:10:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:33:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,5 @@ int exec_destroy(t_exec *exec) ft_lstdel(&exec->op_stack, ft_lst_cfree); ft_lstdel(&jobc->first_job, job_free); i = -1; - while (++i < 10) - ft_lstdel(&exec->fd_save[i], ft_lst_cfree); return (0); } diff --git a/42sh/src/exec/exec_popfds.c b/42sh/src/exec/exec_popfds.c index 39d8c6a1..d4fc942e 100644 --- a/42sh/src/exec/exec_popfds.c +++ b/42sh/src/exec/exec_popfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/26 21:31:05 by jhalford #+# #+# */ -/* Updated: 2017/03/26 21:31:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:58:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,16 @@ int exec_popfds(void) { int i; + int fd; t_exec *exec; exec = &data_singleton()->exec; i = -1; while (++i < 10) - pop(&exec->fd_save[i]); + { + fd = pop(&exec->fd_save[i]); + if (fd != -1) + close(fd); + } return (0); } diff --git a/42sh/src/exec/exec_pushfds.c b/42sh/src/exec/exec_pushfds.c index 7f156b1b..31c16cde 100644 --- a/42sh/src/exec/exec_pushfds.c +++ b/42sh/src/exec/exec_pushfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/26 21:31:46 by jhalford #+# #+# */ -/* Updated: 2017/03/26 21:34:27 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:58:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,15 @@ int exec_pushfds(void) { int i; + int fd; t_exec *exec; exec = &data_singleton()->exec; i = -1; while (++i < 10) - push(&(exec->fd_save[i]), fcntl(i, F_DUPFD_CLOEXEC, 10)); + { + fd = fcntl(i, F_DUPFD_CLOEXEC, 10); + push(&(exec->fd_save[i]), fd); + } return (0); } diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index ebf60e6d..67a1c7f1 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/20 21:13:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:51:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,5 +19,6 @@ int fd_is_valid(int fd, int has_mode) fd_modes = fcntl(fd, F_GETFL); fd_flags = fcntl(fd, F_GETFD); - return ((fd_flags != -1 || errno != EBADF) && fd_modes & has_mode); + return ((fd_flags != -1 || errno != EBADF) + && (has_mode == -1 || fd_modes & has_mode)); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 5dfe74fc..bde46f83 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/26 22:13:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 01:01:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,10 +54,10 @@ int process_launch(t_process *p) { exec_pushfds(); p->map.launch(p); + exec_popfds(); + shell_resetfds(); + shell_resetsig(); } - exec_popfds(); - shell_resetfds(); - shell_resetsig(); process_free(p, 0); return (0); } diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index 42f94d69..cbb3d11a 100644 --- a/42sh/src/exec/process_set.c +++ b/42sh/src/exec/process_set.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/26 22:14:09 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:10:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,15 +58,13 @@ int process_set(t_process *p, t_btree *ast) || (EXEC_IS_OR_IF(exec->attrs) && ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0)) return (1); - fds[PIPE_WRITE] = STDOUT; - fds[PIPE_READ] = STDIN; - (op == TK_PIPE) ? pipe(fds) : (0); exec->job.attrs |= (op == TK_AMP ? JOB_BG : 0); p->fdin = exec->fdin; - p->to_close = fds[PIPE_READ]; - p->fdout = fds[PIPE_WRITE]; + (op == TK_PIPE) ? pipe(fds) : (0); + p->fdout = (op == TK_PIPE) ? fds[PIPE_WRITE] : STDOUT; + exec->fdin = (op == TK_PIPE) ? fds[PIPE_READ] : STDIN; + p->to_close = exec->fdin; p->pid = 0; - exec->fdin = fds[PIPE_READ]; if (ast) p->redirs = ft_lstmap( ((t_astnode *)ast->item)->data.cmd.redir, &redir_copy); diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c index 3e448fd8..a2d57f1c 100644 --- a/42sh/src/exec/redirect_dgreat.c +++ b/42sh/src/exec/redirect_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:07:37 by jhalford #+# #+# */ -/* Updated: 2017/03/25 01:54:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 22:36:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,12 @@ int redirect_dgreat(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, R_OK, - O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) + fdnew = redir->n; + if ((try_access(redir->word, 0, W_OK))) return (ft_perror(NULL)); - dup2(fdold, fdnew); + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) + exit (1); + + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index d0cc00a9..bea4de8d 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/25 02:25:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 22:36:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,10 @@ int redirect_great(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, R_OK, - O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) + if ((try_access(redir->word, 0, W_OK))) return (ft_perror(NULL)); + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) + exit (1); fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index 390c91e9..0e24b2c7 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ -/* Updated: 2017/03/26 18:25:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 01:02:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,10 @@ int redirect_less(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, F_OK | W_OK, - O_RDONLY, 0)) < 0) + if ((try_access(redir->word, 1, R_OK)) != 0) return (ft_perror(NULL)); - dup2(fdold, fdnew); + if ((fdold = open(redir->word, O_RDONLY, 0644)) < 0) + exit (1); + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/line_editing/ft_prompt.c b/42sh/src/line_editing/ft_prompt.c index c4d0201e..3f12ed95 100644 --- a/42sh/src/line_editing/ft_prompt.c +++ b/42sh/src/line_editing/ft_prompt.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ -/* Updated: 2017/03/23 10:52:08 by gwojda ### ########.fr */ +/* Updated: 2017/03/27 01:00:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ static int promt_git_status(int fd) char *line; get_next_line(fd, &line); + close(fd); tmp = line; if (ft_strrchr(line, '/')) line = ft_strdup(ft_strrchr(line, '/') + 1); diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index 42e361c3..fcebafba 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/26 21:59:25 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:36:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ void data_exit(void) { t_data *data; + int i; data = data_singleton(); /* ft_strdel(&data->line.input); */ @@ -25,6 +26,9 @@ void data_exit(void) lexer_destroy(&data->lexer); parser_destroy(&data->parser); exec_destroy(&data->exec); + i = -1; + while (++i < 10) + ft_lstdel(&data->exec.fd_save[i], ft_lst_cfree); ft_lstdel(&data->lst_func, &tree_func_free); ft_save_termios(-1); ft_free_hash_table();