1 |
|
|
/* $OpenBSD: mandoc_aux.c,v 1.8 2017/06/12 18:55:42 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 |
30 |
|
|
mandoc_asprintf(char **dest, const char *fmt, ...) |
31 |
|
|
{ |
32 |
|
448456 |
va_list ap; |
33 |
|
|
int ret; |
34 |
|
|
|
35 |
|
224228 |
va_start(ap, fmt); |
36 |
|
224228 |
ret = vasprintf(dest, fmt, ap); |
37 |
|
224228 |
va_end(ap); |
38 |
|
|
|
39 |
✗✓ |
224228 |
if (ret == -1) |
40 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
41 |
|
224228 |
return ret; |
42 |
|
224228 |
} |
43 |
|
|
|
44 |
|
|
void * |
45 |
|
|
mandoc_calloc(size_t num, size_t size) |
46 |
|
|
{ |
47 |
|
|
void *ptr; |
48 |
|
|
|
49 |
|
4427580 |
ptr = calloc(num, size); |
50 |
✗✓ |
2213790 |
if (ptr == NULL) |
51 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
52 |
|
2213790 |
return ptr; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
void * |
56 |
|
|
mandoc_malloc(size_t size) |
57 |
|
|
{ |
58 |
|
|
void *ptr; |
59 |
|
|
|
60 |
|
9605790 |
ptr = malloc(size); |
61 |
✗✓ |
4802895 |
if (ptr == NULL) |
62 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
63 |
|
4802895 |
return ptr; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
void * |
67 |
|
|
mandoc_realloc(void *ptr, size_t size) |
68 |
|
|
{ |
69 |
|
23686446 |
ptr = realloc(ptr, size); |
70 |
✗✓ |
11843223 |
if (ptr == NULL) |
71 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
72 |
|
11843223 |
return ptr; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void * |
76 |
|
|
mandoc_reallocarray(void *ptr, size_t num, size_t size) |
77 |
|
|
{ |
78 |
|
391422 |
ptr = reallocarray(ptr, num, size); |
79 |
✗✓ |
195711 |
if (ptr == NULL) |
80 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
81 |
|
195711 |
return ptr; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
void * |
85 |
|
|
mandoc_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size) |
86 |
|
|
{ |
87 |
|
286 |
ptr = recallocarray(ptr, oldnum, num, size); |
88 |
✗✓ |
143 |
if (ptr == NULL) |
89 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
90 |
|
143 |
return ptr; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
char * |
94 |
|
|
mandoc_strdup(const char *ptr) |
95 |
|
|
{ |
96 |
|
|
char *p; |
97 |
|
|
|
98 |
|
1687434 |
p = strdup(ptr); |
99 |
✗✓ |
843717 |
if (p == NULL) |
100 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
101 |
|
843717 |
return p; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
char * |
105 |
|
|
mandoc_strndup(const char *ptr, size_t sz) |
106 |
|
|
{ |
107 |
|
|
char *p; |
108 |
|
|
|
109 |
|
3221860 |
p = mandoc_malloc(sz + 1); |
110 |
|
1610930 |
memcpy(p, ptr, sz); |
111 |
|
1610930 |
p[(int)sz] = '\0'; |
112 |
|
1610930 |
return p; |
113 |
|
|
} |