-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_argument.c
More file actions
154 lines (133 loc) · 2.31 KB
/
print_argument.c
File metadata and controls
154 lines (133 loc) · 2.31 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "main.h"
/**
* print_char - prints a character
* @list: list of arguments
*
* Return: Always 1 if printed (successfully) -1 if fails
*/
int print_char(va_list list)
{
char c;
c = va_arg(list, int);
if (c == '\0')
return (0);
return (write(1, &c, 1));
}
/**
* print_string - prints string
* @list: list of arguments
*
* Return: number of characters printed
*/
int print_string(va_list list)
{
char *str = va_arg(list, char*);
int i, len;
if (str == NULL)
str = "(null)";
for (i = 0; str[i] != '\0'; i++)
{
write(1, &str[i], 1);
}
len = _strlen(str);
return (len);
}
/**
* print_digits - prints out the digits passed
* @list: list of all the arguments
*
* Return: number of digits to be printed
*/
int print_digits(va_list list)
{
int num = va_arg(list, int);
int pos = 0;
int is_negative = 0;
int count = 0;
char *buffer;
if (num < 0)
{
is_negative = 1;
num = -num;
}
buffer = malloc(num * sizeof(char));
if (buffer == NULL)
{
printf("Memory allocation failed");
return (-1);
}
do {
buffer[pos++] = num % 10 + '0';
num /= 10;
} while (num > 0);
if (is_negative)
{
buffer[pos++] = '-';
}
while (pos > 0)
{
pos--;
write(1, &buffer[pos], 1);
count++;
}
free(buffer);
return (count);
}
/**
* print_integers - prints only intergers alone
* @list: list of arguments passed
* Return: a number to be printed
*/
int print_integers(va_list list)
{
int num = va_arg(list, int);
int j = 0;
int b;
char *buffer;
if (num < 0)
b = num * -1;
buffer = malloc(b * sizeof(char));
if (buffer == NULL)
{
printf("Memory allocation failed");
return (-1);
}
print_int_str(num, buffer);
j = write(1, buffer, _strlen(buffer));
free(buffer);
return (j);
}
/**
* print_arg - prints the argument passed
* based on the specifier
* @list: list of arguments
* @s: specifier
*
* Return: number of character printed
* according to the argument
*/
int print_arg(va_list list, char s)
{
int count, i, j = 0;
formfunc spec[] = {
{'c', print_char}, {'s', print_string},
{'d', print_digits}, {'i', print_integers},
{'\0', NULL}
};
for (i = 0; spec[i].format != '\0'; i++)
{
if (spec[i].format == s)
{
count = spec[i].func(list);
j++;
return (count);
}
}
if (j == 0)
{
write(1, "%", 1);
write(1, &s, 1);
return (2);
}
return (0);
}