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(pTHX_ void *p);
29 static void unwind_handler_stack(pTHX_ void *p);
32 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
40 /* MGS is typedef'ed to struct magic_state in perl.h */
43 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
46 assert(SvMAGICAL(sv));
48 SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix));
50 mgs = SSPTR(mgs_ix, MGS*);
52 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
53 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
57 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
61 =for apidoc mg_magical
63 Turns on the magical status of an SV. See C<sv_magic>.
69 Perl_mg_magical(pTHX_ SV *sv)
72 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
73 MGVTBL* vtbl = mg->mg_virtual;
75 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
79 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
88 Do magic after a value is retrieved from the SV. See C<sv_magic>.
94 Perl_mg_get(pTHX_ SV *sv)
97 MAGIC *newmg, *head, *cur, *mg;
98 I32 mgs_ix = SSNEW(sizeof(MGS));
100 save_magic(mgs_ix, sv);
102 /* We must call svt_get(sv, mg) for each valid entry in the linked
103 list of magic. svt_get() may delete the current entry, add new
104 magic to the head of the list, or upgrade the SV. AMS 20010810 */
106 newmg = cur = head = mg = SvMAGIC(sv);
108 MGVTBL *vtbl = mg->mg_virtual;
110 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
111 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
112 /* Don't restore the flags for this entry if it was deleted. */
113 if (mg->mg_flags & MGf_GSKIP)
114 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
117 mg = mg->mg_moremagic;
120 /* Have we finished with the new entries we saw? Start again
121 where we left off (unless there are more new entries). */
129 /* Were any new entries added? */
130 if (!new && (newmg = SvMAGIC(sv)) != head) {
137 restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix));
144 Do magic after a value is assigned to the SV. See C<sv_magic>.
150 Perl_mg_set(pTHX_ SV *sv)
156 mgs_ix = SSNEW(sizeof(MGS));
157 save_magic(mgs_ix, sv);
159 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
160 MGVTBL* vtbl = mg->mg_virtual;
161 nextmg = mg->mg_moremagic; /* it may delete itself */
162 if (mg->mg_flags & MGf_GSKIP) {
163 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
164 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
166 if (vtbl && vtbl->svt_set)
167 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
170 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
175 =for apidoc mg_length
177 Report on the SV's length. See C<sv_magic>.
183 Perl_mg_length(pTHX_ SV *sv)
188 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
189 MGVTBL* vtbl = mg->mg_virtual;
190 if (vtbl && vtbl->svt_len) {
193 mgs_ix = SSNEW(sizeof(MGS));
194 save_magic(mgs_ix, sv);
195 /* omit MGf_GSKIP -- not changed here */
196 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
197 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
204 U8 *s = (U8*)SvPV(sv, len);
205 len = Perl_utf8_length(aTHX_ s, s + len);
213 Perl_mg_size(pTHX_ SV *sv)
218 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
219 MGVTBL* vtbl = mg->mg_virtual;
220 if (vtbl && vtbl->svt_len) {
223 mgs_ix = SSNEW(sizeof(MGS));
224 save_magic(mgs_ix, sv);
225 /* omit MGf_GSKIP -- not changed here */
226 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
227 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
234 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
239 Perl_croak(aTHX_ "Size magic not implemented");
248 Clear something magical that the SV represents. See C<sv_magic>.
254 Perl_mg_clear(pTHX_ SV *sv)
259 mgs_ix = SSNEW(sizeof(MGS));
260 save_magic(mgs_ix, sv);
262 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
263 MGVTBL* vtbl = mg->mg_virtual;
264 /* omit GSKIP -- never set here */
266 if (vtbl && vtbl->svt_clear)
267 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
270 restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix));
277 Finds the magic pointer for type matching the SV. See C<sv_magic>.
283 Perl_mg_find(pTHX_ SV *sv, int type)
288 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
289 if (mg->mg_type == type)
298 Copies the magic from one SV to another. See C<sv_magic>.
304 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
308 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
309 if (isUPPER(mg->mg_type)) {
311 mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) :
312 (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj)
314 toLOWER(mg->mg_type), key, klen);
324 Free any magic storage used by the SV. See C<sv_magic>.
330 Perl_mg_free(pTHX_ SV *sv)
334 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
335 MGVTBL* vtbl = mg->mg_virtual;
336 moremagic = mg->mg_moremagic;
337 if (vtbl && vtbl->svt_free)
338 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
339 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
341 Safefree(mg->mg_ptr);
342 else if (mg->mg_len == HEf_SVKEY)
343 SvREFCNT_dec((SV*)mg->mg_ptr);
345 if (mg->mg_flags & MGf_REFCOUNTED)
346 SvREFCNT_dec(mg->mg_obj);
354 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
359 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
363 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
364 if (mg->mg_obj) /* @+ */
367 return rx->lastparen;
374 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
382 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
386 if (paren <= rx->nparens &&
387 (s = rx->startp[paren]) != -1 &&
388 (t = rx->endp[paren]) != -1)
390 if (mg->mg_obj) /* @+ */
395 if (i > 0 && PL_reg_match_utf8) {
396 char *b = rx->subbeg;
398 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
408 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
410 Perl_croak(aTHX_ PL_no_modify);
416 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
423 switch (*mg->mg_ptr) {
424 case '1': case '2': case '3': case '4':
425 case '5': case '6': case '7': case '8': case '9': case '&':
426 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
428 paren = atoi(mg->mg_ptr); /* $& is in [0] */
430 if (paren <= rx->nparens &&
431 (s1 = rx->startp[paren]) != -1 &&
432 (t1 = rx->endp[paren]) != -1)
436 if (i > 0 && PL_reg_match_utf8) {
437 char *s = rx->subbeg + s1;
438 char *send = rx->subbeg + t1;
441 if (is_utf8_string((U8*)s, i))
442 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
445 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
451 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
452 paren = rx->lastparen;
457 case '\016': /* ^N */
458 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
459 paren = rx->lastcloseparen;
465 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
466 if (rx->startp[0] != -1) {
477 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
478 if (rx->endp[0] != -1) {
479 i = rx->sublen - rx->endp[0];
490 if (!SvPOK(sv) && SvNIOK(sv)) {
500 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
507 switch (*mg->mg_ptr) {
508 case '\001': /* ^A */
509 sv_setsv(sv, PL_bodytarget);
511 case '\003': /* ^C */
512 sv_setiv(sv, (IV)PL_minus_c);
515 case '\004': /* ^D */
516 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
517 #if defined(YYDEBUG) && defined(DEBUGGING)
518 PL_yydebug = DEBUG_p_TEST;
521 case '\005': /* ^E */
522 #ifdef MACOS_TRADITIONAL
526 sv_setnv(sv,(double)gMacPerl_OSErr);
527 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
532 # include <descrip.h>
533 # include <starlet.h>
535 $DESCRIPTOR(msgdsc,msg);
536 sv_setnv(sv,(NV) vaxc$errno);
537 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
538 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
544 if (!(_emx_env & 0x200)) { /* Under DOS */
545 sv_setnv(sv, (NV)errno);
546 sv_setpv(sv, errno ? Strerror(errno) : "");
548 if (errno != errno_isOS2) {
549 int tmp = _syserrno();
550 if (tmp) /* 2nd call to _syserrno() makes it 0 */
553 sv_setnv(sv, (NV)Perl_rc);
554 sv_setpv(sv, os2error(Perl_rc));
559 DWORD dwErr = GetLastError();
560 sv_setnv(sv, (NV)dwErr);
563 PerlProc_GetOSError(sv, dwErr);
570 sv_setnv(sv, (NV)errno);
571 sv_setpv(sv, errno ? Strerror(errno) : "");
576 SvNOK_on(sv); /* what a wonderful hack! */
578 case '\006': /* ^F */
579 sv_setiv(sv, (IV)PL_maxsysfd);
581 case '\010': /* ^H */
582 sv_setiv(sv, (IV)PL_hints);
584 case '\011': /* ^I */ /* NOT \t in EBCDIC */
586 sv_setpv(sv, PL_inplace);
588 sv_setsv(sv, &PL_sv_undef);
590 case '\017': /* ^O & ^OPEN */
591 if (*(mg->mg_ptr+1) == '\0')
592 sv_setpv(sv, PL_osname);
593 else if (strEQ(mg->mg_ptr, "\017PEN")) {
594 if (!PL_compiling.cop_io)
595 sv_setsv(sv, &PL_sv_undef);
597 sv_setsv(sv, PL_compiling.cop_io);
601 case '\020': /* ^P */
602 sv_setiv(sv, (IV)PL_perldb);
604 case '\023': /* ^S */
606 if (PL_lex_state != LEX_NOTPARSING)
609 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
614 case '\024': /* ^T */
616 sv_setnv(sv, PL_basetime);
618 sv_setiv(sv, (IV)PL_basetime);
621 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
622 if (*(mg->mg_ptr+1) == '\0')
623 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
624 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
625 if (PL_compiling.cop_warnings == pWARN_NONE ||
626 PL_compiling.cop_warnings == pWARN_STD)
628 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
630 else if (PL_compiling.cop_warnings == pWARN_ALL) {
631 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
634 sv_setsv(sv, PL_compiling.cop_warnings);
638 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
639 sv_setiv(sv, (IV)PL_widesyscalls);
641 case '1': case '2': case '3': case '4':
642 case '5': case '6': case '7': case '8': case '9': case '&':
643 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
647 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
648 * XXX Does the new way break anything?
650 paren = atoi(mg->mg_ptr); /* $& is in [0] */
652 if (paren <= rx->nparens &&
653 (s1 = rx->startp[paren]) != -1 &&
654 (t1 = rx->endp[paren]) != -1)
663 bool was_tainted = FALSE;
665 was_tainted = PL_tainted;
669 if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i))
674 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
679 sv_setsv(sv,&PL_sv_undef);
682 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
683 paren = rx->lastparen;
687 sv_setsv(sv,&PL_sv_undef);
689 case '\016': /* ^N */
690 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
691 paren = rx->lastcloseparen;
695 sv_setsv(sv,&PL_sv_undef);
698 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
699 if ((s = rx->subbeg) && rx->startp[0] != -1) {
704 sv_setsv(sv,&PL_sv_undef);
707 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
708 if (rx->subbeg && rx->endp[0] != -1) {
709 s = rx->subbeg + rx->endp[0];
710 i = rx->sublen - rx->endp[0];
714 sv_setsv(sv,&PL_sv_undef);
718 if (GvIO(PL_last_in_gv)) {
719 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
725 sv_setiv(sv, (IV)STATUS_CURRENT);
726 #ifdef COMPLEX_STATUS
727 LvTARGOFF(sv) = PL_statusvalue;
728 LvTARGLEN(sv) = PL_statusvalue_vms;
733 s = IoTOP_NAME(GvIOp(PL_defoutgv));
737 sv_setpv(sv,GvENAME(PL_defoutgv));
742 s = IoFMT_NAME(GvIOp(PL_defoutgv));
744 s = GvENAME(PL_defoutgv);
749 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
752 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
755 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
763 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
766 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
773 sv_setpv(sv,PL_ofmt);
777 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
778 sv_setpv(sv, errno ? Strerror(errno) : "");
781 int saveerrno = errno;
782 sv_setnv(sv, (NV)errno);
784 if (errno == errno_isOS2 || errno == errno_isOS2_set)
785 sv_setpv(sv, os2error(Perl_rc));
788 sv_setpv(sv, errno ? Strerror(errno) : "");
792 SvNOK_on(sv); /* what a wonderful hack! */
795 sv_setiv(sv, (IV)PL_uid);
798 sv_setiv(sv, (IV)PL_euid);
801 sv_setiv(sv, (IV)PL_gid);
803 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
807 sv_setiv(sv, (IV)PL_egid);
809 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
814 Groups_t gary[NGROUPS];
815 i = getgroups(NGROUPS,gary);
817 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
820 (void)SvIOK_on(sv); /* what a wonderful hack! */
824 #ifndef MACOS_TRADITIONAL
828 #ifdef USE_5005THREADS
830 sv_setsv(sv, thr->errsv);
832 #endif /* USE_5005THREADS */
838 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
840 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
842 if (uf && uf->uf_val)
843 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
848 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
859 #ifdef DYNAMIC_ENV_FETCH
860 /* We just undefd an environment var. Is a replacement */
861 /* waiting in the wings? */
864 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
865 s = SvPV(*valp, len);
869 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
870 /* And you'll never guess what the dog had */
871 /* in its mouth... */
873 MgTAINTEDDIR_off(mg);
875 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
876 char pathbuf[256], eltbuf[256], *cp, *elt = s;
880 do { /* DCL$PATH may be a search list */
881 while (1) { /* as may dev portion of any element */
882 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
883 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
884 cando_by_name(S_IWUSR,0,elt) ) {
889 if ((cp = strchr(elt, ':')) != Nullch)
891 if (my_trnlnm(elt, eltbuf, j++))
897 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
900 if (s && klen == 4 && strEQ(ptr,"PATH")) {
901 char *strend = s + len;
906 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
909 if (i >= sizeof tmpbuf /* too long -- assume the worst */
911 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
918 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
924 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
927 my_setenv(MgPV(mg,n_a),Nullch);
932 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
935 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
940 magic_clear_all_env(sv,mg);
941 hv_iterinit((HV*)sv);
942 while ((entry = hv_iternext((HV*)sv))) {
944 my_setenv(hv_iterkey(entry, &keylen),
945 SvPV(hv_iterval((HV*)sv, entry), n_a));
953 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
955 #if defined(VMS) || defined(EPOC)
956 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
958 # ifdef PERL_IMPLICIT_SYS
962 char *envv = GetEnvironmentStrings();
966 char *end = strchr(cur,'=');
967 if (end && end != cur) {
969 my_setenv(cur,Nullch);
971 cur = end + strlen(end+1)+2;
973 else if ((len = strlen(cur)))
976 FreeEnvironmentStrings(envv);
978 #ifdef USE_ENVIRON_ARRAY
979 # ifndef PERL_USE_SAFE_PUTENV
982 if (environ == PL_origenviron)
983 environ = (char**)safesysmalloc(sizeof(char*));
985 for (i = 0; environ[i]; i++)
986 safesysfree(environ[i]);
987 # endif /* PERL_USE_SAFE_PUTENV */
991 #endif /* USE_ENVIRON_ARRAY */
993 # endif /* PERL_IMPLICIT_SYS */
1000 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1004 /* Are we fetching a signal entry? */
1005 i = whichsig(MgPV(mg,n_a));
1008 sv_setsv(sv,PL_psig_ptr[i]);
1010 Sighandler_t sigstate = rsignal_state(i);
1012 /* cache state so we don't fetch it again */
1013 if(sigstate == SIG_IGN)
1014 sv_setpv(sv,"IGNORE");
1016 sv_setsv(sv,&PL_sv_undef);
1017 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1024 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1028 /* Are we clearing a signal entry? */
1029 i = whichsig(MgPV(mg,n_a));
1031 if(PL_psig_ptr[i]) {
1032 SvREFCNT_dec(PL_psig_ptr[i]);
1035 if(PL_psig_name[i]) {
1036 SvREFCNT_dec(PL_psig_name[i]);
1044 Perl_raise_signal(pTHX_ int sig)
1046 /* Set a flag to say this signal is pending */
1047 PL_psig_pend[sig]++;
1048 /* And one to say _a_ signal is pending */
1053 Perl_csighandler(int sig)
1055 #ifdef PERL_OLD_SIGNALS
1056 /* Call the perl level handler now with risk we may be in malloc() etc. */
1057 (*PL_sighandlerp)(sig);
1060 Perl_raise_signal(aTHX_ sig);
1065 Perl_despatch_signals(pTHX)
1069 for (sig = 1; sig < SIG_SIZE; sig++) {
1070 if (PL_psig_pend[sig]) {
1071 PL_psig_pend[sig] = 0;
1072 (*PL_sighandlerp)(sig);
1078 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1087 if (strEQ(s,"__DIE__"))
1089 else if (strEQ(s,"__WARN__"))
1092 Perl_croak(aTHX_ "No such hook: %s", s);
1100 i = whichsig(s); /* ...no, a brick */
1102 if (ckWARN(WARN_SIGNAL))
1103 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1106 SvREFCNT_dec(PL_psig_name[i]);
1107 SvREFCNT_dec(PL_psig_ptr[i]);
1108 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1109 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1110 PL_psig_name[i] = newSVpvn(s, len);
1111 SvREADONLY_on(PL_psig_name[i]);
1113 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1115 (void)rsignal(i, &Perl_csighandler);
1117 *svp = SvREFCNT_inc(sv);
1120 s = SvPV_force(sv,len);
1121 if (strEQ(s,"IGNORE")) {
1123 (void)rsignal(i, SIG_IGN);
1127 else if (strEQ(s,"DEFAULT") || !*s) {
1129 (void)rsignal(i, SIG_DFL);
1135 * We should warn if HINT_STRICT_REFS, but without
1136 * access to a known hint bit in a known OP, we can't
1137 * tell whether HINT_STRICT_REFS is in force or not.
1139 if (!strchr(s,':') && !strchr(s,'\''))
1140 sv_insert(sv, 0, 0, "main::", 6);
1142 (void)rsignal(i, &Perl_csighandler);
1144 *svp = SvREFCNT_inc(sv);
1148 #endif /* !PERL_MICRO */
1151 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1153 PL_sub_generation++;
1158 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1160 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1161 PL_amagic_generation++;
1167 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1169 HV *hv = (HV*)LvTARG(sv);
1173 (void) hv_iterinit(hv);
1174 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1177 while (hv_iternext(hv))
1182 sv_setiv(sv, (IV)i);
1187 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1190 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1195 /* caller is responsible for stack switching/cleanup */
1197 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1203 PUSHs(SvTIED_obj(sv, mg));
1206 if (mg->mg_len >= 0)
1207 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1208 else if (mg->mg_len == HEf_SVKEY)
1209 PUSHs((SV*)mg->mg_ptr);
1211 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1212 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1220 return call_method(meth, flags);
1224 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1230 PUSHSTACKi(PERLSI_MAGIC);
1232 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1233 sv_setsv(sv, *PL_stack_sp--);
1243 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1245 magic_methpack(sv,mg,"FETCH");
1247 mg->mg_flags |= MGf_GSKIP;
1252 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1256 PUSHSTACKi(PERLSI_MAGIC);
1257 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1264 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1266 return magic_methpack(sv,mg,"DELETE");
1271 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1278 PUSHSTACKi(PERLSI_MAGIC);
1279 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1280 sv = *PL_stack_sp--;
1281 retval = (U32) SvIV(sv)-1;
1290 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1295 PUSHSTACKi(PERLSI_MAGIC);
1297 XPUSHs(SvTIED_obj(sv, mg));
1299 call_method("CLEAR", G_SCALAR|G_DISCARD);
1306 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1309 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1313 PUSHSTACKi(PERLSI_MAGIC);
1316 PUSHs(SvTIED_obj(sv, mg));
1321 if (call_method(meth, G_SCALAR))
1322 sv_setsv(key, *PL_stack_sp--);
1331 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1333 return magic_methpack(sv,mg,"EXISTS");
1337 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1347 svp = av_fetch(GvAV(gv),
1348 atoi(MgPV(mg,n_a)), FALSE);
1349 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1355 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1357 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1362 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1364 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1369 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1371 SV* lsv = LvTARG(sv);
1373 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1374 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1375 if (mg && mg->mg_len >= 0) {
1378 sv_pos_b2u(lsv, &i);
1379 sv_setiv(sv, i + PL_curcop->cop_arybase);
1388 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1390 SV* lsv = LvTARG(sv);
1397 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1398 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1402 sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1403 mg = mg_find(lsv, PERL_MAGIC_regex_global);
1405 else if (!SvOK(sv)) {
1409 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1411 pos = SvIV(sv) - PL_curcop->cop_arybase;
1414 ulen = sv_len_utf8(lsv);
1429 sv_pos_u2b(lsv, &p, 0);
1434 mg->mg_flags &= ~MGf_MINMATCH;
1440 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1442 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1444 gv_efullname3(sv,((GV*)sv), "*");
1448 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1453 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1462 if (*s == '*' && s[1])
1464 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1469 GvGP(sv) = gp_ref(GvGP(gv));
1474 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1477 SV *lsv = LvTARG(sv);
1478 char *tmps = SvPV(lsv,len);
1479 I32 offs = LvTARGOFF(sv);
1480 I32 rem = LvTARGLEN(sv);
1483 sv_pos_u2b(lsv, &offs, &rem);
1486 if (rem + offs > len)
1488 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1495 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1498 char *tmps = SvPV(sv, len);
1499 SV *lsv = LvTARG(sv);
1500 I32 lvoff = LvTARGOFF(sv);
1501 I32 lvlen = LvTARGLEN(sv);
1504 sv_utf8_upgrade(lsv);
1505 sv_pos_u2b(lsv, &lvoff, &lvlen);
1506 sv_insert(lsv, lvoff, lvlen, tmps, len);
1509 else if (lsv && SvUTF8(lsv)) {
1510 sv_pos_u2b(lsv, &lvoff, &lvlen);
1511 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1512 sv_insert(lsv, lvoff, lvlen, tmps, len);
1516 sv_insert(lsv, lvoff, lvlen, tmps, len);
1522 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1524 TAINT_IF((mg->mg_len & 1) ||
1525 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1530 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1532 if (PL_localizing) {
1533 if (PL_localizing == 1)
1538 else if (PL_tainted)
1546 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1548 SV *lsv = LvTARG(sv);
1555 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1560 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1562 do_vecset(sv); /* XXX slurp this routine */
1567 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1570 if (LvTARGLEN(sv)) {
1572 SV *ahv = LvTARG(sv);
1573 if (SvTYPE(ahv) == SVt_PVHV) {
1574 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1579 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1585 AV* av = (AV*)LvTARG(sv);
1586 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1587 targ = AvARRAY(av)[LvTARGOFF(sv)];
1589 if (targ && targ != &PL_sv_undef) {
1590 /* somebody else defined it for us */
1591 SvREFCNT_dec(LvTARG(sv));
1592 LvTARG(sv) = SvREFCNT_inc(targ);
1594 SvREFCNT_dec(mg->mg_obj);
1595 mg->mg_obj = Nullsv;
1596 mg->mg_flags &= ~MGf_REFCOUNTED;
1601 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1606 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1611 sv_setsv(LvTARG(sv), sv);
1612 SvSETMAGIC(LvTARG(sv));
1618 Perl_vivify_defelem(pTHX_ SV *sv)
1623 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
1626 SV *ahv = LvTARG(sv);
1628 if (SvTYPE(ahv) == SVt_PVHV) {
1629 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1634 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1638 if (!value || value == &PL_sv_undef)
1639 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1642 AV* av = (AV*)LvTARG(sv);
1643 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1644 LvTARG(sv) = Nullsv; /* array can't be extended */
1646 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1647 if (!svp || (value = *svp) == &PL_sv_undef)
1648 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1651 (void)SvREFCNT_inc(value);
1652 SvREFCNT_dec(LvTARG(sv));
1655 SvREFCNT_dec(mg->mg_obj);
1656 mg->mg_obj = Nullsv;
1657 mg->mg_flags &= ~MGf_REFCOUNTED;
1661 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1663 AV *av = (AV*)mg->mg_obj;
1664 SV **svp = AvARRAY(av);
1665 I32 i = AvFILLp(av);
1667 if (svp[i] && svp[i] != &PL_sv_undef) {
1668 if (!SvWEAKREF(svp[i]))
1669 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1670 /* XXX Should we check that it hasn't changed? */
1672 (void)SvOK_off(svp[i]);
1673 SvWEAKREF_off(svp[i]);
1674 svp[i] = &PL_sv_undef;
1682 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1690 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1692 sv_unmagic(sv, PERL_MAGIC_bm);
1698 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1700 sv_unmagic(sv, PERL_MAGIC_fm);
1706 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1708 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1710 if (uf && uf->uf_set)
1711 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
1716 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1718 regexp *re = (regexp *)mg->mg_obj;
1723 #ifdef USE_LOCALE_COLLATE
1725 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1728 * RenE<eacute> Descartes said "I think not."
1729 * and vanished with a faint plop.
1732 Safefree(mg->mg_ptr);
1738 #endif /* USE_LOCALE_COLLATE */
1741 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1746 switch (*mg->mg_ptr) {
1747 case '\001': /* ^A */
1748 sv_setsv(PL_bodytarget, sv);
1750 case '\003': /* ^C */
1751 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1754 case '\004': /* ^D */
1755 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG;
1756 DEBUG_x(dump_all());
1758 case '\005': /* ^E */
1759 #ifdef MACOS_TRADITIONAL
1760 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1763 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1766 SetLastError( SvIV(sv) );
1769 os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1771 /* will anyone ever use this? */
1772 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1778 case '\006': /* ^F */
1779 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1781 case '\010': /* ^H */
1782 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1784 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1786 Safefree(PL_inplace);
1788 PL_inplace = savepv(SvPV(sv,len));
1790 PL_inplace = Nullch;
1792 case '\017': /* ^O */
1793 if (*(mg->mg_ptr+1) == '\0') {
1795 Safefree(PL_osname);
1797 PL_osname = savepv(SvPV(sv,len));
1801 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1802 if (!PL_compiling.cop_io)
1803 PL_compiling.cop_io = newSVsv(sv);
1805 sv_setsv(PL_compiling.cop_io,sv);
1808 case '\020': /* ^P */
1809 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1810 if (PL_perldb && !PL_DBsingle)
1813 case '\024': /* ^T */
1815 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1817 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1820 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1821 if (*(mg->mg_ptr+1) == '\0') {
1822 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1823 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1824 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1825 | (i ? G_WARN_ON : G_WARN_OFF) ;
1828 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1829 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1830 if (!SvPOK(sv) && PL_localizing) {
1831 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1832 PL_compiling.cop_warnings = pWARN_NONE;
1837 int accumulate = 0 ;
1838 int any_fatals = 0 ;
1839 char * ptr = (char*)SvPV(sv, len) ;
1840 for (i = 0 ; i < len ; ++i) {
1841 accumulate |= ptr[i] ;
1842 any_fatals |= (ptr[i] & 0xAA) ;
1845 PL_compiling.cop_warnings = pWARN_NONE;
1846 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1847 PL_compiling.cop_warnings = pWARN_ALL;
1848 PL_dowarn |= G_WARN_ONCE ;
1851 if (specialWARN(PL_compiling.cop_warnings))
1852 PL_compiling.cop_warnings = newSVsv(sv) ;
1854 sv_setsv(PL_compiling.cop_warnings, sv);
1855 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1856 PL_dowarn |= G_WARN_ONCE ;
1862 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1863 PL_widesyscalls = SvTRUE(sv);
1866 if (PL_localizing) {
1867 if (PL_localizing == 1)
1868 SAVESPTR(PL_last_in_gv);
1870 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1871 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1874 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1875 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1876 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1879 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1880 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1881 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1884 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1887 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1888 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1889 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1892 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1896 IO *io = GvIOp(PL_defoutgv);
1899 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1900 IoFLAGS(io) &= ~IOf_FLUSH;
1902 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1903 PerlIO *ofp = IoOFP(io);
1905 (void)PerlIO_flush(ofp);
1906 IoFLAGS(io) |= IOf_FLUSH;
1912 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1913 PL_multiline = (i != 0);
1916 SvREFCNT_dec(PL_rs);
1917 PL_rs = newSVsv(sv);
1921 SvREFCNT_dec(PL_ors_sv);
1922 if (SvOK(sv) || SvGMAGICAL(sv)) {
1923 PL_ors_sv = newSVsv(sv);
1931 SvREFCNT_dec(PL_ofs_sv);
1932 if (SvOK(sv) || SvGMAGICAL(sv)) {
1933 PL_ofs_sv = newSVsv(sv);
1942 PL_ofmt = savepv(SvPV(sv,len));
1945 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1948 #ifdef COMPLEX_STATUS
1949 if (PL_localizing == 2) {
1950 PL_statusvalue = LvTARGOFF(sv);
1951 PL_statusvalue_vms = LvTARGLEN(sv);
1955 #ifdef VMSISH_STATUS
1957 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1960 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1963 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1964 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1967 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1968 if (PL_delaymagic) {
1969 PL_delaymagic |= DM_RUID;
1970 break; /* don't do magic till later */
1973 (void)setruid((Uid_t)PL_uid);
1976 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1978 #ifdef HAS_SETRESUID
1979 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1981 if (PL_uid == PL_euid) /* special case $< = $> */
1982 (void)PerlProc_setuid(PL_uid);
1984 PL_uid = PerlProc_getuid();
1985 Perl_croak(aTHX_ "setruid() not implemented");
1990 PL_uid = PerlProc_getuid();
1991 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1994 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1995 if (PL_delaymagic) {
1996 PL_delaymagic |= DM_EUID;
1997 break; /* don't do magic till later */
2000 (void)seteuid((Uid_t)PL_euid);
2003 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2005 #ifdef HAS_SETRESUID
2006 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2008 if (PL_euid == PL_uid) /* special case $> = $< */
2009 PerlProc_setuid(PL_euid);
2011 PL_euid = PerlProc_geteuid();
2012 Perl_croak(aTHX_ "seteuid() not implemented");
2017 PL_euid = PerlProc_geteuid();
2018 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2021 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2022 if (PL_delaymagic) {
2023 PL_delaymagic |= DM_RGID;
2024 break; /* don't do magic till later */
2027 (void)setrgid((Gid_t)PL_gid);
2030 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2032 #ifdef HAS_SETRESGID
2033 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2035 if (PL_gid == PL_egid) /* special case $( = $) */
2036 (void)PerlProc_setgid(PL_gid);
2038 PL_gid = PerlProc_getgid();
2039 Perl_croak(aTHX_ "setrgid() not implemented");
2044 PL_gid = PerlProc_getgid();
2045 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2048 #ifdef HAS_SETGROUPS
2050 char *p = SvPV(sv, len);
2051 Groups_t gary[NGROUPS];
2056 for (i = 0; i < NGROUPS; ++i) {
2057 while (*p && !isSPACE(*p))
2066 (void)setgroups(i, gary);
2068 #else /* HAS_SETGROUPS */
2069 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
2070 #endif /* HAS_SETGROUPS */
2071 if (PL_delaymagic) {
2072 PL_delaymagic |= DM_EGID;
2073 break; /* don't do magic till later */
2076 (void)setegid((Gid_t)PL_egid);
2079 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2081 #ifdef HAS_SETRESGID
2082 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2084 if (PL_egid == PL_gid) /* special case $) = $( */
2085 (void)PerlProc_setgid(PL_egid);
2087 PL_egid = PerlProc_getegid();
2088 Perl_croak(aTHX_ "setegid() not implemented");
2093 PL_egid = PerlProc_getegid();
2094 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2097 PL_chopset = SvPV_force(sv,len);
2099 #ifndef MACOS_TRADITIONAL
2101 #ifdef HAS_SETPROCTITLE
2102 /* The BSDs don't show the argv[] in ps(1) output, they
2103 * show a string from the process struct and provide
2104 * the setproctitle() routine to manipulate that. */
2107 # if __FreeBSD_version > 410001
2108 /* The leading "-" removes the "perl: " prefix,
2109 * but not the "(perl) suffix from the ps(1)
2110 * output, because that's what ps(1) shows if the
2111 * argv[] is modified. */
2112 setproctitle("-%s", s);
2113 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2114 /* This doesn't really work if you assume that
2115 * $0 = 'foobar'; will wipe out 'perl' from the $0
2116 * because in ps(1) output the result will be like
2117 * sprintf("perl: %s (perl)", s)
2118 * I guess this is a security feature:
2119 * one (a user process) cannot get rid of the original name.
2121 setproctitle("%s", s);
2128 /* See if all the arguments are contiguous in memory */
2129 for (i = 1; i < PL_origargc; i++) {
2130 if (PL_origargv[i] == s + 1
2132 || PL_origargv[i] == s + 2
2137 s += strlen(s); /* this one is ok too */
2142 /* can grab env area too? */
2143 if (PL_origenviron && (PL_origenviron[0] == s + 1)) {
2144 my_setenv("NoNe SuCh", Nullch);
2145 /* force copy of environment */
2146 for (i = 0; PL_origenviron[i]; i++)
2147 if (PL_origenviron[i] == s + 1) {
2154 PL_origalen = s - PL_origargv[0];
2156 s = SvPV_force(sv,len);
2158 if (i >= PL_origalen) {
2160 /* don't allow system to limit $0 seen by script */
2161 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2162 Copy(s, PL_origargv[0], i, char);
2163 s = PL_origargv[0]+i;
2167 Copy(s, PL_origargv[0], i, char);
2168 s = PL_origargv[0]+i;
2170 while (++i < PL_origalen)
2172 s = PL_origargv[0]+i;
2173 for (i = 1; i < PL_origargc; i++)
2174 PL_origargv[i] = Nullch;
2178 #ifdef USE_5005THREADS
2180 sv_setsv(thr->errsv, sv);
2182 #endif /* USE_5005THREADS */
2187 #ifdef USE_5005THREADS
2189 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2191 DEBUG_S(PerlIO_printf(Perl_debug_log,
2192 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2193 PTR2UV(thr), PTR2UV(sv)));
2195 Perl_croak(aTHX_ "panic: magic_mutexfree");
2196 MUTEX_DESTROY(MgMUTEXP(mg));
2197 COND_DESTROY(MgCONDP(mg));
2200 #endif /* USE_5005THREADS */
2203 Perl_whichsig(pTHX_ char *sig)
2205 register char **sigv;
2207 for (sigv = PL_sig_name+1; *sigv; sigv++)
2208 if (strEQ(sig,*sigv))
2209 return PL_sig_num[sigv - PL_sig_name];
2211 if (strEQ(sig,"CHLD"))
2215 if (strEQ(sig,"CLD"))
2224 Perl_sighandler(int sig)
2226 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2227 dTHXa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2234 SV *sv = Nullsv, *tSv = PL_Sv;
2240 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2241 PERL_SET_THX(aTHX); /* fake TLS, see above */
2244 if (PL_savestack_ix + 15 <= PL_savestack_max)
2246 if (PL_markstack_ptr < PL_markstack_max - 2)
2248 if (PL_retstack_ix < PL_retstack_max - 2)
2250 if (PL_scopestack_ix < PL_scopestack_max - 3)
2253 if (!PL_psig_ptr[sig])
2254 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2257 /* Max number of items pushed there is 3*n or 4. We cannot fix
2258 infinity, so we fix 4 (in fact 5): */
2260 PL_savestack_ix += 5; /* Protect save in progress. */
2261 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2264 PL_markstack_ptr++; /* Protect mark. */
2267 PL_retstack[PL_retstack_ix] = NULL;
2270 PL_scopestack_ix += 1;
2271 /* sv_2cv is too complicated, try a simpler variant first: */
2272 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2273 || SvTYPE(cv) != SVt_PVCV)
2274 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2276 if (!cv || !CvROOT(cv)) {
2277 if (ckWARN(WARN_SIGNAL))
2278 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2279 PL_sig_name[sig], (gv ? GvENAME(gv)
2286 if(PL_psig_name[sig]) {
2287 sv = SvREFCNT_inc(PL_psig_name[sig]);
2291 sv = sv_newmortal();
2292 sv_setpv(sv,PL_sig_name[sig]);
2295 PUSHSTACKi(PERLSI_SIGNAL);
2300 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2303 if (SvTRUE(ERRSV)) {
2305 #ifdef HAS_SIGPROCMASK
2306 /* Handler "died", for example to get out of a restart-able read().
2307 * Before we re-do that on its behalf re-enable the signal which was
2308 * blocked by the system when we entered.
2312 sigaddset(&set,sig);
2313 sigprocmask(SIG_UNBLOCK, &set, NULL);
2315 /* Not clear if this will work */
2316 (void)rsignal(sig, SIG_IGN);
2317 (void)rsignal(sig, &Perl_csighandler);
2319 #endif /* !PERL_MICRO */
2320 Perl_die(aTHX_ Nullformat);
2324 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2330 PL_scopestack_ix -= 1;
2333 PL_op = myop; /* Apparently not needed... */
2335 PL_Sv = tSv; /* Restore global temporaries. */
2342 restore_magic(pTHX_ void *p)
2344 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2345 SV* sv = mgs->mgs_sv;
2350 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2353 SvFLAGS(sv) |= mgs->mgs_flags;
2357 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2360 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2362 /* If we're still on top of the stack, pop us off. (That condition
2363 * will be satisfied if restore_magic was called explicitly, but *not*
2364 * if it's being called via leave_scope.)
2365 * The reason for doing this is that otherwise, things like sv_2cv()
2366 * may leave alloc gunk on the savestack, and some code
2367 * (e.g. sighandler) doesn't expect that...
2369 if (PL_savestack_ix == mgs->mgs_ss_ix)
2371 I32 popval = SSPOPINT;
2372 assert(popval == SAVEt_DESTRUCTOR_X);
2373 PL_savestack_ix -= 2;
2375 assert(popval == SAVEt_ALLOC);
2377 PL_savestack_ix -= popval;
2383 unwind_handler_stack(pTHX_ void *p)
2385 U32 flags = *(U32*)p;
2388 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2389 /* cxstack_ix-- Not needed, die already unwound it. */
2391 SvREFCNT_dec(sig_sv);