summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/ft_e_builtin.c116
-rw-r--r--src/ft_e_lcom.c2
-rw-r--r--src/ft_f_file.c42
-rw-r--r--src/ft_f_file.h21
4 files changed, 129 insertions, 52 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);
}
}
diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c
index b535813..7922997 100644
--- a/src/ft_e_lcom.c
+++ b/src/ft_e_lcom.c
@@ -28,11 +28,9 @@ static uint8_t
return (i);
}
-#include <stdio.h>
uint8_t
ft_e_lcom(t_msh *msh)
{
- /* TODO: norme */
t_lcom *ptr;
uint8_t bu_id;
diff --git a/src/ft_f_file.c b/src/ft_f_file.c
new file mode 100644
index 0000000..53e0c8b
--- /dev/null
+++ b/src/ft_f_file.c
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_f_file.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include "ft_s_lcom.h"
+#include "ft_s_destroy.h"
+
+void
+ ft_f_file(const char path[],
+ t_msh *msh)
+{
+ if (errno == ENOENT)
+ {
+ ft_dprintf(STDERR_FILENO,
+ "minishell: %s: No such file or directory\n", path);
+ }
+ else if (errno == EACCES)
+ {
+ ft_dprintf(STDERR_FILENO,
+ "minishell: %s: Permission denied\n", path);
+ }
+ else if (errno == EISDIR)
+ {
+ ft_dprintf(STDERR_FILENO,
+ "minishell: %s: Is a directory\n", path);
+ }
+ ft_lcom_clear(&msh->curr);
+ ft_s_destroy(msh);
+ exit(1);
+}
diff --git a/src/ft_f_file.h b/src/ft_f_file.h
new file mode 100644
index 0000000..964dcf9
--- /dev/null
+++ b/src/ft_f_file.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_f_file.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_F_FILE_H
+#define FT_F_FILE_H
+
+#include "ft_s_struct.h"
+
+void ft_f_file(const char path[],
+ t_msh *msh);
+
+#endif