-
Notifications
You must be signed in to change notification settings - Fork 3
/
print_usr.c
39 lines (37 loc) · 985 Bytes
/
print_usr.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
#include "main.h"
/**
* print_usr - prints a string and values of
* non-printed chars
* @arguments: input string
* @buf: buffer pointer
* @ibuf: index for buffer pointer
* Return: number of chars printed
*/
int print_usr(va_list arguments, char *buf, unsigned int ibuf)
{
unsigned char *str;
char *hexadecimal, *binary;
unsigned int i, sum, op;
str = va_arg(arguments, unsigned char *);
binary = malloc(sizeof(char) * (32 + 1));
hexadecimal = malloc(sizeof(char) * (8 + 1));
for (sum = i = 0; str[i]; i++)
{
if (str[i] < 32 || str[i] >= 127)
{
ibuf = handl_buf(buf, '\\', ibuf);
ibuf = handl_buf(buf, 'x', ibuf);
op = str[i];
binary = fill_binary_array(binary, op, 0, 32);
hexadecimal = fill_hex_array(binary, hexadecimal, 1, 8);
ibuf = handl_buf(buf, hexadecimal[6], ibuf);
ibuf = handl_buf(buf, hexadecimal[7], ibuf);
sum += 3;
}
else
ibuf = handl_buf(buf, str[i], ibuf);
}
free(binary);
free(hexadecimal);
return (i + sum);
}