1 /* $RCSfile: util.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 12:20:35 $
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.
9 * Revision 4.0.1.1 91/06/07 12:20:35 lwall
10 * patch4: new copyright notice
12 * Revision 4.0 91/03/20 01:58:25 lwall
26 #define MEM_SIZE unsigned int
28 static char nomem[] = "Out of memory!\n";
30 /* paranoid version of malloc */
41 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
44 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
49 fputs(nomem,stdout) FLUSH;
55 /* paranoid version of realloc */
58 saferealloc(where,size)
65 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
68 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
69 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
75 fputs(nomem,stdout) FLUSH;
81 /* safe version of free */
88 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
93 /* safe version of string copy */
101 register char *dest = to;
104 for (len--; len && (*dest++ = *from++); len--) ;
109 /* copy a string up to some (non-backslashed) delimiter, if any */
112 cpytill(to,from,delim)
113 register char *to, *from;
116 for (; *from; from++,to++) {
118 if (from[1] == delim)
120 else if (from[1] == '\\')
123 else if (*from == delim)
134 register char *to, *from;
137 for (; *from; from++,to++) {
140 else if (*from == '$')
142 else if (*from == delim)
150 /* return ptr to little string in big string, NULL if not found */
157 register char *t, *s, *x;
159 for (t = big; *t; t++) {
160 for (x=t,s=little; *s; x++,s++) {
172 /* copy a string to a safe spot */
178 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
180 (void)strcpy(newaddr,str);
184 /* grow a static string to at least a certain length */
187 growstr(strptr,curlen,newlen)
192 if (newlen > *curlen) { /* need more room? */
194 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
196 *strptr = safemalloc((MEM_SIZE)newlen);
202 fatal(pat,a1,a2,a3,a4)
205 fprintf(stderr,pat,a1,a2,a3,a4);
210 warn(pat,a1,a2,a3,a4)
213 fprintf(stderr,pat,a1,a2,a3,a4);
216 static bool firstsetenv = TRUE;
217 extern char **environ;
223 register int i=envix(nam); /* where does it go? */
225 if (!environ[i]) { /* does not exist yet */
226 if (firstsetenv) { /* need we copy environment? */
229 char **tmpenv = (char**) /* point our wand at memory */
230 safemalloc((i+2) * sizeof(char*));
232 char **tmpenv = Null(char **);
236 for (j=0; j<i; j++) /* copy environment */
237 tmpenv[j] = environ[j];
238 environ = tmpenv; /* tell exec where it is now */
242 environ = (char**) saferealloc((char*) environ,
243 (i+2) * sizeof(char*));
244 /* just expand it a bit */
246 environ[i+1] = Nullch; /* make sure it's null terminated */
248 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
249 /* this may or may not be in */
250 /* the old environ structure */
251 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
258 register int i, len = strlen(nam);
260 for (i = 0; environ[i]; i++) {
261 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
262 break; /* strnEQ must come first to avoid */
263 } /* potential SEGV's */