Skip to content

Commit 6d78430

Browse files
author
Hikari Shinagawa
committed
finished bonus part
1 parent b9b46a2 commit 6d78430

12 files changed

+196
-68
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

Makefile

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,59 @@
66
# By: hshinaga <[email protected]> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2024/11/02 15:37:41 by hshinaga #+# #+# #
9-
# Updated: 2024/11/05 22:59:03 by hshinaga ### ########.fr #
9+
# Updated: 2024/11/06 00:05:25 by hshinaga ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

1313
NAME = libft.a
1414
CC = cc
1515
CFLAGS = -Wall -Wextra -Werror
1616

17-
SRCS = ft_isalpha.c \
18-
ft_isdigit.c \
19-
ft_isalnum.c \
20-
ft_isascii.c \
21-
ft_isprint.c \
22-
ft_strlen.c \
23-
ft_memset.c \
24-
ft_bzero.c \
25-
ft_memcpy.c \
26-
ft_memmove.c \
27-
ft_strlcpy.c \
28-
ft_strlcat.c \
29-
ft_toupper.c \
30-
ft_tolower.c \
31-
ft_strchr.c \
32-
ft_strrchr.c \
33-
ft_strncmp.c \
34-
ft_memchr.c \
35-
ft_memcmp.c \
36-
ft_strnstr.c \
37-
ft_atoi.c \
38-
ft_calloc.c \
39-
ft_strdup.c \
40-
ft_substr.c \
41-
ft_strjoin.c \
42-
ft_strtrim.c \
43-
ft_split.c \
44-
ft_itoa.c \
45-
ft_strmapi.c \
46-
ft_striteri.c \
47-
ft_putchar_fd.c \
48-
ft_putstr_fd.c \
49-
ft_putendl_fd.c \
50-
ft_putnbr_fd.c
17+
SRCS =ft_isalpha.c \
18+
ft_isdigit.c \
19+
ft_isalnum.c \
20+
ft_isascii.c \
21+
ft_isprint.c \
22+
ft_strlen.c \
23+
ft_memset.c \
24+
ft_bzero.c \
25+
ft_memcpy.c \
26+
ft_memmove.c \
27+
ft_strlcpy.c \
28+
ft_strlcat.c \
29+
ft_toupper.c \
30+
ft_tolower.c \
31+
ft_strchr.c \
32+
ft_strrchr.c \
33+
ft_strncmp.c \
34+
ft_memchr.c \
35+
ft_memcmp.c \
36+
ft_strnstr.c \
37+
ft_atoi.c \
38+
ft_calloc.c \
39+
ft_strdup.c \
40+
ft_substr.c \
41+
ft_strjoin.c \
42+
ft_strtrim.c \
43+
ft_split.c \
44+
ft_itoa.c \
45+
ft_strmapi.c \
46+
ft_striteri.c \
47+
ft_putchar_fd.c \
48+
ft_putstr_fd.c \
49+
ft_putendl_fd.c \
50+
ft_putnbr_fd.c
5151

5252
BONUS_SRCS = ft_lstnew.c \
53-
ft_lstadd_front.c \
54-
ft_lstsize.c \
55-
ft_lstlast.c \
56-
ft_lstadd_back.c \
57-
ft_lstdelone.c \
58-
ft_lstclear.c \
59-
ft_lstiter.c \
60-
ft_lstmap.c
53+
ft_lstadd_front.c \
54+
ft_lstsize.c \
55+
ft_lstlast.c \
56+
ft_lstadd_back.c \
57+
ft_lstdelone.c \
58+
ft_lstclear.c \
59+
ft_lstiter.c \
60+
ft_lstmap.c
61+
6162

6263
OBJS = $(SRCS:.c=.o)
6364
BONUS_OBJS = $(BONUS_SRCS:.c=.o)
@@ -67,20 +68,22 @@ all: $(NAME)
6768
$(NAME): $(OBJS)
6869
ar rcs $(NAME) $(OBJS)
6970

71+
bonus: $(OBJS) $(BONUS_OBJS)
72+
ar rcs $(NAME) $(OBJS) $(BONUS_OBJS)
73+
7074
%.o: %.c libft.h
7175
$(CC) $(CFLAGS) -c $< -o $@
7276

7377
clean:
74-
rm -f $(OBJS)
78+
rm -f $(OBJS) $(BONUS_OBJS)
7579

7680
fclean: clean
7781
rm -f $(NAME)
7882

7983
re: fclean all
8084

81-
8285
so:
8386
$(CC) -nostartfiles -fPIC $(CFLAGS) $(SRCS)
8487
gcc -nostartfiles -shared -o libft.so $(OBJS)
8588

86-
.PHONY: all clean fclean re bonus
89+
.PHONY: all clean fclean re bonus so

ft_lstadd_back.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:09:11 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:12 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:18 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
void ft_lstadd_back(t_list **lst, t_list *new)
16+
{
17+
t_list *last;
18+
19+
if (!lst || !new)
20+
return ;
21+
if (*lst == NULL)
22+
{
23+
*lst = new;
24+
return ;
25+
}
26+
last = ft_lstlast(*lst);
27+
last->next = new;
28+
}

ft_lstadd_front.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:09:08 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:09 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:08 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
void ft_lstadd_front(t_list **lst, t_list *new)
16+
{
17+
if (lst && new)
18+
{
19+
new->next = *lst;
20+
*lst = new;
21+
}
22+
}

ft_lstclear.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:09:06 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:07 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:06 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
void ft_lstclear(t_list **lst, void (*del)(void *))
16+
{
17+
t_list *tmp;
18+
19+
if (!lst || !del)
20+
return ;
21+
while (*lst)
22+
{
23+
tmp = (*lst)->next;
24+
ft_lstdelone(*lst, del);
25+
*lst = tmp;
26+
}
27+
*lst = NULL;
28+
}

ft_lstdelone.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:09:04 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:05 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:04 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
void ft_lstdelone(t_list *lst, void (*del)(void *))
16+
{
17+
if (!lst || !del)
18+
return ;
19+
del(lst->content);
20+
free(lst);
21+
}

ft_lstiter.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:09:02 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:03 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:02 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
void ft_lstiter(t_list *lst, void (*f)(void *))
16+
{
17+
if (!lst || !f)
18+
return ;
19+
while (lst)
20+
{
21+
f(lst->content);
22+
lst = lst->next;
23+
}
24+
}

ft_lstlast.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:08:59 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:09:00 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:51:00 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
t_list *ft_lstlast(t_list *lst)
16+
{
17+
if (!lst)
18+
return (NULL);
19+
while (lst->next)
20+
lst = lst->next;
21+
return (lst);
22+
}

ft_lstmap.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,34 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:08:57 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:08:58 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:50:57 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
16+
{
17+
t_list *new_lst;
18+
t_list *new_node;
19+
void *content;
20+
21+
if (!lst || !f || !del)
22+
return (NULL);
23+
new_lst = NULL;
24+
while (lst)
25+
{
26+
content = f(lst->content);
27+
new_node = ft_lstnew(content);
28+
if (!new_node)
29+
{
30+
if (content)
31+
del(content);
32+
ft_lstclear(&new_lst, del);
33+
return (NULL);
34+
}
35+
ft_lstadd_back(&new_lst, new_node);
36+
lst = lst->next;
37+
}
38+
return (new_lst);
39+
}

ft_lstnew.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:08:52 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:08:53 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 22:26:53 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
t_list *ft_lstnew(void *content)
16+
{
17+
t_list *new_node;
18+
19+
new_node = (t_list *)malloc(sizeof(t_list));
20+
if (!new_node)
21+
return (NULL);
22+
new_node->content = content;
23+
new_node->next = NULL;
24+
return (new_node);
25+
}

ft_lstsize.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/05 18:08:55 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 18:08:56 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/05 23:35:20 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "libft.h"
14+
15+
int ft_lstsize(t_list *lst)
16+
{
17+
int count;
18+
t_list *curr;
19+
20+
count = 0;
21+
curr = lst;
22+
while (curr != NULL)
23+
{
24+
count++;
25+
curr = curr->next;
26+
}
27+
return (count);
28+
}

ft_strdup.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
/* By: hshinaga <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/11/02 15:32:09 by hshinaga #+# #+# */
9-
/* Updated: 2024/11/05 17:44:45 by hshinaga ### ########.fr */
9+
/* Updated: 2024/11/06 00:04:02 by hshinaga ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15-
char *ft_strdup(const char *s1)
15+
char *ft_strdup(const char *src_str)
1616
{
17-
char *dup;
18-
size_t len;
17+
char *new_str;
18+
size_t str_len;
1919

20-
len = ft_strlen(s1);
21-
dup = (char *)malloc(len + 1);
22-
if (!dup)
20+
str_len = ft_strlen(src_str);
21+
new_str = (char *)malloc(sizeof(char) * (str_len + 1));
22+
if (!new_str)
2323
return (NULL);
24-
ft_strlcpy(dup, s1, len + 1);
25-
return (dup);
24+
ft_strlcpy(new_str, src_str, str_len + 1);
25+
return (new_str);
2626
}

0 commit comments

Comments
 (0)