xref: /openbsd/usr.bin/mandoc/mandoc_aux.c (revision 2dfc6a66)
1 /*	$OpenBSD: mandoc_aux.c,v 1.9 2018/02/07 20:04:33 schwarze Exp $ */
2 /*
3  * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and 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 AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 #include <sys/types.h>
19 
20 #include <err.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "mandoc.h"
27 #include "mandoc_aux.h"
28 
29 int
mandoc_asprintf(char ** dest,const char * fmt,...)30 mandoc_asprintf(char **dest, const char *fmt, ...)
31 {
32 	va_list	 ap;
33 	int	 ret;
34 
35 	va_start(ap, fmt);
36 	ret = vasprintf(dest, fmt, ap);
37 	va_end(ap);
38 
39 	if (ret == -1)
40 		err((int)MANDOCLEVEL_SYSERR, NULL);
41 	return ret;
42 }
43 
44 void *
mandoc_calloc(size_t num,size_t size)45 mandoc_calloc(size_t num, size_t size)
46 {
47 	void	*ptr;
48 
49 	ptr = calloc(num, size);
50 	if (ptr == NULL)
51 		err((int)MANDOCLEVEL_SYSERR, NULL);
52 	return ptr;
53 }
54 
55 void *
mandoc_malloc(size_t size)56 mandoc_malloc(size_t size)
57 {
58 	void	*ptr;
59 
60 	ptr = malloc(size);
61 	if (ptr == NULL)
62 		err((int)MANDOCLEVEL_SYSERR, NULL);
63 	return ptr;
64 }
65 
66 void *
mandoc_realloc(void * ptr,size_t size)67 mandoc_realloc(void *ptr, size_t size)
68 {
69 	ptr = realloc(ptr, size);
70 	if (ptr == NULL)
71 		err((int)MANDOCLEVEL_SYSERR, NULL);
72 	return ptr;
73 }
74 
75 void *
mandoc_reallocarray(void * ptr,size_t num,size_t size)76 mandoc_reallocarray(void *ptr, size_t num, size_t size)
77 {
78 	ptr = reallocarray(ptr, num, size);
79 	if (ptr == NULL)
80 		err((int)MANDOCLEVEL_SYSERR, NULL);
81 	return ptr;
82 }
83 
84 void *
mandoc_recallocarray(void * ptr,size_t oldnum,size_t num,size_t size)85 mandoc_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size)
86 {
87 	ptr = recallocarray(ptr, oldnum, num, size);
88 	if (ptr == NULL)
89 		err((int)MANDOCLEVEL_SYSERR, NULL);
90 	return ptr;
91 }
92 
93 char *
mandoc_strdup(const char * ptr)94 mandoc_strdup(const char *ptr)
95 {
96 	char	*p;
97 
98 	p = strdup(ptr);
99 	if (p == NULL)
100 		err((int)MANDOCLEVEL_SYSERR, NULL);
101 	return p;
102 }
103 
104 char *
mandoc_strndup(const char * ptr,size_t sz)105 mandoc_strndup(const char *ptr, size_t sz)
106 {
107 	char	*p;
108 
109 	p = strndup(ptr, sz);
110 	if (p == NULL)
111 		err((int)MANDOCLEVEL_SYSERR, NULL);
112 	return p;
113 }
114