summaryrefslogtreecommitdiffstats
path: root/eprintf.c
blob: 9f4aa111c6abe4d6e3b0567d38fff82c4f15f93e (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "eprintf.h"

static char *pname;

// doprintf: print error message
__attribute__((format(printf, 1, 0)))
static void doprintf(const char *fmt, va_list va, int errnum)
{
	assert(fmt != NULL);

	if (pname != NULL)
		fprintf(stderr, "%s: ", pname);

	vfprintf(stderr, fmt, va);

	if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
		fprintf(stderr, " %s", strerror(errnum));
	fputc('\n', stderr);
}

// eprintf: print error and exit, a trailing : in fmt prints strerror(errno)
void eprintf(const char *fmt, ...)
{
	va_list va;

	va_start(va, fmt);
	doprintf(fmt, va, errno); // TODO: can va_start set errno
	va_end(va);

	exit(EXIT_FAILURE);
}

// weprintf: print warning, a trailing : in fmt prints strerror(errno)
void weprintf(const char *fmt, ...)
{
	va_list va;

	va_start(va, fmt);
	doprintf(fmt, va, errno); // TODO: Ditto
	va_end(va);
}

// estrdup: attempt to strdup a string or exit on error
char *estrdup(const char *s)
{
	size_t len;
	char *dup;

	assert(s != NULL);

	len = strlen(s);
	if (len == SIZE_MAX)
		eprintf("estrdup(\"%.20s\") failed: Too long", s);
	dup = malloc(len + 1);
	if (dup == NULL)
		eprintf("estrdup(\"%.20s\") failed:", s);
	strcpy(dup, s);

	return dup;
}

// emalloc: attempt to allocate memory or exit on error
void *emalloc(size_t n)
{
	void *p;

	p = malloc(n);
	if (p == NULL)
		eprintf("emalloc(%zu) failed:", n);

	return p;
}

// ecalloc: attempt to allocate memory or exit on error
void *ecalloc(size_t nmemb, size_t size)
{
	void *p;

	p = calloc(nmemb, size);
	if (p == NULL)
		eprintf("ecalloc(%zu, %zu) failed:", nmemb, size);

	return p;
}

// erealloc: attempt to reallocate memory or exit on error
void *erealloc(void *p, size_t n)
{
	void *r;

	r = realloc(p, n);
	if (r == NULL)
		eprintf("erealloc(%p, %zu) failed:", p, n);

	return r;
}

// setprogname: set stored name of program
void setprogname(const char *s)
{
	assert(s != NULL);

	if (pname != NULL) {
		weprintf("setprogname(\"%.20s\") called more than once", s);
		return;
	}

	pname = estrdup(s);
}