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