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>
30 xstrdup(const char *s
)
43 xcalloc(size_t nmemb
, size_t size
)
47 if (size
== 0 || nmemb
== 0)
49 if (SIZE_MAX
/ nmemb
< size
)
50 fatalx("nmemb * size > SIZE_MAX");
51 if ((ptr
= calloc(nmemb
, size
)) == NULL
)
52 fatal("xcalloc failed");
64 if ((ptr
= malloc(size
)) == NULL
)
65 fatal("xmalloc failed");
71 xrealloc(void *oldptr
, size_t nmemb
, size_t size
)
73 size_t newsize
= nmemb
* size
;
78 if (SIZE_MAX
/ nmemb
< size
)
79 fatalx("nmemb * size > SIZE_MAX");
80 if ((newptr
= realloc(oldptr
, newsize
)) == NULL
)
81 fatal("xrealloc failed");
87 xasprintf(char **ret
, const char *fmt
, ...)
93 i
= xvasprintf(ret
, fmt
, ap
);
100 xvasprintf(char **ret
, const char *fmt
, va_list ap
)
104 i
= vasprintf(ret
, fmt
, ap
);
105 if (i
< 0 || *ret
== NULL
)
106 fatal("xvasprintf failed");
112 xsnprintf(char *buf
, size_t len
, const char *fmt
, ...)
118 i
= xvsnprintf(buf
, len
, fmt
, ap
);
125 xvsnprintf(char *buf
, size_t len
, const char *fmt
, va_list ap
)
130 fatalx("len > INT_MAX");
132 i
= vsnprintf(buf
, len
, fmt
, ap
);
134 fatal("vsnprintf failed");