Commit | Line | Data |
79072805 |
1 | /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $ |
a687059c |
2 | * |
d48672a2 |
3 | * Copyright (c) 1991, 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 | |
16 | #define FLUSH |
8d063cd8 |
17 | |
18 | static char nomem[] = "Out of memory!\n"; |
19 | |
20 | /* paranoid version of malloc */ |
21 | |
8d063cd8 |
22 | |
23 | char * |
24 | safemalloc(size) |
25 | MEM_SIZE size; |
26 | { |
27 | char *ptr; |
28 | char *malloc(); |
29 | |
30 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
31 | #ifdef DEBUGGING |
32 | if (debug & 128) |
33 | fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size); |
34 | #endif |
35 | if (ptr != Nullch) |
36 | return ptr; |
37 | else { |
38 | fputs(nomem,stdout) FLUSH; |
39 | exit(1); |
40 | } |
41 | /*NOTREACHED*/ |
42 | } |
43 | |
44 | /* paranoid version of realloc */ |
45 | |
46 | char * |
47 | saferealloc(where,size) |
48 | char *where; |
49 | MEM_SIZE size; |
50 | { |
51 | char *ptr; |
52 | char *realloc(); |
53 | |
54 | ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ |
55 | #ifdef DEBUGGING |
56 | if (debug & 128) { |
57 | fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++); |
58 | fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size); |
59 | } |
60 | #endif |
61 | if (ptr != Nullch) |
62 | return ptr; |
63 | else { |
64 | fputs(nomem,stdout) FLUSH; |
65 | exit(1); |
66 | } |
67 | /*NOTREACHED*/ |
68 | } |
69 | |
70 | /* safe version of free */ |
71 | |
9c8d0b29 |
72 | void |
8d063cd8 |
73 | safefree(where) |
74 | char *where; |
75 | { |
76 | #ifdef DEBUGGING |
77 | if (debug & 128) |
78 | fprintf(stderr,"0x%x: (%05d) free\n",where,an++); |
79 | #endif |
80 | free(where); |
81 | } |
82 | |
83 | /* safe version of string copy */ |
84 | |
85 | char * |
86 | safecpy(to,from,len) |
87 | char *to; |
88 | register char *from; |
89 | register int len; |
90 | { |
91 | register char *dest = to; |
92 | |
93 | if (from != Nullch) |
94 | for (len--; len && (*dest++ = *from++); len--) ; |
95 | *dest = '\0'; |
96 | return to; |
97 | } |
98 | |
8d063cd8 |
99 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
100 | |
101 | char * |
102 | cpytill(to,from,delim) |
103 | register char *to, *from; |
104 | register int delim; |
105 | { |
106 | for (; *from; from++,to++) { |
378cc40b |
107 | if (*from == '\\') { |
108 | if (from[1] == delim) |
109 | from++; |
110 | else if (from[1] == '\\') |
111 | *to++ = *from++; |
112 | } |
8d063cd8 |
113 | else if (*from == delim) |
114 | break; |
115 | *to = *from; |
116 | } |
117 | *to = '\0'; |
118 | return from; |
119 | } |
120 | |
378cc40b |
121 | |
8d063cd8 |
122 | char * |
123 | cpy2(to,from,delim) |
124 | register char *to, *from; |
125 | register int delim; |
126 | { |
127 | for (; *from; from++,to++) { |
378cc40b |
128 | if (*from == '\\') |
8d063cd8 |
129 | *to++ = *from++; |
130 | else if (*from == '$') |
131 | *to++ = '\\'; |
132 | else if (*from == delim) |
133 | break; |
134 | *to = *from; |
135 | } |
136 | *to = '\0'; |
137 | return from; |
138 | } |
139 | |
140 | /* return ptr to little string in big string, NULL if not found */ |
141 | |
142 | char * |
143 | instr(big, little) |
144 | char *big, *little; |
145 | |
146 | { |
147 | register char *t, *s, *x; |
148 | |
149 | for (t = big; *t; t++) { |
150 | for (x=t,s=little; *s; x++,s++) { |
151 | if (!*x) |
152 | return Nullch; |
153 | if (*s != *x) |
154 | break; |
155 | } |
156 | if (!*s) |
157 | return t; |
158 | } |
159 | return Nullch; |
160 | } |
161 | |
162 | /* copy a string to a safe spot */ |
163 | |
164 | char * |
165 | savestr(str) |
166 | char *str; |
167 | { |
168 | register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1)); |
169 | |
170 | (void)strcpy(newaddr,str); |
171 | return newaddr; |
172 | } |
173 | |
174 | /* grow a static string to at least a certain length */ |
175 | |
176 | void |
177 | growstr(strptr,curlen,newlen) |
178 | char **strptr; |
179 | int *curlen; |
180 | int newlen; |
181 | { |
182 | if (newlen > *curlen) { /* need more room? */ |
183 | if (*curlen) |
184 | *strptr = saferealloc(*strptr,(MEM_SIZE)newlen); |
185 | else |
186 | *strptr = safemalloc((MEM_SIZE)newlen); |
187 | *curlen = newlen; |
188 | } |
189 | } |
190 | |
191 | /*VARARGS1*/ |
9c8d0b29 |
192 | int |
a0d0e21e |
193 | croak(pat,a1,a2,a3,a4) |
194 | char *pat; |
9c8d0b29 |
195 | int a1,a2,a3,a4; |
a0d0e21e |
196 | { |
197 | fprintf(stderr,pat,a1,a2,a3,a4); |
198 | exit(1); |
199 | } |
200 | |
201 | /*VARARGS1*/ |
9c8d0b29 |
202 | int |
8d063cd8 |
203 | fatal(pat,a1,a2,a3,a4) |
204 | char *pat; |
9c8d0b29 |
205 | int a1,a2,a3,a4; |
8d063cd8 |
206 | { |
207 | fprintf(stderr,pat,a1,a2,a3,a4); |
208 | exit(1); |
209 | } |
210 | |
a687059c |
211 | /*VARARGS1*/ |
9c8d0b29 |
212 | void |
a687059c |
213 | warn(pat,a1,a2,a3,a4) |
214 | char *pat; |
9c8d0b29 |
215 | int a1,a2,a3,a4; |
a687059c |
216 | { |
217 | fprintf(stderr,pat,a1,a2,a3,a4); |
218 | } |
219 | |
8d063cd8 |
220 | static bool firstsetenv = TRUE; |
221 | extern char **environ; |
222 | |
223 | void |
224 | setenv(nam,val) |
225 | char *nam, *val; |
226 | { |
227 | register int i=envix(nam); /* where does it go? */ |
228 | |
229 | if (!environ[i]) { /* does not exist yet */ |
230 | if (firstsetenv) { /* need we copy environment? */ |
231 | int j; |
232 | #ifndef lint |
233 | char **tmpenv = (char**) /* point our wand at memory */ |
234 | safemalloc((i+2) * sizeof(char*)); |
235 | #else |
236 | char **tmpenv = Null(char **); |
237 | #endif /* lint */ |
238 | |
239 | firstsetenv = FALSE; |
240 | for (j=0; j<i; j++) /* copy environment */ |
241 | tmpenv[j] = environ[j]; |
242 | environ = tmpenv; /* tell exec where it is now */ |
243 | } |
244 | #ifndef lint |
245 | else |
246 | environ = (char**) saferealloc((char*) environ, |
247 | (i+2) * sizeof(char*)); |
248 | /* just expand it a bit */ |
249 | #endif /* lint */ |
250 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
251 | } |
252 | environ[i] = safemalloc(strlen(nam) + strlen(val) + 2); |
253 | /* this may or may not be in */ |
254 | /* the old environ structure */ |
255 | sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
256 | } |
257 | |
258 | int |
259 | envix(nam) |
260 | char *nam; |
261 | { |
262 | register int i, len = strlen(nam); |
263 | |
264 | for (i = 0; environ[i]; i++) { |
265 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') |
266 | break; /* strnEQ must come first to avoid */ |
267 | } /* potential SEGV's */ |
268 | return i; |
269 | } |