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 /* XXX Configure test needed */
19 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
24 # define SIG_ERR ((Sighandler_t) -1)
27 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
36 /* Put this after #includes because fork and vfork prototypes may
47 # include <sys/file.h>
51 # include <sys/wait.h>
58 static void xstat _((int));
59 long xcount[MAXXCOUNT];
60 long lastxcount[MAXXCOUNT];
61 long xycount[MAXXCOUNT][MAXYCOUNT];
62 long lastxycount[MAXXCOUNT][MAXYCOUNT];
68 /* paranoid version of malloc */
70 /* NOTE: Do not call the next three routines directly. Use the macros
71 * in handy.h, so that we can easily redefine everything to do tracking of
72 * allocated hunks back to the original New to track down any memory leaks.
73 * XXX This advice seems to be widely ignored :-( --AD August 1996.
77 safemalloc(MEM_SIZE size)
82 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
85 #endif /* HAS_64K_LIMIT */
88 croak("panic: malloc");
90 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
91 #if !(defined(I286) || defined(atarist))
92 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
94 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
101 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
108 /* paranoid version of realloc */
111 saferealloc(Malloc_t where,MEM_SIZE size)
114 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
115 Malloc_t PerlMem_realloc();
116 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
120 PerlIO_printf(PerlIO_stderr(),
121 "Reallocation too large: %lx\n", size) FLUSH;
124 #endif /* HAS_64K_LIMIT */
131 return safemalloc(size);
134 croak("panic: realloc");
136 ptr = PerlMem_realloc(where,size);
138 #if !(defined(I286) || defined(atarist))
140 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++);
141 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
145 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++);
146 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
155 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
162 /* safe version of free */
165 safefree(Malloc_t where)
167 #if !(defined(I286) || defined(atarist))
168 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
170 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++));
178 /* safe version of calloc */
181 safecalloc(MEM_SIZE count, MEM_SIZE size)
186 if (size * count > 0xffff) {
187 PerlIO_printf(PerlIO_stderr(),
188 "Allocation too large: %lx\n", size * count) FLUSH;
191 #endif /* HAS_64K_LIMIT */
193 if ((long)size < 0 || (long)count < 0)
194 croak("panic: calloc");
197 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
198 #if !(defined(I286) || defined(atarist))
199 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
201 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
204 memset((void*)ptr, 0, size);
210 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
217 #endif /* !MYMALLOC */
221 struct mem_test_strut {
229 # define ALIGN sizeof(struct mem_test_strut)
231 # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
232 # define typeof_chunk(ch) \
233 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
234 # define set_typeof_chunk(ch,t) \
235 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
236 #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
239 ? ((size) - 1)/8 + 5 \
243 safexmalloc(I32 x, MEM_SIZE size)
245 register char* where = (char*)safemalloc(size + ALIGN);
248 xycount[x][SIZE_TO_Y(size)]++;
249 set_typeof_chunk(where, x);
250 sizeof_chunk(where) = size;
251 return (Malloc_t)(where + ALIGN);
255 safexrealloc(Malloc_t wh, MEM_SIZE size)
257 char *where = (char*)wh;
260 return safexmalloc(0,size);
263 MEM_SIZE old = sizeof_chunk(where - ALIGN);
264 int t = typeof_chunk(where - ALIGN);
265 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
267 xycount[t][SIZE_TO_Y(old)]--;
268 xycount[t][SIZE_TO_Y(size)]++;
269 xcount[t] += size - old;
270 sizeof_chunk(new) = size;
271 return (Malloc_t)(new + ALIGN);
276 safexfree(Malloc_t wh)
279 char *where = (char*)wh;
285 size = sizeof_chunk(where);
286 x = where[0] + 100 * where[1];
288 xycount[x][SIZE_TO_Y(size)]--;
293 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
295 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
297 xycount[x][SIZE_TO_Y(size)]++;
298 memset((void*)(where + ALIGN), 0, size * count);
299 set_typeof_chunk(where, x);
300 sizeof_chunk(where) = size;
301 return (Malloc_t)(where + ALIGN);
307 register I32 i, j, total = 0;
308 I32 subtot[MAXYCOUNT];
310 for (j = 0; j < MAXYCOUNT; j++) {
314 PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
315 for (i = 0; i < MAXXCOUNT; i++) {
317 for (j = 0; j < MAXYCOUNT; j++) {
318 subtot[j] += xycount[i][j];
321 ? xcount[i] /* Have something */
323 ? xcount[i] != lastxcount[i] /* Changed */
324 : xcount[i] > lastxcount[i])) { /* Growed */
325 PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100,
326 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
327 lastxcount[i] = xcount[i];
328 for (j = 0; j < MAXYCOUNT; j++) {
330 ? xycount[i][j] /* Have something */
332 ? xycount[i][j] != lastxycount[i][j] /* Changed */
333 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
334 PerlIO_printf(PerlIO_stderr(),"%3ld ",
336 ? xycount[i][j] - lastxycount[i][j]
338 lastxycount[i][j] = xycount[i][j];
340 PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]);
343 PerlIO_printf(PerlIO_stderr(), "\n");
347 PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total);
348 for (j = 0; j < MAXYCOUNT; j++) {
350 PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]);
352 PerlIO_printf(PerlIO_stderr(), " . ");
355 PerlIO_printf(PerlIO_stderr(), "\n");
359 #endif /* LEAKTEST */
361 /* copy a string up to some (non-backslashed) delimiter, if any */
364 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
367 for (tolen = 0; from < fromend; from++, tolen++) {
369 if (from[1] == delim)
378 else if (*from == delim)
389 /* return ptr to little string in big string, NULL if not found */
390 /* This routine was donated by Corey Satten. */
393 instr(register char *big, register char *little)
395 register char *s, *x;
406 for (x=big,s=little; *s; /**/ ) {
420 /* same as instr but allow embedded nulls */
423 ninstr(register char *big, register char *bigend, char *little, char *lend)
425 register char *s, *x;
426 register I32 first = *little;
427 register char *littleend = lend;
429 if (!first && little >= littleend)
431 if (bigend - big < littleend - little)
433 bigend -= littleend - little++;
434 while (big <= bigend) {
437 for (x=big,s=little; s < littleend; /**/ ) {
449 /* reverse of the above--find last substring */
452 rninstr(register char *big, char *bigend, char *little, char *lend)
454 register char *bigbeg;
455 register char *s, *x;
456 register I32 first = *little;
457 register char *littleend = lend;
459 if (!first && little >= littleend)
462 big = bigend - (littleend - little++);
463 while (big >= bigbeg) {
466 for (x=big+2,s=little; s < littleend; /**/ ) {
479 * Set up for a new ctype locale.
482 perl_new_ctype(char *newctype)
484 #ifdef USE_LOCALE_CTYPE
488 for (i = 0; i < 256; i++) {
490 PL_fold_locale[i] = toLOWER_LC(i);
491 else if (isLOWER_LC(i))
492 PL_fold_locale[i] = toUPPER_LC(i);
494 PL_fold_locale[i] = i;
497 #endif /* USE_LOCALE_CTYPE */
501 * Set up for a new collation locale.
504 perl_new_collate(char *newcoll)
506 #ifdef USE_LOCALE_COLLATE
509 if (PL_collation_name) {
511 Safefree(PL_collation_name);
512 PL_collation_name = NULL;
513 PL_collation_standard = TRUE;
514 PL_collxfrm_base = 0;
515 PL_collxfrm_mult = 2;
520 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
522 Safefree(PL_collation_name);
523 PL_collation_name = savepv(newcoll);
524 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
527 /* 2: at most so many chars ('a', 'b'). */
528 /* 50: surely no system expands a char more. */
529 #define XFRMBUFSIZE (2 * 50)
530 char xbuf[XFRMBUFSIZE];
531 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
532 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
533 SSize_t mult = fb - fa;
535 croak("strxfrm() gets absurd");
536 PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
537 PL_collxfrm_mult = mult;
541 #endif /* USE_LOCALE_COLLATE */
545 * Set up for a new numeric locale.
548 perl_new_numeric(char *newnum)
550 #ifdef USE_LOCALE_NUMERIC
553 if (PL_numeric_name) {
554 Safefree(PL_numeric_name);
555 PL_numeric_name = NULL;
556 PL_numeric_standard = TRUE;
557 PL_numeric_local = TRUE;
562 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
563 Safefree(PL_numeric_name);
564 PL_numeric_name = savepv(newnum);
565 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
566 PL_numeric_local = TRUE;
569 #endif /* USE_LOCALE_NUMERIC */
573 perl_set_numeric_standard(void)
575 #ifdef USE_LOCALE_NUMERIC
577 if (! PL_numeric_standard) {
578 setlocale(LC_NUMERIC, "C");
579 PL_numeric_standard = TRUE;
580 PL_numeric_local = FALSE;
583 #endif /* USE_LOCALE_NUMERIC */
587 perl_set_numeric_local(void)
589 #ifdef USE_LOCALE_NUMERIC
591 if (! PL_numeric_local) {
592 setlocale(LC_NUMERIC, PL_numeric_name);
593 PL_numeric_standard = FALSE;
594 PL_numeric_local = TRUE;
597 #endif /* USE_LOCALE_NUMERIC */
602 * Initialize locale awareness.
605 perl_init_i18nl10n(int printwarn)
609 * 1 = set ok or not applicable,
610 * 0 = fallback to C locale,
611 * -1 = fallback to C locale failed
616 #ifdef USE_LOCALE_CTYPE
617 char *curctype = NULL;
618 #endif /* USE_LOCALE_CTYPE */
619 #ifdef USE_LOCALE_COLLATE
620 char *curcoll = NULL;
621 #endif /* USE_LOCALE_COLLATE */
622 #ifdef USE_LOCALE_NUMERIC
624 #endif /* USE_LOCALE_NUMERIC */
625 char *lc_all = PerlEnv_getenv("LC_ALL");
626 char *lang = PerlEnv_getenv("LANG");
627 bool setlocale_failure = FALSE;
629 #ifdef LOCALE_ENVIRON_REQUIRED
632 * Ultrix setlocale(..., "") fails if there are no environment
633 * variables from which to get a locale name.
640 if (setlocale(LC_ALL, ""))
643 setlocale_failure = TRUE;
645 if (!setlocale_failure) {
646 #ifdef USE_LOCALE_CTYPE
649 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
651 setlocale_failure = TRUE;
652 #endif /* USE_LOCALE_CTYPE */
653 #ifdef USE_LOCALE_COLLATE
655 setlocale(LC_COLLATE,
656 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
658 setlocale_failure = TRUE;
659 #endif /* USE_LOCALE_COLLATE */
660 #ifdef USE_LOCALE_NUMERIC
662 setlocale(LC_NUMERIC,
663 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
665 setlocale_failure = TRUE;
666 #endif /* USE_LOCALE_NUMERIC */
671 #endif /* !LOCALE_ENVIRON_REQUIRED */
674 if (! setlocale(LC_ALL, ""))
675 setlocale_failure = TRUE;
678 if (!setlocale_failure) {
679 #ifdef USE_LOCALE_CTYPE
680 if (! (curctype = setlocale(LC_CTYPE, "")))
681 setlocale_failure = TRUE;
682 #endif /* USE_LOCALE_CTYPE */
683 #ifdef USE_LOCALE_COLLATE
684 if (! (curcoll = setlocale(LC_COLLATE, "")))
685 setlocale_failure = TRUE;
686 #endif /* USE_LOCALE_COLLATE */
687 #ifdef USE_LOCALE_NUMERIC
688 if (! (curnum = setlocale(LC_NUMERIC, "")))
689 setlocale_failure = TRUE;
690 #endif /* USE_LOCALE_NUMERIC */
693 if (setlocale_failure) {
695 bool locwarn = (printwarn > 1 ||
697 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
702 PerlIO_printf(PerlIO_stderr(),
703 "perl: warning: Setting locale failed.\n");
707 PerlIO_printf(PerlIO_stderr(),
708 "perl: warning: Setting locale failed for the categories:\n\t");
709 #ifdef USE_LOCALE_CTYPE
711 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
712 #endif /* USE_LOCALE_CTYPE */
713 #ifdef USE_LOCALE_COLLATE
715 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
716 #endif /* USE_LOCALE_COLLATE */
717 #ifdef USE_LOCALE_NUMERIC
719 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
720 #endif /* USE_LOCALE_NUMERIC */
721 PerlIO_printf(PerlIO_stderr(), "\n");
725 PerlIO_printf(PerlIO_stderr(),
726 "perl: warning: Please check that your locale settings:\n");
728 PerlIO_printf(PerlIO_stderr(),
729 "\tLC_ALL = %c%s%c,\n",
731 lc_all ? lc_all : "unset",
736 for (e = environ; *e; e++) {
737 if (strnEQ(*e, "LC_", 3)
738 && strnNE(*e, "LC_ALL=", 7)
739 && (p = strchr(*e, '=')))
740 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
741 (int)(p - *e), *e, p + 1);
745 PerlIO_printf(PerlIO_stderr(),
748 lang ? lang : "unset",
751 PerlIO_printf(PerlIO_stderr(),
752 " are supported and installed on your system.\n");
757 if (setlocale(LC_ALL, "C")) {
759 PerlIO_printf(PerlIO_stderr(),
760 "perl: warning: Falling back to the standard locale (\"C\").\n");
765 PerlIO_printf(PerlIO_stderr(),
766 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
773 #ifdef USE_LOCALE_CTYPE
774 || !(curctype || setlocale(LC_CTYPE, "C"))
775 #endif /* USE_LOCALE_CTYPE */
776 #ifdef USE_LOCALE_COLLATE
777 || !(curcoll || setlocale(LC_COLLATE, "C"))
778 #endif /* USE_LOCALE_COLLATE */
779 #ifdef USE_LOCALE_NUMERIC
780 || !(curnum || setlocale(LC_NUMERIC, "C"))
781 #endif /* USE_LOCALE_NUMERIC */
785 PerlIO_printf(PerlIO_stderr(),
786 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
790 #endif /* ! LC_ALL */
792 #ifdef USE_LOCALE_CTYPE
793 curctype = setlocale(LC_CTYPE, Nullch);
794 #endif /* USE_LOCALE_CTYPE */
795 #ifdef USE_LOCALE_COLLATE
796 curcoll = setlocale(LC_COLLATE, Nullch);
797 #endif /* USE_LOCALE_COLLATE */
798 #ifdef USE_LOCALE_NUMERIC
799 curnum = setlocale(LC_NUMERIC, Nullch);
800 #endif /* USE_LOCALE_NUMERIC */
803 #ifdef USE_LOCALE_CTYPE
804 perl_new_ctype(curctype);
805 #endif /* USE_LOCALE_CTYPE */
807 #ifdef USE_LOCALE_COLLATE
808 perl_new_collate(curcoll);
809 #endif /* USE_LOCALE_COLLATE */
811 #ifdef USE_LOCALE_NUMERIC
812 perl_new_numeric(curnum);
813 #endif /* USE_LOCALE_NUMERIC */
815 #endif /* USE_LOCALE */
820 /* Backwards compatibility. */
822 perl_init_i18nl14n(int printwarn)
824 return perl_init_i18nl10n(printwarn);
827 #ifdef USE_LOCALE_COLLATE
830 * mem_collxfrm() is a bit like strxfrm() but with two important
831 * differences. First, it handles embedded NULs. Second, it allocates
832 * a bit more memory than needed for the transformed data itself.
833 * The real transformed data begins at offset sizeof(collationix).
834 * Please see sv_collxfrm() to see how this is used.
837 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
840 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
842 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
843 /* the +1 is for the terminating NUL. */
845 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
846 New(171, xbuf, xAlloc, char);
850 *(U32*)xbuf = PL_collation_ix;
851 xout = sizeof(PL_collation_ix);
852 for (xin = 0; xin < len; ) {
856 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
859 if (xused < xAlloc - xout)
861 xAlloc = (2 * xAlloc) + 1;
862 Renew(xbuf, xAlloc, char);
867 xin += strlen(s + xin) + 1;
870 /* Embedded NULs are understood but silently skipped
871 * because they make no sense in locale collation. */
875 *xlen = xout - sizeof(PL_collation_ix);
884 #endif /* USE_LOCALE_COLLATE */
887 fbm_compile(SV *sv, U32 flags /* not used yet */)
896 s = (U8*)SvPV_force(sv, len);
897 (void)SvUPGRADE(sv, SVt_PVBM);
898 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
899 return; /* can't have offsets that big */
901 Sv_Grow(sv,len + 258);
902 table = (unsigned char*)(SvPVX(sv) + len + 1);
904 for (i = 0; i < 256; i++) {
908 while (s >= (unsigned char*)(SvPVX(sv)))
910 if (table[*s] == len)
915 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
918 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
919 for (i = 0; i < len; i++) {
920 if (PL_freq[s[i]] < frequency) {
922 frequency = PL_freq[s[i]];
925 BmRARE(sv) = s[rarest];
926 BmPREVIOUS(sv) = rarest;
927 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
931 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
933 register unsigned char *s;
935 register I32 littlelen;
936 register unsigned char *little;
937 register unsigned char *table;
938 register unsigned char *olds;
939 register unsigned char *oldlittle;
941 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
943 char *l = SvPV(littlestr,len);
945 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
946 substr => we can ignore SvVALID */
949 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
954 if (bigend > big && bigend[-1] == '\n')
955 return (char *)(bigend - 1);
957 return (char *) bigend;
961 return ninstr((char*)big,(char*)bigend, l, l + len);
964 littlelen = SvCUR(littlestr);
965 if (SvTAIL(littlestr) && !PL_multiline) { /* tail anchored? */
966 if (littlelen > bigend - big)
968 little = (unsigned char*)SvPVX(littlestr);
969 s = bigend - littlelen;
971 && bigend[-1] == '\n'
972 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
973 return (char*)s - 1; /* how sweet it is */
974 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
975 return (char*)s; /* how sweet it is */
978 if (littlelen <= 2) {
979 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
980 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
981 /* This may do extra comparisons if littlelen == 2, but this
982 should be hidden in the noise since we do less indirection. */
986 while (s <= bigend) {
988 && (littlelen == 1 || s[1] == c2)
989 && (!SvTAIL(littlestr)
991 || s[littlelen] == '\n')) /* Automatically multiline */
999 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
1000 if (--littlelen >= bigend - big)
1002 s = big + littlelen;
1003 oldlittle = little = table - 2;
1007 if (tmp = table[*s]) {
1009 if (bigend - s > tmp) {
1014 if ((s += tmp) < bigend)
1020 tmp = littlelen; /* less expensive than calling strncmp() */
1023 if (*--s == *--little)
1026 s = olds + 1; /* here we pay the price for failure */
1028 if (s < bigend) /* fake up continue to outer loop */
1032 if (SvTAIL(littlestr) /* automatically multiline */
1033 && olds + 1 != bigend
1042 /* start_shift, end_shift are positive quantities which give offsets
1043 of ends of some substring of bigstr.
1044 If `last' we want the last occurence.
1045 old_posp is the way of communication between consequent calls if
1046 the next call needs to find the .
1047 The initial *old_posp should be -1.
1048 Note that we do not take into account SvTAIL, so it may give wrong
1049 positives if _ALL flag is set.
1053 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
1056 register unsigned char *s, *x;
1057 register unsigned char *big;
1059 register I32 previous;
1061 register unsigned char *little;
1062 register I32 stop_pos;
1063 register unsigned char *littleend;
1067 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
1068 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0))
1070 little = (unsigned char *)(SvPVX(littlestr));
1071 littleend = little + SvCUR(littlestr);
1073 /* The value of pos we can start at: */
1074 previous = BmPREVIOUS(littlestr);
1075 big = (unsigned char *)(SvPVX(bigstr));
1076 /* The value of pos we can stop at: */
1077 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1078 if (previous + start_shift > stop_pos) return Nullch;
1079 while (pos < previous + start_shift) {
1080 if (!(pos += PL_screamnext[pos]))
1085 if (pos >= stop_pos) break;
1086 if (big[pos-previous] != first)
1088 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1094 if (s == littleend) {
1096 if (!last) return (char *)(big+pos-previous);
1099 } while ( pos += PL_screamnext[pos] );
1100 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
1101 #else /* !POINTERRIGOR */
1104 if (pos >= stop_pos) break;
1105 if (big[pos] != first)
1107 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1113 if (s == littleend) {
1115 if (!last) return (char *)(big+pos);
1118 } while ( pos += PL_screamnext[pos] );
1119 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
1120 #endif /* POINTERRIGOR */
1124 ibcmp(char *s1, char *s2, register I32 len)
1126 register U8 *a = (U8 *)s1;
1127 register U8 *b = (U8 *)s2;
1129 if (*a != *b && *a != PL_fold[*b])
1137 ibcmp_locale(char *s1, char *s2, register I32 len)
1139 register U8 *a = (U8 *)s1;
1140 register U8 *b = (U8 *)s2;
1142 if (*a != *b && *a != PL_fold_locale[*b])
1149 /* copy a string to a safe spot */
1154 register char *newaddr;
1156 New(902,newaddr,strlen(sv)+1,char);
1157 (void)strcpy(newaddr,sv);
1161 /* same thing but with a known length */
1164 savepvn(char *sv, register I32 len)
1166 register char *newaddr;
1168 New(903,newaddr,len+1,char);
1169 Copy(sv,newaddr,len,char); /* might not be null terminated */
1170 newaddr[len] = '\0'; /* is now */
1174 /* the SV for form() and mess() is not kept in an arena */
1184 return sv_2mortal(newSVpvn("",0));
1189 /* Create as PVMG now, to avoid any upgrading later */
1190 New(905, sv, 1, SV);
1191 Newz(905, any, 1, XPVMG);
1192 SvFLAGS(sv) = SVt_PVMG;
1193 SvANY(sv) = (void*)any;
1194 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1200 form(const char* pat, ...)
1202 SV *sv = mess_alloc();
1204 va_start(args, pat);
1205 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1211 mess(const char *pat, va_list *args)
1213 SV *sv = mess_alloc();
1214 static char dgd[] = " during global destruction.\n";
1216 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1217 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1222 if (PL_curcop->cop_line)
1223 sv_catpvf(sv, " at %_ line %ld",
1224 GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line);
1225 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
1226 bool line_mode = (RsSIMPLE(PL_rs) &&
1227 SvLEN(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
1228 sv_catpvf(sv, ", <%s> %s %ld",
1229 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
1230 line_mode ? "line" : "chunk",
1231 (long)IoLINES(GvIOp(PL_last_in_gv)));
1233 sv_catpv(sv, ".\n");
1240 die(const char* pat, ...)
1245 int was_in_eval = PL_in_eval;
1250 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1251 "%p: die: curstack = %p, mainstack = %p\n",
1252 thr, PL_curstack, PL_mainstack));
1254 va_start(args, pat);
1255 message = pat ? mess(pat, &args) : Nullch;
1258 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1259 "%p: die: message = %s\ndiehook = %p\n",
1260 thr, message, PL_diehook));
1262 /* sv_2cv might call croak() */
1263 SV *olddiehook = PL_diehook;
1265 SAVESPTR(PL_diehook);
1266 PL_diehook = Nullsv;
1267 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1269 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1275 msg = newSVpv(message, 0);
1283 PUSHSTACKi(PERLSI_DIEHOOK);
1287 perl_call_sv((SV*)cv, G_DISCARD);
1293 PL_restartop = die_where(message);
1294 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1295 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1296 thr, PL_restartop, was_in_eval, PL_top_env));
1297 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
1299 return PL_restartop;
1303 croak(const char* pat, ...)
1312 va_start(args, pat);
1313 message = mess(pat, &args);
1315 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1317 /* sv_2cv might call croak() */
1318 SV *olddiehook = PL_diehook;
1320 SAVESPTR(PL_diehook);
1321 PL_diehook = Nullsv;
1322 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1324 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1329 msg = newSVpv(message, 0);
1333 PUSHSTACKi(PERLSI_DIEHOOK);
1337 perl_call_sv((SV*)cv, G_DISCARD);
1343 PL_restartop = die_where(message);
1346 PerlIO_puts(PerlIO_stderr(),message);
1347 (void)PerlIO_flush(PerlIO_stderr());
1352 warn(const char* pat,...)
1360 va_start(args, pat);
1361 message = mess(pat, &args);
1365 /* sv_2cv might call warn() */
1367 SV *oldwarnhook = PL_warnhook;
1369 SAVESPTR(PL_warnhook);
1370 PL_warnhook = Nullsv;
1371 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1373 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1378 msg = newSVpv(message, 0);
1382 PUSHSTACKi(PERLSI_WARNHOOK);
1386 perl_call_sv((SV*)cv, G_DISCARD);
1392 PerlIO_puts(PerlIO_stderr(),message);
1394 DEBUG_L(*message == '!'
1395 ? (xstat(message[1]=='!'
1396 ? (message[2]=='!' ? 2 : 1)
1401 (void)PerlIO_flush(PerlIO_stderr());
1405 warner(U32 err, const char* pat,...)
1414 va_start(args, pat);
1415 message = mess(pat, &args);
1420 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1421 #endif /* USE_THREADS */
1423 /* sv_2cv might call croak() */
1424 SV *olddiehook = PL_diehook;
1426 SAVESPTR(PL_diehook);
1427 PL_diehook = Nullsv;
1428 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1430 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1435 msg = newSVpv(message, 0);
1442 perl_call_sv((SV*)cv, G_DISCARD);
1448 PL_restartop = die_where(message);
1451 PerlIO_puts(PerlIO_stderr(),message);
1452 (void)PerlIO_flush(PerlIO_stderr());
1458 /* sv_2cv might call warn() */
1460 SV *oldwarnhook = PL_warnhook;
1462 SAVESPTR(PL_warnhook);
1463 PL_warnhook = Nullsv;
1464 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1466 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1471 msg = newSVpv(message, 0);
1478 perl_call_sv((SV*)cv, G_DISCARD);
1484 PerlIO_puts(PerlIO_stderr(),message);
1488 (void)PerlIO_flush(PerlIO_stderr());
1492 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1495 my_setenv(char *nam, char *val)
1497 register I32 i=setenv_getix(nam); /* where does it go? */
1499 if (environ == PL_origenviron) { /* need we copy environment? */
1505 for (max = i; environ[max]; max++) ;
1506 New(901,tmpenv, max+2, char*);
1507 for (j=0; j<max; j++) /* copy environment */
1508 tmpenv[j] = savepv(environ[j]);
1509 tmpenv[max] = Nullch;
1510 environ = tmpenv; /* tell exec where it is now */
1513 Safefree(environ[i]);
1514 while (environ[i]) {
1515 environ[i] = environ[i+1];
1520 if (!environ[i]) { /* does not exist yet */
1521 Renew(environ, i+2, char*); /* just expand it a bit */
1522 environ[i+1] = Nullch; /* make sure it's null terminated */
1525 Safefree(environ[i]);
1526 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1528 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1530 /* MS-DOS requires environment variable names to be in uppercase */
1531 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1532 * some utilities and applications may break because they only look
1533 * for upper case strings. (Fixed strupr() bug here.)]
1535 strcpy(environ[i],nam); strupr(environ[i]);
1536 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1540 #else /* if WIN32 */
1543 my_setenv(char *nam,char *val)
1546 #ifdef USE_WIN32_RTL_ENV
1548 register char *envstr;
1549 STRLEN namlen = strlen(nam);
1551 char *oldstr = environ[setenv_getix(nam)];
1553 /* putenv() has totally broken semantics in both the Borland
1554 * and Microsoft CRTLs. They either store the passed pointer in
1555 * the environment without making a copy, or make a copy and don't
1556 * free it. And on top of that, they dont free() old entries that
1557 * are being replaced/deleted. This means the caller must
1558 * free any old entries somehow, or we end up with a memory
1559 * leak every time my_setenv() is called. One might think
1560 * one could directly manipulate environ[], like the UNIX code
1561 * above, but direct changes to environ are not allowed when
1562 * calling putenv(), since the RTLs maintain an internal
1563 * *copy* of environ[]. Bad, bad, *bad* stink.
1574 vallen = strlen(val);
1575 New(904, envstr, namlen + vallen + 3, char);
1576 (void)sprintf(envstr,"%s=%s",nam,val);
1577 (void)PerlEnv_putenv(envstr);
1581 Safefree(envstr); /* MSVCRT leaks without this */
1584 #else /* !USE_WIN32_RTL_ENV */
1586 /* The sane way to deal with the environment.
1587 * Has these advantages over putenv() & co.:
1588 * * enables us to store a truly empty value in the
1589 * environment (like in UNIX).
1590 * * we don't have to deal with RTL globals, bugs and leaks.
1592 * Why you may want to enable USE_WIN32_RTL_ENV:
1593 * * environ[] and RTL functions will not reflect changes,
1594 * which might be an issue if extensions want to access
1595 * the env. via RTL. This cuts both ways, since RTL will
1596 * not see changes made by extensions that call the Win32
1597 * functions directly, either.
1600 SetEnvironmentVariable(nam,val);
1608 setenv_getix(char *nam)
1610 register I32 i, len = strlen(nam);
1612 for (i = 0; environ[i]; i++) {
1615 strnicmp(environ[i],nam,len) == 0
1617 strnEQ(environ[i],nam,len)
1619 && environ[i][len] == '=')
1620 break; /* strnEQ must come first to avoid */
1621 } /* potential SEGV's */
1627 #ifdef UNLINK_ALL_VERSIONS
1629 unlnk(f) /* unlink all versions of a file */
1634 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
1639 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1641 my_bcopy(register char *from,register char *to,register I32 len)
1645 if (from - to >= 0) {
1653 *(--to) = *(--from);
1661 my_memset(loc,ch,len)
1674 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1688 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1690 my_memcmp(s1,s2,len)
1695 register U8 *a = (U8 *)s1;
1696 register U8 *b = (U8 *)s2;
1700 if (tmp = *a++ - *b++)
1705 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1709 #ifdef USE_CHAR_VSPRINTF
1714 vsprintf(dest, pat, args)
1721 fakebuf._ptr = dest;
1722 fakebuf._cnt = 32767;
1726 fakebuf._flag = _IOWRT|_IOSTRG;
1727 _doprnt(pat, args, &fakebuf); /* what a kludge */
1728 (void)putc('\0', &fakebuf);
1729 #ifdef USE_CHAR_VSPRINTF
1732 return 0; /* perl doesn't use return value */
1736 #endif /* HAS_VPRINTF */
1739 #if BYTEORDER != 0x4321
1743 #if (BYTEORDER & 1) == 0
1746 result = ((s & 255) << 8) + ((s >> 8) & 255);
1758 char c[sizeof(long)];
1761 #if BYTEORDER == 0x1234
1762 u.c[0] = (l >> 24) & 255;
1763 u.c[1] = (l >> 16) & 255;
1764 u.c[2] = (l >> 8) & 255;
1768 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1769 croak("Unknown BYTEORDER\n");
1774 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1775 u.c[o & 0xf] = (l >> s) & 255;
1787 char c[sizeof(long)];
1790 #if BYTEORDER == 0x1234
1791 u.c[0] = (l >> 24) & 255;
1792 u.c[1] = (l >> 16) & 255;
1793 u.c[2] = (l >> 8) & 255;
1797 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1798 croak("Unknown BYTEORDER\n");
1805 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1806 l |= (u.c[o & 0xf] & 255) << s;
1813 #endif /* BYTEORDER != 0x4321 */
1817 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1818 * If these functions are defined,
1819 * the BYTEORDER is neither 0x1234 nor 0x4321.
1820 * However, this is not assumed.
1824 #define HTOV(name,type) \
1831 char c[sizeof(type)]; \
1835 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1836 u.c[i] = (n >> s) & 0xFF; \
1841 #define VTOH(name,type) \
1848 char c[sizeof(type)]; \
1854 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1855 n += (u.c[i] & 0xFF) << s; \
1860 #if defined(HAS_HTOVS) && !defined(htovs)
1863 #if defined(HAS_HTOVL) && !defined(htovl)
1866 #if defined(HAS_VTOHS) && !defined(vtohs)
1869 #if defined(HAS_VTOHL) && !defined(vtohl)
1873 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1874 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
1876 my_popen(char *cmd, char *mode)
1879 register I32 This, that;
1882 I32 doexec = strNE(cmd,"-");
1886 return my_syspopen(cmd,mode);
1889 This = (*mode == 'w');
1891 if (doexec && PL_tainting) {
1893 taint_proper("Insecure %s%s", "EXEC");
1895 if (PerlProc_pipe(p) < 0)
1897 while ((pid = (doexec?vfork():fork())) < 0) {
1898 if (errno != EAGAIN) {
1899 PerlLIO_close(p[This]);
1901 croak("Can't fork");
1913 PerlLIO_close(p[THAT]);
1914 if (p[THIS] != (*mode == 'r')) {
1915 PerlLIO_dup2(p[THIS], *mode == 'r');
1916 PerlLIO_close(p[THIS]);
1919 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1925 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
1928 do_exec(cmd); /* may or may not use the shell */
1932 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1933 sv_setiv(GvSV(tmpgv), (IV)getpid());
1935 hv_clear(PL_pidstatus); /* we have no children */
1940 do_execfree(); /* free any memory malloced by child on vfork */
1941 PerlLIO_close(p[that]);
1942 if (p[that] < p[This]) {
1943 PerlLIO_dup2(p[This], p[that]);
1944 PerlLIO_close(p[This]);
1947 sv = *av_fetch(PL_fdpid,p[This],TRUE);
1948 (void)SvUPGRADE(sv,SVt_IV);
1950 PL_forkprocess = pid;
1951 return PerlIO_fdopen(p[This], mode);
1954 #if defined(atarist) || defined(DJGPP)
1961 /* Needs work for PerlIO ! */
1962 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1963 return popen(PerlIO_exportFILE(cmd, 0), mode);
1967 #endif /* !DOSISH */
1974 struct stat tmpstatbuf;
1976 PerlIO_printf(PerlIO_stderr(),"%s", s);
1977 for (fd = 0; fd < 32; fd++) {
1978 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
1979 PerlIO_printf(PerlIO_stderr()," %d",fd);
1981 PerlIO_printf(PerlIO_stderr(),"\n");
1983 #endif /* DUMP_FDS */
1991 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1994 PerlLIO_close(newfd);
1995 return fcntl(oldfd, F_DUPFD, newfd);
1997 #define DUP2_MAX_FDS 256
1998 int fdtmp[DUP2_MAX_FDS];
2004 PerlLIO_close(newfd);
2005 /* good enough for low fd's... */
2006 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
2007 if (fdx >= DUP2_MAX_FDS) {
2015 PerlLIO_close(fdtmp[--fdx]);
2022 #ifdef HAS_SIGACTION
2025 rsignal(int signo, Sighandler_t handler)
2027 struct sigaction act, oact;
2029 act.sa_handler = handler;
2030 sigemptyset(&act.sa_mask);
2033 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2036 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2037 act.sa_flags |= SA_NOCLDWAIT;
2039 if (sigaction(signo, &act, &oact) == -1)
2042 return oact.sa_handler;
2046 rsignal_state(int signo)
2048 struct sigaction oact;
2050 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2053 return oact.sa_handler;
2057 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2059 struct sigaction act;
2061 act.sa_handler = handler;
2062 sigemptyset(&act.sa_mask);
2065 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2068 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2069 act.sa_flags |= SA_NOCLDWAIT;
2071 return sigaction(signo, &act, save);
2075 rsignal_restore(int signo, Sigsave_t *save)
2077 return sigaction(signo, save, (struct sigaction *)NULL);
2080 #else /* !HAS_SIGACTION */
2083 rsignal(int signo, Sighandler_t handler)
2085 return PerlProc_signal(signo, handler);
2088 static int sig_trapped;
2098 rsignal_state(int signo)
2100 Sighandler_t oldsig;
2103 oldsig = PerlProc_signal(signo, sig_trap);
2104 PerlProc_signal(signo, oldsig);
2106 PerlProc_kill(getpid(), signo);
2111 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2113 *save = PerlProc_signal(signo, handler);
2114 return (*save == SIG_ERR) ? -1 : 0;
2118 rsignal_restore(int signo, Sigsave_t *save)
2120 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
2123 #endif /* !HAS_SIGACTION */
2125 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2126 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
2128 my_pclose(PerlIO *ptr)
2130 Sigsave_t hstat, istat, qstat;
2138 int saved_vaxc_errno;
2141 int saved_win32_errno;
2144 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
2145 pid = (int)SvIVX(*svp);
2147 *svp = &PL_sv_undef;
2149 if (pid == -1) { /* Opened by popen. */
2150 return my_syspclose(ptr);
2153 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2154 saved_errno = errno;
2156 saved_vaxc_errno = vaxc$errno;
2159 saved_win32_errno = GetLastError();
2163 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2165 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2166 rsignal_save(SIGINT, SIG_IGN, &istat);
2167 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2169 pid2 = wait4pid(pid, &status, 0);
2170 } while (pid2 == -1 && errno == EINTR);
2171 rsignal_restore(SIGHUP, &hstat);
2172 rsignal_restore(SIGINT, &istat);
2173 rsignal_restore(SIGQUIT, &qstat);
2175 SETERRNO(saved_errno, saved_vaxc_errno);
2178 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
2180 #endif /* !DOSISH */
2182 #if !defined(DOSISH) || defined(OS2) || defined(WIN32)
2184 wait4pid(int pid, int *statusp, int flags)
2188 char spid[TYPE_CHARS(int)];
2193 sprintf(spid, "%d", pid);
2194 svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
2195 if (svp && *svp != &PL_sv_undef) {
2196 *statusp = SvIVX(*svp);
2197 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2204 hv_iterinit(PL_pidstatus);
2205 if (entry = hv_iternext(PL_pidstatus)) {
2206 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2207 sv = hv_iterval(PL_pidstatus,entry);
2208 *statusp = SvIVX(sv);
2209 sprintf(spid, "%d", pid);
2210 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2215 # ifdef HAS_WAITPID_RUNTIME
2216 if (!HAS_WAITPID_RUNTIME)
2219 return PerlProc_waitpid(pid,statusp,flags);
2221 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2222 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2224 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2229 croak("Can't do waitpid with flags");
2231 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
2232 pidgone(result,*statusp);
2240 #endif /* !DOSISH || OS2 || WIN32 */
2244 pidgone(int pid, int status)
2247 char spid[TYPE_CHARS(int)];
2249 sprintf(spid, "%d", pid);
2250 sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
2251 (void)SvUPGRADE(sv,SVt_IV);
2256 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2259 int /* Cannot prototype with I32
2268 /* Needs work for PerlIO ! */
2269 FILE *f = PerlIO_findFILE(ptr);
2270 I32 result = pclose(f);
2271 PerlIO_releaseFILE(ptr,f);
2277 repeatcpy(register char *to, register char *from, I32 len, register I32 count)
2280 register char *frombase = from;
2288 while (count-- > 0) {
2289 for (todo = len; todo > 0; todo--) {
2297 cast_ulong(double f)
2302 # define BIGDOUBLE 2147483648.0
2304 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2307 return (unsigned long)f;
2309 return (unsigned long)along;
2313 /* Unfortunately, on some systems the cast_uv() function doesn't
2314 work with the system-supplied definition of ULONG_MAX. The
2315 comparison (f >= ULONG_MAX) always comes out true. It must be a
2316 problem with the compiler constant folding.
2318 In any case, this workaround should be fine on any two's complement
2319 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2321 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2324 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2326 -- Kenneth Albanowski <kjahds@kjahds.com>
2330 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2337 return (I32) I32_MAX;
2339 return (I32) I32_MIN;
2357 return (UV) MY_UV_MAX;
2363 same_dirent(char *a, char *b)
2365 char *fa = strrchr(a,'/');
2366 char *fb = strrchr(b,'/');
2367 struct stat tmpstatbuf1;
2368 struct stat tmpstatbuf2;
2369 SV *tmpsv = sv_newmortal();
2382 sv_setpv(tmpsv, ".");
2384 sv_setpvn(tmpsv, a, fa - a);
2385 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2388 sv_setpv(tmpsv, ".");
2390 sv_setpvn(tmpsv, b, fb - b);
2391 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2393 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2394 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2396 #endif /* !HAS_RENAME */
2399 scan_oct(char *start, I32 len, I32 *retlen)
2401 register char *s = start;
2402 register UV retval = 0;
2403 bool overflowed = FALSE;
2405 while (len && *s >= '0' && *s <= '7') {
2406 register UV n = retval << 3;
2407 if (!overflowed && (n >> 3) != retval) {
2408 warn("Integer overflow in octal number");
2411 retval = n | (*s++ - '0');
2414 if (len && (*s == '8' || *s == '9')) {
2416 if (ckWARN(WARN_OCTAL))
2417 warner(WARN_OCTAL, "Illegal octal digit ignored");
2419 *retlen = s - start;
2424 scan_hex(char *start, I32 len, I32 *retlen)
2426 register char *s = start;
2427 register UV retval = 0;
2428 bool overflowed = FALSE;
2432 while (len-- && *s) {
2433 tmp = strchr((char *) PL_hexdigit, *s++);
2435 if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0))
2440 if (ckWARN(WARN_UNSAFE))
2441 warner(WARN_UNSAFE,"Illegal hex digit ignored");
2446 if (!overflowed && (n >> 4) != retval) {
2447 warn("Integer overflow in hex number");
2450 retval = n | ((tmp - PL_hexdigit) & 15);
2452 *retlen = s - start;
2457 find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
2460 char *xfound = Nullch;
2461 char *xfailed = Nullch;
2466 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2467 # define SEARCH_EXTS ".bat", ".cmd", NULL
2468 # define MAX_EXT_LEN 4
2471 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2472 # define MAX_EXT_LEN 4
2475 # define SEARCH_EXTS ".pl", ".com", NULL
2476 # define MAX_EXT_LEN 4
2478 /* additional extensions to try in each dir if scriptname not found */
2480 char *exts[] = { SEARCH_EXTS };
2481 char **ext = search_ext ? search_ext : exts;
2482 int extidx = 0, i = 0;
2483 char *curext = Nullch;
2485 # define MAX_EXT_LEN 0
2489 * If dosearch is true and if scriptname does not contain path
2490 * delimiters, search the PATH for scriptname.
2492 * If SEARCH_EXTS is also defined, will look for each
2493 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2494 * while searching the PATH.
2496 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2497 * proceeds as follows:
2498 * If DOSISH or VMSISH:
2499 * + look for ./scriptname{,.foo,.bar}
2500 * + search the PATH for scriptname{,.foo,.bar}
2503 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2504 * this will not look in '.' if it's not in the PATH)
2509 # ifdef ALWAYS_DEFTYPES
2510 len = strlen(scriptname);
2511 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2512 int hasdir, idx = 0, deftypes = 1;
2515 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2518 int hasdir, idx = 0, deftypes = 1;
2521 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2523 /* The first time through, just add SEARCH_EXTS to whatever we
2524 * already have, so we can check for default file types. */
2526 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
2532 if ((strlen(tmpbuf) + strlen(scriptname)
2533 + MAX_EXT_LEN) >= sizeof tmpbuf)
2534 continue; /* don't search dir with too-long name */
2535 strcat(tmpbuf, scriptname);
2539 if (strEQ(scriptname, "-"))
2541 if (dosearch) { /* Look in '.' first. */
2542 char *cur = scriptname;
2544 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2546 if (strEQ(ext[i++],curext)) {
2547 extidx = -1; /* already has an ext */
2552 DEBUG_p(PerlIO_printf(Perl_debug_log,
2553 "Looking for %s\n",cur));
2554 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2555 && !S_ISDIR(PL_statbuf.st_mode)) {
2563 if (cur == scriptname) {
2564 len = strlen(scriptname);
2565 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
2567 cur = strcpy(tmpbuf, scriptname);
2569 } while (extidx >= 0 && ext[extidx] /* try an extension? */
2570 && strcpy(tmpbuf+len, ext[extidx++]));
2575 if (dosearch && !strchr(scriptname, '/')
2577 && !strchr(scriptname, '\\')
2579 && (s = PerlEnv_getenv("PATH"))) {
2582 PL_bufend = s + strlen(s);
2583 while (s < PL_bufend) {
2584 #if defined(atarist) || defined(DOSISH)
2589 && *s != ';'; len++, s++) {
2590 if (len < sizeof tmpbuf)
2593 if (len < sizeof tmpbuf)
2595 #else /* ! (atarist || DOSISH) */
2596 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
2599 #endif /* ! (atarist || DOSISH) */
2602 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
2603 continue; /* don't search dir with too-long name */
2605 #if defined(atarist) || defined(DOSISH)
2606 && tmpbuf[len - 1] != '/'
2607 && tmpbuf[len - 1] != '\\'
2610 tmpbuf[len++] = '/';
2611 if (len == 2 && tmpbuf[0] == '.')
2613 (void)strcpy(tmpbuf + len, scriptname);
2617 len = strlen(tmpbuf);
2618 if (extidx > 0) /* reset after previous loop */
2622 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
2623 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
2624 if (S_ISDIR(PL_statbuf.st_mode)) {
2628 } while ( retval < 0 /* not there */
2629 && extidx>=0 && ext[extidx] /* try an extension? */
2630 && strcpy(tmpbuf+len, ext[extidx++])
2635 if (S_ISREG(PL_statbuf.st_mode)
2636 && cando(S_IRUSR,TRUE,&PL_statbuf)
2638 && cando(S_IXUSR,TRUE,&PL_statbuf)
2642 xfound = tmpbuf; /* bingo! */
2646 xfailed = savepv(tmpbuf);
2649 if (!xfound && !seen_dot && !xfailed &&
2650 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
2651 || S_ISDIR(PL_statbuf.st_mode)))
2653 seen_dot = 1; /* Disable message. */
2655 if (flags & 1) { /* do or die? */
2656 croak("Can't %s %s%s%s",
2657 (xfailed ? "execute" : "find"),
2658 (xfailed ? xfailed : scriptname),
2659 (xfailed ? "" : " on PATH"),
2660 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2662 scriptname = Nullch;
2666 scriptname = xfound;
2668 return (scriptname ? savepv(scriptname) : Nullch);
2674 /* Very simplistic scheduler for now */
2678 thr = thr->i.next_run;
2682 perl_cond_init(perl_cond *cp)
2688 perl_cond_signal(perl_cond *cp)
2691 perl_cond cond = *cp;
2696 /* Insert t in the runnable queue just ahead of us */
2697 t->i.next_run = thr->i.next_run;
2698 thr->i.next_run->i.prev_run = t;
2699 t->i.prev_run = thr;
2700 thr->i.next_run = t;
2701 thr->i.wait_queue = 0;
2702 /* Remove from the wait queue */
2708 perl_cond_broadcast(perl_cond *cp)
2711 perl_cond cond, cond_next;
2713 for (cond = *cp; cond; cond = cond_next) {
2715 /* Insert t in the runnable queue just ahead of us */
2716 t->i.next_run = thr->i.next_run;
2717 thr->i.next_run->i.prev_run = t;
2718 t->i.prev_run = thr;
2719 thr->i.next_run = t;
2720 thr->i.wait_queue = 0;
2721 /* Remove from the wait queue */
2722 cond_next = cond->next;
2729 perl_cond_wait(perl_cond *cp)
2733 if (thr->i.next_run == thr)
2734 croak("panic: perl_cond_wait called by last runnable thread");
2736 New(666, cond, 1, struct perl_wait_queue);
2740 thr->i.wait_queue = cond;
2741 /* Remove ourselves from runnable queue */
2742 thr->i.next_run->i.prev_run = thr->i.prev_run;
2743 thr->i.prev_run->i.next_run = thr->i.next_run;
2745 #endif /* FAKE_THREADS */
2747 #ifdef PTHREAD_GETSPECIFIC_INT
2748 struct perl_thread *
2753 if (pthread_getspecific(PL_thr_key, &t))
2754 croak("panic: pthread_getspecific");
2755 return (struct perl_thread *) t;
2760 condpair_magic(SV *sv)
2764 SvUPGRADE(sv, SVt_PVMG);
2765 mg = mg_find(sv, 'm');
2769 New(53, cp, 1, condpair_t);
2770 MUTEX_INIT(&cp->mutex);
2771 COND_INIT(&cp->owner_cond);
2772 COND_INIT(&cp->cond);
2775 mg = mg_find(sv, 'm');
2777 /* someone else beat us to initialising it */
2779 MUTEX_DESTROY(&cp->mutex);
2780 COND_DESTROY(&cp->owner_cond);
2781 COND_DESTROY(&cp->cond);
2785 sv_magic(sv, Nullsv, 'm', 0, 0);
2787 mg->mg_ptr = (char *)cp;
2788 mg->mg_len = sizeof(cp);
2790 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2791 "%p: condpair_magic %p\n", thr, sv));)
2798 * Make a new perl thread structure using t as a prototype. Some of the
2799 * fields for the new thread are copied from the prototype thread, t,
2800 * so t should not be running in perl at the time this function is
2801 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2802 * thread calling new_struct_thread) clearly satisfies this constraint.
2804 struct perl_thread *
2805 new_struct_thread(struct perl_thread *t)
2807 struct perl_thread *thr;
2812 sv = newSVpv("", 0);
2813 SvGROW(sv, sizeof(struct perl_thread) + 1);
2814 SvCUR_set(sv, sizeof(struct perl_thread));
2815 thr = (Thread) SvPVX(sv);
2817 memset(thr, 0xab, sizeof(struct perl_thread));
2824 Zero(&PL_hv_fetch_ent_mh, 1, HE);
2826 Zero(thr, 1, struct perl_thread);
2832 PL_curcop = &PL_compiling;
2833 thr->cvcache = newHV();
2834 thr->threadsv = newAV();
2835 thr->specific = newAV();
2836 thr->errsv = newSVpv("", 0);
2837 thr->errhv = newHV();
2838 thr->flags = THRf_R_JOINABLE;
2839 MUTEX_INIT(&thr->mutex);
2841 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
2842 PL_defstash = t->Tdefstash; /* XXX maybe these should */
2843 PL_curstash = t->Tcurstash; /* always be set to main? */
2846 /* top_env needs to be non-zero. It points to an area
2847 in which longjmp() stuff is stored, as C callstack
2848 info there at least is thread specific this has to
2849 be per-thread. Otherwise a 'die' in a thread gives
2850 that thread the C stack of last thread to do an eval {}!
2851 See comments in scope.h
2852 Initialize top entry (as in perl.c for main thread)
2854 PL_start_env.je_prev = NULL;
2855 PL_start_env.je_ret = -1;
2856 PL_start_env.je_mustcatch = TRUE;
2857 PL_top_env = &PL_start_env;
2862 PL_tainted = t->Ttainted;
2863 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
2864 PL_nrs = newSVsv(t->Tnrs);
2865 PL_rs = SvREFCNT_inc(PL_nrs);
2866 PL_last_in_gv = Nullgv;
2867 PL_ofslen = t->Tofslen;
2868 PL_ofs = savepvn(t->Tofs, PL_ofslen);
2869 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2870 PL_chopset = t->Tchopset;
2871 PL_formtarget = newSVsv(t->Tformtarget);
2872 PL_bodytarget = newSVsv(t->Tbodytarget);
2873 PL_toptarget = newSVsv(t->Ttoptarget);
2875 PL_statname = NEWSV(66,0);
2877 PL_regcompp = FUNC_NAME_TO_PTR(pregcomp);
2878 PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags);
2880 PL_reginterp_cnt = 0;
2881 PL_lastscream = Nullsv;
2884 PL_reg_start_tmp = 0;
2885 PL_reg_start_tmpl = 0;
2887 /* Initialise all per-thread SVs that the template thread used */
2888 svp = AvARRAY(t->threadsv);
2889 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
2890 if (*svp && *svp != &PL_sv_undef) {
2891 SV *sv = newSVsv(*svp);
2892 av_store(thr->threadsv, i, sv);
2893 sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1);
2894 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
2895 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
2898 thr->threadsvp = AvARRAY(thr->threadsv);
2900 MUTEX_LOCK(&PL_threads_mutex);
2902 thr->tid = ++PL_threadnum;
2903 thr->next = t->next;
2906 thr->next->prev = thr;
2907 MUTEX_UNLOCK(&PL_threads_mutex);
2909 #ifdef HAVE_THREAD_INTERN
2910 init_thread_intern(thr);
2911 #endif /* HAVE_THREAD_INTERN */
2914 #endif /* USE_THREADS */
2918 * This hack is to force load of "huge" support from libm.a
2919 * So it is in perl for (say) POSIX to use.
2920 * Needed for SunOS with Sun's 'acc' for example.
2929 #ifdef PERL_GLOBAL_STRUCT
2952 return (char*)PL_no_modify;
2962 get_specialsv_list(void)
2964 return PL_specialsv_list;
2969 get_vtbl(int vtbl_id)
2971 MGVTBL* result = Null(MGVTBL*);
2975 result = &PL_vtbl_sv;
2978 result = &PL_vtbl_env;
2980 case want_vtbl_envelem:
2981 result = &PL_vtbl_envelem;
2984 result = &PL_vtbl_sig;
2986 case want_vtbl_sigelem:
2987 result = &PL_vtbl_sigelem;
2989 case want_vtbl_pack:
2990 result = &PL_vtbl_pack;
2992 case want_vtbl_packelem:
2993 result = &PL_vtbl_packelem;
2995 case want_vtbl_dbline:
2996 result = &PL_vtbl_dbline;
2999 result = &PL_vtbl_isa;
3001 case want_vtbl_isaelem:
3002 result = &PL_vtbl_isaelem;
3004 case want_vtbl_arylen:
3005 result = &PL_vtbl_arylen;
3007 case want_vtbl_glob:
3008 result = &PL_vtbl_glob;
3010 case want_vtbl_mglob:
3011 result = &PL_vtbl_mglob;
3013 case want_vtbl_nkeys:
3014 result = &PL_vtbl_nkeys;
3016 case want_vtbl_taint:
3017 result = &PL_vtbl_taint;
3019 case want_vtbl_substr:
3020 result = &PL_vtbl_substr;
3023 result = &PL_vtbl_vec;
3026 result = &PL_vtbl_pos;
3029 result = &PL_vtbl_bm;
3032 result = &PL_vtbl_fm;
3034 case want_vtbl_uvar:
3035 result = &PL_vtbl_uvar;
3038 case want_vtbl_mutex:
3039 result = &PL_vtbl_mutex;
3042 case want_vtbl_defelem:
3043 result = &PL_vtbl_defelem;
3045 case want_vtbl_regexp:
3046 result = &PL_vtbl_regexp;
3048 case want_vtbl_regdata:
3049 result = &PL_vtbl_regdata;
3051 case want_vtbl_regdatum:
3052 result = &PL_vtbl_regdatum;
3054 case want_vtbl_collxfrm:
3055 result = &PL_vtbl_collxfrm;
3057 case want_vtbl_amagic:
3058 result = &PL_vtbl_amagic;
3060 case want_vtbl_amagicelem:
3061 result = &PL_vtbl_amagicelem;