-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorWriteFunc.c
246 lines (208 loc) · 4.17 KB
/
errorWriteFunc.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "main.h"
/**
* _fprintf - custom version of fprintf using write
* @fd: file descriptor
* @format: formatted string containing specifiers
* @...: variable arguments
* Return: number of characters printed
*/
int _fprintf(int fd, const char *format, ...)
{
va_list args;
int count = 0;
va_start(args, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
switch (*format)
{
case 's':
{
char *str = va_arg(args, char *);
write(fd, str, _strlen(str));
count += _strlen(str);
break;
}
/* we will add more cases for more specifiers if ever needed */
default:
/* Handle unsupported specifier or just ignore it */
write(fd, "%", 1);
count++;
break;
}
}
else
{
write(fd, format, 1);
count++;
}
format++;
}
va_end(args);
return (count);
}
/**
* errorPuts - prints an input string
* @str: the string to be printed
*
* Return: Nothing
*/
void errorPuts(char *str)
{
int i = 0;
if (!str)
return;
while (str[i] != '\0')
{
print_error_char(str[i]);
i++;
}
}
/**
* print_error - add descr
* @shellInfo: add descr
* @errorStr: add descr
* Return: add descr
*/
void print_error(commandInfo *shellInfo, char *errorStr)
{
errorPuts(shellInfo->file_name);
errorPuts(": ");
custPrint(shellInfo->current_line_number, STDERR_FILENO);
errorPuts(": ");
errorPuts(shellInfo->command_arguments[0]);
errorPuts(": ");
errorPuts(errorStr);
}
/**
* custPrint - function prints a decimal (integer) number (base 10)
* @input: the input
* @fd: the filedescriptor to write to
*
* Return: number of characters printed
*/
int custPrint(int input, int fd)
{
int (*__putchar)(char) = _putchar;
int i, count = 0;
unsigned int _abs_, current;
if (fd == STDERR_FILENO)
__putchar = _eputchar;
if (input < 0)
{
_abs_ = -input;
__putchar('-');
count++;
}
else
_abs_ = input;
current = _abs_;
for (i = 1000000000; i > 1; i /= 10)
{
if (_abs_ / i)
{
__putchar('0' + current / i);
count++;
}
current %= i;
}
__putchar('0' + current);
count++;
return (count);
}
/**
* _eputchar - writes the character c to stderr
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _eputchar(char c)
{
static int i;
static char buf[BUF_SIZE];
if (c == FLUSH_BUFFER || i >= BUF_SIZE)
{
write(2, buf, i);
i = 0;
}
if (c != FLUSH_BUFFER)
buf[i++] = c;
return (1);
}
/**
* print_error_char - Writes the character to the standard error stream.
* @character: The character to print.
*
* Return: On success, returns 1. On error, -1 is returned.
*/
int print_error_char(char character)
{
static int buffer_index;
static char output_buffer[BUF_SIZE];
if (character == FLUSH_BUFFER || buffer_index >= BUF_SIZE)
{
write(STDERR_FILENO, output_buffer, buffer_index);
buffer_index = 0;
}
if (character != FLUSH_BUFFER)
output_buffer[buffer_index++] = character;
return (1);
}
/**
* print_error_string - Prints an input string to the standard error stream.
* @error_string: The string to be printed.
*
* Return: Nothing.
*/
void print_error_string(char *error_string)
{
int index = 0;
if (!error_string)
return;
while (error_string[index] != '\0')
{
print_error_char(error_string[index]);
index++;
}
}
/**
* write_to_fd - Writes the character to the given file descriptor.
* @character: The character to print.
* @fd: The file descriptor to write to.
*
* Return: add descr
*/
int write_to_fd(char character, int fd)
{
static int buffer_index;
static char output_buffer[BUF_SIZE];
if (character == FLUSH_BUFFER || buffer_index >= BUF_SIZE)
{
write(fd, output_buffer, buffer_index);
buffer_index = 0;
}
if (character != FLUSH_BUFFER)
output_buffer[buffer_index++] = character;
return (1);
}
/**
* print_to_fd - Prints an input string to the given file descriptor.
* @input_string: The string to be printed.
* @fd: The file descriptor to write to.
*
* Return: The number of characters written.
*/
int print_to_fd(char *input_string, int fd)
{
int count = 0;
if (!input_string)
return (0);
while (*input_string)
{
count += write_to_fd(*input_string++, fd);
}
return (count);
}