Merge mandatory and bonus into a single ft_printf, fix edge cases, add README

- Use the flag-aware implementation for both make and make bonus
  (bonus/ moved to src/, _bonus suffix dropped, bonus rule = all)
- Do not treat zero-length output (%s with "", %.0d with 0) as an error
- Return -1 without printing when a % has no conversion ("abc%", "%5")
- Keep field width with '#', '+' and ' '; ignore '0' with '-'
- Fix '#' prefix on zero, '+'/' ' with zero padding, INT_MIN negation UB
- Add README.md required by the subject

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
da7eandClaude Opus 5.5 committed 2026-10-08 22:22:28 +02:00
1 parent b620f5ecdb
commit c8a9ee10cf
24 files changed
+608 -678

No files matched your search

+73 -28
View File
@@ -6,33 +6,81 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 10:54:03 by dgaillet #+# #+# */
/* Updated: 2025/11/21 14:47:19 by dgaillet ### ########lyon.fr */
/* Updated: 2025/11/21 14:59:50 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
static int ft_print_arg(char c, va_list args)
static int ft_print_arg_flags(t_arg *arg, va_list args)
{
if (c == 'c')
return (print_char(va_arg(args, int)));
else if (c == 's')
return (print_str(va_arg(args, char *)));
else if (c == 'p')
return (print_pointer(va_arg(args, unsigned long long)));
else if (c == 'd' || c == 'i')
return (print_number(va_arg(args, int)));
else if (c == 'u')
return (print_unsigned(va_arg(args, unsigned int)));
else if (c == 'x')
return (print_hex(va_arg(args, unsigned int), 0));
else if (c == 'X')
return (print_hex(va_arg(args, unsigned int), 1));
else if (c == '%')
return (print_char('%'));
return (0);
int count;
count = 0;
if (arg->arg == 'c')
count += print_char(arg, va_arg(args, int));
else if (arg->arg == 's')
count += print_str(arg, va_arg(args, char *));
else if (arg->arg == 'p')
count += print_pointer(arg, va_arg(args, long long));
else if (arg->arg == 'd' || arg->arg == 'i')
count += print_number(arg, va_arg(args, int));
else if (arg->arg == 'u')
count += print_unsigned(arg, va_arg(args, unsigned int));
else if (arg->arg == 'x' || arg->arg == 'X')
count += print_hex(arg, va_arg(args, unsigned int));
else
return (-1);
if (count < 0)
return (-1);
count += print_chars(arg->minus - count, ' ');
if (count < 0)
return (-1);
return (count);
}
static int ft_print_arg(char *str, va_list args)
{
t_arg *arg;
int count;
arg = ft_parsing(str);
if (!arg)
return (-1);
if (arg->arg == '%')
count = write(1, "%", 1);
else
count = ft_print_arg_flags(arg, args);
free(arg);
return (count);
}
static int ft_to_skip(char *str)
{
int count;
count = 0;
while (!(*str == 'c' || *str == 's' || *str == 'p' || *str == 'd'
|| *str == 'i' || *str == 'u' || *str == 'x' || *str == 'X'
|| *str == '%') && *str)
{
count++;
str++;
}
return (count);
}
static int handle_arg(const char *s, va_list args, int *i)
{
int n;
(*i)++;
n = ft_print_arg((char *)&s[*i], args);
*i += ft_to_skip((char *)&s[*i]);
return (n);
}
int ft_printf(const char *first_arg, ...)
@@ -42,22 +90,19 @@ int ft_printf(const char *first_arg, ...)
int i;
int temp;
if (!first_arg)
if (!first_arg || !ft_check_format(first_arg))
return (-1);
nb_print = 0;
va_start(args, first_arg);
i = -1;
va_start(args, first_arg);
while (first_arg[++i])
{
temp = nb_print;
if (first_arg[i] == '%' && first_arg[i + 1] != '\0')
{
i++;
nb_print += ft_print_arg(first_arg[i], args);
}
else if (first_arg[i] != '%')
if (first_arg[i] == '%')
nb_print += handle_arg(first_arg, args, &i);
else
nb_print += write(1, &first_arg[i], 1);
if (temp >= nb_print)
if (temp > nb_print)
return (-1);
}
va_end(args);