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_ IV mgs_ix, SV *sv)
46 assert(SvMAGICAL(sv));
48 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*,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*,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*,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)
175 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
176 MGVTBL* vtbl = mg->mg_virtual;
177 if (vtbl && vtbl->svt_len) {
180 mgs_ix = SSNEW(sizeof(MGS));
181 save_magic(mgs_ix, sv);
182 /* omit MGf_GSKIP -- not changed here */
183 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
184 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
189 junk = SvPV(sv, len);
194 Perl_mg_size(pTHX_ SV *sv)
199 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
200 MGVTBL* vtbl = mg->mg_virtual;
201 if (vtbl && vtbl->svt_len) {
204 mgs_ix = SSNEW(sizeof(MGS));
205 save_magic(mgs_ix, sv);
206 /* omit MGf_GSKIP -- not changed here */
207 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
208 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
215 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
220 Perl_croak(aTHX_ "Size magic not implemented");
229 Clear something magical that the SV represents. See C<sv_magic>.
235 Perl_mg_clear(pTHX_ SV *sv)
240 mgs_ix = SSNEW(sizeof(MGS));
241 save_magic(mgs_ix, sv);
243 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
244 MGVTBL* vtbl = mg->mg_virtual;
245 /* omit GSKIP -- never set here */
247 if (vtbl && vtbl->svt_clear)
248 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
251 restore_magic(aTHXo_ INT2PTR(void*,mgs_ix));
258 Finds the magic pointer for type matching the SV. See C<sv_magic>.
264 Perl_mg_find(pTHX_ SV *sv, int type)
269 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
270 if (mg->mg_type == type)
279 Copies the magic from one SV to another. See C<sv_magic>.
285 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
289 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
290 if (isUPPER(mg->mg_type)) {
292 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
293 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
295 toLOWER(mg->mg_type), key, klen);
305 Free any magic storage used by the SV. See C<sv_magic>.
311 Perl_mg_free(pTHX_ SV *sv)
315 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
316 MGVTBL* vtbl = mg->mg_virtual;
317 moremagic = mg->mg_moremagic;
318 if (vtbl && vtbl->svt_free)
319 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
320 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
322 Safefree(mg->mg_ptr);
323 else if (mg->mg_len == HEf_SVKEY)
324 SvREFCNT_dec((SV*)mg->mg_ptr);
326 if (mg->mg_flags & MGf_REFCOUNTED)
327 SvREFCNT_dec(mg->mg_obj);
335 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
340 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
344 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
345 if (mg->mg_obj) /* @+ */
348 return rx->lastparen;
355 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
363 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
367 if (paren <= rx->nparens &&
368 (s = rx->startp[paren]) != -1 &&
369 (t = rx->endp[paren]) != -1)
371 if (mg->mg_obj) /* @+ */
376 if (i > 0 && DO_UTF8(PL_reg_sv)) {
377 char *b = rx->subbeg;
379 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
389 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
391 Perl_croak(aTHX_ PL_no_modify);
397 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
404 switch (*mg->mg_ptr) {
405 case '1': case '2': case '3': case '4':
406 case '5': case '6': case '7': case '8': case '9': case '&':
407 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
409 paren = atoi(mg->mg_ptr); /* $& is in [0] */
411 if (paren <= rx->nparens &&
412 (s1 = rx->startp[paren]) != -1 &&
413 (t1 = rx->endp[paren]) != -1)
417 if (i > 0 && DO_UTF8(PL_reg_sv)) {
418 char *s = rx->subbeg + s1;
419 char *send = rx->subbeg + t1;
422 if (is_utf8_string((U8*)s, i))
423 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
426 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
432 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
433 paren = rx->lastparen;
439 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
440 if (rx->startp[0] != -1) {
451 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
452 if (rx->endp[0] != -1) {
453 i = rx->sublen - rx->endp[0];
464 if (!SvPOK(sv) && SvNIOK(sv)) {
474 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
481 switch (*mg->mg_ptr) {
482 case '\001': /* ^A */
483 sv_setsv(sv, PL_bodytarget);
485 case '\003': /* ^C */
486 sv_setiv(sv, (IV)PL_minus_c);
489 case '\004': /* ^D */
490 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
491 #if defined(YYDEBUG) && defined(DEBUGGING)
492 PL_yydebug = DEBUG_p_TEST;
495 case '\005': /* ^E */
496 #ifdef MACOS_TRADITIONAL
500 sv_setnv(sv,(double)gMacPerl_OSErr);
501 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
506 # include <descrip.h>
507 # include <starlet.h>
509 $DESCRIPTOR(msgdsc,msg);
510 sv_setnv(sv,(NV) vaxc$errno);
511 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
512 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
518 if (!(_emx_env & 0x200)) { /* Under DOS */
519 sv_setnv(sv, (NV)errno);
520 sv_setpv(sv, errno ? Strerror(errno) : "");
522 if (errno != errno_isOS2) {
523 int tmp = _syserrno();
524 if (tmp) /* 2nd call to _syserrno() makes it 0 */
527 sv_setnv(sv, (NV)Perl_rc);
528 sv_setpv(sv, os2error(Perl_rc));
533 DWORD dwErr = GetLastError();
534 sv_setnv(sv, (NV)dwErr);
537 PerlProc_GetOSError(sv, dwErr);
544 sv_setnv(sv, (NV)errno);
545 sv_setpv(sv, errno ? Strerror(errno) : "");
550 SvNOK_on(sv); /* what a wonderful hack! */
552 case '\006': /* ^F */
553 sv_setiv(sv, (IV)PL_maxsysfd);
555 case '\010': /* ^H */
556 sv_setiv(sv, (IV)PL_hints);
558 case '\011': /* ^I */ /* NOT \t in EBCDIC */
560 sv_setpv(sv, PL_inplace);
562 sv_setsv(sv, &PL_sv_undef);
564 case '\017': /* ^O & ^OPEN */
565 if (*(mg->mg_ptr+1) == '\0')
566 sv_setpv(sv, PL_osname);
567 else if (strEQ(mg->mg_ptr, "\017PEN")) {
568 if (!PL_compiling.cop_io)
569 sv_setsv(sv, &PL_sv_undef);
571 sv_setsv(sv, PL_compiling.cop_io);
575 case '\020': /* ^P */
576 sv_setiv(sv, (IV)PL_perldb);
578 case '\023': /* ^S */
580 if (PL_lex_state != LEX_NOTPARSING)
583 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
586 case '\024': /* ^T */
588 sv_setnv(sv, PL_basetime);
590 sv_setiv(sv, (IV)PL_basetime);
593 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
594 if (*(mg->mg_ptr+1) == '\0')
595 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
596 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
597 if (PL_compiling.cop_warnings == pWARN_NONE ||
598 PL_compiling.cop_warnings == pWARN_STD)
600 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
602 else if (PL_compiling.cop_warnings == pWARN_ALL) {
603 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
606 sv_setsv(sv, PL_compiling.cop_warnings);
610 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
611 sv_setiv(sv, (IV)PL_widesyscalls);
613 case '1': case '2': case '3': case '4':
614 case '5': case '6': case '7': case '8': case '9': case '&':
615 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
619 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
620 * XXX Does the new way break anything?
622 paren = atoi(mg->mg_ptr); /* $& is in [0] */
624 if (paren <= rx->nparens &&
625 (s1 = rx->startp[paren]) != -1 &&
626 (t1 = rx->endp[paren]) != -1)
635 bool was_tainted = FALSE;
637 was_tainted = PL_tainted;
641 if (PL_reg_sv && DO_UTF8(PL_reg_sv) && is_utf8_string((U8*)s, i))
646 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
651 sv_setsv(sv,&PL_sv_undef);
654 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
655 paren = rx->lastparen;
659 sv_setsv(sv,&PL_sv_undef);
662 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
663 if ((s = rx->subbeg) && rx->startp[0] != -1) {
668 sv_setsv(sv,&PL_sv_undef);
671 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
672 if (rx->subbeg && rx->endp[0] != -1) {
673 s = rx->subbeg + rx->endp[0];
674 i = rx->sublen - rx->endp[0];
678 sv_setsv(sv,&PL_sv_undef);
682 if (GvIO(PL_last_in_gv)) {
683 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
689 sv_setiv(sv, (IV)STATUS_CURRENT);
690 #ifdef COMPLEX_STATUS
691 LvTARGOFF(sv) = PL_statusvalue;
692 LvTARGLEN(sv) = PL_statusvalue_vms;
697 s = IoTOP_NAME(GvIOp(PL_defoutgv));
701 sv_setpv(sv,GvENAME(PL_defoutgv));
706 s = IoFMT_NAME(GvIOp(PL_defoutgv));
708 s = GvENAME(PL_defoutgv);
713 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
716 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
719 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
727 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
730 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
737 sv_setpv(sv,PL_ofmt);
741 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
742 sv_setpv(sv, errno ? Strerror(errno) : "");
745 int saveerrno = errno;
746 sv_setnv(sv, (NV)errno);
748 if (errno == errno_isOS2 || errno == errno_isOS2_set)
749 sv_setpv(sv, os2error(Perl_rc));
752 sv_setpv(sv, errno ? Strerror(errno) : "");
756 SvNOK_on(sv); /* what a wonderful hack! */
759 sv_setiv(sv, (IV)PL_uid);
762 sv_setiv(sv, (IV)PL_euid);
765 sv_setiv(sv, (IV)PL_gid);
767 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
771 sv_setiv(sv, (IV)PL_egid);
773 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
778 Groups_t gary[NGROUPS];
779 i = getgroups(NGROUPS,gary);
781 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
784 (void)SvIOK_on(sv); /* what a wonderful hack! */
788 #ifndef MACOS_TRADITIONAL
794 sv_setsv(sv, thr->errsv);
796 #endif /* USE_THREADS */
802 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
804 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
806 if (uf && uf->uf_val)
807 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
812 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
823 #ifdef DYNAMIC_ENV_FETCH
824 /* We just undefd an environment var. Is a replacement */
825 /* waiting in the wings? */
828 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
829 s = SvPV(*valp, len);
833 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
834 /* And you'll never guess what the dog had */
835 /* in its mouth... */
837 MgTAINTEDDIR_off(mg);
839 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
840 char pathbuf[256], eltbuf[256], *cp, *elt = s;
844 do { /* DCL$PATH may be a search list */
845 while (1) { /* as may dev portion of any element */
846 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
847 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
848 cando_by_name(S_IWUSR,0,elt) ) {
853 if ((cp = strchr(elt, ':')) != Nullch)
855 if (my_trnlnm(elt, eltbuf, j++))
861 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
864 if (s && klen == 4 && strEQ(ptr,"PATH")) {
865 char *strend = s + len;
870 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
873 if (i >= sizeof tmpbuf /* too long -- assume the worst */
875 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
882 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
888 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
891 my_setenv(MgPV(mg,n_a),Nullch);
896 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
899 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
904 magic_clear_all_env(sv,mg);
905 hv_iterinit((HV*)sv);
906 while ((entry = hv_iternext((HV*)sv))) {
908 my_setenv(hv_iterkey(entry, &keylen),
909 SvPV(hv_iterval((HV*)sv, entry), n_a));
917 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
919 #if defined(VMS) || defined(EPOC)
920 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
922 # ifdef PERL_IMPLICIT_SYS
926 char *envv = GetEnvironmentStrings();
930 char *end = strchr(cur,'=');
931 if (end && end != cur) {
933 my_setenv(cur,Nullch);
935 cur = end + strlen(end+1)+2;
937 else if ((len = strlen(cur)))
940 FreeEnvironmentStrings(envv);
942 #ifdef USE_ENVIRON_ARRAY
943 # ifndef PERL_USE_SAFE_PUTENV
946 if (environ == PL_origenviron)
947 environ = (char**)safesysmalloc(sizeof(char*));
949 for (i = 0; environ[i]; i++)
950 safesysfree(environ[i]);
951 # endif /* PERL_USE_SAFE_PUTENV */
955 #endif /* USE_ENVIRON_ARRAY */
957 # endif /* PERL_IMPLICIT_SYS */
964 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
968 /* Are we fetching a signal entry? */
969 i = whichsig(MgPV(mg,n_a));
972 sv_setsv(sv,PL_psig_ptr[i]);
974 Sighandler_t sigstate = rsignal_state(i);
976 /* cache state so we don't fetch it again */
977 if(sigstate == SIG_IGN)
978 sv_setpv(sv,"IGNORE");
980 sv_setsv(sv,&PL_sv_undef);
981 PL_psig_ptr[i] = SvREFCNT_inc(sv);
988 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
992 /* Are we clearing a signal entry? */
993 i = whichsig(MgPV(mg,n_a));
996 SvREFCNT_dec(PL_psig_ptr[i]);
999 if(PL_psig_name[i]) {
1000 SvREFCNT_dec(PL_psig_name[i]);
1008 Perl_raise_signal(pTHX_ int sig)
1010 /* Set a flag to say this signal is pending */
1011 PL_psig_pend[sig]++;
1012 /* And one to say _a_ signal is pending */
1017 Perl_csighandler(int sig)
1019 #ifdef PERL_OLD_SIGNALS
1020 /* Call the perl level handler now with risk we may be in malloc() etc. */
1021 (*PL_sighandlerp)(sig);
1024 Perl_raise_signal(aTHX_ sig);
1029 Perl_despatch_signals(pTHX)
1033 for (sig = 1; sig < SIG_SIZE; sig++) {
1034 if (PL_psig_pend[sig]) {
1035 PL_psig_pend[sig] = 0;
1036 (*PL_sighandlerp)(sig);
1042 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1051 if (strEQ(s,"__DIE__"))
1053 else if (strEQ(s,"__WARN__"))
1056 Perl_croak(aTHX_ "No such hook: %s", s);
1064 i = whichsig(s); /* ...no, a brick */
1066 if (ckWARN(WARN_SIGNAL))
1067 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1070 SvREFCNT_dec(PL_psig_name[i]);
1071 SvREFCNT_dec(PL_psig_ptr[i]);
1072 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1073 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1074 PL_psig_name[i] = newSVpvn(s, len);
1075 SvREADONLY_on(PL_psig_name[i]);
1077 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1079 (void)rsignal(i, &Perl_csighandler);
1081 *svp = SvREFCNT_inc(sv);
1084 s = SvPV_force(sv,len);
1085 if (strEQ(s,"IGNORE")) {
1087 (void)rsignal(i, SIG_IGN);
1091 else if (strEQ(s,"DEFAULT") || !*s) {
1093 (void)rsignal(i, SIG_DFL);
1099 * We should warn if HINT_STRICT_REFS, but without
1100 * access to a known hint bit in a known OP, we can't
1101 * tell whether HINT_STRICT_REFS is in force or not.
1103 if (!strchr(s,':') && !strchr(s,'\''))
1104 sv_insert(sv, 0, 0, "main::", 6);
1106 (void)rsignal(i, &Perl_csighandler);
1108 *svp = SvREFCNT_inc(sv);
1112 #endif /* !PERL_MICRO */
1115 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1117 PL_sub_generation++;
1122 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1124 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1125 PL_amagic_generation++;
1131 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1133 HV *hv = (HV*)LvTARG(sv);
1138 (void) hv_iterinit(hv);
1139 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1143 while ((entry = hv_iternext(hv))) {
1149 sv_setiv(sv, (IV)i);
1154 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1157 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1162 /* caller is responsible for stack switching/cleanup */
1164 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1170 PUSHs(SvTIED_obj(sv, mg));
1173 if (mg->mg_len >= 0)
1174 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1175 else if (mg->mg_len == HEf_SVKEY)
1176 PUSHs((SV*)mg->mg_ptr);
1178 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1179 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1187 return call_method(meth, flags);
1191 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1197 PUSHSTACKi(PERLSI_MAGIC);
1199 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1200 sv_setsv(sv, *PL_stack_sp--);
1210 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1212 magic_methpack(sv,mg,"FETCH");
1214 mg->mg_flags |= MGf_GSKIP;
1219 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1223 PUSHSTACKi(PERLSI_MAGIC);
1224 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1231 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1233 return magic_methpack(sv,mg,"DELETE");
1238 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1245 PUSHSTACKi(PERLSI_MAGIC);
1246 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1247 sv = *PL_stack_sp--;
1248 retval = (U32) SvIV(sv)-1;
1257 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1262 PUSHSTACKi(PERLSI_MAGIC);
1264 XPUSHs(SvTIED_obj(sv, mg));
1266 call_method("CLEAR", G_SCALAR|G_DISCARD);
1273 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1276 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1280 PUSHSTACKi(PERLSI_MAGIC);
1283 PUSHs(SvTIED_obj(sv, mg));
1288 if (call_method(meth, G_SCALAR))
1289 sv_setsv(key, *PL_stack_sp--);
1298 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1300 return magic_methpack(sv,mg,"EXISTS");
1304 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1314 svp = av_fetch(GvAV(gv),
1315 atoi(MgPV(mg,n_a)), FALSE);
1316 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1322 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1324 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1329 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1331 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1336 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1338 SV* lsv = LvTARG(sv);
1340 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1341 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1342 if (mg && mg->mg_len >= 0) {
1345 sv_pos_b2u(lsv, &i);
1346 sv_setiv(sv, i + PL_curcop->cop_arybase);
1355 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1357 SV* lsv = LvTARG(sv);
1364 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1365 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1369 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1370 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1372 else if (!SvOK(sv)) {
1376 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1378 pos = SvIV(sv) - PL_curcop->cop_arybase;
1381 ulen = sv_len_utf8(lsv);
1396 sv_pos_u2b(lsv, &p, 0);
1401 mg->mg_flags &= ~MGf_MINMATCH;
1407 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1409 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1411 gv_efullname3(sv,((GV*)sv), "*");
1415 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1420 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1429 if (*s == '*' && s[1])
1431 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1436 GvGP(sv) = gp_ref(GvGP(gv));
1441 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1444 SV *lsv = LvTARG(sv);
1445 char *tmps = SvPV(lsv,len);
1446 I32 offs = LvTARGOFF(sv);
1447 I32 rem = LvTARGLEN(sv);
1450 sv_pos_u2b(lsv, &offs, &rem);
1453 if (rem + offs > len)
1455 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1462 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1465 char *tmps = SvPV(sv, len);
1466 SV *lsv = LvTARG(sv);
1467 I32 lvoff = LvTARGOFF(sv);
1468 I32 lvlen = LvTARGLEN(sv);
1471 sv_utf8_upgrade(lsv);
1472 sv_pos_u2b(lsv, &lvoff, &lvlen);
1473 sv_insert(lsv, lvoff, lvlen, tmps, len);
1476 else if (SvUTF8(lsv)) {
1477 sv_pos_u2b(lsv, &lvoff, &lvlen);
1478 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1479 sv_insert(lsv, lvoff, lvlen, tmps, len);
1483 sv_insert(lsv, lvoff, lvlen, tmps, len);
1489 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1491 TAINT_IF((mg->mg_len & 1) ||
1492 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1497 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1499 if (PL_localizing) {
1500 if (PL_localizing == 1)
1505 else if (PL_tainted)
1513 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1515 SV *lsv = LvTARG(sv);
1522 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1527 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1529 do_vecset(sv); /* XXX slurp this routine */
1534 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1537 if (LvTARGLEN(sv)) {
1539 SV *ahv = LvTARG(sv);
1540 if (SvTYPE(ahv) == SVt_PVHV) {
1541 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1546 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1552 AV* av = (AV*)LvTARG(sv);
1553 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1554 targ = AvARRAY(av)[LvTARGOFF(sv)];
1556 if (targ && targ != &PL_sv_undef) {
1557 /* somebody else defined it for us */
1558 SvREFCNT_dec(LvTARG(sv));
1559 LvTARG(sv) = SvREFCNT_inc(targ);
1561 SvREFCNT_dec(mg->mg_obj);
1562 mg->mg_obj = Nullsv;
1563 mg->mg_flags &= ~MGf_REFCOUNTED;
1568 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1573 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1578 sv_setsv(LvTARG(sv), sv);
1579 SvSETMAGIC(LvTARG(sv));
1585 Perl_vivify_defelem(pTHX_ SV *sv)
1590 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1593 SV *ahv = LvTARG(sv);
1595 if (SvTYPE(ahv) == SVt_PVHV) {
1596 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1601 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1605 if (!value || value == &PL_sv_undef)
1606 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1609 AV* av = (AV*)LvTARG(sv);
1610 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1611 LvTARG(sv) = Nullsv; /* array can't be extended */
1613 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1614 if (!svp || (value = *svp) == &PL_sv_undef)
1615 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1618 (void)SvREFCNT_inc(value);
1619 SvREFCNT_dec(LvTARG(sv));
1622 SvREFCNT_dec(mg->mg_obj);
1623 mg->mg_obj = Nullsv;
1624 mg->mg_flags &= ~MGf_REFCOUNTED;
1628 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1630 AV *av = (AV*)mg->mg_obj;
1631 SV **svp = AvARRAY(av);
1632 I32 i = AvFILLp(av);
1634 if (svp[i] && svp[i] != &PL_sv_undef) {
1635 if (!SvWEAKREF(svp[i]))
1636 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1637 /* XXX Should we check that it hasn't changed? */
1639 (void)SvOK_off(svp[i]);
1640 SvWEAKREF_off(svp[i]);
1641 svp[i] = &PL_sv_undef;
1649 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1657 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1659 sv_unmagic(sv, PERL_MAGIC_bm);
1665 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1667 sv_unmagic(sv, PERL_MAGIC_fm);
1673 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1675 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1677 if (uf && uf->uf_set)
1678 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1683 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1685 regexp *re = (regexp *)mg->mg_obj;
1690 #ifdef USE_LOCALE_COLLATE
1692 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1695 * RenE<eacute> Descartes said "I think not."
1696 * and vanished with a faint plop.
1699 Safefree(mg->mg_ptr);
1705 #endif /* USE_LOCALE_COLLATE */
1708 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1713 switch (*mg->mg_ptr) {
1714 case '\001': /* ^A */
1715 sv_setsv(PL_bodytarget, sv);
1717 case '\003': /* ^C */
1718 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1721 case '\004': /* ^D */
1722 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1723 DEBUG_x(dump_all());
1725 case '\005': /* ^E */
1726 #ifdef MACOS_TRADITIONAL
1727 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1730 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1733 SetLastError( SvIV(sv) );
1736 /* will anyone ever use this? */
1737 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1743 case '\006': /* ^F */
1744 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1746 case '\010': /* ^H */
1747 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1749 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1751 Safefree(PL_inplace);
1753 PL_inplace = savepv(SvPV(sv,len));
1755 PL_inplace = Nullch;
1757 case '\017': /* ^O */
1758 if (*(mg->mg_ptr+1) == '\0') {
1760 Safefree(PL_osname);
1762 PL_osname = savepv(SvPV(sv,len));
1766 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1767 if (!PL_compiling.cop_io)
1768 PL_compiling.cop_io = newSVsv(sv);
1770 sv_setsv(PL_compiling.cop_io,sv);
1773 case '\020': /* ^P */
1774 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1775 if (PL_perldb && !PL_DBsingle)
1778 case '\024': /* ^T */
1780 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1782 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1785 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1786 if (*(mg->mg_ptr+1) == '\0') {
1787 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1788 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1789 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1790 | (i ? G_WARN_ON : G_WARN_OFF) ;
1793 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1794 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1795 if (!SvPOK(sv) && PL_localizing) {
1796 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1797 PL_compiling.cop_warnings = pWARN_NONE;
1802 int accumulate = 0 ;
1803 int any_fatals = 0 ;
1804 char * ptr = (char*)SvPV(sv, len) ;
1805 for (i = 0 ; i < len ; ++i) {
1806 accumulate |= ptr[i] ;
1807 any_fatals |= (ptr[i] & 0xAA) ;
1810 PL_compiling.cop_warnings = pWARN_NONE;
1811 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1812 PL_compiling.cop_warnings = pWARN_ALL;
1813 PL_dowarn |= G_WARN_ONCE ;
1816 if (specialWARN(PL_compiling.cop_warnings))
1817 PL_compiling.cop_warnings = newSVsv(sv) ;
1819 sv_setsv(PL_compiling.cop_warnings, sv);
1820 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1821 PL_dowarn |= G_WARN_ONCE ;
1827 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1828 PL_widesyscalls = SvTRUE(sv);
1831 if (PL_localizing) {
1832 if (PL_localizing == 1)
1833 SAVESPTR(PL_last_in_gv);
1835 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1836 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1839 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1840 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1841 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1844 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1845 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1846 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1849 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1852 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1853 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1854 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1857 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1861 IO *io = GvIOp(PL_defoutgv);
1862 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1863 IoFLAGS(io) &= ~IOf_FLUSH;
1865 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1866 PerlIO *ofp = IoOFP(io);
1868 (void)PerlIO_flush(ofp);
1869 IoFLAGS(io) |= IOf_FLUSH;
1875 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1876 PL_multiline = (i != 0);
1879 SvREFCNT_dec(PL_nrs);
1880 PL_nrs = newSVsv(sv);
1881 SvREFCNT_dec(PL_rs);
1882 PL_rs = SvREFCNT_inc(PL_nrs);
1886 SvREFCNT_dec(PL_ors_sv);
1887 if (SvOK(sv) || SvGMAGICAL(sv)) {
1888 PL_ors_sv = newSVsv(sv);
1896 SvREFCNT_dec(PL_ofs_sv);
1897 if (SvOK(sv) || SvGMAGICAL(sv)) {
1898 PL_ofs_sv = newSVsv(sv);
1907 PL_ofmt = savepv(SvPV(sv,len));
1910 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1913 #ifdef COMPLEX_STATUS
1914 if (PL_localizing == 2) {
1915 PL_statusvalue = LvTARGOFF(sv);
1916 PL_statusvalue_vms = LvTARGLEN(sv);
1920 #ifdef VMSISH_STATUS
1922 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1925 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1928 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1929 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1932 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1933 if (PL_delaymagic) {
1934 PL_delaymagic |= DM_RUID;
1935 break; /* don't do magic till later */
1938 (void)setruid((Uid_t)PL_uid);
1941 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1943 #ifdef HAS_SETRESUID
1944 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1946 if (PL_uid == PL_euid) /* special case $< = $> */
1947 (void)PerlProc_setuid(PL_uid);
1949 PL_uid = PerlProc_getuid();
1950 Perl_croak(aTHX_ "setruid() not implemented");
1955 PL_uid = PerlProc_getuid();
1956 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1959 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1960 if (PL_delaymagic) {
1961 PL_delaymagic |= DM_EUID;
1962 break; /* don't do magic till later */
1965 (void)seteuid((Uid_t)PL_euid);
1968 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1970 #ifdef HAS_SETRESUID
1971 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1973 if (PL_euid == PL_uid) /* special case $> = $< */
1974 PerlProc_setuid(PL_euid);
1976 PL_euid = PerlProc_geteuid();
1977 Perl_croak(aTHX_ "seteuid() not implemented");
1982 PL_euid = PerlProc_geteuid();
1983 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1986 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1987 if (PL_delaymagic) {
1988 PL_delaymagic |= DM_RGID;
1989 break; /* don't do magic till later */
1992 (void)setrgid((Gid_t)PL_gid);
1995 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1997 #ifdef HAS_SETRESGID
1998 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2000 if (PL_gid == PL_egid) /* special case $( = $) */
2001 (void)PerlProc_setgid(PL_gid);
2003 PL_gid = PerlProc_getgid();
2004 Perl_croak(aTHX_ "setrgid() not implemented");
2009 PL_gid = PerlProc_getgid();
2010 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2013 #ifdef HAS_SETGROUPS
2015 char *p = SvPV(sv, len);
2016 Groups_t gary[NGROUPS];
2021 for (i = 0; i < NGROUPS; ++i) {
2022 while (*p && !isSPACE(*p))
2031 (void)setgroups(i, gary);
2033 #else /* HAS_SETGROUPS */
2034 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2035 #endif /* HAS_SETGROUPS */
2036 if (PL_delaymagic) {
2037 PL_delaymagic |= DM_EGID;
2038 break; /* don't do magic till later */
2041 (void)setegid((Gid_t)PL_egid);
2044 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2046 #ifdef HAS_SETRESGID
2047 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2049 if (PL_egid == PL_gid) /* special case $) = $( */
2050 (void)PerlProc_setgid(PL_egid);
2052 PL_egid = PerlProc_getegid();
2053 Perl_croak(aTHX_ "setegid() not implemented");
2058 PL_egid = PerlProc_getegid();
2059 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2062 PL_chopset = SvPV_force(sv,len);
2064 #ifndef MACOS_TRADITIONAL
2066 #ifdef HAS_SETPROCTITLE
2067 /* The BSDs don't show the argv[] in ps(1) output, they
2068 * show a string from the process struct and provide
2069 * the setproctitle() routine to manipulate that. */
2072 # if __FreeBSD_version >= 410001
2073 /* The leading "-" removes the "perl: " prefix,
2074 * but not the "(perl) suffix from the ps(1)
2075 * output, because that's what ps(1) shows if the
2076 * argv[] is modified. */
2077 setproctitle("-%s", s, len + 1);
2078 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2079 /* This doesn't really work if you assume that
2080 * $0 = 'foobar'; will wipe out 'perl' from the $0
2081 * because in ps(1) output the result will be like
2082 * sprintf("perl: %s (perl)", s)
2083 * I guess this is a security feature:
2084 * one (a user process) cannot get rid of the original name.
2086 setproctitle("%s", s);
2093 /* See if all the arguments are contiguous in memory */
2094 for (i = 1; i < PL_origargc; i++) {
2095 if (PL_origargv[i] == s + 1
2097 || PL_origargv[i] == s + 2
2102 s += strlen(s); /* this one is ok too */
2107 /* can grab env area too? */
2108 if (PL_origenviron && (PL_origenviron[0] == s + 1
2110 || (PL_origenviron[0] == s + 9 && (s += 8))
2113 my_setenv("NoNe SuCh", Nullch);
2114 /* force copy of environment */
2115 for (i = 0; PL_origenviron[i]; i++)
2116 if (PL_origenviron[i] == s + 1) {
2123 PL_origalen = s - PL_origargv[0];
2125 s = SvPV_force(sv,len);
2127 if (i >= PL_origalen) {
2129 /* don't allow system to limit $0 seen by script */
2130 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2131 Copy(s, PL_origargv[0], i, char);
2132 s = PL_origargv[0]+i;
2136 Copy(s, PL_origargv[0], i, char);
2137 s = PL_origargv[0]+i;
2139 while (++i < PL_origalen)
2141 s = PL_origargv[0]+i;
2142 for (i = 1; i < PL_origargc; i++)
2143 PL_origargv[i] = Nullch;
2149 sv_setsv(thr->errsv, sv);
2151 #endif /* USE_THREADS */
2158 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2160 DEBUG_S(PerlIO_printf(Perl_debug_log,
2161 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2162 PTR2UV(thr), PTR2UV(sv));)
2164 Perl_croak(aTHX_ "panic: magic_mutexfree");
2165 MUTEX_DESTROY(MgMUTEXP(mg));
2166 COND_DESTROY(MgCONDP(mg));
2169 #endif /* USE_THREADS */
2172 Perl_whichsig(pTHX_ char *sig)
2174 register char **sigv;
2176 for (sigv = PL_sig_name+1; *sigv; sigv++)
2177 if (strEQ(sig,*sigv))
2178 return PL_sig_num[sigv - PL_sig_name];
2180 if (strEQ(sig,"CHLD"))
2184 if (strEQ(sig,"CLD"))
2193 Perl_sighandler(int sig)
2195 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2196 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2203 SV *sv = Nullsv, *tSv = PL_Sv;
2207 I32 o_save_i = PL_savestack_ix;
2210 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2211 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2214 if (PL_savestack_ix + 15 <= PL_savestack_max)
2216 if (PL_markstack_ptr < PL_markstack_max - 2)
2218 if (PL_retstack_ix < PL_retstack_max - 2)
2220 if (PL_scopestack_ix < PL_scopestack_max - 3)
2223 if (!PL_psig_ptr[sig])
2224 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2227 /* Max number of items pushed there is 3*n or 4. We cannot fix
2228 infinity, so we fix 4 (in fact 5): */
2230 PL_savestack_ix += 5; /* Protect save in progress. */
2231 o_save_i = PL_savestack_ix;
2232 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2235 PL_markstack_ptr++; /* Protect mark. */
2238 PL_retstack[PL_retstack_ix] = NULL;
2241 PL_scopestack_ix += 1;
2242 /* sv_2cv is too complicated, try a simpler variant first: */
2243 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2244 || SvTYPE(cv) != SVt_PVCV)
2245 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2247 if (!cv || !CvROOT(cv)) {
2248 if (ckWARN(WARN_SIGNAL))
2249 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2250 PL_sig_name[sig], (gv ? GvENAME(gv)
2257 if(PL_psig_name[sig]) {
2258 sv = SvREFCNT_inc(PL_psig_name[sig]);
2262 sv = sv_newmortal();
2263 sv_setpv(sv,PL_sig_name[sig]);
2266 PUSHSTACKi(PERLSI_SIGNAL);
2271 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2274 if (SvTRUE(ERRSV)) {
2276 #ifdef HAS_SIGPROCMASK
2277 /* Handler "died", for example to get out of a restart-able read().
2278 * Before we re-do that on its behalf re-enable the signal which was
2279 * blocked by the system when we entered.
2283 sigaddset(&set,sig);
2284 sigprocmask(SIG_UNBLOCK, &set, NULL);
2286 /* Not clear if this will work */
2287 (void)rsignal(sig, SIG_IGN);
2288 (void)rsignal(sig, &Perl_csighandler);
2290 #endif /* !PERL_MICRO */
2291 Perl_die(aTHX_ Nullch);
2295 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2301 PL_scopestack_ix -= 1;
2304 PL_op = myop; /* Apparently not needed... */
2306 PL_Sv = tSv; /* Restore global temporaries. */
2317 restore_magic(pTHXo_ void *p)
2319 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2320 SV* sv = mgs->mgs_sv;
2325 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2328 SvFLAGS(sv) |= mgs->mgs_flags;
2332 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2335 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2337 /* If we're still on top of the stack, pop us off. (That condition
2338 * will be satisfied if restore_magic was called explicitly, but *not*
2339 * if it's being called via leave_scope.)
2340 * The reason for doing this is that otherwise, things like sv_2cv()
2341 * may leave alloc gunk on the savestack, and some code
2342 * (e.g. sighandler) doesn't expect that...
2344 if (PL_savestack_ix == mgs->mgs_ss_ix)
2346 I32 popval = SSPOPINT;
2347 assert(popval == SAVEt_DESTRUCTOR_X);
2348 PL_savestack_ix -= 2;
2350 assert(popval == SAVEt_ALLOC);
2352 PL_savestack_ix -= popval;
2358 unwind_handler_stack(pTHXo_ void *p)
2360 U32 flags = *(U32*)p;
2363 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2364 /* cxstack_ix-- Not needed, die already unwound it. */
2366 SvREFCNT_dec(sig_sv);