aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: bd7e0bfae8a9608cd1f5ef62447348d31e4e3e1f (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* util.c
 *
 * Copyright (c) 2014 Tharre
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#define _XOPEN_SOURCE 600
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "util.h"
#define _FILENAME "util.c"
#include "dbg.h"


/* Print a given formated error message and die. */
extern void __attribute__((noreturn)) die_(const char *err, ...) {
	assert(err);
	va_list ap;
	va_start(ap, err);

	vfprintf(stderr, err, ap);

	va_end(ap);
	exit(EXIT_FAILURE);
}

void *xmalloc(size_t size) {
	assert(size > 0);
	void *ptr = malloc(size);
	if (!ptr)
		fatal("Cannot allocate %zu bytes", size);

	return ptr;
}

void *xrealloc(void *ptr, size_t size) {
	assert(size > 0 && ptr);
	if (!(ptr = realloc(ptr, size)))
		fatal("Cannot reallocate %zu bytes", size);

	return ptr;
}

char *xstrdup(const char *str) {
	assert(str);
	if (!(str = strdup(str)))
		fatal("Insufficient memory for string allocation");

	return (char*) str;
}

/* For concating multiple strings into a single larger one. */
char *concat(size_t count, ...) {
	assert(count > 0);
	va_list ap, ap2;
	va_start(ap, count);
	va_copy(ap2, ap);
	size_t i, size = 0, args_len[count];
	for (i = 0; i < count; ++i) {
		args_len[i] = strlen(va_arg(ap, char*));
		size += args_len[i];
	}
	++size;
	char *result = xmalloc(size);
	/* debug("Allocated %zu bytes at %p\n", size, result); */
	uintptr_t offset = 0;
	for (i = 0; i < count; ++i) {
		strcpy(&result[offset], va_arg(ap2, char*));
		offset += args_len[i];
	}
	va_end(ap);
	va_end(ap2);
	return result;
}