xref: /dragonfly/usr.sbin/zic/ialloc.c (revision 956939d5)
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
4 */
5 
6 /*
7  * @(#)ialloc.c	8.30
8  * $FreeBSD: src/usr.sbin/zic/ialloc.c,v 1.5 1999/08/28 01:21:18 peter Exp $
9  * $DragonFly: src/usr.sbin/zic/ialloc.c,v 1.5 2008/10/19 20:15:58 swildner Exp $
10  */
11 /*LINTLIBRARY*/
12 
13 #include "private.h"
14 
15 #define nonzero(n)	(((n) == 0) ? 1 : (n))
16 
17 char *
18 imalloc(const int n)
19 {
20 	return malloc((size_t) nonzero(n));
21 }
22 
23 char *
24 icalloc(int nelem, int elsize)
25 {
26 	if (nelem == 0 || elsize == 0)
27 		nelem = elsize = 1;
28 	return calloc((size_t) nelem, (size_t) elsize);
29 }
30 
31 void *
32 irealloc(void *const pointer, const int size)
33 {
34 	if (pointer == NULL)
35 		return imalloc(size);
36 	return realloc((void *) pointer, (size_t) nonzero(size));
37 }
38 
39 char *
40 icatalloc(char *const old, const char *new)
41 {
42 	char *result;
43 	int oldsize, newsize;
44 
45 	newsize = (new == NULL) ? 0 : strlen(new);
46 	if (old == NULL)
47 		oldsize = 0;
48 	else if (newsize == 0)
49 		return old;
50 	else	oldsize = strlen(old);
51 	if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
52 		if (new != NULL)
53 			strcpy(result + oldsize, new);
54 	return result;
55 }
56 
57 char *
58 icpyalloc(const char *string)
59 {
60 	return icatalloc(NULL, string);
61 }
62 
63 void
64 ifree(char * const p)
65 {
66 	if (p != NULL)
67 		free(p);
68 }
69 
70 void
71 icfree(char * const p)
72 {
73 	if (p != NULL)
74 		free(p);
75 }
76