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.
21 static char nomem[] = "Out of memory!\n";
23 /* paranoid version of malloc */
33 ptr = (char *) 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)
58 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 */
82 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
87 /* safe version of string copy */
95 register char *dest = to;
98 for (len--; len && (*dest++ = *from++); len--) ;
103 /* copy a string up to some (non-backslashed) delimiter, if any */
106 cpytill(to,from,delim)
107 register char *to, *from;
110 for (; *from; from++,to++) {
112 if (from[1] == delim)
114 else if (from[1] == '\\')
117 else if (*from == delim)
128 register char *to, *from;
131 for (; *from; from++,to++) {
134 else if (*from == '$')
136 else if (*from == delim)
144 /* return ptr to little string in big string, NULL if not found */
151 register char *t, *s, *x;
153 for (t = big; *t; t++) {
154 for (x=t,s=little; *s; x++,s++) {
166 /* copy a string to a safe spot */
172 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
174 (void)strcpy(newaddr,str);
178 /* grow a static string to at least a certain length */
181 growstr(strptr,curlen,newlen)
186 if (newlen > *curlen) { /* need more room? */
188 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
190 *strptr = safemalloc((MEM_SIZE)newlen);
196 #if defined(I_STDARG) && defined(HAS_VPRINTF)
200 croak(pat,a1,a2,a3,a4)
203 #endif /* I_STDARG */
205 #if defined(I_STDARG) && defined(HAS_VPRINTF)
209 vfprintf(stderr,pat,args);
211 fprintf(stderr,pat,a1,a2,a3,a4);
217 #if defined(I_STDARG) && defined(HAS_VPRINTF)
221 fatal(pat,a1,a2,a3,a4)
224 #endif /* I_STDARG */
226 #if defined(I_STDARG) && defined(HAS_VPRINTF)
230 vfprintf(stderr,pat,args);
232 fprintf(stderr,pat,a1,a2,a3,a4);
238 #if defined(I_STDARG) && defined(HAS_VPRINTF)
242 warn(pat,a1,a2,a3,a4)
245 #endif /* I_STDARG */
247 #if defined(I_STDARG) && defined(HAS_VPRINTF)
251 vfprintf(stderr,pat,args);
253 fprintf(stderr,pat,a1,a2,a3,a4);