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