This commit is contained in:
Etienne Rey-bethbeder 2022-11-10 15:25:19 +01:00
commit c70b29dfd1
13 changed files with 617 additions and 0 deletions

139
Makefile Normal file
View file

@ -0,0 +1,139 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: erey-bet <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# #
# Updated: 2022/10/15 15:39:56 by erey-bet ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_get_size.c ft_itoa.c ft_power.c ft_printf.c ft_putchar.c ft_putnbr.c \
ft_putnbrhex.c ft_putnbrhex_upper.c ft_putstr.c ft_putunbr.c ft_putvd.c
OBJS = ${SRCS:.c=.o}
CC = gcc
CFLAGS = -Wall -Wextra -Werror
NAME = libftprintf.a
all: ${NAME}
${NAME}: ${OBJS}
ar -rc ${NAME} ${OBJS}
%.o:%.c
${CC} ${CFLAGS} -c -o $@ $<
clean:
rm -f ${OBJS}
fclean: clean
rm -f ${NAME}
re: fclean all
coffee:
@clear
@echo ""
@echo " ("
@echo " ) ("
@echo " ___...(-------)-....___"
@echo ' .-"" ) ( ""-.'
@echo " .-''''|-._ ) _.-|"
@echo ' / .--.| `""---...........---""` |'
@echo " / / | |"
@echo " | | | |"
@echo " \ \ | |"
@echo " '\ '\ | |"
@echo " '\ '| |"
@echo " _/ /\ /"
@echo " (__/ \ /"
@echo ' _..---""` \ /`""---.._'
@echo " .-' \ / '-."
@echo ": '-.__ __.-' :"
@echo ': ) ""---...---"" ( :'
@echo "\'._ '"--...___...--"' _.'"
@echo ' \""--..__ __..--""/'
@echo " '._ """----.....______.....----""" _.'"
@echo ' ""--..,,_____ _____,,..--"""'''
@echo ' """------"""'
@sleep 0.3
@clear
@echo ""
@echo " ("
@echo " ) ("
@echo " ___..(.------)--....___"
@echo ' .-"" ) ( ""-.'
@echo " .-''''|-._ ( ) _.-|"
@echo ' / .--.| `""---...........---""` |'
@echo " / / | |"
@echo " | | | |"
@echo " \ \ | |"
@echo " '\ '\ | |"
@echo " '\ '| |"
@echo " _/ /\ /"
@echo " (__/ \ /"
@echo ' _..---""` \ /`""---.._'
@echo " .-' \ / '-."
@echo ": '-.__ __.-' :"
@echo ': ) ""---...---"" ( :'
@echo "\'._ '"--...___...--"' _.'"
@echo ' \""--..__ __..--""/'
@echo " '._ """----.....______.....----""" _.'"
@echo ' ""--..,,_____ _____,,..--"""'''
@echo ' """------"""'
@sleep 0.3
@clear
@echo ""
@echo " ("
@echo " ) ("
@echo " ___..(.------)--....___"
@echo ' .-"" ) ( ""-.'
@echo " .-''''|-._ ( ) _.-|"
@echo ' / .--.| `""---...........---""` |'
@echo " / / | |"
@echo " | | | |"
@echo " \ \ | |"
@echo " '\ '\ | |"
@echo " '\ '| |"
@echo " _/ /\ /"
@echo " (__/ \ /"
@echo ' _..---""` \ /`""---.._'
@echo " .-' \ / '-."
@echo ": '-.__ __.-' :"
@echo ': ) ""---...---"" ( :'
@echo "\'._ '"--...___...--"' _.'"
@echo ' \""--..__ __..--""/'
@echo " '._ """----.....______.....----""" _.'"
@echo ' ""--..,,_____ _____,,..--"""'''
@echo ' """------"""'
@sleep 0.3
@clear
@echo ""
@echo " ( ) "
@echo " ) ("
@echo " ___)...----)----....___"
@echo ' .-"" ) ( ""-.'
@echo " .-''''|-._ ( ) _.-|"
@echo ' / .--.| `""---...........---""` |'
@echo " / / | |"
@echo " | | | |"
@echo " \ \ | |"
@echo " '\ '\ | |"
@echo " '\ '| |"
@echo " _/ /\ /"
@echo " (__/ \ /"
@echo ' _..---""` \ /`""---.._'
@echo " .-' \ / '-."
@echo ": '-.__ __.-' :"
@echo ': ) ""---...---"" ( :'
@echo "\'._ '"--...___...--"' _.'"
@echo ' \""--..__ __..--""/'
@echo " '._ """----.....______.....----""" _.'"
@echo ' ""--..,,_____ _____,,..--"""'''
@echo ' """------"""'
${MAKE} coffee
.PHONY: all clean fclean re coffee

29
ft_get_size.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 22:50:35 by erey-bet #+# #+# */
/* Updated: 2022/10/11 22:14:29 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_get_size(long n)
{
long i;
if (n == 0)
return (1);
i = 0;
n *= (n > 0) - (n < 0);
while (n > 0)
{
i++;
n = n / 10;
}
return (i);
}

53
ft_itoa.c Normal file
View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
/* Updated: 2022/10/10 22:15:30 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_itoa_bis(char *str, int len, int i, long nl)
{
while (len > 1)
{
len--;
str[i] = (nl / ft_power(10, len) % 10) + 48;
i++;
}
str[i] = nl % 10 + 48;
i++;
str[i] = '\0';
}
char *ft_itoa(int n)
{
char *str;
int len;
int i;
long nl;
nl = n;
nl *= (n > 0) - (n < 0);
len = ft_get_size(nl);
i = 0;
if (n < 0)
{
str = malloc(len + 2);
if (str == NULL)
return (NULL);
str[i] = '-';
i++;
}
else
str = malloc(len + 1);
if (str == NULL)
return (NULL);
ft_itoa_bis(str, len, i, nl);
return (str);
}

30
ft_power.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 22:50:22 by erey-bet #+# #+# */
/* Updated: 2022/10/10 22:50:31 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_power(int nb, int power)
{
int i;
int new_nb;
if (power < 0)
return (0);
i = 0;
new_nb = 1;
while (i < power)
{
i++;
new_nb *= nb;
}
return (new_nb);
}

91
ft_printf.c Normal file
View file

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 16:27:59 by erey-bet #+# #+# */
/* Updated: 2022/10/15 23:19:38 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
static int ft_is_format(const char c)
{
return (c == 'c' || c == 's' || c == 'p' || c == 'i' || c == 'd'
|| c == 'u' || c == 'x' || c == 'X' || c == '%');
}
static int ft_post_character_bis(char format, va_list args)
{
int count;
count = 1;
if (format == 'c')
ft_putchar(va_arg(args, int));
else if (format == 's')
count = ft_putstr(va_arg(args, char *));
else if (format == 'd' || format == 'i')
count = ft_putnbr(va_arg(args, int));
else if (format == 'u')
count = ft_putunbr(va_arg(args, unsigned int));
else if (format == 'x')
count = ft_putnbrhex(va_arg(args, int));
else if (format == 'X')
count = ft_putnbrhex_upper(va_arg(args, int));
else if (format == '%')
ft_putchar('%');
else if (format == 'p')
count = ft_putvd(va_arg(args, void *));
return (count);
}
static int ft_post_character(char format, va_list args)
{
int count;
count = ft_post_character_bis(format, args);
if (count == -1)
{
ft_putstr("(null)");
count = 6;
}
else if (count == -2)
{
ft_putstr("(nil)");
count = 5;
}
return (count);
}
int ft_printf(const char *str, ...)
{
va_list args;
int i;
int y;
int count;
va_start(args, str);
i = -1;
y = 0;
count = 0;
while (str[++i])
{
if (str[i] == '%' && ft_is_format(str[i + 1]))
{
count += ft_post_character(str[i + 1], args);
i++;
y++;
}
else
{
ft_putchar(str[i]);
count++;
}
}
va_end(args);
return (count);
}

31
ft_printf.h Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 16:31:10 by erey-bet #+# #+# */
/* Updated: 2022/10/15 23:16:11 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stddef.h>
# include <stdlib.h>
# include <unistd.h>
void ft_putchar(char c);
int ft_putstr(char *s);
char *ft_itoa(int n);
int ft_power(int nb, int power);
int ft_get_size(long n);
int ft_putnbr(int n);
int ft_putunbr(unsigned int n);
int ft_putnbrhex(int v);
int ft_putnbrhex_upper(int v);
int ft_putvd(void *v);
#endif

18
ft_putchar.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 00:21:56 by erey-bet #+# #+# */
/* Updated: 2022/10/05 00:31:51 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}

39
ft_putnbr.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 10:22:16 by erey-bet #+# #+# */
/* Updated: 2022/10/10 22:49:10 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbr(int n)
{
int len;
int count;
long nl;
count = 0;
nl = n;
nl *= (n > 0) - (n < 0);
len = ft_get_size(nl);
if (n < 0)
{
ft_putchar('-');
count++;
}
while (len > 1)
{
--len;
ft_putchar(nl / ft_power(10, len) % 10 + 48);
count++;
}
ft_putchar(nl % 10 + 48);
count++;
return (count);
}

41
ft_putnbrhex.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */
/* Updated: 2022/10/15 22:20:19 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbrhex(int v)
{
int tmp;
int count;
if (v == 0)
ft_putchar('0');
count = 0;
if (v < 0)
{
write(1, "-", 1);
v *= -1;
count++;
}
tmp = 0;
while (v > 0)
{
tmp = v % 16;
if (tmp < 10)
ft_putchar(tmp + 48);
else
ft_putchar(tmp + 87);
v = v / 16;
count++;
}
return (count);
}

41
ft_putnbrhex_upper.c Normal file
View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrhex_upper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 23:24:36 by erey-bet #+# #+# */
/* Updated: 2022/10/15 22:20:49 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbrhex_upper(int v)
{
int tmp;
int count;
if (v == 0)
ft_putchar('0');
count = 0;
if (v < 0)
{
write(1, "-", 1);
v *= -1;
count++;
}
tmp = 0;
while (v > 0)
{
tmp = v % 16;
if (tmp < 10)
ft_putchar(tmp + 48);
else
ft_putchar(tmp + 55);
v = v / 16;
count++;
}
return (count);
}

29
ft_putstr.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 00:32:38 by erey-bet #+# #+# */
/* Updated: 2022/10/15 23:15:57 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr(char *s)
{
int count;
count = 0;
if (s == NULL)
return (-1);
while (*s)
{
ft_putchar(*s);
s++;
count++;
}
return (count);
}

29
ft_putunbr.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putunbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 23:11:19 by erey-bet #+# #+# */
/* Updated: 2022/10/15 00:02:36 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putunbr(unsigned int n)
{
unsigned int len;
unsigned long nl;
nl = n;
len = ft_get_size(nl);
while (len > 1)
{
--len;
ft_putchar(nl / ft_power(10, len) % 10 + 48);
}
ft_putchar(nl % 10 + 48);
return (ft_get_size(nl));
}

47
ft_putvd.c Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putvd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 23:01:21 by erey-bet #+# #+# */
/* Updated: 2022/10/15 22:15:15 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_putnbrhexull(unsigned long v)
{
int tmp;
int count;
count = 0;
tmp = 0;
while (v > 0)
{
tmp = v % 16;
if (tmp < 10)
ft_putchar(tmp + 48);
else
ft_putchar(tmp + 87);
v = v / 16;
count++;
}
return (count);
}
int ft_putvd(void *v)
{
int count;
unsigned long u;
if (v == NULL)
return (-2);
u = (unsigned long)v;
count = 2;
ft_putstr("0x");
count += ft_putnbrhexull(u);
return (count);
}