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.
18 static char nomem[] = "Out of memory!\n";
20 /* paranoid version of malloc */
30 ptr = (char *) malloc(size?size:1); /* malloc(0) is NASTY on our system */
33 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
38 fputs(nomem,stdout) FLUSH;
44 /* paranoid version of realloc */
47 saferealloc(where,size)
55 realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
58 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
59 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
65 fputs(nomem,stdout) FLUSH;
71 /* safe version of free */
79 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
84 /* safe version of string copy */
92 register char *dest = to;
95 for (len--; len && (*dest++ = *from++); len--) ;
100 /* copy a string up to some (non-backslashed) delimiter, if any */
103 cpytill(to,from,delim)
104 register char *to, *from;
107 for (; *from; from++,to++) {
109 if (from[1] == delim)
111 else if (from[1] == '\\')
114 else if (*from == delim)
125 register char *to, *from;
128 for (; *from; from++,to++) {
131 else if (*from == '$')
133 else if (*from == delim)
141 /* return ptr to little string in big string, NULL if not found */
148 register char *t, *s, *x;
150 for (t = big; *t; t++) {
151 for (x=t,s=little; *s; x++,s++) {
163 /* copy a string to a safe spot */
169 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
171 (void)strcpy(newaddr,str);
175 /* grow a static string to at least a certain length */
178 growstr(strptr,curlen,newlen)
183 if (newlen > *curlen) { /* need more room? */
185 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
187 *strptr = safemalloc((MEM_SIZE)newlen);
194 croak(pat,a1,a2,a3,a4)
198 fprintf(stderr,pat,a1,a2,a3,a4);
204 fatal(pat,a1,a2,a3,a4)
208 fprintf(stderr,pat,a1,a2,a3,a4);
214 warn(pat,a1,a2,a3,a4)
218 fprintf(stderr,pat,a1,a2,a3,a4);