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 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
19 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
28 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
29 #if !defined(HAS_SIGACTION) && defined(VMS)
30 # define FAKE_PERSISTENT_SIGNAL_HANDLERS
33 static void restore_magic(pTHX_ void *p);
34 static void unwind_handler_stack(pTHX_ void *p);
37 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
45 /* MGS is typedef'ed to struct magic_state in perl.h */
48 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
51 assert(SvMAGICAL(sv));
53 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
55 mgs = SSPTR(mgs_ix, MGS*);
57 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
58 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
62 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
66 =for apidoc mg_magical
68 Turns on the magical status of an SV. See C<sv_magic>.
74 Perl_mg_magical(pTHX_ SV *sv)
77 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
78 MGVTBL* vtbl = mg->mg_virtual;
80 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
84 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
93 Do magic after a value is retrieved from the SV. See C<sv_magic>.
99 Perl_mg_get(pTHX_ SV *sv)
102 MAGIC *newmg, *head, *cur, *mg;
103 I32 mgs_ix = SSNEW(sizeof(MGS));
105 save_magic(mgs_ix, sv);
107 /* We must call svt_get(sv, mg) for each valid entry in the linked
108 list of magic. svt_get() may delete the current entry, add new
109 magic to the head of the list, or upgrade the SV. AMS 20010810 */
111 newmg = cur = head = mg = SvMAGIC(sv);
113 MGVTBL *vtbl = mg->mg_virtual;
115 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
116 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
117 /* Don't restore the flags for this entry if it was deleted. */
118 if (mg->mg_flags & MGf_GSKIP)
119 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
122 mg = mg->mg_moremagic;
125 /* Have we finished with the new entries we saw? Start again
126 where we left off (unless there are more new entries). */
134 /* Were any new entries added? */
135 if (!new && (newmg = SvMAGIC(sv)) != head) {
142 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
149 Do magic after a value is assigned to the SV. See C<sv_magic>.
155 Perl_mg_set(pTHX_ SV *sv)
161 mgs_ix = SSNEW(sizeof(MGS));
162 save_magic(mgs_ix, sv);
164 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
165 MGVTBL* vtbl = mg->mg_virtual;
166 nextmg = mg->mg_moremagic; /* it may delete itself */
167 if (mg->mg_flags & MGf_GSKIP) {
168 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
169 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
171 if (vtbl && vtbl->svt_set)
172 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
175 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
180 =for apidoc mg_length
182 Report on the SV's length. See C<sv_magic>.
188 Perl_mg_length(pTHX_ SV *sv)
193 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
194 MGVTBL* vtbl = mg->mg_virtual;
195 if (vtbl && vtbl->svt_len) {
198 mgs_ix = SSNEW(sizeof(MGS));
199 save_magic(mgs_ix, sv);
200 /* omit MGf_GSKIP -- not changed here */
201 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
202 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
209 U8 *s = (U8*)SvPV(sv, len);
210 len = Perl_utf8_length(aTHX_ s, s + len);
218 Perl_mg_size(pTHX_ SV *sv)
223 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
224 MGVTBL* vtbl = mg->mg_virtual;
225 if (vtbl && vtbl->svt_len) {
228 mgs_ix = SSNEW(sizeof(MGS));
229 save_magic(mgs_ix, sv);
230 /* omit MGf_GSKIP -- not changed here */
231 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
232 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
239 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
244 Perl_croak(aTHX_ "Size magic not implemented");
253 Clear something magical that the SV represents. See C<sv_magic>.
259 Perl_mg_clear(pTHX_ SV *sv)
264 mgs_ix = SSNEW(sizeof(MGS));
265 save_magic(mgs_ix, sv);
267 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
268 MGVTBL* vtbl = mg->mg_virtual;
269 /* omit GSKIP -- never set here */
271 if (vtbl && vtbl->svt_clear)
272 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
275 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
282 Finds the magic pointer for type matching the SV. See C<sv_magic>.
288 Perl_mg_find(pTHX_ SV *sv, int type)
293 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
294 if (mg->mg_type == type)
303 Copies the magic from one SV to another. See C<sv_magic>.
309 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
313 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
314 if (isUPPER(mg->mg_type)) {
316 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
317 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
319 toLOWER(mg->mg_type), key, klen);
329 Free any magic storage used by the SV. See C<sv_magic>.
335 Perl_mg_free(pTHX_ SV *sv)
339 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
340 MGVTBL* vtbl = mg->mg_virtual;
341 moremagic = mg->mg_moremagic;
342 if (vtbl && vtbl->svt_free)
343 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
344 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
346 Safefree(mg->mg_ptr);
347 else if (mg->mg_len == HEf_SVKEY)
348 SvREFCNT_dec((SV*)mg->mg_ptr);
350 if (mg->mg_flags & MGf_REFCOUNTED)
351 SvREFCNT_dec(mg->mg_obj);
359 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
364 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
368 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
369 if (mg->mg_obj) /* @+ */
372 return rx->lastparen;
379 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
387 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
391 if (paren <= rx->nparens &&
392 (s = rx->startp[paren]) != -1 &&
393 (t = rx->endp[paren]) != -1)
395 if (mg->mg_obj) /* @+ */
400 if (i > 0 && PL_reg_match_utf8) {
401 char *b = rx->subbeg;
403 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
413 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
415 Perl_croak(aTHX_ PL_no_modify);
421 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
428 switch (*mg->mg_ptr) {
429 case '1': case '2': case '3': case '4':
430 case '5': case '6': case '7': case '8': case '9': case '&':
431 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
433 paren = atoi(mg->mg_ptr); /* $& is in [0] */
435 if (paren <= rx->nparens &&
436 (s1 = rx->startp[paren]) != -1 &&
437 (t1 = rx->endp[paren]) != -1)
441 if (i > 0 && PL_reg_match_utf8) {
442 char *s = rx->subbeg + s1;
443 char *send = rx->subbeg + t1;
446 if (is_utf8_string((U8*)s, i))
447 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
450 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
456 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
457 paren = rx->lastparen;
462 case '\016': /* ^N */
463 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
464 paren = rx->lastcloseparen;
470 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
471 if (rx->startp[0] != -1) {
482 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
483 if (rx->endp[0] != -1) {
484 i = rx->sublen - rx->endp[0];
495 if (!SvPOK(sv) && SvNIOK(sv)) {
505 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
512 switch (*mg->mg_ptr) {
513 case '\001': /* ^A */
514 sv_setsv(sv, PL_bodytarget);
516 case '\003': /* ^C */
517 sv_setiv(sv, (IV)PL_minus_c);
520 case '\004': /* ^D */
521 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
522 #if defined(YYDEBUG) && defined(DEBUGGING)
523 PL_yydebug = DEBUG_p_TEST;
526 case '\005': /* ^E */
527 if (*(mg->mg_ptr+1) == '\0') {
528 #ifdef MACOS_TRADITIONAL
532 sv_setnv(sv,(double)gMacPerl_OSErr);
533 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
538 # include <descrip.h>
539 # include <starlet.h>
541 $DESCRIPTOR(msgdsc,msg);
542 sv_setnv(sv,(NV) vaxc$errno);
543 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
544 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
550 if (!(_emx_env & 0x200)) { /* Under DOS */
551 sv_setnv(sv, (NV)errno);
552 sv_setpv(sv, errno ? Strerror(errno) : "");
554 if (errno != errno_isOS2) {
555 int tmp = _syserrno();
556 if (tmp) /* 2nd call to _syserrno() makes it 0 */
559 sv_setnv(sv, (NV)Perl_rc);
560 sv_setpv(sv, os2error(Perl_rc));
565 DWORD dwErr = GetLastError();
566 sv_setnv(sv, (NV)dwErr);
569 PerlProc_GetOSError(sv, dwErr);
576 sv_setnv(sv, (NV)errno);
577 sv_setpv(sv, errno ? Strerror(errno) : "");
582 SvNOK_on(sv); /* what a wonderful hack! */
584 else if (strEQ(mg->mg_ptr+1, "NCODING"))
585 sv_setsv(sv, PL_encoding);
587 case '\006': /* ^F */
588 sv_setiv(sv, (IV)PL_maxsysfd);
590 case '\010': /* ^H */
591 sv_setiv(sv, (IV)PL_hints);
593 case '\011': /* ^I */ /* NOT \t in EBCDIC */
595 sv_setpv(sv, PL_inplace);
597 sv_setsv(sv, &PL_sv_undef);
599 case '\017': /* ^O & ^OPEN */
600 if (*(mg->mg_ptr+1) == '\0')
601 sv_setpv(sv, PL_osname);
602 else if (strEQ(mg->mg_ptr, "\017PEN")) {
603 if (!PL_compiling.cop_io)
604 sv_setsv(sv, &PL_sv_undef);
606 sv_setsv(sv, PL_compiling.cop_io);
610 case '\020': /* ^P */
611 sv_setiv(sv, (IV)PL_perldb);
613 case '\023': /* ^S */
615 if (PL_lex_state != LEX_NOTPARSING)
618 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
623 case '\024': /* ^T */
624 if (*(mg->mg_ptr+1) == '\0') {
626 sv_setnv(sv, PL_basetime);
628 sv_setiv(sv, (IV)PL_basetime);
631 else if (strEQ(mg->mg_ptr, "\024AINT"))
632 sv_setiv(sv, PL_tainting);
634 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
635 if (*(mg->mg_ptr+1) == '\0')
636 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
637 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
638 if (PL_compiling.cop_warnings == pWARN_NONE ||
639 PL_compiling.cop_warnings == pWARN_STD)
641 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
643 else if (PL_compiling.cop_warnings == pWARN_ALL) {
644 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
647 sv_setsv(sv, PL_compiling.cop_warnings);
651 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
652 sv_setiv(sv, (IV)PL_widesyscalls);
654 case '1': case '2': case '3': case '4':
655 case '5': case '6': case '7': case '8': case '9': case '&':
656 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
660 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
661 * XXX Does the new way break anything?
663 paren = atoi(mg->mg_ptr); /* $& is in [0] */
665 if (paren <= rx->nparens &&
666 (s1 = rx->startp[paren]) != -1 &&
667 (t1 = rx->endp[paren]) != -1)
676 bool was_tainted = FALSE;
678 was_tainted = PL_tainted;
682 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
687 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
692 sv_setsv(sv,&PL_sv_undef);
695 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
696 paren = rx->lastparen;
700 sv_setsv(sv,&PL_sv_undef);
702 case '\016': /* ^N */
703 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
704 paren = rx->lastcloseparen;
708 sv_setsv(sv,&PL_sv_undef);
711 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
712 if ((s = rx->subbeg) && rx->startp[0] != -1) {
717 sv_setsv(sv,&PL_sv_undef);
720 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
721 if (rx->subbeg && rx->endp[0] != -1) {
722 s = rx->subbeg + rx->endp[0];
723 i = rx->sublen - rx->endp[0];
727 sv_setsv(sv,&PL_sv_undef);
731 if (GvIO(PL_last_in_gv)) {
732 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
738 sv_setiv(sv, (IV)STATUS_CURRENT);
739 #ifdef COMPLEX_STATUS
740 LvTARGOFF(sv) = PL_statusvalue;
741 LvTARGLEN(sv) = PL_statusvalue_vms;
746 s = IoTOP_NAME(GvIOp(PL_defoutgv));
750 sv_setpv(sv,GvENAME(PL_defoutgv));
755 s = IoFMT_NAME(GvIOp(PL_defoutgv));
757 s = GvENAME(PL_defoutgv);
762 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
765 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
768 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
776 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
779 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
786 sv_setpv(sv,PL_ofmt);
790 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
791 sv_setpv(sv, errno ? Strerror(errno) : "");
794 int saveerrno = errno;
795 sv_setnv(sv, (NV)errno);
797 if (errno == errno_isOS2 || errno == errno_isOS2_set)
798 sv_setpv(sv, os2error(Perl_rc));
801 sv_setpv(sv, errno ? Strerror(errno) : "");
805 SvNOK_on(sv); /* what a wonderful hack! */
808 sv_setiv(sv, (IV)PL_uid);
811 sv_setiv(sv, (IV)PL_euid);
814 sv_setiv(sv, (IV)PL_gid);
816 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
820 sv_setiv(sv, (IV)PL_egid);
822 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
827 Groups_t gary[NGROUPS];
828 i = getgroups(NGROUPS,gary);
830 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
833 (void)SvIOK_on(sv); /* what a wonderful hack! */
837 #ifndef MACOS_TRADITIONAL
841 #ifdef USE_5005THREADS
843 sv_setsv(sv, thr->errsv);
845 #endif /* USE_5005THREADS */
851 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
853 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
855 if (uf && uf->uf_val)
856 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
861 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
872 #ifdef DYNAMIC_ENV_FETCH
873 /* We just undefd an environment var. Is a replacement */
874 /* waiting in the wings? */
877 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
878 s = SvPV(*valp, len);
882 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
883 /* And you'll never guess what the dog had */
884 /* in its mouth... */
886 MgTAINTEDDIR_off(mg);
888 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
889 char pathbuf[256], eltbuf[256], *cp, *elt = s;
893 do { /* DCL$PATH may be a search list */
894 while (1) { /* as may dev portion of any element */
895 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
896 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
897 cando_by_name(S_IWUSR,0,elt) ) {
902 if ((cp = strchr(elt, ':')) != Nullch)
904 if (my_trnlnm(elt, eltbuf, j++))
910 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
913 if (s && klen == 4 && strEQ(ptr,"PATH")) {
914 char *strend = s + len;
919 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
922 if (i >= sizeof tmpbuf /* too long -- assume the worst */
924 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
931 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
937 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
940 my_setenv(MgPV(mg,n_a),Nullch);
945 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
948 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
953 magic_clear_all_env(sv,mg);
954 hv_iterinit((HV*)sv);
955 while ((entry = hv_iternext((HV*)sv))) {
957 my_setenv(hv_iterkey(entry, &keylen),
958 SvPV(hv_iterval((HV*)sv, entry), n_a));
966 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
968 #if defined(VMS) || defined(EPOC)
969 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
971 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
974 # ifdef USE_ENVIRON_ARRAY
975 # ifndef PERL_USE_SAFE_PUTENV
978 if (environ == PL_origenviron)
979 environ = (char**)safesysmalloc(sizeof(char*));
981 for (i = 0; environ[i]; i++)
982 safesysfree(environ[i]);
983 # endif /* PERL_USE_SAFE_PUTENV */
987 # endif /* USE_ENVIRON_ARRAY */
988 # endif /* PERL_IMPLICIT_SYS || WIN32 */
989 #endif /* VMS || EPC */
993 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
994 static int sig_ignoring_initted = 0;
995 static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */
1000 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1004 /* Are we fetching a signal entry? */
1005 i = whichsig(MgPV(mg,n_a));
1008 sv_setsv(sv,PL_psig_ptr[i]);
1010 Sighandler_t sigstate;
1011 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1012 if (sig_ignoring_initted && sig_ignoring[i])
1016 sigstate = rsignal_state(i);
1018 /* cache state so we don't fetch it again */
1019 if(sigstate == SIG_IGN)
1020 sv_setpv(sv,"IGNORE");
1022 sv_setsv(sv,&PL_sv_undef);
1023 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1030 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1034 /* Are we clearing a signal entry? */
1035 i = whichsig(MgPV(mg,n_a));
1037 if(PL_psig_ptr[i]) {
1038 SvREFCNT_dec(PL_psig_ptr[i]);
1041 if(PL_psig_name[i]) {
1042 SvREFCNT_dec(PL_psig_name[i]);
1050 Perl_raise_signal(pTHX_ int sig)
1052 /* Set a flag to say this signal is pending */
1053 PL_psig_pend[sig]++;
1054 /* And one to say _a_ signal is pending */
1059 Perl_csighandler(int sig)
1061 #ifndef PERL_OLD_SIGNALS
1064 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1065 (void) rsignal(sig, &Perl_csighandler);
1066 if (sig_ignoring[sig]) return;
1068 #ifdef PERL_OLD_SIGNALS
1069 /* Call the perl level handler now with risk we may be in malloc() etc. */
1070 (*PL_sighandlerp)(sig);
1072 Perl_raise_signal(aTHX_ sig);
1077 Perl_despatch_signals(pTHX)
1081 for (sig = 1; sig < SIG_SIZE; sig++) {
1082 if (PL_psig_pend[sig]) {
1083 PL_psig_pend[sig] = 0;
1084 (*PL_sighandlerp)(sig);
1090 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1099 if (strEQ(s,"__DIE__"))
1101 else if (strEQ(s,"__WARN__"))
1104 Perl_croak(aTHX_ "No such hook: %s", s);
1112 i = whichsig(s); /* ...no, a brick */
1114 if (ckWARN(WARN_SIGNAL))
1115 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1118 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1119 if (!sig_ignoring_initted) {
1121 for (j = 0; j < SIG_SIZE; j++) sig_ignoring[j] = 0;
1122 sig_ignoring_initted = 1;
1124 sig_ignoring[i] = 0;
1126 SvREFCNT_dec(PL_psig_name[i]);
1127 SvREFCNT_dec(PL_psig_ptr[i]);
1128 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1129 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1130 PL_psig_name[i] = newSVpvn(s, len);
1131 SvREADONLY_on(PL_psig_name[i]);
1133 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1135 (void)rsignal(i, &Perl_csighandler);
1137 *svp = SvREFCNT_inc(sv);
1140 s = SvPV_force(sv,len);
1141 if (strEQ(s,"IGNORE")) {
1143 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1144 sig_ignoring[i] = 1;
1145 (void)rsignal(i, &Perl_csighandler);
1147 (void)rsignal(i, SIG_IGN);
1152 else if (strEQ(s,"DEFAULT") || !*s) {
1154 (void)rsignal(i, SIG_DFL);
1160 * We should warn if HINT_STRICT_REFS, but without
1161 * access to a known hint bit in a known OP, we can't
1162 * tell whether HINT_STRICT_REFS is in force or not.
1164 if (!strchr(s,':') && !strchr(s,'\''))
1165 sv_insert(sv, 0, 0, "main::", 6);
1167 (void)rsignal(i, &Perl_csighandler);
1169 *svp = SvREFCNT_inc(sv);
1173 #endif /* !PERL_MICRO */
1176 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1178 PL_sub_generation++;
1183 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1185 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1186 PL_amagic_generation++;
1192 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1194 HV *hv = (HV*)LvTARG(sv);
1198 (void) hv_iterinit(hv);
1199 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1202 while (hv_iternext(hv))
1207 sv_setiv(sv, (IV)i);
1212 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1215 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1220 /* caller is responsible for stack switching/cleanup */
1222 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1228 PUSHs(SvTIED_obj(sv, mg));
1231 if (mg->mg_len >= 0)
1232 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1233 else if (mg->mg_len == HEf_SVKEY)
1234 PUSHs((SV*)mg->mg_ptr);
1236 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1237 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1245 return call_method(meth, flags);
1249 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1255 PUSHSTACKi(PERLSI_MAGIC);
1257 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1258 sv_setsv(sv, *PL_stack_sp--);
1268 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1270 magic_methpack(sv,mg,"FETCH");
1272 mg->mg_flags |= MGf_GSKIP;
1277 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1281 PUSHSTACKi(PERLSI_MAGIC);
1282 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1289 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1291 return magic_methpack(sv,mg,"DELETE");
1296 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1303 PUSHSTACKi(PERLSI_MAGIC);
1304 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1305 sv = *PL_stack_sp--;
1306 retval = (U32) SvIV(sv)-1;
1315 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1320 PUSHSTACKi(PERLSI_MAGIC);
1322 XPUSHs(SvTIED_obj(sv, mg));
1324 call_method("CLEAR", G_SCALAR|G_DISCARD);
1331 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1334 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1338 PUSHSTACKi(PERLSI_MAGIC);
1341 PUSHs(SvTIED_obj(sv, mg));
1346 if (call_method(meth, G_SCALAR))
1347 sv_setsv(key, *PL_stack_sp--);
1356 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1358 return magic_methpack(sv,mg,"EXISTS");
1362 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1372 svp = av_fetch(GvAV(gv),
1373 atoi(MgPV(mg,n_a)), FALSE);
1374 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1380 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1382 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1387 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1389 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1394 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1396 SV* lsv = LvTARG(sv);
1398 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1399 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1400 if (mg && mg->mg_len >= 0) {
1403 sv_pos_b2u(lsv, &i);
1404 sv_setiv(sv, i + PL_curcop->cop_arybase);
1413 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1415 SV* lsv = LvTARG(sv);
1422 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1423 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1427 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1428 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1430 else if (!SvOK(sv)) {
1434 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1436 pos = SvIV(sv) - PL_curcop->cop_arybase;
1439 ulen = sv_len_utf8(lsv);
1454 sv_pos_u2b(lsv, &p, 0);
1459 mg->mg_flags &= ~MGf_MINMATCH;
1465 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1467 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1469 gv_efullname3(sv,((GV*)sv), "*");
1473 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1478 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1487 if (*s == '*' && s[1])
1489 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1494 GvGP(sv) = gp_ref(GvGP(gv));
1499 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1502 SV *lsv = LvTARG(sv);
1503 char *tmps = SvPV(lsv,len);
1504 I32 offs = LvTARGOFF(sv);
1505 I32 rem = LvTARGLEN(sv);
1508 sv_pos_u2b(lsv, &offs, &rem);
1511 if (rem + offs > len)
1513 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1520 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1523 char *tmps = SvPV(sv, len);
1524 SV *lsv = LvTARG(sv);
1525 I32 lvoff = LvTARGOFF(sv);
1526 I32 lvlen = LvTARGLEN(sv);
1529 sv_utf8_upgrade(lsv);
1530 sv_pos_u2b(lsv, &lvoff, &lvlen);
1531 sv_insert(lsv, lvoff, lvlen, tmps, len);
1534 else if (lsv && SvUTF8(lsv)) {
1535 sv_pos_u2b(lsv, &lvoff, &lvlen);
1536 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1537 sv_insert(lsv, lvoff, lvlen, tmps, len);
1541 sv_insert(lsv, lvoff, lvlen, tmps, len);
1547 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1549 TAINT_IF((mg->mg_len & 1) ||
1550 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1555 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1557 if (PL_localizing) {
1558 if (PL_localizing == 1)
1563 else if (PL_tainted)
1571 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1573 SV *lsv = LvTARG(sv);
1580 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1585 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1587 do_vecset(sv); /* XXX slurp this routine */
1592 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1595 if (LvTARGLEN(sv)) {
1597 SV *ahv = LvTARG(sv);
1598 if (SvTYPE(ahv) == SVt_PVHV) {
1599 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1604 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1610 AV* av = (AV*)LvTARG(sv);
1611 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1612 targ = AvARRAY(av)[LvTARGOFF(sv)];
1614 if (targ && targ != &PL_sv_undef) {
1615 /* somebody else defined it for us */
1616 SvREFCNT_dec(LvTARG(sv));
1617 LvTARG(sv) = SvREFCNT_inc(targ);
1619 SvREFCNT_dec(mg->mg_obj);
1620 mg->mg_obj = Nullsv;
1621 mg->mg_flags &= ~MGf_REFCOUNTED;
1626 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1631 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1636 sv_setsv(LvTARG(sv), sv);
1637 SvSETMAGIC(LvTARG(sv));
1643 Perl_vivify_defelem(pTHX_ SV *sv)
1648 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1651 SV *ahv = LvTARG(sv);
1653 if (SvTYPE(ahv) == SVt_PVHV) {
1654 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1659 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1663 if (!value || value == &PL_sv_undef)
1664 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1667 AV* av = (AV*)LvTARG(sv);
1668 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1669 LvTARG(sv) = Nullsv; /* array can't be extended */
1671 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1672 if (!svp || (value = *svp) == &PL_sv_undef)
1673 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1676 (void)SvREFCNT_inc(value);
1677 SvREFCNT_dec(LvTARG(sv));
1680 SvREFCNT_dec(mg->mg_obj);
1681 mg->mg_obj = Nullsv;
1682 mg->mg_flags &= ~MGf_REFCOUNTED;
1686 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1688 AV *av = (AV*)mg->mg_obj;
1689 SV **svp = AvARRAY(av);
1690 I32 i = AvFILLp(av);
1692 if (svp[i] && svp[i] != &PL_sv_undef) {
1693 if (!SvWEAKREF(svp[i]))
1694 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1695 /* XXX Should we check that it hasn't changed? */
1697 (void)SvOK_off(svp[i]);
1698 SvWEAKREF_off(svp[i]);
1699 svp[i] = &PL_sv_undef;
1707 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1715 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1717 sv_unmagic(sv, PERL_MAGIC_bm);
1723 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1725 sv_unmagic(sv, PERL_MAGIC_fm);
1731 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1733 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1735 if (uf && uf->uf_set)
1736 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1741 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1743 regexp *re = (regexp *)mg->mg_obj;
1748 #ifdef USE_LOCALE_COLLATE
1750 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1753 * RenE<eacute> Descartes said "I think not."
1754 * and vanished with a faint plop.
1757 Safefree(mg->mg_ptr);
1763 #endif /* USE_LOCALE_COLLATE */
1766 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1771 switch (*mg->mg_ptr) {
1772 case '\001': /* ^A */
1773 sv_setsv(PL_bodytarget, sv);
1775 case '\003': /* ^C */
1776 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1779 case '\004': /* ^D */
1780 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1781 DEBUG_x(dump_all());
1783 case '\005': /* ^E */
1784 if (*(mg->mg_ptr+1) == '\0') {
1785 #ifdef MACOS_TRADITIONAL
1786 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1789 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1792 SetLastError( SvIV(sv) );
1795 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1797 /* will anyone ever use this? */
1798 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1804 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
1806 sv_setsv(PL_encoding, sv);
1808 PL_encoding = newSVsv(sv);
1810 case '\006': /* ^F */
1811 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1813 case '\010': /* ^H */
1814 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1816 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1818 Safefree(PL_inplace);
1820 PL_inplace = savepv(SvPV(sv,len));
1822 PL_inplace = Nullch;
1824 case '\017': /* ^O */
1825 if (*(mg->mg_ptr+1) == '\0') {
1827 Safefree(PL_osname);
1829 PL_osname = savepv(SvPV(sv,len));
1833 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1834 if (!PL_compiling.cop_io)
1835 PL_compiling.cop_io = newSVsv(sv);
1837 sv_setsv(PL_compiling.cop_io,sv);
1840 case '\020': /* ^P */
1841 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1842 if (PL_perldb && !PL_DBsingle)
1845 case '\024': /* ^T */
1847 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1849 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1852 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1853 if (*(mg->mg_ptr+1) == '\0') {
1854 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1855 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1856 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1857 | (i ? G_WARN_ON : G_WARN_OFF) ;
1860 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
1861 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1862 if (!SvPOK(sv) && PL_localizing) {
1863 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1864 PL_compiling.cop_warnings = pWARN_NONE;
1869 int accumulate = 0 ;
1870 int any_fatals = 0 ;
1871 char * ptr = (char*)SvPV(sv, len) ;
1872 for (i = 0 ; i < len ; ++i) {
1873 accumulate |= ptr[i] ;
1874 any_fatals |= (ptr[i] & 0xAA) ;
1877 PL_compiling.cop_warnings = pWARN_NONE;
1878 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1879 PL_compiling.cop_warnings = pWARN_ALL;
1880 PL_dowarn |= G_WARN_ONCE ;
1883 if (specialWARN(PL_compiling.cop_warnings))
1884 PL_compiling.cop_warnings = newSVsv(sv) ;
1886 sv_setsv(PL_compiling.cop_warnings, sv);
1887 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1888 PL_dowarn |= G_WARN_ONCE ;
1894 else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS"))
1895 PL_widesyscalls = SvTRUE(sv);
1898 if (PL_localizing) {
1899 if (PL_localizing == 1)
1900 SAVESPTR(PL_last_in_gv);
1902 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1903 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1906 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1907 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1908 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1911 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1912 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1913 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1916 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1919 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1920 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1921 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1924 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1928 IO *io = GvIOp(PL_defoutgv);
1931 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1932 IoFLAGS(io) &= ~IOf_FLUSH;
1934 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1935 PerlIO *ofp = IoOFP(io);
1937 (void)PerlIO_flush(ofp);
1938 IoFLAGS(io) |= IOf_FLUSH;
1944 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1945 PL_multiline = (i != 0);
1948 SvREFCNT_dec(PL_rs);
1949 PL_rs = newSVsv(sv);
1953 SvREFCNT_dec(PL_ors_sv);
1954 if (SvOK(sv) || SvGMAGICAL(sv)) {
1955 PL_ors_sv = newSVsv(sv);
1963 SvREFCNT_dec(PL_ofs_sv);
1964 if (SvOK(sv) || SvGMAGICAL(sv)) {
1965 PL_ofs_sv = newSVsv(sv);
1974 PL_ofmt = savepv(SvPV(sv,len));
1977 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1980 #ifdef COMPLEX_STATUS
1981 if (PL_localizing == 2) {
1982 PL_statusvalue = LvTARGOFF(sv);
1983 PL_statusvalue_vms = LvTARGLEN(sv);
1987 #ifdef VMSISH_STATUS
1989 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1992 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1995 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1996 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1999 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2000 if (PL_delaymagic) {
2001 PL_delaymagic |= DM_RUID;
2002 break; /* don't do magic till later */
2005 (void)setruid((Uid_t)PL_uid);
2008 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2010 #ifdef HAS_SETRESUID
2011 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2013 if (PL_uid == PL_euid) /* special case $< = $> */
2014 (void)PerlProc_setuid(PL_uid);
2016 PL_uid = PerlProc_getuid();
2017 Perl_croak(aTHX_ "setruid() not implemented");
2022 PL_uid = PerlProc_getuid();
2023 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2026 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2027 if (PL_delaymagic) {
2028 PL_delaymagic |= DM_EUID;
2029 break; /* don't do magic till later */
2032 (void)seteuid((Uid_t)PL_euid);
2035 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2037 #ifdef HAS_SETRESUID
2038 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2040 if (PL_euid == PL_uid) /* special case $> = $< */
2041 PerlProc_setuid(PL_euid);
2043 PL_euid = PerlProc_geteuid();
2044 Perl_croak(aTHX_ "seteuid() not implemented");
2049 PL_euid = PerlProc_geteuid();
2050 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2053 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2054 if (PL_delaymagic) {
2055 PL_delaymagic |= DM_RGID;
2056 break; /* don't do magic till later */
2059 (void)setrgid((Gid_t)PL_gid);
2062 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2064 #ifdef HAS_SETRESGID
2065 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2067 if (PL_gid == PL_egid) /* special case $( = $) */
2068 (void)PerlProc_setgid(PL_gid);
2070 PL_gid = PerlProc_getgid();
2071 Perl_croak(aTHX_ "setrgid() not implemented");
2076 PL_gid = PerlProc_getgid();
2077 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2080 #ifdef HAS_SETGROUPS
2082 char *p = SvPV(sv, len);
2083 Groups_t gary[NGROUPS];
2088 for (i = 0; i < NGROUPS; ++i) {
2089 while (*p && !isSPACE(*p))
2098 (void)setgroups(i, gary);
2100 #else /* HAS_SETGROUPS */
2101 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2102 #endif /* HAS_SETGROUPS */
2103 if (PL_delaymagic) {
2104 PL_delaymagic |= DM_EGID;
2105 break; /* don't do magic till later */
2108 (void)setegid((Gid_t)PL_egid);
2111 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2113 #ifdef HAS_SETRESGID
2114 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2116 if (PL_egid == PL_gid) /* special case $) = $( */
2117 (void)PerlProc_setgid(PL_egid);
2119 PL_egid = PerlProc_getegid();
2120 Perl_croak(aTHX_ "setegid() not implemented");
2125 PL_egid = PerlProc_getegid();
2126 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2129 PL_chopset = SvPV_force(sv,len);
2131 #ifndef MACOS_TRADITIONAL
2133 #ifdef HAS_SETPROCTITLE
2134 /* The BSDs don't show the argv[] in ps(1) output, they
2135 * show a string from the process struct and provide
2136 * the setproctitle() routine to manipulate that. */
2139 # if __FreeBSD_version > 410001
2140 /* The leading "-" removes the "perl: " prefix,
2141 * but not the "(perl) suffix from the ps(1)
2142 * output, because that's what ps(1) shows if the
2143 * argv[] is modified. */
2144 setproctitle("-%s", s);
2145 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2146 /* This doesn't really work if you assume that
2147 * $0 = 'foobar'; will wipe out 'perl' from the $0
2148 * because in ps(1) output the result will be like
2149 * sprintf("perl: %s (perl)", s)
2150 * I guess this is a security feature:
2151 * one (a user process) cannot get rid of the original name.
2153 setproctitle("%s", s);
2160 /* See if all the arguments are contiguous in memory */
2161 for (i = 1; i < PL_origargc; i++) {
2162 if (PL_origargv[i] == s + 1
2164 || PL_origargv[i] == s + 2
2169 s += strlen(s); /* this one is ok too */
2174 /* can grab env area too? */
2175 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2176 my_setenv("NoNe SuCh", Nullch);
2177 /* force copy of environment */
2178 for (i = 0; PL_origenviron[i]; i++)
2179 if (PL_origenviron[i] == s + 1) {
2186 PL_origalen = s - PL_origargv[0];
2188 s = SvPV_force(sv,len);
2190 if (i >= PL_origalen) {
2192 /* don't allow system to limit $0 seen by script */
2193 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2194 Copy(s, PL_origargv[0], i, char);
2195 s = PL_origargv[0]+i;
2199 Copy(s, PL_origargv[0], i, char);
2200 s = PL_origargv[0]+i;
2202 while (++i < PL_origalen)
2204 s = PL_origargv[0]+i;
2205 for (i = 1; i < PL_origargc; i++)
2206 PL_origargv[i] = Nullch;
2210 #ifdef USE_5005THREADS
2212 sv_setsv(thr->errsv, sv);
2214 #endif /* USE_5005THREADS */
2219 #ifdef USE_5005THREADS
2221 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2223 DEBUG_S(PerlIO_printf(Perl_debug_log,
2224 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2225 PTR2UV(thr), PTR2UV(sv)));
2227 Perl_croak(aTHX_ "panic: magic_mutexfree");
2228 MUTEX_DESTROY(MgMUTEXP(mg));
2229 COND_DESTROY(MgCONDP(mg));
2232 #endif /* USE_5005THREADS */
2235 Perl_whichsig(pTHX_ char *sig)
2237 register char **sigv;
2239 for (sigv = PL_sig_name+1; *sigv; sigv++)
2240 if (strEQ(sig,*sigv))
2241 return PL_sig_num[sigv - PL_sig_name];
2243 if (strEQ(sig,"CHLD"))
2247 if (strEQ(sig,"CLD"))
2253 #if !defined(PERL_IMPLICIT_CONTEXT)
2258 Perl_sighandler(int sig)
2260 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2261 dTHXa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2268 SV *sv = Nullsv, *tSv = PL_Sv;
2274 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2275 PERL_SET_THX(aTHX); /* fake TLS, see above */
2278 if (PL_savestack_ix + 15 <= PL_savestack_max)
2280 if (PL_markstack_ptr < PL_markstack_max - 2)
2282 if (PL_retstack_ix < PL_retstack_max - 2)
2284 if (PL_scopestack_ix < PL_scopestack_max - 3)
2287 if (!PL_psig_ptr[sig])
2288 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2291 /* Max number of items pushed there is 3*n or 4. We cannot fix
2292 infinity, so we fix 4 (in fact 5): */
2294 PL_savestack_ix += 5; /* Protect save in progress. */
2295 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2298 PL_markstack_ptr++; /* Protect mark. */
2301 PL_retstack[PL_retstack_ix] = NULL;
2304 PL_scopestack_ix += 1;
2305 /* sv_2cv is too complicated, try a simpler variant first: */
2306 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2307 || SvTYPE(cv) != SVt_PVCV)
2308 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2310 if (!cv || !CvROOT(cv)) {
2311 if (ckWARN(WARN_SIGNAL))
2312 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2313 PL_sig_name[sig], (gv ? GvENAME(gv)
2320 if(PL_psig_name[sig]) {
2321 sv = SvREFCNT_inc(PL_psig_name[sig]);
2323 #if !defined(PERL_IMPLICIT_CONTEXT)
2327 sv = sv_newmortal();
2328 sv_setpv(sv,PL_sig_name[sig]);
2331 PUSHSTACKi(PERLSI_SIGNAL);
2336 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2339 if (SvTRUE(ERRSV)) {
2341 #ifdef HAS_SIGPROCMASK
2342 /* Handler "died", for example to get out of a restart-able read().
2343 * Before we re-do that on its behalf re-enable the signal which was
2344 * blocked by the system when we entered.
2348 sigaddset(&set,sig);
2349 sigprocmask(SIG_UNBLOCK, &set, NULL);
2351 /* Not clear if this will work */
2352 (void)rsignal(sig, SIG_IGN);
2353 (void)rsignal(sig, &Perl_csighandler);
2355 #endif /* !PERL_MICRO */
2356 Perl_die(aTHX_ Nullformat);
2360 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2366 PL_scopestack_ix -= 1;
2369 PL_op = myop; /* Apparently not needed... */
2371 PL_Sv = tSv; /* Restore global temporaries. */
2378 restore_magic(pTHX_ void *p)
2380 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2381 SV* sv = mgs->mgs_sv;
2386 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2389 SvFLAGS(sv) |= mgs->mgs_flags;
2393 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2396 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2398 /* If we're still on top of the stack, pop us off. (That condition
2399 * will be satisfied if restore_magic was called explicitly, but *not*
2400 * if it's being called via leave_scope.)
2401 * The reason for doing this is that otherwise, things like sv_2cv()
2402 * may leave alloc gunk on the savestack, and some code
2403 * (e.g. sighandler) doesn't expect that...
2405 if (PL_savestack_ix == mgs->mgs_ss_ix)
2407 I32 popval = SSPOPINT;
2408 assert(popval == SAVEt_DESTRUCTOR_X);
2409 PL_savestack_ix -= 2;
2411 assert(popval == SAVEt_ALLOC);
2413 PL_savestack_ix -= popval;
2419 unwind_handler_stack(pTHX_ void *p)
2421 U32 flags = *(U32*)p;
2424 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2425 /* cxstack_ix-- Not needed, die already unwound it. */
2426 #if !defined(PERL_IMPLICIT_CONTEXT)
2428 SvREFCNT_dec(sig_sv);