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
18 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
23 # define SIG_ERR ((Sighandler_t) -1)
26 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
35 /* Put this after #includes because fork and vfork prototypes may
46 # include <sys/file.h>
50 # include <sys/wait.h>
56 static void xstat _((void));
61 /* paranoid version of malloc */
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.
66 * XXX This advice seems to be widely ignored :-( --AD August 1996.
76 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
79 #endif /* HAS_64K_LIMIT */
82 croak("panic: malloc");
84 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
85 #if !(defined(I286) || defined(atarist))
86 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
88 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
95 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
101 /* paranoid version of realloc */
104 saferealloc(where,size)
109 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
111 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
115 PerlIO_printf(PerlIO_stderr(),
116 "Reallocation too large: %lx\n", size) FLUSH;
119 #endif /* HAS_64K_LIMIT */
121 croak("Null realloc");
124 croak("panic: realloc");
126 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
128 #if !(defined(I286) || defined(atarist))
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);
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);
145 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
151 /* safe version of free */
157 #if !(defined(I286) || defined(atarist))
158 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",where,an++));
160 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",where,an++));
168 /* safe version of calloc */
171 safecalloc(count, size)
178 if (size * count > 0xffff) {
179 PerlIO_printf(PerlIO_stderr(),
180 "Allocation too large: %lx\n", size * count) FLUSH;
183 #endif /* HAS_64K_LIMIT */
185 if ((long)size < 0 || (long)count < 0)
186 croak("panic: calloc");
189 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
190 #if !(defined(I286) || defined(atarist))
191 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
193 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
196 memset((void*)ptr, 0, size);
202 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
208 #endif /* !MYMALLOC */
212 #define ALIGN sizeof(long)
219 register Malloc_t where;
221 where = safemalloc(size + ALIGN);
225 return where + ALIGN;
229 safexrealloc(where,size)
233 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
246 x = where[0] + 100 * where[1];
252 safexcalloc(x,count,size)
257 register Malloc_t where;
259 where = safexmalloc(x, size * count + ALIGN);
261 memset((void*)where + ALIGN, 0, size * count);
264 return where + ALIGN;
272 for (i = 0; i < MAXXCOUNT; i++) {
273 if (xcount[i] > lastxcount[i]) {
274 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
275 lastxcount[i] = xcount[i];
280 #endif /* LEAKTEST */
282 /* copy a string up to some (non-backslashed) delimiter, if any */
285 delimcpy(to, toend, from, fromend, delim, retlen)
287 register char *toend;
289 register char *fromend;
294 for (tolen = 0; from < fromend; from++, tolen++) {
296 if (from[1] == delim)
305 else if (*from == delim)
316 /* return ptr to little string in big string, NULL if not found */
317 /* This routine was donated by Corey Satten. */
322 register char *little;
324 register char *s, *x;
335 for (x=big,s=little; *s; /**/ ) {
349 /* same as instr but allow embedded nulls */
352 ninstr(big, bigend, little, lend)
354 register char *bigend;
358 register char *s, *x;
359 register I32 first = *little;
360 register char *littleend = lend;
362 if (!first && little >= littleend)
364 if (bigend - big < littleend - little)
366 bigend -= littleend - little++;
367 while (big <= bigend) {
370 for (x=big,s=little; s < littleend; /**/ ) {
382 /* reverse of the above--find last substring */
385 rninstr(big, bigend, little, lend)
391 register char *bigbeg;
392 register char *s, *x;
393 register I32 first = *little;
394 register char *littleend = lend;
396 if (!first && little >= littleend)
399 big = bigend - (littleend - little++);
400 while (big >= bigbeg) {
403 for (x=big+2,s=little; s < littleend; /**/ ) {
416 * Set up for a new ctype locale.
419 perl_new_ctype(newctype)
422 #ifdef USE_LOCALE_CTYPE
426 for (i = 0; i < 256; i++) {
428 fold_locale[i] = toLOWER_LC(i);
429 else if (isLOWER_LC(i))
430 fold_locale[i] = toUPPER_LC(i);
435 #endif /* USE_LOCALE_CTYPE */
439 * Set up for a new collation locale.
442 perl_new_collate(newcoll)
445 #ifdef USE_LOCALE_COLLATE
448 if (collation_name) {
450 Safefree(collation_name);
451 collation_name = NULL;
452 collation_standard = TRUE;
459 if (! collation_name || strNE(collation_name, newcoll)) {
461 Safefree(collation_name);
462 collation_name = savepv(newcoll);
463 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
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;
474 croak("strxfrm() gets absurd");
475 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
476 collxfrm_mult = mult;
480 #endif /* USE_LOCALE_COLLATE */
484 * Set up for a new numeric locale.
487 perl_new_numeric(newnum)
490 #ifdef USE_LOCALE_NUMERIC
494 Safefree(numeric_name);
496 numeric_standard = TRUE;
497 numeric_local = TRUE;
502 if (! numeric_name || strNE(numeric_name, newnum)) {
503 Safefree(numeric_name);
504 numeric_name = savepv(newnum);
505 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
506 numeric_local = TRUE;
509 #endif /* USE_LOCALE_NUMERIC */
513 perl_set_numeric_standard()
515 #ifdef USE_LOCALE_NUMERIC
517 if (! numeric_standard) {
518 setlocale(LC_NUMERIC, "C");
519 numeric_standard = TRUE;
520 numeric_local = FALSE;
523 #endif /* USE_LOCALE_NUMERIC */
527 perl_set_numeric_local()
529 #ifdef USE_LOCALE_NUMERIC
531 if (! numeric_local) {
532 setlocale(LC_NUMERIC, numeric_name);
533 numeric_standard = FALSE;
534 numeric_local = TRUE;
537 #endif /* USE_LOCALE_NUMERIC */
542 * Initialize locale awareness.
545 perl_init_i18nl10n(printwarn)
550 * 1 = set ok or not applicable,
551 * 0 = fallback to C locale,
552 * -1 = fallback to C locale failed
557 #ifdef USE_LOCALE_CTYPE
558 char *curctype = NULL;
559 #endif /* USE_LOCALE_CTYPE */
560 #ifdef USE_LOCALE_COLLATE
561 char *curcoll = NULL;
562 #endif /* USE_LOCALE_COLLATE */
563 #ifdef USE_LOCALE_NUMERIC
565 #endif /* USE_LOCALE_NUMERIC */
566 char *lc_all = getenv("LC_ALL");
567 char *lang = getenv("LANG");
568 bool setlocale_failure = FALSE;
570 #ifdef LOCALE_ENVIRON_REQUIRED
573 * Ultrix setlocale(..., "") fails if there are no environment
574 * variables from which to get a locale name.
581 if (setlocale(LC_ALL, ""))
584 setlocale_failure = TRUE;
586 if (!setlocale_failure)
589 #ifdef USE_LOCALE_CTYPE
590 if (! (curctype = setlocale(LC_CTYPE,
591 (!done && (lang || getenv("LC_CTYPE")))
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")))
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")))
605 setlocale_failure = TRUE;
606 #endif /* USE_LOCALE_NUMERIC */
609 #else /* !LOCALE_ENVIRON_REQUIRED */
613 if (! setlocale(LC_ALL, ""))
614 setlocale_failure = TRUE;
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 */
629 #ifdef USE_LOCALE_CTYPE
630 if (! (curctype = setlocale(LC_CTYPE, "")))
631 setlocale_failure = TRUE;
632 #endif /* USE_LOCALE_CTYPE */
633 #ifdef USE_LOCALE_COLLATE
634 if (! (curcoll = setlocale(LC_COLLATE, "")))
635 setlocale_failure = TRUE;
636 #endif /* USE_LOCALE_COLLATE */
637 #ifdef USE_LOCALE_NUMERIC
638 if (! (curnum = setlocale(LC_NUMERIC, "")))
639 setlocale_failure = TRUE;
640 #endif /* USE_LOCALE_NUMERIC */
644 #endif /* !LOCALE_ENVIRON_REQUIRED */
646 if (setlocale_failure) {
648 bool locwarn = (printwarn > 1 ||
650 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
655 PerlIO_printf(PerlIO_stderr(),
656 "perl: warning: Setting locale failed.\n");
660 PerlIO_printf(PerlIO_stderr(),
661 "perl: warning: Setting locale failed for the categories:\n\t");
662 #ifdef USE_LOCALE_CTYPE
664 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
665 #endif /* USE_LOCALE_CTYPE */
666 #ifdef USE_LOCALE_COLLATE
668 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
669 #endif /* USE_LOCALE_COLLATE */
670 #ifdef USE_LOCALE_NUMERIC
672 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
673 #endif /* USE_LOCALE_NUMERIC */
674 PerlIO_printf(PerlIO_stderr(), "\n");
678 PerlIO_printf(PerlIO_stderr(),
679 "perl: warning: Please check that your locale settings:\n");
681 PerlIO_printf(PerlIO_stderr(),
682 "\tLC_ALL = %c%s%c,\n",
684 lc_all ? lc_all : "unset",
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",
694 (p - *e), *e, p + 1);
698 PerlIO_printf(PerlIO_stderr(),
701 lang ? lang : "unset",
704 PerlIO_printf(PerlIO_stderr(),
705 " are supported and installed on your system.\n");
710 if (setlocale(LC_ALL, "C")) {
712 PerlIO_printf(PerlIO_stderr(),
713 "perl: warning: Falling back to the standard locale (\"C\").\n");
718 PerlIO_printf(PerlIO_stderr(),
719 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
726 #ifdef USE_LOCALE_CTYPE
727 || !(curctype || setlocale(LC_CTYPE, "C"))
728 #endif /* USE_LOCALE_CTYPE */
729 #ifdef USE_LOCALE_COLLATE
730 || !(curcoll || setlocale(LC_COLLATE, "C"))
731 #endif /* USE_LOCALE_COLLATE */
732 #ifdef USE_LOCALE_NUMERIC
733 || !(curnum || setlocale(LC_NUMERIC, "C"))
734 #endif /* USE_LOCALE_NUMERIC */
738 PerlIO_printf(PerlIO_stderr(),
739 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
743 #endif /* ! LC_ALL */
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 */
756 #ifdef USE_LOCALE_CTYPE
757 perl_new_ctype(curctype);
758 #endif /* USE_LOCALE_CTYPE */
760 #ifdef USE_LOCALE_COLLATE
761 perl_new_collate(curcoll);
762 #endif /* USE_LOCALE_COLLATE */
764 #ifdef USE_LOCALE_NUMERIC
765 perl_new_numeric(curnum);
766 #endif /* USE_LOCALE_NUMERIC */
768 #endif /* USE_LOCALE */
773 /* Backwards compatibility. */
775 perl_init_i18nl14n(printwarn)
778 return perl_init_i18nl10n(printwarn);
781 #ifdef USE_LOCALE_COLLATE
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.
791 mem_collxfrm(s, len, xlen)
797 STRLEN xalloc, xin, xout;
799 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
800 /* the +1 is for the terminating NUL. */
802 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
803 New(171, xbuf, xalloc, char);
807 *(U32*)xbuf = collation_ix;
808 xout = sizeof(collation_ix);
809 for (xin = 0; xin < len; ) {
813 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
816 if (xused < xalloc - xout)
818 xalloc = (2 * xalloc) + 1;
819 Renew(xbuf, xalloc, char);
824 xin += strlen(s + xin) + 1;
827 /* Embedded NULs are understood but silently skipped
828 * because they make no sense in locale collation. */
832 *xlen = xout - sizeof(collation_ix);
841 #endif /* USE_LOCALE_COLLATE */
847 register unsigned char *s;
848 register unsigned char *table;
850 register U32 len = SvCUR(sv);
855 return; /* can't have offsets that big */
857 table = (unsigned char*)(SvPVX(sv) + len + 1);
859 for (i = 0; i < 256; i++) {
863 while (s >= (unsigned char*)(SvPVX(sv)))
865 if (table[*s] == len)
869 sv_upgrade(sv, SVt_PVBM);
870 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
873 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
874 for (i = 0; i < len; i++) {
875 if (freq[s[i]] < frequency) {
877 frequency = freq[s[i]];
880 BmRARE(sv) = s[rarest];
881 BmPREVIOUS(sv) = rarest;
882 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
886 fbm_instr(big, bigend, littlestr)
888 register unsigned char *bigend;
891 register unsigned char *s;
893 register I32 littlelen;
894 register unsigned char *little;
895 register unsigned char *table;
896 register unsigned char *olds;
897 register unsigned char *oldlittle;
899 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
901 char *l = SvPV(littlestr,len);
904 return ninstr((char*)big,(char*)bigend, l, l + len);
907 littlelen = SvCUR(littlestr);
908 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
909 if (littlelen > bigend - big)
911 little = (unsigned char*)SvPVX(littlestr);
912 s = bigend - littlelen;
913 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
914 return (char*)s; /* how sweet it is */
915 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
918 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
923 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
924 if (--littlelen >= bigend - big)
927 oldlittle = little = table - 2;
931 if (tmp = table[*s]) {
933 if (bigend - s > tmp) {
938 if ((s += tmp) < bigend)
944 tmp = littlelen; /* less expensive than calling strncmp() */
947 if (*--s == *--little)
949 s = olds + 1; /* here we pay the price for failure */
951 if (s < bigend) /* fake up continue to outer loop */
962 screaminstr(bigstr, littlestr)
966 register unsigned char *s, *x;
967 register unsigned char *big;
969 register I32 previous;
971 register unsigned char *little;
972 register unsigned char *bigend;
973 register unsigned char *littleend;
975 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
977 little = (unsigned char *)(SvPVX(littlestr));
978 littleend = little + SvCUR(littlestr);
980 previous = BmPREVIOUS(littlestr);
981 big = (unsigned char *)(SvPVX(bigstr));
982 bigend = big + SvCUR(bigstr);
983 while (pos < previous) {
984 if (!(pos += screamnext[pos]))
989 if (big[pos-previous] != first)
991 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1000 return (char *)(big+pos-previous);
1001 } while ( pos += screamnext[pos] );
1002 #else /* !POINTERRIGOR */
1005 if (big[pos] != first)
1007 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1016 return (char *)(big+pos);
1017 } while ( pos += screamnext[pos] );
1018 #endif /* POINTERRIGOR */
1027 register U8 *a = (U8 *)s1;
1028 register U8 *b = (U8 *)s2;
1030 if (*a != *b && *a != fold[*b])
1038 ibcmp_locale(s1, s2, len)
1042 register U8 *a = (U8 *)s1;
1043 register U8 *b = (U8 *)s2;
1045 if (*a != *b && *a != fold_locale[*b])
1052 /* copy a string to a safe spot */
1058 register char *newaddr;
1060 New(902,newaddr,strlen(sv)+1,char);
1061 (void)strcpy(newaddr,sv);
1065 /* same thing but with a known length */
1072 register char *newaddr;
1074 New(903,newaddr,len+1,char);
1075 Copy(sv,newaddr,len,char); /* might not be null terminated */
1076 newaddr[len] = '\0'; /* is now */
1080 /* the SV for form() and mess() is not kept in an arena */
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 */
1099 form(const char* pat, ...)
1110 va_start(args, pat);
1115 mess_sv = mess_alloc();
1116 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1118 return SvPVX(mess_sv);
1127 static char dgd[] = " during global destruction.\n";
1130 mess_sv = mess_alloc();
1132 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1133 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1138 if (curcop->cop_line)
1139 sv_catpvf(sv, " at %_ line %ld",
1140 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1141 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1142 bool line_mode = (RsSIMPLE(rs) &&
1143 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
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)));
1149 sv_catpv(sv, ".\n");
1157 die(const char* pat, ...)
1169 I32 oldrunlevel = runlevel;
1170 int was_in_eval = in_eval;
1175 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n",
1176 curstack, mainstack));/*debug*/
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) {
1182 SWITCHSTACK(curstack, mainstack);
1186 va_start(args, pat);
1190 message = mess(pat, &args);
1193 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n",
1194 message, diehook));/*debug*/
1196 /* sv_2cv might call croak() */
1197 SV *olddiehook = diehook;
1201 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1203 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1208 msg = newSVpv(message, 0);
1215 perl_call_sv((SV*)cv, G_DISCARD);
1221 restartop = die_where(message);
1222 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1223 "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
1224 restartop, was_in_eval, oldrunlevel));/*debug*/
1225 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1232 croak(const char* pat, ...)
1236 croak(pat, va_alist)
1249 va_start(args, pat);
1253 message = mess(pat, &args);
1256 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1257 #endif /* USE_THREADS */
1259 /* sv_2cv might call croak() */
1260 SV *olddiehook = diehook;
1264 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1266 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1271 msg = newSVpv(message, 0);
1278 perl_call_sv((SV*)cv, G_DISCARD);
1284 restartop = die_where(message);
1287 PerlIO_puts(PerlIO_stderr(),message);
1288 (void)PerlIO_flush(PerlIO_stderr());
1294 warn(const char* pat,...)
1309 va_start(args, pat);
1313 message = mess(pat, &args);
1317 /* sv_2cv might call warn() */
1319 SV *oldwarnhook = warnhook;
1323 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1325 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1330 msg = newSVpv(message, 0);
1337 perl_call_sv((SV*)cv, G_DISCARD);
1343 PerlIO_puts(PerlIO_stderr(),message);
1347 (void)PerlIO_flush(PerlIO_stderr());
1350 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1356 register I32 i=setenv_getix(nam); /* where does it go? */
1358 if (environ == origenviron) { /* need we copy environment? */
1364 for (max = i; environ[max]; max++) ;
1365 New(901,tmpenv, max+2, char*);
1366 for (j=0; j<max; j++) /* copy environment */
1367 tmpenv[j] = savepv(environ[j]);
1368 tmpenv[max] = Nullch;
1369 environ = tmpenv; /* tell exec where it is now */
1372 Safefree(environ[i]);
1373 while (environ[i]) {
1374 environ[i] = environ[i+1];
1379 if (!environ[i]) { /* does not exist yet */
1380 Renew(environ, i+2, char*); /* just expand it a bit */
1381 environ[i+1] = Nullch; /* make sure it's null terminated */
1384 Safefree(environ[i]);
1385 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1387 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1389 /* MS-DOS requires environment variable names to be in uppercase */
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.)]
1394 strcpy(environ[i],nam); strupr(environ[i]);
1395 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1399 #else /* if WIN32 */
1406 #ifdef USE_WIN32_RTL_ENV
1408 register char *envstr;
1409 STRLEN namlen = strlen(nam);
1411 char *oldstr = environ[setenv_getix(nam)];
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.
1434 vallen = strlen(val);
1435 New(904, envstr, namlen + vallen + 3, char);
1436 (void)sprintf(envstr,"%s=%s",nam,val);
1437 (void)putenv(envstr);
1441 Safefree(envstr); /* MSVCRT leaks without this */
1444 #else /* !USE_WIN32_RTL_ENV */
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.
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.
1460 SetEnvironmentVariable(nam,val);
1471 register I32 i, len = strlen(nam);
1473 for (i = 0; environ[i]; i++) {
1476 strnicmp(environ[i],nam,len) == 0
1478 strnEQ(environ[i],nam,len)
1480 && environ[i][len] == '=')
1481 break; /* strnEQ must come first to avoid */
1482 } /* potential SEGV's */
1488 #ifdef UNLINK_ALL_VERSIONS
1490 unlnk(f) /* unlink all versions of a file */
1495 for (i = 0; unlink(f) >= 0; i++) ;
1500 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1502 my_bcopy(from,to,len)
1503 register char *from;
1509 if (from - to >= 0) {
1517 *(--to) = *(--from);
1525 my_memset(loc,ch,len)
1538 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1552 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1554 my_memcmp(s1,s2,len)
1559 register U8 *a = (U8 *)s1;
1560 register U8 *b = (U8 *)s2;
1564 if (tmp = *a++ - *b++)
1569 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1571 #if defined(I_STDARG) || defined(I_VARARGS)
1574 #ifdef USE_CHAR_VSPRINTF
1579 vsprintf(dest, pat, args)
1586 fakebuf._ptr = dest;
1587 fakebuf._cnt = 32767;
1591 fakebuf._flag = _IOWRT|_IOSTRG;
1592 _doprnt(pat, args, &fakebuf); /* what a kludge */
1593 (void)putc('\0', &fakebuf);
1594 #ifdef USE_CHAR_VSPRINTF
1597 return 0; /* perl doesn't use return value */
1601 #endif /* HAS_VPRINTF */
1602 #endif /* I_VARARGS || I_STDARGS */
1605 #if BYTEORDER != 0x4321
1607 #ifndef CAN_PROTOTYPE
1614 #if (BYTEORDER & 1) == 0
1617 result = ((s & 255) << 8) + ((s >> 8) & 255);
1625 #ifndef CAN_PROTOTYPE
1634 char c[sizeof(long)];
1637 #if BYTEORDER == 0x1234
1638 u.c[0] = (l >> 24) & 255;
1639 u.c[1] = (l >> 16) & 255;
1640 u.c[2] = (l >> 8) & 255;
1644 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1645 croak("Unknown BYTEORDER\n");
1650 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1651 u.c[o & 0xf] = (l >> s) & 255;
1659 #ifndef CAN_PROTOTYPE
1668 char c[sizeof(long)];
1671 #if BYTEORDER == 0x1234
1672 u.c[0] = (l >> 24) & 255;
1673 u.c[1] = (l >> 16) & 255;
1674 u.c[2] = (l >> 8) & 255;
1678 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1679 croak("Unknown BYTEORDER\n");
1686 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1687 l |= (u.c[o & 0xf] & 255) << s;
1694 #endif /* BYTEORDER != 0x4321 */
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.
1705 #define HTOV(name,type) \
1712 char c[sizeof(type)]; \
1716 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1717 u.c[i] = (n >> s) & 0xFF; \
1722 #define VTOH(name,type) \
1729 char c[sizeof(type)]; \
1735 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1736 n += (u.c[i] & 0xFF) << s; \
1741 #if defined(HAS_HTOVS) && !defined(htovs)
1744 #if defined(HAS_HTOVL) && !defined(htovl)
1747 #if defined(HAS_VTOHS) && !defined(vtohs)
1750 #if defined(HAS_VTOHL) && !defined(vtohl)
1754 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1755 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1762 register I32 this, that;
1765 I32 doexec = strNE(cmd,"-");
1769 return my_syspopen(cmd,mode);
1774 this = (*mode == 'w');
1776 if (doexec && tainting) {
1778 taint_proper("Insecure %s%s", "EXEC");
1780 while ((pid = (doexec?vfork():fork())) < 0) {
1781 if (errno != EAGAIN) {
1784 croak("Can't fork");
1795 if (p[THIS] != (*mode == 'r')) {
1796 dup2(p[THIS], *mode == 'r');
1800 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1806 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1809 do_exec(cmd); /* may or may not use the shell */
1813 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1814 sv_setiv(GvSV(tmpgv), (IV)getpid());
1816 hv_clear(pidstatus); /* we have no children */
1821 do_execfree(); /* free any memory malloced by child on vfork */
1823 if (p[that] < p[this]) {
1824 dup2(p[this], p[that]);
1828 sv = *av_fetch(fdpid,p[this],TRUE);
1829 (void)SvUPGRADE(sv,SVt_IV);
1832 return PerlIO_fdopen(p[this], mode);
1835 #if defined(atarist) || defined(DJGPP)
1842 /* Needs work for PerlIO ! */
1843 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1844 return popen(PerlIO_exportFILE(cmd, 0), mode);
1848 #endif /* !DOSISH */
1855 struct stat tmpstatbuf;
1857 PerlIO_printf(PerlIO_stderr(),"%s", s);
1858 for (fd = 0; fd < 32; fd++) {
1859 if (Fstat(fd,&tmpstatbuf) >= 0)
1860 PerlIO_printf(PerlIO_stderr()," %d",fd);
1862 PerlIO_printf(PerlIO_stderr(),"\n");
1872 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1876 return fcntl(oldfd, F_DUPFD, newfd);
1878 #define DUP2_MAX_FDS 256
1879 int fdtmp[DUP2_MAX_FDS];
1886 /* good enough for low fd's... */
1887 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1888 if (fdx >= DUP2_MAX_FDS) {
1896 close(fdtmp[--fdx]);
1903 #ifdef HAS_SIGACTION
1906 rsignal(signo, handler)
1908 Sighandler_t handler;
1910 struct sigaction act, oact;
1912 act.sa_handler = handler;
1913 sigemptyset(&act.sa_mask);
1916 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1918 if (sigaction(signo, &act, &oact) == -1)
1921 return oact.sa_handler;
1925 rsignal_state(signo)
1928 struct sigaction oact;
1930 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1933 return oact.sa_handler;
1937 rsignal_save(signo, handler, save)
1939 Sighandler_t handler;
1942 struct sigaction act;
1944 act.sa_handler = handler;
1945 sigemptyset(&act.sa_mask);
1948 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1950 return sigaction(signo, &act, save);
1954 rsignal_restore(signo, save)
1958 return sigaction(signo, save, (struct sigaction *)NULL);
1961 #else /* !HAS_SIGACTION */
1964 rsignal(signo, handler)
1966 Sighandler_t handler;
1968 return signal(signo, handler);
1971 static int sig_trapped;
1982 rsignal_state(signo)
1985 Sighandler_t oldsig;
1988 oldsig = signal(signo, sig_trap);
1989 signal(signo, oldsig);
1991 kill(getpid(), signo);
1996 rsignal_save(signo, handler, save)
1998 Sighandler_t handler;
2001 *save = signal(signo, handler);
2002 return (*save == SIG_ERR) ? -1 : 0;
2006 rsignal_restore(signo, save)
2010 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2013 #endif /* !HAS_SIGACTION */
2015 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2016 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2021 Sigsave_t hstat, istat, qstat;
2028 int saved_vaxc_errno;
2031 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2032 pid = (int)SvIVX(*svp);
2036 if (pid == -1) { /* Opened by popen. */
2037 return my_syspclose(ptr);
2040 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2041 saved_errno = errno;
2043 saved_vaxc_errno = vaxc$errno;
2047 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2049 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2050 rsignal_save(SIGINT, SIG_IGN, &istat);
2051 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2053 pid = wait4pid(pid, &status, 0);
2054 } while (pid == -1 && errno == EINTR);
2055 rsignal_restore(SIGHUP, &hstat);
2056 rsignal_restore(SIGINT, &istat);
2057 rsignal_restore(SIGQUIT, &qstat);
2059 SETERRNO(saved_errno, saved_vaxc_errno);
2062 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
2064 #endif /* !DOSISH */
2066 #if !defined(DOSISH) || defined(OS2)
2068 wait4pid(pid,statusp,flags)
2075 char spid[TYPE_CHARS(int)];
2080 sprintf(spid, "%d", pid);
2081 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2082 if (svp && *svp != &sv_undef) {
2083 *statusp = SvIVX(*svp);
2084 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2091 hv_iterinit(pidstatus);
2092 if (entry = hv_iternext(pidstatus)) {
2093 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2094 sv = hv_iterval(pidstatus,entry);
2095 *statusp = SvIVX(sv);
2096 sprintf(spid, "%d", pid);
2097 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2102 # ifdef HAS_WAITPID_RUNTIME
2103 if (!HAS_WAITPID_RUNTIME)
2106 return waitpid(pid,statusp,flags);
2108 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2109 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2111 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2116 croak("Can't do waitpid with flags");
2118 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2119 pidgone(result,*statusp);
2127 #endif /* !DOSISH */
2136 char spid[TYPE_CHARS(int)];
2138 sprintf(spid, "%d", pid);
2139 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2140 (void)SvUPGRADE(sv,SVt_IV);
2145 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2148 int /* Cannot prototype with I32
2157 /* Needs work for PerlIO ! */
2158 FILE *f = PerlIO_findFILE(ptr);
2159 I32 result = pclose(f);
2160 PerlIO_releaseFILE(ptr,f);
2166 repeatcpy(to,from,len,count)
2168 register char *from;
2173 register char *frombase = from;
2181 while (count-- > 0) {
2182 for (todo = len; todo > 0; todo--) {
2189 #ifndef CASTNEGFLOAT
2197 # define BIGDOUBLE 2147483648.0
2199 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2202 return (unsigned long)f;
2204 return (unsigned long)along;
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.
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
2219 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2222 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2224 -- Kenneth Albanowski <kjahds@kjahds.com>
2228 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2236 return (I32) I32_MAX;
2238 return (I32) I32_MIN;
2258 return (UV) MY_UV_MAX;
2270 char *fa = strrchr(a,'/');
2271 char *fb = strrchr(b,'/');
2272 struct stat tmpstatbuf1;
2273 struct stat tmpstatbuf2;
2274 SV *tmpsv = sv_newmortal();
2287 sv_setpv(tmpsv, ".");
2289 sv_setpvn(tmpsv, a, fa - a);
2290 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2293 sv_setpv(tmpsv, ".");
2295 sv_setpvn(tmpsv, b, fb - b);
2296 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2298 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2299 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2301 #endif /* !HAS_RENAME */
2304 scan_oct(start, len, retlen)
2309 register char *s = start;
2310 register UV retval = 0;
2311 bool overflowed = FALSE;
2313 while (len && *s >= '0' && *s <= '7') {
2314 register UV n = retval << 3;
2315 if (!overflowed && (n >> 3) != retval) {
2316 warn("Integer overflow in octal number");
2319 retval = n | (*s++ - '0');
2322 if (dowarn && len && (*s == '8' || *s == '9'))
2323 warn("Illegal octal digit ignored");
2324 *retlen = s - start;
2329 scan_hex(start, len, retlen)
2334 register char *s = start;
2335 register UV retval = 0;
2336 bool overflowed = FALSE;
2339 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2340 register UV n = retval << 4;
2341 if (!overflowed && (n >> 4) != retval) {
2342 warn("Integer overflow in hex number");
2345 retval = n | (tmp - hexdigit) & 15;
2348 *retlen = s - start;
2354 /* Very simplistic scheduler for now */
2358 thr = thr->next_run;
2369 perl_cond_signal(cp)
2373 perl_cond cond = *cp;
2378 /* Insert t in the runnable queue just ahead of us */
2379 t->next_run = thr->next_run;
2380 thr->next_run->prev_run = t;
2383 thr->wait_queue = 0;
2384 /* Remove from the wait queue */
2390 perl_cond_broadcast(cp)
2394 perl_cond cond, cond_next;
2396 for (cond = *cp; cond; cond = cond_next) {
2398 /* Insert t in the runnable queue just ahead of us */
2399 t->next_run = thr->next_run;
2400 thr->next_run->prev_run = t;
2403 thr->wait_queue = 0;
2404 /* Remove from the wait queue */
2405 cond_next = cond->next;
2417 if (thr->next_run == thr)
2418 croak("panic: perl_cond_wait called by last runnable thread");
2420 New(666, cond, 1, struct perl_wait_queue);
2424 thr->wait_queue = cond;
2425 /* Remove ourselves from runnable queue */
2426 thr->next_run->prev_run = thr->prev_run;
2427 thr->prev_run->next_run = thr->next_run;
2429 #endif /* FAKE_THREADS */
2431 #ifdef OLD_PTHREADS_API
2437 if (pthread_getspecific(thr_key, &t))
2438 croak("panic: pthread_getspecific");
2439 return (struct thread *) t;
2441 #endif /* OLD_PTHREADS_API */
2449 SvUPGRADE(sv, SVt_PVMG);
2450 mg = mg_find(sv, 'm');
2454 New(53, cp, 1, condpair_t);
2455 MUTEX_INIT(&cp->mutex);
2456 COND_INIT(&cp->owner_cond);
2457 COND_INIT(&cp->cond);
2459 MUTEX_LOCK(&sv_mutex);
2460 mg = mg_find(sv, 'm');
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);
2470 sv_magic(sv, Nullsv, 'm', 0, 0);
2472 mg->mg_ptr = (char *)cp;
2473 mg->mg_len = sizeof(cp);
2474 MUTEX_UNLOCK(&sv_mutex);
2475 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2476 "0x%lx: condpair_magic 0x%lx\n",
2478 (unsigned long)sv));)
2483 #endif /* USE_THREADS */
2487 * This hack is to force load of "huge" support from libm.a
2488 * So it is in perl for (say) POSIX to use.
2489 * Needed for SunOS with Sun's 'acc' for example.