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");
188 #if !(defined(I286) || defined(atarist))
189 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
191 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
194 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
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 cpytill(to,from,fromend,delim,retlen)
288 register char *fromend;
294 for (; from < fromend; from++,to++) {
296 if (from[1] == delim)
298 else if (from[1] == '\\')
301 else if (*from == delim)
306 *retlen = to - origto;
310 /* return ptr to little string in big string, NULL if not found */
311 /* This routine was donated by Corey Satten. */
316 register char *little;
318 register char *s, *x;
329 for (x=big,s=little; *s; /**/ ) {
343 /* same as instr but allow embedded nulls */
346 ninstr(big, bigend, little, lend)
348 register char *bigend;
352 register char *s, *x;
353 register I32 first = *little;
354 register char *littleend = lend;
356 if (!first && little >= littleend)
358 if (bigend - big < littleend - little)
360 bigend -= littleend - little++;
361 while (big <= bigend) {
364 for (x=big,s=little; s < littleend; /**/ ) {
376 /* reverse of the above--find last substring */
379 rninstr(big, bigend, little, lend)
385 register char *bigbeg;
386 register char *s, *x;
387 register I32 first = *little;
388 register char *littleend = lend;
390 if (!first && little >= littleend)
393 big = bigend - (littleend - little++);
394 while (big >= bigbeg) {
397 for (x=big+2,s=little; s < littleend; /**/ ) {
410 * Set up for a new ctype locale.
413 perl_new_ctype(newctype)
416 #ifdef USE_LOCALE_CTYPE
420 for (i = 0; i < 256; i++) {
422 fold_locale[i] = toLOWER_LC(i);
423 else if (isLOWER_LC(i))
424 fold_locale[i] = toUPPER_LC(i);
429 #endif /* USE_LOCALE_CTYPE */
433 * Set up for a new collation locale.
436 perl_new_collate(newcoll)
439 #ifdef USE_LOCALE_COLLATE
442 if (collation_name) {
444 Safefree(collation_name);
445 collation_name = NULL;
446 collation_standard = TRUE;
453 if (! collation_name || strNE(collation_name, newcoll)) {
455 Safefree(collation_name);
456 collation_name = savepv(newcoll);
457 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
460 /* 2: at most so many chars ('a', 'b'). */
461 /* 50: surely no system expands a char more. */
462 #define XFRMBUFSIZE (2 * 50)
463 char xbuf[XFRMBUFSIZE];
464 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
465 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
466 SSize_t mult = fb - fa;
468 croak("strxfrm() gets absurd");
469 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
470 collxfrm_mult = mult;
474 #endif /* USE_LOCALE_COLLATE */
478 * Set up for a new numeric locale.
481 perl_new_numeric(newnum)
484 #ifdef USE_LOCALE_NUMERIC
488 Safefree(numeric_name);
490 numeric_standard = TRUE;
491 numeric_local = TRUE;
496 if (! numeric_name || strNE(numeric_name, newnum)) {
497 Safefree(numeric_name);
498 numeric_name = savepv(newnum);
499 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
500 numeric_local = TRUE;
503 #endif /* USE_LOCALE_NUMERIC */
507 perl_set_numeric_standard()
509 #ifdef USE_LOCALE_NUMERIC
511 if (! numeric_standard) {
512 setlocale(LC_NUMERIC, "C");
513 numeric_standard = TRUE;
514 numeric_local = FALSE;
517 #endif /* USE_LOCALE_NUMERIC */
521 perl_set_numeric_local()
523 #ifdef USE_LOCALE_NUMERIC
525 if (! numeric_local) {
526 setlocale(LC_NUMERIC, numeric_name);
527 numeric_standard = FALSE;
528 numeric_local = TRUE;
531 #endif /* USE_LOCALE_NUMERIC */
536 * Initialize locale awareness.
539 perl_init_i18nl10n(printwarn)
544 * 1 = set ok or not applicable,
545 * 0 = fallback to C locale,
546 * -1 = fallback to C locale failed
551 #ifdef USE_LOCALE_CTYPE
552 char *curctype = NULL;
553 #endif /* USE_LOCALE_CTYPE */
554 #ifdef USE_LOCALE_COLLATE
555 char *curcoll = NULL;
556 #endif /* USE_LOCALE_COLLATE */
557 #ifdef USE_LOCALE_NUMERIC
559 #endif /* USE_LOCALE_NUMERIC */
560 char *lc_all = getenv("LC_ALL");
561 char *lang = getenv("LANG");
562 bool setlocale_failure = FALSE;
564 #ifdef LOCALE_ENVIRON_REQUIRED
567 * Ultrix setlocale(..., "") fails if there are no environment
568 * variables from which to get a locale name.
575 if (setlocale(LC_ALL, ""))
578 setlocale_failure = TRUE;
580 if (!setlocale_failure)
583 #ifdef USE_LOCALE_CTYPE
584 if (! (curctype = setlocale(LC_CTYPE,
585 (!done && (lang || getenv("LC_CTYPE")))
587 setlocale_failure = TRUE;
588 #endif /* USE_LOCALE_CTYPE */
589 #ifdef USE_LOCALE_COLLATE
590 if (! (curcoll = setlocale(LC_COLLATE,
591 (!done && (lang || getenv("LC_COLLATE")))
593 setlocale_failure = TRUE;
594 #endif /* USE_LOCALE_COLLATE */
595 #ifdef USE_LOCALE_NUMERIC
596 if (! (curnum = setlocale(LC_NUMERIC,
597 (!done && (lang || getenv("LC_NUMERIC")))
599 setlocale_failure = TRUE;
600 #endif /* USE_LOCALE_NUMERIC */
603 #else /* !LOCALE_ENVIRON_REQUIRED */
607 if (! setlocale(LC_ALL, ""))
608 setlocale_failure = TRUE;
610 #ifdef USE_LOCALE_CTYPE
611 curctype = setlocale(LC_CTYPE, Nullch);
612 #endif /* USE_LOCALE_CTYPE */
613 #ifdef USE_LOCALE_COLLATE
614 curcoll = setlocale(LC_COLLATE, Nullch);
615 #endif /* USE_LOCALE_COLLATE */
616 #ifdef USE_LOCALE_NUMERIC
617 curnum = setlocale(LC_NUMERIC, Nullch);
618 #endif /* USE_LOCALE_NUMERIC */
623 #ifdef USE_LOCALE_CTYPE
624 if (! (curctype = setlocale(LC_CTYPE, "")))
625 setlocale_failure = TRUE;
626 #endif /* USE_LOCALE_CTYPE */
627 #ifdef USE_LOCALE_COLLATE
628 if (! (curcoll = setlocale(LC_COLLATE, "")))
629 setlocale_failure = TRUE;
630 #endif /* USE_LOCALE_COLLATE */
631 #ifdef USE_LOCALE_NUMERIC
632 if (! (curnum = setlocale(LC_NUMERIC, "")))
633 setlocale_failure = TRUE;
634 #endif /* USE_LOCALE_NUMERIC */
638 #endif /* !LOCALE_ENVIRON_REQUIRED */
640 if (setlocale_failure) {
642 bool locwarn = (printwarn > 1 ||
644 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
649 PerlIO_printf(PerlIO_stderr(),
650 "perl: warning: Setting locale failed.\n");
654 PerlIO_printf(PerlIO_stderr(),
655 "perl: warning: Setting locale failed for the categories:\n\t");
656 #ifdef USE_LOCALE_CTYPE
658 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
659 #endif /* USE_LOCALE_CTYPE */
660 #ifdef USE_LOCALE_COLLATE
662 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
663 #endif /* USE_LOCALE_COLLATE */
664 #ifdef USE_LOCALE_NUMERIC
666 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
667 #endif /* USE_LOCALE_NUMERIC */
668 PerlIO_printf(PerlIO_stderr(), "\n");
672 PerlIO_printf(PerlIO_stderr(),
673 "perl: warning: Please check that your locale settings:\n");
675 PerlIO_printf(PerlIO_stderr(),
676 "\tLC_ALL = %c%s%c,\n",
678 lc_all ? lc_all : "unset",
683 for (e = environ; *e; e++) {
684 if (strnEQ(*e, "LC_", 3)
685 && strnNE(*e, "LC_ALL=", 7)
686 && (p = strchr(*e, '=')))
687 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
688 (p - *e), *e, p + 1);
692 PerlIO_printf(PerlIO_stderr(),
695 lang ? lang : "unset",
698 PerlIO_printf(PerlIO_stderr(),
699 " are supported and installed on your system.\n");
704 if (setlocale(LC_ALL, "C")) {
706 PerlIO_printf(PerlIO_stderr(),
707 "perl: warning: Falling back to the standard locale (\"C\").\n");
712 PerlIO_printf(PerlIO_stderr(),
713 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
720 #ifdef USE_LOCALE_CTYPE
721 || !(curctype || setlocale(LC_CTYPE, "C"))
722 #endif /* USE_LOCALE_CTYPE */
723 #ifdef USE_LOCALE_COLLATE
724 || !(curcoll || setlocale(LC_COLLATE, "C"))
725 #endif /* USE_LOCALE_COLLATE */
726 #ifdef USE_LOCALE_NUMERIC
727 || !(curnum || setlocale(LC_NUMERIC, "C"))
728 #endif /* USE_LOCALE_NUMERIC */
732 PerlIO_printf(PerlIO_stderr(),
733 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
737 #endif /* ! LC_ALL */
739 #ifdef USE_LOCALE_CTYPE
740 curctype = setlocale(LC_CTYPE, Nullch);
741 #endif /* USE_LOCALE_CTYPE */
742 #ifdef USE_LOCALE_COLLATE
743 curcoll = setlocale(LC_COLLATE, Nullch);
744 #endif /* USE_LOCALE_COLLATE */
745 #ifdef USE_LOCALE_NUMERIC
746 curnum = setlocale(LC_NUMERIC, Nullch);
747 #endif /* USE_LOCALE_NUMERIC */
750 #ifdef USE_LOCALE_CTYPE
751 perl_new_ctype(curctype);
752 #endif /* USE_LOCALE_CTYPE */
754 #ifdef USE_LOCALE_COLLATE
755 perl_new_collate(curcoll);
756 #endif /* USE_LOCALE_COLLATE */
758 #ifdef USE_LOCALE_NUMERIC
759 perl_new_numeric(curnum);
760 #endif /* USE_LOCALE_NUMERIC */
762 #endif /* USE_LOCALE */
767 /* Backwards compatibility. */
769 perl_init_i18nl14n(printwarn)
772 return perl_init_i18nl10n(printwarn);
775 #ifdef USE_LOCALE_COLLATE
778 * mem_collxfrm() is a bit like strxfrm() but with two important
779 * differences. First, it handles embedded NULs. Second, it allocates
780 * a bit more memory than needed for the transformed data itself.
781 * The real transformed data begins at offset sizeof(collationix).
782 * Please see sv_collxfrm() to see how this is used.
785 mem_collxfrm(s, len, xlen)
791 STRLEN xalloc, xin, xout;
793 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
794 /* the +1 is for the terminating NUL. */
796 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
797 New(171, xbuf, xalloc, char);
801 *(U32*)xbuf = collation_ix;
802 xout = sizeof(collation_ix);
803 for (xin = 0; xin < len; ) {
807 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
810 if (xused < xalloc - xout)
812 xalloc = (2 * xalloc) + 1;
813 Renew(xbuf, xalloc, char);
818 xin += strlen(s + xin) + 1;
821 /* Embedded NULs are understood but silently skipped
822 * because they make no sense in locale collation. */
826 *xlen = xout - sizeof(collation_ix);
835 #endif /* USE_LOCALE_COLLATE */
841 register unsigned char *s;
842 register unsigned char *table;
844 register U32 len = SvCUR(sv);
849 return; /* can't have offsets that big */
851 table = (unsigned char*)(SvPVX(sv) + len + 1);
853 for (i = 0; i < 256; i++) {
857 while (s >= (unsigned char*)(SvPVX(sv)))
859 if (table[*s] == len)
863 sv_upgrade(sv, SVt_PVBM);
864 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
867 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
868 for (i = 0; i < len; i++) {
869 if (freq[s[i]] < frequency) {
871 frequency = freq[s[i]];
874 BmRARE(sv) = s[rarest];
875 BmPREVIOUS(sv) = rarest;
876 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
880 fbm_instr(big, bigend, littlestr)
882 register unsigned char *bigend;
885 register unsigned char *s;
887 register I32 littlelen;
888 register unsigned char *little;
889 register unsigned char *table;
890 register unsigned char *olds;
891 register unsigned char *oldlittle;
893 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
895 char *l = SvPV(littlestr,len);
898 return ninstr((char*)big,(char*)bigend, l, l + len);
901 littlelen = SvCUR(littlestr);
902 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
903 if (littlelen > bigend - big)
905 little = (unsigned char*)SvPVX(littlestr);
906 s = bigend - littlelen;
907 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
908 return (char*)s; /* how sweet it is */
909 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
912 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
917 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
918 if (--littlelen >= bigend - big)
921 oldlittle = little = table - 2;
925 if (tmp = table[*s]) {
927 if (bigend - s > tmp) {
932 if ((s += tmp) < bigend)
938 tmp = littlelen; /* less expensive than calling strncmp() */
941 if (*--s == *--little)
943 s = olds + 1; /* here we pay the price for failure */
945 if (s < bigend) /* fake up continue to outer loop */
956 screaminstr(bigstr, littlestr)
960 register unsigned char *s, *x;
961 register unsigned char *big;
963 register I32 previous;
965 register unsigned char *little;
966 register unsigned char *bigend;
967 register unsigned char *littleend;
969 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
971 little = (unsigned char *)(SvPVX(littlestr));
972 littleend = little + SvCUR(littlestr);
974 previous = BmPREVIOUS(littlestr);
975 big = (unsigned char *)(SvPVX(bigstr));
976 bigend = big + SvCUR(bigstr);
977 while (pos < previous) {
978 if (!(pos += screamnext[pos]))
983 if (big[pos-previous] != first)
985 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
994 return (char *)(big+pos-previous);
995 } while ( pos += screamnext[pos] );
996 #else /* !POINTERRIGOR */
999 if (big[pos] != first)
1001 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1010 return (char *)(big+pos);
1011 } while ( pos += screamnext[pos] );
1012 #endif /* POINTERRIGOR */
1021 register U8 *a = (U8 *)s1;
1022 register U8 *b = (U8 *)s2;
1024 if (*a != *b && *a != fold[*b])
1032 ibcmp_locale(s1, s2, len)
1036 register U8 *a = (U8 *)s1;
1037 register U8 *b = (U8 *)s2;
1039 if (*a != *b && *a != fold_locale[*b])
1046 /* copy a string to a safe spot */
1052 register char *newaddr;
1054 New(902,newaddr,strlen(sv)+1,char);
1055 (void)strcpy(newaddr,sv);
1059 /* same thing but with a known length */
1066 register char *newaddr;
1068 New(903,newaddr,len+1,char);
1069 Copy(sv,newaddr,len,char); /* might not be null terminated */
1070 newaddr[len] = '\0'; /* is now */
1076 mess(const char *pat, va_list *args)
1090 #ifdef USE_CHAR_VSPRINTF
1098 usermess = strEQ(pat, "%s");
1100 tmpstr = sv_newmortal();
1101 sv_setpv(tmpstr, va_arg(*args, char *));
1102 *s++ = SvCUR(tmpstr) ? SvPVX(tmpstr)[SvCUR(tmpstr)-1] : ' ';
1105 (void) vsprintf(s,pat,*args);
1110 if (!(s > s_start && s[-1] == '\n')) {
1112 strcpy(s, " during global destruction.\n");
1114 if (curcop->cop_line) {
1115 (void)sprintf(s," at %s line %ld",
1116 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
1119 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1120 bool line_mode = (RsSIMPLE(rs) &&
1121 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1122 (void)sprintf(s,", <%s> %s %ld",
1123 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1124 line_mode ? "line" : "chunk",
1125 (long)IoLINES(GvIOp(last_in_gv)));
1128 (void)strcpy(s,".\n");
1132 sv_catpv(tmpstr,buf+1);
1135 if (s - s_start >= sizeof(buf)) { /* Ooops! */
1137 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
1139 PerlIO_puts(PerlIO_stderr(), buf);
1140 PerlIO_puts(PerlIO_stderr(), "panic: message overflow - memory corrupted!\n");
1144 return SvPVX(tmpstr);
1151 die(const char* pat, ...)
1162 I32 oldrunlevel = runlevel;
1163 int was_in_eval = in_eval;
1168 /* We have to switch back to mainstack or die_where may try to pop
1169 * the eval block from the wrong stack if die is being called from a
1170 * signal handler. - dkindred@cs.cmu.edu */
1171 if (curstack != mainstack) {
1173 SWITCHSTACK(curstack, mainstack);
1177 va_start(args, pat);
1181 message = mess(pat, &args);
1185 /* sv_2cv might call croak() */
1186 SV *olddiehook = diehook;
1190 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1192 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1197 msg = newSVpv(message, 0);
1204 perl_call_sv((SV*)cv, G_DISCARD);
1210 restartop = die_where(message);
1211 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1218 croak(const char* pat, ...)
1222 croak(pat, va_alist)
1234 va_start(args, pat);
1238 message = mess(pat, &args);
1241 /* sv_2cv might call croak() */
1242 SV *olddiehook = diehook;
1246 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1248 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1253 msg = newSVpv(message, 0);
1260 perl_call_sv((SV*)cv, G_DISCARD);
1266 restartop = die_where(message);
1269 PerlIO_puts(PerlIO_stderr(),message);
1270 (void)PerlIO_flush(PerlIO_stderr());
1276 warn(const char* pat,...)
1291 va_start(args, pat);
1295 message = mess(pat, &args);
1299 /* sv_2cv might call warn() */
1300 SV *oldwarnhook = warnhook;
1304 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1306 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1311 msg = newSVpv(message, 0);
1318 perl_call_sv((SV*)cv, G_DISCARD);
1324 PerlIO_puts(PerlIO_stderr(),message);
1328 (void)PerlIO_flush(PerlIO_stderr());
1331 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1337 register I32 i=setenv_getix(nam); /* where does it go? */
1339 if (environ == origenviron) { /* need we copy environment? */
1345 for (max = i; environ[max]; max++) ;
1346 New(901,tmpenv, max+2, char*);
1347 for (j=0; j<max; j++) /* copy environment */
1348 tmpenv[j] = savepv(environ[j]);
1349 tmpenv[max] = Nullch;
1350 environ = tmpenv; /* tell exec where it is now */
1353 Safefree(environ[i]);
1354 while (environ[i]) {
1355 environ[i] = environ[i+1];
1360 if (!environ[i]) { /* does not exist yet */
1361 Renew(environ, i+2, char*); /* just expand it a bit */
1362 environ[i+1] = Nullch; /* make sure it's null terminated */
1365 Safefree(environ[i]);
1366 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1368 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1370 /* MS-DOS requires environment variable names to be in uppercase */
1371 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1372 * some utilities and applications may break because they only look
1373 * for upper case strings. (Fixed strupr() bug here.)]
1375 strcpy(environ[i],nam); strupr(environ[i]);
1376 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1384 register I32 i, len = strlen(nam);
1386 for (i = 0; environ[i]; i++) {
1387 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1388 break; /* strnEQ must come first to avoid */
1389 } /* potential SEGV's */
1393 #else /* if _WIN32 */
1399 register char *envstr;
1400 STRLEN namlen = strlen(nam);
1401 STRLEN vallen = strlen(val ? val : "");
1403 New(9040, envstr, namlen + vallen + 3, char);
1404 (void)sprintf(envstr,"%s=%s",nam,val);
1406 /* An attempt to delete the entry.
1407 * We try to fix a Win32 process handling goof: Children
1408 * of the current process will end up seeing the
1409 * grandparent's entry if the current process has never
1410 * modified the entry being deleted. So we call _putenv()
1411 * twice: once to pretend to modify the entry, and the
1412 * second time to actually delete it. GSAR 97-03-19
1414 envstr[namlen+1] = 'X'; envstr[namlen+2] = '\0';
1415 (void)_putenv(envstr);
1416 envstr[namlen+1] = '\0';
1418 (void)_putenv(envstr);
1424 #ifdef UNLINK_ALL_VERSIONS
1426 unlnk(f) /* unlink all versions of a file */
1431 for (i = 0; unlink(f) >= 0; i++) ;
1436 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1438 my_bcopy(from,to,len)
1439 register char *from;
1445 if (from - to >= 0) {
1453 *(--to) = *(--from);
1459 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1473 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1475 my_memcmp(s1,s2,len)
1480 register U8 *a = (U8 *)s1;
1481 register U8 *b = (U8 *)s2;
1485 if (tmp = *a++ - *b++)
1490 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1492 #if defined(I_STDARG) || defined(I_VARARGS)
1495 #ifdef USE_CHAR_VSPRINTF
1500 vsprintf(dest, pat, args)
1507 fakebuf._ptr = dest;
1508 fakebuf._cnt = 32767;
1512 fakebuf._flag = _IOWRT|_IOSTRG;
1513 _doprnt(pat, args, &fakebuf); /* what a kludge */
1514 (void)putc('\0', &fakebuf);
1515 #ifdef USE_CHAR_VSPRINTF
1518 return 0; /* perl doesn't use return value */
1522 #endif /* HAS_VPRINTF */
1523 #endif /* I_VARARGS || I_STDARGS */
1526 #if BYTEORDER != 0x4321
1528 #ifndef CAN_PROTOTYPE
1535 #if (BYTEORDER & 1) == 0
1538 result = ((s & 255) << 8) + ((s >> 8) & 255);
1546 #ifndef CAN_PROTOTYPE
1555 char c[sizeof(long)];
1558 #if BYTEORDER == 0x1234
1559 u.c[0] = (l >> 24) & 255;
1560 u.c[1] = (l >> 16) & 255;
1561 u.c[2] = (l >> 8) & 255;
1565 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1566 croak("Unknown BYTEORDER\n");
1571 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1572 u.c[o & 0xf] = (l >> s) & 255;
1580 #ifndef CAN_PROTOTYPE
1589 char c[sizeof(long)];
1592 #if BYTEORDER == 0x1234
1593 u.c[0] = (l >> 24) & 255;
1594 u.c[1] = (l >> 16) & 255;
1595 u.c[2] = (l >> 8) & 255;
1599 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1600 croak("Unknown BYTEORDER\n");
1607 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1608 l |= (u.c[o & 0xf] & 255) << s;
1615 #endif /* BYTEORDER != 0x4321 */
1619 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1620 * If these functions are defined,
1621 * the BYTEORDER is neither 0x1234 nor 0x4321.
1622 * However, this is not assumed.
1626 #define HTOV(name,type) \
1633 char c[sizeof(type)]; \
1637 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1638 u.c[i] = (n >> s) & 0xFF; \
1643 #define VTOH(name,type) \
1650 char c[sizeof(type)]; \
1656 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1657 n += (u.c[i] & 0xFF) << s; \
1662 #if defined(HAS_HTOVS) && !defined(htovs)
1665 #if defined(HAS_HTOVL) && !defined(htovl)
1668 #if defined(HAS_VTOHS) && !defined(vtohs)
1671 #if defined(HAS_VTOHL) && !defined(vtohl)
1675 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1676 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1683 register I32 this, that;
1686 I32 doexec = strNE(cmd,"-");
1690 return my_syspopen(cmd,mode);
1695 this = (*mode == 'w');
1697 if (doexec && tainting) {
1699 taint_proper("Insecure %s%s", "EXEC");
1701 while ((pid = (doexec?vfork():fork())) < 0) {
1702 if (errno != EAGAIN) {
1705 croak("Can't fork");
1716 if (p[THIS] != (*mode == 'r')) {
1717 dup2(p[THIS], *mode == 'r');
1721 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1727 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1730 do_exec(cmd); /* may or may not use the shell */
1734 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1735 sv_setiv(GvSV(tmpgv), (IV)getpid());
1737 hv_clear(pidstatus); /* we have no children */
1742 do_execfree(); /* free any memory malloced by child on vfork */
1744 if (p[that] < p[this]) {
1745 dup2(p[this], p[that]);
1749 sv = *av_fetch(fdpid,p[this],TRUE);
1750 (void)SvUPGRADE(sv,SVt_IV);
1753 return PerlIO_fdopen(p[this], mode);
1756 #if defined(atarist) || defined(DJGPP)
1763 /* Needs work for PerlIO ! */
1764 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1765 return popen(PerlIO_exportFILE(cmd, 0), mode);
1769 #endif /* !DOSISH */
1776 struct stat tmpstatbuf;
1778 PerlIO_printf(PerlIO_stderr(),"%s", s);
1779 for (fd = 0; fd < 32; fd++) {
1780 if (Fstat(fd,&tmpstatbuf) >= 0)
1781 PerlIO_printf(PerlIO_stderr()," %d",fd);
1783 PerlIO_printf(PerlIO_stderr(),"\n");
1793 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1797 return fcntl(oldfd, F_DUPFD, newfd);
1806 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1809 close(fdtmp[--fdx]);
1816 #ifdef HAS_SIGACTION
1819 rsignal(signo, handler)
1821 Sighandler_t handler;
1823 struct sigaction act, oact;
1825 act.sa_handler = handler;
1826 sigemptyset(&act.sa_mask);
1829 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1831 if (sigaction(signo, &act, &oact) == -1)
1834 return oact.sa_handler;
1838 rsignal_state(signo)
1841 struct sigaction oact;
1843 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1846 return oact.sa_handler;
1850 rsignal_save(signo, handler, save)
1852 Sighandler_t handler;
1855 struct sigaction act;
1857 act.sa_handler = handler;
1858 sigemptyset(&act.sa_mask);
1861 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1863 return sigaction(signo, &act, save);
1867 rsignal_restore(signo, save)
1871 return sigaction(signo, save, (struct sigaction *)NULL);
1874 #else /* !HAS_SIGACTION */
1877 rsignal(signo, handler)
1879 Sighandler_t handler;
1881 return signal(signo, handler);
1884 static int sig_trapped;
1895 rsignal_state(signo)
1898 Sighandler_t oldsig;
1901 oldsig = signal(signo, sig_trap);
1902 signal(signo, oldsig);
1904 kill(getpid(), signo);
1909 rsignal_save(signo, handler, save)
1911 Sighandler_t handler;
1914 *save = signal(signo, handler);
1915 return (*save == SIG_ERR) ? -1 : 0;
1919 rsignal_restore(signo, save)
1923 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1926 #endif /* !HAS_SIGACTION */
1928 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
1929 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1934 Sigsave_t hstat, istat, qstat;
1939 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1940 pid = (int)SvIVX(*svp);
1944 if (pid == -1) { /* Opened by popen. */
1945 return my_syspclose(ptr);
1950 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1952 rsignal_save(SIGHUP, SIG_IGN, &hstat);
1953 rsignal_save(SIGINT, SIG_IGN, &istat);
1954 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
1956 pid = wait4pid(pid, &status, 0);
1957 } while (pid == -1 && errno == EINTR);
1958 rsignal_restore(SIGHUP, &hstat);
1959 rsignal_restore(SIGINT, &istat);
1960 rsignal_restore(SIGQUIT, &qstat);
1961 return(pid < 0 ? pid : status);
1963 #endif /* !DOSISH */
1965 #if !defined(DOSISH) || defined(OS2)
1967 wait4pid(pid,statusp,flags)
1979 sprintf(spid, "%d", pid);
1980 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1981 if (svp && *svp != &sv_undef) {
1982 *statusp = SvIVX(*svp);
1983 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1990 hv_iterinit(pidstatus);
1991 if (entry = hv_iternext(pidstatus)) {
1992 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1993 sv = hv_iterval(pidstatus,entry);
1994 *statusp = SvIVX(sv);
1995 sprintf(spid, "%d", pid);
1996 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2001 return waitpid(pid,statusp,flags);
2004 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2009 croak("Can't do waitpid with flags");
2011 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2012 pidgone(result,*statusp);
2021 #endif /* !DOSISH */
2032 sprintf(spid, "%d", pid);
2033 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2034 (void)SvUPGRADE(sv,SVt_IV);
2039 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2042 int /* Cannot prototype with I32
2051 /* Needs work for PerlIO ! */
2052 FILE *f = PerlIO_findFILE(ptr);
2053 I32 result = pclose(f);
2054 PerlIO_releaseFILE(ptr,f);
2060 repeatcpy(to,from,len,count)
2062 register char *from;
2067 register char *frombase = from;
2075 while (count-- > 0) {
2076 for (todo = len; todo > 0; todo--) {
2083 #ifndef CASTNEGFLOAT
2091 # define BIGDOUBLE 2147483648.0
2093 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2096 return (unsigned long)f;
2098 return (unsigned long)along;
2105 /* Unfortunately, on some systems the cast_uv() function doesn't
2106 work with the system-supplied definition of ULONG_MAX. The
2107 comparison (f >= ULONG_MAX) always comes out true. It must be a
2108 problem with the compiler constant folding.
2110 In any case, this workaround should be fine on any two's complement
2111 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2113 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2116 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2118 -- Kenneth Albanowski <kjahds@kjahds.com>
2122 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2130 return (I32) I32_MAX;
2132 return (I32) I32_MIN;
2152 return (UV) MY_UV_MAX;
2164 char *fa = strrchr(a,'/');
2165 char *fb = strrchr(b,'/');
2166 struct stat tmpstatbuf1;
2167 struct stat tmpstatbuf2;
2169 #define MAXPATHLEN 1024
2171 char tmpbuf[MAXPATHLEN+1];
2186 strncpy(tmpbuf, a, fa - a);
2187 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
2192 strncpy(tmpbuf, b, fb - b);
2193 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
2195 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2196 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2198 #endif /* !HAS_RENAME */
2201 scan_oct(start, len, retlen)
2206 register char *s = start;
2207 register UV retval = 0;
2208 bool overflowed = FALSE;
2210 while (len && *s >= '0' && *s <= '7') {
2211 register UV n = retval << 3;
2212 if (!overflowed && (n >> 3) != retval) {
2213 warn("Integer overflow in octal number");
2216 retval = n | (*s++ - '0');
2219 if (dowarn && len && (*s == '8' || *s == '9'))
2220 warn("Illegal octal digit ignored");
2221 *retlen = s - start;
2226 scan_hex(start, len, retlen)
2231 register char *s = start;
2232 register UV retval = 0;
2233 bool overflowed = FALSE;
2236 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2237 register UV n = retval << 4;
2238 if (!overflowed && (n >> 4) != retval) {
2239 warn("Integer overflow in hex number");
2242 retval = n | (tmp - hexdigit) & 15;
2245 *retlen = s - start;
2252 * This hack is to force load of "huge" support from libm.a
2253 * So it is in perl for (say) POSIX to use.
2254 * Needed for SunOS with Sun's 'acc' for example.