-
Notifications
You must be signed in to change notification settings - Fork 1
/
_printf.c
70 lines (69 loc) · 1.19 KB
/
_printf.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
#include "shell.h"
/**
* _printf - print the variables
* @string: constant pointer chat type.
* Return: const.
*/
int _printf(const char *string, ...)
{
char *data;
int i;
int strlen;
int cont = 0;
va_list val;
if (string == NULL)
return (-1);
va_start(val, string);
for (i = 0; string[i]; i++)
{
if (string[i] == '%')
{
i++;
switch (string[i])
{
case 'c':
cont += _putchar(va_arg(val, int));
break;
case 's':
data = va_arg(val, char *);
if (data == NULL)
{
data = "(NULL)";
_puts(data);
cont += _strlen(data);
break;
}
strlen = _strlen(data);
if (data[strlen] != 0)
return (-1);
_puts(data);
cont += _strlen(data);
break;
case '%':
cont += _putchar(string[i]);
break;
case 'i':
cont += display_number(va_arg(val, int));
break;
case 'd':
cont += display_number(va_arg(val, int));
break;
case ' ':
return (-1);
case '\0':
return (-1);
default:
_putchar('%');
_putchar(string[i]);
cont += 2;
break;
}
}
else
{
cont += _putchar(string[i]);
}
}
va_end(val);
return (cont);
}