[fix crash in regexec.c]
[p5sagit/p5-mst-13.2.git] / x2p / util.c
CommitLineData
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
18static char nomem[] = "Out of memory!\n";
19
20/* paranoid version of malloc */
21
8d063cd8 22
23char *
24safemalloc(size)
25MEM_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
46char *
47saferealloc(where,size)
48char *where;
49MEM_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 72void
8d063cd8 73safefree(where)
74char *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
85char *
86safecpy(to,from,len)
87char *to;
88register char *from;
89register 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
101char *
102cpytill(to,from,delim)
103register char *to, *from;
104register 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 122char *
123cpy2(to,from,delim)
124register char *to, *from;
125register 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
142char *
143instr(big, little)
144char *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
164char *
165savestr(str)
166char *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
176void
177growstr(strptr,curlen,newlen)
178char **strptr;
179int *curlen;
180int 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 192int
a0d0e21e 193croak(pat,a1,a2,a3,a4)
194char *pat;
9c8d0b29 195int a1,a2,a3,a4;
a0d0e21e 196{
197 fprintf(stderr,pat,a1,a2,a3,a4);
198 exit(1);
199}
200
201/*VARARGS1*/
9c8d0b29 202int
8d063cd8 203fatal(pat,a1,a2,a3,a4)
204char *pat;
9c8d0b29 205int a1,a2,a3,a4;
8d063cd8 206{
207 fprintf(stderr,pat,a1,a2,a3,a4);
208 exit(1);
209}
210
a687059c 211/*VARARGS1*/
9c8d0b29 212void
a687059c 213warn(pat,a1,a2,a3,a4)
214char *pat;
9c8d0b29 215int a1,a2,a3,a4;
a687059c 216{
217 fprintf(stderr,pat,a1,a2,a3,a4);
218}
219
8d063cd8 220static bool firstsetenv = TRUE;
221extern char **environ;
222
223void
224setenv(nam,val)
225char *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
258int
259envix(nam)
260char *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}