~smlavine/navipage

6a3e6b4e07c131e2a256dabda02d1317b0088a7a — Sebastian LaVine 3 months ago 950360f
Use versioned version (1.1.0) of err

See <https://sr.ht/~smlavine/err>.
2 files changed, 91 insertions(+), 46 deletions(-)

M err.c
M err.h
M err.c => err.c +36 -42
@@ 1,6 1,6 @@
/*
 * err - Small error-printing library
 * Copyright (C) 2021 Sebastian LaVine <mail@smlavine.com>
 * err - Small error-printing library for C
 * Copyright (C) 2021-2022 Sebastian LaVine <mail@smlavine.com>
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public


@@ 8,6 8,14 @@
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/**
 * @file err.c
 * @version 1.1.0
 * @brief Main source code file
 * @details This file contains definitions and declarations of globally
 * available functions and variables.
 */

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


@@ 18,38 26,9 @@

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, then prints 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.
 *
 ***********************************************************************
/**
 * @details Prints argv0, ": ", and the printf(3)-like-formatted error message.
 */

void
vwarn(const char *fmt, va_list ap)
{


@@ 57,21 36,27 @@ vwarn(const char *fmt, va_list ap)
	vfprintf(stderr, fmt, ap);
}

/**
 * @details Calls vwarn(), then prints ": ", strerror(errno), and a newline.
 */
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);
	if (errno != 0) {
		/* To avoid two colons being printed, like
		 * "argv0: : No such file or directory" */
		if (fmt != NULL && fmt[0] != '\0') {
			fputs(": ", stderr);
		}
		fprintf(stderr, "%s", strerror(errno));
	}
	fputc('\n', stderr);
}

/**
 * @details Calls vewarn() and exits the program with the provided code.
 */
void
verr(const int code, const char *fmt, va_list ap)
{


@@ 80,6 65,9 @@ verr(const int code, const char *fmt, va_list ap)
	exit(code);
}

/**
 * @details Variadic wrapper for vwarn().
 */
void
warn(const char *fmt, ...)
{


@@ 90,6 78,9 @@ warn(const char *fmt, ...)
	va_end(ap);
}

/**
 * @details Variadic wrapper for vewarn().
 */
void
ewarn(const char *fmt, ...)
{


@@ 100,6 91,9 @@ ewarn(const char *fmt, ...)
	va_end(ap);
}

/**
 * @details Variadic wrapper for verr().
 */
void
err(const int code, const char *fmt, ...)
{

M err.h => err.h +55 -4
@@ 1,6 1,6 @@
/*
 * err - Small error-printing library
 * Copyright (C) 2021 Sebastian LaVine <mail@smlavine.com>
 * err - Small error-printing library for C
 * Copyright (C) 2021-2022 Sebastian LaVine <mail@smlavine.com>
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public


@@ 8,16 8,67 @@
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/**
 * @file err.h
 * @version 1.1.0
 * @brief Header file
 * @details #include this to use err in your project.
 */

#include <stdarg.h>

/**
 * @brief Global value for the program's name.
 * @details This must be set before any err functions are called.
 */
extern char *argv0;

/* These functions are declared in the order they are called by each other. */

/**
 * @brief Prints a formatted warning message to stderr.
 * @pre argv0 is set
 * @param fmt format string
 * @param ap va_list of arguments for the format string
 */
void vwarn(const char *, va_list);

/**
 * @brief Prints a formatted error message to stderr.
 * @pre argv0 is set
 * @param fmt format string
 * @param ap va_list of arguments for the format string
 */
void vewarn(const char *, va_list);

/**
 * @brief Prints a formatted error message to stderr and exits.
 * @pre argv0 is set
 * @param code exit code
 * @param fmt format string
 * @param ap va_list of arguments for the format string
 */
void verr(const int, const char *, va_list);

/**
 * @brief Prints a formatted warning message to stderr.
 * @pre argv0 is set
 * @param fmt format string
 * @param ... arguments for the format string
 */
void warn(const char *, ...);

/**
 * @brief Prints a formatted error message to stderr.
 * @pre argv0 is set
 * @param fmt format string
 * @param ... arguments for the format string
 */
void ewarn(const char *, ...);

/**
 * @brief Prints a formatted error message to stderr and exits.
 * @pre argv0 is set
 * @param code exit code
 * @param fmt format string
 * @param ... arguments for the format string
 */
void err(const int, const char *, ...);