This commit is contained in:
Camille Chauvet 2022-10-04 22:04:23 +02:00
parent e76297409e
commit d4bc0e05a5
10 changed files with 96 additions and 81 deletions

109
Makefile
View File

@ -6,88 +6,93 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ # # By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# # # Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/04 14:23:05 by cchauvet ### ########.fr # # Updated: 2022/10/04 19:41:01 by cchauvet ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
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 = ft_isalpha.c \ BSRCS = ft_lstnew.c \
ft_isdigit.c \ ft_lstadd_front.c \
ft_isalnum.c \ ft_lstsize.c \
ft_isascii.c \ ft_lstlast.c \
ft_isprint.c \ ft_lstadd_back.c \
ft_strlen.c \ ft_lstdelone.c \
ft_memset.c \ ft_lstclear.c \
ft_strdup.c \ ft_lstiter.c \
ft_strndup.c \ ft_lstmap.c
ft_bzero.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
ft_memcpy.c \
ft_memmove.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_strlcat.c \
ft_atoi.c \
ft_calloc.c \
ft_strlcpy.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_itoa.c \
ft_split.c \
ft_strmapi.c \
ft_striteri.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c \
BSRCS = ft_lstnew.c \ ESRCS = ft_strndup.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} OBJS = ${SRCS:.c=.o}
BOBJS = ${BSRCS:.c=.o} BOBJS = ${BSRCS:.c=.o}
EOBJS = ${ESRCS:.c=.o}
NAME = libft NAME = libft
CFLAG = -Wall -Werror -Wextra CFLAG = -Wall -Werror -Wextra
%.o: %.c libft.h
${CC} ${CFLAGS} -c -o $@ $<
all: ${NAME} all: ${NAME}
${NAME}: ${OBJS} ${NAME}: ${OBJS}
ar -rc ${NAME}.a ${OBJS} ar -rc ${NAME}.a ${OBJS}
bonus: ${OBJS} ${BOJS} bonus: ${OBJS} ${BOBJS} ${EOBJS}
ar -rc ${NAME} ${OBJS} ${BOBJS} ar -rc ${NAME}.a ${OBJS} ${BOBJS}
%.o: %.c libft.h extra:
${CC} ${CFLAGS} -c -o $@ $< ar -rc ${NAME}.a ${OBJS} ${BOBJS} ${EOBJS}
clean: clean:
rm -f ${OBJS} ${BOBJS} rm -f ${OBJS} ${BOBJS} ${EOBJS}
fclean: clean fclean: clean
rm -f ${NAME}.a rm -f ${NAME}.a
re: fclean all re: fclean all
.PHONY: bonus clean fclean re .PHONY: bonus extra clean fclean re
test: fclean test: fclean
rm -f a.out rm -f a.out
gcc ${CFLAGS} -g ${SRCS} ${BSRCS} main.c gcc ${CFLAGS} -g ${SRCS} ${BSRCS} ${ESRCS} main.c
so: so:
gcc -nostartfiles -fPIC $(CFLAG) $(SRCS) gcc -nostartfiles -fPIC $(CFLAG) $(SRCS)

BIN
a.out Executable file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:09:01 by cchauvet #+# #+# */ /* Created: 2022/09/27 16:09:01 by cchauvet #+# #+# */
/* Updated: 2022/09/28 16:27:52 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 14:56:44 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,8 +14,10 @@
void *ft_calloc(size_t nmemb, size_t size) void *ft_calloc(size_t nmemb, size_t size)
{ {
char *tab; void *tab;
if (nmemb != 0 && (size_t)(nmemb * size) / nmemb != size)
return (NULL);
tab = malloc(nmemb * size); tab = malloc(nmemb * size);
if (tab == NULL) if (tab == NULL)
return (NULL); return (NULL);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:50 by cchauvet #+# #+# */ /* Created: 2022/09/29 17:37:50 by cchauvet #+# #+# */
/* Updated: 2022/10/04 13:53:25 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 21:24:29 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@ static size_t ft_seglen(const char *s, char c)
size_t len; size_t len;
size_t i; size_t i;
if (*s == 0)
return (1);
len = 1; len = 1;
i = 0; i = 0;
while (s[i]) while (s[i])
@ -28,6 +30,8 @@ static size_t ft_seglen(const char *s, char c)
while (s[i] != c && s[i]) while (s[i] != c && s[i])
i++; i++;
} }
if (len == 1)
return (2);
return (len); return (len);
} }
@ -62,7 +66,7 @@ static char **ft_segmentator(const char *s, char c, size_t len, char **tab)
start = j; start = j;
while (s[j] != c) while (s[j] != c)
j++; j++;
tab[i] = ft_strndup(s + start, j - start); tab[i] = ft_substr(s, start, j - start);
if (tab[i] == NULL) if (tab[i] == NULL)
return (ft_cancel(len, tab)); return (ft_cancel(len, tab));
i++; i++;
@ -78,10 +82,10 @@ char **ft_split(const char *s, char c)
if (s == NULL) if (s == NULL)
return (NULL); return (NULL);
len = ft_seglen(s, c); len = ft_seglen(s, c);
tab = malloc (sizeof(char *) * len); tab = malloc(sizeof(char *) * (len));
if (tab == NULL) if (tab == NULL)
return (NULL); return (NULL);
if (c == 0) if (c == 0 || !*s)
{ {
tab[0] = ft_strdup(s); tab[0] = ft_strdup(s);
if (tab[0] == NULL) if (tab[0] == NULL)
@ -89,8 +93,6 @@ char **ft_split(const char *s, char c)
} }
else else
tab = ft_segmentator(s, c, len - 1, tab); tab = ft_segmentator(s, c, len - 1, tab);
if (tab == NULL)
return (NULL);
tab[len - 1] = NULL; tab[len - 1] = NULL;
return (tab); return (tab);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */ /* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
/* Updated: 2022/09/28 16:49:07 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 15:08:26 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,11 +14,11 @@
char *ft_strchr(const char *s, int c) char *ft_strchr(const char *s, int c)
{ {
while (*s || *s == c) while (*s != c)
{ {
if (*s == c) if (*s == 0)
return ((char *) s); return (NULL);
s++; s++;
} }
return (NULL); return ((char *) s);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:21:01 by cchauvet #+# #+# */ /* Created: 2022/09/29 22:21:01 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:36:33 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 15:01:00 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,8 @@ void ft_striteri(char *s, void (*f)(unsigned int, char *))
{ {
size_t i; size_t i;
if (s == NULL)
return ;
i = 0; i = 0;
while (s[i]) while (s[i])
{ {

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */ /* Created: 2022/09/29 10:33:40 by cchauvet #+# #+# */
/* Updated: 2022/09/29 11:03:02 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 19:02:02 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,15 +17,15 @@ char *ft_strndup(const char *s, size_t n)
char *out; char *out;
size_t i; size_t i;
out = malloc((n + 1) * sizeof(char)); out = malloc((n + 0) * sizeof(char));
if (out == NULL) if (out == NULL)
return (NULL); return (NULL);
i = 0; i = -1;
while (i < n && *s) while (i < n && *s)
{ {
out[i] = s[i]; out[i] = s[i];
i++; i++;
} }
out[i] = 0; out[i] = -1;
return (out); return (out);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */ /* Created: 2022/09/27 08:25:33 by cchauvet #+# #+# */
/* Updated: 2022/09/28 17:26:42 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 15:03:56 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,12 +16,9 @@ char *ft_strrchr(const char *s, int c)
{ {
size_t i; size_t i;
i = ft_strlen((char *) s); i = ft_strlen(s) + 1;
while (i > 0 || s[i] == c) while (i-- > 0)
{
if (s[i] == c) if (s[i] == c)
return ((char *) &s[i]); return ((char *) s + i);
i--;
}
return (NULL); return (NULL);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */ /* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:03:00 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 19:16:16 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,8 +24,11 @@ char *ft_substr(const char *s, unsigned int start, size_t len)
max = len; max = len;
if (start >= ft_strlen(s)) if (start >= ft_strlen(s))
return (ft_strdup("")); return (ft_strdup(""));
sub = ft_strndup(s + start, max); sub = malloc((max + 1) * sizeof(char));
if (sub == NULL) if (sub == NULL)
return (NULL); return (NULL);
sub[max] = 0;
while (max-- > 0)
sub[max] = s[max + start];
return (sub); return (sub);
} }

8
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 13:56:50 by cchauvet #+# #+# */ /* Created: 2022/10/04 13:56:50 by cchauvet #+# #+# */
/* Updated: 2022/10/04 13:57:56 by cchauvet ### ########.fr */ /* Updated: 2022/10/04 21:20:44 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,10 +16,14 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char **splitted; char **splitted;
char *a;
char c;
if (argc < 2) if (argc < 2)
return (1); return (1);
splitted = ft_split(argv[1], argv[2][0]); a = argv[1];
c = argv[2][0];
splitted = ft_split(a, c);
while (*splitted != NULL) while (*splitted != NULL)
{ {
puts(*splitted); puts(*splitted);