some fix on printf

This commit is contained in:
2026-04-06 02:50:46 +02:00
parent b0a77bc18c
commit b620f5ecdb
12 changed files with 58 additions and 45 deletions
+1 -1
View File
@@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_printf_bonus.h"
#include <stdlib.h>
t_arg *ft_create_arg(void)
{
+2 -2
View File
@@ -12,8 +12,8 @@
static int is_main_arg(char c)
{
if (c == 'c' || c == 's' || c == 'p' || c == 'd'
|| c == 'i' || c == 'u' || c == 'x' || c == 'X' || c == '%')
if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u'
|| c == 'x' || c == 'X' || c == '%')
return (1);
return (0);
}
+4 -4
View File
@@ -41,12 +41,12 @@ static void ft_parse_str(t_arg *arg, char *str, char main_arg)
if (*str == '.')
{
arg->dot = ft_atoi(str + 1);
if (!(arg->minus >= 0 || arg->hash >= 0
|| arg->space >= 0 || arg->plus >= 0))
if (!(arg->minus >= 0 || arg->hash >= 0 || arg->space >= 0
|| arg->plus >= 0))
get_padding(arg, str);
}
else if (!(arg->minus >= 0 || arg->hash >= 0
|| arg->space >= 0 || arg->plus >= 0))
else if (!(arg->minus >= 0 || arg->hash >= 0 || arg->space >= 0
|| arg->plus >= 0))
get_padding(arg, str);
}
+18 -11
View File
@@ -71,6 +71,16 @@ static int ft_to_skip(char *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, ...)
{
va_list args;
@@ -78,23 +88,20 @@ int ft_printf(const char *first_arg, ...)
int i;
int temp;
if (!first_arg)
return (-1);
nb_print = 0;
i = -1;
va_start(args, first_arg);
i = 0;
while (first_arg[i])
while (first_arg[++i])
{
temp = nb_print;
if (first_arg[i] == '%' && first_arg[i + 1] != '\0')
{
i++;
nb_print += ft_print_arg((char *)&first_arg[i], args);
i += ft_to_skip((char *)&first_arg[i]);
}
else
if (first_arg[i] == '%' && first_arg[i + 1])
nb_print += handle_arg(first_arg, args, &i);
else if (first_arg[i] != '%')
nb_print += write(1, &first_arg[i], 1);
if (temp > nb_print)
if (temp >= nb_print)
return (-1);
i++;
}
va_end(args);
return (nb_print);
+2 -2
View File
@@ -40,8 +40,8 @@ int ft_putnbr_base(unsigned int nbr, char *base, int base_size, int limit)
return (ft_putnbr_base_extra(nbr, base, base_size));
}
static int ft_putnbr_base_ll_extra(unsigned long long nbr
, char *base, int base_size)
static int ft_putnbr_base_ll_extra(unsigned long long nbr, char *base,
int base_size)
{
int i;
char temp;
+1 -1
View File
@@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf_bonus.h"
#include "libft.h"
#include <unistd.h>
int print_pointer(t_arg *arg, unsigned long long p)
+1 -1
View File
@@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf_bonus.h"
#include "libft.h"
int print_str(t_arg *arg, char *str)
{
+1
View File
@@ -13,6 +13,7 @@
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
int ft_printf(const char *first_arg, ...) __attribute__((format(printf, 1, 2)));
int ft_printf(const char *first_arg, ...);
int ft_putnbr_base(unsigned long long nbr, char *base, int base_size);
+6 -2
View File
@@ -25,10 +25,14 @@ typedef struct s_arg
int plus;
} t_arg;
int ft_printf(const char *first_arg, ...) __attribute__((format(printf,
1, 2)));
int ft_printf(const char *first_arg, ...);
int ft_putnbr_base_ll(unsigned long long nbr, char *base, int base_size);
int ft_putnbr_base(unsigned int nbr, char *base, int base_size, int limit);
int ft_putnbr_base_ll(unsigned long long nbr, char *base,
int base_size);
int ft_putnbr_base(unsigned int nbr, char *base, int base_size,
int limit);
char ft_main_arg(char *str);
t_arg *ft_create_arg(void);
t_arg *ft_parsing(char *str);
+6 -5
View File
@@ -42,10 +42,12 @@ int ft_printf(const char *first_arg, ...)
int i;
int temp;
if (!first_arg)
return (-1);
nb_print = 0;
va_start(args, first_arg);
i = 0;
while (first_arg[i])
i = -1;
while (first_arg[++i])
{
temp = nb_print;
if (first_arg[i] == '%' && first_arg[i + 1] != '\0')
@@ -53,11 +55,10 @@ int ft_printf(const char *first_arg, ...)
i++;
nb_print += ft_print_arg(first_arg[i], args);
}
else
else if (first_arg[i] != '%')
nb_print += write(1, &first_arg[i], 1);
if (temp > nb_print)
if (temp >= nb_print)
return (-1);
i++;
}
va_end(args);
return (nb_print);
+1 -1
View File
@@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf.h"
#include "libft.h"
int print_pointer(unsigned long long p)
{