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, char *little)
124 register char *t, *s, *x;
126 for (t = big; *t; t++) {
127 for (x=t,s=little; *s; x++,s++) {
139 /* copy a string to a safe spot */
144 register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
146 (void)strcpy(newaddr,str);
150 /* grow a static string to at least a certain length */
153 growstr(char **strptr, int *curlen, int newlen)
155 if (newlen > *curlen) { /* need more room? */
157 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
159 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
165 fatal(const char *pat,...)
167 #if defined(HAS_VPRINTF)
171 vfprintf(stderr,pat,args);
174 fprintf(stderr,pat,a1,a2,a3,a4);
180 __private_extern__ /* warn() conflicts with libc */
183 warn(const char *pat,...)
185 #if defined(HAS_VPRINTF)
189 vfprintf(stderr,pat,args);
192 fprintf(stderr,pat,a1,a2,a3,a4);