This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.003_02: [no incremental changelog available]
[perl5.git] / x2p / util.c
CommitLineData
79072805 1/* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
a687059c 2 *
d48672a2 3 * Copyright (c) 1991, 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
16#define FLUSH
8d063cd8
LW
17
18static char nomem[] = "Out of memory!\n";
19
20/* paranoid version of malloc */
21
8d063cd8 22
75f92628 23Malloc_t
8d063cd8
LW
24safemalloc(size)
25MEM_SIZE size;
26{
27 char *ptr;
75f92628 28 Malloc_t malloc();
8d063cd8 29
75f92628 30 ptr = (char *) malloc(size?size:1); /* malloc(0) is NASTY on our system */
8d063cd8
LW
31#ifdef DEBUGGING
32 if (debug & 128)
33 fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,size);
34#endif
35 if (ptr != Nullch)
36 return ptr;
37 else {
38 fputs(nomem,stdout) FLUSH;
39 exit(1);
40 }
41 /*NOTREACHED*/
42}
43
44/* paranoid version of realloc */
45
75f92628 46Malloc_t
8d063cd8
LW
47saferealloc(where,size)
48char *where;
49MEM_SIZE size;
50{
51 char *ptr;
75f92628 52 Malloc_t realloc();
8d063cd8 53
75f92628
AD
54 ptr = (char *)
55 realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
8d063cd8
LW
56#ifdef DEBUGGING
57 if (debug & 128) {
58 fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
59 fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
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
9c8d0b29 73void
8d063cd8
LW
74safefree(where)
75char *where;
76{
77#ifdef DEBUGGING
78 if (debug & 128)
79 fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
80#endif
81 free(where);
82}
83
84/* safe version of string copy */
85
86char *
87safecpy(to,from,len)
88char *to;
89register char *from;
90register int len;
91{
92 register char *dest = to;
93
94 if (from != Nullch)
95 for (len--; len && (*dest++ = *from++); len--) ;
96 *dest = '\0';
97 return to;
98}
99
8d063cd8
LW
100/* copy a string up to some (non-backslashed) delimiter, if any */
101
102char *
103cpytill(to,from,delim)
104register char *to, *from;
105register int delim;
106{
107 for (; *from; from++,to++) {
378cc40b
LW
108 if (*from == '\\') {
109 if (from[1] == delim)
110 from++;
111 else if (from[1] == '\\')
112 *to++ = *from++;
113 }
8d063cd8
LW
114 else if (*from == delim)
115 break;
116 *to = *from;
117 }
118 *to = '\0';
119 return from;
120}
121
378cc40b 122
8d063cd8
LW
123char *
124cpy2(to,from,delim)
125register char *to, *from;
126register int delim;
127{
128 for (; *from; from++,to++) {
378cc40b 129 if (*from == '\\')
8d063cd8
LW
130 *to++ = *from++;
131 else if (*from == '$')
132 *to++ = '\\';
133 else if (*from == delim)
134 break;
135 *to = *from;
136 }
137 *to = '\0';
138 return from;
139}
140
141/* return ptr to little string in big string, NULL if not found */
142
143char *
144instr(big, little)
145char *big, *little;
146
147{
148 register char *t, *s, *x;
149
150 for (t = big; *t; t++) {
151 for (x=t,s=little; *s; x++,s++) {
152 if (!*x)
153 return Nullch;
154 if (*s != *x)
155 break;
156 }
157 if (!*s)
158 return t;
159 }
160 return Nullch;
161}
162
163/* copy a string to a safe spot */
164
165char *
166savestr(str)
167char *str;
168{
169 register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
170
171 (void)strcpy(newaddr,str);
172 return newaddr;
173}
174
175/* grow a static string to at least a certain length */
176
177void
178growstr(strptr,curlen,newlen)
179char **strptr;
180int *curlen;
181int newlen;
182{
183 if (newlen > *curlen) { /* need more room? */
184 if (*curlen)
185 *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
186 else
187 *strptr = safemalloc((MEM_SIZE)newlen);
188 *curlen = newlen;
189 }
190}
191
192/*VARARGS1*/
e50aee73 193void
a0d0e21e
LW
194croak(pat,a1,a2,a3,a4)
195char *pat;
9c8d0b29 196int a1,a2,a3,a4;
a0d0e21e
LW
197{
198 fprintf(stderr,pat,a1,a2,a3,a4);
199 exit(1);
200}
201
202/*VARARGS1*/
e50aee73 203void
8d063cd8
LW
204fatal(pat,a1,a2,a3,a4)
205char *pat;
9c8d0b29 206int a1,a2,a3,a4;
8d063cd8
LW
207{
208 fprintf(stderr,pat,a1,a2,a3,a4);
209 exit(1);
210}
211
a687059c 212/*VARARGS1*/
9c8d0b29 213void
a687059c
LW
214warn(pat,a1,a2,a3,a4)
215char *pat;
9c8d0b29 216int a1,a2,a3,a4;
a687059c
LW
217{
218 fprintf(stderr,pat,a1,a2,a3,a4);
219}
220