-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_specifiers_a.c
More file actions
103 lines (92 loc) · 2.25 KB
/
print_specifiers_a.c
File metadata and controls
103 lines (92 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_specifiers_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sunko <sunko@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/11 10:35:03 by sunko #+# #+# */
/* Updated: 2023/04/12 11:59:29 by sunko ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int print_character(va_list *ap, int *rst)
{
char value;
value = va_arg(*ap, int);
if (write(1, &value, 1) == -1)
return (ft_error(rst, NULL));
*rst += 1;
return (*rst);
}
int print_string(va_list *ap, int *rst)
{
char *value;
int i;
i = -1;
value = va_arg(*ap, char *);
if (!value)
{
if (write(1, "(null)", 6) == -1)
return (ft_error(rst, NULL));
*rst += 6;
return (*rst);
}
while (value[++i] != '\0')
{
if (write(1, &value[i], 1) == -1)
return (ft_error(rst, NULL));
*rst += 1;
}
return (*rst);
}
int print_decimal(va_list *ap, int *rst)
{
int value;
char *str;
int i;
i = -1;
value = va_arg(*ap, int);
str = ft_itoa(value);
if (!str)
return (ft_error(rst, NULL));
while (str[++i] != '\0')
{
if (write(1, &str[i], 1) == -1)
return (ft_error(rst, str));
*rst += 1;
}
free(str);
str = NULL;
return (*rst);
}
int print_pointer(va_list *ap, int *rst)
{
unsigned long long addr;
if (write(1, "0x", 2) == -1)
return (ft_error(rst, NULL));
else
*rst += 2;
addr = (unsigned long long)va_arg(*ap, void *);
goto_memory(addr, rst);
return (*rst);
}
void goto_memory(unsigned long long nb, int *rst)
{
char *base;
base = "0123456789abcdef";
if (nb >= 16)
{
goto_memory(nb / 16, rst);
goto_memory(nb % 16, rst);
}
else
{
if (write(1, &base[nb], 1) == -1)
{
*rst = -1;
return ;
}
*rst += 1;
}
}