view compat/err.c @ 33:fa93d2ff9c62

Prevent potential division by zero Add safeguard ensuring that there is at least one possible character to generate passwords from in order to exclude the possibility of a division by zero error in rand_uniform.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue, 30 Jul 2019 20:38:08 +0200
parents a7e41e1a79c8
children
line wrap: on
line source

/*
 * Copyright (C) 2011 Guido Berhoerster <guido+pwm@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

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

#include "err.h"
#include "../compat.h"

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

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

	exit(eval);
}

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

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

	exit(eval);
}

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

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

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

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

void
verr(int eval, const char *fmt, va_list args)
{
	vwarn(fmt, args);

	exit(eval);
}

void
verrx(int eval, const char *fmt, va_list args)
{
	vwarnx(fmt, args);

	exit(eval);
}

void
vwarn(const char *fmt, va_list args)
{
	int	old_errno = errno;

	fprintf(stderr, "%s: ", getprogname());
	if (fmt != NULL) {
		vfprintf(stderr, fmt, args);
		fprintf(stderr, ": ");
	}
	fprintf(stderr, "%s\n", strerror(old_errno));
	errno = old_errno;
}

void
vwarnx(const char *fmt, va_list args)
{
	fprintf(stderr, "%s: ", getprogname());
	if (fmt != NULL) {
		vfprintf(stderr, fmt, args);
	}
	fputc('\n', stderr);
}