-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
84 lines (78 loc) · 1.5 KB
/
Copy path_printf.c
File metadata and controls
84 lines (78 loc) · 1.5 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
#include "main.h"
void handle_specifier(const char *format, va_list argums, int *count, int *i);
void write_char(char c, int *count);
/**
* _printf - produces output according to a format
* @format: character string
* Return: the number of characters printed
*/
int _printf(const char *format, ...)
{
va_list argums;
int i = 0, count = 0;
if (!format)
return (-1);
va_start(argums, format);
while (format && format[i])
{
if (format[i] == '%' && format[i + 1])
{
i++;
handle_specifier(format, argums, &count, &i);
}
else
{
write_char(format[i], &count);
}
i++;
}
va_end(argums);
return (count);
}
/**
* handle_specifier - handles the format specifier
* @format: character string
* @argums: argument list
* @count: pointer to the character count
* @i: pointer to the current position in the format string
*/
void handle_specifier(const char *format, va_list argums, int *count, int *i)
{
if (format[*i] == 'c')
{
carry_chars(argums, count);
}
else if (format[*i] == 's')
{
carry_strings(argums, count);
}
else if (format[*i] == 'd')
{
carry_decimals(argums, count);
}
else if (format[*i] == 'i')
{
carry_integers(argums, count);
}
else if (format[*i] == '%')
{
write(1, "%", 1);
(*count)++;
}
else
{
write(1, "%", 1);
write(1, &format[*i], 1);
(*count) += 2;
}
}
/**
* write_char - writes a character to stdout
* @c: character to write
* @count: pointer to the character count
*/
void write_char(char c, int *count)
{
write(1, &c, 1);
(*count)++;
}