This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
No DESTROY on untie. Tie memory leak fixed.
[perl5.git] / mg.c
CommitLineData
a0d0e21e 1/* mg.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805
LW
4 *
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.
7 *
a0d0e21e
LW
8 */
9
10/*
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."
79072805
LW
13 */
14
15#include "EXTERN.h"
16#include "perl.h"
17
e6d9441c 18/* XXX If this causes problems, set i_unistd=undef in the hint file. */
a0d0e21e
LW
19#ifdef I_UNISTD
20# include <unistd.h>
21#endif
a0d0e21e 22
5cd24f17 23#if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
188ea221
CS
24# ifndef NGROUPS
25# define NGROUPS 32
26# endif
27#endif
28
c07a80fd
PP
29/*
30 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
31 */
32
33struct magic_state {
34 SV* mgs_sv;
35 U32 mgs_flags;
36};
37typedef struct magic_state MGS;
38
39static void restore_magic _((void *p));
40
48e43a1c
CS
41static void
42save_magic(mgs, sv)
43MGS* mgs;
c07a80fd
PP
44SV* sv;
45{
c07a80fd
PP
46 assert(SvMAGICAL(sv));
47
c07a80fd
PP
48 mgs->mgs_sv = sv;
49 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
50 SAVEDESTRUCTOR(restore_magic, mgs);
51
52 SvMAGICAL_off(sv);
53 SvREADONLY_off(sv);
54 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
c07a80fd
PP
55}
56
57static void
58restore_magic(p)
59void* p;
60{
48e43a1c 61 MGS* mgs = (MGS*)p;
c07a80fd
PP
62 SV* sv = mgs->mgs_sv;
63
64 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
65 {
66 if (mgs->mgs_flags)
67 SvFLAGS(sv) |= mgs->mgs_flags;
68 else
69 mg_magical(sv);
70 if (SvGMAGICAL(sv))
71 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
72 }
c07a80fd
PP
73}
74
8e07c86e 75
8990e307
LW
76void
77mg_magical(sv)
78SV* sv;
79{
80 MAGIC* mg;
81 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
82 MGVTBL* vtbl = mg->mg_virtual;
83 if (vtbl) {
a0d0e21e 84 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
8990e307
LW
85 SvGMAGICAL_on(sv);
86 if (vtbl->svt_set)
87 SvSMAGICAL_on(sv);
88 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
89 SvRMAGICAL_on(sv);
90 }
91 }
92}
93
79072805
LW
94int
95mg_get(sv)
96SV* sv;
97{
48e43a1c 98 MGS mgs;
79072805 99 MAGIC* mg;
c6496cc7 100 MAGIC** mgp;
760ac839 101 int mgp_valid = 0;
463ee0b2 102
c07a80fd 103 ENTER;
48e43a1c 104 save_magic(&mgs, sv);
463ee0b2 105
c6496cc7
PP
106 mgp = &SvMAGIC(sv);
107 while ((mg = *mgp) != 0) {
79072805 108 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e 109 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
79072805 110 (*vtbl->svt_get)(sv, mg);
c6496cc7 111 /* Ignore this magic if it's been deleted */
48e43a1c
CS
112 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
113 (mg->mg_flags & MGf_GSKIP))
114 mgs.mgs_flags = 0;
a0d0e21e 115 }
c6496cc7 116 /* Advance to next magic (complicated by possible deletion) */
760ac839 117 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
c6496cc7 118 mgp = &mg->mg_moremagic;
760ac839
LW
119 mgp_valid = 1;
120 }
121 else
122 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
79072805 123 }
463ee0b2 124
c07a80fd 125 LEAVE;
79072805
LW
126 return 0;
127}
128
129int
130mg_set(sv)
131SV* sv;
132{
48e43a1c 133 MGS mgs;
79072805 134 MAGIC* mg;
463ee0b2
LW
135 MAGIC* nextmg;
136
c07a80fd 137 ENTER;
48e43a1c 138 save_magic(&mgs, sv);
463ee0b2
LW
139
140 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
79072805 141 MGVTBL* vtbl = mg->mg_virtual;
463ee0b2 142 nextmg = mg->mg_moremagic; /* it may delete itself */
a0d0e21e
LW
143 if (mg->mg_flags & MGf_GSKIP) {
144 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
48e43a1c 145 mgs.mgs_flags = 0;
a0d0e21e 146 }
79072805
LW
147 if (vtbl && vtbl->svt_set)
148 (*vtbl->svt_set)(sv, mg);
149 }
463ee0b2 150
c07a80fd 151 LEAVE;
79072805
LW
152 return 0;
153}
154
155U32
156mg_len(sv)
157SV* sv;
158{
159 MAGIC* mg;
748a9306 160 char *junk;
463ee0b2 161 STRLEN len;
463ee0b2 162
79072805
LW
163 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
164 MGVTBL* vtbl = mg->mg_virtual;
85e6fe83 165 if (vtbl && vtbl->svt_len) {
48e43a1c
CS
166 MGS mgs;
167
c07a80fd 168 ENTER;
48e43a1c 169 save_magic(&mgs, sv);
a0d0e21e 170 /* omit MGf_GSKIP -- not changed here */
85e6fe83 171 len = (*vtbl->svt_len)(sv, mg);
c07a80fd 172 LEAVE;
85e6fe83
LW
173 return len;
174 }
175 }
176
748a9306 177 junk = SvPV(sv, len);
463ee0b2 178 return len;
79072805
LW
179}
180
181int
182mg_clear(sv)
183SV* sv;
184{
48e43a1c 185 MGS mgs;
79072805 186 MAGIC* mg;
463ee0b2 187
c07a80fd 188 ENTER;
48e43a1c 189 save_magic(&mgs, sv);
463ee0b2 190
79072805
LW
191 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
192 MGVTBL* vtbl = mg->mg_virtual;
a0d0e21e
LW
193 /* omit GSKIP -- never set here */
194
79072805
LW
195 if (vtbl && vtbl->svt_clear)
196 (*vtbl->svt_clear)(sv, mg);
197 }
463ee0b2 198
c07a80fd 199 LEAVE;
79072805
LW
200 return 0;
201}
202
93a17b20
LW
203MAGIC*
204mg_find(sv, type)
205SV* sv;
a0d0e21e 206int type;
93a17b20
LW
207{
208 MAGIC* mg;
93a17b20
LW
209 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
210 if (mg->mg_type == type)
211 return mg;
212 }
213 return 0;
214}
215
79072805 216int
463ee0b2 217mg_copy(sv, nsv, key, klen)
79072805 218SV* sv;
463ee0b2
LW
219SV* nsv;
220char *key;
88e89b8a 221I32 klen;
79072805 222{
463ee0b2 223 int count = 0;
79072805 224 MAGIC* mg;
463ee0b2
LW
225 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
226 if (isUPPER(mg->mg_type)) {
a0d0e21e 227 sv_magic(nsv, mg->mg_obj, toLOWER(mg->mg_type), key, klen);
463ee0b2 228 count++;
79072805 229 }
79072805 230 }
463ee0b2 231 return count;
79072805
LW
232}
233
234int
463ee0b2 235mg_free(sv)
79072805
LW
236SV* sv;
237{
238 MAGIC* mg;
239 MAGIC* moremagic;
240 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 moremagic = mg->mg_moremagic;
243 if (vtbl && vtbl->svt_free)
244 (*vtbl->svt_free)(sv, mg);
93a17b20 245 if (mg->mg_ptr && mg->mg_type != 'g')
88e89b8a
PP
246 if (mg->mg_len >= 0)
247 Safefree(mg->mg_ptr);
248 else if (mg->mg_len == HEf_SVKEY)
249 SvREFCNT_dec((SV*)mg->mg_ptr);
85e6fe83 250 if (mg->mg_flags & MGf_REFCOUNTED)
8990e307 251 SvREFCNT_dec(mg->mg_obj);
79072805
LW
252 Safefree(mg);
253 }
254 SvMAGIC(sv) = 0;
255 return 0;
256}
257
258#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
259#include <signal.h>
260#endif
261
93a17b20
LW
262U32
263magic_len(sv, mg)
264SV *sv;
265MAGIC *mg;
266{
267 register I32 paren;
268 register char *s;
269 register I32 i;
bbce6d69 270 register REGEXP *rx;
748a9306 271 char *t;
93a17b20
LW
272
273 switch (*mg->mg_ptr) {
274 case '1': case '2': case '3': case '4':
275 case '5': case '6': case '7': case '8': case '9': case '&':
bbce6d69 276 if (curpm && (rx = curpm->op_pmregexp)) {
93a17b20
LW
277 paren = atoi(mg->mg_ptr);
278 getparen:
bbce6d69
PP
279 if (paren <= rx->nparens &&
280 (s = rx->startp[paren]) &&
281 (t = rx->endp[paren]))
282 {
748a9306 283 i = t - s;
71be2cbc 284 if (i >= 0)
93a17b20 285 return i;
93a17b20 286 }
93a17b20 287 }
748a9306 288 return 0;
93a17b20 289 case '+':
bbce6d69
PP
290 if (curpm && (rx = curpm->op_pmregexp)) {
291 paren = rx->lastparen;
13f57bf8
CS
292 if (paren)
293 goto getparen;
93a17b20 294 }
748a9306 295 return 0;
93a17b20 296 case '`':
bbce6d69 297 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 298 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 299 i = rx->startp[0] - s;
71be2cbc 300 if (i >= 0)
93a17b20 301 return i;
93a17b20 302 }
93a17b20 303 }
748a9306 304 return 0;
93a17b20 305 case '\'':
bbce6d69 306 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
307 if (rx->subend && (s = rx->endp[0])) {
308 i = rx->subend - s;
309 if (i >= 0)
5cd24f17 310 return i;
93a17b20 311 }
93a17b20 312 }
748a9306 313 return 0;
93a17b20
LW
314 case ',':
315 return (STRLEN)ofslen;
316 case '\\':
317 return (STRLEN)orslen;
318 }
319 magic_get(sv,mg);
320 if (!SvPOK(sv) && SvNIOK(sv))
463ee0b2 321 sv_2pv(sv, &na);
93a17b20
LW
322 if (SvPOK(sv))
323 return SvCUR(sv);
324 return 0;
325}
326
79072805
LW
327int
328magic_get(sv, mg)
329SV *sv;
330MAGIC *mg;
331{
332 register I32 paren;
333 register char *s;
334 register I32 i;
bbce6d69 335 register REGEXP *rx;
748a9306 336 char *t;
79072805
LW
337
338 switch (*mg->mg_ptr) {
748a9306
LW
339 case '\001': /* ^A */
340 sv_setsv(sv, bodytarget);
341 break;
79072805 342 case '\004': /* ^D */
188ea221 343 sv_setiv(sv, (IV)(debug & 32767));
79072805 344 break;
28f23441
PP
345 case '\005': /* ^E */
346#ifdef VMS
347 {
348# include <descrip.h>
349# include <starlet.h>
350 char msg[255];
351 $DESCRIPTOR(msgdsc,msg);
946ec16e 352 sv_setnv(sv,(double) vaxc$errno);
28f23441
PP
353 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
354 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
355 else
356 sv_setpv(sv,"");
357 }
358#else
88e89b8a 359#ifdef OS2
946ec16e 360 sv_setnv(sv, (double)Perl_rc);
88e89b8a
PP
361 sv_setpv(sv, os2error(Perl_rc));
362#else
946ec16e 363 sv_setnv(sv, (double)errno);
28f23441
PP
364 sv_setpv(sv, errno ? Strerror(errno) : "");
365#endif
88e89b8a 366#endif
946ec16e 367 SvNOK_on(sv); /* what a wonderful hack! */
28f23441 368 break;
79072805 369 case '\006': /* ^F */
188ea221 370 sv_setiv(sv, (IV)maxsysfd);
79072805 371 break;
a0d0e21e 372 case '\010': /* ^H */
188ea221 373 sv_setiv(sv, (IV)hints);
a0d0e21e 374 break;
79072805
LW
375 case '\t': /* ^I */
376 if (inplace)
377 sv_setpv(sv, inplace);
378 else
188ea221 379 sv_setsv(sv, &sv_undef);
79072805 380 break;
28f23441 381 case '\017': /* ^O */
188ea221 382 sv_setpv(sv, osname);
28f23441 383 break;
79072805 384 case '\020': /* ^P */
188ea221 385 sv_setiv(sv, (IV)perldb);
79072805
LW
386 break;
387 case '\024': /* ^T */
88e89b8a 388#ifdef BIG_TIME
188ea221 389 sv_setnv(sv, basetime);
88e89b8a 390#else
188ea221 391 sv_setiv(sv, (IV)basetime);
88e89b8a 392#endif
79072805
LW
393 break;
394 case '\027': /* ^W */
188ea221 395 sv_setiv(sv, (IV)dowarn);
79072805
LW
396 break;
397 case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9': case '&':
bbce6d69 399 if (curpm && (rx = curpm->op_pmregexp)) {
88e89b8a 400 paren = atoi(GvENAME((GV*)mg->mg_obj));
79072805 401 getparen:
bbce6d69
PP
402 if (paren <= rx->nparens &&
403 (s = rx->startp[paren]) &&
404 (t = rx->endp[paren]))
405 {
748a9306 406 i = t - s;
13f57bf8 407 getrx:
748a9306 408 if (i >= 0) {
13f57bf8
CS
409 bool was_tainted;
410 if (tainting) {
411 was_tainted = tainted;
412 tainted = FALSE;
413 }
79072805 414 sv_setpvn(sv,s,i);
13f57bf8
CS
415 if (tainting)
416 tainted = was_tainted || rx->exec_tainted;
748a9306
LW
417 break;
418 }
79072805 419 }
79072805 420 }
748a9306 421 sv_setsv(sv,&sv_undef);
79072805
LW
422 break;
423 case '+':
bbce6d69
PP
424 if (curpm && (rx = curpm->op_pmregexp)) {
425 paren = rx->lastparen;
a0d0e21e
LW
426 if (paren)
427 goto getparen;
79072805 428 }
748a9306 429 sv_setsv(sv,&sv_undef);
79072805
LW
430 break;
431 case '`':
bbce6d69 432 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8 433 if ((s = rx->subbeg) && rx->startp[0]) {
bbce6d69 434 i = rx->startp[0] - s;
13f57bf8 435 goto getrx;
79072805 436 }
79072805 437 }
748a9306 438 sv_setsv(sv,&sv_undef);
79072805
LW
439 break;
440 case '\'':
bbce6d69 441 if (curpm && (rx = curpm->op_pmregexp)) {
13f57bf8
CS
442 if (rx->subend && (s = rx->endp[0])) {
443 i = rx->subend - s;
444 goto getrx;
79072805 445 }
79072805 446 }
748a9306 447 sv_setsv(sv,&sv_undef);
79072805
LW
448 break;
449 case '.':
450#ifndef lint
a0d0e21e 451 if (GvIO(last_in_gv)) {
188ea221 452 sv_setiv(sv, (IV)IoLINES(GvIO(last_in_gv)));
79072805
LW
453 }
454#endif
455 break;
456 case '?':
ff0cee69
PP
457 sv_setiv(sv, (IV)STATUS_CURRENT);
458#ifdef COMPLEX_STATUS
459 LvTARGOFF(sv) = statusvalue;
460 LvTARGLEN(sv) = statusvalue_vms;
461#endif
79072805
LW
462 break;
463 case '^':
a0d0e21e 464 s = IoTOP_NAME(GvIOp(defoutgv));
79072805
LW
465 if (s)
466 sv_setpv(sv,s);
467 else {
468 sv_setpv(sv,GvENAME(defoutgv));
469 sv_catpv(sv,"_TOP");
470 }
471 break;
472 case '~':
a0d0e21e 473 s = IoFMT_NAME(GvIOp(defoutgv));
79072805
LW
474 if (!s)
475 s = GvENAME(defoutgv);
476 sv_setpv(sv,s);
477 break;
478#ifndef lint
479 case '=':
188ea221 480 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(defoutgv)));
79072805
LW
481 break;
482 case '-':
188ea221 483 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(defoutgv)));
79072805
LW
484 break;
485 case '%':
188ea221 486 sv_setiv(sv, (IV)IoPAGE(GvIOp(defoutgv)));
79072805
LW
487 break;
488#endif
489 case ':':
490 break;
491 case '/':
492 break;
493 case '[':
188ea221 494 sv_setiv(sv, (IV)curcop->cop_arybase);
79072805
LW
495 break;
496 case '|':
188ea221 497 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(defoutgv)) & IOf_FLUSH) != 0 );
79072805
LW
498 break;
499 case ',':
500 sv_setpvn(sv,ofs,ofslen);
501 break;
502 case '\\':
503 sv_setpvn(sv,ors,orslen);
504 break;
505 case '#':
506 sv_setpv(sv,ofmt);
507 break;
508 case '!':
a5f75d66 509#ifdef VMS
946ec16e 510 sv_setnv(sv, (double)((errno == EVMSERR) ? vaxc$errno : errno));
88e89b8a 511 sv_setpv(sv, errno ? Strerror(errno) : "");
a5f75d66 512#else
88e89b8a
PP
513 {
514 int saveerrno = errno;
946ec16e 515 sv_setnv(sv, (double)errno);
88e89b8a
PP
516#ifdef OS2
517 if (errno == errno_isOS2) sv_setpv(sv, os2error(Perl_rc));
518 else
a5f75d66 519#endif
2304df62 520 sv_setpv(sv, errno ? Strerror(errno) : "");
88e89b8a
PP
521 errno = saveerrno;
522 }
523#endif
946ec16e 524 SvNOK_on(sv); /* what a wonderful hack! */
79072805
LW
525 break;
526 case '<':
188ea221 527 sv_setiv(sv, (IV)uid);
79072805
LW
528 break;
529 case '>':
188ea221 530 sv_setiv(sv, (IV)euid);
79072805
LW
531 break;
532 case '(':
188ea221 533 sv_setiv(sv, (IV)gid);
fc36a67e 534 sv_setpvf(sv, "%Vd", (IV)gid);
79072805
LW
535 goto add_groups;
536 case ')':
188ea221 537 sv_setiv(sv, (IV)egid);
fc36a67e 538 sv_setpvf(sv, "%Vd", (IV)egid);
79072805 539 add_groups:
79072805 540#ifdef HAS_GETGROUPS
79072805 541 {
a0d0e21e 542 Groups_t gary[NGROUPS];
79072805 543 i = getgroups(NGROUPS,gary);
46fc3d4c 544 while (--i >= 0)
fc36a67e 545 sv_catpvf(sv, " %Vd", (IV)gary[i]);
79072805
LW
546 }
547#endif
29355cf7 548 SvIOK_on(sv); /* what a wonderful hack! */
79072805
LW
549 break;
550 case '*':
551 break;
552 case '0':
553 break;
554 }
a0d0e21e 555 return 0;
79072805
LW
556}
557
558int
559magic_getuvar(sv, mg)
560SV *sv;
561MAGIC *mg;
562{
563 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
564
565 if (uf && uf->uf_val)
566 (*uf->uf_val)(uf->uf_index, sv);
567 return 0;
568}
569
570int
571magic_setenv(sv,mg)
572SV* sv;
573MAGIC* mg;
574{
575 register char *s;
88e89b8a 576 char *ptr;
5aabfad6 577 STRLEN len, klen;
a0d0e21e 578 I32 i;
1e422769 579
a0d0e21e 580 s = SvPV(sv,len);
5aabfad6 581 ptr = MgPV(mg,klen);
88e89b8a 582 my_setenv(ptr, s);
1e422769 583
a0d0e21e
LW
584#ifdef DYNAMIC_ENV_FETCH
585 /* We just undefd an environment var. Is a replacement */
586 /* waiting in the wings? */
587 if (!len) {
5aabfad6
PP
588 SV **valp;
589 if ((valp = hv_fetch(GvHVn(envgv), ptr, klen, FALSE)))
590 s = SvPV(*valp, len);
a0d0e21e
LW
591 }
592#endif
1e422769 593
68dc0745 594#if !defined(OS2) && !defined(AMIGAOS) && !defined(_WIN32)
79072805
LW
595 /* And you'll never guess what the dog had */
596 /* in its mouth... */
463ee0b2 597 if (tainting) {
1e422769
PP
598 MgTAINTEDDIR_off(mg);
599#ifdef VMS
5aabfad6 600 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1e422769
PP
601 char pathbuf[256], eltbuf[256], *cp, *elt = s;
602 struct stat sbuf;
603 int i = 0, j = 0;
604
605 do { /* DCL$PATH may be a search list */
606 while (1) { /* as may dev portion of any element */
607 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
608 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
609 cando_by_name(S_IWUSR,0,elt) ) {
610 MgTAINTEDDIR_on(mg);
611 return 0;
612 }
613 }
614 if ((cp = strchr(elt, ':')) != Nullch)
615 *cp = '\0';
616 if (my_trnlnm(elt, eltbuf, j++))
617 elt = eltbuf;
618 else
619 break;
620 }
621 j = 0;
622 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
623 }
624#endif /* VMS */
5aabfad6 625 if (s && klen == 4 && strEQ(ptr,"PATH")) {
a0d0e21e 626 char *strend = s + len;
463ee0b2
LW
627
628 while (s < strend) {
1e422769 629 struct stat st;
fc36a67e
PP
630 s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf,
631 s, strend, ':', &i);
463ee0b2 632 s++;
fc36a67e
PP
633 if (i >= sizeof tokenbuf /* too long -- assume the worst */
634 || *tokenbuf != '/'
1e422769 635 || (Stat(tokenbuf, &st) == 0 && (st.st_mode & 2)) ) {
8990e307 636 MgTAINTEDDIR_on(mg);
1e422769
PP
637 return 0;
638 }
463ee0b2 639 }
79072805
LW
640 }
641 }
68dc0745 642#endif /* neither OS2 nor AMIGAOS nor _WIN32 */
1e422769 643
79072805
LW
644 return 0;
645}
646
647int
85e6fe83
LW
648magic_clearenv(sv,mg)
649SV* sv;
650MAGIC* mg;
651{
5aabfad6 652 my_setenv(MgPV(mg,na),Nullch);
85e6fe83
LW
653 return 0;
654}
655
88e89b8a
PP
656int
657magic_getsig(sv,mg)
658SV* sv;
659MAGIC* mg;
660{
661 I32 i;
662 /* Are we fetching a signal entry? */
5aabfad6 663 i = whichsig(MgPV(mg,na));
88e89b8a
PP
664 if (i) {
665 if(psig_ptr[i])
666 sv_setsv(sv,psig_ptr[i]);
667 else {
ff68c719
PP
668 Sighandler_t sigstate = rsignal_state(i);
669
88e89b8a 670 /* cache state so we don't fetch it again */
ff68c719 671 if(sigstate == SIG_IGN)
88e89b8a
PP
672 sv_setpv(sv,"IGNORE");
673 else
674 sv_setsv(sv,&sv_undef);
675 psig_ptr[i] = SvREFCNT_inc(sv);
676 SvTEMP_off(sv);
677 }
678 }
679 return 0;
680}
681int
682magic_clearsig(sv,mg)
683SV* sv;
684MAGIC* mg;
685{
686 I32 i;
687 /* Are we clearing a signal entry? */
5aabfad6 688 i = whichsig(MgPV(mg,na));
88e89b8a
PP
689 if (i) {
690 if(psig_ptr[i]) {
691 SvREFCNT_dec(psig_ptr[i]);
692 psig_ptr[i]=0;
693 }
694 if(psig_name[i]) {
695 SvREFCNT_dec(psig_name[i]);
696 psig_name[i]=0;
697 }
698 }
699 return 0;
700}
3d37d572 701
85e6fe83 702int
79072805
LW
703magic_setsig(sv,mg)
704SV* sv;
705MAGIC* mg;
706{
707 register char *s;
708 I32 i;
748a9306 709 SV** svp;
a0d0e21e 710
5aabfad6 711 s = MgPV(mg,na);
748a9306
LW
712 if (*s == '_') {
713 if (strEQ(s,"__DIE__"))
714 svp = &diehook;
715 else if (strEQ(s,"__WARN__"))
716 svp = &warnhook;
717 else if (strEQ(s,"__PARSE__"))
718 svp = &parsehook;
719 else
720 croak("No such hook: %s", s);
721 i = 0;
4633a7c4
LW
722 if (*svp) {
723 SvREFCNT_dec(*svp);
724 *svp = 0;
725 }
748a9306
LW
726 }
727 else {
728 i = whichsig(s); /* ...no, a brick */
729 if (!i) {
730 if (dowarn || strEQ(s,"ALARM"))
731 warn("No such signal: SIG%s", s);
732 return 0;
733 }
ff0cee69
PP
734 SvREFCNT_dec(psig_name[i]);
735 SvREFCNT_dec(psig_ptr[i]);
88e89b8a 736 psig_ptr[i] = SvREFCNT_inc(sv);
88e89b8a 737 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
ff0cee69 738 psig_name[i] = newSVpv(s, strlen(s));
88e89b8a 739 SvREADONLY_on(psig_name[i]);
748a9306 740 }
a0d0e21e 741 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
748a9306 742 if (i)
ff68c719 743 (void)rsignal(i, sighandler);
748a9306
LW
744 else
745 *svp = SvREFCNT_inc(sv);
a0d0e21e
LW
746 return 0;
747 }
748 s = SvPV_force(sv,na);
748a9306
LW
749 if (strEQ(s,"IGNORE")) {
750 if (i)
ff68c719 751 (void)rsignal(i, SIG_IGN);
748a9306
LW
752 else
753 *svp = 0;
754 }
755 else if (strEQ(s,"DEFAULT") || !*s) {
756 if (i)
ff68c719 757 (void)rsignal(i, SIG_DFL);
748a9306
LW
758 else
759 *svp = 0;
760 }
79072805 761 else {
5aabfad6
PP
762 /*
763 * We should warn if HINT_STRICT_REFS, but without
764 * access to a known hint bit in a known OP, we can't
765 * tell whether HINT_STRICT_REFS is in force or not.
766 */
46fc3d4c
PP
767 if (!strchr(s,':') && !strchr(s,'\''))
768 sv_setpv(sv, form("main::%s", s));
748a9306 769 if (i)
ff68c719 770 (void)rsignal(i, sighandler);
748a9306
LW
771 else
772 *svp = SvREFCNT_inc(sv);
79072805
LW
773 }
774 return 0;
775}
776
777int
463ee0b2 778magic_setisa(sv,mg)
79072805
LW
779SV* sv;
780MAGIC* mg;
781{
463ee0b2
LW
782 sub_generation++;
783 return 0;
784}
785
a0d0e21e
LW
786#ifdef OVERLOAD
787
463ee0b2 788int
a0d0e21e 789magic_setamagic(sv,mg)
463ee0b2
LW
790SV* sv;
791MAGIC* mg;
792{
a0d0e21e
LW
793 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
794 amagic_generation++;
463ee0b2 795
a0d0e21e
LW
796 return 0;
797}
798#endif /* OVERLOAD */
463ee0b2 799
946ec16e
PP
800int
801magic_setnkeys(sv,mg)
802SV* sv;
803MAGIC* mg;
804{
805 if (LvTARG(sv)) {
806 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
807 LvTARG(sv) = Nullsv; /* Don't allow a ref to reassign this. */
808 }
809 return 0;
810}
811
a0d0e21e
LW
812static int
813magic_methpack(sv,mg,meth)
814SV* sv;
815MAGIC* mg;
816char *meth;
817{
818 dSP;
463ee0b2 819
a0d0e21e
LW
820 ENTER;
821 SAVETMPS;
822 PUSHMARK(sp);
823 EXTEND(sp, 2);
824 PUSHs(mg->mg_obj);
88e89b8a
PP
825 if (mg->mg_ptr) {
826 if (mg->mg_len >= 0)
827 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
828 else if (mg->mg_len == HEf_SVKEY)
829 PUSHs((SV*)mg->mg_ptr);
830 }
a0d0e21e
LW
831 else if (mg->mg_type == 'p')
832 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
833 PUTBACK;
834
a0d0e21e
LW
835 if (perl_call_method(meth, G_SCALAR))
836 sv_setsv(sv, *stack_sp--);
463ee0b2 837
a0d0e21e
LW
838 FREETMPS;
839 LEAVE;
840 return 0;
841}
463ee0b2 842
a0d0e21e
LW
843int
844magic_getpack(sv,mg)
845SV* sv;
846MAGIC* mg;
847{
848 magic_methpack(sv,mg,"FETCH");
849 if (mg->mg_ptr)
850 mg->mg_flags |= MGf_GSKIP;
463ee0b2
LW
851 return 0;
852}
853
854int
855magic_setpack(sv,mg)
856SV* sv;
857MAGIC* mg;
858{
463ee0b2 859 dSP;
463ee0b2 860
a0d0e21e
LW
861 PUSHMARK(sp);
862 EXTEND(sp, 3);
863 PUSHs(mg->mg_obj);
88e89b8a
PP
864 if (mg->mg_ptr) {
865 if (mg->mg_len >= 0)
866 PUSHs(sv_2mortal(newSVpv(mg->mg_ptr, mg->mg_len)));
867 else if (mg->mg_len == HEf_SVKEY)
868 PUSHs((SV*)mg->mg_ptr);
869 }
a0d0e21e
LW
870 else if (mg->mg_type == 'p')
871 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
463ee0b2
LW
872 PUSHs(sv);
873 PUTBACK;
874
a0d0e21e 875 perl_call_method("STORE", G_SCALAR|G_DISCARD);
463ee0b2
LW
876
877 return 0;
878}
879
880int
881magic_clearpack(sv,mg)
882SV* sv;
883MAGIC* mg;
884{
a0d0e21e
LW
885 return magic_methpack(sv,mg,"DELETE");
886}
463ee0b2 887
a0d0e21e
LW
888int magic_wipepack(sv,mg)
889SV* sv;
890MAGIC* mg;
891{
892 dSP;
463ee0b2 893
a0d0e21e
LW
894 PUSHMARK(sp);
895 XPUSHs(mg->mg_obj);
463ee0b2 896 PUTBACK;
463ee0b2 897
a0d0e21e 898 perl_call_method("CLEAR", G_SCALAR|G_DISCARD);
463ee0b2
LW
899
900 return 0;
901}
902
903int
904magic_nextpack(sv,mg,key)
905SV* sv;
906MAGIC* mg;
907SV* key;
908{
463ee0b2 909 dSP;
a0d0e21e 910 char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
463ee0b2
LW
911
912 ENTER;
a0d0e21e
LW
913 SAVETMPS;
914 PUSHMARK(sp);
915 EXTEND(sp, 2);
916 PUSHs(mg->mg_obj);
463ee0b2
LW
917 if (SvOK(key))
918 PUSHs(key);
919 PUTBACK;
920
a0d0e21e
LW
921 if (perl_call_method(meth, G_SCALAR))
922 sv_setsv(key, *stack_sp--);
463ee0b2 923
a0d0e21e
LW
924 FREETMPS;
925 LEAVE;
79072805
LW
926 return 0;
927}
928
929int
a0d0e21e
LW
930magic_existspack(sv,mg)
931SV* sv;
932MAGIC* mg;
933{
934 return magic_methpack(sv,mg,"EXISTS");
935}
936
937int
79072805
LW
938magic_setdbline(sv,mg)
939SV* sv;
940MAGIC* mg;
941{
942 OP *o;
943 I32 i;
944 GV* gv;
945 SV** svp;
946
947 gv = DBline;
948 i = SvTRUE(sv);
188ea221 949 svp = av_fetch(GvAV(gv),
5aabfad6 950 atoi(MgPV(mg,na)), FALSE);
8990e307 951 if (svp && SvIOKp(*svp) && (o = (OP*)SvSTASH(*svp)))
93a17b20 952 o->op_private = i;
79072805
LW
953 else
954 warn("Can't break at that line\n");
955 return 0;
956}
957
958int
959magic_getarylen(sv,mg)
960SV* sv;
961MAGIC* mg;
962{
a0d0e21e 963 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + curcop->cop_arybase);
79072805
LW
964 return 0;
965}
966
967int
968magic_setarylen(sv,mg)
969SV* sv;
970MAGIC* mg;
971{
a0d0e21e
LW
972 av_fill((AV*)mg->mg_obj, SvIV(sv) - curcop->cop_arybase);
973 return 0;
974}
975
976int
977magic_getpos(sv,mg)
978SV* sv;
979MAGIC* mg;
980{
981 SV* lsv = LvTARG(sv);
982
983 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
984 mg = mg_find(lsv, 'g');
985 if (mg && mg->mg_len >= 0) {
986 sv_setiv(sv, mg->mg_len + curcop->cop_arybase);
987 return 0;
988 }
989 }
990 (void)SvOK_off(sv);
991 return 0;
992}
993
994int
995magic_setpos(sv,mg)
996SV* sv;
997MAGIC* mg;
998{
999 SV* lsv = LvTARG(sv);
1000 SSize_t pos;
1001 STRLEN len;
1002
1003 mg = 0;
1004
1005 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1006 mg = mg_find(lsv, 'g');
1007 if (!mg) {
1008 if (!SvOK(sv))
1009 return 0;
1010 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1011 mg = mg_find(lsv, 'g');
1012 }
1013 else if (!SvOK(sv)) {
1014 mg->mg_len = -1;
1015 return 0;
1016 }
1017 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1018
1019 pos = SvIV(sv) - curcop->cop_arybase;
1020 if (pos < 0) {
1021 pos += len;
1022 if (pos < 0)
1023 pos = 0;
1024 }
1025 else if (pos > len)
1026 pos = len;
1027 mg->mg_len = pos;
71be2cbc 1028 mg->mg_flags &= ~MGf_MINMATCH;
a0d0e21e 1029
79072805
LW
1030 return 0;
1031}
1032
1033int
1034magic_getglob(sv,mg)
1035SV* sv;
1036MAGIC* mg;
1037{
8646b087
PP
1038 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1039 SvFAKE_off(sv);
946ec16e 1040 gv_efullname3(sv,((GV*)sv), "*");
8646b087
PP
1041 SvFAKE_on(sv);
1042 }
1043 else
946ec16e 1044 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
79072805
LW
1045 return 0;
1046}
1047
1048int
1049magic_setglob(sv,mg)
1050SV* sv;
1051MAGIC* mg;
1052{
1053 register char *s;
1054 GV* gv;
1055
1056 if (!SvOK(sv))
1057 return 0;
463ee0b2 1058 s = SvPV(sv, na);
79072805
LW
1059 if (*s == '*' && s[1])
1060 s++;
85e6fe83 1061 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
79072805
LW
1062 if (sv == (SV*)gv)
1063 return 0;
1064 if (GvGP(sv))
88e89b8a 1065 gp_free((GV*)sv);
79072805 1066 GvGP(sv) = gp_ref(GvGP(gv));
79072805
LW
1067 return 0;
1068}
1069
1070int
1071magic_setsubstr(sv,mg)
1072SV* sv;
1073MAGIC* mg;
1074{
8990e307
LW
1075 STRLEN len;
1076 char *tmps = SvPV(sv,len);
1077 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
79072805
LW
1078 return 0;
1079}
1080
1081int
463ee0b2
LW
1082magic_gettaint(sv,mg)
1083SV* sv;
1084MAGIC* mg;
1085{
bbce6d69
PP
1086 TAINT_IF((mg->mg_len & 1) ||
1087 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
463ee0b2
LW
1088 return 0;
1089}
1090
1091int
1092magic_settaint(sv,mg)
1093SV* sv;
1094MAGIC* mg;
1095{
748a9306
LW
1096 if (localizing) {
1097 if (localizing == 1)
1098 mg->mg_len <<= 1;
1099 else
1100 mg->mg_len >>= 1;
a0d0e21e 1101 }
748a9306
LW
1102 else if (tainted)
1103 mg->mg_len |= 1;
1104 else
1105 mg->mg_len &= ~1;
463ee0b2
LW
1106 return 0;
1107}
1108
1109int
79072805
LW
1110magic_setvec(sv,mg)
1111SV* sv;
1112MAGIC* mg;
1113{
1114 do_vecset(sv); /* XXX slurp this routine */
1115 return 0;
1116}
1117
1118int
68dc0745 1119magic_getdefelem(sv,mg)
5f05dabc
PP
1120SV* sv;
1121MAGIC* mg;
1122{
71be2cbc 1123 SV *targ = Nullsv;
5f05dabc 1124 if (LvTARGLEN(sv)) {
68dc0745
PP
1125 if (mg->mg_obj) {
1126 HV* hv = (HV*)LvTARG(sv);
1127 HE* he = hv_fetch_ent(hv, mg->mg_obj, FALSE, 0);
1128 if (he)
1129 targ = HeVAL(he);
1130 }
1131 else {
1132 AV* av = (AV*)LvTARG(sv);
1133 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1134 targ = AvARRAY(av)[LvTARGOFF(sv)];
1135 }
1136 if (targ && targ != &sv_undef) {
1137 /* somebody else defined it for us */
1138 SvREFCNT_dec(LvTARG(sv));
1139 LvTARG(sv) = SvREFCNT_inc(targ);
1140 LvTARGLEN(sv) = 0;
1141 SvREFCNT_dec(mg->mg_obj);
1142 mg->mg_obj = Nullsv;
1143 mg->mg_flags &= ~MGf_REFCOUNTED;
1144 }
5f05dabc 1145 }
71be2cbc
PP
1146 else
1147 targ = LvTARG(sv);
1148 sv_setsv(sv, targ ? targ : &sv_undef);
1149 return 0;
1150}
1151
1152int
68dc0745 1153magic_setdefelem(sv,mg)
71be2cbc
PP
1154SV* sv;
1155MAGIC* mg;
1156{
1157 if (LvTARGLEN(sv))
68dc0745
PP
1158 vivify_defelem(sv);
1159 if (LvTARG(sv)) {
5f05dabc 1160 sv_setsv(LvTARG(sv), sv);
68dc0745
PP
1161 SvSETMAGIC(LvTARG(sv));
1162 }
5f05dabc
PP
1163 return 0;
1164}
1165
1166int
68dc0745 1167magic_freedefelem(sv,mg)
5f05dabc
PP
1168SV* sv;
1169MAGIC* mg;
1170{
1171 SvREFCNT_dec(LvTARG(sv));
71be2cbc
PP
1172 return 0;
1173}
1174
1175void
68dc0745 1176vivify_defelem(sv)
71be2cbc
PP
1177SV* sv;
1178{
68dc0745
PP
1179 MAGIC* mg;
1180 SV* value;
71be2cbc 1181
68dc0745 1182 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
71be2cbc 1183 return;
68dc0745
PP
1184 if (mg->mg_obj) {
1185 HV* hv = (HV*)LvTARG(sv);
1186 HE* he = hv_fetch_ent(hv, mg->mg_obj, TRUE, 0);
1187 if (!he || (value = HeVAL(he)) == &sv_undef)
1188 croak(no_helem, SvPV(mg->mg_obj, na));
71be2cbc 1189 }
68dc0745
PP
1190 else {
1191 AV* av = (AV*)LvTARG(sv);
5aabfad6 1192 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
68dc0745
PP
1193 LvTARG(sv) = Nullsv; /* array can't be extended */
1194 else {
1195 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1196 if (!svp || (value = *svp) == &sv_undef)
1197 croak(no_aelem, (I32)LvTARGOFF(sv));
1198 }
1199 }
1200 SvREFCNT_inc(value);
1201 SvREFCNT_dec(LvTARG(sv));
1202 LvTARG(sv) = value;
71be2cbc 1203 LvTARGLEN(sv) = 0;
68dc0745
PP
1204 SvREFCNT_dec(mg->mg_obj);
1205 mg->mg_obj = Nullsv;
1206 mg->mg_flags &= ~MGf_REFCOUNTED;
5f05dabc
PP
1207}
1208
1209int
93a17b20
LW
1210magic_setmglob(sv,mg)
1211SV* sv;
1212MAGIC* mg;
1213{
a0d0e21e 1214 mg->mg_len = -1;
c6496cc7 1215 SvSCREAM_off(sv);
93a17b20
LW
1216 return 0;
1217}
1218
1219int
79072805
LW
1220magic_setbm(sv,mg)
1221SV* sv;
1222MAGIC* mg;
1223{
463ee0b2 1224 sv_unmagic(sv, 'B');
79072805
LW
1225 SvVALID_off(sv);
1226 return 0;
1227}
1228
1229int
55497cff
PP
1230magic_setfm(sv,mg)
1231SV* sv;
1232MAGIC* mg;
1233{
1234 sv_unmagic(sv, 'f');
1235 SvCOMPILED_off(sv);
1236 return 0;
1237}
1238
1239int
79072805
LW
1240magic_setuvar(sv,mg)
1241SV* sv;
1242MAGIC* mg;
1243{
1244 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1245
1246 if (uf && uf->uf_set)
1247 (*uf->uf_set)(uf->uf_index, sv);
1248 return 0;
1249}
1250
7a4c00b4 1251#ifdef USE_LOCALE_COLLATE
79072805 1252int
bbce6d69
PP
1253magic_setcollxfrm(sv,mg)
1254SV* sv;
1255MAGIC* mg;
1256{
1257 /*
1258 * René Descartes said "I think not."
1259 * and vanished with a faint plop.
1260 */
7a4c00b4
PP
1261 if (mg->mg_ptr) {
1262 Safefree(mg->mg_ptr);
1263 mg->mg_ptr = NULL;
1264 mg->mg_len = -1;
1265 }
bbce6d69
PP
1266 return 0;
1267}
7a4c00b4 1268#endif /* USE_LOCALE_COLLATE */
bbce6d69
PP
1269
1270int
79072805
LW
1271magic_set(sv,mg)
1272SV* sv;
1273MAGIC* mg;
1274{
1275 register char *s;
1276 I32 i;
8990e307 1277 STRLEN len;
79072805 1278 switch (*mg->mg_ptr) {
748a9306
LW
1279 case '\001': /* ^A */
1280 sv_setsv(bodytarget, sv);
1281 break;
79072805 1282 case '\004': /* ^D */
8990e307 1283 debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
79072805
LW
1284 DEBUG_x(dump_all());
1285 break;
28f23441
PP
1286 case '\005': /* ^E */
1287#ifdef VMS
1288 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1289#else
f86702cc
PP
1290 /* will anyone ever use this? */
1291 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
28f23441
PP
1292#endif
1293 break;
79072805 1294 case '\006': /* ^F */
463ee0b2 1295 maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805 1296 break;
a0d0e21e
LW
1297 case '\010': /* ^H */
1298 hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1299 break;
79072805
LW
1300 case '\t': /* ^I */
1301 if (inplace)
1302 Safefree(inplace);
1303 if (SvOK(sv))
a0d0e21e 1304 inplace = savepv(SvPV(sv,na));
79072805
LW
1305 else
1306 inplace = Nullch;
1307 break;
28f23441
PP
1308 case '\017': /* ^O */
1309 if (osname)
1310 Safefree(osname);
1311 if (SvOK(sv))
1312 osname = savepv(SvPV(sv,na));
1313 else
1314 osname = Nullch;
1315 break;
79072805 1316 case '\020': /* ^P */
463ee0b2 1317 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1318 if (i != perldb) {
1319 if (perldb)
1320 oldlastpm = curpm;
1321 else
1322 curpm = oldlastpm;
1323 }
1324 perldb = i;
1325 break;
1326 case '\024': /* ^T */
88e89b8a
PP
1327#ifdef BIG_TIME
1328 basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1329#else
85e6fe83 1330 basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
88e89b8a 1331#endif
79072805
LW
1332 break;
1333 case '\027': /* ^W */
463ee0b2 1334 dowarn = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1335 break;
1336 case '.':
748a9306
LW
1337 if (localizing) {
1338 if (localizing == 1)
1339 save_sptr((SV**)&last_in_gv);
1340 }
88e89b8a 1341 else if (SvOK(sv) && GvIO(last_in_gv))
a0d0e21e 1342 IoLINES(GvIOp(last_in_gv)) = (long)SvIV(sv);
79072805
LW
1343 break;
1344 case '^':
a0d0e21e
LW
1345 Safefree(IoTOP_NAME(GvIOp(defoutgv)));
1346 IoTOP_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1347 IoTOP_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1348 break;
1349 case '~':
a0d0e21e
LW
1350 Safefree(IoFMT_NAME(GvIOp(defoutgv)));
1351 IoFMT_NAME(GvIOp(defoutgv)) = s = savepv(SvPV(sv,na));
1352 IoFMT_GV(GvIOp(defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
79072805
LW
1353 break;
1354 case '=':
a0d0e21e 1355 IoPAGE_LEN(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1356 break;
1357 case '-':
a0d0e21e
LW
1358 IoLINES_LEFT(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1359 if (IoLINES_LEFT(GvIOp(defoutgv)) < 0L)
1360 IoLINES_LEFT(GvIOp(defoutgv)) = 0L;
79072805
LW
1361 break;
1362 case '%':
a0d0e21e 1363 IoPAGE(GvIOp(defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1364 break;
1365 case '|':
4b65379b
CS
1366 {
1367 IO *io = GvIOp(defoutgv);
1368 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1369 IoFLAGS(io) &= ~IOf_FLUSH;
1370 else {
1371 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1372 PerlIO *ofp = IoOFP(io);
1373 if (ofp)
1374 (void)PerlIO_flush(ofp);
1375 IoFLAGS(io) |= IOf_FLUSH;
1376 }
1377 }
79072805
LW
1378 }
1379 break;
1380 case '*':
463ee0b2 1381 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1382 multiline = (i != 0);
1383 break;
1384 case '/':
c07a80fd
PP
1385 SvREFCNT_dec(nrs);
1386 nrs = newSVsv(sv);
1387 SvREFCNT_dec(rs);
1388 rs = SvREFCNT_inc(nrs);
79072805
LW
1389 break;
1390 case '\\':
1391 if (ors)
1392 Safefree(ors);
e3c19b7b
CS
1393 if (SvOK(sv) || SvGMAGICAL(sv))
1394 ors = savepv(SvPV(sv,orslen));
1395 else {
1396 ors = Nullch;
1397 orslen = 0;
1398 }
79072805
LW
1399 break;
1400 case ',':
1401 if (ofs)
1402 Safefree(ofs);
a0d0e21e 1403 ofs = savepv(SvPV(sv, ofslen));
79072805
LW
1404 break;
1405 case '#':
1406 if (ofmt)
1407 Safefree(ofmt);
a0d0e21e 1408 ofmt = savepv(SvPV(sv,na));
79072805
LW
1409 break;
1410 case '[':
a0d0e21e 1411 compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1412 break;
1413 case '?':
ff0cee69
PP
1414#ifdef COMPLEX_STATUS
1415 if (localizing == 2) {
1416 statusvalue = LvTARGOFF(sv);
1417 statusvalue_vms = LvTARGLEN(sv);
1418 }
1419 else
1420#endif
1421#ifdef VMSISH_STATUS
1422 if (VMSISH_STATUS)
1423 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1424 else
1425#endif
1426 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
79072805
LW
1427 break;
1428 case '!':
f86702cc
PP
1429 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv),
1430 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
79072805
LW
1431 break;
1432 case '<':
463ee0b2 1433 uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1434 if (delaymagic) {
1435 delaymagic |= DM_RUID;
1436 break; /* don't do magic till later */
1437 }
1438#ifdef HAS_SETRUID
85e6fe83 1439 (void)setruid((Uid_t)uid);
79072805
LW
1440#else
1441#ifdef HAS_SETREUID
85e6fe83 1442 (void)setreuid((Uid_t)uid, (Uid_t)-1);
748a9306 1443#else
85e6fe83
LW
1444#ifdef HAS_SETRESUID
1445 (void)setresuid((Uid_t)uid, (Uid_t)-1, (Uid_t)-1);
79072805
LW
1446#else
1447 if (uid == euid) /* special case $< = $> */
1448 (void)setuid(uid);
a0d0e21e
LW
1449 else {
1450 uid = (I32)getuid();
463ee0b2 1451 croak("setruid() not implemented");
a0d0e21e 1452 }
79072805
LW
1453#endif
1454#endif
85e6fe83 1455#endif
748a9306 1456 uid = (I32)getuid();
4633a7c4 1457 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1458 break;
1459 case '>':
463ee0b2 1460 euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1461 if (delaymagic) {
1462 delaymagic |= DM_EUID;
1463 break; /* don't do magic till later */
1464 }
1465#ifdef HAS_SETEUID
85e6fe83 1466 (void)seteuid((Uid_t)euid);
79072805
LW
1467#else
1468#ifdef HAS_SETREUID
85e6fe83
LW
1469 (void)setreuid((Uid_t)-1, (Uid_t)euid);
1470#else
1471#ifdef HAS_SETRESUID
1472 (void)setresuid((Uid_t)-1, (Uid_t)euid, (Uid_t)-1);
79072805
LW
1473#else
1474 if (euid == uid) /* special case $> = $< */
1475 setuid(euid);
a0d0e21e
LW
1476 else {
1477 euid = (I32)geteuid();
463ee0b2 1478 croak("seteuid() not implemented");
a0d0e21e 1479 }
79072805
LW
1480#endif
1481#endif
85e6fe83 1482#endif
79072805 1483 euid = (I32)geteuid();
4633a7c4 1484 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1485 break;
1486 case '(':
463ee0b2 1487 gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
79072805
LW
1488 if (delaymagic) {
1489 delaymagic |= DM_RGID;
1490 break; /* don't do magic till later */
1491 }
1492#ifdef HAS_SETRGID
85e6fe83 1493 (void)setrgid((Gid_t)gid);
79072805
LW
1494#else
1495#ifdef HAS_SETREGID
85e6fe83
LW
1496 (void)setregid((Gid_t)gid, (Gid_t)-1);
1497#else
1498#ifdef HAS_SETRESGID
1499 (void)setresgid((Gid_t)gid, (Gid_t)-1, (Gid_t) 1);
79072805
LW
1500#else
1501 if (gid == egid) /* special case $( = $) */
1502 (void)setgid(gid);
748a9306
LW
1503 else {
1504 gid = (I32)getgid();
463ee0b2 1505 croak("setrgid() not implemented");
748a9306 1506 }
79072805
LW
1507#endif
1508#endif
85e6fe83 1509#endif
79072805 1510 gid = (I32)getgid();
4633a7c4 1511 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1512 break;
1513 case ')':
5cd24f17
PP
1514#ifdef HAS_SETGROUPS
1515 {
1516 char *p = SvPV(sv, na);
1517 Groups_t gary[NGROUPS];
1518
1519 SET_NUMERIC_STANDARD();
1520 while (isSPACE(*p))
1521 ++p;
1522 egid = I_V(atof(p));
1523 for (i = 0; i < NGROUPS; ++i) {
1524 while (*p && !isSPACE(*p))
1525 ++p;
1526 while (isSPACE(*p))
1527 ++p;
1528 if (!*p)
1529 break;
1530 gary[i] = I_V(atof(p));
1531 }
8cc95fdb
PP
1532 if (i)
1533 (void)setgroups(i, gary);
5cd24f17
PP
1534 }
1535#else /* HAS_SETGROUPS */
463ee0b2 1536 egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
5cd24f17 1537#endif /* HAS_SETGROUPS */
79072805
LW
1538 if (delaymagic) {
1539 delaymagic |= DM_EGID;
1540 break; /* don't do magic till later */
1541 }
1542#ifdef HAS_SETEGID
85e6fe83 1543 (void)setegid((Gid_t)egid);
79072805
LW
1544#else
1545#ifdef HAS_SETREGID
85e6fe83
LW
1546 (void)setregid((Gid_t)-1, (Gid_t)egid);
1547#else
1548#ifdef HAS_SETRESGID
1549 (void)setresgid((Gid_t)-1, (Gid_t)egid, (Gid_t)-1);
79072805
LW
1550#else
1551 if (egid == gid) /* special case $) = $( */
1552 (void)setgid(egid);
748a9306
LW
1553 else {
1554 egid = (I32)getegid();
463ee0b2 1555 croak("setegid() not implemented");
748a9306 1556 }
79072805
LW
1557#endif
1558#endif
85e6fe83 1559#endif
79072805 1560 egid = (I32)getegid();
4633a7c4 1561 tainting |= (uid && (euid != uid || egid != gid));
79072805
LW
1562 break;
1563 case ':':
a0d0e21e 1564 chopset = SvPV_force(sv,na);
79072805
LW
1565 break;
1566 case '0':
1567 if (!origalen) {
1568 s = origargv[0];
1569 s += strlen(s);
1570 /* See if all the arguments are contiguous in memory */
1571 for (i = 1; i < origargc; i++) {
1572 if (origargv[i] == s + 1)
1573 s += strlen(++s); /* this one is ok too */
1574 }
bbce6d69
PP
1575 /* can grab env area too? */
1576 if (origenviron && origenviron[0] == s + 1) {
79072805
LW
1577 my_setenv("NoNeSuCh", Nullch);
1578 /* force copy of environment */
1579 for (i = 0; origenviron[i]; i++)
1580 if (origenviron[i] == s + 1)
1581 s += strlen(++s);
1582 }
1583 origalen = s - origargv[0];
1584 }
a0d0e21e 1585 s = SvPV_force(sv,len);
8990e307 1586 i = len;
79072805
LW
1587 if (i >= origalen) {
1588 i = origalen;
1589 SvCUR_set(sv, i);
1590 *SvEND(sv) = '\0';
1591 Copy(s, origargv[0], i, char);
1592 }
1593 else {
1594 Copy(s, origargv[0], i, char);
1595 s = origargv[0]+i;
1596 *s++ = '\0';
1597 while (++i < origalen)
8990e307
LW
1598 *s++ = ' ';
1599 s = origargv[0]+i;
ed6116ce 1600 for (i = 1; i < origargc; i++)
8990e307 1601 origargv[i] = Nullch;
79072805
LW
1602 }
1603 break;
1604 }
1605 return 0;
1606}
1607
1608I32
1609whichsig(sig)
1610char *sig;
1611{
1612 register char **sigv;
1613
1614 for (sigv = sig_name+1; *sigv; sigv++)
1615 if (strEQ(sig,*sigv))
8e07c86e 1616 return sig_num[sigv - sig_name];
79072805
LW
1617#ifdef SIGCLD
1618 if (strEQ(sig,"CHLD"))
1619 return SIGCLD;
1620#endif
1621#ifdef SIGCHLD
1622 if (strEQ(sig,"CLD"))
1623 return SIGCHLD;
1624#endif
1625 return 0;
1626}
1627
ecfc5424 1628Signal_t
79072805 1629sighandler(sig)
a0d0e21e 1630int sig;
79072805
LW
1631{
1632 dSP;
1633 GV *gv;
a0d0e21e 1634 HV *st;
79072805
LW
1635 SV *sv;
1636 CV *cv;
79072805 1637 AV *oldstack;
ff0cee69
PP
1638
1639 if (!psig_ptr[sig])
1640 die("Signal SIG%s received, but no signal handler set.\n",
1641 sig_name[sig]);
1642
88e89b8a 1643 cv = sv_2cv(psig_ptr[sig],&st,&gv,TRUE);
a0d0e21e 1644 if (!cv || !CvROOT(cv)) {
79072805
LW
1645 if (dowarn)
1646 warn("SIG%s handler \"%s\" not defined.\n",
88e89b8a 1647 sig_name[sig], GvENAME(gv) );
79072805
LW
1648 return;
1649 }
1650
88e89b8a
PP
1651 oldstack = curstack;
1652 if (curstack != signalstack)
a0d0e21e 1653 AvFILL(signalstack) = 0;
88e89b8a 1654 SWITCHSTACK(curstack, signalstack);
79072805 1655
88e89b8a
PP
1656 if(psig_name[sig])
1657 sv = SvREFCNT_inc(psig_name[sig]);
1658 else {
ff0cee69
PP
1659 sv = sv_newmortal();
1660 sv_setpv(sv,sig_name[sig]);
88e89b8a 1661 }
a0d0e21e 1662 PUSHMARK(sp);
79072805 1663 PUSHs(sv);
79072805 1664 PUTBACK;
a0d0e21e
LW
1665
1666 perl_call_sv((SV*)cv, G_DISCARD);
79072805
LW
1667
1668 SWITCHSTACK(signalstack, oldstack);
4bf61a0f 1669
79072805
LW
1670 return;
1671}