diff --git a/Makefile b/Makefile index 1fa3764..cd2c016 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gvoelkne +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2026/04/27 16:44:35 by gvoelkne #+# #+# # -# Updated: 2026/05/01 10:11:26 by gvoelkne ### ########.fr # +# Updated: 2026/05/01 11:39:38 by gvoelkne ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ NAME := libftprintf.a -SRC := ft_printf.c ft_btoa.c +SRC := ft_printf.c ft_btoa.c ft_putptr.c OBJ := $(SRC:.c=.o) diff --git a/ft_printf.c b/ft_printf.c index 200bca6..a34c1c3 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,13 +6,13 @@ /* By: gvoelkne +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/04/29 09:00:02 by gvoelkne #+# #+# */ -/* Updated: 2026/05/01 09:39:09 by gvoelkne ### ########.fr */ +/* Updated: 2026/05/01 11:56:39 by gvoelkne ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" -static int ft_strlen(char *str) +int ft_strlen(const char *str) { int len; @@ -41,10 +41,12 @@ static int ft_putchar(int c) static int format(char format, va_list args) { - if (format == 's') - return (ft_putstr(va_arg(args, char *))); if (format == 'c') return (ft_putchar(va_arg(args, int))); + if (format == 's') + return (ft_putstr(va_arg(args, char *))); + if (format == 'p') + return (ft_putptr(va_arg(args, void*))); if (format == '%') return (ft_putchar('%')); if (format == 'X') diff --git a/ft_printf.h b/ft_printf.h index b50f86f..95925c9 100644 --- a/ft_printf.h +++ b/ft_printf.h @@ -6,7 +6,7 @@ /* By: gvoelkne +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/04/29 09:07:17 by gvoelkne #+# #+# */ -/* Updated: 2026/05/01 08:45:31 by gvoelkne ### ########.fr */ +/* Updated: 2026/05/01 11:52:38 by gvoelkne ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,13 @@ # define FT_PRINTF_H # include # include +# include # include +int ft_strlen(const char*); + char *ft_btoa(int num, int base); +int ft_putptr(void *ptr); int ft_printf(const char *s, ...); diff --git a/ft_putptr.c b/ft_putptr.c new file mode 100644 index 0000000..f46b510 --- /dev/null +++ b/ft_putptr.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gvoelkne +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/05/01 11:36:45 by gvoelkne #+# #+# */ +/* Updated: 2026/05/01 12:01:55 by gvoelkne ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static unsigned int calc_digits(uintptr_t num, int base) +{ + int count; + + count = 0; + while (num > 0) + { + count += 1; + num /= base; + } + return (count); +} + +static char *ptr_to_hex(uintptr_t ptr) +{ + int curr; + char *res; + int res_size; + + curr = 0; + res_size = calc_digits(ptr, 16) + 3; + res = malloc(sizeof(char) * res_size); + if (!res) + return (NULL); + res[res_size--] = '\0'; + while (ptr > 0) + { + curr = ptr % 16; + if (curr > 9) + curr = (curr % 10) + 'A'; + else + curr += '0'; + res[(res_size - 1)] = curr; + ptr /= 16; + res_size -= 1; + } + res[0] = '0'; + res[1] = 'x'; + return (res); +} + +int ft_putptr(void *ptr) +{ + char *s; + if (!ptr) + s = "(null)"; + else + s = ptr_to_hex((uintptr_t)ptr); + write(1, s, ft_strlen(s)); + return (ft_strlen(s)); +} diff --git a/main.c b/main.c index b6c8558..932821f 100644 --- a/main.c +++ b/main.c @@ -45,10 +45,16 @@ void test(char *testname, char *s1, char *s2, unsigned int n) //* int main(void) { - ft_printf("%%: %%\n"); - ft_printf("%%s: %s\n", "ulla"); + void *ptr = ""; + void *null_ptr = NULL; + ft_printf("%%c, %c\n", 'A'); + ft_printf("%%s: %s\n", "ulla"); + ft_printf("%%p, %p\n", ptr); + printf("%%p (stdlib printf), '%p'\n", ptr); + ft_printf("%%p (null ptr), %p\n", null_ptr); ft_printf("%%X, %X\n", 1234); + ft_printf("%%: %%\n"); return (0); } //*/