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.
9 * Revision 4.1 92/08/07 18:29:29 lwall
11 * Revision 4.0.1.1 91/06/07 12:20:35 lwall
12 * patch4: new copyright notice
14 * Revision 4.0 91/03/20 01:58:25 lwall
28 #define MEM_SIZE unsigned int
30 static char nomem[] = "Out of memory!\n";
32 /* paranoid version of malloc */
43 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
46 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
51 fputs(nomem,stdout) FLUSH;
57 /* paranoid version of realloc */
60 saferealloc(where,size)
67 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
70 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
71 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
77 fputs(nomem,stdout) FLUSH;
83 /* safe version of free */
90 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
95 /* safe version of string copy */
103 register char *dest = to;
106 for (len--; len && (*dest++ = *from++); len--) ;
111 /* copy a string up to some (non-backslashed) delimiter, if any */
114 cpytill(to,from,delim)
115 register char *to, *from;
118 for (; *from; from++,to++) {
120 if (from[1] == delim)
122 else if (from[1] == '\\')
125 else if (*from == delim)
136 register char *to, *from;
139 for (; *from; from++,to++) {
142 else if (*from == '$')
144 else if (*from == delim)
152 /* return ptr to little string in big string, NULL if not found */
159 register char *t, *s, *x;
161 for (t = big; *t; t++) {
162 for (x=t,s=little; *s; x++,s++) {
174 /* copy a string to a safe spot */
180 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
182 (void)strcpy(newaddr,str);
186 /* grow a static string to at least a certain length */
189 growstr(strptr,curlen,newlen)
194 if (newlen > *curlen) { /* need more room? */
196 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
198 *strptr = safemalloc((MEM_SIZE)newlen);
204 fatal(pat,a1,a2,a3,a4)
207 fprintf(stderr,pat,a1,a2,a3,a4);
212 warn(pat,a1,a2,a3,a4)
215 fprintf(stderr,pat,a1,a2,a3,a4);
218 static bool firstsetenv = TRUE;
219 extern char **environ;
225 register int i=envix(nam); /* where does it go? */
227 if (!environ[i]) { /* does not exist yet */
228 if (firstsetenv) { /* need we copy environment? */
231 char **tmpenv = (char**) /* point our wand at memory */
232 safemalloc((i+2) * sizeof(char*));
234 char **tmpenv = Null(char **);
238 for (j=0; j<i; j++) /* copy environment */
239 tmpenv[j] = environ[j];
240 environ = tmpenv; /* tell exec where it is now */
244 environ = (char**) saferealloc((char*) environ,
245 (i+2) * sizeof(char*));
246 /* just expand it a bit */
248 environ[i+1] = Nullch; /* make sure it's null terminated */
250 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
251 /* this may or may not be in */
252 /* the old environ structure */
253 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
260 register int i, len = strlen(nam);
262 for (i = 0; environ[i]; i++) {
263 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
264 break; /* strnEQ must come first to avoid */
265 } /* potential SEGV's */