forked from AlfredPianist/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformats.c
executable file
·90 lines (74 loc) · 1.75 KB
/
formats.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
#include "holberton.h"
/**
* f_char - The "%c" format specifier.
* @arg_l: The argument list to be operated.
* @precision: The precision amount.
*
* Return: The formatted "%c" string.
*/
char *f_char(va_list arg_l, unsigned int precision)
{
char *ch;
precision = precision;
ch = NULL;
ch = _alloc(ch, 2 * sizeof(*ch));
ch[0] = (char) va_arg(arg_l, int);
ch[1] = '\0';
return (ch);
}
/**
* f_str - The "%s" format specifier.
* @arg_l: The argument list to be operated.
* @precision: The precision amount.
*
* Return: The formatted "%s" string.
*/
char *f_str(va_list arg_l, unsigned int precision)
{
char *orig, *buf;
unsigned int len_buf;
buf = NULL;
len_buf = 0;
orig = va_arg(arg_l, char *);
if (orig == NULL)
orig = "(null)";
len_buf = str_len(orig);
buf = _alloc(buf, len_buf * sizeof(*buf));
buf = str_cpy(orig, buf);
if ((precision != UINT_MAX) && (len_buf - 1 > precision))
buf = _realloc(buf, len_buf * sizeof(*buf), precision * sizeof(*buf));
return (buf);
}
/**
* f_perc - Prints the '%' character.
* @arg_l: The argument list to be operated.
* @precision: The precision amount.
*
* Return: The "%" string.
*/
char *f_perc(va_list arg_l, unsigned int precision)
{
char *perc;
arg_l = arg_l;
precision = precision;
perc = NULL;
perc = _alloc(perc, 2 * sizeof(*perc));
perc[0] = '%';
perc[1] = '\0';
return (perc);
}
/**
* f_int - The "%d" and "%i" format specifier.
* @arg_l: The argument list to be operated.
* @precision: The precision amount.
*
* Return: The formatted "%d", "%i", "%hd" or "%hi" string.
*/
char *f_int(va_list arg_l, unsigned int precision)
{
char *integer;
integer = _itoa(va_arg(arg_l, int));
if (precision != UINT_MAX)
integer = prec_int(integer, precision);
return (integer);
}