cryptutils

Cryptographic Software Utilities
git clone http://git.omkov.net/cryptutils
Log | Tree | Refs | README | LICENCE | Download

cryptutils/src/util/error.c (42 lines, 1.3 KiB) -rw-r--r-- blame download

01234567891011121314151617181920212223242526272829303132333435363738394041
// util/error.h, version 1.1.2
// Error source file from libutil
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

#include "error.h"

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

char *A0 = NULL;
bool warned = false;

/* Print an error message and exit. */
noreturn void error(int status, const char *format, ...) {
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
	fputc('\n', stderr); exit(status);
}

/* Print a warning message and set the warned flag. */
void warn(const char *format, ...) {
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
	fputc('\n', stderr); warned = true; return;
}

/* Print a warning message but do not set the warned flag. */
void alert(const char *format, ...) {
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
	fputc('\n', stderr); return;
}

/* Shorthand for strerror(errno). DEPRECIATED, use the SERR macro. */
char *serr(void) { return strerror(errno); }