1 /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
3 * Copyright (c) 1991-2001, 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.
19 static char nomem[] = "Out of memory!\n";
21 /* paranoid version of malloc */
25 safemalloc(MEM_SIZE size)
29 /* malloc(0) is NASTY on some systems */
30 ptr = malloc(size ? size : 1);
33 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
39 fputs(nomem,stdout) FLUSH;
46 /* paranoid version of realloc */
49 saferealloc(Malloc_t where, MEM_SIZE size)
53 /* realloc(0) is NASTY on some systems */
54 ptr = realloc(where, size ? size : 1);
57 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
58 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
64 fputs(nomem,stdout) FLUSH;
71 /* safe version of free */
74 safefree(Malloc_t where)
78 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
83 /* safe version of string copy */
86 safecpy(char *to, register char *from, register int len)
88 register char *dest = to;
91 for (len--; len && (*dest++ = *from++); len--) ;
96 /* copy a string up to some (non-backslashed) delimiter, if any */
99 cpytill(register char *to, register char *from, register int delim)
101 for (; *from; from++,to++) {
103 if (from[1] == delim)
105 else if (from[1] == '\\')
108 else if (*from == delim)
118 cpy2(register char *to, register char *from, register int delim)
120 for (; *from; from++,to++) {
123 else if (*from == '$')
125 else if (*from == delim)
133 /* return ptr to little string in big string, NULL if not found */
136 instr(char *big, char *little)
138 register char *t, *s, *x;
140 for (t = big; *t; t++) {
141 for (x=t,s=little; *s; x++,s++) {
153 /* copy a string to a safe spot */
158 register char *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
160 (void)strcpy(newaddr,str);
164 /* grow a static string to at least a certain length */
167 growstr(char **strptr, int *curlen, int newlen)
169 if (newlen > *curlen) { /* need more room? */
171 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
173 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
181 #if defined(HAS_VPRINTF)
185 vfprintf(stderr,pat,args);
188 fprintf(stderr,pat,a1,a2,a3,a4);
196 #if defined(HAS_VPRINTF)
200 vfprintf(stderr,pat,args);
203 fprintf(stderr,pat,a1,a2,a3,a4);
208 #if defined(__APPLE_CC__)
209 __private_extern__ /* warn() conflicts with libc */
214 #if defined(HAS_VPRINTF)
218 vfprintf(stderr,pat,args);
221 fprintf(stderr,pat,a1,a2,a3,a4);