-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_directive_functions.c
120 lines (110 loc) · 1.89 KB
/
format_directive_functions.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "main.h"
/**
* handle_c - handle c directive
*
* @arguments: the list that we will get
* the corresponding argument from it.
*
* Return: the length of printed characters
*/
int handle_c(va_list arguments)
{
_putchar(va_arg(arguments, int));
return (1);
}
/**
* handle_d - handle d directive
*
* @arguments: the list that we will get
* the corresponding argument from it.
*
* Return: the length of printed characters
*/
int handle_d(va_list arguments)
{
unsigned int m, d, digit_count, character_count = 0;
int target;
target = va_arg(arguments, int);
if (target < 0)
{
_putchar(45);
character_count++;
m = target * -1;
}
else
{
m = target;
}
d = m;
digit_count = 1;
while (d > 9)
{
d /= 10;
digit_count *= 10;
}
while (digit_count >= 1)
{
_putchar(((m / digit_count) % 10) + 48);
character_count++;
digit_count /= 10;
}
return (character_count);
}
/**
* handle_i - handle i directive
*
* @arguments: the list that we will get
* the corresponding argument from it.
*
* Return: the length of printed characters
*/
int handle_i(va_list arguments __attribute__ ((unused)))
{
return (0);
}
/**
* handle_s - handle s directive
*
* @arguments: the list that we will get
* the corresponding argument from it.
*
* Return: the length of printed characters
*/
int handle_s(va_list arguments)
{
char *str = va_arg(arguments, char *);
int length = 0;
if (str == NULL)
{
str = "(null)";
while (str[length] != 0)
{
_putchar(str[length]);
length++;
}
return (length);
}
else
{
while (*str != '\0')
{
_putchar(*str);
str++;
length++;
}
}
return (length);
}
/**
* handle_mod_sign - handle modulo sign
*
* @arguments: the list that we will get
* the corresponding argument from it.
*
* Return: the length of printed characters
*/
int handle_mod_sign(va_list arguments __attribute__((unused)))
{
_putchar('%');
return (1);
}