Commit | Line | Data |
79072805 |
1 | /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $ |
a687059c |
2 | * |
be3c0a43 |
3 | * Copyright (c) 1991-2002, Larry Wall |
a687059c |
4 | * |
d48672a2 |
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 |
7 | * |
8 | * $Log: util.c,v $ |
8d063cd8 |
9 | */ |
10 | |
8d063cd8 |
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 |
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 |
29 | /* malloc(0) is NASTY on some systems */ |
30 | ptr = malloc(size ? size : 1); |
8d063cd8 |
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 |
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 |
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 |
53 | /* realloc(0) is NASTY on some systems */ |
54 | ptr = realloc(where, size ? size : 1); |
8d063cd8 |
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 |
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 |
69 | } |
70 | |
71 | /* safe version of free */ |
72 | |
6d82b384 |
73 | Free_t |
e9cb6d14 |
74 | safefree(Malloc_t where) |
8d063cd8 |
75 | { |
76 | #ifdef DEBUGGING |
77 | if (debug & 128) |
68dc0745 |
78 | fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++); |
8d063cd8 |
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 |
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 |
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 |
100 | { |
101 | for (; *from; from++,to++) { |
378cc40b |
102 | if (*from == '\\') { |
103 | if (from[1] == delim) |
104 | from++; |
105 | else if (from[1] == '\\') |
106 | *to++ = *from++; |
107 | } |
8d063cd8 |
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 |
119 | { |
120 | for (; *from; from++,to++) { |
378cc40b |
121 | if (*from == '\\') |
8d063cd8 |
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 |
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 |
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 |
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 |
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); |
304b2fa9 |
186 | va_end(args); |
55497cff |
187 | #else |
a0d0e21e |
188 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff |
189 | #endif |
a0d0e21e |
190 | exit(1); |
191 | } |
192 | |
e50aee73 |
193 | void |
55497cff |
194 | fatal(char *pat,...) |
8d063cd8 |
195 | { |
17c3b450 |
196 | #if defined(HAS_VPRINTF) |
55497cff |
197 | va_list args; |
198 | |
199 | va_start(args, pat); |
200 | vfprintf(stderr,pat,args); |
304b2fa9 |
201 | va_end(args); |
55497cff |
202 | #else |
8d063cd8 |
203 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff |
204 | #endif |
8d063cd8 |
205 | exit(1); |
206 | } |
207 | |
8f1f23e8 |
208 | #if defined(__APPLE_CC__) |
209 | __private_extern__ /* warn() conflicts with libc */ |
210 | #endif |
9c8d0b29 |
211 | void |
55497cff |
212 | warn(char *pat,...) |
a687059c |
213 | { |
17c3b450 |
214 | #if defined(HAS_VPRINTF) |
55497cff |
215 | va_list args; |
216 | |
217 | va_start(args, pat); |
218 | vfprintf(stderr,pat,args); |
304b2fa9 |
219 | va_end(args); |
55497cff |
220 | #else |
a687059c |
221 | fprintf(stderr,pat,a1,a2,a3,a4); |
55497cff |
222 | #endif |
a687059c |
223 | } |
224 | |