└── printf ├── ft_putchar.c ├── Makefile ├── ft_printf.h ├── ft_putnbr.c ├── ft_printf.c └── ft_putstr.c /printf/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/13 19:04:30 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/16 00:34:30 by eelhafia ### ########.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 | -------------------------------------------------------------------------------- /printf/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: eelhafia +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/11/13 18:34:16 by eelhafia #+# #+# # 9 | # Updated: 2022/11/16 01:22:17 by eelhafia ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libftprintf.a 14 | RM = rm -f 15 | AR = ar rc 16 | CC = cc -Wall -Wextra -Werror 17 | 18 | SRC = ft_printf.c ft_putnbr.c ft_putchar.c ft_putstr.c 19 | 20 | OBJ = $(SRC:.c=.o) 21 | 22 | all : $(NAME) 23 | 24 | $(NAME) : $(OBJ) 25 | $(AR) $(NAME) $(OBJ) 26 | 27 | %.o: %.c ft_printf.h 28 | $(CC) -c $< 29 | 30 | clean : 31 | $(RM) $(OBJ) 32 | 33 | fclean : clean 34 | $(RM) $(NAME) 35 | 36 | re : fclean all 37 | -------------------------------------------------------------------------------- /printf/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/13 18:59:27 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/16 01:17:06 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | int ft_printf(const char *form, ...); 21 | unsigned int ft_unputnbr(unsigned int a); 22 | int hixap(unsigned long long n); 23 | int hixad(unsigned int n, char c); 24 | int lenhixa(unsigned long long n); 25 | int ft_putnbr(int n); 26 | int llen(long n); 27 | int ft_putchar(char c); 28 | int ft_putstr(char *s); 29 | #endif -------------------------------------------------------------------------------- /printf/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/14 17:28:48 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/16 00:54:25 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int llen(long n) 16 | { 17 | int j; 18 | 19 | j = 0; 20 | if (n == 0) 21 | j++; 22 | if (n < 0) 23 | { 24 | n *= -1; 25 | j++; 26 | } 27 | while (n > 0) 28 | { 29 | n = n / 10; 30 | j++; 31 | } 32 | return (j); 33 | } 34 | 35 | int lenhixa(unsigned long long n) 36 | { 37 | int j; 38 | 39 | j = 0; 40 | if (n == 0) 41 | j++; 42 | while (n > 0) 43 | { 44 | n = n / 16; 45 | j++; 46 | } 47 | return (j); 48 | } 49 | 50 | unsigned int ft_unputnbr(unsigned int a) 51 | { 52 | if (a >= 0 && a <= 9) 53 | { 54 | ft_putchar(a + 48); 55 | } 56 | if (a > 9) 57 | { 58 | ft_putnbr (a / 10); 59 | ft_putchar(a % 10 + 48); 60 | } 61 | return (llen(a)); 62 | } 63 | 64 | int ft_putnbr(int n) 65 | { 66 | unsigned int a; 67 | 68 | a = n; 69 | if (n < 0) 70 | { 71 | a = n * -1; 72 | ft_putchar ('-'); 73 | } 74 | if (a >= 0 && a <= 9) 75 | { 76 | ft_putchar(a + 48); 77 | } 78 | if (a > 9) 79 | { 80 | ft_putnbr (a / 10); 81 | ft_putchar(a % 10 + 48); 82 | } 83 | return (llen(n)); 84 | } 85 | -------------------------------------------------------------------------------- /printf/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/13 21:42:58 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/16 01:26:31 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int check(char form, va_list args) 16 | { 17 | int count; 18 | long c; 19 | 20 | count = 0; 21 | c = 0; 22 | if (form == 'c') 23 | count += ft_putchar(va_arg(args, int)); 24 | else if (form == 'd' || form == 'i') 25 | count += ft_putnbr (va_arg(args, int)); 26 | else if (form == 's') 27 | count += ft_putstr(va_arg(args, char *)); 28 | else if (form == 'u') 29 | count += ft_unputnbr (va_arg(args, unsigned int)); 30 | else if (form == 'p') 31 | { 32 | count += ft_putstr("0x"); 33 | count += hixap ((long)va_arg(args, void *)); 34 | } 35 | else if (form == 'x' || form == 'X') 36 | count += hixad(va_arg(args, unsigned int), form); 37 | else 38 | count += ft_putchar(form); 39 | return (count); 40 | } 41 | 42 | int ft_printf(const char *form, ...) 43 | { 44 | int i; 45 | int a; 46 | va_list args; 47 | 48 | va_start(args, form); 49 | i = 0; 50 | a = 0; 51 | while (form[i]) 52 | { 53 | if (form[i] == '%') 54 | { 55 | a += check(form[i +1], args); 56 | i++; 57 | } 58 | else 59 | a += ft_putchar(form[i]); 60 | i++; 61 | } 62 | va_end (args); 63 | return (a); 64 | } 65 | -------------------------------------------------------------------------------- /printf/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/14 17:43:04 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/16 01:20:12 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_putstr(char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!s) 21 | { 22 | write(1, "(null)", 6); 23 | return (6); 24 | } 25 | while (s[i]) 26 | { 27 | ft_putchar(s[i]); 28 | i++; 29 | } 30 | return (i); 31 | } 32 | 33 | void helphixad(unsigned int n) 34 | { 35 | char *ss; 36 | 37 | ss = "0123456789ABCDEF"; 38 | if (n >= 0 && n <= 15) 39 | ft_putchar(ss[n]); 40 | else if (n > 9) 41 | { 42 | helphixad(n / 16); 43 | ft_putchar(ss[n % 16]); 44 | } 45 | } 46 | 47 | int hixad(unsigned int n, char c) 48 | { 49 | char *s; 50 | 51 | s = "0123456789abcdef"; 52 | if (c == 'x') 53 | { 54 | if (n >= 0 && n <= 15) 55 | ft_putchar(s[n]); 56 | else if (n > 9) 57 | { 58 | hixad(n / 16, c); 59 | ft_putchar(s[n % 16]); 60 | } 61 | } 62 | else if (c == 'X') 63 | { 64 | helphixad(n); 65 | } 66 | return (lenhixa(n)); 67 | } 68 | 69 | int hixap(unsigned long long n) 70 | { 71 | char *s; 72 | 73 | s = "0123456789abcdef"; 74 | if (n >= 0 && n <= 15) 75 | ft_putchar(s[n]); 76 | else if (n > 9) 77 | { 78 | hixap(n / 16); 79 | ft_putchar(s[n % 16]); 80 | } 81 | return (lenhixa(n)); 82 | } 83 | --------------------------------------------------------------------------------