summaryrefslogtreecommitdiff
path: root/tools/include/nolibc/err.h
blob: e22ae87a72892082f18c77d54b0581038daeea8a (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
/*
 * formatted error message for NOLIBC
 * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
 */

/* make sure to include all global symbols */
#include "nolibc.h"

#ifndef _NOLIBC_ERR_H
#define _NOLIBC_ERR_H

#include "errno.h"
#include "stdarg.h"
#include "sys.h"

static __attribute__((unused))
void vwarn(const char *fmt, va_list args)
{
	fprintf(stderr, "%s: ", program_invocation_short_name);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, ": %m\n");
}

static __attribute__((unused))
void vwarnx(const char *fmt, va_list args)
{
	fprintf(stderr, "%s: ", program_invocation_short_name);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
}

static __attribute__((unused))
void warn(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vwarn(fmt, args);
	va_end(args);
}

static __attribute__((unused))
void warnx(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vwarnx(fmt, args);
	va_end(args);
}

static __attribute__((noreturn, unused))
void verr(int eval, const char *fmt, va_list args)
{
	vwarn(fmt, args);
	exit(eval);
}

static __attribute__((noreturn, unused))
void verrx(int eval, const char *fmt, va_list args)
{
	warnx(fmt, args);
	exit(eval);
}

static __attribute__((noreturn, unused))
void err(int eval, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	verr(eval, fmt, args);
	va_end(args);
}

static __attribute__((noreturn, unused))
void errx(int eval, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	verrx(eval, fmt, args);
	va_end(args);
}

#endif /* _NOLIBC_ERR_H */