File: xmalloc.c

package info (click to toggle)
wraplinux 1.7-10
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 424 kB
  • sloc: ansic: 1,552; asm: 427; perl: 155; sh: 152; makefile: 88
file content (53 lines) | stat: -rw-r--r-- 739 bytes parent folder | download | duplicates (3)
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
/*
 * xmalloc.c
 *
 * Simple error-checking version of malloc()
 *
 */

#include "wraplinux.h"

#include <errno.h>
#include <stdarg.h>
#include <string.h>

void *xmalloc(size_t size)
{
	void *p = malloc(size);

	if (!p) {
		fprintf(stderr, "%s: %s\n", program, strerror(errno));
		exit(EX_OSERR);
	}

	return p;
}

void *xcalloc(size_t nmemb, size_t size)
{
	void *p = calloc(nmemb, size);

	if (!p) {
		fprintf(stderr, "%s: %s\n", program, strerror(errno));
		exit(EX_OSERR);
	}

	return p;
}

int xasprintf(char **strp, const char *fmt, ...)
{
	va_list va;
	int n;

	va_start(va, fmt);
	n = vasprintf(strp, fmt, va);
	va_end(va);

	if (n < 0) {
		fprintf(stderr, "%s: %s\n", program, strerror(errno));
		exit(EX_OSERR);
	}

	return n;
}