3 * Copyright (c) 1991-1999, 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>
57 static void xstat _((int));
58 long xcount[MAXXCOUNT];
59 long lastxcount[MAXXCOUNT];
60 long xycount[MAXXCOUNT][MAXYCOUNT];
61 long lastxycount[MAXXCOUNT][MAXYCOUNT];
65 /* paranoid version of system's malloc() */
67 /* NOTE: Do not call the next three routines directly. Use the macros
68 * in handy.h, so that we can easily redefine everything to do tracking of
69 * allocated hunks back to the original New to track down any memory leaks.
70 * XXX This advice seems to be widely ignored :-( --AD August 1996.
74 safesysmalloc(MEM_SIZE size)
79 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
82 #endif /* HAS_64K_LIMIT */
85 croak("panic: malloc");
87 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
88 #if !(defined(I286) || defined(atarist))
89 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
91 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
98 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
105 /* paranoid version of system's realloc() */
108 safesysrealloc(Malloc_t where,MEM_SIZE size)
111 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
112 Malloc_t PerlMem_realloc();
113 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
117 PerlIO_printf(PerlIO_stderr(),
118 "Reallocation too large: %lx\n", size) FLUSH;
121 #endif /* HAS_64K_LIMIT */
128 return safesysmalloc(size);
131 croak("panic: realloc");
133 ptr = PerlMem_realloc(where,size);
135 #if !(defined(I286) || defined(atarist))
137 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++);
138 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
142 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++);
143 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
152 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
159 /* safe version of system's free() */
162 safesysfree(Malloc_t where)
164 #if !(defined(I286) || defined(atarist))
165 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
167 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++));
175 /* safe version of system's calloc() */
178 safesyscalloc(MEM_SIZE count, MEM_SIZE size)
183 if (size * count > 0xffff) {
184 PerlIO_printf(PerlIO_stderr(),
185 "Allocation too large: %lx\n", size * count) FLUSH;
188 #endif /* HAS_64K_LIMIT */
190 if ((long)size < 0 || (long)count < 0)
191 croak("panic: calloc");
194 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
195 #if !(defined(I286) || defined(atarist))
196 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
198 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
201 memset((void*)ptr, 0, size);
207 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
216 struct mem_test_strut {
224 # define ALIGN sizeof(struct mem_test_strut)
226 # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
227 # define typeof_chunk(ch) \
228 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
229 # define set_typeof_chunk(ch,t) \
230 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
231 #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
234 ? ((size) - 1)/8 + 5 \
238 safexmalloc(I32 x, MEM_SIZE size)
240 register char* where = (char*)safemalloc(size + ALIGN);
243 xycount[x][SIZE_TO_Y(size)]++;
244 set_typeof_chunk(where, x);
245 sizeof_chunk(where) = size;
246 return (Malloc_t)(where + ALIGN);
250 safexrealloc(Malloc_t wh, MEM_SIZE size)
252 char *where = (char*)wh;
255 return safexmalloc(0,size);
258 MEM_SIZE old = sizeof_chunk(where - ALIGN);
259 int t = typeof_chunk(where - ALIGN);
260 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
262 xycount[t][SIZE_TO_Y(old)]--;
263 xycount[t][SIZE_TO_Y(size)]++;
264 xcount[t] += size - old;
265 sizeof_chunk(new) = size;
266 return (Malloc_t)(new + ALIGN);
271 safexfree(Malloc_t wh)
274 char *where = (char*)wh;
280 size = sizeof_chunk(where);
281 x = where[0] + 100 * where[1];
283 xycount[x][SIZE_TO_Y(size)]--;
288 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
290 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
292 xycount[x][SIZE_TO_Y(size)]++;
293 memset((void*)(where + ALIGN), 0, size * count);
294 set_typeof_chunk(where, x);
295 sizeof_chunk(where) = size;
296 return (Malloc_t)(where + ALIGN);
302 register I32 i, j, total = 0;
303 I32 subtot[MAXYCOUNT];
305 for (j = 0; j < MAXYCOUNT; j++) {
309 PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
310 for (i = 0; i < MAXXCOUNT; i++) {
312 for (j = 0; j < MAXYCOUNT; j++) {
313 subtot[j] += xycount[i][j];
316 ? xcount[i] /* Have something */
318 ? xcount[i] != lastxcount[i] /* Changed */
319 : xcount[i] > lastxcount[i])) { /* Growed */
320 PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100,
321 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
322 lastxcount[i] = xcount[i];
323 for (j = 0; j < MAXYCOUNT; j++) {
325 ? xycount[i][j] /* Have something */
327 ? xycount[i][j] != lastxycount[i][j] /* Changed */
328 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
329 PerlIO_printf(PerlIO_stderr(),"%3ld ",
331 ? xycount[i][j] - lastxycount[i][j]
333 lastxycount[i][j] = xycount[i][j];
335 PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]);
338 PerlIO_printf(PerlIO_stderr(), "\n");
342 PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total);
343 for (j = 0; j < MAXYCOUNT; j++) {
345 PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]);
347 PerlIO_printf(PerlIO_stderr(), " . ");
350 PerlIO_printf(PerlIO_stderr(), "\n");
354 #endif /* LEAKTEST */
356 /* copy a string up to some (non-backslashed) delimiter, if any */
359 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
362 for (tolen = 0; from < fromend; from++, tolen++) {
364 if (from[1] == delim)
373 else if (*from == delim)
384 /* return ptr to little string in big string, NULL if not found */
385 /* This routine was donated by Corey Satten. */
388 instr(register const char *big, register const char *little)
390 register const char *s, *x;
401 for (x=big,s=little; *s; /**/ ) {
410 return (char*)(big-1);
415 /* same as instr but allow embedded nulls */
418 ninstr(register const char *big, register const char *bigend, const char *little, const char *lend)
420 register const char *s, *x;
421 register I32 first = *little;
422 register const char *littleend = lend;
424 if (!first && little >= littleend)
426 if (bigend - big < littleend - little)
428 bigend -= littleend - little++;
429 while (big <= bigend) {
432 for (x=big,s=little; s < littleend; /**/ ) {
439 return (char*)(big-1);
444 /* reverse of the above--find last substring */
447 rninstr(register const char *big, const char *bigend, const char *little, const char *lend)
449 register const char *bigbeg;
450 register const char *s, *x;
451 register I32 first = *little;
452 register const char *littleend = lend;
454 if (!first && little >= littleend)
455 return (char*)bigend;
457 big = bigend - (littleend - little++);
458 while (big >= bigbeg) {
461 for (x=big+2,s=little; s < littleend; /**/ ) {
468 return (char*)(big+1);
474 * Set up for a new ctype locale.
477 perl_new_ctype(const char *newctype)
479 #ifdef USE_LOCALE_CTYPE
483 for (i = 0; i < 256; i++) {
485 PL_fold_locale[i] = toLOWER_LC(i);
486 else if (isLOWER_LC(i))
487 PL_fold_locale[i] = toUPPER_LC(i);
489 PL_fold_locale[i] = i;
492 #endif /* USE_LOCALE_CTYPE */
496 * Set up for a new collation locale.
499 perl_new_collate(const char *newcoll)
501 #ifdef USE_LOCALE_COLLATE
504 if (PL_collation_name) {
506 Safefree(PL_collation_name);
507 PL_collation_name = NULL;
508 PL_collation_standard = TRUE;
509 PL_collxfrm_base = 0;
510 PL_collxfrm_mult = 2;
515 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
517 Safefree(PL_collation_name);
518 PL_collation_name = savepv(newcoll);
519 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
522 /* 2: at most so many chars ('a', 'b'). */
523 /* 50: surely no system expands a char more. */
524 #define XFRMBUFSIZE (2 * 50)
525 char xbuf[XFRMBUFSIZE];
526 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
527 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
528 SSize_t mult = fb - fa;
530 croak("strxfrm() gets absurd");
531 PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
532 PL_collxfrm_mult = mult;
536 #endif /* USE_LOCALE_COLLATE */
540 * Set up for a new numeric locale.
543 perl_new_numeric(const char *newnum)
545 #ifdef USE_LOCALE_NUMERIC
548 if (PL_numeric_name) {
549 Safefree(PL_numeric_name);
550 PL_numeric_name = NULL;
551 PL_numeric_standard = TRUE;
552 PL_numeric_local = TRUE;
557 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
558 Safefree(PL_numeric_name);
559 PL_numeric_name = savepv(newnum);
560 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
561 PL_numeric_local = TRUE;
564 #endif /* USE_LOCALE_NUMERIC */
568 perl_set_numeric_standard(void)
570 #ifdef USE_LOCALE_NUMERIC
572 if (! PL_numeric_standard) {
573 setlocale(LC_NUMERIC, "C");
574 PL_numeric_standard = TRUE;
575 PL_numeric_local = FALSE;
578 #endif /* USE_LOCALE_NUMERIC */
582 perl_set_numeric_local(void)
584 #ifdef USE_LOCALE_NUMERIC
586 if (! PL_numeric_local) {
587 setlocale(LC_NUMERIC, PL_numeric_name);
588 PL_numeric_standard = FALSE;
589 PL_numeric_local = TRUE;
592 #endif /* USE_LOCALE_NUMERIC */
597 * Initialize locale awareness.
600 perl_init_i18nl10n(int printwarn)
604 * 1 = set ok or not applicable,
605 * 0 = fallback to C locale,
606 * -1 = fallback to C locale failed
611 #ifdef USE_LOCALE_CTYPE
612 char *curctype = NULL;
613 #endif /* USE_LOCALE_CTYPE */
614 #ifdef USE_LOCALE_COLLATE
615 char *curcoll = NULL;
616 #endif /* USE_LOCALE_COLLATE */
617 #ifdef USE_LOCALE_NUMERIC
619 #endif /* USE_LOCALE_NUMERIC */
621 char *language = PerlEnv_getenv("LANGUAGE");
623 char *lc_all = PerlEnv_getenv("LC_ALL");
624 char *lang = PerlEnv_getenv("LANG");
625 bool setlocale_failure = FALSE;
627 #ifdef LOCALE_ENVIRON_REQUIRED
630 * Ultrix setlocale(..., "") fails if there are no environment
631 * variables from which to get a locale name.
638 if (setlocale(LC_ALL, ""))
641 setlocale_failure = TRUE;
643 if (!setlocale_failure) {
644 #ifdef USE_LOCALE_CTYPE
647 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
649 setlocale_failure = TRUE;
650 #endif /* USE_LOCALE_CTYPE */
651 #ifdef USE_LOCALE_COLLATE
653 setlocale(LC_COLLATE,
654 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
656 setlocale_failure = TRUE;
657 #endif /* USE_LOCALE_COLLATE */
658 #ifdef USE_LOCALE_NUMERIC
660 setlocale(LC_NUMERIC,
661 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
663 setlocale_failure = TRUE;
664 #endif /* USE_LOCALE_NUMERIC */
669 #endif /* !LOCALE_ENVIRON_REQUIRED */
672 if (! setlocale(LC_ALL, ""))
673 setlocale_failure = TRUE;
676 if (!setlocale_failure) {
677 #ifdef USE_LOCALE_CTYPE
678 if (! (curctype = setlocale(LC_CTYPE, "")))
679 setlocale_failure = TRUE;
680 #endif /* USE_LOCALE_CTYPE */
681 #ifdef USE_LOCALE_COLLATE
682 if (! (curcoll = setlocale(LC_COLLATE, "")))
683 setlocale_failure = TRUE;
684 #endif /* USE_LOCALE_COLLATE */
685 #ifdef USE_LOCALE_NUMERIC
686 if (! (curnum = setlocale(LC_NUMERIC, "")))
687 setlocale_failure = TRUE;
688 #endif /* USE_LOCALE_NUMERIC */
691 if (setlocale_failure) {
693 bool locwarn = (printwarn > 1 ||
695 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
700 PerlIO_printf(PerlIO_stderr(),
701 "perl: warning: Setting locale failed.\n");
705 PerlIO_printf(PerlIO_stderr(),
706 "perl: warning: Setting locale failed for the categories:\n\t");
707 #ifdef USE_LOCALE_CTYPE
709 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
710 #endif /* USE_LOCALE_CTYPE */
711 #ifdef USE_LOCALE_COLLATE
713 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
714 #endif /* USE_LOCALE_COLLATE */
715 #ifdef USE_LOCALE_NUMERIC
717 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
718 #endif /* USE_LOCALE_NUMERIC */
719 PerlIO_printf(PerlIO_stderr(), "\n");
723 PerlIO_printf(PerlIO_stderr(),
724 "perl: warning: Please check that your locale settings:\n");
727 PerlIO_printf(PerlIO_stderr(),
728 "\tLANGUAGE = %c%s%c,\n",
729 language ? '"' : '(',
730 language ? language : "unset",
731 language ? '"' : ')');
734 PerlIO_printf(PerlIO_stderr(),
735 "\tLC_ALL = %c%s%c,\n",
737 lc_all ? lc_all : "unset",
742 for (e = environ; *e; e++) {
743 if (strnEQ(*e, "LC_", 3)
744 && strnNE(*e, "LC_ALL=", 7)
745 && (p = strchr(*e, '=')))
746 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
747 (int)(p - *e), *e, p + 1);
751 PerlIO_printf(PerlIO_stderr(),
754 lang ? lang : "unset",
757 PerlIO_printf(PerlIO_stderr(),
758 " are supported and installed on your system.\n");
763 if (setlocale(LC_ALL, "C")) {
765 PerlIO_printf(PerlIO_stderr(),
766 "perl: warning: Falling back to the standard locale (\"C\").\n");
771 PerlIO_printf(PerlIO_stderr(),
772 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
779 #ifdef USE_LOCALE_CTYPE
780 || !(curctype || setlocale(LC_CTYPE, "C"))
781 #endif /* USE_LOCALE_CTYPE */
782 #ifdef USE_LOCALE_COLLATE
783 || !(curcoll || setlocale(LC_COLLATE, "C"))
784 #endif /* USE_LOCALE_COLLATE */
785 #ifdef USE_LOCALE_NUMERIC
786 || !(curnum || setlocale(LC_NUMERIC, "C"))
787 #endif /* USE_LOCALE_NUMERIC */
791 PerlIO_printf(PerlIO_stderr(),
792 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
796 #endif /* ! LC_ALL */
798 #ifdef USE_LOCALE_CTYPE
799 curctype = setlocale(LC_CTYPE, Nullch);
800 #endif /* USE_LOCALE_CTYPE */
801 #ifdef USE_LOCALE_COLLATE
802 curcoll = setlocale(LC_COLLATE, Nullch);
803 #endif /* USE_LOCALE_COLLATE */
804 #ifdef USE_LOCALE_NUMERIC
805 curnum = setlocale(LC_NUMERIC, Nullch);
806 #endif /* USE_LOCALE_NUMERIC */
809 #ifdef USE_LOCALE_CTYPE
810 perl_new_ctype(curctype);
811 #endif /* USE_LOCALE_CTYPE */
813 #ifdef USE_LOCALE_COLLATE
814 perl_new_collate(curcoll);
815 #endif /* USE_LOCALE_COLLATE */
817 #ifdef USE_LOCALE_NUMERIC
818 perl_new_numeric(curnum);
819 #endif /* USE_LOCALE_NUMERIC */
821 #endif /* USE_LOCALE */
826 /* Backwards compatibility. */
828 perl_init_i18nl14n(int printwarn)
830 return perl_init_i18nl10n(printwarn);
833 #ifdef USE_LOCALE_COLLATE
836 * mem_collxfrm() is a bit like strxfrm() but with two important
837 * differences. First, it handles embedded NULs. Second, it allocates
838 * a bit more memory than needed for the transformed data itself.
839 * The real transformed data begins at offset sizeof(collationix).
840 * Please see sv_collxfrm() to see how this is used.
843 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
846 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
848 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
849 /* the +1 is for the terminating NUL. */
851 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
852 New(171, xbuf, xAlloc, char);
856 *(U32*)xbuf = PL_collation_ix;
857 xout = sizeof(PL_collation_ix);
858 for (xin = 0; xin < len; ) {
862 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
865 if (xused < xAlloc - xout)
867 xAlloc = (2 * xAlloc) + 1;
868 Renew(xbuf, xAlloc, char);
873 xin += strlen(s + xin) + 1;
876 /* Embedded NULs are understood but silently skipped
877 * because they make no sense in locale collation. */
881 *xlen = xout - sizeof(PL_collation_ix);
890 #endif /* USE_LOCALE_COLLATE */
892 #define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/
894 /* As a space optimization, we do not compile tables for strings of length
895 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
896 special-cased in fbm_instr().
898 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
901 fbm_compile(SV *sv, U32 flags /* not used yet */)
910 if (flags & FBMcf_TAIL)
911 sv_catpvn(sv, "\n", 1); /* Taken into account in fbm_instr() */
912 s = (U8*)SvPV_force(sv, len);
913 (void)SvUPGRADE(sv, SVt_PVBM);
914 if (len == 0) /* TAIL might be on on a zero-length string. */
922 Sv_Grow(sv,len + 256 + FBM_TABLE_OFFSET);
923 table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET);
924 s = table - 1 - FBM_TABLE_OFFSET; /* Last char */
925 for (i = 0; i < 256; i++) {
928 table[-1] = flags; /* Not used yet */
932 if (table[*s] == mlen)
937 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
940 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
941 for (i = 0; i < len; i++) {
942 if (PL_freq[s[i]] < frequency) {
944 frequency = PL_freq[s[i]];
947 BmRARE(sv) = s[rarest];
948 BmPREVIOUS(sv) = rarest;
949 BmUSEFUL(sv) = 100; /* Initial value */
950 if (flags & FBMcf_TAIL)
952 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
955 /* If SvTAIL(littlestr), it has a fake '\n' at end. */
956 /* If SvTAIL is actually due to \Z or \z, this gives false positives
960 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
962 register unsigned char *s;
964 register unsigned char *little = (unsigned char *)SvPV(littlestr,l);
965 register STRLEN littlelen = l;
966 register I32 multiline = flags & FBMrf_MULTILINE;
968 if (bigend - big < littlelen) {
970 if ( SvTAIL(littlestr)
971 && (bigend - big == littlelen - 1)
973 || *big == *little && memEQ(big, little, littlelen - 1)))
978 if (littlelen <= 2) { /* Special-cased */
981 if (littlelen == 1) {
982 if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
983 /* Know that bigend != big. */
984 if (bigend[-1] == '\n')
985 return (char *)(bigend - 1);
986 return (char *) bigend;
994 if (SvTAIL(littlestr))
995 return (char *) bigend;
999 return (char*)big; /* Cannot be SvTAIL! */
1001 /* littlelen is 2 */
1002 if (SvTAIL(littlestr) && !multiline) {
1003 if (bigend[-1] == '\n' && bigend[-2] == *little)
1004 return (char*)bigend - 2;
1005 if (bigend[-1] == *little)
1006 return (char*)bigend - 1;
1010 /* This should be better than FBM if c1 == c2, and almost
1011 as good otherwise: maybe better since we do less indirection.
1012 And we save a lot of memory by caching no table. */
1013 register unsigned char c1 = little[0];
1014 register unsigned char c2 = little[1];
1019 while (s <= bigend) {
1022 return (char*)s - 1;
1029 goto check_1char_anchor;
1040 goto check_1char_anchor;
1043 while (s <= bigend) {
1046 return (char*)s - 1;
1048 goto check_1char_anchor;
1057 check_1char_anchor: /* One char and anchor! */
1058 if (SvTAIL(littlestr) && (*bigend == *little))
1059 return (char *)bigend; /* bigend is already decremented. */
1062 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
1063 s = bigend - littlelen;
1065 && bigend[-1] == '\n'
1067 /* Automatically of length > 2 */
1068 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
1069 return (char*)s; /* how sweet it is */
1070 if (s[1] == *little && memEQ((char*)s + 2,(char*)little + 1,
1072 return (char*)s + 1; /* how sweet it is */
1075 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
1076 char *b = ninstr((char*)big,(char*)bigend,
1077 (char*)little, (char*)little + littlelen);
1079 if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */
1080 /* Chop \n from littlestr: */
1081 s = bigend - littlelen + 1;
1082 if (*s == *little && memEQ((char*)s + 1, (char*)little + 1,
1090 { /* Do actual FBM. */
1091 register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET;
1092 register unsigned char *oldlittle;
1094 if (littlelen > bigend - big)
1096 --littlelen; /* Last char found by table lookup */
1098 s = big + littlelen;
1099 little += littlelen; /* last char */
1106 if (tmp = table[*s]) {
1108 if (bigend - s > tmp) {
1114 if ((s += tmp) < bigend)
1119 else { /* less expensive than calling strncmp() */
1120 register unsigned char *olds = s;
1125 if (*--s == *--little)
1128 s = olds + 1; /* here we pay the price for failure */
1130 if (s < bigend) /* fake up continue to outer loop */
1138 if ( s == bigend && (table[-1] & FBMcf_TAIL)
1139 && memEQ(bigend - littlelen, oldlittle - littlelen, littlelen) )
1140 return (char*)bigend - littlelen;
1145 /* start_shift, end_shift are positive quantities which give offsets
1146 of ends of some substring of bigstr.
1147 If `last' we want the last occurence.
1148 old_posp is the way of communication between consequent calls if
1149 the next call needs to find the .
1150 The initial *old_posp should be -1.
1152 Note that we take into account SvTAIL, so one can get extra
1153 optimizations if _ALL flag is set.
1156 /* If SvTAIL is actually due to \Z or \z, this gives false positives
1157 if PL_multiline. In fact if !PL_multiline the autoritative answer
1158 is not supported yet. */
1161 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
1164 register unsigned char *s, *x;
1165 register unsigned char *big;
1167 register I32 previous;
1169 register unsigned char *little;
1170 register I32 stop_pos;
1171 register unsigned char *littleend;
1175 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
1176 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) {
1178 if ( BmRARE(littlestr) == '\n'
1179 && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
1180 little = (unsigned char *)(SvPVX(littlestr));
1181 littleend = little + SvCUR(littlestr);
1188 little = (unsigned char *)(SvPVX(littlestr));
1189 littleend = little + SvCUR(littlestr);
1191 /* The value of pos we can start at: */
1192 previous = BmPREVIOUS(littlestr);
1193 big = (unsigned char *)(SvPVX(bigstr));
1194 /* The value of pos we can stop at: */
1195 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1196 if (previous + start_shift > stop_pos) {
1197 if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */
1201 while (pos < previous + start_shift) {
1202 if (!(pos += PL_screamnext[pos]))
1207 if (pos >= stop_pos) break;
1208 if (big[pos-previous] != first)
1210 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1216 if (s == littleend) {
1218 if (!last) return (char *)(big+pos-previous);
1221 } while ( pos += PL_screamnext[pos] );
1222 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
1223 #else /* !POINTERRIGOR */
1226 if (pos >= stop_pos) break;
1227 if (big[pos] != first)
1229 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1235 if (s == littleend) {
1237 if (!last) return (char *)(big+pos);
1240 } while ( pos += PL_screamnext[pos] );
1242 return (char *)(big+(*old_posp));
1243 #endif /* POINTERRIGOR */
1245 if (!SvTAIL(littlestr) || (end_shift > 0))
1247 /* Ignore the trailing "\n". This code is not microoptimized */
1248 big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr));
1249 stop_pos = littleend - little; /* Actual littlestr len */
1254 && ((stop_pos == 1) || memEQ(big + 1, little, stop_pos - 1)))
1260 ibcmp(const char *s1, const char *s2, register I32 len)
1262 register U8 *a = (U8 *)s1;
1263 register U8 *b = (U8 *)s2;
1265 if (*a != *b && *a != PL_fold[*b])
1273 ibcmp_locale(const char *s1, const char *s2, register I32 len)
1275 register U8 *a = (U8 *)s1;
1276 register U8 *b = (U8 *)s2;
1278 if (*a != *b && *a != PL_fold_locale[*b])
1285 /* copy a string to a safe spot */
1288 savepv(const char *sv)
1290 register char *newaddr;
1292 New(902,newaddr,strlen(sv)+1,char);
1293 (void)strcpy(newaddr,sv);
1297 /* same thing but with a known length */
1300 savepvn(const char *sv, register I32 len)
1302 register char *newaddr;
1304 New(903,newaddr,len+1,char);
1305 Copy(sv,newaddr,len,char); /* might not be null terminated */
1306 newaddr[len] = '\0'; /* is now */
1310 /* the SV for form() and mess() is not kept in an arena */
1320 return sv_2mortal(newSVpvn("",0));
1325 /* Create as PVMG now, to avoid any upgrading later */
1326 New(905, sv, 1, SV);
1327 Newz(905, any, 1, XPVMG);
1328 SvFLAGS(sv) = SVt_PVMG;
1329 SvANY(sv) = (void*)any;
1330 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1336 form(const char* pat, ...)
1338 SV *sv = mess_alloc();
1340 va_start(args, pat);
1341 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1347 mess(const char *pat, va_list *args)
1349 SV *sv = mess_alloc();
1350 static char dgd[] = " during global destruction.\n";
1352 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1353 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1355 if (PL_curcop->cop_line)
1356 sv_catpvf(sv, " at %_ line %ld",
1357 GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line);
1358 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
1359 bool line_mode = (RsSIMPLE(PL_rs) &&
1360 SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
1361 sv_catpvf(sv, ", <%s> %s %ld",
1362 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
1363 line_mode ? "line" : "chunk",
1364 (long)IoLINES(GvIOp(PL_last_in_gv)));
1366 sv_catpv(sv, PL_dirty ? dgd : ".\n");
1372 die(const char* pat, ...)
1377 int was_in_eval = PL_in_eval;
1384 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1385 "%p: die: curstack = %p, mainstack = %p\n",
1386 thr, PL_curstack, PL_mainstack));
1388 va_start(args, pat);
1390 msv = mess(pat, &args);
1391 message = SvPV(msv,msglen);
1398 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1399 "%p: die: message = %s\ndiehook = %p\n",
1400 thr, message, PL_diehook));
1402 /* sv_2cv might call croak() */
1403 SV *olddiehook = PL_diehook;
1405 SAVESPTR(PL_diehook);
1406 PL_diehook = Nullsv;
1407 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1409 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1415 msg = newSVpvn(message, msglen);
1423 PUSHSTACKi(PERLSI_DIEHOOK);
1427 perl_call_sv((SV*)cv, G_DISCARD);
1433 PL_restartop = die_where(message, msglen);
1434 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
1435 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1436 thr, PL_restartop, was_in_eval, PL_top_env));
1437 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
1439 return PL_restartop;
1443 croak(const char* pat, ...)
1454 va_start(args, pat);
1455 msv = mess(pat, &args);
1456 message = SvPV(msv,msglen);
1458 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1460 /* sv_2cv might call croak() */
1461 SV *olddiehook = PL_diehook;
1463 SAVESPTR(PL_diehook);
1464 PL_diehook = Nullsv;
1465 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1467 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1472 msg = newSVpvn(message, msglen);
1476 PUSHSTACKi(PERLSI_DIEHOOK);
1480 perl_call_sv((SV*)cv, G_DISCARD);
1486 PL_restartop = die_where(message, msglen);
1491 /* SFIO can really mess with your errno */
1494 PerlIO_write(PerlIO_stderr(), message, msglen);
1495 (void)PerlIO_flush(PerlIO_stderr());
1504 warn(const char* pat,...)
1514 va_start(args, pat);
1515 msv = mess(pat, &args);
1516 message = SvPV(msv, msglen);
1520 /* sv_2cv might call warn() */
1522 SV *oldwarnhook = PL_warnhook;
1524 SAVESPTR(PL_warnhook);
1525 PL_warnhook = Nullsv;
1526 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1528 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1533 msg = newSVpvn(message, msglen);
1537 PUSHSTACKi(PERLSI_WARNHOOK);
1541 perl_call_sv((SV*)cv, G_DISCARD);
1547 PerlIO_write(PerlIO_stderr(), message, msglen);
1549 DEBUG_L(*message == '!'
1550 ? (xstat(message[1]=='!'
1551 ? (message[2]=='!' ? 2 : 1)
1556 (void)PerlIO_flush(PerlIO_stderr());
1560 warner(U32 err, const char* pat,...)
1571 va_start(args, pat);
1572 msv = mess(pat, &args);
1573 message = SvPV(msv, msglen);
1578 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1579 #endif /* USE_THREADS */
1581 /* sv_2cv might call croak() */
1582 SV *olddiehook = PL_diehook;
1584 SAVESPTR(PL_diehook);
1585 PL_diehook = Nullsv;
1586 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1588 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1593 msg = newSVpvn(message, msglen);
1600 perl_call_sv((SV*)cv, G_DISCARD);
1606 PL_restartop = die_where(message, msglen);
1609 PerlIO_write(PerlIO_stderr(), message, msglen);
1610 (void)PerlIO_flush(PerlIO_stderr());
1616 /* sv_2cv might call warn() */
1618 SV *oldwarnhook = PL_warnhook;
1620 SAVESPTR(PL_warnhook);
1621 PL_warnhook = Nullsv;
1622 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1624 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1629 msg = newSVpvn(message, msglen);
1636 perl_call_sv((SV*)cv, G_DISCARD);
1642 PerlIO_write(PerlIO_stderr(), message, msglen);
1646 (void)PerlIO_flush(PerlIO_stderr());
1650 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1651 #if !defined(WIN32) && !defined(CYGWIN32)
1653 my_setenv(char *nam, char *val)
1655 #ifndef PERL_USE_SAFE_PUTENV
1656 /* most putenv()s leak, so we manipulate environ directly */
1657 register I32 i=setenv_getix(nam); /* where does it go? */
1659 if (environ == PL_origenviron) { /* need we copy environment? */
1665 for (max = i; environ[max]; max++) ;
1666 tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
1667 for (j=0; j<max; j++) { /* copy environment */
1668 tmpenv[j] = (char*)safesysmalloc((strlen(environ[j])+1)*sizeof(char));
1669 strcpy(tmpenv[j], environ[j]);
1671 tmpenv[max] = Nullch;
1672 environ = tmpenv; /* tell exec where it is now */
1675 safesysfree(environ[i]);
1676 while (environ[i]) {
1677 environ[i] = environ[i+1];
1682 if (!environ[i]) { /* does not exist yet */
1683 environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
1684 environ[i+1] = Nullch; /* make sure it's null terminated */
1687 safesysfree(environ[i]);
1688 environ[i] = (char*)safesysmalloc((strlen(nam)+strlen(val)+2) * sizeof(char));
1691 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1693 /* MS-DOS requires environment variable names to be in uppercase */
1694 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1695 * some utilities and applications may break because they only look
1696 * for upper case strings. (Fixed strupr() bug here.)]
1698 strcpy(environ[i],nam); strupr(environ[i]);
1699 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1702 #else /* PERL_USE_SAFE_PUTENV */
1705 new_env = (char*)safesysmalloc((strlen(nam) + strlen(val) + 2) * sizeof(char));
1707 (void)sprintf(new_env,"%s=%s",nam,val);/* all that work just for this */
1709 strcpy(new_env,nam); strupr(new_env);
1710 (void)sprintf(new_env + strlen(nam),"=%s",val);
1712 (void)putenv(new_env);
1713 #endif /* PERL_USE_SAFE_PUTENV */
1716 #else /* if WIN32 */
1719 my_setenv(char *nam,char *val)
1722 #ifdef USE_WIN32_RTL_ENV
1724 register char *envstr;
1725 STRLEN namlen = strlen(nam);
1727 char *oldstr = environ[setenv_getix(nam)];
1729 /* putenv() has totally broken semantics in both the Borland
1730 * and Microsoft CRTLs. They either store the passed pointer in
1731 * the environment without making a copy, or make a copy and don't
1732 * free it. And on top of that, they dont free() old entries that
1733 * are being replaced/deleted. This means the caller must
1734 * free any old entries somehow, or we end up with a memory
1735 * leak every time my_setenv() is called. One might think
1736 * one could directly manipulate environ[], like the UNIX code
1737 * above, but direct changes to environ are not allowed when
1738 * calling putenv(), since the RTLs maintain an internal
1739 * *copy* of environ[]. Bad, bad, *bad* stink.
1750 vallen = strlen(val);
1751 envstr = (char*)safesysmalloc((namlen + vallen + 3) * sizeof(char));
1752 (void)sprintf(envstr,"%s=%s",nam,val);
1753 (void)PerlEnv_putenv(envstr);
1755 safesysfree(oldstr);
1757 safesysfree(envstr); /* MSVCRT leaks without this */
1760 #else /* !USE_WIN32_RTL_ENV */
1762 register char *envstr;
1763 STRLEN len = strlen(nam) + 3;
1768 New(904, envstr, len, char);
1769 (void)sprintf(envstr,"%s=%s",nam,val);
1770 (void)PerlEnv_putenv(envstr);
1779 setenv_getix(char *nam)
1781 register I32 i, len = strlen(nam);
1783 for (i = 0; environ[i]; i++) {
1786 strnicmp(environ[i],nam,len) == 0
1788 strnEQ(environ[i],nam,len)
1790 && environ[i][len] == '=')
1791 break; /* strnEQ must come first to avoid */
1792 } /* potential SEGV's */
1798 #ifdef UNLINK_ALL_VERSIONS
1800 unlnk(f) /* unlink all versions of a file */
1805 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
1810 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1812 my_bcopy(register const char *from,register char *to,register I32 len)
1816 if (from - to >= 0) {
1824 *(--to) = *(--from);
1832 my_memset(register char *loc, register I32 ch, register I32 len)
1842 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1844 my_bzero(register char *loc, register I32 len)
1854 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1856 my_memcmp(const char *s1, const char *s2, register I32 len)
1858 register U8 *a = (U8 *)s1;
1859 register U8 *b = (U8 *)s2;
1863 if (tmp = *a++ - *b++)
1868 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1872 #ifdef USE_CHAR_VSPRINTF
1877 vsprintf(char *dest, const char *pat, char *args)
1881 fakebuf._ptr = dest;
1882 fakebuf._cnt = 32767;
1886 fakebuf._flag = _IOWRT|_IOSTRG;
1887 _doprnt(pat, args, &fakebuf); /* what a kludge */
1888 (void)putc('\0', &fakebuf);
1889 #ifdef USE_CHAR_VSPRINTF
1892 return 0; /* perl doesn't use return value */
1896 #endif /* HAS_VPRINTF */
1899 #if BYTEORDER != 0x4321
1903 #if (BYTEORDER & 1) == 0
1906 result = ((s & 255) << 8) + ((s >> 8) & 255);
1918 char c[sizeof(long)];
1921 #if BYTEORDER == 0x1234
1922 u.c[0] = (l >> 24) & 255;
1923 u.c[1] = (l >> 16) & 255;
1924 u.c[2] = (l >> 8) & 255;
1928 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1929 croak("Unknown BYTEORDER\n");
1934 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1935 u.c[o & 0xf] = (l >> s) & 255;
1947 char c[sizeof(long)];
1950 #if BYTEORDER == 0x1234
1951 u.c[0] = (l >> 24) & 255;
1952 u.c[1] = (l >> 16) & 255;
1953 u.c[2] = (l >> 8) & 255;
1957 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1958 croak("Unknown BYTEORDER\n");
1965 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1966 l |= (u.c[o & 0xf] & 255) << s;
1973 #endif /* BYTEORDER != 0x4321 */
1977 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1978 * If these functions are defined,
1979 * the BYTEORDER is neither 0x1234 nor 0x4321.
1980 * However, this is not assumed.
1984 #define HTOV(name,type) \
1991 char c[sizeof(type)]; \
1995 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1996 u.c[i] = (n >> s) & 0xFF; \
2001 #define VTOH(name,type) \
2008 char c[sizeof(type)]; \
2014 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
2015 n += (u.c[i] & 0xFF) << s; \
2020 #if defined(HAS_HTOVS) && !defined(htovs)
2023 #if defined(HAS_HTOVL) && !defined(htovl)
2026 #if defined(HAS_VTOHS) && !defined(vtohs)
2029 #if defined(HAS_VTOHL) && !defined(vtohl)
2033 /* VMS' my_popen() is in VMS.c, same with OS/2. */
2034 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
2036 my_popen(char *cmd, char *mode)
2039 register I32 This, that;
2042 I32 doexec = strNE(cmd,"-");
2046 PERL_FLUSHALL_FOR_CHILD;
2049 return my_syspopen(cmd,mode);
2052 This = (*mode == 'w');
2054 if (doexec && PL_tainting) {
2056 taint_proper("Insecure %s%s", "EXEC");
2058 if (PerlProc_pipe(p) < 0)
2060 if (doexec && PerlProc_pipe(pp) >= 0)
2062 while ((pid = (doexec?vfork():fork())) < 0) {
2063 if (errno != EAGAIN) {
2064 PerlLIO_close(p[This]);
2066 PerlLIO_close(pp[0]);
2067 PerlLIO_close(pp[1]);
2070 croak("Can't fork");
2082 PerlLIO_close(p[THAT]);
2084 PerlLIO_close(pp[0]);
2085 #if defined(HAS_FCNTL) && defined(F_SETFD)
2086 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2089 if (p[THIS] != (*mode == 'r')) {
2090 PerlLIO_dup2(p[THIS], *mode == 'r');
2091 PerlLIO_close(p[THIS]);
2094 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2100 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2104 do_exec3(cmd,pp[1],did_pipes); /* may or may not use the shell */
2108 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
2109 sv_setiv(GvSV(tmpgv), (IV)getpid());
2111 hv_clear(PL_pidstatus); /* we have no children */
2116 do_execfree(); /* free any memory malloced by child on vfork */
2117 PerlLIO_close(p[that]);
2119 PerlLIO_close(pp[1]);
2120 if (p[that] < p[This]) {
2121 PerlLIO_dup2(p[This], p[that]);
2122 PerlLIO_close(p[This]);
2125 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2126 (void)SvUPGRADE(sv,SVt_IV);
2128 PL_forkprocess = pid;
2129 if (did_pipes && pid > 0) {
2133 while (n < sizeof(int)) {
2134 n1 = PerlLIO_read(pp[0],
2135 (void*)(((char*)&errkid)+n),
2141 if (n) { /* Error */
2142 if (n != sizeof(int))
2143 croak("panic: kid popen errno read");
2144 PerlLIO_close(pp[0]);
2145 errno = errkid; /* Propagate errno from kid */
2150 PerlLIO_close(pp[0]);
2151 return PerlIO_fdopen(p[This], mode);
2154 #if defined(atarist) || defined(DJGPP)
2157 my_popen(char *cmd, char *mode)
2159 /* Needs work for PerlIO ! */
2160 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
2161 PERL_FLUSHALL_FOR_CHILD;
2162 return popen(PerlIO_exportFILE(cmd, 0), mode);
2166 #endif /* !DOSISH */
2173 struct stat tmpstatbuf;
2175 PerlIO_printf(PerlIO_stderr(),"%s", s);
2176 for (fd = 0; fd < 32; fd++) {
2177 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
2178 PerlIO_printf(PerlIO_stderr()," %d",fd);
2180 PerlIO_printf(PerlIO_stderr(),"\n");
2182 #endif /* DUMP_FDS */
2190 #if defined(HAS_FCNTL) && defined(F_DUPFD)
2193 PerlLIO_close(newfd);
2194 return fcntl(oldfd, F_DUPFD, newfd);
2196 #define DUP2_MAX_FDS 256
2197 int fdtmp[DUP2_MAX_FDS];
2203 PerlLIO_close(newfd);
2204 /* good enough for low fd's... */
2205 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
2206 if (fdx >= DUP2_MAX_FDS) {
2214 PerlLIO_close(fdtmp[--fdx]);
2221 #ifdef HAS_SIGACTION
2224 rsignal(int signo, Sighandler_t handler)
2226 struct sigaction act, oact;
2228 act.sa_handler = handler;
2229 sigemptyset(&act.sa_mask);
2232 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2235 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2236 act.sa_flags |= SA_NOCLDWAIT;
2238 if (sigaction(signo, &act, &oact) == -1)
2241 return oact.sa_handler;
2245 rsignal_state(int signo)
2247 struct sigaction oact;
2249 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2252 return oact.sa_handler;
2256 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2258 struct sigaction act;
2260 act.sa_handler = handler;
2261 sigemptyset(&act.sa_mask);
2264 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2267 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2268 act.sa_flags |= SA_NOCLDWAIT;
2270 return sigaction(signo, &act, save);
2274 rsignal_restore(int signo, Sigsave_t *save)
2276 return sigaction(signo, save, (struct sigaction *)NULL);
2279 #else /* !HAS_SIGACTION */
2282 rsignal(int signo, Sighandler_t handler)
2284 return PerlProc_signal(signo, handler);
2287 static int sig_trapped;
2297 rsignal_state(int signo)
2299 Sighandler_t oldsig;
2302 oldsig = PerlProc_signal(signo, sig_trap);
2303 PerlProc_signal(signo, oldsig);
2305 PerlProc_kill(getpid(), signo);
2310 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
2312 *save = PerlProc_signal(signo, handler);
2313 return (*save == SIG_ERR) ? -1 : 0;
2317 rsignal_restore(int signo, Sigsave_t *save)
2319 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
2322 #endif /* !HAS_SIGACTION */
2324 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2325 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
2327 my_pclose(PerlIO *ptr)
2329 Sigsave_t hstat, istat, qstat;
2337 int saved_vaxc_errno;
2340 int saved_win32_errno;
2343 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
2344 pid = (int)SvIVX(*svp);
2346 *svp = &PL_sv_undef;
2348 if (pid == -1) { /* Opened by popen. */
2349 return my_syspclose(ptr);
2352 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2353 saved_errno = errno;
2355 saved_vaxc_errno = vaxc$errno;
2358 saved_win32_errno = GetLastError();
2362 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2364 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2365 rsignal_save(SIGINT, SIG_IGN, &istat);
2366 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2368 pid2 = wait4pid(pid, &status, 0);
2369 } while (pid2 == -1 && errno == EINTR);
2370 rsignal_restore(SIGHUP, &hstat);
2371 rsignal_restore(SIGINT, &istat);
2372 rsignal_restore(SIGQUIT, &qstat);
2374 SETERRNO(saved_errno, saved_vaxc_errno);
2377 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
2379 #endif /* !DOSISH */
2381 #if !defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(CYGWIN32)
2383 wait4pid(int pid, int *statusp, int flags)
2387 char spid[TYPE_CHARS(int)];
2392 sprintf(spid, "%d", pid);
2393 svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
2394 if (svp && *svp != &PL_sv_undef) {
2395 *statusp = SvIVX(*svp);
2396 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2403 hv_iterinit(PL_pidstatus);
2404 if (entry = hv_iternext(PL_pidstatus)) {
2405 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2406 sv = hv_iterval(PL_pidstatus,entry);
2407 *statusp = SvIVX(sv);
2408 sprintf(spid, "%d", pid);
2409 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2414 # ifdef HAS_WAITPID_RUNTIME
2415 if (!HAS_WAITPID_RUNTIME)
2418 return PerlProc_waitpid(pid,statusp,flags);
2420 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2421 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2423 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2428 croak("Can't do waitpid with flags");
2430 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
2431 pidgone(result,*statusp);
2439 #endif /* !DOSISH || OS2 || WIN32 */
2443 pidgone(int pid, int status)
2446 char spid[TYPE_CHARS(int)];
2448 sprintf(spid, "%d", pid);
2449 sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
2450 (void)SvUPGRADE(sv,SVt_IV);
2455 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2458 int /* Cannot prototype with I32
2467 /* Needs work for PerlIO ! */
2468 FILE *f = PerlIO_findFILE(ptr);
2469 I32 result = pclose(f);
2470 PerlIO_releaseFILE(ptr,f);
2476 repeatcpy(register char *to, register const char *from, I32 len, register I32 count)
2479 register const char *frombase = from;
2482 register const char c = *from;
2487 while (count-- > 0) {
2488 for (todo = len; todo > 0; todo--) {
2496 cast_ulong(double f)
2501 # define BIGDOUBLE 2147483648.0
2503 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2506 return (unsigned long)f;
2508 return (unsigned long)along;
2512 /* Unfortunately, on some systems the cast_uv() function doesn't
2513 work with the system-supplied definition of ULONG_MAX. The
2514 comparison (f >= ULONG_MAX) always comes out true. It must be a
2515 problem with the compiler constant folding.
2517 In any case, this workaround should be fine on any two's complement
2518 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2520 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2523 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2525 -- Kenneth Albanowski <kjahds@kjahds.com>
2529 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2536 return (I32) I32_MAX;
2538 return (I32) I32_MIN;
2548 if (f >= (double)UV_MAX)
2562 return (UV) MY_UV_MAX;
2576 same_dirent(char *a, char *b)
2578 char *fa = strrchr(a,'/');
2579 char *fb = strrchr(b,'/');
2580 struct stat tmpstatbuf1;
2581 struct stat tmpstatbuf2;
2582 SV *tmpsv = sv_newmortal();
2595 sv_setpv(tmpsv, ".");
2597 sv_setpvn(tmpsv, a, fa - a);
2598 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2601 sv_setpv(tmpsv, ".");
2603 sv_setpvn(tmpsv, b, fb - b);
2604 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2606 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2607 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2609 #endif /* !HAS_RENAME */
2612 scan_bin(char *start, I32 len, I32 *retlen)
2614 register char *s = start;
2615 register UV retval = 0;
2616 bool overflowed = FALSE;
2617 while (len && *s >= '0' && *s <= '1') {
2618 register UV n = retval << 1;
2619 if (!overflowed && (n >> 1) != retval) {
2620 warn("Integer overflow in binary number");
2623 retval = n | (*s++ - '0');
2626 if (len && (*s >= '2' && *s <= '9')) {
2628 if (ckWARN(WARN_UNSAFE))
2629 warner(WARN_UNSAFE, "Illegal binary digit '%c' ignored", *s);
2631 *retlen = s - start;
2635 scan_oct(char *start, I32 len, I32 *retlen)
2637 register char *s = start;
2638 register UV retval = 0;
2639 bool overflowed = FALSE;
2641 while (len && *s >= '0' && *s <= '7') {
2642 register UV n = retval << 3;
2643 if (!overflowed && (n >> 3) != retval) {
2644 warn("Integer overflow in octal number");
2647 retval = n | (*s++ - '0');
2650 if (len && (*s == '8' || *s == '9')) {
2652 if (ckWARN(WARN_OCTAL))
2653 warner(WARN_OCTAL, "Illegal octal digit '%c' ignored", *s);
2655 *retlen = s - start;
2660 scan_hex(char *start, I32 len, I32 *retlen)
2662 register char *s = start;
2663 register UV retval = 0;
2664 bool overflowed = FALSE;
2668 while (len-- && *s) {
2669 tmp = strchr((char *) PL_hexdigit, *s++);
2671 if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0))
2676 if (ckWARN(WARN_UNSAFE))
2677 warner(WARN_UNSAFE,"Illegal hex digit '%c' ignored", *s);
2682 if (!overflowed && (n >> 4) != retval) {
2683 warn("Integer overflow in hex number");
2686 retval = n | ((tmp - PL_hexdigit) & 15);
2688 *retlen = s - start;
2693 find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
2696 char *xfound = Nullch;
2697 char *xfailed = Nullch;
2698 char tmpbuf[MAXPATHLEN];
2702 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2703 # define SEARCH_EXTS ".bat", ".cmd", NULL
2704 # define MAX_EXT_LEN 4
2707 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2708 # define MAX_EXT_LEN 4
2711 # define SEARCH_EXTS ".pl", ".com", NULL
2712 # define MAX_EXT_LEN 4
2714 /* additional extensions to try in each dir if scriptname not found */
2716 char *exts[] = { SEARCH_EXTS };
2717 char **ext = search_ext ? search_ext : exts;
2718 int extidx = 0, i = 0;
2719 char *curext = Nullch;
2721 # define MAX_EXT_LEN 0
2725 * If dosearch is true and if scriptname does not contain path
2726 * delimiters, search the PATH for scriptname.
2728 * If SEARCH_EXTS is also defined, will look for each
2729 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2730 * while searching the PATH.
2732 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2733 * proceeds as follows:
2734 * If DOSISH or VMSISH:
2735 * + look for ./scriptname{,.foo,.bar}
2736 * + search the PATH for scriptname{,.foo,.bar}
2739 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2740 * this will not look in '.' if it's not in the PATH)
2745 # ifdef ALWAYS_DEFTYPES
2746 len = strlen(scriptname);
2747 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2748 int hasdir, idx = 0, deftypes = 1;
2751 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2754 int hasdir, idx = 0, deftypes = 1;
2757 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2759 /* The first time through, just add SEARCH_EXTS to whatever we
2760 * already have, so we can check for default file types. */
2762 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
2768 if ((strlen(tmpbuf) + strlen(scriptname)
2769 + MAX_EXT_LEN) >= sizeof tmpbuf)
2770 continue; /* don't search dir with too-long name */
2771 strcat(tmpbuf, scriptname);
2775 if (strEQ(scriptname, "-"))
2777 if (dosearch) { /* Look in '.' first. */
2778 char *cur = scriptname;
2780 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2782 if (strEQ(ext[i++],curext)) {
2783 extidx = -1; /* already has an ext */
2788 DEBUG_p(PerlIO_printf(Perl_debug_log,
2789 "Looking for %s\n",cur));
2790 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2791 && !S_ISDIR(PL_statbuf.st_mode)) {
2799 if (cur == scriptname) {
2800 len = strlen(scriptname);
2801 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
2803 cur = strcpy(tmpbuf, scriptname);
2805 } while (extidx >= 0 && ext[extidx] /* try an extension? */
2806 && strcpy(tmpbuf+len, ext[extidx++]));
2811 if (dosearch && !strchr(scriptname, '/')
2813 && !strchr(scriptname, '\\')
2815 && (s = PerlEnv_getenv("PATH"))) {
2818 PL_bufend = s + strlen(s);
2819 while (s < PL_bufend) {
2820 #if defined(atarist) || defined(DOSISH)
2825 && *s != ';'; len++, s++) {
2826 if (len < sizeof tmpbuf)
2829 if (len < sizeof tmpbuf)
2831 #else /* ! (atarist || DOSISH) */
2832 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
2835 #endif /* ! (atarist || DOSISH) */
2838 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
2839 continue; /* don't search dir with too-long name */
2841 #if defined(atarist) || defined(__MINT__) || defined(DOSISH)
2842 && tmpbuf[len - 1] != '/'
2843 && tmpbuf[len - 1] != '\\'
2846 tmpbuf[len++] = '/';
2847 if (len == 2 && tmpbuf[0] == '.')
2849 (void)strcpy(tmpbuf + len, scriptname);
2853 len = strlen(tmpbuf);
2854 if (extidx > 0) /* reset after previous loop */
2858 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
2859 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
2860 if (S_ISDIR(PL_statbuf.st_mode)) {
2864 } while ( retval < 0 /* not there */
2865 && extidx>=0 && ext[extidx] /* try an extension? */
2866 && strcpy(tmpbuf+len, ext[extidx++])
2871 if (S_ISREG(PL_statbuf.st_mode)
2872 && cando(S_IRUSR,TRUE,&PL_statbuf)
2874 && cando(S_IXUSR,TRUE,&PL_statbuf)
2878 xfound = tmpbuf; /* bingo! */
2882 xfailed = savepv(tmpbuf);
2885 if (!xfound && !seen_dot && !xfailed &&
2886 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
2887 || S_ISDIR(PL_statbuf.st_mode)))
2889 seen_dot = 1; /* Disable message. */
2891 if (flags & 1) { /* do or die? */
2892 croak("Can't %s %s%s%s",
2893 (xfailed ? "execute" : "find"),
2894 (xfailed ? xfailed : scriptname),
2895 (xfailed ? "" : " on PATH"),
2896 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2898 scriptname = Nullch;
2902 scriptname = xfound;
2904 return (scriptname ? savepv(scriptname) : Nullch);
2910 /* Very simplistic scheduler for now */
2914 thr = thr->i.next_run;
2918 perl_cond_init(perl_cond *cp)
2924 perl_cond_signal(perl_cond *cp)
2927 perl_cond cond = *cp;
2932 /* Insert t in the runnable queue just ahead of us */
2933 t->i.next_run = thr->i.next_run;
2934 thr->i.next_run->i.prev_run = t;
2935 t->i.prev_run = thr;
2936 thr->i.next_run = t;
2937 thr->i.wait_queue = 0;
2938 /* Remove from the wait queue */
2944 perl_cond_broadcast(perl_cond *cp)
2947 perl_cond cond, cond_next;
2949 for (cond = *cp; cond; cond = cond_next) {
2951 /* Insert t in the runnable queue just ahead of us */
2952 t->i.next_run = thr->i.next_run;
2953 thr->i.next_run->i.prev_run = t;
2954 t->i.prev_run = thr;
2955 thr->i.next_run = t;
2956 thr->i.wait_queue = 0;
2957 /* Remove from the wait queue */
2958 cond_next = cond->next;
2965 perl_cond_wait(perl_cond *cp)
2969 if (thr->i.next_run == thr)
2970 croak("panic: perl_cond_wait called by last runnable thread");
2972 New(666, cond, 1, struct perl_wait_queue);
2976 thr->i.wait_queue = cond;
2977 /* Remove ourselves from runnable queue */
2978 thr->i.next_run->i.prev_run = thr->i.prev_run;
2979 thr->i.prev_run->i.next_run = thr->i.next_run;
2981 #endif /* FAKE_THREADS */
2983 #ifdef PTHREAD_GETSPECIFIC_INT
2984 struct perl_thread *
2989 if (pthread_getspecific(PL_thr_key, &t))
2990 croak("panic: pthread_getspecific");
2991 return (struct perl_thread *) t;
2996 condpair_magic(SV *sv)
3000 SvUPGRADE(sv, SVt_PVMG);
3001 mg = mg_find(sv, 'm');
3005 New(53, cp, 1, condpair_t);
3006 MUTEX_INIT(&cp->mutex);
3007 COND_INIT(&cp->owner_cond);
3008 COND_INIT(&cp->cond);
3010 MUTEX_LOCK(&PL_cred_mutex); /* XXX need separate mutex? */
3011 mg = mg_find(sv, 'm');
3013 /* someone else beat us to initialising it */
3014 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */
3015 MUTEX_DESTROY(&cp->mutex);
3016 COND_DESTROY(&cp->owner_cond);
3017 COND_DESTROY(&cp->cond);
3021 sv_magic(sv, Nullsv, 'm', 0, 0);
3023 mg->mg_ptr = (char *)cp;
3024 mg->mg_len = sizeof(cp);
3025 MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */
3026 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
3027 "%p: condpair_magic %p\n", thr, sv));)
3034 * Make a new perl thread structure using t as a prototype. Some of the
3035 * fields for the new thread are copied from the prototype thread, t,
3036 * so t should not be running in perl at the time this function is
3037 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
3038 * thread calling new_struct_thread) clearly satisfies this constraint.
3040 struct perl_thread *
3041 new_struct_thread(struct perl_thread *t)
3043 struct perl_thread *thr;
3048 sv = newSVpvn("", 0);
3049 SvGROW(sv, sizeof(struct perl_thread) + 1);
3050 SvCUR_set(sv, sizeof(struct perl_thread));
3051 thr = (Thread) SvPVX(sv);
3053 memset(thr, 0xab, sizeof(struct perl_thread));
3060 Zero(&PL_hv_fetch_ent_mh, 1, HE);
3062 Zero(thr, 1, struct perl_thread);
3065 PL_protect = FUNC_NAME_TO_PTR(default_protect);
3070 PL_curcop = &PL_compiling;
3071 thr->cvcache = newHV();
3072 thr->threadsv = newAV();
3073 thr->specific = newAV();
3074 thr->errsv = newSVpvn("", 0);
3075 thr->errhv = newHV();
3076 thr->flags = THRf_R_JOINABLE;
3077 MUTEX_INIT(&thr->mutex);
3079 /* top_env needs to be non-zero. It points to an area
3080 in which longjmp() stuff is stored, as C callstack
3081 info there at least is thread specific this has to
3082 be per-thread. Otherwise a 'die' in a thread gives
3083 that thread the C stack of last thread to do an eval {}!
3084 See comments in scope.h
3085 Initialize top entry (as in perl.c for main thread)
3087 PL_start_env.je_prev = NULL;
3088 PL_start_env.je_ret = -1;
3089 PL_start_env.je_mustcatch = TRUE;
3090 PL_top_env = &PL_start_env;
3092 PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR) */
3095 PL_statname = NEWSV(66,0);
3097 PL_regcompp = FUNC_NAME_TO_PTR(pregcomp);
3098 PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags);
3100 PL_reginterp_cnt = 0;
3101 PL_lastscream = Nullsv;
3104 PL_reg_start_tmp = 0;
3105 PL_reg_start_tmpl = 0;
3107 /* parent thread's data needs to be locked while we make copy */
3108 MUTEX_LOCK(&t->mutex);
3110 PL_protect = t->Tprotect;
3112 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
3113 PL_defstash = t->Tdefstash; /* XXX maybe these should */
3114 PL_curstash = t->Tcurstash; /* always be set to main? */
3116 PL_tainted = t->Ttainted;
3117 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
3118 PL_nrs = newSVsv(t->Tnrs);
3119 PL_rs = SvREFCNT_inc(PL_nrs);
3120 PL_last_in_gv = Nullgv;
3121 PL_ofslen = t->Tofslen;
3122 PL_ofs = savepvn(t->Tofs, PL_ofslen);
3123 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
3124 PL_chopset = t->Tchopset;
3125 PL_formtarget = newSVsv(t->Tformtarget);
3126 PL_bodytarget = newSVsv(t->Tbodytarget);
3127 PL_toptarget = newSVsv(t->Ttoptarget);
3129 /* Initialise all per-thread SVs that the template thread used */
3130 svp = AvARRAY(t->threadsv);
3131 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
3132 if (*svp && *svp != &PL_sv_undef) {
3133 SV *sv = newSVsv(*svp);
3134 av_store(thr->threadsv, i, sv);
3135 sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1);
3136 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
3137 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
3140 thr->threadsvp = AvARRAY(thr->threadsv);
3142 MUTEX_LOCK(&PL_threads_mutex);
3144 thr->tid = ++PL_threadnum;
3145 thr->next = t->next;
3148 thr->next->prev = thr;
3149 MUTEX_UNLOCK(&PL_threads_mutex);
3151 /* done copying parent's state */
3152 MUTEX_UNLOCK(&t->mutex);
3154 #ifdef HAVE_THREAD_INTERN
3155 init_thread_intern(thr);
3156 #endif /* HAVE_THREAD_INTERN */
3159 #endif /* USE_THREADS */
3163 * This hack is to force load of "huge" support from libm.a
3164 * So it is in perl for (say) POSIX to use.
3165 * Needed for SunOS with Sun's 'acc' for example.
3174 #ifdef PERL_GLOBAL_STRUCT
3197 return (char*)PL_no_modify;
3207 get_specialsv_list(void)
3209 return PL_specialsv_list;
3212 #ifndef HAS_GETENV_LEN
3214 getenv_len(char *env_elem, unsigned long *len)
3216 char *env_trans = PerlEnv_getenv(env_elem);
3218 *len = strlen(env_trans);
3225 get_vtbl(int vtbl_id)
3227 MGVTBL* result = Null(MGVTBL*);
3231 result = &PL_vtbl_sv;
3234 result = &PL_vtbl_env;
3236 case want_vtbl_envelem:
3237 result = &PL_vtbl_envelem;
3240 result = &PL_vtbl_sig;
3242 case want_vtbl_sigelem:
3243 result = &PL_vtbl_sigelem;
3245 case want_vtbl_pack:
3246 result = &PL_vtbl_pack;
3248 case want_vtbl_packelem:
3249 result = &PL_vtbl_packelem;
3251 case want_vtbl_dbline:
3252 result = &PL_vtbl_dbline;
3255 result = &PL_vtbl_isa;
3257 case want_vtbl_isaelem:
3258 result = &PL_vtbl_isaelem;
3260 case want_vtbl_arylen:
3261 result = &PL_vtbl_arylen;
3263 case want_vtbl_glob:
3264 result = &PL_vtbl_glob;
3266 case want_vtbl_mglob:
3267 result = &PL_vtbl_mglob;
3269 case want_vtbl_nkeys:
3270 result = &PL_vtbl_nkeys;
3272 case want_vtbl_taint:
3273 result = &PL_vtbl_taint;
3275 case want_vtbl_substr:
3276 result = &PL_vtbl_substr;
3279 result = &PL_vtbl_vec;
3282 result = &PL_vtbl_pos;
3285 result = &PL_vtbl_bm;
3288 result = &PL_vtbl_fm;
3290 case want_vtbl_uvar:
3291 result = &PL_vtbl_uvar;
3294 case want_vtbl_mutex:
3295 result = &PL_vtbl_mutex;
3298 case want_vtbl_defelem:
3299 result = &PL_vtbl_defelem;
3301 case want_vtbl_regexp:
3302 result = &PL_vtbl_regexp;
3304 case want_vtbl_regdata:
3305 result = &PL_vtbl_regdata;
3307 case want_vtbl_regdatum:
3308 result = &PL_vtbl_regdatum;
3310 #ifdef USE_LOCALE_COLLATE
3311 case want_vtbl_collxfrm:
3312 result = &PL_vtbl_collxfrm;
3315 case want_vtbl_amagic:
3316 result = &PL_vtbl_amagic;
3318 case want_vtbl_amagicelem:
3319 result = &PL_vtbl_amagicelem;
3321 case want_vtbl_backref:
3322 result = &PL_vtbl_backref;
3332 return PerlIO_flush(NULL);
3335 # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3336 # ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3337 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3339 # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3340 open_max = sysconf(_SC_OPEN_MAX);
3343 open_max = FOPEN_MAX;
3346 open_max = OPEN_MAX;
3357 for (i = 0; i < open_max; i++)
3358 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3359 STDIO_STREAM_ARRAY[i]._file < open_max &&
3360 STDIO_STREAM_ARRAY[i]._flag)
3361 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3365 SETERRNO(EBADF,RMS$_IFI);