This commit is contained in:
Ladebeze66 2023-12-12 17:35:30 +01:00
commit 729c3a52f7
63 changed files with 2155 additions and 0 deletions

80
Makefile Executable file
View File

@ -0,0 +1,80 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/24 16:08:54 by fgras-ca #+# #+# #
# Updated: 2023/07/24 16:55:37 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
NAME = client
NAMES = server
BONUS_NAME = client_bonus
BONUS_NAMES = server_bonus
LIBFT = libft.a
PRINTF = libftprintf.a
SRC_CLIENT = client.c
SRC_SERVER = server.c
BONUS_CLIENT = client_bonus.c
BONUS_SERVER = server_bonus.c
SRC_DIR_LIBFT = libft/
SRC_DIR_PRINTF = printf/
SRC_LIBFT = $(addprefix $(SRC_DIR_LIBFT), $(LIBFT))
SRC_PRINTF = $(addprefix $(SRC_DIR_PRINTF), $(PRINTF))
OBJC = $(SRC_CLIENT:.c=.o)
OBJS = $(SRC_SERVER:.c=.o)
OBJCB = $(BONUS_CLIENT:.c=.o)
OBJSB = $(BONUS_SERVER:.c=.o)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Werror -Wextra
all: $(NAME) $(NAMES)
$(NAME) : $(OBJC)
@make -C printf
@make -C libft
$(CC) $(CLFAGS) $(OBJC) $(SRC_PRINTF) $(SRC_LIBFT) -o $(NAME)
$(NAMES) : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(SRC_PRINTF) $(SRC_LIBFT) -o $(NAMES)
bonus : $(BONUS_NAME) $(BONUS_NAMES)
$(BONUS_NAME) : $(OBJCB)
@make -C printf
@make -C libft
$(CC) $(CFLAGS) $(OBJCB) $(SRC_PRINTF) $(SRC_LIBFT) -o $(BONUS_NAME)
$(BONUS_NAMES) : $(OBJSB)
$(CC) $(CFLAGS) $(OBJSB) $(SRC_PRINTF) $(SRC_LIBFT) -o $(BONUS_NAMES)
clean :
@make clean -C printf
@make clean -C libft
$(RM) $(OBJC)
$(RM) $(OBJS)
$(RM) $(OBJCB)
$(RM) $(OBJSB)
fclean : clean
@make fclean -C printf
@make fclean -C libft
$(RM) $(NAME)
$(RM) $(NAMES)
$(RM) $(BONUS_NAME)
$(RM) $(BONUS_NAMES)
$(RM) $(PRINTF)
$(RM) $(LIBFT)
re : fclean all
.PHONY : all bonus clean fclean re

53
client.c Executable file
View File

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 16:12:18 by fgras-ca #+# #+# */
/* Updated: 2023/07/27 13:27:39 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void sigusr1_handler(int sig)
{
(void)sig;
g_acknowledged = 1;
}
static void send_char(int pid, char c)
{
int i;
i = 8;
while (i--)
{
g_acknowledged = 0;
if ((c >> i) & 1)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(100);
while (!g_acknowledged)
pause();
}
}
static void mt_kill(int pid, char *str)
{
signal(SIGUSR1, sigusr1_handler);
while (*str)
send_char(pid, *str++);
send_char(pid, '\0');
}
int main(int argc, char **argv)
{
if (argc != 3 || !ft_strlen(argv[2]))
return (1);
mt_kill(ft_atoi(argv[1]), argv[2]);
return (0);
}

60
client_bonus.c Executable file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 22:37:50 by fgras-ca #+# #+# */
/* Updated: 2023/07/27 13:54:38 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
void sigusr1_handler(int sig)
{
(void)sig;
g_acknowledged = 1;
}
void message_received_handler(int sig)
{
(void)sig;
ft_putstr_fd("server >> message received.\n", 1);
}
static void send_char(int pid, char c)
{
int i;
i = 8;
while (i--)
{
g_acknowledged = 0;
if ((c >> i) & 1)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(100);
while (!g_acknowledged)
pause();
}
}
static void mt_kill(int pid, char *str)
{
signal(SIGUSR1, sigusr1_handler);
signal(SIGUSR2, message_received_handler);
while (*str)
send_char(pid, *str++);
send_char(pid, '\0');
}
int main(int argc, char **argv)
{
if (argc != 3 || !ft_strlen(argv[2]))
return (1);
mt_kill(ft_atoi(argv[1]), argv[2]);
return (0);
}

194
fr.subject.pdf Normal file
View File

@ -0,0 +1,194 @@
Minitalk
Résumé:
Ce projet a pour but de vous faire réaliser un petit programme déchange de données
utilisant les signaux UNIX.
Version: 3
Table des matières
I Préambule 2
II Règles communes 3
III Consignes spécifiques au projet 4
IV Partie obligatoire 5
V Partie bonus 6
VI Rendu et peer-evaluation 7
1
Chapitre I
Préambule
Voici les paroles du générique de Firefly :
Take my love, take my land
Take me where I cannot stand
I dont care, Im still free
You cant take the sky from me.
Take me out to the black
Tell them I aint comin back
Burn the land and boil the sea
You cant take the sky from me.
Leave the men where they lay
Theyll never see another day
Lost my soul, lost my dream
You cant take the sky from me.
I feel the black reaching out
I hear its song without a doubt
I still hear and I still see
That you cant take the sky from me.
Lost my love, lost my land
Lost the last place I could stand
Theres no place I can be
Since Ive found Serenity
And you cant take the sky from me.
Ce projet est plus facile si vous avez regardé lintégralité de Firefly.
2
Chapitre II
Règles communes
• Votre projet doit être écrit en C.
• Votre projet doit être codé à la Norme. Si vous avez des fichiers ou fonctions bonus,
celles-ci seront inclues dans la vérification de la norme et vous aurez 0 au projet
en cas de faute de norme.
• Vos fonctions ne doivent pas sarrêter de manière inattendue (segmentation fault,
bus error, double free, etc) mis à part dans le cas dun comportement indéfini. Si
cela arrive, votre projet sera considéré non fonctionnel et vous aurez 0 au projet.
• Toute mémoire allouée sur la heap doit être libéré lorsque cest nécessaire. Aucun
leak ne sera toléré.
• Si le projet le demande, vous devez rendre un Makefile qui compilera vos sources
pour créer la sortie demandée, en utilisant les flags -Wall, -Wextra et -Werror.
Votre Makefile ne doit pas relink.
• Si le projet demande un Makefile, votre Makefile doit au minimum contenir les
règles $(NAME), all, clean, fclean et re.
• Pour rendre des bonus, vous devez inclure une règle bonus à votre Makefile qui
ajoutera les divers headers, librairies ou fonctions qui ne sont pas autorisées dans
la partie principale du projet. Les bonus doivent être dans un fichier différent :
_bonus.{c/h}. Lévaluation de la partie obligatoire et de la partie bonus sont
faites séparément.
• Si le projet autorise votre libft, vous devez copier ses sources et son Makefile
associé dans un dossier libft contenu à la racine. Le Makefile de votre projet doit
compiler la librairie à laide de son Makefile, puis compiler le projet.
• Nous vous recommandons de créer des programmes de test pour votre projet, bien
que ce travail ne sera pas rendu ni noté. Cela vous donnera une chance de
tester facilement votre travail ainsi que celui de vos pairs.
• Vous devez rendre votre travail sur le git qui vous est assigné. Seul le travail déposé
sur git sera évalué. Si Deepthought doit corriger votre travail, cela sera fait à la fin
des peer-evaluations. Si une erreur se produit pendant lévaluation Deepthought,
celle-ci sarrête.
3
Chapitre III
Consignes spécifiques au projet
• Les fichiers exécutables doivent être nommés client et server.
• Vous devez rendre un Makefile qui compilera vos fichiers sources. Il ne doit pas
relink.
• Vous devez gérer les erreurs avec du bon sens. En aucun cas votre programme ne
doit quitter de manière inattendue (faute de segmentation, erreur de bus, double
free, etc.).
• Votre programme ne doit pas avoir de fuites de mémoire.
• Vous pouvez utiliser une variable globale par programme (une pour le client
et une pour le serveur) mais leur usage doit être justifié.
• Afin de faire la partie obligatoire, vous avez le droit dutiliser les fonctions sui-
vantes :
◦ write
◦ ft_printf et tout équivalent que VOUS avez codé
◦ signal
◦ sigemptyset
◦ sigaddset
◦ sigaction
◦ kill
◦ getpid
◦ malloc
◦ free
◦ pause
◦ sleep
◦ usleep
◦ exit
4
Chapitre IV
Partie obligatoire
Vous devez réaliser un programme de communication sous la forme dun client et
dun serveur.
• Le serveur doit être lancé en premier et doit, après le lancement, afficher son PID.
• Le client prend deux paramètres :
◦ Le PID du serveur.
◦ Une chaîne de caractères à transmettre.
• Le client doit communiquer au serveur la chaîne passée en paramètre.
Une fois la chaîne entièrement reçue, le serveur doit lafficher.
• Le serveur doit être capable dafficher la chaîne rapidement. Par rapidement, on
entend que si vous pensez que cest trop long, alors cest sûrement trop long.
1 seconde pour afficher 100 caractères, cest COLOSSAL !
• Votre serveur doit pouvoir recevoir des chaînes de plusieurs clients à la suite sans
nécessiter dêtre relancé.
• La communication entre vos programmes doit se faire uniquement à laide de
signaux UNIX.
• Vous ne pouvez utiliser que les deux signaux suivants : SIGUSR1 et SIGUSR2.
Le système Linux ne met PAS les signaux en file dattente lorsque
vous avez déjà un signal en attente de ce type ! Bonus ?
5
Chapitre V
Partie bonus
Liste des bonus :
• Le serveur confirme la réception de chaque message en envoyant un signal au client.
• Le support des caractères Unicode !
Les bonus ne seront évalués que si la partie obligatoire est
PARFAITE. Par parfaite, nous entendons complète et sans aucun
dysfonctionnement. Si vous navez pas réussi TOUS les points de la
partie obligatoire, votre partie bonus ne sera pas prise en compte.
6
Chapitre VI
Rendu et peer-evaluation
Rendez votre travail sur votre dépot Git comme dhabitude. Seul le travail présent
sur votre dépot sera évalué en soutenance. Vérifiez bien les noms de vos dossiers et de
vos fichiers afin que ces derniers soient conformes aux demandes du sujet.
file.bfe:VAAe8ElCrUAbXivz0ueiIpv/u/ia9PL50+HI+8/bgPKLESHlp
tPLpu0PW9zWV/LwDVaOqCRCGu6Gopk1X0i6Kn7t
7

91
libft/Makefile Executable file
View File

@ -0,0 +1,91 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/02/06 12:19:21 by fgras-ca #+# #+# #
# Updated: 2023/02/24 10:58:49 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
NAME = libft.a
SOURCES = ft_atoi.c \
ft_isalpha.c \
ft_memchr.c \
ft_memset.c \
ft_strlcat.c \
ft_strnstr.c \
ft_toupper.c \
ft_bzero.c \
ft_isascii.c \
ft_memcmp.c \
ft_strchr.c \
ft_strlcpy.c \
ft_strrchr.c \
ft_calloc.c \
ft_isdigit.c \
ft_memcpy.c \
ft_strdup.c \
ft_strlen.c \
ft_substr.c \
ft_isalnum.c \
ft_isprint.c \
ft_memmove.c \
ft_strjoin.c \
ft_strncmp.c \
ft_tolower.c\
ft_strtrim.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c \
ft_strmapi.c \
ft_striteri.c \
ft_split.c \
ft_itoa.c \
SRCBONUS = 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_lstiter.c \
ft_lstmap.c \
OBJECTS = $(SOURCES:.c=.o)
BONUS_OBJ = $(SRCBONUS:.c=.o)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Werror -Wextra
all: $(NAME)
$(NAME): $(OBJECTS)
ar rcs $(NAME) $(OBJECTS)
bonus: $(OBJECTS) $(BONUS_OBJ)
ar rcs $(NAME) $(OBJECTS) $(BONUS_OBJ)
main.o:
gcc -c main.c
ccmainlib:
$(CC) $(CFLAGS) main.c -L -lft
clean:
$(RM) $(OBJECTS) $(BONUS_OBJ)
fclean: clean
$(RM) $(NAME)
re: fclean all

38
libft/ft_atoi.c Executable file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 16:16:54 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 09:57:55 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int i;
int sign;
int res;
i = 0;
sign = 1;
res = 0;
while (nptr[i] == 32 || (nptr[i] >= 9 && nptr[i] <= 13))
i++;
if (nptr[i] == '-' || nptr[i] == '+')
{
if (nptr[i] == '-')
sign = - (1);
i++;
}
while (nptr[i] >= 48 && nptr[i] <= 57)
{
res = res * 10 + nptr[i] - 48;
i++;
}
return (res * sign);
}

25
libft/ft_bzero.c Executable file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 11:37:30 by fgras-ca #+# #+# */
/* Updated: 2023/02/20 16:07:19 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *ptr, size_t len)
{
size_t i;
i = 0;
while (i < len)
{
*(char *)(ptr + i) = 0;
i++;
}
}

28
libft/ft_calloc.c Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 17:47:38 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:45:58 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
if (!nmemb || !size)
return (malloc(0));
if ((nmemb * size) / size != nmemb)
return (NULL);
ptr = malloc(size * nmemb);
if (!ptr)
return (0);
ft_bzero(ptr, size * nmemb);
return (ptr);
}

21
libft/ft_isalnum.c Executable file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/01 16:21:11 by fgras-ca #+# #+# */
/* Updated: 2023/02/01 16:24:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57))
return (1);
else
return (0);
}

21
libft/ft_isalpha.c Executable file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/01 13:36:34 by fgras-ca #+# #+# */
/* Updated: 2023/02/01 13:58:21 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
return (1);
else
return (0);
}

20
libft/ft_isascii.c Executable file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/01 16:40:31 by fgras-ca #+# #+# */
/* Updated: 2023/02/01 16:46:11 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

21
libft/ft_isdigit.c Executable file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/01 14:46:59 by fgras-ca #+# #+# */
/* Updated: 2023/02/01 15:55:00 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
if (c >= 48 && c <= 57)
return (1);
else
return (0);
}

20
libft/ft_isprint.c Executable file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/03 12:43:03 by fgras-ca #+# #+# */
/* Updated: 2023/02/03 13:25:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

56
libft/ft_itoa.c Executable file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:53:35 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:21:07 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_len_nb(int nb)
{
int len;
len = 0;
if (nb <= 0)
len++;
while (nb)
{
len++;
nb = nb / 10;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
char *str;
long nb;
len = ft_len_nb(n);
nb = n;
str = malloc(sizeof(char) * len +1);
if (!str)
return (0);
if (nb < 0)
{
str[0] = '-';
nb = -nb;
}
if (nb == 0)
str[0] = '0';
str[len--] = '\0';
while (nb)
{
str[len] = nb % 10 + '0';
len--;
nb = nb / 10;
}
return (str);
}

29
libft/ft_lstadd_back.c Executable file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 18:08:00 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:15:14 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *temp;
if (new && lst)
{
if (*lst)
{
temp = ft_lstlast(*lst);
temp->next = new;
}
else
*lst = new;
}
}

22
libft/ft_lstadd_front.c Executable file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 18:06:54 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 17:13:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new)
{
new->next = *lst;
*lst = new;
}
}

25
libft/ft_lstclear.c Executable file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 18:19:42 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:22:17 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *temp;
while (*lst && lst)
{
temp = (*lst)->next;
ft_lstdelone(*lst, (*del));
*lst = temp;
}
}

21
libft/ft_lstdelone.c Executable file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 18:17:06 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:33:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void*))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

24
libft/ft_lstiter.c Executable file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 18:22:52 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:24:51 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

24
libft/ft_lstlast.c Executable file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 17:48:29 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:02:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
while (lst)
{
if (lst->next == NULL)
return (lst);
lst = lst->next;
}
return (lst);
}

35
libft/ft_lstmap.c Executable file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 18:25:24 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 18:29:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tmp;
t_list *res;
if (!lst || !f)
return (NULL);
res = 0;
while (lst)
{
tmp = ft_lstnew((*f)(lst->content));
if (!tmp)
{
ft_lstclear(&res, del);
return (NULL);
}
ft_lstadd_back(&res, tmp);
lst = lst->next;
}
return (res);
}

25
libft/ft_lstnew.c Executable file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 17:09:25 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 16:32:39 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *new;
new = malloc(sizeof(t_list));
if (!new)
return (0);
new->content = content;
new->next = 0;
return (new);
}

28
libft/ft_lstsize.c Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 17:17:36 by fgras-ca #+# #+# */
/* Updated: 2023/02/23 17:44:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
t_list *tmp;
tmp = lst;
i = 0;
while (tmp)
{
tmp = tmp->next;
i++;
}
return (i);
}

27
libft/ft_memchr.c Executable file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 13:42:10 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 13:03:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *)s)[i] == (unsigned char)c)
return ((void *)(s + i));
i++;
}
return (0);
}

27
libft/ft_memcmp.c Executable file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/10 14:45:56 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 13:05:03 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

32
libft/ft_memcpy.c Executable file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/04 14:43:15 by fgras-ca #+# #+# */
/* Updated: 2023/02/20 16:13:43 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
char *destmov;
char *srcmov;
size_t i;
destmov = (char *)dest;
srcmov = (char *)src;
if (!dest && !src)
return (0);
i = 0;
while (i < n)
{
destmov[i] = srcmov[i];
i++;
}
return (dest);
}

42
libft/ft_memmove.c Executable file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/07 11:11:49 by fgras-ca #+# #+# */
/* Updated: 2023/02/07 16:04:25 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
char *destmov;
char *srcmov;
size_t i;
if (!dest && !src)
return (0);
destmov = (char *)dest;
srcmov = (char *)src;
i = 0;
if (destmov > srcmov)
{
while (n--)
{
destmov[n] = srcmov[n];
}
}
else
{
while (n--)
{
destmov[i] = srcmov[i];
i++;
}
}
return (destmov);
}

26
libft/ft_memset.c Executable file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/03 15:49:51 by fgras-ca #+# #+# */
/* Updated: 2023/02/20 16:05:34 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
*(unsigned char *)(s + i) = (unsigned char)c;
i++;
}
return (s);
}

18
libft/ft_putchar_fd.c Executable file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 11:23:07 by fgras-ca #+# #+# */
/* Updated: 2023/02/15 11:30:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

28
libft/ft_putendl_fd.c Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 13:33:49 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:19:05 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
int i;
i = 0;
if (!s)
return ;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
write(fd, "\n", 1);
}

36
libft/ft_putnbr_fd.c Executable file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 13:53:25 by fgras-ca #+# #+# */
/* Updated: 2023/02/15 14:00:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
{
write(fd, "-2147483648", 11);
}
else if (n >= 0 && n < 10)
{
n += 48;
ft_putchar_fd(n, fd);
}
else if (n < 0)
{
ft_putchar_fd('-', fd);
ft_putnbr_fd((n * (-1)), fd);
}
else
{
ft_putnbr_fd(n / 10, fd);
ft_putnbr_fd(n % 10, fd);
}
}

27
libft/ft_putstr_fd.c Executable file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 12:29:14 by fgras-ca #+# #+# */
/* Updated: 2023/02/15 19:02:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
int i;
i = 0;
if (!s || !fd)
return ;
while (s[i])
{
write(fd, &s[i], 1);
i++;
}
}

75
libft/ft_split.c Executable file
View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:12:04 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 11:04:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_words(const char *s, char c)
{
int count;
int trigger;
count = 0;
trigger = 0;
while (*s)
{
if (*s != c && trigger == 0)
{
trigger = 1;
count++;
}
else if (*s == c)
trigger = 0;
s++;
}
return (count);
}
static char *word_cpy(const char *s, int start, int end)
{
char *word;
int i;
i = 0;
word = malloc(sizeof(char) * (end - start + 1));
while (start < end)
word[i++] = s[start++];
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
size_t i;
size_t j;
int index;
char **split;
split = malloc(sizeof(char *) * (count_words(s, c) + 1));
if (!s || !split)
return (0);
i = 0;
j = 0;
index = -1;
while (i <= ft_strlen(s))
{
if (s[i] != c && index < 0)
index = i;
else if ((s[i] == c || i == ft_strlen(s)) && index >= 0)
{
split[j++] = word_cpy(s, index, i);
index = -1;
}
i++;
}
split[j] = 0;
return (split);
}

32
libft/ft_strchr.c Executable file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 14:27:10 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:54:39 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
int i;
char *str;
str = (char *)s;
i = 0;
while (str[i])
{
if (str[i] == (char)c)
return (&str[i]);
i++;
}
if (str[i] == (char)c)
return (&str[i]);
return (0);
}

31
libft/ft_strdup.c Executable file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 18:40:02 by fgras-ca #+# #+# */
/* Updated: 2023/03/13 13:51:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
char *str;
size_t i;
str = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!str)
return (NULL);
i = 0;
while (s[i])
{
str[i] = s[i];
i++;
}
str[i] = 0;
return (str);
}

25
libft/ft_striteri.c Executable file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 14:25:00 by fgras-ca #+# #+# */
/* Updated: 2023/02/16 15:35:25 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
if (!s)
return ;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

39
libft/ft_strjoin.c Executable file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/13 16:54:51 by fgras-ca #+# #+# */
/* Updated: 2023/03/13 13:58:13 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *new_s;
size_t i;
size_t j;
if (!s1 || !s2)
return (0);
new_s = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!new_s)
return (0);
i = 0;
j = 0;
while (s1[i])
{
new_s[j] = s1[i];
i++;
j++;
}
i = 0;
while (s2[i])
new_s[j++] = s2[i++];
new_s[j] = '\0';
return (new_s);
}

33
libft/ft_strlcat.c Executable file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 14:10:58 by fgras-ca #+# #+# */
/* Updated: 2023/02/20 16:19:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t i;
size_t dst_len;
i = 0;
if (!dst && !size)
return (0);
dst_len = ft_strlen(dst);
if (size <= dst_len)
return (size + ft_strlen(src));
while (src[i] && i < size - dst_len -1)
{
dst[dst_len + i] = src[i];
i++;
}
dst[dst_len + i] = 0;
return (dst_len + ft_strlen(src));
}

32
libft/ft_strlcpy.c Executable file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 13:24:31 by fgras-ca #+# #+# */
/* Updated: 2023/02/08 13:33:32 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
if (size > 0)
{
while (src[i] && i < (size - 1))
{
dst[i] = src[i];
i++;
}
dst[i] = 0;
}
while (src[i])
i++;
return (i);
}

23
libft/ft_strlen.c Executable file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/03 14:10:21 by fgras-ca #+# #+# */
/* Updated: 2023/02/03 14:17:21 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}

33
libft/ft_strmapi.c Executable file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 13:38:52 by fgras-ca #+# #+# */
/* Updated: 2023/02/16 13:51:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
{
char *result;
unsigned int i;
if (!s)
return (0);
result = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!result)
return (0);
i = 0;
while (s[i])
{
result[i] = f(i, s[i]);
i++;
}
result[i] = 0;
return (result);
}

29
libft/ft_strncmp.c Executable file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 17:12:38 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 13:26:15 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
{
return (0);
}
while (s1[i] == s2[i] && (s1[i] != '\0' || s2[i] != '\0') && i < (n - 1))
{
i++;
}
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}

41
libft/ft_strnstr.c Executable file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 14:31:40 by fgras-ca #+# #+# */
/* Updated: 2023/02/21 10:08:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
i = 0;
if (needle == haystack)
return ((char *)haystack);
if (!len && !haystack)
return (0);
while (haystack[i] != '\0')
{
j = 0;
while (haystack[i + j] == needle[j] && (i + j) < len)
{
if (haystack[i + j] == '\0' && needle[j] == '\0')
{
return ((char *)&haystack[j]);
}
j++;
}
if (needle[j] == '\0')
return ((char *)(haystack + i));
i++;
}
return (NULL);
}

29
libft/ft_strrchr.c Executable file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 16:10:11 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 15:36:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
i++;
while (i >= 0)
{
if (s[i] == (char)c)
return ((char *)(s + i));
i--;
}
return (0);
}

52
libft/ft_strtrim.c Executable file
View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 16:30:26 by fgras-ca #+# #+# */
/* Updated: 2023/03/13 14:12:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int char_in_set(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t i;
size_t start;
size_t end;
if (!s1)
return (0);
start = 0;
while (s1[start] && char_in_set(s1[start], set))
start++;
end = ft_strlen(s1);
while (end > start && char_in_set(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(*s1) * (end - start + 1));
if (!str)
return (0);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = 0;
return (str);
}

37
libft/ft_substr.c Executable file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/13 15:40:49 by fgras-ca #+# #+# */
/* Updated: 2023/03/02 13:20:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new_s;
size_t i;
size_t j;
new_s = (char *)malloc(sizeof(char) * (len + 1));
if (!s || !new_s)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
if (i >= start && j < len)
{
new_s[j] = s[i];
j++;
}
i++;
}
new_s[j] = '\0';
return (new_s);
}

20
libft/ft_tolower.c Executable file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 14:09:59 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 13:36:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 65 && c <= 90)
c += 32;
return (c);
}

20
libft/ft_toupper.c Executable file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/08 16:02:00 by fgras-ca #+# #+# */
/* Updated: 2023/02/14 13:36:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
c -= 32;
return (c);
}

71
libft/libft.h Executable file
View File

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/12 10:46:53 by fgras-ca #+# #+# */
/* Updated: 2023/02/24 11:20:17 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <unistd.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
int ft_atoi(const char *nptr);
int ft_lstsize(t_list *lst);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *ptr, size_t len);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
void *ft_calloc(size_t nmemb, size_t size);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_lstadd_front(t_list **lst, t_list *new);
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 *));
size_t ft_strlen(const char *s);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
char *ft_strdup(const char *s);
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_strmapi(const char *s, char (*f)(unsigned int, char));
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
t_list *ft_lstnew(void *content);
t_list *ft_lstlast(t_list *lst);
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

28
minitalk.h Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minitalk.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 22:30:31 by fgras-ca #+# #+# */
/* Updated: 2023/07/21 22:35:00 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINITALK_H
# define MINITALK_H
# include <signal.h>
# include <stdlib.h>
# include <unistd.h>
# include "libft/libft.h"
# include "printf/ft_printf.h"
int g_acknowledged;
void sigusr1_handler(int sig);
void send_ack(int pid);
void receive_bit(int sig, siginfo_t *info, void *context);
#endif

28
minitalk_bonus.h Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minitalk_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 23:01:57 by fgras-ca #+# #+# */
/* Updated: 2023/07/21 23:06:08 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINITALK_BONUS_H
# define MINITALK_BONUS_H
# include <signal.h>
# include <stdlib.h>
# include <unistd.h>
# include "libft/libft.h"
# include "printf/ft_printf.h"
int g_acknowledged;
void sigusr1_handler(int sig);
void send_ack(int pid);
void receive_bit(int sig, siginfo_t *info, void *context);
#endif

46
printf/Makefile Executable file
View File

@ -0,0 +1,46 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/03/18 16:20:00 by fgras-ca #+# #+# #
# Updated: 2023/03/18 16:32:40 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
SOURCES = libftprintf/ft_decimal_pf.c \
libftprintf/ft_hexnum_pf.c \
libftprintf/ft_print_type.c \
libftprintf/ft_ptrhex_pf.c \
libftprintf/ft_putchar_pf.c \
libftprintf/ft_putstr_pf.c \
libftprintf/ft_unsigned_pf.c \
ft_printf.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Werror -Wextra
all: $(NAME)
$(NAME): $(OBJECTS)
ar rcs $(NAME) $(OBJECTS)
main:
$(CC) $(CFLAGS) main.c -L. -lftprintf
clean:
$(RM) $(OBJECTS)
fclean: clean
$(RM) $(NAME)
re: fclean all

37
printf/ft_printf.c Executable file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 16:36:14 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:51:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *s, ...)
{
int i;
int print_len;
va_list args;
i = 0;
print_len = 0;
va_start(args, s);
while (s[i])
{
if (s[i] == '%')
{
print_len += ft_print_type(&args, s[i + 1]);
i++;
}
else
print_len += ft_putchar_pf(s[i]);
i++;
}
va_end(args);
return (print_len);
}

28
printf/ft_printf.h Executable file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 15:16:04 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 15:56:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdio.h>
# include <unistd.h>
# include <stdarg.h>
int ft_printf(const char *s, ...);
int ft_decimal_pf(int c);
int ft_hexnum_pf(unsigned int a, char c);
int ft_ptrhex_pf(unsigned long c, int pref);
int ft_unsigned_pf(unsigned int c);
int ft_print_type(va_list *args, char c);
int ft_putchar_pf(char c);
int ft_putstr_pf(char *s);
#endif

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_decimal_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 18:32:20 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 15:47:12 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_decimal_pf(int c)
{
int count;
count = 0;
if (c == 0)
return (write(1, "0", 1));
if (c == -2147483648)
return (write(1, "-2147483648", 11));
if (c < 0)
{
count += write(1, "-", 1);
c *= -1;
}
if (c >= 10)
count += ft_decimal_pf(c / 10);
write(1, &"0123456789"[c % 10], 1);
return (count + 1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexnum_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 14:34:00 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 16:42:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_hexnum_pf(unsigned int a, char c)
{
int count;
count = 0;
if (a >= 16)
count += ft_hexnum_pf(a / 16, c);
if (c == 'x')
write(1, &"0123456789abcdef"[a % 16], 1);
if (c == 'X')
write(1, &"0123456789ABCDEF"[a % 16], 1);
return (count +1);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 15:28:13 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 17:13:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_print_type(va_list *args, char c)
{
if (c == 'c')
return (ft_putchar_pf(va_arg((*args), int)));
else if (c == 's')
return (ft_putstr_pf(va_arg((*args), char *)));
else if (c == 'p')
return (ft_ptrhex_pf(va_arg((*args), unsigned long), 1));
else if (c == 'd')
return (ft_decimal_pf(va_arg((*args), int)));
else if (c == 'i')
return (ft_decimal_pf(va_arg((*args), int)));
else if (c == 'u')
return (ft_unsigned_pf(va_arg((*args), unsigned int)));
else if (c == 'x' || c == 'X')
return (ft_hexnum_pf(va_arg((*args), unsigned int), c));
else if (c == '%')
return (write(1, "%", 1));
else
return (0);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ptrhex_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 13:31:16 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:45:30 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_ptrhex_pf(unsigned long c, int pref)
{
int count;
count = 0;
if (!c)
return (write(1, "(nil)", 5));
if (pref == 1)
{
count += write(1, "0x", 2);
pref = 0;
}
if (c >= 16)
count += ft_ptrhex_pf(c / 16, pref);
write(1, &"0123456789abcdef"[c % 16], 1);
return (count + 1);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 17:54:21 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 17:01:04 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_putchar_pf(char c)
{
write(1, &c, 1);
return (1);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 18:08:18 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 17:49:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_putstr_pf(char *s)
{
int i;
i = 0;
if (!s)
return (write(1, "(null)", 6));
while (s[i])
{
write(1, &s[i], 1);
i++;
}
return (i);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigned_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 13:12:40 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:47:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_unsigned_pf(unsigned int c)
{
int count;
count = 0;
if (c >= 10)
count += ft_unsigned_pf(c / 10);
write(1, &"0123456789"[c % 10], 1);
return (count + 1);
}

69
server.c Executable file
View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 20:17:46 by fgras-ca #+# #+# */
/* Updated: 2023/07/24 18:55:48 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void send_ack(int pid)
{
kill(pid, SIGUSR1);
}
void receive_bit(int sig, siginfo_t *info, void *context)
{
static int i = 0;
static pid_t client_pid = 0;
static unsigned char c = 0;
(void)context;
if (!client_pid)
client_pid = info->si_pid;
c |= (sig == SIGUSR2);
if (++i == 8)
{
i = 0;
if (!c)
{
ft_putchar_fd('\n', 1);
send_ack(client_pid);
client_pid = 0;
return ;
}
ft_putchar_fd(c, 1);
c = 0;
}
else
c <<= 1;
send_ack(client_pid);
}
void setup_signals(void)
{
struct sigaction s_sigaction;
ft_memset(&s_sigaction, 0, sizeof(struct sigaction));
s_sigaction.sa_sigaction = receive_bit;
s_sigaction.sa_flags = SA_SIGINFO;
sigemptyset(&s_sigaction.sa_mask);
sigaction(SIGUSR1, &s_sigaction, NULL);
sigaction(SIGUSR2, &s_sigaction, NULL);
}
int main(void)
{
ft_printf("Server PID: ");
ft_printf("%d", getpid());
ft_printf("\n");
setup_signals();
while (1)
pause();
return (0);
}

70
server_bonus.c Executable file
View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/21 22:48:09 by fgras-ca #+# #+# */
/* Updated: 2023/07/27 13:55:56 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
void send_ack(int pid)
{
kill(pid, SIGUSR1);
}
void receive_bit(int sig, siginfo_t *info, void *context)
{
static int i = 0;
static pid_t client_pid = 0;
static unsigned char c = 0;
(void)context;
if (!client_pid)
client_pid = info->si_pid;
c |= (sig == SIGUSR2);
if (++i == 8)
{
i = 0;
if (!c)
{
ft_putchar_fd('\n', 1);
send_ack(client_pid);
kill(client_pid, SIGUSR2);
client_pid = 0;
return ;
}
ft_putchar_fd(c, 1);
c = 0;
}
else
c <<= 1;
send_ack(client_pid);
}
void setup_signals(void)
{
struct sigaction s_sigaction;
ft_memset(&s_sigaction, 0, sizeof(struct sigaction));
s_sigaction.sa_sigaction = receive_bit;
s_sigaction.sa_flags = SA_SIGINFO;
sigemptyset(&s_sigaction.sa_mask);
sigaction(SIGUSR1, &s_sigaction, NULL);
sigaction(SIGUSR2, &s_sigaction, NULL);
}
int main(void)
{
ft_printf("Server PID: ");
ft_printf("%d", getpid());
ft_printf("\n");
setup_signals();
while (1)
pause();
return (0);
}