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)
25 static void restore_magic(pTHXo_ void *p);
26 static void unwind_handler_stack(pTHXo_ void *p);
29 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
37 /* MGS is typedef'ed to struct magic_state in perl.h */
40 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
43 assert(SvMAGICAL(sv));
45 SAVEDESTRUCTOR_X(restore_magic, (void*)mgs_ix);
47 mgs = SSPTR(mgs_ix, MGS*);
49 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
50 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
54 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
58 =for apidoc mg_magical
60 Turns on the magical status of an SV. See C<sv_magic>.
66 Perl_mg_magical(pTHX_ SV *sv)
69 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
70 MGVTBL* vtbl = mg->mg_virtual;
72 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
76 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
85 Do magic after a value is retrieved from the SV. See C<sv_magic>.
91 Perl_mg_get(pTHX_ SV *sv)
98 mgs_ix = SSNEW(sizeof(MGS));
99 save_magic(mgs_ix, sv);
102 while ((mg = *mgp) != 0) {
103 MGVTBL* vtbl = mg->mg_virtual;
104 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
105 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
106 /* Ignore this magic if it's been deleted */
107 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
108 (mg->mg_flags & MGf_GSKIP))
109 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
111 /* Advance to next magic (complicated by possible deletion) */
112 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
113 mgp = &mg->mg_moremagic;
117 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
120 restore_magic(aTHXo_ (void*)mgs_ix);
127 Do magic after a value is assigned to the SV. See C<sv_magic>.
133 Perl_mg_set(pTHX_ SV *sv)
139 mgs_ix = SSNEW(sizeof(MGS));
140 save_magic(mgs_ix, sv);
142 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
143 MGVTBL* vtbl = mg->mg_virtual;
144 nextmg = mg->mg_moremagic; /* it may delete itself */
145 if (mg->mg_flags & MGf_GSKIP) {
146 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
147 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
149 if (vtbl && vtbl->svt_set)
150 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
153 restore_magic(aTHXo_ (void*)mgs_ix);
158 =for apidoc mg_length
160 Report on the SV's length. See C<sv_magic>.
166 Perl_mg_length(pTHX_ SV *sv)
172 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 if (vtbl && vtbl->svt_len) {
177 mgs_ix = SSNEW(sizeof(MGS));
178 save_magic(mgs_ix, sv);
179 /* omit MGf_GSKIP -- not changed here */
180 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
181 restore_magic(aTHXo_ (void*)mgs_ix);
186 junk = SvPV(sv, len);
191 Perl_mg_size(pTHX_ SV *sv)
196 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
197 MGVTBL* vtbl = mg->mg_virtual;
198 if (vtbl && vtbl->svt_len) {
201 mgs_ix = SSNEW(sizeof(MGS));
202 save_magic(mgs_ix, sv);
203 /* omit MGf_GSKIP -- not changed here */
204 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
205 restore_magic(aTHXo_ (void*)mgs_ix);
212 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
217 Perl_croak(aTHX_ "Size magic not implemented");
226 Clear something magical that the SV represents. See C<sv_magic>.
232 Perl_mg_clear(pTHX_ SV *sv)
237 mgs_ix = SSNEW(sizeof(MGS));
238 save_magic(mgs_ix, sv);
240 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 /* omit GSKIP -- never set here */
244 if (vtbl && vtbl->svt_clear)
245 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
248 restore_magic(aTHXo_ (void*)mgs_ix);
255 Finds the magic pointer for type matching the SV. See C<sv_magic>.
261 Perl_mg_find(pTHX_ SV *sv, int type)
266 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
267 if (mg->mg_type == type)
276 Copies the magic from one SV to another. See C<sv_magic>.
282 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
286 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
287 if (isUPPER(mg->mg_type)) {
289 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) :
290 (mg->mg_type == 'D' && mg->mg_obj) ? sv : mg->mg_obj,
291 toLOWER(mg->mg_type), key, klen);
301 Free any magic storage used by the SV. See C<sv_magic>.
307 Perl_mg_free(pTHX_ SV *sv)
311 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
312 MGVTBL* vtbl = mg->mg_virtual;
313 moremagic = mg->mg_moremagic;
314 if (vtbl && vtbl->svt_free)
315 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
316 if (mg->mg_ptr && mg->mg_type != 'g') {
318 Safefree(mg->mg_ptr);
319 else if (mg->mg_len == HEf_SVKEY)
320 SvREFCNT_dec((SV*)mg->mg_ptr);
322 if (mg->mg_flags & MGf_REFCOUNTED)
323 SvREFCNT_dec(mg->mg_obj);
331 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
336 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
340 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
341 if (mg->mg_obj) /* @+ */
344 return rx->lastparen;
351 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
359 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
363 if (paren <= rx->nparens &&
364 (s = rx->startp[paren]) != -1 &&
365 (t = rx->endp[paren]) != -1)
367 if (mg->mg_obj) /* @+ */
372 if (i > 0 && DO_UTF8(PL_reg_sv)) {
373 char *b = rx->subbeg;
374 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
383 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
385 Perl_croak(aTHX_ PL_no_modify);
391 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
398 switch (*mg->mg_ptr) {
399 case '1': case '2': case '3': case '4':
400 case '5': case '6': case '7': case '8': case '9': case '&':
401 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
403 paren = atoi(mg->mg_ptr); /* $& is in [0] */
405 if (paren <= rx->nparens &&
406 (s1 = rx->startp[paren]) != -1 &&
407 (t1 = rx->endp[paren]) != -1)
411 if (i > 0 && DO_UTF8(PL_reg_sv)) {
412 char *s = rx->subbeg + s1;
413 char *send = rx->subbeg + t1;
415 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
418 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
424 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
425 paren = rx->lastparen;
431 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
432 if (rx->startp[0] != -1) {
443 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
444 if (rx->endp[0] != -1) {
445 i = rx->sublen - rx->endp[0];
456 if (!SvPOK(sv) && SvNIOK(sv)) {
466 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
473 switch (*mg->mg_ptr) {
474 case '\001': /* ^A */
475 sv_setsv(sv, PL_bodytarget);
477 case '\003': /* ^C */
478 sv_setiv(sv, (IV)PL_minus_c);
481 case '\004': /* ^D */
482 sv_setiv(sv, (IV)(PL_debug & 32767));
483 #if defined(YYDEBUG) && defined(DEBUGGING)
484 PL_yydebug = (PL_debug & 1);
487 case '\005': /* ^E */
488 #ifdef MACOS_TRADITIONAL
492 sv_setnv(sv,(double)gMacPerl_OSErr);
493 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
498 # include <descrip.h>
499 # include <starlet.h>
501 $DESCRIPTOR(msgdsc,msg);
502 sv_setnv(sv,(NV) vaxc$errno);
503 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
504 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
510 if (!(_emx_env & 0x200)) { /* Under DOS */
511 sv_setnv(sv, (NV)errno);
512 sv_setpv(sv, errno ? Strerror(errno) : "");
514 if (errno != errno_isOS2) {
515 int tmp = _syserrno();
516 if (tmp) /* 2nd call to _syserrno() makes it 0 */
519 sv_setnv(sv, (NV)Perl_rc);
520 sv_setpv(sv, os2error(Perl_rc));
525 DWORD dwErr = GetLastError();
526 sv_setnv(sv, (NV)dwErr);
529 PerlProc_GetOSError(sv, dwErr);
536 sv_setnv(sv, (NV)errno);
537 sv_setpv(sv, errno ? Strerror(errno) : "");
542 SvNOK_on(sv); /* what a wonderful hack! */
544 case '\006': /* ^F */
545 sv_setiv(sv, (IV)PL_maxsysfd);
547 case '\010': /* ^H */
548 sv_setiv(sv, (IV)PL_hints);
550 case '\011': /* ^I */ /* NOT \t in EBCDIC */
552 sv_setpv(sv, PL_inplace);
554 sv_setsv(sv, &PL_sv_undef);
556 case '\017': /* ^O & ^OPEN */
557 if (*(mg->mg_ptr+1) == '\0')
558 sv_setpv(sv, PL_osname);
559 else if (strEQ(mg->mg_ptr, "\017PEN")) {
560 if (!PL_compiling.cop_io)
561 sv_setsv(sv, &PL_sv_undef);
563 sv_setsv(sv, PL_compiling.cop_io);
567 case '\020': /* ^P */
568 sv_setiv(sv, (IV)PL_perldb);
570 case '\023': /* ^S */
572 if (PL_lex_state != LEX_NOTPARSING)
575 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
578 case '\024': /* ^T */
580 sv_setnv(sv, PL_basetime);
582 sv_setiv(sv, (IV)PL_basetime);
585 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
586 if (*(mg->mg_ptr+1) == '\0')
587 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
588 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
589 if (PL_compiling.cop_warnings == pWARN_NONE ||
590 PL_compiling.cop_warnings == pWARN_STD)
592 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
594 else if (PL_compiling.cop_warnings == pWARN_ALL) {
595 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
598 sv_setsv(sv, PL_compiling.cop_warnings);
602 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
603 sv_setiv(sv, (IV)PL_widesyscalls);
605 case '1': case '2': case '3': case '4':
606 case '5': case '6': case '7': case '8': case '9': case '&':
607 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
611 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
612 * XXX Does the new way break anything?
614 paren = atoi(mg->mg_ptr); /* $& is in [0] */
616 if (paren <= rx->nparens &&
617 (s1 = rx->startp[paren]) != -1 &&
618 (t1 = rx->endp[paren]) != -1)
629 was_tainted = PL_tainted;
633 if (DO_UTF8(PL_reg_sv))
638 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
643 sv_setsv(sv,&PL_sv_undef);
646 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
647 paren = rx->lastparen;
651 sv_setsv(sv,&PL_sv_undef);
654 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
655 if ((s = rx->subbeg) && rx->startp[0] != -1) {
660 sv_setsv(sv,&PL_sv_undef);
663 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
664 if (rx->subbeg && rx->endp[0] != -1) {
665 s = rx->subbeg + rx->endp[0];
666 i = rx->sublen - rx->endp[0];
670 sv_setsv(sv,&PL_sv_undef);
674 if (GvIO(PL_last_in_gv)) {
675 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
681 sv_setiv(sv, (IV)STATUS_CURRENT);
682 #ifdef COMPLEX_STATUS
683 LvTARGOFF(sv) = PL_statusvalue;
684 LvTARGLEN(sv) = PL_statusvalue_vms;
689 s = IoTOP_NAME(GvIOp(PL_defoutgv));
693 sv_setpv(sv,GvENAME(PL_defoutgv));
698 s = IoFMT_NAME(GvIOp(PL_defoutgv));
700 s = GvENAME(PL_defoutgv);
705 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
708 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
711 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
719 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
722 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
729 sv_setpv(sv,PL_ofmt);
733 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
734 sv_setpv(sv, errno ? Strerror(errno) : "");
737 int saveerrno = errno;
738 sv_setnv(sv, (NV)errno);
740 if (errno == errno_isOS2 || errno == errno_isOS2_set)
741 sv_setpv(sv, os2error(Perl_rc));
744 sv_setpv(sv, errno ? Strerror(errno) : "");
748 SvNOK_on(sv); /* what a wonderful hack! */
751 sv_setiv(sv, (IV)PL_uid);
754 sv_setiv(sv, (IV)PL_euid);
757 sv_setiv(sv, (IV)PL_gid);
759 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
763 sv_setiv(sv, (IV)PL_egid);
765 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
770 Groups_t gary[NGROUPS];
771 i = getgroups(NGROUPS,gary);
773 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
776 (void)SvIOK_on(sv); /* what a wonderful hack! */
780 #ifndef MACOS_TRADITIONAL
786 sv_setsv(sv, thr->errsv);
788 #endif /* USE_THREADS */
794 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
796 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
798 if (uf && uf->uf_val)
799 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
804 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
815 #ifdef DYNAMIC_ENV_FETCH
816 /* We just undefd an environment var. Is a replacement */
817 /* waiting in the wings? */
820 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
821 s = SvPV(*valp, len);
825 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
826 /* And you'll never guess what the dog had */
827 /* in its mouth... */
829 MgTAINTEDDIR_off(mg);
831 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
832 char pathbuf[256], eltbuf[256], *cp, *elt = s;
836 do { /* DCL$PATH may be a search list */
837 while (1) { /* as may dev portion of any element */
838 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
839 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
840 cando_by_name(S_IWUSR,0,elt) ) {
845 if ((cp = strchr(elt, ':')) != Nullch)
847 if (my_trnlnm(elt, eltbuf, j++))
853 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
856 if (s && klen == 4 && strEQ(ptr,"PATH")) {
857 char *strend = s + len;
862 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
865 if (i >= sizeof tmpbuf /* too long -- assume the worst */
867 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
874 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
880 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
883 my_setenv(MgPV(mg,n_a),Nullch);
888 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
891 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
896 magic_clear_all_env(sv,mg);
897 hv_iterinit((HV*)sv);
898 while ((entry = hv_iternext((HV*)sv))) {
900 my_setenv(hv_iterkey(entry, &keylen),
901 SvPV(hv_iterval((HV*)sv, entry), n_a));
909 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
911 #if defined(VMS) || defined(EPOC)
912 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
914 # ifdef PERL_IMPLICIT_SYS
918 char *envv = GetEnvironmentStrings();
922 char *end = strchr(cur,'=');
923 if (end && end != cur) {
925 my_setenv(cur,Nullch);
927 cur = end + strlen(end+1)+2;
929 else if ((len = strlen(cur)))
932 FreeEnvironmentStrings(envv);
934 #ifdef USE_ENVIRON_ARRAY
935 # ifndef PERL_USE_SAFE_PUTENV
938 if (environ == PL_origenviron)
939 environ = (char**)safesysmalloc(sizeof(char*));
941 for (i = 0; environ[i]; i++)
942 safesysfree(environ[i]);
943 # endif /* PERL_USE_SAFE_PUTENV */
947 #endif /* USE_ENVIRON_ARRAY */
949 # endif /* PERL_IMPLICIT_SYS */
956 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
960 /* Are we fetching a signal entry? */
961 i = whichsig(MgPV(mg,n_a));
964 sv_setsv(sv,PL_psig_ptr[i]);
966 Sighandler_t sigstate = rsignal_state(i);
968 /* cache state so we don't fetch it again */
969 if(sigstate == SIG_IGN)
970 sv_setpv(sv,"IGNORE");
972 sv_setsv(sv,&PL_sv_undef);
973 PL_psig_ptr[i] = SvREFCNT_inc(sv);
980 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
984 /* Are we clearing a signal entry? */
985 i = whichsig(MgPV(mg,n_a));
988 SvREFCNT_dec(PL_psig_ptr[i]);
991 if(PL_psig_name[i]) {
992 SvREFCNT_dec(PL_psig_name[i]);
1000 Perl_raise_signal(pTHX_ int sig)
1002 /* Set a flag to say this signal is pending */
1003 PL_psig_pend[sig]++;
1004 /* And one to say _a_ signal is pending */
1009 Perl_csighandler(int sig)
1011 #ifdef PERL_OLD_SIGNALS
1012 /* Call the perl level handler now with risk we may be in malloc() etc. */
1013 (*PL_sighandlerp)(sig);
1016 Perl_raise_signal(aTHX_ sig);
1021 Perl_despatch_signals(pTHX)
1025 for (sig = 1; sig < SIG_SIZE; sig++) {
1026 if (PL_psig_pend[sig]) {
1027 PL_psig_pend[sig] = 0;
1028 (*PL_sighandlerp)(sig);
1034 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1043 if (strEQ(s,"__DIE__"))
1045 else if (strEQ(s,"__WARN__"))
1048 Perl_croak(aTHX_ "No such hook: %s", s);
1056 i = whichsig(s); /* ...no, a brick */
1058 if (ckWARN(WARN_SIGNAL))
1059 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1062 SvREFCNT_dec(PL_psig_name[i]);
1063 SvREFCNT_dec(PL_psig_ptr[i]);
1064 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1065 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1066 PL_psig_name[i] = newSVpvn(s, len);
1067 SvREADONLY_on(PL_psig_name[i]);
1069 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1071 (void)rsignal(i, &Perl_csighandler);
1073 *svp = SvREFCNT_inc(sv);
1076 s = SvPV_force(sv,len);
1077 if (strEQ(s,"IGNORE")) {
1079 (void)rsignal(i, SIG_IGN);
1083 else if (strEQ(s,"DEFAULT") || !*s) {
1085 (void)rsignal(i, SIG_DFL);
1091 * We should warn if HINT_STRICT_REFS, but without
1092 * access to a known hint bit in a known OP, we can't
1093 * tell whether HINT_STRICT_REFS is in force or not.
1095 if (!strchr(s,':') && !strchr(s,'\''))
1096 sv_insert(sv, 0, 0, "main::", 6);
1098 (void)rsignal(i, &Perl_csighandler);
1100 *svp = SvREFCNT_inc(sv);
1104 #endif /* !PERL_MICRO */
1107 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1109 PL_sub_generation++;
1114 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1116 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1117 PL_amagic_generation++;
1123 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1125 HV *hv = (HV*)LvTARG(sv);
1130 (void) hv_iterinit(hv);
1131 if (! SvTIED_mg((SV*)hv, 'P'))
1135 while ((entry = hv_iternext(hv))) {
1141 sv_setiv(sv, (IV)i);
1146 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1149 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1154 /* caller is responsible for stack switching/cleanup */
1156 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1162 PUSHs(SvTIED_obj(sv, mg));
1165 if (mg->mg_len >= 0)
1166 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1167 else if (mg->mg_len == HEf_SVKEY)
1168 PUSHs((SV*)mg->mg_ptr);
1170 else if (mg->mg_type == 'p') {
1171 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1179 return call_method(meth, flags);
1183 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1189 PUSHSTACKi(PERLSI_MAGIC);
1191 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1192 sv_setsv(sv, *PL_stack_sp--);
1202 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1204 magic_methpack(sv,mg,"FETCH");
1206 mg->mg_flags |= MGf_GSKIP;
1211 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1215 PUSHSTACKi(PERLSI_MAGIC);
1216 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1223 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1225 return magic_methpack(sv,mg,"DELETE");
1230 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1237 PUSHSTACKi(PERLSI_MAGIC);
1238 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1239 sv = *PL_stack_sp--;
1240 retval = (U32) SvIV(sv)-1;
1249 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1254 PUSHSTACKi(PERLSI_MAGIC);
1256 XPUSHs(SvTIED_obj(sv, mg));
1258 call_method("CLEAR", G_SCALAR|G_DISCARD);
1265 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1268 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1272 PUSHSTACKi(PERLSI_MAGIC);
1275 PUSHs(SvTIED_obj(sv, mg));
1280 if (call_method(meth, G_SCALAR))
1281 sv_setsv(key, *PL_stack_sp--);
1290 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1292 return magic_methpack(sv,mg,"EXISTS");
1296 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1306 svp = av_fetch(GvAV(gv),
1307 atoi(MgPV(mg,n_a)), FALSE);
1308 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1314 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1316 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1321 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1323 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1328 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1330 SV* lsv = LvTARG(sv);
1332 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1333 mg = mg_find(lsv, 'g');
1334 if (mg && mg->mg_len >= 0) {
1337 sv_pos_b2u(lsv, &i);
1338 sv_setiv(sv, i + PL_curcop->cop_arybase);
1347 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1349 SV* lsv = LvTARG(sv);
1356 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1357 mg = mg_find(lsv, 'g');
1361 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1362 mg = mg_find(lsv, 'g');
1364 else if (!SvOK(sv)) {
1368 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1370 pos = SvIV(sv) - PL_curcop->cop_arybase;
1373 ulen = sv_len_utf8(lsv);
1388 sv_pos_u2b(lsv, &p, 0);
1393 mg->mg_flags &= ~MGf_MINMATCH;
1399 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1401 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1403 gv_efullname3(sv,((GV*)sv), "*");
1407 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1412 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1421 if (*s == '*' && s[1])
1423 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1428 GvGP(sv) = gp_ref(GvGP(gv));
1433 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1436 SV *lsv = LvTARG(sv);
1437 char *tmps = SvPV(lsv,len);
1438 I32 offs = LvTARGOFF(sv);
1439 I32 rem = LvTARGLEN(sv);
1442 sv_pos_u2b(lsv, &offs, &rem);
1445 if (rem + offs > len)
1447 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1454 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1457 char *tmps = SvPV(sv, len);
1458 SV *lsv = LvTARG(sv);
1459 I32 lvoff = LvTARGOFF(sv);
1460 I32 lvlen = LvTARGLEN(sv);
1463 sv_utf8_upgrade(lsv);
1464 sv_pos_u2b(lsv, &lvoff, &lvlen);
1465 sv_insert(lsv, lvoff, lvlen, tmps, len);
1468 else if (SvUTF8(lsv)) {
1469 sv_pos_u2b(lsv, &lvoff, &lvlen);
1470 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1471 sv_insert(lsv, lvoff, lvlen, tmps, len);
1475 sv_insert(lsv, lvoff, lvlen, tmps, len);
1481 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1483 TAINT_IF((mg->mg_len & 1) ||
1484 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1489 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1491 if (PL_localizing) {
1492 if (PL_localizing == 1)
1497 else if (PL_tainted)
1505 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1507 SV *lsv = LvTARG(sv);
1514 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1519 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1521 do_vecset(sv); /* XXX slurp this routine */
1526 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1529 if (LvTARGLEN(sv)) {
1531 SV *ahv = LvTARG(sv);
1532 if (SvTYPE(ahv) == SVt_PVHV) {
1533 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1538 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1544 AV* av = (AV*)LvTARG(sv);
1545 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1546 targ = AvARRAY(av)[LvTARGOFF(sv)];
1548 if (targ && targ != &PL_sv_undef) {
1549 /* somebody else defined it for us */
1550 SvREFCNT_dec(LvTARG(sv));
1551 LvTARG(sv) = SvREFCNT_inc(targ);
1553 SvREFCNT_dec(mg->mg_obj);
1554 mg->mg_obj = Nullsv;
1555 mg->mg_flags &= ~MGf_REFCOUNTED;
1560 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1565 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1570 sv_setsv(LvTARG(sv), sv);
1571 SvSETMAGIC(LvTARG(sv));
1577 Perl_vivify_defelem(pTHX_ SV *sv)
1582 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1585 SV *ahv = LvTARG(sv);
1587 if (SvTYPE(ahv) == SVt_PVHV) {
1588 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1593 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1597 if (!value || value == &PL_sv_undef)
1598 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1601 AV* av = (AV*)LvTARG(sv);
1602 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1603 LvTARG(sv) = Nullsv; /* array can't be extended */
1605 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1606 if (!svp || (value = *svp) == &PL_sv_undef)
1607 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1610 (void)SvREFCNT_inc(value);
1611 SvREFCNT_dec(LvTARG(sv));
1614 SvREFCNT_dec(mg->mg_obj);
1615 mg->mg_obj = Nullsv;
1616 mg->mg_flags &= ~MGf_REFCOUNTED;
1620 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1622 AV *av = (AV*)mg->mg_obj;
1623 SV **svp = AvARRAY(av);
1624 I32 i = AvFILLp(av);
1626 if (svp[i] && svp[i] != &PL_sv_undef) {
1627 if (!SvWEAKREF(svp[i]))
1628 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1629 /* XXX Should we check that it hasn't changed? */
1631 (void)SvOK_off(svp[i]);
1632 SvWEAKREF_off(svp[i]);
1633 svp[i] = &PL_sv_undef;
1641 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1649 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1651 sv_unmagic(sv, 'B');
1657 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1659 sv_unmagic(sv, 'f');
1665 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1667 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1669 if (uf && uf->uf_set)
1670 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1675 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1677 regexp *re = (regexp *)mg->mg_obj;
1682 #ifdef USE_LOCALE_COLLATE
1684 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1687 * RenE<eacute> Descartes said "I think not."
1688 * and vanished with a faint plop.
1691 Safefree(mg->mg_ptr);
1697 #endif /* USE_LOCALE_COLLATE */
1700 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1705 switch (*mg->mg_ptr) {
1706 case '\001': /* ^A */
1707 sv_setsv(PL_bodytarget, sv);
1709 case '\003': /* ^C */
1710 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1713 case '\004': /* ^D */
1714 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1715 DEBUG_x(dump_all());
1717 case '\005': /* ^E */
1718 #ifdef MACOS_TRADITIONAL
1719 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1722 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1725 SetLastError( SvIV(sv) );
1728 /* will anyone ever use this? */
1729 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1735 case '\006': /* ^F */
1736 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1738 case '\010': /* ^H */
1739 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1741 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1743 Safefree(PL_inplace);
1745 PL_inplace = savepv(SvPV(sv,len));
1747 PL_inplace = Nullch;
1749 case '\017': /* ^O */
1750 if (*(mg->mg_ptr+1) == '\0') {
1752 Safefree(PL_osname);
1754 PL_osname = savepv(SvPV(sv,len));
1758 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1759 if (!PL_compiling.cop_io)
1760 PL_compiling.cop_io = newSVsv(sv);
1762 sv_setsv(PL_compiling.cop_io,sv);
1765 case '\020': /* ^P */
1766 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1767 if (PL_perldb && !PL_DBsingle)
1770 case '\024': /* ^T */
1772 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1774 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1777 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1778 if (*(mg->mg_ptr+1) == '\0') {
1779 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1780 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1781 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1782 | (i ? G_WARN_ON : G_WARN_OFF) ;
1785 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1786 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1787 if (!SvPOK(sv) && PL_localizing) {
1788 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1789 PL_compiling.cop_warnings = pWARN_NONE;
1794 int accumulate = 0 ;
1795 int any_fatals = 0 ;
1796 char * ptr = (char*)SvPV(sv, len) ;
1797 for (i = 0 ; i < len ; ++i) {
1798 accumulate |= ptr[i] ;
1799 any_fatals |= (ptr[i] & 0xAA) ;
1802 PL_compiling.cop_warnings = pWARN_NONE;
1803 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1804 PL_compiling.cop_warnings = pWARN_ALL;
1805 PL_dowarn |= G_WARN_ONCE ;
1808 if (specialWARN(PL_compiling.cop_warnings))
1809 PL_compiling.cop_warnings = newSVsv(sv) ;
1811 sv_setsv(PL_compiling.cop_warnings, sv);
1812 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1813 PL_dowarn |= G_WARN_ONCE ;
1819 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1820 PL_widesyscalls = SvTRUE(sv);
1823 if (PL_localizing) {
1824 if (PL_localizing == 1)
1825 SAVESPTR(PL_last_in_gv);
1827 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1828 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1831 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1832 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1833 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1836 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1837 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1838 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1841 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1844 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1845 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1846 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1849 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1853 IO *io = GvIOp(PL_defoutgv);
1854 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1855 IoFLAGS(io) &= ~IOf_FLUSH;
1857 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1858 PerlIO *ofp = IoOFP(io);
1860 (void)PerlIO_flush(ofp);
1861 IoFLAGS(io) |= IOf_FLUSH;
1867 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1868 PL_multiline = (i != 0);
1871 SvREFCNT_dec(PL_nrs);
1872 PL_nrs = newSVsv(sv);
1873 SvREFCNT_dec(PL_rs);
1874 PL_rs = SvREFCNT_inc(PL_nrs);
1878 SvREFCNT_dec(PL_ors_sv);
1879 if (SvOK(sv) || SvGMAGICAL(sv)) {
1880 PL_ors_sv = newSVsv(sv);
1888 SvREFCNT_dec(PL_ofs_sv);
1889 if (SvOK(sv) || SvGMAGICAL(sv)) {
1890 PL_ofs_sv = newSVsv(sv);
1899 PL_ofmt = savepv(SvPV(sv,len));
1902 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1905 #ifdef COMPLEX_STATUS
1906 if (PL_localizing == 2) {
1907 PL_statusvalue = LvTARGOFF(sv);
1908 PL_statusvalue_vms = LvTARGLEN(sv);
1912 #ifdef VMSISH_STATUS
1914 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1917 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1920 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1921 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1924 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1925 if (PL_delaymagic) {
1926 PL_delaymagic |= DM_RUID;
1927 break; /* don't do magic till later */
1930 (void)setruid((Uid_t)PL_uid);
1933 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1935 #ifdef HAS_SETRESUID
1936 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1938 if (PL_uid == PL_euid) /* special case $< = $> */
1939 (void)PerlProc_setuid(PL_uid);
1941 PL_uid = PerlProc_getuid();
1942 Perl_croak(aTHX_ "setruid() not implemented");
1947 PL_uid = PerlProc_getuid();
1948 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1951 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1952 if (PL_delaymagic) {
1953 PL_delaymagic |= DM_EUID;
1954 break; /* don't do magic till later */
1957 (void)seteuid((Uid_t)PL_euid);
1960 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1962 #ifdef HAS_SETRESUID
1963 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1965 if (PL_euid == PL_uid) /* special case $> = $< */
1966 PerlProc_setuid(PL_euid);
1968 PL_euid = PerlProc_geteuid();
1969 Perl_croak(aTHX_ "seteuid() not implemented");
1974 PL_euid = PerlProc_geteuid();
1975 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1978 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1979 if (PL_delaymagic) {
1980 PL_delaymagic |= DM_RGID;
1981 break; /* don't do magic till later */
1984 (void)setrgid((Gid_t)PL_gid);
1987 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1989 #ifdef HAS_SETRESGID
1990 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1992 if (PL_gid == PL_egid) /* special case $( = $) */
1993 (void)PerlProc_setgid(PL_gid);
1995 PL_gid = PerlProc_getgid();
1996 Perl_croak(aTHX_ "setrgid() not implemented");
2001 PL_gid = PerlProc_getgid();
2002 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2005 #ifdef HAS_SETGROUPS
2007 char *p = SvPV(sv, len);
2008 Groups_t gary[NGROUPS];
2013 for (i = 0; i < NGROUPS; ++i) {
2014 while (*p && !isSPACE(*p))
2023 (void)setgroups(i, gary);
2025 #else /* HAS_SETGROUPS */
2026 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2027 #endif /* HAS_SETGROUPS */
2028 if (PL_delaymagic) {
2029 PL_delaymagic |= DM_EGID;
2030 break; /* don't do magic till later */
2033 (void)setegid((Gid_t)PL_egid);
2036 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2038 #ifdef HAS_SETRESGID
2039 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2041 if (PL_egid == PL_gid) /* special case $) = $( */
2042 (void)PerlProc_setgid(PL_egid);
2044 PL_egid = PerlProc_getegid();
2045 Perl_croak(aTHX_ "setegid() not implemented");
2050 PL_egid = PerlProc_getegid();
2051 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2054 PL_chopset = SvPV_force(sv,len);
2056 #ifndef MACOS_TRADITIONAL
2058 #ifdef HAS_SETPROCTITLE
2059 /* The BSDs don't show the argv[] in ps(1) output, they
2060 * show a string from the process struct and provide
2061 * the setproctitle() routine to manipulate that. */
2064 # if __FreeBSD_version >= 410001
2065 /* The leading "-" removes the "perl: " prefix,
2066 * but not the "(perl) suffix from the ps(1)
2067 * output, because that's what ps(1) shows if the
2068 * argv[] is modified. */
2069 setproctitle("-%s", s, len + 1);
2070 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2071 /* This doesn't really work if you assume that
2072 * $0 = 'foobar'; will wipe out 'perl' from the $0
2073 * because in ps(1) output the result will be like
2074 * sprintf("perl: %s (perl)", s)
2075 * I guess this is a security feature:
2076 * one (a user process) cannot get rid of the original name.
2078 setproctitle("%s", s);
2085 /* See if all the arguments are contiguous in memory */
2086 for (i = 1; i < PL_origargc; i++) {
2087 if (PL_origargv[i] == s + 1
2089 || PL_origargv[i] == s + 2
2094 s += strlen(s); /* this one is ok too */
2099 /* can grab env area too? */
2100 if (PL_origenviron && (PL_origenviron[0] == s + 1
2102 || (PL_origenviron[0] == s + 9 && (s += 8))
2105 my_setenv("NoNe SuCh", Nullch);
2106 /* force copy of environment */
2107 for (i = 0; PL_origenviron[i]; i++)
2108 if (PL_origenviron[i] == s + 1) {
2115 PL_origalen = s - PL_origargv[0];
2117 s = SvPV_force(sv,len);
2119 if (i >= PL_origalen) {
2121 /* don't allow system to limit $0 seen by script */
2122 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2123 Copy(s, PL_origargv[0], i, char);
2124 s = PL_origargv[0]+i;
2128 Copy(s, PL_origargv[0], i, char);
2129 s = PL_origargv[0]+i;
2131 while (++i < PL_origalen)
2133 s = PL_origargv[0]+i;
2134 for (i = 1; i < PL_origargc; i++)
2135 PL_origargv[i] = Nullch;
2141 sv_setsv(thr->errsv, sv);
2143 #endif /* USE_THREADS */
2150 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2152 DEBUG_S(PerlIO_printf(Perl_debug_log,
2153 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2154 PTR2UV(thr), PTR2UV(sv));)
2156 Perl_croak(aTHX_ "panic: magic_mutexfree");
2157 MUTEX_DESTROY(MgMUTEXP(mg));
2158 COND_DESTROY(MgCONDP(mg));
2161 #endif /* USE_THREADS */
2164 Perl_whichsig(pTHX_ char *sig)
2166 register char **sigv;
2168 for (sigv = PL_sig_name+1; *sigv; sigv++)
2169 if (strEQ(sig,*sigv))
2170 return PL_sig_num[sigv - PL_sig_name];
2172 if (strEQ(sig,"CHLD"))
2176 if (strEQ(sig,"CLD"))
2185 Perl_sighandler(int sig)
2187 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2188 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2195 SV *sv, *tSv = PL_Sv;
2199 I32 o_save_i = PL_savestack_ix;
2202 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2203 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2206 if (PL_savestack_ix + 15 <= PL_savestack_max)
2208 if (PL_markstack_ptr < PL_markstack_max - 2)
2210 if (PL_retstack_ix < PL_retstack_max - 2)
2212 if (PL_scopestack_ix < PL_scopestack_max - 3)
2215 if (!PL_psig_ptr[sig])
2216 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2219 /* Max number of items pushed there is 3*n or 4. We cannot fix
2220 infinity, so we fix 4 (in fact 5): */
2222 PL_savestack_ix += 5; /* Protect save in progress. */
2223 o_save_i = PL_savestack_ix;
2224 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2227 PL_markstack_ptr++; /* Protect mark. */
2230 PL_retstack[PL_retstack_ix] = NULL;
2233 PL_scopestack_ix += 1;
2234 /* sv_2cv is too complicated, try a simpler variant first: */
2235 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2236 || SvTYPE(cv) != SVt_PVCV)
2237 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2239 if (!cv || !CvROOT(cv)) {
2240 if (ckWARN(WARN_SIGNAL))
2241 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2242 PL_sig_name[sig], (gv ? GvENAME(gv)
2249 if(PL_psig_name[sig]) {
2250 sv = SvREFCNT_inc(PL_psig_name[sig]);
2254 sv = sv_newmortal();
2255 sv_setpv(sv,PL_sig_name[sig]);
2258 PUSHSTACKi(PERLSI_SIGNAL);
2263 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2266 if (SvTRUE(ERRSV)) {
2268 #ifdef HAS_SIGPROCMASK
2269 /* Handler "died", for example to get out of a restart-able read().
2270 * Before we re-do that on its behalf re-enable the signal which was
2271 * blocked by the system when we entered.
2275 sigaddset(&set,sig);
2276 sigprocmask(SIG_UNBLOCK, &set, NULL);
2278 /* Not clear if this will work */
2279 (void)rsignal(sig, SIG_IGN);
2280 (void)rsignal(sig, &Perl_csighandler);
2282 #endif /* !PERL_MICRO */
2283 Perl_die(aTHX_ Nullch);
2287 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2293 PL_scopestack_ix -= 1;
2296 PL_op = myop; /* Apparently not needed... */
2298 PL_Sv = tSv; /* Restore global temporaries. */
2309 restore_magic(pTHXo_ void *p)
2311 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2312 SV* sv = mgs->mgs_sv;
2317 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2320 SvFLAGS(sv) |= mgs->mgs_flags;
2324 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2327 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2329 /* If we're still on top of the stack, pop us off. (That condition
2330 * will be satisfied if restore_magic was called explicitly, but *not*
2331 * if it's being called via leave_scope.)
2332 * The reason for doing this is that otherwise, things like sv_2cv()
2333 * may leave alloc gunk on the savestack, and some code
2334 * (e.g. sighandler) doesn't expect that...
2336 if (PL_savestack_ix == mgs->mgs_ss_ix)
2338 I32 popval = SSPOPINT;
2339 assert(popval == SAVEt_DESTRUCTOR_X);
2340 PL_savestack_ix -= 2;
2342 assert(popval == SAVEt_ALLOC);
2344 PL_savestack_ix -= popval;
2350 unwind_handler_stack(pTHXo_ void *p)
2352 U32 flags = *(U32*)p;
2355 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2356 /* cxstack_ix-- Not needed, die already unwound it. */
2358 SvREFCNT_dec(sig_sv);