diff --git a/Makefile b/Makefile index d6fdeee..0036ced 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/15 00:23:05 by gbaconni ### lausanne.ch # +# Updated: 2022/04/15 17:19:14 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -31,23 +31,25 @@ HDR = $(LIBFTPRINTF)/libftprintf.h CC = gcc CFLAGS = -Wall -Wextra -Werror ifeq ($(FAST), 1) - CFLAGS += -v -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops - #CFLAGS += -v -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer + CFLAGS += -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops + #CFLAGS += -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer endif ifeq ($(DEBUG), 1) - CFLAGS += -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow - #CFLAGS += -v -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow + CFLAGS += -g -O0 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow +endif +ifeq ($(LEAK), 1) + CFLAGS += -g -O0 +endif + +VALGRIND = valgrind +VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all +ifeq ($(LEAK), 1) + PREFIX += $(VALGRIND) $(VALGRINDFLAGS) endif RM = rm RMFLAGS = -f -VALGRIND = valgrind -VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all -ifeq ($(DEBUG), 1) - PREFIX += $(VALGRIND) $(VALGRINDFLAGS) -endif - NORMINETTE = norminette NORMINETTEFLAGS = -o @@ -66,17 +68,19 @@ fclean: clean @$(MAKE) -C $(LIBFTPRINTF) fclean re: fclean all + @$(MAKE) -C $(LIBFTPRINTF) re check: @$(MAKE) -C $(LIBFTPRINTF) check +leak: + @$(MAKE) -C $(LIBFTPRINTF) re LEAK=1 + debug: @$(MAKE) -C $(LIBFTPRINTF) re DEBUG=1 - @$(MAKE) re DEBUG=1 fast: @$(MAKE) -C $(LIBFTPRINTF) re FAST=1 - @$(MAKE) re FAST=1 test: clean $(NAME) @$(PREFIX) ./$(NAME) "%c" C || true @@ -86,11 +90,15 @@ test: clean $(NAME) @$(PREFIX) ./$(NAME) "%X" 42 || true @$(PREFIX) ./$(NAME) "42" "" || true @$(PREFIX) ./$(NAME) "%%" "" || true + @$(PREFIX) ./$(NAME) "%p" "" || true -test2: debug +test2: leak + @$(MAKE) test LEAK=1 + +test3: debug @$(MAKE) test DEBUG=1 -test3: fast +test4: fast @$(MAKE) test FAST=1 update: config online pull diff --git a/libftprintf/Makefile b/libftprintf/Makefile index 12593f7..d9e14e4 100644 --- a/libftprintf/Makefile +++ b/libftprintf/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/14 23:54:03 by gbaconni ### lausanne.ch # +# Updated: 2022/04/15 17:19:30 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -52,12 +52,20 @@ HDR = libftprintf.h CC = gcc CFLAGS = -Wall -Wextra -Werror ifeq ($(FAST), 1) - CFLAGS += -v -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops - #CFLAGS += -v -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer + CFLAGS += -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops + #CFLAGS += -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer endif ifeq ($(DEBUG), 1) - CFLAGS += -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow - #CFLAGS += -v -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow + CFLAGS += -g -O0 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow +endif +ifeq ($(LEAK), 1) + CFLAGS += -g -O0 +endif + +VALGRIND = valgrind +VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all +ifeq ($(LEAK), 1) + PREFIX += $(VALGRIND) $(VALGRINDFLAGS) endif AR = ar @@ -102,6 +110,9 @@ so: $(OBJ) $(OBJ_BONUS) check: @$(NORMINETTE) $(NORMINETTEFLAGS) $(HDR) $(SRC) ${SRC_BONUS} 2>&1 | grep -v '\.[ch]: OK!' || true +leak: + @$(MAKE) re LEAK=1 + debug: @$(MAKE) re DEBUG=1 diff --git a/libftprintf/ft_vprintf.c b/libftprintf/ft_vprintf.c index 733439b..57d9c93 100644 --- a/libftprintf/ft_vprintf.c +++ b/libftprintf/ft_vprintf.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:43:52 by gbaconni #+# #+# */ -/* Updated: 2022/04/15 00:25:52 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/15 16:37:06 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int ft_vprintf(char const *format, va_list ap) { if (*format == '%') { - if (*format++ == '%') + if (*++format == '%') ret += ft_vprintf_percent(format, ap); else if (*format == 'c') ret += ft_vprintf_char(format, ap); diff --git a/libftprintf/ft_vprintf_pointer.c b/libftprintf/ft_vprintf_pointer.c index 3e509a0..f305224 100644 --- a/libftprintf/ft_vprintf_pointer.c +++ b/libftprintf/ft_vprintf_pointer.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:56:16 by gbaconni #+# #+# */ -/* Updated: 2022/04/13 08:00:14 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/15 17:12:36 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -14,7 +14,25 @@ int ft_vprintf_pointer(const char *format, va_list ap) { - (void) ap; + int ret; + int len; + void *p; + char *s; + (void) format; - return (0); + ret = 0; + len = 0; + p = va_arg(ap, void *); + if (p == NULL) + { + len = ft_strlen(NIL); + write(1, NIL, len); + return (len); + } + s = ft_itoa_base((unsigned long) p, "0123456789ABCDEF"); + len = ft_strlen(s); + write(1, s, len); + ret += len; + free(s); + return (ret); } diff --git a/libftprintf/libftprintf.h b/libftprintf/libftprintf.h index e1d20e2..9349c2d 100644 --- a/libftprintf/libftprintf.h +++ b/libftprintf/libftprintf.h @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/18 15:26:06 by gbaconni #+# #+# */ -/* Updated: 2022/04/14 23:56:24 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/15 16:50:38 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -17,6 +17,12 @@ # include # include +# if defined (__APPLE__) +# define NIL "0x0" +# else +# define NIL "(nil)" +# endif + /* Helper */ char *ft_itoa_base(int n, char *base); diff --git a/main.c b/main.c index 3a4a7a5..5e96ba5 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: baco +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */ -/* Updated: 2022/04/13 07:54:40 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/15 17:19:36 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -75,10 +75,14 @@ int main(int argc, char *argv[]) int d; char c; char *s; + int x; + void *ptr; d = 0; c = '\0'; s = ""; + x = 42; + ptr = &x; (void)s; if (argc > 1) { @@ -107,6 +111,14 @@ int main(int argc, char *argv[]) ret = ft_printf(format, s); printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s); } + else if (ft_strlen(argv[2]) == 0) + { + s = argv[2]; + ret = printf(format, ptr); + printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s); + ret = ft_printf(format, ptr); + printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s); + } else { printf("Error: invalid argument\n");