| 1 |
|
| /* util.c |
| 2 |
|
| * |
| 3 |
|
| * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999, |
| 4 |
|
| * 2000, 2001, 2005 by Larry Wall and others |
| 5 |
|
| * |
| 6 |
|
| * You may distribute under the terms of either the GNU General Public |
| 7 |
|
| * License or the Artistic License, as specified in the README file. |
| 8 |
|
| */ |
| 9 |
|
| |
| 10 |
|
| #include "EXTERN.h" |
| 11 |
|
| #include "a2p.h" |
| 12 |
|
| #include "INTERN.h" |
| 13 |
|
| #include "util.h" |
| 14 |
|
| |
| 15 |
|
| #include <stdarg.h> |
| 16 |
|
| #define FLUSH |
| 17 |
|
| |
| 18 |
|
| static const char nomem[] = "Out of memory!\n"; |
| 19 |
|
| |
| 20 |
|
| /* paranoid version of malloc */ |
| 21 |
|
| |
| 22 |
|
| |
| 23 |
|
| Malloc_t |
| 24 |
0 |
| safemalloc(MEM_SIZE size) |
| 25 |
|
| { |
| 26 |
|
| Malloc_t ptr; |
| 27 |
|
| |
| 28 |
|
| /* malloc(0) is NASTY on some systems */ |
| 29 |
0 |
▶ | ptr = malloc(size ? size : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| ptr = malloc(size ? size : 1); |
0 |
0 |
0 |
|
| 30 |
|
| #ifdef DEBUGGING |
| 31 |
|
| if (debug & 128) |
| 32 |
|
| fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr, |
| 33 |
|
| an++,(long)size); |
| 34 |
|
| #endif |
| 35 |
0 |
▶ | if (ptr != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr != NULL) |
0 |
0 |
0 |
|
| 36 |
0 |
| return ptr; |
| 37 |
|
| else { |
| 38 |
0 |
| fputs(nomem,stdout) FLUSH; |
| 39 |
0 |
| exit(1); |
| 40 |
|
| } |
| 41 |
|
| /*NOTREACHED*/ |
| 42 |
|
| return 0; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| /* paranoid version of realloc */ |
| 46 |
|
| |
| 47 |
|
| Malloc_t |
| 48 |
0 |
| saferealloc(Malloc_t where, MEM_SIZE size) |
| 49 |
|
| { |
| 50 |
|
| Malloc_t ptr; |
| 51 |
|
| |
| 52 |
|
| /* realloc(0) is NASTY on some systems */ |
| 53 |
0 |
▶ | ptr = realloc(where, size ? size : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| ptr = realloc(where, size ? size : 1); |
0 |
0 |
0 |
|
| 54 |
|
| #ifdef DEBUGGING |
| 55 |
|
| if (debug & 128) { |
| 56 |
|
| fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++); |
| 57 |
|
| fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size); |
| 58 |
|
| } |
| 59 |
|
| #endif |
| 60 |
0 |
▶ | if (ptr != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr != NULL) |
0 |
0 |
0 |
|
| 61 |
0 |
| return ptr; |
| 62 |
|
| else { |
| 63 |
0 |
| fputs(nomem,stdout) FLUSH; |
| 64 |
0 |
| exit(1); |
| 65 |
|
| } |
| 66 |
|
| /*NOTREACHED*/ |
| 67 |
|
| return 0; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| /* safe version of free */ |
| 71 |
|
| |
| 72 |
|
| Free_t |
| 73 |
0 |
| safefree(Malloc_t where) |
| 74 |
|
| { |
| 75 |
|
| #ifdef DEBUGGING |
| 76 |
|
| if (debug & 128) |
| 77 |
|
| fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++); |
| 78 |
|
| #endif |
| 79 |
0 |
| free(where); |
| 80 |
0 |
| } |
| 81 |
|
| |
| 82 |
|
| /* copy a string up to some (non-backslashed) delimiter, if any */ |
| 83 |
|
| |
| 84 |
|
| char * |
| 85 |
0 |
| cpytill(char *to, char *from, int delim) |
| 86 |
|
| { |
| 87 |
0 |
▶ | for (; *from; from++,to++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; *from; from++,to++) { |
0 |
0 |
0 |
|
| 88 |
0 |
▶ | if (*from == '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*from == '\\') { |
0 |
0 |
0 |
|
| 89 |
0 |
▶ | if (from[1] == delim) |
Branch: 1 branch
| Branch | True | False | Total |
| if (from[1] == delim) |
0 |
0 |
0 |
|
| 90 |
0 |
| from++; |
| 91 |
0 |
▶ | else if (from[1] == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (from[1] == '\\') |
0 |
0 |
0 |
|
| 92 |
0 |
| *to++ = *from++; |
| 93 |
|
| } |
| 94 |
0 |
▶ | else if (*from == delim) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*from == delim) |
0 |
0 |
0 |
|
| 95 |
0 |
| break; |
| 96 |
0 |
| *to = *from; |
| 97 |
|
| } |
| 98 |
0 |
| *to = '\0'; |
| 99 |
0 |
| return from; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| |
| 103 |
|
| char * |
| 104 |
0 |
| cpy2(char *to, char *from, int delim) |
| 105 |
|
| { |
| 106 |
0 |
▶ | for (; *from; from++,to++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; *from; from++,to++) { |
0 |
0 |
0 |
|
| 107 |
0 |
▶ | if (*from == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*from == '\\') |
0 |
0 |
0 |
|
| 108 |
0 |
| *to++ = *from++; |
| 109 |
0 |
▶ | else if (*from == '$') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*from == '$') |
0 |
0 |
0 |
|
| 110 |
0 |
| *to++ = '\\'; |
| 111 |
0 |
▶ | else if (*from == delim) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*from == delim) |
0 |
0 |
0 |
|
| 112 |
0 |
| break; |
| 113 |
0 |
| *to = *from; |
| 114 |
|
| } |
| 115 |
0 |
| *to = '\0'; |
| 116 |
0 |
| return from; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| /* return ptr to little string in big string, NULL if not found */ |
| 120 |
|
| |
| 121 |
|
| char * |
| 122 |
0 |
| instr(char *big, const char *little) |
| 123 |
|
| { |
| 124 |
|
| char *t, *x; |
| 125 |
|
| const char *s; |
| 126 |
|
| |
| 127 |
0 |
▶ | for (t = big; *t; t++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (t = big; *t; t++) { |
0 |
0 |
0 |
|
| 128 |
0 |
▶ | for (x=t,s=little; *s; x++,s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x=t,s=little; *s; x++,s++) { |
0 |
0 |
0 |
|
| 129 |
0 |
▶ | if (!*x) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*x) |
0 |
0 |
0 |
|
| 130 |
0 |
| return NULL; |
| 131 |
0 |
▶ | if (*s != *x) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s != *x) |
0 |
0 |
0 |
|
| 132 |
0 |
| break; |
| 133 |
|
| } |
| 134 |
0 |
▶ | if (!*s) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*s) |
0 |
0 |
0 |
|
| 135 |
0 |
| return t; |
| 136 |
|
| } |
| 137 |
0 |
| return NULL; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| /* copy a string to a safe spot */ |
| 141 |
|
| |
| 142 |
|
| char * |
| 143 |
0 |
| savestr(const char *str) |
| 144 |
|
| { |
| 145 |
0 |
| char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1)); |
| 146 |
|
| |
| 147 |
0 |
| (void)strcpy(newaddr,str); |
| 148 |
0 |
| return newaddr; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| /* grow a static string to at least a certain length */ |
| 152 |
|
| |
| 153 |
|
| void |
| 154 |
0 |
| growstr(char **strptr, size_t *curlen, size_t newlen) |
| 155 |
|
| { |
| 156 |
0 |
▶ | if (newlen > *curlen) { /* need more room? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (newlen > *curlen) { /* need more room? */ |
0 |
0 |
0 |
|
| 157 |
0 |
▶ | if (*curlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*curlen) |
0 |
0 |
0 |
|
| 158 |
0 |
| *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen); |
| 159 |
|
| else |
| 160 |
0 |
| *strptr = (char *) safemalloc((MEM_SIZE)newlen); |
| 161 |
0 |
| *curlen = newlen; |
| 162 |
|
| } |
| 163 |
0 |
| } |
| 164 |
|
| |
| 165 |
|
| void |
| 166 |
0 |
| fatal(const char *pat,...) |
| 167 |
|
| { |
| 168 |
|
| va_list args; |
| 169 |
|
| |
| 170 |
0 |
| va_start(args, pat); |
| 171 |
0 |
| vfprintf(stderr,pat,args); |
| 172 |
0 |
| va_end(args); |
| 173 |
0 |
| exit(1); |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| #if defined(DARWIN) |
| 177 |
|
| __private_extern__ /* warn() conflicts with libc */ |
| 178 |
|
| #endif |
| 179 |
|
| void |
| 180 |
0 |
| warn(const char *pat,...) |
| 181 |
|
| { |
| 182 |
|
| va_list args; |
| 183 |
|
| |
| 184 |
0 |
| va_start(args, pat); |
| 185 |
0 |
| vfprintf(stderr,pat,args); |
| 186 |
0 |
| va_end(args); |
| 187 |
0 |
| } |
| 188 |
|
| |