Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 145 lines (114 sloc) 2.7 kB
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
1 /* $Id: xmalloc.c 2553 2011-07-09 09:42:33Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
2
3 /*
4 * Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net>
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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20
21 #include <errno.h>
22 #include <libgen.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "tmux.h"
27
28 char *
29 xstrdup(const char *s)
30 {
31 char *ptr;
32 size_t len;
33
34 len = strlen(s) + 1;
35 ptr = xmalloc(len);
36
37 strlcpy(ptr, s, len);
38 return (ptr);
39 }
40
41 void *
42 xcalloc(size_t nmemb, size_t size)
43 {
44 void *ptr;
45
46 if (size == 0 || nmemb == 0)
47 fatalx("zero size");
48 if (SIZE_MAX / nmemb < size)
49 fatalx("nmemb * size > SIZE_MAX");
50 if ((ptr = calloc(nmemb, size)) == NULL)
51 fatal("xcalloc failed");
52
53 return (ptr);
54 }
55
56 void *
57 xmalloc(size_t size)
58 {
59 void *ptr;
60
61 if (size == 0)
62 fatalx("zero size");
63 if ((ptr = malloc(size)) == NULL)
64 fatal("xmalloc failed");
65
66 return (ptr);
67 }
68
69 void *
70 xrealloc(void *oldptr, size_t nmemb, size_t size)
71 {
72 size_t newsize = nmemb * size;
73 void *newptr;
74
75 if (newsize == 0)
76 fatalx("zero size");
77 if (SIZE_MAX / nmemb < size)
78 fatalx("nmemb * size > SIZE_MAX");
79 if ((newptr = realloc(oldptr, newsize)) == NULL)
80 fatal("xrealloc failed");
81
82 return (newptr);
83 }
84
85 void
86 xfree(void *ptr)
87 {
88 if (ptr == NULL)
89 fatalx("null pointer");
90 free(ptr);
91 }
92
93 int printflike2
94 xasprintf(char **ret, const char *fmt, ...)
95 {
96 va_list ap;
97 int i;
98
99 va_start(ap, fmt);
100 i = xvasprintf(ret, fmt, ap);
101 va_end(ap);
102
103 return (i);
104 }
105
106 int
107 xvasprintf(char **ret, const char *fmt, va_list ap)
108 {
109 int i;
110
111 i = vasprintf(ret, fmt, ap);
112 if (i < 0 || *ret == NULL)
113 fatal("xvasprintf failed");
114
115 return (i);
116 }
117
118 int printflike3
119 xsnprintf(char *buf, size_t len, const char *fmt, ...)
120 {
121 va_list ap;
122 int i;
123
124 va_start(ap, fmt);
125 i = xvsnprintf(buf, len, fmt, ap);
126 va_end(ap);
127
128 return (i);
129 }
130
131 int
132 xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
133 {
134 int i;
135
136 if (len > INT_MAX)
137 fatalx("len > INT_MAX");
138
139 i = vsnprintf(buf, len, fmt, ap);
140 if (i < 0)
141 fatal("vsnprintf failed");
142
143 return (i);
144 }
Something went wrong with that request. Please try again.