diff options
-rw-r--r-- | Makefile | 26 | ||||
-rw-r--r-- | inc/minishell.h | 10 | ||||
-rw-r--r-- | src/ft_echo.c | 31 | ||||
-rw-r--r-- | src/ft_process_arg.c | 22 | ||||
-rw-r--r-- | src/ft_pwd.c | 13 | ||||
-rw-r--r-- | src/main.c | 10 |
6 files changed, 98 insertions, 14 deletions
@@ -13,34 +13,37 @@ SRCS_DIR = src/ SRCS = \ - ${SRCS_DIR}main.c + ${SRCS_DIR}main.c \ + ${SRCS_DIR}ft_process_arg.c \ + ${SRCS_DIR}ft_echo.c \ + ${SRCS_DIR}ft_pwd.c OBJS_DIR = obj/ OBJS = $(patsubst ${SRCS_DIR}%.c,${OBJS_DIR}%.o,${SRCS}) -INCS_DIR = -Linc/ \ - -Llibft/ -INCS = -iminishell.h \ - -ilibft.h +INCS_DIR = -Iinc/ -Ilibft/ -LIB_DIR = libft/ -LIB = ft +LIB_DIR = -Llibft/ +LIB = -lft CC = gcc CFLAGS = -Wall -Wextra -Werror +DEBUG = -g3 +FSANITIZE = -fsanitize=address + NAME = minishell RM = rm -rf MKDIR = mkdir -p -${OBJS_DIR}%.o: ${SRCS_DIR}%.c +${OBJS_DIR}%.o: ${SRCS_DIR}%.c inc/minishell.h libft/libft.h ${MKDIR} ${OBJS_DIR} - ${CC} ${CFLAGS} ${INCS_DIR} ${INCS} -o $@ -c $< + ${CC} ${CFLAGS} ${DEBUG} ${INCS_DIR} -o $@ -c $< $(NAME): ${OBJS} - ${CC} ${CFLAGS} -L${LIB_DIR} -l${LIB} -o ${NAME} ${OBJS} + ${CC} ${CFLAGS} ${DEBUG} ${LIB_DIR} ${LIB} -o ${NAME} ${OBJS} all: ${NAME} @@ -54,8 +57,7 @@ fclean: clean re: fclean all build: ${OBJS} - ${CC} ${CFLAGS} -g3 -fsanitize=address -I./ -o a.out \ - ${OBJS} + ${CC} ${CFLAGS} ${DEBUG} ${FSANITIZE} ${LIB_DIR} ${LIB} -o a.out ${OBJS} default: all diff --git a/inc/minishell.h b/inc/minishell.h index e69de29..f92195c 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -0,0 +1,10 @@ +#ifndef _MINISHELL_H__ +#define _MINISHELL_H__ + +#include <inttypes.h> + +int ft_process_arg(const char *arg); +int ft_echo(char **com, uint8_t n); +int ft_pwd(void); + +#endif diff --git a/src/ft_echo.c b/src/ft_echo.c index a7e6303..66d803f 100644 --- a/src/ft_echo.c +++ b/src/ft_echo.c @@ -1,5 +1,32 @@ #include <libft.h> -void -ft_echo(const char *arg) +#include <minishell.h> +#include <inttypes.h> + +int +ft_echo(char **com, uint8_t n) { + uint8_t i; + int fd; + + i = 1; + fd = 1; + if (!com[1]) + ft_putendl_fd("", fd); + else if (ft_strncmp(com[1], "-n", ft_strlen(com[1]))) + { + while (i < n) + { + ft_putendl_fd(com[i], fd); + i++; + } + } + else + { + while (i < n) + { + ft_putstr_fd(com[i], fd); + i++; + } + } + return (0); } diff --git a/src/ft_process_arg.c b/src/ft_process_arg.c new file mode 100644 index 0000000..d82f9da --- /dev/null +++ b/src/ft_process_arg.c @@ -0,0 +1,22 @@ +#include <libft.h> +#include <minishell.h> +#include <inttypes.h> + +int +ft_process_arg(const char *arg) +{ + char **com; + uint8_t i; + + i = 0; + com = ft_split(arg, ' '); + while (com[i]) + i++; + if (!ft_strncmp(com[0], "exit", ft_strlen(com[0]))) + return (8); + else if (!ft_strncmp(com[0], "echo", ft_strlen(com[0]))) + ft_echo(com, i); + else if (!ft_strncmp(com[0], "pwd", ft_strlen(com[0]))) + ft_pwd(); + return (0); +} diff --git a/src/ft_pwd.c b/src/ft_pwd.c new file mode 100644 index 0000000..24394fe --- /dev/null +++ b/src/ft_pwd.c @@ -0,0 +1,13 @@ +#include <libft.h> +#include <stddef.h> +#include <unistd.h> + +int +ft_pwd(void) +{ + char *buff; + + buff = NULL; + ft_putstr(getcwd(buff, 1000)); + return (0); +} @@ -12,6 +12,7 @@ /* ************************************************************************** */ #include <libft.h> +#include <minishell.h> #include <stdlib.h> #include <stddef.h> #include <unistd.h> @@ -25,8 +26,14 @@ int uint8_t i; i = 0; + arg = NULL; while (1) { + if (arg) + { + free(arg); + arg = NULL; + } if (!(arg = (char*)ft_calloc(129, sizeof(char)))) return (1); write(1, "joe-shell~> ", 12); @@ -44,7 +51,10 @@ int i++; } arg[i] = '\0'; + if (ft_process_arg(arg) == 8) + break ; } free(arg); + arg = NULL; return (0); } |