3 * Copyright (c) 1991-2001, 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
16 #define PERL_IN_UTIL_C
20 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
25 # define SIG_ERR ((Sighandler_t) -1)
30 # include <sys/wait.h>
37 long xcount[MAXXCOUNT];
38 long lastxcount[MAXXCOUNT];
39 long xycount[MAXXCOUNT][MAXYCOUNT];
40 long lastxycount[MAXXCOUNT][MAXYCOUNT];
44 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
45 # define FD_CLOEXEC 1 /* NeXT needs this */
48 /* NOTE: Do not call the next three routines directly. Use the macros
49 * in handy.h, so that we can easily redefine everything to do tracking of
50 * allocated hunks back to the original New to track down any memory leaks.
51 * XXX This advice seems to be widely ignored :-( --AD August 1996.
54 /* paranoid version of system's malloc() */
57 Perl_safesysmalloc(MEM_SIZE size)
63 PerlIO_printf(Perl_error_log,
64 "Allocation too large: %lx\n", size) FLUSH;
67 #endif /* HAS_64K_LIMIT */
70 Perl_croak_nocontext("panic: malloc");
72 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
73 PERL_ALLOC_CHECK(ptr);
74 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
80 PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH;
87 /* paranoid version of system's realloc() */
90 Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
94 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO)
95 Malloc_t PerlMem_realloc();
96 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
100 PerlIO_printf(Perl_error_log,
101 "Reallocation too large: %lx\n", size) FLUSH;
104 #endif /* HAS_64K_LIMIT */
111 return safesysmalloc(size);
114 Perl_croak_nocontext("panic: realloc");
116 ptr = (Malloc_t)PerlMem_realloc(where,size);
117 PERL_ALLOC_CHECK(ptr);
119 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
120 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
127 PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH;
134 /* safe version of system's free() */
137 Perl_safesysfree(Malloc_t where)
139 #ifdef PERL_IMPLICIT_SYS
142 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
149 /* safe version of system's calloc() */
152 Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
158 if (size * count > 0xffff) {
159 PerlIO_printf(Perl_error_log,
160 "Allocation too large: %lx\n", size * count) FLUSH;
163 #endif /* HAS_64K_LIMIT */
165 if ((long)size < 0 || (long)count < 0)
166 Perl_croak_nocontext("panic: calloc");
169 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
170 PERL_ALLOC_CHECK(ptr);
171 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)size));
173 memset((void*)ptr, 0, size);
179 PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH;
188 struct mem_test_strut {
196 # define ALIGN sizeof(struct mem_test_strut)
198 # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
199 # define typeof_chunk(ch) \
200 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
201 # define set_typeof_chunk(ch,t) \
202 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
203 #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
206 ? ((size) - 1)/8 + 5 \
210 Perl_safexmalloc(I32 x, MEM_SIZE size)
212 register char* where = (char*)safemalloc(size + ALIGN);
215 xycount[x][SIZE_TO_Y(size)]++;
216 set_typeof_chunk(where, x);
217 sizeof_chunk(where) = size;
218 return (Malloc_t)(where + ALIGN);
222 Perl_safexrealloc(Malloc_t wh, MEM_SIZE size)
224 char *where = (char*)wh;
227 return safexmalloc(0,size);
230 MEM_SIZE old = sizeof_chunk(where - ALIGN);
231 int t = typeof_chunk(where - ALIGN);
232 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
234 xycount[t][SIZE_TO_Y(old)]--;
235 xycount[t][SIZE_TO_Y(size)]++;
236 xcount[t] += size - old;
237 sizeof_chunk(new) = size;
238 return (Malloc_t)(new + ALIGN);
243 Perl_safexfree(Malloc_t wh)
246 char *where = (char*)wh;
252 size = sizeof_chunk(where);
253 x = where[0] + 100 * where[1];
255 xycount[x][SIZE_TO_Y(size)]--;
260 Perl_safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
262 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
264 xycount[x][SIZE_TO_Y(size)]++;
265 memset((void*)(where + ALIGN), 0, size * count);
266 set_typeof_chunk(where, x);
267 sizeof_chunk(where) = size;
268 return (Malloc_t)(where + ALIGN);
272 S_xstat(pTHX_ int flag)
274 register I32 i, j, total = 0;
275 I32 subtot[MAXYCOUNT];
277 for (j = 0; j < MAXYCOUNT; j++) {
281 PerlIO_printf(Perl_debug_log, " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
282 for (i = 0; i < MAXXCOUNT; i++) {
284 for (j = 0; j < MAXYCOUNT; j++) {
285 subtot[j] += xycount[i][j];
288 ? xcount[i] /* Have something */
290 ? xcount[i] != lastxcount[i] /* Changed */
291 : xcount[i] > lastxcount[i])) { /* Growed */
292 PerlIO_printf(Perl_debug_log,"%2d %02d %7ld ", i / 100, i % 100,
293 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
294 lastxcount[i] = xcount[i];
295 for (j = 0; j < MAXYCOUNT; j++) {
297 ? xycount[i][j] /* Have something */
299 ? xycount[i][j] != lastxycount[i][j] /* Changed */
300 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
301 PerlIO_printf(Perl_debug_log,"%3ld ",
303 ? xycount[i][j] - lastxycount[i][j]
305 lastxycount[i][j] = xycount[i][j];
307 PerlIO_printf(Perl_debug_log, " . ", xycount[i][j]);
310 PerlIO_printf(Perl_debug_log, "\n");
314 PerlIO_printf(Perl_debug_log, "Total %7ld ", total);
315 for (j = 0; j < MAXYCOUNT; j++) {
317 PerlIO_printf(Perl_debug_log, "%3ld ", subtot[j]);
319 PerlIO_printf(Perl_debug_log, " . ");
322 PerlIO_printf(Perl_debug_log, "\n");
326 #endif /* LEAKTEST */
328 /* These must be defined when not using Perl's malloc for binary
333 Malloc_t Perl_malloc (MEM_SIZE nbytes)
336 return PerlMem_malloc(nbytes);
339 Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
342 return PerlMem_calloc(elements, size);
345 Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
348 return PerlMem_realloc(where, nbytes);
351 Free_t Perl_mfree (Malloc_t where)
359 /* copy a string up to some (non-backslashed) delimiter, if any */
362 Perl_delimcpy(pTHX_ register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
365 for (tolen = 0; from < fromend; from++, tolen++) {
367 if (from[1] == delim)
376 else if (*from == delim)
387 /* return ptr to little string in big string, NULL if not found */
388 /* This routine was donated by Corey Satten. */
391 Perl_instr(pTHX_ register const char *big, register const char *little)
393 register const char *s, *x;
404 for (x=big,s=little; *s; /**/ ) {
413 return (char*)(big-1);
418 /* same as instr but allow embedded nulls */
421 Perl_ninstr(pTHX_ register const char *big, register const char *bigend, const char *little, const char *lend)
423 register const char *s, *x;
424 register I32 first = *little;
425 register const char *littleend = lend;
427 if (!first && little >= littleend)
429 if (bigend - big < littleend - little)
431 bigend -= littleend - little++;
432 while (big <= bigend) {
435 for (x=big,s=little; s < littleend; /**/ ) {
442 return (char*)(big-1);
447 /* reverse of the above--find last substring */
450 Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend)
452 register const char *bigbeg;
453 register const char *s, *x;
454 register I32 first = *little;
455 register const char *littleend = lend;
457 if (!first && little >= littleend)
458 return (char*)bigend;
460 big = bigend - (littleend - little++);
461 while (big >= bigbeg) {
464 for (x=big+2,s=little; s < littleend; /**/ ) {
471 return (char*)(big+1);
476 #define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/
478 /* As a space optimization, we do not compile tables for strings of length
479 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
480 special-cased in fbm_instr().
482 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
485 =for apidoc fbm_compile
487 Analyses the string in order to make fast searches on it using fbm_instr()
488 -- the Boyer-Moore algorithm.
494 Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
503 if (flags & FBMcf_TAIL)
504 sv_catpvn(sv, "\n", 1); /* Taken into account in fbm_instr() */
505 s = (U8*)SvPV_force(sv, len);
506 (void)SvUPGRADE(sv, SVt_PVBM);
507 if (len == 0) /* TAIL might be on a zero-length string. */
517 Sv_Grow(sv, len + 256 + FBM_TABLE_OFFSET);
518 table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET);
519 s = table - 1 - FBM_TABLE_OFFSET; /* last char */
520 memset((void*)table, mlen, 256);
521 table[-1] = (U8)flags;
523 sb = s - mlen + 1; /* first char (maybe) */
525 if (table[*s] == mlen)
530 sv_magic(sv, Nullsv, PERL_MAGIC_bm, Nullch, 0); /* deep magic */
533 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
534 for (i = 0; i < len; i++) {
535 if (PL_freq[s[i]] < frequency) {
537 frequency = PL_freq[s[i]];
540 BmRARE(sv) = s[rarest];
541 BmPREVIOUS(sv) = rarest;
542 BmUSEFUL(sv) = 100; /* Initial value */
543 if (flags & FBMcf_TAIL)
545 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",
546 BmRARE(sv),BmPREVIOUS(sv)));
549 /* If SvTAIL(littlestr), it has a fake '\n' at end. */
550 /* If SvTAIL is actually due to \Z or \z, this gives false positives
554 =for apidoc fbm_instr
556 Returns the location of the SV in the string delimited by C<str> and
557 C<strend>. It returns C<Nullch> if the string can't be found. The C<sv>
558 does not have to be fbm_compiled, but the search will not be as fast
565 Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
567 register unsigned char *s;
569 register unsigned char *little = (unsigned char *)SvPV(littlestr,l);
570 register STRLEN littlelen = l;
571 register I32 multiline = flags & FBMrf_MULTILINE;
573 if (bigend - big < littlelen) {
574 if ( SvTAIL(littlestr)
575 && (bigend - big == littlelen - 1)
577 || (*big == *little &&
578 memEQ((char *)big, (char *)little, littlelen - 1))))
583 if (littlelen <= 2) { /* Special-cased */
585 if (littlelen == 1) {
586 if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
587 /* Know that bigend != big. */
588 if (bigend[-1] == '\n')
589 return (char *)(bigend - 1);
590 return (char *) bigend;
598 if (SvTAIL(littlestr))
599 return (char *) bigend;
603 return (char*)big; /* Cannot be SvTAIL! */
606 if (SvTAIL(littlestr) && !multiline) {
607 if (bigend[-1] == '\n' && bigend[-2] == *little)
608 return (char*)bigend - 2;
609 if (bigend[-1] == *little)
610 return (char*)bigend - 1;
614 /* This should be better than FBM if c1 == c2, and almost
615 as good otherwise: maybe better since we do less indirection.
616 And we save a lot of memory by caching no table. */
617 register unsigned char c1 = little[0];
618 register unsigned char c2 = little[1];
623 while (s <= bigend) {
633 goto check_1char_anchor;
644 goto check_1char_anchor;
647 while (s <= bigend) {
652 goto check_1char_anchor;
661 check_1char_anchor: /* One char and anchor! */
662 if (SvTAIL(littlestr) && (*bigend == *little))
663 return (char *)bigend; /* bigend is already decremented. */
666 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
667 s = bigend - littlelen;
668 if (s >= big && bigend[-1] == '\n' && *s == *little
669 /* Automatically of length > 2 */
670 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
672 return (char*)s; /* how sweet it is */
675 && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
677 return (char*)s + 1; /* how sweet it is */
681 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
682 char *b = ninstr((char*)big,(char*)bigend,
683 (char*)little, (char*)little + littlelen);
685 if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */
686 /* Chop \n from littlestr: */
687 s = bigend - littlelen + 1;
689 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
698 { /* Do actual FBM. */
699 register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET;
700 register unsigned char *oldlittle;
702 if (littlelen > bigend - big)
704 --littlelen; /* Last char found by table lookup */
707 little += littlelen; /* last char */
714 if ((tmp = table[*s])) {
715 if ((s += tmp) < bigend)
719 else { /* less expensive than calling strncmp() */
720 register unsigned char *olds = s;
725 if (*--s == *--little)
727 s = olds + 1; /* here we pay the price for failure */
729 if (s < bigend) /* fake up continue to outer loop */
737 if ( s == bigend && (table[-1] & FBMcf_TAIL)
738 && memEQ((char *)(bigend - littlelen),
739 (char *)(oldlittle - littlelen), littlelen) )
740 return (char*)bigend - littlelen;
745 /* start_shift, end_shift are positive quantities which give offsets
746 of ends of some substring of bigstr.
747 If `last' we want the last occurence.
748 old_posp is the way of communication between consequent calls if
749 the next call needs to find the .
750 The initial *old_posp should be -1.
752 Note that we take into account SvTAIL, so one can get extra
753 optimizations if _ALL flag is set.
756 /* If SvTAIL is actually due to \Z or \z, this gives false positives
757 if PL_multiline. In fact if !PL_multiline the authoritative answer
758 is not supported yet. */
761 Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
763 register unsigned char *s, *x;
764 register unsigned char *big;
766 register I32 previous;
768 register unsigned char *little;
769 register I32 stop_pos;
770 register unsigned char *littleend;
774 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
775 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) {
777 if ( BmRARE(littlestr) == '\n'
778 && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
779 little = (unsigned char *)(SvPVX(littlestr));
780 littleend = little + SvCUR(littlestr);
787 little = (unsigned char *)(SvPVX(littlestr));
788 littleend = little + SvCUR(littlestr);
790 /* The value of pos we can start at: */
791 previous = BmPREVIOUS(littlestr);
792 big = (unsigned char *)(SvPVX(bigstr));
793 /* The value of pos we can stop at: */
794 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
795 if (previous + start_shift > stop_pos) {
797 stop_pos does not include SvTAIL in the count, so this check is incorrect
798 (I think) - see [ID 20010618.006] and t/op/study.t. HVDS 2001/06/19
801 if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */
806 while (pos < previous + start_shift) {
807 if (!(pos += PL_screamnext[pos]))
812 if (pos >= stop_pos) break;
813 if (big[pos] != first)
815 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
821 if (s == littleend) {
823 if (!last) return (char *)(big+pos);
826 } while ( pos += PL_screamnext[pos] );
828 return (char *)(big+(*old_posp));
830 if (!SvTAIL(littlestr) || (end_shift > 0))
832 /* Ignore the trailing "\n". This code is not microoptimized */
833 big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr));
834 stop_pos = littleend - little; /* Actual littlestr len */
839 && ((stop_pos == 1) ||
840 memEQ((char *)(big + 1), (char *)little, stop_pos - 1)))
846 Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len)
848 register U8 *a = (U8 *)s1;
849 register U8 *b = (U8 *)s2;
851 if (*a != *b && *a != PL_fold[*b])
859 Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len)
861 register U8 *a = (U8 *)s1;
862 register U8 *b = (U8 *)s2;
864 if (*a != *b && *a != PL_fold_locale[*b])
871 /* copy a string to a safe spot */
876 Copy a string to a safe spot. This does not use an SV.
882 Perl_savepv(pTHX_ const char *sv)
884 register char *newaddr;
886 New(902,newaddr,strlen(sv)+1,char);
887 (void)strcpy(newaddr,sv);
891 /* same thing but with a known length */
896 Copy a string to a safe spot. The C<len> indicates number of bytes to
897 copy. This does not use an SV.
903 Perl_savepvn(pTHX_ const char *sv, register I32 len)
905 register char *newaddr;
907 New(903,newaddr,len+1,char);
908 Copy(sv,newaddr,len,char); /* might not be null terminated */
909 newaddr[len] = '\0'; /* is now */
913 /* the SV for Perl_form() and mess() is not kept in an arena */
922 return sv_2mortal(newSVpvn("",0));
927 /* Create as PVMG now, to avoid any upgrading later */
929 Newz(905, any, 1, XPVMG);
930 SvFLAGS(sv) = SVt_PVMG;
931 SvANY(sv) = (void*)any;
932 SvREFCNT(sv) = 1 << 30; /* practically infinite */
937 #if defined(PERL_IMPLICIT_CONTEXT)
939 Perl_form_nocontext(const char* pat, ...)
945 retval = vform(pat, &args);
949 #endif /* PERL_IMPLICIT_CONTEXT */
952 Perl_form(pTHX_ const char* pat, ...)
957 retval = vform(pat, &args);
963 Perl_vform(pTHX_ const char *pat, va_list *args)
965 SV *sv = mess_alloc();
966 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
970 #if defined(PERL_IMPLICIT_CONTEXT)
972 Perl_mess_nocontext(const char *pat, ...)
978 retval = vmess(pat, &args);
982 #endif /* PERL_IMPLICIT_CONTEXT */
985 Perl_mess(pTHX_ const char *pat, ...)
990 retval = vmess(pat, &args);
996 S_closest_cop(pTHX_ COP *cop, OP *o)
998 /* Look for PL_op starting from o. cop is the last COP we've seen. */
1000 if (!o || o == PL_op) return cop;
1002 if (o->op_flags & OPf_KIDS) {
1004 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1008 /* If the OP_NEXTSTATE has been optimised away we can still use it
1009 * the get the file and line number. */
1011 if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
1014 /* Keep searching, and return when we've found something. */
1016 new_cop = closest_cop(cop, kid);
1017 if (new_cop) return new_cop;
1021 /* Nothing found. */
1027 Perl_vmess(pTHX_ const char *pat, va_list *args)
1029 SV *sv = mess_alloc();
1030 static char dgd[] = " during global destruction.\n";
1033 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1034 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1037 * Try and find the file and line for PL_op. This will usually be
1038 * PL_curcop, but it might be a cop that has been optimised away. We
1039 * can try to find such a cop by searching through the optree starting
1040 * from the sibling of PL_curcop.
1043 cop = closest_cop(PL_curcop, PL_curcop->op_sibling);
1044 if (!cop) cop = PL_curcop;
1047 Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf,
1048 CopFILE(cop), (IV)CopLINE(cop));
1049 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
1050 bool line_mode = (RsSIMPLE(PL_rs) &&
1051 SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
1052 Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf,
1053 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
1054 line_mode ? "line" : "chunk",
1055 (IV)IoLINES(GvIOp(PL_last_in_gv)));
1057 #ifdef USE_5005THREADS
1059 Perl_sv_catpvf(aTHX_ sv, " thread %ld", thr->tid);
1061 sv_catpv(sv, PL_dirty ? dgd : ".\n");
1067 Perl_vdie(pTHX_ const char* pat, va_list *args)
1070 int was_in_eval = PL_in_eval;
1077 DEBUG_S(PerlIO_printf(Perl_debug_log,
1078 "%p: die: curstack = %p, mainstack = %p\n",
1079 thr, PL_curstack, PL_mainstack));
1082 msv = vmess(pat, args);
1083 if (PL_errors && SvCUR(PL_errors)) {
1084 sv_catsv(PL_errors, msv);
1085 message = SvPV(PL_errors, msglen);
1086 SvCUR_set(PL_errors, 0);
1089 message = SvPV(msv,msglen);
1096 DEBUG_S(PerlIO_printf(Perl_debug_log,
1097 "%p: die: message = %s\ndiehook = %p\n",
1098 thr, message, PL_diehook));
1100 /* sv_2cv might call Perl_croak() */
1101 SV *olddiehook = PL_diehook;
1103 SAVESPTR(PL_diehook);
1104 PL_diehook = Nullsv;
1105 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1107 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1114 msg = newSVpvn(message, msglen);
1122 PUSHSTACKi(PERLSI_DIEHOOK);
1126 call_sv((SV*)cv, G_DISCARD);
1132 PL_restartop = die_where(message, msglen);
1133 DEBUG_S(PerlIO_printf(Perl_debug_log,
1134 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1135 thr, PL_restartop, was_in_eval, PL_top_env));
1136 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
1138 return PL_restartop;
1141 #if defined(PERL_IMPLICIT_CONTEXT)
1143 Perl_die_nocontext(const char* pat, ...)
1148 va_start(args, pat);
1149 o = vdie(pat, &args);
1153 #endif /* PERL_IMPLICIT_CONTEXT */
1156 Perl_die(pTHX_ const char* pat, ...)
1160 va_start(args, pat);
1161 o = vdie(pat, &args);
1167 Perl_vcroak(pTHX_ const char* pat, va_list *args)
1177 msv = vmess(pat, args);
1178 if (PL_errors && SvCUR(PL_errors)) {
1179 sv_catsv(PL_errors, msv);
1180 message = SvPV(PL_errors, msglen);
1181 SvCUR_set(PL_errors, 0);
1184 message = SvPV(msv,msglen);
1191 DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s",
1192 PTR2UV(thr), message));
1195 /* sv_2cv might call Perl_croak() */
1196 SV *olddiehook = PL_diehook;
1198 SAVESPTR(PL_diehook);
1199 PL_diehook = Nullsv;
1200 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1202 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1209 msg = newSVpvn(message, msglen);
1217 PUSHSTACKi(PERLSI_DIEHOOK);
1221 call_sv((SV*)cv, G_DISCARD);
1227 PL_restartop = die_where(message, msglen);
1231 message = SvPVx(ERRSV, msglen);
1235 /* SFIO can really mess with your errno */
1238 PerlIO *serr = Perl_error_log;
1240 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1241 (void)PerlIO_flush(serr);
1249 #if defined(PERL_IMPLICIT_CONTEXT)
1251 Perl_croak_nocontext(const char *pat, ...)
1255 va_start(args, pat);
1260 #endif /* PERL_IMPLICIT_CONTEXT */
1265 This is the XSUB-writer's interface to Perl's C<die> function.
1266 Normally use this function the same way you use the C C<printf>
1267 function. See C<warn>.
1269 If you want to throw an exception object, assign the object to
1270 C<$@> and then pass C<Nullch> to croak():
1272 errsv = get_sv("@", TRUE);
1273 sv_setsv(errsv, exception_object);
1280 Perl_croak(pTHX_ const char *pat, ...)
1283 va_start(args, pat);
1290 Perl_vwarn(pTHX_ const char* pat, va_list *args)
1299 msv = vmess(pat, args);
1300 message = SvPV(msv, msglen);
1303 /* sv_2cv might call Perl_warn() */
1304 SV *oldwarnhook = PL_warnhook;
1306 SAVESPTR(PL_warnhook);
1307 PL_warnhook = Nullsv;
1308 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1310 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1316 msg = newSVpvn(message, msglen);
1320 PUSHSTACKi(PERLSI_WARNHOOK);
1324 call_sv((SV*)cv, G_DISCARD);
1331 PerlIO *serr = Perl_error_log;
1333 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1335 DEBUG_L(*message == '!'
1336 ? (xstat(message[1]=='!'
1337 ? (message[2]=='!' ? 2 : 1)
1342 (void)PerlIO_flush(serr);
1346 #if defined(PERL_IMPLICIT_CONTEXT)
1348 Perl_warn_nocontext(const char *pat, ...)
1352 va_start(args, pat);
1356 #endif /* PERL_IMPLICIT_CONTEXT */
1361 This is the XSUB-writer's interface to Perl's C<warn> function. Use this
1362 function the same way you use the C C<printf> function. See
1369 Perl_warn(pTHX_ const char *pat, ...)
1372 va_start(args, pat);
1377 #if defined(PERL_IMPLICIT_CONTEXT)
1379 Perl_warner_nocontext(U32 err, const char *pat, ...)
1383 va_start(args, pat);
1384 vwarner(err, pat, &args);
1387 #endif /* PERL_IMPLICIT_CONTEXT */
1390 Perl_warner(pTHX_ U32 err, const char* pat,...)
1393 va_start(args, pat);
1394 vwarner(err, pat, &args);
1399 Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args)
1408 msv = vmess(pat, args);
1409 message = SvPV(msv, msglen);
1412 #ifdef USE_5005THREADS
1413 DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s", PTR2UV(thr), message));
1414 #endif /* USE_5005THREADS */
1416 /* sv_2cv might call Perl_croak() */
1417 SV *olddiehook = PL_diehook;
1419 SAVESPTR(PL_diehook);
1420 PL_diehook = Nullsv;
1421 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1423 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1429 msg = newSVpvn(message, msglen);
1433 PUSHSTACKi(PERLSI_DIEHOOK);
1437 call_sv((SV*)cv, G_DISCARD);
1443 PL_restartop = die_where(message, msglen);
1447 PerlIO *serr = Perl_error_log;
1448 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1449 (void)PerlIO_flush(serr);
1456 /* sv_2cv might call Perl_warn() */
1457 SV *oldwarnhook = PL_warnhook;
1459 SAVESPTR(PL_warnhook);
1460 PL_warnhook = Nullsv;
1461 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1463 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1469 msg = newSVpvn(message, msglen);
1473 PUSHSTACKi(PERLSI_WARNHOOK);
1477 call_sv((SV*)cv, G_DISCARD);
1484 PerlIO *serr = Perl_error_log;
1485 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1487 DEBUG_L(*message == '!'
1488 ? (xstat(message[1]=='!'
1489 ? (message[2]=='!' ? 2 : 1)
1494 (void)PerlIO_flush(serr);
1499 /* since we've already done strlen() for both nam and val
1500 * we can use that info to make things faster than
1501 * sprintf(s, "%s=%s", nam, val)
1503 #define my_setenv_format(s, nam, nlen, val, vlen) \
1504 Copy(nam, s, nlen, char); \
1506 Copy(val, s+(nlen+1), vlen, char); \
1507 *(s+(nlen+1+vlen)) = '\0'
1509 #ifdef USE_ENVIRON_ARRAY
1510 /* VMS' and EPOC's my_setenv() is in vms.c and epoc.c */
1511 #if !defined(WIN32) && !defined(NETWARE)
1513 Perl_my_setenv(pTHX_ char *nam, char *val)
1515 #ifndef PERL_USE_SAFE_PUTENV
1516 /* most putenv()s leak, so we manipulate environ directly */
1517 register I32 i=setenv_getix(nam); /* where does it go? */
1520 if (environ == PL_origenviron) { /* need we copy environment? */
1526 for (max = i; environ[max]; max++) ;
1527 tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
1528 for (j=0; j<max; j++) { /* copy environment */
1529 int len = strlen(environ[j]);
1530 tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
1531 Copy(environ[j], tmpenv[j], len+1, char);
1533 tmpenv[max] = Nullch;
1534 environ = tmpenv; /* tell exec where it is now */
1537 safesysfree(environ[i]);
1538 while (environ[i]) {
1539 environ[i] = environ[i+1];
1544 if (!environ[i]) { /* does not exist yet */
1545 environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
1546 environ[i+1] = Nullch; /* make sure it's null terminated */
1549 safesysfree(environ[i]);
1553 environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
1554 /* all that work just for this */
1555 my_setenv_format(environ[i], nam, nlen, val, vlen);
1557 #else /* PERL_USE_SAFE_PUTENV */
1558 # if defined(__CYGWIN__)
1559 setenv(nam, val, 1);
1562 int nlen = strlen(nam), vlen;
1567 new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
1568 /* all that work just for this */
1569 my_setenv_format(new_env, nam, nlen, val, vlen);
1570 (void)putenv(new_env);
1571 # endif /* __CYGWIN__ */
1572 #endif /* PERL_USE_SAFE_PUTENV */
1575 #else /* WIN32 || NETWARE */
1578 Perl_my_setenv(pTHX_ char *nam,char *val)
1580 register char *envstr;
1581 int nlen = strlen(nam), vlen;
1587 New(904, envstr, nlen+vlen+2, char);
1588 my_setenv_format(envstr, nam, nlen, val, vlen);
1589 (void)PerlEnv_putenv(envstr);
1593 #endif /* WIN32 || NETWARE */
1596 Perl_setenv_getix(pTHX_ char *nam)
1598 register I32 i, len = strlen(nam);
1600 for (i = 0; environ[i]; i++) {
1603 strnicmp(environ[i],nam,len) == 0
1605 strnEQ(environ[i],nam,len)
1607 && environ[i][len] == '=')
1608 break; /* strnEQ must come first to avoid */
1609 } /* potential SEGV's */
1613 #endif /* !VMS && !EPOC*/
1615 #ifdef UNLINK_ALL_VERSIONS
1617 Perl_unlnk(pTHX_ char *f) /* unlink all versions of a file */
1621 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
1626 /* this is a drop-in replacement for bcopy() */
1627 #if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY))
1629 Perl_my_bcopy(register const char *from,register char *to,register I32 len)
1633 if (from - to >= 0) {
1641 *(--to) = *(--from);
1647 /* this is a drop-in replacement for memset() */
1650 Perl_my_memset(register char *loc, register I32 ch, register I32 len)
1660 /* this is a drop-in replacement for bzero() */
1661 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1663 Perl_my_bzero(register char *loc, register I32 len)
1673 /* this is a drop-in replacement for memcmp() */
1674 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1676 Perl_my_memcmp(const char *s1, const char *s2, register I32 len)
1678 register U8 *a = (U8 *)s1;
1679 register U8 *b = (U8 *)s2;
1683 if (tmp = *a++ - *b++)
1688 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1692 #ifdef USE_CHAR_VSPRINTF
1697 vsprintf(char *dest, const char *pat, char *args)
1701 fakebuf._ptr = dest;
1702 fakebuf._cnt = 32767;
1706 fakebuf._flag = _IOWRT|_IOSTRG;
1707 _doprnt(pat, args, &fakebuf); /* what a kludge */
1708 (void)putc('\0', &fakebuf);
1709 #ifdef USE_CHAR_VSPRINTF
1712 return 0; /* perl doesn't use return value */
1716 #endif /* HAS_VPRINTF */
1719 #if BYTEORDER != 0x4321
1721 Perl_my_swap(pTHX_ short s)
1723 #if (BYTEORDER & 1) == 0
1726 result = ((s & 255) << 8) + ((s >> 8) & 255);
1734 Perl_my_htonl(pTHX_ long l)
1738 char c[sizeof(long)];
1741 #if BYTEORDER == 0x1234
1742 u.c[0] = (l >> 24) & 255;
1743 u.c[1] = (l >> 16) & 255;
1744 u.c[2] = (l >> 8) & 255;
1748 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1749 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
1754 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1755 u.c[o & 0xf] = (l >> s) & 255;
1763 Perl_my_ntohl(pTHX_ long l)
1767 char c[sizeof(long)];
1770 #if BYTEORDER == 0x1234
1771 u.c[0] = (l >> 24) & 255;
1772 u.c[1] = (l >> 16) & 255;
1773 u.c[2] = (l >> 8) & 255;
1777 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1778 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
1785 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1786 l |= (u.c[o & 0xf] & 255) << s;
1793 #endif /* BYTEORDER != 0x4321 */
1797 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1798 * If these functions are defined,
1799 * the BYTEORDER is neither 0x1234 nor 0x4321.
1800 * However, this is not assumed.
1804 #define HTOV(name,type) \
1806 name (register type n) \
1810 char c[sizeof(type)]; \
1814 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1815 u.c[i] = (n >> s) & 0xFF; \
1820 #define VTOH(name,type) \
1822 name (register type n) \
1826 char c[sizeof(type)]; \
1832 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1833 n += (u.c[i] & 0xFF) << s; \
1838 #if defined(HAS_HTOVS) && !defined(htovs)
1841 #if defined(HAS_HTOVL) && !defined(htovl)
1844 #if defined(HAS_VTOHS) && !defined(vtohs)
1847 #if defined(HAS_VTOHL) && !defined(vtohl)
1852 Perl_my_popen_list(pTHX_ char *mode, int n, SV **args)
1854 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) && !defined(NETWARE)
1856 register I32 This, that;
1862 PERL_FLUSHALL_FOR_CHILD;
1863 This = (*mode == 'w');
1867 taint_proper("Insecure %s%s", "EXEC");
1869 if (PerlProc_pipe(p) < 0)
1871 /* Try for another pipe pair for error return */
1872 if (PerlProc_pipe(pp) >= 0)
1874 while ((pid = PerlProc_fork()) < 0) {
1875 if (errno != EAGAIN) {
1876 PerlLIO_close(p[This]);
1878 PerlLIO_close(pp[0]);
1879 PerlLIO_close(pp[1]);
1891 /* Close parent's end of _the_ pipe */
1892 PerlLIO_close(p[THAT]);
1893 /* Close parent's end of error status pipe (if any) */
1895 PerlLIO_close(pp[0]);
1896 #if defined(HAS_FCNTL) && defined(F_SETFD)
1897 /* Close error pipe automatically if exec works */
1898 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
1901 /* Now dup our end of _the_ pipe to right position */
1902 if (p[THIS] != (*mode == 'r')) {
1903 PerlLIO_dup2(p[THIS], *mode == 'r');
1904 PerlLIO_close(p[THIS]);
1906 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1907 /* No automatic close - do it by hand */
1914 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
1920 do_aexec5(Nullsv, args-1, args-1+n, pp[1], did_pipes);
1926 do_execfree(); /* free any memory malloced by child on fork */
1927 /* Close child's end of pipe */
1928 PerlLIO_close(p[that]);
1930 PerlLIO_close(pp[1]);
1931 /* Keep the lower of the two fd numbers */
1932 if (p[that] < p[This]) {
1933 PerlLIO_dup2(p[This], p[that]);
1934 PerlLIO_close(p[This]);
1938 sv = *av_fetch(PL_fdpid,p[This],TRUE);
1940 (void)SvUPGRADE(sv,SVt_IV);
1942 PL_forkprocess = pid;
1943 /* If we managed to get status pipe check for exec fail */
1944 if (did_pipes && pid > 0) {
1948 while (n < sizeof(int)) {
1949 n1 = PerlLIO_read(pp[0],
1950 (void*)(((char*)&errkid)+n),
1956 PerlLIO_close(pp[0]);
1958 if (n) { /* Error */
1960 PerlLIO_close(p[This]);
1961 if (n != sizeof(int))
1962 Perl_croak(aTHX_ "panic: kid popen errno read");
1964 pid2 = wait4pid(pid, &status, 0);
1965 } while (pid2 == -1 && errno == EINTR);
1966 errno = errkid; /* Propagate errno from kid */
1971 PerlLIO_close(pp[0]);
1972 return PerlIO_fdopen(p[This], mode);
1974 Perl_croak(aTHX_ "List form of piped open not implemented");
1975 return (PerlIO *) NULL;
1979 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1980 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
1982 Perl_my_popen(pTHX_ char *cmd, char *mode)
1985 register I32 This, that;
1988 I32 doexec = strNE(cmd,"-");
1992 PERL_FLUSHALL_FOR_CHILD;
1995 return my_syspopen(aTHX_ cmd,mode);
1998 This = (*mode == 'w');
2000 if (doexec && PL_tainting) {
2002 taint_proper("Insecure %s%s", "EXEC");
2004 if (PerlProc_pipe(p) < 0)
2006 if (doexec && PerlProc_pipe(pp) >= 0)
2008 while ((pid = PerlProc_fork()) < 0) {
2009 if (errno != EAGAIN) {
2010 PerlLIO_close(p[This]);
2012 PerlLIO_close(pp[0]);
2013 PerlLIO_close(pp[1]);
2016 Perl_croak(aTHX_ "Can't fork");
2028 PerlLIO_close(p[THAT]);
2030 PerlLIO_close(pp[0]);
2031 #if defined(HAS_FCNTL) && defined(F_SETFD)
2032 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2035 if (p[THIS] != (*mode == 'r')) {
2036 PerlLIO_dup2(p[THIS], *mode == 'r');
2037 PerlLIO_close(p[THIS]);
2041 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2050 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2055 /* may or may not use the shell */
2056 do_exec3(cmd, pp[1], did_pipes);
2059 #endif /* defined OS2 */
2061 if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) {
2062 SvREADONLY_off(GvSV(tmpgv));
2063 sv_setiv(GvSV(tmpgv), PerlProc_getpid());
2064 SvREADONLY_on(GvSV(tmpgv));
2067 hv_clear(PL_pidstatus); /* we have no children */
2072 do_execfree(); /* free any memory malloced by child on fork */
2073 PerlLIO_close(p[that]);
2075 PerlLIO_close(pp[1]);
2076 if (p[that] < p[This]) {
2077 PerlLIO_dup2(p[This], p[that]);
2078 PerlLIO_close(p[This]);
2082 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2084 (void)SvUPGRADE(sv,SVt_IV);
2086 PL_forkprocess = pid;
2087 if (did_pipes && pid > 0) {
2091 while (n < sizeof(int)) {
2092 n1 = PerlLIO_read(pp[0],
2093 (void*)(((char*)&errkid)+n),
2099 PerlLIO_close(pp[0]);
2101 if (n) { /* Error */
2103 PerlLIO_close(p[This]);
2104 if (n != sizeof(int))
2105 Perl_croak(aTHX_ "panic: kid popen errno read");
2107 pid2 = wait4pid(pid, &status, 0);
2108 } while (pid2 == -1 && errno == EINTR);
2109 errno = errkid; /* Propagate errno from kid */
2114 PerlLIO_close(pp[0]);
2115 return PerlIO_fdopen(p[This], mode);
2118 #if defined(atarist)
2121 Perl_my_popen(pTHX_ char *cmd, char *mode)
2123 PERL_FLUSHALL_FOR_CHILD;
2124 /* Call system's popen() to get a FILE *, then import it.
2125 used 0 for 2nd parameter to PerlIO_importFILE;
2128 return PerlIO_importFILE(popen(cmd, mode), 0);
2132 FILE *djgpp_popen();
2134 Perl_my_popen(pTHX_ char *cmd, char *mode)
2136 PERL_FLUSHALL_FOR_CHILD;
2137 /* Call system's popen() to get a FILE *, then import it.
2138 used 0 for 2nd parameter to PerlIO_importFILE;
2141 return PerlIO_importFILE(djgpp_popen(cmd, mode), 0);
2146 #endif /* !DOSISH */
2148 /* this is called in parent before the fork() */
2150 Perl_atfork_lock(void)
2152 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
2153 /* locks must be held in locking order (if any) */
2155 MUTEX_LOCK(&PL_malloc_mutex);
2161 /* this is called in both parent and child after the fork() */
2163 Perl_atfork_unlock(void)
2165 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
2166 /* locks must be released in same order as in atfork_lock() */
2168 MUTEX_UNLOCK(&PL_malloc_mutex);
2177 #if defined(HAS_FORK)
2179 #if (defined(USE_5005THREADS) || defined(USE_ITHREADS)) && !defined(HAS_PTHREAD_ATFORK)
2184 /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2185 * handlers elsewhere in the code */
2190 /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2191 Perl_croak_nocontext("fork() not available");
2193 #endif /* HAS_FORK */
2198 Perl_dump_fds(pTHX_ char *s)
2201 struct stat tmpstatbuf;
2203 PerlIO_printf(Perl_debug_log,"%s", s);
2204 for (fd = 0; fd < 32; fd++) {
2205 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
2206 PerlIO_printf(Perl_debug_log," %d",fd);
2208 PerlIO_printf(Perl_debug_log,"\n");
2210 #endif /* DUMP_FDS */
2214 dup2(int oldfd, int newfd)
2216 #if defined(HAS_FCNTL) && defined(F_DUPFD)
2219 PerlLIO_close(newfd);
2220 return fcntl(oldfd, F_DUPFD, newfd);
2222 #define DUP2_MAX_FDS 256
2223 int fdtmp[DUP2_MAX_FDS];
2229 PerlLIO_close(newfd);
2230 /* good enough for low fd's... */
2231 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
2232 if (fdx >= DUP2_MAX_FDS) {
2240 PerlLIO_close(fdtmp[--fdx]);
2247 #ifdef HAS_SIGACTION
2250 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2252 struct sigaction act, oact;
2254 act.sa_handler = handler;
2255 sigemptyset(&act.sa_mask);
2258 #if defined(PERL_OLD_SIGNALS)
2259 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2263 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2264 act.sa_flags |= SA_NOCLDWAIT;
2266 if (sigaction(signo, &act, &oact) == -1)
2269 return oact.sa_handler;
2273 Perl_rsignal_state(pTHX_ int signo)
2275 struct sigaction oact;
2277 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2280 return oact.sa_handler;
2284 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
2286 struct sigaction act;
2288 act.sa_handler = handler;
2289 sigemptyset(&act.sa_mask);
2292 #if defined(PERL_OLD_SIGNALS)
2293 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2297 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2298 act.sa_flags |= SA_NOCLDWAIT;
2300 return sigaction(signo, &act, save);
2304 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
2306 return sigaction(signo, save, (struct sigaction *)NULL);
2309 #else /* !HAS_SIGACTION */
2312 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2314 return PerlProc_signal(signo, handler);
2317 static int sig_trapped; /* XXX signals are process-wide anyway, so we
2318 ignore the implications of this for threading */
2328 Perl_rsignal_state(pTHX_ int signo)
2330 Sighandler_t oldsig;
2333 oldsig = PerlProc_signal(signo, sig_trap);
2334 PerlProc_signal(signo, oldsig);
2336 PerlProc_kill(PerlProc_getpid(), signo);
2341 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
2343 *save = PerlProc_signal(signo, handler);
2344 return (*save == SIG_ERR) ? -1 : 0;
2348 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
2350 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
2353 #endif /* !HAS_SIGACTION */
2354 #endif /* !PERL_MICRO */
2356 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2357 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
2359 Perl_my_pclose(pTHX_ PerlIO *ptr)
2361 Sigsave_t hstat, istat, qstat;
2367 int saved_errno = 0;
2369 int saved_vaxc_errno;
2372 int saved_win32_errno;
2376 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
2378 pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
2380 *svp = &PL_sv_undef;
2382 if (pid == -1) { /* Opened by popen. */
2383 return my_syspclose(ptr);
2386 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2387 saved_errno = errno;
2389 saved_vaxc_errno = vaxc$errno;
2392 saved_win32_errno = GetLastError();
2396 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2399 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2400 rsignal_save(SIGINT, SIG_IGN, &istat);
2401 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2404 pid2 = wait4pid(pid, &status, 0);
2405 } while (pid2 == -1 && errno == EINTR);
2407 rsignal_restore(SIGHUP, &hstat);
2408 rsignal_restore(SIGINT, &istat);
2409 rsignal_restore(SIGQUIT, &qstat);
2412 SETERRNO(saved_errno, saved_vaxc_errno);
2415 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
2417 #endif /* !DOSISH */
2419 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(MACOS_TRADITIONAL)
2421 Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
2426 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2430 char spid[TYPE_CHARS(int)];
2433 sprintf(spid, "%"IVdf, (IV)pid);
2434 svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
2435 if (svp && *svp != &PL_sv_undef) {
2436 *statusp = SvIVX(*svp);
2437 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2444 hv_iterinit(PL_pidstatus);
2445 if ((entry = hv_iternext(PL_pidstatus))) {
2446 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2447 sv = hv_iterval(PL_pidstatus,entry);
2448 *statusp = SvIVX(sv);
2449 sprintf(spid, "%"IVdf, (IV)pid);
2450 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2457 # ifdef HAS_WAITPID_RUNTIME
2458 if (!HAS_WAITPID_RUNTIME)
2461 result = PerlProc_waitpid(pid,statusp,flags);
2463 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2464 result = wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2466 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2470 Perl_croak(aTHX_ "Can't do waitpid with flags");
2472 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
2473 pidgone(result,*statusp);
2479 if (result < 0 && errno == EINTR) {
2484 #endif /* !DOSISH || OS2 || WIN32 || NETWARE */
2488 Perl_pidgone(pTHX_ Pid_t pid, int status)
2491 char spid[TYPE_CHARS(int)];
2493 sprintf(spid, "%"IVdf, (IV)pid);
2494 sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
2495 (void)SvUPGRADE(sv,SVt_IV);
2500 #if defined(atarist) || defined(OS2)
2503 int /* Cannot prototype with I32
2505 my_syspclose(PerlIO *ptr)
2508 Perl_my_pclose(pTHX_ PerlIO *ptr)
2511 /* Needs work for PerlIO ! */
2512 FILE *f = PerlIO_findFILE(ptr);
2513 I32 result = pclose(f);
2514 PerlIO_releaseFILE(ptr,f);
2522 Perl_my_pclose(pTHX_ PerlIO *ptr)
2524 /* Needs work for PerlIO ! */
2525 FILE *f = PerlIO_findFILE(ptr);
2526 I32 result = djgpp_pclose(f);
2527 result = (result << 8) & 0xff00;
2528 PerlIO_releaseFILE(ptr,f);
2534 Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count)
2537 register const char *frombase = from;
2540 register const char c = *from;
2545 while (count-- > 0) {
2546 for (todo = len; todo > 0; todo--) {
2555 Perl_same_dirent(pTHX_ char *a, char *b)
2557 char *fa = strrchr(a,'/');
2558 char *fb = strrchr(b,'/');
2559 struct stat tmpstatbuf1;
2560 struct stat tmpstatbuf2;
2561 SV *tmpsv = sv_newmortal();
2574 sv_setpv(tmpsv, ".");
2576 sv_setpvn(tmpsv, a, fa - a);
2577 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2580 sv_setpv(tmpsv, ".");
2582 sv_setpvn(tmpsv, b, fb - b);
2583 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2585 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2586 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2588 #endif /* !HAS_RENAME */
2591 Perl_find_script(pTHX_ char *scriptname, bool dosearch, char **search_ext, I32 flags)
2593 char *xfound = Nullch;
2594 char *xfailed = Nullch;
2595 char tmpbuf[MAXPATHLEN];
2599 #if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2600 # define SEARCH_EXTS ".bat", ".cmd", NULL
2601 # define MAX_EXT_LEN 4
2604 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2605 # define MAX_EXT_LEN 4
2608 # define SEARCH_EXTS ".pl", ".com", NULL
2609 # define MAX_EXT_LEN 4
2611 /* additional extensions to try in each dir if scriptname not found */
2613 char *exts[] = { SEARCH_EXTS };
2614 char **ext = search_ext ? search_ext : exts;
2615 int extidx = 0, i = 0;
2616 char *curext = Nullch;
2618 # define MAX_EXT_LEN 0
2622 * If dosearch is true and if scriptname does not contain path
2623 * delimiters, search the PATH for scriptname.
2625 * If SEARCH_EXTS is also defined, will look for each
2626 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2627 * while searching the PATH.
2629 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2630 * proceeds as follows:
2631 * If DOSISH or VMSISH:
2632 * + look for ./scriptname{,.foo,.bar}
2633 * + search the PATH for scriptname{,.foo,.bar}
2636 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2637 * this will not look in '.' if it's not in the PATH)
2642 # ifdef ALWAYS_DEFTYPES
2643 len = strlen(scriptname);
2644 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2645 int hasdir, idx = 0, deftypes = 1;
2648 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2651 int hasdir, idx = 0, deftypes = 1;
2654 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2656 /* The first time through, just add SEARCH_EXTS to whatever we
2657 * already have, so we can check for default file types. */
2659 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
2665 if ((strlen(tmpbuf) + strlen(scriptname)
2666 + MAX_EXT_LEN) >= sizeof tmpbuf)
2667 continue; /* don't search dir with too-long name */
2668 strcat(tmpbuf, scriptname);
2672 if (strEQ(scriptname, "-"))
2674 if (dosearch) { /* Look in '.' first. */
2675 char *cur = scriptname;
2677 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2679 if (strEQ(ext[i++],curext)) {
2680 extidx = -1; /* already has an ext */
2685 DEBUG_p(PerlIO_printf(Perl_debug_log,
2686 "Looking for %s\n",cur));
2687 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2688 && !S_ISDIR(PL_statbuf.st_mode)) {
2696 if (cur == scriptname) {
2697 len = strlen(scriptname);
2698 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
2700 cur = strcpy(tmpbuf, scriptname);
2702 } while (extidx >= 0 && ext[extidx] /* try an extension? */
2703 && strcpy(tmpbuf+len, ext[extidx++]));
2708 #ifdef MACOS_TRADITIONAL
2709 if (dosearch && !strchr(scriptname, ':') &&
2710 (s = PerlEnv_getenv("Commands")))
2712 if (dosearch && !strchr(scriptname, '/')
2714 && !strchr(scriptname, '\\')
2716 && (s = PerlEnv_getenv("PATH")))
2721 PL_bufend = s + strlen(s);
2722 while (s < PL_bufend) {
2723 #ifdef MACOS_TRADITIONAL
2724 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
2728 #if defined(atarist) || defined(DOSISH)
2733 && *s != ';'; len++, s++) {
2734 if (len < sizeof tmpbuf)
2737 if (len < sizeof tmpbuf)
2739 #else /* ! (atarist || DOSISH) */
2740 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
2743 #endif /* ! (atarist || DOSISH) */
2744 #endif /* MACOS_TRADITIONAL */
2747 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
2748 continue; /* don't search dir with too-long name */
2749 #ifdef MACOS_TRADITIONAL
2750 if (len && tmpbuf[len - 1] != ':')
2751 tmpbuf[len++] = ':';
2754 #if defined(atarist) || defined(__MINT__) || defined(DOSISH)
2755 && tmpbuf[len - 1] != '/'
2756 && tmpbuf[len - 1] != '\\'
2759 tmpbuf[len++] = '/';
2760 if (len == 2 && tmpbuf[0] == '.')
2763 (void)strcpy(tmpbuf + len, scriptname);
2767 len = strlen(tmpbuf);
2768 if (extidx > 0) /* reset after previous loop */
2772 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
2773 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
2774 if (S_ISDIR(PL_statbuf.st_mode)) {
2778 } while ( retval < 0 /* not there */
2779 && extidx>=0 && ext[extidx] /* try an extension? */
2780 && strcpy(tmpbuf+len, ext[extidx++])
2785 if (S_ISREG(PL_statbuf.st_mode)
2786 && cando(S_IRUSR,TRUE,&PL_statbuf)
2787 #if !defined(DOSISH) && !defined(MACOS_TRADITIONAL)
2788 && cando(S_IXUSR,TRUE,&PL_statbuf)
2792 xfound = tmpbuf; /* bingo! */
2796 xfailed = savepv(tmpbuf);
2799 if (!xfound && !seen_dot && !xfailed &&
2800 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
2801 || S_ISDIR(PL_statbuf.st_mode)))
2803 seen_dot = 1; /* Disable message. */
2805 if (flags & 1) { /* do or die? */
2806 Perl_croak(aTHX_ "Can't %s %s%s%s",
2807 (xfailed ? "execute" : "find"),
2808 (xfailed ? xfailed : scriptname),
2809 (xfailed ? "" : " on PATH"),
2810 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2812 scriptname = Nullch;
2816 scriptname = xfound;
2818 return (scriptname ? savepv(scriptname) : Nullch);
2821 #ifndef PERL_GET_CONTEXT_DEFINED
2824 Perl_get_context(void)
2826 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
2827 # ifdef OLD_PTHREADS_API
2829 if (pthread_getspecific(PL_thr_key, &t))
2830 Perl_croak_nocontext("panic: pthread_getspecific");
2833 # ifdef I_MACH_CTHREADS
2834 return (void*)cthread_data(cthread_self());
2836 return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
2845 Perl_set_context(void *t)
2847 #if defined(USE_5005THREADS) || defined(USE_ITHREADS)
2848 # ifdef I_MACH_CTHREADS
2849 cthread_set_data(cthread_self(), t);
2851 if (pthread_setspecific(PL_thr_key, t))
2852 Perl_croak_nocontext("panic: pthread_setspecific");
2857 #endif /* !PERL_GET_CONTEXT_DEFINED */
2859 #ifdef USE_5005THREADS
2862 /* Very simplistic scheduler for now */
2866 thr = thr->i.next_run;
2870 Perl_cond_init(pTHX_ perl_cond *cp)
2876 Perl_cond_signal(pTHX_ perl_cond *cp)
2879 perl_cond cond = *cp;
2884 /* Insert t in the runnable queue just ahead of us */
2885 t->i.next_run = thr->i.next_run;
2886 thr->i.next_run->i.prev_run = t;
2887 t->i.prev_run = thr;
2888 thr->i.next_run = t;
2889 thr->i.wait_queue = 0;
2890 /* Remove from the wait queue */
2896 Perl_cond_broadcast(pTHX_ perl_cond *cp)
2899 perl_cond cond, cond_next;
2901 for (cond = *cp; cond; cond = cond_next) {
2903 /* Insert t in the runnable queue just ahead of us */
2904 t->i.next_run = thr->i.next_run;
2905 thr->i.next_run->i.prev_run = t;
2906 t->i.prev_run = thr;
2907 thr->i.next_run = t;
2908 thr->i.wait_queue = 0;
2909 /* Remove from the wait queue */
2910 cond_next = cond->next;
2917 Perl_cond_wait(pTHX_ perl_cond *cp)
2921 if (thr->i.next_run == thr)
2922 Perl_croak(aTHX_ "panic: perl_cond_wait called by last runnable thread");
2924 New(666, cond, 1, struct perl_wait_queue);
2928 thr->i.wait_queue = cond;
2929 /* Remove ourselves from runnable queue */
2930 thr->i.next_run->i.prev_run = thr->i.prev_run;
2931 thr->i.prev_run->i.next_run = thr->i.next_run;
2933 #endif /* FAKE_THREADS */
2936 Perl_condpair_magic(pTHX_ SV *sv)
2940 (void)SvUPGRADE(sv, SVt_PVMG);
2941 mg = mg_find(sv, PERL_MAGIC_mutex);
2945 New(53, cp, 1, condpair_t);
2946 MUTEX_INIT(&cp->mutex);
2947 COND_INIT(&cp->owner_cond);
2948 COND_INIT(&cp->cond);
2950 LOCK_CRED_MUTEX; /* XXX need separate mutex? */
2951 mg = mg_find(sv, PERL_MAGIC_mutex);
2953 /* someone else beat us to initialising it */
2954 UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */
2955 MUTEX_DESTROY(&cp->mutex);
2956 COND_DESTROY(&cp->owner_cond);
2957 COND_DESTROY(&cp->cond);
2961 sv_magic(sv, Nullsv, PERL_MAGIC_mutex, 0, 0);
2963 mg->mg_ptr = (char *)cp;
2964 mg->mg_len = sizeof(cp);
2965 UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */
2966 DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log,
2967 "%p: condpair_magic %p\n", thr, sv)));
2974 Perl_sv_lock(pTHX_ SV *osv)
2984 mg = condpair_magic(sv);
2985 MUTEX_LOCK(MgMUTEXP(mg));
2986 if (MgOWNER(mg) == thr)
2987 MUTEX_UNLOCK(MgMUTEXP(mg));
2990 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2992 DEBUG_S(PerlIO_printf(Perl_debug_log,
2993 "0x%"UVxf": Perl_lock lock 0x%"UVxf"\n",
2994 PTR2UV(thr), PTR2UV(sv)));
2995 MUTEX_UNLOCK(MgMUTEXP(mg));
2996 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
2998 UNLOCK_SV_LOCK_MUTEX;
3003 * Make a new perl thread structure using t as a prototype. Some of the
3004 * fields for the new thread are copied from the prototype thread, t,
3005 * so t should not be running in perl at the time this function is
3006 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
3007 * thread calling new_struct_thread) clearly satisfies this constraint.
3009 struct perl_thread *
3010 Perl_new_struct_thread(pTHX_ struct perl_thread *t)
3012 #if !defined(PERL_IMPLICIT_CONTEXT)
3013 struct perl_thread *thr;
3019 sv = newSVpvn("", 0);
3020 SvGROW(sv, sizeof(struct perl_thread) + 1);
3021 SvCUR_set(sv, sizeof(struct perl_thread));
3022 thr = (Thread) SvPVX(sv);
3024 memset(thr, 0xab, sizeof(struct perl_thread));
3031 Zero(&PL_hv_fetch_ent_mh, 1, HE);
3032 PL_efloatbuf = (char*)NULL;
3035 Zero(thr, 1, struct perl_thread);
3041 PL_curcop = &PL_compiling;
3042 thr->interp = t->interp;
3043 thr->cvcache = newHV();
3044 thr->threadsv = newAV();
3045 thr->specific = newAV();
3046 thr->errsv = newSVpvn("", 0);
3047 thr->flags = THRf_R_JOINABLE;
3049 MUTEX_INIT(&thr->mutex);
3053 PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR|EVAL_INREQUIRE) */
3056 PL_statname = NEWSV(66,0);
3057 PL_errors = newSVpvn("", 0);
3059 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3060 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3061 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3062 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3063 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
3065 PL_reginterp_cnt = 0;
3066 PL_lastscream = Nullsv;
3069 PL_reg_start_tmp = 0;
3070 PL_reg_start_tmpl = 0;
3071 PL_reg_poscache = Nullch;
3073 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
3075 /* parent thread's data needs to be locked while we make copy */
3076 MUTEX_LOCK(&t->mutex);
3078 #ifdef PERL_FLEXIBLE_EXCEPTIONS
3079 PL_protect = t->Tprotect;
3082 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
3083 PL_defstash = t->Tdefstash; /* XXX maybe these should */
3084 PL_curstash = t->Tcurstash; /* always be set to main? */
3086 PL_tainted = t->Ttainted;
3087 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
3088 PL_rs = newSVsv(t->Trs);
3089 PL_last_in_gv = Nullgv;
3090 PL_ofs_sv = t->Tofs_sv ? SvREFCNT_inc(PL_ofs_sv) : Nullsv;
3091 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
3092 PL_chopset = t->Tchopset;
3093 PL_bodytarget = newSVsv(t->Tbodytarget);
3094 PL_toptarget = newSVsv(t->Ttoptarget);
3095 if (t->Tformtarget == t->Ttoptarget)
3096 PL_formtarget = PL_toptarget;
3098 PL_formtarget = PL_bodytarget;
3100 /* Initialise all per-thread SVs that the template thread used */
3101 svp = AvARRAY(t->threadsv);
3102 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
3103 if (*svp && *svp != &PL_sv_undef) {
3104 SV *sv = newSVsv(*svp);
3105 av_store(thr->threadsv, i, sv);
3106 sv_magic(sv, 0, PERL_MAGIC_sv, &PL_threadsv_names[i], 1);
3107 DEBUG_S(PerlIO_printf(Perl_debug_log,
3108 "new_struct_thread: copied threadsv %"IVdf" %p->%p\n",
3112 thr->threadsvp = AvARRAY(thr->threadsv);
3114 MUTEX_LOCK(&PL_threads_mutex);
3116 thr->tid = ++PL_threadnum;
3117 thr->next = t->next;
3120 thr->next->prev = thr;
3121 MUTEX_UNLOCK(&PL_threads_mutex);
3123 /* done copying parent's state */
3124 MUTEX_UNLOCK(&t->mutex);
3126 #ifdef HAVE_THREAD_INTERN
3127 Perl_init_thread_intern(thr);
3128 #endif /* HAVE_THREAD_INTERN */
3131 #endif /* USE_5005THREADS */
3133 #ifdef PERL_GLOBAL_STRUCT
3142 Perl_get_op_names(pTHX)
3148 Perl_get_op_descs(pTHX)
3154 Perl_get_no_modify(pTHX)
3156 return (char*)PL_no_modify;
3160 Perl_get_opargs(pTHX)
3166 Perl_get_ppaddr(pTHX)
3168 return (PPADDR_t*)PL_ppaddr;
3171 #ifndef HAS_GETENV_LEN
3173 Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
3175 char *env_trans = PerlEnv_getenv(env_elem);
3177 *len = strlen(env_trans);
3184 Perl_get_vtbl(pTHX_ int vtbl_id)
3186 MGVTBL* result = Null(MGVTBL*);
3190 result = &PL_vtbl_sv;
3193 result = &PL_vtbl_env;
3195 case want_vtbl_envelem:
3196 result = &PL_vtbl_envelem;
3199 result = &PL_vtbl_sig;
3201 case want_vtbl_sigelem:
3202 result = &PL_vtbl_sigelem;
3204 case want_vtbl_pack:
3205 result = &PL_vtbl_pack;
3207 case want_vtbl_packelem:
3208 result = &PL_vtbl_packelem;
3210 case want_vtbl_dbline:
3211 result = &PL_vtbl_dbline;
3214 result = &PL_vtbl_isa;
3216 case want_vtbl_isaelem:
3217 result = &PL_vtbl_isaelem;
3219 case want_vtbl_arylen:
3220 result = &PL_vtbl_arylen;
3222 case want_vtbl_glob:
3223 result = &PL_vtbl_glob;
3225 case want_vtbl_mglob:
3226 result = &PL_vtbl_mglob;
3228 case want_vtbl_nkeys:
3229 result = &PL_vtbl_nkeys;
3231 case want_vtbl_taint:
3232 result = &PL_vtbl_taint;
3234 case want_vtbl_substr:
3235 result = &PL_vtbl_substr;
3238 result = &PL_vtbl_vec;
3241 result = &PL_vtbl_pos;
3244 result = &PL_vtbl_bm;
3247 result = &PL_vtbl_fm;
3249 case want_vtbl_uvar:
3250 result = &PL_vtbl_uvar;
3252 #ifdef USE_5005THREADS
3253 case want_vtbl_mutex:
3254 result = &PL_vtbl_mutex;
3257 case want_vtbl_defelem:
3258 result = &PL_vtbl_defelem;
3260 case want_vtbl_regexp:
3261 result = &PL_vtbl_regexp;
3263 case want_vtbl_regdata:
3264 result = &PL_vtbl_regdata;
3266 case want_vtbl_regdatum:
3267 result = &PL_vtbl_regdatum;
3269 #ifdef USE_LOCALE_COLLATE
3270 case want_vtbl_collxfrm:
3271 result = &PL_vtbl_collxfrm;
3274 case want_vtbl_amagic:
3275 result = &PL_vtbl_amagic;
3277 case want_vtbl_amagicelem:
3278 result = &PL_vtbl_amagicelem;
3280 case want_vtbl_backref:
3281 result = &PL_vtbl_backref;
3288 Perl_my_fflush_all(pTHX)
3290 #if defined(FFLUSH_NULL)
3291 return PerlIO_flush(NULL);
3293 # if defined(HAS__FWALK)
3294 /* undocumented, unprototyped, but very useful BSDism */
3295 extern void _fwalk(int (*)(FILE *));
3299 # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3301 # ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3302 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3304 # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3305 open_max = sysconf(_SC_OPEN_MAX);
3308 open_max = FOPEN_MAX;
3311 open_max = OPEN_MAX;
3322 for (i = 0; i < open_max; i++)
3323 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3324 STDIO_STREAM_ARRAY[i]._file < open_max &&
3325 STDIO_STREAM_ARRAY[i]._flag)
3326 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3330 SETERRNO(EBADF,RMS$_IFI);
3337 Perl_report_evil_fh(pTHX_ GV *gv, IO *io, I32 op)
3342 op == OP_READLINE ? "readline" : /* "<HANDLE>" not nice */
3343 op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */
3345 char *pars = OP_IS_FILETEST(op) ? "" : "()";
3346 char *type = OP_IS_SOCKET(op) ||
3347 (gv && io && IoTYPE(io) == IoTYPE_SOCKET) ?
3348 "socket" : "filehandle";
3351 if (gv && io && IoTYPE(io) == IoTYPE_CLOSED) {
3353 warn_type = WARN_CLOSED;
3357 warn_type = WARN_UNOPENED;
3360 if (gv && isGV(gv)) {
3361 SV *sv = sv_newmortal();
3362 gv_efullname4(sv, gv, Nullch, FALSE);
3366 if (op == OP_phoney_OUTPUT_ONLY || op == OP_phoney_INPUT_ONLY) {
3368 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for %sput",
3370 (op == OP_phoney_INPUT_ONLY ? "in" : "out"));
3372 Perl_warner(aTHX_ WARN_IO, "Filehandle opened only for %sput",
3373 (op == OP_phoney_INPUT_ONLY ? "in" : "out"));
3374 } else if (name && *name) {
3375 Perl_warner(aTHX_ warn_type,
3376 "%s%s on %s %s %s", func, pars, vile, type, name);
3377 if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3378 Perl_warner(aTHX_ warn_type,
3379 "\t(Are you trying to call %s%s on dirhandle %s?)\n",
3383 Perl_warner(aTHX_ warn_type,
3384 "%s%s on %s %s", func, pars, vile, type);
3385 if (gv && io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3386 Perl_warner(aTHX_ warn_type,
3387 "\t(Are you trying to call %s%s on dirhandle?)\n",
3393 /* in ASCII order, not that it matters */
3394 static const char controllablechars[] = "?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
3397 Perl_ebcdic_control(pTHX_ int ch)
3405 if ((ctlp = strchr(controllablechars, ch)) == 0) {
3406 Perl_die(aTHX_ "unrecognised control character '%c'\n", ch);
3409 if (ctlp == controllablechars)
3410 return('\177'); /* DEL */
3412 return((unsigned char)(ctlp - controllablechars - 1));
3413 } else { /* Want uncontrol */
3414 if (ch == '\177' || ch == -1)
3416 else if (ch == '\157')
3418 else if (ch == '\174')
3420 else if (ch == '^') /* '\137' in 1047, '\260' in 819 */
3422 else if (ch == '\155')
3424 else if (0 < ch && ch < (sizeof(controllablechars) - 1))
3425 return(controllablechars[ch+1]);
3427 Perl_die(aTHX_ "invalid control request: '\\%03o'\n", ch & 0xFF);
3432 /* XXX struct tm on some systems (SunOS4/BSD) contains extra (non POSIX)
3433 * fields for which we don't have Configure support yet:
3434 * char *tm_zone; -- abbreviation of timezone name
3435 * long tm_gmtoff; -- offset from GMT in seconds
3436 * To workaround core dumps from the uninitialised tm_zone we get the
3437 * system to give us a reasonable struct to copy. This fix means that
3438 * strftime uses the tm_zone and tm_gmtoff values returned by
3439 * localtime(time()). That should give the desired result most of the
3440 * time. But probably not always!
3442 * This is a temporary workaround to be removed once Configure
3443 * support is added and NETaa14816 is considered in full.
3444 * It does not address tzname aspects of NETaa14816.
3447 # ifndef STRUCT_TM_HASZONE
3448 # define STRUCT_TM_HASZONE
3453 Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */
3455 #ifdef STRUCT_TM_HASZONE
3458 Copy(localtime(&now), ptm, 1, struct tm);
3463 * mini_mktime - normalise struct tm values without the localtime()
3464 * semantics (and overhead) of mktime().
3467 Perl_mini_mktime(pTHX_ struct tm *ptm)
3471 int month, mday, year, jday;
3472 int odd_cent, odd_year;
3474 #define DAYS_PER_YEAR 365
3475 #define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1)
3476 #define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1)
3477 #define DAYS_PER_QCENT (4*DAYS_PER_CENT+1)
3478 #define SECS_PER_HOUR (60*60)
3479 #define SECS_PER_DAY (24*SECS_PER_HOUR)
3480 /* parentheses deliberately absent on these two, otherwise they don't work */
3481 #define MONTH_TO_DAYS 153/5
3482 #define DAYS_TO_MONTH 5/153
3483 /* offset to bias by March (month 4) 1st between month/mday & year finding */
3484 #define YEAR_ADJUST (4*MONTH_TO_DAYS+1)
3485 /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */
3486 #define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */
3489 * Year/day algorithm notes:
3491 * With a suitable offset for numeric value of the month, one can find
3492 * an offset into the year by considering months to have 30.6 (153/5) days,
3493 * using integer arithmetic (i.e., with truncation). To avoid too much
3494 * messing about with leap days, we consider January and February to be
3495 * the 13th and 14th month of the previous year. After that transformation,
3496 * we need the month index we use to be high by 1 from 'normal human' usage,
3497 * so the month index values we use run from 4 through 15.
3499 * Given that, and the rules for the Gregorian calendar (leap years are those
3500 * divisible by 4 unless also divisible by 100, when they must be divisible
3501 * by 400 instead), we can simply calculate the number of days since some
3502 * arbitrary 'beginning of time' by futzing with the (adjusted) year number,
3503 * the days we derive from our month index, and adding in the day of the
3504 * month. The value used here is not adjusted for the actual origin which
3505 * it normally would use (1 January A.D. 1), since we're not exposing it.
3506 * We're only building the value so we can turn around and get the
3507 * normalised values for the year, month, day-of-month, and day-of-year.
3509 * For going backward, we need to bias the value we're using so that we find
3510 * the right year value. (Basically, we don't want the contribution of
3511 * March 1st to the number to apply while deriving the year). Having done
3512 * that, we 'count up' the contribution to the year number by accounting for
3513 * full quadracenturies (400-year periods) with their extra leap days, plus
3514 * the contribution from full centuries (to avoid counting in the lost leap
3515 * days), plus the contribution from full quad-years (to count in the normal
3516 * leap days), plus the leftover contribution from any non-leap years.
3517 * At this point, if we were working with an actual leap day, we'll have 0
3518 * days left over. This is also true for March 1st, however. So, we have
3519 * to special-case that result, and (earlier) keep track of the 'odd'
3520 * century and year contributions. If we got 4 extra centuries in a qcent,
3521 * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb.
3522 * Otherwise, we add back in the earlier bias we removed (the 123 from
3523 * figuring in March 1st), find the month index (integer division by 30.6),
3524 * and the remainder is the day-of-month. We then have to convert back to
3525 * 'real' months (including fixing January and February from being 14/15 in
3526 * the previous year to being in the proper year). After that, to get
3527 * tm_yday, we work with the normalised year and get a new yearday value for
3528 * January 1st, which we subtract from the yearday value we had earlier,
3529 * representing the date we've re-built. This is done from January 1
3530 * because tm_yday is 0-origin.
3532 * Since POSIX time routines are only guaranteed to work for times since the
3533 * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm
3534 * applies Gregorian calendar rules even to dates before the 16th century
3535 * doesn't bother me. Besides, you'd need cultural context for a given
3536 * date to know whether it was Julian or Gregorian calendar, and that's
3537 * outside the scope for this routine. Since we convert back based on the
3538 * same rules we used to build the yearday, you'll only get strange results
3539 * for input which needed normalising, or for the 'odd' century years which
3540 * were leap years in the Julian calander but not in the Gregorian one.
3541 * I can live with that.
3543 * This algorithm also fails to handle years before A.D. 1 gracefully, but
3544 * that's still outside the scope for POSIX time manipulation, so I don't
3548 year = 1900 + ptm->tm_year;
3549 month = ptm->tm_mon;
3550 mday = ptm->tm_mday;
3551 /* allow given yday with no month & mday to dominate the result */
3552 if (ptm->tm_yday >= 0 && mday <= 0 && month <= 0) {
3555 jday = 1 + ptm->tm_yday;
3564 yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400;
3565 yearday += month*MONTH_TO_DAYS + mday + jday;
3567 * Note that we don't know when leap-seconds were or will be,
3568 * so we have to trust the user if we get something which looks
3569 * like a sensible leap-second. Wild values for seconds will
3570 * be rationalised, however.
3572 if ((unsigned) ptm->tm_sec <= 60) {
3579 secs += 60 * ptm->tm_min;
3580 secs += SECS_PER_HOUR * ptm->tm_hour;
3582 if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) {
3583 /* got negative remainder, but need positive time */
3584 /* back off an extra day to compensate */
3585 yearday += (secs/SECS_PER_DAY)-1;
3586 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1);
3589 yearday += (secs/SECS_PER_DAY);
3590 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY);
3593 else if (secs >= SECS_PER_DAY) {
3594 yearday += (secs/SECS_PER_DAY);
3595 secs %= SECS_PER_DAY;
3597 ptm->tm_hour = secs/SECS_PER_HOUR;
3598 secs %= SECS_PER_HOUR;
3599 ptm->tm_min = secs/60;
3601 ptm->tm_sec += secs;
3602 /* done with time of day effects */
3604 * The algorithm for yearday has (so far) left it high by 428.
3605 * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to
3606 * bias it by 123 while trying to figure out what year it
3607 * really represents. Even with this tweak, the reverse
3608 * translation fails for years before A.D. 0001.
3609 * It would still fail for Feb 29, but we catch that one below.
3611 jday = yearday; /* save for later fixup vis-a-vis Jan 1 */
3612 yearday -= YEAR_ADJUST;
3613 year = (yearday / DAYS_PER_QCENT) * 400;
3614 yearday %= DAYS_PER_QCENT;
3615 odd_cent = yearday / DAYS_PER_CENT;
3616 year += odd_cent * 100;
3617 yearday %= DAYS_PER_CENT;
3618 year += (yearday / DAYS_PER_QYEAR) * 4;
3619 yearday %= DAYS_PER_QYEAR;
3620 odd_year = yearday / DAYS_PER_YEAR;
3622 yearday %= DAYS_PER_YEAR;
3623 if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */
3628 yearday += YEAR_ADJUST; /* recover March 1st crock */
3629 month = yearday*DAYS_TO_MONTH;
3630 yearday -= month*MONTH_TO_DAYS;
3631 /* recover other leap-year adjustment */
3640 ptm->tm_year = year - 1900;
3642 ptm->tm_mday = yearday;
3643 ptm->tm_mon = month;
3647 ptm->tm_mon = month - 1;
3649 /* re-build yearday based on Jan 1 to get tm_yday */
3651 yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400;
3652 yearday += 14*MONTH_TO_DAYS + 1;
3653 ptm->tm_yday = jday - yearday;
3654 /* fix tm_wday if not overridden by caller */
3655 if ((unsigned)ptm->tm_wday > 6)
3656 ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
3660 Perl_my_strftime(pTHX_ char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst)
3668 init_tm(&mytm); /* XXX workaround - see init_tm() above */
3671 mytm.tm_hour = hour;
3672 mytm.tm_mday = mday;
3674 mytm.tm_year = year;
3675 mytm.tm_wday = wday;
3676 mytm.tm_yday = yday;
3677 mytm.tm_isdst = isdst;
3680 New(0, buf, buflen, char);
3681 len = strftime(buf, buflen, fmt, &mytm);
3683 ** The following is needed to handle to the situation where
3684 ** tmpbuf overflows. Basically we want to allocate a buffer
3685 ** and try repeatedly. The reason why it is so complicated
3686 ** is that getting a return value of 0 from strftime can indicate
3687 ** one of the following:
3688 ** 1. buffer overflowed,
3689 ** 2. illegal conversion specifier, or
3690 ** 3. the format string specifies nothing to be returned(not
3691 ** an error). This could be because format is an empty string
3692 ** or it specifies %p that yields an empty string in some locale.
3693 ** If there is a better way to make it portable, go ahead by
3696 if ((len > 0 && len < buflen) || (len == 0 && *fmt == '\0'))
3699 /* Possibly buf overflowed - try again with a bigger buf */
3700 int fmtlen = strlen(fmt);
3701 int bufsize = fmtlen + buflen;
3703 New(0, buf, bufsize, char);
3705 buflen = strftime(buf, bufsize, fmt, &mytm);
3706 if (buflen > 0 && buflen < bufsize)
3708 /* heuristic to prevent out-of-memory errors */
3709 if (bufsize > 100*fmtlen) {
3715 Renew(buf, bufsize, char);
3720 Perl_croak(aTHX_ "panic: no strftime");
3725 #define SV_CWD_RETURN_UNDEF \
3726 sv_setsv(sv, &PL_sv_undef); \
3729 #define SV_CWD_ISDOT(dp) \
3730 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
3731 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
3734 =for apidoc getcwd_sv
3736 Fill the sv with current working directory
3741 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
3742 * rewritten again by dougm, optimized for use with xs TARG, and to prefer
3743 * getcwd(3) if available
3744 * Comments from the orignal:
3745 * This is a faster version of getcwd. It's also more dangerous
3746 * because you might chdir out of a directory that you can't chdir
3750 Perl_getcwd_sv(pTHX_ register SV *sv)
3754 #ifndef INCOMPLETE_TAINTS
3760 char buf[MAXPATHLEN];
3762 /* Some getcwd()s automatically allocate a buffer of the given
3763 * size from the heap if they are given a NULL buffer pointer.
3764 * The problem is that this behaviour is not portable. */
3765 if (getcwd(buf, sizeof(buf) - 1)) {
3766 STRLEN len = strlen(buf);
3767 sv_setpvn(sv, buf, len);
3771 sv_setsv(sv, &PL_sv_undef);
3778 struct stat statbuf;
3779 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
3780 int namelen, pathlen=0;
3784 (void)SvUPGRADE(sv, SVt_PV);
3786 if (PerlLIO_lstat(".", &statbuf) < 0) {
3787 SV_CWD_RETURN_UNDEF;
3790 orig_cdev = statbuf.st_dev;
3791 orig_cino = statbuf.st_ino;
3799 if (PerlDir_chdir("..") < 0) {
3800 SV_CWD_RETURN_UNDEF;
3802 if (PerlLIO_stat(".", &statbuf) < 0) {
3803 SV_CWD_RETURN_UNDEF;
3806 cdev = statbuf.st_dev;
3807 cino = statbuf.st_ino;
3809 if (odev == cdev && oino == cino) {
3812 if (!(dir = PerlDir_open("."))) {
3813 SV_CWD_RETURN_UNDEF;
3816 while ((dp = PerlDir_read(dir)) != NULL) {
3818 namelen = dp->d_namlen;
3820 namelen = strlen(dp->d_name);
3823 if (SV_CWD_ISDOT(dp)) {
3827 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
3828 SV_CWD_RETURN_UNDEF;
3831 tdev = statbuf.st_dev;
3832 tino = statbuf.st_ino;
3833 if (tino == oino && tdev == odev) {
3839 SV_CWD_RETURN_UNDEF;
3842 if (pathlen + namelen + 1 >= MAXPATHLEN) {
3843 SV_CWD_RETURN_UNDEF;
3846 SvGROW(sv, pathlen + namelen + 1);
3850 Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
3853 /* prepend current directory to the front */
3855 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
3856 pathlen += (namelen + 1);
3858 #ifdef VOID_CLOSEDIR
3861 if (PerlDir_close(dir) < 0) {
3862 SV_CWD_RETURN_UNDEF;
3868 SvCUR_set(sv, pathlen);
3872 if (PerlDir_chdir(SvPVX(sv)) < 0) {
3873 SV_CWD_RETURN_UNDEF;
3876 if (PerlLIO_stat(".", &statbuf) < 0) {
3877 SV_CWD_RETURN_UNDEF;
3880 cdev = statbuf.st_dev;
3881 cino = statbuf.st_ino;
3883 if (cdev != orig_cdev || cino != orig_cino) {
3884 Perl_croak(aTHX_ "Unstable directory path, "
3885 "current directory changed unexpectedly");
3896 =for apidoc new_vstring
3898 Returns a pointer to the next character after the parsed
3899 vstring, as well as updating the passed in sv.
3901 Function must be called like
3904 s = new_vstring(s,sv);
3906 The sv must already be large enough to store the vstring
3913 Perl_new_vstring(pTHX_ char *s, SV *sv)
3916 if (*pos == 'v') pos++; /* get past 'v' */
3917 while (isDIGIT(*pos) || *pos == '_')
3919 if (!isALPHA(*pos)) {
3921 U8 tmpbuf[UTF8_MAXLEN+1];
3924 if (*s == 'v') s++; /* get past 'v' */
3926 sv_setpvn(sv, "", 0);
3931 /* this is atoi() that tolerates underscores */
3934 if ( *(s-1) == '_') {
3937 while (--end >= s) {
3940 rev += (*end - '0') * mult;
3942 if (orev > rev && ckWARN_d(WARN_OVERFLOW))
3943 Perl_warner(aTHX_ WARN_OVERFLOW,
3944 "Integer overflow in decimal number");
3947 /* Append native character for the rev point */
3948 tmpend = uvchr_to_utf8(tmpbuf, rev);
3949 sv_catpvn(sv, (const char*)tmpbuf, tmpend - tmpbuf);
3950 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(rev)))
3952 if ( (*pos == '.' || *pos == '_') && isDIGIT(pos[1]))
3958 while (isDIGIT(*pos) )