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