-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
60 lines (51 loc) · 1.29 KB
/
error.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
#include "error.h"
/**
* perrorl - print a formatted message to standard error
* @msg: error message
* @...: NULL-terminated list of context strings to prepend
*/
void perrorl(const char *msg, ...)
{
const char *str;
va_list context;
va_start(context, msg);
while ((str = va_arg(context, char *)))
{
write(STDERR_FILENO, str, _strlen(str));
write(STDERR_FILENO, ": ", 2);
}
va_end(context);
if (msg)
write(STDERR_FILENO, msg, _strlen(msg));
write(STDERR_FILENO, "\n", 1);
}
/**
* perrorl_default - print a formatted message to standard error
* @arg0: argument vector
* @lineno: line number
* @msg: error message
* @...: NULL-terminated list of context strings to prepend
*/
void perrorl_default(const char *arg0, size_t lineno, const char *msg, ...)
{
char *linenostr = num_to_str(lineno);
const char *str = NULL;
va_list ap;
if (arg0)
write(STDERR_FILENO, arg0, _strlen(arg0));
write(STDERR_FILENO, ": ", 2);
if (linenostr)
write(STDERR_FILENO, linenostr, _strlen(linenostr));
write(STDERR_FILENO, ": ", 2);
va_start(ap, msg);
while ((str = va_arg(ap, char *)))
{
write(STDERR_FILENO, str, _strlen(str));
write(STDERR_FILENO, ": ", 2);
}
va_end(ap);
if (msg)
write(STDERR_FILENO, msg, _strlen(msg));
write(STDERR_FILENO, "\n", 1);
free(linenostr);
}