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;
283 If the delimiter is ';', then do not consider backslashes -
284 used only for PATH on DOSISH systems. */
287 delimcpy(to, toend, from, fromend, delim, retlen)
289 register char *toend;
291 register char *fromend;
296 for (tolen = 0; from < fromend; from++, tolen++) {
297 if (*from == '\\' && delim != ';') {
298 if (from[1] == delim)
307 else if (*from == delim)
318 /* return ptr to little string in big string, NULL if not found */
319 /* This routine was donated by Corey Satten. */
324 register char *little;
326 register char *s, *x;
337 for (x=big,s=little; *s; /**/ ) {
351 /* same as instr but allow embedded nulls */
354 ninstr(big, bigend, little, lend)
356 register char *bigend;
360 register char *s, *x;
361 register I32 first = *little;
362 register char *littleend = lend;
364 if (!first && little >= littleend)
366 if (bigend - big < littleend - little)
368 bigend -= littleend - little++;
369 while (big <= bigend) {
372 for (x=big,s=little; s < littleend; /**/ ) {
384 /* reverse of the above--find last substring */
387 rninstr(big, bigend, little, lend)
393 register char *bigbeg;
394 register char *s, *x;
395 register I32 first = *little;
396 register char *littleend = lend;
398 if (!first && little >= littleend)
401 big = bigend - (littleend - little++);
402 while (big >= bigbeg) {
405 for (x=big+2,s=little; s < littleend; /**/ ) {
418 * Set up for a new ctype locale.
421 perl_new_ctype(newctype)
424 #ifdef USE_LOCALE_CTYPE
428 for (i = 0; i < 256; i++) {
430 fold_locale[i] = toLOWER_LC(i);
431 else if (isLOWER_LC(i))
432 fold_locale[i] = toUPPER_LC(i);
437 #endif /* USE_LOCALE_CTYPE */
441 * Set up for a new collation locale.
444 perl_new_collate(newcoll)
447 #ifdef USE_LOCALE_COLLATE
450 if (collation_name) {
452 Safefree(collation_name);
453 collation_name = NULL;
454 collation_standard = TRUE;
461 if (! collation_name || strNE(collation_name, newcoll)) {
463 Safefree(collation_name);
464 collation_name = savepv(newcoll);
465 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
468 /* 2: at most so many chars ('a', 'b'). */
469 /* 50: surely no system expands a char more. */
470 #define XFRMBUFSIZE (2 * 50)
471 char xbuf[XFRMBUFSIZE];
472 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
473 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
474 SSize_t mult = fb - fa;
476 croak("strxfrm() gets absurd");
477 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
478 collxfrm_mult = mult;
482 #endif /* USE_LOCALE_COLLATE */
486 * Set up for a new numeric locale.
489 perl_new_numeric(newnum)
492 #ifdef USE_LOCALE_NUMERIC
496 Safefree(numeric_name);
498 numeric_standard = TRUE;
499 numeric_local = TRUE;
504 if (! numeric_name || strNE(numeric_name, newnum)) {
505 Safefree(numeric_name);
506 numeric_name = savepv(newnum);
507 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
508 numeric_local = TRUE;
511 #endif /* USE_LOCALE_NUMERIC */
515 perl_set_numeric_standard()
517 #ifdef USE_LOCALE_NUMERIC
519 if (! numeric_standard) {
520 setlocale(LC_NUMERIC, "C");
521 numeric_standard = TRUE;
522 numeric_local = FALSE;
525 #endif /* USE_LOCALE_NUMERIC */
529 perl_set_numeric_local()
531 #ifdef USE_LOCALE_NUMERIC
533 if (! numeric_local) {
534 setlocale(LC_NUMERIC, numeric_name);
535 numeric_standard = FALSE;
536 numeric_local = TRUE;
539 #endif /* USE_LOCALE_NUMERIC */
544 * Initialize locale awareness.
547 perl_init_i18nl10n(printwarn)
552 * 1 = set ok or not applicable,
553 * 0 = fallback to C locale,
554 * -1 = fallback to C locale failed
559 #ifdef USE_LOCALE_CTYPE
560 char *curctype = NULL;
561 #endif /* USE_LOCALE_CTYPE */
562 #ifdef USE_LOCALE_COLLATE
563 char *curcoll = NULL;
564 #endif /* USE_LOCALE_COLLATE */
565 #ifdef USE_LOCALE_NUMERIC
567 #endif /* USE_LOCALE_NUMERIC */
568 char *lc_all = getenv("LC_ALL");
569 char *lang = getenv("LANG");
570 bool setlocale_failure = FALSE;
572 #ifdef LOCALE_ENVIRON_REQUIRED
575 * Ultrix setlocale(..., "") fails if there are no environment
576 * variables from which to get a locale name.
583 if (setlocale(LC_ALL, ""))
586 setlocale_failure = TRUE;
588 if (!setlocale_failure)
591 #ifdef USE_LOCALE_CTYPE
592 if (! (curctype = setlocale(LC_CTYPE,
593 (!done && (lang || getenv("LC_CTYPE")))
595 setlocale_failure = TRUE;
596 #endif /* USE_LOCALE_CTYPE */
597 #ifdef USE_LOCALE_COLLATE
598 if (! (curcoll = setlocale(LC_COLLATE,
599 (!done && (lang || getenv("LC_COLLATE")))
601 setlocale_failure = TRUE;
602 #endif /* USE_LOCALE_COLLATE */
603 #ifdef USE_LOCALE_NUMERIC
604 if (! (curnum = setlocale(LC_NUMERIC,
605 (!done && (lang || getenv("LC_NUMERIC")))
607 setlocale_failure = TRUE;
608 #endif /* USE_LOCALE_NUMERIC */
611 #else /* !LOCALE_ENVIRON_REQUIRED */
615 if (! setlocale(LC_ALL, ""))
616 setlocale_failure = TRUE;
618 #ifdef USE_LOCALE_CTYPE
619 curctype = setlocale(LC_CTYPE, Nullch);
620 #endif /* USE_LOCALE_CTYPE */
621 #ifdef USE_LOCALE_COLLATE
622 curcoll = setlocale(LC_COLLATE, Nullch);
623 #endif /* USE_LOCALE_COLLATE */
624 #ifdef USE_LOCALE_NUMERIC
625 curnum = setlocale(LC_NUMERIC, Nullch);
626 #endif /* USE_LOCALE_NUMERIC */
631 #ifdef USE_LOCALE_CTYPE
632 if (! (curctype = setlocale(LC_CTYPE, "")))
633 setlocale_failure = TRUE;
634 #endif /* USE_LOCALE_CTYPE */
635 #ifdef USE_LOCALE_COLLATE
636 if (! (curcoll = setlocale(LC_COLLATE, "")))
637 setlocale_failure = TRUE;
638 #endif /* USE_LOCALE_COLLATE */
639 #ifdef USE_LOCALE_NUMERIC
640 if (! (curnum = setlocale(LC_NUMERIC, "")))
641 setlocale_failure = TRUE;
642 #endif /* USE_LOCALE_NUMERIC */
646 #endif /* !LOCALE_ENVIRON_REQUIRED */
648 if (setlocale_failure) {
650 bool locwarn = (printwarn > 1 ||
652 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
657 PerlIO_printf(PerlIO_stderr(),
658 "perl: warning: Setting locale failed.\n");
662 PerlIO_printf(PerlIO_stderr(),
663 "perl: warning: Setting locale failed for the categories:\n\t");
664 #ifdef USE_LOCALE_CTYPE
666 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
667 #endif /* USE_LOCALE_CTYPE */
668 #ifdef USE_LOCALE_COLLATE
670 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
671 #endif /* USE_LOCALE_COLLATE */
672 #ifdef USE_LOCALE_NUMERIC
674 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
675 #endif /* USE_LOCALE_NUMERIC */
676 PerlIO_printf(PerlIO_stderr(), "\n");
680 PerlIO_printf(PerlIO_stderr(),
681 "perl: warning: Please check that your locale settings:\n");
683 PerlIO_printf(PerlIO_stderr(),
684 "\tLC_ALL = %c%s%c,\n",
686 lc_all ? lc_all : "unset",
691 for (e = environ; *e; e++) {
692 if (strnEQ(*e, "LC_", 3)
693 && strnNE(*e, "LC_ALL=", 7)
694 && (p = strchr(*e, '=')))
695 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
696 (p - *e), *e, p + 1);
700 PerlIO_printf(PerlIO_stderr(),
703 lang ? lang : "unset",
706 PerlIO_printf(PerlIO_stderr(),
707 " are supported and installed on your system.\n");
712 if (setlocale(LC_ALL, "C")) {
714 PerlIO_printf(PerlIO_stderr(),
715 "perl: warning: Falling back to the standard locale (\"C\").\n");
720 PerlIO_printf(PerlIO_stderr(),
721 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
728 #ifdef USE_LOCALE_CTYPE
729 || !(curctype || setlocale(LC_CTYPE, "C"))
730 #endif /* USE_LOCALE_CTYPE */
731 #ifdef USE_LOCALE_COLLATE
732 || !(curcoll || setlocale(LC_COLLATE, "C"))
733 #endif /* USE_LOCALE_COLLATE */
734 #ifdef USE_LOCALE_NUMERIC
735 || !(curnum || setlocale(LC_NUMERIC, "C"))
736 #endif /* USE_LOCALE_NUMERIC */
740 PerlIO_printf(PerlIO_stderr(),
741 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
745 #endif /* ! LC_ALL */
747 #ifdef USE_LOCALE_CTYPE
748 curctype = setlocale(LC_CTYPE, Nullch);
749 #endif /* USE_LOCALE_CTYPE */
750 #ifdef USE_LOCALE_COLLATE
751 curcoll = setlocale(LC_COLLATE, Nullch);
752 #endif /* USE_LOCALE_COLLATE */
753 #ifdef USE_LOCALE_NUMERIC
754 curnum = setlocale(LC_NUMERIC, Nullch);
755 #endif /* USE_LOCALE_NUMERIC */
758 #ifdef USE_LOCALE_CTYPE
759 perl_new_ctype(curctype);
760 #endif /* USE_LOCALE_CTYPE */
762 #ifdef USE_LOCALE_COLLATE
763 perl_new_collate(curcoll);
764 #endif /* USE_LOCALE_COLLATE */
766 #ifdef USE_LOCALE_NUMERIC
767 perl_new_numeric(curnum);
768 #endif /* USE_LOCALE_NUMERIC */
770 #endif /* USE_LOCALE */
775 /* Backwards compatibility. */
777 perl_init_i18nl14n(printwarn)
780 return perl_init_i18nl10n(printwarn);
783 #ifdef USE_LOCALE_COLLATE
786 * mem_collxfrm() is a bit like strxfrm() but with two important
787 * differences. First, it handles embedded NULs. Second, it allocates
788 * a bit more memory than needed for the transformed data itself.
789 * The real transformed data begins at offset sizeof(collationix).
790 * Please see sv_collxfrm() to see how this is used.
793 mem_collxfrm(s, len, xlen)
799 STRLEN xalloc, xin, xout;
801 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
802 /* the +1 is for the terminating NUL. */
804 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
805 New(171, xbuf, xalloc, char);
809 *(U32*)xbuf = collation_ix;
810 xout = sizeof(collation_ix);
811 for (xin = 0; xin < len; ) {
815 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
818 if (xused < xalloc - xout)
820 xalloc = (2 * xalloc) + 1;
821 Renew(xbuf, xalloc, char);
826 xin += strlen(s + xin) + 1;
829 /* Embedded NULs are understood but silently skipped
830 * because they make no sense in locale collation. */
834 *xlen = xout - sizeof(collation_ix);
843 #endif /* USE_LOCALE_COLLATE */
849 register unsigned char *s;
850 register unsigned char *table;
852 register U32 len = SvCUR(sv);
857 return; /* can't have offsets that big */
859 table = (unsigned char*)(SvPVX(sv) + len + 1);
861 for (i = 0; i < 256; i++) {
865 while (s >= (unsigned char*)(SvPVX(sv)))
867 if (table[*s] == len)
871 sv_upgrade(sv, SVt_PVBM);
872 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
875 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
876 for (i = 0; i < len; i++) {
877 if (freq[s[i]] < frequency) {
879 frequency = freq[s[i]];
882 BmRARE(sv) = s[rarest];
883 BmPREVIOUS(sv) = rarest;
884 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
888 fbm_instr(big, bigend, littlestr)
890 register unsigned char *bigend;
893 register unsigned char *s;
895 register I32 littlelen;
896 register unsigned char *little;
897 register unsigned char *table;
898 register unsigned char *olds;
899 register unsigned char *oldlittle;
901 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
903 char *l = SvPV(littlestr,len);
906 return ninstr((char*)big,(char*)bigend, l, l + len);
909 littlelen = SvCUR(littlestr);
910 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
911 if (littlelen > bigend - big)
913 little = (unsigned char*)SvPVX(littlestr);
914 s = bigend - littlelen;
915 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
916 return (char*)s; /* how sweet it is */
917 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
920 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
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)
951 s = olds + 1; /* here we pay the price for failure */
953 if (s < bigend) /* fake up continue to outer loop */
964 screaminstr(bigstr, littlestr)
968 register unsigned char *s, *x;
969 register unsigned char *big;
971 register I32 previous;
973 register unsigned char *little;
974 register unsigned char *bigend;
975 register unsigned char *littleend;
977 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
979 little = (unsigned char *)(SvPVX(littlestr));
980 littleend = little + SvCUR(littlestr);
982 previous = BmPREVIOUS(littlestr);
983 big = (unsigned char *)(SvPVX(bigstr));
984 bigend = big + SvCUR(bigstr);
985 while (pos < previous) {
986 if (!(pos += screamnext[pos]))
991 if (big[pos-previous] != first)
993 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1002 return (char *)(big+pos-previous);
1003 } while ( pos += screamnext[pos] );
1004 #else /* !POINTERRIGOR */
1007 if (big[pos] != first)
1009 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1018 return (char *)(big+pos);
1019 } while ( pos += screamnext[pos] );
1020 #endif /* POINTERRIGOR */
1029 register U8 *a = (U8 *)s1;
1030 register U8 *b = (U8 *)s2;
1032 if (*a != *b && *a != fold[*b])
1040 ibcmp_locale(s1, s2, len)
1044 register U8 *a = (U8 *)s1;
1045 register U8 *b = (U8 *)s2;
1047 if (*a != *b && *a != fold_locale[*b])
1054 /* copy a string to a safe spot */
1060 register char *newaddr;
1062 New(902,newaddr,strlen(sv)+1,char);
1063 (void)strcpy(newaddr,sv);
1067 /* same thing but with a known length */
1074 register char *newaddr;
1076 New(903,newaddr,len+1,char);
1077 Copy(sv,newaddr,len,char); /* might not be null terminated */
1078 newaddr[len] = '\0'; /* is now */
1082 /* the SV for form() and mess() is not kept in an arena */
1090 /* Create as PVMG now, to avoid any upgrading later */
1091 New(905, sv, 1, SV);
1092 Newz(905, any, 1, XPVMG);
1093 SvFLAGS(sv) = SVt_PVMG;
1094 SvANY(sv) = (void*)any;
1095 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1101 form(const char* pat, ...)
1112 va_start(args, pat);
1117 mess_sv = mess_alloc();
1118 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1120 return SvPVX(mess_sv);
1129 static char dgd[] = " during global destruction.\n";
1132 mess_sv = mess_alloc();
1134 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1135 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1139 if (curcop->cop_line)
1140 sv_catpvf(sv, " at %_ line %ld",
1141 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1142 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1143 bool line_mode = (RsSIMPLE(rs) &&
1144 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1145 sv_catpvf(sv, ", <%s> %s %ld",
1146 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1147 line_mode ? "line" : "chunk",
1148 (long)IoLINES(GvIOp(last_in_gv)));
1150 sv_catpv(sv, ".\n");
1158 die(const char* pat, ...)
1169 I32 oldrunlevel = runlevel;
1170 int was_in_eval = in_eval;
1175 /* We have to switch back to mainstack or die_where may try to pop
1176 * the eval block from the wrong stack if die is being called from a
1177 * signal handler. - dkindred@cs.cmu.edu */
1178 if (curstack != mainstack) {
1180 SWITCHSTACK(curstack, mainstack);
1184 va_start(args, pat);
1188 message = mess(pat, &args);
1192 /* sv_2cv might call croak() */
1193 SV *olddiehook = diehook;
1197 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1199 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1204 msg = newSVpv(message, 0);
1211 perl_call_sv((SV*)cv, G_DISCARD);
1217 restartop = die_where(message);
1218 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1225 croak(const char* pat, ...)
1229 croak(pat, va_alist)
1241 va_start(args, pat);
1245 message = mess(pat, &args);
1248 /* sv_2cv might call croak() */
1249 SV *olddiehook = diehook;
1253 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1255 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1260 msg = newSVpv(message, 0);
1267 perl_call_sv((SV*)cv, G_DISCARD);
1273 restartop = die_where(message);
1276 PerlIO_puts(PerlIO_stderr(),message);
1277 (void)PerlIO_flush(PerlIO_stderr());
1283 warn(const char* pat,...)
1298 va_start(args, pat);
1302 message = mess(pat, &args);
1306 /* sv_2cv might call warn() */
1307 SV *oldwarnhook = warnhook;
1311 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1313 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1318 msg = newSVpv(message, 0);
1325 perl_call_sv((SV*)cv, G_DISCARD);
1331 PerlIO_puts(PerlIO_stderr(),message);
1335 (void)PerlIO_flush(PerlIO_stderr());
1338 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1344 register I32 i=setenv_getix(nam); /* where does it go? */
1346 if (environ == origenviron) { /* need we copy environment? */
1352 for (max = i; environ[max]; max++) ;
1353 New(901,tmpenv, max+2, char*);
1354 for (j=0; j<max; j++) /* copy environment */
1355 tmpenv[j] = savepv(environ[j]);
1356 tmpenv[max] = Nullch;
1357 environ = tmpenv; /* tell exec where it is now */
1360 Safefree(environ[i]);
1361 while (environ[i]) {
1362 environ[i] = environ[i+1];
1367 if (!environ[i]) { /* does not exist yet */
1368 Renew(environ, i+2, char*); /* just expand it a bit */
1369 environ[i+1] = Nullch; /* make sure it's null terminated */
1372 Safefree(environ[i]);
1373 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1375 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1377 /* MS-DOS requires environment variable names to be in uppercase */
1378 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1379 * some utilities and applications may break because they only look
1380 * for upper case strings. (Fixed strupr() bug here.)]
1382 strcpy(environ[i],nam); strupr(environ[i]);
1383 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1387 #else /* if WIN32 */
1394 #ifdef USE_WIN32_RTL_ENV
1396 register char *envstr;
1397 STRLEN namlen = strlen(nam);
1399 char *oldstr = environ[setenv_getix(nam)];
1401 /* putenv() has totally broken semantics in both the Borland
1402 * and Microsoft CRTLs. They either store the passed pointer in
1403 * the environment without making a copy, or make a copy and don't
1404 * free it. And on top of that, they dont free() old entries that
1405 * are being replaced/deleted. This means the caller must
1406 * free any old entries somehow, or we end up with a memory
1407 * leak every time my_setenv() is called. One might think
1408 * one could directly manipulate environ[], like the UNIX code
1409 * above, but direct changes to environ are not allowed when
1410 * calling putenv(), since the RTLs maintain an internal
1411 * *copy* of environ[]. Bad, bad, *bad* stink.
1422 vallen = strlen(val);
1423 New(904, envstr, namlen + vallen + 3, char);
1424 (void)sprintf(envstr,"%s=%s",nam,val);
1425 (void)putenv(envstr);
1429 Safefree(envstr); /* MSVCRT leaks without this */
1432 #else /* !USE_WIN32_RTL_ENV */
1434 /* The sane way to deal with the environment.
1435 * Has these advantages over putenv() & co.:
1436 * * enables us to store a truly empty value in the
1437 * environment (like in UNIX).
1438 * * we don't have to deal with RTL globals, bugs and leaks.
1440 * Why you may want to enable USE_WIN32_RTL_ENV:
1441 * * environ[] and RTL functions will not reflect changes,
1442 * which might be an issue if extensions want to access
1443 * the env. via RTL. This cuts both ways, since RTL will
1444 * not see changes made by extensions that call the Win32
1445 * functions directly, either.
1448 SetEnvironmentVariable(nam,val);
1459 register I32 i, len = strlen(nam);
1461 for (i = 0; environ[i]; i++) {
1464 strnicmp(environ[i],nam,len) == 0
1466 strnEQ(environ[i],nam,len)
1468 && environ[i][len] == '=')
1469 break; /* strnEQ must come first to avoid */
1470 } /* potential SEGV's */
1476 #ifdef UNLINK_ALL_VERSIONS
1478 unlnk(f) /* unlink all versions of a file */
1483 for (i = 0; unlink(f) >= 0; i++) ;
1488 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1490 my_bcopy(from,to,len)
1491 register char *from;
1497 if (from - to >= 0) {
1505 *(--to) = *(--from);
1513 my_memset(loc,ch,len)
1526 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1540 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1542 my_memcmp(s1,s2,len)
1547 register U8 *a = (U8 *)s1;
1548 register U8 *b = (U8 *)s2;
1552 if (tmp = *a++ - *b++)
1557 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1559 #if defined(I_STDARG) || defined(I_VARARGS)
1562 #ifdef USE_CHAR_VSPRINTF
1567 vsprintf(dest, pat, args)
1574 fakebuf._ptr = dest;
1575 fakebuf._cnt = 32767;
1579 fakebuf._flag = _IOWRT|_IOSTRG;
1580 _doprnt(pat, args, &fakebuf); /* what a kludge */
1581 (void)putc('\0', &fakebuf);
1582 #ifdef USE_CHAR_VSPRINTF
1585 return 0; /* perl doesn't use return value */
1589 #endif /* HAS_VPRINTF */
1590 #endif /* I_VARARGS || I_STDARGS */
1593 #if BYTEORDER != 0x4321
1595 #ifndef CAN_PROTOTYPE
1602 #if (BYTEORDER & 1) == 0
1605 result = ((s & 255) << 8) + ((s >> 8) & 255);
1613 #ifndef CAN_PROTOTYPE
1622 char c[sizeof(long)];
1625 #if BYTEORDER == 0x1234
1626 u.c[0] = (l >> 24) & 255;
1627 u.c[1] = (l >> 16) & 255;
1628 u.c[2] = (l >> 8) & 255;
1632 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1633 croak("Unknown BYTEORDER\n");
1638 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1639 u.c[o & 0xf] = (l >> s) & 255;
1647 #ifndef CAN_PROTOTYPE
1656 char c[sizeof(long)];
1659 #if BYTEORDER == 0x1234
1660 u.c[0] = (l >> 24) & 255;
1661 u.c[1] = (l >> 16) & 255;
1662 u.c[2] = (l >> 8) & 255;
1666 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1667 croak("Unknown BYTEORDER\n");
1674 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1675 l |= (u.c[o & 0xf] & 255) << s;
1682 #endif /* BYTEORDER != 0x4321 */
1686 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1687 * If these functions are defined,
1688 * the BYTEORDER is neither 0x1234 nor 0x4321.
1689 * However, this is not assumed.
1693 #define HTOV(name,type) \
1700 char c[sizeof(type)]; \
1704 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1705 u.c[i] = (n >> s) & 0xFF; \
1710 #define VTOH(name,type) \
1717 char c[sizeof(type)]; \
1723 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1724 n += (u.c[i] & 0xFF) << s; \
1729 #if defined(HAS_HTOVS) && !defined(htovs)
1732 #if defined(HAS_HTOVL) && !defined(htovl)
1735 #if defined(HAS_VTOHS) && !defined(vtohs)
1738 #if defined(HAS_VTOHL) && !defined(vtohl)
1742 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1743 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1750 register I32 this, that;
1753 I32 doexec = strNE(cmd,"-");
1757 return my_syspopen(cmd,mode);
1762 this = (*mode == 'w');
1764 if (doexec && tainting) {
1766 taint_proper("Insecure %s%s", "EXEC");
1768 while ((pid = (doexec?vfork():fork())) < 0) {
1769 if (errno != EAGAIN) {
1772 croak("Can't fork");
1783 if (p[THIS] != (*mode == 'r')) {
1784 dup2(p[THIS], *mode == 'r');
1788 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1794 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1797 do_exec(cmd); /* may or may not use the shell */
1801 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1802 sv_setiv(GvSV(tmpgv), (IV)getpid());
1804 hv_clear(pidstatus); /* we have no children */
1809 do_execfree(); /* free any memory malloced by child on vfork */
1811 if (p[that] < p[this]) {
1812 dup2(p[this], p[that]);
1816 sv = *av_fetch(fdpid,p[this],TRUE);
1817 (void)SvUPGRADE(sv,SVt_IV);
1820 return PerlIO_fdopen(p[this], mode);
1823 #if defined(atarist) || defined(DJGPP)
1830 /* Needs work for PerlIO ! */
1831 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1832 return popen(PerlIO_exportFILE(cmd, 0), mode);
1836 #endif /* !DOSISH */
1843 struct stat tmpstatbuf;
1845 PerlIO_printf(PerlIO_stderr(),"%s", s);
1846 for (fd = 0; fd < 32; fd++) {
1847 if (Fstat(fd,&tmpstatbuf) >= 0)
1848 PerlIO_printf(PerlIO_stderr()," %d",fd);
1850 PerlIO_printf(PerlIO_stderr(),"\n");
1860 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1864 return fcntl(oldfd, F_DUPFD, newfd);
1866 #define DUP2_MAX_FDS 256
1867 int fdtmp[DUP2_MAX_FDS];
1874 /* good enough for low fd's... */
1875 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1876 if (fdx >= DUP2_MAX_FDS) {
1884 close(fdtmp[--fdx]);
1891 #ifdef HAS_SIGACTION
1894 rsignal(signo, handler)
1896 Sighandler_t handler;
1898 struct sigaction act, oact;
1900 act.sa_handler = handler;
1901 sigemptyset(&act.sa_mask);
1904 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1906 if (sigaction(signo, &act, &oact) == -1)
1909 return oact.sa_handler;
1913 rsignal_state(signo)
1916 struct sigaction oact;
1918 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1921 return oact.sa_handler;
1925 rsignal_save(signo, handler, save)
1927 Sighandler_t handler;
1930 struct sigaction act;
1932 act.sa_handler = handler;
1933 sigemptyset(&act.sa_mask);
1936 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1938 return sigaction(signo, &act, save);
1942 rsignal_restore(signo, save)
1946 return sigaction(signo, save, (struct sigaction *)NULL);
1949 #else /* !HAS_SIGACTION */
1952 rsignal(signo, handler)
1954 Sighandler_t handler;
1956 return signal(signo, handler);
1959 static int sig_trapped;
1970 rsignal_state(signo)
1973 Sighandler_t oldsig;
1976 oldsig = signal(signo, sig_trap);
1977 signal(signo, oldsig);
1979 kill(getpid(), signo);
1984 rsignal_save(signo, handler, save)
1986 Sighandler_t handler;
1989 *save = signal(signo, handler);
1990 return (*save == SIG_ERR) ? -1 : 0;
1994 rsignal_restore(signo, save)
1998 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2001 #endif /* !HAS_SIGACTION */
2003 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2004 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2009 Sigsave_t hstat, istat, qstat;
2016 int saved_vaxc_errno;
2019 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2020 pid = (int)SvIVX(*svp);
2024 if (pid == -1) { /* Opened by popen. */
2025 return my_syspclose(ptr);
2028 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2029 saved_errno = errno;
2031 saved_vaxc_errno = vaxc$errno;
2035 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2037 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2038 rsignal_save(SIGINT, SIG_IGN, &istat);
2039 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2041 pid = wait4pid(pid, &status, 0);
2042 } while (pid == -1 && errno == EINTR);
2043 rsignal_restore(SIGHUP, &hstat);
2044 rsignal_restore(SIGINT, &istat);
2045 rsignal_restore(SIGQUIT, &qstat);
2047 SETERRNO(saved_errno, saved_vaxc_errno);
2050 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
2052 #endif /* !DOSISH */
2054 #if !defined(DOSISH) || defined(OS2)
2056 wait4pid(pid,statusp,flags)
2063 char spid[TYPE_CHARS(int)];
2068 sprintf(spid, "%d", pid);
2069 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2070 if (svp && *svp != &sv_undef) {
2071 *statusp = SvIVX(*svp);
2072 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2079 hv_iterinit(pidstatus);
2080 if (entry = hv_iternext(pidstatus)) {
2081 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2082 sv = hv_iterval(pidstatus,entry);
2083 *statusp = SvIVX(sv);
2084 sprintf(spid, "%d", pid);
2085 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2090 # ifdef HAS_WAITPID_RUNTIME
2091 if (!HAS_WAITPID_RUNTIME)
2094 return waitpid(pid,statusp,flags);
2096 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2097 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2099 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2104 croak("Can't do waitpid with flags");
2106 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2107 pidgone(result,*statusp);
2115 #endif /* !DOSISH */
2124 char spid[TYPE_CHARS(int)];
2126 sprintf(spid, "%d", pid);
2127 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2128 (void)SvUPGRADE(sv,SVt_IV);
2133 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2136 int /* Cannot prototype with I32
2145 /* Needs work for PerlIO ! */
2146 FILE *f = PerlIO_findFILE(ptr);
2147 I32 result = pclose(f);
2148 PerlIO_releaseFILE(ptr,f);
2154 repeatcpy(to,from,len,count)
2156 register char *from;
2161 register char *frombase = from;
2169 while (count-- > 0) {
2170 for (todo = len; todo > 0; todo--) {
2177 #ifndef CASTNEGFLOAT
2185 # define BIGDOUBLE 2147483648.0
2187 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2190 return (unsigned long)f;
2192 return (unsigned long)along;
2199 /* Unfortunately, on some systems the cast_uv() function doesn't
2200 work with the system-supplied definition of ULONG_MAX. The
2201 comparison (f >= ULONG_MAX) always comes out true. It must be a
2202 problem with the compiler constant folding.
2204 In any case, this workaround should be fine on any two's complement
2205 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2207 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2210 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2212 -- Kenneth Albanowski <kjahds@kjahds.com>
2216 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2224 return (I32) I32_MAX;
2226 return (I32) I32_MIN;
2246 return (UV) MY_UV_MAX;
2258 char *fa = strrchr(a,'/');
2259 char *fb = strrchr(b,'/');
2260 struct stat tmpstatbuf1;
2261 struct stat tmpstatbuf2;
2262 SV *tmpsv = sv_newmortal();
2275 sv_setpv(tmpsv, ".");
2277 sv_setpvn(tmpsv, a, fa - a);
2278 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2281 sv_setpv(tmpsv, ".");
2283 sv_setpvn(tmpsv, b, fb - b);
2284 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2286 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2287 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2289 #endif /* !HAS_RENAME */
2292 scan_oct(start, len, retlen)
2297 register char *s = start;
2298 register UV retval = 0;
2299 bool overflowed = FALSE;
2301 while (len && *s >= '0' && *s <= '7') {
2302 register UV n = retval << 3;
2303 if (!overflowed && (n >> 3) != retval) {
2304 warn("Integer overflow in octal number");
2307 retval = n | (*s++ - '0');
2310 if (dowarn && len && (*s == '8' || *s == '9'))
2311 warn("Illegal octal digit ignored");
2312 *retlen = s - start;
2317 scan_hex(start, len, retlen)
2322 register char *s = start;
2323 register UV retval = 0;
2324 bool overflowed = FALSE;
2327 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2328 register UV n = retval << 4;
2329 if (!overflowed && (n >> 4) != retval) {
2330 warn("Integer overflow in hex number");
2333 retval = n | (tmp - hexdigit) & 15;
2336 *retlen = s - start;
2343 * This hack is to force load of "huge" support from libm.a
2344 * So it is in perl for (say) POSIX to use.
2345 * Needed for SunOS with Sun's 'acc' for example.