-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterr.c
64 lines (58 loc) · 1.12 KB
/
printerr.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
#include "main.h"
int print_string(char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++)
;
write(STDERR_FILENO, s, i);
return (i);
}
int print_numbers(int k)
{
int kcopy, count = 0, power_10;
char c;
kcopy = k;
for (power_10 = 1; kcopy / 10; kcopy /= 10, power_10 *= 10)
;
while (power_10)
{
c = ((k / power_10) % 10) + '0';
write(STDERR_FILENO, &c, 1);
count++;
power_10 /= 10;
}
return (count);
}
/**
* _printf - works just like the standard library printf
* @format: format specifier
* Return: number of bytes written to standard output
*/
int printerr(char *format, ...)
{
int write_count = 0, i;
char c;
va_list args;
if (format == NULL)
return (-1);
va_start(args, format);
for (i = 0; format[i] != '\0'; i++)
{
while (format[i] != '%' && format[i] != '\0')
{
c = format[i];
write(STDERR_FILENO, &c, 1);
i++;
write_count++;
}
if (format[i] == '\0')
return (write_count);
i++;
if (format[i] == 's')
write_count += print_string(va_arg(args, char *));
else if (format[i] == 'd')
write_count += print_numbers(va_arg(args, int));
}
va_end(args);
return (write_count);
}