This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove global macro "self". Change thr->Tself to thr->self.
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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
PP
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
PP
49#ifdef I_SYS_WAIT
50# include <sys/wait.h>
51#endif
52
8d063cd8 53#define FLUSH
8d063cd8 54
a0d0e21e
LW
55#ifdef LEAKTEST
56static 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 69Malloc_t
8d063cd8
LW
70safemalloc(size)
71MEM_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 103Malloc_t
8d063cd8 104saferealloc(where,size)
bd4080b3 105Malloc_t where;
8d063cd8
LW
106MEM_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
PP
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
54310121 153Free_t
8d063cd8 154safefree(where)
bd4080b3 155Malloc_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
PP
168/* safe version of calloc */
169
bd4080b3 170Malloc_t
1050c9ca
PP
171safecalloc(count, size)
172MEM_SIZE count;
173MEM_SIZE size;
174{
bd4080b3 175 Malloc_t ptr;
1050c9ca 176
55497cff 177#ifdef HAS_64K_LIMIT
5f05dabc
PP
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
PP
184#ifdef DEBUGGING
185 if ((long)size < 0 || (long)count < 0)
186 croak("panic: calloc");
187#endif
0b7c1c42
GS
188 size *= count;
189 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
1050c9ca 190#if !(defined(I286) || defined(atarist))
fb73857a 191 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 192#else
fb73857a 193 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 194#endif
1050c9ca
PP
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
PP
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 214Malloc_t
a687059c 215safexmalloc(x,size)
79072805 216I32 x;
a687059c 217MEM_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 228Malloc_t
a687059c 229safexrealloc(where,size)
bd4080b3 230Malloc_t where;
a687059c
LW
231MEM_SIZE size;
232{
bd4080b3 233 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
a0d0e21e 234 return new + ALIGN;
a687059c
LW
235}
236
237void
238safexfree(where)
bd4080b3 239Malloc_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 251Malloc_t
1050c9ca
PP
252safexcalloc(x,count,size)
253I32 x;
254MEM_SIZE count;
255MEM_SIZE size;
256{
bd4080b3 257 register Malloc_t where;
1050c9ca
PP
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 267static void
a687059c 268xstat()
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
284char *
fc36a67e 285delimcpy(to, toend, from, fromend, delim, retlen)
62b28dd9 286register char *to;
fc36a67e 287register char *toend;
62b28dd9 288register char *from;
a687059c 289register char *fromend;
a0d0e21e 290register int delim;
79072805 291I32 *retlen;
8d063cd8 292{
fc36a67e
PP
293 register I32 tolen;
294 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b
LW
295 if (*from == '\\') {
296 if (from[1] == delim)
297 from++;
fc36a67e
PP
298 else {
299 if (to < toend)
300 *to++ = *from;
301 tolen++;
302 from++;
303 }
378cc40b 304 }
bedebaa5 305 else if (*from == delim)
8d063cd8 306 break;
fc36a67e
PP
307 if (to < toend)
308 *to++ = *from;
8d063cd8 309 }
bedebaa5
CS
310 if (to < toend)
311 *to = '\0';
fc36a67e 312 *retlen = tolen;
8d063cd8
LW
313 return from;
314}
315
316/* return ptr to little string in big string, NULL if not found */
378cc40b 317/* This routine was donated by Corey Satten. */
8d063cd8
LW
318
319char *
320instr(big, little)
378cc40b
LW
321register char *big;
322register char *little;
323{
324 register char *s, *x;
79072805 325 register I32 first;
378cc40b 326
a687059c
LW
327 if (!little)
328 return big;
329 first = *little++;
378cc40b
LW
330 if (!first)
331 return big;
332 while (*big) {
333 if (*big++ != first)
334 continue;
335 for (x=big,s=little; *s; /**/ ) {
336 if (!*x)
337 return Nullch;
338 if (*s++ != *x++) {
339 s--;
340 break;
341 }
342 }
343 if (!*s)
344 return big-1;
345 }
346 return Nullch;
347}
8d063cd8 348
a687059c
LW
349/* same as instr but allow embedded nulls */
350
351char *
352ninstr(big, bigend, little, lend)
353register char *big;
354register char *bigend;
355char *little;
356char *lend;
8d063cd8 357{
a687059c 358 register char *s, *x;
79072805 359 register I32 first = *little;
a687059c 360 register char *littleend = lend;
378cc40b 361
a0d0e21e 362 if (!first && little >= littleend)
a687059c 363 return big;
de3bb511
LW
364 if (bigend - big < littleend - little)
365 return Nullch;
a687059c
LW
366 bigend -= littleend - little++;
367 while (big <= bigend) {
368 if (*big++ != first)
369 continue;
370 for (x=big,s=little; s < littleend; /**/ ) {
371 if (*s++ != *x++) {
372 s--;
373 break;
374 }
375 }
376 if (s >= littleend)
377 return big-1;
378cc40b 378 }
a687059c
LW
379 return Nullch;
380}
381
382/* reverse of the above--find last substring */
383
384char *
385rninstr(big, bigend, little, lend)
386register char *big;
387char *bigend;
388char *little;
389char *lend;
390{
391 register char *bigbeg;
392 register char *s, *x;
79072805 393 register I32 first = *little;
a687059c
LW
394 register char *littleend = lend;
395
a0d0e21e 396 if (!first && little >= littleend)
a687059c
LW
397 return bigend;
398 bigbeg = big;
399 big = bigend - (littleend - little++);
400 while (big >= bigbeg) {
401 if (*big-- != first)
402 continue;
403 for (x=big+2,s=little; s < littleend; /**/ ) {
404 if (*s++ != *x++) {
405 s--;
406 break;
407 }
408 }
409 if (s >= littleend)
410 return big+1;
378cc40b 411 }
a687059c 412 return Nullch;
378cc40b 413}
a687059c 414
bbce6d69
PP
415/*
416 * Set up for a new ctype locale.
417 */
55497cff 418void
bbce6d69
PP
419perl_new_ctype(newctype)
420 char *newctype;
ef7eada9 421{
36477c24
PP
422#ifdef USE_LOCALE_CTYPE
423
bbce6d69 424 int i;
ef7eada9 425
bbce6d69
PP
426 for (i = 0; i < 256; i++) {
427 if (isUPPER_LC(i))
428 fold_locale[i] = toLOWER_LC(i);
429 else if (isLOWER_LC(i))
430 fold_locale[i] = toUPPER_LC(i);
431 else
432 fold_locale[i] = i;
433 }
bbce6d69 434
36477c24
PP
435#endif /* USE_LOCALE_CTYPE */
436}
bbce6d69
PP
437
438/*
439 * Set up for a new collation locale.
440 */
441void
442perl_new_collate(newcoll)
443 char *newcoll;
444{
36477c24
PP
445#ifdef USE_LOCALE_COLLATE
446
bbce6d69
PP
447 if (! newcoll) {
448 if (collation_name) {
449 ++collation_ix;
450 Safefree(collation_name);
451 collation_name = NULL;
452 collation_standard = TRUE;
bbce6d69
PP
453 collxfrm_base = 0;
454 collxfrm_mult = 2;
bbce6d69
PP
455 }
456 return;
457 }
458
459 if (! collation_name || strNE(collation_name, newcoll)) {
460 ++collation_ix;
461 Safefree(collation_name);
462 collation_name = savepv(newcoll);
ff68c719 463 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
bbce6d69 464
bbce6d69
PP
465 {
466 /* 2: at most so many chars ('a', 'b'). */
467 /* 50: surely no system expands a char more. */
468#define XFRMBUFSIZE (2 * 50)
469 char xbuf[XFRMBUFSIZE];
470 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
471 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
472 SSize_t mult = fb - fa;
473 if (mult < 1)
474 croak("strxfrm() gets absurd");
475 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
476 collxfrm_mult = mult;
477 }
bbce6d69 478 }
bbce6d69 479
36477c24
PP
480#endif /* USE_LOCALE_COLLATE */
481}
bbce6d69
PP
482
483/*
484 * Set up for a new numeric locale.
485 */
486void
487perl_new_numeric(newnum)
488 char *newnum;
489{
36477c24
PP
490#ifdef USE_LOCALE_NUMERIC
491
bbce6d69
PP
492 if (! newnum) {
493 if (numeric_name) {
494 Safefree(numeric_name);
495 numeric_name = NULL;
496 numeric_standard = TRUE;
497 numeric_local = TRUE;
498 }
499 return;
500 }
501
502 if (! numeric_name || strNE(numeric_name, newnum)) {
503 Safefree(numeric_name);
504 numeric_name = savepv(newnum);
ff68c719 505 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
bbce6d69
PP
506 numeric_local = TRUE;
507 }
36477c24
PP
508
509#endif /* USE_LOCALE_NUMERIC */
bbce6d69
PP
510}
511
512void
36477c24 513perl_set_numeric_standard()
bbce6d69 514{
5f05dabc
PP
515#ifdef USE_LOCALE_NUMERIC
516
bbce6d69
PP
517 if (! numeric_standard) {
518 setlocale(LC_NUMERIC, "C");
519 numeric_standard = TRUE;
520 numeric_local = FALSE;
521 }
5f05dabc
PP
522
523#endif /* USE_LOCALE_NUMERIC */
bbce6d69
PP
524}
525
526void
36477c24 527perl_set_numeric_local()
bbce6d69 528{
5f05dabc
PP
529#ifdef USE_LOCALE_NUMERIC
530
bbce6d69
PP
531 if (! numeric_local) {
532 setlocale(LC_NUMERIC, numeric_name);
533 numeric_standard = FALSE;
534 numeric_local = TRUE;
535 }
bbce6d69 536
36477c24 537#endif /* USE_LOCALE_NUMERIC */
5f05dabc 538}
36477c24 539
bbce6d69 540
36477c24
PP
541/*
542 * Initialize locale awareness.
543 */
f0c5b223 544int
1050c9ca 545perl_init_i18nl10n(printwarn)
f0c5b223
TB
546 int printwarn;
547{
548 int ok = 1;
549 /* returns
550 * 1 = set ok or not applicable,
551 * 0 = fallback to C locale,
552 * -1 = fallback to C locale failed
553 */
bbce6d69 554
36477c24 555#ifdef USE_LOCALE
bbce6d69 556
36477c24 557#ifdef USE_LOCALE_CTYPE
bbce6d69 558 char *curctype = NULL;
36477c24
PP
559#endif /* USE_LOCALE_CTYPE */
560#ifdef USE_LOCALE_COLLATE
bbce6d69 561 char *curcoll = NULL;
36477c24
PP
562#endif /* USE_LOCALE_COLLATE */
563#ifdef USE_LOCALE_NUMERIC
bbce6d69 564 char *curnum = NULL;
36477c24 565#endif /* USE_LOCALE_NUMERIC */
02b32252 566 char *lc_all = getenv("LC_ALL");
5f05dabc 567 char *lang = getenv("LANG");
bbce6d69 568 bool setlocale_failure = FALSE;
f0c5b223 569
02b32252
CS
570#ifdef LOCALE_ENVIRON_REQUIRED
571
572 /*
573 * Ultrix setlocale(..., "") fails if there are no environment
574 * variables from which to get a locale name.
575 */
576
577 bool done = FALSE;
578
579#ifdef LC_ALL
580 if (lang) {
581 if (setlocale(LC_ALL, ""))
582 done = TRUE;
583 else
584 setlocale_failure = TRUE;
585 }
586 if (!setlocale_failure)
587#endif /* LC_ALL */
588 {
589#ifdef USE_LOCALE_CTYPE
590 if (! (curctype = setlocale(LC_CTYPE,
591 (!done && (lang || getenv("LC_CTYPE")))
592 ? "" : Nullch)))
593 setlocale_failure = TRUE;
594#endif /* USE_LOCALE_CTYPE */
595#ifdef USE_LOCALE_COLLATE
596 if (! (curcoll = setlocale(LC_COLLATE,
597 (!done && (lang || getenv("LC_COLLATE")))
598 ? "" : Nullch)))
599 setlocale_failure = TRUE;
600#endif /* USE_LOCALE_COLLATE */
601#ifdef USE_LOCALE_NUMERIC
602 if (! (curnum = setlocale(LC_NUMERIC,
603 (!done && (lang || getenv("LC_NUMERIC")))
604 ? "" : Nullch)))
605 setlocale_failure = TRUE;
606#endif /* USE_LOCALE_NUMERIC */
607 }
608
609#else /* !LOCALE_ENVIRON_REQUIRED */
610
bbce6d69 611#ifdef LC_ALL
5f05dabc 612
bbce6d69
PP
613 if (! setlocale(LC_ALL, ""))
614 setlocale_failure = TRUE;
5f05dabc
PP
615 else {
616#ifdef USE_LOCALE_CTYPE
617 curctype = setlocale(LC_CTYPE, Nullch);
618#endif /* USE_LOCALE_CTYPE */
619#ifdef USE_LOCALE_COLLATE
620 curcoll = setlocale(LC_COLLATE, Nullch);
621#endif /* USE_LOCALE_COLLATE */
622#ifdef USE_LOCALE_NUMERIC
623 curnum = setlocale(LC_NUMERIC, Nullch);
624#endif /* USE_LOCALE_NUMERIC */
625 }
626
627#else /* !LC_ALL */
bbce6d69 628
36477c24 629#ifdef USE_LOCALE_CTYPE
5f05dabc 630 if (! (curctype = setlocale(LC_CTYPE, "")))
bbce6d69 631 setlocale_failure = TRUE;
36477c24
PP
632#endif /* USE_LOCALE_CTYPE */
633#ifdef USE_LOCALE_COLLATE
5f05dabc 634 if (! (curcoll = setlocale(LC_COLLATE, "")))
bbce6d69 635 setlocale_failure = TRUE;
36477c24
PP
636#endif /* USE_LOCALE_COLLATE */
637#ifdef USE_LOCALE_NUMERIC
5f05dabc 638 if (! (curnum = setlocale(LC_NUMERIC, "")))
bbce6d69 639 setlocale_failure = TRUE;
36477c24 640#endif /* USE_LOCALE_NUMERIC */
bbce6d69 641
5f05dabc
PP
642#endif /* LC_ALL */
643
02b32252
CS
644#endif /* !LOCALE_ENVIRON_REQUIRED */
645
5f05dabc
PP
646 if (setlocale_failure) {
647 char *p;
648 bool locwarn = (printwarn > 1 ||
649 printwarn &&
650 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
20cec16a 651
5f05dabc
PP
652 if (locwarn) {
653#ifdef LC_ALL
654
655 PerlIO_printf(PerlIO_stderr(),
656 "perl: warning: Setting locale failed.\n");
657
658#else /* !LC_ALL */
659
ef7eada9 660 PerlIO_printf(PerlIO_stderr(),
bbce6d69 661 "perl: warning: Setting locale failed for the categories:\n\t");
36477c24 662#ifdef USE_LOCALE_CTYPE
bbce6d69 663 if (! curctype)
5f05dabc 664 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
36477c24
PP
665#endif /* USE_LOCALE_CTYPE */
666#ifdef USE_LOCALE_COLLATE
bbce6d69 667 if (! curcoll)
5f05dabc 668 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
36477c24
PP
669#endif /* USE_LOCALE_COLLATE */
670#ifdef USE_LOCALE_NUMERIC
bbce6d69 671 if (! curnum)
5f05dabc 672 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
36477c24 673#endif /* USE_LOCALE_NUMERIC */
bbce6d69
PP
674 PerlIO_printf(PerlIO_stderr(), "\n");
675
5f05dabc
PP
676#endif /* LC_ALL */
677
760ac839 678 PerlIO_printf(PerlIO_stderr(),
bbce6d69 679 "perl: warning: Please check that your locale settings:\n");
ef7eada9
JH
680
681 PerlIO_printf(PerlIO_stderr(),
bbce6d69
PP
682 "\tLC_ALL = %c%s%c,\n",
683 lc_all ? '"' : '(',
684 lc_all ? lc_all : "unset",
685 lc_all ? '"' : ')');
5f05dabc
PP
686
687 {
688 char **e;
689 for (e = environ; *e; e++) {
690 if (strnEQ(*e, "LC_", 3)
691 && strnNE(*e, "LC_ALL=", 7)
692 && (p = strchr(*e, '=')))
693 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
fb73857a 694 (int)(p - *e), *e, p + 1);
5f05dabc
PP
695 }
696 }
697
ef7eada9 698 PerlIO_printf(PerlIO_stderr(),
bbce6d69 699 "\tLANG = %c%s%c\n",
5f05dabc 700 lang ? '"' : '(',
bbce6d69
PP
701 lang ? lang : "unset",
702 lang ? '"' : ')');
ef7eada9 703
bbce6d69
PP
704 PerlIO_printf(PerlIO_stderr(),
705 " are supported and installed on your system.\n");
5f05dabc 706 }
ef7eada9 707
5f05dabc
PP
708#ifdef LC_ALL
709
710 if (setlocale(LC_ALL, "C")) {
711 if (locwarn)
712 PerlIO_printf(PerlIO_stderr(),
713 "perl: warning: Falling back to the standard locale (\"C\").\n");
bbce6d69 714 ok = 0;
ef7eada9 715 }
5f05dabc
PP
716 else {
717 if (locwarn)
718 PerlIO_printf(PerlIO_stderr(),
719 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
720 ok = -1;
721 }
bbce6d69 722
5f05dabc
PP
723#else /* ! LC_ALL */
724
725 if (0
36477c24 726#ifdef USE_LOCALE_CTYPE
5f05dabc 727 || !(curctype || setlocale(LC_CTYPE, "C"))
36477c24
PP
728#endif /* USE_LOCALE_CTYPE */
729#ifdef USE_LOCALE_COLLATE
5f05dabc 730 || !(curcoll || setlocale(LC_COLLATE, "C"))
36477c24
PP
731#endif /* USE_LOCALE_COLLATE */
732#ifdef USE_LOCALE_NUMERIC
5f05dabc 733 || !(curnum || setlocale(LC_NUMERIC, "C"))
36477c24 734#endif /* USE_LOCALE_NUMERIC */
5f05dabc
PP
735 )
736 {
737 if (locwarn)
bbce6d69 738 PerlIO_printf(PerlIO_stderr(),
5f05dabc
PP
739 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
740 ok = -1;
bbce6d69 741 }
5f05dabc 742
bbce6d69 743#endif /* ! LC_ALL */
5f05dabc
PP
744
745#ifdef USE_LOCALE_CTYPE
746 curctype = setlocale(LC_CTYPE, Nullch);
747#endif /* USE_LOCALE_CTYPE */
748#ifdef USE_LOCALE_COLLATE
749 curcoll = setlocale(LC_COLLATE, Nullch);
750#endif /* USE_LOCALE_COLLATE */
751#ifdef USE_LOCALE_NUMERIC
752 curnum = setlocale(LC_NUMERIC, Nullch);
753#endif /* USE_LOCALE_NUMERIC */
ef7eada9
JH
754 }
755
36477c24 756#ifdef USE_LOCALE_CTYPE
bbce6d69 757 perl_new_ctype(curctype);
36477c24 758#endif /* USE_LOCALE_CTYPE */
bbce6d69 759
36477c24 760#ifdef USE_LOCALE_COLLATE
bbce6d69 761 perl_new_collate(curcoll);
36477c24 762#endif /* USE_LOCALE_COLLATE */
bbce6d69 763
36477c24 764#ifdef USE_LOCALE_NUMERIC
bbce6d69 765 perl_new_numeric(curnum);
36477c24 766#endif /* USE_LOCALE_NUMERIC */
ef7eada9 767
36477c24 768#endif /* USE_LOCALE */
ef7eada9 769
f0c5b223
TB
770 return ok;
771}
772
bbce6d69
PP
773/* Backwards compatibility. */
774int
775perl_init_i18nl14n(printwarn)
776 int printwarn;
777{
5f05dabc 778 return perl_init_i18nl10n(printwarn);
bbce6d69 779}
ef7eada9 780
36477c24 781#ifdef USE_LOCALE_COLLATE
ef7eada9 782
bbce6d69
PP
783/*
784 * mem_collxfrm() is a bit like strxfrm() but with two important
785 * differences. First, it handles embedded NULs. Second, it allocates
786 * a bit more memory than needed for the transformed data itself.
787 * The real transformed data begins at offset sizeof(collationix).
788 * Please see sv_collxfrm() to see how this is used.
789 */
790char *
791mem_collxfrm(s, len, xlen)
792 const char *s;
793 STRLEN len;
794 STRLEN *xlen;
795{
796 char *xbuf;
797 STRLEN xalloc, xin, xout;
798
799 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
800 /* the +1 is for the terminating NUL. */
801
802 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
803 New(171, xbuf, xalloc, char);
804 if (! xbuf)
805 goto bad;
806
807 *(U32*)xbuf = collation_ix;
808 xout = sizeof(collation_ix);
809 for (xin = 0; xin < len; ) {
810 SSize_t xused;
811
812 for (;;) {
813 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
814 if (xused == -1)
815 goto bad;
816 if (xused < xalloc - xout)
817 break;
818 xalloc = (2 * xalloc) + 1;
819 Renew(xbuf, xalloc, char);
820 if (! xbuf)
821 goto bad;
822 }
ef7eada9 823
bbce6d69
PP
824 xin += strlen(s + xin) + 1;
825 xout += xused;
826
827 /* Embedded NULs are understood but silently skipped
828 * because they make no sense in locale collation. */
829 }
ef7eada9 830
bbce6d69
PP
831 xbuf[xout] = '\0';
832 *xlen = xout - sizeof(collation_ix);
833 return xbuf;
834
835 bad:
836 Safefree(xbuf);
837 *xlen = 0;
838 return NULL;
ef7eada9
JH
839}
840
36477c24 841#endif /* USE_LOCALE_COLLATE */
bbce6d69 842
378cc40b 843void
bbce6d69 844fbm_compile(sv)
79072805 845SV *sv;
378cc40b 846{
a687059c
LW
847 register unsigned char *s;
848 register unsigned char *table;
79072805
LW
849 register U32 i;
850 register U32 len = SvCUR(sv);
851 I32 rarest = 0;
852 U32 frequency = 256;
853
748a9306
LW
854 if (len > 255)
855 return; /* can't have offsets that big */
79072805 856 Sv_Grow(sv,len+258);
463ee0b2 857 table = (unsigned char*)(SvPVX(sv) + len + 1);
a687059c
LW
858 s = table - 2;
859 for (i = 0; i < 256; i++) {
378cc40b
LW
860 table[i] = len;
861 }
862 i = 0;
463ee0b2 863 while (s >= (unsigned char*)(SvPVX(sv)))
a687059c 864 {
bbce6d69
PP
865 if (table[*s] == len)
866 table[*s] = i;
378cc40b
LW
867 s--,i++;
868 }
79072805 869 sv_upgrade(sv, SVt_PVBM);
bbce6d69 870 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
79072805 871 SvVALID_on(sv);
378cc40b 872
463ee0b2 873 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
bbce6d69
PP
874 for (i = 0; i < len; i++) {
875 if (freq[s[i]] < frequency) {
876 rarest = i;
877 frequency = freq[s[i]];
378cc40b
LW
878 }
879 }
79072805
LW
880 BmRARE(sv) = s[rarest];
881 BmPREVIOUS(sv) = rarest;
760ac839 882 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
883}
884
378cc40b 885char *
79072805 886fbm_instr(big, bigend, littlestr)
a687059c
LW
887unsigned char *big;
888register unsigned char *bigend;
79072805 889SV *littlestr;
378cc40b 890{
a687059c 891 register unsigned char *s;
79072805
LW
892 register I32 tmp;
893 register I32 littlelen;
a687059c
LW
894 register unsigned char *little;
895 register unsigned char *table;
896 register unsigned char *olds;
897 register unsigned char *oldlittle;
378cc40b 898
79072805 899 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
a0d0e21e
LW
900 STRLEN len;
901 char *l = SvPV(littlestr,len);
902 if (!len)
d48672a2 903 return (char*)big;
a0d0e21e 904 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 905 }
378cc40b 906
79072805
LW
907 littlelen = SvCUR(littlestr);
908 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
0f85fab0
LW
909 if (littlelen > bigend - big)
910 return Nullch;
463ee0b2 911 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 912 s = bigend - littlelen;
36477c24 913 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69
PP
914 return (char*)s; /* how sweet it is */
915 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
916 && s > big) {
917 s--;
36477c24 918 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 919 return (char*)s;
a687059c 920 }
bbce6d69 921 return Nullch;
a687059c 922 }
463ee0b2 923 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9
LW
924 if (--littlelen >= bigend - big)
925 return Nullch;
926 s = big + littlelen;
a687059c 927 oldlittle = little = table - 2;
bbce6d69
PP
928 if (s < bigend) {
929 top2:
930 /*SUPPRESS 560*/
931 if (tmp = table[*s]) {
62b28dd9 932#ifdef POINTERRIGOR
bbce6d69
PP
933 if (bigend - s > tmp) {
934 s += tmp;
935 goto top2;
936 }
62b28dd9 937#else
bbce6d69
PP
938 if ((s += tmp) < bigend)
939 goto top2;
62b28dd9 940#endif
bbce6d69 941 return Nullch;
a687059c 942 }
bbce6d69
PP
943 else {
944 tmp = littlelen; /* less expensive than calling strncmp() */
945 olds = s;
946 while (tmp--) {
947 if (*--s == *--little)
948 continue;
949 s = olds + 1; /* here we pay the price for failure */
950 little = oldlittle;
951 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 952 goto top2;
62b28dd9 953 return Nullch;
a687059c 954 }
bbce6d69 955 return (char *)s;
378cc40b
LW
956 }
957 }
958 return Nullch;
959}
960
961char *
962screaminstr(bigstr, littlestr)
79072805
LW
963SV *bigstr;
964SV *littlestr;
378cc40b 965{
a687059c
LW
966 register unsigned char *s, *x;
967 register unsigned char *big;
79072805
LW
968 register I32 pos;
969 register I32 previous;
970 register I32 first;
a687059c
LW
971 register unsigned char *little;
972 register unsigned char *bigend;
973 register unsigned char *littleend;
378cc40b 974
79072805 975 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
378cc40b 976 return Nullch;
463ee0b2 977 little = (unsigned char *)(SvPVX(littlestr));
79072805 978 littleend = little + SvCUR(littlestr);
378cc40b 979 first = *little++;
79072805 980 previous = BmPREVIOUS(littlestr);
463ee0b2 981 big = (unsigned char *)(SvPVX(bigstr));
79072805 982 bigend = big + SvCUR(bigstr);
378cc40b
LW
983 while (pos < previous) {
984 if (!(pos += screamnext[pos]))
985 return Nullch;
986 }
de3bb511 987#ifdef POINTERRIGOR
bbce6d69
PP
988 do {
989 if (big[pos-previous] != first)
990 continue;
991 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
992 if (x >= bigend)
993 return Nullch;
994 if (*s++ != *x++) {
995 s--;
996 break;
de3bb511 997 }
bbce6d69
PP
998 }
999 if (s == littleend)
1000 return (char *)(big+pos-previous);
1001 } while ( pos += screamnext[pos] );
de3bb511
LW
1002#else /* !POINTERRIGOR */
1003 big -= previous;
bbce6d69
PP
1004 do {
1005 if (big[pos] != first)
1006 continue;
1007 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1008 if (x >= bigend)
1009 return Nullch;
1010 if (*s++ != *x++) {
1011 s--;
1012 break;
378cc40b 1013 }
bbce6d69
PP
1014 }
1015 if (s == littleend)
1016 return (char *)(big+pos);
1017 } while ( pos += screamnext[pos] );
de3bb511 1018#endif /* POINTERRIGOR */
8d063cd8
LW
1019 return Nullch;
1020}
1021
79072805 1022I32
bbce6d69
PP
1023ibcmp(s1, s2, len)
1024char *s1, *s2;
79072805
LW
1025register I32 len;
1026{
bbce6d69
PP
1027 register U8 *a = (U8 *)s1;
1028 register U8 *b = (U8 *)s2;
79072805 1029 while (len--) {
bbce6d69
PP
1030 if (*a != *b && *a != fold[*b])
1031 return 1;
1032 a++,b++;
1033 }
1034 return 0;
1035}
1036
1037I32
1038ibcmp_locale(s1, s2, len)
1039char *s1, *s2;
1040register I32 len;
1041{
1042 register U8 *a = (U8 *)s1;
1043 register U8 *b = (U8 *)s2;
1044 while (len--) {
1045 if (*a != *b && *a != fold_locale[*b])
1046 return 1;
1047 a++,b++;
79072805
LW
1048 }
1049 return 0;
1050}
1051
8d063cd8
LW
1052/* copy a string to a safe spot */
1053
1054char *
a0d0e21e 1055savepv(sv)
79072805 1056char *sv;
8d063cd8 1057{
a687059c 1058 register char *newaddr;
8d063cd8 1059
79072805
LW
1060 New(902,newaddr,strlen(sv)+1,char);
1061 (void)strcpy(newaddr,sv);
8d063cd8
LW
1062 return newaddr;
1063}
1064
a687059c
LW
1065/* same thing but with a known length */
1066
1067char *
a0d0e21e 1068savepvn(sv, len)
79072805
LW
1069char *sv;
1070register I32 len;
a687059c
LW
1071{
1072 register char *newaddr;
1073
1074 New(903,newaddr,len+1,char);
79072805 1075 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c
LW
1076 newaddr[len] = '\0'; /* is now */
1077 return newaddr;
1078}
1079
fc36a67e
PP
1080/* the SV for form() and mess() is not kept in an arena */
1081
1082static SV *
1083mess_alloc()
1084{
1085 SV *sv;
1086 XPVMG *any;
1087
1088 /* Create as PVMG now, to avoid any upgrading later */
1089 New(905, sv, 1, SV);
1090 Newz(905, any, 1, XPVMG);
1091 SvFLAGS(sv) = SVt_PVMG;
1092 SvANY(sv) = (void*)any;
1093 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1094 return sv;
1095}
1096
a0d0e21e 1097#ifdef I_STDARG
8990e307 1098char *
46fc3d4c 1099form(const char* pat, ...)
a687059c
LW
1100#else
1101/*VARARGS0*/
de3bb511 1102char *
46fc3d4c 1103form(pat, va_alist)
71be2cbc 1104 const char *pat;
46fc3d4c 1105 va_dcl
8990e307
LW
1106#endif
1107{
46fc3d4c
PP
1108 va_list args;
1109#ifdef I_STDARG
1110 va_start(args, pat);
a687059c 1111#else
46fc3d4c 1112 va_start(args);
d48672a2 1113#endif
fc36a67e
PP
1114 if (!mess_sv)
1115 mess_sv = mess_alloc();
1116 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1117 va_end(args);
fc36a67e 1118 return SvPVX(mess_sv);
46fc3d4c 1119}
a687059c 1120
46fc3d4c
PP
1121char *
1122mess(pat, args)
1123 const char *pat;
1124 va_list *args;
1125{
1126 SV *sv;
1127 static char dgd[] = " during global destruction.\n";
1128
46fc3d4c 1129 if (!mess_sv)
fc36a67e 1130 mess_sv = mess_alloc();
46fc3d4c 1131 sv = mess_sv;
fc36a67e 1132 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1133 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1134 dTHR;
2304df62 1135 if (dirty)
46fc3d4c 1136 sv_catpv(sv, dgd);
2304df62 1137 else {
46fc3d4c 1138 if (curcop->cop_line)
fc36a67e 1139 sv_catpvf(sv, " at %_ line %ld",
46fc3d4c 1140 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
c07a80fd
PP
1141 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1142 bool line_mode = (RsSIMPLE(rs) &&
1143 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
46fc3d4c
PP
1144 sv_catpvf(sv, ", <%s> %s %ld",
1145 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1146 line_mode ? "line" : "chunk",
1147 (long)IoLINES(GvIOp(last_in_gv)));
2304df62 1148 }
46fc3d4c 1149 sv_catpv(sv, ".\n");
a687059c 1150 }
a687059c 1151 }
46fc3d4c 1152 return SvPVX(sv);
a687059c
LW
1153}
1154
ecfc5424 1155#ifdef I_STDARG
36477c24 1156OP *
71be2cbc 1157die(const char* pat, ...)
36477c24
PP
1158#else
1159/*VARARGS0*/
1160OP *
1161die(pat, va_alist)
71be2cbc 1162 const char *pat;
36477c24
PP
1163 va_dcl
1164#endif
1165{
5dc0d613 1166 dTHR;
36477c24
PP
1167 va_list args;
1168 char *message;
035902c7 1169 I32 oldrunlevel = runlevel;
36477c24
PP
1170 int was_in_eval = in_eval;
1171 HV *stash;
1172 GV *gv;
1173 CV *cv;
1174
d9f997d7 1175 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n",
0f15f207 1176 curstack, mainstack));/*debug*/
36477c24
PP
1177 /* We have to switch back to mainstack or die_where may try to pop
1178 * the eval block from the wrong stack if die is being called from a
1179 * signal handler. - dkindred@cs.cmu.edu */
1180 if (curstack != mainstack) {
1181 dSP;
1182 SWITCHSTACK(curstack, mainstack);
1183 }
1184
1185#ifdef I_STDARG
1186 va_start(args, pat);
1187#else
1188 va_start(args);
1189#endif
1190 message = mess(pat, &args);
1191 va_end(args);
1192
d9f997d7 1193 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n",
0f15f207 1194 message, diehook));/*debug*/
1738f5c4
CS
1195 if (diehook) {
1196 /* sv_2cv might call croak() */
1197 SV *olddiehook = diehook;
1198 ENTER;
1199 SAVESPTR(diehook);
1200 diehook = Nullsv;
1201 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1202 LEAVE;
1203 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1204 dSP;
774d564b
PP
1205 SV *msg;
1206
1207 ENTER;
1208 msg = newSVpv(message, 0);
1209 SvREADONLY_on(msg);
1210 SAVEFREESV(msg);
1738f5c4
CS
1211
1212 PUSHMARK(sp);
1213 XPUSHs(msg);
1214 PUTBACK;
1215 perl_call_sv((SV*)cv, G_DISCARD);
1216
774d564b 1217 LEAVE;
1738f5c4 1218 }
36477c24
PP
1219 }
1220
1221 restartop = die_where(message);
d9f997d7 1222 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
0f15f207
MB
1223 "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
1224 restartop, was_in_eval, oldrunlevel));/*debug*/
36477c24 1225 if ((!restartop && was_in_eval) || oldrunlevel > 1)
54310121 1226 JMPENV_JUMP(3);
36477c24
PP
1227 return restartop;
1228}
1229
1230#ifdef I_STDARG
79072805 1231void
71be2cbc 1232croak(const char* pat, ...)
463ee0b2 1233#else
8990e307
LW
1234/*VARARGS0*/
1235void
1236croak(pat, va_alist)
1237 char *pat;
1238 va_dcl
463ee0b2 1239#endif
a687059c 1240{
11343788 1241 dTHR;
a687059c 1242 va_list args;
de3bb511 1243 char *message;
748a9306
LW
1244 HV *stash;
1245 GV *gv;
1246 CV *cv;
a687059c 1247
a0d0e21e 1248#ifdef I_STDARG
8990e307
LW
1249 va_start(args, pat);
1250#else
a687059c 1251 va_start(args);
8990e307 1252#endif
2304df62 1253 message = mess(pat, &args);
a687059c 1254 va_end(args);
11343788 1255#ifdef USE_THREADS
d9f997d7 1256 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
11343788 1257#endif /* USE_THREADS */
20cec16a 1258 if (diehook) {
1738f5c4 1259 /* sv_2cv might call croak() */
20cec16a 1260 SV *olddiehook = diehook;
1738f5c4
CS
1261 ENTER;
1262 SAVESPTR(diehook);
1263 diehook = Nullsv;
20cec16a 1264 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4
CS
1265 LEAVE;
1266 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1267 dSP;
774d564b
PP
1268 SV *msg;
1269
1270 ENTER;
1271 msg = newSVpv(message, 0);
1272 SvREADONLY_on(msg);
1273 SAVEFREESV(msg);
20cec16a
PP
1274
1275 PUSHMARK(sp);
1738f5c4 1276 XPUSHs(msg);
20cec16a
PP
1277 PUTBACK;
1278 perl_call_sv((SV*)cv, G_DISCARD);
36477c24 1279
774d564b 1280 LEAVE;
20cec16a 1281 }
748a9306 1282 }
a0d0e21e
LW
1283 if (in_eval) {
1284 restartop = die_where(message);
54310121 1285 JMPENV_JUMP(3);
a0d0e21e 1286 }
760ac839
LW
1287 PerlIO_puts(PerlIO_stderr(),message);
1288 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1289 my_failure_exit();
a687059c
LW
1290}
1291
8990e307 1292void
ecfc5424 1293#ifdef I_STDARG
71be2cbc 1294warn(const char* pat,...)
463ee0b2 1295#else
8990e307
LW
1296/*VARARGS0*/
1297warn(pat,va_alist)
71be2cbc 1298 const char *pat;
8990e307 1299 va_dcl
463ee0b2 1300#endif
a687059c
LW
1301{
1302 va_list args;
de3bb511 1303 char *message;
748a9306
LW
1304 HV *stash;
1305 GV *gv;
1306 CV *cv;
a687059c 1307
a0d0e21e 1308#ifdef I_STDARG
8990e307
LW
1309 va_start(args, pat);
1310#else
a687059c 1311 va_start(args);
8990e307 1312#endif
2304df62 1313 message = mess(pat, &args);
a687059c
LW
1314 va_end(args);
1315
20cec16a 1316 if (warnhook) {
1738f5c4 1317 /* sv_2cv might call warn() */
11343788 1318 dTHR;
20cec16a 1319 SV *oldwarnhook = warnhook;
1738f5c4
CS
1320 ENTER;
1321 SAVESPTR(warnhook);
1322 warnhook = Nullsv;
20cec16a 1323 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4
CS
1324 LEAVE;
1325 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1326 dSP;
774d564b
PP
1327 SV *msg;
1328
1329 ENTER;
1330 msg = newSVpv(message, 0);
1331 SvREADONLY_on(msg);
1332 SAVEFREESV(msg);
1333
20cec16a 1334 PUSHMARK(sp);
774d564b 1335 XPUSHs(msg);
20cec16a
PP
1336 PUTBACK;
1337 perl_call_sv((SV*)cv, G_DISCARD);
774d564b
PP
1338
1339 LEAVE;
20cec16a
PP
1340 return;
1341 }
748a9306 1342 }
20cec16a 1343 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1344#ifdef LEAKTEST
20cec16a 1345 DEBUG_L(xstat());
a687059c 1346#endif
20cec16a 1347 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1348}
8d063cd8 1349
a0d0e21e 1350#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1351#ifndef WIN32
8d063cd8 1352void
7c0587c8 1353my_setenv(nam,val)
8d063cd8
LW
1354char *nam, *val;
1355{
79072805 1356 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1357
fe14fcc3 1358 if (environ == origenviron) { /* need we copy environment? */
79072805
LW
1359 I32 j;
1360 I32 max;
fe14fcc3
LW
1361 char **tmpenv;
1362
de3bb511 1363 /*SUPPRESS 530*/
fe14fcc3
LW
1364 for (max = i; environ[max]; max++) ;
1365 New(901,tmpenv, max+2, char*);
1366 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1367 tmpenv[j] = savepv(environ[j]);
fe14fcc3
LW
1368 tmpenv[max] = Nullch;
1369 environ = tmpenv; /* tell exec where it is now */
1370 }
a687059c 1371 if (!val) {
e5ebf479 1372 Safefree(environ[i]);
a687059c
LW
1373 while (environ[i]) {
1374 environ[i] = environ[i+1];
1375 i++;
1376 }
1377 return;
1378 }
8d063cd8 1379 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1380 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8
LW
1381 environ[i+1] = Nullch; /* make sure it's null terminated */
1382 }
fe14fcc3
LW
1383 else
1384 Safefree(environ[i]);
a687059c 1385 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1386#ifndef MSDOS
a687059c 1387 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9
LW
1388#else
1389 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3
LW
1390 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1391 * some utilities and applications may break because they only look
1392 * for upper case strings. (Fixed strupr() bug here.)]
1393 */
1394 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9
LW
1395 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1396#endif /* MSDOS */
8d063cd8
LW
1397}
1398
3e3baf6d 1399#else /* if WIN32 */
68dc0745
PP
1400
1401void
1402my_setenv(nam,val)
1403char *nam, *val;
1404{
3e3baf6d
TB
1405
1406#ifdef USE_WIN32_RTL_ENV
1407
68dc0745
PP
1408 register char *envstr;
1409 STRLEN namlen = strlen(nam);
3e3baf6d
TB
1410 STRLEN vallen;
1411 char *oldstr = environ[setenv_getix(nam)];
1412
1413 /* putenv() has totally broken semantics in both the Borland
1414 * and Microsoft CRTLs. They either store the passed pointer in
1415 * the environment without making a copy, or make a copy and don't
1416 * free it. And on top of that, they dont free() old entries that
1417 * are being replaced/deleted. This means the caller must
1418 * free any old entries somehow, or we end up with a memory
1419 * leak every time my_setenv() is called. One might think
1420 * one could directly manipulate environ[], like the UNIX code
1421 * above, but direct changes to environ are not allowed when
1422 * calling putenv(), since the RTLs maintain an internal
1423 * *copy* of environ[]. Bad, bad, *bad* stink.
1424 * GSAR 97-06-07
1425 */
68dc0745 1426
3e3baf6d
TB
1427 if (!val) {
1428 if (!oldstr)
1429 return;
1430 val = "";
1431 vallen = 0;
1432 }
1433 else
1434 vallen = strlen(val);
fc36a67e 1435 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1436 (void)sprintf(envstr,"%s=%s",nam,val);
3e3baf6d
TB
1437 (void)putenv(envstr);
1438 if (oldstr)
1439 Safefree(oldstr);
1440#ifdef _MSC_VER
1441 Safefree(envstr); /* MSVCRT leaks without this */
1442#endif
1443
1444#else /* !USE_WIN32_RTL_ENV */
1445
1446 /* The sane way to deal with the environment.
1447 * Has these advantages over putenv() & co.:
1448 * * enables us to store a truly empty value in the
1449 * environment (like in UNIX).
1450 * * we don't have to deal with RTL globals, bugs and leaks.
1451 * * Much faster.
1452 * Why you may want to enable USE_WIN32_RTL_ENV:
1453 * * environ[] and RTL functions will not reflect changes,
1454 * which might be an issue if extensions want to access
1455 * the env. via RTL. This cuts both ways, since RTL will
1456 * not see changes made by extensions that call the Win32
1457 * functions directly, either.
1458 * GSAR 97-06-07
1459 */
1460 SetEnvironmentVariable(nam,val);
1461
1462#endif
1463}
1464
1465#endif /* WIN32 */
1466
1467I32
1468setenv_getix(nam)
1469char *nam;
1470{
1471 register I32 i, len = strlen(nam);
1472
1473 for (i = 0; environ[i]; i++) {
1474 if (
1475#ifdef WIN32
1476 strnicmp(environ[i],nam,len) == 0
1477#else
1478 strnEQ(environ[i],nam,len)
1479#endif
1480 && environ[i][len] == '=')
1481 break; /* strnEQ must come first to avoid */
1482 } /* potential SEGV's */
1483 return i;
68dc0745
PP
1484}
1485
a0d0e21e 1486#endif /* !VMS */
378cc40b 1487
16d20bd9 1488#ifdef UNLINK_ALL_VERSIONS
79072805 1489I32
378cc40b
LW
1490unlnk(f) /* unlink all versions of a file */
1491char *f;
1492{
79072805 1493 I32 i;
378cc40b
LW
1494
1495 for (i = 0; unlink(f) >= 0; i++) ;
1496 return i ? 0 : -1;
1497}
1498#endif
1499
85e6fe83 1500#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1501char *
7c0587c8 1502my_bcopy(from,to,len)
378cc40b
LW
1503register char *from;
1504register char *to;
79072805 1505register I32 len;
378cc40b
LW
1506{
1507 char *retval = to;
1508
7c0587c8
LW
1509 if (from - to >= 0) {
1510 while (len--)
1511 *to++ = *from++;
1512 }
1513 else {
1514 to += len;
1515 from += len;
1516 while (len--)
faf8582f 1517 *(--to) = *(--from);
7c0587c8 1518 }
378cc40b
LW
1519 return retval;
1520}
ffed7fef 1521#endif
378cc40b 1522
fc36a67e
PP
1523#ifndef HAS_MEMSET
1524void *
1525my_memset(loc,ch,len)
1526register char *loc;
1527register I32 ch;
1528register I32 len;
1529{
1530 char *retval = loc;
1531
1532 while (len--)
1533 *loc++ = ch;
1534 return retval;
1535}
1536#endif
1537
7c0587c8 1538#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1539char *
7c0587c8 1540my_bzero(loc,len)
378cc40b 1541register char *loc;
79072805 1542register I32 len;
378cc40b
LW
1543{
1544 char *retval = loc;
1545
1546 while (len--)
1547 *loc++ = 0;
1548 return retval;
1549}
1550#endif
7c0587c8 1551
36477c24 1552#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1553I32
7c0587c8 1554my_memcmp(s1,s2,len)
36477c24
PP
1555char *s1;
1556char *s2;
79072805 1557register I32 len;
7c0587c8 1558{
36477c24
PP
1559 register U8 *a = (U8 *)s1;
1560 register U8 *b = (U8 *)s2;
79072805 1561 register I32 tmp;
7c0587c8
LW
1562
1563 while (len--) {
36477c24 1564 if (tmp = *a++ - *b++)
7c0587c8
LW
1565 return tmp;
1566 }
1567 return 0;
1568}
36477c24 1569#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1570
4633a7c4 1571#if defined(I_STDARG) || defined(I_VARARGS)
fe14fcc3 1572#ifndef HAS_VPRINTF
a687059c 1573
85e6fe83 1574#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1575char *
1576#else
1577int
1578#endif
1579vsprintf(dest, pat, args)
71be2cbc
PP
1580char *dest;
1581const char *pat;
1582char *args;
a687059c
LW
1583{
1584 FILE fakebuf;
1585
1586 fakebuf._ptr = dest;
1587 fakebuf._cnt = 32767;
35c8bce7
LW
1588#ifndef _IOSTRG
1589#define _IOSTRG 0
1590#endif
a687059c
LW
1591 fakebuf._flag = _IOWRT|_IOSTRG;
1592 _doprnt(pat, args, &fakebuf); /* what a kludge */
1593 (void)putc('\0', &fakebuf);
85e6fe83 1594#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1595 return(dest);
1596#else
1597 return 0; /* perl doesn't use return value */
1598#endif
1599}
1600
fe14fcc3 1601#endif /* HAS_VPRINTF */
4633a7c4 1602#endif /* I_VARARGS || I_STDARGS */
a687059c
LW
1603
1604#ifdef MYSWAP
ffed7fef 1605#if BYTEORDER != 0x4321
a687059c 1606short
748a9306 1607#ifndef CAN_PROTOTYPE
a687059c
LW
1608my_swap(s)
1609short s;
748a9306
LW
1610#else
1611my_swap(short s)
1612#endif
a687059c
LW
1613{
1614#if (BYTEORDER & 1) == 0
1615 short result;
1616
1617 result = ((s & 255) << 8) + ((s >> 8) & 255);
1618 return result;
1619#else
1620 return s;
1621#endif
1622}
1623
1624long
748a9306
LW
1625#ifndef CAN_PROTOTYPE
1626my_htonl(l)
a687059c 1627register long l;
748a9306
LW
1628#else
1629my_htonl(long l)
1630#endif
a687059c
LW
1631{
1632 union {
1633 long result;
ffed7fef 1634 char c[sizeof(long)];
a687059c
LW
1635 } u;
1636
ffed7fef 1637#if BYTEORDER == 0x1234
a687059c
LW
1638 u.c[0] = (l >> 24) & 255;
1639 u.c[1] = (l >> 16) & 255;
1640 u.c[2] = (l >> 8) & 255;
1641 u.c[3] = l & 255;
1642 return u.result;
1643#else
ffed7fef 1644#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1645 croak("Unknown BYTEORDER\n");
a687059c 1646#else
79072805
LW
1647 register I32 o;
1648 register I32 s;
a687059c 1649
ffed7fef
LW
1650 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1651 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1652 }
1653 return u.result;
1654#endif
1655#endif
1656}
1657
1658long
748a9306
LW
1659#ifndef CAN_PROTOTYPE
1660my_ntohl(l)
a687059c 1661register long l;
748a9306
LW
1662#else
1663my_ntohl(long l)
1664#endif
a687059c
LW
1665{
1666 union {
1667 long l;
ffed7fef 1668 char c[sizeof(long)];
a687059c
LW
1669 } u;
1670
ffed7fef 1671#if BYTEORDER == 0x1234
a687059c
LW
1672 u.c[0] = (l >> 24) & 255;
1673 u.c[1] = (l >> 16) & 255;
1674 u.c[2] = (l >> 8) & 255;
1675 u.c[3] = l & 255;
1676 return u.l;
1677#else
ffed7fef 1678#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1679 croak("Unknown BYTEORDER\n");
a687059c 1680#else
79072805
LW
1681 register I32 o;
1682 register I32 s;
a687059c
LW
1683
1684 u.l = l;
1685 l = 0;
ffed7fef
LW
1686 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1687 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1688 }
1689 return l;
1690#endif
1691#endif
1692}
1693
ffed7fef 1694#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1695#endif /* MYSWAP */
1696
1697/*
1698 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1699 * If these functions are defined,
1700 * the BYTEORDER is neither 0x1234 nor 0x4321.
1701 * However, this is not assumed.
1702 * -DWS
1703 */
1704
1705#define HTOV(name,type) \
1706 type \
1707 name (n) \
1708 register type n; \
1709 { \
1710 union { \
1711 type value; \
1712 char c[sizeof(type)]; \
1713 } u; \
79072805
LW
1714 register I32 i; \
1715 register I32 s; \
988174c1
LW
1716 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1717 u.c[i] = (n >> s) & 0xFF; \
1718 } \
1719 return u.value; \
1720 }
1721
1722#define VTOH(name,type) \
1723 type \
1724 name (n) \
1725 register type n; \
1726 { \
1727 union { \
1728 type value; \
1729 char c[sizeof(type)]; \
1730 } u; \
79072805
LW
1731 register I32 i; \
1732 register I32 s; \
988174c1
LW
1733 u.value = n; \
1734 n = 0; \
1735 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1736 n += (u.c[i] & 0xFF) << s; \
1737 } \
1738 return n; \
1739 }
1740
1741#if defined(HAS_HTOVS) && !defined(htovs)
1742HTOV(htovs,short)
1743#endif
1744#if defined(HAS_HTOVL) && !defined(htovl)
1745HTOV(htovl,long)
1746#endif
1747#if defined(HAS_VTOHS) && !defined(vtohs)
1748VTOH(vtohs,short)
1749#endif
1750#if defined(HAS_VTOHL) && !defined(vtohl)
1751VTOH(vtohl,long)
1752#endif
a687059c 1753
5f05dabc
PP
1754 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1755#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
760ac839 1756PerlIO *
79072805 1757my_popen(cmd,mode)
a687059c
LW
1758char *cmd;
1759char *mode;
1760{
1761 int p[2];
79072805
LW
1762 register I32 this, that;
1763 register I32 pid;
1764 SV *sv;
1738f5c4 1765 I32 doexec = strNE(cmd,"-");
a687059c 1766
ddcf38b7
IZ
1767#ifdef OS2
1768 if (doexec) {
1769 return my_syspopen(cmd,mode);
1770 }
1771#endif
a687059c
LW
1772 if (pipe(p) < 0)
1773 return Nullfp;
1774 this = (*mode == 'w');
1775 that = !this;
bbce6d69
PP
1776 if (doexec && tainting) {
1777 taint_env();
1778 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1779 }
a687059c
LW
1780 while ((pid = (doexec?vfork():fork())) < 0) {
1781 if (errno != EAGAIN) {
1782 close(p[this]);
1783 if (!doexec)
463ee0b2 1784 croak("Can't fork");
a687059c
LW
1785 return Nullfp;
1786 }
1787 sleep(5);
1788 }
1789 if (pid == 0) {
79072805
LW
1790 GV* tmpgv;
1791
a687059c
LW
1792#define THIS that
1793#define THAT this
1794 close(p[THAT]);
1795 if (p[THIS] != (*mode == 'r')) {
1796 dup2(p[THIS], *mode == 'r');
1797 close(p[THIS]);
1798 }
1799 if (doexec) {
a0d0e21e 1800#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
1801 int fd;
1802
1803#ifndef NOFILE
1804#define NOFILE 20
1805#endif
d48672a2 1806 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
ae986130
LW
1807 close(fd);
1808#endif
a687059c
LW
1809 do_exec(cmd); /* may or may not use the shell */
1810 _exit(1);
1811 }
de3bb511 1812 /*SUPPRESS 560*/
85e6fe83 1813 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1814 sv_setiv(GvSV(tmpgv), (IV)getpid());
9f68db38 1815 forkprocess = 0;
463ee0b2 1816 hv_clear(pidstatus); /* we have no children */
a687059c
LW
1817 return Nullfp;
1818#undef THIS
1819#undef THAT
1820 }
62b28dd9 1821 do_execfree(); /* free any memory malloced by child on vfork */
a687059c 1822 close(p[that]);
62b28dd9
LW
1823 if (p[that] < p[this]) {
1824 dup2(p[this], p[that]);
1825 close(p[this]);
1826 p[this] = p[that];
1827 }
79072805 1828 sv = *av_fetch(fdpid,p[this],TRUE);
a0d0e21e 1829 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1830 SvIVX(sv) = pid;
a687059c 1831 forkprocess = pid;
760ac839 1832 return PerlIO_fdopen(p[this], mode);
a687059c 1833}
7c0587c8 1834#else
55497cff 1835#if defined(atarist) || defined(DJGPP)
7c0587c8 1836FILE *popen();
760ac839 1837PerlIO *
79072805 1838my_popen(cmd,mode)
7c0587c8
LW
1839char *cmd;
1840char *mode;
1841{
760ac839 1842 /* Needs work for PerlIO ! */
55497cff
PP
1843 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1844 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8
LW
1845}
1846#endif
1847
1848#endif /* !DOSISH */
a687059c 1849
748a9306 1850#ifdef DUMP_FDS
79072805 1851dump_fds(s)
ae986130
LW
1852char *s;
1853{
1854 int fd;
1855 struct stat tmpstatbuf;
1856
760ac839 1857 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1858 for (fd = 0; fd < 32; fd++) {
a0d0e21e 1859 if (Fstat(fd,&tmpstatbuf) >= 0)
760ac839 1860 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1861 }
760ac839 1862 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130
LW
1863}
1864#endif
1865
fe14fcc3 1866#ifndef HAS_DUP2
fec02dd3 1867int
a687059c
LW
1868dup2(oldfd,newfd)
1869int oldfd;
1870int newfd;
1871{
a0d0e21e 1872#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
1873 if (oldfd == newfd)
1874 return oldfd;
62b28dd9 1875 close(newfd);
fec02dd3 1876 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 1877#else
fc36a67e
PP
1878#define DUP2_MAX_FDS 256
1879 int fdtmp[DUP2_MAX_FDS];
79072805 1880 I32 fdx = 0;
ae986130
LW
1881 int fd;
1882
fe14fcc3 1883 if (oldfd == newfd)
fec02dd3 1884 return oldfd;
a687059c 1885 close(newfd);
fc36a67e
PP
1886 /* good enough for low fd's... */
1887 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1888 if (fdx >= DUP2_MAX_FDS) {
1889 close(fd);
1890 fd = -1;
1891 break;
1892 }
ae986130 1893 fdtmp[fdx++] = fd;
fc36a67e 1894 }
ae986130
LW
1895 while (fdx > 0)
1896 close(fdtmp[--fdx]);
fec02dd3 1897 return fd;
62b28dd9 1898#endif
a687059c
LW
1899}
1900#endif
1901
ff68c719
PP
1902
1903#ifdef HAS_SIGACTION
1904
1905Sighandler_t
1906rsignal(signo, handler)
1907int signo;
1908Sighandler_t handler;
1909{
1910 struct sigaction act, oact;
1911
1912 act.sa_handler = handler;
1913 sigemptyset(&act.sa_mask);
1914 act.sa_flags = 0;
1915#ifdef SA_RESTART
1916 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1917#endif
1918 if (sigaction(signo, &act, &oact) == -1)
36477c24 1919 return SIG_ERR;
ff68c719 1920 else
36477c24 1921 return oact.sa_handler;
ff68c719
PP
1922}
1923
1924Sighandler_t
1925rsignal_state(signo)
1926int signo;
1927{
1928 struct sigaction oact;
1929
1930 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1931 return SIG_ERR;
1932 else
1933 return oact.sa_handler;
1934}
1935
1936int
1937rsignal_save(signo, handler, save)
1938int signo;
1939Sighandler_t handler;
1940Sigsave_t *save;
1941{
1942 struct sigaction act;
1943
1944 act.sa_handler = handler;
1945 sigemptyset(&act.sa_mask);
1946 act.sa_flags = 0;
1947#ifdef SA_RESTART
1948 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1949#endif
1950 return sigaction(signo, &act, save);
1951}
1952
1953int
1954rsignal_restore(signo, save)
1955int signo;
1956Sigsave_t *save;
1957{
1958 return sigaction(signo, save, (struct sigaction *)NULL);
1959}
1960
1961#else /* !HAS_SIGACTION */
1962
1963Sighandler_t
1964rsignal(signo, handler)
1965int signo;
1966Sighandler_t handler;
1967{
1968 return signal(signo, handler);
1969}
1970
1971static int sig_trapped;
1972
1973static
1974Signal_t
1975sig_trap(signo)
1976int signo;
1977{
1978 sig_trapped++;
1979}
1980
1981Sighandler_t
1982rsignal_state(signo)
1983int signo;
1984{
1985 Sighandler_t oldsig;
1986
1987 sig_trapped = 0;
1988 oldsig = signal(signo, sig_trap);
1989 signal(signo, oldsig);
1990 if (sig_trapped)
1991 kill(getpid(), signo);
1992 return oldsig;
1993}
1994
1995int
1996rsignal_save(signo, handler, save)
1997int signo;
1998Sighandler_t handler;
1999Sigsave_t *save;
2000{
2001 *save = signal(signo, handler);
2002 return (*save == SIG_ERR) ? -1 : 0;
2003}
2004
2005int
36477c24 2006rsignal_restore(signo, save)
ff68c719
PP
2007int signo;
2008Sigsave_t *save;
2009{
2010 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2011}
2012
2013#endif /* !HAS_SIGACTION */
2014
5f05dabc
PP
2015 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2016#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
79072805
LW
2017I32
2018my_pclose(ptr)
760ac839 2019PerlIO *ptr;
a687059c 2020{
ff68c719 2021 Sigsave_t hstat, istat, qstat;
a687059c 2022 int status;
a0d0e21e 2023 SV **svp;
20188a90 2024 int pid;
03136e13
CS
2025 bool close_failed;
2026 int saved_errno;
2027#ifdef VMS
2028 int saved_vaxc_errno;
2029#endif
a687059c 2030
760ac839 2031 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 2032 pid = (int)SvIVX(*svp);
a0d0e21e
LW
2033 SvREFCNT_dec(*svp);
2034 *svp = &sv_undef;
ddcf38b7
IZ
2035#ifdef OS2
2036 if (pid == -1) { /* Opened by popen. */
2037 return my_syspclose(ptr);
2038 }
2039#endif
03136e13
CS
2040 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2041 saved_errno = errno;
2042#ifdef VMS
2043 saved_vaxc_errno = vaxc$errno;
2044#endif
2045 }
7c0587c8
LW
2046#ifdef UTS
2047 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2048#endif
ff68c719
PP
2049 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2050 rsignal_save(SIGINT, SIG_IGN, &istat);
2051 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306
LW
2052 do {
2053 pid = wait4pid(pid, &status, 0);
2054 } while (pid == -1 && errno == EINTR);
ff68c719
PP
2055 rsignal_restore(SIGHUP, &hstat);
2056 rsignal_restore(SIGINT, &istat);
2057 rsignal_restore(SIGQUIT, &qstat);
03136e13
CS
2058 if (close_failed) {
2059 SETERRNO(saved_errno, saved_vaxc_errno);
2060 return -1;
2061 }
2062 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
20188a90 2063}
4633a7c4
LW
2064#endif /* !DOSISH */
2065
2066#if !defined(DOSISH) || defined(OS2)
79072805 2067I32
20188a90
LW
2068wait4pid(pid,statusp,flags)
2069int pid;
2070int *statusp;
2071int flags;
2072{
79072805
LW
2073 SV *sv;
2074 SV** svp;
fc36a67e 2075 char spid[TYPE_CHARS(int)];
20188a90
LW
2076
2077 if (!pid)
2078 return -1;
20188a90
LW
2079 if (pid > 0) {
2080 sprintf(spid, "%d", pid);
79072805
LW
2081 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2082 if (svp && *svp != &sv_undef) {
463ee0b2 2083 *statusp = SvIVX(*svp);
748a9306 2084 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2085 return pid;
2086 }
2087 }
2088 else {
79072805 2089 HE *entry;
20188a90 2090
79072805
LW
2091 hv_iterinit(pidstatus);
2092 if (entry = hv_iternext(pidstatus)) {
a0d0e21e 2093 pid = atoi(hv_iterkey(entry,(I32*)statusp));
79072805 2094 sv = hv_iterval(pidstatus,entry);
463ee0b2 2095 *statusp = SvIVX(sv);
20188a90 2096 sprintf(spid, "%d", pid);
748a9306 2097 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2098 return pid;
2099 }
2100 }
79072805 2101#ifdef HAS_WAITPID
367f3c24
IZ
2102# ifdef HAS_WAITPID_RUNTIME
2103 if (!HAS_WAITPID_RUNTIME)
2104 goto hard_way;
2105# endif
79072805 2106 return waitpid(pid,statusp,flags);
367f3c24
IZ
2107#endif
2108#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2109 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24
IZ
2110#endif
2111#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2112 hard_way:
a0d0e21e
LW
2113 {
2114 I32 result;
2115 if (flags)
2116 croak("Can't do waitpid with flags");
2117 else {
2118 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2119 pidgone(result,*statusp);
2120 if (result < 0)
2121 *statusp = -1;
2122 }
2123 return result;
a687059c
LW
2124 }
2125#endif
a687059c 2126}
7c0587c8 2127#endif /* !DOSISH */
a687059c 2128
7c0587c8 2129void
de3bb511 2130/*SUPPRESS 590*/
a687059c
LW
2131pidgone(pid,status)
2132int pid;
2133int status;
2134{
79072805 2135 register SV *sv;
fc36a67e 2136 char spid[TYPE_CHARS(int)];
a687059c 2137
20188a90 2138 sprintf(spid, "%d", pid);
79072805 2139 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2140 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2141 SvIVX(sv) = status;
20188a90 2142 return;
a687059c
LW
2143}
2144
55497cff 2145#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2146int pclose();
ddcf38b7
IZ
2147#ifdef HAS_FORK
2148int /* Cannot prototype with I32
2149 in os2ish.h. */
2150my_syspclose(ptr)
2151#else
79072805
LW
2152I32
2153my_pclose(ptr)
ddcf38b7 2154#endif
760ac839 2155PerlIO *ptr;
a687059c 2156{
760ac839
LW
2157 /* Needs work for PerlIO ! */
2158 FILE *f = PerlIO_findFILE(ptr);
2159 I32 result = pclose(f);
2160 PerlIO_releaseFILE(ptr,f);
2161 return result;
a687059c 2162}
7c0587c8 2163#endif
9f68db38
LW
2164
2165void
2166repeatcpy(to,from,len,count)
2167register char *to;
2168register char *from;
79072805
LW
2169I32 len;
2170register I32 count;
9f68db38 2171{
79072805 2172 register I32 todo;
9f68db38
LW
2173 register char *frombase = from;
2174
2175 if (len == 1) {
2176 todo = *from;
2177 while (count-- > 0)
2178 *to++ = todo;
2179 return;
2180 }
2181 while (count-- > 0) {
2182 for (todo = len; todo > 0; todo--) {
2183 *to++ = *from++;
2184 }
2185 from = frombase;
2186 }
2187}
0f85fab0
LW
2188
2189#ifndef CASTNEGFLOAT
463ee0b2 2190U32
79072805 2191cast_ulong(f)
0f85fab0
LW
2192double f;
2193{
2194 long along;
2195
27e2fb84 2196#if CASTFLAGS & 2
34de22dd
LW
2197# define BIGDOUBLE 2147483648.0
2198 if (f >= BIGDOUBLE)
2199 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2200#endif
0f85fab0
LW
2201 if (f >= 0.0)
2202 return (unsigned long)f;
2203 along = (long)f;
2204 return (unsigned long)along;
2205}
ed6116ce
LW
2206# undef BIGDOUBLE
2207#endif
2208
2209#ifndef CASTI32
5d94fbed 2210
5d94fbed
AD
2211/* Unfortunately, on some systems the cast_uv() function doesn't
2212 work with the system-supplied definition of ULONG_MAX. The
2213 comparison (f >= ULONG_MAX) always comes out true. It must be a
2214 problem with the compiler constant folding.
2215
2216 In any case, this workaround should be fine on any two's complement
2217 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2218 ccflags.
2219 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2220*/
1eb770ff
PP
2221
2222/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2223 of LONG_(MIN/MAX).
2224 -- Kenneth Albanowski <kjahds@kjahds.com>
2225*/
2226
20cec16a
PP
2227#ifndef MY_UV_MAX
2228# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed
AD
2229#endif
2230
ed6116ce
LW
2231I32
2232cast_i32(f)
2233double f;
2234{
20cec16a
PP
2235 if (f >= I32_MAX)
2236 return (I32) I32_MAX;
2237 if (f <= I32_MIN)
2238 return (I32) I32_MIN;
ed6116ce
LW
2239 return (I32) f;
2240}
a0d0e21e
LW
2241
2242IV
2243cast_iv(f)
2244double f;
2245{
20cec16a
PP
2246 if (f >= IV_MAX)
2247 return (IV) IV_MAX;
2248 if (f <= IV_MIN)
2249 return (IV) IV_MIN;
a0d0e21e
LW
2250 return (IV) f;
2251}
5d94fbed
AD
2252
2253UV
2254cast_uv(f)
2255double f;
2256{
20cec16a
PP
2257 if (f >= MY_UV_MAX)
2258 return (UV) MY_UV_MAX;
5d94fbed
AD
2259 return (UV) f;
2260}
2261
0f85fab0 2262#endif
62b28dd9 2263
fe14fcc3 2264#ifndef HAS_RENAME
79072805 2265I32
62b28dd9
LW
2266same_dirent(a,b)
2267char *a;
2268char *b;
2269{
93a17b20
LW
2270 char *fa = strrchr(a,'/');
2271 char *fb = strrchr(b,'/');
62b28dd9
LW
2272 struct stat tmpstatbuf1;
2273 struct stat tmpstatbuf2;
46fc3d4c 2274 SV *tmpsv = sv_newmortal();
62b28dd9
LW
2275
2276 if (fa)
2277 fa++;
2278 else
2279 fa = a;
2280 if (fb)
2281 fb++;
2282 else
2283 fb = b;
2284 if (strNE(a,b))
2285 return FALSE;
2286 if (fa == a)
46fc3d4c 2287 sv_setpv(tmpsv, ".");
62b28dd9 2288 else
46fc3d4c
PP
2289 sv_setpvn(tmpsv, a, fa - a);
2290 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2291 return FALSE;
2292 if (fb == b)
46fc3d4c 2293 sv_setpv(tmpsv, ".");
62b28dd9 2294 else
46fc3d4c
PP
2295 sv_setpvn(tmpsv, b, fb - b);
2296 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2297 return FALSE;
2298 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2299 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2300}
fe14fcc3
LW
2301#endif /* !HAS_RENAME */
2302
55497cff 2303UV
79072805 2304scan_oct(start, len, retlen)
fe14fcc3 2305char *start;
79072805
LW
2306I32 len;
2307I32 *retlen;
fe14fcc3
LW
2308{
2309 register char *s = start;
55497cff
PP
2310 register UV retval = 0;
2311 bool overflowed = FALSE;
fe14fcc3 2312
748a9306 2313 while (len && *s >= '0' && *s <= '7') {
55497cff
PP
2314 register UV n = retval << 3;
2315 if (!overflowed && (n >> 3) != retval) {
2316 warn("Integer overflow in octal number");
2317 overflowed = TRUE;
2318 }
2319 retval = n | (*s++ - '0');
748a9306 2320 len--;
fe14fcc3 2321 }
748a9306
LW
2322 if (dowarn && len && (*s == '8' || *s == '9'))
2323 warn("Illegal octal digit ignored");
fe14fcc3
LW
2324 *retlen = s - start;
2325 return retval;
2326}
2327
71be2cbc 2328UV
79072805 2329scan_hex(start, len, retlen)
fe14fcc3 2330char *start;
79072805
LW
2331I32 len;
2332I32 *retlen;
fe14fcc3
LW
2333{
2334 register char *s = start;
55497cff
PP
2335 register UV retval = 0;
2336 bool overflowed = FALSE;
fe14fcc3
LW
2337 char *tmp;
2338
93a17b20 2339 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
55497cff
PP
2340 register UV n = retval << 4;
2341 if (!overflowed && (n >> 4) != retval) {
2342 warn("Integer overflow in hex number");
2343 overflowed = TRUE;
2344 }
2345 retval = n | (tmp - hexdigit) & 15;
fe14fcc3
LW
2346 s++;
2347 }
2348 *retlen = s - start;
2349 return retval;
2350}
760ac839 2351
11343788 2352#ifdef USE_THREADS
12ca11f6
MB
2353#ifdef FAKE_THREADS
2354/* Very simplistic scheduler for now */
2355void
2356schedule(void)
2357{
c7848ba1 2358 thr = thr->i.next_run;
12ca11f6
MB
2359}
2360
2361void
2362perl_cond_init(cp)
2363perl_cond *cp;
2364{
2365 *cp = 0;
2366}
2367
2368void
2369perl_cond_signal(cp)
2370perl_cond *cp;
2371{
2372 perl_thread t;
2373 perl_cond cond = *cp;
2374
2375 if (!cond)
2376 return;
2377 t = cond->thread;
2378 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2379 t->i.next_run = thr->i.next_run;
2380 thr->i.next_run->i.prev_run = t;
2381 t->i.prev_run = thr;
2382 thr->i.next_run = t;
2383 thr->i.wait_queue = 0;
12ca11f6
MB
2384 /* Remove from the wait queue */
2385 *cp = cond->next;
2386 Safefree(cond);
2387}
2388
2389void
2390perl_cond_broadcast(cp)
2391perl_cond *cp;
2392{
2393 perl_thread t;
2394 perl_cond cond, cond_next;
2395
2396 for (cond = *cp; cond; cond = cond_next) {
2397 t = cond->thread;
2398 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2399 t->i.next_run = thr->i.next_run;
2400 thr->i.next_run->i.prev_run = t;
2401 t->i.prev_run = thr;
2402 thr->i.next_run = t;
2403 thr->i.wait_queue = 0;
12ca11f6
MB
2404 /* Remove from the wait queue */
2405 cond_next = cond->next;
2406 Safefree(cond);
2407 }
2408 *cp = 0;
2409}
2410
2411void
2412perl_cond_wait(cp)
2413perl_cond *cp;
2414{
2415 perl_cond cond;
2416
c7848ba1 2417 if (thr->i.next_run == thr)
12ca11f6
MB
2418 croak("panic: perl_cond_wait called by last runnable thread");
2419
0f15f207 2420 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
2421 cond->thread = thr;
2422 cond->next = *cp;
2423 *cp = cond;
c7848ba1 2424 thr->i.wait_queue = cond;
12ca11f6 2425 /* Remove ourselves from runnable queue */
c7848ba1
MB
2426 thr->i.next_run->i.prev_run = thr->i.prev_run;
2427 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
2428}
2429#endif /* FAKE_THREADS */
2430
11343788
MB
2431#ifdef OLD_PTHREADS_API
2432struct thread *
2433getTHR _((void))
2434{
2435 pthread_addr_t t;
2436
2437 if (pthread_getspecific(thr_key, &t))
2438 croak("panic: pthread_getspecific");
2439 return (struct thread *) t;
2440}
2441#endif /* OLD_PTHREADS_API */
f93b4edd
MB
2442
2443MAGIC *
2444condpair_magic(sv)
2445SV *sv;
2446{
2447 MAGIC *mg;
2448
2449 SvUPGRADE(sv, SVt_PVMG);
2450 mg = mg_find(sv, 'm');
2451 if (!mg) {
2452 condpair_t *cp;
2453
2454 New(53, cp, 1, condpair_t);
2455 MUTEX_INIT(&cp->mutex);
2456 COND_INIT(&cp->owner_cond);
2457 COND_INIT(&cp->cond);
2458 cp->owner = 0;
2459 MUTEX_LOCK(&sv_mutex);
2460 mg = mg_find(sv, 'm');
2461 if (mg) {
2462 /* someone else beat us to initialising it */
2463 MUTEX_UNLOCK(&sv_mutex);
2464 MUTEX_DESTROY(&cp->mutex);
2465 COND_DESTROY(&cp->owner_cond);
2466 COND_DESTROY(&cp->cond);
2467 Safefree(cp);
2468 }
2469 else {
2470 sv_magic(sv, Nullsv, 'm', 0, 0);
2471 mg = SvMAGIC(sv);
2472 mg->mg_ptr = (char *)cp;
2473 mg->mg_len = sizeof(cp);
2474 MUTEX_UNLOCK(&sv_mutex);
bc1f4c86 2475 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2476 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd
MB
2477 }
2478 }
2479 return mg;
2480}
11343788 2481#endif /* USE_THREADS */
760ac839
LW
2482
2483#ifdef HUGE_VAL
2484/*
2485 * This hack is to force load of "huge" support from libm.a
2486 * So it is in perl for (say) POSIX to use.
2487 * Needed for SunOS with Sun's 'acc' for example.
2488 */
2489double
2490Perl_huge()
2491{
2492 return HUGE_VAL;
2493}
2494#endif