1 /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
3 * Copyright (c) 1991, Larry Wall
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.
18 static char nomem[] = "Out of memory!\n";
20 /* paranoid version of malloc */
30 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
33 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
38 fputs(nomem,stdout) FLUSH;
44 /* paranoid version of realloc */
47 saferealloc(where,size)
54 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
57 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
58 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
64 fputs(nomem,stdout) FLUSH;
70 /* safe version of free */
78 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
83 /* safe version of string copy */
91 register char *dest = to;
94 for (len--; len && (*dest++ = *from++); len--) ;
99 /* copy a string up to some (non-backslashed) delimiter, if any */
102 cpytill(to,from,delim)
103 register char *to, *from;
106 for (; *from; from++,to++) {
108 if (from[1] == delim)
110 else if (from[1] == '\\')
113 else if (*from == delim)
124 register char *to, *from;
127 for (; *from; from++,to++) {
130 else if (*from == '$')
132 else if (*from == delim)
140 /* return ptr to little string in big string, NULL if not found */
147 register char *t, *s, *x;
149 for (t = big; *t; t++) {
150 for (x=t,s=little; *s; x++,s++) {
162 /* copy a string to a safe spot */
168 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
170 (void)strcpy(newaddr,str);
174 /* grow a static string to at least a certain length */
177 growstr(strptr,curlen,newlen)
182 if (newlen > *curlen) { /* need more room? */
184 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
186 *strptr = safemalloc((MEM_SIZE)newlen);
193 croak(pat,a1,a2,a3,a4)
197 fprintf(stderr,pat,a1,a2,a3,a4);
203 fatal(pat,a1,a2,a3,a4)
207 fprintf(stderr,pat,a1,a2,a3,a4);
213 warn(pat,a1,a2,a3,a4)
217 fprintf(stderr,pat,a1,a2,a3,a4);
220 static bool firstsetenv = TRUE;
221 extern char **environ;
227 register int i=envix(nam); /* where does it go? */
229 if (!environ[i]) { /* does not exist yet */
230 if (firstsetenv) { /* need we copy environment? */
233 char **tmpenv = (char**) /* point our wand at memory */
234 safemalloc((i+2) * sizeof(char*));
236 char **tmpenv = Null(char **);
240 for (j=0; j<i; j++) /* copy environment */
241 tmpenv[j] = environ[j];
242 environ = tmpenv; /* tell exec where it is now */
246 environ = (char**) saferealloc((char*) environ,
247 (i+2) * sizeof(char*));
248 /* just expand it a bit */
250 environ[i+1] = Nullch; /* make sure it's null terminated */
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 */
262 register int i, len = strlen(nam);
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 */