-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
122 lines (110 loc) · 1.91 KB
/
Copy path_printf.c
File metadata and controls
122 lines (110 loc) · 1.91 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "main.h"
#include <stdio.h>
/**
* print_char - Prints a character and increments the count.
* @args: Variable argument list containing
* the character to be printed.
* Return: Updated count of characters printed.
*/
int print_char(va_list args)
{
char c;
c = va_arg(args, int);
_putchar(c);
return (1);
}
/**
* print_string - Prints a string and increments the count.
* @args: Variable argument list containing
* the string to be printed.
* Return: Updated count of characters printed.
*/
int print_string(va_list args)
{
char *s;
int count;
s = va_arg(args, char *);
if (s == NULL)
s = "(null)";
for (count = 0; s[count] != '\0'; count++)
{
_putchar(s[count]);
}
return (count);
}
/**
* print_number - Prints an integer using putchar
* @args: Variable argument list containing
* Return: Updated count of characters printed.
*/
int print_number(va_list args)
{
unsigned int divisor;
unsigned int num;
int count;
num = va_arg(args, unsigned int);
count = 0;
if ((signed int)num < 0)
{
num = -num;
_putchar('-');
count++;
}
divisor = 1;
while (num / divisor > 9)
divisor *= 10;
while ((signed int)divisor > 0)
{
_putchar('0' + num / divisor);
num %= divisor;
divisor /= 10;
count++;
}
return (count);
}
/**
* _printf - printf string according to format specified
* @format: The format string specifying the
* types of arguments to be printed.
* Return: the number of characters printed
* (excluding the null byte used to end output to strings)
*/
int _printf(const char *format, ...)
{
va_list args;
int i, count;
int (*func)(va_list);
count = 0;
va_start(args, format);
if (format == NULL)
return (-1);
for (i = 0; format[i] && format[i] != '\0'; i++)
{
if (format[i] == '%')
{
i++;
func = get_print_func(format[i]);
if (func)
{
count += func(args);
}
else if (!func && format[i] != '\0')
{
_putchar('%');
_putchar(format[i]);
count += 2;
}
else
{
return (-1);
}
}
else
{
_putchar(format[i]);
count++;
}
}
va_end(args);
return (count);
}