From a66aec036337673f4bc41480d1c103f5b4fd0761 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 21 Dec 2021 10:07:39 +0100 Subject: [PATCH] First release --- .gitmodules | 3 +++ Makefile | 34 ++++++++++++++++++++++++++++++ ft_printf.c | 39 ++++++++++++++++++++++++++++++++++ ft_printf.h | 33 +++++++++++++++++++++++++++++ ft_putchars.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ ft_putnbrs.c | 47 +++++++++++++++++++++++++++++++++++++++++ ft_putptr.c | 31 +++++++++++++++++++++++++++ ft_putx.c | 47 +++++++++++++++++++++++++++++++++++++++++ libft | 1 + 9 files changed, 293 insertions(+) create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 ft_printf.c create mode 100644 ft_printf.h create mode 100644 ft_putchars.c create mode 100644 ft_putnbrs.c create mode 100644 ft_putptr.c create mode 100644 ft_putx.c create mode 160000 libft diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..83b9355 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = git@45.76.46.66:narnaud/libft.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..30c0e10 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +NAME = libftprintf.a + +SRCS = ft_printf.c ft_putchars.c ft_putnbrs.c ft_putptr.c ft_putx.c +OBJS = ${SRCS:.c=.o} +MAIN = main.c +MAIN_O = ${MAIN:.c=.o} + +RM = rm -rf +CC = gcc +CFLAGS = -Werror -Wall -Wextra +AR = ar -rcs + +.c.o: + ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} + +all: $(NAME) + +$(NAME): $(OBJS) + ${MAKE} -C ./libft + cp ./libft/libft.a libftprintf.a + ${AR} ${NAME} ${OBJS} + +clean: + ${RM} ${OBJS} + ${RM} ./libft/*.o + +fclean: clean + ${RM} ${NAME} + ${RM} ./libft/libft.a + + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ft_printf.c b/ft_printf.c new file mode 100644 index 0000000..cff61af --- /dev/null +++ b/ft_printf.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */ +/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./libft/libft.h" +#include "ft_printf.h" + +int ft_printf(const char *str, ...) +{ + va_list args; + size_t n; + size_t i; + const char *g_types = {"cspdiuxX%"}; + static int (*g_printf_fcts[9])(va_list arg, const char *str) = {\ + va_putchar, va_putstr, va_putptr, va_putnbr, va_putnbr, va_putunsign, \ + va_putx, va_putx_cap, va_putperc}; + + i = 0; + va_start (args, str); + while (*str) + { + n = -1; + if ((*str == '%' && str++) || !(++i && ft_putchar((int)*str))) + while (++n < 9) + if (g_types[n] == *str) + i += (g_printf_fcts[n])(args, str); + str++; + } + va_end(args); + return (i); +} diff --git a/ft_printf.h b/ft_printf.h new file mode 100644 index 0000000..1987d43 --- /dev/null +++ b/ft_printf.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 08:09:45 by narnaud #+# #+# */ +/* Updated: 2021/12/21 09:39:34 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# define MEM_ADD_SIZE 6 + +# include "./libft/libft.h" +# include + +int ft_putchar(int ch); +int ft_putstr(char *str); +int va_putchar(va_list va_ch, const char *str); +int va_putstr(va_list va_str, const char *str); +int va_putptr(va_list va_ptr, const char *str); +int va_putnbr(va_list va_int, const char *str); +int va_putunsign(va_list va_uint, const char *str); +int va_putx(va_list va_uint, const char *str); +int va_putx_cap(va_list va_uint, const char *str); +int va_putperc(va_list va, const char *str); +int ft_printf(const char *str, ...); + +#endif diff --git a/ft_putchars.c b/ft_putchars.c new file mode 100644 index 0000000..7845979 --- /dev/null +++ b/ft_putchars.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:17:03 by narnaud #+# #+# */ +/* Updated: 2021/11/17 09:57:53 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putchar(int ch) +{ + char c; + + c = (char)ch; + write(1, &c, 1); + return (1); +} + +int ft_putstr(char *str) +{ + int i; + + i = 0; + if (!str) + { + ft_putstr("(null)"); + return (6); + } + while (str[i]) + ft_putchar(str[i++]); + return (i); +} + +int va_putchar(va_list va_ch, const char *str) +{ + (void)*str; + ft_putchar(va_arg(va_ch, int)); + return (1); +} + +int va_putstr(va_list va_str, const char *str) +{ + (void)*str; + return (ft_putstr(va_arg(va_str, char *))); +} + +int va_putperc(va_list va, const char *str) +{ + (void)*str; + (void)va; + ft_putchar('%'); + return (1); +} diff --git a/ft_putnbrs.c b/ft_putnbrs.c new file mode 100644 index 0000000..8e4dfe6 --- /dev/null +++ b/ft_putnbrs.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:34:01 by narnaud #+# #+# */ +/* Updated: 2021/12/21 10:06:28 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putnbr(int n, const char *str) +{ + char *n_str; + int ret; + + (void)*str; + n_str = ft_itoa(n); + ret = ft_putstr(n_str); + free(n_str); + return (ret); +} + +int ft_putunsign(unsigned int n, const char *str) +{ + char *n_str; + int ret; + + (void)*str; + n_str = ft_utoa(n); + ret = ft_putstr(n_str); + free(n_str); + return (ret); +} + +int va_putnbr(va_list va, const char *str) +{ + return (ft_putnbr(va_arg(va, int), str)); +} + +int va_putunsign(va_list va, const char *str) +{ + return (ft_putunsign(va_arg(va, unsigned int), str)); +} diff --git a/ft_putptr.c b/ft_putptr.c new file mode 100644 index 0000000..d4d3386 --- /dev/null +++ b/ft_putptr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 10:13:07 by narnaud #+# #+# */ +/* Updated: 2021/12/21 09:50:41 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putptr(void *ptr) +{ + char *str; + int ret; + + str = ft_itox((unsigned long long int)ptr, "0123456789abcdef"); + ft_putstr("0x"); + ret = ft_putstr(str); + free(str); + return (ret + 2); +} + +int va_putptr(va_list va_ptr, const char *str) +{ + (void)*str; + return (ft_putptr(va_arg(va_ptr, void *))); +} diff --git a/ft_putx.c b/ft_putx.c new file mode 100644 index 0000000..7652079 --- /dev/null +++ b/ft_putx.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/27 09:54:24 by narnaud #+# #+# */ +/* Updated: 2021/11/17 10:24:24 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_putx(unsigned int n) +{ + char *str; + int ret; + + str = ft_itox((unsigned long int)n, "0123456789abcdef"); + ret = ft_putstr(str); + free(str); + return (ret); +} + +int ft_putx_cap(unsigned int n) +{ + char *str; + int ret; + + str = ft_itox((unsigned long int)n, "0123456789ABCDEF"); + ret = ft_putstr(str); + free(str); + return (ret); +} + +int va_putx(va_list va_uint, const char *str) +{ + (void)*str; + return (ft_putx(va_arg(va_uint, unsigned int))); +} + +int va_putx_cap(va_list va_uint, const char *str) +{ + (void)*str; + return (ft_putx_cap(va_arg(va_uint, unsigned int))); +} diff --git a/libft b/libft new file mode 160000 index 0000000..0ba7ffd --- /dev/null +++ b/libft @@ -0,0 +1 @@ +Subproject commit 0ba7ffd7c6a28b0f02cc937d32773fb9d4095eca