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 = (char *) 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)
55 realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
58 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
59 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
65 fputs(nomem,stdout) FLUSH;
71 /* safe version of free */
79 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
84 /* safe version of string copy */
92 register char *dest = to;
95 for (len--; len && (*dest++ = *from++); len--) ;
100 /* copy a string up to some (non-backslashed) delimiter, if any */
103 cpytill(to,from,delim)
104 register char *to, *from;
107 for (; *from; from++,to++) {
109 if (from[1] == delim)
111 else if (from[1] == '\\')
114 else if (*from == delim)
125 register char *to, *from;
128 for (; *from; from++,to++) {
131 else if (*from == '$')
133 else if (*from == delim)
141 /* return ptr to little string in big string, NULL if not found */
148 register char *t, *s, *x;
150 for (t = big; *t; t++) {
151 for (x=t,s=little; *s; x++,s++) {
163 /* copy a string to a safe spot */
169 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
171 (void)strcpy(newaddr,str);
175 /* grow a static string to at least a certain length */
178 growstr(strptr,curlen,newlen)
183 if (newlen > *curlen) { /* need more room? */
185 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
187 *strptr = safemalloc((MEM_SIZE)newlen);
194 croak(pat,a1,a2,a3,a4)
198 fprintf(stderr,pat,a1,a2,a3,a4);
204 fatal(pat,a1,a2,a3,a4)
208 fprintf(stderr,pat,a1,a2,a3,a4);
214 warn(pat,a1,a2,a3,a4)
218 fprintf(stderr,pat,a1,a2,a3,a4);
221 static bool firstsetenv = TRUE;
222 extern char **environ;
228 register int i=envix(nam); /* where does it go? */
230 if (!environ[i]) { /* does not exist yet */
231 if (firstsetenv) { /* need we copy environment? */
234 char **tmpenv = (char**) /* point our wand at memory */
235 safemalloc((i+2) * sizeof(char*));
237 char **tmpenv = Null(char **);
241 for (j=0; j<i; j++) /* copy environment */
242 tmpenv[j] = environ[j];
243 environ = tmpenv; /* tell exec where it is now */
247 environ = (char**) saferealloc((char*) environ,
248 (i+2) * sizeof(char*));
249 /* just expand it a bit */
251 environ[i+1] = Nullch; /* make sure it's null terminated */
253 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
254 /* this may or may not be in */
255 /* the old environ structure */
256 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
263 register int i, len = strlen(nam);
265 for (i = 0; environ[i]; i++) {
266 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
267 break; /* strnEQ must come first to avoid */
268 } /* potential SEGV's */