3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
12 * not content." --Gandalf
19 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
24 # define SIG_ERR ((Sighandler_t) -1)
27 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
36 /* Put this after #includes because fork and vfork prototypes may
47 # include <sys/file.h>
51 # include <sys/wait.h>
57 static void xstat _((void));
61 static U32 threadnum = 0;
62 #endif /* USE_THREADS */
66 /* paranoid version of malloc */
68 /* NOTE: Do not call the next three routines directly. Use the macros
69 * in handy.h, so that we can easily redefine everything to do tracking of
70 * allocated hunks back to the original New to track down any memory leaks.
71 * XXX This advice seems to be widely ignored :-( --AD August 1996.
75 safemalloc(MEM_SIZE size)
80 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
83 #endif /* HAS_64K_LIMIT */
86 croak("panic: malloc");
88 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
89 #if !(defined(I286) || defined(atarist))
90 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
92 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
99 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
106 /* paranoid version of realloc */
109 saferealloc(Malloc_t where,MEM_SIZE size)
112 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
113 Malloc_t PerlMem_realloc();
114 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
118 PerlIO_printf(PerlIO_stderr(),
119 "Reallocation too large: %lx\n", size) FLUSH;
122 #endif /* HAS_64K_LIMIT */
124 croak("Null realloc");
127 croak("panic: realloc");
129 ptr = PerlMem_realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
131 #if !(defined(I286) || defined(atarist))
133 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
134 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
138 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
139 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
148 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
155 /* safe version of free */
158 safefree(Malloc_t where)
160 #if !(defined(I286) || defined(atarist))
161 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
163 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
171 /* safe version of calloc */
174 safecalloc(MEM_SIZE count, MEM_SIZE size)
179 if (size * count > 0xffff) {
180 PerlIO_printf(PerlIO_stderr(),
181 "Allocation too large: %lx\n", size * count) FLUSH;
184 #endif /* HAS_64K_LIMIT */
186 if ((long)size < 0 || (long)count < 0)
187 croak("panic: calloc");
190 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
191 #if !(defined(I286) || defined(atarist))
192 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
194 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
197 memset((void*)ptr, 0, size);
203 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
210 #endif /* !MYMALLOC */
214 #define ALIGN sizeof(long)
217 safexmalloc(I32 x, MEM_SIZE size)
219 register Malloc_t where;
221 where = safemalloc(size + ALIGN);
225 return where + ALIGN;
229 safexrealloc(Malloc_t where, MEM_SIZE size)
231 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
236 safexfree(Malloc_t where)
243 x = where[0] + 100 * where[1];
249 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
251 register Malloc_t where;
253 where = safexmalloc(x, size * count + ALIGN);
255 memset((void*)where + ALIGN, 0, size * count);
258 return where + ALIGN;
266 for (i = 0; i < MAXXCOUNT; i++) {
267 if (xcount[i] > lastxcount[i]) {
268 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
269 lastxcount[i] = xcount[i];
274 #endif /* LEAKTEST */
276 /* copy a string up to some (non-backslashed) delimiter, if any */
279 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
282 for (tolen = 0; from < fromend; from++, tolen++) {
284 if (from[1] == delim)
293 else if (*from == delim)
304 /* return ptr to little string in big string, NULL if not found */
305 /* This routine was donated by Corey Satten. */
308 instr(register char *big, register char *little)
310 register char *s, *x;
321 for (x=big,s=little; *s; /**/ ) {
335 /* same as instr but allow embedded nulls */
338 ninstr(register char *big, register char *bigend, char *little, char *lend)
340 register char *s, *x;
341 register I32 first = *little;
342 register char *littleend = lend;
344 if (!first && little >= littleend)
346 if (bigend - big < littleend - little)
348 bigend -= littleend - little++;
349 while (big <= bigend) {
352 for (x=big,s=little; s < littleend; /**/ ) {
364 /* reverse of the above--find last substring */
367 rninstr(register char *big, char *bigend, char *little, char *lend)
369 register char *bigbeg;
370 register char *s, *x;
371 register I32 first = *little;
372 register char *littleend = lend;
374 if (!first && little >= littleend)
377 big = bigend - (littleend - little++);
378 while (big >= bigbeg) {
381 for (x=big+2,s=little; s < littleend; /**/ ) {
394 * Set up for a new ctype locale.
397 perl_new_ctype(char *newctype)
399 #ifdef USE_LOCALE_CTYPE
403 for (i = 0; i < 256; i++) {
405 fold_locale[i] = toLOWER_LC(i);
406 else if (isLOWER_LC(i))
407 fold_locale[i] = toUPPER_LC(i);
412 #endif /* USE_LOCALE_CTYPE */
416 * Set up for a new collation locale.
419 perl_new_collate(char *newcoll)
421 #ifdef USE_LOCALE_COLLATE
424 if (collation_name) {
426 Safefree(collation_name);
427 collation_name = NULL;
428 collation_standard = TRUE;
435 if (! collation_name || strNE(collation_name, newcoll)) {
437 Safefree(collation_name);
438 collation_name = savepv(newcoll);
439 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
442 /* 2: at most so many chars ('a', 'b'). */
443 /* 50: surely no system expands a char more. */
444 #define XFRMBUFSIZE (2 * 50)
445 char xbuf[XFRMBUFSIZE];
446 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
447 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
448 SSize_t mult = fb - fa;
450 croak("strxfrm() gets absurd");
451 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
452 collxfrm_mult = mult;
456 #endif /* USE_LOCALE_COLLATE */
460 * Set up for a new numeric locale.
463 perl_new_numeric(char *newnum)
465 #ifdef USE_LOCALE_NUMERIC
469 Safefree(numeric_name);
471 numeric_standard = TRUE;
472 numeric_local = TRUE;
477 if (! numeric_name || strNE(numeric_name, newnum)) {
478 Safefree(numeric_name);
479 numeric_name = savepv(newnum);
480 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
481 numeric_local = TRUE;
484 #endif /* USE_LOCALE_NUMERIC */
488 perl_set_numeric_standard(void)
490 #ifdef USE_LOCALE_NUMERIC
492 if (! numeric_standard) {
493 setlocale(LC_NUMERIC, "C");
494 numeric_standard = TRUE;
495 numeric_local = FALSE;
498 #endif /* USE_LOCALE_NUMERIC */
502 perl_set_numeric_local(void)
504 #ifdef USE_LOCALE_NUMERIC
506 if (! numeric_local) {
507 setlocale(LC_NUMERIC, numeric_name);
508 numeric_standard = FALSE;
509 numeric_local = TRUE;
512 #endif /* USE_LOCALE_NUMERIC */
517 * Initialize locale awareness.
520 perl_init_i18nl10n(int printwarn)
524 * 1 = set ok or not applicable,
525 * 0 = fallback to C locale,
526 * -1 = fallback to C locale failed
531 #ifdef USE_LOCALE_CTYPE
532 char *curctype = NULL;
533 #endif /* USE_LOCALE_CTYPE */
534 #ifdef USE_LOCALE_COLLATE
535 char *curcoll = NULL;
536 #endif /* USE_LOCALE_COLLATE */
537 #ifdef USE_LOCALE_NUMERIC
539 #endif /* USE_LOCALE_NUMERIC */
540 char *lc_all = PerlEnv_getenv("LC_ALL");
541 char *lang = PerlEnv_getenv("LANG");
542 bool setlocale_failure = FALSE;
544 #ifdef LOCALE_ENVIRON_REQUIRED
547 * Ultrix setlocale(..., "") fails if there are no environment
548 * variables from which to get a locale name.
555 if (setlocale(LC_ALL, ""))
558 setlocale_failure = TRUE;
560 if (!setlocale_failure)
563 #ifdef USE_LOCALE_CTYPE
564 if (! (curctype = setlocale(LC_CTYPE,
565 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
567 setlocale_failure = TRUE;
568 #endif /* USE_LOCALE_CTYPE */
569 #ifdef USE_LOCALE_COLLATE
570 if (! (curcoll = setlocale(LC_COLLATE,
571 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
573 setlocale_failure = TRUE;
574 #endif /* USE_LOCALE_COLLATE */
575 #ifdef USE_LOCALE_NUMERIC
576 if (! (curnum = setlocale(LC_NUMERIC,
577 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
579 setlocale_failure = TRUE;
580 #endif /* USE_LOCALE_NUMERIC */
583 #else /* !LOCALE_ENVIRON_REQUIRED */
587 if (! setlocale(LC_ALL, ""))
588 setlocale_failure = TRUE;
590 #ifdef USE_LOCALE_CTYPE
591 curctype = setlocale(LC_CTYPE, Nullch);
592 #endif /* USE_LOCALE_CTYPE */
593 #ifdef USE_LOCALE_COLLATE
594 curcoll = setlocale(LC_COLLATE, Nullch);
595 #endif /* USE_LOCALE_COLLATE */
596 #ifdef USE_LOCALE_NUMERIC
597 curnum = setlocale(LC_NUMERIC, Nullch);
598 #endif /* USE_LOCALE_NUMERIC */
603 #ifdef USE_LOCALE_CTYPE
604 if (! (curctype = setlocale(LC_CTYPE, "")))
605 setlocale_failure = TRUE;
606 #endif /* USE_LOCALE_CTYPE */
607 #ifdef USE_LOCALE_COLLATE
608 if (! (curcoll = setlocale(LC_COLLATE, "")))
609 setlocale_failure = TRUE;
610 #endif /* USE_LOCALE_COLLATE */
611 #ifdef USE_LOCALE_NUMERIC
612 if (! (curnum = setlocale(LC_NUMERIC, "")))
613 setlocale_failure = TRUE;
614 #endif /* USE_LOCALE_NUMERIC */
618 #endif /* !LOCALE_ENVIRON_REQUIRED */
620 if (setlocale_failure) {
622 bool locwarn = (printwarn > 1 ||
624 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
629 PerlIO_printf(PerlIO_stderr(),
630 "perl: warning: Setting locale failed.\n");
634 PerlIO_printf(PerlIO_stderr(),
635 "perl: warning: Setting locale failed for the categories:\n\t");
636 #ifdef USE_LOCALE_CTYPE
638 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
639 #endif /* USE_LOCALE_CTYPE */
640 #ifdef USE_LOCALE_COLLATE
642 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
643 #endif /* USE_LOCALE_COLLATE */
644 #ifdef USE_LOCALE_NUMERIC
646 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
647 #endif /* USE_LOCALE_NUMERIC */
648 PerlIO_printf(PerlIO_stderr(), "\n");
652 PerlIO_printf(PerlIO_stderr(),
653 "perl: warning: Please check that your locale settings:\n");
655 PerlIO_printf(PerlIO_stderr(),
656 "\tLC_ALL = %c%s%c,\n",
658 lc_all ? lc_all : "unset",
663 for (e = environ; *e; e++) {
664 if (strnEQ(*e, "LC_", 3)
665 && strnNE(*e, "LC_ALL=", 7)
666 && (p = strchr(*e, '=')))
667 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
668 (int)(p - *e), *e, p + 1);
672 PerlIO_printf(PerlIO_stderr(),
675 lang ? lang : "unset",
678 PerlIO_printf(PerlIO_stderr(),
679 " are supported and installed on your system.\n");
684 if (setlocale(LC_ALL, "C")) {
686 PerlIO_printf(PerlIO_stderr(),
687 "perl: warning: Falling back to the standard locale (\"C\").\n");
692 PerlIO_printf(PerlIO_stderr(),
693 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
700 #ifdef USE_LOCALE_CTYPE
701 || !(curctype || setlocale(LC_CTYPE, "C"))
702 #endif /* USE_LOCALE_CTYPE */
703 #ifdef USE_LOCALE_COLLATE
704 || !(curcoll || setlocale(LC_COLLATE, "C"))
705 #endif /* USE_LOCALE_COLLATE */
706 #ifdef USE_LOCALE_NUMERIC
707 || !(curnum || setlocale(LC_NUMERIC, "C"))
708 #endif /* USE_LOCALE_NUMERIC */
712 PerlIO_printf(PerlIO_stderr(),
713 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
717 #endif /* ! LC_ALL */
719 #ifdef USE_LOCALE_CTYPE
720 curctype = setlocale(LC_CTYPE, Nullch);
721 #endif /* USE_LOCALE_CTYPE */
722 #ifdef USE_LOCALE_COLLATE
723 curcoll = setlocale(LC_COLLATE, Nullch);
724 #endif /* USE_LOCALE_COLLATE */
725 #ifdef USE_LOCALE_NUMERIC
726 curnum = setlocale(LC_NUMERIC, Nullch);
727 #endif /* USE_LOCALE_NUMERIC */
730 #ifdef USE_LOCALE_CTYPE
731 perl_new_ctype(curctype);
732 #endif /* USE_LOCALE_CTYPE */
734 #ifdef USE_LOCALE_COLLATE
735 perl_new_collate(curcoll);
736 #endif /* USE_LOCALE_COLLATE */
738 #ifdef USE_LOCALE_NUMERIC
739 perl_new_numeric(curnum);
740 #endif /* USE_LOCALE_NUMERIC */
742 #endif /* USE_LOCALE */
747 /* Backwards compatibility. */
749 perl_init_i18nl14n(int printwarn)
751 return perl_init_i18nl10n(printwarn);
754 #ifdef USE_LOCALE_COLLATE
757 * mem_collxfrm() is a bit like strxfrm() but with two important
758 * differences. First, it handles embedded NULs. Second, it allocates
759 * a bit more memory than needed for the transformed data itself.
760 * The real transformed data begins at offset sizeof(collationix).
761 * Please see sv_collxfrm() to see how this is used.
764 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
767 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
769 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
770 /* the +1 is for the terminating NUL. */
772 xAlloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
773 New(171, xbuf, xAlloc, char);
777 *(U32*)xbuf = collation_ix;
778 xout = sizeof(collation_ix);
779 for (xin = 0; xin < len; ) {
783 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
786 if (xused < xAlloc - xout)
788 xAlloc = (2 * xAlloc) + 1;
789 Renew(xbuf, xAlloc, char);
794 xin += strlen(s + xin) + 1;
797 /* Embedded NULs are understood but silently skipped
798 * because they make no sense in locale collation. */
802 *xlen = xout - sizeof(collation_ix);
811 #endif /* USE_LOCALE_COLLATE */
816 register unsigned char *s;
817 register unsigned char *table;
819 register U32 len = SvCUR(sv);
823 sv_upgrade(sv, SVt_PVBM);
824 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
825 return; /* can't have offsets that big */
827 Sv_Grow(sv,len + 258);
828 table = (unsigned char*)(SvPVX(sv) + len + 1);
830 for (i = 0; i < 256; i++) {
834 while (s >= (unsigned char*)(SvPVX(sv)))
836 if (table[*s] == len)
841 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
844 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
845 for (i = 0; i < len; i++) {
846 if (freq[s[i]] < frequency) {
848 frequency = freq[s[i]];
851 BmRARE(sv) = s[rarest];
852 BmPREVIOUS(sv) = rarest;
853 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
857 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
859 register unsigned char *s;
861 register I32 littlelen;
862 register unsigned char *little;
863 register unsigned char *table;
864 register unsigned char *olds;
865 register unsigned char *oldlittle;
867 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
869 char *l = SvPV(littlestr,len);
871 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
872 substr => we can ignore SvVALID */
875 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
880 if (bigend > big && bigend[-1] == '\n')
881 return (char *)(bigend - 1);
883 return (char *) bigend;
887 return ninstr((char*)big,(char*)bigend, l, l + len);
890 littlelen = SvCUR(littlestr);
891 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
892 if (littlelen > bigend - big)
894 little = (unsigned char*)SvPVX(littlestr);
895 s = bigend - littlelen;
897 && bigend[-1] == '\n'
898 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
899 return (char*)s - 1; /* how sweet it is */
900 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
901 return (char*)s; /* how sweet it is */
904 if (littlelen <= 2) {
905 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
906 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
907 /* This may do extra comparisons if littlelen == 2, but this
908 should be hidden in the noise since we do less indirection. */
912 while (s <= bigend) {
914 && (littlelen == 1 || s[1] == c2)
915 && (!SvTAIL(littlestr)
917 || s[littlelen] == '\n')) /* Automatically multiline */
925 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
926 if (--littlelen >= bigend - big)
929 oldlittle = little = table - 2;
933 if (tmp = table[*s]) {
935 if (bigend - s > tmp) {
940 if ((s += tmp) < bigend)
946 tmp = littlelen; /* less expensive than calling strncmp() */
949 if (*--s == *--little)
952 s = olds + 1; /* here we pay the price for failure */
954 if (s < bigend) /* fake up continue to outer loop */
958 if (SvTAIL(littlestr) /* automatically multiline */
959 && olds + 1 != bigend
968 /* start_shift, end_shift are positive quantities which give offsets
969 of ends of some substring of bigstr.
970 If `last' we want the last occurence.
971 old_posp is the way of communication between consequent calls if
972 the next call needs to find the .
973 The initial *old_posp should be -1.
974 Note that we do not take into account SvTAIL, so it may give wrong
975 positives if _ALL flag is set.
979 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
981 register unsigned char *s, *x;
982 register unsigned char *big;
984 register I32 previous;
986 register unsigned char *little;
987 register I32 stop_pos;
988 register unsigned char *littleend;
992 ? (pos = screamfirst[BmRARE(littlestr)]) < 0
993 : (((pos = *old_posp), pos += screamnext[pos]) == 0))
995 little = (unsigned char *)(SvPVX(littlestr));
996 littleend = little + SvCUR(littlestr);
998 /* The value of pos we can start at: */
999 previous = BmPREVIOUS(littlestr);
1000 big = (unsigned char *)(SvPVX(bigstr));
1001 /* The value of pos we can stop at: */
1002 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1003 if (previous + start_shift > stop_pos) return Nullch;
1004 while (pos < previous + start_shift) {
1005 if (!(pos += screamnext[pos]))
1010 if (pos >= stop_pos) return Nullch;
1011 if (big[pos-previous] != first)
1013 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1019 if (s == littleend) {
1021 if (!last) return (char *)(big+pos-previous);
1024 } while ( pos += screamnext[pos] );
1025 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
1026 #else /* !POINTERRIGOR */
1029 if (pos >= stop_pos) return Nullch;
1030 if (big[pos] != first)
1032 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1038 if (s == littleend) {
1040 if (!last) return (char *)(big+pos);
1043 } while ( pos += screamnext[pos] );
1044 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
1045 #endif /* POINTERRIGOR */
1049 ibcmp(char *s1, char *s2, register I32 len)
1051 register U8 *a = (U8 *)s1;
1052 register U8 *b = (U8 *)s2;
1054 if (*a != *b && *a != fold[*b])
1062 ibcmp_locale(char *s1, char *s2, register I32 len)
1064 register U8 *a = (U8 *)s1;
1065 register U8 *b = (U8 *)s2;
1067 if (*a != *b && *a != fold_locale[*b])
1074 /* copy a string to a safe spot */
1079 register char *newaddr;
1081 New(902,newaddr,strlen(sv)+1,char);
1082 (void)strcpy(newaddr,sv);
1086 /* same thing but with a known length */
1089 savepvn(char *sv, register I32 len)
1091 register char *newaddr;
1093 New(903,newaddr,len+1,char);
1094 Copy(sv,newaddr,len,char); /* might not be null terminated */
1095 newaddr[len] = '\0'; /* is now */
1099 /* the SV for form() and mess() is not kept in an arena */
1107 /* Create as PVMG now, to avoid any upgrading later */
1108 New(905, sv, 1, SV);
1109 Newz(905, any, 1, XPVMG);
1110 SvFLAGS(sv) = SVt_PVMG;
1111 SvANY(sv) = (void*)any;
1112 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1118 form(const char* pat, ...)
1129 va_start(args, pat);
1134 mess_sv = mess_alloc();
1135 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1137 return SvPVX(mess_sv);
1141 mess(const char *pat, va_list *args)
1144 static char dgd[] = " during global destruction.\n";
1147 mess_sv = mess_alloc();
1149 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1150 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1155 if (curcop->cop_line)
1156 sv_catpvf(sv, " at %_ line %ld",
1157 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1158 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1159 bool line_mode = (RsSIMPLE(rs) &&
1160 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1161 sv_catpvf(sv, ", <%s> %s %ld",
1162 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1163 line_mode ? "line" : "chunk",
1164 (long)IoLINES(GvIOp(last_in_gv)));
1166 sv_catpv(sv, ".\n");
1174 die(const char* pat, ...)
1186 int was_in_eval = in_eval;
1192 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1193 "%p: die: curstack = %p, mainstack = %p\n",
1194 thr, curstack, mainstack));
1195 #endif /* USE_THREADS */
1196 /* We have to switch back to mainstack or die_where may try to pop
1197 * the eval block from the wrong stack if die is being called from a
1198 * signal handler. - dkindred@cs.cmu.edu */
1199 if (curstack != mainstack) {
1201 SWITCHSTACK(curstack, mainstack);
1205 va_start(args, pat);
1209 message = mess(pat, &args);
1213 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1214 "%p: die: message = %s\ndiehook = %p\n",
1215 thr, message, diehook));
1216 #endif /* USE_THREADS */
1218 /* sv_2cv might call croak() */
1219 SV *olddiehook = diehook;
1223 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1225 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1230 msg = newSVpv(message, 0);
1237 perl_call_sv((SV*)cv, G_DISCARD);
1243 restartop = die_where(message);
1245 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1246 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1247 thr, restartop, was_in_eval, top_env));
1248 #endif /* USE_THREADS */
1249 if ((!restartop && was_in_eval) || top_env->je_prev)
1256 croak(const char* pat, ...)
1260 croak(pat, va_alist)
1273 va_start(args, pat);
1277 message = mess(pat, &args);
1280 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1281 #endif /* USE_THREADS */
1283 /* sv_2cv might call croak() */
1284 SV *olddiehook = diehook;
1288 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1290 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1295 msg = newSVpv(message, 0);
1302 perl_call_sv((SV*)cv, G_DISCARD);
1308 restartop = die_where(message);
1311 PerlIO_puts(PerlIO_stderr(),message);
1312 (void)PerlIO_flush(PerlIO_stderr());
1318 warn(const char* pat,...)
1333 va_start(args, pat);
1337 message = mess(pat, &args);
1341 /* sv_2cv might call warn() */
1343 SV *oldwarnhook = warnhook;
1347 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1349 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1354 msg = newSVpv(message, 0);
1361 perl_call_sv((SV*)cv, G_DISCARD);
1367 PerlIO_puts(PerlIO_stderr(),message);
1371 (void)PerlIO_flush(PerlIO_stderr());
1374 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1377 my_setenv(char *nam, char *val)
1379 register I32 i=setenv_getix(nam); /* where does it go? */
1381 if (environ == origenviron) { /* need we copy environment? */
1387 for (max = i; environ[max]; max++) ;
1388 New(901,tmpenv, max+2, char*);
1389 for (j=0; j<max; j++) /* copy environment */
1390 tmpenv[j] = savepv(environ[j]);
1391 tmpenv[max] = Nullch;
1392 environ = tmpenv; /* tell exec where it is now */
1395 Safefree(environ[i]);
1396 while (environ[i]) {
1397 environ[i] = environ[i+1];
1402 if (!environ[i]) { /* does not exist yet */
1403 Renew(environ, i+2, char*); /* just expand it a bit */
1404 environ[i+1] = Nullch; /* make sure it's null terminated */
1407 Safefree(environ[i]);
1408 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1410 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1412 /* MS-DOS requires environment variable names to be in uppercase */
1413 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1414 * some utilities and applications may break because they only look
1415 * for upper case strings. (Fixed strupr() bug here.)]
1417 strcpy(environ[i],nam); strupr(environ[i]);
1418 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1422 #else /* if WIN32 */
1425 my_setenv(char *nam,char *val)
1428 #ifdef USE_WIN32_RTL_ENV
1430 register char *envstr;
1431 STRLEN namlen = strlen(nam);
1433 char *oldstr = environ[setenv_getix(nam)];
1435 /* putenv() has totally broken semantics in both the Borland
1436 * and Microsoft CRTLs. They either store the passed pointer in
1437 * the environment without making a copy, or make a copy and don't
1438 * free it. And on top of that, they dont free() old entries that
1439 * are being replaced/deleted. This means the caller must
1440 * free any old entries somehow, or we end up with a memory
1441 * leak every time my_setenv() is called. One might think
1442 * one could directly manipulate environ[], like the UNIX code
1443 * above, but direct changes to environ are not allowed when
1444 * calling putenv(), since the RTLs maintain an internal
1445 * *copy* of environ[]. Bad, bad, *bad* stink.
1456 vallen = strlen(val);
1457 New(904, envstr, namlen + vallen + 3, char);
1458 (void)sprintf(envstr,"%s=%s",nam,val);
1459 (void)PerlEnv_putenv(envstr);
1463 Safefree(envstr); /* MSVCRT leaks without this */
1466 #else /* !USE_WIN32_RTL_ENV */
1468 /* The sane way to deal with the environment.
1469 * Has these advantages over putenv() & co.:
1470 * * enables us to store a truly empty value in the
1471 * environment (like in UNIX).
1472 * * we don't have to deal with RTL globals, bugs and leaks.
1474 * Why you may want to enable USE_WIN32_RTL_ENV:
1475 * * environ[] and RTL functions will not reflect changes,
1476 * which might be an issue if extensions want to access
1477 * the env. via RTL. This cuts both ways, since RTL will
1478 * not see changes made by extensions that call the Win32
1479 * functions directly, either.
1482 SetEnvironmentVariable(nam,val);
1490 setenv_getix(char *nam)
1492 register I32 i, len = strlen(nam);
1494 for (i = 0; environ[i]; i++) {
1497 strnicmp(environ[i],nam,len) == 0
1499 strnEQ(environ[i],nam,len)
1501 && environ[i][len] == '=')
1502 break; /* strnEQ must come first to avoid */
1503 } /* potential SEGV's */
1509 #ifdef UNLINK_ALL_VERSIONS
1511 unlnk(f) /* unlink all versions of a file */
1516 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
1521 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1523 my_bcopy(register char *from,register char *to,register I32 len)
1527 if (from - to >= 0) {
1535 *(--to) = *(--from);
1543 my_memset(loc,ch,len)
1556 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1570 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1572 my_memcmp(s1,s2,len)
1577 register U8 *a = (U8 *)s1;
1578 register U8 *b = (U8 *)s2;
1582 if (tmp = *a++ - *b++)
1587 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1589 #if defined(I_STDARG) || defined(I_VARARGS)
1592 #ifdef USE_CHAR_VSPRINTF
1597 vsprintf(dest, pat, args)
1604 fakebuf._ptr = dest;
1605 fakebuf._cnt = 32767;
1609 fakebuf._flag = _IOWRT|_IOSTRG;
1610 _doprnt(pat, args, &fakebuf); /* what a kludge */
1611 (void)putc('\0', &fakebuf);
1612 #ifdef USE_CHAR_VSPRINTF
1615 return 0; /* perl doesn't use return value */
1619 #endif /* HAS_VPRINTF */
1620 #endif /* I_VARARGS || I_STDARGS */
1623 #if BYTEORDER != 0x4321
1625 #ifndef CAN_PROTOTYPE
1632 #if (BYTEORDER & 1) == 0
1635 result = ((s & 255) << 8) + ((s >> 8) & 255);
1643 #ifndef CAN_PROTOTYPE
1652 char c[sizeof(long)];
1655 #if BYTEORDER == 0x1234
1656 u.c[0] = (l >> 24) & 255;
1657 u.c[1] = (l >> 16) & 255;
1658 u.c[2] = (l >> 8) & 255;
1662 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1663 croak("Unknown BYTEORDER\n");
1668 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1669 u.c[o & 0xf] = (l >> s) & 255;
1677 #ifndef CAN_PROTOTYPE
1686 char c[sizeof(long)];
1689 #if BYTEORDER == 0x1234
1690 u.c[0] = (l >> 24) & 255;
1691 u.c[1] = (l >> 16) & 255;
1692 u.c[2] = (l >> 8) & 255;
1696 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1697 croak("Unknown BYTEORDER\n");
1704 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1705 l |= (u.c[o & 0xf] & 255) << s;
1712 #endif /* BYTEORDER != 0x4321 */
1716 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1717 * If these functions are defined,
1718 * the BYTEORDER is neither 0x1234 nor 0x4321.
1719 * However, this is not assumed.
1723 #define HTOV(name,type) \
1730 char c[sizeof(type)]; \
1734 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1735 u.c[i] = (n >> s) & 0xFF; \
1740 #define VTOH(name,type) \
1747 char c[sizeof(type)]; \
1753 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1754 n += (u.c[i] & 0xFF) << s; \
1759 #if defined(HAS_HTOVS) && !defined(htovs)
1762 #if defined(HAS_HTOVL) && !defined(htovl)
1765 #if defined(HAS_VTOHS) && !defined(vtohs)
1768 #if defined(HAS_VTOHL) && !defined(vtohl)
1772 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1773 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1775 my_popen(char *cmd, char *mode)
1778 register I32 This, that;
1781 I32 doexec = strNE(cmd,"-");
1785 return my_syspopen(cmd,mode);
1788 if (PerlProc_pipe(p) < 0)
1790 This = (*mode == 'w');
1792 if (doexec && tainting) {
1794 taint_proper("Insecure %s%s", "EXEC");
1796 while ((pid = (doexec?vfork():fork())) < 0) {
1797 if (errno != EAGAIN) {
1798 PerlLIO_close(p[This]);
1800 croak("Can't fork");
1810 PerlLIO_close(p[THAT]);
1811 if (p[THIS] != (*mode == 'r')) {
1812 PerlLIO_dup2(p[THIS], *mode == 'r');
1813 PerlLIO_close(p[THIS]);
1816 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1822 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1825 do_exec(cmd); /* may or may not use the shell */
1829 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1830 sv_setiv(GvSV(tmpgv), (IV)getpid());
1832 hv_clear(pidstatus); /* we have no children */
1837 do_execfree(); /* free any memory malloced by child on vfork */
1838 PerlLIO_close(p[that]);
1839 if (p[that] < p[This]) {
1840 PerlLIO_dup2(p[This], p[that]);
1841 PerlLIO_close(p[This]);
1844 sv = *av_fetch(fdpid,p[This],TRUE);
1845 (void)SvUPGRADE(sv,SVt_IV);
1848 return PerlIO_fdopen(p[This], mode);
1851 #if defined(atarist) || defined(DJGPP)
1858 /* Needs work for PerlIO ! */
1859 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1860 return popen(PerlIO_exportFILE(cmd, 0), mode);
1864 #endif /* !DOSISH */
1871 struct stat tmpstatbuf;
1873 PerlIO_printf(PerlIO_stderr(),"%s", s);
1874 for (fd = 0; fd < 32; fd++) {
1875 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
1876 PerlIO_printf(PerlIO_stderr()," %d",fd);
1878 PerlIO_printf(PerlIO_stderr(),"\n");
1888 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1891 PerlLIO_close(newfd);
1892 return fcntl(oldfd, F_DUPFD, newfd);
1894 #define DUP2_MAX_FDS 256
1895 int fdtmp[DUP2_MAX_FDS];
1901 PerlLIO_close(newfd);
1902 /* good enough for low fd's... */
1903 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
1904 if (fdx >= DUP2_MAX_FDS) {
1912 PerlLIO_close(fdtmp[--fdx]);
1919 #ifdef HAS_SIGACTION
1922 rsignal(int signo, Sighandler_t handler)
1924 struct sigaction act, oact;
1926 act.sa_handler = handler;
1927 sigemptyset(&act.sa_mask);
1930 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1932 if (sigaction(signo, &act, &oact) == -1)
1935 return oact.sa_handler;
1939 rsignal_state(int signo)
1941 struct sigaction oact;
1943 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1946 return oact.sa_handler;
1950 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
1952 struct sigaction act;
1954 act.sa_handler = handler;
1955 sigemptyset(&act.sa_mask);
1958 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1960 return sigaction(signo, &act, save);
1964 rsignal_restore(int signo, Sigsave_t *save)
1966 return sigaction(signo, save, (struct sigaction *)NULL);
1969 #else /* !HAS_SIGACTION */
1972 rsignal(int signo, Sighandler_t handler)
1974 return PerlProc_signal(signo, handler);
1977 static int sig_trapped;
1987 rsignal_state(int signo)
1989 Sighandler_t oldsig;
1992 oldsig = PerlProc_signal(signo, sig_trap);
1993 PerlProc_signal(signo, oldsig);
1995 PerlProc_kill(getpid(), signo);
2000 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2002 *save = PerlProc_signal(signo, handler);
2003 return (*save == SIG_ERR) ? -1 : 0;
2007 rsignal_restore(int signo, Sigsave_t *save)
2009 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
2012 #endif /* !HAS_SIGACTION */
2014 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2015 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2017 my_pclose(PerlIO *ptr)
2019 Sigsave_t hstat, istat, qstat;
2026 int saved_vaxc_errno;
2029 int saved_win32_errno;
2032 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2033 pid = (int)SvIVX(*svp);
2037 if (pid == -1) { /* Opened by popen. */
2038 return my_syspclose(ptr);
2041 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2042 saved_errno = errno;
2044 saved_vaxc_errno = vaxc$errno;
2047 saved_win32_errno = GetLastError();
2051 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2053 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2054 rsignal_save(SIGINT, SIG_IGN, &istat);
2055 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2057 pid = wait4pid(pid, &status, 0);
2058 } while (pid == -1 && errno == EINTR);
2059 rsignal_restore(SIGHUP, &hstat);
2060 rsignal_restore(SIGINT, &istat);
2061 rsignal_restore(SIGQUIT, &qstat);
2063 SETERRNO(saved_errno, saved_vaxc_errno);
2066 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
2068 #endif /* !DOSISH */
2070 #if !defined(DOSISH) || defined(OS2) || defined(WIN32)
2072 wait4pid(int pid, int *statusp, int flags)
2076 char spid[TYPE_CHARS(int)];
2081 sprintf(spid, "%d", pid);
2082 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2083 if (svp && *svp != &sv_undef) {
2084 *statusp = SvIVX(*svp);
2085 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2092 hv_iterinit(pidstatus);
2093 if (entry = hv_iternext(pidstatus)) {
2094 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2095 sv = hv_iterval(pidstatus,entry);
2096 *statusp = SvIVX(sv);
2097 sprintf(spid, "%d", pid);
2098 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2103 # ifdef HAS_WAITPID_RUNTIME
2104 if (!HAS_WAITPID_RUNTIME)
2107 return waitpid(pid,statusp,flags);
2109 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2110 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2112 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2117 croak("Can't do waitpid with flags");
2119 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
2120 pidgone(result,*statusp);
2128 #endif /* !DOSISH || OS2 || WIN32 */
2132 pidgone(int pid, int status)
2135 char spid[TYPE_CHARS(int)];
2137 sprintf(spid, "%d", pid);
2138 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2139 (void)SvUPGRADE(sv,SVt_IV);
2144 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2147 int /* Cannot prototype with I32
2156 /* Needs work for PerlIO ! */
2157 FILE *f = PerlIO_findFILE(ptr);
2158 I32 result = pclose(f);
2159 PerlIO_releaseFILE(ptr,f);
2165 repeatcpy(register char *to, register char *from, I32 len, register I32 count)
2168 register char *frombase = from;
2176 while (count-- > 0) {
2177 for (todo = len; todo > 0; todo--) {
2184 #ifndef CASTNEGFLOAT
2192 # define BIGDOUBLE 2147483648.0
2194 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2197 return (unsigned long)f;
2199 return (unsigned long)along;
2206 /* Unfortunately, on some systems the cast_uv() function doesn't
2207 work with the system-supplied definition of ULONG_MAX. The
2208 comparison (f >= ULONG_MAX) always comes out true. It must be a
2209 problem with the compiler constant folding.
2211 In any case, this workaround should be fine on any two's complement
2212 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2214 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2217 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2219 -- Kenneth Albanowski <kjahds@kjahds.com>
2223 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2231 return (I32) I32_MAX;
2233 return (I32) I32_MIN;
2253 return (UV) MY_UV_MAX;
2265 char *fa = strrchr(a,'/');
2266 char *fb = strrchr(b,'/');
2267 struct stat tmpstatbuf1;
2268 struct stat tmpstatbuf2;
2269 SV *tmpsv = sv_newmortal();
2282 sv_setpv(tmpsv, ".");
2284 sv_setpvn(tmpsv, a, fa - a);
2285 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2288 sv_setpv(tmpsv, ".");
2290 sv_setpvn(tmpsv, b, fb - b);
2291 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2293 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2294 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2296 #endif /* !HAS_RENAME */
2299 scan_oct(char *start, I32 len, I32 *retlen)
2301 register char *s = start;
2302 register UV retval = 0;
2303 bool overflowed = FALSE;
2305 while (len && *s >= '0' && *s <= '7') {
2306 register UV n = retval << 3;
2307 if (!overflowed && (n >> 3) != retval) {
2308 warn("Integer overflow in octal number");
2311 retval = n | (*s++ - '0');
2314 if (dowarn && len && (*s == '8' || *s == '9'))
2315 warn("Illegal octal digit ignored");
2316 *retlen = s - start;
2321 scan_hex(char *start, I32 len, I32 *retlen)
2323 register char *s = start;
2324 register UV retval = 0;
2325 bool overflowed = FALSE;
2328 while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
2329 register UV n = retval << 4;
2330 if (!overflowed && (n >> 4) != retval) {
2331 warn("Integer overflow in hex number");
2334 retval = n | ((tmp - hexdigit) & 15);
2337 *retlen = s - start;
2343 /* Very simplistic scheduler for now */
2347 thr = thr->i.next_run;
2358 perl_cond_signal(cp)
2362 perl_cond cond = *cp;
2367 /* Insert t in the runnable queue just ahead of us */
2368 t->i.next_run = thr->i.next_run;
2369 thr->i.next_run->i.prev_run = t;
2370 t->i.prev_run = thr;
2371 thr->i.next_run = t;
2372 thr->i.wait_queue = 0;
2373 /* Remove from the wait queue */
2379 perl_cond_broadcast(cp)
2383 perl_cond cond, cond_next;
2385 for (cond = *cp; cond; cond = cond_next) {
2387 /* Insert t in the runnable queue just ahead of us */
2388 t->i.next_run = thr->i.next_run;
2389 thr->i.next_run->i.prev_run = t;
2390 t->i.prev_run = thr;
2391 thr->i.next_run = t;
2392 thr->i.wait_queue = 0;
2393 /* Remove from the wait queue */
2394 cond_next = cond->next;
2406 if (thr->i.next_run == thr)
2407 croak("panic: perl_cond_wait called by last runnable thread");
2409 New(666, cond, 1, struct perl_wait_queue);
2413 thr->i.wait_queue = cond;
2414 /* Remove ourselves from runnable queue */
2415 thr->i.next_run->i.prev_run = thr->i.prev_run;
2416 thr->i.prev_run->i.next_run = thr->i.next_run;
2418 #endif /* FAKE_THREADS */
2420 #ifdef OLD_PTHREADS_API
2421 struct perl_thread *
2426 if (pthread_getspecific(thr_key, &t))
2427 croak("panic: pthread_getspecific");
2428 return (struct perl_thread *) t;
2430 #endif /* OLD_PTHREADS_API */
2433 condpair_magic(SV *sv)
2437 SvUPGRADE(sv, SVt_PVMG);
2438 mg = mg_find(sv, 'm');
2442 New(53, cp, 1, condpair_t);
2443 MUTEX_INIT(&cp->mutex);
2444 COND_INIT(&cp->owner_cond);
2445 COND_INIT(&cp->cond);
2447 MUTEX_LOCK(&sv_mutex);
2448 mg = mg_find(sv, 'm');
2450 /* someone else beat us to initialising it */
2451 MUTEX_UNLOCK(&sv_mutex);
2452 MUTEX_DESTROY(&cp->mutex);
2453 COND_DESTROY(&cp->owner_cond);
2454 COND_DESTROY(&cp->cond);
2458 sv_magic(sv, Nullsv, 'm', 0, 0);
2460 mg->mg_ptr = (char *)cp;
2461 mg->mg_len = sizeof(cp);
2462 MUTEX_UNLOCK(&sv_mutex);
2463 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2464 "%p: condpair_magic %p\n", thr, sv));)
2471 * Make a new perl thread structure using t as a prototype. Some of the
2472 * fields for the new thread are copied from the prototype thread, t,
2473 * so t should not be running in perl at the time this function is
2474 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2475 * thread calling new_struct_thread) clearly satisfies this constraint.
2477 struct perl_thread *
2478 new_struct_thread(struct perl_thread *t)
2480 struct perl_thread *thr;
2485 sv = newSVpv("", 0);
2486 SvGROW(sv, sizeof(struct perl_thread) + 1);
2487 SvCUR_set(sv, sizeof(struct perl_thread));
2488 thr = (Thread) SvPVX(sv);
2490 memset(thr, 0xab, sizeof(struct perl_thread));
2502 curcop = &compiling;
2503 thr->cvcache = newHV();
2504 thr->threadsv = newAV();
2505 thr->specific = newAV();
2506 thr->errsv = newSVpv("", 0);
2507 thr->errhv = newHV();
2508 thr->flags = THRf_R_JOINABLE;
2509 MUTEX_INIT(&thr->mutex);
2511 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2512 defstash = t->Tdefstash; /* XXX maybe these should */
2513 curstash = t->Tcurstash; /* always be set to main? */
2516 /* top_env needs to be non-zero. It points to an area
2517 in which longjmp() stuff is stored, as C callstack
2518 info there at least is thread specific this has to
2519 be per-thread. Otherwise a 'die' in a thread gives
2520 that thread the C stack of last thread to do an eval {}!
2521 See comments in scope.h
2522 Initialize top entry (as in perl.c for main thread)
2524 start_env.je_prev = NULL;
2525 start_env.je_ret = -1;
2526 start_env.je_mustcatch = TRUE;
2527 top_env = &start_env;
2532 tainted = t->Ttainted;
2533 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2534 nrs = newSVsv(t->Tnrs);
2535 rs = newSVsv(t->Trs);
2536 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2537 ofslen = t->Tofslen;
2538 ofs = savepvn(t->Tofs, ofslen);
2539 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2540 chopset = t->Tchopset;
2541 formtarget = newSVsv(t->Tformtarget);
2542 bodytarget = newSVsv(t->Tbodytarget);
2543 toptarget = newSVsv(t->Ttoptarget);
2545 /* Initialise all per-thread SVs that the template thread used */
2546 svp = AvARRAY(t->threadsv);
2547 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
2548 if (*svp && *svp != &sv_undef) {
2549 SV *sv = newSVsv(*svp);
2550 av_store(thr->threadsv, i, sv);
2551 sv_magic(sv, 0, 0, &threadsv_names[i], 1);
2552 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2553 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
2557 MUTEX_LOCK(&threads_mutex);
2559 thr->tid = ++threadnum;
2560 thr->next = t->next;
2563 thr->next->prev = thr;
2564 MUTEX_UNLOCK(&threads_mutex);
2566 #ifdef HAVE_THREAD_INTERN
2567 init_thread_intern(thr);
2568 #endif /* HAVE_THREAD_INTERN */
2571 #endif /* USE_THREADS */
2575 * This hack is to force load of "huge" support from libm.a
2576 * So it is in perl for (say) POSIX to use.
2577 * Needed for SunOS with Sun's 'acc' for example.
2586 #ifdef PERL_GLOBAL_STRUCT