oui
This commit is contained in:
Executable
+30
@@ -0,0 +1,30 @@
|
||||
NAME = libftprintf.a
|
||||
CC = cc
|
||||
CFLAGS = -Wall -Wextra -Werror -c -I$(HEADER_DIR) -g3
|
||||
RM = rm -f
|
||||
AR = ar rcs
|
||||
SOURCES = ft_printf.c ft_putchar_fd.c ft_putnbr_fd.c ft_putptr_fd.c ft_put_unsigned_nbr_fd.c ft_puthex_upper_fd.c ft_puthex_lower_fd.c ft_putstr_fd.c
|
||||
SOURCES_DIR = source/
|
||||
HEADER = ft_printf.h
|
||||
HEADER_DIR = header/
|
||||
OBJECTS = $(addprefix $(SOURCES_DIR), $(SOURCES:.c=.o))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
$(AR) $(NAME) $(OBJECTS)
|
||||
|
||||
$(OBJECTS): $(SOURCES_DIR)%.o :$(SOURCES_DIR)%.c
|
||||
$(CC) $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJECTS)
|
||||
|
||||
fclean: clean
|
||||
$(RM) $(NAME)
|
||||
|
||||
re: fclean
|
||||
@make all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: yantoine <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/22 10:18:58 by yantoine #+# #+# */
|
||||
/* Updated: 2023/12/26 16:09:17 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FT_PRINTF_H
|
||||
# define FT_PRINTF_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <limits.h>
|
||||
# include <stdarg.h>
|
||||
|
||||
int pf_puthex_lower_fd(unsigned int n, int fd);
|
||||
int pf_puthex_upper_fd(unsigned int n, int fd);
|
||||
int ft_printf(const char *format, ...);
|
||||
int pf_putchar_fd(char c, int fd);
|
||||
int pf_putstr_fd(char *s, int fd);
|
||||
int pf_putnbr_fd(long int n, int fd);
|
||||
int pf_put_unsigned_nbr_fd(unsigned int n, int fd);
|
||||
int pf_putptr_fd(unsigned long long n, int fd);
|
||||
|
||||
#endif
|
||||
Executable
BIN
Binary file not shown.
Executable
+78
@@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/29 19:11:52 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 16:06:47 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
static int ft_ptr_format(va_list parameters_info)
|
||||
{
|
||||
unsigned long long ptr_check;
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
ptr_check = va_arg(parameters_info, unsigned long long);
|
||||
if (ptr_check == 0)
|
||||
count += pf_putstr_fd("(nil)", 1);
|
||||
else
|
||||
{
|
||||
count += pf_putstr_fd("0x", 1);
|
||||
count += pf_putptr_fd(ptr_check, 1);
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static int ft_format_c(char s, va_list parameters_info)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (s == 'c')
|
||||
count += pf_putchar_fd(va_arg(parameters_info, int), 1);
|
||||
else if (s == 'i' || s == 'd')
|
||||
count += pf_putnbr_fd(va_arg(parameters_info, long), 1);
|
||||
else if (s == 'u')
|
||||
count += pf_put_unsigned_nbr_fd(va_arg(parameters_info, \
|
||||
unsigned int), 1);
|
||||
else if (s == 's')
|
||||
count += pf_putstr_fd(va_arg(parameters_info, char *), 1);
|
||||
else if (s == 'x')
|
||||
count += pf_puthex_lower_fd(va_arg(parameters_info, long), 1);
|
||||
else if (s == 'X')
|
||||
count += pf_puthex_upper_fd(va_arg(parameters_info, long), 1);
|
||||
else if (s == 'p')
|
||||
count += ft_ptr_format(parameters_info);
|
||||
else if (s == '%')
|
||||
count += pf_putchar_fd('%', 1);
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_printf(const char *s, ...)
|
||||
{
|
||||
va_list parameters_info;
|
||||
unsigned int index;
|
||||
int count;
|
||||
|
||||
index = 0;
|
||||
count = 0;
|
||||
va_start(parameters_info, s);
|
||||
while (s[index])
|
||||
{
|
||||
if (s[index] == '%')
|
||||
{
|
||||
index++;
|
||||
count += ft_format_c(s[index], parameters_info);
|
||||
}
|
||||
else if (s[index] != '%' && s[index])
|
||||
count += pf_putchar_fd(s[index], 1);
|
||||
index++;
|
||||
}
|
||||
va_end(parameters_info);
|
||||
return (count);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+29
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put_unsigned_nbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/02 23:56:02 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:23:28 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_put_unsigned_nbr_fd(unsigned int n, int fd)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (n >= 10)
|
||||
{
|
||||
count += pf_put_unsigned_nbr_fd(n / 10, fd);
|
||||
count += pf_put_unsigned_nbr_fd(n % 10, fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
count += pf_putchar_fd(n + '0', fd);
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+18
@@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/23 14:08:31 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:23:41 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
return (1);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+29
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_puthex_lower_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/02 15:23:28 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:24:01 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_puthex_lower_fd(unsigned int n, int fd)
|
||||
{
|
||||
int i;
|
||||
char *hex;
|
||||
|
||||
i = 0;
|
||||
hex = "0123456789abcdef";
|
||||
if (n >= 16)
|
||||
{
|
||||
i += pf_puthex_lower_fd(n / 16, fd);
|
||||
i += pf_puthex_lower_fd(n % 16, fd);
|
||||
}
|
||||
else
|
||||
i += pf_putchar_fd(hex[n], fd);
|
||||
return (i);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+29
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_puthex_upper_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/02 15:27:12 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:24:21 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_puthex_upper_fd(unsigned int n, int fd)
|
||||
{
|
||||
int i;
|
||||
char *hex;
|
||||
|
||||
i = 0;
|
||||
hex = "0123456789ABCDEF";
|
||||
if (n >= 16)
|
||||
{
|
||||
i += pf_puthex_upper_fd(n / 16, fd);
|
||||
i += pf_puthex_upper_fd(n % 16, fd);
|
||||
}
|
||||
else
|
||||
i += pf_putchar_fd(hex[n], fd);
|
||||
return (i);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+29
@@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 23:41:52 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:24:42 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_putnbr_fd(long int n, int fd)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (n < 0)
|
||||
{
|
||||
n = -n;
|
||||
count += pf_putchar_fd('-', fd);
|
||||
}
|
||||
if ((n / 10) > 0)
|
||||
count += pf_putnbr_fd(n / 10, fd);
|
||||
pf_putchar_fd((n % 10) + 48, fd);
|
||||
return (count + 1);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+32
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putptr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/02 15:14:08 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:25:02 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_putptr_fd(unsigned long long n, int fd)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = 0;
|
||||
if (n >= 16)
|
||||
{
|
||||
len += pf_putptr_fd(n / 16, fd);
|
||||
len += pf_putptr_fd(n % 16, fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n < 10)
|
||||
len += pf_putchar_fd(n + '0', fd);
|
||||
else
|
||||
len += pf_putchar_fd(n + 'a' - 10, fd);
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+33
@@ -0,0 +1,33 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hexplor <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/23 14:24:20 by hexplor #+# #+# */
|
||||
/* Updated: 2023/12/26 15:25:19 by hexplor ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../header/ft_printf.h"
|
||||
|
||||
int pf_putstr_fd(char *s, int fd)
|
||||
{
|
||||
unsigned int i;
|
||||
int count;
|
||||
|
||||
if (!s)
|
||||
{
|
||||
pf_putstr_fd("(null)", fd);
|
||||
return (6);
|
||||
}
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (*(s + i))
|
||||
{
|
||||
count += pf_putchar_fd(*(s + i), fd);
|
||||
i++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user