aboutsummaryrefslogtreecommitdiff
path: root/src/error.c
blob: 2e561173390115774881c393228ebe50450e64e5 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   error.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/06/14 11:02:52 by charles           #+#    #+#             */
/*   Updated: 2020/09/10 20:29:41 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "eval.h"

/*
** \brief   printf like function that only works with `%s`,
**          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	*arg;

	ft_putstr_fd(g_basename, STDERR_FILENO);
	ft_putstr_fd(": ", STDERR_FILENO);
	while (*format != '\0')
	{
		if (format[0] == '%' && format[1] == 's')
		{
			arg = va_arg(ap, char*);
			ft_putstr_fd(arg, 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);
}