From 7ac66b7410d78a8188da72f2bd8e0020d693fdd2 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Sat, 11 Nov 2023 17:58:42 +0000 Subject: [PATCH 1/2] Handled if str == NULL --- print_literals.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/print_literals.c b/print_literals.c index 41c1877..15a0bba 100644 --- a/print_literals.c +++ b/print_literals.c @@ -1,6 +1,6 @@ #include -#include #include "main.h" +#include /** * print_char - prints char @@ -28,8 +28,8 @@ int print_string(va_list ap) int char_count = 0; char *str = va_arg(ap, char *); - if (!str) - print_error("The argument does not match %s specifier"); + if (str == NULL) + return (0); while (*str) { char_count += write(1, str, 1); From b64bc100ae3f2fdb285d170a09e0bb654d1f58b3 Mon Sep 17 00:00:00 2001 From: NairaMescudi Date: Sun, 12 Nov 2023 07:22:17 +0000 Subject: [PATCH 2/2] Made changes to _printf.c to address if format == NULL --- _printf.c | 16 +++++++++++----- main.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/_printf.c b/_printf.c index 86177f3..7817ecc 100644 --- a/_printf.c +++ b/_printf.c @@ -1,5 +1,5 @@ #include -#include +#include #include "main.h" /** @@ -18,6 +18,8 @@ int _printf(const char *format, ...) if (!(*format)) write(1, "User input is needed", 21); + if (format == NULL) + print_error("Cannot enter NULL as printf format string"); while (*format) { @@ -33,10 +35,10 @@ int _printf(const char *format, ...) } /** - * p_func - executes print function - * @ap: Variadic arguments - * @specifier: Format type pointer - * + * p_func - Auxilliary Function + * Description: It calls other print functions based on the specifier + * @ap: Argument Pointer + * @specifier: Format Specifier Character * Return: Number of chars printed */ int p_func(va_list ap, char specifier) @@ -54,6 +56,10 @@ int p_func(va_list ap, char specifier) case '%': char_count += write(1, "%", 1); break; + case '\0': + print_error("Invalid Format"); + exit(1); + break; } return (char_count); diff --git a/main.h b/main.h index 4d68fee..4e6d399 100644 --- a/main.h +++ b/main.h @@ -1,5 +1,6 @@ #ifndef MAIN_H #define MAIN_H + #include int _printf(const char *format, ...);