forked from nicolas-arnaud/Minishell
narnaud
3 years ago
72 changed files with 2852 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||
NAME = minishell |
|||
LIBFT = 42-libft/libft.a |
|||
SRCS = minishell.c lexer.c parser.c caller.c built-in.c utils.c |
|||
OBJS = ${SRCS:.c=.o} |
|||
|
|||
READLINE_INC = ~/.brew/opt/readline/include |
|||
LIB = -L ~/.brew/lib -lreadline -L ~/.brew/lib -lhistory -L. -lft |
|||
|
|||
$(NAME): $(OBJS) |
|||
gcc ${OBJS} ${LIB} -o ${NAME} |
|||
|
|||
$(LIBFT): |
|||
${MAKE} -C ./42-libft |
|||
cp ./42-libft/libft.a . |
|||
|
|||
all: $(LIBFT) $(NAME) |
|||
|
|||
%.o: %.c |
|||
gcc -Werror -Wextra -Wall -g -c $< -I ${READLINE_INC} #for rl_replace_line |
|||
|
|||
clean: |
|||
rm -rf ${OBJS} |
|||
|
|||
fclean: clean |
|||
rm -rf ${NAME} |
|||
|
|||
re: fclean all |
|||
|
|||
.PHONY: all clean fclean re |
@ -0,0 +1,140 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* lexer.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ |
|||
/* Updated: 2022/05/02 17:05:17 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
int create_token(t_lexer *lex, char str[]) |
|||
{ |
|||
t_token *tok; |
|||
t_type type; |
|||
|
|||
type = lex->type; |
|||
lex->type = (lex->next_type || WORD); |
|||
lex->next_type = WORD; |
|||
if (!str[0]) |
|||
return (0); |
|||
tok = lex->tokens; |
|||
if (!lex->tokens) |
|||
tok = ft_calloc(1, sizeof *tok); |
|||
else |
|||
{ |
|||
while (tok->next) |
|||
tok = tok->next; |
|||
tok->next = ft_calloc(1, sizeof *tok); |
|||
tok = tok->next; |
|||
} |
|||
tok->type = type; |
|||
tok->value = strdup(str); |
|||
if (!lex->tokens) |
|||
lex->tokens = tok; |
|||
return (1); |
|||
} |
|||
|
|||
|
|||
int check_register(t_lexer *lex, char **line, char *tmp) |
|||
{ |
|||
int spaces; |
|||
|
|||
if (lex->state != ROOT_ST) |
|||
return (0); |
|||
spaces = 0; |
|||
if (is_space(**line)) |
|||
{ |
|||
while (is_space(**line)) |
|||
(*line)++; |
|||
spaces = 1; |
|||
} |
|||
if (**line == '>' && (*line)++) |
|||
{ |
|||
if (**line == '>' && (*line)++) |
|||
lex->next_type = ADD; |
|||
else |
|||
lex->next_type = OUT; |
|||
return (create_token(*lex, tmp)); |
|||
} |
|||
else if (**line == '<' && (*line)++) |
|||
{ |
|||
if (**line == '<' && (*line)++) |
|||
lex->next_type = HD; |
|||
else |
|||
lex->next_type = IN; |
|||
return (create_token(*lex, tmp)); |
|||
} |
|||
else if (**line == '|') |
|||
{ |
|||
|
|||
(*line)++; |
|||
lex->next_type = PIPE; |
|||
create_token(*lex, tmp); |
|||
return (create_token(*lex, "|")); |
|||
} |
|||
if (!spaces) |
|||
return (0); |
|||
return (create_token(l-*ex, tmp)); |
|||
} |
|||
|
|||
int replace_var(char **line, char *tmp, int tmp_i) |
|||
{ |
|||
char **env; |
|||
int i; |
|||
char *var_name; |
|||
|
|||
// TODO : free split and var_name
|
|||
i = 1; |
|||
env = NULL; |
|||
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') |
|||
i++; |
|||
var_name = ft_substr(*line, 1, i - 1); |
|||
i = 0; |
|||
while (g_env[i]) |
|||
{ |
|||
env = ft_split(g_env[i], '='); |
|||
if (!ft_strncmp(var_name, env[0], ft_strlen(var_name) + 1)) |
|||
break ; |
|||
i++; |
|||
} |
|||
i = 0; |
|||
while (env[1][i]) |
|||
tmp[tmp_i++] = env[1][i++]; |
|||
tmp[tmp_i] = 0; |
|||
*line += i; |
|||
return (tmp_i); |
|||
} |
|||
|
|||
|
|||
int check_state(t_lexer *lex, char **line) |
|||
{ |
|||
t_state new; |
|||
|
|||
new = OLD_ST; |
|||
if (**line == '"') |
|||
{ |
|||
if (lex->state == D_QUOTE_ST) |
|||
new = ROOT_ST; |
|||
else if (lex->state == ROOT_ST) |
|||
new = D_QUOTE_ST; |
|||
} |
|||
else if (**line == '\'') |
|||
{ |
|||
if (lex->state == S_QUOTE_ST) |
|||
new = ROOT_ST; |
|||
else if (lex->state == ROOT_ST) |
|||
new = S_QUOTE_ST; |
|||
} |
|||
if (new) |
|||
{ |
|||
(*line)++; |
|||
lex->state = new; |
|||
return (1); |
|||
} |
|||
return (0); |
|||
} |
@ -0,0 +1,57 @@ |
|||
NAME = libft.a |
|||
|
|||
SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \
|
|||
is/ft_isalnum.c |
|||
|
|||
SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \
|
|||
str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \
|
|||
str/ft_sub.c str/ft_join.c str/ft_trim.c str/ft_split.c \
|
|||
str/ft_rev.c str/ft_mapi.c str/ft_iteri.c |
|||
|
|||
SRCS += conv/ft_toupper.c conv/ft_tolower.c conv/ft_atoi.c conv/ft_itoa.c \
|
|||
conv/ft_itox.c conv/ft_utoa.c |
|||
|
|||
SRCS += mem/ft_bzero.c mem/ft_memset.c mem/ft_memchr.c mem/ft_memcpy.c \
|
|||
mem/ft_memcmp.c mem/ft_memmove.c mem/ft_calloc.c mem/ft_free_split.c |
|||
|
|||
SRCS += put/ft_putchar_fd.c put/ft_putnbr_fd.c put/ft_putendl_fd.c \
|
|||
put/ft_putstr_fd.c |
|||
|
|||
SRCS += nbr/ft_nbrlen.c nbr/ft_croissant.c nbr/ft_decroissant.c |
|||
|
|||
SRCS += slist/ft_size.c slist/ft_new.c slist/ft_last.c \
|
|||
slist/ft_add_back.c slist/ft_add_front.c slist/ft_delone.c \
|
|||
slist/ft_clear.c slist/ft_iter.c slist/ft_map.c |
|||
|
|||
SRCS += i_slist/ft_free.c i_slist/ft_first.c i_slist/ft_is_in.c |
|||
|
|||
SRCS += dlist/ft_add.c dlist/ft_n.c dlist/ft_to_arr.c |
|||
|
|||
OBJS = $(SRCS:.c=.o) |
|||
|
|||
CC = gcc |
|||
AR = ar -rcs |
|||
RM = rm -rf |
|||
|
|||
CFLAGS = -Wall -Wextra -Werror |
|||
|
|||
.c.o: |
|||
${CC} ${CFLAGS} -c $< -o ${<:.c=.o} |
|||
|
|||
all : $(NAME) |
|||
|
|||
$(NAME): $(OBJS) |
|||
${AR} ${NAME} ${OBJS} |
|||
|
|||
clean: |
|||
${RM} ${OBJS} |
|||
${RM} ${OBJS_BONUS} |
|||
|
|||
fclean: clean |
|||
${RM} ${NAME} |
|||
|
|||
re: fclean all |
|||
|
|||
rebonus: fclean bonus |
|||
|
|||
.PHONY: all bonus re rebonus clean fclean |
@ -0,0 +1,40 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_atoi.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 14:14:45 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_atoi(const char *str) |
|||
{ |
|||
int sign; |
|||
double nb; |
|||
|
|||
nb = 0; |
|||
sign = 1; |
|||
if (!ft_strncmp(str, "-2147483648", 11)) |
|||
return (-2147483648); |
|||
while (*str == ' ' || (*str >= '\t' && *str <= '\r')) |
|||
str++; |
|||
if (*str == '-' && ++str) |
|||
sign = -1; |
|||
else if (*str == '+') |
|||
str++; |
|||
while (*str >= '0' && *str <= '9') |
|||
{ |
|||
nb = nb * 10 + (*str - '0') % 10; |
|||
if (nb > 2147483647 && sign > 0) |
|||
return (-1); |
|||
else if (nb > 2147486648 && sign < 0) |
|||
return (0); |
|||
str++; |
|||
} |
|||
return ((int)(sign * nb)); |
|||
} |
@ -0,0 +1,42 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_itoa.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 15:50:47 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_itoa(int n) |
|||
{ |
|||
char *ret; |
|||
unsigned int i; |
|||
unsigned int neg; |
|||
|
|||
if (n == -2147483648) |
|||
return (ft_strdup("-2147483648")); |
|||
if (n == 0) |
|||
return (ft_strdup("0")); |
|||
i = 0; |
|||
neg = 0; |
|||
ret = ft_calloc(ft_ilen(n) + 1, sizeof(char)); |
|||
if (!ret) |
|||
return (NULL); |
|||
if (n < 0) |
|||
{ |
|||
n = -n; |
|||
neg = 1; |
|||
} |
|||
while (n) |
|||
{ |
|||
ret[i++] = (n % 10) + '0'; |
|||
n = n / 10; |
|||
} |
|||
ft_strrev(&ret, neg); |
|||
return (ret); |
|||
} |
@ -0,0 +1,35 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_itox.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/12/21 09:51:26 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_itox(unsigned long int n, const char *base) |
|||
{ |
|||
char *ret; |
|||
size_t i; |
|||
unsigned int neg; |
|||
size_t size; |
|||
|
|||
size = ft_longbaselen(n, 16); |
|||
if (n == 0) |
|||
return (ft_strdup("0")); |
|||
i = 0; |
|||
neg = 0; |
|||
ret = ft_calloc(size + 1, sizeof(char)); |
|||
while (n) |
|||
{ |
|||
ret[i++] = base[n % 16]; |
|||
n = n / 16; |
|||
} |
|||
ft_strrev(&ret, neg); |
|||
return (ret); |
|||
} |
@ -0,0 +1,19 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_tolower.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 17:02:25 by narnaud #+# #+# */ |
|||
/* Updated: 2021/10/19 12:35:58 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
int ft_tolower(int c) |
|||
{ |
|||
if (c > 64 && c < 91) |
|||
return (c + 32); |
|||
else |
|||
return (c); |
|||
} |
@ -0,0 +1,19 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_toupper.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 17:02:28 by narnaud #+# #+# */ |
|||
/* Updated: 2021/10/19 12:36:28 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
int ft_toupper(int c) |
|||
{ |
|||
if (c > 96 && c < 123) |
|||
return (c - 32); |
|||
else |
|||
return (c); |
|||
} |
@ -0,0 +1,33 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_utoa.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/12/21 09:48:38 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_utoa(unsigned int n) |
|||
{ |
|||
char *ret; |
|||
unsigned int i; |
|||
|
|||
if (n == 4294967295) |
|||
return (ft_strdup("4294967295")); |
|||
if (n == 0) |
|||
return (ft_strdup("0")); |
|||
i = 0; |
|||
ret = ft_calloc(ft_ulen(n) + 1, sizeof(char)); |
|||
while (n) |
|||
{ |
|||
ret[i++] = (n % 10) + '0'; |
|||
n = n / 10; |
|||
} |
|||
ft_strrev(&ret, 0); |
|||
return (ret); |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_add.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:46:25 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 14:15:13 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
t_dlist *ft_dlst_add(t_dlist *prev, void *content) |
|||
{ |
|||
t_dlist *new; |
|||
|
|||
if (!content) |
|||
return (prev); |
|||
new = ft_calloc(1, sizeof(t_dlist)); |
|||
if (prev) |
|||
prev->next = new; |
|||
new->previous = prev; |
|||
new->content = content; |
|||
return (new); |
|||
} |
@ -0,0 +1,20 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_dlst_n.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:51:20 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:27:40 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
t_dlist *ft_dlst_n(t_dlist *lst, size_t n) |
|||
{ |
|||
while (n-- && lst) |
|||
lst = lst->next; |
|||
return (lst); |
|||
} |
@ -0,0 +1,41 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_dlst_to_arr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:50:10 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:33:57 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char **ft_dlst_to_arr(t_dlist *ptr) |
|||
{ |
|||
char **ret; |
|||
t_dlist *last; |
|||
t_dlist *tmp; |
|||
size_t count; |
|||
|
|||
last = ptr; |
|||
count = 1; |
|||
if (!ptr) |
|||
return (ft_calloc(1, sizeof(char *))); |
|||
while (ptr->previous) |
|||
{ |
|||
ptr = ptr->previous; |
|||
count++; |
|||
} |
|||
ret = ft_calloc(count + 1, sizeof(char *)); |
|||
ret[count] = NULL; |
|||
while (count--) |
|||
{ |
|||
ret[count] = last->content; |
|||
tmp = last; |
|||
free(last); |
|||
last = tmp->previous; |
|||
} |
|||
return (ret); |
|||
} |
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_first.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 09:06:44 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 13:26:04 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b)) |
|||
{ |
|||
int previous; |
|||
size_t i; |
|||
|
|||
i = 1; |
|||
if (!lst) |
|||
return (0); |
|||
while (lst) |
|||
{ |
|||
previous = lst->nb; |
|||
lst = lst->next; |
|||
if (lst && fct(previous, lst->nb)) |
|||
return (i); |
|||
i++; |
|||
} |
|||
return (-1); |
|||
} |
@ -0,0 +1,25 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_free.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 09:27:41 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 14:18:37 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_ilst_free(t_i_slist *list) |
|||
{ |
|||
t_i_slist *next; |
|||
|
|||
while (list) |
|||
{ |
|||
next = list->next; |
|||
free(list); |
|||
list = next; |
|||
} |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_is_in.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 13:22:49 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 14:19:02 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_ilst_is_in(int value, t_i_slist lst) |
|||
{ |
|||
if (lst.nb == value && lst.next) |
|||
return (1); |
|||
while (lst.next) |
|||
{ |
|||
if (lst.nb == value) |
|||
return (1); |
|||
else |
|||
lst = *(lst.next); |
|||
} |
|||
return (0); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isalnum.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:13:39 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isalnum(int ch) |
|||
{ |
|||
if (ft_isalpha(ch) || ft_isdigit(ch)) |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isalpha.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 15:38:41 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isalpha(int ch) |
|||
{ |
|||
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isascii.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:14:38 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isascii(int ch) |
|||
{ |
|||
if (ch >= 0 && ch < 128) |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isdigit.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:13:16 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isdigit(int ch) |
|||
{ |
|||
if (ch >= '0' && ch <= '9') |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
@ -0,0 +1,20 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_islower.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:04:18 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 10:07:46 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_islower(char c) |
|||
{ |
|||
if (c >= 'a' && c <= 'z') |
|||
return (1); |
|||
return (0); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isprint.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:14:02 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isprint(int ch) |
|||
{ |
|||
if (ch > 31 && ch < 127) |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isspace.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:05:51 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 10:06:15 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isspace(char c) |
|||
{ |
|||
if (c > 8 && c < 14) |
|||
return (1); |
|||
if (c == ' ') |
|||
return (1); |
|||
return (0); |
|||
} |
@ -0,0 +1,20 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_isupper.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/24 10:06:18 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 10:07:15 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_isupper(char c) |
|||
{ |
|||
if (c >= 'A' && c <= 'Z') |
|||
return (1); |
|||
return (0); |
|||
} |
@ -0,0 +1,103 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* libft.h :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 13:31:43 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#ifndef LIBFT_H |
|||
# define LIBFT_H |
|||
|
|||
# include <stddef.h> |
|||
# include <stdlib.h> |
|||
# include <unistd.h> |
|||
|
|||
typedef struct s_slist |
|||
{ |
|||
void *content; |
|||
struct s_slist *next; |
|||
} t_slist; |
|||
|
|||
typedef struct s_i_slist |
|||
{ |
|||
int nb; |
|||
struct s_i_slist *next; |
|||
} t_i_slist; |
|||
|
|||
typedef struct s_dlist |
|||
{ |
|||
void *content; |
|||
struct s_dlist *next; |
|||
struct s_dlist *previous; |
|||
} t_dlist; |
|||
|
|||
int ft_atoi(const char *str); |
|||
void ft_bzero(void *s, size_t n); |
|||
void *ft_calloc( size_t num, size_t size ); |
|||
int ft_isalpha(int ch); |
|||
int ft_isascii(int ch); |
|||
int ft_isdigit(int ch); |
|||
int ft_isprint(int ch); |
|||
int ft_isalnum(int ch); |
|||
void *ft_memchr(const void *b, int c, size_t n); |
|||
int ft_memcmp(const void *s1, const void *s2, size_t n); |
|||
void *ft_memcpy(void *dst, const void *src, size_t n); |
|||
void *ft_memmove(void *dst, const void *src, size_t n); |
|||
void ft_free_split(char **split); |
|||
|
|||
size_t ft_ilen(int nbr); |
|||
size_t ft_ulen(unsigned int nbr); |
|||
size_t ft_longbaselen(long nbr, size_t base); |
|||
int ft_croissant(int a, int b); |
|||
int ft_decroissant(int a, int b); |
|||
|
|||
void *ft_memset(void *b, int c, size_t len); |
|||
char *ft_strchr(const char *s, int c); |
|||
char *ft_strdup(const char *src); |
|||
size_t ft_strlcat(char *dst, const char *src, size_t dstsize); |
|||
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); |
|||
size_t ft_strlen(const char *s); |
|||
size_t ft_strlen_to(const char *str, char ch); |
|||
int ft_strncmp(const char *s1, const char *s2, size_t n); |
|||
char *ft_strnstr(const char *haystack, const char *needle, size_t len); |
|||
char *ft_strrchr(const char *s, int c); |
|||
void ft_strrev(char **str, unsigned int neg); |
|||
int ft_tolower(int c); |
|||
int ft_toupper(int c); |
|||
char *ft_substr(char const *s, unsigned int start, size_t len); |
|||
char *ft_strjoin(const char *s1, const char *s2); |
|||
char *ft_strtrim(char const *s1, char const *set); |
|||
char **ft_split(char const *str, char ch); |
|||
char *ft_itoa(int n); |
|||
char *ft_itox(unsigned long n, const char *base); |
|||
char *ft_utoa(unsigned int n); |
|||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); |
|||
void ft_striteri(char *s, void (*f)(unsigned int, char*)); |
|||
void ft_putchar_fd(char c, int fd); |
|||
void ft_putstr_fd(char *s, int fd); |
|||
void ft_putendl_fd(char *s, int fd); |
|||
void ft_putnbr_fd(int n, int fd); |
|||
|
|||
t_slist *ft_slst_new(void *content); |
|||
void ft_slst_add_front(t_slist **alst, t_slist *new); |
|||
int ft_slst_size(t_slist *lst); |
|||
t_slist *ft_slst_last(t_slist *lst); |
|||
void ft_slst_add_back(t_slist **alst, t_slist *new); |
|||
void ft_slst_delone(t_slist *lst, void (*del)(void *)); |
|||
void ft_slst_clear(t_slist **lst, void (*del)(void *)); |
|||
void ft_slst_iter(t_slist *lst, void (*f)(void *)); |
|||
t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *)); |
|||
|
|||
size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b)); |
|||
void ft_ilst_free(t_i_slist *list); |
|||
int ft_ilst_is_in(int value, t_i_slist lst); |
|||
|
|||
t_dlist *ft_dlst_add(t_dlist *prev, void *content); |
|||
t_dlist *ft_dlst_n(t_dlist *lst, size_t n); |
|||
char **ft_dlst_to_arr(t_dlist *ptr); |
|||
#endif |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_bzero.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/19 11:46:58 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_bzero(void *s, size_t n) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (i < n) |
|||
*(char *)(s + i++) = 0; |
|||
} |
@ -0,0 +1,24 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_calloc.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/19 14:11:11 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void *ft_calloc(size_t count, size_t size) |
|||
{ |
|||
void *ret; |
|||
|
|||
ret = malloc(count * size); |
|||
if (!ret) |
|||
return (NULL); |
|||
ft_bzero(ret, count * size); |
|||
return (ret); |
|||
} |
@ -0,0 +1,26 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_free_split.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 08:55:20 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 08:57:26 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_free_split(char **split) |
|||
{ |
|||
int i; |
|||
|
|||
i = 0; |
|||
while (split[i]) |
|||
{ |
|||
free(split[i]); |
|||
i++; |
|||
} |
|||
free(split); |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_memchr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:15:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void *ft_memchr(const void *b, int c, size_t n) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (i < n) |
|||
{ |
|||
if (*(unsigned char *)(b + i) == (unsigned char)c) |
|||
return ((void *)(b + i)); |
|||
i++; |
|||
} |
|||
return (NULL); |
|||
} |
@ -0,0 +1,29 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_memcmp.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:16:23 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_memcmp(const void *s1, const void *s2, size_t n) |
|||
{ |
|||
size_t i; |
|||
int diff; |
|||
|
|||
i = 0; |
|||
while (i < n) |
|||
{ |
|||
diff = *(unsigned char *)(s1 + i) - *(unsigned char *)(s2 + i); |
|||
if (diff) |
|||
return (diff); |
|||
i++; |
|||
} |
|||
return (0); |
|||
} |
@ -0,0 +1,28 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_memcpy.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:16:47 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void *ft_memcpy(void *dst, const void *src, size_t n) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
if (!src && !dst) |
|||
return (NULL); |
|||
while (i < n) |
|||
{ |
|||
*(char *)(dst + i) = *(char *)(src + i); |
|||
i++; |
|||
} |
|||
return (dst); |
|||
} |
@ -0,0 +1,33 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_memmove.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:17:21 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void *ft_memmove(void *dst, const void *src, size_t n) |
|||
{ |
|||
size_t i; |
|||
unsigned char *ptr_dst; |
|||
unsigned char *ptr_src; |
|||
|
|||
ptr_dst = (unsigned char *)dst; |
|||
ptr_src = (unsigned char *)src; |
|||
i = 0; |
|||
if (!dst && !src) |
|||
return (NULL); |
|||
if (ptr_src < ptr_dst) |
|||
while (++i <= n) |
|||
ptr_dst[n - i] = ptr_src[n - i]; |
|||
else |
|||
while (n--) |
|||
*(ptr_dst++) = *(ptr_src++); |
|||
return (dst); |
|||
} |
@ -0,0 +1,23 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_memset.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/19 11:43:44 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void *ft_memset(void *b, int c, size_t len) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (i < len) |
|||
*(unsigned char *)(b + i++) = (unsigned char)c; |
|||
return ((void *)b); |
|||
} |
@ -0,0 +1,18 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_croissant.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 09:14:45 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 09:15:32 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_croissant(int a, int b) |
|||
{ |
|||
return (a <= b); |
|||
} |
@ -0,0 +1,18 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_decroissant.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/03/25 09:14:45 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 10:53:14 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_decroissant(int a, int b) |
|||
{ |
|||
return (a > b); |
|||
} |
@ -0,0 +1,50 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_nbrlen.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/12/21 09:42:43 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
size_t ft_ilen(int nbr) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
if (nbr == 0) |
|||
return (1); |
|||
else if (nbr < 0) |
|||
i++; |
|||
while (nbr && ++i) |
|||
nbr = nbr / 10; |
|||
return (i); |
|||
} |
|||
|
|||
size_t ft_ulen(unsigned int nbr) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (nbr && ++i) |
|||
nbr = nbr / 10; |
|||
return (i); |
|||
} |
|||
|
|||
size_t ft_longbaselen(long nbr, size_t base) |
|||
{ |
|||
size_t i; |
|||
|
|||
if (nbr <= 0) |
|||
i = 1; |
|||
else |
|||
i = 0; |
|||
while (nbr && ++i) |
|||
nbr = nbr / base; |
|||
return (i); |
|||
} |
@ -0,0 +1,18 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_putchar_fd.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 16:32:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_putchar_fd(char c, int fd) |
|||
{ |
|||
write(fd, &c, 1); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_putendl_fd.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:11:11 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_putendl_fd(char *s, int fd) |
|||
{ |
|||
if (!s) |
|||
return ; |
|||
ft_putstr_fd(s, fd); |
|||
ft_putchar_fd('\n', fd); |
|||
} |
@ -0,0 +1,61 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_putnbr_fd.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:12:01 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
static size_t get_nbrlength(int nbr) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
if (nbr == 0) |
|||
return (1); |
|||
while (nbr && ++i) |
|||
nbr = nbr / 10; |
|||
return (i); |
|||
} |
|||
|
|||
static int pow_10(size_t n) |
|||
{ |
|||
int ret; |
|||
|
|||
if (!n) |
|||
return (1); |
|||
ret = 10; |
|||
while (--n) |
|||
ret *= 10; |
|||
return (ret); |
|||
} |
|||
|
|||
void ft_putnbr_fd(int n, int fd) |
|||
{ |
|||
int fact; |
|||
|
|||
if (n == -2147483648) |
|||
{ |
|||
ft_putstr_fd("-2147483648", fd); |
|||
return ; |
|||
} |
|||
if (n < 0) |
|||
{ |
|||
ft_putchar_fd('-', fd); |
|||
n = -n; |
|||
} |
|||
fact = pow_10(get_nbrlength(n) - 1); |
|||
while (fact >= 10) |
|||
{ |
|||
ft_putchar_fd(n / fact + '0', fd); |
|||
n = n % fact; |
|||
fact /= 10; |
|||
} |
|||
ft_putchar_fd(n + '0', fd); |
|||
} |
@ -0,0 +1,21 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_putstr_fd.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 15:10:39 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_putstr_fd(char *s, int fd) |
|||
{ |
|||
if (!s) |
|||
return ; |
|||
while (*s) |
|||
ft_putchar_fd(*(s++), fd); |
|||
} |
@ -0,0 +1,28 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstadd_back.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:51:11 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_slst_add_back(t_slist **alst, t_slist *new) |
|||
{ |
|||
t_slist *i_cell; |
|||
|
|||
if (!alst || !new) |
|||
return ; |
|||
if (*alst) |
|||
{ |
|||
i_cell = ft_slst_last(*alst); |
|||
i_cell->next = new; |
|||
} |
|||
else |
|||
*alst = new; |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstadd_front.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:48:11 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_slst_add_front(t_slist **alst, t_slist *new) |
|||
{ |
|||
if (!alst || !new) |
|||
return ; |
|||
if (*alst) |
|||
new->next = *alst; |
|||
*alst = new; |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstclear.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:53:04 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_slst_clear(t_slist **lst, void (*del)(void *)) |
|||
{ |
|||
t_slist *next_cell; |
|||
|
|||
if (!del) |
|||
return ; |
|||
while (lst && *lst) |
|||
{ |
|||
next_cell = (*lst)->next; |
|||
ft_slst_delone(*lst, del); |
|||
*lst = next_cell; |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstdelone.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:51:53 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_slst_delone(t_slist *lst, void (*del)(void *)) |
|||
{ |
|||
if (!lst || !del) |
|||
return ; |
|||
(*del)(lst->content); |
|||
free(lst); |
|||
lst = NULL; |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstiter.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:54:03 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_slst_iter(t_slist *lst, void (*f)(void *)) |
|||
{ |
|||
if (!lst || !f) |
|||
return ; |
|||
(*f)(lst->content); |
|||
if (lst->next) |
|||
ft_slst_iter(lst->next, f); |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstlast.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:50:21 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
t_slist *ft_slst_last(t_slist *lst) |
|||
{ |
|||
if (!lst) |
|||
return (NULL); |
|||
while (lst->next) |
|||
lst = lst->next; |
|||
return (lst); |
|||
} |
@ -0,0 +1,29 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstmap.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:54:31 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *)) |
|||
{ |
|||
t_slist *new_lst; |
|||
|
|||
if (!lst || !f) |
|||
return (NULL); |
|||
new_lst = ft_slst_new((*f)(lst->content)); |
|||
if (!new_lst) |
|||
{ |
|||
ft_slst_clear(&new_lst, del); |
|||
return (NULL); |
|||
} |
|||
new_lst->next = ft_slst_map(lst->next, f, del); |
|||
return (new_lst); |
|||
} |
@ -0,0 +1,25 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstnew.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 17:36:09 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
t_slist *ft_slst_new(void *content) |
|||
{ |
|||
t_slist *i_cell; |
|||
|
|||
i_cell = (t_slist *)malloc(sizeof(*i_cell)); |
|||
if (!i_cell) |
|||
return (NULL); |
|||
i_cell->content = content; |
|||
i_cell->next = NULL; |
|||
return (i_cell); |
|||
} |
@ -0,0 +1,23 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_lstsize.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/22 14:49:32 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 11:31:05 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_slst_size(t_slist *lst) |
|||
{ |
|||
size_t lst_size; |
|||
|
|||
lst_size = 0; |
|||
while (lst && ++lst_size) |
|||
lst = lst->next; |
|||
return (lst_size); |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strchr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:21:06 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strchr(const char *s, int c) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (s[i] || c == '\0') |
|||
{ |
|||
if (s[i] == (c % 256)) |
|||
return ((char *)(s + i)); |
|||
i++; |
|||
} |
|||
return (0); |
|||
} |
@ -0,0 +1,31 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strdup.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/19 14:07:50 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strdup(const char *src) |
|||
{ |
|||
char *dup; |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
dup = malloc((ft_strlen(src) + 1) * sizeof(char)); |
|||
if (!dup) |
|||
return (NULL); |
|||
while (src[i]) |
|||
{ |
|||
dup[i] = src[i]; |
|||
i++; |
|||
} |
|||
dup[i] = '\0'; |
|||
return (dup); |
|||
} |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_striteri.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 16:23:41 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_striteri(char *s, void (*f)(unsigned int, char*)) |
|||
{ |
|||
unsigned int i; |
|||
|
|||
i = 0; |
|||
if (!s || !f) |
|||
return ; |
|||
while (s[i]) |
|||
{ |
|||
f(i, s + i); |
|||
i++; |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strjoin.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 06:49:25 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strjoin(const char *s1, const char *s2) |
|||
{ |
|||
char *ret; |
|||
size_t i; |
|||
size_t j; |
|||
|
|||
if (!s1 || !s2) |
|||
return (NULL); |
|||
ret = malloc(ft_strlen(s1) + ft_strlen(s2) + 1); |
|||
if (ret == NULL) |
|||
return (NULL); |
|||
i = 0; |
|||
while (s1[i]) |
|||
{ |
|||
ret[i] = s1[i]; |
|||
i++; |
|||
} |
|||
j = 0; |
|||
while (s2[j]) |
|||
{ |
|||
ret[i] = s2[j]; |
|||
i++; |
|||
j++; |
|||
} |
|||
ret[i] = '\0'; |
|||
return (ret); |
|||
} |
@ -0,0 +1,37 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strlcat.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:13:02 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
size_t ft_strlcat(char *dst, const char *src, size_t dstsize) |
|||
{ |
|||
size_t i; |
|||
size_t j; |
|||
|
|||
i = 0; |
|||
j = 0; |
|||
while (dst[i]) |
|||
i++; |
|||
while (dstsize && i + j < dstsize - 1 && src[j]) |
|||
{ |
|||
dst[i + j] = src[j]; |
|||
j++; |
|||
} |
|||
if (dstsize) |
|||
dst[i + j] = '\0'; |
|||
while (src[j]) |
|||
j++; |
|||
if (i > dstsize) |
|||
return (dstsize + j); |
|||
else |
|||
return (i + j); |
|||
} |
@ -0,0 +1,30 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strlcpy.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:08:31 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (dstsize && i < dstsize - 1 && src[i]) |
|||
{ |
|||
dst[i] = src[i]; |
|||
i++; |
|||
} |
|||
if (dstsize) |
|||
dst[i] = '\0'; |
|||
while (src[i]) |
|||
i++; |
|||
return (i); |
|||
} |
@ -0,0 +1,33 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_len.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:04:47 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 11:57:59 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
size_t ft_strlen_to(const char *str, char ch) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (str[i] && str[i] != ch) |
|||
i++; |
|||
return (i); |
|||
} |
|||
|
|||
size_t ft_strlen(const char *str) |
|||
{ |
|||
size_t i; |
|||
|
|||
i = 0; |
|||
while (str[i] != '\0') |
|||
i++; |
|||
return (i); |
|||
} |
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strmapi.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 16:12:32 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) |
|||
{ |
|||
unsigned int i; |
|||
char *ret; |
|||
|
|||
if (!s) |
|||
return (NULL); |
|||
ret = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char)); |
|||
if (!ret) |
|||
return (NULL); |
|||
i = 0; |
|||
while (s[i]) |
|||
{ |
|||
ret[i] = (*f)(i, s[i]); |
|||
i++; |
|||
} |
|||
return (ret); |
|||
} |
@ -0,0 +1,34 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strncmp.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:16:37 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
int ft_strncmp(const char *s1, const char *s2, size_t n) |
|||
{ |
|||
size_t i; |
|||
int ret; |
|||
unsigned char *s1cp; |
|||
unsigned char *s2cp; |
|||
|
|||
if (!n) |
|||
return (0); |
|||
i = 0; |
|||
s1cp = (unsigned char *)s1; |
|||
s2cp = (unsigned char *)s2; |
|||
ret = s1cp[i] - s2cp[i]; |
|||
while ((!ret && i < n) && (s1[i] || s2[i])) |
|||
{ |
|||
ret = s1cp[i] - s2cp[i]; |
|||
i++; |
|||
} |
|||
return (ret); |
|||
} |
@ -0,0 +1,42 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strnstr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:52:56 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strnstr(const char *haystack, const char *needle, size_t len) |
|||
{ |
|||
size_t i; |
|||
size_t j; |
|||
int found; |
|||
|
|||
i = 0; |
|||
j = 0; |
|||
found = 0; |
|||
while (haystack[i] && i < len && needle[j]) |
|||
{ |
|||
if (haystack[i] == needle[j]) |
|||
{ |
|||
found = 1; |
|||
j++; |
|||
} |
|||
else if (found) |
|||
{ |
|||
found = 0; |
|||
i -= j; |
|||
j = 0; |
|||
} |
|||
i++; |
|||
} |
|||
if (!needle[j]) |
|||
return ((char *)(haystack + i - j)); |
|||
return (0); |
|||
} |
@ -0,0 +1,33 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strrchr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/18 16:47:21 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_strrchr(const char *s, int c) |
|||
{ |
|||
size_t i; |
|||
int found; |
|||
|
|||
i = 0; |
|||
found = -1; |
|||
while (s[i]) |
|||
{ |
|||
if (s[i] == (c % 256)) |
|||
found = i; |
|||
i++; |
|||
} |
|||
if (c == '\0') |
|||
return ((char *)(s + i)); |
|||
if (found >= 0) |
|||
return ((char *)(s + found)); |
|||
return (0); |
|||
} |
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strrev.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/27 09:36:21 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
void ft_strrev(char **str, unsigned int neg) |
|||
{ |
|||
char ch; |
|||
size_t size; |
|||
size_t i; |
|||
|
|||
size = ft_strlen(*str); |
|||
if (neg) |
|||
(*str)[size++] = '-'; |
|||
i = 0; |
|||
while (i < size / 2) |
|||
{ |
|||
ch = (*str)[i]; |
|||
(*str)[i] = (*str)[size - i - 1]; |
|||
(*str)[size - i - 1] = ch; |
|||
i++; |
|||
} |
|||
} |
@ -0,0 +1,42 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_split.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 15:43:48 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/25 14:56:37 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char **ft_split(const char *str, char c) |
|||
{ |
|||
t_dlist *words; |
|||
char *word; |
|||
int i; |
|||
int j; |
|||
|
|||
i = -1; |
|||
word = NULL; |
|||
words = NULL; |
|||
if (!str) |
|||
return (NULL); |
|||
while (str[++i]) |
|||
{ |
|||
if (!word && str[i] != c) |
|||
j = 0; |
|||
if (!word && str[i] != c) |
|||
word = ft_calloc((int)ft_strlen_to(str + i, c) + 1, sizeof(char)); |
|||
if (word && str[i] == c) |
|||
words = ft_dlst_add(words, word); |
|||
if (word && str[i] == c) |
|||
word = NULL; |
|||
if (str[i] != c) |
|||
word[j++] = str[i]; |
|||
} |
|||
words = ft_dlst_add(words, word); |
|||
return ((char **)ft_dlst_to_arr(words)); |
|||
} |
@ -0,0 +1,42 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_substr.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 06:45:32 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
char *ft_substr(char const *s, unsigned int start, size_t len) |
|||
{ |
|||
size_t i; |
|||
char *ret; |
|||
size_t s_size; |
|||
|
|||
if (!s) |
|||
return (NULL); |
|||
s_size = ft_strlen(s); |
|||
if (start > s_size) |
|||
{ |
|||
start = s_size; |
|||
len = 0; |
|||
} |
|||
else if (s_size < start + len) |
|||
len = s_size - start; |
|||
ret = ft_calloc(len + 1, sizeof(char)); |
|||
if (!ret) |
|||
return (NULL); |
|||
i = 0; |
|||
while (i < len && s[start + i]) |
|||
{ |
|||
ret[i] = s[start + i]; |
|||
i++; |
|||
} |
|||
ret[i] = '\0'; |
|||
return (ret); |
|||
} |
@ -0,0 +1,53 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ft_strtrim.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2021/10/20 13:49:37 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 09:22:49 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "../libft.h" |
|||
|
|||
static int ft_search_char(char c, char const *set) |
|||
{ |
|||
size_t i; |
|||
size_t set_len; |
|||
|
|||
if (!c) |
|||
return (0); |
|||
i = 0; |
|||
set_len = ft_strlen(set); |
|||
while (i < set_len) |
|||
if (c == set[i++]) |
|||
return (1); |
|||
return (0); |
|||
} |
|||
|
|||
char *ft_strtrim(char const *s1, char const *set) |
|||
{ |
|||
unsigned int i; |
|||
unsigned int b_out; |
|||
unsigned int e_out; |
|||
char *ret; |
|||
|
|||
if (!s1) |
|||
return (NULL); |
|||
i = 0; |
|||
b_out = 0; |
|||
e_out = ft_strlen(s1); |
|||
while (ft_search_char(s1[b_out], set)) |
|||
b_out++; |
|||
while (e_out > b_out && ft_search_char(s1[e_out - 1], set)) |
|||
e_out--; |
|||
ret = malloc((e_out - b_out + 1) * sizeof(char)); |
|||
if (!ret) |
|||
return (NULL); |
|||
while (b_out < e_out) |
|||
ret[i++] = s1[b_out++]; |
|||
ret[i] = '\0'; |
|||
return (ret); |
|||
} |
@ -0,0 +1,106 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* minishell.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ |
|||
/* Updated: 2022/05/02 17:43:27 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
# include "minishell.h" |
|||
|
|||
t_command *parser(t_token *tok) |
|||
{ |
|||
int i; |
|||
t_command *cmd; |
|||
t_command *ret; |
|||
|
|||
cmd = ft_calloc(1, sizeof(t_command)); |
|||
ret = cmd; |
|||
while (tok->value) |
|||
{ |
|||
i = 0; |
|||
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); |
|||
while(tok->type != PIPE) |
|||
{ |
|||
if (tok->type) |
|||
update_redir(cmd, tok); |
|||
else |
|||
cmd->argv[i++] = tok->value; |
|||
if (!tok->next) |
|||
break ; |
|||
tok = tok->next; |
|||
} |
|||
//printf("Input : %d\n", cmd->input);
|
|||
//printf("Output : %d\n", cmd->output);
|
|||
cmd->argv[i] = NULL; |
|||
if (tok->next) |
|||
tok = tok->next; |
|||
else |
|||
break ; |
|||
cmd->next = ft_calloc(1, sizeof(t_command)); |
|||
cmd->next->prev = cmd; |
|||
cmd = cmd->next; |
|||
} |
|||
return (ret); |
|||
} |
|||
|
|||
t_token *lexer(char *line) |
|||
{ |
|||
t_lexer *lex; |
|||
char *tmp; |
|||
int tmp_i; |
|||
|
|||
lex = ft_calloc(1, sizeof *ret); |
|||
lex->state = IN_ROOT; |
|||
tmp = ft_calloc(1024, sizeof *tmp); |
|||
tmp_i = 0; |
|||
while (*line) |
|||
{ |
|||
if (check_state(lex, &line) == 0) |
|||
continue; |
|||
if (lex->state != S_QUOTE_ST && *line == '$') |
|||
tmp_i = replace_var(&line, tmp, tmp_i); |
|||
if (check_register(lex, &line, tmp)) |
|||
{ |
|||
i = 0; |
|||
ft_bzero(tmp, 1024); |
|||
continue ; |
|||
} |
|||
tmp[tmp_i] = *line; |
|||
line++; |
|||
tmp_i++; |
|||
} |
|||
create_token(lex, tmp); |
|||
return (lex->tokens); |
|||
} |
|||
|
|||
int launcher(t_env env) |
|||
{ |
|||
char *line; |
|||
int ret; |
|||
|
|||
line = readline("$ "); |
|||
if (line == NULL) |
|||
halt(EXIT_FAILURE); |
|||
if (is_empty(line)) |
|||
return (EXIT_FAILURE); |
|||
ret = piper(parser(lexer(line))); |
|||
add_history(line); |
|||
return (ret); |
|||
} |
|||
|
|||
int main(int argc, char **argv, char**envp) |
|||
{ |
|||
if (argc < 2) |
|||
(void)argv; |
|||
(void)envp; |
|||
t_env env; |
|||
while (1) |
|||
launcher(env); |
|||
clear_history(); |
|||
return (EXIT_SUCCESS); |
|||
} |
@ -0,0 +1,95 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* minishell.h :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ |
|||
/* Updated: 2022/05/02 16:45:28 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#ifndef MINISHELL_H |
|||
# define MINISHELL_H |
|||
# define PIPE_MAX_SIZE 8192 |
|||
# define PATHS_MAX_SIZE 126 |
|||
# define PROMPT_PREFIX 0 |
|||
|
|||
# include "libft/libft.h" |
|||
# include <fcntl.h> |
|||
# include <stdio.h> |
|||
# include <stdlib.h> |
|||
# include <readline/readline.h> |
|||
# include <readline/history.h> |
|||
# include <sys/types.h> |
|||
# include <sys/wait.h> |
|||
# include <signal.h> |
|||
# include <unistd.h> |
|||
# include <dirent.h> |
|||
# include <errno.h> |
|||
# include <termios.h> |
|||
|
|||
char **g_env; |
|||
|
|||
|
|||
// ----------------------------------Piper.c
|
|||
typedef struct s_command |
|||
{ |
|||
char **argv; |
|||
char **envp; |
|||
int argc; |
|||
int fd[2]; |
|||
char *heredoc; |
|||
struct s_command *next; |
|||
struct s_command *prev; |
|||
} t_command; |
|||
|
|||
|
|||
// ----------------------------------Parser.c
|
|||
|
|||
typedef struct s_token |
|||
{ |
|||
char *value; |
|||
t_type type; |
|||
struct s_token *next; |
|||
} t_token; |
|||
|
|||
size_t count_arguments(t_token tok); |
|||
void update_redir(t_command *cmd, t_token tok); |
|||
|
|||
// ----------------------------------Lexer.c
|
|||
|
|||
typedef enum e_state |
|||
{ |
|||
OLD_ST, |
|||
ROOT_ST, |
|||
S_QUOTE_ST, |
|||
D_QUOTE_ST, |
|||
} t_state; |
|||
|
|||
typedef enum e_type |
|||
{ |
|||
WORD, |
|||
PIPE, |
|||
OUT, |
|||
ADD, |
|||
IN, |
|||
HD, |
|||
} t_type; |
|||
|
|||
typedef struct s_lex |
|||
{ |
|||
t_state state; |
|||
t_type type; |
|||
t_type next_type; |
|||
t_token *tokens; |
|||
int n_elem; |
|||
} t_lexer; |
|||
|
|||
int create_token(t_lexer *lex, char str[]); |
|||
int check_register(t_lexer *lex, char **line, char *tmp); |
|||
int replace_var(char **line, char *tmp, int tmp_i); |
|||
int check_state(t_lexer *lex, char *line, int i); |
|||
|
|||
#endif |
@ -0,0 +1,83 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* built-in.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ |
|||
/* Updated: 2022/04/14 07:45:37 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
int ft_echo(char *argv[], int output_fd) |
|||
{ |
|||
int no_nl; |
|||
|
|||
|
|||
no_nl = 0; |
|||
if (argv[1]) |
|||
{ |
|||
argv++; |
|||
if (ft_strncmp(*argv, "-n", 3) == 0 && argv++) |
|||
no_nl = 1; |
|||
while (*argv) |
|||
{ |
|||
ft_putstr_fd(*argv, output_fd); |
|||
argv++; |
|||
if (*argv) |
|||
ft_putstr_fd(" ", output_fd); |
|||
} |
|||
} |
|||
if (!no_nl) |
|||
ft_putstr_fd("\n", output_fd); |
|||
return (0); |
|||
} |
|||
|
|||
int ft_pwd(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
char *dir; |
|||
|
|||
(void)argv; |
|||
(void)input_fd; |
|||
dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char)); |
|||
getcwd(dir, PATHS_MAX_SIZE); |
|||
ft_putstr_fd(dir, output_fd); |
|||
ft_putstr_fd("\n", output_fd); |
|||
free(dir); |
|||
return (0); |
|||
} |
|||
|
|||
int ft_cd(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
|
|||
if (argv[1]) |
|||
{ |
|||
if (access(argv[1], X_OK)) |
|||
{ |
|||
printf("Minishell: %s: %s: Permission denied\n", argv[0], argv[1]); |
|||
return (0); |
|||
} |
|||
else if (access(argv[1], F_OK)) |
|||
{ |
|||
printf("Minishell: %s: %s: Not a directory\n", argv[0], argv[1]); |
|||
return (0); |
|||
} |
|||
chdir(argv[1]); |
|||
return (1); |
|||
} |
|||
else |
|||
{ |
|||
chdir(getenv("HOME")); |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int close_minishell(int exit_code) |
|||
{ |
|||
exit(exit_code); |
|||
} |
@ -0,0 +1,102 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* caller.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/02/16 16:46:53 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/24 17:15:24 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
int command_call(char *argv[], int input_fd, int output_fd) |
|||
{ |
|||
if (ft_strncmp(argv[0], "echo", 4) == 0) |
|||
return (ft_echo(argv, output_fd)); |
|||
else if (ft_strncmp(argv[0], "pwd", 3) == 0) |
|||
return (ft_pwd(argv, input_fd, output_fd)); |
|||
else if (ft_strncmp(argv[0], "cd", 2) == 0) |
|||
return (ft_cd(argv, input_fd, output_fd)); |
|||
else if (ft_strncmp(argv[0], "export", 6) == 0) |
|||
return (ft_export(argv, input_fd, output_fd)); |
|||
else if (ft_strncmp(argv[0], "unset", 5) == 0) |
|||
return (ft_unset(argv, input_fd, output_fd)); |
|||
else if (ft_strncmp(argv[0], "env", 3) == 0) |
|||
return (ft_env(argv, input_fd, output_fd)); |
|||
else if (ft_strncmp(argv[0], "exit", 4) == 0) |
|||
return (close_minishell(0)); |
|||
else |
|||
{ |
|||
errno = EINVAL; |
|||
return (-1); |
|||
} |
|||
} |
|||
|
|||
void exe(char *argv[], char *const envp[]) |
|||
{ |
|||
char **path_dirs; |
|||
char *path; |
|||
size_t path_length; |
|||
|
|||
|
|||
execve(argv[0], argv, envp); |
|||
path_dirs = ft_split(getenv("PATH"), ':'); |
|||
while (path_dirs) |
|||
{ |
|||
path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; |
|||
path = ft_calloc(path_length, sizeof(char)); |
|||
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); |
|||
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); |
|||
ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0])); |
|||
free(path); |
|||
execve(path, argv, envp); |
|||
path_dirs++; |
|||
} |
|||
} |
|||
|
|||
int caller(t_command *command, int input_fd, int output_fd) |
|||
{ |
|||
pid_t pid; |
|||
int cmd_ret; |
|||
int status; |
|||
|
|||
if (command->input != 0) |
|||
input_fd = command->fd[0]; |
|||
if (command->output != 1) |
|||
output_fd = command->fd[1]; |
|||
else if (!command->next) |
|||
output_fd = 1; |
|||
cmd_ret = command_call(command->argv, input_fd, output_fd); |
|||
if (cmd_ret == -1 && errno == EINVAL) |
|||
{ |
|||
pid = fork(); |
|||
termios(1); |
|||
if (pid == 0) |
|||
{ |
|||
if (command->prev || command->input || command->heredoc) |
|||
{ |
|||
dup2(input_fd, STDIN_FILENO); |
|||
close(input_fd); |
|||
} |
|||
if (command->next || command->output) |
|||
dup2(output_fd, STDOUT_FILENO); |
|||
exe(command->argv, command->envp); |
|||
} |
|||
//signal(SIGINT, NULL);
|
|||
//signal(SIGQUIT, NULL);
|
|||
waitpid(pid, &status, 0); |
|||
//signal(SIGINT, sigs_handler);
|
|||
//signal(SIGQUIT, sigs_handler);
|
|||
termios(0); |
|||
if (command->output) |
|||
close(command->output); |
|||
if (WIFSIGNALED(status)) |
|||
return (-1); |
|||
if (!WIFEXITED(status)) |
|||
printf("Minishell: command not found: '%s'\n", command->argv[0]); |
|||
} |
|||
return (0); |
|||
} |
@ -0,0 +1,90 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* env.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ |
|||
/* Updated: 2022/04/14 07:45:21 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
int ft_unset(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
if (!argv[1]) |
|||
{ |
|||
//read(input_fd, PIPE_MAX_SIZE);
|
|||
return (1); |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int ft_env(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
(void)argv; |
|||
int i; |
|||
|
|||
i = 0; |
|||
while (g_env[i]) |
|||
{ |
|||
printf("%s\n", g_env[i]); |
|||
i++; |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int is_valid_identifier(char *ident) |
|||
{ |
|||
if (ft_isalpha(ident[0]) || ident[0] == '_') |
|||
{ |
|||
while (*ident) |
|||
{ |
|||
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') |
|||
return (0); |
|||
ident++; |
|||
} |
|||
return (1); |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int ft_export(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
char **new; |
|||
char **env; |
|||
int i; |
|||
|
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
i = 0; |
|||
new = ft_split(argv[1], '='); |
|||
if (!is_valid_identifier(new[0])) |
|||
{ |
|||
printf ("export: not an identifier: %s\n", new[0]); |
|||
return (0); |
|||
} |
|||
while (g_env[i]) |
|||
{ |
|||
env = ft_split(g_env[i], '='); |
|||
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1)) |
|||
{ |
|||
g_env[i] = argv[1]; |
|||
return (1); |
|||
} |
|||
i++; |
|||
} |
|||
g_env[i] = argv[1]; |
|||
g_env[i + 1] = NULL; |
|||
return (1); |
|||
} |
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1,90 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* export.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ |
|||
/* Updated: 2022/04/14 07:45:21 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
int ft_unset(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
if (!argv[1]) |
|||
{ |
|||
//read(input_fd, PIPE_MAX_SIZE);
|
|||
return (1); |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int ft_env(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
(void)argv; |
|||
int i; |
|||
|
|||
i = 0; |
|||
while (g_env[i]) |
|||
{ |
|||
printf("%s\n", g_env[i]); |
|||
i++; |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int is_valid_identifier(char *ident) |
|||
{ |
|||
if (ft_isalpha(ident[0]) || ident[0] == '_') |
|||
{ |
|||
while (*ident) |
|||
{ |
|||
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') |
|||
return (0); |
|||
ident++; |
|||
} |
|||
return (1); |
|||
} |
|||
return (0); |
|||
} |
|||
|
|||
int ft_export(char **argv, int input_fd, int output_fd) |
|||
{ |
|||
char **new; |
|||
char **env; |
|||
int i; |
|||
|
|||
(void)output_fd; |
|||
(void)input_fd; |
|||
i = 0; |
|||
new = ft_split(argv[1], '='); |
|||
if (!is_valid_identifier(new[0])) |
|||
{ |
|||
printf ("export: not an identifier: %s\n", new[0]); |
|||
return (0); |
|||
} |
|||
while (g_env[i]) |
|||
{ |
|||
env = ft_split(g_env[i], '='); |
|||
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1)) |
|||
{ |
|||
g_env[i] = argv[1]; |
|||
return (1); |
|||
} |
|||
i++; |
|||
} |
|||
g_env[i] = argv[1]; |
|||
g_env[i + 1] = NULL; |
|||
return (1); |
|||
} |
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1,100 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* parser.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/01/06 07:52:29 by narnaud #+# #+# */ |
|||
/* Updated: 2022/04/14 09:19:12 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
|
|||
size_t count_arguments(t_token tok) |
|||
{ |
|||
size_t ret; |
|||
|
|||
ret = 0; |
|||
while (tok.value) |
|||
{ |
|||
if (tok.type == 0 && tok.value[0] != '|') |
|||
ret++; |
|||
if (tok.next) |
|||
tok = *(tok.next); |
|||
else |
|||
break ; |
|||
} |
|||
return (ret); |
|||
} |
|||
|
|||
void update_redir(t_command *cmd, t_token tok) |
|||
{ |
|||
if (tok.type == OUT) |
|||
{ |
|||
if (cmd->fd[1]) |
|||
close(cmd->fd[1]); |
|||
cmd->output = init_file(tok.value, O_TRUNC | O_WRONLY); |
|||
} |
|||
else if (tok.type == ADD) |
|||
{ |
|||
if (cmd->fd[1]) |
|||
close(cmd->fd[1]); |
|||
cmd->fd[1] = init_file(tok.value, O_APPEND | O_WRONLY); |
|||
} |
|||
else if (tok.type == IN) |
|||
{ |
|||
if (cmd->fd[0]) |
|||
close(cmd->fd[0]); |
|||
cmd->fd[0] = init_file(tok.value, O_RDONLY); |
|||
} |
|||
else if (tok.type == HD) |
|||
{ |
|||
if (cmd->fd[0]) |
|||
close(cmd->fd[0]); |
|||
cmd->fd[0] = 0; |
|||
cmd->heredoc = tok.value; |
|||
} |
|||
} |
|||
|
|||
t_command *parser(t_token tok) |
|||
{ |
|||
int i; |
|||
t_command *cmd; |
|||
t_command *ret; |
|||
|
|||
cmd = ft_calloc(1, sizeof(t_command)); |
|||
ret = cmd; |
|||
while (tok.value) |
|||
{ |
|||
i = 0; |
|||
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); |
|||
while(tok.value[0] != '|') |
|||
{ |
|||
if (tok.type) |
|||
update_redir(cmd, tok); |
|||
else |
|||
{ |
|||
cmd->argv[i] = tok.value; |
|||
i++; |
|||
} |
|||
if (tok.next) |
|||
tok = *(tok.next); |
|||
else |
|||
break ; |
|||
} |
|||
//printf("Input : %d\n", cmd->input);
|
|||
//printf("Output : %d\n", cmd->output);
|
|||
cmd->argv[i] = NULL; |
|||
if (tok.next) |
|||
tok = *(tok.next); |
|||
else |
|||
break ; |
|||
cmd->next = ft_calloc(1, sizeof(t_command)); |
|||
cmd->next->prev = cmd; |
|||
cmd = cmd->next; |
|||
} |
|||
return (ret); |
|||
} |
@ -0,0 +1,94 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* utils.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/01/07 11:54:34 by narnaud #+# #+# */ |
|||
/* Updated: 2022/03/22 15:38:08 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
char **get_commands(char *line) |
|||
{ |
|||
char **split; |
|||
|
|||
split = ft_split(line, '|'); |
|||
return (split); |
|||
} |
|||
|
|||
int is_empty(char *line) |
|||
{ |
|||
while (*line) |
|||
{ |
|||
if (*line <= 9 || (*line >= 13 && *line != 32)) |
|||
return (0); |
|||
line++; |
|||
} |
|||
return (1); |
|||
} |
|||
|
|||
int msg_n_ret(char *msg, int ret) |
|||
{ |
|||
printf("%s", msg); |
|||
return (ret); |
|||
} |
|||
|
|||
int init_file(char *filename, int mode) |
|||
{ |
|||
return (open(filename, mode | O_CREAT, 0644)); |
|||
} |
|||
|
|||
int is_space(char ch) |
|||
{ |
|||
if (ch == 32 || (ch > 8 && ch < 14)) |
|||
return (1); |
|||
else |
|||
return (0); |
|||
} |
|||
|
|||
void termios(int ctl) |
|||
{ |
|||
struct termios termios_p; |
|||
int tty; |
|||
|
|||
tty = ttyslot(); |
|||
if (tcgetattr(tty, &termios_p) < 0) |
|||
{ |
|||
perror("stdin"); |
|||
exit (EXIT_FAILURE); |
|||
} |
|||
//termios_p.c_oflag &= CRDLY;
|
|||
//termios_p.c_lflag &= ICANON;
|
|||
if (ctl) |
|||
{ |
|||
termios_p.c_lflag |= ECHOCTL; |
|||
termios_p.c_cc[VQUIT] = 28; |
|||
} |
|||
else |
|||
{ |
|||
termios_p.c_lflag &= ~(ECHOCTL); |
|||
termios_p.c_cc[VQUIT] = 0; |
|||
} |
|||
tcsetattr(tty, TCSANOW, &termios_p); |
|||
} |
|||
|
|||
void sigs_handler(int sig_num) |
|||
{ |
|||
if (sig_num == SIGQUIT) |
|||
printf("QUIT: 3\n"); |
|||
else if (sig_num == SIGINT) |
|||
printf("\n"); |
|||
rl_on_new_line(); |
|||
rl_replace_line("", 0); |
|||
rl_redisplay(); |
|||
return ; |
|||
} |
|||
void halt(int ret_code) |
|||
{ |
|||
printf("exit"); |
|||
exit(ret_code); |
|||
} |
@ -0,0 +1,61 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* parser.c :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ |
|||
/* Updated: 2022/05/02 17:43:27 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "minishell.h" |
|||
|
|||
size_t count_arguments(t_token *tok) |
|||
{ |
|||
size_t ret; |
|||
|
|||
ret = 0; |
|||
while (tok->value) |
|||
{ |
|||
if (tok->type == WORD && tok->value[0] != '|') |
|||
ret++; |
|||
if (tok->next) |
|||
tok = tok->next; |
|||
else |
|||
break ; |
|||
} |
|||
return (ret); |
|||
} |
|||
|
|||
void update_redir(t_command *cmd, t_token *tok) |
|||
{ |
|||
if (tok->type == OUT) |
|||
{ |
|||
if (cmd->fd[1]) |
|||
close(cmd->fd[1]); |
|||
cmd->output = init_file(tok->value, O_TRUNC | O_WRONLY); |
|||
} |
|||
else if (tok->type == ADD) |
|||
{ |
|||
if (cmd->fd[1]) |
|||
close(cmd->fd[1]); |
|||
cmd->fd[1] = init_file(tok->value, O_APPEND | O_WRONLY); |
|||
} |
|||
else if (tok->type == IN) |
|||
{ |
|||
if (cmd->fd[0]) |
|||
close(cmd->fd[0]); |
|||
cmd->fd[0] = init_file(tok->value, O_RDONLY); |
|||
} |
|||
else if (tok->type == HD) |
|||
{ |
|||
if (cmd->fd[0]) |
|||
close(cmd->fd[0]); |
|||
cmd->fd[0] = 0; |
|||
cmd->heredoc = tok->value; |
|||
} |
|||
} |
|||
|
|||
|
Loading…
Reference in new issue