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 | |
36477c24 | 22 | #ifndef SIG_ERR |
23 | # define SIG_ERR ((Sighandler_t) -1) | |
24 | #endif | |
25 | ||
bd4080b3 | 26 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ |
85e6fe83 | 27 | #ifdef I_UNISTD |
8990e307 LW |
28 | # include <unistd.h> |
29 | #endif | |
30 | ||
a687059c LW |
31 | #ifdef I_VFORK |
32 | # include <vfork.h> | |
33 | #endif | |
34 | ||
94b6baf5 AD |
35 | /* Put this after #includes because fork and vfork prototypes may |
36 | conflict. | |
37 | */ | |
38 | #ifndef HAS_VFORK | |
39 | # define vfork fork | |
40 | #endif | |
41 | ||
fe14fcc3 LW |
42 | #ifdef I_FCNTL |
43 | # include <fcntl.h> | |
44 | #endif | |
45 | #ifdef I_SYS_FILE | |
46 | # include <sys/file.h> | |
47 | #endif | |
48 | ||
ff68c719 | 49 | #ifdef I_SYS_WAIT |
50 | # include <sys/wait.h> | |
51 | #endif | |
52 | ||
8d063cd8 | 53 | #define FLUSH |
8d063cd8 | 54 | |
a0d0e21e LW |
55 | #ifdef LEAKTEST |
56 | static void xstat _((void)); | |
57 | #endif | |
58 | ||
55497cff | 59 | #ifndef MYMALLOC |
de3bb511 | 60 | |
8d063cd8 LW |
61 | /* paranoid version of malloc */ |
62 | ||
a687059c LW |
63 | /* NOTE: Do not call the next three routines directly. Use the macros |
64 | * in handy.h, so that we can easily redefine everything to do tracking of | |
65 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 66 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
67 | */ |
68 | ||
bd4080b3 | 69 | Malloc_t |
8d063cd8 LW |
70 | safemalloc(size) |
71 | MEM_SIZE size; | |
72 | { | |
bd4080b3 | 73 | Malloc_t ptr; |
55497cff | 74 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 75 | if (size > 0xffff) { |
760ac839 | 76 | PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH; |
79072805 | 77 | my_exit(1); |
62b28dd9 | 78 | } |
55497cff | 79 | #endif /* HAS_64K_LIMIT */ |
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 | 85 | #if !(defined(I286) || defined(atarist)) |
760ac839 | 86 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size)); |
79072805 | 87 | #else |
760ac839 | 88 | DEBUG_m(PerlIO_printf(Perl_debug_log, "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 { |
760ac839 | 95 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
79072805 | 96 | my_exit(1); |
8d063cd8 LW |
97 | } |
98 | /*NOTREACHED*/ | |
99 | } | |
100 | ||
101 | /* paranoid version of realloc */ | |
102 | ||
bd4080b3 | 103 | Malloc_t |
8d063cd8 | 104 | saferealloc(where,size) |
bd4080b3 | 105 | Malloc_t where; |
8d063cd8 LW |
106 | MEM_SIZE size; |
107 | { | |
bd4080b3 | 108 | Malloc_t ptr; |
ecfc5424 | 109 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) |
bd4080b3 | 110 | Malloc_t realloc(); |
ecfc5424 | 111 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 112 | |
55497cff | 113 | #ifdef HAS_64K_LIMIT |
5f05dabc | 114 | if (size > 0xffff) { |
115 | PerlIO_printf(PerlIO_stderr(), | |
116 | "Reallocation too large: %lx\n", size) FLUSH; | |
117 | my_exit(1); | |
118 | } | |
55497cff | 119 | #endif /* HAS_64K_LIMIT */ |
378cc40b | 120 | if (!where) |
463ee0b2 | 121 | croak("Null realloc"); |
34de22dd LW |
122 | #ifdef DEBUGGING |
123 | if ((long)size < 0) | |
463ee0b2 | 124 | croak("panic: realloc"); |
34de22dd | 125 | #endif |
8d063cd8 | 126 | ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */ |
79072805 LW |
127 | |
128 | #if !(defined(I286) || defined(atarist)) | |
129 | DEBUG_m( { | |
760ac839 LW |
130 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++); |
131 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); | |
79072805 LW |
132 | } ) |
133 | #else | |
134 | DEBUG_m( { | |
760ac839 LW |
135 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++); |
136 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size); | |
79072805 | 137 | } ) |
8d063cd8 | 138 | #endif |
79072805 | 139 | |
8d063cd8 LW |
140 | if (ptr != Nullch) |
141 | return ptr; | |
7c0587c8 LW |
142 | else if (nomemok) |
143 | return Nullch; | |
8d063cd8 | 144 | else { |
760ac839 | 145 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
79072805 | 146 | my_exit(1); |
8d063cd8 LW |
147 | } |
148 | /*NOTREACHED*/ | |
149 | } | |
150 | ||
151 | /* safe version of free */ | |
152 | ||
a687059c | 153 | void |
8d063cd8 | 154 | safefree(where) |
bd4080b3 | 155 | Malloc_t where; |
8d063cd8 | 156 | { |
79072805 | 157 | #if !(defined(I286) || defined(atarist)) |
760ac839 | 158 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",where,an++)); |
79072805 | 159 | #else |
760ac839 | 160 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",where,an++)); |
8d063cd8 | 161 | #endif |
378cc40b | 162 | if (where) { |
de3bb511 | 163 | /*SUPPRESS 701*/ |
378cc40b LW |
164 | free(where); |
165 | } | |
8d063cd8 LW |
166 | } |
167 | ||
1050c9ca | 168 | /* safe version of calloc */ |
169 | ||
bd4080b3 | 170 | Malloc_t |
1050c9ca | 171 | safecalloc(count, size) |
172 | MEM_SIZE count; | |
173 | MEM_SIZE size; | |
174 | { | |
bd4080b3 | 175 | Malloc_t ptr; |
1050c9ca | 176 | |
55497cff | 177 | #ifdef HAS_64K_LIMIT |
5f05dabc | 178 | if (size * count > 0xffff) { |
179 | PerlIO_printf(PerlIO_stderr(), | |
180 | "Allocation too large: %lx\n", size * count) FLUSH; | |
181 | my_exit(1); | |
182 | } | |
55497cff | 183 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 184 | #ifdef DEBUGGING |
185 | if ((long)size < 0 || (long)count < 0) | |
186 | croak("panic: calloc"); | |
187 | #endif | |
188 | #if !(defined(I286) || defined(atarist)) | |
760ac839 | 189 | DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size)); |
1050c9ca | 190 | #else |
760ac839 | 191 | DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size)); |
1050c9ca | 192 | #endif |
193 | size *= count; | |
194 | ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */ | |
195 | if (ptr != Nullch) { | |
196 | memset((void*)ptr, 0, size); | |
197 | return ptr; | |
198 | } | |
199 | else if (nomemok) | |
200 | return Nullch; | |
201 | else { | |
760ac839 | 202 | PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH; |
1050c9ca | 203 | my_exit(1); |
204 | } | |
205 | /*NOTREACHED*/ | |
206 | } | |
207 | ||
55497cff | 208 | #endif /* !MYMALLOC */ |
de3bb511 | 209 | |
a687059c LW |
210 | #ifdef LEAKTEST |
211 | ||
212 | #define ALIGN sizeof(long) | |
8d063cd8 | 213 | |
bd4080b3 | 214 | Malloc_t |
a687059c | 215 | safexmalloc(x,size) |
79072805 | 216 | I32 x; |
a687059c | 217 | MEM_SIZE size; |
8d063cd8 | 218 | { |
bd4080b3 | 219 | register Malloc_t where; |
8d063cd8 | 220 | |
a687059c LW |
221 | where = safemalloc(size + ALIGN); |
222 | xcount[x]++; | |
223 | where[0] = x % 100; | |
224 | where[1] = x / 100; | |
225 | return where + ALIGN; | |
8d063cd8 | 226 | } |
8d063cd8 | 227 | |
bd4080b3 | 228 | Malloc_t |
a687059c | 229 | safexrealloc(where,size) |
bd4080b3 | 230 | Malloc_t where; |
a687059c LW |
231 | MEM_SIZE size; |
232 | { | |
bd4080b3 | 233 | register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN); |
a0d0e21e | 234 | return new + ALIGN; |
a687059c LW |
235 | } |
236 | ||
237 | void | |
238 | safexfree(where) | |
bd4080b3 | 239 | Malloc_t where; |
a687059c | 240 | { |
79072805 | 241 | I32 x; |
a687059c LW |
242 | |
243 | if (!where) | |
244 | return; | |
245 | where -= ALIGN; | |
246 | x = where[0] + 100 * where[1]; | |
247 | xcount[x]--; | |
248 | safefree(where); | |
249 | } | |
250 | ||
bd4080b3 | 251 | Malloc_t |
1050c9ca | 252 | safexcalloc(x,count,size) |
253 | I32 x; | |
254 | MEM_SIZE count; | |
255 | MEM_SIZE size; | |
256 | { | |
bd4080b3 | 257 | register Malloc_t where; |
1050c9ca | 258 | |
259 | where = safexmalloc(x, size * count + ALIGN); | |
260 | xcount[x]++; | |
261 | memset((void*)where + ALIGN, 0, size * count); | |
262 | where[0] = x % 100; | |
263 | where[1] = x / 100; | |
264 | return where + ALIGN; | |
265 | } | |
266 | ||
7c0587c8 | 267 | static void |
a687059c | 268 | xstat() |
8d063cd8 | 269 | { |
79072805 | 270 | register I32 i; |
8d063cd8 | 271 | |
a687059c | 272 | for (i = 0; i < MAXXCOUNT; i++) { |
de3bb511 | 273 | if (xcount[i] > lastxcount[i]) { |
760ac839 | 274 | PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]); |
a687059c | 275 | lastxcount[i] = xcount[i]; |
8d063cd8 LW |
276 | } |
277 | } | |
8d063cd8 | 278 | } |
a687059c LW |
279 | |
280 | #endif /* LEAKTEST */ | |
8d063cd8 LW |
281 | |
282 | /* copy a string up to some (non-backslashed) delimiter, if any */ | |
283 | ||
284 | char * | |
a687059c | 285 | cpytill(to,from,fromend,delim,retlen) |
62b28dd9 LW |
286 | register char *to; |
287 | register char *from; | |
a687059c | 288 | register char *fromend; |
a0d0e21e | 289 | register int delim; |
79072805 | 290 | I32 *retlen; |
8d063cd8 | 291 | { |
a687059c LW |
292 | char *origto = to; |
293 | ||
294 | for (; from < fromend; from++,to++) { | |
378cc40b LW |
295 | if (*from == '\\') { |
296 | if (from[1] == delim) | |
297 | from++; | |
298 | else if (from[1] == '\\') | |
299 | *to++ = *from++; | |
300 | } | |
8d063cd8 LW |
301 | else if (*from == delim) |
302 | break; | |
303 | *to = *from; | |
304 | } | |
305 | *to = '\0'; | |
a687059c | 306 | *retlen = to - origto; |
8d063cd8 LW |
307 | return from; |
308 | } | |
309 | ||
310 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 311 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
312 | |
313 | char * | |
314 | instr(big, little) | |
378cc40b LW |
315 | register char *big; |
316 | register char *little; | |
317 | { | |
318 | register char *s, *x; | |
79072805 | 319 | register I32 first; |
378cc40b | 320 | |
a687059c LW |
321 | if (!little) |
322 | return big; | |
323 | first = *little++; | |
378cc40b LW |
324 | if (!first) |
325 | return big; | |
326 | while (*big) { | |
327 | if (*big++ != first) | |
328 | continue; | |
329 | for (x=big,s=little; *s; /**/ ) { | |
330 | if (!*x) | |
331 | return Nullch; | |
332 | if (*s++ != *x++) { | |
333 | s--; | |
334 | break; | |
335 | } | |
336 | } | |
337 | if (!*s) | |
338 | return big-1; | |
339 | } | |
340 | return Nullch; | |
341 | } | |
8d063cd8 | 342 | |
a687059c LW |
343 | /* same as instr but allow embedded nulls */ |
344 | ||
345 | char * | |
346 | ninstr(big, bigend, little, lend) | |
347 | register char *big; | |
348 | register char *bigend; | |
349 | char *little; | |
350 | char *lend; | |
8d063cd8 | 351 | { |
a687059c | 352 | register char *s, *x; |
79072805 | 353 | register I32 first = *little; |
a687059c | 354 | register char *littleend = lend; |
378cc40b | 355 | |
a0d0e21e | 356 | if (!first && little >= littleend) |
a687059c | 357 | return big; |
de3bb511 LW |
358 | if (bigend - big < littleend - little) |
359 | return Nullch; | |
a687059c LW |
360 | bigend -= littleend - little++; |
361 | while (big <= bigend) { | |
362 | if (*big++ != first) | |
363 | continue; | |
364 | for (x=big,s=little; s < littleend; /**/ ) { | |
365 | if (*s++ != *x++) { | |
366 | s--; | |
367 | break; | |
368 | } | |
369 | } | |
370 | if (s >= littleend) | |
371 | return big-1; | |
378cc40b | 372 | } |
a687059c LW |
373 | return Nullch; |
374 | } | |
375 | ||
376 | /* reverse of the above--find last substring */ | |
377 | ||
378 | char * | |
379 | rninstr(big, bigend, little, lend) | |
380 | register char *big; | |
381 | char *bigend; | |
382 | char *little; | |
383 | char *lend; | |
384 | { | |
385 | register char *bigbeg; | |
386 | register char *s, *x; | |
79072805 | 387 | register I32 first = *little; |
a687059c LW |
388 | register char *littleend = lend; |
389 | ||
a0d0e21e | 390 | if (!first && little >= littleend) |
a687059c LW |
391 | return bigend; |
392 | bigbeg = big; | |
393 | big = bigend - (littleend - little++); | |
394 | while (big >= bigbeg) { | |
395 | if (*big-- != first) | |
396 | continue; | |
397 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
398 | if (*s++ != *x++) { | |
399 | s--; | |
400 | break; | |
401 | } | |
402 | } | |
403 | if (s >= littleend) | |
404 | return big+1; | |
378cc40b | 405 | } |
a687059c | 406 | return Nullch; |
378cc40b | 407 | } |
a687059c | 408 | |
bbce6d69 | 409 | /* |
410 | * Set up for a new ctype locale. | |
411 | */ | |
55497cff | 412 | void |
bbce6d69 | 413 | perl_new_ctype(newctype) |
414 | char *newctype; | |
ef7eada9 | 415 | { |
36477c24 | 416 | #ifdef USE_LOCALE_CTYPE |
417 | ||
bbce6d69 | 418 | int i; |
ef7eada9 | 419 | |
bbce6d69 | 420 | for (i = 0; i < 256; i++) { |
421 | if (isUPPER_LC(i)) | |
422 | fold_locale[i] = toLOWER_LC(i); | |
423 | else if (isLOWER_LC(i)) | |
424 | fold_locale[i] = toUPPER_LC(i); | |
425 | else | |
426 | fold_locale[i] = i; | |
427 | } | |
bbce6d69 | 428 | |
36477c24 | 429 | #endif /* USE_LOCALE_CTYPE */ |
430 | } | |
bbce6d69 | 431 | |
432 | /* | |
433 | * Set up for a new collation locale. | |
434 | */ | |
435 | void | |
436 | perl_new_collate(newcoll) | |
437 | char *newcoll; | |
438 | { | |
36477c24 | 439 | #ifdef USE_LOCALE_COLLATE |
440 | ||
bbce6d69 | 441 | if (! newcoll) { |
442 | if (collation_name) { | |
443 | ++collation_ix; | |
444 | Safefree(collation_name); | |
445 | collation_name = NULL; | |
446 | collation_standard = TRUE; | |
bbce6d69 | 447 | collxfrm_base = 0; |
448 | collxfrm_mult = 2; | |
bbce6d69 | 449 | } |
450 | return; | |
451 | } | |
452 | ||
453 | if (! collation_name || strNE(collation_name, newcoll)) { | |
454 | ++collation_ix; | |
455 | Safefree(collation_name); | |
456 | collation_name = savepv(newcoll); | |
ff68c719 | 457 | collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX")); |
bbce6d69 | 458 | |
bbce6d69 | 459 | { |
460 | /* 2: at most so many chars ('a', 'b'). */ | |
461 | /* 50: surely no system expands a char more. */ | |
462 | #define XFRMBUFSIZE (2 * 50) | |
463 | char xbuf[XFRMBUFSIZE]; | |
464 | Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); | |
465 | Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
466 | SSize_t mult = fb - fa; | |
467 | if (mult < 1) | |
468 | croak("strxfrm() gets absurd"); | |
469 | collxfrm_base = (fa > mult) ? (fa - mult) : 0; | |
470 | collxfrm_mult = mult; | |
471 | } | |
bbce6d69 | 472 | } |
bbce6d69 | 473 | |
36477c24 | 474 | #endif /* USE_LOCALE_COLLATE */ |
475 | } | |
bbce6d69 | 476 | |
477 | /* | |
478 | * Set up for a new numeric locale. | |
479 | */ | |
480 | void | |
481 | perl_new_numeric(newnum) | |
482 | char *newnum; | |
483 | { | |
36477c24 | 484 | #ifdef USE_LOCALE_NUMERIC |
485 | ||
bbce6d69 | 486 | if (! newnum) { |
487 | if (numeric_name) { | |
488 | Safefree(numeric_name); | |
489 | numeric_name = NULL; | |
490 | numeric_standard = TRUE; | |
491 | numeric_local = TRUE; | |
492 | } | |
493 | return; | |
494 | } | |
495 | ||
496 | if (! numeric_name || strNE(numeric_name, newnum)) { | |
497 | Safefree(numeric_name); | |
498 | numeric_name = savepv(newnum); | |
ff68c719 | 499 | numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX")); |
bbce6d69 | 500 | numeric_local = TRUE; |
501 | } | |
36477c24 | 502 | |
503 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 504 | } |
505 | ||
506 | void | |
36477c24 | 507 | perl_set_numeric_standard() |
bbce6d69 | 508 | { |
5f05dabc | 509 | #ifdef USE_LOCALE_NUMERIC |
510 | ||
bbce6d69 | 511 | if (! numeric_standard) { |
512 | setlocale(LC_NUMERIC, "C"); | |
513 | numeric_standard = TRUE; | |
514 | numeric_local = FALSE; | |
515 | } | |
5f05dabc | 516 | |
517 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 518 | } |
519 | ||
520 | void | |
36477c24 | 521 | perl_set_numeric_local() |
bbce6d69 | 522 | { |
5f05dabc | 523 | #ifdef USE_LOCALE_NUMERIC |
524 | ||
bbce6d69 | 525 | if (! numeric_local) { |
526 | setlocale(LC_NUMERIC, numeric_name); | |
527 | numeric_standard = FALSE; | |
528 | numeric_local = TRUE; | |
529 | } | |
bbce6d69 | 530 | |
36477c24 | 531 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 532 | } |
36477c24 | 533 | |
bbce6d69 | 534 | |
36477c24 | 535 | /* |
536 | * Initialize locale awareness. | |
537 | */ | |
f0c5b223 | 538 | int |
1050c9ca | 539 | perl_init_i18nl10n(printwarn) |
f0c5b223 TB |
540 | int printwarn; |
541 | { | |
542 | int ok = 1; | |
543 | /* returns | |
544 | * 1 = set ok or not applicable, | |
545 | * 0 = fallback to C locale, | |
546 | * -1 = fallback to C locale failed | |
547 | */ | |
bbce6d69 | 548 | |
36477c24 | 549 | #ifdef USE_LOCALE |
bbce6d69 | 550 | |
36477c24 | 551 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 552 | char *curctype = NULL; |
36477c24 | 553 | #endif /* USE_LOCALE_CTYPE */ |
554 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 555 | char *curcoll = NULL; |
36477c24 | 556 | #endif /* USE_LOCALE_COLLATE */ |
557 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 558 | char *curnum = NULL; |
36477c24 | 559 | #endif /* USE_LOCALE_NUMERIC */ |
02b32252 | 560 | char *lc_all = getenv("LC_ALL"); |
5f05dabc | 561 | char *lang = getenv("LANG"); |
bbce6d69 | 562 | bool setlocale_failure = FALSE; |
f0c5b223 | 563 | |
02b32252 CS |
564 | #ifdef LOCALE_ENVIRON_REQUIRED |
565 | ||
566 | /* | |
567 | * Ultrix setlocale(..., "") fails if there are no environment | |
568 | * variables from which to get a locale name. | |
569 | */ | |
570 | ||
571 | bool done = FALSE; | |
572 | ||
573 | #ifdef LC_ALL | |
574 | if (lang) { | |
575 | if (setlocale(LC_ALL, "")) | |
576 | done = TRUE; | |
577 | else | |
578 | setlocale_failure = TRUE; | |
579 | } | |
580 | if (!setlocale_failure) | |
581 | #endif /* LC_ALL */ | |
582 | { | |
583 | #ifdef USE_LOCALE_CTYPE | |
584 | if (! (curctype = setlocale(LC_CTYPE, | |
585 | (!done && (lang || getenv("LC_CTYPE"))) | |
586 | ? "" : Nullch))) | |
587 | setlocale_failure = TRUE; | |
588 | #endif /* USE_LOCALE_CTYPE */ | |
589 | #ifdef USE_LOCALE_COLLATE | |
590 | if (! (curcoll = setlocale(LC_COLLATE, | |
591 | (!done && (lang || getenv("LC_COLLATE"))) | |
592 | ? "" : Nullch))) | |
593 | setlocale_failure = TRUE; | |
594 | #endif /* USE_LOCALE_COLLATE */ | |
595 | #ifdef USE_LOCALE_NUMERIC | |
596 | if (! (curnum = setlocale(LC_NUMERIC, | |
597 | (!done && (lang || getenv("LC_NUMERIC"))) | |
598 | ? "" : Nullch))) | |
599 | setlocale_failure = TRUE; | |
600 | #endif /* USE_LOCALE_NUMERIC */ | |
601 | } | |
602 | ||
603 | #else /* !LOCALE_ENVIRON_REQUIRED */ | |
604 | ||
bbce6d69 | 605 | #ifdef LC_ALL |
5f05dabc | 606 | |
bbce6d69 | 607 | if (! setlocale(LC_ALL, "")) |
608 | setlocale_failure = TRUE; | |
5f05dabc | 609 | else { |
610 | #ifdef USE_LOCALE_CTYPE | |
611 | curctype = setlocale(LC_CTYPE, Nullch); | |
612 | #endif /* USE_LOCALE_CTYPE */ | |
613 | #ifdef USE_LOCALE_COLLATE | |
614 | curcoll = setlocale(LC_COLLATE, Nullch); | |
615 | #endif /* USE_LOCALE_COLLATE */ | |
616 | #ifdef USE_LOCALE_NUMERIC | |
617 | curnum = setlocale(LC_NUMERIC, Nullch); | |
618 | #endif /* USE_LOCALE_NUMERIC */ | |
619 | } | |
620 | ||
621 | #else /* !LC_ALL */ | |
bbce6d69 | 622 | |
36477c24 | 623 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 624 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
bbce6d69 | 625 | setlocale_failure = TRUE; |
36477c24 | 626 | #endif /* USE_LOCALE_CTYPE */ |
627 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 628 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
bbce6d69 | 629 | setlocale_failure = TRUE; |
36477c24 | 630 | #endif /* USE_LOCALE_COLLATE */ |
631 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 632 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
bbce6d69 | 633 | setlocale_failure = TRUE; |
36477c24 | 634 | #endif /* USE_LOCALE_NUMERIC */ |
bbce6d69 | 635 | |
5f05dabc | 636 | #endif /* LC_ALL */ |
637 | ||
02b32252 CS |
638 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
639 | ||
5f05dabc | 640 | if (setlocale_failure) { |
641 | char *p; | |
642 | bool locwarn = (printwarn > 1 || | |
643 | printwarn && | |
644 | (!(p = getenv("PERL_BADLANG")) || atoi(p))); | |
20cec16a | 645 | |
5f05dabc | 646 | if (locwarn) { |
647 | #ifdef LC_ALL | |
648 | ||
649 | PerlIO_printf(PerlIO_stderr(), | |
650 | "perl: warning: Setting locale failed.\n"); | |
651 | ||
652 | #else /* !LC_ALL */ | |
653 | ||
ef7eada9 | 654 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 655 | "perl: warning: Setting locale failed for the categories:\n\t"); |
36477c24 | 656 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 657 | if (! curctype) |
5f05dabc | 658 | PerlIO_printf(PerlIO_stderr(), "LC_CTYPE "); |
36477c24 | 659 | #endif /* USE_LOCALE_CTYPE */ |
660 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 661 | if (! curcoll) |
5f05dabc | 662 | PerlIO_printf(PerlIO_stderr(), "LC_COLLATE "); |
36477c24 | 663 | #endif /* USE_LOCALE_COLLATE */ |
664 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 665 | if (! curnum) |
5f05dabc | 666 | PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC "); |
36477c24 | 667 | #endif /* USE_LOCALE_NUMERIC */ |
bbce6d69 | 668 | PerlIO_printf(PerlIO_stderr(), "\n"); |
669 | ||
5f05dabc | 670 | #endif /* LC_ALL */ |
671 | ||
760ac839 | 672 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 673 | "perl: warning: Please check that your locale settings:\n"); |
ef7eada9 JH |
674 | |
675 | PerlIO_printf(PerlIO_stderr(), | |
bbce6d69 | 676 | "\tLC_ALL = %c%s%c,\n", |
677 | lc_all ? '"' : '(', | |
678 | lc_all ? lc_all : "unset", | |
679 | lc_all ? '"' : ')'); | |
5f05dabc | 680 | |
681 | { | |
682 | char **e; | |
683 | for (e = environ; *e; e++) { | |
684 | if (strnEQ(*e, "LC_", 3) | |
685 | && strnNE(*e, "LC_ALL=", 7) | |
686 | && (p = strchr(*e, '='))) | |
687 | PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n", | |
688 | (p - *e), *e, p + 1); | |
689 | } | |
690 | } | |
691 | ||
ef7eada9 | 692 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 693 | "\tLANG = %c%s%c\n", |
5f05dabc | 694 | lang ? '"' : '(', |
bbce6d69 | 695 | lang ? lang : "unset", |
696 | lang ? '"' : ')'); | |
ef7eada9 | 697 | |
bbce6d69 | 698 | PerlIO_printf(PerlIO_stderr(), |
699 | " are supported and installed on your system.\n"); | |
5f05dabc | 700 | } |
ef7eada9 | 701 | |
5f05dabc | 702 | #ifdef LC_ALL |
703 | ||
704 | if (setlocale(LC_ALL, "C")) { | |
705 | if (locwarn) | |
706 | PerlIO_printf(PerlIO_stderr(), | |
707 | "perl: warning: Falling back to the standard locale (\"C\").\n"); | |
bbce6d69 | 708 | ok = 0; |
ef7eada9 | 709 | } |
5f05dabc | 710 | else { |
711 | if (locwarn) | |
712 | PerlIO_printf(PerlIO_stderr(), | |
713 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); | |
714 | ok = -1; | |
715 | } | |
bbce6d69 | 716 | |
5f05dabc | 717 | #else /* ! LC_ALL */ |
718 | ||
719 | if (0 | |
36477c24 | 720 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 721 | || !(curctype || setlocale(LC_CTYPE, "C")) |
36477c24 | 722 | #endif /* USE_LOCALE_CTYPE */ |
723 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 724 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
36477c24 | 725 | #endif /* USE_LOCALE_COLLATE */ |
726 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 727 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
36477c24 | 728 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 729 | ) |
730 | { | |
731 | if (locwarn) | |
bbce6d69 | 732 | PerlIO_printf(PerlIO_stderr(), |
5f05dabc | 733 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
734 | ok = -1; | |
bbce6d69 | 735 | } |
5f05dabc | 736 | |
bbce6d69 | 737 | #endif /* ! LC_ALL */ |
5f05dabc | 738 | |
739 | #ifdef USE_LOCALE_CTYPE | |
740 | curctype = setlocale(LC_CTYPE, Nullch); | |
741 | #endif /* USE_LOCALE_CTYPE */ | |
742 | #ifdef USE_LOCALE_COLLATE | |
743 | curcoll = setlocale(LC_COLLATE, Nullch); | |
744 | #endif /* USE_LOCALE_COLLATE */ | |
745 | #ifdef USE_LOCALE_NUMERIC | |
746 | curnum = setlocale(LC_NUMERIC, Nullch); | |
747 | #endif /* USE_LOCALE_NUMERIC */ | |
ef7eada9 JH |
748 | } |
749 | ||
36477c24 | 750 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 751 | perl_new_ctype(curctype); |
36477c24 | 752 | #endif /* USE_LOCALE_CTYPE */ |
bbce6d69 | 753 | |
36477c24 | 754 | #ifdef USE_LOCALE_COLLATE |
bbce6d69 | 755 | perl_new_collate(curcoll); |
36477c24 | 756 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 757 | |
36477c24 | 758 | #ifdef USE_LOCALE_NUMERIC |
bbce6d69 | 759 | perl_new_numeric(curnum); |
36477c24 | 760 | #endif /* USE_LOCALE_NUMERIC */ |
ef7eada9 | 761 | |
36477c24 | 762 | #endif /* USE_LOCALE */ |
ef7eada9 | 763 | |
f0c5b223 TB |
764 | return ok; |
765 | } | |
766 | ||
bbce6d69 | 767 | /* Backwards compatibility. */ |
768 | int | |
769 | perl_init_i18nl14n(printwarn) | |
770 | int printwarn; | |
771 | { | |
5f05dabc | 772 | return perl_init_i18nl10n(printwarn); |
bbce6d69 | 773 | } |
ef7eada9 | 774 | |
36477c24 | 775 | #ifdef USE_LOCALE_COLLATE |
ef7eada9 | 776 | |
bbce6d69 | 777 | /* |
778 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
779 | * differences. First, it handles embedded NULs. Second, it allocates | |
780 | * a bit more memory than needed for the transformed data itself. | |
781 | * The real transformed data begins at offset sizeof(collationix). | |
782 | * Please see sv_collxfrm() to see how this is used. | |
783 | */ | |
784 | char * | |
785 | mem_collxfrm(s, len, xlen) | |
786 | const char *s; | |
787 | STRLEN len; | |
788 | STRLEN *xlen; | |
789 | { | |
790 | char *xbuf; | |
791 | STRLEN xalloc, xin, xout; | |
792 | ||
793 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ | |
794 | /* the +1 is for the terminating NUL. */ | |
795 | ||
796 | xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1; | |
797 | New(171, xbuf, xalloc, char); | |
798 | if (! xbuf) | |
799 | goto bad; | |
800 | ||
801 | *(U32*)xbuf = collation_ix; | |
802 | xout = sizeof(collation_ix); | |
803 | for (xin = 0; xin < len; ) { | |
804 | SSize_t xused; | |
805 | ||
806 | for (;;) { | |
807 | xused = strxfrm(xbuf + xout, s + xin, xalloc - xout); | |
808 | if (xused == -1) | |
809 | goto bad; | |
810 | if (xused < xalloc - xout) | |
811 | break; | |
812 | xalloc = (2 * xalloc) + 1; | |
813 | Renew(xbuf, xalloc, char); | |
814 | if (! xbuf) | |
815 | goto bad; | |
816 | } | |
ef7eada9 | 817 | |
bbce6d69 | 818 | xin += strlen(s + xin) + 1; |
819 | xout += xused; | |
820 | ||
821 | /* Embedded NULs are understood but silently skipped | |
822 | * because they make no sense in locale collation. */ | |
823 | } | |
ef7eada9 | 824 | |
bbce6d69 | 825 | xbuf[xout] = '\0'; |
826 | *xlen = xout - sizeof(collation_ix); | |
827 | return xbuf; | |
828 | ||
829 | bad: | |
830 | Safefree(xbuf); | |
831 | *xlen = 0; | |
832 | return NULL; | |
ef7eada9 JH |
833 | } |
834 | ||
36477c24 | 835 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 836 | |
378cc40b | 837 | void |
bbce6d69 | 838 | fbm_compile(sv) |
79072805 | 839 | SV *sv; |
378cc40b | 840 | { |
a687059c LW |
841 | register unsigned char *s; |
842 | register unsigned char *table; | |
79072805 LW |
843 | register U32 i; |
844 | register U32 len = SvCUR(sv); | |
845 | I32 rarest = 0; | |
846 | U32 frequency = 256; | |
847 | ||
748a9306 LW |
848 | if (len > 255) |
849 | return; /* can't have offsets that big */ | |
79072805 | 850 | Sv_Grow(sv,len+258); |
463ee0b2 | 851 | table = (unsigned char*)(SvPVX(sv) + len + 1); |
a687059c LW |
852 | s = table - 2; |
853 | for (i = 0; i < 256; i++) { | |
378cc40b LW |
854 | table[i] = len; |
855 | } | |
856 | i = 0; | |
463ee0b2 | 857 | while (s >= (unsigned char*)(SvPVX(sv))) |
a687059c | 858 | { |
bbce6d69 | 859 | if (table[*s] == len) |
860 | table[*s] = i; | |
378cc40b LW |
861 | s--,i++; |
862 | } | |
79072805 | 863 | sv_upgrade(sv, SVt_PVBM); |
bbce6d69 | 864 | sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */ |
79072805 | 865 | SvVALID_on(sv); |
378cc40b | 866 | |
463ee0b2 | 867 | s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
bbce6d69 | 868 | for (i = 0; i < len; i++) { |
869 | if (freq[s[i]] < frequency) { | |
870 | rarest = i; | |
871 | frequency = freq[s[i]]; | |
378cc40b LW |
872 | } |
873 | } | |
79072805 LW |
874 | BmRARE(sv) = s[rarest]; |
875 | BmPREVIOUS(sv) = rarest; | |
760ac839 | 876 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv))); |
378cc40b LW |
877 | } |
878 | ||
378cc40b | 879 | char * |
79072805 | 880 | fbm_instr(big, bigend, littlestr) |
a687059c LW |
881 | unsigned char *big; |
882 | register unsigned char *bigend; | |
79072805 | 883 | SV *littlestr; |
378cc40b | 884 | { |
a687059c | 885 | register unsigned char *s; |
79072805 LW |
886 | register I32 tmp; |
887 | register I32 littlelen; | |
a687059c LW |
888 | register unsigned char *little; |
889 | register unsigned char *table; | |
890 | register unsigned char *olds; | |
891 | register unsigned char *oldlittle; | |
378cc40b | 892 | |
79072805 | 893 | if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
a0d0e21e LW |
894 | STRLEN len; |
895 | char *l = SvPV(littlestr,len); | |
896 | if (!len) | |
d48672a2 | 897 | return (char*)big; |
a0d0e21e | 898 | return ninstr((char*)big,(char*)bigend, l, l + len); |
d48672a2 | 899 | } |
378cc40b | 900 | |
79072805 LW |
901 | littlelen = SvCUR(littlestr); |
902 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ | |
0f85fab0 LW |
903 | if (littlelen > bigend - big) |
904 | return Nullch; | |
463ee0b2 | 905 | little = (unsigned char*)SvPVX(littlestr); |
bbce6d69 | 906 | s = bigend - littlelen; |
36477c24 | 907 | if (*s == *little && memEQ((char*)s,(char*)little,littlelen)) |
bbce6d69 | 908 | return (char*)s; /* how sweet it is */ |
909 | else if (bigend[-1] == '\n' && little[littlelen-1] != '\n' | |
910 | && s > big) { | |
911 | s--; | |
36477c24 | 912 | if (*s == *little && memEQ((char*)s,(char*)little,littlelen)) |
bbce6d69 | 913 | return (char*)s; |
a687059c | 914 | } |
bbce6d69 | 915 | return Nullch; |
a687059c | 916 | } |
463ee0b2 | 917 | table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1); |
62b28dd9 LW |
918 | if (--littlelen >= bigend - big) |
919 | return Nullch; | |
920 | s = big + littlelen; | |
a687059c | 921 | oldlittle = little = table - 2; |
bbce6d69 | 922 | if (s < bigend) { |
923 | top2: | |
924 | /*SUPPRESS 560*/ | |
925 | if (tmp = table[*s]) { | |
62b28dd9 | 926 | #ifdef POINTERRIGOR |
bbce6d69 | 927 | if (bigend - s > tmp) { |
928 | s += tmp; | |
929 | goto top2; | |
930 | } | |
62b28dd9 | 931 | #else |
bbce6d69 | 932 | if ((s += tmp) < bigend) |
933 | goto top2; | |
62b28dd9 | 934 | #endif |
bbce6d69 | 935 | return Nullch; |
a687059c | 936 | } |
bbce6d69 | 937 | else { |
938 | tmp = littlelen; /* less expensive than calling strncmp() */ | |
939 | olds = s; | |
940 | while (tmp--) { | |
941 | if (*--s == *--little) | |
942 | continue; | |
943 | s = olds + 1; /* here we pay the price for failure */ | |
944 | little = oldlittle; | |
945 | if (s < bigend) /* fake up continue to outer loop */ | |
62b28dd9 | 946 | goto top2; |
62b28dd9 | 947 | return Nullch; |
a687059c | 948 | } |
bbce6d69 | 949 | return (char *)s; |
378cc40b LW |
950 | } |
951 | } | |
952 | return Nullch; | |
953 | } | |
954 | ||
955 | char * | |
956 | screaminstr(bigstr, littlestr) | |
79072805 LW |
957 | SV *bigstr; |
958 | SV *littlestr; | |
378cc40b | 959 | { |
a687059c LW |
960 | register unsigned char *s, *x; |
961 | register unsigned char *big; | |
79072805 LW |
962 | register I32 pos; |
963 | register I32 previous; | |
964 | register I32 first; | |
a687059c LW |
965 | register unsigned char *little; |
966 | register unsigned char *bigend; | |
967 | register unsigned char *littleend; | |
378cc40b | 968 | |
79072805 | 969 | if ((pos = screamfirst[BmRARE(littlestr)]) < 0) |
378cc40b | 970 | return Nullch; |
463ee0b2 | 971 | little = (unsigned char *)(SvPVX(littlestr)); |
79072805 | 972 | littleend = little + SvCUR(littlestr); |
378cc40b | 973 | first = *little++; |
79072805 | 974 | previous = BmPREVIOUS(littlestr); |
463ee0b2 | 975 | big = (unsigned char *)(SvPVX(bigstr)); |
79072805 | 976 | bigend = big + SvCUR(bigstr); |
378cc40b LW |
977 | while (pos < previous) { |
978 | if (!(pos += screamnext[pos])) | |
979 | return Nullch; | |
980 | } | |
de3bb511 | 981 | #ifdef POINTERRIGOR |
bbce6d69 | 982 | do { |
983 | if (big[pos-previous] != first) | |
984 | continue; | |
985 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { | |
986 | if (x >= bigend) | |
987 | return Nullch; | |
988 | if (*s++ != *x++) { | |
989 | s--; | |
990 | break; | |
de3bb511 | 991 | } |
bbce6d69 | 992 | } |
993 | if (s == littleend) | |
994 | return (char *)(big+pos-previous); | |
995 | } while ( pos += screamnext[pos] ); | |
de3bb511 LW |
996 | #else /* !POINTERRIGOR */ |
997 | big -= previous; | |
bbce6d69 | 998 | do { |
999 | if (big[pos] != first) | |
1000 | continue; | |
1001 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { | |
1002 | if (x >= bigend) | |
1003 | return Nullch; | |
1004 | if (*s++ != *x++) { | |
1005 | s--; | |
1006 | break; | |
378cc40b | 1007 | } |
bbce6d69 | 1008 | } |
1009 | if (s == littleend) | |
1010 | return (char *)(big+pos); | |
1011 | } while ( pos += screamnext[pos] ); | |
de3bb511 | 1012 | #endif /* POINTERRIGOR */ |
8d063cd8 LW |
1013 | return Nullch; |
1014 | } | |
1015 | ||
79072805 | 1016 | I32 |
bbce6d69 | 1017 | ibcmp(s1, s2, len) |
1018 | char *s1, *s2; | |
79072805 LW |
1019 | register I32 len; |
1020 | { | |
bbce6d69 | 1021 | register U8 *a = (U8 *)s1; |
1022 | register U8 *b = (U8 *)s2; | |
79072805 | 1023 | while (len--) { |
bbce6d69 | 1024 | if (*a != *b && *a != fold[*b]) |
1025 | return 1; | |
1026 | a++,b++; | |
1027 | } | |
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | I32 | |
1032 | ibcmp_locale(s1, s2, len) | |
1033 | char *s1, *s2; | |
1034 | register I32 len; | |
1035 | { | |
1036 | register U8 *a = (U8 *)s1; | |
1037 | register U8 *b = (U8 *)s2; | |
1038 | while (len--) { | |
1039 | if (*a != *b && *a != fold_locale[*b]) | |
1040 | return 1; | |
1041 | a++,b++; | |
79072805 LW |
1042 | } |
1043 | return 0; | |
1044 | } | |
1045 | ||
8d063cd8 LW |
1046 | /* copy a string to a safe spot */ |
1047 | ||
1048 | char * | |
a0d0e21e | 1049 | savepv(sv) |
79072805 | 1050 | char *sv; |
8d063cd8 | 1051 | { |
a687059c | 1052 | register char *newaddr; |
8d063cd8 | 1053 | |
79072805 LW |
1054 | New(902,newaddr,strlen(sv)+1,char); |
1055 | (void)strcpy(newaddr,sv); | |
8d063cd8 LW |
1056 | return newaddr; |
1057 | } | |
1058 | ||
a687059c LW |
1059 | /* same thing but with a known length */ |
1060 | ||
1061 | char * | |
a0d0e21e | 1062 | savepvn(sv, len) |
79072805 LW |
1063 | char *sv; |
1064 | register I32 len; | |
a687059c LW |
1065 | { |
1066 | register char *newaddr; | |
1067 | ||
1068 | New(903,newaddr,len+1,char); | |
79072805 | 1069 | Copy(sv,newaddr,len,char); /* might not be null terminated */ |
a687059c LW |
1070 | newaddr[len] = '\0'; /* is now */ |
1071 | return newaddr; | |
1072 | } | |
1073 | ||
a0d0e21e | 1074 | #ifdef I_STDARG |
8990e307 | 1075 | char * |
71be2cbc | 1076 | mess(const char *pat, va_list *args) |
a687059c LW |
1077 | #else |
1078 | /*VARARGS0*/ | |
de3bb511 | 1079 | char * |
8990e307 | 1080 | mess(pat, args) |
71be2cbc | 1081 | const char *pat; |
2304df62 | 1082 | va_list *args; |
8990e307 LW |
1083 | #endif |
1084 | { | |
a687059c | 1085 | char *s; |
f0c5b223 | 1086 | char *s_start; |
79072805 LW |
1087 | SV *tmpstr; |
1088 | I32 usermess; | |
d48672a2 | 1089 | #ifndef HAS_VPRINTF |
85e6fe83 | 1090 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1091 | char *vsprintf(); |
1092 | #else | |
79072805 | 1093 | I32 vsprintf(); |
a687059c | 1094 | #endif |
d48672a2 | 1095 | #endif |
a687059c | 1096 | |
f0c5b223 | 1097 | s = s_start = buf; |
de3bb511 LW |
1098 | usermess = strEQ(pat, "%s"); |
1099 | if (usermess) { | |
8990e307 | 1100 | tmpstr = sv_newmortal(); |
2304df62 | 1101 | sv_setpv(tmpstr, va_arg(*args, char *)); |
8ebc5c01 | 1102 | *s++ = SvCUR(tmpstr) ? SvPVX(tmpstr)[SvCUR(tmpstr)-1] : ' '; |
de3bb511 LW |
1103 | } |
1104 | else { | |
2304df62 | 1105 | (void) vsprintf(s,pat,*args); |
de3bb511 LW |
1106 | s += strlen(s); |
1107 | } | |
2304df62 | 1108 | va_end(*args); |
a687059c | 1109 | |
5f05dabc | 1110 | if (!(s > s_start && s[-1] == '\n')) { |
2304df62 AD |
1111 | if (dirty) |
1112 | strcpy(s, " during global destruction.\n"); | |
1113 | else { | |
1114 | if (curcop->cop_line) { | |
1115 | (void)sprintf(s," at %s line %ld", | |
1116 | SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line); | |
1117 | s += strlen(s); | |
1118 | } | |
c07a80fd | 1119 | if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) { |
1120 | bool line_mode = (RsSIMPLE(rs) && | |
1121 | SvLEN(rs) == 1 && *SvPVX(rs) == '\n'); | |
2304df62 AD |
1122 | (void)sprintf(s,", <%s> %s %ld", |
1123 | last_in_gv == argvgv ? "" : GvNAME(last_in_gv), | |
c07a80fd | 1124 | line_mode ? "line" : "chunk", |
a0d0e21e | 1125 | (long)IoLINES(GvIOp(last_in_gv))); |
2304df62 AD |
1126 | s += strlen(s); |
1127 | } | |
1128 | (void)strcpy(s,".\n"); | |
f0c5b223 | 1129 | s += 2; |
a687059c | 1130 | } |
de3bb511 | 1131 | if (usermess) |
79072805 | 1132 | sv_catpv(tmpstr,buf+1); |
a687059c | 1133 | } |
de3bb511 | 1134 | |
f0c5b223 TB |
1135 | if (s - s_start >= sizeof(buf)) { /* Ooops! */ |
1136 | if (usermess) | |
760ac839 | 1137 | PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr)); |
f0c5b223 | 1138 | else |
760ac839 LW |
1139 | PerlIO_puts(PerlIO_stderr(), buf); |
1140 | PerlIO_puts(PerlIO_stderr(), "panic: message overflow - memory corrupted!\n"); | |
f0c5b223 TB |
1141 | my_exit(1); |
1142 | } | |
de3bb511 | 1143 | if (usermess) |
463ee0b2 | 1144 | return SvPVX(tmpstr); |
de3bb511 LW |
1145 | else |
1146 | return buf; | |
a687059c LW |
1147 | } |
1148 | ||
ecfc5424 | 1149 | #ifdef I_STDARG |
36477c24 | 1150 | OP * |
71be2cbc | 1151 | die(const char* pat, ...) |
36477c24 | 1152 | #else |
1153 | /*VARARGS0*/ | |
1154 | OP * | |
1155 | die(pat, va_alist) | |
71be2cbc | 1156 | const char *pat; |
36477c24 | 1157 | va_dcl |
1158 | #endif | |
1159 | { | |
1160 | va_list args; | |
1161 | char *message; | |
1162 | int oldrunlevel = runlevel; | |
1163 | int was_in_eval = in_eval; | |
1164 | HV *stash; | |
1165 | GV *gv; | |
1166 | CV *cv; | |
1167 | ||
1168 | /* We have to switch back to mainstack or die_where may try to pop | |
1169 | * the eval block from the wrong stack if die is being called from a | |
1170 | * signal handler. - dkindred@cs.cmu.edu */ | |
1171 | if (curstack != mainstack) { | |
1172 | dSP; | |
1173 | SWITCHSTACK(curstack, mainstack); | |
1174 | } | |
1175 | ||
1176 | #ifdef I_STDARG | |
1177 | va_start(args, pat); | |
1178 | #else | |
1179 | va_start(args); | |
1180 | #endif | |
1181 | message = mess(pat, &args); | |
1182 | va_end(args); | |
1183 | ||
1184 | if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) { | |
1185 | dSP; | |
1186 | SV *msg = sv_2mortal(newSVpv(message, 0)); | |
1187 | ||
1188 | PUSHMARK(sp); | |
1189 | EXTEND(sp, 1); | |
1190 | PUSHs(msg); | |
1191 | PUTBACK; | |
1192 | perl_call_sv((SV*)cv, G_DISCARD); | |
1193 | ||
1194 | /* It's okay for the __DIE__ hook to modify the message. */ | |
1195 | message = SvPV(msg, na); | |
1196 | } | |
1197 | ||
1198 | restartop = die_where(message); | |
1199 | if ((!restartop && was_in_eval) || oldrunlevel > 1) | |
1200 | Siglongjmp(top_env, 3); | |
1201 | return restartop; | |
1202 | } | |
1203 | ||
1204 | #ifdef I_STDARG | |
79072805 | 1205 | void |
71be2cbc | 1206 | croak(const char* pat, ...) |
463ee0b2 | 1207 | #else |
8990e307 LW |
1208 | /*VARARGS0*/ |
1209 | void | |
1210 | croak(pat, va_alist) | |
1211 | char *pat; | |
1212 | va_dcl | |
463ee0b2 | 1213 | #endif |
a687059c LW |
1214 | { |
1215 | va_list args; | |
de3bb511 | 1216 | char *message; |
748a9306 LW |
1217 | HV *stash; |
1218 | GV *gv; | |
1219 | CV *cv; | |
a687059c | 1220 | |
a0d0e21e | 1221 | #ifdef I_STDARG |
8990e307 LW |
1222 | va_start(args, pat); |
1223 | #else | |
a687059c | 1224 | va_start(args); |
8990e307 | 1225 | #endif |
2304df62 | 1226 | message = mess(pat, &args); |
a687059c | 1227 | va_end(args); |
20cec16a | 1228 | if (diehook) { |
1229 | SV *olddiehook = diehook; | |
1230 | diehook = Nullsv; /* sv_2cv might call croak() */ | |
1231 | cv = sv_2cv(olddiehook, &stash, &gv, 0); | |
1232 | diehook = olddiehook; | |
1233 | if (cv && !CvDEPTH(cv)) { | |
1234 | dSP; | |
36477c24 | 1235 | SV *msg = sv_2mortal(newSVpv(message, 0)); |
20cec16a | 1236 | |
1237 | PUSHMARK(sp); | |
1238 | EXTEND(sp, 1); | |
36477c24 | 1239 | PUSHs(msg); |
20cec16a | 1240 | PUTBACK; |
1241 | perl_call_sv((SV*)cv, G_DISCARD); | |
36477c24 | 1242 | |
1243 | /* It's okay for the __DIE__ hook to modify the message. */ | |
1244 | message = SvPV(msg, na); | |
20cec16a | 1245 | } |
748a9306 | 1246 | } |
a0d0e21e LW |
1247 | if (in_eval) { |
1248 | restartop = die_where(message); | |
a5f75d66 | 1249 | Siglongjmp(top_env, 3); |
a0d0e21e | 1250 | } |
760ac839 LW |
1251 | PerlIO_puts(PerlIO_stderr(),message); |
1252 | (void)PerlIO_flush(PerlIO_stderr()); | |
38cd9116 | 1253 | if (e_tmpname) { |
1254 | if (e_fp) { | |
760ac839 | 1255 | PerlIO_close(e_fp); |
38cd9116 | 1256 | e_fp = Nullfp; |
1257 | } | |
a687059c | 1258 | (void)UNLINK(e_tmpname); |
38cd9116 | 1259 | Safefree(e_tmpname); |
1260 | e_tmpname = Nullch; | |
f0c5b223 | 1261 | } |
748a9306 LW |
1262 | statusvalue = SHIFTSTATUS(statusvalue); |
1263 | #ifdef VMS | |
1264 | my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44))); | |
1265 | #else | |
1266 | my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255))); | |
1267 | #endif | |
a687059c LW |
1268 | } |
1269 | ||
8990e307 | 1270 | void |
ecfc5424 | 1271 | #ifdef I_STDARG |
71be2cbc | 1272 | warn(const char* pat,...) |
463ee0b2 | 1273 | #else |
8990e307 LW |
1274 | /*VARARGS0*/ |
1275 | warn(pat,va_alist) | |
71be2cbc | 1276 | const char *pat; |
8990e307 | 1277 | va_dcl |
463ee0b2 | 1278 | #endif |
a687059c LW |
1279 | { |
1280 | va_list args; | |
de3bb511 | 1281 | char *message; |
748a9306 LW |
1282 | HV *stash; |
1283 | GV *gv; | |
1284 | CV *cv; | |
a687059c | 1285 | |
a0d0e21e | 1286 | #ifdef I_STDARG |
8990e307 LW |
1287 | va_start(args, pat); |
1288 | #else | |
a687059c | 1289 | va_start(args); |
8990e307 | 1290 | #endif |
2304df62 | 1291 | message = mess(pat, &args); |
a687059c LW |
1292 | va_end(args); |
1293 | ||
20cec16a | 1294 | if (warnhook) { |
1295 | SV *oldwarnhook = warnhook; | |
1296 | warnhook = Nullsv; /* sv_2cv might end up calling warn() */ | |
1297 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); | |
1298 | warnhook = oldwarnhook; | |
1299 | if (cv && !CvDEPTH(cv)) { | |
1300 | dSP; | |
1301 | ||
1302 | PUSHMARK(sp); | |
1303 | EXTEND(sp, 1); | |
1304 | PUSHs(sv_2mortal(newSVpv(message,0))); | |
1305 | PUTBACK; | |
1306 | perl_call_sv((SV*)cv, G_DISCARD); | |
1307 | return; | |
1308 | } | |
748a9306 | 1309 | } |
20cec16a | 1310 | PerlIO_puts(PerlIO_stderr(),message); |
a687059c | 1311 | #ifdef LEAKTEST |
20cec16a | 1312 | DEBUG_L(xstat()); |
a687059c | 1313 | #endif |
20cec16a | 1314 | (void)PerlIO_flush(PerlIO_stderr()); |
a687059c | 1315 | } |
8d063cd8 | 1316 | |
a0d0e21e | 1317 | #ifndef VMS /* VMS' my_setenv() is in VMS.c */ |
8d063cd8 | 1318 | void |
7c0587c8 | 1319 | my_setenv(nam,val) |
8d063cd8 LW |
1320 | char *nam, *val; |
1321 | { | |
79072805 | 1322 | register I32 i=setenv_getix(nam); /* where does it go? */ |
8d063cd8 | 1323 | |
fe14fcc3 | 1324 | if (environ == origenviron) { /* need we copy environment? */ |
79072805 LW |
1325 | I32 j; |
1326 | I32 max; | |
fe14fcc3 LW |
1327 | char **tmpenv; |
1328 | ||
de3bb511 | 1329 | /*SUPPRESS 530*/ |
fe14fcc3 LW |
1330 | for (max = i; environ[max]; max++) ; |
1331 | New(901,tmpenv, max+2, char*); | |
1332 | for (j=0; j<max; j++) /* copy environment */ | |
a0d0e21e | 1333 | tmpenv[j] = savepv(environ[j]); |
fe14fcc3 LW |
1334 | tmpenv[max] = Nullch; |
1335 | environ = tmpenv; /* tell exec where it is now */ | |
1336 | } | |
a687059c LW |
1337 | if (!val) { |
1338 | while (environ[i]) { | |
1339 | environ[i] = environ[i+1]; | |
1340 | i++; | |
1341 | } | |
1342 | return; | |
1343 | } | |
8d063cd8 | 1344 | if (!environ[i]) { /* does not exist yet */ |
fe14fcc3 | 1345 | Renew(environ, i+2, char*); /* just expand it a bit */ |
8d063cd8 LW |
1346 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
1347 | } | |
fe14fcc3 LW |
1348 | else |
1349 | Safefree(environ[i]); | |
a687059c | 1350 | New(904, environ[i], strlen(nam) + strlen(val) + 2, char); |
62b28dd9 | 1351 | #ifndef MSDOS |
a687059c | 1352 | (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
62b28dd9 LW |
1353 | #else |
1354 | /* MS-DOS requires environment variable names to be in uppercase */ | |
fe14fcc3 LW |
1355 | /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but |
1356 | * some utilities and applications may break because they only look | |
1357 | * for upper case strings. (Fixed strupr() bug here.)] | |
1358 | */ | |
1359 | strcpy(environ[i],nam); strupr(environ[i]); | |
62b28dd9 LW |
1360 | (void)sprintf(environ[i] + strlen(nam),"=%s",val); |
1361 | #endif /* MSDOS */ | |
8d063cd8 LW |
1362 | } |
1363 | ||
79072805 LW |
1364 | I32 |
1365 | setenv_getix(nam) | |
8d063cd8 LW |
1366 | char *nam; |
1367 | { | |
79072805 | 1368 | register I32 i, len = strlen(nam); |
8d063cd8 LW |
1369 | |
1370 | for (i = 0; environ[i]; i++) { | |
1371 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') | |
1372 | break; /* strnEQ must come first to avoid */ | |
1373 | } /* potential SEGV's */ | |
1374 | return i; | |
1375 | } | |
a0d0e21e | 1376 | #endif /* !VMS */ |
378cc40b | 1377 | |
16d20bd9 | 1378 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 1379 | I32 |
378cc40b LW |
1380 | unlnk(f) /* unlink all versions of a file */ |
1381 | char *f; | |
1382 | { | |
79072805 | 1383 | I32 i; |
378cc40b LW |
1384 | |
1385 | for (i = 0; unlink(f) >= 0; i++) ; | |
1386 | return i ? 0 : -1; | |
1387 | } | |
1388 | #endif | |
1389 | ||
85e6fe83 | 1390 | #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY) |
378cc40b | 1391 | char * |
7c0587c8 | 1392 | my_bcopy(from,to,len) |
378cc40b LW |
1393 | register char *from; |
1394 | register char *to; | |
79072805 | 1395 | register I32 len; |
378cc40b LW |
1396 | { |
1397 | char *retval = to; | |
1398 | ||
7c0587c8 LW |
1399 | if (from - to >= 0) { |
1400 | while (len--) | |
1401 | *to++ = *from++; | |
1402 | } | |
1403 | else { | |
1404 | to += len; | |
1405 | from += len; | |
1406 | while (len--) | |
faf8582f | 1407 | *(--to) = *(--from); |
7c0587c8 | 1408 | } |
378cc40b LW |
1409 | return retval; |
1410 | } | |
ffed7fef | 1411 | #endif |
378cc40b | 1412 | |
7c0587c8 | 1413 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 1414 | char * |
7c0587c8 | 1415 | my_bzero(loc,len) |
378cc40b | 1416 | register char *loc; |
79072805 | 1417 | register I32 len; |
378cc40b LW |
1418 | { |
1419 | char *retval = loc; | |
1420 | ||
1421 | while (len--) | |
1422 | *loc++ = 0; | |
1423 | return retval; | |
1424 | } | |
1425 | #endif | |
7c0587c8 | 1426 | |
36477c24 | 1427 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 1428 | I32 |
7c0587c8 | 1429 | my_memcmp(s1,s2,len) |
36477c24 | 1430 | char *s1; |
1431 | char *s2; | |
79072805 | 1432 | register I32 len; |
7c0587c8 | 1433 | { |
36477c24 | 1434 | register U8 *a = (U8 *)s1; |
1435 | register U8 *b = (U8 *)s2; | |
79072805 | 1436 | register I32 tmp; |
7c0587c8 LW |
1437 | |
1438 | while (len--) { | |
36477c24 | 1439 | if (tmp = *a++ - *b++) |
7c0587c8 LW |
1440 | return tmp; |
1441 | } | |
1442 | return 0; | |
1443 | } | |
36477c24 | 1444 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 1445 | |
4633a7c4 | 1446 | #if defined(I_STDARG) || defined(I_VARARGS) |
fe14fcc3 | 1447 | #ifndef HAS_VPRINTF |
a687059c | 1448 | |
85e6fe83 | 1449 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1450 | char * |
1451 | #else | |
1452 | int | |
1453 | #endif | |
1454 | vsprintf(dest, pat, args) | |
71be2cbc | 1455 | char *dest; |
1456 | const char *pat; | |
1457 | char *args; | |
a687059c LW |
1458 | { |
1459 | FILE fakebuf; | |
1460 | ||
1461 | fakebuf._ptr = dest; | |
1462 | fakebuf._cnt = 32767; | |
35c8bce7 LW |
1463 | #ifndef _IOSTRG |
1464 | #define _IOSTRG 0 | |
1465 | #endif | |
a687059c LW |
1466 | fakebuf._flag = _IOWRT|_IOSTRG; |
1467 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
1468 | (void)putc('\0', &fakebuf); | |
85e6fe83 | 1469 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1470 | return(dest); |
1471 | #else | |
1472 | return 0; /* perl doesn't use return value */ | |
1473 | #endif | |
1474 | } | |
1475 | ||
fe14fcc3 | 1476 | #endif /* HAS_VPRINTF */ |
4633a7c4 | 1477 | #endif /* I_VARARGS || I_STDARGS */ |
a687059c LW |
1478 | |
1479 | #ifdef MYSWAP | |
ffed7fef | 1480 | #if BYTEORDER != 0x4321 |
a687059c | 1481 | short |
748a9306 | 1482 | #ifndef CAN_PROTOTYPE |
a687059c LW |
1483 | my_swap(s) |
1484 | short s; | |
748a9306 LW |
1485 | #else |
1486 | my_swap(short s) | |
1487 | #endif | |
a687059c LW |
1488 | { |
1489 | #if (BYTEORDER & 1) == 0 | |
1490 | short result; | |
1491 | ||
1492 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
1493 | return result; | |
1494 | #else | |
1495 | return s; | |
1496 | #endif | |
1497 | } | |
1498 | ||
1499 | long | |
748a9306 LW |
1500 | #ifndef CAN_PROTOTYPE |
1501 | my_htonl(l) | |
a687059c | 1502 | register long l; |
748a9306 LW |
1503 | #else |
1504 | my_htonl(long l) | |
1505 | #endif | |
a687059c LW |
1506 | { |
1507 | union { | |
1508 | long result; | |
ffed7fef | 1509 | char c[sizeof(long)]; |
a687059c LW |
1510 | } u; |
1511 | ||
ffed7fef | 1512 | #if BYTEORDER == 0x1234 |
a687059c LW |
1513 | u.c[0] = (l >> 24) & 255; |
1514 | u.c[1] = (l >> 16) & 255; | |
1515 | u.c[2] = (l >> 8) & 255; | |
1516 | u.c[3] = l & 255; | |
1517 | return u.result; | |
1518 | #else | |
ffed7fef | 1519 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1520 | croak("Unknown BYTEORDER\n"); |
a687059c | 1521 | #else |
79072805 LW |
1522 | register I32 o; |
1523 | register I32 s; | |
a687059c | 1524 | |
ffed7fef LW |
1525 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1526 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
1527 | } |
1528 | return u.result; | |
1529 | #endif | |
1530 | #endif | |
1531 | } | |
1532 | ||
1533 | long | |
748a9306 LW |
1534 | #ifndef CAN_PROTOTYPE |
1535 | my_ntohl(l) | |
a687059c | 1536 | register long l; |
748a9306 LW |
1537 | #else |
1538 | my_ntohl(long l) | |
1539 | #endif | |
a687059c LW |
1540 | { |
1541 | union { | |
1542 | long l; | |
ffed7fef | 1543 | char c[sizeof(long)]; |
a687059c LW |
1544 | } u; |
1545 | ||
ffed7fef | 1546 | #if BYTEORDER == 0x1234 |
a687059c LW |
1547 | u.c[0] = (l >> 24) & 255; |
1548 | u.c[1] = (l >> 16) & 255; | |
1549 | u.c[2] = (l >> 8) & 255; | |
1550 | u.c[3] = l & 255; | |
1551 | return u.l; | |
1552 | #else | |
ffed7fef | 1553 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1554 | croak("Unknown BYTEORDER\n"); |
a687059c | 1555 | #else |
79072805 LW |
1556 | register I32 o; |
1557 | register I32 s; | |
a687059c LW |
1558 | |
1559 | u.l = l; | |
1560 | l = 0; | |
ffed7fef LW |
1561 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1562 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
1563 | } |
1564 | return l; | |
1565 | #endif | |
1566 | #endif | |
1567 | } | |
1568 | ||
ffed7fef | 1569 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
1570 | #endif /* MYSWAP */ |
1571 | ||
1572 | /* | |
1573 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
1574 | * If these functions are defined, | |
1575 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
1576 | * However, this is not assumed. | |
1577 | * -DWS | |
1578 | */ | |
1579 | ||
1580 | #define HTOV(name,type) \ | |
1581 | type \ | |
1582 | name (n) \ | |
1583 | register type n; \ | |
1584 | { \ | |
1585 | union { \ | |
1586 | type value; \ | |
1587 | char c[sizeof(type)]; \ | |
1588 | } u; \ | |
79072805 LW |
1589 | register I32 i; \ |
1590 | register I32 s; \ | |
988174c1 LW |
1591 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
1592 | u.c[i] = (n >> s) & 0xFF; \ | |
1593 | } \ | |
1594 | return u.value; \ | |
1595 | } | |
1596 | ||
1597 | #define VTOH(name,type) \ | |
1598 | type \ | |
1599 | name (n) \ | |
1600 | register type n; \ | |
1601 | { \ | |
1602 | union { \ | |
1603 | type value; \ | |
1604 | char c[sizeof(type)]; \ | |
1605 | } u; \ | |
79072805 LW |
1606 | register I32 i; \ |
1607 | register I32 s; \ | |
988174c1 LW |
1608 | u.value = n; \ |
1609 | n = 0; \ | |
1610 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ | |
1611 | n += (u.c[i] & 0xFF) << s; \ | |
1612 | } \ | |
1613 | return n; \ | |
1614 | } | |
1615 | ||
1616 | #if defined(HAS_HTOVS) && !defined(htovs) | |
1617 | HTOV(htovs,short) | |
1618 | #endif | |
1619 | #if defined(HAS_HTOVL) && !defined(htovl) | |
1620 | HTOV(htovl,long) | |
1621 | #endif | |
1622 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
1623 | VTOH(vtohs,short) | |
1624 | #endif | |
1625 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
1626 | VTOH(vtohl,long) | |
1627 | #endif | |
a687059c | 1628 | |
5f05dabc | 1629 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
1630 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) | |
760ac839 | 1631 | PerlIO * |
79072805 | 1632 | my_popen(cmd,mode) |
a687059c LW |
1633 | char *cmd; |
1634 | char *mode; | |
1635 | { | |
1636 | int p[2]; | |
79072805 LW |
1637 | register I32 this, that; |
1638 | register I32 pid; | |
1639 | SV *sv; | |
55497cff | 1640 | I32 doexec = |
1641 | #ifdef AMIGAOS | |
1642 | 1; | |
1643 | #else | |
1644 | strNE(cmd,"-"); | |
1645 | #endif | |
a687059c | 1646 | |
ddcf38b7 IZ |
1647 | #ifdef OS2 |
1648 | if (doexec) { | |
1649 | return my_syspopen(cmd,mode); | |
1650 | } | |
1651 | #endif | |
a687059c LW |
1652 | if (pipe(p) < 0) |
1653 | return Nullfp; | |
1654 | this = (*mode == 'w'); | |
1655 | that = !this; | |
bbce6d69 | 1656 | if (doexec && tainting) { |
1657 | taint_env(); | |
1658 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 1659 | } |
a687059c LW |
1660 | while ((pid = (doexec?vfork():fork())) < 0) { |
1661 | if (errno != EAGAIN) { | |
1662 | close(p[this]); | |
1663 | if (!doexec) | |
463ee0b2 | 1664 | croak("Can't fork"); |
a687059c LW |
1665 | return Nullfp; |
1666 | } | |
1667 | sleep(5); | |
1668 | } | |
1669 | if (pid == 0) { | |
79072805 LW |
1670 | GV* tmpgv; |
1671 | ||
a687059c LW |
1672 | #define THIS that |
1673 | #define THAT this | |
1674 | close(p[THAT]); | |
1675 | if (p[THIS] != (*mode == 'r')) { | |
1676 | dup2(p[THIS], *mode == 'r'); | |
1677 | close(p[THIS]); | |
1678 | } | |
1679 | if (doexec) { | |
a0d0e21e | 1680 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
1681 | int fd; |
1682 | ||
1683 | #ifndef NOFILE | |
1684 | #define NOFILE 20 | |
1685 | #endif | |
d48672a2 | 1686 | for (fd = maxsysfd + 1; fd < NOFILE; fd++) |
ae986130 LW |
1687 | close(fd); |
1688 | #endif | |
a687059c LW |
1689 | do_exec(cmd); /* may or may not use the shell */ |
1690 | _exit(1); | |
1691 | } | |
de3bb511 | 1692 | /*SUPPRESS 560*/ |
85e6fe83 | 1693 | if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV)) |
79072805 | 1694 | sv_setiv(GvSV(tmpgv),(I32)getpid()); |
9f68db38 | 1695 | forkprocess = 0; |
463ee0b2 | 1696 | hv_clear(pidstatus); /* we have no children */ |
a687059c LW |
1697 | return Nullfp; |
1698 | #undef THIS | |
1699 | #undef THAT | |
1700 | } | |
62b28dd9 | 1701 | do_execfree(); /* free any memory malloced by child on vfork */ |
a687059c | 1702 | close(p[that]); |
62b28dd9 LW |
1703 | if (p[that] < p[this]) { |
1704 | dup2(p[this], p[that]); | |
1705 | close(p[this]); | |
1706 | p[this] = p[that]; | |
1707 | } | |
79072805 | 1708 | sv = *av_fetch(fdpid,p[this],TRUE); |
a0d0e21e | 1709 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 1710 | SvIVX(sv) = pid; |
a687059c | 1711 | forkprocess = pid; |
760ac839 | 1712 | return PerlIO_fdopen(p[this], mode); |
a687059c | 1713 | } |
7c0587c8 | 1714 | #else |
55497cff | 1715 | #if defined(atarist) || defined(DJGPP) |
7c0587c8 | 1716 | FILE *popen(); |
760ac839 | 1717 | PerlIO * |
79072805 | 1718 | my_popen(cmd,mode) |
7c0587c8 LW |
1719 | char *cmd; |
1720 | char *mode; | |
1721 | { | |
760ac839 | 1722 | /* Needs work for PerlIO ! */ |
55497cff | 1723 | /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */ |
1724 | return popen(PerlIO_exportFILE(cmd, 0), mode); | |
7c0587c8 LW |
1725 | } |
1726 | #endif | |
1727 | ||
1728 | #endif /* !DOSISH */ | |
a687059c | 1729 | |
748a9306 | 1730 | #ifdef DUMP_FDS |
79072805 | 1731 | dump_fds(s) |
ae986130 LW |
1732 | char *s; |
1733 | { | |
1734 | int fd; | |
1735 | struct stat tmpstatbuf; | |
1736 | ||
760ac839 | 1737 | PerlIO_printf(PerlIO_stderr(),"%s", s); |
ae986130 | 1738 | for (fd = 0; fd < 32; fd++) { |
a0d0e21e | 1739 | if (Fstat(fd,&tmpstatbuf) >= 0) |
760ac839 | 1740 | PerlIO_printf(PerlIO_stderr()," %d",fd); |
ae986130 | 1741 | } |
760ac839 | 1742 | PerlIO_printf(PerlIO_stderr(),"\n"); |
ae986130 LW |
1743 | } |
1744 | #endif | |
1745 | ||
fe14fcc3 | 1746 | #ifndef HAS_DUP2 |
fec02dd3 | 1747 | int |
a687059c LW |
1748 | dup2(oldfd,newfd) |
1749 | int oldfd; | |
1750 | int newfd; | |
1751 | { | |
a0d0e21e | 1752 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
1753 | if (oldfd == newfd) |
1754 | return oldfd; | |
62b28dd9 | 1755 | close(newfd); |
fec02dd3 | 1756 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 1757 | #else |
d48672a2 | 1758 | int fdtmp[256]; |
79072805 | 1759 | I32 fdx = 0; |
ae986130 LW |
1760 | int fd; |
1761 | ||
fe14fcc3 | 1762 | if (oldfd == newfd) |
fec02dd3 | 1763 | return oldfd; |
a687059c | 1764 | close(newfd); |
fec02dd3 | 1765 | while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */ |
ae986130 LW |
1766 | fdtmp[fdx++] = fd; |
1767 | while (fdx > 0) | |
1768 | close(fdtmp[--fdx]); | |
fec02dd3 | 1769 | return fd; |
62b28dd9 | 1770 | #endif |
a687059c LW |
1771 | } |
1772 | #endif | |
1773 | ||
ff68c719 | 1774 | |
1775 | #ifdef HAS_SIGACTION | |
1776 | ||
1777 | Sighandler_t | |
1778 | rsignal(signo, handler) | |
1779 | int signo; | |
1780 | Sighandler_t handler; | |
1781 | { | |
1782 | struct sigaction act, oact; | |
1783 | ||
1784 | act.sa_handler = handler; | |
1785 | sigemptyset(&act.sa_mask); | |
1786 | act.sa_flags = 0; | |
1787 | #ifdef SA_RESTART | |
1788 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
1789 | #endif | |
1790 | if (sigaction(signo, &act, &oact) == -1) | |
36477c24 | 1791 | return SIG_ERR; |
ff68c719 | 1792 | else |
36477c24 | 1793 | return oact.sa_handler; |
ff68c719 | 1794 | } |
1795 | ||
1796 | Sighandler_t | |
1797 | rsignal_state(signo) | |
1798 | int signo; | |
1799 | { | |
1800 | struct sigaction oact; | |
1801 | ||
1802 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
1803 | return SIG_ERR; | |
1804 | else | |
1805 | return oact.sa_handler; | |
1806 | } | |
1807 | ||
1808 | int | |
1809 | rsignal_save(signo, handler, save) | |
1810 | int signo; | |
1811 | Sighandler_t handler; | |
1812 | Sigsave_t *save; | |
1813 | { | |
1814 | struct sigaction act; | |
1815 | ||
1816 | act.sa_handler = handler; | |
1817 | sigemptyset(&act.sa_mask); | |
1818 | act.sa_flags = 0; | |
1819 | #ifdef SA_RESTART | |
1820 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
1821 | #endif | |
1822 | return sigaction(signo, &act, save); | |
1823 | } | |
1824 | ||
1825 | int | |
1826 | rsignal_restore(signo, save) | |
1827 | int signo; | |
1828 | Sigsave_t *save; | |
1829 | { | |
1830 | return sigaction(signo, save, (struct sigaction *)NULL); | |
1831 | } | |
1832 | ||
1833 | #else /* !HAS_SIGACTION */ | |
1834 | ||
1835 | Sighandler_t | |
1836 | rsignal(signo, handler) | |
1837 | int signo; | |
1838 | Sighandler_t handler; | |
1839 | { | |
1840 | return signal(signo, handler); | |
1841 | } | |
1842 | ||
1843 | static int sig_trapped; | |
1844 | ||
1845 | static | |
1846 | Signal_t | |
1847 | sig_trap(signo) | |
1848 | int signo; | |
1849 | { | |
1850 | sig_trapped++; | |
1851 | } | |
1852 | ||
1853 | Sighandler_t | |
1854 | rsignal_state(signo) | |
1855 | int signo; | |
1856 | { | |
1857 | Sighandler_t oldsig; | |
1858 | ||
1859 | sig_trapped = 0; | |
1860 | oldsig = signal(signo, sig_trap); | |
1861 | signal(signo, oldsig); | |
1862 | if (sig_trapped) | |
1863 | kill(getpid(), signo); | |
1864 | return oldsig; | |
1865 | } | |
1866 | ||
1867 | int | |
1868 | rsignal_save(signo, handler, save) | |
1869 | int signo; | |
1870 | Sighandler_t handler; | |
1871 | Sigsave_t *save; | |
1872 | { | |
1873 | *save = signal(signo, handler); | |
1874 | return (*save == SIG_ERR) ? -1 : 0; | |
1875 | } | |
1876 | ||
1877 | int | |
36477c24 | 1878 | rsignal_restore(signo, save) |
ff68c719 | 1879 | int signo; |
1880 | Sigsave_t *save; | |
1881 | { | |
1882 | return (signal(signo, *save) == SIG_ERR) ? -1 : 0; | |
1883 | } | |
1884 | ||
1885 | #endif /* !HAS_SIGACTION */ | |
1886 | ||
5f05dabc | 1887 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
1888 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) | |
79072805 LW |
1889 | I32 |
1890 | my_pclose(ptr) | |
760ac839 | 1891 | PerlIO *ptr; |
a687059c | 1892 | { |
ff68c719 | 1893 | Sigsave_t hstat, istat, qstat; |
a687059c | 1894 | int status; |
a0d0e21e | 1895 | SV **svp; |
20188a90 | 1896 | int pid; |
a687059c | 1897 | |
760ac839 | 1898 | svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE); |
748a9306 | 1899 | pid = (int)SvIVX(*svp); |
a0d0e21e LW |
1900 | SvREFCNT_dec(*svp); |
1901 | *svp = &sv_undef; | |
ddcf38b7 IZ |
1902 | #ifdef OS2 |
1903 | if (pid == -1) { /* Opened by popen. */ | |
1904 | return my_syspclose(ptr); | |
1905 | } | |
1906 | #endif | |
760ac839 | 1907 | PerlIO_close(ptr); |
7c0587c8 LW |
1908 | #ifdef UTS |
1909 | if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ | |
1910 | #endif | |
ff68c719 | 1911 | rsignal_save(SIGHUP, SIG_IGN, &hstat); |
1912 | rsignal_save(SIGINT, SIG_IGN, &istat); | |
1913 | rsignal_save(SIGQUIT, SIG_IGN, &qstat); | |
748a9306 LW |
1914 | do { |
1915 | pid = wait4pid(pid, &status, 0); | |
1916 | } while (pid == -1 && errno == EINTR); | |
ff68c719 | 1917 | rsignal_restore(SIGHUP, &hstat); |
1918 | rsignal_restore(SIGINT, &istat); | |
1919 | rsignal_restore(SIGQUIT, &qstat); | |
20188a90 LW |
1920 | return(pid < 0 ? pid : status); |
1921 | } | |
4633a7c4 LW |
1922 | #endif /* !DOSISH */ |
1923 | ||
1924 | #if !defined(DOSISH) || defined(OS2) | |
79072805 | 1925 | I32 |
20188a90 LW |
1926 | wait4pid(pid,statusp,flags) |
1927 | int pid; | |
1928 | int *statusp; | |
1929 | int flags; | |
1930 | { | |
79072805 LW |
1931 | SV *sv; |
1932 | SV** svp; | |
20188a90 LW |
1933 | char spid[16]; |
1934 | ||
1935 | if (!pid) | |
1936 | return -1; | |
20188a90 LW |
1937 | if (pid > 0) { |
1938 | sprintf(spid, "%d", pid); | |
79072805 LW |
1939 | svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE); |
1940 | if (svp && *svp != &sv_undef) { | |
463ee0b2 | 1941 | *statusp = SvIVX(*svp); |
748a9306 | 1942 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
1943 | return pid; |
1944 | } | |
1945 | } | |
1946 | else { | |
79072805 | 1947 | HE *entry; |
20188a90 | 1948 | |
79072805 LW |
1949 | hv_iterinit(pidstatus); |
1950 | if (entry = hv_iternext(pidstatus)) { | |
a0d0e21e | 1951 | pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
79072805 | 1952 | sv = hv_iterval(pidstatus,entry); |
463ee0b2 | 1953 | *statusp = SvIVX(sv); |
20188a90 | 1954 | sprintf(spid, "%d", pid); |
748a9306 | 1955 | (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
1956 | return pid; |
1957 | } | |
1958 | } | |
79072805 LW |
1959 | #ifdef HAS_WAITPID |
1960 | return waitpid(pid,statusp,flags); | |
1961 | #else | |
a0d0e21e LW |
1962 | #ifdef HAS_WAIT4 |
1963 | return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); | |
1964 | #else | |
1965 | { | |
1966 | I32 result; | |
1967 | if (flags) | |
1968 | croak("Can't do waitpid with flags"); | |
1969 | else { | |
1970 | while ((result = wait(statusp)) != pid && pid > 0 && result >= 0) | |
1971 | pidgone(result,*statusp); | |
1972 | if (result < 0) | |
1973 | *statusp = -1; | |
1974 | } | |
1975 | return result; | |
a687059c LW |
1976 | } |
1977 | #endif | |
20188a90 | 1978 | #endif |
a687059c | 1979 | } |
7c0587c8 | 1980 | #endif /* !DOSISH */ |
a687059c | 1981 | |
7c0587c8 | 1982 | void |
de3bb511 | 1983 | /*SUPPRESS 590*/ |
a687059c LW |
1984 | pidgone(pid,status) |
1985 | int pid; | |
1986 | int status; | |
1987 | { | |
79072805 | 1988 | register SV *sv; |
20188a90 | 1989 | char spid[16]; |
a687059c | 1990 | |
20188a90 | 1991 | sprintf(spid, "%d", pid); |
79072805 | 1992 | sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE); |
a0d0e21e | 1993 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 1994 | SvIVX(sv) = status; |
20188a90 | 1995 | return; |
a687059c LW |
1996 | } |
1997 | ||
55497cff | 1998 | #if defined(atarist) || defined(OS2) || defined(DJGPP) |
7c0587c8 | 1999 | int pclose(); |
ddcf38b7 IZ |
2000 | #ifdef HAS_FORK |
2001 | int /* Cannot prototype with I32 | |
2002 | in os2ish.h. */ | |
2003 | my_syspclose(ptr) | |
2004 | #else | |
79072805 LW |
2005 | I32 |
2006 | my_pclose(ptr) | |
ddcf38b7 | 2007 | #endif |
760ac839 | 2008 | PerlIO *ptr; |
a687059c | 2009 | { |
760ac839 LW |
2010 | /* Needs work for PerlIO ! */ |
2011 | FILE *f = PerlIO_findFILE(ptr); | |
2012 | I32 result = pclose(f); | |
2013 | PerlIO_releaseFILE(ptr,f); | |
2014 | return result; | |
a687059c | 2015 | } |
7c0587c8 | 2016 | #endif |
9f68db38 LW |
2017 | |
2018 | void | |
2019 | repeatcpy(to,from,len,count) | |
2020 | register char *to; | |
2021 | register char *from; | |
79072805 LW |
2022 | I32 len; |
2023 | register I32 count; | |
9f68db38 | 2024 | { |
79072805 | 2025 | register I32 todo; |
9f68db38 LW |
2026 | register char *frombase = from; |
2027 | ||
2028 | if (len == 1) { | |
2029 | todo = *from; | |
2030 | while (count-- > 0) | |
2031 | *to++ = todo; | |
2032 | return; | |
2033 | } | |
2034 | while (count-- > 0) { | |
2035 | for (todo = len; todo > 0; todo--) { | |
2036 | *to++ = *from++; | |
2037 | } | |
2038 | from = frombase; | |
2039 | } | |
2040 | } | |
0f85fab0 LW |
2041 | |
2042 | #ifndef CASTNEGFLOAT | |
463ee0b2 | 2043 | U32 |
79072805 | 2044 | cast_ulong(f) |
0f85fab0 LW |
2045 | double f; |
2046 | { | |
2047 | long along; | |
2048 | ||
27e2fb84 | 2049 | #if CASTFLAGS & 2 |
34de22dd LW |
2050 | # define BIGDOUBLE 2147483648.0 |
2051 | if (f >= BIGDOUBLE) | |
2052 | return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000; | |
2053 | #endif | |
0f85fab0 LW |
2054 | if (f >= 0.0) |
2055 | return (unsigned long)f; | |
2056 | along = (long)f; | |
2057 | return (unsigned long)along; | |
2058 | } | |
ed6116ce LW |
2059 | # undef BIGDOUBLE |
2060 | #endif | |
2061 | ||
2062 | #ifndef CASTI32 | |
5d94fbed | 2063 | |
5d94fbed AD |
2064 | /* Unfortunately, on some systems the cast_uv() function doesn't |
2065 | work with the system-supplied definition of ULONG_MAX. The | |
2066 | comparison (f >= ULONG_MAX) always comes out true. It must be a | |
2067 | problem with the compiler constant folding. | |
2068 | ||
2069 | In any case, this workaround should be fine on any two's complement | |
2070 | system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your | |
2071 | ccflags. | |
2072 | --Andy Dougherty <doughera@lafcol.lafayette.edu> | |
2073 | */ | |
1eb770ff | 2074 | |
2075 | /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead | |
2076 | of LONG_(MIN/MAX). | |
2077 | -- Kenneth Albanowski <kjahds@kjahds.com> | |
2078 | */ | |
2079 | ||
20cec16a | 2080 | #ifndef MY_UV_MAX |
2081 | # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1) | |
5d94fbed AD |
2082 | #endif |
2083 | ||
ed6116ce LW |
2084 | I32 |
2085 | cast_i32(f) | |
2086 | double f; | |
2087 | { | |
20cec16a | 2088 | if (f >= I32_MAX) |
2089 | return (I32) I32_MAX; | |
2090 | if (f <= I32_MIN) | |
2091 | return (I32) I32_MIN; | |
ed6116ce LW |
2092 | return (I32) f; |
2093 | } | |
a0d0e21e LW |
2094 | |
2095 | IV | |
2096 | cast_iv(f) | |
2097 | double f; | |
2098 | { | |
20cec16a | 2099 | if (f >= IV_MAX) |
2100 | return (IV) IV_MAX; | |
2101 | if (f <= IV_MIN) | |
2102 | return (IV) IV_MIN; | |
a0d0e21e LW |
2103 | return (IV) f; |
2104 | } | |
5d94fbed AD |
2105 | |
2106 | UV | |
2107 | cast_uv(f) | |
2108 | double f; | |
2109 | { | |
20cec16a | 2110 | if (f >= MY_UV_MAX) |
2111 | return (UV) MY_UV_MAX; | |
5d94fbed AD |
2112 | return (UV) f; |
2113 | } | |
2114 | ||
0f85fab0 | 2115 | #endif |
62b28dd9 | 2116 | |
fe14fcc3 | 2117 | #ifndef HAS_RENAME |
79072805 | 2118 | I32 |
62b28dd9 LW |
2119 | same_dirent(a,b) |
2120 | char *a; | |
2121 | char *b; | |
2122 | { | |
93a17b20 LW |
2123 | char *fa = strrchr(a,'/'); |
2124 | char *fb = strrchr(b,'/'); | |
62b28dd9 LW |
2125 | struct stat tmpstatbuf1; |
2126 | struct stat tmpstatbuf2; | |
2127 | #ifndef MAXPATHLEN | |
2128 | #define MAXPATHLEN 1024 | |
2129 | #endif | |
2130 | char tmpbuf[MAXPATHLEN+1]; | |
2131 | ||
2132 | if (fa) | |
2133 | fa++; | |
2134 | else | |
2135 | fa = a; | |
2136 | if (fb) | |
2137 | fb++; | |
2138 | else | |
2139 | fb = b; | |
2140 | if (strNE(a,b)) | |
2141 | return FALSE; | |
2142 | if (fa == a) | |
6eb13c3b | 2143 | strcpy(tmpbuf,"."); |
62b28dd9 LW |
2144 | else |
2145 | strncpy(tmpbuf, a, fa - a); | |
a0d0e21e | 2146 | if (Stat(tmpbuf, &tmpstatbuf1) < 0) |
62b28dd9 LW |
2147 | return FALSE; |
2148 | if (fb == b) | |
6eb13c3b | 2149 | strcpy(tmpbuf,"."); |
62b28dd9 LW |
2150 | else |
2151 | strncpy(tmpbuf, b, fb - b); | |
a0d0e21e | 2152 | if (Stat(tmpbuf, &tmpstatbuf2) < 0) |
62b28dd9 LW |
2153 | return FALSE; |
2154 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
2155 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
2156 | } | |
fe14fcc3 LW |
2157 | #endif /* !HAS_RENAME */ |
2158 | ||
55497cff | 2159 | UV |
79072805 | 2160 | scan_oct(start, len, retlen) |
fe14fcc3 | 2161 | char *start; |
79072805 LW |
2162 | I32 len; |
2163 | I32 *retlen; | |
fe14fcc3 LW |
2164 | { |
2165 | register char *s = start; | |
55497cff | 2166 | register UV retval = 0; |
2167 | bool overflowed = FALSE; | |
fe14fcc3 | 2168 | |
748a9306 | 2169 | while (len && *s >= '0' && *s <= '7') { |
55497cff | 2170 | register UV n = retval << 3; |
2171 | if (!overflowed && (n >> 3) != retval) { | |
2172 | warn("Integer overflow in octal number"); | |
2173 | overflowed = TRUE; | |
2174 | } | |
2175 | retval = n | (*s++ - '0'); | |
748a9306 | 2176 | len--; |
fe14fcc3 | 2177 | } |
748a9306 LW |
2178 | if (dowarn && len && (*s == '8' || *s == '9')) |
2179 | warn("Illegal octal digit ignored"); | |
fe14fcc3 LW |
2180 | *retlen = s - start; |
2181 | return retval; | |
2182 | } | |
2183 | ||
71be2cbc | 2184 | UV |
79072805 | 2185 | scan_hex(start, len, retlen) |
fe14fcc3 | 2186 | char *start; |
79072805 LW |
2187 | I32 len; |
2188 | I32 *retlen; | |
fe14fcc3 LW |
2189 | { |
2190 | register char *s = start; | |
55497cff | 2191 | register UV retval = 0; |
2192 | bool overflowed = FALSE; | |
fe14fcc3 LW |
2193 | char *tmp; |
2194 | ||
93a17b20 | 2195 | while (len-- && *s && (tmp = strchr(hexdigit, *s))) { |
55497cff | 2196 | register UV n = retval << 4; |
2197 | if (!overflowed && (n >> 4) != retval) { | |
2198 | warn("Integer overflow in hex number"); | |
2199 | overflowed = TRUE; | |
2200 | } | |
2201 | retval = n | (tmp - hexdigit) & 15; | |
fe14fcc3 LW |
2202 | s++; |
2203 | } | |
2204 | *retlen = s - start; | |
2205 | return retval; | |
2206 | } | |
760ac839 LW |
2207 | |
2208 | ||
2209 | #ifdef HUGE_VAL | |
2210 | /* | |
2211 | * This hack is to force load of "huge" support from libm.a | |
2212 | * So it is in perl for (say) POSIX to use. | |
2213 | * Needed for SunOS with Sun's 'acc' for example. | |
2214 | */ | |
2215 | double | |
2216 | Perl_huge() | |
2217 | { | |
2218 | return HUGE_VAL; | |
2219 | } | |
2220 | #endif |