WIP added libft & creating useable shell

This commit is contained in:
Tanguy MAZE 2018-11-23 16:41:43 +01:00
parent 85bc8d2847
commit cf788bc62c
5 changed files with 139 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libft"]
path = libft
url = https://github.com/tvdu29/libft

49
Makefile Normal file
View File

@ -0,0 +1,49 @@
#******************************************************************************#
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

20
includes/minishell.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
# include <sys/stat.h>
# include "libft.h"
#endif

1
libft Submodule

@ -0,0 +1 @@
Subproject commit 99ce0ff95663d888be3bdb3c29563fffe8158caf

66
srcs/main.c Normal file
View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}