Repository navigation
Expand file tree
/
Copy pathprint_types.c
More file actions
138 lines (123 loc) · 2.75 KB
/
Copy pathprint_types.c
File metadata and controls
138 lines (123 loc) · 2.75 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "main.h"
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
/**
* _print_char - A function printing a uniq char.
* @arg: The char to print.
* @next_char: the letter used after '%'
*
* Description: _print_char prints a char. It is a function created for _printf
*
* Return: it returns 1 for the lenght of a single char.
*/
int _print_char (va_list *arg, char next_char)
{
char letter;
(void)next_char;
letter = va_arg(*arg, int);
_putchar(letter);
return (1);
}
/**
* _print_str - Prints a string.
* @arg: Pointer to the variadic argument list.
* @next_char: The format specifier character (after '%'), unused here.
*
* Description: Extracts a string from the variadic arguments and prints it.
* If the string is NULL, prints "(null)" instead.
*
* Return: Returns the length of the printed string.
*/
int _print_str(va_list *arg, char next_char)
{
int i = 0;
char *str = va_arg(*arg, char *);
(void)next_char;
if (!str)
{
write(1, "(null)", 6);
return (6);
}
while (str[i] != '\0')
{
_putchar (str[i]);
i++;
}
return (i);
}
/**
* _print_percentage_or_unknown - Prints '%' or an unknown format.
* @arg: Not used.
* @next_char: The character after '%'.
*
* Description: If the next character is '%', prints '%'.
* Otherwise, prints '%' followed by the unknown character.
*
* Return: 1 if only '%' was printed, 2 if '%'
* and an unknown character were printed,
* 0 if next_char is '\0'.
*/
int _print_percentage_or_unknown(va_list *arg, char next_char)
{
(void)arg;
if (next_char == '%')
{
_putchar('%');
return (1);
}
else
{
_putchar('%');
_putchar(next_char);
return (2);
}
}
/**
* _print_recursive_number - Prints an integer recursively.
* @num: The non-negative integer to print.
*
* Description: Recursively prints each digit of num
* from most significant to least.
*
* Return: Returns the number of characters printed.
*/
int _print_recursive_number(int num)
{
int len = 0;
if (num >= 10)
len += _print_recursive_number(num / 10);
_putchar(num % 10 + '0');
len++;
return (len);
}
/**
* _print_int - Prints a signed integer.
* @arg: Pointer to the variadic argument list.
* @next_char: The format specifier character (after '%'), unused here.
*
* Description: Extracts an integer from the variadic arguments and prints it.
* Handles negative numbers, including INT_MIN.
*
* Return: Returns the total number of characters printed.
*/
int _print_int(va_list *arg, char next_char)
{
int number, len = 0;
(void)next_char;
number = va_arg(*arg, int);
if (number < 0)
{
_putchar('-');
len = 1;
if (number == INT_MIN)
{
write(1, "2147483648", 10);
return (len += 10);
}
number = -number;
}
len += _print_recursive_number(number);
return (len);
}