This commit is contained in:
Jack Halford 2017-03-27 01:03:57 +02:00
parent d7ad419073
commit 31d0a2e5ed
16 changed files with 67 additions and 53 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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)

View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,23 +6,19 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

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

View file

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

View file

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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

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

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();