1*260e9a87SYuri Pankov /* $Id: mandoc_aux.c,v 1.4 2014/08/10 23:54:41 schwarze Exp $ */ 2*260e9a87SYuri Pankov /* 3*260e9a87SYuri Pankov * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4*260e9a87SYuri Pankov * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org> 5*260e9a87SYuri Pankov * 6*260e9a87SYuri Pankov * Permission to use, copy, modify, and distribute this software for any 7*260e9a87SYuri Pankov * purpose with or without fee is hereby granted, provided that the above 8*260e9a87SYuri Pankov * copyright notice and this permission notice appear in all copies. 9*260e9a87SYuri Pankov * 10*260e9a87SYuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 11*260e9a87SYuri Pankov * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12*260e9a87SYuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 13*260e9a87SYuri Pankov * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14*260e9a87SYuri Pankov * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15*260e9a87SYuri Pankov * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16*260e9a87SYuri Pankov * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17*260e9a87SYuri Pankov */ 18*260e9a87SYuri Pankov #include "config.h" 19*260e9a87SYuri Pankov 20*260e9a87SYuri Pankov #include <sys/types.h> 21*260e9a87SYuri Pankov 22*260e9a87SYuri Pankov #include <stdarg.h> 23*260e9a87SYuri Pankov #include <stdlib.h> 24*260e9a87SYuri Pankov #include <stdio.h> 25*260e9a87SYuri Pankov #include <string.h> 26*260e9a87SYuri Pankov 27*260e9a87SYuri Pankov #include "mandoc.h" 28*260e9a87SYuri Pankov #include "mandoc_aux.h" 29*260e9a87SYuri Pankov 30*260e9a87SYuri Pankov int 31*260e9a87SYuri Pankov mandoc_asprintf(char **dest, const char *fmt, ...) 32*260e9a87SYuri Pankov { 33*260e9a87SYuri Pankov va_list ap; 34*260e9a87SYuri Pankov int ret; 35*260e9a87SYuri Pankov 36*260e9a87SYuri Pankov va_start(ap, fmt); 37*260e9a87SYuri Pankov ret = vasprintf(dest, fmt, ap); 38*260e9a87SYuri Pankov va_end(ap); 39*260e9a87SYuri Pankov 40*260e9a87SYuri Pankov if (-1 == ret) { 41*260e9a87SYuri Pankov perror(NULL); 42*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 43*260e9a87SYuri Pankov } 44*260e9a87SYuri Pankov return(ret); 45*260e9a87SYuri Pankov } 46*260e9a87SYuri Pankov 47*260e9a87SYuri Pankov void * 48*260e9a87SYuri Pankov mandoc_calloc(size_t num, size_t size) 49*260e9a87SYuri Pankov { 50*260e9a87SYuri Pankov void *ptr; 51*260e9a87SYuri Pankov 52*260e9a87SYuri Pankov ptr = calloc(num, size); 53*260e9a87SYuri Pankov if (NULL == ptr) { 54*260e9a87SYuri Pankov perror(NULL); 55*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 56*260e9a87SYuri Pankov } 57*260e9a87SYuri Pankov return(ptr); 58*260e9a87SYuri Pankov } 59*260e9a87SYuri Pankov 60*260e9a87SYuri Pankov void * 61*260e9a87SYuri Pankov mandoc_malloc(size_t size) 62*260e9a87SYuri Pankov { 63*260e9a87SYuri Pankov void *ptr; 64*260e9a87SYuri Pankov 65*260e9a87SYuri Pankov ptr = malloc(size); 66*260e9a87SYuri Pankov if (NULL == ptr) { 67*260e9a87SYuri Pankov perror(NULL); 68*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 69*260e9a87SYuri Pankov } 70*260e9a87SYuri Pankov return(ptr); 71*260e9a87SYuri Pankov } 72*260e9a87SYuri Pankov 73*260e9a87SYuri Pankov void * 74*260e9a87SYuri Pankov mandoc_realloc(void *ptr, size_t size) 75*260e9a87SYuri Pankov { 76*260e9a87SYuri Pankov 77*260e9a87SYuri Pankov ptr = realloc(ptr, size); 78*260e9a87SYuri Pankov if (NULL == ptr) { 79*260e9a87SYuri Pankov perror(NULL); 80*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 81*260e9a87SYuri Pankov } 82*260e9a87SYuri Pankov return(ptr); 83*260e9a87SYuri Pankov } 84*260e9a87SYuri Pankov 85*260e9a87SYuri Pankov void * 86*260e9a87SYuri Pankov mandoc_reallocarray(void *ptr, size_t num, size_t size) 87*260e9a87SYuri Pankov { 88*260e9a87SYuri Pankov 89*260e9a87SYuri Pankov ptr = reallocarray(ptr, num, size); 90*260e9a87SYuri Pankov if (NULL == ptr) { 91*260e9a87SYuri Pankov perror(NULL); 92*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 93*260e9a87SYuri Pankov } 94*260e9a87SYuri Pankov return(ptr); 95*260e9a87SYuri Pankov } 96*260e9a87SYuri Pankov 97*260e9a87SYuri Pankov char * 98*260e9a87SYuri Pankov mandoc_strdup(const char *ptr) 99*260e9a87SYuri Pankov { 100*260e9a87SYuri Pankov char *p; 101*260e9a87SYuri Pankov 102*260e9a87SYuri Pankov p = strdup(ptr); 103*260e9a87SYuri Pankov if (NULL == p) { 104*260e9a87SYuri Pankov perror(NULL); 105*260e9a87SYuri Pankov exit((int)MANDOCLEVEL_SYSERR); 106*260e9a87SYuri Pankov } 107*260e9a87SYuri Pankov return(p); 108*260e9a87SYuri Pankov } 109*260e9a87SYuri Pankov 110*260e9a87SYuri Pankov char * 111*260e9a87SYuri Pankov mandoc_strndup(const char *ptr, size_t sz) 112*260e9a87SYuri Pankov { 113*260e9a87SYuri Pankov char *p; 114*260e9a87SYuri Pankov 115*260e9a87SYuri Pankov p = mandoc_malloc(sz + 1); 116*260e9a87SYuri Pankov memcpy(p, ptr, sz); 117*260e9a87SYuri Pankov p[(int)sz] = '\0'; 118*260e9a87SYuri Pankov return(p); 119*260e9a87SYuri Pankov } 120