This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Builds C++ Borland, MSVC++ (Win32) and GCC++ (Solaris)
[perl5.git] / x2p / util.c
1 /* $RCSfile: util.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:29 $
2  *
3  *    Copyright (c) 1991-1997, Larry Wall
4  *
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.
7  *
8  * $Log:        util.c,v $
9  */
10
11 #include "EXTERN.h"
12 #include "a2p.h"
13 #include "INTERN.h"
14 #include "util.h"
15
16 #ifdef I_STDARG
17 #  include <stdarg.h>
18 #endif
19 #define FLUSH
20
21 static char nomem[] = "Out of memory!\n";
22
23 /* paranoid version of malloc */
24
25
26 Malloc_t
27 safemalloc(size_t size)
28 {
29     Malloc_t ptr;
30
31     /* malloc(0) is NASTY on some systems */
32     ptr = malloc(size ? size : 1);
33 #ifdef DEBUGGING
34     if (debug & 128)
35         fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
36                 an++,(long)size);
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
49 Malloc_t
50 saferealloc(void *where, size_t size)
51 {
52     Malloc_t ptr;
53
54     /* realloc(0) is NASTY on some systems */
55     ptr = realloc(where, size ? size : 1);
56 #ifdef DEBUGGING
57     if (debug & 128) {
58         fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
59         fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)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
73 Free_t
74 safefree(void *where)
75 {
76 #ifdef DEBUGGING
77     if (debug & 128)
78         fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
79 #endif
80     free(where);
81 }
82
83 /* safe version of string copy */
84
85 char *
86 safecpy(char *to, register char *from, register int len)
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
96 /* copy a string up to some (non-backslashed) delimiter, if any */
97
98 char *
99 cpytill(register char *to, register char *from, register int delim)
100 {
101     for (; *from; from++,to++) {
102         if (*from == '\\') {
103             if (from[1] == delim)
104                 from++;
105             else if (from[1] == '\\')
106                 *to++ = *from++;
107         }
108         else if (*from == delim)
109             break;
110         *to = *from;
111     }
112     *to = '\0';
113     return from;
114 }
115
116
117 char *
118 cpy2(register char *to, register char *from, register int delim)
119 {
120     for (; *from; from++,to++) {
121         if (*from == '\\')
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
135 char *
136 instr(char *big, char *little)
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
155 char *
156 savestr(char *str)
157 {
158     register char *newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
159
160     (void)strcpy(newaddr,str);
161     return newaddr;
162 }
163
164 /* grow a static string to at least a certain length */
165
166 void
167 growstr(char **strptr, int *curlen, int newlen)
168 {
169     if (newlen > *curlen) {             /* need more room? */
170         if (*curlen)
171             *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
172         else
173             *strptr = (char *) safemalloc((MEM_SIZE)newlen);
174         *curlen = newlen;
175     }
176 }
177
178 void
179 #if defined(I_STDARG) && defined(HAS_VPRINTF)
180 croak(char *pat,...)
181 #else /* I_STDARG */
182 /*VARARGS1*/
183 croak(pat,a1,a2,a3,a4)
184     char *pat;
185     int a1,a2,a3,a4;
186 #endif /* I_STDARG */
187 {
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
194     fprintf(stderr,pat,a1,a2,a3,a4);
195 #endif
196     exit(1);
197 }
198
199 void
200 #if defined(I_STDARG) && defined(HAS_VPRINTF)
201 fatal(char *pat,...)
202 #else /* I_STDARG */
203 /*VARARGS1*/
204 fatal(pat,a1,a2,a3,a4)
205     char *pat;
206     int a1,a2,a3,a4;
207 #endif /* I_STDARG */
208 {
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
215     fprintf(stderr,pat,a1,a2,a3,a4);
216 #endif
217     exit(1);
218 }
219
220 void
221 #if defined(I_STDARG) && defined(HAS_VPRINTF)
222 warn(char *pat,...)
223 #else /* I_STDARG */
224 /*VARARGS1*/
225 warn(pat,a1,a2,a3,a4)
226     char *pat;
227     int a1,a2,a3,a4;
228 #endif /* I_STDARG */
229 {
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
236     fprintf(stderr,pat,a1,a2,a3,a4);
237 #endif
238 }
239