3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2000, 2001, 2005 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.
18 static const char nomem[] = "Out of memory!\n";
20 /* paranoid version of malloc */
24 safemalloc(MEM_SIZE size)
28 /* malloc(0) is NASTY on some systems */
29 ptr = malloc(size ? size : 1);
32 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
38 fputs(nomem,stdout) FLUSH;
45 /* paranoid version of realloc */
48 saferealloc(Malloc_t where, MEM_SIZE size)
52 /* realloc(0) is NASTY on some systems */
53 ptr = realloc(where, size ? size : 1);
56 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
57 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
63 fputs(nomem,stdout) FLUSH;
70 /* safe version of free */
73 safefree(Malloc_t where)
77 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
82 /* copy a string up to some (non-backslashed) delimiter, if any */
85 cpytill(register char *to, register char *from, register int delim)
87 for (; *from; from++,to++) {
91 else if (from[1] == '\\')
94 else if (*from == delim)
104 cpy2(register char *to, register char *from, register int delim)
106 for (; *from; from++,to++) {
109 else if (*from == '$')
111 else if (*from == delim)
119 /* return ptr to little string in big string, NULL if not found */
122 instr(char *big, const char *little)
124 register char *t, *x;
125 register const char *s;
127 for (t = big; *t; t++) {
128 for (x=t,s=little; *s; x++,s++) {
140 /* copy a string to a safe spot */
143 savestr(const char *str)
145 register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
147 (void)strcpy(newaddr,str);
151 /* grow a static string to at least a certain length */
154 growstr(char **strptr, int *curlen, int newlen)
156 if (newlen > *curlen) { /* need more room? */
158 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
160 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
166 fatal(const char *pat,...)
168 #if defined(HAS_VPRINTF)
172 vfprintf(stderr,pat,args);
175 fprintf(stderr,pat,a1,a2,a3,a4);
181 __private_extern__ /* warn() conflicts with libc */
184 warn(const char *pat,...)
186 #if defined(HAS_VPRINTF)
190 vfprintf(stderr,pat,args);
193 fprintf(stderr,pat,a1,a2,a3,a4);