err 2.0.0
Small error-printing library written in C
Loading...
Searching...
No Matches
err.c
Go to the documentation of this file.
1/*
2 * err - Small error-printing library for C
3 * Copyright (C) 2021-2022 Sebastian LaVine <mail@smlavine.com>
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10
11/**
12 * @file err.c
13 * @version 2.0.0
14 * @brief Main source code file
15 * @details This file contains definitions and declarations of globally
16 * available functions and variables.
17 */
18
19#include <errno.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25/* With glibc and perhaps some other environments, this variable is
26 * declared in errno.h. But as it doesn't seem to cause any issues
27 * compiling, We declare it again here without a preprocessor guard for
28 * maximum portability. */
30
31/**
32 * @details Prints program_invocation_name, ": ", and the
33 * printf(3)-like-formatted error message.
34 */
35void
36vwarn(const char *fmt, va_list ap)
37{
38 fprintf(stderr, "%s: ", program_invocation_name);
39 vfprintf(stderr, fmt, ap);
40}
41
42/**
43 * @details Calls vwarn(), then prints ": ", strerror(errno), and a newline.
44 */
45void
46vewarn(const char *fmt, va_list ap)
47{
48 vwarn(fmt, ap);
49 if (errno != 0) {
50 /* To avoid two colons being printed, like
51 * "program_invocation_name: : No such file or directory" */
52 if (fmt != NULL && fmt[0] != '\0') {
53 fputs(": ", stderr);
54 }
55 fprintf(stderr, "%s", strerror(errno));
56 }
57 fputc('\n', stderr);
58}
59
60/**
61 * @details Calls vewarn() and exits the program with the provided code.
62 */
63void
64verrc(const int code, const char *fmt, va_list ap)
65{
66 vewarn(fmt, ap);
67
68 exit(code);
69}
70
71/**
72 * @details Calls verrc() with EXIT_FAILURE.
73 */
74void
75verr(const char *fmt, va_list ap)
76{
77 verrc(EXIT_FAILURE, fmt, ap);
78}
79
80/**
81 * @details Variadic wrapper for vwarn().
82 */
83void
84warn(const char *fmt, ...)
85{
86 va_list ap;
87
88 va_start(ap, fmt);
89 vwarn(fmt, ap);
90 va_end(ap);
91}
92
93/**
94 * @details Variadic wrapper for vewarn().
95 */
96void
97ewarn(const char *fmt, ...)
98{
99 va_list ap;
100
101 va_start(ap, fmt);
102 vewarn(fmt, ap);
103 va_end(ap);
104}
105
106/**
107 * @details Variadic wrapper for verrc().
108 */
109void
110errc(const int code, const char *fmt, ...)
111{
112 va_list ap;
113
114 va_start(ap, fmt);
115 verrc(code, fmt, ap);
116 va_end(ap);
117}
118
119/**
120 * @details Variadic wrapper for verr().
121 */
122void
123err(const char *fmt, ...)
124{
125 va_list ap;
126
127 va_start(ap, fmt);
128 verr(fmt, ap);
129 va_end(ap);
130}
void vwarn(const char *fmt, va_list ap)
Prints a formatted warning message to stderr.
Definition: err.c:36
void verrc(const int code, const char *fmt, va_list ap)
Prints a formatted error message to stderr and exits with the given exit code.
Definition: err.c:64
char * program_invocation_name
Global value for the program's name.
Definition: err.c:29
void ewarn(const char *fmt,...)
Prints a formatted error message to stderr.
Definition: err.c:97
void vewarn(const char *fmt, va_list ap)
Prints a formatted error message to stderr.
Definition: err.c:46
void err(const char *fmt,...)
Prints a formatted error message to stderr and exits.
Definition: err.c:123
void verr(const char *fmt, va_list ap)
Prints a formatted error message to stderr and exits.
Definition: err.c:75
void warn(const char *fmt,...)
Prints a formatted warning message to stderr.
Definition: err.c:84
void errc(const int code, const char *fmt,...)
Prints a formatted error message to stderr and exits with the given exit code.
Definition: err.c:110