intra to gitea
This commit is contained in:
commit
fc25e29714
|
|
@ -0,0 +1,52 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# #
|
||||
# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
NAMEC = client
|
||||
NAMES = server
|
||||
BONUS_NAMEC = client_bonus
|
||||
BONUS_NAMES = server_bonus
|
||||
PRINTF = libftprintf.a
|
||||
SRCC_FILES = client.c
|
||||
SRCS_FILES = server.c
|
||||
SRC_DIR = src/
|
||||
SRCC = $(addprefix $(SRC_DIR), $(SRCC_FILES))
|
||||
SRCS = $(addprefix $(SRC_DIR), $(SRCS_FILES))
|
||||
OBJC = ${SRCC:.c=.o}
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Werror -Wextra
|
||||
INCLUDE = -I include
|
||||
RM = rm -rf
|
||||
|
||||
all: $(NAMEC) $(NAMES)
|
||||
|
||||
$(NAMEC) : $(OBJC)
|
||||
@make -C ft_printf
|
||||
$(CC) $(CFLAGS) $(OBJC) $(INCLUDE) ft_printf/$(PRINTF) -o $(NAMEC)
|
||||
|
||||
$(NAMES) : $(OBJS)
|
||||
@make -C ft_printf
|
||||
$(CC) $(CFLAGS) $(OBJS) $(INCLUDE) ft_printf/$(PRINTF) -o $(NAMES)
|
||||
|
||||
clean :
|
||||
@make clean -C ft_printf
|
||||
${RM} ${OBJC}
|
||||
${RM} ${OBJS}
|
||||
|
||||
fclean : clean
|
||||
@make fclean -C ft_printf
|
||||
${RM} $(NAMEC)
|
||||
${RM} $(NAMES)
|
||||
${RM} $(PRINTF)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# #
|
||||
# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = libftprintf.a
|
||||
SRCS_DIR = srcs/
|
||||
INCS_DIR = includes/
|
||||
OBJ_DIR = obj/
|
||||
|
||||
SRC = ft_putchar.c ft_put_ptr.c ft_conv_ith.c ft_hex_len.c \
|
||||
ft_sorting.c ft_printf.c ft_print_ith.c ft_printchar.c \
|
||||
ft_printstr.c ft_printpercent.c ft_print_unsigned.c \
|
||||
ft_print_nbr.c ft_print_ptr.c ft_adrr_len.c ft_print_null.c \
|
||||
ft_itoa.c ft_unsigned_itoa.c ft_count.c
|
||||
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
INCLUDE = -I $(INCS_DIR)
|
||||
SRCS = $(addprefix $(SRCS_DIR), $(SRC))
|
||||
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
|
||||
|
||||
# Colors
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[0;33m
|
||||
RESET = \033[0m
|
||||
WHITE = \033[0;97m
|
||||
|
||||
# Loading animation
|
||||
LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@printf "$(YELLOW)Compiling ft_printf, Please wait...$(RESET)"
|
||||
@for char in $(LOADING_CHARS); do \
|
||||
printf "\r$(YELLOW)Compiling ft_printf, Please wait... $$char$(RESET)"; \
|
||||
sleep 0.1; \
|
||||
done
|
||||
@ar rc $(NAME) $(OBJ)
|
||||
@ranlib $(NAME)
|
||||
@printf "\r$(GREEN)Great news ! $(WHITE)ft_printf compiled successfully ! $(RESET)\n"
|
||||
|
||||
$(OBJ_DIR)%.o: $(SRCS_DIR)%.c
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@printf "$(WHITE)Clean process completed for $(GREEN)ft_printf.$(RESET)\n"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@printf "$(WHITE)Full clean process completed for $(GREEN)ft_printf.$(RESET)\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
# ft_printf
|
||||
|
||||
Your own implementation of the printf function from libc.
|
||||
|
||||
## 💡 About
|
||||
|
||||
The ft_printf project consists of programming your own version of the famous printf function. You will learn about variadic arguments and will implement formatted output conversion. This project is a great way to improve your programming skills and is frequently reused in subsequent 42 projects.
|
||||
|
||||
## 🛠️ Supported Conversions
|
||||
|
||||
The function handles the following conversions:
|
||||
- `%c` - Single character
|
||||
- `%s` - String
|
||||
- `%p` - Pointer address
|
||||
- `%d` - Decimal (base 10) integer
|
||||
- `%i` - Integer in base 10
|
||||
- `%u` - Unsigned decimal (base 10) integer
|
||||
- `%x` - Hexadecimal (base 16) integer lowercase
|
||||
- `%X` - Hexadecimal (base 16) integer uppercase
|
||||
- `%%` - Percentage sign
|
||||
|
||||
## 📋 Usage
|
||||
|
||||
```c
|
||||
#include "ft_printf.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ft_printf("Hello %s!\n", "world");
|
||||
ft_printf("Number: %d\n", 42);
|
||||
ft_printf("Hexadecimal: %x\n", 42);
|
||||
return (0);
|
||||
}
|
||||
```
|
||||
|
||||
## ⚠️ Function Prototype
|
||||
|
||||
```c
|
||||
int ft_printf(const char *format, ...);
|
||||
```
|
||||
|
||||
Returns the number of characters printed (excluding the null byte used to end output to strings).
|
||||
|
||||
## 📊 Expected Output
|
||||
|
||||
The function should work exactly like the original printf, handling all the specified conversions correctly.
|
||||
|
||||
---
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_adrr_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 05:16:57 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:50:34 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_adrr_len(unsigned long long nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nbr == 0)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (nbr != 0)
|
||||
{
|
||||
i++;
|
||||
nbr = nbr / 16;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_conv_ith.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:46:47 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:23 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_conv_ith(unsigned int nbr, const char str)
|
||||
{
|
||||
if (nbr >= 16)
|
||||
{
|
||||
ft_conv_ith((nbr / 16), str);
|
||||
nbr = nbr % 16;
|
||||
}
|
||||
if (nbr <= 9)
|
||||
ft_putchar(nbr + '0');
|
||||
else
|
||||
{
|
||||
if (str == 'x')
|
||||
ft_putchar(nbr - 10 + 'a');
|
||||
else if (str == 'X')
|
||||
ft_putchar(nbr - 10 + 'A');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_count.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:52:36 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:47:07 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
size_t ft_count(long long int n)
|
||||
{
|
||||
size_t c;
|
||||
|
||||
c = 0;
|
||||
if (n <= 0)
|
||||
c = 1;
|
||||
while (n != 0)
|
||||
{
|
||||
n = n / 10;
|
||||
c++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_hex_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:45:50 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:49:59 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_hex_len(unsigned int nbr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nbr == 0)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (nbr != 0)
|
||||
{
|
||||
i++;
|
||||
nbr = nbr / 16;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:51:58 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:53:33 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *str;
|
||||
size_t size;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
size = ft_count(num);
|
||||
str = (char *)malloc(size + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[size] = '\0';
|
||||
if (num < 0)
|
||||
num = -num;
|
||||
while (size--)
|
||||
{
|
||||
str[size] = num % 10 + '0';
|
||||
num = num / 10;
|
||||
if (n < 0 && size == 0)
|
||||
str[size] = '-';
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_ith.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:49:51 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_ith(unsigned int nbr, const char str)
|
||||
{
|
||||
if (nbr == 0)
|
||||
return (write(1, "0", 1));
|
||||
ft_conv_ith(nbr, str);
|
||||
return (ft_hex_len(nbr));
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_nbr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:52:47 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_nbr(int nb)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = ft_itoa(nb);
|
||||
len = ft_printstr(str);
|
||||
free (str);
|
||||
return (len);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_null.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 07:01:50 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:39 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_null(void)
|
||||
{
|
||||
write(1, "(null)", 6);
|
||||
return (6);
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_ptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:10 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_ptr(unsigned long long ptr)
|
||||
{
|
||||
int print_length;
|
||||
|
||||
if (ptr == 0)
|
||||
{
|
||||
write(1, "(nil)", 5);
|
||||
return (5);
|
||||
}
|
||||
print_length = 2;
|
||||
write(1, "0x", 2);
|
||||
{
|
||||
ft_put_ptr(ptr);
|
||||
print_length = print_length + ft_adrr_len(ptr);
|
||||
}
|
||||
return (print_length);
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_print_unsigned.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:50:53 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:31 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_print_unsigned(unsigned int nb)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = ft_unsigned_itoa(nb);
|
||||
len = ft_printstr(str);
|
||||
free (str);
|
||||
return (len);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printchar.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:56:02 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:56:25 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printchar(int c)
|
||||
{
|
||||
write (1, &c, 1);
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:55:41 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printf(const char *str, ...)
|
||||
{
|
||||
int i;
|
||||
va_list list;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
va_start(list, str);
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%')
|
||||
{
|
||||
count += ft_sorting(list, str[i + 1]);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
write (1, &str[i], 1);
|
||||
count++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
va_end(list);
|
||||
return (count);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printpercent.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:47:38 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:50:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printpercent(void)
|
||||
{
|
||||
ft_putchar('%');
|
||||
return (1);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printstr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:55:08 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:05 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_printstr(char *s)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!s)
|
||||
return (ft_print_null());
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
write (1, &s[i], 1);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put_ptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:54:06 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:46:52 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_put_ptr(unsigned long long num)
|
||||
{
|
||||
if (num >= 16)
|
||||
{
|
||||
ft_put_ptr(num / 16);
|
||||
ft_put_ptr(num % 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (num <= 9)
|
||||
ft_putchar(num + '0');
|
||||
else
|
||||
ft_putchar(num - 10 + 'a');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:45:04 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 04:49:11 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sorting.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:56:45 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:51:17 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
int ft_sorting(va_list list, const char str)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (str == 'c')
|
||||
count += ft_printchar(va_arg(list, int));
|
||||
else if (str == 's')
|
||||
count += ft_printstr(va_arg(list, char *));
|
||||
else if (str == 'p')
|
||||
count += ft_print_ptr(va_arg(list, unsigned long long));
|
||||
else if (str == 'd' || str == 'i')
|
||||
count += ft_print_nbr(va_arg(list, int));
|
||||
else if (str == 'u')
|
||||
count += ft_print_unsigned(va_arg(list, unsigned int));
|
||||
else if (str == 'x' || str == 'X')
|
||||
count += ft_print_ith(va_arg(list, unsigned int), str);
|
||||
else if (str == '%')
|
||||
count += ft_printpercent();
|
||||
return (count);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_unsigned_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:51:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:45:46 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "ftprintf.h"
|
||||
|
||||
char *ft_unsigned_itoa(unsigned int n)
|
||||
{
|
||||
char *str;
|
||||
size_t size;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
size = ft_count(num);
|
||||
str = (char *)malloc(size + 1);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[size] = '\0';
|
||||
if (num < 0)
|
||||
num = -num;
|
||||
while (size--)
|
||||
{
|
||||
str[size] = num % 10 + '0';
|
||||
num = num / 10;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ftprintf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/06/19 07:49:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FTPRINTF_H
|
||||
# define FTPRINTF_H
|
||||
|
||||
# include <stdarg.h>
|
||||
# include <unistd.h>
|
||||
# include <stdio.h>
|
||||
# include "stdlib.h"
|
||||
|
||||
void ft_putchar(char c);
|
||||
void ft_put_ptr(unsigned long long num);
|
||||
void ft_conv_ith(unsigned int nbr, const char str);
|
||||
|
||||
int ft_hex_len(unsigned int nbr);
|
||||
int ft_sorting(va_list list, const char str);
|
||||
int ft_printf(const char *str, ...);
|
||||
int ft_print_ith(unsigned int nbr, const char str);
|
||||
int ft_printchar(int c);
|
||||
int ft_printstr(char *s);
|
||||
int ft_printpercent(void);
|
||||
int ft_print_unsigned(unsigned int nb);
|
||||
int ft_print_nbr(int nb);
|
||||
int ft_print_ptr(unsigned long long ptr);
|
||||
int ft_adrr_len(unsigned long long nbr);
|
||||
int ft_print_null(void);
|
||||
|
||||
char *ft_itoa(int n);
|
||||
char *ft_unsigned_itoa(unsigned int n);
|
||||
|
||||
size_t ft_count(long long int n);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minitalk.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/12 07:07:49 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/08/12 09:03:30 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINITALK_H
|
||||
# define MINITALK_H
|
||||
|
||||
# include "../ft_printf/srcs/ftprintf.h"
|
||||
# include <signal.h>
|
||||
# include <errno.h>
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* client.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/12 07:07:31 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/08/12 09:25:00 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/minitalk.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
int sign;
|
||||
unsigned long int result;
|
||||
|
||||
i = 0;
|
||||
sign = 1;
|
||||
result = 0;
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
if (str[i] == '-')
|
||||
{
|
||||
sign = -1;
|
||||
i++;
|
||||
}
|
||||
else if (str[i] == '+')
|
||||
i++;
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
result *= 10;
|
||||
result += str[i] - '0';
|
||||
i++;
|
||||
}
|
||||
return (result * sign);
|
||||
}
|
||||
|
||||
static int g_signal_received = 0;
|
||||
|
||||
static void handle_confirmation(int signo, siginfo_t *info, void *context)
|
||||
{
|
||||
(void)info;
|
||||
(void)context;
|
||||
(void)signo;
|
||||
g_signal_received = 1;
|
||||
}
|
||||
|
||||
static void setup_sigaction(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
|
||||
sa.sa_sigaction = handle_confirmation;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
}
|
||||
|
||||
static void ft_send(int pid, char c)
|
||||
{
|
||||
int bit;
|
||||
|
||||
bit = 0;
|
||||
while (bit < 8)
|
||||
{
|
||||
g_signal_received = 0;
|
||||
if ((c & (0x01 << bit)))
|
||||
kill(pid, SIGUSR1);
|
||||
else
|
||||
kill(pid, SIGUSR2);
|
||||
while (!g_signal_received)
|
||||
usleep(100);
|
||||
bit++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int pid;
|
||||
int i;
|
||||
|
||||
if (ac != 3)
|
||||
{
|
||||
ft_printf("Client Usage ➡️ ./a.out <Server PID> <STR>\n");
|
||||
return (1);
|
||||
}
|
||||
setup_sigaction();
|
||||
pid = ft_atoi(av[1]);
|
||||
if (pid < 1)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (av[2][i] != '\0')
|
||||
{
|
||||
ft_send(pid, av[2][i]);
|
||||
i++;
|
||||
}
|
||||
ft_send(pid, '\n');
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* server.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/12 07:07:31 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/minitalk.h"
|
||||
|
||||
static void handle_signal(int signo, siginfo_t *info, void *context)
|
||||
{
|
||||
static int bit = 0;
|
||||
static char c = 0;
|
||||
|
||||
(void)context;
|
||||
if (signo == SIGUSR1)
|
||||
c |= (1 << bit);
|
||||
bit++;
|
||||
if (bit == 8)
|
||||
{
|
||||
ft_printf("%c", c);
|
||||
bit = 0;
|
||||
c = 0;
|
||||
}
|
||||
usleep(100);
|
||||
kill(info->si_pid, SIGUSR1);
|
||||
}
|
||||
|
||||
static void setup_sigaction(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
|
||||
sa.sa_sigaction = handle_signal;
|
||||
sa.sa_flags = SA_SIGINFO;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
sigaction(SIGUSR2, &sa, NULL);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ft_printf("Server PID: %d\n", getpid());
|
||||
setup_sigaction();
|
||||
while (1)
|
||||
pause();
|
||||
return (0);
|
||||
}
|
||||
Loading…
Reference in New Issue