X-Git-Url: https://git.cameronkatri.com/mandoc.git/blobdiff_plain/542b6aa18abe9cadcf68c4a5c88bde4353e5eac9..f1eb9ac11c55c6ac35cb8ea29c074c6b23f587ee:/mandoc_aux.c diff --git a/mandoc_aux.c b/mandoc_aux.c index 2275bbcf..5d595ce0 100644 --- a/mandoc_aux.c +++ b/mandoc_aux.c @@ -1,7 +1,7 @@ -/* $Id: mandoc_aux.c,v 1.4 2014/08/10 23:54:41 schwarze Exp $ */ +/* $Id: mandoc_aux.c,v 1.11 2018/02/07 20:04:57 schwarze Exp $ */ /* * Copyright (c) 2009, 2011 Kristaps Dzonsons - * Copyright (c) 2014 Ingo Schwarze + * Copyright (c) 2014, 2015, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,6 +19,9 @@ #include +#if HAVE_ERR +#include +#endif #include #include #include @@ -27,6 +30,7 @@ #include "mandoc.h" #include "mandoc_aux.h" + int mandoc_asprintf(char **dest, const char *fmt, ...) { @@ -37,11 +41,9 @@ mandoc_asprintf(char **dest, const char *fmt, ...) ret = vasprintf(dest, fmt, ap); va_end(ap); - if (-1 == ret) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(ret); + if (ret == -1) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ret; } void * @@ -50,11 +52,9 @@ mandoc_calloc(size_t num, size_t size) void *ptr; ptr = calloc(num, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(ptr); + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ptr; } void * @@ -63,35 +63,36 @@ mandoc_malloc(size_t size) void *ptr; ptr = malloc(size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(ptr); + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ptr; } void * mandoc_realloc(void *ptr, size_t size) { - ptr = realloc(ptr, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(ptr); + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ptr; } void * mandoc_reallocarray(void *ptr, size_t num, size_t size) { - ptr = reallocarray(ptr, num, size); - if (NULL == ptr) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(ptr); + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ptr; +} + +void * +mandoc_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size) +{ + ptr = recallocarray(ptr, oldnum, num, size); + if (ptr == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return ptr; } char * @@ -100,11 +101,9 @@ mandoc_strdup(const char *ptr) char *p; p = strdup(ptr); - if (NULL == p) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - return(p); + if (p == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return p; } char * @@ -112,8 +111,8 @@ mandoc_strndup(const char *ptr, size_t sz) { char *p; - p = mandoc_malloc(sz + 1); - memcpy(p, ptr, sz); - p[(int)sz] = '\0'; - return(p); + p = strndup(ptr, sz); + if (p == NULL) + err((int)MANDOCLEVEL_SYSERR, NULL); + return p; }