Mandatory finished

This commit is contained in:
2025-11-17 12:40:49 +01:00
parent 506d90d9c1
commit dabbfa7a35
73 changed files with 238 additions and 11 deletions

View File

@@ -5,6 +5,13 @@ INC_DIR= include
LIBFT_DIR= libft LIBFT_DIR= libft
SRC= $(SRC_DIR)/ft_printf.c \ SRC= $(SRC_DIR)/ft_printf.c \
$(SRC_DIR)/ft_putnbr_base.c \
$(SRC_DIR)/print_char.c \
$(SRC_DIR)/print_hex.c \
$(SRC_DIR)/print_number.c \
$(SRC_DIR)/print_pointer.c \
$(SRC_DIR)/print_str.c \
$(SRC_DIR)/print_unsigned.c \
OBJ= $(SRC:.c=.o) OBJ= $(SRC:.c=.o)
DEP= $(SRC:.c=.d) DEP= $(SRC:.c=.d)
@@ -16,20 +23,20 @@ AR= ar rcs
all: $(NAME) all: $(NAME)
$(NAME): $(OBJ) $(NAME): $(OBJ)
@$(MAKE) -C $(LIBFT_DIR) $(MAKE) -C $(LIBFT_DIR)
@cp $(LIBFT_DIR)/libft.a $(NAME) cp $(LIBFT_DIR)/libft.a $(NAME)
@$(AR) $(NAME) $(OBJ) $(AR) $(NAME) $(OBJ)
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
clean: clean:
@rm -f $(OBJ) $(DEP) rm -f $(OBJ) $(DEP)
@$(MAKE) -C $(LIBFT_DIR) clean $(MAKE) -C $(LIBFT_DIR) clean
fclean: clean fclean: clean
@rm -f $(NAME) rm -f $(NAME)
@$(MAKE) -C $(LIBFT_DIR) fclean $(MAKE) -C $(LIBFT_DIR) fclean
re: fclean all re: fclean all

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 10:52:23 by dgaillet #+# #+# */ /* Created: 2025/11/17 10:52:23 by dgaillet #+# #+# */
/* Updated: 2025/11/17 11:25:46 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/17 12:34:18 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,4 +15,13 @@
int ft_printf(const char *first_arg, ...); int ft_printf(const char *first_arg, ...);
int ft_putnbr_base(unsigned long long nbr, char *base, int base_size);
int print_char(char c);
int print_str(char *str);
int print_pointer(unsigned long long p);
int print_number(int nbr);
int print_unsigned(unsigned int nbr);
int print_hex(unsigned long long p, int upper);
#endif #endif

View File

@@ -71,7 +71,7 @@ ALL_DEP= $(DEP) $(BONUS_DEP)
%.o: %.c %.o: %.c
$(CC) -MMD -MP -o $@ -c $< $(CFLAGS) -I$(HEADER) $(CC) -MMD -MP -o $@ -c $< $(CFLAGS) -I.
all: $(NAME) all: $(NAME)

BIN
libftprintf.a Normal file

Binary file not shown.

View File

@@ -6,13 +6,54 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 10:54:03 by dgaillet #+# #+# */ /* Created: 2025/11/17 10:54:03 by dgaillet #+# #+# */
/* Updated: 2025/11/17 11:26:09 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/17 12:35:20 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h>
#include "../include/ft_printf.h"
static int ft_print_arg(char c, va_list args)
{
if (c == 'c')
return (print_char(va_arg(args, int)));
else if (c == 's')
return (print_str(va_arg(args, char *)));
else if (c == 'p')
return (print_pointer(va_arg(args, unsigned long long)));
else if (c == 'd' || c == 'i')
return (print_number(va_arg(args, int)));
else if (c == 'u')
return (print_unsigned(va_arg(args, unsigned int)));
else if (c == 'x')
return (print_hex(va_arg(args, unsigned int), 0));
else if (c == 'X')
return (print_hex(va_arg(args, unsigned int), 1));
else if (c == '%')
return (print_char('%'));
return (0);
}
int ft_printf(const char *first_arg, ...) int ft_printf(const char *first_arg, ...)
{ {
return (1); va_list args;
int nb_print;
int i;
nb_print = 0;
va_start(args, first_arg);
i = 0;
while (first_arg[i])
{
if (first_arg[i] == '%')
{
i++;
nb_print += ft_print_arg(first_arg[i], args);
}
else
nb_print += write(1, &first_arg[i], 1);
i++;
}
return (nb_print);
} }

2
src/ft_printf.d Normal file
View File

@@ -0,0 +1,2 @@
src/ft_printf.o: src/ft_printf.c src/../include/ft_printf.h
src/../include/ft_printf.h:

BIN
src/ft_printf.o Normal file

Binary file not shown.

24
src/ft_putnbr_base.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:16:44 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:17:14 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../libft/libft.h"
int ft_putnbr_base(unsigned long long nbr, char *base, int base_size)
{
int i;
if (nbr == 0)
return (0);
i = 1 + ft_putnbr_base(nbr / base_size, base, base_size);
ft_putchar_fd(base[nbr % base_size], 1);
return (i);
}

2
src/ft_putnbr_base.d Normal file
View File

@@ -0,0 +1,2 @@
src/ft_putnbr_base.o: src/ft_putnbr_base.c src/../libft/libft.h
src/../libft/libft.h:

BIN
src/ft_putnbr_base.o Normal file

Binary file not shown.

18
src/print_char.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 11:56:36 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:01:47 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int print_char(char c)
{
return (write(1, &c, 1));
}

1
src/print_char.d Normal file
View File

@@ -0,0 +1 @@
src/print_char.o: src/print_char.c

BIN
src/print_char.o Normal file

Binary file not shown.

25
src/print_hex.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_hex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:06:35 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:38:35 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../include/ft_printf.h"
#include <unistd.h>
int print_hex(unsigned long long nbr, int upper)
{
if (nbr == 0)
return (write(1, "0", 1));
if (upper)
return (ft_putnbr_base(nbr, "0123456789ABCDEF", 16));
else
return (ft_putnbr_base(nbr, "0123456789abcdef", 16));
}

2
src/print_hex.d Normal file
View File

@@ -0,0 +1,2 @@
src/print_hex.o: src/print_hex.c src/../include/ft_printf.h
src/../include/ft_printf.h:

BIN
src/print_hex.o Normal file

Binary file not shown.

26
src/print_number.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_number.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:31:22 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../include/ft_printf.h"
#include <unistd.h>
int print_number(int nbr)
{
if (nbr == 0)
return (write(1, "0", 1));
if (nbr < 0)
{
write(1, "-", 1);
return (1 + ft_putnbr_base(nbr * -1, "0123456789", 10));
}
return (ft_putnbr_base(nbr, "0123456789", 10));
}

2
src/print_number.d Normal file
View File

@@ -0,0 +1,2 @@
src/print_number.o: src/print_number.c src/../include/ft_printf.h
src/../include/ft_printf.h:

BIN
src/print_number.o Normal file

Binary file not shown.

20
src/print_pointer.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:19:16 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:25:20 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../libft/libft.h"
#include "../include/ft_printf.h"
int print_pointer(unsigned long long p)
{
ft_putstr_fd("0x", 1);
return (2 + ft_putnbr_base(p, "0123456789abcdef", 16));
}

4
src/print_pointer.d Normal file
View File

@@ -0,0 +1,4 @@
src/print_pointer.o: src/print_pointer.c src/../libft/libft.h \
src/../include/ft_printf.h
src/../libft/libft.h:
src/../include/ft_printf.h:

BIN
src/print_pointer.o Normal file

Binary file not shown.

19
src/print_str.c Normal file
View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:02:03 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:03:27 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../libft/libft.h"
int print_str(char *str)
{
ft_putstr_fd(str, 1);
return (ft_strlen(str));
}

2
src/print_str.d Normal file
View File

@@ -0,0 +1,2 @@
src/print_str.o: src/print_str.c src/../libft/libft.h
src/../libft/libft.h:

BIN
src/print_str.o Normal file

Binary file not shown.

21
src/print_unsigned.c Normal file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */
/* Updated: 2025/11/17 12:34:00 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../include/ft_printf.h"
#include <unistd.h>
int print_unsigned(unsigned int nbr)
{
if (nbr == 0)
return (write(1, "0", 1));
return (ft_putnbr_base(nbr, "0123456789", 10));
}

2
src/print_unsigned.d Normal file
View File

@@ -0,0 +1,2 @@
src/print_unsigned.o: src/print_unsigned.c src/../include/ft_printf.h
src/../include/ft_printf.h:

BIN
src/print_unsigned.o Normal file

Binary file not shown.