c70bab9f07f82c088980bdc2489baa323ff4c9ae
[p5sagit/p5-mst-13.2.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)
28 MEM_SIZE size;
29 {
30     Malloc_t ptr;
31
32     /* malloc(0) is NASTY on some systems */
33     ptr = malloc(size ? size : 1);
34 #ifdef DEBUGGING
35     if (debug & 128)
36         fprintf(stderr,"0x%x: (%05d) malloc %d bytes\n",ptr,an++,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(where,size)
51 Malloc_t where;
52 MEM_SIZE size;
53 {
54     Malloc_t ptr;
55
56     /* realloc(0) is NASTY on some systems */
57     ptr = realloc(where, size ? size : 1);
58 #ifdef DEBUGGING
59     if (debug & 128) {
60         fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
61         fprintf(stderr,"0x%x: (%05d) realloc %d bytes\n",ptr,an++,size);
62     }
63 #endif
64     if (ptr != Nullch)
65         return ptr;
66     else {
67         fputs(nomem,stdout) FLUSH;
68         exit(1);
69     }
70     /*NOTREACHED*/
71 }
72
73 /* safe version of free */
74
75 Free_t
76 safefree(where)
77 Malloc_t where;
78 {
79 #ifdef DEBUGGING
80     if (debug & 128)
81         fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
82 #endif
83     free(where);
84 }
85
86 /* safe version of string copy */
87
88 char *
89 safecpy(to,from,len)
90 char *to;
91 register char *from;
92 register int len;
93 {
94     register char *dest = to;
95
96     if (from != Nullch) 
97         for (len--; len && (*dest++ = *from++); len--) ;
98     *dest = '\0';
99     return to;
100 }
101
102 /* copy a string up to some (non-backslashed) delimiter, if any */
103
104 char *
105 cpytill(to,from,delim)
106 register char *to, *from;
107 register int delim;
108 {
109     for (; *from; from++,to++) {
110         if (*from == '\\') {
111             if (from[1] == delim)
112                 from++;
113             else if (from[1] == '\\')
114                 *to++ = *from++;
115         }
116         else if (*from == delim)
117             break;
118         *to = *from;
119     }
120     *to = '\0';
121     return from;
122 }
123
124
125 char *
126 cpy2(to,from,delim)
127 register char *to, *from;
128 register int delim;
129 {
130     for (; *from; from++,to++) {
131         if (*from == '\\')
132             *to++ = *from++;
133         else if (*from == '$')
134             *to++ = '\\';
135         else if (*from == delim)
136             break;
137         *to = *from;
138     }
139     *to = '\0';
140     return from;
141 }
142
143 /* return ptr to little string in big string, NULL if not found */
144
145 char *
146 instr(big, little)
147 char *big, *little;
148
149 {
150     register char *t, *s, *x;
151
152     for (t = big; *t; t++) {
153         for (x=t,s=little; *s; x++,s++) {
154             if (!*x)
155                 return Nullch;
156             if (*s != *x)
157                 break;
158         }
159         if (!*s)
160             return t;
161     }
162     return Nullch;
163 }
164
165 /* copy a string to a safe spot */
166
167 char *
168 savestr(str)
169 char *str;
170 {
171     register char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
172
173     (void)strcpy(newaddr,str);
174     return newaddr;
175 }
176
177 /* grow a static string to at least a certain length */
178
179 void
180 growstr(strptr,curlen,newlen)
181 char **strptr;
182 int *curlen;
183 int newlen;
184 {
185     if (newlen > *curlen) {             /* need more room? */
186         if (*curlen)
187             *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
188         else
189             *strptr = safemalloc((MEM_SIZE)newlen);
190         *curlen = newlen;
191     }
192 }
193
194 void
195 #if defined(I_STDARG) && defined(HAS_VPRINTF)
196 croak(char *pat,...)
197 #else /* I_STDARG */
198 /*VARARGS1*/
199 croak(pat,a1,a2,a3,a4)
200     char *pat;
201     int a1,a2,a3,a4;
202 #endif /* I_STDARG */
203 {
204 #if defined(I_STDARG) && defined(HAS_VPRINTF)
205     va_list args;
206
207     va_start(args, pat);
208     vfprintf(stderr,pat,args);
209 #else
210     fprintf(stderr,pat,a1,a2,a3,a4);
211 #endif
212     exit(1);
213 }
214
215 void
216 #if defined(I_STDARG) && defined(HAS_VPRINTF)
217 fatal(char *pat,...)
218 #else /* I_STDARG */
219 /*VARARGS1*/
220 fatal(pat,a1,a2,a3,a4)
221     char *pat;
222     int a1,a2,a3,a4;
223 #endif /* I_STDARG */
224 {
225 #if defined(I_STDARG) && defined(HAS_VPRINTF)
226     va_list args;
227
228     va_start(args, pat);
229     vfprintf(stderr,pat,args);
230 #else
231     fprintf(stderr,pat,a1,a2,a3,a4);
232 #endif
233     exit(1);
234 }
235
236 void
237 #if defined(I_STDARG) && defined(HAS_VPRINTF)
238 warn(char *pat,...)
239 #else /* I_STDARG */
240 /*VARARGS1*/
241 warn(pat,a1,a2,a3,a4)
242     char *pat;
243     int a1,a2,a3,a4;
244 #endif /* I_STDARG */
245 {
246 #if defined(I_STDARG) && defined(HAS_VPRINTF)
247     va_list args;
248
249     va_start(args, pat);
250     vfprintf(stderr,pat,args);
251 #else
252     fprintf(stderr,pat,a1,a2,a3,a4);
253 #endif
254 }
255