Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ** This file is in the public domain, so clarified as of |
michael@0 | 3 | ** 2006-07-17 by Arthur David Olson. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef lint |
michael@0 | 7 | #ifndef NOID |
michael@0 | 8 | static char elsieid[] = "@(#)ialloc.c 8.30"; |
michael@0 | 9 | #endif /* !defined NOID */ |
michael@0 | 10 | #endif /* !defined lint */ |
michael@0 | 11 | |
michael@0 | 12 | /*LINTLIBRARY*/ |
michael@0 | 13 | |
michael@0 | 14 | #include "private.h" |
michael@0 | 15 | |
michael@0 | 16 | #define nonzero(n) (((n) == 0) ? 1 : (n)) |
michael@0 | 17 | |
michael@0 | 18 | char * |
michael@0 | 19 | imalloc(n) |
michael@0 | 20 | const int n; |
michael@0 | 21 | { |
michael@0 | 22 | return malloc((size_t) nonzero(n)); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | char * |
michael@0 | 26 | icalloc(nelem, elsize) |
michael@0 | 27 | int nelem; |
michael@0 | 28 | int elsize; |
michael@0 | 29 | { |
michael@0 | 30 | if (nelem == 0 || elsize == 0) |
michael@0 | 31 | nelem = elsize = 1; |
michael@0 | 32 | return calloc((size_t) nelem, (size_t) elsize); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | void * |
michael@0 | 36 | irealloc(pointer, size) |
michael@0 | 37 | void * const pointer; |
michael@0 | 38 | const int size; |
michael@0 | 39 | { |
michael@0 | 40 | if (pointer == NULL) |
michael@0 | 41 | return imalloc(size); |
michael@0 | 42 | return realloc((void *) pointer, (size_t) nonzero(size)); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | char * |
michael@0 | 46 | icatalloc(old, new) |
michael@0 | 47 | char * const old; |
michael@0 | 48 | const char * const new; |
michael@0 | 49 | { |
michael@0 | 50 | register char * result; |
michael@0 | 51 | register int oldsize, newsize; |
michael@0 | 52 | |
michael@0 | 53 | newsize = (new == NULL) ? 0 : strlen(new); |
michael@0 | 54 | if (old == NULL) |
michael@0 | 55 | oldsize = 0; |
michael@0 | 56 | else if (newsize == 0) |
michael@0 | 57 | return old; |
michael@0 | 58 | else oldsize = strlen(old); |
michael@0 | 59 | if ((result = irealloc(old, oldsize + newsize + 1)) != NULL) |
michael@0 | 60 | if (new != NULL) |
michael@0 | 61 | (void) strcpy(result + oldsize, new); |
michael@0 | 62 | return result; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | char * |
michael@0 | 66 | icpyalloc(string) |
michael@0 | 67 | const char * const string; |
michael@0 | 68 | { |
michael@0 | 69 | return icatalloc((char *) NULL, string); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | void |
michael@0 | 73 | ifree(p) |
michael@0 | 74 | char * const p; |
michael@0 | 75 | { |
michael@0 | 76 | if (p != NULL) |
michael@0 | 77 | (void) free(p); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | void |
michael@0 | 81 | icfree(p) |
michael@0 | 82 | char * const p; |
michael@0 | 83 | { |
michael@0 | 84 | if (p != NULL) |
michael@0 | 85 | (void) free(p); |
michael@0 | 86 | } |