xref: /dragonfly/usr.sbin/zic/ialloc.c (revision 1ab20d67)
1 #ifndef lint
2 #ifndef NOID
3 static char	elsieid[] = "@(#)ialloc.c	8.29";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
6 
7 /*
8  * @(#)ialloc.c	8.29
9  * $FreeBSD: src/usr.sbin/zic/ialloc.c,v 1.5 1999/08/28 01:21:18 peter Exp $
10  * $DragonFly: src/usr.sbin/zic/ialloc.c,v 1.3 2004/02/29 16:55:28 joerg Exp $
11  */
12 /*LINTLIBRARY*/
13 
14 #include "private.h"
15 
16 #define nonzero(n)	(((n) == 0) ? 1 : (n))
17 
18 char *
19 imalloc(const int n)
20 {
21 	return malloc((size_t) nonzero(n));
22 }
23 
24 char *
25 icalloc(int nelem, int elsize)
26 {
27 	if (nelem == 0 || elsize == 0)
28 		nelem = elsize = 1;
29 	return calloc((size_t) nelem, (size_t) elsize);
30 }
31 
32 void *
33 irealloc(void *const pointer, const int size)
34 {
35 	if (pointer == NULL)
36 		return imalloc(size);
37 	return realloc((void *) pointer, (size_t) nonzero(size));
38 }
39 
40 char *
41 icatalloc(char *const old, const char *new)
42 {
43 	char *result;
44 	int oldsize, newsize;
45 
46 	newsize = (new == NULL) ? 0 : strlen(new);
47 	if (old == NULL)
48 		oldsize = 0;
49 	else if (newsize == 0)
50 		return old;
51 	else	oldsize = strlen(old);
52 	if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
53 		if (new != NULL)
54 			(void) strcpy(result + oldsize, new);
55 	return result;
56 }
57 
58 char *
59 icpyalloc(const char *string)
60 {
61 	return icatalloc((char *) NULL, string);
62 }
63 
64 void
65 ifree(char * const p)
66 {
67 	if (p != NULL)
68 		(void) free(p);
69 }
70 
71 void
72 icfree(char * const p)
73 {
74 	if (p != NULL)
75 		(void) free(p);
76 }
77