-
Notifications
You must be signed in to change notification settings - Fork 1
/
5-print_chars.c
114 lines (106 loc) · 2.88 KB
/
5-print_chars.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
#include "main.h"
/**
* print_string - s function is for printing strings
* @arg: Is a list of arguments passed to the function
* @buffer: Is the buffer array for handling the way of printing
* @flags: Is for calcutating active flags
* @width: Is for width specification
* @precision: Is for precision specification
* @size: Is for size specification
* Return: The length of printed string
*/
int print_string(va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
int length = 0;
char *str = va_arg(arg, char *);
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
/* check if string is NULL */
if (str == NULL)
{
str = "(null)";
if (precision >= 6)
str = " ";
}
/* get length of string */
while (str[length] != '\0')
length++;
/* handle precision */
if (precision >= 0 && precision < length)
length = precision;
/* handle width */
if (width > length)
{
return (justify_string(str, length, width, flags));
}
/* no width and no precision */
return (write(1, str, length));
}
/**
* justify_string - Writes a string justified left or right
* @str: The string to write
* @length: The length of the string
* @width: The desired width
* @flags: The active flags, including the F_MINUS flag for left justification
* Return: The number of characters written
*/
int justify_string(char *str, int length, int width, int flags)
{
int i, written = 0;
if (flags & MINUS)
{
/* left justify */
written += write(1, str, length);
for (i = width - length; i > 0; i--)
written += write(1, " ", 1);
}
else
{
/* right justify */
for (i = width - length; i > 0; i--)
written += write(1, " ", 1);
written += write(1, str, length);
}
return (written);
}
/**
* print_char - This function is for printing a character
* @arg: Is the list of arguments passed to the function
* @buffer: Is the buffer array for handling the way of printing
* @flags: Is for calculating active flags
* @width: Is for width specification
* @precision: Is for precision specification
* @size: Is for size specification
* Return: The length of printed string
*/
int print_char(va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
char ch = va_arg(arg, int);
return (write_char(ch, buffer, flags, width, precision, size));
}
/**
* print_percent - This function is for printing a '%'
* @arg: Is the list of arguments passed
* @buffer: Is a buffer array for handling the way of printing
* @flags: Is for calculating active flags
* @width: Is for width specification
* @precision: Is for precision specification
* @size: Is for size specification
* Return: The length of printed string
*/
int print_percent(va_list arg, char buffer[],
int flags, int width, int precision, int size)
{
UNUSED(arg);
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
return (write(1, "%%", 1));
}