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.
70 safemalloc(MEM_SIZE size)
75 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
78 #endif /* HAS_64K_LIMIT */
81 croak("panic: malloc");
83 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
84 #if !(defined(I286) || defined(atarist))
85 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
87 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
94 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
100 /* paranoid version of realloc */
103 saferealloc(Malloc_t where,MEM_SIZE size)
106 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
108 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
112 PerlIO_printf(PerlIO_stderr(),
113 "Reallocation too large: %lx\n", size) FLUSH;
116 #endif /* HAS_64K_LIMIT */
118 croak("Null realloc");
121 croak("panic: realloc");
123 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
125 #if !(defined(I286) || defined(atarist))
127 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
128 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
132 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
133 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
142 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
148 /* safe version of free */
151 safefree(Malloc_t where)
153 #if !(defined(I286) || defined(atarist))
154 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
156 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
164 /* safe version of calloc */
167 safecalloc(MEM_SIZE count, MEM_SIZE size)
172 if (size * count > 0xffff) {
173 PerlIO_printf(PerlIO_stderr(),
174 "Allocation too large: %lx\n", size * count) FLUSH;
177 #endif /* HAS_64K_LIMIT */
179 if ((long)size < 0 || (long)count < 0)
180 croak("panic: calloc");
183 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
184 #if !(defined(I286) || defined(atarist))
185 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
187 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
190 memset((void*)ptr, 0, size);
196 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
202 #endif /* !MYMALLOC */
206 #define ALIGN sizeof(long)
209 safexmalloc(I32 x, MEM_SIZE size)
211 register Malloc_t where;
213 where = safemalloc(size + ALIGN);
217 return where + ALIGN;
221 safexrealloc(Malloc_t where, MEM_SIZE size)
223 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
228 safexfree(Malloc_t where)
235 x = where[0] + 100 * where[1];
241 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
243 register Malloc_t where;
245 where = safexmalloc(x, size * count + ALIGN);
247 memset((void*)where + ALIGN, 0, size * count);
250 return where + ALIGN;
258 for (i = 0; i < MAXXCOUNT; i++) {
259 if (xcount[i] > lastxcount[i]) {
260 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
261 lastxcount[i] = xcount[i];
266 #endif /* LEAKTEST */
268 /* copy a string up to some (non-backslashed) delimiter, if any */
271 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
274 for (tolen = 0; from < fromend; from++, tolen++) {
276 if (from[1] == delim)
285 else if (*from == delim)
296 /* return ptr to little string in big string, NULL if not found */
297 /* This routine was donated by Corey Satten. */
300 instr(register char *big, register char *little)
302 register char *s, *x;
313 for (x=big,s=little; *s; /**/ ) {
327 /* same as instr but allow embedded nulls */
330 ninstr(register char *big, register char *bigend, char *little, char *lend)
332 register char *s, *x;
333 register I32 first = *little;
334 register char *littleend = lend;
336 if (!first && little >= littleend)
338 if (bigend - big < littleend - little)
340 bigend -= littleend - little++;
341 while (big <= bigend) {
344 for (x=big,s=little; s < littleend; /**/ ) {
356 /* reverse of the above--find last substring */
359 rninstr(register char *big, char *bigend, char *little, char *lend)
361 register char *bigbeg;
362 register char *s, *x;
363 register I32 first = *little;
364 register char *littleend = lend;
366 if (!first && little >= littleend)
369 big = bigend - (littleend - little++);
370 while (big >= bigbeg) {
373 for (x=big+2,s=little; s < littleend; /**/ ) {
386 * Set up for a new ctype locale.
389 perl_new_ctype(char *newctype)
391 #ifdef USE_LOCALE_CTYPE
395 for (i = 0; i < 256; i++) {
397 fold_locale[i] = toLOWER_LC(i);
398 else if (isLOWER_LC(i))
399 fold_locale[i] = toUPPER_LC(i);
404 #endif /* USE_LOCALE_CTYPE */
408 * Set up for a new collation locale.
411 perl_new_collate(char *newcoll)
413 #ifdef USE_LOCALE_COLLATE
416 if (collation_name) {
418 Safefree(collation_name);
419 collation_name = NULL;
420 collation_standard = TRUE;
427 if (! collation_name || strNE(collation_name, newcoll)) {
429 Safefree(collation_name);
430 collation_name = savepv(newcoll);
431 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
434 /* 2: at most so many chars ('a', 'b'). */
435 /* 50: surely no system expands a char more. */
436 #define XFRMBUFSIZE (2 * 50)
437 char xbuf[XFRMBUFSIZE];
438 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
439 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
440 SSize_t mult = fb - fa;
442 croak("strxfrm() gets absurd");
443 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
444 collxfrm_mult = mult;
448 #endif /* USE_LOCALE_COLLATE */
452 * Set up for a new numeric locale.
455 perl_new_numeric(char *newnum)
457 #ifdef USE_LOCALE_NUMERIC
461 Safefree(numeric_name);
463 numeric_standard = TRUE;
464 numeric_local = TRUE;
469 if (! numeric_name || strNE(numeric_name, newnum)) {
470 Safefree(numeric_name);
471 numeric_name = savepv(newnum);
472 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
473 numeric_local = TRUE;
476 #endif /* USE_LOCALE_NUMERIC */
480 perl_set_numeric_standard(void)
482 #ifdef USE_LOCALE_NUMERIC
484 if (! numeric_standard) {
485 setlocale(LC_NUMERIC, "C");
486 numeric_standard = TRUE;
487 numeric_local = FALSE;
490 #endif /* USE_LOCALE_NUMERIC */
494 perl_set_numeric_local(void)
496 #ifdef USE_LOCALE_NUMERIC
498 if (! numeric_local) {
499 setlocale(LC_NUMERIC, numeric_name);
500 numeric_standard = FALSE;
501 numeric_local = TRUE;
504 #endif /* USE_LOCALE_NUMERIC */
509 * Initialize locale awareness.
512 perl_init_i18nl10n(int printwarn)
516 * 1 = set ok or not applicable,
517 * 0 = fallback to C locale,
518 * -1 = fallback to C locale failed
523 #ifdef USE_LOCALE_CTYPE
524 char *curctype = NULL;
525 #endif /* USE_LOCALE_CTYPE */
526 #ifdef USE_LOCALE_COLLATE
527 char *curcoll = NULL;
528 #endif /* USE_LOCALE_COLLATE */
529 #ifdef USE_LOCALE_NUMERIC
531 #endif /* USE_LOCALE_NUMERIC */
532 char *lc_all = getenv("LC_ALL");
533 char *lang = getenv("LANG");
534 bool setlocale_failure = FALSE;
536 #ifdef LOCALE_ENVIRON_REQUIRED
539 * Ultrix setlocale(..., "") fails if there are no environment
540 * variables from which to get a locale name.
547 if (setlocale(LC_ALL, ""))
550 setlocale_failure = TRUE;
552 if (!setlocale_failure)
555 #ifdef USE_LOCALE_CTYPE
556 if (! (curctype = setlocale(LC_CTYPE,
557 (!done && (lang || getenv("LC_CTYPE")))
559 setlocale_failure = TRUE;
560 #endif /* USE_LOCALE_CTYPE */
561 #ifdef USE_LOCALE_COLLATE
562 if (! (curcoll = setlocale(LC_COLLATE,
563 (!done && (lang || getenv("LC_COLLATE")))
565 setlocale_failure = TRUE;
566 #endif /* USE_LOCALE_COLLATE */
567 #ifdef USE_LOCALE_NUMERIC
568 if (! (curnum = setlocale(LC_NUMERIC,
569 (!done && (lang || getenv("LC_NUMERIC")))
571 setlocale_failure = TRUE;
572 #endif /* USE_LOCALE_NUMERIC */
575 #else /* !LOCALE_ENVIRON_REQUIRED */
579 if (! setlocale(LC_ALL, ""))
580 setlocale_failure = TRUE;
582 #ifdef USE_LOCALE_CTYPE
583 curctype = setlocale(LC_CTYPE, Nullch);
584 #endif /* USE_LOCALE_CTYPE */
585 #ifdef USE_LOCALE_COLLATE
586 curcoll = setlocale(LC_COLLATE, Nullch);
587 #endif /* USE_LOCALE_COLLATE */
588 #ifdef USE_LOCALE_NUMERIC
589 curnum = setlocale(LC_NUMERIC, Nullch);
590 #endif /* USE_LOCALE_NUMERIC */
595 #ifdef USE_LOCALE_CTYPE
596 if (! (curctype = setlocale(LC_CTYPE, "")))
597 setlocale_failure = TRUE;
598 #endif /* USE_LOCALE_CTYPE */
599 #ifdef USE_LOCALE_COLLATE
600 if (! (curcoll = setlocale(LC_COLLATE, "")))
601 setlocale_failure = TRUE;
602 #endif /* USE_LOCALE_COLLATE */
603 #ifdef USE_LOCALE_NUMERIC
604 if (! (curnum = setlocale(LC_NUMERIC, "")))
605 setlocale_failure = TRUE;
606 #endif /* USE_LOCALE_NUMERIC */
610 #endif /* !LOCALE_ENVIRON_REQUIRED */
612 if (setlocale_failure) {
614 bool locwarn = (printwarn > 1 ||
616 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
621 PerlIO_printf(PerlIO_stderr(),
622 "perl: warning: Setting locale failed.\n");
626 PerlIO_printf(PerlIO_stderr(),
627 "perl: warning: Setting locale failed for the categories:\n\t");
628 #ifdef USE_LOCALE_CTYPE
630 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
631 #endif /* USE_LOCALE_CTYPE */
632 #ifdef USE_LOCALE_COLLATE
634 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
635 #endif /* USE_LOCALE_COLLATE */
636 #ifdef USE_LOCALE_NUMERIC
638 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
639 #endif /* USE_LOCALE_NUMERIC */
640 PerlIO_printf(PerlIO_stderr(), "\n");
644 PerlIO_printf(PerlIO_stderr(),
645 "perl: warning: Please check that your locale settings:\n");
647 PerlIO_printf(PerlIO_stderr(),
648 "\tLC_ALL = %c%s%c,\n",
650 lc_all ? lc_all : "unset",
655 for (e = environ; *e; e++) {
656 if (strnEQ(*e, "LC_", 3)
657 && strnNE(*e, "LC_ALL=", 7)
658 && (p = strchr(*e, '=')))
659 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
660 (int)(p - *e), *e, p + 1);
664 PerlIO_printf(PerlIO_stderr(),
667 lang ? lang : "unset",
670 PerlIO_printf(PerlIO_stderr(),
671 " are supported and installed on your system.\n");
676 if (setlocale(LC_ALL, "C")) {
678 PerlIO_printf(PerlIO_stderr(),
679 "perl: warning: Falling back to the standard locale (\"C\").\n");
684 PerlIO_printf(PerlIO_stderr(),
685 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
692 #ifdef USE_LOCALE_CTYPE
693 || !(curctype || setlocale(LC_CTYPE, "C"))
694 #endif /* USE_LOCALE_CTYPE */
695 #ifdef USE_LOCALE_COLLATE
696 || !(curcoll || setlocale(LC_COLLATE, "C"))
697 #endif /* USE_LOCALE_COLLATE */
698 #ifdef USE_LOCALE_NUMERIC
699 || !(curnum || setlocale(LC_NUMERIC, "C"))
700 #endif /* USE_LOCALE_NUMERIC */
704 PerlIO_printf(PerlIO_stderr(),
705 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
709 #endif /* ! LC_ALL */
711 #ifdef USE_LOCALE_CTYPE
712 curctype = setlocale(LC_CTYPE, Nullch);
713 #endif /* USE_LOCALE_CTYPE */
714 #ifdef USE_LOCALE_COLLATE
715 curcoll = setlocale(LC_COLLATE, Nullch);
716 #endif /* USE_LOCALE_COLLATE */
717 #ifdef USE_LOCALE_NUMERIC
718 curnum = setlocale(LC_NUMERIC, Nullch);
719 #endif /* USE_LOCALE_NUMERIC */
722 #ifdef USE_LOCALE_CTYPE
723 perl_new_ctype(curctype);
724 #endif /* USE_LOCALE_CTYPE */
726 #ifdef USE_LOCALE_COLLATE
727 perl_new_collate(curcoll);
728 #endif /* USE_LOCALE_COLLATE */
730 #ifdef USE_LOCALE_NUMERIC
731 perl_new_numeric(curnum);
732 #endif /* USE_LOCALE_NUMERIC */
734 #endif /* USE_LOCALE */
739 /* Backwards compatibility. */
741 perl_init_i18nl14n(int printwarn)
743 return perl_init_i18nl10n(printwarn);
746 #ifdef USE_LOCALE_COLLATE
749 * mem_collxfrm() is a bit like strxfrm() but with two important
750 * differences. First, it handles embedded NULs. Second, it allocates
751 * a bit more memory than needed for the transformed data itself.
752 * The real transformed data begins at offset sizeof(collationix).
753 * Please see sv_collxfrm() to see how this is used.
756 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
759 STRLEN xalloc, xin, xout;
761 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
762 /* the +1 is for the terminating NUL. */
764 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
765 New(171, xbuf, xalloc, char);
769 *(U32*)xbuf = collation_ix;
770 xout = sizeof(collation_ix);
771 for (xin = 0; xin < len; ) {
775 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
778 if (xused < xalloc - xout)
780 xalloc = (2 * xalloc) + 1;
781 Renew(xbuf, xalloc, char);
786 xin += strlen(s + xin) + 1;
789 /* Embedded NULs are understood but silently skipped
790 * because they make no sense in locale collation. */
794 *xlen = xout - sizeof(collation_ix);
803 #endif /* USE_LOCALE_COLLATE */
808 register unsigned char *s;
809 register unsigned char *table;
811 register U32 len = SvCUR(sv);
816 return; /* can't have offsets that big */
818 table = (unsigned char*)(SvPVX(sv) + len + 1);
820 for (i = 0; i < 256; i++) {
824 while (s >= (unsigned char*)(SvPVX(sv)))
826 if (table[*s] == len)
830 sv_upgrade(sv, SVt_PVBM);
831 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
834 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
835 for (i = 0; i < len; i++) {
836 if (freq[s[i]] < frequency) {
838 frequency = freq[s[i]];
841 BmRARE(sv) = s[rarest];
842 BmPREVIOUS(sv) = rarest;
843 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
847 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
849 register unsigned char *s;
851 register I32 littlelen;
852 register unsigned char *little;
853 register unsigned char *table;
854 register unsigned char *olds;
855 register unsigned char *oldlittle;
857 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
859 char *l = SvPV(littlestr,len);
862 return ninstr((char*)big,(char*)bigend, l, l + len);
865 littlelen = SvCUR(littlestr);
866 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
867 if (littlelen > bigend - big)
869 little = (unsigned char*)SvPVX(littlestr);
870 s = bigend - littlelen;
871 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
872 return (char*)s; /* how sweet it is */
873 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
876 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
881 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
882 if (--littlelen >= bigend - big)
885 oldlittle = little = table - 2;
889 if (tmp = table[*s]) {
891 if (bigend - s > tmp) {
896 if ((s += tmp) < bigend)
902 tmp = littlelen; /* less expensive than calling strncmp() */
905 if (*--s == *--little)
907 s = olds + 1; /* here we pay the price for failure */
909 if (s < bigend) /* fake up continue to outer loop */
920 screaminstr(SV *bigstr, SV *littlestr)
922 register unsigned char *s, *x;
923 register unsigned char *big;
925 register I32 previous;
927 register unsigned char *little;
928 register unsigned char *bigend;
929 register unsigned char *littleend;
931 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
933 little = (unsigned char *)(SvPVX(littlestr));
934 littleend = little + SvCUR(littlestr);
936 previous = BmPREVIOUS(littlestr);
937 big = (unsigned char *)(SvPVX(bigstr));
938 bigend = big + SvCUR(bigstr);
939 while (pos < previous) {
940 if (!(pos += screamnext[pos]))
945 if (big[pos-previous] != first)
947 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
956 return (char *)(big+pos-previous);
957 } while ( pos += screamnext[pos] );
958 #else /* !POINTERRIGOR */
961 if (big[pos] != first)
963 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
972 return (char *)(big+pos);
973 } while ( pos += screamnext[pos] );
974 #endif /* POINTERRIGOR */
979 ibcmp(char *s1, char *s2, register I32 len)
981 register U8 *a = (U8 *)s1;
982 register U8 *b = (U8 *)s2;
984 if (*a != *b && *a != fold[*b])
992 ibcmp_locale(char *s1, char *s2, register I32 len)
994 register U8 *a = (U8 *)s1;
995 register U8 *b = (U8 *)s2;
997 if (*a != *b && *a != fold_locale[*b])
1004 /* copy a string to a safe spot */
1009 register char *newaddr;
1011 New(902,newaddr,strlen(sv)+1,char);
1012 (void)strcpy(newaddr,sv);
1016 /* same thing but with a known length */
1019 savepvn(char *sv, register I32 len)
1021 register char *newaddr;
1023 New(903,newaddr,len+1,char);
1024 Copy(sv,newaddr,len,char); /* might not be null terminated */
1025 newaddr[len] = '\0'; /* is now */
1029 /* the SV for form() and mess() is not kept in an arena */
1037 /* Create as PVMG now, to avoid any upgrading later */
1038 New(905, sv, 1, SV);
1039 Newz(905, any, 1, XPVMG);
1040 SvFLAGS(sv) = SVt_PVMG;
1041 SvANY(sv) = (void*)any;
1042 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1048 form(const char* pat, ...)
1059 va_start(args, pat);
1064 mess_sv = mess_alloc();
1065 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1067 return SvPVX(mess_sv);
1071 mess(const char *pat, va_list *args)
1074 static char dgd[] = " during global destruction.\n";
1077 mess_sv = mess_alloc();
1079 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1080 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1085 if (curcop->cop_line)
1086 sv_catpvf(sv, " at %_ line %ld",
1087 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1088 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1089 bool line_mode = (RsSIMPLE(rs) &&
1090 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1091 sv_catpvf(sv, ", <%s> %s %ld",
1092 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1093 line_mode ? "line" : "chunk",
1094 (long)IoLINES(GvIOp(last_in_gv)));
1096 sv_catpv(sv, ".\n");
1104 die(const char* pat, ...)
1116 I32 oldrunlevel = runlevel;
1117 int was_in_eval = in_eval;
1122 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: curstack = %p, mainstack= %p\n",
1123 curstack, mainstack));/*debug*/
1124 /* We have to switch back to mainstack or die_where may try to pop
1125 * the eval block from the wrong stack if die is being called from a
1126 * signal handler. - dkindred@cs.cmu.edu */
1127 if (curstack != mainstack) {
1129 SWITCHSTACK(curstack, mainstack);
1133 va_start(args, pat);
1137 message = mess(pat, &args);
1140 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "die: message = %s\ndiehook = %p\n",
1141 message, diehook));/*debug*/
1143 /* sv_2cv might call croak() */
1144 SV *olddiehook = diehook;
1148 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1150 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1155 msg = newSVpv(message, 0);
1162 perl_call_sv((SV*)cv, G_DISCARD);
1168 restartop = die_where(message);
1169 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1170 "die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
1171 restartop, was_in_eval, oldrunlevel));/*debug*/
1172 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1179 croak(const char* pat, ...)
1183 croak(pat, va_alist)
1196 va_start(args, pat);
1200 message = mess(pat, &args);
1203 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1204 #endif /* USE_THREADS */
1206 /* sv_2cv might call croak() */
1207 SV *olddiehook = diehook;
1211 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1213 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1218 msg = newSVpv(message, 0);
1225 perl_call_sv((SV*)cv, G_DISCARD);
1231 restartop = die_where(message);
1234 PerlIO_puts(PerlIO_stderr(),message);
1235 (void)PerlIO_flush(PerlIO_stderr());
1241 warn(const char* pat,...)
1256 va_start(args, pat);
1260 message = mess(pat, &args);
1264 /* sv_2cv might call warn() */
1266 SV *oldwarnhook = warnhook;
1270 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1272 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1277 msg = newSVpv(message, 0);
1284 perl_call_sv((SV*)cv, G_DISCARD);
1290 PerlIO_puts(PerlIO_stderr(),message);
1294 (void)PerlIO_flush(PerlIO_stderr());
1297 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1300 my_setenv(char *nam, char *val)
1302 register I32 i=setenv_getix(nam); /* where does it go? */
1304 if (environ == origenviron) { /* need we copy environment? */
1310 for (max = i; environ[max]; max++) ;
1311 New(901,tmpenv, max+2, char*);
1312 for (j=0; j<max; j++) /* copy environment */
1313 tmpenv[j] = savepv(environ[j]);
1314 tmpenv[max] = Nullch;
1315 environ = tmpenv; /* tell exec where it is now */
1318 Safefree(environ[i]);
1319 while (environ[i]) {
1320 environ[i] = environ[i+1];
1325 if (!environ[i]) { /* does not exist yet */
1326 Renew(environ, i+2, char*); /* just expand it a bit */
1327 environ[i+1] = Nullch; /* make sure it's null terminated */
1330 Safefree(environ[i]);
1331 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1333 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1335 /* MS-DOS requires environment variable names to be in uppercase */
1336 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1337 * some utilities and applications may break because they only look
1338 * for upper case strings. (Fixed strupr() bug here.)]
1340 strcpy(environ[i],nam); strupr(environ[i]);
1341 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1345 #else /* if WIN32 */
1352 #ifdef USE_WIN32_RTL_ENV
1354 register char *envstr;
1355 STRLEN namlen = strlen(nam);
1357 char *oldstr = environ[setenv_getix(nam)];
1359 /* putenv() has totally broken semantics in both the Borland
1360 * and Microsoft CRTLs. They either store the passed pointer in
1361 * the environment without making a copy, or make a copy and don't
1362 * free it. And on top of that, they dont free() old entries that
1363 * are being replaced/deleted. This means the caller must
1364 * free any old entries somehow, or we end up with a memory
1365 * leak every time my_setenv() is called. One might think
1366 * one could directly manipulate environ[], like the UNIX code
1367 * above, but direct changes to environ are not allowed when
1368 * calling putenv(), since the RTLs maintain an internal
1369 * *copy* of environ[]. Bad, bad, *bad* stink.
1380 vallen = strlen(val);
1381 New(904, envstr, namlen + vallen + 3, char);
1382 (void)sprintf(envstr,"%s=%s",nam,val);
1383 (void)putenv(envstr);
1387 Safefree(envstr); /* MSVCRT leaks without this */
1390 #else /* !USE_WIN32_RTL_ENV */
1392 /* The sane way to deal with the environment.
1393 * Has these advantages over putenv() & co.:
1394 * * enables us to store a truly empty value in the
1395 * environment (like in UNIX).
1396 * * we don't have to deal with RTL globals, bugs and leaks.
1398 * Why you may want to enable USE_WIN32_RTL_ENV:
1399 * * environ[] and RTL functions will not reflect changes,
1400 * which might be an issue if extensions want to access
1401 * the env. via RTL. This cuts both ways, since RTL will
1402 * not see changes made by extensions that call the Win32
1403 * functions directly, either.
1406 SetEnvironmentVariable(nam,val);
1414 setenv_getix(char *nam)
1416 register I32 i, len = strlen(nam);
1418 for (i = 0; environ[i]; i++) {
1421 strnicmp(environ[i],nam,len) == 0
1423 strnEQ(environ[i],nam,len)
1425 && environ[i][len] == '=')
1426 break; /* strnEQ must come first to avoid */
1427 } /* potential SEGV's */
1433 #ifdef UNLINK_ALL_VERSIONS
1435 unlnk(f) /* unlink all versions of a file */
1440 for (i = 0; unlink(f) >= 0; i++) ;
1445 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1447 my_bcopy(from,to,len)
1448 register char *from;
1454 if (from - to >= 0) {
1462 *(--to) = *(--from);
1470 my_memset(loc,ch,len)
1483 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1497 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1499 my_memcmp(s1,s2,len)
1504 register U8 *a = (U8 *)s1;
1505 register U8 *b = (U8 *)s2;
1509 if (tmp = *a++ - *b++)
1514 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1516 #if defined(I_STDARG) || defined(I_VARARGS)
1519 #ifdef USE_CHAR_VSPRINTF
1524 vsprintf(dest, pat, args)
1531 fakebuf._ptr = dest;
1532 fakebuf._cnt = 32767;
1536 fakebuf._flag = _IOWRT|_IOSTRG;
1537 _doprnt(pat, args, &fakebuf); /* what a kludge */
1538 (void)putc('\0', &fakebuf);
1539 #ifdef USE_CHAR_VSPRINTF
1542 return 0; /* perl doesn't use return value */
1546 #endif /* HAS_VPRINTF */
1547 #endif /* I_VARARGS || I_STDARGS */
1550 #if BYTEORDER != 0x4321
1552 #ifndef CAN_PROTOTYPE
1559 #if (BYTEORDER & 1) == 0
1562 result = ((s & 255) << 8) + ((s >> 8) & 255);
1570 #ifndef CAN_PROTOTYPE
1579 char c[sizeof(long)];
1582 #if BYTEORDER == 0x1234
1583 u.c[0] = (l >> 24) & 255;
1584 u.c[1] = (l >> 16) & 255;
1585 u.c[2] = (l >> 8) & 255;
1589 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1590 croak("Unknown BYTEORDER\n");
1595 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1596 u.c[o & 0xf] = (l >> s) & 255;
1604 #ifndef CAN_PROTOTYPE
1613 char c[sizeof(long)];
1616 #if BYTEORDER == 0x1234
1617 u.c[0] = (l >> 24) & 255;
1618 u.c[1] = (l >> 16) & 255;
1619 u.c[2] = (l >> 8) & 255;
1623 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1624 croak("Unknown BYTEORDER\n");
1631 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1632 l |= (u.c[o & 0xf] & 255) << s;
1639 #endif /* BYTEORDER != 0x4321 */
1643 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1644 * If these functions are defined,
1645 * the BYTEORDER is neither 0x1234 nor 0x4321.
1646 * However, this is not assumed.
1650 #define HTOV(name,type) \
1657 char c[sizeof(type)]; \
1661 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1662 u.c[i] = (n >> s) & 0xFF; \
1667 #define VTOH(name,type) \
1674 char c[sizeof(type)]; \
1680 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1681 n += (u.c[i] & 0xFF) << s; \
1686 #if defined(HAS_HTOVS) && !defined(htovs)
1689 #if defined(HAS_HTOVL) && !defined(htovl)
1692 #if defined(HAS_VTOHS) && !defined(vtohs)
1695 #if defined(HAS_VTOHL) && !defined(vtohl)
1699 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1700 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1702 my_popen(char *cmd, char *mode)
1705 register I32 This, that;
1708 I32 doexec = strNE(cmd,"-");
1712 return my_syspopen(cmd,mode);
1717 This = (*mode == 'w');
1719 if (doexec && tainting) {
1721 taint_proper("Insecure %s%s", "EXEC");
1723 while ((pid = (doexec?vfork():fork())) < 0) {
1724 if (errno != EAGAIN) {
1727 croak("Can't fork");
1738 if (p[THIS] != (*mode == 'r')) {
1739 dup2(p[THIS], *mode == 'r');
1743 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1749 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1752 do_exec(cmd); /* may or may not use the shell */
1756 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1757 sv_setiv(GvSV(tmpgv), (IV)getpid());
1759 hv_clear(pidstatus); /* we have no children */
1764 do_execfree(); /* free any memory malloced by child on vfork */
1766 if (p[that] < p[This]) {
1767 dup2(p[This], p[that]);
1771 sv = *av_fetch(fdpid,p[This],TRUE);
1772 (void)SvUPGRADE(sv,SVt_IV);
1775 return PerlIO_fdopen(p[This], mode);
1778 #if defined(atarist) || defined(DJGPP)
1785 /* Needs work for PerlIO ! */
1786 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1787 return popen(PerlIO_exportFILE(cmd, 0), mode);
1791 #endif /* !DOSISH */
1798 struct stat tmpstatbuf;
1800 PerlIO_printf(PerlIO_stderr(),"%s", s);
1801 for (fd = 0; fd < 32; fd++) {
1802 if (Fstat(fd,&tmpstatbuf) >= 0)
1803 PerlIO_printf(PerlIO_stderr()," %d",fd);
1805 PerlIO_printf(PerlIO_stderr(),"\n");
1815 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1819 return fcntl(oldfd, F_DUPFD, newfd);
1821 #define DUP2_MAX_FDS 256
1822 int fdtmp[DUP2_MAX_FDS];
1829 /* good enough for low fd's... */
1830 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1831 if (fdx >= DUP2_MAX_FDS) {
1839 close(fdtmp[--fdx]);
1846 #ifdef HAS_SIGACTION
1849 rsignal(int signo, Sighandler_t handler)
1851 struct sigaction act, oact;
1853 act.sa_handler = handler;
1854 sigemptyset(&act.sa_mask);
1857 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1859 if (sigaction(signo, &act, &oact) == -1)
1862 return oact.sa_handler;
1866 rsignal_state(int signo)
1868 struct sigaction oact;
1870 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1873 return oact.sa_handler;
1877 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
1879 struct sigaction act;
1881 act.sa_handler = handler;
1882 sigemptyset(&act.sa_mask);
1885 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1887 return sigaction(signo, &act, save);
1891 rsignal_restore(int signo, Sigsave_t *save)
1893 return sigaction(signo, save, (struct sigaction *)NULL);
1896 #else /* !HAS_SIGACTION */
1899 rsignal(signo, handler)
1901 Sighandler_t handler;
1903 return signal(signo, handler);
1906 static int sig_trapped;
1917 rsignal_state(signo)
1920 Sighandler_t oldsig;
1923 oldsig = signal(signo, sig_trap);
1924 signal(signo, oldsig);
1926 kill(getpid(), signo);
1931 rsignal_save(signo, handler, save)
1933 Sighandler_t handler;
1936 *save = signal(signo, handler);
1937 return (*save == SIG_ERR) ? -1 : 0;
1941 rsignal_restore(signo, save)
1945 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1948 #endif /* !HAS_SIGACTION */
1950 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
1951 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1953 my_pclose(FILE *ptr)
1955 Sigsave_t hstat, istat, qstat;
1962 int saved_vaxc_errno;
1965 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1966 pid = (int)SvIVX(*svp);
1970 if (pid == -1) { /* Opened by popen. */
1971 return my_syspclose(ptr);
1974 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
1975 saved_errno = errno;
1977 saved_vaxc_errno = vaxc$errno;
1981 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1983 rsignal_save(SIGHUP, SIG_IGN, &hstat);
1984 rsignal_save(SIGINT, SIG_IGN, &istat);
1985 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
1987 pid = wait4pid(pid, &status, 0);
1988 } while (pid == -1 && errno == EINTR);
1989 rsignal_restore(SIGHUP, &hstat);
1990 rsignal_restore(SIGINT, &istat);
1991 rsignal_restore(SIGQUIT, &qstat);
1993 SETERRNO(saved_errno, saved_vaxc_errno);
1996 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
1998 #endif /* !DOSISH */
2000 #if !defined(DOSISH) || defined(OS2)
2002 wait4pid(int pid, int *statusp, int flags)
2006 char spid[TYPE_CHARS(int)];
2011 sprintf(spid, "%d", pid);
2012 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2013 if (svp && *svp != &sv_undef) {
2014 *statusp = SvIVX(*svp);
2015 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2022 hv_iterinit(pidstatus);
2023 if (entry = hv_iternext(pidstatus)) {
2024 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2025 sv = hv_iterval(pidstatus,entry);
2026 *statusp = SvIVX(sv);
2027 sprintf(spid, "%d", pid);
2028 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2033 # ifdef HAS_WAITPID_RUNTIME
2034 if (!HAS_WAITPID_RUNTIME)
2037 return waitpid(pid,statusp,flags);
2039 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2040 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2042 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2047 croak("Can't do waitpid with flags");
2049 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2050 pidgone(result,*statusp);
2058 #endif /* !DOSISH */
2062 pidgone(int pid, int status)
2065 char spid[TYPE_CHARS(int)];
2067 sprintf(spid, "%d", pid);
2068 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2069 (void)SvUPGRADE(sv,SVt_IV);
2074 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2077 int /* Cannot prototype with I32
2086 /* Needs work for PerlIO ! */
2087 FILE *f = PerlIO_findFILE(ptr);
2088 I32 result = pclose(f);
2089 PerlIO_releaseFILE(ptr,f);
2095 repeatcpy(register char *to, register char *from, I32 len, register I32 count)
2098 register char *frombase = from;
2106 while (count-- > 0) {
2107 for (todo = len; todo > 0; todo--) {
2114 #ifndef CASTNEGFLOAT
2122 # define BIGDOUBLE 2147483648.0
2124 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2127 return (unsigned long)f;
2129 return (unsigned long)along;
2136 /* Unfortunately, on some systems the cast_uv() function doesn't
2137 work with the system-supplied definition of ULONG_MAX. The
2138 comparison (f >= ULONG_MAX) always comes out true. It must be a
2139 problem with the compiler constant folding.
2141 In any case, this workaround should be fine on any two's complement
2142 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2144 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2147 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2149 -- Kenneth Albanowski <kjahds@kjahds.com>
2153 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2161 return (I32) I32_MAX;
2163 return (I32) I32_MIN;
2183 return (UV) MY_UV_MAX;
2195 char *fa = strrchr(a,'/');
2196 char *fb = strrchr(b,'/');
2197 struct stat tmpstatbuf1;
2198 struct stat tmpstatbuf2;
2199 SV *tmpsv = sv_newmortal();
2212 sv_setpv(tmpsv, ".");
2214 sv_setpvn(tmpsv, a, fa - a);
2215 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2218 sv_setpv(tmpsv, ".");
2220 sv_setpvn(tmpsv, b, fb - b);
2221 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2223 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2224 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2226 #endif /* !HAS_RENAME */
2229 scan_oct(char *start, I32 len, I32 *retlen)
2231 register char *s = start;
2232 register UV retval = 0;
2233 bool overflowed = FALSE;
2235 while (len && *s >= '0' && *s <= '7') {
2236 register UV n = retval << 3;
2237 if (!overflowed && (n >> 3) != retval) {
2238 warn("Integer overflow in octal number");
2241 retval = n | (*s++ - '0');
2244 if (dowarn && len && (*s == '8' || *s == '9'))
2245 warn("Illegal octal digit ignored");
2246 *retlen = s - start;
2251 scan_hex(char *start, I32 len, I32 *retlen)
2253 register char *s = start;
2254 register UV retval = 0;
2255 bool overflowed = FALSE;
2258 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2259 register UV n = retval << 4;
2260 if (!overflowed && (n >> 4) != retval) {
2261 warn("Integer overflow in hex number");
2264 retval = n | (tmp - hexdigit) & 15;
2267 *retlen = s - start;
2273 /* Very simplistic scheduler for now */
2277 thr = thr->i.next_run;
2288 perl_cond_signal(cp)
2292 perl_cond cond = *cp;
2297 /* Insert t in the runnable queue just ahead of us */
2298 t->i.next_run = thr->i.next_run;
2299 thr->i.next_run->i.prev_run = t;
2300 t->i.prev_run = thr;
2301 thr->i.next_run = t;
2302 thr->i.wait_queue = 0;
2303 /* Remove from the wait queue */
2309 perl_cond_broadcast(cp)
2313 perl_cond cond, cond_next;
2315 for (cond = *cp; cond; cond = cond_next) {
2317 /* Insert t in the runnable queue just ahead of us */
2318 t->i.next_run = thr->i.next_run;
2319 thr->i.next_run->i.prev_run = t;
2320 t->i.prev_run = thr;
2321 thr->i.next_run = t;
2322 thr->i.wait_queue = 0;
2323 /* Remove from the wait queue */
2324 cond_next = cond->next;
2336 if (thr->i.next_run == thr)
2337 croak("panic: perl_cond_wait called by last runnable thread");
2339 New(666, cond, 1, struct perl_wait_queue);
2343 thr->i.wait_queue = cond;
2344 /* Remove ourselves from runnable queue */
2345 thr->i.next_run->i.prev_run = thr->i.prev_run;
2346 thr->i.prev_run->i.next_run = thr->i.next_run;
2348 #endif /* FAKE_THREADS */
2350 #ifdef OLD_PTHREADS_API
2356 if (pthread_getspecific(thr_key, &t))
2357 croak("panic: pthread_getspecific");
2358 return (struct thread *) t;
2360 #endif /* OLD_PTHREADS_API */
2363 condpair_magic(SV *sv)
2367 SvUPGRADE(sv, SVt_PVMG);
2368 mg = mg_find(sv, 'm');
2372 New(53, cp, 1, condpair_t);
2373 MUTEX_INIT(&cp->mutex);
2374 COND_INIT(&cp->owner_cond);
2375 COND_INIT(&cp->cond);
2377 MUTEX_LOCK(&sv_mutex);
2378 mg = mg_find(sv, 'm');
2380 /* someone else beat us to initialising it */
2381 MUTEX_UNLOCK(&sv_mutex);
2382 MUTEX_DESTROY(&cp->mutex);
2383 COND_DESTROY(&cp->owner_cond);
2384 COND_DESTROY(&cp->cond);
2388 sv_magic(sv, Nullsv, 'm', 0, 0);
2390 mg->mg_ptr = (char *)cp;
2391 mg->mg_len = sizeof(cp);
2392 MUTEX_UNLOCK(&sv_mutex);
2393 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2394 "%p: condpair_magic %p\n", thr, sv));)
2399 #endif /* USE_THREADS */
2403 * This hack is to force load of "huge" support from libm.a
2404 * So it is in perl for (say) POSIX to use.
2405 * Needed for SunOS with Sun's 'acc' for example.