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 */
32 /* malloc(0) is NASTY on some systems */
33 ptr = malloc(size ? size : 1);
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)
56 /* realloc(0) is NASTY on some systems */
57 ptr = realloc(where, size ? size : 1);
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 */
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 #if defined(I_STDARG) && defined(HAS_VPRINTF)
199 croak(pat,a1,a2,a3,a4)
202 #endif /* I_STDARG */
204 #if defined(I_STDARG) && defined(HAS_VPRINTF)
208 vfprintf(stderr,pat,args);
210 fprintf(stderr,pat,a1,a2,a3,a4);
216 #if defined(I_STDARG) && defined(HAS_VPRINTF)
220 fatal(pat,a1,a2,a3,a4)
223 #endif /* I_STDARG */
225 #if defined(I_STDARG) && defined(HAS_VPRINTF)
229 vfprintf(stderr,pat,args);
231 fprintf(stderr,pat,a1,a2,a3,a4);
237 #if defined(I_STDARG) && defined(HAS_VPRINTF)
241 warn(pat,a1,a2,a3,a4)
244 #endif /* I_STDARG */
246 #if defined(I_STDARG) && defined(HAS_VPRINTF)
250 vfprintf(stderr,pat,args);
252 fprintf(stderr,pat,a1,a2,a3,a4);