Commit | Line | Data |
---|---|---|
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 | |
18 | static char nomem[] = "Out of memory!\n"; | |
19 | ||
20 | /* paranoid version of malloc */ | |
21 | ||
8d063cd8 | 22 | |
75f92628 | 23 | Malloc_t |
8d063cd8 LW |
24 | safemalloc(size) |
25 | MEM_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 | 46 | Malloc_t |
8d063cd8 LW |
47 | saferealloc(where,size) |
48 | char *where; | |
49 | MEM_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 | 73 | void |
8d063cd8 LW |
74 | safefree(where) |
75 | char *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 | ||
86 | char * | |
87 | safecpy(to,from,len) | |
88 | char *to; | |
89 | register char *from; | |
90 | register 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 | ||
102 | char * | |
103 | cpytill(to,from,delim) | |
104 | register char *to, *from; | |
105 | register 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 |
123 | char * |
124 | cpy2(to,from,delim) | |
125 | register char *to, *from; | |
126 | register 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 | ||
143 | char * | |
144 | instr(big, little) | |
145 | char *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 | ||
165 | char * | |
166 | savestr(str) | |
167 | char *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 | ||
177 | void | |
178 | growstr(strptr,curlen,newlen) | |
179 | char **strptr; | |
180 | int *curlen; | |
181 | int 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 | 193 | void |
a0d0e21e LW |
194 | croak(pat,a1,a2,a3,a4) |
195 | char *pat; | |
9c8d0b29 | 196 | int a1,a2,a3,a4; |
a0d0e21e LW |
197 | { |
198 | fprintf(stderr,pat,a1,a2,a3,a4); | |
199 | exit(1); | |
200 | } | |
201 | ||
202 | /*VARARGS1*/ | |
e50aee73 | 203 | void |
8d063cd8 LW |
204 | fatal(pat,a1,a2,a3,a4) |
205 | char *pat; | |
9c8d0b29 | 206 | int a1,a2,a3,a4; |
8d063cd8 LW |
207 | { |
208 | fprintf(stderr,pat,a1,a2,a3,a4); | |
209 | exit(1); | |
210 | } | |
211 | ||
a687059c | 212 | /*VARARGS1*/ |
9c8d0b29 | 213 | void |
a687059c LW |
214 | warn(pat,a1,a2,a3,a4) |
215 | char *pat; | |
9c8d0b29 | 216 | int a1,a2,a3,a4; |
a687059c LW |
217 | { |
218 | fprintf(stderr,pat,a1,a2,a3,a4); | |
219 | } | |
220 |