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.
20 #define MEM_SIZE unsigned int
22 static char nomem[] = "Out of memory!\n";
24 /* paranoid version of malloc */
34 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
37 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
42 fputs(nomem,stdout) FLUSH;
48 /* paranoid version of realloc */
51 saferealloc(where,size)
58 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
61 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
62 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
68 fputs(nomem,stdout) FLUSH;
74 /* safe version of free */
81 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
86 /* safe version of string copy */
94 register char *dest = to;
97 for (len--; len && (*dest++ = *from++); len--) ;
102 /* copy a string up to some (non-backslashed) delimiter, if any */
105 cpytill(to,from,delim)
106 register char *to, *from;
109 for (; *from; from++,to++) {
111 if (from[1] == delim)
113 else if (from[1] == '\\')
116 else if (*from == delim)
127 register char *to, *from;
130 for (; *from; from++,to++) {
133 else if (*from == '$')
135 else if (*from == delim)
143 /* return ptr to little string in big string, NULL if not found */
150 register char *t, *s, *x;
152 for (t = big; *t; t++) {
153 for (x=t,s=little; *s; x++,s++) {
165 /* copy a string to a safe spot */
171 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
173 (void)strcpy(newaddr,str);
177 /* grow a static string to at least a certain length */
180 growstr(strptr,curlen,newlen)
185 if (newlen > *curlen) { /* need more room? */
187 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
189 *strptr = safemalloc((MEM_SIZE)newlen);
195 croak(pat,a1,a2,a3,a4)
198 fprintf(stderr,pat,a1,a2,a3,a4);
203 fatal(pat,a1,a2,a3,a4)
206 fprintf(stderr,pat,a1,a2,a3,a4);
211 warn(pat,a1,a2,a3,a4)
214 fprintf(stderr,pat,a1,a2,a3,a4);
217 static bool firstsetenv = TRUE;
218 extern char **environ;
224 register int i=envix(nam); /* where does it go? */
226 if (!environ[i]) { /* does not exist yet */
227 if (firstsetenv) { /* need we copy environment? */
230 char **tmpenv = (char**) /* point our wand at memory */
231 safemalloc((i+2) * sizeof(char*));
233 char **tmpenv = Null(char **);
237 for (j=0; j<i; j++) /* copy environment */
238 tmpenv[j] = environ[j];
239 environ = tmpenv; /* tell exec where it is now */
243 environ = (char**) saferealloc((char*) environ,
244 (i+2) * sizeof(char*));
245 /* just expand it a bit */
247 environ[i+1] = Nullch; /* make sure it's null terminated */
249 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
250 /* this may or may not be in */
251 /* the old environ structure */
252 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
259 register int i, len = strlen(nam);
261 for (i = 0; environ[i]; i++) {
262 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
263 break; /* strnEQ must come first to avoid */
264 } /* potential SEGV's */