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 static void restore_magic(pTHXo_ void *p);
29 static void unwind_handler_stack(pTHXo_ void *p);
32 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
40 /* MGS is typedef'ed to struct magic_state in perl.h */
43 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
46 assert(SvMAGICAL(sv));
48 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
50 mgs = SSPTR(mgs_ix, MGS*);
52 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
53 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
57 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
61 =for apidoc mg_magical
63 Turns on the magical status of an SV. See C<sv_magic>.
69 Perl_mg_magical(pTHX_ SV *sv)
72 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
73 MGVTBL* vtbl = mg->mg_virtual;
75 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
79 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
88 Do magic after a value is retrieved from the SV. See C<sv_magic>.
94 Perl_mg_get(pTHX_ SV *sv)
101 mgs_ix = SSNEW(sizeof(MGS));
102 save_magic(mgs_ix, sv);
105 while ((mg = *mgp) != 0) {
106 MGVTBL* vtbl = mg->mg_virtual;
107 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
108 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
109 /* Ignore this magic if it's been deleted */
110 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
111 (mg->mg_flags & MGf_GSKIP))
112 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
114 /* Advance to next magic (complicated by possible deletion) */
115 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
116 mgp = &mg->mg_moremagic;
120 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
123 restore_magic(aTHXo_ INT2PTR(void*, (IV)mgs_ix));
130 Do magic after a value is assigned to the SV. See C<sv_magic>.
136 Perl_mg_set(pTHX_ SV *sv)
142 mgs_ix = SSNEW(sizeof(MGS));
143 save_magic(mgs_ix, sv);
145 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
146 MGVTBL* vtbl = mg->mg_virtual;
147 nextmg = mg->mg_moremagic; /* it may delete itself */
148 if (mg->mg_flags & MGf_GSKIP) {
149 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
150 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
152 if (vtbl && vtbl->svt_set)
153 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
156 restore_magic(aTHXo_ INT2PTR(void*, (IV)mgs_ix));
161 =for apidoc mg_length
163 Report on the SV's length. See C<sv_magic>.
169 Perl_mg_length(pTHX_ SV *sv)
174 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
175 MGVTBL* vtbl = mg->mg_virtual;
176 if (vtbl && vtbl->svt_len) {
179 mgs_ix = SSNEW(sizeof(MGS));
180 save_magic(mgs_ix, sv);
181 /* omit MGf_GSKIP -- not changed here */
182 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
183 restore_magic(aTHXo_ INT2PTR(void*, (IV)mgs_ix));
193 Perl_mg_size(pTHX_ SV *sv)
198 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
199 MGVTBL* vtbl = mg->mg_virtual;
200 if (vtbl && vtbl->svt_len) {
203 mgs_ix = SSNEW(sizeof(MGS));
204 save_magic(mgs_ix, sv);
205 /* omit MGf_GSKIP -- not changed here */
206 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
207 restore_magic(aTHXo_ INT2PTR(void*, (IV)mgs_ix));
214 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
219 Perl_croak(aTHX_ "Size magic not implemented");
228 Clear something magical that the SV represents. See C<sv_magic>.
234 Perl_mg_clear(pTHX_ SV *sv)
239 mgs_ix = SSNEW(sizeof(MGS));
240 save_magic(mgs_ix, sv);
242 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
243 MGVTBL* vtbl = mg->mg_virtual;
244 /* omit GSKIP -- never set here */
246 if (vtbl && vtbl->svt_clear)
247 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
250 restore_magic(aTHXo_ INT2PTR(void*, (IV)mgs_ix));
257 Finds the magic pointer for type matching the SV. See C<sv_magic>.
263 Perl_mg_find(pTHX_ SV *sv, int type)
268 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
269 if (mg->mg_type == type)
278 Copies the magic from one SV to another. See C<sv_magic>.
284 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
288 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
289 if (isUPPER(mg->mg_type)) {
291 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
292 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
294 toLOWER(mg->mg_type), key, klen);
304 Free any magic storage used by the SV. See C<sv_magic>.
310 Perl_mg_free(pTHX_ SV *sv)
314 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
315 MGVTBL* vtbl = mg->mg_virtual;
316 moremagic = mg->mg_moremagic;
317 if (vtbl && vtbl->svt_free)
318 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
319 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
321 Safefree(mg->mg_ptr);
322 else if (mg->mg_len == HEf_SVKEY)
323 SvREFCNT_dec((SV*)mg->mg_ptr);
325 if (mg->mg_flags & MGf_REFCOUNTED)
326 SvREFCNT_dec(mg->mg_obj);
334 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
339 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
343 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
344 if (mg->mg_obj) /* @+ */
347 return rx->lastparen;
354 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
362 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
366 if (paren <= rx->nparens &&
367 (s = rx->startp[paren]) != -1 &&
368 (t = rx->endp[paren]) != -1)
370 if (mg->mg_obj) /* @+ */
375 if (i > 0 && DO_UTF8(PL_reg_sv)) {
376 char *b = rx->subbeg;
378 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
388 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
390 Perl_croak(aTHX_ PL_no_modify);
396 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
403 switch (*mg->mg_ptr) {
404 case '1': case '2': case '3': case '4':
405 case '5': case '6': case '7': case '8': case '9': case '&':
406 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
408 paren = atoi(mg->mg_ptr); /* $& is in [0] */
410 if (paren <= rx->nparens &&
411 (s1 = rx->startp[paren]) != -1 &&
412 (t1 = rx->endp[paren]) != -1)
416 if (i > 0 && DO_UTF8(PL_reg_sv)) {
417 char *s = rx->subbeg + s1;
418 char *send = rx->subbeg + t1;
421 if (is_utf8_string((U8*)s, i))
422 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
425 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
431 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
432 paren = rx->lastparen;
437 case '\016': /* ^N */
438 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
439 paren = rx->lastcloseparen;
445 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
446 if (rx->startp[0] != -1) {
457 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
458 if (rx->endp[0] != -1) {
459 i = rx->sublen - rx->endp[0];
470 if (!SvPOK(sv) && SvNIOK(sv)) {
480 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
487 switch (*mg->mg_ptr) {
488 case '\001': /* ^A */
489 sv_setsv(sv, PL_bodytarget);
491 case '\003': /* ^C */
492 sv_setiv(sv, (IV)PL_minus_c);
495 case '\004': /* ^D */
496 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
497 #if defined(YYDEBUG) && defined(DEBUGGING)
498 PL_yydebug = DEBUG_p_TEST;
501 case '\005': /* ^E */
502 #ifdef MACOS_TRADITIONAL
506 sv_setnv(sv,(double)gMacPerl_OSErr);
507 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
512 # include <descrip.h>
513 # include <starlet.h>
515 $DESCRIPTOR(msgdsc,msg);
516 sv_setnv(sv,(NV) vaxc$errno);
517 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
518 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
524 if (!(_emx_env & 0x200)) { /* Under DOS */
525 sv_setnv(sv, (NV)errno);
526 sv_setpv(sv, errno ? Strerror(errno) : "");
528 if (errno != errno_isOS2) {
529 int tmp = _syserrno();
530 if (tmp) /* 2nd call to _syserrno() makes it 0 */
533 sv_setnv(sv, (NV)Perl_rc);
534 sv_setpv(sv, os2error(Perl_rc));
539 DWORD dwErr = GetLastError();
540 sv_setnv(sv, (NV)dwErr);
543 PerlProc_GetOSError(sv, dwErr);
550 sv_setnv(sv, (NV)errno);
551 sv_setpv(sv, errno ? Strerror(errno) : "");
556 SvNOK_on(sv); /* what a wonderful hack! */
558 case '\006': /* ^F */
559 sv_setiv(sv, (IV)PL_maxsysfd);
561 case '\010': /* ^H */
562 sv_setiv(sv, (IV)PL_hints);
564 case '\011': /* ^I */ /* NOT \t in EBCDIC */
566 sv_setpv(sv, PL_inplace);
568 sv_setsv(sv, &PL_sv_undef);
570 case '\017': /* ^O & ^OPEN */
571 if (*(mg->mg_ptr+1) == '\0')
572 sv_setpv(sv, PL_osname);
573 else if (strEQ(mg->mg_ptr, "\017PEN")) {
574 if (!PL_compiling.cop_io)
575 sv_setsv(sv, &PL_sv_undef);
577 sv_setsv(sv, PL_compiling.cop_io);
581 case '\020': /* ^P */
582 sv_setiv(sv, (IV)PL_perldb);
584 case '\023': /* ^S */
586 if (PL_lex_state != LEX_NOTPARSING)
589 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
594 case '\024': /* ^T */
596 sv_setnv(sv, PL_basetime);
598 sv_setiv(sv, (IV)PL_basetime);
601 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
602 if (*(mg->mg_ptr+1) == '\0')
603 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
604 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
605 if (PL_compiling.cop_warnings == pWARN_NONE ||
606 PL_compiling.cop_warnings == pWARN_STD)
608 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
610 else if (PL_compiling.cop_warnings == pWARN_ALL) {
611 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
614 sv_setsv(sv, PL_compiling.cop_warnings);
618 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
619 sv_setiv(sv, (IV)PL_widesyscalls);
621 case '1': case '2': case '3': case '4':
622 case '5': case '6': case '7': case '8': case '9': case '&':
623 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
627 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
628 * XXX Does the new way break anything?
630 paren = atoi(mg->mg_ptr); /* $& is in [0] */
632 if (paren <= rx->nparens &&
633 (s1 = rx->startp[paren]) != -1 &&
634 (t1 = rx->endp[paren]) != -1)
643 bool was_tainted = FALSE;
645 was_tainted = PL_tainted;
649 if (PL_reg_sv && DO_UTF8(PL_reg_sv) && is_utf8_string((U8*)s, i))
654 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
659 sv_setsv(sv,&PL_sv_undef);
662 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
663 paren = rx->lastparen;
667 sv_setsv(sv,&PL_sv_undef);
669 case '\016': /* ^N */
670 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
671 paren = rx->lastcloseparen;
675 sv_setsv(sv,&PL_sv_undef);
678 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
679 if ((s = rx->subbeg) && rx->startp[0] != -1) {
684 sv_setsv(sv,&PL_sv_undef);
687 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
688 if (rx->subbeg && rx->endp[0] != -1) {
689 s = rx->subbeg + rx->endp[0];
690 i = rx->sublen - rx->endp[0];
694 sv_setsv(sv,&PL_sv_undef);
698 if (GvIO(PL_last_in_gv)) {
699 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
705 sv_setiv(sv, (IV)STATUS_CURRENT);
706 #ifdef COMPLEX_STATUS
707 LvTARGOFF(sv) = PL_statusvalue;
708 LvTARGLEN(sv) = PL_statusvalue_vms;
713 s = IoTOP_NAME(GvIOp(PL_defoutgv));
717 sv_setpv(sv,GvENAME(PL_defoutgv));
722 s = IoFMT_NAME(GvIOp(PL_defoutgv));
724 s = GvENAME(PL_defoutgv);
729 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
732 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
735 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
743 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
746 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
753 sv_setpv(sv,PL_ofmt);
757 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
758 sv_setpv(sv, errno ? Strerror(errno) : "");
761 int saveerrno = errno;
762 sv_setnv(sv, (NV)errno);
764 if (errno == errno_isOS2 || errno == errno_isOS2_set)
765 sv_setpv(sv, os2error(Perl_rc));
768 sv_setpv(sv, errno ? Strerror(errno) : "");
772 SvNOK_on(sv); /* what a wonderful hack! */
775 sv_setiv(sv, (IV)PL_uid);
778 sv_setiv(sv, (IV)PL_euid);
781 sv_setiv(sv, (IV)PL_gid);
783 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
787 sv_setiv(sv, (IV)PL_egid);
789 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
794 Groups_t gary[NGROUPS];
795 i = getgroups(NGROUPS,gary);
797 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
800 (void)SvIOK_on(sv); /* what a wonderful hack! */
804 #ifndef MACOS_TRADITIONAL
810 sv_setsv(sv, thr->errsv);
812 #endif /* USE_THREADS */
818 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
820 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
822 if (uf && uf->uf_val)
823 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
828 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
839 #ifdef DYNAMIC_ENV_FETCH
840 /* We just undefd an environment var. Is a replacement */
841 /* waiting in the wings? */
844 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
845 s = SvPV(*valp, len);
849 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
850 /* And you'll never guess what the dog had */
851 /* in its mouth... */
853 MgTAINTEDDIR_off(mg);
855 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
856 char pathbuf[256], eltbuf[256], *cp, *elt = s;
860 do { /* DCL$PATH may be a search list */
861 while (1) { /* as may dev portion of any element */
862 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
863 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
864 cando_by_name(S_IWUSR,0,elt) ) {
869 if ((cp = strchr(elt, ':')) != Nullch)
871 if (my_trnlnm(elt, eltbuf, j++))
877 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
880 if (s && klen == 4 && strEQ(ptr,"PATH")) {
881 char *strend = s + len;
886 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
889 if (i >= sizeof tmpbuf /* too long -- assume the worst */
891 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
898 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
904 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
907 my_setenv(MgPV(mg,n_a),Nullch);
912 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
915 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
920 magic_clear_all_env(sv,mg);
921 hv_iterinit((HV*)sv);
922 while ((entry = hv_iternext((HV*)sv))) {
924 my_setenv(hv_iterkey(entry, &keylen),
925 SvPV(hv_iterval((HV*)sv, entry), n_a));
933 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
935 #if defined(VMS) || defined(EPOC)
936 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
938 # ifdef PERL_IMPLICIT_SYS
942 char *envv = GetEnvironmentStrings();
946 char *end = strchr(cur,'=');
947 if (end && end != cur) {
949 my_setenv(cur,Nullch);
951 cur = end + strlen(end+1)+2;
953 else if ((len = strlen(cur)))
956 FreeEnvironmentStrings(envv);
958 #ifdef USE_ENVIRON_ARRAY
959 # ifndef PERL_USE_SAFE_PUTENV
962 if (environ == PL_origenviron)
963 environ = (char**)safesysmalloc(sizeof(char*));
965 for (i = 0; environ[i]; i++)
966 safesysfree(environ[i]);
967 # endif /* PERL_USE_SAFE_PUTENV */
971 #endif /* USE_ENVIRON_ARRAY */
973 # endif /* PERL_IMPLICIT_SYS */
980 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
984 /* Are we fetching a signal entry? */
985 i = whichsig(MgPV(mg,n_a));
988 sv_setsv(sv,PL_psig_ptr[i]);
990 Sighandler_t sigstate = rsignal_state(i);
992 /* cache state so we don't fetch it again */
993 if(sigstate == SIG_IGN)
994 sv_setpv(sv,"IGNORE");
996 sv_setsv(sv,&PL_sv_undef);
997 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1004 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1008 /* Are we clearing a signal entry? */
1009 i = whichsig(MgPV(mg,n_a));
1011 if(PL_psig_ptr[i]) {
1012 SvREFCNT_dec(PL_psig_ptr[i]);
1015 if(PL_psig_name[i]) {
1016 SvREFCNT_dec(PL_psig_name[i]);
1024 Perl_raise_signal(pTHX_ int sig)
1026 /* Set a flag to say this signal is pending */
1027 PL_psig_pend[sig]++;
1028 /* And one to say _a_ signal is pending */
1033 Perl_csighandler(int sig)
1035 #ifdef PERL_OLD_SIGNALS
1036 /* Call the perl level handler now with risk we may be in malloc() etc. */
1037 (*PL_sighandlerp)(sig);
1040 Perl_raise_signal(aTHX_ sig);
1045 Perl_despatch_signals(pTHX)
1049 for (sig = 1; sig < SIG_SIZE; sig++) {
1050 if (PL_psig_pend[sig]) {
1051 PL_psig_pend[sig] = 0;
1052 (*PL_sighandlerp)(sig);
1058 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1067 if (strEQ(s,"__DIE__"))
1069 else if (strEQ(s,"__WARN__"))
1072 Perl_croak(aTHX_ "No such hook: %s", s);
1080 i = whichsig(s); /* ...no, a brick */
1082 if (ckWARN(WARN_SIGNAL))
1083 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1086 SvREFCNT_dec(PL_psig_name[i]);
1087 SvREFCNT_dec(PL_psig_ptr[i]);
1088 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1089 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1090 PL_psig_name[i] = newSVpvn(s, len);
1091 SvREADONLY_on(PL_psig_name[i]);
1093 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1095 (void)rsignal(i, &Perl_csighandler);
1097 *svp = SvREFCNT_inc(sv);
1100 s = SvPV_force(sv,len);
1101 if (strEQ(s,"IGNORE")) {
1103 (void)rsignal(i, SIG_IGN);
1107 else if (strEQ(s,"DEFAULT") || !*s) {
1109 (void)rsignal(i, SIG_DFL);
1115 * We should warn if HINT_STRICT_REFS, but without
1116 * access to a known hint bit in a known OP, we can't
1117 * tell whether HINT_STRICT_REFS is in force or not.
1119 if (!strchr(s,':') && !strchr(s,'\''))
1120 sv_insert(sv, 0, 0, "main::", 6);
1122 (void)rsignal(i, &Perl_csighandler);
1124 *svp = SvREFCNT_inc(sv);
1128 #endif /* !PERL_MICRO */
1131 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1133 PL_sub_generation++;
1138 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1140 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1141 PL_amagic_generation++;
1147 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1149 HV *hv = (HV*)LvTARG(sv);
1153 (void) hv_iterinit(hv);
1154 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1157 while (hv_iternext(hv))
1162 sv_setiv(sv, (IV)i);
1167 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1170 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1175 /* caller is responsible for stack switching/cleanup */
1177 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1183 PUSHs(SvTIED_obj(sv, mg));
1186 if (mg->mg_len >= 0)
1187 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1188 else if (mg->mg_len == HEf_SVKEY)
1189 PUSHs((SV*)mg->mg_ptr);
1191 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1192 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1200 return call_method(meth, flags);
1204 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1210 PUSHSTACKi(PERLSI_MAGIC);
1212 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1213 sv_setsv(sv, *PL_stack_sp--);
1223 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1225 magic_methpack(sv,mg,"FETCH");
1227 mg->mg_flags |= MGf_GSKIP;
1232 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1236 PUSHSTACKi(PERLSI_MAGIC);
1237 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1244 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1246 return magic_methpack(sv,mg,"DELETE");
1251 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1258 PUSHSTACKi(PERLSI_MAGIC);
1259 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1260 sv = *PL_stack_sp--;
1261 retval = (U32) SvIV(sv)-1;
1270 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1275 PUSHSTACKi(PERLSI_MAGIC);
1277 XPUSHs(SvTIED_obj(sv, mg));
1279 call_method("CLEAR", G_SCALAR|G_DISCARD);
1286 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1289 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1293 PUSHSTACKi(PERLSI_MAGIC);
1296 PUSHs(SvTIED_obj(sv, mg));
1301 if (call_method(meth, G_SCALAR))
1302 sv_setsv(key, *PL_stack_sp--);
1311 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1313 return magic_methpack(sv,mg,"EXISTS");
1317 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1327 svp = av_fetch(GvAV(gv),
1328 atoi(MgPV(mg,n_a)), FALSE);
1329 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1335 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1337 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1342 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1344 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1349 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1351 SV* lsv = LvTARG(sv);
1353 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1354 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1355 if (mg && mg->mg_len >= 0) {
1358 sv_pos_b2u(lsv, &i);
1359 sv_setiv(sv, i + PL_curcop->cop_arybase);
1368 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1370 SV* lsv = LvTARG(sv);
1377 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1378 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1382 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1383 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1385 else if (!SvOK(sv)) {
1389 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1391 pos = SvIV(sv) - PL_curcop->cop_arybase;
1394 ulen = sv_len_utf8(lsv);
1409 sv_pos_u2b(lsv, &p, 0);
1414 mg->mg_flags &= ~MGf_MINMATCH;
1420 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1422 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1424 gv_efullname3(sv,((GV*)sv), "*");
1428 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1433 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1442 if (*s == '*' && s[1])
1444 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1449 GvGP(sv) = gp_ref(GvGP(gv));
1454 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1457 SV *lsv = LvTARG(sv);
1458 char *tmps = SvPV(lsv,len);
1459 I32 offs = LvTARGOFF(sv);
1460 I32 rem = LvTARGLEN(sv);
1463 sv_pos_u2b(lsv, &offs, &rem);
1466 if (rem + offs > len)
1468 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1475 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1478 char *tmps = SvPV(sv, len);
1479 SV *lsv = LvTARG(sv);
1480 I32 lvoff = LvTARGOFF(sv);
1481 I32 lvlen = LvTARGLEN(sv);
1484 sv_utf8_upgrade(lsv);
1485 sv_pos_u2b(lsv, &lvoff, &lvlen);
1486 sv_insert(lsv, lvoff, lvlen, tmps, len);
1489 else if (SvUTF8(lsv)) {
1490 sv_pos_u2b(lsv, &lvoff, &lvlen);
1491 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1492 sv_insert(lsv, lvoff, lvlen, tmps, len);
1496 sv_insert(lsv, lvoff, lvlen, tmps, len);
1502 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1504 TAINT_IF((mg->mg_len & 1) ||
1505 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1510 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1512 if (PL_localizing) {
1513 if (PL_localizing == 1)
1518 else if (PL_tainted)
1526 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1528 SV *lsv = LvTARG(sv);
1535 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1540 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1542 do_vecset(sv); /* XXX slurp this routine */
1547 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1550 if (LvTARGLEN(sv)) {
1552 SV *ahv = LvTARG(sv);
1553 if (SvTYPE(ahv) == SVt_PVHV) {
1554 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1559 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1565 AV* av = (AV*)LvTARG(sv);
1566 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1567 targ = AvARRAY(av)[LvTARGOFF(sv)];
1569 if (targ && targ != &PL_sv_undef) {
1570 /* somebody else defined it for us */
1571 SvREFCNT_dec(LvTARG(sv));
1572 LvTARG(sv) = SvREFCNT_inc(targ);
1574 SvREFCNT_dec(mg->mg_obj);
1575 mg->mg_obj = Nullsv;
1576 mg->mg_flags &= ~MGf_REFCOUNTED;
1581 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1586 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1591 sv_setsv(LvTARG(sv), sv);
1592 SvSETMAGIC(LvTARG(sv));
1598 Perl_vivify_defelem(pTHX_ SV *sv)
1603 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1606 SV *ahv = LvTARG(sv);
1608 if (SvTYPE(ahv) == SVt_PVHV) {
1609 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1614 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1618 if (!value || value == &PL_sv_undef)
1619 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1622 AV* av = (AV*)LvTARG(sv);
1623 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1624 LvTARG(sv) = Nullsv; /* array can't be extended */
1626 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1627 if (!svp || (value = *svp) == &PL_sv_undef)
1628 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1631 (void)SvREFCNT_inc(value);
1632 SvREFCNT_dec(LvTARG(sv));
1635 SvREFCNT_dec(mg->mg_obj);
1636 mg->mg_obj = Nullsv;
1637 mg->mg_flags &= ~MGf_REFCOUNTED;
1641 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1643 AV *av = (AV*)mg->mg_obj;
1644 SV **svp = AvARRAY(av);
1645 I32 i = AvFILLp(av);
1647 if (svp[i] && svp[i] != &PL_sv_undef) {
1648 if (!SvWEAKREF(svp[i]))
1649 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1650 /* XXX Should we check that it hasn't changed? */
1652 (void)SvOK_off(svp[i]);
1653 SvWEAKREF_off(svp[i]);
1654 svp[i] = &PL_sv_undef;
1662 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1670 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1672 sv_unmagic(sv, PERL_MAGIC_bm);
1678 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1680 sv_unmagic(sv, PERL_MAGIC_fm);
1686 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1688 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1690 if (uf && uf->uf_set)
1691 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1696 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1698 regexp *re = (regexp *)mg->mg_obj;
1703 #ifdef USE_LOCALE_COLLATE
1705 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1708 * RenE<eacute> Descartes said "I think not."
1709 * and vanished with a faint plop.
1712 Safefree(mg->mg_ptr);
1718 #endif /* USE_LOCALE_COLLATE */
1721 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1726 switch (*mg->mg_ptr) {
1727 case '\001': /* ^A */
1728 sv_setsv(PL_bodytarget, sv);
1730 case '\003': /* ^C */
1731 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1734 case '\004': /* ^D */
1735 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1736 DEBUG_x(dump_all());
1738 case '\005': /* ^E */
1739 #ifdef MACOS_TRADITIONAL
1740 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1743 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1746 SetLastError( SvIV(sv) );
1749 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1751 /* will anyone ever use this? */
1752 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1758 case '\006': /* ^F */
1759 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1761 case '\010': /* ^H */
1762 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1764 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1766 Safefree(PL_inplace);
1768 PL_inplace = savepv(SvPV(sv,len));
1770 PL_inplace = Nullch;
1772 case '\017': /* ^O */
1773 if (*(mg->mg_ptr+1) == '\0') {
1775 Safefree(PL_osname);
1777 PL_osname = savepv(SvPV(sv,len));
1781 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1782 if (!PL_compiling.cop_io)
1783 PL_compiling.cop_io = newSVsv(sv);
1785 sv_setsv(PL_compiling.cop_io,sv);
1788 case '\020': /* ^P */
1789 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1790 if (PL_perldb && !PL_DBsingle)
1793 case '\024': /* ^T */
1795 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1797 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1800 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1801 if (*(mg->mg_ptr+1) == '\0') {
1802 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1803 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1804 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1805 | (i ? G_WARN_ON : G_WARN_OFF) ;
1808 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1809 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1810 if (!SvPOK(sv) && PL_localizing) {
1811 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1812 PL_compiling.cop_warnings = pWARN_NONE;
1817 int accumulate = 0 ;
1818 int any_fatals = 0 ;
1819 char * ptr = (char*)SvPV(sv, len) ;
1820 for (i = 0 ; i < len ; ++i) {
1821 accumulate |= ptr[i] ;
1822 any_fatals |= (ptr[i] & 0xAA) ;
1825 PL_compiling.cop_warnings = pWARN_NONE;
1826 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1827 PL_compiling.cop_warnings = pWARN_ALL;
1828 PL_dowarn |= G_WARN_ONCE ;
1831 if (specialWARN(PL_compiling.cop_warnings))
1832 PL_compiling.cop_warnings = newSVsv(sv) ;
1834 sv_setsv(PL_compiling.cop_warnings, sv);
1835 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1836 PL_dowarn |= G_WARN_ONCE ;
1842 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1843 PL_widesyscalls = SvTRUE(sv);
1846 if (PL_localizing) {
1847 if (PL_localizing == 1)
1848 SAVESPTR(PL_last_in_gv);
1850 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1851 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1854 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1855 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1856 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1859 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1860 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1861 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1864 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1867 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1868 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1869 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1872 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1876 IO *io = GvIOp(PL_defoutgv);
1877 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1878 IoFLAGS(io) &= ~IOf_FLUSH;
1880 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1881 PerlIO *ofp = IoOFP(io);
1883 (void)PerlIO_flush(ofp);
1884 IoFLAGS(io) |= IOf_FLUSH;
1890 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1891 PL_multiline = (i != 0);
1894 SvREFCNT_dec(PL_nrs);
1895 PL_nrs = newSVsv(sv);
1896 SvREFCNT_dec(PL_rs);
1897 PL_rs = SvREFCNT_inc(PL_nrs);
1901 SvREFCNT_dec(PL_ors_sv);
1902 if (SvOK(sv) || SvGMAGICAL(sv)) {
1903 PL_ors_sv = newSVsv(sv);
1911 SvREFCNT_dec(PL_ofs_sv);
1912 if (SvOK(sv) || SvGMAGICAL(sv)) {
1913 PL_ofs_sv = newSVsv(sv);
1922 PL_ofmt = savepv(SvPV(sv,len));
1925 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1928 #ifdef COMPLEX_STATUS
1929 if (PL_localizing == 2) {
1930 PL_statusvalue = LvTARGOFF(sv);
1931 PL_statusvalue_vms = LvTARGLEN(sv);
1935 #ifdef VMSISH_STATUS
1937 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1940 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1943 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1944 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1947 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1948 if (PL_delaymagic) {
1949 PL_delaymagic |= DM_RUID;
1950 break; /* don't do magic till later */
1953 (void)setruid((Uid_t)PL_uid);
1956 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1958 #ifdef HAS_SETRESUID
1959 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1961 if (PL_uid == PL_euid) /* special case $< = $> */
1962 (void)PerlProc_setuid(PL_uid);
1964 PL_uid = PerlProc_getuid();
1965 Perl_croak(aTHX_ "setruid() not implemented");
1970 PL_uid = PerlProc_getuid();
1971 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1974 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1975 if (PL_delaymagic) {
1976 PL_delaymagic |= DM_EUID;
1977 break; /* don't do magic till later */
1980 (void)seteuid((Uid_t)PL_euid);
1983 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1985 #ifdef HAS_SETRESUID
1986 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1988 if (PL_euid == PL_uid) /* special case $> = $< */
1989 PerlProc_setuid(PL_euid);
1991 PL_euid = PerlProc_geteuid();
1992 Perl_croak(aTHX_ "seteuid() not implemented");
1997 PL_euid = PerlProc_geteuid();
1998 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2001 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2002 if (PL_delaymagic) {
2003 PL_delaymagic |= DM_RGID;
2004 break; /* don't do magic till later */
2007 (void)setrgid((Gid_t)PL_gid);
2010 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2012 #ifdef HAS_SETRESGID
2013 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2015 if (PL_gid == PL_egid) /* special case $( = $) */
2016 (void)PerlProc_setgid(PL_gid);
2018 PL_gid = PerlProc_getgid();
2019 Perl_croak(aTHX_ "setrgid() not implemented");
2024 PL_gid = PerlProc_getgid();
2025 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2028 #ifdef HAS_SETGROUPS
2030 char *p = SvPV(sv, len);
2031 Groups_t gary[NGROUPS];
2036 for (i = 0; i < NGROUPS; ++i) {
2037 while (*p && !isSPACE(*p))
2046 (void)setgroups(i, gary);
2048 #else /* HAS_SETGROUPS */
2049 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2050 #endif /* HAS_SETGROUPS */
2051 if (PL_delaymagic) {
2052 PL_delaymagic |= DM_EGID;
2053 break; /* don't do magic till later */
2056 (void)setegid((Gid_t)PL_egid);
2059 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2061 #ifdef HAS_SETRESGID
2062 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2064 if (PL_egid == PL_gid) /* special case $) = $( */
2065 (void)PerlProc_setgid(PL_egid);
2067 PL_egid = PerlProc_getegid();
2068 Perl_croak(aTHX_ "setegid() not implemented");
2073 PL_egid = PerlProc_getegid();
2074 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2077 PL_chopset = SvPV_force(sv,len);
2079 #ifndef MACOS_TRADITIONAL
2081 #ifdef HAS_SETPROCTITLE
2082 /* The BSDs don't show the argv[] in ps(1) output, they
2083 * show a string from the process struct and provide
2084 * the setproctitle() routine to manipulate that. */
2087 # if __FreeBSD_version >= 410001
2088 /* The leading "-" removes the "perl: " prefix,
2089 * but not the "(perl) suffix from the ps(1)
2090 * output, because that's what ps(1) shows if the
2091 * argv[] is modified. */
2092 setproctitle("-%s", s);
2093 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2094 /* This doesn't really work if you assume that
2095 * $0 = 'foobar'; will wipe out 'perl' from the $0
2096 * because in ps(1) output the result will be like
2097 * sprintf("perl: %s (perl)", s)
2098 * I guess this is a security feature:
2099 * one (a user process) cannot get rid of the original name.
2101 setproctitle("%s", s);
2108 /* See if all the arguments are contiguous in memory */
2109 for (i = 1; i < PL_origargc; i++) {
2110 if (PL_origargv[i] == s + 1
2112 || PL_origargv[i] == s + 2
2117 s += strlen(s); /* this one is ok too */
2122 /* can grab env area too? */
2123 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2124 my_setenv("NoNe SuCh", Nullch);
2125 /* force copy of environment */
2126 for (i = 0; PL_origenviron[i]; i++)
2127 if (PL_origenviron[i] == s + 1) {
2134 PL_origalen = s - PL_origargv[0];
2136 s = SvPV_force(sv,len);
2138 if (i >= PL_origalen) {
2140 /* don't allow system to limit $0 seen by script */
2141 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2142 Copy(s, PL_origargv[0], i, char);
2143 s = PL_origargv[0]+i;
2147 Copy(s, PL_origargv[0], i, char);
2148 s = PL_origargv[0]+i;
2150 while (++i < PL_origalen)
2152 s = PL_origargv[0]+i;
2153 for (i = 1; i < PL_origargc; i++)
2154 PL_origargv[i] = Nullch;
2160 sv_setsv(thr->errsv, sv);
2162 #endif /* USE_THREADS */
2169 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2171 DEBUG_S(PerlIO_printf(Perl_debug_log,
2172 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2173 PTR2UV(thr), PTR2UV(sv)));
2175 Perl_croak(aTHX_ "panic: magic_mutexfree");
2176 MUTEX_DESTROY(MgMUTEXP(mg));
2177 COND_DESTROY(MgCONDP(mg));
2180 #endif /* USE_THREADS */
2183 Perl_whichsig(pTHX_ char *sig)
2185 register char **sigv;
2187 for (sigv = PL_sig_name+1; *sigv; sigv++)
2188 if (strEQ(sig,*sigv))
2189 return PL_sig_num[sigv - PL_sig_name];
2191 if (strEQ(sig,"CHLD"))
2195 if (strEQ(sig,"CLD"))
2204 Perl_sighandler(int sig)
2206 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2207 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2214 SV *sv = Nullsv, *tSv = PL_Sv;
2220 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2221 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2224 if (PL_savestack_ix + 15 <= PL_savestack_max)
2226 if (PL_markstack_ptr < PL_markstack_max - 2)
2228 if (PL_retstack_ix < PL_retstack_max - 2)
2230 if (PL_scopestack_ix < PL_scopestack_max - 3)
2233 if (!PL_psig_ptr[sig])
2234 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2237 /* Max number of items pushed there is 3*n or 4. We cannot fix
2238 infinity, so we fix 4 (in fact 5): */
2240 PL_savestack_ix += 5; /* Protect save in progress. */
2241 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2244 PL_markstack_ptr++; /* Protect mark. */
2247 PL_retstack[PL_retstack_ix] = NULL;
2250 PL_scopestack_ix += 1;
2251 /* sv_2cv is too complicated, try a simpler variant first: */
2252 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2253 || SvTYPE(cv) != SVt_PVCV)
2254 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2256 if (!cv || !CvROOT(cv)) {
2257 if (ckWARN(WARN_SIGNAL))
2258 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2259 PL_sig_name[sig], (gv ? GvENAME(gv)
2266 if(PL_psig_name[sig]) {
2267 sv = SvREFCNT_inc(PL_psig_name[sig]);
2271 sv = sv_newmortal();
2272 sv_setpv(sv,PL_sig_name[sig]);
2275 PUSHSTACKi(PERLSI_SIGNAL);
2280 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2283 if (SvTRUE(ERRSV)) {
2285 #ifdef HAS_SIGPROCMASK
2286 /* Handler "died", for example to get out of a restart-able read().
2287 * Before we re-do that on its behalf re-enable the signal which was
2288 * blocked by the system when we entered.
2292 sigaddset(&set,sig);
2293 sigprocmask(SIG_UNBLOCK, &set, NULL);
2295 /* Not clear if this will work */
2296 (void)rsignal(sig, SIG_IGN);
2297 (void)rsignal(sig, &Perl_csighandler);
2299 #endif /* !PERL_MICRO */
2300 Perl_die(aTHX_ Nullch);
2304 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2310 PL_scopestack_ix -= 1;
2313 PL_op = myop; /* Apparently not needed... */
2315 PL_Sv = tSv; /* Restore global temporaries. */
2326 restore_magic(pTHXo_ void *p)
2328 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2329 SV* sv = mgs->mgs_sv;
2334 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2337 SvFLAGS(sv) |= mgs->mgs_flags;
2341 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2344 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2346 /* If we're still on top of the stack, pop us off. (That condition
2347 * will be satisfied if restore_magic was called explicitly, but *not*
2348 * if it's being called via leave_scope.)
2349 * The reason for doing this is that otherwise, things like sv_2cv()
2350 * may leave alloc gunk on the savestack, and some code
2351 * (e.g. sighandler) doesn't expect that...
2353 if (PL_savestack_ix == mgs->mgs_ss_ix)
2355 I32 popval = SSPOPINT;
2356 assert(popval == SAVEt_DESTRUCTOR_X);
2357 PL_savestack_ix -= 2;
2359 assert(popval == SAVEt_ALLOC);
2361 PL_savestack_ix -= popval;
2367 unwind_handler_stack(pTHXo_ void *p)
2369 U32 flags = *(U32*)p;
2372 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2373 /* cxstack_ix-- Not needed, die already unwound it. */
2375 SvREFCNT_dec(sig_sv);