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-- file download

03b01d2 Jamozed 2022-03-06 17:10:01
0
// util/error.h, version 1.1.2
c01a723 Jamozed 2022-02-05 19:04:03
1
// Error source file from libutil
30b5241 Jamozed 2020-07-26 23:09:46
2
// Copyright (C) 2020, Jakob Wakeling
03b01d2 Jamozed 2022-03-06 17:10:01
3
// MIT Licence
30b5241 Jamozed 2020-07-26 23:09:46
4
30b5241 Jamozed 2020-07-26 23:09:46
5
#include "error.h"
30b5241 Jamozed 2020-07-26 23:09:46
6
30b5241 Jamozed 2020-07-26 23:09:46
7
#include <errno.h>
30b5241 Jamozed 2020-07-26 23:09:46
8
#include <stdarg.h>
595bd6b Jamozed 2021-02-16 01:28:45
9
#include <stdbool.h>
30b5241 Jamozed 2020-07-26 23:09:46
10
#include <stdio.h>
30b5241 Jamozed 2020-07-26 23:09:46
11
#include <stdlib.h>
595bd6b Jamozed 2021-02-16 01:28:45
12
#include <stdnoreturn.h>
30b5241 Jamozed 2020-07-26 23:09:46
13
#include <string.h>
30b5241 Jamozed 2020-07-26 23:09:46
14
30b5241 Jamozed 2020-07-26 23:09:46
15
char *A0 = NULL;
595bd6b Jamozed 2021-02-16 01:28:45
16
bool warned = false;
30b5241 Jamozed 2020-07-26 23:09:46
17
c01a723 Jamozed 2022-02-05 19:04:03
18
/* Print an error message and exit. */
595bd6b Jamozed 2021-02-16 01:28:45
19
noreturn void error(int status, const char *format, ...) {
30b5241 Jamozed 2020-07-26 23:09:46
20
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
30b5241 Jamozed 2020-07-26 23:09:46
21
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
30b5241 Jamozed 2020-07-26 23:09:46
22
	fputc('\n', stderr); exit(status);
30b5241 Jamozed 2020-07-26 23:09:46
23
}
30b5241 Jamozed 2020-07-26 23:09:46
24
c01a723 Jamozed 2022-02-05 19:04:03
25
/* Print a warning message and set the warned flag. */
30b5241 Jamozed 2020-07-26 23:09:46
26
void warn(const char *format, ...) {
30b5241 Jamozed 2020-07-26 23:09:46
27
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
30b5241 Jamozed 2020-07-26 23:09:46
28
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
595bd6b Jamozed 2021-02-16 01:28:45
29
	fputc('\n', stderr); warned = true; return;
30b5241 Jamozed 2020-07-26 23:09:46
30
}
30b5241 Jamozed 2020-07-26 23:09:46
31
c01a723 Jamozed 2022-02-05 19:04:03
32
/* Print a warning message but do not set the warned flag. */
c01a723 Jamozed 2022-02-05 19:04:03
33
void alert(const char *format, ...) {
c01a723 Jamozed 2022-02-05 19:04:03
34
	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
c01a723 Jamozed 2022-02-05 19:04:03
35
	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
c01a723 Jamozed 2022-02-05 19:04:03
36
	fputc('\n', stderr); return;
c01a723 Jamozed 2022-02-05 19:04:03
37
}
c01a723 Jamozed 2022-02-05 19:04:03
38
c01a723 Jamozed 2022-02-05 19:04:03
39
/* Shorthand for strerror(errno). DEPRECIATED, use the SERR macro. */
30b5241 Jamozed 2020-07-26 23:09:46
40
char *serr(void) { return strerror(errno); }
41