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