summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--inc/minishell.h2
-rw-r--r--src/ft_error.c14
-rw-r--r--src/ft_exit.c29
-rw-r--r--src/ft_process_arg.c17
-rw-r--r--src/main.c4
6 files changed, 54 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index f441681..46fce04 100644
--- a/Makefile
+++ b/Makefile
@@ -15,8 +15,10 @@ SRCS_DIR = src/
SRCS = \
${SRCS_DIR}main.c \
${SRCS_DIR}ft_process_arg.c \
+ ${SRCS_DIR}ft_error.c \
${SRCS_DIR}ft_echo.c \
- ${SRCS_DIR}ft_pwd.c
+ ${SRCS_DIR}ft_pwd.c \
+ ${SRCS_DIR}ft_exit.c
OBJS_DIR = obj/
OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS})
diff --git a/inc/minishell.h b/inc/minishell.h
index f92195c..ef33b88 100644
--- a/inc/minishell.h
+++ b/inc/minishell.h
@@ -6,5 +6,7 @@
int ft_process_arg(const char *arg);
int ft_echo(char **com, uint8_t n);
int ft_pwd(void);
+uint8_t ft_exit(const char **com);
+int ft_error(const char *com, int errno);
#endif
diff --git a/src/ft_error.c b/src/ft_error.c
new file mode 100644
index 0000000..2cc9cb6
--- /dev/null
+++ b/src/ft_error.c
@@ -0,0 +1,14 @@
+#include <libft.h>
+#include <minishell.h>
+
+void
+ft_error(const char *com, int errno)
+{
+ ft_putstr("joe-sh: ");
+ ft_putstr(com);
+ if (errno == 1)
+ ft_putendl(": too many arguments");
+ else if (errno == 127)
+ ft_putendl(": command not found");
+ return (errno);
+}
diff --git a/src/ft_exit.c b/src/ft_exit.c
new file mode 100644
index 0000000..e09212c
--- /dev/null
+++ b/src/ft_exit.c
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/* LE - / */
+/* / */
+/* ft_exit.c .:: .:/ . .:: */
+/* +:+:+ +: +: +:+:+ */
+/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */
+/* #+# #+ #+ #+# */
+/* Created: 2019/10/30 16:05:48 by rbousset #+# ## ## #+# */
+/* Updated: 2019/10/30 16:05:50 by rbousset ### #+. /#+ ###.fr */
+/* / */
+/* / */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <minishell.h>
+#include <stdlib.h>
+
+uint8_t
+ft_exit(const char **com)
+{
+ uint8_t i;
+
+ i = 0;
+ ft_putendl("exit");
+ while (com[i])
+ i++;
+ if (i > 2)
+ return (ft_error(com[0], 1));
+}
diff --git a/src/ft_process_arg.c b/src/ft_process_arg.c
index e1b6f8f..4f6ccf6 100644
--- a/src/ft_process_arg.c
+++ b/src/ft_process_arg.c
@@ -3,15 +3,6 @@
#include <inttypes.h>
int
-ft_not_found(const char *com)
-{
- ft_putstr("joe-sh: ");
- ft_putstr(com);
- ft_putendl(": command not found");
- return (127);
-}
-
-int
ft_process_arg(const char *arg)
{
char **com;
@@ -24,12 +15,12 @@ ft_process_arg(const char *arg)
while (com[i])
i++;
if (!ft_strncmp(com[0], "exit", ft_strlen(com[0])))
- return (8);
+ return (ft_exit(com));
else if (!ft_strncmp(com[0], "echo", ft_strlen(com[0])))
- ft_echo(com, i);
+ return (ft_echo(com, i));
else if (!ft_strncmp(com[0], "pwd", ft_strlen(com[0])))
- ft_pwd();
+ return (ft_pwd());
else
- ft_not_found(com[0]);
+ return (ft_error(com[0]), 127);
return (0);
}
diff --git a/src/main.c b/src/main.c
index 9df9d88..cfd3587 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,6 +24,7 @@ int
char c;
char *arg;
uint8_t i;
+ int ret;
i = 0;
arg = NULL;
@@ -51,8 +52,7 @@ int
i++;
}
arg[i] = '\0';
- if (ft_process_arg(arg) == 8)
- break ;
+ ret = ft_process_arg(arg);
}
free(arg);
arg = NULL;