-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0-specifiers.c
More file actions
152 lines (134 loc) · 3.51 KB
/
Copy path0-specifiers.c
File metadata and controls
152 lines (134 loc) · 3.51 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "main.h"
/**
* print_char - A function that prints a character.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves a character from the va_list in the arg_data_t
* struct, prints it, increments the count of printed characters in the struct
* and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_char(arg_data_t *data)
{
char c = (char)va_arg(data->args, int);
_putchar(c, data);
data->count++;
return (data->count);
}
/**
* print_string - A function that prints a string.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves a string from the va_list in the arg_data_t struct,
* prints it character by character, increments the count of printed characters
* in the struct, and returns the count. If the string is NULL, it prints
* "(null)".
*
* Return: The total count of printed characters after the function call.
*/
int print_string(arg_data_t *data)
{
char *str = va_arg(data->args, char *);
int count = 0;
if (str == NULL)
{
str = "(null)";
}
while (*str)
{
_putchar(*str, data);
str++;
count++;
}
data->count += count;
return (data->count);
}
/**
* print_percent - A function that prints a percent sign.
*
* @data: A pointer to an arg_data_t struct. This paramenter is not used in the
* function.
*
* This function prints a percent sign and returns the result of the print op.
*
* Return: The result of the print operation.
*/
int print_percent(arg_data_t *data)
{
(void)data;
_putchar('%', data);
data->count++;
return (data->count);
}
/**
* print_int - A function that prints an integer.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an integer from the va_list in the arg_data_t
* struct, converts it to a string, prints it character by character,
* increments the count of printed characters in the struct, and returns the
* count.
*
* Return: The total count of printed characters after the function call.
*/
int print_int(arg_data_t *data)
{
int num = va_arg(data->args, int);
char buffer[12]; /* Buffer to hold the string repres. of the integer */
char *str = &buffer[11]; /* Start from the end of the buffer */
int count = 0;
int is_negative = 0;
*str = '\0'; /* Null-terminate the string */
if (num < 0)
{
is_negative = 1;
}
/* Convert the integer to a string */
int_to_str(&num, &str, &count, is_negative);
handle_int_flags(is_negative, &str, &count, data);
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}
/**
* print_binary - A function that prints an unsigned int as binary.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an unsigned int from the va_list in the arg_data_t
* struct, converts it to binary, prints it, increments the count of printed
* characters in the struct, and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_binary(arg_data_t *data)
{
unsigned int num = va_arg(data->args, unsigned int);
char buffer[33]; /* Buffer to hold the binary repres. of the number */
char *str = &buffer[32]; /* Start from the end of the buffer */
int count = 0;
*str = '\0'; /* Null-terminate the string */
/* Convert the number to binary */
do {
str--;
*str = '0' + (num % 2);
num /= 2;
count++;
} while (num != 0);
/* Print the binary representation and update the count */
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}