1 |
|
|
/* $OpenBSD: mandoc_aux.c,v 1.7 2015/10/12 21:09:08 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2014 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 |
|
|
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 * |
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 * |
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 * |
67 |
|
|
mandoc_realloc(void *ptr, size_t size) |
68 |
|
|
{ |
69 |
|
|
|
70 |
|
|
ptr = realloc(ptr, size); |
71 |
|
|
if (ptr == NULL) |
72 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
73 |
|
|
return ptr; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
void * |
77 |
|
|
mandoc_reallocarray(void *ptr, size_t num, size_t size) |
78 |
|
|
{ |
79 |
|
|
|
80 |
|
|
ptr = reallocarray(ptr, num, size); |
81 |
|
|
if (ptr == NULL) |
82 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
83 |
|
|
return ptr; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
char * |
87 |
|
|
mandoc_strdup(const char *ptr) |
88 |
|
|
{ |
89 |
|
|
char *p; |
90 |
|
|
|
91 |
|
|
p = strdup(ptr); |
92 |
|
|
if (p == NULL) |
93 |
|
|
err((int)MANDOCLEVEL_SYSERR, NULL); |
94 |
|
|
return p; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
char * |
98 |
|
|
mandoc_strndup(const char *ptr, size_t sz) |
99 |
|
|
{ |
100 |
|
|
char *p; |
101 |
|
|
|
102 |
|
|
p = mandoc_malloc(sz + 1); |
103 |
|
|
memcpy(p, ptr, sz); |
104 |
|
|
p[(int)sz] = '\0'; |
105 |
|
|
return p; |
106 |
|
|
} |