-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
57 lines (51 loc) · 1.28 KB
/
_printf.c
File metadata and controls
57 lines (51 loc) · 1.28 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
#include "main.h"
int _printf(const char *format, ...)
{
va_list list;
int len;
int printed_chars = 0;
va_start(list, format);
if (format == NULL)
return -1;
while (*format)
{
if (*format == '%' && *(format + 1) != '\0')
{
format++;
switch (*format)
{
case 'c':
{
char c = va_arg(list, int);
write(1, &c, 1);
printed_chars++;
break;
}
case 's':
{
char *str = va_arg(list, char *);
if (str == NULL)
str = "(null)";
len = 0; /* Initialize len here */
while (str[len])
len++;
write(1, str, len);
printed_chars += len;
break;
}
case '%':
write(1, "%", 1);
printed_chars++;
break;
}
}
else
{
write(1, format, 1);
printed_chars++;
}
format++;
}
va_end(list);
return printed_chars;
}