-
Notifications
You must be signed in to change notification settings - Fork 559
/
pp_sys.c
5798 lines (5246 loc) · 136 KB
/
pp_sys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pp_sys.c
*
* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
* 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* But only a short way ahead its floor and the walls on either side were
* cloven by a great fissure, out of which the red glare came, now leaping
* up, now dying down into darkness; and all the while far below there was
* a rumour and a trouble as of great engines throbbing and labouring.
*
* [p.945 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
*/
/* This file contains system pp ("push/pop") functions that
* execute the opcodes that make up a perl program. A typical pp function
* expects to find its arguments on the stack, and usually pushes its
* results onto the stack, hence the 'pp' terminology. Each OP structure
* contains a pointer to the relevant pp_foo() function.
*
* By 'system', we mean ops which interact with the OS, such as pp_open().
*/
#include "EXTERN.h"
#define PERL_IN_PP_SYS_C
#include "perl.h"
#include "time64.h"
#include "time64.c"
#ifdef I_SHADOW
/* Shadow password support for solaris - pdo@cs.umd.edu
* Not just Solaris: at least HP-UX, IRIX, Linux.
* The API is from SysV.
*
* There are at least two more shadow interfaces,
* see the comments in pp_gpwent().
*
* --jhi */
# ifdef __hpux__
/* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
* and another MAXINT from "perl.h" <- <sys/param.h>. */
# undef MAXINT
# endif
# include <shadow.h>
#endif
#ifdef I_SYS_RESOURCE
# include <sys/resource.h>
#endif
#ifdef NETWARE
NETDB_DEFINE_CONTEXT
#endif
#ifdef HAS_SELECT
# ifdef I_SYS_SELECT
# include <sys/select.h>
# endif
#endif
/* XXX Configure test needed.
h_errno might not be a simple 'int', especially for multi-threaded
applications, see "extern int errno in perl.h". Creating such
a test requires taking into account the differences between
compiling multithreaded and singlethreaded ($ccflags et al).
HOST_NOT_FOUND is typically defined in <netdb.h>.
*/
#if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
extern int h_errno;
#endif
#ifdef HAS_PASSWD
# ifdef I_PWD
# include <pwd.h>
# else
# if !defined(VMS)
struct passwd *getpwnam (char *);
struct passwd *getpwuid (Uid_t);
# endif
# endif
# ifdef HAS_GETPWENT
#ifndef getpwent
struct passwd *getpwent (void);
#elif defined (VMS) && defined (my_getpwent)
struct passwd *Perl_my_getpwent (pTHX);
#endif
# endif
#endif
#ifdef HAS_GROUP
# ifdef I_GRP
# include <grp.h>
# else
struct group *getgrnam (char *);
struct group *getgrgid (Gid_t);
# endif
# ifdef HAS_GETGRENT
#ifndef getgrent
struct group *getgrent (void);
#endif
# endif
#endif
#ifdef I_UTIME
# if defined(_MSC_VER) || defined(__MINGW32__)
# include <sys/utime.h>
# else
# include <utime.h>
# endif
#endif
#ifdef HAS_CHSIZE
# ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
# undef my_chsize
# endif
# define my_chsize PerlLIO_chsize
#else
# ifdef HAS_TRUNCATE
# define my_chsize PerlLIO_chsize
# else
I32 my_chsize(int fd, Off_t length);
# endif
#endif
#ifdef HAS_FLOCK
# define FLOCK flock
#else /* no flock() */
/* fcntl.h might not have been included, even if it exists, because
the current Configure only sets I_FCNTL if it's needed to pick up
the *_OK constants. Make sure it has been included before testing
the fcntl() locking constants. */
# if defined(HAS_FCNTL) && !defined(I_FCNTL)
# include <fcntl.h>
# endif
# if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
# define FLOCK fcntl_emulate_flock
# define FCNTL_EMULATE_FLOCK
# else /* no flock() or fcntl(F_SETLK,...) */
# ifdef HAS_LOCKF
# define FLOCK lockf_emulate_flock
# define LOCKF_EMULATE_FLOCK
# endif /* lockf */
# endif /* no flock() or fcntl(F_SETLK,...) */
# ifdef FLOCK
static int FLOCK (int, int);
/*
* These are the flock() constants. Since this sytems doesn't have
* flock(), the values of the constants are probably not available.
*/
# ifndef LOCK_SH
# define LOCK_SH 1
# endif
# ifndef LOCK_EX
# define LOCK_EX 2
# endif
# ifndef LOCK_NB
# define LOCK_NB 4
# endif
# ifndef LOCK_UN
# define LOCK_UN 8
# endif
# endif /* emulating flock() */
#endif /* no flock() */
#define ZBTLEN 10
static const char zero_but_true[ZBTLEN + 1] = "0 but true";
#if defined(I_SYS_ACCESS) && !defined(R_OK)
# include <sys/access.h>
#endif
#include "reentr.h"
#ifdef __Lynx__
/* Missing protos on LynxOS */
void sethostent(int);
void endhostent(void);
void setnetent(int);
void endnetent(void);
void setprotoent(int);
void endprotoent(void);
void setservent(int);
void endservent(void);
#endif
#undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
/* F_OK unused: if stat() cannot find it... */
#if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
/* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
# define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
#endif
#if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
# ifdef I_SYS_SECURITY
# include <sys/security.h>
# endif
# ifdef ACC_SELF
/* HP SecureWare */
# define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
# else
/* SCO */
# define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
# endif
#endif
#if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
/* AIX */
# define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
#endif
#if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
&& (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
|| defined(HAS_SETREGID) || defined(HAS_SETRESGID))
/* The Hard Way. */
STATIC int
S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
{
const Uid_t ruid = getuid();
const Uid_t euid = geteuid();
const Gid_t rgid = getgid();
const Gid_t egid = getegid();
int res;
#if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
Perl_croak(aTHX_ "switching effective uid is not implemented");
#else
#ifdef HAS_SETREUID
if (setreuid(euid, ruid))
#else
#ifdef HAS_SETRESUID
if (setresuid(euid, ruid, (Uid_t)-1))
#endif
#endif
/* diag_listed_as: entering effective %s failed */
Perl_croak(aTHX_ "entering effective uid failed");
#endif
#if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
Perl_croak(aTHX_ "switching effective gid is not implemented");
#else
#ifdef HAS_SETREGID
if (setregid(egid, rgid))
#else
#ifdef HAS_SETRESGID
if (setresgid(egid, rgid, (Gid_t)-1))
#endif
#endif
/* diag_listed_as: entering effective %s failed */
Perl_croak(aTHX_ "entering effective gid failed");
#endif
res = access(path, mode);
#ifdef HAS_SETREUID
if (setreuid(ruid, euid))
#else
#ifdef HAS_SETRESUID
if (setresuid(ruid, euid, (Uid_t)-1))
#endif
#endif
/* diag_listed_as: leaving effective %s failed */
Perl_croak(aTHX_ "leaving effective uid failed");
#ifdef HAS_SETREGID
if (setregid(rgid, egid))
#else
#ifdef HAS_SETRESGID
if (setresgid(rgid, egid, (Gid_t)-1))
#endif
#endif
/* diag_listed_as: leaving effective %s failed */
Perl_croak(aTHX_ "leaving effective gid failed");
return res;
}
# define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
#endif
PP(pp_backtick)
{
dSP; dTARGET;
PerlIO *fp;
const char * const tmps = POPpconstx;
const I32 gimme = GIMME_V;
const char *mode = "r";
TAINT_PROPER("``");
if (PL_op->op_private & OPpOPEN_IN_RAW)
mode = "rb";
else if (PL_op->op_private & OPpOPEN_IN_CRLF)
mode = "rt";
fp = PerlProc_popen(tmps, mode);
if (fp) {
const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
if (type && *type)
PerlIO_apply_layers(aTHX_ fp,mode,type);
if (gimme == G_VOID) {
char tmpbuf[256];
while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
NOOP;
}
else if (gimme == G_SCALAR) {
ENTER_with_name("backtick");
SAVESPTR(PL_rs);
PL_rs = &PL_sv_undef;
sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
NOOP;
LEAVE_with_name("backtick");
XPUSHs(TARG);
SvTAINTED_on(TARG);
}
else {
for (;;) {
SV * const sv = newSV(79);
if (sv_gets(sv, fp, 0) == NULL) {
SvREFCNT_dec(sv);
break;
}
mXPUSHs(sv);
if (SvLEN(sv) - SvCUR(sv) > 20) {
SvPV_shrink_to_cur(sv);
}
SvTAINTED_on(sv);
}
}
STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
TAINT; /* "I believe that this is not gratuitous!" */
}
else {
STATUS_NATIVE_CHILD_SET(-1);
if (gimme == G_SCALAR)
RETPUSHUNDEF;
}
RETURN;
}
PP(pp_glob)
{
OP *result;
dSP;
GV * const gv = (PL_op->op_flags & OPf_SPECIAL) ? NULL : (GV *)POPs;
PUTBACK;
/* make a copy of the pattern if it is gmagical, to ensure that magic
* is called once and only once */
if (SvGMAGICAL(TOPs)) TOPs = sv_2mortal(newSVsv(TOPs));
tryAMAGICunTARGETlist(iter_amg, (PL_op->op_flags & OPf_SPECIAL));
if (PL_op->op_flags & OPf_SPECIAL) {
/* call Perl-level glob function instead. Stack args are:
* MARK, wildcard
* and following OPs should be: gv(CORE::GLOBAL::glob), entersub
* */
return NORMAL;
}
if (PL_globhook) {
PL_globhook(aTHX);
return NORMAL;
}
/* Note that we only ever get here if File::Glob fails to load
* without at the same time croaking, for some reason, or if
* perl was built with PERL_EXTERNAL_GLOB */
ENTER_with_name("glob");
#ifndef VMS
if (TAINTING_get) {
/*
* The external globbing program may use things we can't control,
* so for security reasons we must assume the worst.
*/
TAINT;
taint_proper(PL_no_security, "glob");
}
#endif /* !VMS */
SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
PL_last_in_gv = gv;
SAVESPTR(PL_rs); /* This is not permanent, either. */
PL_rs = newSVpvs_flags("\000", SVs_TEMP);
#ifndef DOSISH
#ifndef CSH
*SvPVX(PL_rs) = '\n';
#endif /* !CSH */
#endif /* !DOSISH */
result = do_readline();
LEAVE_with_name("glob");
return result;
}
PP(pp_rcatline)
{
PL_last_in_gv = cGVOP_gv;
return do_readline();
}
PP(pp_warn)
{
dSP; dMARK;
SV *exsv;
STRLEN len;
if (SP - MARK > 1) {
dTARGET;
do_join(TARG, &PL_sv_no, MARK, SP);
exsv = TARG;
SP = MARK + 1;
}
else if (SP == MARK) {
exsv = &PL_sv_no;
EXTEND(SP, 1);
SP = MARK + 1;
}
else {
exsv = TOPs;
if (SvGMAGICAL(exsv)) exsv = sv_mortalcopy(exsv);
}
if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
/* well-formed exception supplied */
}
else {
SV * const errsv = ERRSV;
SvGETMAGIC(errsv);
if (SvROK(errsv)) {
if (SvGMAGICAL(errsv)) {
exsv = sv_newmortal();
sv_setsv_nomg(exsv, errsv);
}
else exsv = errsv;
}
else if (SvPOKp(errsv) ? SvCUR(errsv) : SvNIOKp(errsv)) {
exsv = sv_newmortal();
sv_setsv_nomg(exsv, errsv);
sv_catpvs(exsv, "\t...caught");
}
else {
exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
}
}
if (SvROK(exsv) && !PL_warnhook)
Perl_warn(aTHX_ "%"SVf, SVfARG(exsv));
else warn_sv(exsv);
RETSETYES;
}
PP(pp_die)
{
dSP; dMARK;
SV *exsv;
STRLEN len;
#ifdef VMS
VMSISH_HUSHED =
VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH);
#endif
if (SP - MARK != 1) {
dTARGET;
do_join(TARG, &PL_sv_no, MARK, SP);
exsv = TARG;
SP = MARK + 1;
}
else {
exsv = TOPs;
}
if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
/* well-formed exception supplied */
}
else {
SV * const errsv = ERRSV;
SvGETMAGIC(errsv);
if (SvROK(errsv)) {
exsv = errsv;
if (sv_isobject(exsv)) {
HV * const stash = SvSTASH(SvRV(exsv));
GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
if (gv) {
SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
EXTEND(SP, 3);
PUSHMARK(SP);
PUSHs(exsv);
PUSHs(file);
PUSHs(line);
PUTBACK;
call_sv(MUTABLE_SV(GvCV(gv)),
G_SCALAR|G_EVAL|G_KEEPERR);
exsv = sv_mortalcopy(*PL_stack_sp--);
}
}
}
else if (SvPOK(errsv) && SvCUR(errsv)) {
exsv = sv_mortalcopy(errsv);
sv_catpvs(exsv, "\t...propagated");
}
else {
exsv = newSVpvs_flags("Died", SVs_TEMP);
}
}
die_sv(exsv);
NOT_REACHED; /* NOTREACHED */
return NULL; /* avoid missing return from non-void function warning */
}
/* I/O. */
OP *
Perl_tied_method(pTHX_ SV *methname, SV **sp, SV *const sv,
const MAGIC *const mg, const U32 flags, U32 argc, ...)
{
SV **orig_sp = sp;
I32 ret_args;
PERL_ARGS_ASSERT_TIED_METHOD;
/* Ensure that our flag bits do not overlap. */
STATIC_ASSERT_STMT((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
STATIC_ASSERT_STMT((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
STATIC_ASSERT_STMT((TIED_METHOD_SAY & G_WANT) == 0);
PUTBACK; /* sp is at *foot* of args, so this pops args from old stack */
PUSHSTACKi(PERLSI_MAGIC);
EXTEND(SP, argc+1); /* object + args */
PUSHMARK(sp);
PUSHs(SvTIED_obj(sv, mg));
if (flags & TIED_METHOD_ARGUMENTS_ON_STACK) {
Copy(orig_sp + 2, sp + 1, argc, SV*); /* copy args to new stack */
sp += argc;
}
else if (argc) {
const U32 mortalize_not_needed
= flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
va_list args;
va_start(args, argc);
do {
SV *const arg = va_arg(args, SV *);
if(mortalize_not_needed)
PUSHs(arg);
else
mPUSHs(arg);
} while (--argc);
va_end(args);
}
PUTBACK;
ENTER_with_name("call_tied_method");
if (flags & TIED_METHOD_SAY) {
/* local $\ = "\n" */
SAVEGENERICSV(PL_ors_sv);
PL_ors_sv = newSVpvs("\n");
}
ret_args = call_sv(methname, (flags & G_WANT)|G_METHOD_NAMED);
SPAGAIN;
orig_sp = sp;
POPSTACK;
SPAGAIN;
if (ret_args) { /* copy results back to original stack */
EXTEND(sp, ret_args);
Copy(orig_sp - ret_args + 1, sp + 1, ret_args, SV*);
sp += ret_args;
PUTBACK;
}
LEAVE_with_name("call_tied_method");
return NORMAL;
}
#define tied_method0(a,b,c,d) \
Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
#define tied_method1(a,b,c,d,e) \
Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
#define tied_method2(a,b,c,d,e,f) \
Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
PP(pp_open)
{
dSP;
dMARK; dORIGMARK;
dTARGET;
SV *sv;
IO *io;
const char *tmps;
STRLEN len;
bool ok;
GV * const gv = MUTABLE_GV(*++MARK);
if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
DIE(aTHX_ PL_no_usym, "filehandle");
if ((io = GvIOp(gv))) {
const MAGIC *mg;
IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
if (IoDIRP(io))
Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
"Opening dirhandle %"HEKf" also as a file",
HEKfARG(GvENAME_HEK(gv)));
mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
if (mg) {
/* Method's args are same as ours ... */
/* ... except handle is replaced by the object */
return Perl_tied_method(aTHX_ SV_CONST(OPEN), mark - 1, MUTABLE_SV(io), mg,
G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
sp - mark);
}
}
if (MARK < SP) {
sv = *++MARK;
}
else {
sv = GvSVn(gv);
}
tmps = SvPV_const(sv, len);
ok = do_open6(gv, tmps, len, NULL, MARK+1, (SP-MARK));
SP = ORIGMARK;
if (ok)
PUSHi( (I32)PL_forkprocess );
else if (PL_forkprocess == 0) /* we are a new child */
PUSHi(0);
else
RETPUSHUNDEF;
RETURN;
}
PP(pp_close)
{
dSP;
GV * const gv =
MAXARG == 0 || (!TOPs && !POPs) ? PL_defoutgv : MUTABLE_GV(POPs);
if (MAXARG == 0)
EXTEND(SP, 1);
if (gv) {
IO * const io = GvIO(gv);
if (io) {
const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
if (mg) {
return tied_method0(SV_CONST(CLOSE), SP, MUTABLE_SV(io), mg);
}
}
}
PUSHs(boolSV(do_close(gv, TRUE)));
RETURN;
}
PP(pp_pipe_op)
{
#ifdef HAS_PIPE
dSP;
IO *rstio;
IO *wstio;
int fd[2];
GV * const wgv = MUTABLE_GV(POPs);
GV * const rgv = MUTABLE_GV(POPs);
assert (isGV_with_GP(rgv));
assert (isGV_with_GP(wgv));
rstio = GvIOn(rgv);
if (IoIFP(rstio))
do_close(rgv, FALSE);
wstio = GvIOn(wgv);
if (IoIFP(wstio))
do_close(wgv, FALSE);
if (PerlProc_pipe(fd) < 0)
goto badexit;
IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
IoOFP(rstio) = IoIFP(rstio);
IoIFP(wstio) = IoOFP(wstio);
IoTYPE(rstio) = IoTYPE_RDONLY;
IoTYPE(wstio) = IoTYPE_WRONLY;
if (!IoIFP(rstio) || !IoOFP(wstio)) {
if (IoIFP(rstio))
PerlIO_close(IoIFP(rstio));
else
PerlLIO_close(fd[0]);
if (IoOFP(wstio))
PerlIO_close(IoOFP(wstio));
else
PerlLIO_close(fd[1]);
goto badexit;
}
#if defined(HAS_FCNTL) && defined(F_SETFD)
/* ensure close-on-exec */
if ((fcntl(fd[0], F_SETFD,fd[0] > PL_maxsysfd) < 0) ||
(fcntl(fd[1], F_SETFD,fd[1] > PL_maxsysfd) < 0))
goto badexit;
#endif
RETPUSHYES;
badexit:
RETPUSHUNDEF;
#else
DIE(aTHX_ PL_no_func, "pipe");
#endif
}
PP(pp_fileno)
{
dSP; dTARGET;
GV *gv;
IO *io;
PerlIO *fp;
const MAGIC *mg;
if (MAXARG < 1)
RETPUSHUNDEF;
gv = MUTABLE_GV(POPs);
io = GvIO(gv);
if (io
&& (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
{
return tied_method0(SV_CONST(FILENO), SP, MUTABLE_SV(io), mg);
}
if (io && IoDIRP(io)) {
#if defined(HAS_DIRFD) || defined(HAS_DIR_DD_FD)
PUSHi(my_dirfd(IoDIRP(io)));
RETURN;
#elif defined(ENOTSUP)
errno = ENOTSUP; /* Operation not supported */
RETPUSHUNDEF;
#elif defined(EOPNOTSUPP)
errno = EOPNOTSUPP; /* Operation not supported on socket */
RETPUSHUNDEF;
#else
errno = EINVAL; /* Invalid argument */
RETPUSHUNDEF;
#endif
}
if (!io || !(fp = IoIFP(io))) {
/* Can't do this because people seem to do things like
defined(fileno($foo)) to check whether $foo is a valid fh.
report_evil_fh(gv);
*/
RETPUSHUNDEF;
}
PUSHi(PerlIO_fileno(fp));
RETURN;
}
PP(pp_umask)
{
dSP;
#ifdef HAS_UMASK
dTARGET;
Mode_t anum;
if (MAXARG < 1 || (!TOPs && !POPs)) {
anum = PerlLIO_umask(022);
/* setting it to 022 between the two calls to umask avoids
* to have a window where the umask is set to 0 -- meaning
* that another thread could create world-writeable files. */
if (anum != 022)
(void)PerlLIO_umask(anum);
}
else
anum = PerlLIO_umask(POPi);
TAINT_PROPER("umask");
XPUSHi(anum);
#else
/* Only DIE if trying to restrict permissions on "user" (self).
* Otherwise it's harmless and more useful to just return undef
* since 'group' and 'other' concepts probably don't exist here. */
if (MAXARG >= 1 && (TOPs||POPs) && (POPi & 0700))
DIE(aTHX_ "umask not implemented");
XPUSHs(&PL_sv_undef);
#endif
RETURN;
}
PP(pp_binmode)
{
dSP;
GV *gv;
IO *io;
PerlIO *fp;
SV *discp = NULL;
if (MAXARG < 1)
RETPUSHUNDEF;
if (MAXARG > 1) {
discp = POPs;
}
gv = MUTABLE_GV(POPs);
io = GvIO(gv);
if (io) {
const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
if (mg) {
/* This takes advantage of the implementation of the varargs
function, which I don't think that the optimiser will be able to
figure out. Although, as it's a static function, in theory it
could. */
return Perl_tied_method(aTHX_ SV_CONST(BINMODE), SP, MUTABLE_SV(io), mg,
G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
discp ? 1 : 0, discp);
}
}
if (!io || !(fp = IoIFP(io))) {
report_evil_fh(gv);
SETERRNO(EBADF,RMS_IFI);
RETPUSHUNDEF;
}
PUTBACK;
{
STRLEN len = 0;
const char *d = NULL;
int mode;
if (discp)
d = SvPV_const(discp, len);
mode = mode_from_discipline(d, len);
if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
SPAGAIN;
RETPUSHUNDEF;
}
}
SPAGAIN;
RETPUSHYES;
}
else {
SPAGAIN;
RETPUSHUNDEF;
}
}
}
PP(pp_tie)
{
dSP; dMARK;
HV* stash;
GV *gv = NULL;
SV *sv;
const I32 markoff = MARK - PL_stack_base;
const char *methname;
int how = PERL_MAGIC_tied;
U32 items;
SV *varsv = *++MARK;
switch(SvTYPE(varsv)) {
case SVt_PVHV:
{
HE *entry;
methname = "TIEHASH";
if (HvLAZYDEL(varsv) && (entry = HvEITER((HV *)varsv))) {
HvLAZYDEL_off(varsv);
hv_free_ent((HV *)varsv, entry);
}
HvEITER_set(MUTABLE_HV(varsv), 0);
break;
}
case SVt_PVAV:
methname = "TIEARRAY";
if (!AvREAL(varsv)) {
if (!AvREIFY(varsv))
Perl_croak(aTHX_ "Cannot tie unreifiable array");
av_clear((AV *)varsv);
AvREIFY_off(varsv);
AvREAL_on(varsv);
}
break;
case SVt_PVGV:
case SVt_PVLV:
if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
methname = "TIEHANDLE";
how = PERL_MAGIC_tiedscalar;
/* For tied filehandles, we apply tiedscalar magic to the IO
slot of the GP rather than the GV itself. AMS 20010812 */
if (!GvIOp(varsv))
GvIOp(varsv) = newIO();
varsv = MUTABLE_SV(GvIOp(varsv));
break;
}
if (SvTYPE(varsv) == SVt_PVLV && LvTYPE(varsv) == 'y') {
vivify_defelem(varsv);
varsv = LvTARG(varsv);
}
/* FALLTHROUGH */
default:
methname = "TIESCALAR";
how = PERL_MAGIC_tiedscalar;
break;
}
items = SP - MARK++;
if (sv_isobject(*MARK)) { /* Calls GET magic. */
ENTER_with_name("call_TIE");
PUSHSTACKi(PERLSI_MAGIC);
PUSHMARK(SP);
EXTEND(SP,(I32)items);
while (items--)
PUSHs(*MARK++);
PUTBACK;
call_method(methname, G_SCALAR);
}
else {
/* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
* will attempt to invoke IO::File::TIEARRAY, with (best case) the
* wrong error message, and worse case, supreme action at a distance.
* (Sorry obfuscation writers. You're not going to be given this one.)
*/
stash = gv_stashsv(*MARK, 0);
if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
}
ENTER_with_name("call_TIE");
PUSHSTACKi(PERLSI_MAGIC);
PUSHMARK(SP);
EXTEND(SP,(I32)items);
while (items--)
PUSHs(*MARK++);
PUTBACK;
call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
}
SPAGAIN;
sv = TOPs;
POPSTACK;
if (sv_isobject(sv)) {
sv_unmagic(varsv, how);
/* Croak if a self-tie on an aggregate is attempted. */
if (varsv == SvRV(sv) &&
(SvTYPE(varsv) == SVt_PVAV ||
SvTYPE(varsv) == SVt_PVHV))
Perl_croak(aTHX_
"Self-ties of arrays and hashes are not supported");
sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
}
LEAVE_with_name("call_TIE");
SP = PL_stack_base + markoff;
PUSHs(sv);
RETURN;
}
/* also used for: pp_dbmclose() */
PP(pp_untie)
{
dSP;
MAGIC *mg;
SV *sv = POPs;
const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
RETPUSHYES;
if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y' &&
!(sv = defelem_target(sv, NULL))) RETPUSHUNDEF;
if ((mg = SvTIED_mg(sv, how))) {
SV * const obj = SvRV(SvTIED_obj(sv, mg));
if (obj) {
GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
CV *cv;
if (gv && isGV(gv) && (cv = GvCV(gv))) {
PUSHMARK(SP);
PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
mXPUSHi(SvREFCNT(obj) - 1);
PUTBACK;
ENTER_with_name("call_UNTIE");