comparison util.c @ 0:15a06aa20298

misc: initial import
author David Demelier <markand@malikania.fr>
date Tue, 04 Feb 2020 13:35:52 +0100
parents
children 836a698946f8
comparison
equal deleted inserted replaced
-1:000000000000 0:15a06aa20298
1 /*
2 * util.c -- various utilities
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdnoreturn.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "util.h"
28
29 noreturn void
30 die(const char *fmt, ...)
31 {
32 va_list ap;
33
34 va_start(ap, fmt);
35 vfprintf(stderr, fmt, ap);
36 va_end(ap);
37
38 exit(1);
39 }
40
41 char *
42 estrdup(const char *str)
43 {
44 assert(str);
45
46 char *ret;
47 size_t length = strlen(str);
48
49 if (!(ret = calloc(1, length + 1)))
50 die(strerror(errno));
51
52 return strcpy(ret, str);
53 }