3 * Copyright (c) 1991-1994, 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
552 char *lc_all = getenv("LC_ALL");
554 #ifdef USE_LOCALE_CTYPE
555 char *lc_ctype = getenv("LC_CTYPE");
556 char *curctype = NULL;
557 #endif /* USE_LOCALE_CTYPE */
558 #ifdef USE_LOCALE_COLLATE
559 char *lc_collate = getenv("LC_COLLATE");
560 char *curcoll = NULL;
561 #endif /* USE_LOCALE_COLLATE */
562 #ifdef USE_LOCALE_NUMERIC
563 char *lc_numeric = getenv("LC_NUMERIC");
565 #endif /* USE_LOCALE_NUMERIC */
566 char *lang = getenv("LANG");
567 bool setlocale_failure = FALSE;
571 if (! setlocale(LC_ALL, ""))
572 setlocale_failure = TRUE;
574 #ifdef USE_LOCALE_CTYPE
575 curctype = setlocale(LC_CTYPE, Nullch);
576 #endif /* USE_LOCALE_CTYPE */
577 #ifdef USE_LOCALE_COLLATE
578 curcoll = setlocale(LC_COLLATE, Nullch);
579 #endif /* USE_LOCALE_COLLATE */
580 #ifdef USE_LOCALE_NUMERIC
581 curnum = setlocale(LC_NUMERIC, Nullch);
582 #endif /* USE_LOCALE_NUMERIC */
587 #ifdef USE_LOCALE_CTYPE
588 if (! (curctype = setlocale(LC_CTYPE, "")))
589 setlocale_failure = TRUE;
590 #endif /* USE_LOCALE_CTYPE */
591 #ifdef USE_LOCALE_COLLATE
592 if (! (curcoll = setlocale(LC_COLLATE, "")))
593 setlocale_failure = TRUE;
594 #endif /* USE_LOCALE_COLLATE */
595 #ifdef USE_LOCALE_NUMERIC
596 if (! (curnum = setlocale(LC_NUMERIC, "")))
597 setlocale_failure = TRUE;
598 #endif /* USE_LOCALE_NUMERIC */
602 if (setlocale_failure) {
604 bool locwarn = (printwarn > 1 ||
606 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
611 PerlIO_printf(PerlIO_stderr(),
612 "perl: warning: Setting locale failed.\n");
616 PerlIO_printf(PerlIO_stderr(),
617 "perl: warning: Setting locale failed for the categories:\n\t");
618 #ifdef USE_LOCALE_CTYPE
620 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
621 #endif /* USE_LOCALE_CTYPE */
622 #ifdef USE_LOCALE_COLLATE
624 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
625 #endif /* USE_LOCALE_COLLATE */
626 #ifdef USE_LOCALE_NUMERIC
628 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
629 #endif /* USE_LOCALE_NUMERIC */
630 PerlIO_printf(PerlIO_stderr(), "\n");
634 PerlIO_printf(PerlIO_stderr(),
635 "perl: warning: Please check that your locale settings:\n");
638 PerlIO_printf(PerlIO_stderr(),
639 "\tLC_ALL = %c%s%c,\n",
641 lc_all ? lc_all : "unset",
647 for (e = environ; *e; e++) {
648 if (strnEQ(*e, "LC_", 3)
649 && strnNE(*e, "LC_ALL=", 7)
650 && (p = strchr(*e, '=')))
651 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
652 (p - *e), *e, p + 1);
656 PerlIO_printf(PerlIO_stderr(),
659 lang ? lang : "unset",
662 PerlIO_printf(PerlIO_stderr(),
663 " are supported and installed on your system.\n");
668 if (setlocale(LC_ALL, "C")) {
670 PerlIO_printf(PerlIO_stderr(),
671 "perl: warning: Falling back to the standard locale (\"C\").\n");
676 PerlIO_printf(PerlIO_stderr(),
677 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
684 #ifdef USE_LOCALE_CTYPE
685 || !(curctype || setlocale(LC_CTYPE, "C"))
686 #endif /* USE_LOCALE_CTYPE */
687 #ifdef USE_LOCALE_COLLATE
688 || !(curcoll || setlocale(LC_COLLATE, "C"))
689 #endif /* USE_LOCALE_COLLATE */
690 #ifdef USE_LOCALE_NUMERIC
691 || !(curnum || setlocale(LC_NUMERIC, "C"))
692 #endif /* USE_LOCALE_NUMERIC */
696 PerlIO_printf(PerlIO_stderr(),
697 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
701 #endif /* ! LC_ALL */
703 #ifdef USE_LOCALE_CTYPE
704 curctype = setlocale(LC_CTYPE, Nullch);
705 #endif /* USE_LOCALE_CTYPE */
706 #ifdef USE_LOCALE_COLLATE
707 curcoll = setlocale(LC_COLLATE, Nullch);
708 #endif /* USE_LOCALE_COLLATE */
709 #ifdef USE_LOCALE_NUMERIC
710 curnum = setlocale(LC_NUMERIC, Nullch);
711 #endif /* USE_LOCALE_NUMERIC */
714 #ifdef USE_LOCALE_CTYPE
715 perl_new_ctype(curctype);
716 #endif /* USE_LOCALE_CTYPE */
718 #ifdef USE_LOCALE_COLLATE
719 perl_new_collate(curcoll);
720 #endif /* USE_LOCALE_COLLATE */
722 #ifdef USE_LOCALE_NUMERIC
723 perl_new_numeric(curnum);
724 #endif /* USE_LOCALE_NUMERIC */
726 #endif /* USE_LOCALE */
731 /* Backwards compatibility. */
733 perl_init_i18nl14n(printwarn)
736 return perl_init_i18nl10n(printwarn);
739 #ifdef USE_LOCALE_COLLATE
742 * mem_collxfrm() is a bit like strxfrm() but with two important
743 * differences. First, it handles embedded NULs. Second, it allocates
744 * a bit more memory than needed for the transformed data itself.
745 * The real transformed data begins at offset sizeof(collationix).
746 * Please see sv_collxfrm() to see how this is used.
749 mem_collxfrm(s, len, xlen)
755 STRLEN xalloc, xin, xout;
757 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
758 /* the +1 is for the terminating NUL. */
760 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
761 New(171, xbuf, xalloc, char);
765 *(U32*)xbuf = collation_ix;
766 xout = sizeof(collation_ix);
767 for (xin = 0; xin < len; ) {
771 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
774 if (xused < xalloc - xout)
776 xalloc = (2 * xalloc) + 1;
777 Renew(xbuf, xalloc, char);
782 xin += strlen(s + xin) + 1;
785 /* Embedded NULs are understood but silently skipped
786 * because they make no sense in locale collation. */
790 *xlen = xout - sizeof(collation_ix);
799 #endif /* USE_LOCALE_COLLATE */
805 register unsigned char *s;
806 register unsigned char *table;
808 register U32 len = SvCUR(sv);
813 return; /* can't have offsets that big */
815 table = (unsigned char*)(SvPVX(sv) + len + 1);
817 for (i = 0; i < 256; i++) {
821 while (s >= (unsigned char*)(SvPVX(sv)))
823 if (table[*s] == len)
827 sv_upgrade(sv, SVt_PVBM);
828 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
831 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
832 for (i = 0; i < len; i++) {
833 if (freq[s[i]] < frequency) {
835 frequency = freq[s[i]];
838 BmRARE(sv) = s[rarest];
839 BmPREVIOUS(sv) = rarest;
840 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
844 fbm_instr(big, bigend, littlestr)
846 register unsigned char *bigend;
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(bigstr, littlestr)
924 register unsigned char *s, *x;
925 register unsigned char *big;
927 register I32 previous;
929 register unsigned char *little;
930 register unsigned char *bigend;
931 register unsigned char *littleend;
933 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
935 little = (unsigned char *)(SvPVX(littlestr));
936 littleend = little + SvCUR(littlestr);
938 previous = BmPREVIOUS(littlestr);
939 big = (unsigned char *)(SvPVX(bigstr));
940 bigend = big + SvCUR(bigstr);
941 while (pos < previous) {
942 if (!(pos += screamnext[pos]))
947 if (big[pos-previous] != first)
949 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
958 return (char *)(big+pos-previous);
959 } while ( pos += screamnext[pos] );
960 #else /* !POINTERRIGOR */
963 if (big[pos] != first)
965 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
974 return (char *)(big+pos);
975 } while ( pos += screamnext[pos] );
976 #endif /* POINTERRIGOR */
985 register U8 *a = (U8 *)s1;
986 register U8 *b = (U8 *)s2;
988 if (*a != *b && *a != fold[*b])
996 ibcmp_locale(s1, s2, len)
1000 register U8 *a = (U8 *)s1;
1001 register U8 *b = (U8 *)s2;
1003 if (*a != *b && *a != fold_locale[*b])
1010 /* copy a string to a safe spot */
1016 register char *newaddr;
1018 New(902,newaddr,strlen(sv)+1,char);
1019 (void)strcpy(newaddr,sv);
1023 /* same thing but with a known length */
1030 register char *newaddr;
1032 New(903,newaddr,len+1,char);
1033 Copy(sv,newaddr,len,char); /* might not be null terminated */
1034 newaddr[len] = '\0'; /* is now */
1040 mess(const char *pat, va_list *args)
1054 #ifdef USE_CHAR_VSPRINTF
1062 usermess = strEQ(pat, "%s");
1064 tmpstr = sv_newmortal();
1065 sv_setpv(tmpstr, va_arg(*args, char *));
1066 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
1069 (void) vsprintf(s,pat,*args);
1074 if (!(s > s_start && s[-1] == '\n')) {
1076 strcpy(s, " during global destruction.\n");
1078 if (curcop->cop_line) {
1079 (void)sprintf(s," at %s line %ld",
1080 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
1083 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1084 bool line_mode = (RsSIMPLE(rs) &&
1085 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1086 (void)sprintf(s,", <%s> %s %ld",
1087 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1088 line_mode ? "line" : "chunk",
1089 (long)IoLINES(GvIOp(last_in_gv)));
1092 (void)strcpy(s,".\n");
1096 sv_catpv(tmpstr,buf+1);
1099 if (s - s_start >= sizeof(buf)) { /* Ooops! */
1101 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
1103 PerlIO_puts(PerlIO_stderr(), buf);
1104 PerlIO_puts(PerlIO_stderr(), "panic: message overflow - memory corrupted!\n");
1108 return SvPVX(tmpstr);
1115 die(const char* pat, ...)
1126 int oldrunlevel = runlevel;
1127 int was_in_eval = in_eval;
1132 /* We have to switch back to mainstack or die_where may try to pop
1133 * the eval block from the wrong stack if die is being called from a
1134 * signal handler. - dkindred@cs.cmu.edu */
1135 if (curstack != mainstack) {
1137 SWITCHSTACK(curstack, mainstack);
1141 va_start(args, pat);
1145 message = mess(pat, &args);
1148 if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
1150 SV *msg = sv_2mortal(newSVpv(message, 0));
1156 perl_call_sv((SV*)cv, G_DISCARD);
1158 /* It's okay for the __DIE__ hook to modify the message. */
1159 message = SvPV(msg, na);
1162 restartop = die_where(message);
1163 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1164 Siglongjmp(top_env, 3);
1170 croak(const char* pat, ...)
1174 croak(pat, va_alist)
1186 va_start(args, pat);
1190 message = mess(pat, &args);
1193 SV *olddiehook = diehook;
1194 diehook = Nullsv; /* sv_2cv might call croak() */
1195 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1196 diehook = olddiehook;
1197 if (cv && !CvDEPTH(cv)) {
1199 SV *msg = sv_2mortal(newSVpv(message, 0));
1205 perl_call_sv((SV*)cv, G_DISCARD);
1207 /* It's okay for the __DIE__ hook to modify the message. */
1208 message = SvPV(msg, na);
1212 restartop = die_where(message);
1213 Siglongjmp(top_env, 3);
1215 PerlIO_puts(PerlIO_stderr(),message);
1216 (void)PerlIO_flush(PerlIO_stderr());
1222 (void)UNLINK(e_tmpname);
1223 Safefree(e_tmpname);
1226 statusvalue = SHIFTSTATUS(statusvalue);
1228 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1230 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1236 warn(const char* pat,...)
1251 va_start(args, pat);
1255 message = mess(pat, &args);
1259 SV *oldwarnhook = warnhook;
1260 warnhook = Nullsv; /* sv_2cv might end up calling warn() */
1261 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1262 warnhook = oldwarnhook;
1263 if (cv && !CvDEPTH(cv)) {
1268 PUSHs(sv_2mortal(newSVpv(message,0)));
1270 perl_call_sv((SV*)cv, G_DISCARD);
1274 PerlIO_puts(PerlIO_stderr(),message);
1278 (void)PerlIO_flush(PerlIO_stderr());
1281 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1286 register I32 i=setenv_getix(nam); /* where does it go? */
1288 if (environ == origenviron) { /* need we copy environment? */
1294 for (max = i; environ[max]; max++) ;
1295 New(901,tmpenv, max+2, char*);
1296 for (j=0; j<max; j++) /* copy environment */
1297 tmpenv[j] = savepv(environ[j]);
1298 tmpenv[max] = Nullch;
1299 environ = tmpenv; /* tell exec where it is now */
1302 while (environ[i]) {
1303 environ[i] = environ[i+1];
1308 if (!environ[i]) { /* does not exist yet */
1309 Renew(environ, i+2, char*); /* just expand it a bit */
1310 environ[i+1] = Nullch; /* make sure it's null terminated */
1313 Safefree(environ[i]);
1314 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1316 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1318 /* MS-DOS requires environment variable names to be in uppercase */
1319 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1320 * some utilities and applications may break because they only look
1321 * for upper case strings. (Fixed strupr() bug here.)]
1323 strcpy(environ[i],nam); strupr(environ[i]);
1324 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1332 register I32 i, len = strlen(nam);
1334 for (i = 0; environ[i]; i++) {
1335 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1336 break; /* strnEQ must come first to avoid */
1337 } /* potential SEGV's */
1342 #ifdef UNLINK_ALL_VERSIONS
1344 unlnk(f) /* unlink all versions of a file */
1349 for (i = 0; unlink(f) >= 0; i++) ;
1354 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1356 my_bcopy(from,to,len)
1357 register char *from;
1363 if (from - to >= 0) {
1371 *(--to) = *(--from);
1377 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1391 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1393 my_memcmp(s1,s2,len)
1398 register U8 *a = (U8 *)s1;
1399 register U8 *b = (U8 *)s2;
1403 if (tmp = *a++ - *b++)
1408 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1410 #if defined(I_STDARG) || defined(I_VARARGS)
1413 #ifdef USE_CHAR_VSPRINTF
1418 vsprintf(dest, pat, args)
1425 fakebuf._ptr = dest;
1426 fakebuf._cnt = 32767;
1430 fakebuf._flag = _IOWRT|_IOSTRG;
1431 _doprnt(pat, args, &fakebuf); /* what a kludge */
1432 (void)putc('\0', &fakebuf);
1433 #ifdef USE_CHAR_VSPRINTF
1436 return 0; /* perl doesn't use return value */
1440 #endif /* HAS_VPRINTF */
1441 #endif /* I_VARARGS || I_STDARGS */
1444 #if BYTEORDER != 0x4321
1446 #ifndef CAN_PROTOTYPE
1453 #if (BYTEORDER & 1) == 0
1456 result = ((s & 255) << 8) + ((s >> 8) & 255);
1464 #ifndef CAN_PROTOTYPE
1473 char c[sizeof(long)];
1476 #if BYTEORDER == 0x1234
1477 u.c[0] = (l >> 24) & 255;
1478 u.c[1] = (l >> 16) & 255;
1479 u.c[2] = (l >> 8) & 255;
1483 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1484 croak("Unknown BYTEORDER\n");
1489 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1490 u.c[o & 0xf] = (l >> s) & 255;
1498 #ifndef CAN_PROTOTYPE
1507 char c[sizeof(long)];
1510 #if BYTEORDER == 0x1234
1511 u.c[0] = (l >> 24) & 255;
1512 u.c[1] = (l >> 16) & 255;
1513 u.c[2] = (l >> 8) & 255;
1517 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1518 croak("Unknown BYTEORDER\n");
1525 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1526 l |= (u.c[o & 0xf] & 255) << s;
1533 #endif /* BYTEORDER != 0x4321 */
1537 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1538 * If these functions are defined,
1539 * the BYTEORDER is neither 0x1234 nor 0x4321.
1540 * However, this is not assumed.
1544 #define HTOV(name,type) \
1551 char c[sizeof(type)]; \
1555 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1556 u.c[i] = (n >> s) & 0xFF; \
1561 #define VTOH(name,type) \
1568 char c[sizeof(type)]; \
1574 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1575 n += (u.c[i] & 0xFF) << s; \
1580 #if defined(HAS_HTOVS) && !defined(htovs)
1583 #if defined(HAS_HTOVL) && !defined(htovl)
1586 #if defined(HAS_VTOHS) && !defined(vtohs)
1589 #if defined(HAS_VTOHL) && !defined(vtohl)
1593 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1594 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1601 register I32 this, that;
1613 return my_syspopen(cmd,mode);
1618 this = (*mode == 'w');
1620 if (doexec && tainting) {
1622 taint_proper("Insecure %s%s", "EXEC");
1624 while ((pid = (doexec?vfork():fork())) < 0) {
1625 if (errno != EAGAIN) {
1628 croak("Can't fork");
1639 if (p[THIS] != (*mode == 'r')) {
1640 dup2(p[THIS], *mode == 'r');
1644 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1650 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1653 do_exec(cmd); /* may or may not use the shell */
1657 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1658 sv_setiv(GvSV(tmpgv),(I32)getpid());
1660 hv_clear(pidstatus); /* we have no children */
1665 do_execfree(); /* free any memory malloced by child on vfork */
1667 if (p[that] < p[this]) {
1668 dup2(p[this], p[that]);
1672 sv = *av_fetch(fdpid,p[this],TRUE);
1673 (void)SvUPGRADE(sv,SVt_IV);
1676 return PerlIO_fdopen(p[this], mode);
1679 #if defined(atarist) || defined(DJGPP)
1686 /* Needs work for PerlIO ! */
1687 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1688 return popen(PerlIO_exportFILE(cmd, 0), mode);
1692 #endif /* !DOSISH */
1699 struct stat tmpstatbuf;
1701 PerlIO_printf(PerlIO_stderr(),"%s", s);
1702 for (fd = 0; fd < 32; fd++) {
1703 if (Fstat(fd,&tmpstatbuf) >= 0)
1704 PerlIO_printf(PerlIO_stderr()," %d",fd);
1706 PerlIO_printf(PerlIO_stderr(),"\n");
1716 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1720 return fcntl(oldfd, F_DUPFD, newfd);
1729 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1732 close(fdtmp[--fdx]);
1739 #ifdef HAS_SIGACTION
1742 rsignal(signo, handler)
1744 Sighandler_t handler;
1746 struct sigaction act, oact;
1748 act.sa_handler = handler;
1749 sigemptyset(&act.sa_mask);
1752 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1754 if (sigaction(signo, &act, &oact) == -1)
1757 return oact.sa_handler;
1761 rsignal_state(signo)
1764 struct sigaction oact;
1766 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1769 return oact.sa_handler;
1773 rsignal_save(signo, handler, save)
1775 Sighandler_t handler;
1778 struct sigaction act;
1780 act.sa_handler = handler;
1781 sigemptyset(&act.sa_mask);
1784 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1786 return sigaction(signo, &act, save);
1790 rsignal_restore(signo, save)
1794 return sigaction(signo, save, (struct sigaction *)NULL);
1797 #else /* !HAS_SIGACTION */
1800 rsignal(signo, handler)
1802 Sighandler_t handler;
1804 return signal(signo, handler);
1807 static int sig_trapped;
1818 rsignal_state(signo)
1821 Sighandler_t oldsig;
1824 oldsig = signal(signo, sig_trap);
1825 signal(signo, oldsig);
1827 kill(getpid(), signo);
1832 rsignal_save(signo, handler, save)
1834 Sighandler_t handler;
1837 *save = signal(signo, handler);
1838 return (*save == SIG_ERR) ? -1 : 0;
1842 rsignal_restore(signo, save)
1846 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1849 #endif /* !HAS_SIGACTION */
1851 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
1852 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1857 Sigsave_t hstat, istat, qstat;
1862 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1863 pid = (int)SvIVX(*svp);
1867 if (pid == -1) { /* Opened by popen. */
1868 return my_syspclose(ptr);
1873 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1875 rsignal_save(SIGHUP, SIG_IGN, &hstat);
1876 rsignal_save(SIGINT, SIG_IGN, &istat);
1877 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
1879 pid = wait4pid(pid, &status, 0);
1880 } while (pid == -1 && errno == EINTR);
1881 rsignal_restore(SIGHUP, &hstat);
1882 rsignal_restore(SIGINT, &istat);
1883 rsignal_restore(SIGQUIT, &qstat);
1884 return(pid < 0 ? pid : status);
1886 #endif /* !DOSISH */
1888 #if !defined(DOSISH) || defined(OS2)
1890 wait4pid(pid,statusp,flags)
1902 sprintf(spid, "%d", pid);
1903 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1904 if (svp && *svp != &sv_undef) {
1905 *statusp = SvIVX(*svp);
1906 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1913 hv_iterinit(pidstatus);
1914 if (entry = hv_iternext(pidstatus)) {
1915 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1916 sv = hv_iterval(pidstatus,entry);
1917 *statusp = SvIVX(sv);
1918 sprintf(spid, "%d", pid);
1919 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1924 return waitpid(pid,statusp,flags);
1927 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1932 croak("Can't do waitpid with flags");
1934 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1935 pidgone(result,*statusp);
1944 #endif /* !DOSISH */
1955 sprintf(spid, "%d", pid);
1956 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1957 (void)SvUPGRADE(sv,SVt_IV);
1962 #if defined(atarist) || defined(OS2) || defined(DJGPP)
1965 int /* Cannot prototype with I32
1974 /* Needs work for PerlIO ! */
1975 FILE *f = PerlIO_findFILE(ptr);
1976 I32 result = pclose(f);
1977 PerlIO_releaseFILE(ptr,f);
1983 repeatcpy(to,from,len,count)
1985 register char *from;
1990 register char *frombase = from;
1998 while (count-- > 0) {
1999 for (todo = len; todo > 0; todo--) {
2006 #ifndef CASTNEGFLOAT
2014 # define BIGDOUBLE 2147483648.0
2016 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2019 return (unsigned long)f;
2021 return (unsigned long)along;
2028 /* Unfortunately, on some systems the cast_uv() function doesn't
2029 work with the system-supplied definition of ULONG_MAX. The
2030 comparison (f >= ULONG_MAX) always comes out true. It must be a
2031 problem with the compiler constant folding.
2033 In any case, this workaround should be fine on any two's complement
2034 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2036 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2039 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2041 -- Kenneth Albanowski <kjahds@kjahds.com>
2045 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2053 return (I32) I32_MAX;
2055 return (I32) I32_MIN;
2075 return (UV) MY_UV_MAX;
2087 char *fa = strrchr(a,'/');
2088 char *fb = strrchr(b,'/');
2089 struct stat tmpstatbuf1;
2090 struct stat tmpstatbuf2;
2092 #define MAXPATHLEN 1024
2094 char tmpbuf[MAXPATHLEN+1];
2109 strncpy(tmpbuf, a, fa - a);
2110 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
2115 strncpy(tmpbuf, b, fb - b);
2116 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
2118 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2119 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2121 #endif /* !HAS_RENAME */
2124 scan_oct(start, len, retlen)
2129 register char *s = start;
2130 register UV retval = 0;
2131 bool overflowed = FALSE;
2133 while (len && *s >= '0' && *s <= '7') {
2134 register UV n = retval << 3;
2135 if (!overflowed && (n >> 3) != retval) {
2136 warn("Integer overflow in octal number");
2139 retval = n | (*s++ - '0');
2142 if (dowarn && len && (*s == '8' || *s == '9'))
2143 warn("Illegal octal digit ignored");
2144 *retlen = s - start;
2149 scan_hex(start, len, retlen)
2154 register char *s = start;
2155 register UV retval = 0;
2156 bool overflowed = FALSE;
2159 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2160 register UV n = retval << 4;
2161 if (!overflowed && (n >> 4) != retval) {
2162 warn("Integer overflow in hex number");
2165 retval = n | (tmp - hexdigit) & 15;
2168 *retlen = s - start;
2175 * This hack is to force load of "huge" support from libm.a
2176 * So it is in perl for (say) POSIX to use.
2177 * Needed for SunOS with Sun's 'acc' for example.