This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further ANSI changes now builds and passes (most) tests
[perl5.git] / x2p / util.c
CommitLineData
79072805 1/* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
d48672a2
LW
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
LW
7 *
8 * $Log: util.c,v $
8d063cd8
LW
9 */
10
8d063cd8
LW
11#include "EXTERN.h"
12#include "a2p.h"
13#include "INTERN.h"
14#include "util.h"
15
55497cff
PP
16#ifdef I_STDARG
17# include <stdarg.h>
18#endif
8d063cd8 19#define FLUSH
8d063cd8
LW
20
21static char nomem[] = "Out of memory!\n";
22
23/* paranoid version of malloc */
24
8d063cd8 25
75f92628 26Malloc_t
f0f333f4 27safemalloc(size_t size)
8d063cd8 28{
6d82b384 29 Malloc_t ptr;
8d063cd8 30
6d82b384
CS
31 /* malloc(0) is NASTY on some systems */
32 ptr = malloc(size ? size : 1);
8d063cd8
LW
33#ifdef DEBUGGING
34 if (debug & 128)
fb73857a
PP
35 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
36 an++,(long)size);
8d063cd8
LW
37#endif
38 if (ptr != Nullch)
39 return ptr;
40 else {
41 fputs(nomem,stdout) FLUSH;
42 exit(1);
43 }
44 /*NOTREACHED*/
45}
46
47/* paranoid version of realloc */
48
75f92628 49Malloc_t
f0f333f4 50saferealloc(void *where, size_t size)
8d063cd8 51{
6d82b384 52 Malloc_t ptr;
8d063cd8 53
6d82b384
CS
54 /* realloc(0) is NASTY on some systems */
55 ptr = realloc(where, size ? size : 1);
8d063cd8
LW
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
LW
60 }
61#endif
62 if (ptr != Nullch)
63 return ptr;
64 else {
65 fputs(nomem,stdout) FLUSH;
66 exit(1);
67 }
68 /*NOTREACHED*/
69}
70
71/* safe version of free */
72
6d82b384 73Free_t
f0f333f4 74safefree(void *where)
8d063cd8
LW
75{
76#ifdef DEBUGGING
77 if (debug & 128)
68dc0745 78 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
8d063cd8
LW
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
LW
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
LW
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
LW
100{
101 for (; *from; from++,to++) {
378cc40b
LW
102 if (*from == '\\') {
103 if (from[1] == delim)
104 from++;
105 else if (from[1] == '\\')
106 *to++ = *from++;
107 }
8d063cd8
LW
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
LW
119{
120 for (; *from; from++,to++) {
378cc40b 121 if (*from == '\\')
8d063cd8
LW
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
LW
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
LW
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
LW
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
LW
174 *curlen = newlen;
175 }
176}
177
e50aee73 178void
55497cff
PP
179#if defined(I_STDARG) && defined(HAS_VPRINTF)
180croak(char *pat,...)
181#else /* I_STDARG */
182/*VARARGS1*/
a0d0e21e 183croak(pat,a1,a2,a3,a4)
55497cff
PP
184 char *pat;
185 int a1,a2,a3,a4;
186#endif /* I_STDARG */
a0d0e21e 187{
55497cff
PP
188#if defined(I_STDARG) && defined(HAS_VPRINTF)
189 va_list args;
190
191 va_start(args, pat);
192 vfprintf(stderr,pat,args);
193#else
a0d0e21e 194 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 195#endif
a0d0e21e
LW
196 exit(1);
197}
198
e50aee73 199void
55497cff
PP
200#if defined(I_STDARG) && defined(HAS_VPRINTF)
201fatal(char *pat,...)
202#else /* I_STDARG */
203/*VARARGS1*/
8d063cd8 204fatal(pat,a1,a2,a3,a4)
55497cff
PP
205 char *pat;
206 int a1,a2,a3,a4;
207#endif /* I_STDARG */
8d063cd8 208{
55497cff
PP
209#if defined(I_STDARG) && defined(HAS_VPRINTF)
210 va_list args;
211
212 va_start(args, pat);
213 vfprintf(stderr,pat,args);
214#else
8d063cd8 215 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 216#endif
8d063cd8
LW
217 exit(1);
218}
219
9c8d0b29 220void
55497cff
PP
221#if defined(I_STDARG) && defined(HAS_VPRINTF)
222warn(char *pat,...)
223#else /* I_STDARG */
224/*VARARGS1*/
a687059c 225warn(pat,a1,a2,a3,a4)
55497cff
PP
226 char *pat;
227 int a1,a2,a3,a4;
228#endif /* I_STDARG */
a687059c 229{
55497cff
PP
230#if defined(I_STDARG) && defined(HAS_VPRINTF)
231 va_list args;
232
233 va_start(args, pat);
234 vfprintf(stderr,pat,args);
235#else
a687059c 236 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 237#endif
a687059c
LW
238}
239