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) {
317 /* return ptr to little string in big string, NULL if not found */
318 /* This routine was donated by Corey Satten. */
323 register char *little;
325 register char *s, *x;
336 for (x=big,s=little; *s; /**/ ) {
350 /* same as instr but allow embedded nulls */
353 ninstr(big, bigend, little, lend)
355 register char *bigend;
359 register char *s, *x;
360 register I32 first = *little;
361 register char *littleend = lend;
363 if (!first && little >= littleend)
365 if (bigend - big < littleend - little)
367 bigend -= littleend - little++;
368 while (big <= bigend) {
371 for (x=big,s=little; s < littleend; /**/ ) {
383 /* reverse of the above--find last substring */
386 rninstr(big, bigend, little, lend)
392 register char *bigbeg;
393 register char *s, *x;
394 register I32 first = *little;
395 register char *littleend = lend;
397 if (!first && little >= littleend)
400 big = bigend - (littleend - little++);
401 while (big >= bigbeg) {
404 for (x=big+2,s=little; s < littleend; /**/ ) {
417 * Set up for a new ctype locale.
420 perl_new_ctype(newctype)
423 #ifdef USE_LOCALE_CTYPE
427 for (i = 0; i < 256; i++) {
429 fold_locale[i] = toLOWER_LC(i);
430 else if (isLOWER_LC(i))
431 fold_locale[i] = toUPPER_LC(i);
436 #endif /* USE_LOCALE_CTYPE */
440 * Set up for a new collation locale.
443 perl_new_collate(newcoll)
446 #ifdef USE_LOCALE_COLLATE
449 if (collation_name) {
451 Safefree(collation_name);
452 collation_name = NULL;
453 collation_standard = TRUE;
460 if (! collation_name || strNE(collation_name, newcoll)) {
462 Safefree(collation_name);
463 collation_name = savepv(newcoll);
464 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
467 /* 2: at most so many chars ('a', 'b'). */
468 /* 50: surely no system expands a char more. */
469 #define XFRMBUFSIZE (2 * 50)
470 char xbuf[XFRMBUFSIZE];
471 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
472 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
473 SSize_t mult = fb - fa;
475 croak("strxfrm() gets absurd");
476 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
477 collxfrm_mult = mult;
481 #endif /* USE_LOCALE_COLLATE */
485 * Set up for a new numeric locale.
488 perl_new_numeric(newnum)
491 #ifdef USE_LOCALE_NUMERIC
495 Safefree(numeric_name);
497 numeric_standard = TRUE;
498 numeric_local = TRUE;
503 if (! numeric_name || strNE(numeric_name, newnum)) {
504 Safefree(numeric_name);
505 numeric_name = savepv(newnum);
506 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
507 numeric_local = TRUE;
510 #endif /* USE_LOCALE_NUMERIC */
514 perl_set_numeric_standard()
516 #ifdef USE_LOCALE_NUMERIC
518 if (! numeric_standard) {
519 setlocale(LC_NUMERIC, "C");
520 numeric_standard = TRUE;
521 numeric_local = FALSE;
524 #endif /* USE_LOCALE_NUMERIC */
528 perl_set_numeric_local()
530 #ifdef USE_LOCALE_NUMERIC
532 if (! numeric_local) {
533 setlocale(LC_NUMERIC, numeric_name);
534 numeric_standard = FALSE;
535 numeric_local = TRUE;
538 #endif /* USE_LOCALE_NUMERIC */
543 * Initialize locale awareness.
546 perl_init_i18nl10n(printwarn)
551 * 1 = set ok or not applicable,
552 * 0 = fallback to C locale,
553 * -1 = fallback to C locale failed
558 #ifdef USE_LOCALE_CTYPE
559 char *curctype = NULL;
560 #endif /* USE_LOCALE_CTYPE */
561 #ifdef USE_LOCALE_COLLATE
562 char *curcoll = NULL;
563 #endif /* USE_LOCALE_COLLATE */
564 #ifdef USE_LOCALE_NUMERIC
566 #endif /* USE_LOCALE_NUMERIC */
567 char *lc_all = getenv("LC_ALL");
568 char *lang = getenv("LANG");
569 bool setlocale_failure = FALSE;
571 #ifdef LOCALE_ENVIRON_REQUIRED
574 * Ultrix setlocale(..., "") fails if there are no environment
575 * variables from which to get a locale name.
582 if (setlocale(LC_ALL, ""))
585 setlocale_failure = TRUE;
587 if (!setlocale_failure)
590 #ifdef USE_LOCALE_CTYPE
591 if (! (curctype = setlocale(LC_CTYPE,
592 (!done && (lang || getenv("LC_CTYPE")))
594 setlocale_failure = TRUE;
595 #endif /* USE_LOCALE_CTYPE */
596 #ifdef USE_LOCALE_COLLATE
597 if (! (curcoll = setlocale(LC_COLLATE,
598 (!done && (lang || getenv("LC_COLLATE")))
600 setlocale_failure = TRUE;
601 #endif /* USE_LOCALE_COLLATE */
602 #ifdef USE_LOCALE_NUMERIC
603 if (! (curnum = setlocale(LC_NUMERIC,
604 (!done && (lang || getenv("LC_NUMERIC")))
606 setlocale_failure = TRUE;
607 #endif /* USE_LOCALE_NUMERIC */
610 #else /* !LOCALE_ENVIRON_REQUIRED */
614 if (! setlocale(LC_ALL, ""))
615 setlocale_failure = TRUE;
617 #ifdef USE_LOCALE_CTYPE
618 curctype = setlocale(LC_CTYPE, Nullch);
619 #endif /* USE_LOCALE_CTYPE */
620 #ifdef USE_LOCALE_COLLATE
621 curcoll = setlocale(LC_COLLATE, Nullch);
622 #endif /* USE_LOCALE_COLLATE */
623 #ifdef USE_LOCALE_NUMERIC
624 curnum = setlocale(LC_NUMERIC, Nullch);
625 #endif /* USE_LOCALE_NUMERIC */
630 #ifdef USE_LOCALE_CTYPE
631 if (! (curctype = setlocale(LC_CTYPE, "")))
632 setlocale_failure = TRUE;
633 #endif /* USE_LOCALE_CTYPE */
634 #ifdef USE_LOCALE_COLLATE
635 if (! (curcoll = setlocale(LC_COLLATE, "")))
636 setlocale_failure = TRUE;
637 #endif /* USE_LOCALE_COLLATE */
638 #ifdef USE_LOCALE_NUMERIC
639 if (! (curnum = setlocale(LC_NUMERIC, "")))
640 setlocale_failure = TRUE;
641 #endif /* USE_LOCALE_NUMERIC */
645 #endif /* !LOCALE_ENVIRON_REQUIRED */
647 if (setlocale_failure) {
649 bool locwarn = (printwarn > 1 ||
651 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
656 PerlIO_printf(PerlIO_stderr(),
657 "perl: warning: Setting locale failed.\n");
661 PerlIO_printf(PerlIO_stderr(),
662 "perl: warning: Setting locale failed for the categories:\n\t");
663 #ifdef USE_LOCALE_CTYPE
665 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
666 #endif /* USE_LOCALE_CTYPE */
667 #ifdef USE_LOCALE_COLLATE
669 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
670 #endif /* USE_LOCALE_COLLATE */
671 #ifdef USE_LOCALE_NUMERIC
673 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
674 #endif /* USE_LOCALE_NUMERIC */
675 PerlIO_printf(PerlIO_stderr(), "\n");
679 PerlIO_printf(PerlIO_stderr(),
680 "perl: warning: Please check that your locale settings:\n");
682 PerlIO_printf(PerlIO_stderr(),
683 "\tLC_ALL = %c%s%c,\n",
685 lc_all ? lc_all : "unset",
690 for (e = environ; *e; e++) {
691 if (strnEQ(*e, "LC_", 3)
692 && strnNE(*e, "LC_ALL=", 7)
693 && (p = strchr(*e, '=')))
694 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
695 (p - *e), *e, p + 1);
699 PerlIO_printf(PerlIO_stderr(),
702 lang ? lang : "unset",
705 PerlIO_printf(PerlIO_stderr(),
706 " are supported and installed on your system.\n");
711 if (setlocale(LC_ALL, "C")) {
713 PerlIO_printf(PerlIO_stderr(),
714 "perl: warning: Falling back to the standard locale (\"C\").\n");
719 PerlIO_printf(PerlIO_stderr(),
720 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
727 #ifdef USE_LOCALE_CTYPE
728 || !(curctype || setlocale(LC_CTYPE, "C"))
729 #endif /* USE_LOCALE_CTYPE */
730 #ifdef USE_LOCALE_COLLATE
731 || !(curcoll || setlocale(LC_COLLATE, "C"))
732 #endif /* USE_LOCALE_COLLATE */
733 #ifdef USE_LOCALE_NUMERIC
734 || !(curnum || setlocale(LC_NUMERIC, "C"))
735 #endif /* USE_LOCALE_NUMERIC */
739 PerlIO_printf(PerlIO_stderr(),
740 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
744 #endif /* ! LC_ALL */
746 #ifdef USE_LOCALE_CTYPE
747 curctype = setlocale(LC_CTYPE, Nullch);
748 #endif /* USE_LOCALE_CTYPE */
749 #ifdef USE_LOCALE_COLLATE
750 curcoll = setlocale(LC_COLLATE, Nullch);
751 #endif /* USE_LOCALE_COLLATE */
752 #ifdef USE_LOCALE_NUMERIC
753 curnum = setlocale(LC_NUMERIC, Nullch);
754 #endif /* USE_LOCALE_NUMERIC */
757 #ifdef USE_LOCALE_CTYPE
758 perl_new_ctype(curctype);
759 #endif /* USE_LOCALE_CTYPE */
761 #ifdef USE_LOCALE_COLLATE
762 perl_new_collate(curcoll);
763 #endif /* USE_LOCALE_COLLATE */
765 #ifdef USE_LOCALE_NUMERIC
766 perl_new_numeric(curnum);
767 #endif /* USE_LOCALE_NUMERIC */
769 #endif /* USE_LOCALE */
774 /* Backwards compatibility. */
776 perl_init_i18nl14n(printwarn)
779 return perl_init_i18nl10n(printwarn);
782 #ifdef USE_LOCALE_COLLATE
785 * mem_collxfrm() is a bit like strxfrm() but with two important
786 * differences. First, it handles embedded NULs. Second, it allocates
787 * a bit more memory than needed for the transformed data itself.
788 * The real transformed data begins at offset sizeof(collationix).
789 * Please see sv_collxfrm() to see how this is used.
792 mem_collxfrm(s, len, xlen)
798 STRLEN xalloc, xin, xout;
800 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
801 /* the +1 is for the terminating NUL. */
803 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
804 New(171, xbuf, xalloc, char);
808 *(U32*)xbuf = collation_ix;
809 xout = sizeof(collation_ix);
810 for (xin = 0; xin < len; ) {
814 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
817 if (xused < xalloc - xout)
819 xalloc = (2 * xalloc) + 1;
820 Renew(xbuf, xalloc, char);
825 xin += strlen(s + xin) + 1;
828 /* Embedded NULs are understood but silently skipped
829 * because they make no sense in locale collation. */
833 *xlen = xout - sizeof(collation_ix);
842 #endif /* USE_LOCALE_COLLATE */
848 register unsigned char *s;
849 register unsigned char *table;
851 register U32 len = SvCUR(sv);
856 return; /* can't have offsets that big */
858 table = (unsigned char*)(SvPVX(sv) + len + 1);
860 for (i = 0; i < 256; i++) {
864 while (s >= (unsigned char*)(SvPVX(sv)))
866 if (table[*s] == len)
870 sv_upgrade(sv, SVt_PVBM);
871 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
874 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
875 for (i = 0; i < len; i++) {
876 if (freq[s[i]] < frequency) {
878 frequency = freq[s[i]];
881 BmRARE(sv) = s[rarest];
882 BmPREVIOUS(sv) = rarest;
883 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
887 fbm_instr(big, bigend, littlestr)
889 register unsigned char *bigend;
892 register unsigned char *s;
894 register I32 littlelen;
895 register unsigned char *little;
896 register unsigned char *table;
897 register unsigned char *olds;
898 register unsigned char *oldlittle;
900 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
902 char *l = SvPV(littlestr,len);
905 return ninstr((char*)big,(char*)bigend, l, l + len);
908 littlelen = SvCUR(littlestr);
909 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
910 if (littlelen > bigend - big)
912 little = (unsigned char*)SvPVX(littlestr);
913 s = bigend - littlelen;
914 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
915 return (char*)s; /* how sweet it is */
916 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
919 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
924 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
925 if (--littlelen >= bigend - big)
928 oldlittle = little = table - 2;
932 if (tmp = table[*s]) {
934 if (bigend - s > tmp) {
939 if ((s += tmp) < bigend)
945 tmp = littlelen; /* less expensive than calling strncmp() */
948 if (*--s == *--little)
950 s = olds + 1; /* here we pay the price for failure */
952 if (s < bigend) /* fake up continue to outer loop */
963 screaminstr(bigstr, littlestr)
967 register unsigned char *s, *x;
968 register unsigned char *big;
970 register I32 previous;
972 register unsigned char *little;
973 register unsigned char *bigend;
974 register unsigned char *littleend;
976 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
978 little = (unsigned char *)(SvPVX(littlestr));
979 littleend = little + SvCUR(littlestr);
981 previous = BmPREVIOUS(littlestr);
982 big = (unsigned char *)(SvPVX(bigstr));
983 bigend = big + SvCUR(bigstr);
984 while (pos < previous) {
985 if (!(pos += screamnext[pos]))
990 if (big[pos-previous] != first)
992 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1001 return (char *)(big+pos-previous);
1002 } while ( pos += screamnext[pos] );
1003 #else /* !POINTERRIGOR */
1006 if (big[pos] != first)
1008 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1017 return (char *)(big+pos);
1018 } while ( pos += screamnext[pos] );
1019 #endif /* POINTERRIGOR */
1028 register U8 *a = (U8 *)s1;
1029 register U8 *b = (U8 *)s2;
1031 if (*a != *b && *a != fold[*b])
1039 ibcmp_locale(s1, s2, len)
1043 register U8 *a = (U8 *)s1;
1044 register U8 *b = (U8 *)s2;
1046 if (*a != *b && *a != fold_locale[*b])
1053 /* copy a string to a safe spot */
1059 register char *newaddr;
1061 New(902,newaddr,strlen(sv)+1,char);
1062 (void)strcpy(newaddr,sv);
1066 /* same thing but with a known length */
1073 register char *newaddr;
1075 New(903,newaddr,len+1,char);
1076 Copy(sv,newaddr,len,char); /* might not be null terminated */
1077 newaddr[len] = '\0'; /* is now */
1081 /* the SV for form() and mess() is not kept in an arena */
1089 /* Create as PVMG now, to avoid any upgrading later */
1090 New(905, sv, 1, SV);
1091 Newz(905, any, 1, XPVMG);
1092 SvFLAGS(sv) = SVt_PVMG;
1093 SvANY(sv) = (void*)any;
1094 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1100 form(const char* pat, ...)
1111 va_start(args, pat);
1116 mess_sv = mess_alloc();
1117 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1119 return SvPVX(mess_sv);
1128 static char dgd[] = " during global destruction.\n";
1131 mess_sv = mess_alloc();
1133 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1134 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, ...)
1168 I32 oldrunlevel = runlevel;
1169 int was_in_eval = in_eval;
1174 /* We have to switch back to mainstack or die_where may try to pop
1175 * the eval block from the wrong stack if die is being called from a
1176 * signal handler. - dkindred@cs.cmu.edu */
1177 if (curstack != mainstack) {
1179 SWITCHSTACK(curstack, mainstack);
1183 va_start(args, pat);
1187 message = mess(pat, &args);
1191 /* sv_2cv might call croak() */
1192 SV *olddiehook = diehook;
1196 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1198 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1203 msg = newSVpv(message, 0);
1210 perl_call_sv((SV*)cv, G_DISCARD);
1216 restartop = die_where(message);
1217 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1224 croak(const char* pat, ...)
1228 croak(pat, va_alist)
1240 va_start(args, pat);
1244 message = mess(pat, &args);
1247 /* sv_2cv might call croak() */
1248 SV *olddiehook = diehook;
1252 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1254 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1259 msg = newSVpv(message, 0);
1266 perl_call_sv((SV*)cv, G_DISCARD);
1272 restartop = die_where(message);
1275 PerlIO_puts(PerlIO_stderr(),message);
1276 (void)PerlIO_flush(PerlIO_stderr());
1282 warn(const char* pat,...)
1297 va_start(args, pat);
1301 message = mess(pat, &args);
1305 /* sv_2cv might call warn() */
1306 SV *oldwarnhook = warnhook;
1310 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1312 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1317 msg = newSVpv(message, 0);
1324 perl_call_sv((SV*)cv, G_DISCARD);
1330 PerlIO_puts(PerlIO_stderr(),message);
1334 (void)PerlIO_flush(PerlIO_stderr());
1337 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1343 register I32 i=setenv_getix(nam); /* where does it go? */
1345 if (environ == origenviron) { /* need we copy environment? */
1351 for (max = i; environ[max]; max++) ;
1352 New(901,tmpenv, max+2, char*);
1353 for (j=0; j<max; j++) /* copy environment */
1354 tmpenv[j] = savepv(environ[j]);
1355 tmpenv[max] = Nullch;
1356 environ = tmpenv; /* tell exec where it is now */
1359 Safefree(environ[i]);
1360 while (environ[i]) {
1361 environ[i] = environ[i+1];
1366 if (!environ[i]) { /* does not exist yet */
1367 Renew(environ, i+2, char*); /* just expand it a bit */
1368 environ[i+1] = Nullch; /* make sure it's null terminated */
1371 Safefree(environ[i]);
1372 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1374 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1376 /* MS-DOS requires environment variable names to be in uppercase */
1377 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1378 * some utilities and applications may break because they only look
1379 * for upper case strings. (Fixed strupr() bug here.)]
1381 strcpy(environ[i],nam); strupr(environ[i]);
1382 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1390 register I32 i, len = strlen(nam);
1392 for (i = 0; environ[i]; i++) {
1393 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1394 break; /* strnEQ must come first to avoid */
1395 } /* potential SEGV's */
1399 #else /* if _WIN32 */
1405 register char *envstr;
1406 STRLEN namlen = strlen(nam);
1407 STRLEN vallen = strlen(val ? val : "");
1409 New(904, envstr, namlen + vallen + 3, char);
1410 (void)sprintf(envstr,"%s=%s",nam,val);
1412 /* An attempt to delete the entry.
1413 * We try to fix a Win32 process handling goof: Children
1414 * of the current process will end up seeing the
1415 * grandparent's entry if the current process has never
1416 * modified the entry being deleted. So we call _putenv()
1417 * twice: once to pretend to modify the entry, and the
1418 * second time to actually delete it. GSAR 97-03-19
1420 envstr[namlen+1] = 'X'; envstr[namlen+2] = '\0';
1421 (void)_putenv(envstr);
1422 envstr[namlen+1] = '\0';
1424 (void)_putenv(envstr);
1430 #ifdef UNLINK_ALL_VERSIONS
1432 unlnk(f) /* unlink all versions of a file */
1437 for (i = 0; unlink(f) >= 0; i++) ;
1442 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1444 my_bcopy(from,to,len)
1445 register char *from;
1451 if (from - to >= 0) {
1459 *(--to) = *(--from);
1467 my_memset(loc,ch,len)
1480 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1494 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1496 my_memcmp(s1,s2,len)
1501 register U8 *a = (U8 *)s1;
1502 register U8 *b = (U8 *)s2;
1506 if (tmp = *a++ - *b++)
1511 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1513 #if defined(I_STDARG) || defined(I_VARARGS)
1516 #ifdef USE_CHAR_VSPRINTF
1521 vsprintf(dest, pat, args)
1528 fakebuf._ptr = dest;
1529 fakebuf._cnt = 32767;
1533 fakebuf._flag = _IOWRT|_IOSTRG;
1534 _doprnt(pat, args, &fakebuf); /* what a kludge */
1535 (void)putc('\0', &fakebuf);
1536 #ifdef USE_CHAR_VSPRINTF
1539 return 0; /* perl doesn't use return value */
1543 #endif /* HAS_VPRINTF */
1544 #endif /* I_VARARGS || I_STDARGS */
1547 #if BYTEORDER != 0x4321
1549 #ifndef CAN_PROTOTYPE
1556 #if (BYTEORDER & 1) == 0
1559 result = ((s & 255) << 8) + ((s >> 8) & 255);
1567 #ifndef CAN_PROTOTYPE
1576 char c[sizeof(long)];
1579 #if BYTEORDER == 0x1234
1580 u.c[0] = (l >> 24) & 255;
1581 u.c[1] = (l >> 16) & 255;
1582 u.c[2] = (l >> 8) & 255;
1586 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1587 croak("Unknown BYTEORDER\n");
1592 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1593 u.c[o & 0xf] = (l >> s) & 255;
1601 #ifndef CAN_PROTOTYPE
1610 char c[sizeof(long)];
1613 #if BYTEORDER == 0x1234
1614 u.c[0] = (l >> 24) & 255;
1615 u.c[1] = (l >> 16) & 255;
1616 u.c[2] = (l >> 8) & 255;
1620 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1621 croak("Unknown BYTEORDER\n");
1628 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1629 l |= (u.c[o & 0xf] & 255) << s;
1636 #endif /* BYTEORDER != 0x4321 */
1640 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1641 * If these functions are defined,
1642 * the BYTEORDER is neither 0x1234 nor 0x4321.
1643 * However, this is not assumed.
1647 #define HTOV(name,type) \
1654 char c[sizeof(type)]; \
1658 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1659 u.c[i] = (n >> s) & 0xFF; \
1664 #define VTOH(name,type) \
1671 char c[sizeof(type)]; \
1677 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1678 n += (u.c[i] & 0xFF) << s; \
1683 #if defined(HAS_HTOVS) && !defined(htovs)
1686 #if defined(HAS_HTOVL) && !defined(htovl)
1689 #if defined(HAS_VTOHS) && !defined(vtohs)
1692 #if defined(HAS_VTOHL) && !defined(vtohl)
1696 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1697 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1704 register I32 this, that;
1707 I32 doexec = strNE(cmd,"-");
1711 return my_syspopen(cmd,mode);
1716 this = (*mode == 'w');
1718 if (doexec && tainting) {
1720 taint_proper("Insecure %s%s", "EXEC");
1722 while ((pid = (doexec?vfork():fork())) < 0) {
1723 if (errno != EAGAIN) {
1726 croak("Can't fork");
1737 if (p[THIS] != (*mode == 'r')) {
1738 dup2(p[THIS], *mode == 'r');
1742 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1748 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1751 do_exec(cmd); /* may or may not use the shell */
1755 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1756 sv_setiv(GvSV(tmpgv), (IV)getpid());
1758 hv_clear(pidstatus); /* we have no children */
1763 do_execfree(); /* free any memory malloced by child on vfork */
1765 if (p[that] < p[this]) {
1766 dup2(p[this], p[that]);
1770 sv = *av_fetch(fdpid,p[this],TRUE);
1771 (void)SvUPGRADE(sv,SVt_IV);
1774 return PerlIO_fdopen(p[this], mode);
1777 #if defined(atarist) || defined(DJGPP)
1784 /* Needs work for PerlIO ! */
1785 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1786 return popen(PerlIO_exportFILE(cmd, 0), mode);
1790 #endif /* !DOSISH */
1797 struct stat tmpstatbuf;
1799 PerlIO_printf(PerlIO_stderr(),"%s", s);
1800 for (fd = 0; fd < 32; fd++) {
1801 if (Fstat(fd,&tmpstatbuf) >= 0)
1802 PerlIO_printf(PerlIO_stderr()," %d",fd);
1804 PerlIO_printf(PerlIO_stderr(),"\n");
1814 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1818 return fcntl(oldfd, F_DUPFD, newfd);
1820 #define DUP2_MAX_FDS 256
1821 int fdtmp[DUP2_MAX_FDS];
1828 /* good enough for low fd's... */
1829 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1830 if (fdx >= DUP2_MAX_FDS) {
1838 close(fdtmp[--fdx]);
1845 #ifdef HAS_SIGACTION
1848 rsignal(signo, handler)
1850 Sighandler_t handler;
1852 struct sigaction act, oact;
1854 act.sa_handler = handler;
1855 sigemptyset(&act.sa_mask);
1858 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1860 if (sigaction(signo, &act, &oact) == -1)
1863 return oact.sa_handler;
1867 rsignal_state(signo)
1870 struct sigaction oact;
1872 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1875 return oact.sa_handler;
1879 rsignal_save(signo, handler, save)
1881 Sighandler_t handler;
1884 struct sigaction act;
1886 act.sa_handler = handler;
1887 sigemptyset(&act.sa_mask);
1890 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1892 return sigaction(signo, &act, save);
1896 rsignal_restore(signo, save)
1900 return sigaction(signo, save, (struct sigaction *)NULL);
1903 #else /* !HAS_SIGACTION */
1906 rsignal(signo, handler)
1908 Sighandler_t handler;
1910 return signal(signo, handler);
1913 static int sig_trapped;
1924 rsignal_state(signo)
1927 Sighandler_t oldsig;
1930 oldsig = signal(signo, sig_trap);
1931 signal(signo, oldsig);
1933 kill(getpid(), signo);
1938 rsignal_save(signo, handler, save)
1940 Sighandler_t handler;
1943 *save = signal(signo, handler);
1944 return (*save == SIG_ERR) ? -1 : 0;
1948 rsignal_restore(signo, save)
1952 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1955 #endif /* !HAS_SIGACTION */
1957 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
1958 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1963 Sigsave_t hstat, istat, qstat;
1970 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1971 pid = (int)SvIVX(*svp);
1975 if (pid == -1) { /* Opened by popen. */
1976 return my_syspclose(ptr);
1979 if ((close_failed = (PerlIO_close(ptr) == EOF)))
1980 saved_errno = errno;
1982 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1984 rsignal_save(SIGHUP, SIG_IGN, &hstat);
1985 rsignal_save(SIGINT, SIG_IGN, &istat);
1986 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
1988 pid = wait4pid(pid, &status, 0);
1989 } while (pid == -1 && errno == EINTR);
1990 rsignal_restore(SIGHUP, &hstat);
1991 rsignal_restore(SIGINT, &istat);
1992 rsignal_restore(SIGQUIT, &qstat);
1994 errno = saved_errno;
1997 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
1999 #endif /* !DOSISH */
2001 #if !defined(DOSISH) || defined(OS2)
2003 wait4pid(pid,statusp,flags)
2010 char spid[TYPE_CHARS(int)];
2015 sprintf(spid, "%d", pid);
2016 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2017 if (svp && *svp != &sv_undef) {
2018 *statusp = SvIVX(*svp);
2019 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2026 hv_iterinit(pidstatus);
2027 if (entry = hv_iternext(pidstatus)) {
2028 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2029 sv = hv_iterval(pidstatus,entry);
2030 *statusp = SvIVX(sv);
2031 sprintf(spid, "%d", pid);
2032 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2037 return waitpid(pid,statusp,flags);
2040 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2045 croak("Can't do waitpid with flags");
2047 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2048 pidgone(result,*statusp);
2057 #endif /* !DOSISH */
2066 char spid[TYPE_CHARS(int)];
2068 sprintf(spid, "%d", pid);
2069 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2070 (void)SvUPGRADE(sv,SVt_IV);
2075 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2078 int /* Cannot prototype with I32
2087 /* Needs work for PerlIO ! */
2088 FILE *f = PerlIO_findFILE(ptr);
2089 I32 result = pclose(f);
2090 PerlIO_releaseFILE(ptr,f);
2096 repeatcpy(to,from,len,count)
2098 register char *from;
2103 register char *frombase = from;
2111 while (count-- > 0) {
2112 for (todo = len; todo > 0; todo--) {
2119 #ifndef CASTNEGFLOAT
2127 # define BIGDOUBLE 2147483648.0
2129 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2132 return (unsigned long)f;
2134 return (unsigned long)along;
2141 /* Unfortunately, on some systems the cast_uv() function doesn't
2142 work with the system-supplied definition of ULONG_MAX. The
2143 comparison (f >= ULONG_MAX) always comes out true. It must be a
2144 problem with the compiler constant folding.
2146 In any case, this workaround should be fine on any two's complement
2147 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2149 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2152 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2154 -- Kenneth Albanowski <kjahds@kjahds.com>
2158 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2166 return (I32) I32_MAX;
2168 return (I32) I32_MIN;
2188 return (UV) MY_UV_MAX;
2200 char *fa = strrchr(a,'/');
2201 char *fb = strrchr(b,'/');
2202 struct stat tmpstatbuf1;
2203 struct stat tmpstatbuf2;
2204 SV *tmpsv = sv_newmortal();
2217 sv_setpv(tmpsv, ".");
2219 sv_setpvn(tmpsv, a, fa - a);
2220 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2223 sv_setpv(tmpsv, ".");
2225 sv_setpvn(tmpsv, b, fb - b);
2226 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2228 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2229 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2231 #endif /* !HAS_RENAME */
2234 scan_oct(start, len, retlen)
2239 register char *s = start;
2240 register UV retval = 0;
2241 bool overflowed = FALSE;
2243 while (len && *s >= '0' && *s <= '7') {
2244 register UV n = retval << 3;
2245 if (!overflowed && (n >> 3) != retval) {
2246 warn("Integer overflow in octal number");
2249 retval = n | (*s++ - '0');
2252 if (dowarn && len && (*s == '8' || *s == '9'))
2253 warn("Illegal octal digit ignored");
2254 *retlen = s - start;
2259 scan_hex(start, len, retlen)
2264 register char *s = start;
2265 register UV retval = 0;
2266 bool overflowed = FALSE;
2269 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2270 register UV n = retval << 4;
2271 if (!overflowed && (n >> 4) != retval) {
2272 warn("Integer overflow in hex number");
2275 retval = n | (tmp - hexdigit) & 15;
2278 *retlen = s - start;
2285 * This hack is to force load of "huge" support from libm.a
2286 * So it is in perl for (say) POSIX to use.
2287 * Needed for SunOS with Sun's 'acc' for example.