~smlavine/navipage

ref: 0ac3636ffe9b39e8e02f5862fc41556bec334a96 navipage/err.c -rw-r--r-- 2.6 KiB
0ac3636fSebastian LaVine Use err from the released version of the library 9 months ago
                                                                                
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
/*
 * err - Small error-printing library
 * Copyright (C) 2021 Sebastian LaVine <mail@smlavine.com>
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "err.h"

char *argv0;

/***********************************************************************
 * This file contains six error and warning functions:
 * vwarn(), vewarn(), and verr(), plus variadic wrappers for each.
 *
 ***********************************************************************
 * vwarn() prints argv0, ": ", and a message formatted from the given
 * format string and va_list.
 *
 * warn() is a variadic wrapper for vwarn().
 *
 ***********************************************************************
 * vewarn() calls vwarn(), then prints ": " if the format string is
 * neither NULL nor empty, the value of strerror(errno), and a newline.
 *
 * ewarn() is a variadic wrapper for vewarn().
 *
 ***********************************************************************
 * verr() calls vewarn() and exits the program with the provided code.
 *
 * err() is a variadic wrapper for verr().
 *
 ***********************************************************************
 * If argv0 is not set by the caller, behavior is undefined.
 *
 * Some functions in this file are named the same as functions
 * provided by libbsd's err.h. This file does NOT implement those
 * functions, and might behave completely differently.
 *
 ***********************************************************************
 */

void
vwarn(const char *fmt, va_list ap)
{
	fprintf(stderr, "%s: ", argv0);
	vfprintf(stderr, fmt, ap);
}

void
vewarn(const char *fmt, va_list ap)
{
	/* strerror(3) must be called before anything else is done,
	 * otherwise errno could be modified by function calls made
	 * between when vewarn() was called and when output is printed.
	 */
	const char *errstr = strerror(errno);

	vwarn(fmt, ap);
	if (fmt != NULL && fmt[0] != '\0')
		fputs(": ", stderr);
	fprintf(stderr, "%s\n", errstr);
}

void
verr(const int code, const char *fmt, va_list ap)
{
	vewarn(fmt, ap);

	exit(code);
}

void
warn(const char *fmt, ...)
{
	va_list ap;

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

void
ewarn(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vewarn(fmt, ap);
	va_end(ap);
}

void
err(const int code, const char *fmt, ...)
{
	va_list ap;
	
	va_start(ap, fmt);
	verr(code, fmt, ap);
	va_end(ap);
}