1 /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2000, 2001, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
20 static char nomem[] = "Out of memory!\n";
22 /* paranoid version of malloc */
26 safemalloc(MEM_SIZE size)
30 /* malloc(0) is NASTY on some systems */
31 ptr = malloc(size ? size : 1);
34 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
40 fputs(nomem,stdout) FLUSH;
47 /* paranoid version of realloc */
50 saferealloc(Malloc_t where, MEM_SIZE size)
54 /* realloc(0) is NASTY on some systems */
55 ptr = realloc(where, size ? size : 1);
58 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
59 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
65 fputs(nomem,stdout) FLUSH;
72 /* safe version of free */
75 safefree(Malloc_t where)
79 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
84 /* safe version of string copy */
87 safecpy(char *to, register char *from, register int len)
89 register char *dest = to;
92 for (len--; len && (*dest++ = *from++); len--) ;
97 /* copy a string up to some (non-backslashed) delimiter, if any */
100 cpytill(register char *to, register char *from, register int delim)
102 for (; *from; from++,to++) {
104 if (from[1] == delim)
106 else if (from[1] == '\\')
109 else if (*from == delim)
119 cpy2(register char *to, register char *from, register int delim)
121 for (; *from; from++,to++) {
124 else if (*from == '$')
126 else if (*from == delim)
134 /* return ptr to little string in big string, NULL if not found */
137 instr(char *big, char *little)
139 register char *t, *s, *x;
141 for (t = big; *t; t++) {
142 for (x=t,s=little; *s; x++,s++) {
154 /* copy a string to a safe spot */
159 register char *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
161 (void)strcpy(newaddr,str);
165 /* grow a static string to at least a certain length */
168 growstr(char **strptr, int *curlen, int newlen)
170 if (newlen > *curlen) { /* need more room? */
172 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
174 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
180 croak(const char *pat,...)
182 #if defined(HAS_VPRINTF)
186 vfprintf(stderr,pat,args);
189 fprintf(stderr,pat,a1,a2,a3,a4);
195 fatal(const char *pat,...)
197 #if defined(HAS_VPRINTF)
201 vfprintf(stderr,pat,args);
204 fprintf(stderr,pat,a1,a2,a3,a4);
210 __private_extern__ /* warn() conflicts with libc */
213 warn(const char *pat,...)
215 #if defined(HAS_VPRINTF)
219 vfprintf(stderr,pat,args);
222 fprintf(stderr,pat,a1,a2,a3,a4);