-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-functions.c
132 lines (123 loc) · 2.6 KB
/
6-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
121
122
123
124
125
126
127
128
129
130
131
132
#include "main.h"
/**
* print_address - Prints the address of a input
*
* @arg_list: Input integers argument (int)
* @buffer: Buffer array (char)
* @index: Number of buffer index (int)
*
* Return: The length of address
*/
int print_address(va_list arg_list, char *buffer, unsigned int index)
{
void *address;
long int number;
int i, count, n, check_negative;
char *hexa, *binary;
char none[] = "(nil)";
address = (va_arg(arg_list, void *));
if (address == NULL)
{
for (i = 0; none[i]; i++)
index = _puts(buffer, none[i], index);
return (5);
}
number = (intptr_t)address;
check_negative = 0;
if (number < 0)
{
number = (number * -1) - 1;
check_negative = 1;
}
binary = malloc(sizeof(char) * (64 + 1));
binary = input_to_binary(binary, number, check_negative, 64);
hexa = malloc(sizeof(char) * (16 + 1));
hexa = input_to_hexa(binary, hexa, 0, 16);
index = _puts(buffer, '0', index);
index = _puts(buffer, 'x', index);
for (n = i = count = 0; hexa[i]; i++)
{
if (hexa[i] != '0' && n == 0)
n = 1;
if (n)
{
index = _puts(buffer, hexa[i], index);
count++;
}
}
free(binary);
free(hexa);
return (count + 2);
}
/**
* print_space_int - Prints integers with ' ' flag
*
* @arg_list: Input integers argument (int)
* @buffer: Buffer array (char)
* @index: Number of buffer index (int)
*
* Return: The length of an integer output
*/
int print_space_int(va_list arg_list, char *buffer, unsigned int index)
{
int number = va_arg(arg_list, int);
unsigned int n, temp_num, i, diviser;
if (number < 0)
{
n = number * -1;
index = _puts(buffer, '-', index);
}
else
{
n = number;
index = _puts(buffer, ' ', index);
}
temp_num = n;
diviser = 1;
while (temp_num > 9)
{
diviser *= 10;
temp_num /= 10;
}
for (i = 0; diviser > 0; diviser /= 10, i++)
{
index = _puts(buffer, ((n / diviser) % 10) + '0', index);
}
return (i + 1);
}
/**
* print_space_dig - Prints digits with ' ' flag
*
* @arg_list: Input digits argument (int)
* @buffer: Buffer array (char)
* @index: Number of buffer index (int)
*
* Return: The length of an digit output
*/
int print_space_dig(va_list arg_list, char *buffer, unsigned int index)
{
int number = va_arg(arg_list, int);
unsigned int n, temp_num, i, diviser;
if (number < 0)
{
n = number * -1;
index = _puts(buffer, '-', index);
}
else
{
n = number;
index = _puts(buffer, ' ', index);
}
temp_num = n;
diviser = 1;
while (temp_num > 9)
{
diviser *= 10;
temp_num /= 10;
}
for (i = 0; diviser > 0; diviser /= 10, i++)
{
index = _puts(buffer, ((n / diviser) % 10) + '0', index);
}
return (i + 1);
}