S_del_body is sufficiently small that inlining it is a space win.
[p5sagit/p5-mst-13.2.git] / x2p / util.c
CommitLineData
79072805 1/* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2000, 2001, by Larry Wall and others
a687059c 5 *
d48672a2 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.
8d063cd8 8 *
9 * $Log: util.c,v $
8d063cd8 10 */
11
8d063cd8 12#include "EXTERN.h"
13#include "a2p.h"
14#include "INTERN.h"
15#include "util.h"
16
17c3b450 17#include <stdarg.h>
8d063cd8 18#define FLUSH
8d063cd8 19
20static char nomem[] = "Out of memory!\n";
21
22/* paranoid version of malloc */
23
8d063cd8 24
75f92628 25Malloc_t
e9cb6d14 26safemalloc(MEM_SIZE size)
8d063cd8 27{
6d82b384 28 Malloc_t ptr;
8d063cd8 29
6d82b384 30 /* malloc(0) is NASTY on some systems */
31 ptr = malloc(size ? size : 1);
8d063cd8 32#ifdef DEBUGGING
33 if (debug & 128)
fb73857a 34 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
35 an++,(long)size);
8d063cd8 36#endif
37 if (ptr != Nullch)
38 return ptr;
39 else {
40 fputs(nomem,stdout) FLUSH;
41 exit(1);
42 }
43 /*NOTREACHED*/
2c5424a7 44 return 0;
8d063cd8 45}
46
47/* paranoid version of realloc */
48
75f92628 49Malloc_t
e9cb6d14 50saferealloc(Malloc_t where, MEM_SIZE size)
8d063cd8 51{
6d82b384 52 Malloc_t ptr;
8d063cd8 53
6d82b384 54 /* realloc(0) is NASTY on some systems */
55 ptr = realloc(where, size ? size : 1);
8d063cd8 56#ifdef DEBUGGING
57 if (debug & 128) {
68dc0745 58 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
fb73857a 59 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
8d063cd8 60 }
61#endif
62 if (ptr != Nullch)
63 return ptr;
64 else {
65 fputs(nomem,stdout) FLUSH;
66 exit(1);
67 }
68 /*NOTREACHED*/
2c5424a7 69 return 0;
8d063cd8 70}
71
72/* safe version of free */
73
6d82b384 74Free_t
e9cb6d14 75safefree(Malloc_t where)
8d063cd8 76{
77#ifdef DEBUGGING
78 if (debug & 128)
68dc0745 79 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
8d063cd8 80#endif
81 free(where);
82}
83
84/* safe version of string copy */
85
86char *
f0f333f4 87safecpy(char *to, register char *from, register int len)
8d063cd8 88{
89 register char *dest = to;
90
91 if (from != Nullch)
92 for (len--; len && (*dest++ = *from++); len--) ;
93 *dest = '\0';
94 return to;
95}
96
8d063cd8 97/* copy a string up to some (non-backslashed) delimiter, if any */
98
99char *
f0f333f4 100cpytill(register char *to, register char *from, register int delim)
8d063cd8 101{
102 for (; *from; from++,to++) {
378cc40b 103 if (*from == '\\') {
104 if (from[1] == delim)
105 from++;
106 else if (from[1] == '\\')
107 *to++ = *from++;
108 }
8d063cd8 109 else if (*from == delim)
110 break;
111 *to = *from;
112 }
113 *to = '\0';
114 return from;
115}
116
378cc40b 117
8d063cd8 118char *
f0f333f4 119cpy2(register char *to, register char *from, register int delim)
8d063cd8 120{
121 for (; *from; from++,to++) {
378cc40b 122 if (*from == '\\')
8d063cd8 123 *to++ = *from++;
124 else if (*from == '$')
125 *to++ = '\\';
126 else if (*from == delim)
127 break;
128 *to = *from;
129 }
130 *to = '\0';
131 return from;
132}
133
134/* return ptr to little string in big string, NULL if not found */
135
136char *
f0f333f4 137instr(char *big, char *little)
8d063cd8 138{
139 register char *t, *s, *x;
140
141 for (t = big; *t; t++) {
142 for (x=t,s=little; *s; x++,s++) {
143 if (!*x)
144 return Nullch;
145 if (*s != *x)
146 break;
147 }
148 if (!*s)
149 return t;
150 }
151 return Nullch;
152}
153
154/* copy a string to a safe spot */
155
156char *
f0f333f4 157savestr(char *str)
8d063cd8 158{
f0f333f4 159 register char *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
8d063cd8 160
161 (void)strcpy(newaddr,str);
162 return newaddr;
163}
164
165/* grow a static string to at least a certain length */
166
167void
f0f333f4 168growstr(char **strptr, int *curlen, int newlen)
8d063cd8 169{
170 if (newlen > *curlen) { /* need more room? */
171 if (*curlen)
f0f333f4 172 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
8d063cd8 173 else
f0f333f4 174 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
8d063cd8 175 *curlen = newlen;
176 }
177}
178
e50aee73 179void
b7787f18 180croak(const char *pat,...)
a0d0e21e 181{
17c3b450 182#if defined(HAS_VPRINTF)
55497cff 183 va_list args;
184
185 va_start(args, pat);
186 vfprintf(stderr,pat,args);
304b2fa9 187 va_end(args);
55497cff 188#else
a0d0e21e 189 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 190#endif
a0d0e21e 191 exit(1);
192}
193
e50aee73 194void
b7787f18 195fatal(const char *pat,...)
8d063cd8 196{
17c3b450 197#if defined(HAS_VPRINTF)
55497cff 198 va_list args;
199
200 va_start(args, pat);
201 vfprintf(stderr,pat,args);
304b2fa9 202 va_end(args);
55497cff 203#else
8d063cd8 204 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 205#endif
8d063cd8 206 exit(1);
207}
208
f72d1791 209#if defined(DARWIN)
8f1f23e8 210__private_extern__ /* warn() conflicts with libc */
211#endif
9c8d0b29 212void
b7787f18 213warn(const char *pat,...)
a687059c 214{
17c3b450 215#if defined(HAS_VPRINTF)
55497cff 216 va_list args;
217
218 va_start(args, pat);
219 vfprintf(stderr,pat,args);
304b2fa9 220 va_end(args);
55497cff 221#else
a687059c 222 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 223#endif
a687059c 224}
225