Mercurial > paster
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util.c Tue Feb 04 13:35:52 2020 +0100 @@ -0,0 +1,53 @@ +/* + * util.c -- various utilities + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> +#include <errno.h> +#include <stdarg.h> +#include <stdlib.h> +#include <stdnoreturn.h> +#include <string.h> +#include <stdio.h> + +#include "util.h" + +noreturn void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + exit(1); +} + +char * +estrdup(const char *str) +{ + assert(str); + + char *ret; + size_t length = strlen(str); + + if (!(ret = calloc(1, length + 1))) + die(strerror(errno)); + + return strcpy(ret, str); +}