added first builtin

corrected binary execution + added echo
This commit is contained in:
Tanguy MAZE 2018-11-27 18:38:24 +01:00
parent cf788bc62c
commit 4ce47a9f0b
7 changed files with 149 additions and 19 deletions

View File

@ -6,7 +6,7 @@
# 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 #
# Updated: 2018/11/27 17:36:14 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -16,9 +16,9 @@ CCSTD :=
NAME := minishell
SRCS := main.c
SRCS := main.c exec.c cmd_echo.c
OBJS_DIR := objs
OBJS := $(SRCS:.c=.o)
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:.c=.o))
INCLS := -Iincludes -Ilibft
LIBS := -Llibft -lft
@ -26,8 +26,8 @@ LIBS := -Llibft -lft
all: $(NAME)
$(NAME): $(OBJS_DIR)/$(OBJS) libft/libft.a
$(CC) $(CCFLAGS) $(CCSTD) $(INCLS) $< -o $(NAME) $(LIBS)
$(NAME): $(OBJS) libft/libft.a
$(CC) $(CCFLAGS) $(CCSTD) $(INCLS) $(OBJS) -o $(NAME) $(LIBS)
$(OBJS_DIR)/%.o: srcs/%.c includes/minishell.h $(OBJS_DIR)
$(CC) $(CCFLAGS) $(CCSTD) $(INCLS) -c $< -o $@

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2018/11/27 16:23:49 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,4 +17,13 @@
# include <sys/stat.h>
# include "libft.h"
typedef struct s_builtin
{
char *cmd;
int (*f)(char **argv, char **envp);
} t_builtin;
int exec_cmd(char **argv, char **env);
int cmd_echo(char **argv, char **env);
#endif

28
srcs/cmd_echo.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/27 16:14:07 by tmaze #+# #+# */
/* Updated: 2018/11/27 17:29:35 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_echo(char **argv, char **env)
{
size_t i;
(void)env;
i = 0;
while (argv[0] && argv[++i])
{
ft_putstr(argv[i]);
ft_putchar(' ');
}
ft_putchar('\n');
return (0);
}

35
srcs/exec.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/27 15:32:29 by tmaze #+# #+# */
/* Updated: 2018/11/27 16:36:48 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define S_BIN 1
int exec_cmd(char **argv, char **env)
{
size_t i;
static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo}};
/* ,{"cd", &cmd_cd}, */
/* {"setenv", &cmd_senv},{"unsetenv", &cmd_senv}, {"env", &cmd_env}}; */
i = 0;
while (i < S_BIN)
{
if (ft_strcmp(argv[0], builtins[i].cmd) == 0)
return ((*(builtins[i].f))(argv, env));
i++;
}
execve(argv[0], argv, env);
ft_putendl("minishell: error");
return (1);
}

View File

@ -6,12 +6,35 @@
/* 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 */
/* Updated: 2018/11/27 17:52:17 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char **ft_strtabcpy(char **tab)
{
char **ret;
size_t size_tab;
size_tab = 0;
while (tab[size_tab])
size_tab++;
if ((ret = (char**)ft_memalloc(sizeof(char*) * size_tab)) == NULL)
return (NULL);
size_tab = 0;
while (tab[size_tab])
{
if ((ret[size_tab] = ft_strdup(tab[size_tab])) == NULL)
{
ft_del_words_tables(&ret);
break ;
}
size_tab++;
}
return (ret);
}
int main(void)
{
char *cmd;
@ -20,20 +43,24 @@ int main(void)
int ret;
extern char **environ;
if ((myenv = ft_memalloc(sizeof(environ))) == NULL)
if ((myenv = ft_strtabcpy(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_COLOR_GREEN);
ft_putstr("$> ");
ft_putstr(FT_RESET);
while (ret == 0)
ret = get_next_line(0, &cmd);
ret = get_next_line(1, &cmd);
if (ret == -1)
{
ft_memdel((void*)&myenv);
ft_strdel(&cmd);
return (2);
}
if (ft_strcmp("exit", cmd) == 0)
{
ft_memdel((void*)&myenv);
@ -48,12 +75,7 @@ int main(void)
return (2);
}
if ((ret = fork()) == 0)
{
execve(cmd, tab_cmd, environ);
ft_putstr("Error: ");
ft_putendl(cmd);
exit(0);
}
exit(exec_cmd(tab_cmd, myenv));
else
{
waitpid(ret, NULL, 0);
@ -62,5 +84,5 @@ int main(void)
}
}
ft_strdel((void*)&myenv);
return (3);
return (0);
}

BIN
tests/a.out Executable file

Binary file not shown.

36
tests/main.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/23 14:24:53 by tmaze #+# #+# */
/* Updated: 2018/11/26 12:50:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
#include "../libft/libft.h"
static void ft_cntdelstr(void* content, size_t content_size)
{
ft_strdel((char**)content);
content_size = 0;
}
int main(int argc, char **argv)
{
extern char **environ;
t_list *lst;
int i;
i = -1;
while (environ[++i])
printf("%s\n", environ[i]);
if (argc > 1)
if ((lst = ft_strsplitstr2()))
return (0);
}