From cf788bc62c2f0ed4ee93e4d10c9603ddf00dd10a Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Fri, 23 Nov 2018 16:41:43 +0100 Subject: [PATCH] WIP added libft & creating useable shell --- .gitmodules | 3 ++ Makefile | 49 ++++++++++++++++++++++++++++++++ includes/minishell.h | 20 ++++++++++++++ libft | 1 + srcs/main.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 includes/minishell.h create mode 160000 libft create mode 100644 srcs/main.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b03f5d3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = https://github.com/tvdu29/libft diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9e99a1f --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +#******************************************************************************# +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tmaze +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/11/22 14:43:33 by tmaze #+# #+# # +# Updated: 2018/11/23 10:38:13 by tmaze ### ########.fr # +# # +#******************************************************************************# + +CC := gcc +CCFLAGS := -Wall -Werror -Wextra +CCSTD := + +NAME := minishell + +SRCS := main.c +OBJS_DIR := objs +OBJS := $(SRCS:.c=.o) +INCLS := -Iincludes -Ilibft +LIBS := -Llibft -lft + +.PHONY = all clean fclean re + +all: $(NAME) + +$(NAME): $(OBJS_DIR)/$(OBJS) libft/libft.a + $(CC) $(CCFLAGS) $(CCSTD) $(INCLS) $< -o $(NAME) $(LIBS) + +$(OBJS_DIR)/%.o: srcs/%.c includes/minishell.h $(OBJS_DIR) + $(CC) $(CCFLAGS) $(CCSTD) $(INCLS) -c $< -o $@ + +$(OBJS_DIR): + mkdir $(OBJS_DIR) + +libft/libft.a: + $(MAKE) -Clibft all + +clean: + rm -rf $(OBJS_DIR) + $(MAKE) -Clibft clean + +fclean: clean + rm -f $(NAME) + $(MAKE) -Clibft fclean + +re: fclean all diff --git a/includes/minishell.h b/includes/minishell.h new file mode 100644 index 0000000..f2088ff --- /dev/null +++ b/includes/minishell.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* minishell.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */ +/* Updated: 2018/11/22 14:47:05 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_H +# define MINISHELL_H + +# include +# include +# include "libft.h" + +#endif diff --git a/libft b/libft new file mode 160000 index 0000000..99ce0ff --- /dev/null +++ b/libft @@ -0,0 +1 @@ +Subproject commit 99ce0ff95663d888be3bdb3c29563fffe8158caf diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..f51326c --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/18 13:09:55 by tmaze #+# #+# */ +/* Updated: 2018/11/23 11:28:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int main(void) +{ + char *cmd; + char **myenv; + char **tab_cmd; + int ret; + extern char **environ; + + if ((myenv = ft_memalloc(sizeof(environ))) == NULL) + { + ft_putendl("error alloc"); + return (2); + } + ft_memcpy((void*)myenv, (void*)environ, sizeof(environ)); + while (1) + { + ret = 0; + ft_putstr(FT_COLOR_BLUE); + ft_putstr("$> "); + ft_putstr(FT_RESET); + while (ret == 0) + ret = get_next_line(0, &cmd); + if (ft_strcmp("exit", cmd) == 0) + { + ft_memdel((void*)&myenv); + ft_strdel(&cmd); + return (0); + } + else if ((tab_cmd = ft_strsplit(cmd, ' ')) == NULL) + { + ft_memdel((void*)&myenv); + ft_strdel(&cmd); + ft_putendl("error split"); + return (2); + } + if ((ret = fork()) == 0) + { + execve(cmd, tab_cmd, environ); + ft_putstr("Error: "); + ft_putendl(cmd); + exit(0); + } + else + { + waitpid(ret, NULL, 0); + ft_strdel(&cmd); + ft_del_words_tables(&tab_cmd); + } + } + ft_strdel((void*)&myenv); + return (3); +}