Xinqi Bao's Git

source utility functions out to util.c
[st.git] / util.c
1 /* See LICENSE file for copyright and license details. */
2 #include "util.h"
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 void *
10 emallocz(unsigned int size) {
11 void *res = calloc(1, size);
12
13 if(!res)
14 eprint("fatal: could not malloc() %u bytes\n", size);
15 return res;
16 }
17
18 void
19 eprint(const char *errstr, ...) {
20 va_list ap;
21
22 va_start(ap, errstr);
23 vfprintf(stderr, errstr, ap);
24 va_end(ap);
25 exit(EXIT_FAILURE);
26 }
27
28 void
29 eprintn(const char *errstr, ...) {
30 va_list ap;
31
32 va_start(ap, errstr);
33 vfprintf(stderr, errstr, ap);
34 va_end(ap);
35 fprintf(stderr, ": %s\n", strerror(errno));
36 exit(EXIT_FAILURE);
37 }