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