diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-23 16:16:09 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-23 16:16:09 +0200 |
commit | 4dfd892f968fb840eddd5fc192c061a997afa588 (patch) | |
tree | 3016314080aa4f8e0a211f278a756753a81231b6 /src/ft_e_builtin.c | |
parent | Fine builtins execution, now norme (diff) | |
download | 42-minishell-4dfd892f968fb840eddd5fc192c061a997afa588.tar.gz 42-minishell-4dfd892f968fb840eddd5fc192c061a997afa588.tar.bz2 42-minishell-4dfd892f968fb840eddd5fc192c061a997afa588.tar.xz 42-minishell-4dfd892f968fb840eddd5fc192c061a997afa588.tar.zst 42-minishell-4dfd892f968fb840eddd5fc192c061a997afa588.zip |
Very good redirs
Diffstat (limited to '')
-rw-r--r-- | src/ft_e_builtin.c | 116 |
1 files changed, 66 insertions, 50 deletions
diff --git a/src/ft_e_builtin.c b/src/ft_e_builtin.c index dada893..4f58f44 100644 --- a/src/ft_e_builtin.c +++ b/src/ft_e_builtin.c @@ -17,77 +17,93 @@ #include <unistd.h> #include <sys/wait.h> #include <errno.h> +#include "ft_f_file.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" +static void + ft_dup_redirs(const t_lcom *ptr, + t_msh *msh) +{ + int32_t fd; + + if (ptr->redir == -1) + { + if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) + ft_f_file(ptr->rdrpath, msh); + /* TODO: handle < redir */ + } + if (ptr->redir == 1) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) + ft_f_file(ptr->rdrpath, msh); + dup2(fd, STDOUT_FILENO); + close(fd); + } + if (ptr->redir == 2) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) + ft_f_file(ptr->rdrpath, msh); + dup2(fd, STDOUT_FILENO); + close(fd); + } +} + +static void + ft_e_builtin_child(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + int32_t ret; + + ft_dup_redirs(ptr, msh); + ret = msh->bu_ptr[bu_id](ptr->args, msh); + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + exit(ret); +} + +static void + ft_e_builtin_parent(pid_t pid, + t_msh *msh) +{ + int32_t status; + + while (wait(&status) != pid) + { + /* TODO: fix this unnormed syntax */ + } + msh->ret = WEXITSTATUS(status); +} + void ft_e_builtin(const t_lcom *ptr, uint8_t bu_id, t_msh *msh) { - int32_t fd; - int32_t status; - int32_t ret; pid_t pid; /* TODO: handle exit | bu_id = 6 */ - if ((pid = fork()) == 0) + if (bu_id != 6) { - if (ptr->redir == -1) + if ((pid = fork()) == 0) { - if (ptr->redir == -1 && (fd = open(ptr->rdrpath, O_RDONLY)) == -1) - { - if (errno == ENOENT) - ft_dprintf(STDERR_FILENO, - "minishell: %s: No such file or directory\n", ptr->rdrpath); - else if (errno == EACCES) - ft_dprintf(STDERR_FILENO, - "minishell: %s: Permission denied\n", ptr->rdrpath); - } + ft_e_builtin_child(ptr, bu_id, msh); } - else if (ptr->redir == 1) + else if (pid < 0) { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) - { - /* TODO: handle err with errno */ - if (errno == EACCES) - ft_dprintf(STDERR_FILENO, - "minishell: %s: Permission denied\n", ptr->rdrpath); - } - dup2(fd, STDOUT_FILENO); - close(fd); + /* TODO: handle fork failed */ } - else if (ptr->redir == 2) + else { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) - { - /* TODO: handle err with errno */ - if (errno == EACCES) - ft_dprintf(STDERR_FILENO, - "minishell: %s: Permission denied\n", ptr->rdrpath); - } - dup2(fd, STDOUT_FILENO); - close(fd); + ft_e_builtin_parent(pid, msh); } - msh->ret = msh->bu_ptr[bu_id](ptr->args, msh); - ret = msh->ret; - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - exit(ret); - } - else if (pid < 0) - { - /* TODO: handle fork failed */ } else { - while (wait(&status) != pid) - { - /* TODO: fix this unnormed syntax */ - } - msh->ret = status; + msh->ret = msh->bu_ptr[bu_id](ptr->args, msh); } } |