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