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