-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus.c
84 lines (75 loc) · 1.93 KB
/
bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/13 20:36:36 by smbaabu #+# #+# */
/* Updated: 2019/04/16 21:03:14 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *b(va_list args, int *base)
{
char *ret;
*base = 2;
ret = ft_itlu(va_arg(args, unsigned long long), *base, 0);
return (ret);
}
static char *convert_num(unsigned char c)
{
char *snum;
char *tmp;
snum = ft_itoa_base_l(c, 16, 0);
tmp = snum;
snum = ft_strjoin("0x", snum);
free(tmp);
return (snum);
}
static int handle_r(char *buf, char *ptr, int num)
{
int count;
int i;
int l;
char *snum;
count = 0;
i = 0;
while (i < num)
{
if (!ft_isprint(ptr[i]))
{
snum = convert_num((unsigned char)ptr[i]);
l = ft_strlen(snum);
ft_memcpy(buf + count, snum, l);
free(snum);
count += l;
}
else
{
buf[i] = ptr[i];
count++;
}
i++;
}
return (count);
}
char *r(va_list args)
{
char buf[R_SIZE];
int num;
char *ptr;
int count;
ptr = va_arg(args, char *);
num = va_arg(args, unsigned int);
if (ptr == NULL)
ptr = ft_strdup(NULL_PRINT);
else
{
count = handle_r(buf, ptr, num);
ptr = (char *)malloc((1 + count) * sizeof(char));
ft_memcpy(ptr, buf, count);
ptr[count] = 0;
}
return (ptr);
}