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)
22 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
31 /* Put this after #includes because fork and vfork prototypes may
42 # include <sys/file.h>
48 static void xstat _((void));
53 /* paranoid version of malloc */
55 /* NOTE: Do not call the next three routines directly. Use the macros
56 * in handy.h, so that we can easily redefine everything to do tracking of
57 * allocated hunks back to the original New to track down any memory leaks.
58 * XXX This advice seems to be widely ignored :-( --AD August 1996.
68 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
71 #endif /* HAS_64K_LIMIT */
74 croak("panic: malloc");
76 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
77 #if !(defined(I286) || defined(atarist))
78 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
80 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
87 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
93 /* paranoid version of realloc */
96 saferealloc(where,size)
101 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
103 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
107 PerlIO_printf(PerlIO_stderr(), "Reallocation too large: %lx\n", size) FLUSH;
110 #endif /* HAS_64K_LIMIT */
112 croak("Null realloc");
115 croak("panic: realloc");
117 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
119 #if !(defined(I286) || defined(atarist))
121 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
122 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
126 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
127 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
136 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
142 /* safe version of free */
148 #if !(defined(I286) || defined(atarist))
149 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",where,an++));
151 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",where,an++));
159 /* safe version of calloc */
162 safecalloc(count, size)
169 if (size * count > 0xffff) {
170 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size * count) FLUSH;
173 #endif /* HAS_64K_LIMIT */
175 if ((long)size < 0 || (long)count < 0)
176 croak("panic: calloc");
178 #if !(defined(I286) || defined(atarist))
179 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
181 DEBUG_m(PerlIO_printf(PerlIO_stderr(), "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
184 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
186 memset((void*)ptr, 0, size);
192 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
198 #endif /* !MYMALLOC */
202 #define ALIGN sizeof(long)
209 register Malloc_t where;
211 where = safemalloc(size + ALIGN);
215 return where + ALIGN;
219 safexrealloc(where,size)
223 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
236 x = where[0] + 100 * where[1];
242 safexcalloc(x,count,size)
247 register Malloc_t where;
249 where = safexmalloc(x, size * count + ALIGN);
251 memset((void*)where + ALIGN, 0, size * count);
254 return where + ALIGN;
262 for (i = 0; i < MAXXCOUNT; i++) {
263 if (xcount[i] > lastxcount[i]) {
264 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
265 lastxcount[i] = xcount[i];
270 #endif /* LEAKTEST */
272 /* copy a string up to some (non-backslashed) delimiter, if any */
275 cpytill(to,from,fromend,delim,retlen)
278 register char *fromend;
284 for (; from < fromend; from++,to++) {
286 if (from[1] == delim)
288 else if (from[1] == '\\')
291 else if (*from == delim)
296 *retlen = to - origto;
300 /* return ptr to little string in big string, NULL if not found */
301 /* This routine was donated by Corey Satten. */
306 register char *little;
308 register char *s, *x;
319 for (x=big,s=little; *s; /**/ ) {
333 /* same as instr but allow embedded nulls */
336 ninstr(big, bigend, little, lend)
338 register char *bigend;
342 register char *s, *x;
343 register I32 first = *little;
344 register char *littleend = lend;
346 if (!first && little >= littleend)
348 if (bigend - big < littleend - little)
350 bigend -= littleend - little++;
351 while (big <= bigend) {
354 for (x=big,s=little; s < littleend; /**/ ) {
366 /* reverse of the above--find last substring */
369 rninstr(big, bigend, little, lend)
375 register char *bigbeg;
376 register char *s, *x;
377 register I32 first = *little;
378 register char *littleend = lend;
380 if (!first && little >= littleend)
383 big = bigend - (littleend - little++);
384 while (big >= bigbeg) {
387 for (x=big+2,s=little; s < littleend; /**/ ) {
402 * Set up for a new ctype locale.
405 perl_new_ctype(newctype)
410 for (i = 0; i < 256; i++) {
412 fold_locale[i] = toLOWER_LC(i);
413 else if (isLOWER_LC(i))
414 fold_locale[i] = toUPPER_LC(i);
420 #endif /* LC_CTYPE */
425 * Set up for a new collation locale.
428 perl_new_collate(newcoll)
432 if (collation_name) {
434 Safefree(collation_name);
435 collation_name = NULL;
436 collation_standard = TRUE;
440 #endif /* HAS_STRXFRM */
445 if (! collation_name || strNE(collation_name, newcoll)) {
447 Safefree(collation_name);
448 collation_name = savepv(newcoll);
449 collation_standard = strEQ(newcoll, "C");
453 /* 2: at most so many chars ('a', 'b'). */
454 /* 50: surely no system expands a char more. */
455 #define XFRMBUFSIZE (2 * 50)
456 char xbuf[XFRMBUFSIZE];
457 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
458 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
459 SSize_t mult = fb - fa;
461 croak("strxfrm() gets absurd");
462 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
463 collxfrm_mult = mult;
465 #endif /* HAS_STRXFRM */
469 #endif /* LC_COLLATE */
474 * Set up for a new numeric locale.
477 perl_new_numeric(newnum)
482 Safefree(numeric_name);
484 numeric_standard = TRUE;
485 numeric_local = TRUE;
490 if (! numeric_name || strNE(numeric_name, newnum)) {
491 Safefree(numeric_name);
492 numeric_name = savepv(newnum);
493 numeric_standard = strEQ(newnum, "C");
494 numeric_local = TRUE;
499 perl_numeric_standard()
501 if (! numeric_standard) {
502 setlocale(LC_NUMERIC, "C");
503 numeric_standard = TRUE;
504 numeric_local = FALSE;
511 if (! numeric_local) {
512 setlocale(LC_NUMERIC, numeric_name);
513 numeric_standard = FALSE;
514 numeric_local = TRUE;
518 #endif /* LC_NUMERIC */
520 /* Initialize locale awareness */
522 perl_init_i18nl10n(printwarn)
527 * 1 = set ok or not applicable,
528 * 0 = fallback to C locale,
529 * -1 = fallback to C locale failed
534 char *lc_all = getenv("LC_ALL");
535 char *lang = getenv("LANG");
537 char *lc_ctype = getenv("LC_CTYPE");
538 char *curctype = NULL;
539 #endif /* LC_CTYPE */
541 char *lc_collate = getenv("LC_COLLATE");
542 char *curcoll = NULL;
543 #endif /* LC_COLLATE */
545 char *lc_numeric = getenv("LC_NUMERIC");
547 #endif /* LC_NUMERIC */
548 bool setlocale_failure = FALSE;
553 if (! setlocale(LC_ALL, ""))
554 setlocale_failure = TRUE;
560 if (! (curctype = setlocale(LC_CTYPE, subloc)))
561 setlocale_failure = TRUE;
562 #endif /* LC_CTYPE */
564 if (! (curcoll = setlocale(LC_COLLATE, subloc)))
565 setlocale_failure = TRUE;
566 #endif /* LC_COLLATE */
568 if (! (curnum = setlocale(LC_NUMERIC, subloc)))
569 setlocale_failure = TRUE;
570 #endif /* LC_NUMERIC */
572 if (setlocale_failure && (lc_all || lang)) {
577 (!(perl_badlang = getenv("PERL_BADLANG")) || atoi(perl_badlang))) {
579 PerlIO_printf(PerlIO_stderr(),
580 "perl: warning: Setting locale failed for the categories:\n\t");
583 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
584 #endif /* LC_CTYPE */
587 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
588 #endif /* LC_COLLATE */
591 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
592 #endif /* LC_NUMERIC */
593 PerlIO_printf(PerlIO_stderr(), "\n");
595 PerlIO_printf(PerlIO_stderr(),
596 "perl: warning: Please check that your locale settings:\n");
598 PerlIO_printf(PerlIO_stderr(),
599 "\tLC_ALL = %c%s%c,\n",
601 lc_all ? lc_all : "unset",
605 PerlIO_printf(PerlIO_stderr(),
606 "\tLC_CTYPE = %c%s%c,\n",
607 lc_ctype ? '"' : '(',
608 lc_ctype ? lc_ctype : "unset",
609 lc_ctype ? '"' : ')');
610 #endif /* LC_CTYPE */
613 PerlIO_printf(PerlIO_stderr(),
614 "\tLC_COLLATE = %c%s%c,\n",
615 lc_collate ? '"' : '(',
616 lc_collate ? lc_collate : "unset",
617 lc_collate ? '"' : ')');
618 #endif /* LC_COLLATE */
621 PerlIO_printf(PerlIO_stderr(),
622 "\tLC_NUMERIC = %c%s%c,\n",
623 lc_numeric ? '"' : '(',
624 lc_numeric ? lc_numeric : "unset",
625 lc_numeric ? '"' : ')');
626 #endif /* LC_NUMERIC */
627 PerlIO_printf(PerlIO_stderr(),
630 lang ? lang : "unset",
633 PerlIO_printf(PerlIO_stderr(),
634 " are supported and installed on your system.\n");
640 if (setlocale_failure) {
641 PerlIO_printf(PerlIO_stderr(),
642 "perl: warning: Falling back to the \"C\" locale.\n");
643 if (setlocale(LC_ALL, "C")) {
646 #endif /* LC_CTYPE */
649 #endif /* LC_COLLATE */
652 #endif /* LC_NUMERIC */
655 PerlIO_printf(PerlIO_stderr(),
656 "perl: warning: Failed to fall back to the \"C\" locale.\n");
661 PerlIO_printf(PerlIO_stderr(),
662 "perl: warning: Cannot fall back to the \"C\" locale.\n");
663 #endif /* ! LC_ALL */
667 perl_new_ctype(curctype);
668 #endif /* LC_NUMERIC */
671 perl_new_collate(curcoll);
672 #endif /* LC_NUMERIC */
675 perl_new_numeric(curnum);
676 #endif /* LC_NUMERIC */
678 #endif /* #if defined(HAS_SETLOCALE) */
683 /* Backwards compatibility. */
685 perl_init_i18nl14n(printwarn)
688 perl_init_i18nl10n(printwarn);
694 * mem_collxfrm() is a bit like strxfrm() but with two important
695 * differences. First, it handles embedded NULs. Second, it allocates
696 * a bit more memory than needed for the transformed data itself.
697 * The real transformed data begins at offset sizeof(collationix).
698 * Please see sv_collxfrm() to see how this is used.
701 mem_collxfrm(s, len, xlen)
707 STRLEN xalloc, xin, xout;
709 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
710 /* the +1 is for the terminating NUL. */
712 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
713 New(171, xbuf, xalloc, char);
717 *(U32*)xbuf = collation_ix;
718 xout = sizeof(collation_ix);
719 for (xin = 0; xin < len; ) {
723 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
726 if (xused < xalloc - xout)
728 xalloc = (2 * xalloc) + 1;
729 Renew(xbuf, xalloc, char);
734 xin += strlen(s + xin) + 1;
737 /* Embedded NULs are understood but silently skipped
738 * because they make no sense in locale collation. */
742 *xlen = xout - sizeof(collation_ix);
751 #endif /* HAS_STRXFRM */
757 register unsigned char *s;
758 register unsigned char *table;
760 register U32 len = SvCUR(sv);
765 return; /* can't have offsets that big */
767 table = (unsigned char*)(SvPVX(sv) + len + 1);
769 for (i = 0; i < 256; i++) {
773 while (s >= (unsigned char*)(SvPVX(sv)))
775 if (table[*s] == len)
779 sv_upgrade(sv, SVt_PVBM);
780 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
783 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
784 for (i = 0; i < len; i++) {
785 if (freq[s[i]] < frequency) {
787 frequency = freq[s[i]];
790 BmRARE(sv) = s[rarest];
791 BmPREVIOUS(sv) = rarest;
792 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
796 fbm_instr(big, bigend, littlestr)
798 register unsigned char *bigend;
801 register unsigned char *s;
803 register I32 littlelen;
804 register unsigned char *little;
805 register unsigned char *table;
806 register unsigned char *olds;
807 register unsigned char *oldlittle;
809 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
811 char *l = SvPV(littlestr,len);
814 return ninstr((char*)big,(char*)bigend, l, l + len);
817 littlelen = SvCUR(littlestr);
818 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
819 if (littlelen > bigend - big)
821 little = (unsigned char*)SvPVX(littlestr);
822 s = bigend - littlelen;
823 if (*s == *little && memcmp((char*)s,(char*)little,littlelen)==0)
824 return (char*)s; /* how sweet it is */
825 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
828 if (*s == *little && memcmp((char*)s,(char*)little,littlelen)==0)
833 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
834 if (--littlelen >= bigend - big)
837 oldlittle = little = table - 2;
841 if (tmp = table[*s]) {
843 if (bigend - s > tmp) {
848 if ((s += tmp) < bigend)
854 tmp = littlelen; /* less expensive than calling strncmp() */
857 if (*--s == *--little)
859 s = olds + 1; /* here we pay the price for failure */
861 if (s < bigend) /* fake up continue to outer loop */
872 screaminstr(bigstr, littlestr)
876 register unsigned char *s, *x;
877 register unsigned char *big;
879 register I32 previous;
881 register unsigned char *little;
882 register unsigned char *bigend;
883 register unsigned char *littleend;
885 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
887 little = (unsigned char *)(SvPVX(littlestr));
888 littleend = little + SvCUR(littlestr);
890 previous = BmPREVIOUS(littlestr);
891 big = (unsigned char *)(SvPVX(bigstr));
892 bigend = big + SvCUR(bigstr);
893 while (pos < previous) {
894 if (!(pos += screamnext[pos]))
899 if (big[pos-previous] != first)
901 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
910 return (char *)(big+pos-previous);
911 } while ( pos += screamnext[pos] );
912 #else /* !POINTERRIGOR */
915 if (big[pos] != first)
917 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
926 return (char *)(big+pos);
927 } while ( pos += screamnext[pos] );
928 #endif /* POINTERRIGOR */
937 register U8 *a = (U8 *)s1;
938 register U8 *b = (U8 *)s2;
940 if (*a != *b && *a != fold[*b])
948 ibcmp_locale(s1, s2, len)
952 register U8 *a = (U8 *)s1;
953 register U8 *b = (U8 *)s2;
955 if (*a != *b && *a != fold_locale[*b])
962 /* copy a string to a safe spot */
968 register char *newaddr;
970 New(902,newaddr,strlen(sv)+1,char);
971 (void)strcpy(newaddr,sv);
975 /* same thing but with a known length */
982 register char *newaddr;
984 New(903,newaddr,len+1,char);
985 Copy(sv,newaddr,len,char); /* might not be null terminated */
986 newaddr[len] = '\0'; /* is now */
990 #if !defined(I_STDARG) && !defined(I_VARARGS)
993 * Fallback on the old hackers way of doing varargs
998 mess(pat,a1,a2,a3,a4)
1000 long a1, a2, a3, a4;
1004 I32 usermess = strEQ(pat,"%s");
1009 tmpstr = sv_newmortal();
1010 sv_setpv(tmpstr, (char*)a1);
1011 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
1014 (void)sprintf(s,pat,a1,a2,a3,a4);
1018 if (s[-1] != '\n') {
1020 strcpy(s, " during global destruction.\n");
1022 if (curcop->cop_line) {
1023 (void)sprintf(s," at %s line %ld",
1024 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
1027 if (GvIO(last_in_gv) &&
1028 IoLINES(GvIOp(last_in_gv)) ) {
1029 (void)sprintf(s,", <%s> %s %ld",
1030 last_in_gv == argvgv ? "" : GvENAME(last_in_gv),
1031 strEQ(rs,"\n") ? "line" : "chunk",
1032 (long)IoLINES(GvIOp(last_in_gv)));
1035 (void)strcpy(s,".\n");
1039 sv_catpv(tmpstr,buf+1);
1042 if (s - s_start >= sizeof(buf)) { /* Ooops! */
1044 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
1046 PerlIO_puts(PerlIO_stderr(), buf);
1047 PerlIO_puts(PerlIO_stderr(),"panic: message overflow - memory corrupted!\n");
1051 return SvPVX(tmpstr);
1057 void croak(pat,a1,a2,a3,a4)
1059 long a1, a2, a3, a4;
1067 message = mess(pat,a1,a2,a3,a4);
1069 SV *olddiehook = diehook;
1070 diehook = Nullsv; /* sv_2cv might call croak() */
1071 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1072 diehook = olddiehook;
1073 if (cv && !CvDEPTH(cv)) {
1078 PUSHs(sv_2mortal(newSVpv(message,0)));
1080 perl_call_sv((SV*)cv, G_DISCARD);
1084 restartop = die_where(message);
1085 Siglongjmp(top_env, 3);
1087 PerlIO_puts(PerlIO_stderr(),message);
1088 (void)PerlIO_flush(PerlIO_stderr());
1094 (void)UNLINK(e_tmpname);
1095 Safefree(e_tmpname);
1098 statusvalue = SHIFTSTATUS(statusvalue);
1100 my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
1102 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1107 void warn(pat,a1,a2,a3,a4)
1109 long a1, a2, a3, a4;
1117 message = mess(pat,a1,a2,a3,a4);
1119 SV *oldwarnhook = warnhook;
1120 warnhook = Nullsv; /* sv_2cv might end up calling warn() */
1121 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1122 warnhook = oldwarnhook;
1123 if (cv && !CvDEPTH(cv)) {
1128 PUSHs(sv_2mortal(newSVpv(message,0)));
1130 perl_call_sv((SV*)cv, G_DISCARD);
1134 PerlIO_puts(PerlIO_stderr(),message);
1138 (void)PerlIO_flush(PerlIO_stderr());
1141 #else /* !defined(I_STDARG) && !defined(I_VARARGS) */
1145 mess(char *pat, va_list *args)
1159 #ifdef USE_CHAR_VSPRINTF
1167 usermess = strEQ(pat, "%s");
1169 tmpstr = sv_newmortal();
1170 sv_setpv(tmpstr, va_arg(*args, char *));
1171 *s++ = SvPVX(tmpstr)[SvCUR(tmpstr)-1];
1174 (void) vsprintf(s,pat,*args);
1179 if (s[-1] != '\n') {
1181 strcpy(s, " during global destruction.\n");
1183 if (curcop->cop_line) {
1184 (void)sprintf(s," at %s line %ld",
1185 SvPVX(GvSV(curcop->cop_filegv)), (long)curcop->cop_line);
1188 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1189 bool line_mode = (RsSIMPLE(rs) &&
1190 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1191 (void)sprintf(s,", <%s> %s %ld",
1192 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1193 line_mode ? "line" : "chunk",
1194 (long)IoLINES(GvIOp(last_in_gv)));
1197 (void)strcpy(s,".\n");
1201 sv_catpv(tmpstr,buf+1);
1204 if (s - s_start >= sizeof(buf)) { /* Ooops! */
1206 PerlIO_puts(PerlIO_stderr(), SvPVX(tmpstr));
1208 PerlIO_puts(PerlIO_stderr(), buf);
1209 PerlIO_puts(PerlIO_stderr(), "panic: message overflow - memory corrupted!\n");
1213 return SvPVX(tmpstr);
1220 croak(char* pat, ...)
1224 croak(pat, va_alist)
1236 va_start(args, pat);
1240 message = mess(pat, &args);
1243 SV *olddiehook = diehook;
1244 diehook = Nullsv; /* sv_2cv might call croak() */
1245 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1246 diehook = olddiehook;
1247 if (cv && !CvDEPTH(cv)) {
1252 PUSHs(sv_2mortal(newSVpv(message,0)));
1254 perl_call_sv((SV*)cv, G_DISCARD);
1258 restartop = die_where(message);
1259 Siglongjmp(top_env, 3);
1261 PerlIO_puts(PerlIO_stderr(),message);
1262 (void)PerlIO_flush(PerlIO_stderr());
1268 (void)UNLINK(e_tmpname);
1269 Safefree(e_tmpname);
1272 statusvalue = SHIFTSTATUS(statusvalue);
1274 my_exit((U32)(vaxc$errno?vaxc$errno:(statusvalue?statusvalue:44)));
1276 my_exit((U32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
1297 va_start(args, pat);
1301 message = mess(pat, &args);
1305 SV *oldwarnhook = warnhook;
1306 warnhook = Nullsv; /* sv_2cv might end up calling warn() */
1307 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1308 warnhook = oldwarnhook;
1309 if (cv && !CvDEPTH(cv)) {
1314 PUSHs(sv_2mortal(newSVpv(message,0)));
1316 perl_call_sv((SV*)cv, G_DISCARD);
1320 PerlIO_puts(PerlIO_stderr(),message);
1324 (void)PerlIO_flush(PerlIO_stderr());
1326 #endif /* !defined(I_STDARG) && !defined(I_VARARGS) */
1328 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1333 register I32 i=setenv_getix(nam); /* where does it go? */
1335 if (environ == origenviron) { /* need we copy environment? */
1341 for (max = i; environ[max]; max++) ;
1342 New(901,tmpenv, max+2, char*);
1343 for (j=0; j<max; j++) /* copy environment */
1344 tmpenv[j] = savepv(environ[j]);
1345 tmpenv[max] = Nullch;
1346 environ = tmpenv; /* tell exec where it is now */
1349 while (environ[i]) {
1350 environ[i] = environ[i+1];
1355 if (!environ[i]) { /* does not exist yet */
1356 Renew(environ, i+2, char*); /* just expand it a bit */
1357 environ[i+1] = Nullch; /* make sure it's null terminated */
1360 Safefree(environ[i]);
1361 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1363 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1365 /* MS-DOS requires environment variable names to be in uppercase */
1366 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1367 * some utilities and applications may break because they only look
1368 * for upper case strings. (Fixed strupr() bug here.)]
1370 strcpy(environ[i],nam); strupr(environ[i]);
1371 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1379 register I32 i, len = strlen(nam);
1381 for (i = 0; environ[i]; i++) {
1382 if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
1383 break; /* strnEQ must come first to avoid */
1384 } /* potential SEGV's */
1389 #ifdef UNLINK_ALL_VERSIONS
1391 unlnk(f) /* unlink all versions of a file */
1396 for (i = 0; unlink(f) >= 0; i++) ;
1401 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1403 my_bcopy(from,to,len)
1404 register char *from;
1410 if (from - to >= 0) {
1418 *(--to) = *(--from);
1424 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1440 my_memcmp(s1,s2,len)
1441 register unsigned char *s1;
1442 register unsigned char *s2;
1448 if (tmp = *s1++ - *s2++)
1453 #endif /* HAS_MEMCMP */
1455 #if defined(I_STDARG) || defined(I_VARARGS)
1458 #ifdef USE_CHAR_VSPRINTF
1463 vsprintf(dest, pat, args)
1464 char *dest, *pat, *args;
1468 fakebuf._ptr = dest;
1469 fakebuf._cnt = 32767;
1473 fakebuf._flag = _IOWRT|_IOSTRG;
1474 _doprnt(pat, args, &fakebuf); /* what a kludge */
1475 (void)putc('\0', &fakebuf);
1476 #ifdef USE_CHAR_VSPRINTF
1479 return 0; /* perl doesn't use return value */
1483 #endif /* HAS_VPRINTF */
1484 #endif /* I_VARARGS || I_STDARGS */
1487 #if BYTEORDER != 0x4321
1489 #ifndef CAN_PROTOTYPE
1496 #if (BYTEORDER & 1) == 0
1499 result = ((s & 255) << 8) + ((s >> 8) & 255);
1507 #ifndef CAN_PROTOTYPE
1516 char c[sizeof(long)];
1519 #if BYTEORDER == 0x1234
1520 u.c[0] = (l >> 24) & 255;
1521 u.c[1] = (l >> 16) & 255;
1522 u.c[2] = (l >> 8) & 255;
1526 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1527 croak("Unknown BYTEORDER\n");
1532 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1533 u.c[o & 0xf] = (l >> s) & 255;
1541 #ifndef CAN_PROTOTYPE
1550 char c[sizeof(long)];
1553 #if BYTEORDER == 0x1234
1554 u.c[0] = (l >> 24) & 255;
1555 u.c[1] = (l >> 16) & 255;
1556 u.c[2] = (l >> 8) & 255;
1560 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1561 croak("Unknown BYTEORDER\n");
1568 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1569 l |= (u.c[o & 0xf] & 255) << s;
1576 #endif /* BYTEORDER != 0x4321 */
1580 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1581 * If these functions are defined,
1582 * the BYTEORDER is neither 0x1234 nor 0x4321.
1583 * However, this is not assumed.
1587 #define HTOV(name,type) \
1594 char c[sizeof(type)]; \
1598 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1599 u.c[i] = (n >> s) & 0xFF; \
1604 #define VTOH(name,type) \
1611 char c[sizeof(type)]; \
1617 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1618 n += (u.c[i] & 0xFF) << s; \
1623 #if defined(HAS_HTOVS) && !defined(htovs)
1626 #if defined(HAS_HTOVL) && !defined(htovl)
1629 #if defined(HAS_VTOHS) && !defined(vtohs)
1632 #if defined(HAS_VTOHL) && !defined(vtohl)
1636 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) \
1637 && !defined(VMS) /* VMS' my_popen() is in VMS.c, same with OS/2. */
1644 register I32 this, that;
1656 return my_syspopen(cmd,mode);
1661 this = (*mode == 'w');
1663 if (doexec && tainting) {
1665 taint_proper("Insecure %s%s", "EXEC");
1667 while ((pid = (doexec?vfork():fork())) < 0) {
1668 if (errno != EAGAIN) {
1671 croak("Can't fork");
1682 if (p[THIS] != (*mode == 'r')) {
1683 dup2(p[THIS], *mode == 'r');
1687 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1693 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1696 do_exec(cmd); /* may or may not use the shell */
1700 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1701 sv_setiv(GvSV(tmpgv),(I32)getpid());
1703 hv_clear(pidstatus); /* we have no children */
1708 do_execfree(); /* free any memory malloced by child on vfork */
1710 if (p[that] < p[this]) {
1711 dup2(p[this], p[that]);
1715 sv = *av_fetch(fdpid,p[this],TRUE);
1716 (void)SvUPGRADE(sv,SVt_IV);
1719 return PerlIO_fdopen(p[this], mode);
1722 #if defined(atarist) || defined(DJGPP)
1729 /* Needs work for PerlIO ! */
1730 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1731 return popen(PerlIO_exportFILE(cmd, 0), mode);
1735 #endif /* !DOSISH */
1742 struct stat tmpstatbuf;
1744 PerlIO_printf(PerlIO_stderr(),"%s", s);
1745 for (fd = 0; fd < 32; fd++) {
1746 if (Fstat(fd,&tmpstatbuf) >= 0)
1747 PerlIO_printf(PerlIO_stderr()," %d",fd);
1749 PerlIO_printf(PerlIO_stderr(),"\n");
1759 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1763 return fcntl(oldfd, F_DUPFD, newfd);
1772 while ((fd = dup(oldfd)) != newfd && fd >= 0) /* good enough for low fd's */
1775 close(fdtmp[--fdx]);
1781 #ifdef HAS_SIGACTION
1783 Sighandler_t rsignal(signo,handler)
1785 Sighandler_t handler;
1787 struct sigaction act,oact;
1789 act.sa_handler = handler;
1790 sigemptyset(&act.sa_mask);
1793 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1795 if (sigaction(signo, &act, &oact) < 0)
1798 return(oact.sa_handler);
1801 int rsignalsave(signo, handler, save)
1803 Sighandler_t handler;
1806 struct sigaction act;
1808 act.sa_handler = handler;
1809 sigemptyset(&act.sa_mask);
1812 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1814 return sigaction(signo, &act, save);
1817 int rsignalrestore(signo, save)
1821 return sigaction(signo, save, 0);
1824 Sighandler_t rsignalstate(signo)
1827 struct sigaction oact;
1828 if (sigaction(signo, 0, &oact)<0)
1831 return (oact.sa_handler);
1836 static int sig_trapped;
1839 Signal_t sig_trap(signo)
1845 Sighandler_t rsignalstate(signo)
1848 Sighandler_t oldsig;
1850 oldsig = signal(signo, sig_trap);
1851 signal(signo, oldsig);
1853 kill(getpid(),signo);
1858 Sighandler_t rsignal(signo,handler)
1860 Sighandler_t handler;
1862 return signal(signo,handler);
1865 int rsignalsave(signo, handler, save)
1867 Sighandler_t handler;
1870 *save = signal(signo,handler);
1871 return (*save == SIG_ERR) ? -1 : 0;
1874 int rsignalrestore(signo, save)
1878 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
1884 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) \
1885 && !defined(VMS) /* VMS' my_popen() is in VMS.c */
1890 Sigsave_t hstat, istat, qstat;
1895 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
1896 pid = (int)SvIVX(*svp);
1900 if (pid == -1) { /* Opened by popen. */
1901 return my_syspclose(ptr);
1906 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
1908 rsignalsave(SIGHUP, SIG_IGN, &hstat);
1909 rsignalsave(SIGINT, SIG_IGN, &istat);
1910 rsignalsave(SIGQUIT, SIG_IGN, &qstat);
1912 pid = wait4pid(pid, &status, 0);
1913 } while (pid == -1 && errno == EINTR);
1914 rsignalrestore(SIGHUP, &hstat);
1915 rsignalrestore(SIGINT, &istat);
1916 rsignalrestore(SIGQUIT, &qstat);
1917 return(pid < 0 ? pid : status);
1919 #endif /* !DOSISH */
1921 #if !defined(DOSISH) || defined(OS2)
1923 wait4pid(pid,statusp,flags)
1935 sprintf(spid, "%d", pid);
1936 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
1937 if (svp && *svp != &sv_undef) {
1938 *statusp = SvIVX(*svp);
1939 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1946 hv_iterinit(pidstatus);
1947 if (entry = hv_iternext(pidstatus)) {
1948 pid = atoi(hv_iterkey(entry,(I32*)statusp));
1949 sv = hv_iterval(pidstatus,entry);
1950 *statusp = SvIVX(sv);
1951 sprintf(spid, "%d", pid);
1952 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
1957 return waitpid(pid,statusp,flags);
1960 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
1965 croak("Can't do waitpid with flags");
1967 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
1968 pidgone(result,*statusp);
1977 #endif /* !DOSISH */
1988 sprintf(spid, "%d", pid);
1989 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
1990 (void)SvUPGRADE(sv,SVt_IV);
1995 #if defined(atarist) || defined(OS2) || defined(DJGPP)
1998 int /* Cannot prototype with I32
2007 /* Needs work for PerlIO ! */
2008 FILE *f = PerlIO_findFILE(ptr);
2009 I32 result = pclose(f);
2010 PerlIO_releaseFILE(ptr,f);
2016 repeatcpy(to,from,len,count)
2018 register char *from;
2023 register char *frombase = from;
2031 while (count-- > 0) {
2032 for (todo = len; todo > 0; todo--) {
2039 #ifndef CASTNEGFLOAT
2047 # define BIGDOUBLE 2147483648.0
2049 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2052 return (unsigned long)f;
2054 return (unsigned long)along;
2061 /* Unfortunately, on some systems the cast_uv() function doesn't
2062 work with the system-supplied definition of ULONG_MAX. The
2063 comparison (f >= ULONG_MAX) always comes out true. It must be a
2064 problem with the compiler constant folding.
2066 In any case, this workaround should be fine on any two's complement
2067 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2069 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2072 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2074 -- Kenneth Albanowski <kjahds@kjahds.com>
2078 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2086 return (I32) I32_MAX;
2088 return (I32) I32_MIN;
2108 return (UV) MY_UV_MAX;
2120 char *fa = strrchr(a,'/');
2121 char *fb = strrchr(b,'/');
2122 struct stat tmpstatbuf1;
2123 struct stat tmpstatbuf2;
2125 #define MAXPATHLEN 1024
2127 char tmpbuf[MAXPATHLEN+1];
2142 strncpy(tmpbuf, a, fa - a);
2143 if (Stat(tmpbuf, &tmpstatbuf1) < 0)
2148 strncpy(tmpbuf, b, fb - b);
2149 if (Stat(tmpbuf, &tmpstatbuf2) < 0)
2151 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2152 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2154 #endif /* !HAS_RENAME */
2157 scan_oct(start, len, retlen)
2162 register char *s = start;
2163 register UV retval = 0;
2164 bool overflowed = FALSE;
2166 while (len && *s >= '0' && *s <= '7') {
2167 register UV n = retval << 3;
2168 if (!overflowed && (n >> 3) != retval) {
2169 warn("Integer overflow in octal number");
2172 retval = n | (*s++ - '0');
2175 if (dowarn && len && (*s == '8' || *s == '9'))
2176 warn("Illegal octal digit ignored");
2177 *retlen = s - start;
2182 scan_hex(start, len, retlen)
2187 register char *s = start;
2188 register UV retval = 0;
2189 bool overflowed = FALSE;
2192 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2193 register UV n = retval << 4;
2194 if (!overflowed && (n >> 4) != retval) {
2195 warn("Integer overflow in hex number");
2198 retval = n | (tmp - hexdigit) & 15;
2201 *retlen = s - start;
2208 * This hack is to force load of "huge" support from libm.a
2209 * So it is in perl for (say) POSIX to use.
2210 * Needed for SunOS with Sun's 'acc' for example.