diff options
author | kdx <kikoodx@paranoici.org> | 2023-01-24 17:12:42 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-01-24 17:12:44 +0100 |
commit | 0233628d75680c138f9d9c7db2ba410a5d4d7e54 (patch) | |
tree | c5a1c14518f2696bb35e29ed6f2a5d30f4b0a09e | |
download | minitalk-0233628d75680c138f9d9c7db2ba410a5d4d7e54.tar.gz |
60 files changed, 1952 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a2ddf6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o +client +server +client_bonus +server_bonus +tags diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0939aee --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +CC = gcc +LD = gcc +CFLAGS = -Wall -Wextra -Werror +LDFLAGS = -Llibft -lft +NAME = server + +all: client server + +bonus: client_bonus server_bonus + +$(NAME): libft/libft.a server.o + $(LD) -o $@ server.o $(LDFLAGS) + +client: libft/libft.a client.o + $(LD) -o $@ client.o $(LDFLAGS) + +server_bonus: libft/libft.a server_bonus.o + $(LD) -o $@ server_bonus.o $(LDFLAGS) + +client_bonus: libft/libft.a client_bonus.o + $(LD) -o $@ client_bonus.o $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +libft/libft.a: + make -C libft + +clean: + rm -f server.o client.o server_bonus.o client_bonus.o + make -C libft clean + +fclean: clean + rm -f server client server_bonus client_bonus + make -C libft fclean + +re: + make fclean + make all + make -C libft re + +.PHONY: all bonus clean fclean re diff --git a/client.c b/client.c new file mode 100644 index 0000000..843bf52 --- /dev/null +++ b/client.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 03:36:54 by kdx #+# #+# */ +/* Updated: 2022/10/12 05:38:29 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include <sys/types.h> +#include <signal.h> +#include <unistd.h> + +static void send(const unsigned char *s, pid_t pid) +{ + size_t bit; + int sig; + size_t i; + + i = 0; + while (1) + { + bit = 0; + while (bit < 8) + { + sig = SIGUSR1; + if (s[i] & (1 << bit)) + sig = SIGUSR2; + kill(pid, sig); + usleep(50 + i / 100); + bit++; + } + if (s[i] == '\0') + break ; + i++; + } +} + +int main(int argc, char **argv) +{ + pid_t server_pid; + + if (argc != 3) + return (1 | ft_dprintf(2, "usage: client <server PID> <string>\n")); + server_pid = ft_atoi(argv[1]); + send((const unsigned char *)argv[2], server_pid); + return (0); +} diff --git a/client_bonus.c b/client_bonus.c new file mode 100644 index 0000000..d24773c --- /dev/null +++ b/client_bonus.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 03:36:54 by kdx #+# #+# */ +/* Updated: 2022/10/12 05:40:57 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include <sys/types.h> +#include <signal.h> +#include <unistd.h> + +static void send(const unsigned char *s, pid_t pid) +{ + size_t bit; + int sig; + size_t i; + + i = 0; + while (1) + { + bit = 0; + while (bit < 8) + { + sig = SIGUSR1; + if (s[i] & (1 << bit)) + sig = SIGUSR2; + kill(pid, sig); + usleep(50 + i / 100); + bit++; + } + if (s[i] == '\0') + break ; + i++; + } +} + +int main(int argc, char **argv) +{ + pid_t server_pid; + + if (argc != 3) + return (1 | ft_dprintf(2, "usage: client <server PID> <string>\n")); + server_pid = ft_atoi(argv[1]); + send((const unsigned char *)argv[2], server_pid); + return (0); +} diff --git a/libft/.gitignore b/libft/.gitignore new file mode 100644 index 0000000..b32c3ed --- /dev/null +++ b/libft/.gitignore @@ -0,0 +1,4 @@ +*.a +*.o +a.out +libft.so diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..9e1fb34 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,49 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: nkimel <nkimel@student.42angouleme.fr +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/09/26 12:10:27 by nkimel #+# #+# # +# Updated: 2022/10/07 13:33:21 by nkimel ### ########.fr # +# # +# **************************************************************************** # + +CC = gcc +CFLAGS = -Wall -Wextra -Werror +NAME = libft.a +SRC = 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 \ + 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 \ + ft_puti128_fd.c ft_putu128x_fd.c ft_vdprintf.c ft_dprintf.c ft_vprintf.c \ + ft_printf.c ft_atol.c ft_free.c +OBJ := $(patsubst %.c,%.o,$(SRC)) + +all: $(NAME) + +$(NAME): $(OBJ) + ar -rc $(NAME) $(OBJ) + +bonus: $(NAME) + +%.o: %.c libft.h + $(CC) $(CFLAGS) -c -o $@ $< + +so: $(OBJ) + $(CC) -nostartfiles -shared -o libft.so $(OBJ) + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) libft.so + +re: fclean all + +.PHONY: bonus so clean fclean re diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100644 index 0000000..3b498e1 --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */ +/* Updated: 2022/09/28 04:46:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <limits.h> + +int ft_atoi(const char *nptr) +{ + long result; + int sign; + + while (*nptr == ' ' || *nptr == '\n' || *nptr == '\t' + || *nptr == '\r' || *nptr == '\f' || *nptr == '\v') + nptr++; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -sign; + nptr++; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + (*nptr - '0') * sign; + nptr++; + if (result > INT_MAX) + return (-1); + if (result < INT_MIN) + return (0); + } + return (result); +} diff --git a/libft/ft_atol.c b/libft/ft_atol.c new file mode 100644 index 0000000..10d96a9 --- /dev/null +++ b/libft/ft_atol.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/25 16:53:01 by kdx #+# #+# */ +/* Updated: 2022/10/07 13:25:39 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <limits.h> + +t_i64 ft_atol(const char *nptr) +{ + t_i64 result; + int sign; + + while (*nptr == ' ' || *nptr == '\n' || *nptr == '\t' + || *nptr == '\r' || *nptr == '\f' || *nptr == '\v') + nptr++; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -sign; + nptr++; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + (*nptr - '0') * sign; + if (result >= LONG_MAX / 10 || result <= LONG_MIN / 10) + return (result); + nptr++; + } + return (result); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100644 index 0000000..f5320e7 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 04:18:59 by kdx #+# #+# */ +/* Updated: 2022/09/27 05:15:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c new file mode 100644 index 0000000..b7c710d --- /dev/null +++ b/libft/ft_calloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:18:38 by kdx #+# #+# */ +/* Updated: 2022/09/29 20:49:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdint.h> + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (nmemb != 0 && (size_t)(nmemb * size) / nmemb != size) + return (NULL); + ptr = malloc(nmemb * size); + if (ptr == NULL) + return (ptr); + ft_bzero(ptr, nmemb * size); + return (ptr); +} diff --git a/libft/ft_dprintf.c b/libft/ft_dprintf.c new file mode 100644 index 0000000..2a2234a --- /dev/null +++ b/libft/ft_dprintf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:33:24 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_dprintf(int fd, const char *fmt, ...) +{ + va_list va; + t_i32 rv; + + va_start(va, fmt); + rv = ft_vdprintf(fd, fmt, va); + va_end(va); + return (rv); +} diff --git a/libft/ft_free.c b/libft/ft_free.c new file mode 100644 index 0000000..1713236 --- /dev/null +++ b/libft/ft_free.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/07 13:31:11 by kdx #+# #+# */ +/* Updated: 2022/10/07 14:51:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_free(void *ptr) +{ + if (ptr != NULL) + free(ptr); + return (NULL); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100644 index 0000000..75c9552 --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:35:09 by kdx #+# #+# */ +/* Updated: 2022/09/26 15:05:32 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100644 index 0000000..8e3ac7b --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:23:43 by kdx #+# #+# */ +/* Updated: 2022/09/26 16:59:14 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100644 index 0000000..4ffcc7d --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:15:42 by kdx #+# #+# */ +/* Updated: 2022/09/26 15:05:45 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100644 index 0000000..760e7fe --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 12:32:05 by kdx #+# #+# */ +/* Updated: 2022/09/28 04:43:31 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100644 index 0000000..65226a0 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 13:40:45 by kdx #+# #+# */ +/* Updated: 2022/09/26 17:19:40 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100644 index 0000000..35c84d1 --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 19:00:04 by kdx #+# #+# */ +/* Updated: 2022/09/28 19:26:22 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *_itoa(long v, int sign, long div, char *ptr) +{ + size_t i; + + i = 0; + if (sign == -1) + ptr[i++] = '-'; + while (div / 10 > 0) + { + div /= 10; + ptr[i++] = '0' + v / div % 10; + } + ptr[i] = '\0'; + return (ptr); +} + +char *ft_itoa(int n) +{ + long v; + int sign; + long div; + size_t i; + char *ptr; + + if (n == 0) + return (ft_strdup("0")); + v = n; + sign = (v > 0) - (v < 0); + v *= sign; + div = 1; + i = (sign == -1); + while (div <= v && ++i) + div *= 10; + ptr = malloc(i + 1); + if (ptr == NULL) + return (NULL); + return (_itoa(v, sign, div, ptr)); +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100644 index 0000000..953f771 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:06:14 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:10:36 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *last; + + if (lst == NULL || new == NULL) + return ; + last = ft_lstlast(*lst); + if (last == NULL) + *lst = new; + else + last->next = new; +} diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c new file mode 100644 index 0000000..ca3b6ca --- /dev/null +++ b/libft/ft_lstadd_front.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:58:48 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:00:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **list, t_list *new) +{ + if (new == NULL) + return ; + new->next = *list; + *list = new; +} diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c new file mode 100644 index 0000000..a262722 --- /dev/null +++ b/libft/ft_lstclear.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:23:00 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:26:07 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *next; + + if (lst == NULL || del == NULL) + return ; + while (*lst != NULL) + { + next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100644 index 0000000..6975f88 --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:11:51 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:21:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (del == NULL || lst == NULL) + return ; + if (lst != NULL) + { + if (lst->content != NULL) + del(lst->content); + free(lst); + } +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100644 index 0000000..4f66050 --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:30:19 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:31:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (f == NULL) + return ; + while (lst != NULL) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c new file mode 100644 index 0000000..926b5d3 --- /dev/null +++ b/libft/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:02:22 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:03:20 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..e962de6 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:32:46 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:40:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *root; + t_list *last; + + if (f == NULL || del == NULL) + return (NULL); + root = ft_lstnew(f(lst->content)); + if (root == NULL) + return (NULL); + last = root; + lst = lst->next; + while (lst != NULL) + { + last->next = ft_lstnew(f(lst->content)); + if (last->next == NULL) + { + ft_lstclear(&root, del); + return (NULL); + } + lst = lst->next; + last = last->next; + } + return (root); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100644 index 0000000..6ccb7a8 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:49:36 by kdx #+# #+# */ +/* Updated: 2022/09/29 02:50:55 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *lst; + + lst = malloc(sizeof(t_list)); + if (lst == NULL) + return (NULL); + lst->content = content; + lst->next = NULL; + return (lst); +} diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c new file mode 100644 index 0000000..5e0c30e --- /dev/null +++ b/libft/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:00:40 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:01:35 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + + count = 0; + while (lst != NULL) + { + count++; + lst = lst->next; + } + return (count); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100644 index 0000000..f3856fb --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:53:11 by kdx #+# #+# */ +/* Updated: 2022/09/28 00:04:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + const unsigned char *data; + unsigned char ch; + size_t i; + + data = s; + ch = c; + i = 0; + while (i < n) + { + if (data[i] == ch) + return ((void *)s + i); + i++; + } + return (NULL); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100644 index 0000000..ab37316 --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:37:18 by kdx #+# #+# */ +/* Updated: 2022/09/28 05:50:38 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdint.h> + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + const uint8_t *data_1; + const uint8_t *data_2; + size_t i; + + if (n == 0) + return (0); + data_1 = s1; + data_2 = s2; + i = 0; + while (i + 1 < n) + { + if (data_1[i] != data_2[i]) + return (data_1[i] - data_2[i]); + i++; + } + return (data_1[i] - data_2[i]); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100644 index 0000000..90bd13c --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 06:34:25 by kdx #+# #+# */ +/* Updated: 2022/09/27 08:41:48 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + if (dest == src) + return (dest); + while (n-- > 0) + ((char *)dest)[n] = ((char *)src)[n]; + return (dest); +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100644 index 0000000..41f3100 --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 08:42:29 by kdx #+# #+# */ +/* Updated: 2022/09/27 08:46:12 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + + if (dest == src) + return (dest); + if (dest < src) + { + i = 0; + while (i < n) + { + ((char *)dest)[i] = ((char *)src)[i]; + i++; + } + } + else + ft_memcpy(dest, src, n); + return (dest); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100644 index 0000000..b031e6b --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 01:05:33 by kdx #+# #+# */ +/* Updated: 2022/09/27 01:09:27 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *data; + size_t i; + + data = s; + i = 0; + while (i < n) + data[i++] = c; + return (data); +} diff --git a/libft/ft_printf.c b/libft/ft_printf.c new file mode 100644 index 0000000..49643cb --- /dev/null +++ b/libft/ft_printf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:34:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_printf(const char *fmt, ...) +{ + va_list va; + t_i32 rv; + + va_start(va, fmt); + rv = ft_vprintf(fmt, va); + va_end(va); + return (rv); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100644 index 0000000..120c9cc --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:11:58 by kdx #+# #+# */ +/* Updated: 2022/10/04 04:54:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +size_t ft_putchar_fd(char c, int fd) +{ + return (write(fd, &c, 1) == 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100644 index 0000000..9fc0cb3 --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:23:50 by kdx #+# #+# */ +/* Updated: 2022/09/29 02:24:32 by kdx ### ########.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/libft/ft_puti128_fd.c b/libft/ft_puti128_fd.c new file mode 100644 index 0000000..a8bc0fe --- /dev/null +++ b/libft/ft_puti128_fd.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_puti128_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 05:53:17 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_puti128_fd(t_i128 n, int fd) +{ + t_u128 v; + t_u128 div; + size_t rv; + + rv = 0; + v = n; + if (n < 0) + { + rv += ft_putchar_fd('-', fd); + v = -n; + } + div = 1; + while (div <= v / 10) + div *= 10; + while (div > 0) + { + rv += ft_putchar_fd('0' + v / div % 10, fd); + div /= 10; + } + return (rv); +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..7705180 --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 04:54:24 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long v; + long div; + + if (n == 0) + { + ft_putchar_fd('0', fd); + return ; + } + v = n; + if (v < 0) + { + ft_putchar_fd('-', fd); + v *= -1; + } + div = 1; + while (div <= v) + div *= 10; + while (div / 10 > 0) + { + div /= 10; + ft_putchar_fd('0' + v / div % 10, fd); + } +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100644 index 0000000..e971974 --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:16:42 by kdx #+# #+# */ +/* Updated: 2022/10/04 07:56:40 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +size_t ft_putstr_fd(char *s, int fd) +{ + if (s == NULL) + return (ft_putstr_fd("(null)", fd)); + else + write(fd, s, ft_strlen(s)); + return (ft_strlen(s)); +} diff --git a/libft/ft_putu128x_fd.c b/libft/ft_putu128x_fd.c new file mode 100644 index 0000000..01989b4 --- /dev/null +++ b/libft/ft_putu128x_fd.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putu128x_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:29:50 by kdx #+# #+# */ +/* Updated: 2022/10/04 05:58:16 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_putu128x_fd(t_u128 n, int fd, int capitalize) +{ + t_u128 div; + size_t rv; + const char *set; + + set = "0123456789abcdef"; + if (capitalize) + set = "0123456789ABCDEF"; + rv = 0; + div = 1; + while (div <= n / 16) + div *= 16; + while (div > 0) + { + rv += ft_putchar_fd(set[n / div % 16], fd); + div /= 16; + } + return (rv); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100644 index 0000000..d3ed86b --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 16:55:02 by kdx #+# #+# */ +/* Updated: 2022/09/29 19:13:03 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <stdbool.h> + +static void *free_array(char **a) +{ + size_t i; + + if (a != NULL) + { + i = 0; + while (a[i] != NULL) + free(a[i++]); + free(a); + } + return (NULL); +} + +static size_t skip_char(const char **s, char c, bool invert) +{ + size_t i; + const char *p; + + i = 0; + p = *s; + while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0') + { + p++; + i++; + } + *s = p; + return (i); +} + +static size_t count_elems(const char *s, char c) +{ + size_t n_elem; + + n_elem = 0; + skip_char(&s, c, false); + while (*s != '\0') + { + skip_char(&s, c, true); + skip_char(&s, c, false); + n_elem++; + } + return (n_elem); +} + +char **ft_split(char const *s, char c) +{ + char **array; + size_t n_elem; + size_t arr_i; + const char *rem_s; + size_t len; + + if (s == NULL) + return (NULL); + n_elem = count_elems(s, c); + array = ft_calloc(n_elem + 1, sizeof(char *)); + if (array == NULL) + return (array); + skip_char(&s, c, false); + arr_i = 0; + while (arr_i < n_elem) + { + rem_s = s; + len = skip_char(&s, c, true); + array[arr_i] = ft_substr(rem_s, 0, len); + if (array[arr_i++] == NULL) + return (free_array(array)); + skip_char(&s, c, false); + } + return (array); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100644 index 0000000..a806312 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:37:56 by kdx #+# #+# */ +/* Updated: 2022/09/27 23:41:04 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != c) + { + if (*s == '\0') + return (NULL); + s++; + } + return ((char *)s); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100644 index 0000000..0bbfc2e --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 05:21:01 by kdx #+# #+# */ +/* Updated: 2022/09/28 05:28:25 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *copy; + size_t s_len; + + s_len = ft_strlen(s) + 1; + copy = malloc(s_len); + if (copy == NULL) + return (NULL); + ft_memcpy(copy, s, s_len); + return (copy); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100644 index 0000000..bce3e09 --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 02:06:44 by kdx #+# #+# */ +/* Updated: 2022/09/29 02:22:50 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + size_t i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i] != '\0') + { + f(i, s + i); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..72512d4 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 07:29:30 by kdx #+# #+# */ +/* Updated: 2022/09/28 07:40:08 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t s1_len; + size_t s2_len; + char *ptr; + + if (s1 == NULL || s2 == NULL) + return (NULL); + s1_len = ft_strlen(s1); + s2_len = ft_strlen(s2); + ptr = malloc(s1_len + s2_len + 1); + if (ptr == NULL) + return (NULL); + ptr[s1_len + s2_len] = '\0'; + while (s2_len-- > 0) + ptr[s1_len + s2_len] = s2[s2_len]; + while (s1_len-- > 0) + ptr[s1_len] = s1[s1_len]; + return (ptr); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100644 index 0000000..cd1f70b --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:02:56 by kdx #+# #+# */ +/* Updated: 2022/09/28 17:03:21 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dest_size; + + if (size == 0) + return (ft_strlen(src)); + dest_size = ft_strlen(dst); + if (dest_size >= size) + return (ft_strlen(src) + size); + return (dest_size + ft_strlcpy(dst + dest_size, src, size - dest_size)); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100644 index 0000000..2b42ed2 --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 08:46:49 by kdx #+# #+# */ +/* Updated: 2022/09/27 09:01:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + while (i + 1 < size && src[i] != '\0') + { + dst[i] = src[i]; + i++; + } + if (size) + dst[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..e5bd3fe --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/26 14:34:15 by kdx #+# #+# */ +/* Updated: 2022/09/26 15:07:30 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t len; + + len = 0; + while (s[len] != '\0') + len++; + return (len); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100644 index 0000000..241b6d0 --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 20:06:57 by kdx #+# #+# */ +/* Updated: 2022/09/28 22:53:46 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *copy; + size_t i; + + if (s == NULL || f == NULL) + return (NULL); + copy = ft_strdup(s); + if (copy == NULL) + return (NULL); + i = 0; + while (copy[i] != '\0') + { + copy[i] = f(i, copy[i]); + i++; + } + return (copy); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..d870c83 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/18 18:00:30 by kdx #+# #+# */ +/* Updated: 2022/09/27 23:52:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + if (n == 0) + return (0); + while (--n > 0 && (*s1 != '\0' || *s2 != '\0')) + { + if (*s1 != *s2) + return ((unsigned char)*s1 - (unsigned char)*s2); + s1++; + s2++; + } + return ((unsigned char)*s1 - (unsigned char)*s2); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..9143d70 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 00:07:22 by kdx #+# #+# */ +/* Updated: 2022/09/29 20:45:27 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t little_len; + size_t cmp_len; + + little_len = ft_strlen(little); + if (little_len == 0 || (big == little && little_len <= len)) + return ((char *)big); + if (len == 0) + return (NULL); + i = 0; + while (big[i] != '\0' && i < len && little_len <= len - i) + { + cmp_len = len - i; + if (little_len < cmp_len) + cmp_len = little_len; + if (ft_strncmp(big + i, little, cmp_len) == 0) + return ((char *)big + i); + i++; + } + return (NULL); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100644 index 0000000..78bad68 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 23:37:56 by kdx #+# #+# */ +/* Updated: 2022/09/27 23:43:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s) + 1; + while (i-- > 0) + if (s[i] == c) + return ((char *)(s + i)); + return (NULL); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..62f41aa --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 09:23:36 by kdx #+# #+# */ +/* Updated: 2022/09/28 22:07:02 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t begin; + size_t to_copy; + size_t len; + + if (s1 == NULL || set == NULL) + return (NULL); + len = ft_strlen(s1); + if (len == 0) + return (ft_strdup("")); + begin = 0; + while (ft_strchr(set, s1[begin]) != NULL) + begin++; + to_copy = len - begin; + while (ft_strchr(set, s1[begin + to_copy - 1]) != NULL && to_copy) + to_copy--; + return (ft_substr(s1, begin, to_copy)); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100644 index 0000000..4b9196a --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 06:35:57 by kdx #+# #+# */ +/* Updated: 2022/09/28 07:27:31 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + ssize_t size; + char *ptr; + + if (s == NULL) + return (NULL); + size = ft_strlen(s); + size -= start; + if (size < 0) + size = 0; + if ((size_t)size > len) + size = len; + ptr = malloc(size + 1); + if (ptr == NULL) + return (NULL); + ptr[size] = '\0'; + while (size-- > 0) + ptr[size] = s[start + size]; + return (ptr); +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100644 index 0000000..5c5d65b --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:39:00 by kdx #+# #+# */ +/* Updated: 2022/09/27 09:43:03 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c - 'A' + 'a'); + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100644 index 0000000..ba6983a --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/27 09:39:00 by kdx #+# #+# */ +/* Updated: 2022/09/27 09:39:52 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c + 'A' - 'a'); + return (c); +} diff --git a/libft/ft_vdprintf.c b/libft/ft_vdprintf.c new file mode 100644 index 0000000..619a6c3 --- /dev/null +++ b/libft/ft_vdprintf.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vdprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/30 05:04:45 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:29:44 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include <unistd.h> + +static t_i32 put_ptr(void *ptr, int fd) +{ + t_i32 rv; + + rv = 0; + if (ptr != NULL) + { + rv += ft_putstr_fd("0x", fd); + rv += ft_putu128x_fd((t_u64)ptr, fd, 0); + } + else + rv += ft_putstr_fd("(nil)", fd); + return (rv); +} + +static t_i32 put_element(t_i32 fd, const char *fmt, va_list va) +{ + if (*fmt == '%') + return (ft_putchar_fd(*fmt, fd)); + if (*fmt == 'c') + return (ft_putchar_fd(va_arg(va, int), fd)); + if (*fmt == 'd' || *fmt == 'i') + return (ft_puti128_fd(va_arg(va, int), fd)); + if (*fmt == 'u') + return (ft_puti128_fd(va_arg(va, unsigned int), fd)); + if (*fmt == 'p') + return (put_ptr(va_arg(va, void *), fd)); + if (*fmt == 'x' || *fmt == 'X') + return (ft_putu128x_fd(va_arg(va, unsigned int), fd, *fmt == 'X')); + if (*fmt == 's') + return (ft_putstr_fd(va_arg(va, char *), fd)); + return (0); +} + +t_i32 ft_vdprintf(t_i32 fd, const char *fmt, va_list va) +{ + t_i32 rv; + + rv = 0; + while (*fmt != '\0') + { + if (*fmt == '%' && ft_strchr("cdiupxXs%", fmt[1]) != NULL) + rv += put_element(fd, ++fmt, va); + else + rv += ft_putchar_fd(*fmt, fd); + fmt++; + } + return (rv); +} diff --git a/libft/ft_vprintf.c b/libft/ft_vprintf.c new file mode 100644 index 0000000..db53f97 --- /dev/null +++ b/libft/ft_vprintf.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 14:29:46 by kdx #+# #+# */ +/* Updated: 2022/10/06 14:33:43 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_i32 ft_vprintf(const char *fmt, va_list va) +{ + return (ft_vdprintf(1, fmt, va)); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100644 index 0000000..24ec367 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 04:34:06 by kdx #+# #+# */ +/* Updated: 2022/10/07 13:31:01 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include <stdlib.h> +# include <stdint.h> +# include <stdarg.h> + +typedef int8_t t_i8; +typedef int16_t t_i16; +typedef int32_t t_i32; +typedef int64_t t_i64; +typedef __int128_t t_i128; +typedef uint8_t t_u8; +typedef uint16_t t_u16; +typedef uint32_t t_u32; +typedef uint64_t t_u64; +typedef __uint128_t t_u128; +typedef t_u32 t_err; + +int ft_atoi(const char *nptr); +t_i64 ft_atol(const char *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); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strrchr(const char *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); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +size_t ft_putchar_fd(char c, int fd); +size_t ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +size_t ft_puti128_fd(t_i128 n, int fd); +size_t ft_putu128x_fd(t_u128 n, int fd, int capitalize); + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +t_i32 ft_vdprintf(int fd, const char *fmt, va_list va); +t_i32 ft_dprintf(int fd, const char *fmt, ...); +t_i32 ft_vprintf(const char *fmt, va_list va); +t_i32 ft_printf(const char *fmt, ...); + +void *ft_free(void *ptr); + +#endif diff --git a/server.c b/server.c new file mode 100644 index 0000000..7497970 --- /dev/null +++ b/server.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* server.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 03:37:08 by kdx #+# #+# */ +/* Updated: 2022/10/12 05:32:45 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include <unistd.h> +#include <signal.h> + +static uint8_t *g_buffer; + +static void buffer_append(uint8_t byte) +{ + uint8_t *new_buffer; + size_t len; + + if (g_buffer == NULL) + { + g_buffer = ft_calloc(2, sizeof(char)); + if (g_buffer == NULL) + exit(1); + g_buffer[0] = byte; + } + else + { + len = ft_strlen((char *)g_buffer); + new_buffer = ft_calloc(len + 2, sizeof(char)); + if (new_buffer == NULL) + { + ft_free(g_buffer); + exit(1); + } + ft_strlcpy((char *)new_buffer, (char *)g_buffer, -1); + new_buffer[len] = byte; + ft_free(g_buffer); + g_buffer = new_buffer; + } +} + +static void handler(int signum) +{ + static uint8_t byte; + static size_t bit; + + byte |= (signum == SIGUSR2) << bit; + bit += 1; + if (bit >= 8) + { + if (g_buffer != NULL && byte == 0) + { + write(1, g_buffer, ft_strlen((char *)g_buffer)); + ft_free(g_buffer); + g_buffer = NULL; + } + else if (byte != 0) + buffer_append(byte); + byte = 0; + bit = 0; + } + return ; +} + +static void sigint_handler(int signum) +{ + (void)signum; + ft_free(g_buffer); + exit(0); +} + +int main(void) +{ + pid_t pid; + + pid = getpid(); + if (signal(SIGUSR1, handler) == SIG_ERR || signal(SIGUSR2, handler)) + return (1); + if (signal(SIGINT, sigint_handler) == SIG_ERR) + return (1); + ft_puti128_fd(pid, 1); + ft_putchar_fd('\n', 1); + while (1) + ; + return (0); +} diff --git a/server_bonus.c b/server_bonus.c new file mode 100644 index 0000000..f47fd62 --- /dev/null +++ b/server_bonus.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* server_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/09 03:37:08 by kdx #+# #+# */ +/* Updated: 2022/10/12 05:40:59 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/libft.h" +#include <unistd.h> +#include <signal.h> + +static uint8_t *g_buffer; + +static void buffer_append(uint8_t byte) +{ + uint8_t *new_buffer; + size_t len; + + if (g_buffer == NULL) + { + g_buffer = ft_calloc(2, sizeof(char)); + if (g_buffer == NULL) + exit(1); + g_buffer[0] = byte; + } + else + { + len = ft_strlen((char *)g_buffer); + new_buffer = ft_calloc(len + 2, sizeof(char)); + if (new_buffer == NULL) + { + ft_free(g_buffer); + exit(1); + } + ft_strlcpy((char *)new_buffer, (char *)g_buffer, -1); + new_buffer[len] = byte; + ft_free(g_buffer); + g_buffer = new_buffer; + } +} + +static void handler(int signum) +{ + static uint8_t byte; + static size_t bit; + + byte |= (signum == SIGUSR2) << bit; + bit += 1; + if (bit >= 8) + { + if (g_buffer != NULL && byte == 0) + { + write(1, g_buffer, ft_strlen((char *)g_buffer)); + ft_free(g_buffer); + g_buffer = NULL; + } + else if (byte != 0) + buffer_append(byte); + byte = 0; + bit = 0; + } + return ; +} + +static void sigint_handler(int signum) +{ + (void)signum; + ft_free(g_buffer); + exit(0); +} + +int main(void) +{ + pid_t pid; + + pid = getpid(); + if (signal(SIGUSR1, handler) == SIG_ERR || signal(SIGUSR2, handler)) + return (1); + if (signal(SIGINT, sigint_handler) == SIG_ERR) + return (1); + ft_puti128_fd(pid, 1); + ft_putchar_fd('\n', 1); + while (1) + ; + return (0); +} |