4 * Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/param.h>
29 xstrdup(const char *s
)
42 xcalloc(size_t nmemb
, size_t size
)
46 if (size
== 0 || nmemb
== 0)
48 if (SIZE_MAX
/ nmemb
< size
)
49 fatalx("nmemb * size > SIZE_MAX");
50 if ((ptr
= calloc(nmemb
, size
)) == NULL
)
51 fatal("xcalloc failed");
63 if ((ptr
= malloc(size
)) == NULL
)
64 fatal("xmalloc failed");
70 xrealloc(void *oldptr
, size_t nmemb
, size_t size
)
72 size_t newsize
= nmemb
* size
;
77 if (SIZE_MAX
/ nmemb
< size
)
78 fatalx("nmemb * size > SIZE_MAX");
79 if ((newptr
= realloc(oldptr
, newsize
)) == NULL
)
80 fatal("xrealloc failed");
86 xasprintf(char **ret
, const char *fmt
, ...)
92 i
= xvasprintf(ret
, fmt
, ap
);
99 xvasprintf(char **ret
, const char *fmt
, va_list ap
)
103 i
= vasprintf(ret
, fmt
, ap
);
104 if (i
< 0 || *ret
== NULL
)
105 fatal("xvasprintf failed");
111 xsnprintf(char *buf
, size_t len
, const char *fmt
, ...)
117 i
= xvsnprintf(buf
, len
, fmt
, ap
);
124 xvsnprintf(char *buf
, size_t len
, const char *fmt
, va_list ap
)
129 fatalx("len > INT_MAX");
131 i
= vsnprintf(buf
, len
, fmt
, ap
);
133 fatal("vsnprintf failed");