File: error.c

package info (click to toggle)
debfoster 2.7-2.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,188 kB
  • sloc: sh: 4,243; ansic: 2,397; perl: 39; makefile: 26; sed: 16
file content (51 lines) | stat: -rw-r--r-- 927 bytes parent folder | download | duplicates (5)
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
#include "error.h"
#include <string.h>
#include <gc/gc.h>

void error_exit(error_t e, const char *msg, ...) {
	va_list ap;

	va_start(ap, msg);
	vfprintf(stderr, msg, ap);
	va_end(ap);

	exit(e);
}

void perror_exit(error_t e, const char *msg) {
	perror(msg);
	exit(e);
}

void *xmalloc(size_t l) {
	void *r = GC_malloc(l);
	if(!r)
		perror_exit(ERROR_SYSTEM, "GC_alloc");
	return r;
}

char *xstrdup(const char *s) {
	char * p = GC_strdup(s);
	if (!p)
		perror_exit(ERROR_SYSTEM, "GC_strdup");
	return p;
}

char *xstrndup(const char *s, int n) {
	char *p = GC_malloc_atomic(n + 1);
	if (!p)
		perror_exit(ERROR_SYSTEM, "gc_malloc_atomic");
	memcpy(p, s, n);
	p[n] = 0;
	return p;
}

char *xstrcat(const char *s1, const char *s2) {
	size_t s1len = strlen(s1);
	size_t s2len = strlen(s2);
	char *s = GC_malloc_atomic(s1len + s2len + 1);
	memcpy(s, s1, s1len);
	memcpy(s + s1len, s2, s2len);
	s[s1len + s2len] = 0;
	return s;
}