libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/error.c (42 lines, 1.3 KiB) -rw-r--r-- file download

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