Commit | Line | Data |
a0d0e21e |
1 | /* util.c |
a687059c |
2 | * |
a0d0e21e |
3 | * Copyright (c) 1991-1994, Larry Wall |
a687059c |
4 | * |
d48672a2 |
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 |
7 | * |
8d063cd8 |
8 | */ |
a0d0e21e |
9 | |
10 | /* |
11 | * "Very useful, no doubt, that was to Saruman; yet it seems that he was |
12 | * not content." --Gandalf |
13 | */ |
8d063cd8 |
14 | |
8d063cd8 |
15 | #include "EXTERN.h" |
8d063cd8 |
16 | #include "perl.h" |
62b28dd9 |
17 | |
6eb13c3b |
18 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
a687059c |
19 | #include <signal.h> |
62b28dd9 |
20 | #endif |
a687059c |
21 | |
ecfc5424 |
22 | /* Omit this -- it causes too much grief on mixed systems. |
85e6fe83 |
23 | #ifdef I_UNISTD |
8990e307 |
24 | # include <unistd.h> |
25 | #endif |
ecfc5424 |
26 | */ |
8990e307 |
27 | |
a687059c |
28 | #ifdef I_VFORK |
29 | # include <vfork.h> |
30 | #endif |
31 | |
5d94fbed |
32 | #ifdef I_LIMITS /* Needed for cast_xxx() functions below. */ |
33 | # include <limits.h> |
34 | #endif |
35 | |
94b6baf5 |
36 | /* Put this after #includes because fork and vfork prototypes may |
37 | conflict. |
38 | */ |
39 | #ifndef HAS_VFORK |
40 | # define vfork fork |
41 | #endif |
42 | |
fe14fcc3 |
43 | #ifdef I_FCNTL |
44 | # include <fcntl.h> |
45 | #endif |
46 | #ifdef I_SYS_FILE |
47 | # include <sys/file.h> |
48 | #endif |
49 | |
8d063cd8 |
50 | #define FLUSH |
8d063cd8 |
51 | |
a0d0e21e |
52 | #ifdef LEAKTEST |
53 | static void xstat _((void)); |
54 | #endif |
55 | |
de3bb511 |
56 | #ifndef safemalloc |
57 | |
8d063cd8 |
58 | /* paranoid version of malloc */ |
59 | |
a687059c |
60 | /* NOTE: Do not call the next three routines directly. Use the macros |
61 | * in handy.h, so that we can easily redefine everything to do tracking of |
62 | * allocated hunks back to the original New to track down any memory leaks. |
63 | */ |
64 | |
8d063cd8 |
65 | char * |
66 | safemalloc(size) |
62b28dd9 |
67 | #ifdef MSDOS |
68 | unsigned long size; |
69 | #else |
8d063cd8 |
70 | MEM_SIZE size; |
62b28dd9 |
71 | #endif /* MSDOS */ |
8d063cd8 |
72 | { |
2304df62 |
73 | char *ptr; |
62b28dd9 |
74 | #ifdef MSDOS |
75 | if (size > 0xffff) { |
76 | fprintf(stderr, "Allocation too large: %lx\n", size) FLUSH; |
79072805 |
77 | my_exit(1); |
62b28dd9 |
78 | } |
79 | #endif /* MSDOS */ |
34de22dd |
80 | #ifdef DEBUGGING |
81 | if ((long)size < 0) |
463ee0b2 |
82 | croak("panic: malloc"); |
34de22dd |
83 | #endif |
8d063cd8 |
84 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
79072805 |
85 | #if !(defined(I286) || defined(atarist)) |
86 | DEBUG_m(fprintf(stderr,"0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size)); |
87 | #else |
88 | DEBUG_m(fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size)); |
8d063cd8 |
89 | #endif |
90 | if (ptr != Nullch) |
91 | return ptr; |
7c0587c8 |
92 | else if (nomemok) |
93 | return Nullch; |
8d063cd8 |
94 | else { |
79072805 |
95 | fputs(no_mem,stderr) FLUSH; |
96 | my_exit(1); |
8d063cd8 |
97 | } |
98 | /*NOTREACHED*/ |
99 | } |
100 | |
101 | /* paranoid version of realloc */ |
102 | |
103 | char * |
104 | saferealloc(where,size) |
105 | char *where; |
62b28dd9 |
106 | #ifndef MSDOS |
8d063cd8 |
107 | MEM_SIZE size; |
62b28dd9 |
108 | #else |
109 | unsigned long size; |
110 | #endif /* MSDOS */ |
8d063cd8 |
111 | { |
112 | char *ptr; |
ecfc5424 |
113 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) |
8d063cd8 |
114 | char *realloc(); |
ecfc5424 |
115 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 |
116 | |
62b28dd9 |
117 | #ifdef MSDOS |
118 | if (size > 0xffff) { |
119 | fprintf(stderr, "Reallocation too large: %lx\n", size) FLUSH; |
79072805 |
120 | my_exit(1); |
62b28dd9 |
121 | } |
122 | #endif /* MSDOS */ |
378cc40b |
123 | if (!where) |
463ee0b2 |
124 | croak("Null realloc"); |
34de22dd |
125 | #ifdef DEBUGGING |
126 | if ((long)size < 0) |
463ee0b2 |
127 | croak("panic: realloc"); |
34de22dd |
128 | #endif |
8d063cd8 |
129 | ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ |
79072805 |
130 | |
131 | #if !(defined(I286) || defined(atarist)) |
132 | DEBUG_m( { |
8d063cd8 |
133 | fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++); |
7c0587c8 |
134 | fprintf(stderr,"0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); |
79072805 |
135 | } ) |
136 | #else |
137 | DEBUG_m( { |
a687059c |
138 | fprintf(stderr,"0x%lx: (%05d) rfree\n",where,an++); |
7c0587c8 |
139 | fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); |
79072805 |
140 | } ) |
8d063cd8 |
141 | #endif |
79072805 |
142 | |
8d063cd8 |
143 | if (ptr != Nullch) |
144 | return ptr; |
7c0587c8 |
145 | else if (nomemok) |
146 | return Nullch; |
8d063cd8 |
147 | else { |
79072805 |
148 | fputs(no_mem,stderr) FLUSH; |
149 | my_exit(1); |
8d063cd8 |
150 | } |
151 | /*NOTREACHED*/ |
152 | } |
153 | |
154 | /* safe version of free */ |
155 | |
a687059c |
156 | void |
8d063cd8 |
157 | safefree(where) |
158 | char *where; |
159 | { |
79072805 |
160 | #if !(defined(I286) || defined(atarist)) |
161 | DEBUG_m( fprintf(stderr,"0x%x: (%05d) free\n",where,an++)); |
162 | #else |
163 | DEBUG_m( fprintf(stderr,"0x%lx: (%05d) free\n",where,an++)); |
8d063cd8 |
164 | #endif |
378cc40b |
165 | if (where) { |
de3bb511 |
166 | /*SUPPRESS 701*/ |
378cc40b |
167 | free(where); |
168 | } |
8d063cd8 |
169 | } |
170 | |
de3bb511 |
171 | #endif /* !safemalloc */ |
172 | |
a687059c |
173 | #ifdef LEAKTEST |
174 | |
175 | #define ALIGN sizeof(long) |
8d063cd8 |
176 | |
177 | char * |
a687059c |
178 | safexmalloc(x,size) |
79072805 |
179 | I32 x; |
a687059c |
180 | MEM_SIZE size; |
8d063cd8 |
181 | { |
a687059c |
182 | register char *where; |
8d063cd8 |
183 | |
a687059c |
184 | where = safemalloc(size + ALIGN); |
185 | xcount[x]++; |
186 | where[0] = x % 100; |
187 | where[1] = x / 100; |
188 | return where + ALIGN; |
8d063cd8 |
189 | } |
8d063cd8 |
190 | |
191 | char * |
a687059c |
192 | safexrealloc(where,size) |
193 | char *where; |
194 | MEM_SIZE size; |
195 | { |
a0d0e21e |
196 | register char *new = saferealloc(where - ALIGN, size + ALIGN); |
197 | return new + ALIGN; |
a687059c |
198 | } |
199 | |
200 | void |
201 | safexfree(where) |
202 | char *where; |
203 | { |
79072805 |
204 | I32 x; |
a687059c |
205 | |
206 | if (!where) |
207 | return; |
208 | where -= ALIGN; |
209 | x = where[0] + 100 * where[1]; |
210 | xcount[x]--; |
211 | safefree(where); |
212 | } |
213 | |
7c0587c8 |
214 | static void |
a687059c |
215 | xstat() |
8d063cd8 |
216 | { |
79072805 |
217 | register I32 i; |
8d063cd8 |
218 | |
a687059c |
219 | for (i = 0; i < MAXXCOUNT; i++) { |
de3bb511 |
220 | if (xcount[i] > lastxcount[i]) { |
a687059c |
221 | fprintf(stderr,"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]); |
222 | lastxcount[i] = xcount[i]; |
8d063cd8 |
223 | } |
224 | } |
8d063cd8 |
225 | } |
a687059c |
226 | |
227 | #endif /* LEAKTEST */ |
8d063cd8 |
228 | |
229 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
230 | |
231 | char * |
a687059c |
232 | cpytill(to,from,fromend,delim,retlen) |
62b28dd9 |
233 | register char *to; |
234 | register char *from; |
a687059c |
235 | register char *fromend; |
a0d0e21e |
236 | register int delim; |
79072805 |
237 | I32 *retlen; |
8d063cd8 |
238 | { |
a687059c |
239 | char *origto = to; |
240 | |
241 | for (; from < fromend; from++,to++) { |
378cc40b |
242 | if (*from == '\\') { |
243 | if (from[1] == delim) |
244 | from++; |
245 | else if (from[1] == '\\') |
246 | *to++ = *from++; |
247 | } |
8d063cd8 |
248 | else if (*from == delim) |
249 | break; |
250 | *to = *from; |
251 | } |
252 | *to = '\0'; |
a687059c |
253 | *retlen = to - origto; |
8d063cd8 |
254 | return from; |
255 | } |
256 | |
257 | /* return ptr to little string in big string, NULL if not found */ |
378cc40b |
258 | /* This routine was donated by Corey Satten. */ |
8d063cd8 |
259 | |
260 | char * |
261 | instr(big, little) |
378cc40b |
262 | register char *big; |
263 | register char *little; |
264 | { |
265 | register char *s, *x; |
79072805 |
266 | register I32 first; |
378cc40b |
267 | |
a687059c |
268 | if (!little) |
269 | return big; |
270 | first = *little++; |
378cc40b |
271 | if (!first) |
272 | return big; |
273 | while (*big) { |
274 | if (*big++ != first) |
275 | continue; |
276 | for (x=big,s=little; *s; /**/ ) { |
277 | if (!*x) |
278 | return Nullch; |
279 | if (*s++ != *x++) { |
280 | s--; |
281 | break; |
282 | } |
283 | } |
284 | if (!*s) |
285 | return big-1; |
286 | } |
287 | return Nullch; |
288 | } |
8d063cd8 |
289 | |
a687059c |
290 | /* same as instr but allow embedded nulls */ |
291 | |
292 | char * |
293 | ninstr(big, bigend, little, lend) |
294 | register char *big; |
295 | register char *bigend; |
296 | char *little; |
297 | char *lend; |
8d063cd8 |
298 | { |
a687059c |
299 | register char *s, *x; |
79072805 |
300 | register I32 first = *little; |
a687059c |
301 | register char *littleend = lend; |
378cc40b |
302 | |
a0d0e21e |
303 | if (!first && little >= littleend) |
a687059c |
304 | return big; |
de3bb511 |
305 | if (bigend - big < littleend - little) |
306 | return Nullch; |
a687059c |
307 | bigend -= littleend - little++; |
308 | while (big <= bigend) { |
309 | if (*big++ != first) |
310 | continue; |
311 | for (x=big,s=little; s < littleend; /**/ ) { |
312 | if (*s++ != *x++) { |
313 | s--; |
314 | break; |
315 | } |
316 | } |
317 | if (s >= littleend) |
318 | return big-1; |
378cc40b |
319 | } |
a687059c |
320 | return Nullch; |
321 | } |
322 | |
323 | /* reverse of the above--find last substring */ |
324 | |
325 | char * |
326 | rninstr(big, bigend, little, lend) |
327 | register char *big; |
328 | char *bigend; |
329 | char *little; |
330 | char *lend; |
331 | { |
332 | register char *bigbeg; |
333 | register char *s, *x; |
79072805 |
334 | register I32 first = *little; |
a687059c |
335 | register char *littleend = lend; |
336 | |
a0d0e21e |
337 | if (!first && little >= littleend) |
a687059c |
338 | return bigend; |
339 | bigbeg = big; |
340 | big = bigend - (littleend - little++); |
341 | while (big >= bigbeg) { |
342 | if (*big-- != first) |
343 | continue; |
344 | for (x=big+2,s=little; s < littleend; /**/ ) { |
345 | if (*s++ != *x++) { |
346 | s--; |
347 | break; |
348 | } |
349 | } |
350 | if (s >= littleend) |
351 | return big+1; |
378cc40b |
352 | } |
a687059c |
353 | return Nullch; |
378cc40b |
354 | } |
a687059c |
355 | |
378cc40b |
356 | void |
79072805 |
357 | fbm_compile(sv, iflag) |
358 | SV *sv; |
359 | I32 iflag; |
378cc40b |
360 | { |
a687059c |
361 | register unsigned char *s; |
362 | register unsigned char *table; |
79072805 |
363 | register U32 i; |
364 | register U32 len = SvCUR(sv); |
365 | I32 rarest = 0; |
366 | U32 frequency = 256; |
367 | |
748a9306 |
368 | if (len > 255) |
369 | return; /* can't have offsets that big */ |
79072805 |
370 | Sv_Grow(sv,len+258); |
463ee0b2 |
371 | table = (unsigned char*)(SvPVX(sv) + len + 1); |
a687059c |
372 | s = table - 2; |
373 | for (i = 0; i < 256; i++) { |
378cc40b |
374 | table[i] = len; |
375 | } |
376 | i = 0; |
463ee0b2 |
377 | while (s >= (unsigned char*)(SvPVX(sv))) |
a687059c |
378 | { |
379 | if (table[*s] == len) { |
380 | #ifndef pdp11 |
381 | if (iflag) |
382 | table[*s] = table[fold[*s]] = i; |
383 | #else |
384 | if (iflag) { |
79072805 |
385 | I32 j; |
a687059c |
386 | j = fold[*s]; |
387 | table[j] = i; |
388 | table[*s] = i; |
389 | } |
390 | #endif /* pdp11 */ |
391 | else |
392 | table[*s] = i; |
393 | } |
378cc40b |
394 | s--,i++; |
395 | } |
79072805 |
396 | sv_upgrade(sv, SVt_PVBM); |
a0d0e21e |
397 | sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */ |
79072805 |
398 | SvVALID_on(sv); |
378cc40b |
399 | |
463ee0b2 |
400 | s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
a687059c |
401 | if (iflag) { |
79072805 |
402 | register U32 tmp, foldtmp; |
403 | SvCASEFOLD_on(sv); |
a687059c |
404 | for (i = 0; i < len; i++) { |
405 | tmp=freq[s[i]]; |
406 | foldtmp=freq[fold[s[i]]]; |
407 | if (tmp < frequency && foldtmp < frequency) { |
408 | rarest = i; |
409 | /* choose most frequent among the two */ |
410 | frequency = (tmp > foldtmp) ? tmp : foldtmp; |
411 | } |
412 | } |
413 | } |
414 | else { |
415 | for (i = 0; i < len; i++) { |
416 | if (freq[s[i]] < frequency) { |
417 | rarest = i; |
418 | frequency = freq[s[i]]; |
419 | } |
378cc40b |
420 | } |
421 | } |
79072805 |
422 | BmRARE(sv) = s[rarest]; |
423 | BmPREVIOUS(sv) = rarest; |
424 | DEBUG_r(fprintf(stderr,"rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv))); |
378cc40b |
425 | } |
426 | |
378cc40b |
427 | char * |
79072805 |
428 | fbm_instr(big, bigend, littlestr) |
a687059c |
429 | unsigned char *big; |
430 | register unsigned char *bigend; |
79072805 |
431 | SV *littlestr; |
378cc40b |
432 | { |
a687059c |
433 | register unsigned char *s; |
79072805 |
434 | register I32 tmp; |
435 | register I32 littlelen; |
a687059c |
436 | register unsigned char *little; |
437 | register unsigned char *table; |
438 | register unsigned char *olds; |
439 | register unsigned char *oldlittle; |
378cc40b |
440 | |
79072805 |
441 | if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
a0d0e21e |
442 | STRLEN len; |
443 | char *l = SvPV(littlestr,len); |
444 | if (!len) |
d48672a2 |
445 | return (char*)big; |
a0d0e21e |
446 | return ninstr((char*)big,(char*)bigend, l, l + len); |
d48672a2 |
447 | } |
378cc40b |
448 | |
79072805 |
449 | littlelen = SvCUR(littlestr); |
450 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
0f85fab0 |
451 | if (littlelen > bigend - big) |
452 | return Nullch; |
463ee0b2 |
453 | little = (unsigned char*)SvPVX(littlestr); |
79072805 |
454 | if (SvCASEFOLD(littlestr)) { /* oops, fake it */ |
a687059c |
455 | big = bigend - littlelen; /* just start near end */ |
456 | if (bigend[-1] == '\n' && little[littlelen-1] != '\n') |
457 | big--; |
378cc40b |
458 | } |
459 | else { |
a687059c |
460 | s = bigend - littlelen; |
1aef975c |
461 | if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0) |
a687059c |
462 | return (char*)s; /* how sweet it is */ |
34de22dd |
463 | else if (bigend[-1] == '\n' && little[littlelen-1] != '\n' |
464 | && s > big) { |
a687059c |
465 | s--; |
1aef975c |
466 | if (*s == *little && bcmp((char*)s,(char*)little,littlelen)==0) |
a687059c |
467 | return (char*)s; |
468 | } |
469 | return Nullch; |
470 | } |
471 | } |
463ee0b2 |
472 | table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1); |
62b28dd9 |
473 | if (--littlelen >= bigend - big) |
474 | return Nullch; |
475 | s = big + littlelen; |
a687059c |
476 | oldlittle = little = table - 2; |
79072805 |
477 | if (SvCASEFOLD(littlestr)) { /* case insensitive? */ |
20188a90 |
478 | if (s < bigend) { |
a687059c |
479 | top1: |
de3bb511 |
480 | /*SUPPRESS 560*/ |
a687059c |
481 | if (tmp = table[*s]) { |
62b28dd9 |
482 | #ifdef POINTERRIGOR |
483 | if (bigend - s > tmp) { |
484 | s += tmp; |
485 | goto top1; |
486 | } |
487 | #else |
488 | if ((s += tmp) < bigend) |
489 | goto top1; |
490 | #endif |
491 | return Nullch; |
a687059c |
492 | } |
493 | else { |
494 | tmp = littlelen; /* less expensive than calling strncmp() */ |
495 | olds = s; |
496 | while (tmp--) { |
497 | if (*--s == *--little || fold[*s] == *little) |
498 | continue; |
499 | s = olds + 1; /* here we pay the price for failure */ |
500 | little = oldlittle; |
501 | if (s < bigend) /* fake up continue to outer loop */ |
502 | goto top1; |
503 | return Nullch; |
504 | } |
a687059c |
505 | return (char *)s; |
a687059c |
506 | } |
507 | } |
508 | } |
509 | else { |
20188a90 |
510 | if (s < bigend) { |
a687059c |
511 | top2: |
de3bb511 |
512 | /*SUPPRESS 560*/ |
a687059c |
513 | if (tmp = table[*s]) { |
62b28dd9 |
514 | #ifdef POINTERRIGOR |
515 | if (bigend - s > tmp) { |
516 | s += tmp; |
517 | goto top2; |
518 | } |
519 | #else |
520 | if ((s += tmp) < bigend) |
521 | goto top2; |
522 | #endif |
523 | return Nullch; |
a687059c |
524 | } |
525 | else { |
526 | tmp = littlelen; /* less expensive than calling strncmp() */ |
527 | olds = s; |
528 | while (tmp--) { |
529 | if (*--s == *--little) |
530 | continue; |
531 | s = olds + 1; /* here we pay the price for failure */ |
532 | little = oldlittle; |
533 | if (s < bigend) /* fake up continue to outer loop */ |
534 | goto top2; |
535 | return Nullch; |
536 | } |
a687059c |
537 | return (char *)s; |
378cc40b |
538 | } |
378cc40b |
539 | } |
540 | } |
541 | return Nullch; |
542 | } |
543 | |
544 | char * |
545 | screaminstr(bigstr, littlestr) |
79072805 |
546 | SV *bigstr; |
547 | SV *littlestr; |
378cc40b |
548 | { |
a687059c |
549 | register unsigned char *s, *x; |
550 | register unsigned char *big; |
79072805 |
551 | register I32 pos; |
552 | register I32 previous; |
553 | register I32 first; |
a687059c |
554 | register unsigned char *little; |
555 | register unsigned char *bigend; |
556 | register unsigned char *littleend; |
378cc40b |
557 | |
79072805 |
558 | if ((pos = screamfirst[BmRARE(littlestr)]) < 0) |
378cc40b |
559 | return Nullch; |
463ee0b2 |
560 | little = (unsigned char *)(SvPVX(littlestr)); |
79072805 |
561 | littleend = little + SvCUR(littlestr); |
378cc40b |
562 | first = *little++; |
79072805 |
563 | previous = BmPREVIOUS(littlestr); |
463ee0b2 |
564 | big = (unsigned char *)(SvPVX(bigstr)); |
79072805 |
565 | bigend = big + SvCUR(bigstr); |
378cc40b |
566 | while (pos < previous) { |
567 | if (!(pos += screamnext[pos])) |
568 | return Nullch; |
569 | } |
de3bb511 |
570 | #ifdef POINTERRIGOR |
79072805 |
571 | if (SvCASEFOLD(littlestr)) { /* case insignificant? */ |
a687059c |
572 | do { |
988174c1 |
573 | if (big[pos-previous] != first && big[pos-previous] != fold[first]) |
574 | continue; |
de3bb511 |
575 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { |
576 | if (x >= bigend) |
577 | return Nullch; |
578 | if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) { |
579 | s--; |
580 | break; |
581 | } |
582 | } |
583 | if (s == littleend) |
de3bb511 |
584 | return (char *)(big+pos-previous); |
de3bb511 |
585 | } while ( |
de3bb511 |
586 | pos += screamnext[pos] /* does this goof up anywhere? */ |
de3bb511 |
587 | ); |
588 | } |
589 | else { |
590 | do { |
988174c1 |
591 | if (big[pos-previous] != first) |
592 | continue; |
de3bb511 |
593 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { |
594 | if (x >= bigend) |
595 | return Nullch; |
596 | if (*s++ != *x++) { |
597 | s--; |
598 | break; |
599 | } |
600 | } |
601 | if (s == littleend) |
de3bb511 |
602 | return (char *)(big+pos-previous); |
79072805 |
603 | } while ( pos += screamnext[pos] ); |
de3bb511 |
604 | } |
605 | #else /* !POINTERRIGOR */ |
606 | big -= previous; |
79072805 |
607 | if (SvCASEFOLD(littlestr)) { /* case insignificant? */ |
de3bb511 |
608 | do { |
988174c1 |
609 | if (big[pos] != first && big[pos] != fold[first]) |
610 | continue; |
a687059c |
611 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { |
612 | if (x >= bigend) |
613 | return Nullch; |
614 | if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) { |
615 | s--; |
616 | break; |
617 | } |
618 | } |
619 | if (s == littleend) |
a687059c |
620 | return (char *)(big+pos); |
a687059c |
621 | } while ( |
a687059c |
622 | pos += screamnext[pos] /* does this goof up anywhere? */ |
a687059c |
623 | ); |
624 | } |
625 | else { |
626 | do { |
988174c1 |
627 | if (big[pos] != first) |
628 | continue; |
a687059c |
629 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { |
630 | if (x >= bigend) |
631 | return Nullch; |
632 | if (*s++ != *x++) { |
633 | s--; |
634 | break; |
635 | } |
378cc40b |
636 | } |
a687059c |
637 | if (s == littleend) |
a687059c |
638 | return (char *)(big+pos); |
a687059c |
639 | } while ( |
a687059c |
640 | pos += screamnext[pos] |
a687059c |
641 | ); |
642 | } |
de3bb511 |
643 | #endif /* POINTERRIGOR */ |
8d063cd8 |
644 | return Nullch; |
645 | } |
646 | |
79072805 |
647 | I32 |
648 | ibcmp(a,b,len) |
a0d0e21e |
649 | register U8 *a; |
650 | register U8 *b; |
79072805 |
651 | register I32 len; |
652 | { |
653 | while (len--) { |
654 | if (*a == *b) { |
655 | a++,b++; |
656 | continue; |
657 | } |
658 | if (fold[*a++] == *b++) |
659 | continue; |
660 | return 1; |
661 | } |
662 | return 0; |
663 | } |
664 | |
8d063cd8 |
665 | /* copy a string to a safe spot */ |
666 | |
667 | char * |
a0d0e21e |
668 | savepv(sv) |
79072805 |
669 | char *sv; |
8d063cd8 |
670 | { |
a687059c |
671 | register char *newaddr; |
8d063cd8 |
672 | |
79072805 |
673 | New(902,newaddr,strlen(sv)+1,char); |
674 | (void)strcpy(newaddr,sv); |
8d063cd8 |
675 | return newaddr; |
676 | } |
677 | |
a687059c |
678 | /* same thing but with a known length */ |
679 | |
680 | char * |
a0d0e21e |
681 | savepvn(sv, len) |
79072805 |
682 | char *sv; |
683 | register I32 len; |
a687059c |
684 | { |
685 | register char *newaddr; |
686 | |
687 | New(903,newaddr,len+1,char); |
79072805 |
688 | Copy(sv,newaddr,len,char); /* might not be null terminated */ |
a687059c |
689 | newaddr[len] = '\0'; /* is now */ |
690 | return newaddr; |
691 | } |
692 | |
a0d0e21e |
693 | #if !defined(I_STDARG) && !defined(I_VARARGS) |
8d063cd8 |
694 | |
8990e307 |
695 | /* |
696 | * Fallback on the old hackers way of doing varargs |
697 | */ |
8d063cd8 |
698 | |
378cc40b |
699 | /*VARARGS1*/ |
7c0587c8 |
700 | char * |
378cc40b |
701 | mess(pat,a1,a2,a3,a4) |
702 | char *pat; |
a687059c |
703 | long a1, a2, a3, a4; |
378cc40b |
704 | { |
705 | char *s; |
79072805 |
706 | I32 usermess = strEQ(pat,"%s"); |
707 | SV *tmpstr; |
378cc40b |
708 | |
a687059c |
709 | s = buf; |
de3bb511 |
710 | if (usermess) { |
8990e307 |
711 | tmpstr = sv_newmortal(); |
79072805 |
712 | sv_setpv(tmpstr, (char*)a1); |
463ee0b2 |
713 | *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1]; |
de3bb511 |
714 | } |
715 | else { |
716 | (void)sprintf(s,pat,a1,a2,a3,a4); |
717 | s += strlen(s); |
718 | } |
719 | |
378cc40b |
720 | if (s[-1] != '\n') { |
2304df62 |
721 | if (dirty) |
722 | strcpy(s, " during global destruction.\n"); |
723 | else { |
724 | if (curcop->cop_line) { |
725 | (void)sprintf(s," at %s line %ld", |
726 | SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line); |
727 | s += strlen(s); |
728 | } |
a0d0e21e |
729 | if (GvIO(last_in_gv) && |
730 | IoLINES(GvIOp(last_in_gv)) ) { |
2304df62 |
731 | (void)sprintf(s,", <%s> %s %ld", |
732 | last_in_gv == argvgv ? "" : GvENAME(last_in_gv), |
733 | strEQ(rs,"\n") ? "line" : "chunk", |
a0d0e21e |
734 | (long)IoLINES(GvIOp(last_in_gv))); |
2304df62 |
735 | s += strlen(s); |
736 | } |
737 | (void)strcpy(s,".\n"); |
378cc40b |
738 | } |
de3bb511 |
739 | if (usermess) |
79072805 |
740 | sv_catpv(tmpstr,buf+1); |
378cc40b |
741 | } |
de3bb511 |
742 | if (usermess) |
463ee0b2 |
743 | return SvPVX(tmpstr); |
de3bb511 |
744 | else |
745 | return buf; |
378cc40b |
746 | } |
747 | |
8d063cd8 |
748 | /*VARARGS1*/ |
463ee0b2 |
749 | void croak(pat,a1,a2,a3,a4) |
8d063cd8 |
750 | char *pat; |
a687059c |
751 | long a1, a2, a3, a4; |
8d063cd8 |
752 | { |
9f68db38 |
753 | char *tmps; |
de3bb511 |
754 | char *message; |
748a9306 |
755 | HV *stash; |
756 | GV *gv; |
757 | CV *cv; |
8d063cd8 |
758 | |
de3bb511 |
759 | message = mess(pat,a1,a2,a3,a4); |
748a9306 |
760 | if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) { |
761 | dSP; |
762 | |
763 | PUSHMARK(sp); |
764 | EXTEND(sp, 1); |
765 | PUSHs(sv_2mortal(newSVpv(message,0))); |
766 | PUTBACK; |
767 | perl_call_sv((SV*)cv, G_DISCARD); |
768 | } |
a0d0e21e |
769 | if (in_eval) { |
770 | restartop = die_where(message); |
771 | longjmp(top_env, 3); |
772 | } |
de3bb511 |
773 | fputs(message,stderr); |
a687059c |
774 | (void)fflush(stderr); |
8d063cd8 |
775 | if (e_fp) |
a687059c |
776 | (void)UNLINK(e_tmpname); |
748a9306 |
777 | statusvalue = SHIFTSTATUS(statusvalue); |
778 | #ifdef VMS |
779 | my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT); |
780 | #else |
781 | my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255))); |
782 | #endif |
378cc40b |
783 | } |
784 | |
785 | /*VARARGS1*/ |
7c0587c8 |
786 | void warn(pat,a1,a2,a3,a4) |
378cc40b |
787 | char *pat; |
a687059c |
788 | long a1, a2, a3, a4; |
378cc40b |
789 | { |
de3bb511 |
790 | char *message; |
748a9306 |
791 | SV *sv; |
792 | HV *stash; |
793 | GV *gv; |
794 | CV *cv; |
de3bb511 |
795 | |
796 | message = mess(pat,a1,a2,a3,a4); |
748a9306 |
797 | if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) { |
798 | dSP; |
799 | |
800 | PUSHMARK(sp); |
801 | EXTEND(sp, 1); |
802 | PUSHs(sv_2mortal(newSVpv(message,0))); |
803 | PUTBACK; |
804 | perl_call_sv((SV*)cv, G_DISCARD); |
805 | } |
806 | else { |
807 | fputs(message,stderr); |
a687059c |
808 | #ifdef LEAKTEST |
748a9306 |
809 | DEBUG_L(xstat()); |
a687059c |
810 | #endif |
748a9306 |
811 | (void)fflush(stderr); |
812 | } |
8d063cd8 |
813 | } |
8990e307 |
814 | |
a0d0e21e |
815 | #else /* !defined(I_STDARG) && !defined(I_VARARGS) */ |
8990e307 |
816 | |
a0d0e21e |
817 | #ifdef I_STDARG |
8990e307 |
818 | char * |
2304df62 |
819 | mess(char *pat, va_list *args) |
a687059c |
820 | #else |
821 | /*VARARGS0*/ |
de3bb511 |
822 | char * |
8990e307 |
823 | mess(pat, args) |
a687059c |
824 | char *pat; |
2304df62 |
825 | va_list *args; |
8990e307 |
826 | #endif |
827 | { |
a687059c |
828 | char *s; |
79072805 |
829 | SV *tmpstr; |
830 | I32 usermess; |
d48672a2 |
831 | #ifndef HAS_VPRINTF |
85e6fe83 |
832 | #ifdef USE_CHAR_VSPRINTF |
a687059c |
833 | char *vsprintf(); |
834 | #else |
79072805 |
835 | I32 vsprintf(); |
a687059c |
836 | #endif |
d48672a2 |
837 | #endif |
a687059c |
838 | |
de3bb511 |
839 | s = buf; |
840 | usermess = strEQ(pat, "%s"); |
841 | if (usermess) { |
8990e307 |
842 | tmpstr = sv_newmortal(); |
2304df62 |
843 | sv_setpv(tmpstr, va_arg(*args, char *)); |
463ee0b2 |
844 | *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1]; |
de3bb511 |
845 | } |
846 | else { |
2304df62 |
847 | (void) vsprintf(s,pat,*args); |
de3bb511 |
848 | s += strlen(s); |
849 | } |
2304df62 |
850 | va_end(*args); |
a687059c |
851 | |
a687059c |
852 | if (s[-1] != '\n') { |
2304df62 |
853 | if (dirty) |
854 | strcpy(s, " during global destruction.\n"); |
855 | else { |
856 | if (curcop->cop_line) { |
857 | (void)sprintf(s," at %s line %ld", |
858 | SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line); |
859 | s += strlen(s); |
860 | } |
a0d0e21e |
861 | if (GvIO(last_in_gv) && |
862 | IoLINES(GvIOp(last_in_gv)) ) { |
2304df62 |
863 | (void)sprintf(s,", <%s> %s %ld", |
864 | last_in_gv == argvgv ? "" : GvNAME(last_in_gv), |
865 | strEQ(rs,"\n") ? "line" : "chunk", |
a0d0e21e |
866 | (long)IoLINES(GvIOp(last_in_gv))); |
2304df62 |
867 | s += strlen(s); |
868 | } |
869 | (void)strcpy(s,".\n"); |
a687059c |
870 | } |
de3bb511 |
871 | if (usermess) |
79072805 |
872 | sv_catpv(tmpstr,buf+1); |
a687059c |
873 | } |
de3bb511 |
874 | |
875 | if (usermess) |
463ee0b2 |
876 | return SvPVX(tmpstr); |
de3bb511 |
877 | else |
878 | return buf; |
a687059c |
879 | } |
880 | |
ecfc5424 |
881 | #ifdef I_STDARG |
79072805 |
882 | void |
8990e307 |
883 | croak(char* pat, ...) |
463ee0b2 |
884 | #else |
8990e307 |
885 | /*VARARGS0*/ |
886 | void |
887 | croak(pat, va_alist) |
888 | char *pat; |
889 | va_dcl |
463ee0b2 |
890 | #endif |
a687059c |
891 | { |
892 | va_list args; |
de3bb511 |
893 | char *message; |
748a9306 |
894 | HV *stash; |
895 | GV *gv; |
896 | CV *cv; |
a687059c |
897 | |
a0d0e21e |
898 | #ifdef I_STDARG |
8990e307 |
899 | va_start(args, pat); |
900 | #else |
a687059c |
901 | va_start(args); |
8990e307 |
902 | #endif |
2304df62 |
903 | message = mess(pat, &args); |
a687059c |
904 | va_end(args); |
748a9306 |
905 | if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) { |
906 | dSP; |
907 | |
908 | PUSHMARK(sp); |
909 | EXTEND(sp, 1); |
910 | PUSHs(sv_2mortal(newSVpv(message,0))); |
911 | PUTBACK; |
912 | perl_call_sv((SV*)cv, G_DISCARD); |
913 | } |
a0d0e21e |
914 | if (in_eval) { |
915 | restartop = die_where(message); |
79072805 |
916 | longjmp(top_env, 3); |
a0d0e21e |
917 | } |
de3bb511 |
918 | fputs(message,stderr); |
a687059c |
919 | (void)fflush(stderr); |
920 | if (e_fp) |
921 | (void)UNLINK(e_tmpname); |
748a9306 |
922 | statusvalue = SHIFTSTATUS(statusvalue); |
923 | #ifdef VMS |
924 | my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44))); |
925 | #else |
926 | my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255))); |
927 | #endif |
a687059c |
928 | } |
929 | |
8990e307 |
930 | void |
ecfc5424 |
931 | #ifdef I_STDARG |
8990e307 |
932 | warn(char* pat,...) |
463ee0b2 |
933 | #else |
8990e307 |
934 | /*VARARGS0*/ |
935 | warn(pat,va_alist) |
936 | char *pat; |
937 | va_dcl |
463ee0b2 |
938 | #endif |
a687059c |
939 | { |
940 | va_list args; |
de3bb511 |
941 | char *message; |
748a9306 |
942 | HV *stash; |
943 | GV *gv; |
944 | CV *cv; |
a687059c |
945 | |
a0d0e21e |
946 | #ifdef I_STDARG |
8990e307 |
947 | va_start(args, pat); |
948 | #else |
a687059c |
949 | va_start(args); |
8990e307 |
950 | #endif |
2304df62 |
951 | message = mess(pat, &args); |
a687059c |
952 | va_end(args); |
953 | |
748a9306 |
954 | if (warnhook && (cv = sv_2cv(warnhook, &stash, &gv, 0)) && !CvDEPTH(cv)) { |
955 | dSP; |
956 | |
957 | PUSHMARK(sp); |
958 | EXTEND(sp, 1); |
959 | PUSHs(sv_2mortal(newSVpv(message,0))); |
960 | PUTBACK; |
961 | perl_call_sv((SV*)cv, G_DISCARD); |
962 | } |
963 | else { |
964 | fputs(message,stderr); |
a687059c |
965 | #ifdef LEAKTEST |
748a9306 |
966 | DEBUG_L(xstat()); |
a687059c |
967 | #endif |
748a9306 |
968 | (void)fflush(stderr); |
969 | } |
a687059c |
970 | } |
a0d0e21e |
971 | #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */ |
8d063cd8 |
972 | |
a0d0e21e |
973 | #ifndef VMS /* VMS' my_setenv() is in VMS.c */ |
8d063cd8 |
974 | void |
7c0587c8 |
975 | my_setenv(nam,val) |
8d063cd8 |
976 | char *nam, *val; |
977 | { |
79072805 |
978 | register I32 i=setenv_getix(nam); /* where does it go? */ |
8d063cd8 |
979 | |
fe14fcc3 |
980 | if (environ == origenviron) { /* need we copy environment? */ |
79072805 |
981 | I32 j; |
982 | I32 max; |
fe14fcc3 |
983 | char **tmpenv; |
984 | |
de3bb511 |
985 | /*SUPPRESS 530*/ |
fe14fcc3 |
986 | for (max = i; environ[max]; max++) ; |
987 | New(901,tmpenv, max+2, char*); |
988 | for (j=0; j<max; j++) /* copy environment */ |
a0d0e21e |
989 | tmpenv[j] = savepv(environ[j]); |
fe14fcc3 |
990 | tmpenv[max] = Nullch; |
991 | environ = tmpenv; /* tell exec where it is now */ |
992 | } |
a687059c |
993 | if (!val) { |
994 | while (environ[i]) { |
995 | environ[i] = environ[i+1]; |
996 | i++; |
997 | } |
998 | return; |
999 | } |
8d063cd8 |
1000 | if (!environ[i]) { /* does not exist yet */ |
fe14fcc3 |
1001 | Renew(environ, i+2, char*); /* just expand it a bit */ |
8d063cd8 |
1002 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
1003 | } |
fe14fcc3 |
1004 | else |
1005 | Safefree(environ[i]); |
a687059c |
1006 | New(904, environ[i], strlen(nam) + strlen(val) + 2, char); |
62b28dd9 |
1007 | #ifndef MSDOS |
a687059c |
1008 | (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
62b28dd9 |
1009 | #else |
1010 | /* MS-DOS requires environment variable names to be in uppercase */ |
fe14fcc3 |
1011 | /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but |
1012 | * some utilities and applications may break because they only look |
1013 | * for upper case strings. (Fixed strupr() bug here.)] |
1014 | */ |
1015 | strcpy(environ[i],nam); strupr(environ[i]); |
62b28dd9 |
1016 | (void)sprintf(environ[i] + strlen(nam),"=%s",val); |
1017 | #endif /* MSDOS */ |
8d063cd8 |
1018 | } |
1019 | |
79072805 |
1020 | I32 |
1021 | setenv_getix(nam) |
8d063cd8 |
1022 | char *nam; |
1023 | { |
79072805 |
1024 | register I32 i, len = strlen(nam); |
8d063cd8 |
1025 | |
1026 | for (i = 0; environ[i]; i++) { |
1027 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') |
1028 | break; /* strnEQ must come first to avoid */ |
1029 | } /* potential SEGV's */ |
1030 | return i; |
1031 | } |
a0d0e21e |
1032 | #endif /* !VMS */ |
378cc40b |
1033 | |
16d20bd9 |
1034 | #ifdef UNLINK_ALL_VERSIONS |
79072805 |
1035 | I32 |
378cc40b |
1036 | unlnk(f) /* unlink all versions of a file */ |
1037 | char *f; |
1038 | { |
79072805 |
1039 | I32 i; |
378cc40b |
1040 | |
1041 | for (i = 0; unlink(f) >= 0; i++) ; |
1042 | return i ? 0 : -1; |
1043 | } |
1044 | #endif |
1045 | |
85e6fe83 |
1046 | #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY) |
378cc40b |
1047 | char * |
7c0587c8 |
1048 | my_bcopy(from,to,len) |
378cc40b |
1049 | register char *from; |
1050 | register char *to; |
79072805 |
1051 | register I32 len; |
378cc40b |
1052 | { |
1053 | char *retval = to; |
1054 | |
7c0587c8 |
1055 | if (from - to >= 0) { |
1056 | while (len--) |
1057 | *to++ = *from++; |
1058 | } |
1059 | else { |
1060 | to += len; |
1061 | from += len; |
1062 | while (len--) |
faf8582f |
1063 | *(--to) = *(--from); |
7c0587c8 |
1064 | } |
378cc40b |
1065 | return retval; |
1066 | } |
ffed7fef |
1067 | #endif |
378cc40b |
1068 | |
7c0587c8 |
1069 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b |
1070 | char * |
7c0587c8 |
1071 | my_bzero(loc,len) |
378cc40b |
1072 | register char *loc; |
79072805 |
1073 | register I32 len; |
378cc40b |
1074 | { |
1075 | char *retval = loc; |
1076 | |
1077 | while (len--) |
1078 | *loc++ = 0; |
1079 | return retval; |
1080 | } |
1081 | #endif |
7c0587c8 |
1082 | |
1083 | #ifndef HAS_MEMCMP |
79072805 |
1084 | I32 |
7c0587c8 |
1085 | my_memcmp(s1,s2,len) |
1086 | register unsigned char *s1; |
1087 | register unsigned char *s2; |
79072805 |
1088 | register I32 len; |
7c0587c8 |
1089 | { |
79072805 |
1090 | register I32 tmp; |
7c0587c8 |
1091 | |
1092 | while (len--) { |
1093 | if (tmp = *s1++ - *s2++) |
1094 | return tmp; |
1095 | } |
1096 | return 0; |
1097 | } |
1098 | #endif /* HAS_MEMCMP */ |
a687059c |
1099 | |
4633a7c4 |
1100 | #if defined(I_STDARG) || defined(I_VARARGS) |
fe14fcc3 |
1101 | #ifndef HAS_VPRINTF |
a687059c |
1102 | |
85e6fe83 |
1103 | #ifdef USE_CHAR_VSPRINTF |
a687059c |
1104 | char * |
1105 | #else |
1106 | int |
1107 | #endif |
1108 | vsprintf(dest, pat, args) |
1109 | char *dest, *pat, *args; |
1110 | { |
1111 | FILE fakebuf; |
1112 | |
1113 | fakebuf._ptr = dest; |
1114 | fakebuf._cnt = 32767; |
35c8bce7 |
1115 | #ifndef _IOSTRG |
1116 | #define _IOSTRG 0 |
1117 | #endif |
a687059c |
1118 | fakebuf._flag = _IOWRT|_IOSTRG; |
1119 | _doprnt(pat, args, &fakebuf); /* what a kludge */ |
1120 | (void)putc('\0', &fakebuf); |
85e6fe83 |
1121 | #ifdef USE_CHAR_VSPRINTF |
a687059c |
1122 | return(dest); |
1123 | #else |
1124 | return 0; /* perl doesn't use return value */ |
1125 | #endif |
1126 | } |
1127 | |
a687059c |
1128 | int |
1129 | vfprintf(fd, pat, args) |
1130 | FILE *fd; |
1131 | char *pat, *args; |
1132 | { |
1133 | _doprnt(pat, args, fd); |
1134 | return 0; /* wrong, but perl doesn't use the return value */ |
1135 | } |
fe14fcc3 |
1136 | #endif /* HAS_VPRINTF */ |
4633a7c4 |
1137 | #endif /* I_VARARGS || I_STDARGS */ |
a687059c |
1138 | |
1139 | #ifdef MYSWAP |
ffed7fef |
1140 | #if BYTEORDER != 0x4321 |
a687059c |
1141 | short |
748a9306 |
1142 | #ifndef CAN_PROTOTYPE |
a687059c |
1143 | my_swap(s) |
1144 | short s; |
748a9306 |
1145 | #else |
1146 | my_swap(short s) |
1147 | #endif |
a687059c |
1148 | { |
1149 | #if (BYTEORDER & 1) == 0 |
1150 | short result; |
1151 | |
1152 | result = ((s & 255) << 8) + ((s >> 8) & 255); |
1153 | return result; |
1154 | #else |
1155 | return s; |
1156 | #endif |
1157 | } |
1158 | |
1159 | long |
748a9306 |
1160 | #ifndef CAN_PROTOTYPE |
1161 | my_htonl(l) |
a687059c |
1162 | register long l; |
748a9306 |
1163 | #else |
1164 | my_htonl(long l) |
1165 | #endif |
a687059c |
1166 | { |
1167 | union { |
1168 | long result; |
ffed7fef |
1169 | char c[sizeof(long)]; |
a687059c |
1170 | } u; |
1171 | |
ffed7fef |
1172 | #if BYTEORDER == 0x1234 |
a687059c |
1173 | u.c[0] = (l >> 24) & 255; |
1174 | u.c[1] = (l >> 16) & 255; |
1175 | u.c[2] = (l >> 8) & 255; |
1176 | u.c[3] = l & 255; |
1177 | return u.result; |
1178 | #else |
ffed7fef |
1179 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 |
1180 | croak("Unknown BYTEORDER\n"); |
a687059c |
1181 | #else |
79072805 |
1182 | register I32 o; |
1183 | register I32 s; |
a687059c |
1184 | |
ffed7fef |
1185 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1186 | u.c[o & 0xf] = (l >> s) & 255; |
a687059c |
1187 | } |
1188 | return u.result; |
1189 | #endif |
1190 | #endif |
1191 | } |
1192 | |
1193 | long |
748a9306 |
1194 | #ifndef CAN_PROTOTYPE |
1195 | my_ntohl(l) |
a687059c |
1196 | register long l; |
748a9306 |
1197 | #else |
1198 | my_ntohl(long l) |
1199 | #endif |
a687059c |
1200 | { |
1201 | union { |
1202 | long l; |
ffed7fef |
1203 | char c[sizeof(long)]; |
a687059c |
1204 | } u; |
1205 | |
ffed7fef |
1206 | #if BYTEORDER == 0x1234 |
a687059c |
1207 | u.c[0] = (l >> 24) & 255; |
1208 | u.c[1] = (l >> 16) & 255; |
1209 | u.c[2] = (l >> 8) & 255; |
1210 | u.c[3] = l & 255; |
1211 | return u.l; |
1212 | #else |
ffed7fef |
1213 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 |
1214 | croak("Unknown BYTEORDER\n"); |
a687059c |
1215 | #else |
79072805 |
1216 | register I32 o; |
1217 | register I32 s; |
a687059c |
1218 | |
1219 | u.l = l; |
1220 | l = 0; |
ffed7fef |
1221 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1222 | l |= (u.c[o & 0xf] & 255) << s; |
a687059c |
1223 | } |
1224 | return l; |
1225 | #endif |
1226 | #endif |
1227 | } |
1228 | |
ffed7fef |
1229 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 |
1230 | #endif /* MYSWAP */ |
1231 | |
1232 | /* |
1233 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. |
1234 | * If these functions are defined, |
1235 | * the BYTEORDER is neither 0x1234 nor 0x4321. |
1236 | * However, this is not assumed. |
1237 | * -DWS |
1238 | */ |
1239 | |
1240 | #define HTOV(name,type) \ |
1241 | type \ |
1242 | name (n) \ |
1243 | register type n; \ |
1244 | { \ |
1245 | union { \ |
1246 | type value; \ |
1247 | char c[sizeof(type)]; \ |
1248 | } u; \ |
79072805 |
1249 | register I32 i; \ |
1250 | register I32 s; \ |
988174c1 |
1251 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
1252 | u.c[i] = (n >> s) & 0xFF; \ |
1253 | } \ |
1254 | return u.value; \ |
1255 | } |
1256 | |
1257 | #define VTOH(name,type) \ |
1258 | type \ |
1259 | name (n) \ |
1260 | register type n; \ |
1261 | { \ |
1262 | union { \ |
1263 | type value; \ |
1264 | char c[sizeof(type)]; \ |
1265 | } u; \ |
79072805 |
1266 | register I32 i; \ |
1267 | register I32 s; \ |
988174c1 |
1268 | u.value = n; \ |
1269 | n = 0; \ |
1270 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
1271 | n += (u.c[i] & 0xFF) << s; \ |
1272 | } \ |
1273 | return n; \ |
1274 | } |
1275 | |
1276 | #if defined(HAS_HTOVS) && !defined(htovs) |
1277 | HTOV(htovs,short) |
1278 | #endif |
1279 | #if defined(HAS_HTOVL) && !defined(htovl) |
1280 | HTOV(htovl,long) |
1281 | #endif |
1282 | #if defined(HAS_VTOHS) && !defined(vtohs) |
1283 | VTOH(vtohs,short) |
1284 | #endif |
1285 | #if defined(HAS_VTOHL) && !defined(vtohl) |
1286 | VTOH(vtohl,long) |
1287 | #endif |
a687059c |
1288 | |
a0d0e21e |
1289 | #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in VMS.c */ |
a687059c |
1290 | FILE * |
79072805 |
1291 | my_popen(cmd,mode) |
a687059c |
1292 | char *cmd; |
1293 | char *mode; |
1294 | { |
1295 | int p[2]; |
79072805 |
1296 | register I32 this, that; |
1297 | register I32 pid; |
1298 | SV *sv; |
1299 | I32 doexec = strNE(cmd,"-"); |
a687059c |
1300 | |
1301 | if (pipe(p) < 0) |
1302 | return Nullfp; |
1303 | this = (*mode == 'w'); |
1304 | that = !this; |
463ee0b2 |
1305 | if (tainting) { |
1306 | if (doexec) { |
1307 | taint_env(); |
1308 | taint_proper("Insecure %s%s", "EXEC"); |
1309 | } |
d48672a2 |
1310 | } |
a687059c |
1311 | while ((pid = (doexec?vfork():fork())) < 0) { |
1312 | if (errno != EAGAIN) { |
1313 | close(p[this]); |
1314 | if (!doexec) |
463ee0b2 |
1315 | croak("Can't fork"); |
a687059c |
1316 | return Nullfp; |
1317 | } |
1318 | sleep(5); |
1319 | } |
1320 | if (pid == 0) { |
79072805 |
1321 | GV* tmpgv; |
1322 | |
a687059c |
1323 | #define THIS that |
1324 | #define THAT this |
1325 | close(p[THAT]); |
1326 | if (p[THIS] != (*mode == 'r')) { |
1327 | dup2(p[THIS], *mode == 'r'); |
1328 | close(p[THIS]); |
1329 | } |
1330 | if (doexec) { |
a0d0e21e |
1331 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 |
1332 | int fd; |
1333 | |
1334 | #ifndef NOFILE |
1335 | #define NOFILE 20 |
1336 | #endif |
d48672a2 |
1337 | for (fd = maxsysfd + 1; fd < NOFILE; fd++) |
ae986130 |
1338 | close(fd); |
1339 | #endif |
a687059c |
1340 | do_exec(cmd); /* may or may not use the shell */ |
1341 | _exit(1); |
1342 | } |
de3bb511 |
1343 | /*SUPPRESS 560*/ |
85e6fe83 |
1344 | if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV)) |
79072805 |
1345 | sv_setiv(GvSV(tmpgv),(I32)getpid()); |
9f68db38 |
1346 | forkprocess = 0; |
463ee0b2 |
1347 | hv_clear(pidstatus); /* we have no children */ |
a687059c |
1348 | return Nullfp; |
1349 | #undef THIS |
1350 | #undef THAT |
1351 | } |
62b28dd9 |
1352 | do_execfree(); /* free any memory malloced by child on vfork */ |
a687059c |
1353 | close(p[that]); |
62b28dd9 |
1354 | if (p[that] < p[this]) { |
1355 | dup2(p[this], p[that]); |
1356 | close(p[this]); |
1357 | p[this] = p[that]; |
1358 | } |
79072805 |
1359 | sv = *av_fetch(fdpid,p[this],TRUE); |
a0d0e21e |
1360 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 |
1361 | SvIVX(sv) = pid; |
a687059c |
1362 | forkprocess = pid; |
1363 | return fdopen(p[this], mode); |
1364 | } |
7c0587c8 |
1365 | #else |
4633a7c4 |
1366 | #if defined(atarist) || defined(OS2) |
7c0587c8 |
1367 | FILE *popen(); |
1368 | FILE * |
79072805 |
1369 | my_popen(cmd,mode) |
7c0587c8 |
1370 | char *cmd; |
1371 | char *mode; |
1372 | { |
1373 | return popen(cmd, mode); |
1374 | } |
1375 | #endif |
1376 | |
1377 | #endif /* !DOSISH */ |
a687059c |
1378 | |
748a9306 |
1379 | #ifdef DUMP_FDS |
79072805 |
1380 | dump_fds(s) |
ae986130 |
1381 | char *s; |
1382 | { |
1383 | int fd; |
1384 | struct stat tmpstatbuf; |
1385 | |
1386 | fprintf(stderr,"%s", s); |
1387 | for (fd = 0; fd < 32; fd++) { |
a0d0e21e |
1388 | if (Fstat(fd,&tmpstatbuf) >= 0) |
ae986130 |
1389 | fprintf(stderr," %d",fd); |
1390 | } |
1391 | fprintf(stderr,"\n"); |
1392 | } |
1393 | #endif |
1394 | |
fe14fcc3 |
1395 | #ifndef HAS_DUP2 |
fec02dd3 |
1396 | int |
a687059c |
1397 | dup2(oldfd,newfd) |
1398 | int oldfd; |
1399 | int newfd; |
1400 | { |
a0d0e21e |
1401 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 |
1402 | if (oldfd == newfd) |
1403 | return oldfd; |
62b28dd9 |
1404 | close(newfd); |
fec02dd3 |
1405 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 |
1406 | #else |
d48672a2 |
1407 | int fdtmp[256]; |
79072805 |
1408 | I32 fdx = 0; |
ae986130 |
1409 | int fd; |
1410 | |
fe14fcc3 |
1411 | if (oldfd == newfd) |
fec02dd3 |
1412 | return oldfd; |
a687059c |
1413 | close(newfd); |
fec02dd3 |
1414 | while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */ |
ae986130 |
1415 | fdtmp[fdx++] = fd; |
1416 | while (fdx > 0) |
1417 | close(fdtmp[--fdx]); |
fec02dd3 |
1418 | return fd; |
62b28dd9 |
1419 | #endif |
a687059c |
1420 | } |
1421 | #endif |
1422 | |
4633a7c4 |
1423 | #if !defined(DOSISH) && !defined(VMS) /* VMS' my_popen() is in VMS.c */ |
79072805 |
1424 | I32 |
1425 | my_pclose(ptr) |
a687059c |
1426 | FILE *ptr; |
1427 | { |
ecfc5424 |
1428 | Signal_t (*hstat)(), (*istat)(), (*qstat)(); |
a687059c |
1429 | int status; |
a0d0e21e |
1430 | SV **svp; |
20188a90 |
1431 | int pid; |
a687059c |
1432 | |
a0d0e21e |
1433 | svp = av_fetch(fdpid,fileno(ptr),TRUE); |
748a9306 |
1434 | pid = (int)SvIVX(*svp); |
a0d0e21e |
1435 | SvREFCNT_dec(*svp); |
1436 | *svp = &sv_undef; |
a687059c |
1437 | fclose(ptr); |
7c0587c8 |
1438 | #ifdef UTS |
1439 | if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ |
1440 | #endif |
a687059c |
1441 | hstat = signal(SIGHUP, SIG_IGN); |
1442 | istat = signal(SIGINT, SIG_IGN); |
1443 | qstat = signal(SIGQUIT, SIG_IGN); |
748a9306 |
1444 | do { |
1445 | pid = wait4pid(pid, &status, 0); |
1446 | } while (pid == -1 && errno == EINTR); |
20188a90 |
1447 | signal(SIGHUP, hstat); |
1448 | signal(SIGINT, istat); |
1449 | signal(SIGQUIT, qstat); |
1450 | return(pid < 0 ? pid : status); |
1451 | } |
4633a7c4 |
1452 | #endif /* !DOSISH */ |
1453 | |
1454 | #if !defined(DOSISH) || defined(OS2) |
79072805 |
1455 | I32 |
20188a90 |
1456 | wait4pid(pid,statusp,flags) |
1457 | int pid; |
1458 | int *statusp; |
1459 | int flags; |
1460 | { |
79072805 |
1461 | SV *sv; |
1462 | SV** svp; |
20188a90 |
1463 | char spid[16]; |
1464 | |
1465 | if (!pid) |
1466 | return -1; |
20188a90 |
1467 | if (pid > 0) { |
1468 | sprintf(spid, "%d", pid); |
79072805 |
1469 | svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE); |
1470 | if (svp && *svp != &sv_undef) { |
463ee0b2 |
1471 | *statusp = SvIVX(*svp); |
748a9306 |
1472 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 |
1473 | return pid; |
1474 | } |
1475 | } |
1476 | else { |
79072805 |
1477 | HE *entry; |
20188a90 |
1478 | |
79072805 |
1479 | hv_iterinit(pidstatus); |
1480 | if (entry = hv_iternext(pidstatus)) { |
a0d0e21e |
1481 | pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
79072805 |
1482 | sv = hv_iterval(pidstatus,entry); |
463ee0b2 |
1483 | *statusp = SvIVX(sv); |
20188a90 |
1484 | sprintf(spid, "%d", pid); |
748a9306 |
1485 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 |
1486 | return pid; |
1487 | } |
1488 | } |
79072805 |
1489 | #ifdef HAS_WAITPID |
1490 | return waitpid(pid,statusp,flags); |
1491 | #else |
a0d0e21e |
1492 | #ifdef HAS_WAIT4 |
1493 | return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); |
1494 | #else |
1495 | { |
1496 | I32 result; |
1497 | if (flags) |
1498 | croak("Can't do waitpid with flags"); |
1499 | else { |
1500 | while ((result = wait(statusp)) != pid && pid > 0 && result >= 0) |
1501 | pidgone(result,*statusp); |
1502 | if (result < 0) |
1503 | *statusp = -1; |
1504 | } |
1505 | return result; |
a687059c |
1506 | } |
1507 | #endif |
20188a90 |
1508 | #endif |
a687059c |
1509 | } |
7c0587c8 |
1510 | #endif /* !DOSISH */ |
a687059c |
1511 | |
7c0587c8 |
1512 | void |
de3bb511 |
1513 | /*SUPPRESS 590*/ |
a687059c |
1514 | pidgone(pid,status) |
1515 | int pid; |
1516 | int status; |
1517 | { |
79072805 |
1518 | register SV *sv; |
20188a90 |
1519 | char spid[16]; |
a687059c |
1520 | |
20188a90 |
1521 | sprintf(spid, "%d", pid); |
79072805 |
1522 | sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE); |
a0d0e21e |
1523 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 |
1524 | SvIVX(sv) = status; |
20188a90 |
1525 | return; |
a687059c |
1526 | } |
1527 | |
4633a7c4 |
1528 | #if defined(atarist) || defined(OS2) |
7c0587c8 |
1529 | int pclose(); |
79072805 |
1530 | I32 |
1531 | my_pclose(ptr) |
7c0587c8 |
1532 | FILE *ptr; |
a687059c |
1533 | { |
7c0587c8 |
1534 | return pclose(ptr); |
a687059c |
1535 | } |
7c0587c8 |
1536 | #endif |
9f68db38 |
1537 | |
1538 | void |
1539 | repeatcpy(to,from,len,count) |
1540 | register char *to; |
1541 | register char *from; |
79072805 |
1542 | I32 len; |
1543 | register I32 count; |
9f68db38 |
1544 | { |
79072805 |
1545 | register I32 todo; |
9f68db38 |
1546 | register char *frombase = from; |
1547 | |
1548 | if (len == 1) { |
1549 | todo = *from; |
1550 | while (count-- > 0) |
1551 | *to++ = todo; |
1552 | return; |
1553 | } |
1554 | while (count-- > 0) { |
1555 | for (todo = len; todo > 0; todo--) { |
1556 | *to++ = *from++; |
1557 | } |
1558 | from = frombase; |
1559 | } |
1560 | } |
0f85fab0 |
1561 | |
1562 | #ifndef CASTNEGFLOAT |
463ee0b2 |
1563 | U32 |
79072805 |
1564 | cast_ulong(f) |
0f85fab0 |
1565 | double f; |
1566 | { |
1567 | long along; |
1568 | |
27e2fb84 |
1569 | #if CASTFLAGS & 2 |
34de22dd |
1570 | # define BIGDOUBLE 2147483648.0 |
1571 | if (f >= BIGDOUBLE) |
1572 | return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000; |
1573 | #endif |
0f85fab0 |
1574 | if (f >= 0.0) |
1575 | return (unsigned long)f; |
1576 | along = (long)f; |
1577 | return (unsigned long)along; |
1578 | } |
ed6116ce |
1579 | # undef BIGDOUBLE |
1580 | #endif |
1581 | |
1582 | #ifndef CASTI32 |
5d94fbed |
1583 | |
1584 | /* Look for MAX and MIN integral values. If we can't find them, |
1585 | we'll use 32-bit two's complement defaults. |
1586 | */ |
1587 | #ifndef LONG_MAX |
1588 | # ifdef MAXLONG /* Often used in <values.h> */ |
1589 | # define LONG_MAX MAXLONG |
1590 | # else |
1591 | # define LONG_MAX 2147483647L |
1592 | # endif |
1593 | #endif |
1594 | |
1595 | #ifndef LONG_MIN |
1596 | # define LONG_MIN (-LONG_MAX - 1) |
1597 | #endif |
1598 | |
1599 | #ifndef ULONG_MAX |
1600 | # ifdef MAXULONG |
1601 | # define LONG_MAX MAXULONG |
1602 | # else |
1603 | # define ULONG_MAX 4294967295L |
1604 | # endif |
1605 | #endif |
1606 | |
1607 | /* Unfortunately, on some systems the cast_uv() function doesn't |
1608 | work with the system-supplied definition of ULONG_MAX. The |
1609 | comparison (f >= ULONG_MAX) always comes out true. It must be a |
1610 | problem with the compiler constant folding. |
1611 | |
1612 | In any case, this workaround should be fine on any two's complement |
1613 | system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your |
1614 | ccflags. |
1615 | --Andy Dougherty <doughera@lafcol.lafayette.edu> |
1616 | */ |
1617 | #ifndef MY_ULONG_MAX |
1618 | # define MY_ULONG_MAX ((UV)LONG_MAX * (UV)2 + (UV)1) |
1619 | #endif |
1620 | |
ed6116ce |
1621 | I32 |
1622 | cast_i32(f) |
1623 | double f; |
1624 | { |
5d94fbed |
1625 | if (f >= LONG_MAX) |
1626 | return (I32) LONG_MAX; |
1627 | if (f <= LONG_MIN) |
1628 | return (I32) LONG_MIN; |
ed6116ce |
1629 | return (I32) f; |
1630 | } |
a0d0e21e |
1631 | |
1632 | IV |
1633 | cast_iv(f) |
1634 | double f; |
1635 | { |
5d94fbed |
1636 | if (f >= LONG_MAX) |
1637 | return (IV) LONG_MAX; |
1638 | if (f <= LONG_MIN) |
1639 | return (IV) LONG_MIN; |
a0d0e21e |
1640 | return (IV) f; |
1641 | } |
5d94fbed |
1642 | |
1643 | UV |
1644 | cast_uv(f) |
1645 | double f; |
1646 | { |
1647 | if (f >= MY_ULONG_MAX) |
1648 | return (UV) MY_ULONG_MAX; |
1649 | return (UV) f; |
1650 | } |
1651 | |
0f85fab0 |
1652 | #endif |
62b28dd9 |
1653 | |
fe14fcc3 |
1654 | #ifndef HAS_RENAME |
79072805 |
1655 | I32 |
62b28dd9 |
1656 | same_dirent(a,b) |
1657 | char *a; |
1658 | char *b; |
1659 | { |
93a17b20 |
1660 | char *fa = strrchr(a,'/'); |
1661 | char *fb = strrchr(b,'/'); |
62b28dd9 |
1662 | struct stat tmpstatbuf1; |
1663 | struct stat tmpstatbuf2; |
1664 | #ifndef MAXPATHLEN |
1665 | #define MAXPATHLEN 1024 |
1666 | #endif |
1667 | char tmpbuf[MAXPATHLEN+1]; |
1668 | |
1669 | if (fa) |
1670 | fa++; |
1671 | else |
1672 | fa = a; |
1673 | if (fb) |
1674 | fb++; |
1675 | else |
1676 | fb = b; |
1677 | if (strNE(a,b)) |
1678 | return FALSE; |
1679 | if (fa == a) |
6eb13c3b |
1680 | strcpy(tmpbuf,"."); |
62b28dd9 |
1681 | else |
1682 | strncpy(tmpbuf, a, fa - a); |
a0d0e21e |
1683 | if (Stat(tmpbuf, &tmpstatbuf1) < 0) |
62b28dd9 |
1684 | return FALSE; |
1685 | if (fb == b) |
6eb13c3b |
1686 | strcpy(tmpbuf,"."); |
62b28dd9 |
1687 | else |
1688 | strncpy(tmpbuf, b, fb - b); |
a0d0e21e |
1689 | if (Stat(tmpbuf, &tmpstatbuf2) < 0) |
62b28dd9 |
1690 | return FALSE; |
1691 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && |
1692 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; |
1693 | } |
fe14fcc3 |
1694 | #endif /* !HAS_RENAME */ |
1695 | |
1696 | unsigned long |
79072805 |
1697 | scan_oct(start, len, retlen) |
fe14fcc3 |
1698 | char *start; |
79072805 |
1699 | I32 len; |
1700 | I32 *retlen; |
fe14fcc3 |
1701 | { |
1702 | register char *s = start; |
1703 | register unsigned long retval = 0; |
1704 | |
748a9306 |
1705 | while (len && *s >= '0' && *s <= '7') { |
fe14fcc3 |
1706 | retval <<= 3; |
1707 | retval |= *s++ - '0'; |
748a9306 |
1708 | len--; |
fe14fcc3 |
1709 | } |
748a9306 |
1710 | if (dowarn && len && (*s == '8' || *s == '9')) |
1711 | warn("Illegal octal digit ignored"); |
fe14fcc3 |
1712 | *retlen = s - start; |
1713 | return retval; |
1714 | } |
1715 | |
1716 | unsigned long |
79072805 |
1717 | scan_hex(start, len, retlen) |
fe14fcc3 |
1718 | char *start; |
79072805 |
1719 | I32 len; |
1720 | I32 *retlen; |
fe14fcc3 |
1721 | { |
1722 | register char *s = start; |
1723 | register unsigned long retval = 0; |
1724 | char *tmp; |
1725 | |
93a17b20 |
1726 | while (len-- && *s && (tmp = strchr(hexdigit, *s))) { |
fe14fcc3 |
1727 | retval <<= 4; |
1728 | retval |= (tmp - hexdigit) & 15; |
1729 | s++; |
1730 | } |
1731 | *retlen = s - start; |
1732 | return retval; |
1733 | } |