1 /* $Header: util.c,v 3.0 89/10/18 15:35:35 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 3.0 89/10/18 15:35:35 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--) ;
107 /* safe version of string concatenate, with \n deletion and space padding */
115 register char *dest = to;
117 len--; /* leave room for null */
119 while (len && *dest++) len--;
126 while (len && (*dest++ = *from++)) len--;
129 if (*(dest-1) == '\n')
136 /* copy a string up to some (non-backslashed) delimiter, if any */
139 cpytill(to,from,delim)
140 register char *to, *from;
143 for (; *from; from++,to++) {
145 if (from[1] == delim)
147 else if (from[1] == '\\')
150 else if (*from == delim)
161 register char *to, *from;
164 for (; *from; from++,to++) {
167 else if (*from == '$')
169 else if (*from == delim)
177 /* return ptr to little string in big string, NULL if not found */
184 register char *t, *s, *x;
186 for (t = big; *t; t++) {
187 for (x=t,s=little; *s; x++,s++) {
199 /* copy a string to a safe spot */
205 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
207 (void)strcpy(newaddr,str);
211 /* grow a static string to at least a certain length */
214 growstr(strptr,curlen,newlen)
219 if (newlen > *curlen) { /* need more room? */
221 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
223 *strptr = safemalloc((MEM_SIZE)newlen);
229 fatal(pat,a1,a2,a3,a4)
232 fprintf(stderr,pat,a1,a2,a3,a4);
237 warn(pat,a1,a2,a3,a4)
240 fprintf(stderr,pat,a1,a2,a3,a4);
243 static bool firstsetenv = TRUE;
244 extern char **environ;
250 register int i=envix(nam); /* where does it go? */
252 if (!environ[i]) { /* does not exist yet */
253 if (firstsetenv) { /* need we copy environment? */
256 char **tmpenv = (char**) /* point our wand at memory */
257 safemalloc((i+2) * sizeof(char*));
259 char **tmpenv = Null(char **);
263 for (j=0; j<i; j++) /* copy environment */
264 tmpenv[j] = environ[j];
265 environ = tmpenv; /* tell exec where it is now */
269 environ = (char**) saferealloc((char*) environ,
270 (i+2) * sizeof(char*));
271 /* just expand it a bit */
273 environ[i+1] = Nullch; /* make sure it's null terminated */
275 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
276 /* this may or may not be in */
277 /* the old environ structure */
278 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
285 register int i, len = strlen(nam);
287 for (i = 0; environ[i]; i++) {
288 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
289 break; /* strnEQ must come first to avoid */
290 } /* potential SEGV's */