mirror of
https://github.com/Ladebeze66/printf.git
synced 2026-11-09 13:27:09 +01:00
printf
This commit is contained in:
46
Makefile
Executable file
46
Makefile
Executable 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
|
||||
BIN
en.subject.pdf
Normal file
BIN
en.subject.pdf
Normal file
Binary file not shown.
37
ft_printf.c
Executable file
37
ft_printf.c
Executable 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
ft_printf.h
Executable file
28
ft_printf.h
Executable 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
|
||||
33
libftprintf/ft_decimal_pf.c
Executable file
33
libftprintf/ft_decimal_pf.c
Executable 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);
|
||||
}
|
||||
27
libftprintf/ft_hexnum_pf.c
Executable file
27
libftprintf/ft_hexnum_pf.c
Executable 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);
|
||||
}
|
||||
35
libftprintf/ft_print_type.c
Executable file
35
libftprintf/ft_print_type.c
Executable 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);
|
||||
}
|
||||
31
libftprintf/ft_ptrhex_pf.c
Executable file
31
libftprintf/ft_ptrhex_pf.c
Executable 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);
|
||||
}
|
||||
19
libftprintf/ft_putchar_pf.c
Executable file
19
libftprintf/ft_putchar_pf.c
Executable 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);
|
||||
}
|
||||
28
libftprintf/ft_putstr_pf.c
Executable file
28
libftprintf/ft_putstr_pf.c
Executable 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);
|
||||
}
|
||||
24
libftprintf/ft_unsigned_pf.c
Executable file
24
libftprintf/ft_unsigned_pf.c
Executable 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);
|
||||
}
|
||||
Reference in New Issue
Block a user