#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *new;
new = (t_list *)malloc(sizeof(t_list));
if (new == NULL)
return (NULL);
new->content = content;
new->next = NULL;
return (new);
}
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
new -> next = *lst;
*lst = new;
}
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
t_list *temp;
i = 0;
temp = lst;
while (temp != NULL)
{
temp = temp -> next;
i++;
}
return (i);
}
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (NULL);
while (lst -> next != NULL)
lst = lst -> next;
return (lst);
}
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (lst == NULL || new == NULL)
return ;
if (*lst == NULL)
{
*lst = new;
return ;
}
last = ft_lstlast(*lst);
new -> next = last -> next;
last -> next = new;
}
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void*))
{
if (lst == NULL || del == NULL)
return ;
del(lst -> content);
free(lst);
}
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *curr;
t_list *next;
curr = *lst;
while (curr)
{
next = curr -> next;
ft_lstdelone(curr, del);
curr = next;
}
*lst = NULL;
}
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (lst == NULL || f == NULL)
return ;
while (lst)
{
f(lst -> content);
lst = lst -> next;
}
}
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *result;
t_list *tmp;
if (lst == NULL || f == NULL)
return (NULL);
result = NULL;
while (lst)
{
tmp = ft_lstnew((*f)(lst -> content));
if (tmp == NULL)
{
ft_lstclear(&result, del);
return (NULL);
}
ft_lstadd_back(&result, tmp);
tmp = tmp -> next;
lst = lst -> next;
}
return (result);
}
NAME = libft.a # 최종적으로 만들어질 라이브러리 이름
CC = gcc # 컴파일 명령어
CFLAGS = -Wall -Wextra -Werror # 컴파일 플래그
SRCS = \
ft_isalpha.c \
ft_isdigit.c \
ft_isalnum.c \
ft_isascii.c \
ft_isprint.c \
ft_strlen.c \
ft_memset.c \
ft_bzero.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_strlcat.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.c \
ft_itoa.c \
ft_strmapi.c \
ft_striteri.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c
SRCS_BONUS = ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
ft_lstmap.c
OBJS = $(SRCS:.c=.o) # srcs에 있는 .c를 모두 .o로 바꿔서 불러온다는 의미
OBJS_BONUS = $(SRCS_BONUS:.c=.o)
all: $(NAME)
$(NAME): $(OBJS)
bonus: $(OBJS) $(OBJS_BONUS)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
ar crsu $(NAME) $@
# ar은 파일을 모아 라이브러리로 압축하는 명령어, crsu는 ar 옵션
clean:
rm -f $(OBJS) $(OBJS_BONUS)
fclean: clean
rm -f $(NAME)
re: clean all
.PHONY: all bonus clean fclean re
# 여기에 명시된 이름은 파일이 아닌 명령어로 보게 함. 이름이 같은 파일과 충돌 방지
파일 간의 종속 관계를 파악하여 Makefile(기술 파일)에 적힌 대로 컴파일러에 명령하여 SHELL 명령이 순차적으로 실행될 수 있게 한다.