Commit | Line | Data |
---|---|---|
79072805 | 1 | /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $ |
a687059c | 2 | * |
9607fc9c | 3 | * Copyright (c) 1991-1997, Larry Wall |
a687059c | 4 | * |
d48672a2 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 LW |
7 | * |
8 | * $Log: util.c,v $ | |
8d063cd8 LW |
9 | */ |
10 | ||
8d063cd8 LW |
11 | #include "EXTERN.h" |
12 | #include "a2p.h" | |
13 | #include "INTERN.h" | |
14 | #include "util.h" | |
15 | ||
17c3b450 | 16 | #include <stdarg.h> |
8d063cd8 | 17 | #define FLUSH |
8d063cd8 LW |
18 | |
19 | static char nomem[] = "Out of memory!\n"; | |
20 | ||
21 | /* paranoid version of malloc */ | |
22 | ||
8d063cd8 | 23 | |
75f92628 | 24 | Malloc_t |
e9cb6d14 | 25 | safemalloc(MEM_SIZE size) |
8d063cd8 | 26 | { |
6d82b384 | 27 | Malloc_t ptr; |
8d063cd8 | 28 | |
6d82b384 CS |
29 | /* malloc(0) is NASTY on some systems */ |
30 | ptr = malloc(size ? size : 1); | |
8d063cd8 LW |
31 | #ifdef DEBUGGING |
32 | if (debug & 128) | |
fb73857a | 33 | fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr, |
34 | an++,(long)size); | |
8d063cd8 LW |
35 | #endif |
36 | if (ptr != Nullch) | |
37 | return ptr; | |
38 | else { | |
39 | fputs(nomem,stdout) FLUSH; | |
40 | exit(1); | |
41 | } | |
42 | /*NOTREACHED*/ | |
2c5424a7 | 43 | return 0; |
8d063cd8 LW |
44 | } |
45 | ||
46 | /* paranoid version of realloc */ | |
47 | ||
75f92628 | 48 | Malloc_t |
e9cb6d14 | 49 | saferealloc(Malloc_t where, MEM_SIZE size) |
8d063cd8 | 50 | { |
6d82b384 | 51 | Malloc_t ptr; |
8d063cd8 | 52 | |
6d82b384 CS |
53 | /* realloc(0) is NASTY on some systems */ |
54 | ptr = realloc(where, size ? size : 1); | |
8d063cd8 LW |
55 | #ifdef DEBUGGING |
56 | if (debug & 128) { | |
68dc0745 | 57 | fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++); |
fb73857a | 58 | fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size); |
8d063cd8 LW |
59 | } |
60 | #endif | |
61 | if (ptr != Nullch) | |
62 | return ptr; | |
63 | else { | |
64 | fputs(nomem,stdout) FLUSH; | |
65 | exit(1); | |
66 | } | |
67 | /*NOTREACHED*/ | |
2c5424a7 | 68 | return 0; |
8d063cd8 LW |
69 | } |
70 | ||
71 | /* safe version of free */ | |
72 | ||
6d82b384 | 73 | Free_t |
e9cb6d14 | 74 | safefree(Malloc_t where) |
8d063cd8 LW |
75 | { |
76 | #ifdef DEBUGGING | |
77 | if (debug & 128) | |
68dc0745 | 78 | fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++); |
8d063cd8 LW |
79 | #endif |
80 | free(where); | |
81 | } | |
82 | ||
83 | /* safe version of string copy */ | |
84 | ||
85 | char * | |
f0f333f4 | 86 | safecpy(char *to, register char *from, register int len) |
8d063cd8 LW |
87 | { |
88 | register char *dest = to; | |
89 | ||
90 | if (from != Nullch) | |
91 | for (len--; len && (*dest++ = *from++); len--) ; | |
92 | *dest = '\0'; | |
93 | return to; | |
94 | } | |
95 | ||
8d063cd8 LW |
96 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
97 | ||
98 | char * | |
f0f333f4 | 99 | cpytill(register char *to, register char *from, register int delim) |
8d063cd8 LW |
100 | { |
101 | for (; *from; from++,to++) { | |
378cc40b LW |
102 | if (*from == '\\') { |
103 | if (from[1] == delim) | |
104 | from++; | |
105 | else if (from[1] == '\\') | |
106 | *to++ = *from++; | |
107 | } | |
8d063cd8 LW |
108 | else if (*from == delim) |
109 | break; | |
110 | *to = *from; | |
111 | } | |
112 | *to = '\0'; | |
113 | return from; | |
114 | } | |
115 | ||
378cc40b | 116 | |
8d063cd8 | 117 | char * |
f0f333f4 | 118 | cpy2(register char *to, register char *from, register int delim) |
8d063cd8 LW |
119 | { |
120 | for (; *from; from++,to++) { | |
378cc40b | 121 | if (*from == '\\') |
8d063cd8 LW |
122 | *to++ = *from++; |
123 | else if (*from == '$') | |
124 | *to++ = '\\'; | |
125 | else if (*from == delim) | |
126 | break; | |
127 | *to = *from; | |
128 | } | |
129 | *to = '\0'; | |
130 | return from; | |
131 | } | |
132 | ||
133 | /* return ptr to little string in big string, NULL if not found */ | |
134 | ||
135 | char * | |
f0f333f4 | 136 | instr(char *big, char *little) |
8d063cd8 LW |
137 | { |
138 | register char *t, *s, *x; | |
139 | ||
140 | for (t = big; *t; t++) { | |
141 | for (x=t,s=little; *s; x++,s++) { | |
142 | if (!*x) | |
143 | return Nullch; | |
144 | if (*s != *x) | |
145 | break; | |
146 | } | |
147 | if (!*s) | |
148 | return t; | |
149 | } | |
150 | return Nullch; | |
151 | } | |
152 | ||
153 | /* copy a string to a safe spot */ | |
154 | ||
155 | char * | |
f0f333f4 | 156 | savestr(char *str) |
8d063cd8 | 157 | { |
f0f333f4 | 158 | register char *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1)); |
8d063cd8 LW |
159 | |
160 | (void)strcpy(newaddr,str); | |
161 | return newaddr; | |
162 | } | |
163 | ||
164 | /* grow a static string to at least a certain length */ | |
165 | ||
166 | void | |
f0f333f4 | 167 | growstr(char **strptr, int *curlen, int newlen) |
8d063cd8 LW |
168 | { |
169 | if (newlen > *curlen) { /* need more room? */ | |
170 | if (*curlen) | |
f0f333f4 | 171 | *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen); |
8d063cd8 | 172 | else |
f0f333f4 | 173 | *strptr = (char *) safemalloc((MEM_SIZE)newlen); |
8d063cd8 LW |
174 | *curlen = newlen; |
175 | } | |
176 | } | |
177 | ||
e50aee73 | 178 | void |
55497cff | 179 | croak(char *pat,...) |
a0d0e21e | 180 | { |
17c3b450 | 181 | #if defined(HAS_VPRINTF) |
55497cff | 182 | va_list args; |
183 | ||
184 | va_start(args, pat); | |
185 | vfprintf(stderr,pat,args); | |
186 | #else | |
a0d0e21e | 187 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff | 188 | #endif |
a0d0e21e LW |
189 | exit(1); |
190 | } | |
191 | ||
e50aee73 | 192 | void |
55497cff | 193 | fatal(char *pat,...) |
8d063cd8 | 194 | { |
17c3b450 | 195 | #if defined(HAS_VPRINTF) |
55497cff | 196 | va_list args; |
197 | ||
198 | va_start(args, pat); | |
199 | vfprintf(stderr,pat,args); | |
200 | #else | |
8d063cd8 | 201 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff | 202 | #endif |
8d063cd8 LW |
203 | exit(1); |
204 | } | |
205 | ||
8f1f23e8 W |
206 | #if defined(__APPLE_CC__) |
207 | __private_extern__ /* warn() conflicts with libc */ | |
208 | #endif | |
9c8d0b29 | 209 | void |
55497cff | 210 | warn(char *pat,...) |
a687059c | 211 | { |
17c3b450 | 212 | #if defined(HAS_VPRINTF) |
55497cff | 213 | va_list args; |
214 | ||
215 | va_start(args, pat); | |
216 | vfprintf(stderr,pat,args); | |
217 | #else | |
a687059c | 218 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff | 219 | #endif |
a687059c LW |
220 | } |
221 |