diff --git a/Makefile b/Makefile index 5cf2fc4..e0a97bc 100644 --- a/Makefile +++ b/Makefile @@ -33,31 +33,32 @@ _SRC += ft_strnstr.c _SRC += ft_strrchr.c _SRC += ft_tolower.c _SRC += ft_toupper.c -# _SRC += ft_putchar_fd.c -# _SRC += ft_putendl_fd.c -# _SRC += ft_putnbr_fd.c -# _SRC += ft_putstr_fd.c +_SRC += ft_putchar_fd.c +_SRC += ft_putendl_fd.c +_SRC += ft_putnbr_fd.c +_SRC += ft_putstr_fd.c -# _SRC += ft_itoa.c +_SRC += ft_itoa.c _SRC += ft_strtrim.c _SRC += ft_substr.c _SRC += ft_strjoin.c _SRC += ft_split.c -# _SRC += ft_striteri.c -# _SRC += ft_strmapi.c +_SRC += ft_striteri.c +_SRC += ft_strmapi.c +_SRC += ft_lstnew.c +_SRC += ft_lstadd_front.c # _SRC += ft_lstadd_back.c -# _SRC += ft_lstadd_front.c # _SRC += ft_lstclear # _SRC += ft_lstdelone.c # _SRC += ft_lstiter.c # _SRC += ft_lstlast.c # _SRC += ft_lstmap.c -# _SRC += ft_lstnew.c # _SRC += ft_lstsize.c _SRC += ft_char_in_charset.c _SRC += ft_isspace.c +_SRC += ft_abs.c unexport _DEPS _DEPS += libft.h @@ -68,10 +69,6 @@ OBJ = $(addprefix $(OBJDIR)/,$(_OBJ)) SRC = $(addprefix $(SRCDIR)/,$(_SRC)) DEPS = $(addprefix $(INCDIR)/,$(_DEPS)) -so: - $(CC) -nostartfiles -fPIC $(CFLAGS) $(SRC) - gcc -nostartfiles -shared -o libft.so $(OBJ) - all: $(NAME) $(NAME): $(OBJ) diff --git a/ft_abs.c b/ft_abs.c new file mode 100644 index 0000000..c268d53 --- /dev/null +++ b/ft_abs.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/03 11:52:45 by tosuman #+# #+# */ +/* Updated: 2023/06/03 11:52:45 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +unsigned int ft_abs(int n) +{ + if (n > 0) + return (n); + else + return (-(unsigned int) n); +} diff --git a/ft_atoi.o b/ft_atoi.o deleted file mode 100644 index a66baf1..0000000 Binary files a/ft_atoi.o and /dev/null differ diff --git a/ft_bzero.o b/ft_bzero.o deleted file mode 100644 index 748546e..0000000 Binary files a/ft_bzero.o and /dev/null differ diff --git a/ft_calloc.o b/ft_calloc.o deleted file mode 100644 index 91213ea..0000000 Binary files a/ft_calloc.o and /dev/null differ diff --git a/ft_isalnum.o b/ft_isalnum.o deleted file mode 100644 index e154d47..0000000 Binary files a/ft_isalnum.o and /dev/null differ diff --git a/ft_isalpha.o b/ft_isalpha.o deleted file mode 100644 index dcf3c8c..0000000 Binary files a/ft_isalpha.o and /dev/null differ diff --git a/ft_isascii.o b/ft_isascii.o deleted file mode 100644 index 0e03552..0000000 Binary files a/ft_isascii.o and /dev/null differ diff --git a/ft_isdigit.o b/ft_isdigit.o deleted file mode 100644 index bfc0bfb..0000000 Binary files a/ft_isdigit.o and /dev/null differ diff --git a/ft_isprint.o b/ft_isprint.o deleted file mode 100644 index 6a2f7b9..0000000 Binary files a/ft_isprint.o and /dev/null differ diff --git a/ft_isspace.o b/ft_isspace.o deleted file mode 100644 index e96eb9c..0000000 Binary files a/ft_isspace.o and /dev/null differ diff --git a/ft_itoa.c b/ft_itoa.c index e69de29..ac6b9fb 100644 --- a/ft_itoa.c +++ b/ft_itoa.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/03 11:32:41 by tosuman #+# #+# */ +/* Updated: 2023/06/03 11:32:41 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_itoa(int n) +{ + char *str; + size_t n_len; + unsigned int n_cpy; + int is_neg; + + is_neg = n < 0; + n_len = 0; + n_cpy = ft_abs(n) + !n; + while (n_cpy * ++n_len != 0) + n_cpy /= 10; + --n_len; + str = malloc(sizeof(*str) * (n_len + is_neg + 1)); + if (!str) + return (0); + str += n_len + is_neg; + n_cpy = ft_abs(n); + *str-- = 0; + while (n_len--) + { + *str-- = n_cpy % 10 + '0'; + n_cpy /= 10; + } + if (is_neg) + *str-- = '-'; + return (++str); +} diff --git a/ft_lstadd_front.c b/ft_lstadd_front.c index e69de29..e8c412f 100644 --- a/ft_lstadd_front.c +++ b/ft_lstadd_front.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/14 11:57:15 by tosuman #+# #+# */ +/* Updated: 2023/06/14 11:57:15 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (lst) + { + if (*lst) + new->next = *lst; + *lst = new; + } +} diff --git a/ft_lstnew.c b/ft_lstnew.c index e69de29..e017942 100644 --- a/ft_lstnew.c +++ b/ft_lstnew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:55:31 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:55:31 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +t_list *ft_lstnew(void *content) +{ + t_list *new; + + new = malloc(sizeof(*new)); + if (!new) + return (0); + new->content = content; + new->next = 0; + return (new); +} diff --git a/ft_memchr.o b/ft_memchr.o deleted file mode 100644 index a940e2c..0000000 Binary files a/ft_memchr.o and /dev/null differ diff --git a/ft_memcmp.o b/ft_memcmp.o deleted file mode 100644 index 0b59749..0000000 Binary files a/ft_memcmp.o and /dev/null differ diff --git a/ft_memcpy.o b/ft_memcpy.o deleted file mode 100644 index 39fc966..0000000 Binary files a/ft_memcpy.o and /dev/null differ diff --git a/ft_memmove.o b/ft_memmove.o deleted file mode 100644 index 99a0ff5..0000000 Binary files a/ft_memmove.o and /dev/null differ diff --git a/ft_memset.o b/ft_memset.o deleted file mode 100644 index 57bee77..0000000 Binary files a/ft_memset.o and /dev/null differ diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c index e69de29..6d61be1 100644 --- a/ft_putchar_fd.c +++ b/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:24:36 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:24:37 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c index e69de29..150705b 100644 --- a/ft_putendl_fd.c +++ b/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:27:38 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:28:47 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index e69de29..5128ae6 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:29:25 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:29:26 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_putnbr_fd(int nb, int fd) +{ + if (nb > 9) + { + ft_putnbr_fd(nb / 10, fd); + ft_putchar_fd(nb % 10 + '0', fd); + } + else if (nb == INT_MIN) + { + ft_putnbr_fd(nb / 10, fd); + ft_putnbr_fd(-(nb % 10), fd); + } + else if (nb < 0) + { + ft_putchar_fd('-', fd); + ft_putnbr_fd(-nb, fd); + } + else + ft_putchar_fd(nb % 10 + '0', fd); +} diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index e69de29..2646480 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:26:11 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:26:11 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + while (*s) + ft_putchar_fd(*s++, fd); +} diff --git a/ft_split.c b/ft_split.c index a8b3c33..f0b9c28 100644 --- a/ft_split.c +++ b/ft_split.c @@ -22,6 +22,8 @@ static size_t split_arr_len(char const *s, char c) { while (*s == c) ++s; + if (!*s) + return (len); ++len; while (*s != c && *s) ++s; @@ -50,7 +52,7 @@ char **ft_split(char const *s, char c) char const *start; arr_len = split_arr_len(s, c); - arr = malloc(sizeof(char *) * (arr_len + 1)); + arr = malloc(sizeof(*arr) * (arr_len + 1)); if (!arr) return (0); idx = 0; @@ -58,10 +60,12 @@ char **ft_split(char const *s, char c) { while (*s == c) ++s; + if (!*s) + break ; start = s; while (*s != c && *s) ++s; - arr[idx] = malloc(sizeof(char) * (s - start + 1)); + arr[idx] = malloc(sizeof(**arr) * (s - start + 1)); if (!save_strlcpy(arr, ++idx, start, s - start + 1)) return (0); } diff --git a/ft_strchr.o b/ft_strchr.o deleted file mode 100644 index 3c234fc..0000000 Binary files a/ft_strchr.o and /dev/null differ diff --git a/ft_strdup.c b/ft_strdup.c index 29d3751..e9b72f1 100644 --- a/ft_strdup.c +++ b/ft_strdup.c @@ -19,7 +19,7 @@ char *ft_strdup(char const *s) size_t len; len = ft_strlen(s); - s2 = malloc(sizeof(char) * (len + 1)); + s2 = malloc(sizeof(*s2) * (len + 1)); if (!s2) return (0); ft_strlcpy(s2, (char *) s, len + 1); diff --git a/ft_strdup.o b/ft_strdup.o deleted file mode 100644 index eaca3d6..0000000 Binary files a/ft_strdup.o and /dev/null differ diff --git a/ft_striteri.c b/ft_striteri.c index e69de29..30a2f48 100644 --- a/ft_striteri.c +++ b/ft_striteri.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/09 14:47:33 by tosuman #+# #+# */ +/* Updated: 2023/06/09 14:47:34 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + int idx; + + idx = -1; + while (s[++idx]) + (f)(idx, &s[idx]); +} diff --git a/ft_strjoin.c b/ft_strjoin.c index 4170a7f..a020ffb 100644 --- a/ft_strjoin.c +++ b/ft_strjoin.c @@ -18,7 +18,7 @@ char *ft_strjoin(char const *s1, char const *s2) char *joined_str; total_len = ft_strlen(s1) + ft_strlen(s2); - joined_str = ft_calloc(total_len + 1, sizeof(char)); + joined_str = ft_calloc(total_len + 1, sizeof(*joined_str)); if (!joined_str) return (0); ft_strlcat(joined_str, s1, total_len + 1); diff --git a/ft_strlcat.o b/ft_strlcat.o deleted file mode 100644 index 674ebad..0000000 Binary files a/ft_strlcat.o and /dev/null differ diff --git a/ft_strlcpy.o b/ft_strlcpy.o deleted file mode 100644 index 9d6feab..0000000 Binary files a/ft_strlcpy.o and /dev/null differ diff --git a/ft_strlen.o b/ft_strlen.o deleted file mode 100644 index 839e175..0000000 Binary files a/ft_strlen.o and /dev/null differ diff --git a/ft_strmapi.c b/ft_strmapi.c index e69de29..b0b4bb7 100644 --- a/ft_strmapi.c +++ b/ft_strmapi.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/03 12:41:34 by tosuman #+# #+# */ +/* Updated: 2023/06/03 12:41:34 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strmapi(char const *s, char (f)(unsigned int, char)) +{ + char *str; + int idx; + + str = malloc(sizeof(*str) * (ft_strlen(s) + 1)); + if (!str) + return (0); + idx = -1; + while (s[++idx]) + str[idx] = (f)(idx, s[idx]); + str[idx] = 0; + return (str); +} diff --git a/ft_strncmp.o b/ft_strncmp.o deleted file mode 100644 index 7f8c6a7..0000000 Binary files a/ft_strncmp.o and /dev/null differ diff --git a/ft_strnstr.o b/ft_strnstr.o deleted file mode 100644 index 3c5aa4a..0000000 Binary files a/ft_strnstr.o and /dev/null differ diff --git a/ft_strrchr.o b/ft_strrchr.o deleted file mode 100644 index b903928..0000000 Binary files a/ft_strrchr.o and /dev/null differ diff --git a/ft_strtrim.c b/ft_strtrim.c index c2967fd..ec67e52 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -40,7 +40,7 @@ char *ft_strtrim(char const *s1, char const *set) char *trimmed_str; trimmed_len = len_after_trim(s1, set); - trimmed_str = malloc(sizeof(char) * (trimmed_len + 1)); + trimmed_str = malloc(sizeof(*trimmed_str) * (trimmed_len + 1)); if (!trimmed_str) return (0); while (ft_char_in_charset(*s1, set)) diff --git a/ft_substr.c b/ft_substr.c index b249a65..4e43e5e 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -25,7 +25,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len) ; if (!s[idx]) { - empty = ft_calloc(1, sizeof(char)); + empty = ft_calloc(1, sizeof(*empty)); if (!empty) return (0); return (empty); @@ -33,7 +33,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len) real_len = 0; while (++real_len <= len && s[idx + real_len - 1]) ; - substr = malloc(sizeof(char) * real_len); + substr = malloc(sizeof(*substr) * real_len); if (!substr) return (0); ft_strlcpy(substr, s + idx, real_len); diff --git a/ft_substr.o b/ft_substr.o deleted file mode 100644 index 2801b67..0000000 Binary files a/ft_substr.o and /dev/null differ diff --git a/ft_tolower.o b/ft_tolower.o deleted file mode 100644 index 560b038..0000000 Binary files a/ft_tolower.o and /dev/null differ diff --git a/ft_toupper.o b/ft_toupper.o deleted file mode 100644 index 7bd3b7e..0000000 Binary files a/ft_toupper.o and /dev/null differ diff --git a/libft.a b/libft.a deleted file mode 100644 index d78df99..0000000 Binary files a/libft.a and /dev/null differ diff --git a/libft.h b/libft.h index 8f462d7..4a8eb3f 100644 --- a/libft.h +++ b/libft.h @@ -12,42 +12,54 @@ #ifndef LIBFT_H # define LIBFT_H +# define UINT unsigned int # include -int ft_atoi(char const *nptr); -void ft_bzero(void *s, size_t n); -void *ft_calloc(size_t nmemb, size_t size); -int ft_isalnum(int c); -int ft_isalpha(int c); -int ft_isascii(int c); -int ft_isdigit(int c); -int ft_isprint(int c); -int ft_isspace(int c); -void *ft_memchr(void const *s, int c, size_t n); -int ft_memcmp(void const *s1, void const *s2, size_t n); -void *ft_memcpy(void *dest, void const *src, size_t n); -void *ft_memmove(void *dest, void const *src, size_t n); -void *ft_memset(void *s, int c, size_t n); -char *ft_strchr(char const *s, int c); -char *ft_strdup(char const *s); -size_t ft_strlcat(char *dst, char const *src, size_t size); -size_t ft_strlcpy(char *dst, char const *src, size_t size); -size_t ft_strlen(char const *s); -int ft_strncmp(char const *s1, char const *s2, size_t n); -char *ft_strnstr(char const *big, char const *little, size_t len); -char *ft_strrchr(char const *s, int c); -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(char const *s1, char const *s2); -char *ft_strtrim(char const *s1, char const *set); -int ft_char_in_charset(char c, char const *charset); -char **ft_split(char const *s, char c); - typedef struct s_list { void *content; struct s_list *next; } t_list; +void *ft_memmove(void *dest, void const *src, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +void *ft_memset(void *s, int c, unsigned long n); +void ft_bzero(void *s, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(void const *s1, void const *s2, size_t n); +void *ft_memcpy(void *dest, void const *src, size_t n); +int ft_isalnum(int c); +int ft_isprint(int c); +int ft_isascii(int c); +int ft_isspace(int c); +int ft_isalpha(int c); +int ft_isdigit(int c); +char *ft_strnstr(char const *big, char const *little, size_t len); +char *ft_strchr(char const *s, int c); +char *ft_strrchr(char const *s, int c); +char *ft_strdup(char const *s); +int ft_strncmp(char const *s1, char const *s2, size_t n); +size_t ft_strlen(char const *s); +char **ft_split(char const *s, char c); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strtrim(char const *s1, char const *set); +size_t ft_strlcat(char *dst, char const *src, size_t size); +size_t ft_strlcat(char *dst, char const *src, size_t size); +size_t ft_strlcpy(char *dst, char const *src, size_t size); +char *ft_strmapi(char const *s, char (f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +char *ft_itoa(int n); +int ft_atoi(char const *nptr); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int nb, int fd); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +int ft_char_in_charset(char c, char const *charset); +UINT ft_abs(int n); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); + #endif diff --git a/libft.so b/libft.so deleted file mode 100755 index 1384d14..0000000 Binary files a/libft.so and /dev/null differ