-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6-print_others.c
95 lines (82 loc) · 2.36 KB
/
6-print_others.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
#include "main.h"
/**
* print_pointer - This function prints the value of a variable in a pointer
*
* @arg: Is the list of arguments passed
* @buffer: Is a buffer array for handling the way of printing
* @flags: Are the bitwise flags
* @width: Is the minimum width for the field
* @precision: Is the precision specification
* @size: Is the size specification
*
* Return: The length of the printed string
*/
int print_pointer(va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
char padding_char = ' ';
int buffer_index = BUFFER_SIZE - 2, length = 2;
unsigned long address_num;
char hex_map[] = "0123456789abcdef";
void *address = va_arg(arg, void *);
UNUSED(width);
UNUSED(size);
/* If pointer is null, print (nil) and return 5 */
if (address == NULL)
return (write(1, "(nil)", 5));
buffer[BUFFER_SIZE - 1] = '\0';
UNUSED(precision);
/* Convert the pointer value to hexadecimal */
address_num = (unsigned long) address;
while (address_num > 0)
{
buffer[buffer_index--] = hex_map[address_num % 16];
address_num /= 16;
length++;
}
/* Check for flags */
if ((flags & ZERO) && !(flags & MINUS))
padding_char = '0';
if (flags & (PLUS | SPACE))
length++;
/* Set index to point to the first character to be printed */
buffer_index++;
return (write_pointer(buffer, buffer_index, length, width,
flags, padding_char, padding_char != ' ', 1));
}
/**
* print_non_printable - Prints ascii codes in hexa of non printable chars
* @arg: List of arguments
* @buffer: Buffer array to handle print
* @flags: Bitwise flags
* @width: Minimum field width
* @precision: Precision specification
* @size: Size specifier
*
* Return: Number of chars printed
*/
int print_non_printable(va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
int i = 0, offset = 0;
char *str = va_arg(arg, char *);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
/* If string is null, print (null) and return 6 */
if (str == NULL)
return (write(1, "(null)", 6));
/* Check each character and convert non-printables to hex codes */
while (str[i] != '\0')
{
if (_isprintable(str[i]))
buffer[i + offset] = str[i];
else
offset += append_hexacode(str[i], buffer, i + offset);
i++;
}
buffer[i + offset] = '\0';
/* Write the converted string to the output buffer */
return (write(1, buffer, i + offset));
}