]> git.cameronkatri.com Git - mandoc.git/blob - mandoc_aux.c
To make the code more readable, delete 283 /* FALLTHROUGH */ comments
[mandoc.git] / mandoc_aux.c
1 /* $Id: mandoc_aux.c,v 1.6 2015/10/11 21:12:54 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 "config.h"
19
20 #include <sys/types.h>
21
22 #include <err.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30
31 #if !HAVE_PROGNAME
32 const char *mandoc_progname;
33 #endif
34
35 int
36 mandoc_asprintf(char **dest, const char *fmt, ...)
37 {
38 va_list ap;
39 int ret;
40
41 va_start(ap, fmt);
42 ret = vasprintf(dest, fmt, ap);
43 va_end(ap);
44
45 if (ret == -1)
46 err((int)MANDOCLEVEL_SYSERR, NULL);
47 return ret;
48 }
49
50 void *
51 mandoc_calloc(size_t num, size_t size)
52 {
53 void *ptr;
54
55 ptr = calloc(num, size);
56 if (ptr == NULL)
57 err((int)MANDOCLEVEL_SYSERR, NULL);
58 return ptr;
59 }
60
61 void *
62 mandoc_malloc(size_t size)
63 {
64 void *ptr;
65
66 ptr = malloc(size);
67 if (ptr == NULL)
68 err((int)MANDOCLEVEL_SYSERR, NULL);
69 return ptr;
70 }
71
72 void *
73 mandoc_realloc(void *ptr, size_t size)
74 {
75
76 ptr = realloc(ptr, size);
77 if (ptr == NULL)
78 err((int)MANDOCLEVEL_SYSERR, NULL);
79 return ptr;
80 }
81
82 void *
83 mandoc_reallocarray(void *ptr, size_t num, size_t size)
84 {
85
86 ptr = reallocarray(ptr, num, size);
87 if (ptr == NULL)
88 err((int)MANDOCLEVEL_SYSERR, NULL);
89 return ptr;
90 }
91
92 char *
93 mandoc_strdup(const char *ptr)
94 {
95 char *p;
96
97 p = strdup(ptr);
98 if (ptr == NULL)
99 err((int)MANDOCLEVEL_SYSERR, NULL);
100 return p;
101 }
102
103 char *
104 mandoc_strndup(const char *ptr, size_t sz)
105 {
106 char *p;
107
108 p = mandoc_malloc(sz + 1);
109 memcpy(p, ptr, sz);
110 p[(int)sz] = '\0';
111 return p;
112 }