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 == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
290 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
292 toLOWER(mg->mg_type), key, klen);
302 Free any magic storage used by the SV. See C<sv_magic>.
308 Perl_mg_free(pTHX_ SV *sv)
312 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
313 MGVTBL* vtbl = mg->mg_virtual;
314 moremagic = mg->mg_moremagic;
315 if (vtbl && vtbl->svt_free)
316 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
317 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
319 Safefree(mg->mg_ptr);
320 else if (mg->mg_len == HEf_SVKEY)
321 SvREFCNT_dec((SV*)mg->mg_ptr);
323 if (mg->mg_flags & MGf_REFCOUNTED)
324 SvREFCNT_dec(mg->mg_obj);
332 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
337 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
341 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
342 if (mg->mg_obj) /* @+ */
345 return rx->lastparen;
352 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
360 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
364 if (paren <= rx->nparens &&
365 (s = rx->startp[paren]) != -1 &&
366 (t = rx->endp[paren]) != -1)
368 if (mg->mg_obj) /* @+ */
373 if (i > 0 && DO_UTF8(PL_reg_sv)) {
374 char *b = rx->subbeg;
375 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
384 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
386 Perl_croak(aTHX_ PL_no_modify);
392 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
399 switch (*mg->mg_ptr) {
400 case '1': case '2': case '3': case '4':
401 case '5': case '6': case '7': case '8': case '9': case '&':
402 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
404 paren = atoi(mg->mg_ptr); /* $& is in [0] */
406 if (paren <= rx->nparens &&
407 (s1 = rx->startp[paren]) != -1 &&
408 (t1 = rx->endp[paren]) != -1)
412 if (i > 0 && DO_UTF8(PL_reg_sv)) {
413 char *s = rx->subbeg + s1;
414 char *send = rx->subbeg + t1;
417 if (is_utf8_string((U8*)s, i))
418 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
421 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
427 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
428 paren = rx->lastparen;
434 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
435 if (rx->startp[0] != -1) {
446 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
447 if (rx->endp[0] != -1) {
448 i = rx->sublen - rx->endp[0];
459 if (!SvPOK(sv) && SvNIOK(sv)) {
469 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
476 switch (*mg->mg_ptr) {
477 case '\001': /* ^A */
478 sv_setsv(sv, PL_bodytarget);
480 case '\003': /* ^C */
481 sv_setiv(sv, (IV)PL_minus_c);
484 case '\004': /* ^D */
485 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
486 #if defined(YYDEBUG) && defined(DEBUGGING)
487 PL_yydebug = DEBUG_p_TEST;
490 case '\005': /* ^E */
491 #ifdef MACOS_TRADITIONAL
495 sv_setnv(sv,(double)gMacPerl_OSErr);
496 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
501 # include <descrip.h>
502 # include <starlet.h>
504 $DESCRIPTOR(msgdsc,msg);
505 sv_setnv(sv,(NV) vaxc$errno);
506 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
507 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
513 if (!(_emx_env & 0x200)) { /* Under DOS */
514 sv_setnv(sv, (NV)errno);
515 sv_setpv(sv, errno ? Strerror(errno) : "");
517 if (errno != errno_isOS2) {
518 int tmp = _syserrno();
519 if (tmp) /* 2nd call to _syserrno() makes it 0 */
522 sv_setnv(sv, (NV)Perl_rc);
523 sv_setpv(sv, os2error(Perl_rc));
528 DWORD dwErr = GetLastError();
529 sv_setnv(sv, (NV)dwErr);
532 PerlProc_GetOSError(sv, dwErr);
539 sv_setnv(sv, (NV)errno);
540 sv_setpv(sv, errno ? Strerror(errno) : "");
545 SvNOK_on(sv); /* what a wonderful hack! */
547 case '\006': /* ^F */
548 sv_setiv(sv, (IV)PL_maxsysfd);
550 case '\010': /* ^H */
551 sv_setiv(sv, (IV)PL_hints);
553 case '\011': /* ^I */ /* NOT \t in EBCDIC */
555 sv_setpv(sv, PL_inplace);
557 sv_setsv(sv, &PL_sv_undef);
559 case '\017': /* ^O & ^OPEN */
560 if (*(mg->mg_ptr+1) == '\0')
561 sv_setpv(sv, PL_osname);
562 else if (strEQ(mg->mg_ptr, "\017PEN")) {
563 if (!PL_compiling.cop_io)
564 sv_setsv(sv, &PL_sv_undef);
566 sv_setsv(sv, PL_compiling.cop_io);
570 case '\020': /* ^P */
571 sv_setiv(sv, (IV)PL_perldb);
573 case '\023': /* ^S */
575 if (PL_lex_state != LEX_NOTPARSING)
578 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
581 case '\024': /* ^T */
583 sv_setnv(sv, PL_basetime);
585 sv_setiv(sv, (IV)PL_basetime);
588 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
589 if (*(mg->mg_ptr+1) == '\0')
590 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
591 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
592 if (PL_compiling.cop_warnings == pWARN_NONE ||
593 PL_compiling.cop_warnings == pWARN_STD)
595 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
597 else if (PL_compiling.cop_warnings == pWARN_ALL) {
598 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
601 sv_setsv(sv, PL_compiling.cop_warnings);
605 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
606 sv_setiv(sv, (IV)PL_widesyscalls);
608 case '1': case '2': case '3': case '4':
609 case '5': case '6': case '7': case '8': case '9': case '&':
610 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
614 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
615 * XXX Does the new way break anything?
617 paren = atoi(mg->mg_ptr); /* $& is in [0] */
619 if (paren <= rx->nparens &&
620 (s1 = rx->startp[paren]) != -1 &&
621 (t1 = rx->endp[paren]) != -1)
632 was_tainted = PL_tainted;
636 if (DO_UTF8(PL_reg_sv) && is_utf8_string((U8*)s, i))
641 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
646 sv_setsv(sv,&PL_sv_undef);
649 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
650 paren = rx->lastparen;
654 sv_setsv(sv,&PL_sv_undef);
657 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
658 if ((s = rx->subbeg) && rx->startp[0] != -1) {
663 sv_setsv(sv,&PL_sv_undef);
666 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
667 if (rx->subbeg && rx->endp[0] != -1) {
668 s = rx->subbeg + rx->endp[0];
669 i = rx->sublen - rx->endp[0];
673 sv_setsv(sv,&PL_sv_undef);
677 if (GvIO(PL_last_in_gv)) {
678 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
684 sv_setiv(sv, (IV)STATUS_CURRENT);
685 #ifdef COMPLEX_STATUS
686 LvTARGOFF(sv) = PL_statusvalue;
687 LvTARGLEN(sv) = PL_statusvalue_vms;
692 s = IoTOP_NAME(GvIOp(PL_defoutgv));
696 sv_setpv(sv,GvENAME(PL_defoutgv));
701 s = IoFMT_NAME(GvIOp(PL_defoutgv));
703 s = GvENAME(PL_defoutgv);
708 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
711 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
714 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
722 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
725 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
732 sv_setpv(sv,PL_ofmt);
736 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
737 sv_setpv(sv, errno ? Strerror(errno) : "");
740 int saveerrno = errno;
741 sv_setnv(sv, (NV)errno);
743 if (errno == errno_isOS2 || errno == errno_isOS2_set)
744 sv_setpv(sv, os2error(Perl_rc));
747 sv_setpv(sv, errno ? Strerror(errno) : "");
751 SvNOK_on(sv); /* what a wonderful hack! */
754 sv_setiv(sv, (IV)PL_uid);
757 sv_setiv(sv, (IV)PL_euid);
760 sv_setiv(sv, (IV)PL_gid);
762 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
766 sv_setiv(sv, (IV)PL_egid);
768 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
773 Groups_t gary[NGROUPS];
774 i = getgroups(NGROUPS,gary);
776 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
779 (void)SvIOK_on(sv); /* what a wonderful hack! */
783 #ifndef MACOS_TRADITIONAL
789 sv_setsv(sv, thr->errsv);
791 #endif /* USE_THREADS */
797 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
799 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
801 if (uf && uf->uf_val)
802 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
807 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
818 #ifdef DYNAMIC_ENV_FETCH
819 /* We just undefd an environment var. Is a replacement */
820 /* waiting in the wings? */
823 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
824 s = SvPV(*valp, len);
828 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
829 /* And you'll never guess what the dog had */
830 /* in its mouth... */
832 MgTAINTEDDIR_off(mg);
834 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
835 char pathbuf[256], eltbuf[256], *cp, *elt = s;
839 do { /* DCL$PATH may be a search list */
840 while (1) { /* as may dev portion of any element */
841 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
842 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
843 cando_by_name(S_IWUSR,0,elt) ) {
848 if ((cp = strchr(elt, ':')) != Nullch)
850 if (my_trnlnm(elt, eltbuf, j++))
856 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
859 if (s && klen == 4 && strEQ(ptr,"PATH")) {
860 char *strend = s + len;
865 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
868 if (i >= sizeof tmpbuf /* too long -- assume the worst */
870 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
877 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
883 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
886 my_setenv(MgPV(mg,n_a),Nullch);
891 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
894 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
899 magic_clear_all_env(sv,mg);
900 hv_iterinit((HV*)sv);
901 while ((entry = hv_iternext((HV*)sv))) {
903 my_setenv(hv_iterkey(entry, &keylen),
904 SvPV(hv_iterval((HV*)sv, entry), n_a));
912 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
914 #if defined(VMS) || defined(EPOC)
915 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
917 # ifdef PERL_IMPLICIT_SYS
921 char *envv = GetEnvironmentStrings();
925 char *end = strchr(cur,'=');
926 if (end && end != cur) {
928 my_setenv(cur,Nullch);
930 cur = end + strlen(end+1)+2;
932 else if ((len = strlen(cur)))
935 FreeEnvironmentStrings(envv);
937 #ifdef USE_ENVIRON_ARRAY
938 # ifndef PERL_USE_SAFE_PUTENV
941 if (environ == PL_origenviron)
942 environ = (char**)safesysmalloc(sizeof(char*));
944 for (i = 0; environ[i]; i++)
945 safesysfree(environ[i]);
946 # endif /* PERL_USE_SAFE_PUTENV */
950 #endif /* USE_ENVIRON_ARRAY */
952 # endif /* PERL_IMPLICIT_SYS */
959 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
963 /* Are we fetching a signal entry? */
964 i = whichsig(MgPV(mg,n_a));
967 sv_setsv(sv,PL_psig_ptr[i]);
969 Sighandler_t sigstate = rsignal_state(i);
971 /* cache state so we don't fetch it again */
972 if(sigstate == SIG_IGN)
973 sv_setpv(sv,"IGNORE");
975 sv_setsv(sv,&PL_sv_undef);
976 PL_psig_ptr[i] = SvREFCNT_inc(sv);
983 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
987 /* Are we clearing a signal entry? */
988 i = whichsig(MgPV(mg,n_a));
991 SvREFCNT_dec(PL_psig_ptr[i]);
994 if(PL_psig_name[i]) {
995 SvREFCNT_dec(PL_psig_name[i]);
1003 Perl_raise_signal(pTHX_ int sig)
1005 /* Set a flag to say this signal is pending */
1006 PL_psig_pend[sig]++;
1007 /* And one to say _a_ signal is pending */
1012 Perl_csighandler(int sig)
1014 #ifdef PERL_OLD_SIGNALS
1015 /* Call the perl level handler now with risk we may be in malloc() etc. */
1016 (*PL_sighandlerp)(sig);
1019 Perl_raise_signal(aTHX_ sig);
1024 Perl_despatch_signals(pTHX)
1028 for (sig = 1; sig < SIG_SIZE; sig++) {
1029 if (PL_psig_pend[sig]) {
1030 PL_psig_pend[sig] = 0;
1031 (*PL_sighandlerp)(sig);
1037 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1046 if (strEQ(s,"__DIE__"))
1048 else if (strEQ(s,"__WARN__"))
1051 Perl_croak(aTHX_ "No such hook: %s", s);
1059 i = whichsig(s); /* ...no, a brick */
1061 if (ckWARN(WARN_SIGNAL))
1062 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1065 SvREFCNT_dec(PL_psig_name[i]);
1066 SvREFCNT_dec(PL_psig_ptr[i]);
1067 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1068 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1069 PL_psig_name[i] = newSVpvn(s, len);
1070 SvREADONLY_on(PL_psig_name[i]);
1072 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1074 (void)rsignal(i, &Perl_csighandler);
1076 *svp = SvREFCNT_inc(sv);
1079 s = SvPV_force(sv,len);
1080 if (strEQ(s,"IGNORE")) {
1082 (void)rsignal(i, SIG_IGN);
1086 else if (strEQ(s,"DEFAULT") || !*s) {
1088 (void)rsignal(i, SIG_DFL);
1094 * We should warn if HINT_STRICT_REFS, but without
1095 * access to a known hint bit in a known OP, we can't
1096 * tell whether HINT_STRICT_REFS is in force or not.
1098 if (!strchr(s,':') && !strchr(s,'\''))
1099 sv_insert(sv, 0, 0, "main::", 6);
1101 (void)rsignal(i, &Perl_csighandler);
1103 *svp = SvREFCNT_inc(sv);
1107 #endif /* !PERL_MICRO */
1110 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1112 PL_sub_generation++;
1117 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1119 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1120 PL_amagic_generation++;
1126 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1128 HV *hv = (HV*)LvTARG(sv);
1133 (void) hv_iterinit(hv);
1134 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1138 while ((entry = hv_iternext(hv))) {
1144 sv_setiv(sv, (IV)i);
1149 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1152 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1157 /* caller is responsible for stack switching/cleanup */
1159 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1165 PUSHs(SvTIED_obj(sv, mg));
1168 if (mg->mg_len >= 0)
1169 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1170 else if (mg->mg_len == HEf_SVKEY)
1171 PUSHs((SV*)mg->mg_ptr);
1173 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1174 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1182 return call_method(meth, flags);
1186 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1192 PUSHSTACKi(PERLSI_MAGIC);
1194 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1195 sv_setsv(sv, *PL_stack_sp--);
1205 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1207 magic_methpack(sv,mg,"FETCH");
1209 mg->mg_flags |= MGf_GSKIP;
1214 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1218 PUSHSTACKi(PERLSI_MAGIC);
1219 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1226 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1228 return magic_methpack(sv,mg,"DELETE");
1233 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1240 PUSHSTACKi(PERLSI_MAGIC);
1241 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1242 sv = *PL_stack_sp--;
1243 retval = (U32) SvIV(sv)-1;
1252 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1257 PUSHSTACKi(PERLSI_MAGIC);
1259 XPUSHs(SvTIED_obj(sv, mg));
1261 call_method("CLEAR", G_SCALAR|G_DISCARD);
1268 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1271 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1275 PUSHSTACKi(PERLSI_MAGIC);
1278 PUSHs(SvTIED_obj(sv, mg));
1283 if (call_method(meth, G_SCALAR))
1284 sv_setsv(key, *PL_stack_sp--);
1293 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1295 return magic_methpack(sv,mg,"EXISTS");
1299 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1309 svp = av_fetch(GvAV(gv),
1310 atoi(MgPV(mg,n_a)), FALSE);
1311 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1317 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1319 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1324 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1326 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1331 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1333 SV* lsv = LvTARG(sv);
1335 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1336 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1337 if (mg && mg->mg_len >= 0) {
1340 sv_pos_b2u(lsv, &i);
1341 sv_setiv(sv, i + PL_curcop->cop_arybase);
1350 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1352 SV* lsv = LvTARG(sv);
1359 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1360 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1364 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1365 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1367 else if (!SvOK(sv)) {
1371 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1373 pos = SvIV(sv) - PL_curcop->cop_arybase;
1376 ulen = sv_len_utf8(lsv);
1391 sv_pos_u2b(lsv, &p, 0);
1396 mg->mg_flags &= ~MGf_MINMATCH;
1402 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1404 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1406 gv_efullname3(sv,((GV*)sv), "*");
1410 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1415 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1424 if (*s == '*' && s[1])
1426 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1431 GvGP(sv) = gp_ref(GvGP(gv));
1436 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1439 SV *lsv = LvTARG(sv);
1440 char *tmps = SvPV(lsv,len);
1441 I32 offs = LvTARGOFF(sv);
1442 I32 rem = LvTARGLEN(sv);
1445 sv_pos_u2b(lsv, &offs, &rem);
1448 if (rem + offs > len)
1450 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1457 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1460 char *tmps = SvPV(sv, len);
1461 SV *lsv = LvTARG(sv);
1462 I32 lvoff = LvTARGOFF(sv);
1463 I32 lvlen = LvTARGLEN(sv);
1466 sv_utf8_upgrade(lsv);
1467 sv_pos_u2b(lsv, &lvoff, &lvlen);
1468 sv_insert(lsv, lvoff, lvlen, tmps, len);
1471 else if (SvUTF8(lsv)) {
1472 sv_pos_u2b(lsv, &lvoff, &lvlen);
1473 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1474 sv_insert(lsv, lvoff, lvlen, tmps, len);
1478 sv_insert(lsv, lvoff, lvlen, tmps, len);
1484 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1486 TAINT_IF((mg->mg_len & 1) ||
1487 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1492 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1494 if (PL_localizing) {
1495 if (PL_localizing == 1)
1500 else if (PL_tainted)
1508 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1510 SV *lsv = LvTARG(sv);
1517 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1522 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1524 do_vecset(sv); /* XXX slurp this routine */
1529 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1532 if (LvTARGLEN(sv)) {
1534 SV *ahv = LvTARG(sv);
1535 if (SvTYPE(ahv) == SVt_PVHV) {
1536 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1541 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1547 AV* av = (AV*)LvTARG(sv);
1548 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1549 targ = AvARRAY(av)[LvTARGOFF(sv)];
1551 if (targ && targ != &PL_sv_undef) {
1552 /* somebody else defined it for us */
1553 SvREFCNT_dec(LvTARG(sv));
1554 LvTARG(sv) = SvREFCNT_inc(targ);
1556 SvREFCNT_dec(mg->mg_obj);
1557 mg->mg_obj = Nullsv;
1558 mg->mg_flags &= ~MGf_REFCOUNTED;
1563 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1568 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1573 sv_setsv(LvTARG(sv), sv);
1574 SvSETMAGIC(LvTARG(sv));
1580 Perl_vivify_defelem(pTHX_ SV *sv)
1585 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1588 SV *ahv = LvTARG(sv);
1590 if (SvTYPE(ahv) == SVt_PVHV) {
1591 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1596 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1600 if (!value || value == &PL_sv_undef)
1601 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1604 AV* av = (AV*)LvTARG(sv);
1605 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1606 LvTARG(sv) = Nullsv; /* array can't be extended */
1608 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1609 if (!svp || (value = *svp) == &PL_sv_undef)
1610 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1613 (void)SvREFCNT_inc(value);
1614 SvREFCNT_dec(LvTARG(sv));
1617 SvREFCNT_dec(mg->mg_obj);
1618 mg->mg_obj = Nullsv;
1619 mg->mg_flags &= ~MGf_REFCOUNTED;
1623 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1625 AV *av = (AV*)mg->mg_obj;
1626 SV **svp = AvARRAY(av);
1627 I32 i = AvFILLp(av);
1629 if (svp[i] && svp[i] != &PL_sv_undef) {
1630 if (!SvWEAKREF(svp[i]))
1631 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1632 /* XXX Should we check that it hasn't changed? */
1634 (void)SvOK_off(svp[i]);
1635 SvWEAKREF_off(svp[i]);
1636 svp[i] = &PL_sv_undef;
1644 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1652 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1654 sv_unmagic(sv, PERL_MAGIC_bm);
1660 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1662 sv_unmagic(sv, PERL_MAGIC_fm);
1668 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1670 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1672 if (uf && uf->uf_set)
1673 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1678 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1680 regexp *re = (regexp *)mg->mg_obj;
1685 #ifdef USE_LOCALE_COLLATE
1687 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1690 * RenE<eacute> Descartes said "I think not."
1691 * and vanished with a faint plop.
1694 Safefree(mg->mg_ptr);
1700 #endif /* USE_LOCALE_COLLATE */
1703 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1708 switch (*mg->mg_ptr) {
1709 case '\001': /* ^A */
1710 sv_setsv(PL_bodytarget, sv);
1712 case '\003': /* ^C */
1713 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1716 case '\004': /* ^D */
1717 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1718 DEBUG_x(dump_all());
1720 case '\005': /* ^E */
1721 #ifdef MACOS_TRADITIONAL
1722 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1725 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1728 SetLastError( SvIV(sv) );
1731 /* will anyone ever use this? */
1732 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1738 case '\006': /* ^F */
1739 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1741 case '\010': /* ^H */
1742 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1744 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1746 Safefree(PL_inplace);
1748 PL_inplace = savepv(SvPV(sv,len));
1750 PL_inplace = Nullch;
1752 case '\017': /* ^O */
1753 if (*(mg->mg_ptr+1) == '\0') {
1755 Safefree(PL_osname);
1757 PL_osname = savepv(SvPV(sv,len));
1761 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1762 if (!PL_compiling.cop_io)
1763 PL_compiling.cop_io = newSVsv(sv);
1765 sv_setsv(PL_compiling.cop_io,sv);
1768 case '\020': /* ^P */
1769 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1770 if (PL_perldb && !PL_DBsingle)
1773 case '\024': /* ^T */
1775 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1777 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1780 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1781 if (*(mg->mg_ptr+1) == '\0') {
1782 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1783 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1784 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1785 | (i ? G_WARN_ON : G_WARN_OFF) ;
1788 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1789 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1790 if (!SvPOK(sv) && PL_localizing) {
1791 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1792 PL_compiling.cop_warnings = pWARN_NONE;
1797 int accumulate = 0 ;
1798 int any_fatals = 0 ;
1799 char * ptr = (char*)SvPV(sv, len) ;
1800 for (i = 0 ; i < len ; ++i) {
1801 accumulate |= ptr[i] ;
1802 any_fatals |= (ptr[i] & 0xAA) ;
1805 PL_compiling.cop_warnings = pWARN_NONE;
1806 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1807 PL_compiling.cop_warnings = pWARN_ALL;
1808 PL_dowarn |= G_WARN_ONCE ;
1811 if (specialWARN(PL_compiling.cop_warnings))
1812 PL_compiling.cop_warnings = newSVsv(sv) ;
1814 sv_setsv(PL_compiling.cop_warnings, sv);
1815 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1816 PL_dowarn |= G_WARN_ONCE ;
1822 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1823 PL_widesyscalls = SvTRUE(sv);
1826 if (PL_localizing) {
1827 if (PL_localizing == 1)
1828 SAVESPTR(PL_last_in_gv);
1830 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1831 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1834 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1835 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1836 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1839 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1840 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1841 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1844 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1847 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1848 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1849 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1852 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1856 IO *io = GvIOp(PL_defoutgv);
1857 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1858 IoFLAGS(io) &= ~IOf_FLUSH;
1860 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1861 PerlIO *ofp = IoOFP(io);
1863 (void)PerlIO_flush(ofp);
1864 IoFLAGS(io) |= IOf_FLUSH;
1870 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1871 PL_multiline = (i != 0);
1874 SvREFCNT_dec(PL_nrs);
1875 PL_nrs = newSVsv(sv);
1876 SvREFCNT_dec(PL_rs);
1877 PL_rs = SvREFCNT_inc(PL_nrs);
1881 SvREFCNT_dec(PL_ors_sv);
1882 if (SvOK(sv) || SvGMAGICAL(sv)) {
1883 PL_ors_sv = newSVsv(sv);
1891 SvREFCNT_dec(PL_ofs_sv);
1892 if (SvOK(sv) || SvGMAGICAL(sv)) {
1893 PL_ofs_sv = newSVsv(sv);
1902 PL_ofmt = savepv(SvPV(sv,len));
1905 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1908 #ifdef COMPLEX_STATUS
1909 if (PL_localizing == 2) {
1910 PL_statusvalue = LvTARGOFF(sv);
1911 PL_statusvalue_vms = LvTARGLEN(sv);
1915 #ifdef VMSISH_STATUS
1917 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1920 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1923 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1924 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1927 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1928 if (PL_delaymagic) {
1929 PL_delaymagic |= DM_RUID;
1930 break; /* don't do magic till later */
1933 (void)setruid((Uid_t)PL_uid);
1936 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1938 #ifdef HAS_SETRESUID
1939 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1941 if (PL_uid == PL_euid) /* special case $< = $> */
1942 (void)PerlProc_setuid(PL_uid);
1944 PL_uid = PerlProc_getuid();
1945 Perl_croak(aTHX_ "setruid() not implemented");
1950 PL_uid = PerlProc_getuid();
1951 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1954 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1955 if (PL_delaymagic) {
1956 PL_delaymagic |= DM_EUID;
1957 break; /* don't do magic till later */
1960 (void)seteuid((Uid_t)PL_euid);
1963 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1965 #ifdef HAS_SETRESUID
1966 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1968 if (PL_euid == PL_uid) /* special case $> = $< */
1969 PerlProc_setuid(PL_euid);
1971 PL_euid = PerlProc_geteuid();
1972 Perl_croak(aTHX_ "seteuid() not implemented");
1977 PL_euid = PerlProc_geteuid();
1978 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1981 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1982 if (PL_delaymagic) {
1983 PL_delaymagic |= DM_RGID;
1984 break; /* don't do magic till later */
1987 (void)setrgid((Gid_t)PL_gid);
1990 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1992 #ifdef HAS_SETRESGID
1993 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1995 if (PL_gid == PL_egid) /* special case $( = $) */
1996 (void)PerlProc_setgid(PL_gid);
1998 PL_gid = PerlProc_getgid();
1999 Perl_croak(aTHX_ "setrgid() not implemented");
2004 PL_gid = PerlProc_getgid();
2005 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2008 #ifdef HAS_SETGROUPS
2010 char *p = SvPV(sv, len);
2011 Groups_t gary[NGROUPS];
2016 for (i = 0; i < NGROUPS; ++i) {
2017 while (*p && !isSPACE(*p))
2026 (void)setgroups(i, gary);
2028 #else /* HAS_SETGROUPS */
2029 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2030 #endif /* HAS_SETGROUPS */
2031 if (PL_delaymagic) {
2032 PL_delaymagic |= DM_EGID;
2033 break; /* don't do magic till later */
2036 (void)setegid((Gid_t)PL_egid);
2039 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2041 #ifdef HAS_SETRESGID
2042 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2044 if (PL_egid == PL_gid) /* special case $) = $( */
2045 (void)PerlProc_setgid(PL_egid);
2047 PL_egid = PerlProc_getegid();
2048 Perl_croak(aTHX_ "setegid() not implemented");
2053 PL_egid = PerlProc_getegid();
2054 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2057 PL_chopset = SvPV_force(sv,len);
2059 #ifndef MACOS_TRADITIONAL
2061 #ifdef HAS_SETPROCTITLE
2062 /* The BSDs don't show the argv[] in ps(1) output, they
2063 * show a string from the process struct and provide
2064 * the setproctitle() routine to manipulate that. */
2067 # if __FreeBSD_version >= 410001
2068 /* The leading "-" removes the "perl: " prefix,
2069 * but not the "(perl) suffix from the ps(1)
2070 * output, because that's what ps(1) shows if the
2071 * argv[] is modified. */
2072 setproctitle("-%s", s, len + 1);
2073 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2074 /* This doesn't really work if you assume that
2075 * $0 = 'foobar'; will wipe out 'perl' from the $0
2076 * because in ps(1) output the result will be like
2077 * sprintf("perl: %s (perl)", s)
2078 * I guess this is a security feature:
2079 * one (a user process) cannot get rid of the original name.
2081 setproctitle("%s", s);
2088 /* See if all the arguments are contiguous in memory */
2089 for (i = 1; i < PL_origargc; i++) {
2090 if (PL_origargv[i] == s + 1
2092 || PL_origargv[i] == s + 2
2097 s += strlen(s); /* this one is ok too */
2102 /* can grab env area too? */
2103 if (PL_origenviron && (PL_origenviron[0] == s + 1
2105 || (PL_origenviron[0] == s + 9 && (s += 8))
2108 my_setenv("NoNe SuCh", Nullch);
2109 /* force copy of environment */
2110 for (i = 0; PL_origenviron[i]; i++)
2111 if (PL_origenviron[i] == s + 1) {
2118 PL_origalen = s - PL_origargv[0];
2120 s = SvPV_force(sv,len);
2122 if (i >= PL_origalen) {
2124 /* don't allow system to limit $0 seen by script */
2125 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2126 Copy(s, PL_origargv[0], i, char);
2127 s = PL_origargv[0]+i;
2131 Copy(s, PL_origargv[0], i, char);
2132 s = PL_origargv[0]+i;
2134 while (++i < PL_origalen)
2136 s = PL_origargv[0]+i;
2137 for (i = 1; i < PL_origargc; i++)
2138 PL_origargv[i] = Nullch;
2144 sv_setsv(thr->errsv, sv);
2146 #endif /* USE_THREADS */
2153 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2155 DEBUG_S(PerlIO_printf(Perl_debug_log,
2156 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2157 PTR2UV(thr), PTR2UV(sv));)
2159 Perl_croak(aTHX_ "panic: magic_mutexfree");
2160 MUTEX_DESTROY(MgMUTEXP(mg));
2161 COND_DESTROY(MgCONDP(mg));
2164 #endif /* USE_THREADS */
2167 Perl_whichsig(pTHX_ char *sig)
2169 register char **sigv;
2171 for (sigv = PL_sig_name+1; *sigv; sigv++)
2172 if (strEQ(sig,*sigv))
2173 return PL_sig_num[sigv - PL_sig_name];
2175 if (strEQ(sig,"CHLD"))
2179 if (strEQ(sig,"CLD"))
2188 Perl_sighandler(int sig)
2190 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2191 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2198 SV *sv, *tSv = PL_Sv;
2202 I32 o_save_i = PL_savestack_ix;
2205 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2206 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2209 if (PL_savestack_ix + 15 <= PL_savestack_max)
2211 if (PL_markstack_ptr < PL_markstack_max - 2)
2213 if (PL_retstack_ix < PL_retstack_max - 2)
2215 if (PL_scopestack_ix < PL_scopestack_max - 3)
2218 if (!PL_psig_ptr[sig])
2219 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2222 /* Max number of items pushed there is 3*n or 4. We cannot fix
2223 infinity, so we fix 4 (in fact 5): */
2225 PL_savestack_ix += 5; /* Protect save in progress. */
2226 o_save_i = PL_savestack_ix;
2227 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2230 PL_markstack_ptr++; /* Protect mark. */
2233 PL_retstack[PL_retstack_ix] = NULL;
2236 PL_scopestack_ix += 1;
2237 /* sv_2cv is too complicated, try a simpler variant first: */
2238 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2239 || SvTYPE(cv) != SVt_PVCV)
2240 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2242 if (!cv || !CvROOT(cv)) {
2243 if (ckWARN(WARN_SIGNAL))
2244 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2245 PL_sig_name[sig], (gv ? GvENAME(gv)
2252 if(PL_psig_name[sig]) {
2253 sv = SvREFCNT_inc(PL_psig_name[sig]);
2257 sv = sv_newmortal();
2258 sv_setpv(sv,PL_sig_name[sig]);
2261 PUSHSTACKi(PERLSI_SIGNAL);
2266 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2269 if (SvTRUE(ERRSV)) {
2271 #ifdef HAS_SIGPROCMASK
2272 /* Handler "died", for example to get out of a restart-able read().
2273 * Before we re-do that on its behalf re-enable the signal which was
2274 * blocked by the system when we entered.
2278 sigaddset(&set,sig);
2279 sigprocmask(SIG_UNBLOCK, &set, NULL);
2281 /* Not clear if this will work */
2282 (void)rsignal(sig, SIG_IGN);
2283 (void)rsignal(sig, &Perl_csighandler);
2285 #endif /* !PERL_MICRO */
2286 Perl_die(aTHX_ Nullch);
2290 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2296 PL_scopestack_ix -= 1;
2299 PL_op = myop; /* Apparently not needed... */
2301 PL_Sv = tSv; /* Restore global temporaries. */
2312 restore_magic(pTHXo_ void *p)
2314 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2315 SV* sv = mgs->mgs_sv;
2320 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2323 SvFLAGS(sv) |= mgs->mgs_flags;
2327 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2330 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2332 /* If we're still on top of the stack, pop us off. (That condition
2333 * will be satisfied if restore_magic was called explicitly, but *not*
2334 * if it's being called via leave_scope.)
2335 * The reason for doing this is that otherwise, things like sv_2cv()
2336 * may leave alloc gunk on the savestack, and some code
2337 * (e.g. sighandler) doesn't expect that...
2339 if (PL_savestack_ix == mgs->mgs_ss_ix)
2341 I32 popval = SSPOPINT;
2342 assert(popval == SAVEt_DESTRUCTOR_X);
2343 PL_savestack_ix -= 2;
2345 assert(popval == SAVEt_ALLOC);
2347 PL_savestack_ix -= popval;
2353 unwind_handler_stack(pTHXo_ void *p)
2355 U32 flags = *(U32*)p;
2358 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2359 /* cxstack_ix-- Not needed, die already unwound it. */
2361 SvREFCNT_dec(sig_sv);