xref: /illumos-gate/usr/src/cmd/zic/ialloc.c (revision d0b93cc368ad7d502aead4f14c835c5648cf73fd)
1 /* static char	elsieid[] = "@(#)ialloc.c	8.29"; */
2 
3 /*LINTLIBRARY*/
4 
5 #include "private.h"
6 
7 #define	nonzero(n)	(((n) == 0) ? 1 : (n))
8 
9 char *
10 imalloc(n)
11 const int	n;
12 {
13 	return (malloc((size_t) nonzero(n)));
14 }
15 
16 void *
17 irealloc(pointer, size)
18 void * const	pointer;
19 const int	size;
20 {
21 	if (pointer == NULL)
22 		return (imalloc(size));
23 	return (realloc((void *) pointer, (size_t) nonzero(size)));
24 }
25 
26 char *
27 icatalloc(old, new)
28 char * const		old;
29 const char * const	new;
30 {
31 	register char *	result;
32 	register int	oldsize, newsize;
33 
34 	newsize = (new == NULL) ? 0 : strlen(new);
35 	if (old == NULL)
36 		oldsize = 0;
37 	else if (newsize == 0)
38 		return (old);
39 	else	oldsize = strlen(old);
40 	if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
41 		if (new != NULL)
42 			(void) strcpy(result + oldsize, new);
43 	return (result);
44 }
45 
46 char *
47 icpyalloc(string)
48 const char * const	string;
49 {
50 	return (icatalloc((char *) NULL, string));
51 }
52 
53 void
54 ifree(p)
55 char * const	p;
56 {
57 	if (p != NULL)
58 		(void) free(p);
59 }
60