This commit is contained in:
HexPloR
2024-06-23 23:59:58 +02:00
parent d5bc369618
commit 60eef6cebe
98 changed files with 2196 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
CLIENT = client
SERVER = server
SOURCE_CLIENT = src/client/client.c
SOURCE_SERVER = src/server/server.c
LIBFT = lib/libft/libft.a
FT_PRINTF = lib/ft_printf/libftprintf.a
$(CLIENT): $(SOURCE_CLIENT)
cc -Iincludes -Wall -Werror -Wextra -g -o client $(SOURCE_CLIENT) $(LIBFT)
$(SERVER): $(SOURCE_SERVER)
cc -Iincludes -Wall -Werror -Wextra -g -o server $(SOURCE_SERVER) $(FT_PRINTF) $(LIBFT)
all : $(CLIENT) $(SERVER)
clean :
@rm client
@rm server
fclean :
@rm client
@rm server
re : fclean
make all
.PHONY: all re fclean clean
Executable
BIN
View File
Binary file not shown.
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minitalk.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 23:04:05 by yantoine #+# #+# */
/* Updated: 2024/04/16 05:50:54 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINITALK_H
# define MINITALK_H
# include <unistd.h>
# include <signal.h>
# include "../lib/ft_printf/header/ft_printf.h"
# include "../lib/libft/libft.h"
#endif
+30
View File
@@ -0,0 +1,30 @@
NAME = libftprintf.a
CC = cc
CFLAGS = -Wall -Wextra -Werror -c -I$(HEADER_DIR) -g3
RM = rm -f
AR = ar rcs
SOURCES = ft_printf.c ft_putchar_fd.c ft_putnbr_fd.c ft_putptr_fd.c ft_put_unsigned_nbr_fd.c ft_puthex_upper_fd.c ft_puthex_lower_fd.c ft_putstr_fd.c
SOURCES_DIR = source/
HEADER = ft_printf.h
HEADER_DIR = header/
OBJECTS = $(addprefix $(SOURCES_DIR), $(SOURCES:.c=.o))
all: $(NAME)
$(NAME): $(OBJECTS)
$(AR) $(NAME) $(OBJECTS)
$(OBJECTS): $(SOURCES_DIR)%.o :$(SOURCES_DIR)%.c
$(CC) $(CFLAGS) $< -o $@
clean:
$(RM) $(OBJECTS)
fclean: clean
$(RM) $(NAME)
re: fclean
@make all
.PHONY: all clean fclean re
+31
View File
@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 10:18:58 by yantoine #+# #+# */
/* Updated: 2023/12/26 16:09:17 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <limits.h>
# include <stdarg.h>
int pf_puthex_lower_fd(unsigned int n, int fd);
int pf_puthex_upper_fd(unsigned int n, int fd);
int ft_printf(const char *format, ...);
int pf_putchar_fd(char c, int fd);
int pf_putstr_fd(char *s, int fd);
int pf_putnbr_fd(long int n, int fd);
int pf_put_unsigned_nbr_fd(unsigned int n, int fd);
int pf_putptr_fd(unsigned long long n, int fd);
#endif
Binary file not shown.
+78
View File
@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/29 19:11:52 by hexplor #+# #+# */
/* Updated: 2023/12/26 16:06:47 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
static int ft_ptr_format(va_list parameters_info)
{
unsigned long long ptr_check;
int count;
count = 0;
ptr_check = va_arg(parameters_info, unsigned long long);
if (ptr_check == 0)
count += pf_putstr_fd("(nil)", 1);
else
{
count += pf_putstr_fd("0x", 1);
count += pf_putptr_fd(ptr_check, 1);
}
return (count);
}
static int ft_format_c(char s, va_list parameters_info)
{
int count;
count = 0;
if (s == 'c')
count += pf_putchar_fd(va_arg(parameters_info, int), 1);
else if (s == 'i' || s == 'd')
count += pf_putnbr_fd(va_arg(parameters_info, long), 1);
else if (s == 'u')
count += pf_put_unsigned_nbr_fd(va_arg(parameters_info, \
unsigned int), 1);
else if (s == 's')
count += pf_putstr_fd(va_arg(parameters_info, char *), 1);
else if (s == 'x')
count += pf_puthex_lower_fd(va_arg(parameters_info, long), 1);
else if (s == 'X')
count += pf_puthex_upper_fd(va_arg(parameters_info, long), 1);
else if (s == 'p')
count += ft_ptr_format(parameters_info);
else if (s == '%')
count += pf_putchar_fd('%', 1);
return (count);
}
int ft_printf(const char *s, ...)
{
va_list parameters_info;
unsigned int index;
int count;
index = 0;
count = 0;
va_start(parameters_info, s);
while (s[index])
{
if (s[index] == '%')
{
index++;
count += ft_format_c(s[index], parameters_info);
}
else if (s[index] != '%' && s[index])
count += pf_putchar_fd(s[index], 1);
index++;
}
va_end(parameters_info);
return (count);
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_unsigned_nbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/02 23:56:02 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:23:28 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_put_unsigned_nbr_fd(unsigned int n, int fd)
{
int count;
count = 0;
if (n >= 10)
{
count += pf_put_unsigned_nbr_fd(n / 10, fd);
count += pf_put_unsigned_nbr_fd(n % 10, fd);
}
else
{
count += pf_putchar_fd(n + '0', fd);
}
return (count);
}
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:08:31 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:23:41 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
return (1);
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex_lower_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/02 15:23:28 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:24:01 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_puthex_lower_fd(unsigned int n, int fd)
{
int i;
char *hex;
i = 0;
hex = "0123456789abcdef";
if (n >= 16)
{
i += pf_puthex_lower_fd(n / 16, fd);
i += pf_puthex_lower_fd(n % 16, fd);
}
else
i += pf_putchar_fd(hex[n], fd);
return (i);
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex_upper_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/02 15:27:12 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:24:21 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_puthex_upper_fd(unsigned int n, int fd)
{
int i;
char *hex;
i = 0;
hex = "0123456789ABCDEF";
if (n >= 16)
{
i += pf_puthex_upper_fd(n / 16, fd);
i += pf_puthex_upper_fd(n % 16, fd);
}
else
i += pf_putchar_fd(hex[n], fd);
return (i);
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 23:41:52 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:24:42 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_putnbr_fd(long int n, int fd)
{
int count;
count = 0;
if (n < 0)
{
n = -n;
count += pf_putchar_fd('-', fd);
}
if ((n / 10) > 0)
count += pf_putnbr_fd(n / 10, fd);
pf_putchar_fd((n % 10) + 48, fd);
return (count + 1);
}
Binary file not shown.
+32
View File
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/02 15:14:08 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:25:02 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_putptr_fd(unsigned long long n, int fd)
{
int len;
len = 0;
if (n >= 16)
{
len += pf_putptr_fd(n / 16, fd);
len += pf_putptr_fd(n % 16, fd);
}
else
{
if (n < 10)
len += pf_putchar_fd(n + '0', fd);
else
len += pf_putchar_fd(n + 'a' - 10, fd);
}
return (len);
}
Binary file not shown.
+33
View File
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:24:20 by hexplor #+# #+# */
/* Updated: 2023/12/26 15:25:19 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../header/ft_printf.h"
int pf_putstr_fd(char *s, int fd)
{
unsigned int i;
int count;
if (!s)
{
pf_putstr_fd("(null)", fd);
return (6);
}
i = 0;
count = 0;
while (*(s + i))
{
count += pf_putchar_fd(*(s + i), fd);
i++;
}
return (count);
}
Binary file not shown.
+31
View File
@@ -0,0 +1,31 @@
SOURCES = ft_putnbr_fd.c ft_putendl_fd.c ft_putstr_fd.c ft_putchar_fd.c ft_striteri.c ft_strmapi.c ft_itoa.c ft_split.c ft_strtrim.c ft_strjoin.c ft_substr.c ft_strdup.c ft_calloc.c ft_atoi.c ft_strnstr.c ft_memcmp.c ft_memchr.c ft_strncmp.c ft_strrchr.c ft_strchr.c ft_tolower.c ft_toupper.c ft_strlcpy.c ft_strlen.c ft_isprint.c ft_strlcat.c ft_memmove.c ft_memcpy.c ft_bzero.c ft_isalpha.c ft_isdigit.c ft_memset.c ft_isalnum.c ft_isascii.c
OBJECTS = $(SOURCES:.c=.o)
EXTRA = a.out a.out.dSYM main.c
NAME = libft.a
COMPILER = cc
FLAGS = -c -Wall -Werror -Wextra -I -g3
all : $(NAME)
$(NAME) : $(OBJECTS)
@ar -rcs libft.a *.o
$(OBJECTS) : $(SOURCES)
$(COMPILER) $(FLAGS) $(SOURCES)
clean :
@rm -f $(OBJECTS)
fclean : clean
@rm -rf $(NAME) $(EXTRA)
re :
@make fclean
@make all
.PHONY: fclean clean all re
+106
View File
@@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 15:57:24 by hexplor #+# #+# */
/* Updated: 2023/11/27 22:23:38 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
if (c == '\t' || c == '\n' || c == '\v' \
|| c == '\f' || c == '\r' || c == ' ')
return (1);
return (0);
}
static int ft_fsign(char c)
{
if (c == '-')
return (-1);
return (1);
}
int ft_atoi(const char *text)
{
long sign;
long number;
number = 0;
sign = 1;
while (ft_isspace(*text) == 1)
text++;
if (*text == '-' || *text == '+')
{
sign = ft_fsign(*text);
text++;
}
while (ft_isdigit(*text) == 1)
{
number *= 10;
number += *text - '0';
text++;
}
return (sign * number);
}
/*
#include <stdlib.h>
#include <limits.h>
int main(void)
{
char *texte1 = "12";
char *texte2 = "+12";
char *texte3 = "-12";
char *texte4 = " \t\n\v\f\r12";
char *texte5 = " \t\n\v\f\r+12";
char *texte6 = " \t\n\v\f\r-12";
char *texte7 = "--12";
char *texte8 = "++12";
char *texte9 = "abcd7";
char *texte10 = "12abs";
char *texte11 = "";
char *texte12 = "0";
char *texte13 = "-2147483649";
char *texte14 = "2147483649";
if (ft_atoi(texte1) != 12)
printf("1: NON\n");
if(ft_atoi(texte2) != 12)
printf("2: NON\n");
if(ft_atoi(texte3) != -12)
printf("3: NON\n");
if(ft_atoi(texte4) != 12)
printf("4: NON\n");
if(ft_atoi(texte5) != 12)
printf("5: NON\n");
if(ft_atoi(texte6) != -12)
printf("6: NON\n");
if(ft_atoi(texte7) != 0)
printf("7: NON\n");
if(ft_atoi(texte8) != 0)
printf("8: NON\n");
if(ft_atoi(texte9) != 0)
printf("9: NON\n");
if(ft_atoi(texte10) != 12)
printf("10: NON\n");
if(ft_atoi(texte11) != 0)
printf("11: NON\n");
if(ft_atoi(texte12) != 0)
printf("12: NON\n");
if(ft_atoi(texte13) != -2147483649L)
printf("13: NON\n");
if(ft_atoi(texte14) != 2147483649L)
printf("14: NON\n");
write(1, "\n", 1);
return (0);
}*/
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:47:55 by yantoine #+# #+# */
/* Updated: 2023/11/09 15:02:31 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdio.h>
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
if (n <= 0)
return ;
ft_memset(s, 0, n);
}
/*int main()
{
void mess[10] = "oui";
ft_bzero()
return (0);
}*/
Binary file not shown.
+42
View File
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 15:24:33 by hexplor #+# #+# */
/* Updated: 2023/11/30 08:53:17 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t size)
{
char *tab;
size_t sizemax;
sizemax = n * size;
if (size && sizemax / size < n)
return (NULL);
if (n == 0 || size == 0)
{
tab = malloc(1);
return (tab);
}
tab = malloc(n * size);
if (!tab)
return (NULL);
ft_bzero(tab, n * size);
return (tab);
}
/*#include <limits.h>
int main()
{
void *p;
p = ft_calloc(SIZE_MAX, SIZE_MAX);
free(p);
return (0);
}*/
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:17:01 by yantoine #+# #+# */
/* Updated: 2023/11/08 08:05:51 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isalpha(c) == 1 || ft_isdigit(c) == 1)
return (1);
else
return (0);
}
Binary file not shown.
+21
View File
@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:02:38 by yantoine #+# #+# */
/* Updated: 2023/11/08 08:06:13 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 65 && c <= 90) \
|| (c >= 97 && c <= 122))
return (1);
else
return (0);
}
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:17:41 by yantoine #+# #+# */
/* Updated: 2023/11/08 08:06:30 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
else
return (0);
}
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:06:07 by yantoine #+# #+# */
/* Updated: 2023/11/08 08:06:49 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
if ((c >= 48 && c <= 57))
return (1);
else
return (0);
}
Binary file not shown.
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 12:37:20 by yantoine #+# #+# */
/* Updated: 2023/11/30 09:02:47 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}
Binary file not shown.
+104
View File
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/22 15:16:11 by hexplor #+# #+# */
/* Updated: 2023/11/30 07:44:52 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_n_digit(int n)
{
long index;
if (n == 0)
return (1);
index = 0;
while (n != 0)
{
n /= 10;
index++;
}
return (index);
}
static int ft_pow(int n)
{
long index;
index = 1;
while (n != 0)
{
index *= 10;
n--;
}
return (index / 10);
}
static void ft_set_number(char *tab)
{
long index;
index = 0;
while (index != 11)
{
tab[index] = index + '0';
index++;
}
tab[index] = '\0';
}
static void ft_set_digit(long nbr_digit, long index, char *nbr, long n)
{
char digit[15];
long pow;
if (n == 0)
{
*nbr = '0';
*(nbr + 1) = 0;
return ;
}
ft_set_number(digit);
pow = ft_pow(nbr_digit);
while (nbr_digit != 0)
{
*(nbr + index++) = digit[(n / pow) % 10];
nbr_digit--;
pow /= 10;
}
*(nbr + index++) = '\0';
}
char *ft_itoa(int n)
{
char *nbr;
long index;
long n2;
long nbr_digit;
n2 = (long) n;
nbr_digit = ft_n_digit(n);
index = 0;
if (n2 < 0)
{
nbr = ft_calloc((nbr_digit + 2), 1);
if (!nbr)
return (NULL);
*(nbr + index++) = '-';
n2 *= -1;
}
else
{
nbr = ft_calloc((nbr_digit + 1), 1);
if (!nbr)
return (NULL);
}
ft_set_digit(nbr_digit, index, nbr, n2);
return (nbr);
}
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 09:03:40 by yantoine #+# #+# */
/* Updated: 2023/11/13 16:24:52 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *p;
size_t index;
p = (unsigned char *)s;
index = 0;
while (p[index] && index < n)
{
if (*(p + index) == (unsigned char) c)
return ((void *)(p + index));
index++;
}
return (NULL);
}
Binary file not shown.
+37
View File
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 10:08:42 by yantoine #+# #+# */
/* Updated: 2023/11/13 16:23:41 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *p1;
unsigned char *p2;
size_t index;
index = 0;
p1 = (unsigned char *)s1;
p2 = (unsigned char *)s2;
while (index < n)
{
if (*(p1 + index) != *(p2 + index))
return ((*(p1 + index) - *(p2 + index)));
index++;
}
return (0);
}
/*int main()
{
ft_memcmp("001270", "coucou", 7);
return (0);
}*/
Binary file not shown.
+26
View File
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 11:03:13 by yantoine #+# #+# */
/* Updated: 2023/11/09 12:15:15 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
size_t index;
index = 0;
while (index < n)
{
*(unsigned char *)(dest + index) = *(unsigned char *)(src + index);
index++;
}
return (dest);
}
Binary file not shown.
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 15:13:56 by hexplor #+# #+# */
/* Updated: 2023/11/09 12:45:23 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t index;
int index2;
if (dest > src)
{
index2 = n - 1;
while (index2 >= 0)
{
*(unsigned char *)(dest + index2) = \
*(unsigned char *)(src + index2);
index2--;
}
}
else
{
index = 0;
while (index < n)
{
*(unsigned char *)(dest + index) = *(unsigned char *)(src + index);
index++;
}
}
return (dest);
}
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:33:18 by yantoine #+# #+# */
/* Updated: 2023/11/09 14:55:20 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdio.h>
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
size_t index;
index = 0;
while (index < n)
{
*(unsigned char *)(s + index) = c;
index++;
}
return (s);
}
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:08:31 by hexplor #+# #+# */
/* Updated: 2023/11/23 15:04:00 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
Binary file not shown.
+19
View File
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:34:12 by hexplor #+# #+# */
/* Updated: 2023/11/24 17:45:40 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}
Binary file not shown.
+87
View File
@@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:41:48 by hexplor #+# #+# */
/* Updated: 2023/11/27 22:27:06 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_n_digit(int n)
{
long index;
index = 0;
while (n != 0)
{
n /= 10;
index++;
}
return (index);
}
static int ft_pow(int n)
{
long index;
index = 1;
while (n != 0)
{
index *= 10;
n--;
}
return (index / 10);
}
static void ft_set_number(char *tab)
{
long index;
index = 0;
while (index != 11)
{
tab[index] = index + '0';
index++;
}
tab[index] = '\0';
}
static void ft_set_digit(long nbr_digit, long n, int fd)
{
char digit[15];
long pow;
if (n == 0)
{
ft_putchar_fd('0', fd);
return ;
}
ft_set_number(digit);
pow = ft_pow(nbr_digit);
while (nbr_digit != 0)
{
ft_putchar_fd(digit[(n / pow) % 10], fd);
nbr_digit--;
pow /= 10;
}
}
void ft_putnbr_fd(int n, int fd)
{
long n2;
long nbr_digit;
n2 = (long) n;
nbr_digit = ft_n_digit(n);
if (n2 < 0)
{
ft_putchar_fd('-', fd);
n2 *= -1;
}
ft_set_digit(nbr_digit, n2, fd);
}
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 14:24:20 by hexplor #+# #+# */
/* Updated: 2023/11/23 14:32:00 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
unsigned int i;
i = 0;
while (*(s + i))
{
ft_putchar_fd(*(s + i), fd);
i++;
}
}
Binary file not shown.
+108
View File
@@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:06:15 by hexplor #+# #+# */
/* Updated: 2023/11/29 13:19:56 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_word(char *str, char c)
{
int count;
count = 0;
while (*str)
{
while (*str == c)
str++;
if (*str != c && *str)
count++;
while (*str && *str != c)
str++;
}
return (count);
}
static size_t len_word(char *s, char c, size_t start)
{
size_t i;
i = 0;
while (s[start] && s[start] != c)
{
i++;
start++;
}
return (i);
}
static void free_array(char **s1)
{
size_t i;
i = 0;
while (s1[i])
{
free(s1[i]);
i++;
}
free(s1);
}
static char **divisor(char *str1, char c, char **array, size_t len)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (i < len)
{
while (str1[j] && str1[j] == c)
j++;
array[i] = ft_substr(str1, j, len_word(str1, c, j));
if (!array[i])
{
free_array(array);
return (NULL);
}
while (str1[j] && str1[j] != c)
j++;
i++;
}
array[i] = NULL;
return (array);
}
char **ft_split(char const *s, char c)
{
size_t words;
char **array;
if (!s)
return (NULL);
words = count_word((char *)s, c);
array = (char **)malloc((words + 1) * (sizeof(char *)));
if (!array)
return (NULL);
array = divisor((char *)s, c, array, words);
return (array);
}
/*
int main()
{
char **list;
list = ft_split("--1-2--3---4----5-----42", '-');
for (int i = 0; list[i] != NULL; ++i)
free(list[i]);
free(list);
return (0);
}
*/
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 12:07:02 by hexplor #+# #+# */
/* Updated: 2023/11/29 15:22:10 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
char *chaine;
chaine = (char *)s;
while (*chaine)
{
if (*chaine == (unsigned char)c)
return (chaine);
chaine++;
}
if (c == '\0')
return (chaine);
return (NULL);
}
Binary file not shown.
+41
View File
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 15:58:53 by hexplor #+# #+# */
/* Updated: 2023/11/29 14:52:09 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *s2;
int index;
s2 = (char *)ft_calloc(ft_strlen(s1) + 1, 1);
if (s2 == NULL)
return (NULL);
index = 0;
while (*(s1 + index))
{
*(s2 + index) = *(s1 + index);
index++;
}
*(s2 + index) = '\0';
return (s2);
}
/*int main()
{
char *dest;
dest = ft_strdup("BONJOUR");
dest = ft_strdup("");
dest = ft_strdup("\0");
return (0);
}*/
Binary file not shown.
+26
View File
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 12:50:39 by hexplor #+# #+# */
/* Updated: 2023/11/23 13:54:15 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
unsigned int i;
i = 0;
while (*s)
{
f(i, s);
i++;
s++;
}
}
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:21:11 by hexplor #+# #+# */
/* Updated: 2023/11/30 08:49:26 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t len;
char *str;
len = ft_strlen(s1);
len += ft_strlen(s2);
str = (char *)ft_calloc((len + 1), 1);
if (!str)
return (NULL);
ft_memmove(str, s1, ft_strlen(s1));
ft_memmove((str + ft_strlen(s1)), s2, ft_strlen(s2));
return (str);
}
Binary file not shown.
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:02:08 by hexplor #+# #+# */
/* Updated: 2023/11/10 09:54:44 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t k;
i = 0;
k = 0;
while (dest[i] && i < size)
i++;
while (src[k] && (i + k + 1) < size)
{
dest[i + k] = src[k];
k++;
}
if (i < size)
dest[i + k] = '\0';
return (i + ft_strlen(src));
}
/*int main()
{
char mess1[10] = "oui";
char mess2[10] = "non";
ft_strlcat(mess1, mess2, 3);
return (0);
}*/
Binary file not shown.
+43
View File
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 17:27:30 by hexplor #+# #+# */
/* Updated: 2023/11/09 20:22:10 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t c;
size_t i;
c = 0;
while (src[c] != '\0')
c++;
i = 0;
if (size != 0)
{
while (src[i] != '\0' && i < (size - 1))
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
return (c);
}
/*int main()
{
char dest[10] = "oui";
char src[3] = "non";
ft_strlcpy(dest, src, 10);
return (0);
}*/
Binary file not shown.
+22
View File
@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 10:18:42 by yantoine #+# #+# */
/* Updated: 2023/11/09 12:31:17 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *c)
{
int index;
index = 0;
while (c[index] != 0)
index += 1;
return (index);
}
Binary file not shown.
+31
View File
@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 11:52:03 by hexplor #+# #+# */
/* Updated: 2023/11/30 09:04:01 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *str;
i = 0;
str = ft_calloc((ft_strlen(s) + 1), 1);
if (!str)
return (0);
while (*(s + i))
{
*(str + i) = f(i, *(s + i));
i++;
}
*(str + i) = '\0';
return (str);
}
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 08:46:16 by yantoine #+# #+# */
/* Updated: 2023/11/30 09:28:36 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t index;
index = 0;
while (*(unsigned char *)(s1 + index) && \
*(unsigned char *)(s2 + index) && index < n)
{
if (*(s1 + index) != *(s2 + index))
return (*(s1 + index) - *(s2 + index));
index++;
}
return (0);
}
Binary file not shown.
+46
View File
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 12:16:17 by yantoine #+# #+# */
/* Updated: 2023/11/27 22:29:37 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
char *b;
b = (char *) big;
i = 0;
if (little[i] == '\0' || little == NULL)
return (b);
if (len == 0)
return (0);
while (b[i] != '\0' && i < len)
{
j = 0;
while (little[j] && (i + j) < len)
{
if (b[i + j] == little[j])
j++;
else
break ;
}
if (little[j] == 0)
return (&b[i]);
i++;
}
return (0);
}
/*int main()
{
ft_strnstr("aaabcabcd", "abcd", -1);
return (0);
}*/
Binary file not shown.
+34
View File
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 08:02:01 by yantoine #+# #+# */
/* Updated: 2023/11/29 14:34:30 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int len;
len = 0;
while (*s)
{
len++;
s++;
}
if (c == '\0' && *s == '\0')
return ((char *)s);
while (len >= 0)
{
if (*s == (unsigned char)c)
return ((char *)s);
s--;
len--;
}
return (NULL);
}
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 13:48:16 by hexplor #+# #+# */
/* Updated: 2023/11/29 13:09:29 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
i = 0;
j = ft_strlen(s1) - 1;
while (s1[i] && ft_strchr(set, s1[i]))
i++;
while (s1[j] && ft_strchr(set, s1[j]))
j--;
return (ft_substr(s1, i, (j - i + 1)));
}
Binary file not shown.
+47
View File
@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 20:04:44 by hexplor #+# #+# */
/* Updated: 2023/11/30 08:45:42 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
if (!s)
return (NULL);
i = 0;
if (len > ft_strlen(s) - start)
{
len = ft_strlen(s) - start;
}
if (start >= ft_strlen(s))
return (ft_strdup(""));
str = (char *)malloc(((sizeof(char) * (len + 1))));
if (!str)
return (NULL);
while (i < len && s[start] != '\0')
{
str[i++] = s[start++];
}
str[i] = '\0';
return (str);
}
/*
int main()
{
char *text;
text = ft_substr("tripouille", 100, 1);
free(text);
return (0);
}*/
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 10:09:46 by hexplor #+# #+# */
/* Updated: 2023/11/10 10:13:40 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 97 - 32 && c <= 122 - 32)
return (c + 32);
return (c);
}
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 10:05:42 by hexplor #+# #+# */
/* Updated: 2023/11/10 10:09:20 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
return (c - 32);
return (c);
}
Binary file not shown.
BIN
View File
Binary file not shown.
+89
View File
@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 09:46:33 by yantoine #+# #+# */
/* Updated: 2023/11/23 14:41:28 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <limits.h>
void *ft_calloc(size_t count, size_t size);
void ft_putnbr_fd(int n, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void *ft_memchr(const void *s, int c, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
size_t ft_strlcat(char *dest, const char *src, size_t size);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
size_t ft_strlen(const char *c);
char **ft_split(char const *s, char c);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
char *ft_itoa(int n);
char *ft_strtrim(char const *s1, char const *set);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strdup(const char *s1);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *str);
int ft_memcmp(const void *s1, const void *s2, size_t n);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_isprint(int c);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
#endif
+52
View File
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 16:51:00 by yantoine #+# #+# */
/* Updated: 2024/05/17 16:13:47 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void pausing(int signum)
{
(void)signum;
}
static void send_bits(int pid, char c)
{
int bit;
usleep(50);
bit = 0;
while (bit < 8)
{
if ((c & (0x01 << bit)) != 0)
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
signal(SIGUSR1, pausing);
pause();
bit++;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 3 || !ft_strlen(argv[2]) || ft_atoi(argv[1]) <= 0)
return (1);
i = 0;
while (argv[2][i] != '\0')
{
send_bits(ft_atoi(argv[1]), argv[2][i]);
i++;
}
send_bits(ft_atoi(argv[1]), '\n');
send_bits(ft_atoi(argv[1]), '\0');
}
+127
View File
@@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hexplor <hexplor@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 22:48:59 by yantoine #+# #+# */
/* Updated: 2024/05/13 21:22:13 by hexplor ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
char *g_str;
static void print_hello(void)
{
ft_printf(" , ----.\n");
ft_printf(" - - `\n");
ft_printf(" ,__.,' \\\n");
ft_printf(" .' *`\\\n");
ft_printf(" / | | / **\\\n");
ft_printf(" . / ****.\n");
ft_printf(" | mm | ****|\n");
ft_printf(" \\ | ****|\n");
ft_printf(" ` ._______ \\ ****/\n");
ft_printf(" \\ /`---'\n");
ft_printf(" \\___(\n");
ft_printf(" /~~~~\\\n");
ft_printf(" / \\\n");
ft_printf(" / | \\\n");
ft_printf(" | | \\\n");
ft_printf(" , ~~ . |, ~~ . | |\\\n");
ft_printf(" ( |||| ) ( |||| )(,,,)`\n");
ft_printf(" ( |||||| )-( |||||| ) | ^\n");
ft_printf(" ( |||||| ) ( |||||| ) |'/\n");
ft_printf(" ( |||||| )-( |||||| )___,'-\n");
ft_printf(" ( |||| ) ( |||| )\n");
ft_printf(" ` ~~ ' ` ~~ '\n");
}
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
static char *add_char_to_string(char *str, char c)
{
int len;
char *new_str;
len = 0;
new_str = NULL;
if (str == NULL)
{
str = (char *)malloc(2 * sizeof(char));
str[0] = c;
str[1] = '\0';
}
else
{
len = ft_strlen(str);
new_str = (char *)malloc((len + 2) * sizeof(char));
ft_strcpy(new_str, str);
new_str[len] = c;
new_str[len + 1] = '\0';
free(str);
str = new_str;
}
return (str);
}
static void action(int signum, siginfo_t *info, void *context)
{
static int current_byte;
static int bit_count;
(void)context;
if (signum == SIGUSR1)
current_byte |= (0x01 << bit_count);
bit_count++;
if (bit_count == 8)
{
if (current_byte == '\0')
{
ft_printf("%s", g_str);
free(g_str);
g_str = NULL;
kill(info->si_pid, SIGUSR1);
current_byte = 0;
bit_count = 0;
return ;
}
g_str = add_char_to_string(g_str, current_byte);
current_byte = 0;
bit_count = 0;
}
usleep(50);
kill(info->si_pid, SIGUSR1);
}
int main(void)
{
struct sigaction sa;
print_hello();
sa.sa_sigaction = action;
sa.sa_flags = SA_SIGINFO;
ft_printf("pid: %d\n", getpid());
while (1)
{
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
pause();
}
return (0);
}
File diff suppressed because one or more lines are too long
+231
View File
@@ -0,0 +1,231 @@
#!/bin/bash
###################################################
################# Minitalk Tester #################
echo -e "\n\033[1;34m========= MINITALK TESTER =========\033[0m\n"
# Correct paths if needed
minitalk_path="../"
results_path=results/
client_file=results/client.out.txt
server_file=results/server.out.txt
error_file=results/error.log
KO="[\033[31mKO\033[0m]"
OK="[\033[32mOK\033[0m]"
ERROR="[\033[33mERROR\033[0m]"
rm -rf $results_path 2> /dev/null
mkdir $results_path 2> /dev/null
###################################################
################### Compilation ###################
cd $minitalk_path
make re > /dev/null 2> /dev/null
# check if a program have been correctly creates
test_compile() {
ls $1 > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
echo -e "$1 \033[32m✔\033[0m"
else
echo -e "$1 \033[31mx\033[0m"
echo -e "Compilation $KO\n"
echo -e "\033[1mMinitalk failed ❌\033[0m\n"
make fclean > /dev/null 2> /dev/null
cd $tester_folder
exit 1
fi
}
test_compile client
test_compile server
echo ""
cd - > /dev/null 2> /dev/null
###################################################
#################### Execution ####################
# Execution of server
$minitalk_path/server > $server_file & server_pid=$!
sleep 1
# Check if the string has been received by the server
check_string(){
cat $server_file | grep "$@" > /dev/null 2> /dev/null
ret=$?
if [ $ret -eq 1 ]
then
test_ok=1
elif [ $ret -eq 2 ]
then
test_ok=2
else
test_ok=0
fi
}
# Execute client and check the transmission
tester_ok=0
test_transmission() {
# test_ok=0
$minitalk_path/client $server_pid "$@" > $client_file 2>> $client_file
check_string "$@"
if [ $test_ok -eq 0 ]
then
echo -en $OK
elif [ $test_ok -eq 1 ]
then
tester_ok=1
echo -en $KO
else
echo -en $ERROR
fi
}
###################################################
###################### Tests ######################
# Manual test
# Usage:./minitalk_tester.sh [string to pass]
if [ $# -ne 0 ]
then
echo -e "\t\033[2m## Client output ##\033[0m"
str="$@"
$minitalk_path/client $server_pid "$str"
kill $server_pid 2> /dev/null
wait $server_pid 2> /dev/null
echo -e "\n\t\033[2m## Server output ##\033[0m"
cat $server_file
len=${#str}
echo -e "\n____________________________________"
echo -e "\nstring size = $len\n"
rm -rf $results_path 2> /dev/null
cd $minitalk_path
make fclean > /dev/null 2> /dev/null
exit 0 2> /dev/null
fi
echo -e "\t\033[2m## Tests ##\033[0m\n"
# Tests of 10 characters
echo "10 characters:"
test_transmission "Hello World"
test_transmission "0123456789"
test_transmission "qwertyuiop"
test_transmission "zxcvbnm,./"
echo -e "\n"
# Tests of 100 characters
echo "100 characters:"
test_transmission "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, to"
test_transmission "The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog. Junk MTV quiz graced"
test_transmission "Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musi"
test_transmission "A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring whi"
echo -e "\n"
# Tests of 1000 characters
echo "1000 characters:"
test_transmission "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. \
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec \
quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec \
pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, \
venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. \
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat \
vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra \
nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur \
ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, \
sem quam semper libero, sit amet adipiscing sem neque sed ipsum. N"
test_transmission "One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his \
bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see \
his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover \
it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest \
of him, waved about helplessly as he looked. \"What's happened to me?\" he thought. It wasn't a dream. His room, \
a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of \
textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture \
that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted \
out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards t"
test_transmission "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and \
I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, \
the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but \
because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor \
again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally \
circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us \
ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault \
with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no \
resultant pleasure? On the other hand, we denounc"
test_transmission "The European languages are members of the same family. Their separate existence is a myth. For science, music, \
sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their \
most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive \
translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If \
several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual \
languages. The new common language will be more simple and regular than the existing European languages. It will be as \
simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a \
skeptical Cambridge friend of mine told me what Occidental is. The European languages are members of the same family. \
Their separate existence is a myth. For science, music, spo"
echo -e "\n"
# Test of a big string
bigstr=`cat bigTest.txt`
len=${#bigstr}
echo "$len characters:"
$minitalk_path/client $server_pid "$bigstr" > $client_file 2>> $client_file
check_string "$bigstr"
if [ $test_ok -eq 0 ]
then
echo -e $OK
elif [ $test_ok -eq 1 ]
then
echo -e $KO
tester_ok=1
else
echo -e $ERROR
fi
echo ""
###################################################
######################## END ######################
# Stop the server
kill $server_pid 2> /dev/null
wait $server_pid 2> /dev/null
# Display result ans clean up
if [ $tester_ok -ne 0 ]
then
echo -e "\033[1mMinitalk failed ❌\033[0m"
echo -e "Check \033[4m$server_file\033[0m\n"
else
echo -e "\033[1mMinitalk succeed ✅\033[0m\n"
rm -rf $results_path 2> /dev/null
cd $minitalk_path
make fclean > /dev/null 2> /dev/null
cd - > /dev/null 2> /dev/null
fi
exit 0
Binary file not shown.

After

Width:  |  Height:  |  Size: 499 KiB