coreutils

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

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

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