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 |
|
1418602 |
va_list ap; |
33 |
|
|
int ret; |
34 |
|
|
|
35 |
|
709301 |
va_start(ap, fmt); |
36 |
|
709301 |
ret = vasprintf(dest, fmt, ap); |
37 |
|
709301 |
va_end(ap); |
38 |
|
|
|
39 |
✗✓ |
709301 |
if (ret == -1) |
40 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
41 |
|
709301 |
return ret; |
42 |
|
709301 |
} |
43 |
|
|
|
44 |
|
|
void * |
45 |
|
|
mandoc_calloc(size_t num, size_t size) |
46 |
|
|
{ |
47 |
|
|
void *ptr; |
48 |
|
|
|
49 |
|
13615660 |
ptr = calloc(num, size); |
50 |
✗✓ |
6807830 |
if (ptr == NULL) |
51 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
52 |
|
6807830 |
return ptr; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
void * |
56 |
|
|
mandoc_malloc(size_t size) |
57 |
|
|
{ |
58 |
|
|
void *ptr; |
59 |
|
|
|
60 |
|
28938948 |
ptr = malloc(size); |
61 |
✗✓ |
14469474 |
if (ptr == NULL) |
62 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
63 |
|
14469474 |
return ptr; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
void * |
67 |
|
|
mandoc_realloc(void *ptr, size_t size) |
68 |
|
|
{ |
69 |
|
76423004 |
ptr = realloc(ptr, size); |
70 |
✗✓ |
38211502 |
if (ptr == NULL) |
71 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
72 |
|
38211502 |
return ptr; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void * |
76 |
|
|
mandoc_reallocarray(void *ptr, size_t num, size_t size) |
77 |
|
|
{ |
78 |
|
1255702 |
ptr = reallocarray(ptr, num, size); |
79 |
✗✓ |
627851 |
if (ptr == NULL) |
80 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
81 |
|
627851 |
return ptr; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
void * |
85 |
|
|
mandoc_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size) |
86 |
|
|
{ |
87 |
|
846 |
ptr = recallocarray(ptr, oldnum, num, size); |
88 |
✗✓ |
423 |
if (ptr == NULL) |
89 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
90 |
|
423 |
return ptr; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
char * |
94 |
|
|
mandoc_strdup(const char *ptr) |
95 |
|
|
{ |
96 |
|
|
char *p; |
97 |
|
|
|
98 |
|
5151542 |
p = strdup(ptr); |
99 |
✗✓ |
2575771 |
if (p == NULL) |
100 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
101 |
|
2575771 |
return p; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
char * |
105 |
|
|
mandoc_strndup(const char *ptr, size_t sz) |
106 |
|
|
{ |
107 |
|
|
char *p; |
108 |
|
|
|
109 |
|
9698692 |
p = mandoc_malloc(sz + 1); |
110 |
|
4849346 |
memcpy(p, ptr, sz); |
111 |
|
4849346 |
p[(int)sz] = '\0'; |
112 |
|
4849346 |
return p; |
113 |
|
|
} |