]> git.saurik.com Git - apple/system_cmds.git/blob - zic.tproj/ialloc.c
490d2faf522f736cf14fe50fbdbf56b1a3beab69
[apple/system_cmds.git] / zic.tproj / ialloc.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #if defined(LIBC_SCCS) && !defined(lint)
25 #if 0
26 static char elsieid[] = "@(#)ialloc.c 8.28";
27 #else
28 static char rcsid[] = "$OpenBSD: ialloc.c,v 1.3 1997/01/14 03:16:45 millert Exp $";
29 #endif
30 #endif /* LIBC_SCCS and not lint */
31
32 /*LINTLIBRARY*/
33
34 #include "private.h"
35
36 #define nonzero(n) (((n) == 0) ? 1 : (n))
37
38 char * icalloc P((int nelem, int elsize));
39 char * icatalloc P((char * old, const char * new));
40 char * icpyalloc P((const char * string));
41 char * imalloc P((int n));
42 void * irealloc P((void * pointer, int size));
43 void ifree P((char * pointer));
44
45 char *
46 imalloc(n)
47 const int n;
48 {
49 return malloc((size_t) nonzero(n));
50 }
51
52 char *
53 icalloc(nelem, elsize)
54 int nelem;
55 int elsize;
56 {
57 if (nelem == 0 || elsize == 0)
58 nelem = elsize = 1;
59 return calloc((size_t) nelem, (size_t) elsize);
60 }
61
62 void *
63 irealloc(pointer, size)
64 void * const pointer;
65 const int size;
66 {
67 if (pointer == NULL)
68 return imalloc(size);
69 return realloc((void *) pointer, (size_t) nonzero(size));
70 }
71
72 char *
73 icatalloc(old, new)
74 char * const old;
75 const char * const new;
76 {
77 register char * result;
78 register int oldsize, newsize;
79
80 newsize = (new == NULL) ? 0 : strlen(new);
81 if (old == NULL)
82 oldsize = 0;
83 else if (newsize == 0)
84 return old;
85 else oldsize = strlen(old);
86 if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
87 if (new != NULL)
88 (void) strcpy(result + oldsize, new);
89 return result;
90 }
91
92 char *
93 icpyalloc(string)
94 const char * const string;
95 {
96 return icatalloc((char *) NULL, string);
97 }
98
99 void
100 ifree(p)
101 char * const p;
102 {
103 if (p != NULL)
104 (void) free(p);
105 }
106
107 void
108 icfree(p)
109 char * const p;
110 {
111 if (p != NULL)
112 (void) free(p);
113 }