File: error.c

package info (click to toggle)
httping 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 468 kB
  • sloc: ansic: 4,475; python: 66; php: 15; makefile: 12
file content (56 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */

#include "gen.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>

char last_error[4096] = { 0 };

extern char json_output;

void error_exit(char *format, ...)
{
	int e = errno;
	va_list ap;

	va_start(ap, format);
	(void)vfprintf(stderr, format, ap);
	va_end(ap);

	if (json_output) {
	  printf("\n]\n");
	}

	fprintf(stderr, gettext("\n\nerrno=%d which means %s (if applicable)\n"), e, strerror(e));

	exit(1);
}

void set_error(const char *fmt, ...)
{
	int buffer_size = sizeof last_error;
	va_list ap;

	if (last_error[0])
		fprintf(stderr, "%s\n", last_error);

	va_start(ap, fmt);
	if (vsnprintf(last_error, sizeof last_error, fmt, ap) >= buffer_size)
		error_exit(gettext("Error message '%s' truncated"), last_error);
	va_end(ap);
}

void clear_error()
{
	last_error[0] = 0x00;
}

char * get_error()
{
	return last_error;
}