1 /* $Header: util.c,v 1.0 87/12/18 13:07:34 root Exp $
4 * Revision 1.0 87/12/18 13:07:34 root
18 #define MEM_SIZE unsigned int
20 static char nomem[] = "Out of memory!\n";
22 /* paranoid version of malloc */
33 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
36 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
41 fputs(nomem,stdout) FLUSH;
47 /* paranoid version of realloc */
50 saferealloc(where,size)
57 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
60 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
61 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
67 fputs(nomem,stdout) FLUSH;
73 /* safe version of free */
80 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
85 /* safe version of string copy */
93 register char *dest = to;
96 for (len--; len && (*dest++ = *from++); len--) ;
102 /* safe version of string concatenate, with \n deletion and space padding */
110 register char *dest = to;
112 len--; /* leave room for null */
114 while (len && *dest++) len--;
121 while (len && (*dest++ = *from++)) len--;
124 if (*(dest-1) == '\n')
131 /* copy a string up to some (non-backslashed) delimiter, if any */
134 cpytill(to,from,delim)
135 register char *to, *from;
138 for (; *from; from++,to++) {
139 if (*from == '\\' && from[1] == delim)
141 else if (*from == delim)
151 register char *to, *from;
154 for (; *from; from++,to++) {
155 if (*from == '\\' && from[1] == delim)
157 else if (*from == '$')
159 else if (*from == delim)
167 /* return ptr to little string in big string, NULL if not found */
174 register char *t, *s, *x;
176 for (t = big; *t; t++) {
177 for (x=t,s=little; *s; x++,s++) {
189 /* copy a string to a safe spot */
195 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
197 (void)strcpy(newaddr,str);
201 /* grow a static string to at least a certain length */
204 growstr(strptr,curlen,newlen)
209 if (newlen > *curlen) { /* need more room? */
211 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
213 *strptr = safemalloc((MEM_SIZE)newlen);
219 fatal(pat,a1,a2,a3,a4)
222 fprintf(stderr,pat,a1,a2,a3,a4);
226 static bool firstsetenv = TRUE;
227 extern char **environ;
233 register int i=envix(nam); /* where does it go? */
235 if (!environ[i]) { /* does not exist yet */
236 if (firstsetenv) { /* need we copy environment? */
239 char **tmpenv = (char**) /* point our wand at memory */
240 safemalloc((i+2) * sizeof(char*));
242 char **tmpenv = Null(char **);
246 for (j=0; j<i; j++) /* copy environment */
247 tmpenv[j] = environ[j];
248 environ = tmpenv; /* tell exec where it is now */
252 environ = (char**) saferealloc((char*) environ,
253 (i+2) * sizeof(char*));
254 /* just expand it a bit */
256 environ[i+1] = Nullch; /* make sure it's null terminated */
258 environ[i] = safemalloc(strlen(nam) + strlen(val) + 2);
259 /* this may or may not be in */
260 /* the old environ structure */
261 sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
268 register int i, len = strlen(nam);
270 for (i = 0; environ[i]; i++) {
271 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
272 break; /* strnEQ must come first to avoid */
273 } /* potential SEGV's */