fixed error return
This commit is contained in:
parent
16a9ae0f60
commit
e30f6f7fa8
30
ft_printf.c
30
ft_printf.c
@ -14,6 +14,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
#include "libft/libft.h"
|
||||||
#include "ft_printf.h"
|
#include "ft_printf.h"
|
||||||
|
|
||||||
int ft_isbase(char *str)
|
int ft_isbase(char *str)
|
||||||
@ -161,7 +162,7 @@ int print_hex(va_list args, int uppercase)
|
|||||||
return (wrote);
|
return (wrote);
|
||||||
}
|
}
|
||||||
|
|
||||||
int print_pointer(va_list args, int uppercase)
|
int print_pointer(va_list args)
|
||||||
{
|
{
|
||||||
long n;
|
long n;
|
||||||
size_t len;
|
size_t len;
|
||||||
@ -178,13 +179,11 @@ int print_pointer(va_list args, int uppercase)
|
|||||||
str = ft_calloc(sizeof(char), 20);
|
str = ft_calloc(sizeof(char), 20);
|
||||||
if (!str)
|
if (!str)
|
||||||
return (wrote);
|
return (wrote);
|
||||||
if (uppercase)
|
|
||||||
ft_putnbr_base(n, "0123456789ABCDEF", str);
|
ft_strlcpy(str, "0x", 3);
|
||||||
else
|
|
||||||
ft_putnbr_base(n, "0123456789abcdef", str);
|
ft_putnbr_base(n, "0123456789abcdef", str);
|
||||||
len = ft_strlen(str);
|
len = ft_strlen(str);
|
||||||
wrote = write(1, "0x", 2);
|
wrote = write(1, str, len);
|
||||||
wrote += write(1, str, len);
|
|
||||||
free(str);
|
free(str);
|
||||||
return (wrote);
|
return (wrote);
|
||||||
}
|
}
|
||||||
@ -203,7 +202,7 @@ int parse_conversion(const char **format, va_list args)
|
|||||||
else if (ft_strchr("u", **format))
|
else if (ft_strchr("u", **format))
|
||||||
wrote = print_unumber(args);
|
wrote = print_unumber(args);
|
||||||
else if (ft_strchr("p", **format))
|
else if (ft_strchr("p", **format))
|
||||||
wrote = print_pointer(args, 0);
|
wrote = print_pointer(args);
|
||||||
else if (ft_strchr("x", **format))
|
else if (ft_strchr("x", **format))
|
||||||
wrote = print_hex(args, 0);
|
wrote = print_hex(args, 0);
|
||||||
else if (ft_strchr("X", **format))
|
else if (ft_strchr("X", **format))
|
||||||
@ -230,20 +229,27 @@ int parse_placeholder(const char **format, va_list args)
|
|||||||
int ft_printf(const char *format, ...)
|
int ft_printf(const char *format, ...)
|
||||||
{
|
{
|
||||||
int wrote;
|
int wrote;
|
||||||
|
int result;
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
wrote = 0;
|
result = 0;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
while (*format)
|
while (*format && result >= 0)
|
||||||
{
|
{
|
||||||
if (*format == '%')
|
if (*format == '%')
|
||||||
wrote += parse_placeholder(&format, args);
|
wrote = parse_placeholder(&format, args);
|
||||||
else
|
else
|
||||||
wrote += write(1, format, 1);
|
wrote = write(1, format, 1);
|
||||||
|
if (wrote < 0)
|
||||||
|
{
|
||||||
|
result = wrote;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result += wrote;
|
||||||
format++;
|
format++;
|
||||||
}
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return (wrote);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
14
ft_printf.h
14
ft_printf.h
@ -13,6 +13,20 @@
|
|||||||
#ifndef FT_PRINTF_H
|
#ifndef FT_PRINTF_H
|
||||||
# define FT_PRINTF_H
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
# include <stdarg.h>
|
||||||
|
|
||||||
int ft_printf(const char *format, ...);
|
int ft_printf(const char *format, ...);
|
||||||
|
int ft_isbase(char *str);
|
||||||
|
void ft_write_str(char c, char *str);
|
||||||
|
void ft_putnbr_base(unsigned long nbr, char *base, char *result);
|
||||||
|
void ft_putnbr_signed(long nbr, char *base, char *result);
|
||||||
|
int print_char(va_list args);
|
||||||
|
int print_string(va_list args);
|
||||||
|
int print_number(va_list args);
|
||||||
|
int print_unumber(va_list args);
|
||||||
|
int print_hex(va_list args, int uppercase);
|
||||||
|
int print_pointer(va_list args);
|
||||||
|
int parse_conversion(const char **format, va_list args);
|
||||||
|
int parse_placeholder(const char **format, va_list args);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -69,4 +69,10 @@ void ft_lstclear(t_list **lst, void (*del)(void *));
|
|||||||
void ft_lstiter(t_list *lst, void (*f)(void *));
|
void ft_lstiter(t_list *lst, void (*f)(void *));
|
||||||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ft_printf
|
||||||
|
*/
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user