-
Notifications
You must be signed in to change notification settings - Fork 8
/
3-printf.c
132 lines (121 loc) · 2.43 KB
/
3-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
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
123
124
125
126
127
128
129
130
131
132
#include "main.h"
/**
* print_char - prints character
* @ap: argument pointer
* @params: the parameters struct
*
* Return: number chars printed
*/
int print_char(va_list ap, params_t *params)
{
char pad_char = ' ';
unsigned int pad = 1, sum = 0, ch = va_arg(ap, int);
if (params->minus_flag)
sum += _putchar(ch);
while (pad++ < params->width)
sum += _putchar(pad_char);
if (!params->minus_flag)
sum += _putchar(ch);
return (sum);
}
/**
* print_int - prints integer
* @ap: argument pointer
* @params: the parameters struct
*
* Return: number chars printed
*/
int print_int(va_list ap, params_t *params)
{
long l;
if (params->l_modifier)
l = va_arg(ap, long);
else if (params->h_modifier)
l = (short int)va_arg(ap, int);
else
l = (int)va_arg(ap, int);
return (print_number(convert(l, 10, 0, params), params));
}
/**
* print_string - prints string
* @ap: argument pointer
* @params: the parameters struct
*
* Return: number chars printed
*/
int print_string(va_list ap, params_t *params)
{
char *str = va_arg(ap, char *), pad_char = ' ';
unsigned int pad = 0, sum = 0, i = 0, j;
(void)params;
switch ((int)(!str))
case 1:
str = NULL_STRING;
j = pad = _strlen(str);
if (params->precision < pad)
j = pad = params->precision;
if (params->minus_flag)
{
if (params->precision != UINT_MAX)
for (i = 0; i < pad; i++)
sum += _putchar(*str++);
else
sum += _puts(str);
}
while (j++ < params->width)
sum += _putchar(pad_char);
if (!params->minus_flag)
{
if (params->precision != UINT_MAX)
for (i = 0; i < pad; i++)
sum += _putchar(*str++);
else
sum += _puts(str);
}
return (sum);
}
/**
* print_percent - prints string
* @ap: argument pointer
* @params: the parameters struct
*
* Return: number chars printed
*/
int print_percent(va_list ap, params_t *params)
{
(void)ap;
(void)params;
return (_putchar('%'));
}
/**
* print_S - custom format specifier
* @ap: argument pointer
* @params: the parameters struct
*
* Return: number chars printed
*/
int print_S(va_list ap, params_t *params)
{
char *str = va_arg(ap, char *);
char *hex;
int sum = 0;
if ((int)(!str))
return (_puts(NULL_STRING));
for (; *str; str++)
{
if ((*str > 0 && *str < 32) || *str >= 127)
{
sum += _putchar('\\');
sum += _putchar('x');
hex = convert(*str, 16, 0, params);
if (!hex[1])
sum += _putchar('0');
sum += _puts(hex);
}
else
{
sum += _putchar(*str);
}
}
return (sum);
}