├── Makefile ├── README.md ├── ft_formats.c ├── ft_print_addr.c ├── ft_print_hex.c ├── ft_printf.c ├── ft_printf.h └── ft_printf_utils.c /Makefile: -------------------------------------------------------------------------------- 1 | SRCS = ft_printf.c \ 2 | ft_formats.c \ 3 | ft_printf_utils.c \ 4 | ft_print_hex.c \ 5 | ft_print_addr.c 6 | 7 | OBJS = ${SRCS:.c=.o} 8 | 9 | NAME = libftprintf.a 10 | CC = gcc 11 | CFLAGS = -Wall -Wextra -Werror 12 | RM = rm -f 13 | 14 | all: ${NAME} 15 | 16 | .c.o: 17 | 18 | ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} 19 | 20 | $(NAME): $(OBJS) 21 | ar -rc $(NAME) $(OBJS) 22 | 23 | clean: 24 | ${RM} ${OBJS} 25 | 26 | fclean: clean 27 | ${RM} ${NAME} 28 | 29 | re: fclean all 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FT_Printf -------------------------------------------------------------------------------- /ft_formats.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_formats.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/07 20:51:11 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/14 20:05:11 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_formats(const char *s, va_list ap, int i) 16 | { 17 | int len; 18 | 19 | len = 0; 20 | if (s[i] == 'c') 21 | len += ft_putchar(va_arg(ap, int)); 22 | else if (s[i] == 's') 23 | len += ft_putstr(va_arg(ap, char *)); 24 | else if (s[i] == 'd') 25 | len += ft_putnbr(va_arg(ap, int)); 26 | else if (s[i] == 'i') 27 | len += ft_putnbr(va_arg(ap, int)); 28 | else if (s[i] == 'u') 29 | len += ft_putnbr_u(va_arg(ap, unsigned int)); 30 | else if (s[i] == '%') 31 | len += ft_print_percent(); 32 | else if (s[i] == 'x') 33 | len += ft_print_hex(va_arg(ap, unsigned int), 'x'); 34 | else if (s[i] == 'X') 35 | len += ft_print_hex(va_arg(ap, unsigned int), 'X'); 36 | else if (s[i] == 'p') 37 | len += ft_print_addr(va_arg(ap, unsigned long)); 38 | return (len); 39 | } 40 | -------------------------------------------------------------------------------- /ft_print_addr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_addr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/15 16:32:48 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/15 16:32:49 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_addr(unsigned long long n) 16 | { 17 | int len; 18 | 19 | len = 0; 20 | len += write(1, "0x", 2); 21 | len += ft_print_hex(n, 'x'); 22 | return (len); 23 | } 24 | -------------------------------------------------------------------------------- /ft_print_hex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_hex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/15 15:29:35 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/15 15:29:36 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void ft_put_hex(unsigned long long n, char *base) 16 | { 17 | if (n < 16) 18 | ft_putchar(base[n]); 19 | else if (n > 15) 20 | { 21 | ft_put_hex(n / 16, base); 22 | ft_put_hex(n % 16, base); 23 | } 24 | else 25 | ft_putchar(n + 48); 26 | } 27 | 28 | int ft_print_hex(unsigned long long n, char format) 29 | { 30 | char *base_x; 31 | char *base_xx; 32 | int len; 33 | 34 | base_x = "0123456789abcdef"; 35 | base_xx = "0123456789ABCDEF"; 36 | len = 0; 37 | if (n == 0) 38 | len += write(1, "0", 1); 39 | else 40 | { 41 | if (format == 'x') 42 | ft_put_hex(n, base_x); 43 | if (format == 'X') 44 | ft_put_hex(n, base_xx); 45 | } 46 | while (n) 47 | { 48 | n /= 16; 49 | ++len; 50 | } 51 | return (len); 52 | } 53 | -------------------------------------------------------------------------------- /ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/07 12:51:48 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/14 19:50:35 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_printf(const char *s, ...) 16 | { 17 | va_list ap; 18 | int i; 19 | int len; 20 | 21 | len = 0; 22 | i = 0; 23 | va_start(ap, s); 24 | while (s[i]) 25 | { 26 | if (s[i] == '%') 27 | { 28 | len += ft_formats(s, ap, ++i); 29 | } 30 | else 31 | len += ft_putchar(s[i]); 32 | i++; 33 | } 34 | va_end(ap); 35 | return (len); 36 | } 37 | -------------------------------------------------------------------------------- /ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/15 21:16:47 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/15 21:16:48 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | 19 | int ft_printf(const char *s, ...); 20 | int ft_putchar(char c); 21 | int ft_putstr(char *str); 22 | int ft_formats(const char *str, va_list ap, int i); 23 | int ft_putnbr(int n); 24 | int ft_print_percent(void); 25 | int ft_putnbr_u(unsigned long n); 26 | int ft_print_hex(unsigned long long n, char format); 27 | int ft_print_addr(unsigned long long n); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /ft_printf_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: hahadiou +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/10/14 16:59:16 by hahadiou #+# #+# */ 9 | /* Updated: 2022/10/14 20:04:16 by hahadiou ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | return (1); 19 | } 20 | 21 | int ft_putstr(char *s) 22 | { 23 | int i; 24 | 25 | i = -1; 26 | if (s == NULL) 27 | { 28 | ft_putstr("(null)"); 29 | return (6); 30 | } 31 | while (s[++i]) 32 | write(1, &s[i], 1); 33 | return (i); 34 | } 35 | 36 | int ft_putnbr(int n) 37 | { 38 | int len; 39 | 40 | len = 0; 41 | if (n == -2147483648) 42 | return (write(1, "-2147483648", 11)); 43 | if (n < 0) 44 | { 45 | len += ft_putchar('-'); 46 | n = -n; 47 | } 48 | if (n > 9) 49 | len += ft_putnbr(n / 10); 50 | len += ft_putchar(n % 10 + 48); 51 | return (len); 52 | } 53 | 54 | int ft_print_percent(void) 55 | { 56 | write(1, "%", 1); 57 | return (1); 58 | } 59 | 60 | int ft_putnbr_u(unsigned long n) 61 | { 62 | int len; 63 | 64 | len = 0; 65 | if (n > 9) 66 | len += ft_putnbr_u(n / 10); 67 | len += ft_putchar(n % 10 + 48); 68 | return (len); 69 | } 70 | --------------------------------------------------------------------------------