-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
74 lines (70 loc) · 1.34 KB
/
Copy pathprintf.c
File metadata and controls
74 lines (70 loc) · 1.34 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
#include "holberton.h"
/**
* _printf - custom function that format and print data
* @format: list of types of arguments passed to the function
* Return: int
*/
int _printf(const char *format, ...)
{
va_list list;
int idx, j;
int len_buf = 0;
char *s;
char *create_buff;
type_t ops[] = {
{"c", print_c},
{"s", print_s},
{"i", print_i},
{"d", print_i},
{"b", print_bin},
{NULL, NULL}
};
create_buff = malloc(1024 * sizeof(char));
if (create_buff == NULL)
{
free(create_buff);
return (-1);
}
va_start(list, format);
if (format == NULL || list == NULL)
return (-1);
for (idx = 0; format[idx] != '\0'; idx++)
{
if (format[idx] == '%' && format[idx + 1] == '%')
continue;
else if (format[idx] == '%')
{
if (format[idx + 1] == ' ')
idx += _position(format, idx);
for (j = 0; ops[j].f != NULL; j++)
{
if (format[idx + 1] == *(ops[j].op))
{
s = ops[j].f(list);
if (s == NULL)
return (-1);
_strlen(s);
_strcat(create_buff, s, len_buf);
len_buf += _strlen(s);
idx++;
break;
}
}
if (ops[j].f == NULL)
{
create_buff[len_buf] = format[idx];
len_buf++;
}
}
else
{
create_buff[len_buf] = format[idx];
len_buf++;
}
}
create_buff[len_buf] = '\0';
write(1, create_buff, len_buf);
va_end(list);
free(create_buff);
return (len_buf);
}