Skip to content

Commit 29edfdf

Browse files
author
Mickael TACNET
committed
refonte minishell sans leaks
1 parent 5c694fc commit 29edfdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2899
-0
lines changed

Makefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# **************************************************************************** #
2+
# #
3+
# ::: :::::::: #
4+
# Makefile :+: :+: :+: #
5+
# +:+ +:+ +:+ #
6+
# By: mtacnet <mtacnet@student.42.fr> +#+ +:+ +#+ #
7+
# +#+#+#+#+#+ +#+ #
8+
# Created: 2017/03/01 11:47:50 by mtacnet #+# #+# #
9+
# Updated: 2017/08/16 11:52:20 by nbouchin ### ########.fr #
10+
# #
11+
# **************************************************************************** #
12+
13+
NAME = minishell
14+
15+
CC = gcc
16+
17+
CFLAGS = -Wall -Wextra -Werror -I. -g
18+
19+
RM = rm -rf
20+
21+
LIBFT = -L./libft -lft
22+
23+
SRCS = *.c
24+
25+
OBJS = $(SRCS:.c=.o)
26+
27+
LIBFT_PATH = ./libft
28+
29+
GREEN = "\033[32m"
30+
31+
all: $(NAME)
32+
33+
$(NAME):$(OBJS)
34+
@make -C $(LIBFT_PATH)
35+
@$(CC) $(CFLAGS) $(SRCS) $(LIBFT) -o $(NAME)
36+
@echo $(GREEN)"Compilation done !"
37+
38+
%.o: %.c
39+
@gcc -o $@ -c $< $(CFLAGS)
40+
41+
clean:
42+
@$(RM) $(OBJS)
43+
@make -C $(LIBFT_PATH) clean
44+
@echo "clean OK"
45+
46+
fclean: clean
47+
@$(RM) $(NAME)
48+
@make -C $(LIBFT_PATH) fclean
49+
@echo "fclean OK"
50+
51+
re: fclean all
52+
53+
.PHONY: all clean fclean re

auteur

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mtacnet

cmd.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* cmd.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mtacnet <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2017/08/18 12:10:58 by mtacnet #+# #+# */
9+
/* Updated: 2017/08/18 12:33:11 by mtacnet ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
int check_cmd(int val, t_elem **lst_env, char **tab_arg, t_elem **lst_path)
16+
{
17+
if (val != 0)
18+
{
19+
if (val == 1 || val == 2 || val == 3)
20+
process_env(lst_env, val, tab_arg, lst_path);
21+
if (val == 4)
22+
process_cd(lst_env, tab_arg);
23+
if (val == 5)
24+
process_echo(tab_arg);
25+
if (val == 6)
26+
process_exit(lst_env, lst_path, tab_arg);
27+
return (1);
28+
}
29+
else
30+
return (0);
31+
}
32+
33+
int parsing_cmd(char *command)
34+
{
35+
if (ft_strcmp(command, "env") == 0)
36+
return (1);
37+
else if (ft_strcmp(command, "setenv") == 0)
38+
return (2);
39+
else if (ft_strcmp(command, "unsetenv") == 0)
40+
return (3);
41+
else if (ft_strcmp(command, "cd") == 0)
42+
return (4);
43+
else if (ft_strcmp(command, "echo") == 0)
44+
return (5);
45+
else if (ft_strcmp(command, "exit") == 0)
46+
return (6);
47+
else
48+
return (0);
49+
}

core.c

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* core.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mtacnet <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2017/08/18 11:04:46 by mtacnet #+# #+# */
9+
/* Updated: 2017/08/18 15:47:39 by mtacnet ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
static void list_path(t_elem **lst_path, char **path)
16+
{
17+
int i;
18+
19+
i = 0;
20+
while (path[i] && path[i][0] != '\0')
21+
{
22+
push_elem(lst_path, path[i]);
23+
i++;
24+
}
25+
free_tab(path);
26+
}
27+
28+
void get_elem(t_elem **lst_path, char **env, char *elem)
29+
{
30+
char **tmp_tab;
31+
char *tmp;
32+
size_t i;
33+
size_t j;
34+
size_t name_len;
35+
36+
tmp_tab = NULL;
37+
tmp = NULL;
38+
i = 0;
39+
j = 0;
40+
name_len = 0;
41+
while (env[i] && env[i][0] != '\0')
42+
{
43+
if (ft_strncmp(env[i], elem, 4) == 0)
44+
j = i;
45+
i++;
46+
}
47+
if (j == 0)
48+
return ;
49+
tmp_tab = ft_strsplit(env[j], ':');
50+
name_len = ft_strlen(tmp_tab[0]);
51+
tmp = ft_strsub(tmp_tab[0], 5, name_len);
52+
ft_strdel(&tmp_tab[0]);
53+
tmp_tab[0] = ft_strsub(tmp, 0, name_len);
54+
list_path(lst_path, tmp_tab);
55+
ft_strdel(&tmp);
56+
}
57+
58+
void process_core(char *line, char **tab_arg, char **env)
59+
{
60+
int i;
61+
t_elem *lst_path;
62+
t_elem *lst_env;
63+
64+
i = 0;
65+
get_elem(&lst_path, env, "PATH");
66+
lst_env = new_list();
67+
tab_to_list(&lst_env, env);
68+
modif_line(&line);
69+
tab_arg = ft_strsplit(line, 040);
70+
if (tab_arg[0] != NULL)
71+
{
72+
i = parsing_cmd(tab_arg[0]);
73+
if (check_cmd(i, &lst_env, tab_arg, &lst_path) == 0)
74+
{
75+
go_bin(&lst_path, &lst_env, tab_arg);
76+
free_tab(tab_arg);
77+
return ;
78+
}
79+
free_tab(tab_arg);
80+
freelst(&lst_path);
81+
}
82+
83+
}
84+
85+
void if_path(char *valid_path, char **tab_arg, char **env)
86+
{
87+
pid_t child_pid;
88+
int status;
89+
90+
child_pid = fork();
91+
if (child_pid == -1)
92+
{
93+
ft_putstr_fd("ERROR WITH CHILD PROCESSUS", 2);
94+
exit(EXIT_FAILURE);
95+
}
96+
else if (child_pid == 0)
97+
{
98+
execve(valid_path, tab_arg, env);
99+
exit(0);
100+
}
101+
else
102+
waitpid(child_pid, &status, WUNTRACED);
103+
}
104+
105+
void get_line(char **env)
106+
{
107+
char *line;
108+
char **tab_arg;
109+
int ret;
110+
111+
line = NULL;
112+
tab_arg = NULL;
113+
ft_putstr("$> ");
114+
while (1)
115+
{
116+
while ((ret = get_next_line(1, &line) > 0))
117+
{
118+
if (verif_line(line) == 1)
119+
ft_putstr("$> ");
120+
else
121+
process_core(line, tab_arg, env);
122+
ft_putstr("$> ");
123+
free(line);
124+
}
125+
}
126+
}

error.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* error.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mtacnet <mtacnet@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2017/07/21 13:36:54 by mtacnet #+# #+# */
9+
/* Updated: 2017/08/08 13:50:20 by mtacnet ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
static void cd_error(char *bad_cmd)
16+
{
17+
ft_putstr_fd(bad_cmd, 2);
18+
ft_putstr_fd(": OLDPWD not set\n", 2);
19+
}
20+
21+
static void error_cmd(char *bad_cmd)
22+
{
23+
if (ft_strncmp(bad_cmd, "-i", 2) == 0)
24+
return ;
25+
ft_putstr_fd("minishell: command not found: ", 2);
26+
ft_putstr_fd(bad_cmd, 2);
27+
ft_putstr_fd("\n", 2);
28+
}
29+
30+
static void view_error(char *bad_cmd, char *bad_flag)
31+
{
32+
ft_putstr_fd(bad_cmd, 2);
33+
ft_putstr_fd(": ", 2);
34+
ft_putstr_fd("no such file or directory: ", 2);
35+
ft_putstr_fd(bad_flag, 2);
36+
ft_putstr_fd("\n", 2);
37+
}
38+
39+
void error(int value, char *bad_cmd, char *bad_flag)
40+
{
41+
if (value == 1)
42+
view_error(bad_cmd, bad_flag);
43+
if (value == 2)
44+
error_cmd(bad_cmd);
45+
if (value == 3)
46+
cd_error(bad_cmd);
47+
}

init_list.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* init_list.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mtacnet <marvin@42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2017/08/18 11:12:48 by mtacnet #+# #+# */
9+
/* Updated: 2017/08/18 12:19:48 by mtacnet ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
t_elem *new_list(void)
16+
{
17+
return (NULL);
18+
}
19+
20+
int is_empty(t_elem **lst)
21+
{
22+
if ((*lst) == NULL)
23+
return (1);
24+
else
25+
return (0);
26+
}
27+
28+
t_elem *init_element(t_elem *element)
29+
{
30+
if (!(element = (t_elem*)malloc(sizeof(t_elem))))
31+
{
32+
ft_putendl("MEMORY ALLOCATION ERROR");
33+
exit(EXIT_FAILURE);
34+
}
35+
element->content = NULL;
36+
element->next = NULL;
37+
return (element);
38+
}
39+
40+
void freelst(t_elem **lst)
41+
{
42+
t_elem *list;
43+
t_elem *tmp;
44+
45+
list = (*lst);
46+
while (list)
47+
{
48+
tmp = list;
49+
ft_strdel(&(list->content));
50+
free(list);
51+
list = tmp->next;
52+
}
53+
(*lst) = NULL;
54+
}

libft/Makefile

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
NAME = libft.a
2+
3+
CC = gcc
4+
5+
RM = rm -rf
6+
7+
CFLAGS = -Wall -Wextra -Werror -c
8+
9+
SRCS = ft_atoi.c ft_bzero.c ft_countword.c ft_intlen.c ft_isalnum.c \
10+
ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c \
11+
ft_lstadd.c ft_lstdel.c ft_lstdelone.c ft_lstiter.c ft_lstmap.c \
12+
ft_lstnew.c ft_lstrepel.c ft_memalloc.c ft_memccpy.c ft_memchr.c \
13+
ft_memcmp.c ft_memcpy.c ft_memdel.c ft_memmove.c ft_memset.c \
14+
ft_putchar.c ft_putchar_fd.c ft_putendl.c ft_putendl_fd.c ft_putnbr.c \
15+
ft_putnbr_fd.c ft_putstr.c ft_putstr_fd.c ft_strcat.c ft_strchr.c \
16+
ft_strclr.c ft_strcmp.c ft_strcpy.c ft_strdel.c ft_strdup.c ft_strequ.c \
17+
ft_striter.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlen.c \
18+
ft_strmap.c ft_strmapi.c ft_strncat.c ft_strncmp.c ft_strncpy.c \
19+
ft_strnequ.c ft_strnew.c ft_strnstr.c ft_strrchr.c ft_strrev.c \
20+
ft_strsplit.c ft_strstr.c ft_strsub.c ft_tolower.c ft_toupper.c \
21+
ft_swap.c get_next_line.c ft_strlenint.c
22+
OBJS = $(SRCS:.c=.o)
23+
24+
GREEN = "\033[32m"
25+
26+
all: $(NAME)
27+
28+
$(NAME): $(OBJS)
29+
@ar rc $(NAME) $(OBJS)
30+
@ranlib $(NAME)
31+
32+
%.o: %.c
33+
@$(CC) -o $@ -c $< $(CFLAGS)
34+
@echo $(GREEN)"Libft: "$@
35+
36+
clean:
37+
@$(RM) $(OBJS)
38+
39+
fclean: clean
40+
@$(RM) $(NAME)
41+
42+
re: fclean all
43+
44+
.PHONY: all clean fclean re

0 commit comments

Comments
 (0)