1 /* $Header: util.c,v 4.0 91/03/20 01:58:25 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 4.0 91/03/20 01:58:25 lwall
23 #define MEM_SIZE unsigned int
25 static char nomem[] = "Out of memory!\n";
27 /* paranoid version of malloc */
38 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
41 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
46 fputs(nomem,stdout) FLUSH;
52 /* paranoid version of realloc */
55 saferealloc(where,size)
62 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
65 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
66 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
72 fputs(nomem,stdout) FLUSH;
78 /* safe version of free */
85 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
90 /* safe version of string copy */
98 register char *dest = to;
101 for (len--; len && (*dest++ = *from++); len--) ;
106 /* copy a string up to some (non-backslashed) delimiter, if any */
109 cpytill(to,from,delim)
110 register char *to, *from;
113 for (; *from; from++,to++) {
115 if (from[1] == delim)
117 else if (from[1] == '\\')
120 else if (*from == delim)
131 register char *to, *from;
134 for (; *from; from++,to++) {
137 else if (*from == '$')
139 else if (*from == delim)
147 /* return ptr to little string in big string, NULL if not found */
154 register char *t, *s, *x;
156 for (t = big; *t; t++) {
157 for (x=t,s=little; *s; x++,s++) {
169 /* copy a string to a safe spot */
175 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
177 (void)strcpy(newaddr,str);
181 /* grow a static string to at least a certain length */
184 growstr(strptr,curlen,newlen)
189 if (newlen > *curlen) { /* need more room? */
191 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
193 *strptr = safemalloc((MEM_SIZE)newlen);
199 fatal(pat,a1,a2,a3,a4)
202 fprintf(stderr,pat,a1,a2,a3,a4);
207 warn(pat,a1,a2,a3,a4)
210 fprintf(stderr,pat,a1,a2,a3,a4);
213 static bool firstsetenv = TRUE;
214 extern char **environ;
220 register int i=envix(nam); /* where does it go? */
222 if (!environ[i]) { /* does not exist yet */
223 if (firstsetenv) { /* need we copy environment? */
226 char **tmpenv = (char**) /* point our wand at memory */
227 safemalloc((i+2) * sizeof(char*));
229 char **tmpenv = Null(char **);
233 for (j=0; j<i; j++) /* copy environment */
234 tmpenv[j] = environ[j];
235 environ = tmpenv; /* tell exec where it is now */
239 environ = (char**) saferealloc((char*) environ,
240 (i+2) * sizeof(char*));
241 /* just expand it a bit */
243 environ[i+1] = Nullch; /* make sure it's null terminated */
245 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
246 /* this may or may not be in */
247 /* the old environ structure */
248 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
255 register int i, len = strlen(nam);
257 for (i = 0; environ[i]; i++) {
258 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
259 break; /* strnEQ must come first to avoid */
260 } /* potential SEGV's */