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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 11:02:52 by charles #+# #+# */
/* Updated: 2020/09/14 16:42:01 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
/*
** \brief printf like function that only works with `%s` and `%c`,
** prefix the message with the program name
** and output on STDERR
** \note NULL arguments are ignored
*/
void errorf(const char *format, ...)
{
va_list ap;
va_start(ap, format);
verrorf(format, ap);
va_end(ap);
}
/*
** \brief errorf with an argument pointer (ap) instead of arguments
*/
void verrorf(const char *format, va_list ap)
{
char *str;
char c;
ft_putstr_fd(g_basename, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
while (*format != '\0')
{
if (format[0] == '%' && format[1] == 's')
{
str = va_arg(ap, char*);
ft_putstr_fd(str, STDERR_FILENO);
format += 2;
}
else if (format[0] == '%' && format[1] == 'c')
{
c = va_arg(ap, int);
ft_putchar_fd(c, STDERR_FILENO);
format += 2;
}
else
{
ft_putchar_fd(*format, STDERR_FILENO);
format++;
}
}
}
/*
** \brief errorf helper to return an status code and print the error
*/
int errorf_ret(int status, const char *format, ...)
{
va_list ap;
va_start(ap, format);
verrorf(format, ap);
va_end(ap);
return (status);
}
|