3 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 * 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * But only a short way ahead its floor and the walls on either side were
13 * cloven by a great fissure, out of which the red glare came, now leaping
14 * up, now dying down into darkness; and all the while far below there was
15 * a rumour and a trouble as of great engines throbbing and labouring.
17 * [p.945 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
20 /* This file contains system pp ("push/pop") functions that
21 * execute the opcodes that make up a perl program. A typical pp function
22 * expects to find its arguments on the stack, and usually pushes its
23 * results onto the stack, hence the 'pp' terminology. Each OP structure
24 * contains a pointer to the relevant pp_foo() function.
26 * By 'system', we mean ops which interact with the OS, such as pp_open().
30 #define PERL_IN_PP_SYS_C
36 /* Shadow password support for solaris - pdo@cs.umd.edu
37 * Not just Solaris: at least HP-UX, IRIX, Linux.
38 * The API is from SysV.
40 * There are at least two more shadow interfaces,
41 * see the comments in pp_gpwent().
45 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
46 * and another MAXINT from "perl.h" <- <sys/param.h>. */
53 # include <sys/wait.h>
57 # include <sys/resource.h>
66 # include <sys/select.h>
70 /* XXX Configure test needed.
71 h_errno might not be a simple 'int', especially for multi-threaded
72 applications, see "extern int errno in perl.h". Creating such
73 a test requires taking into account the differences between
74 compiling multithreaded and singlethreaded ($ccflags et al).
75 HOST_NOT_FOUND is typically defined in <netdb.h>.
77 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
86 struct passwd *getpwnam (char *);
87 struct passwd *getpwuid (Uid_t);
92 struct passwd *getpwent (void);
93 #elif defined (VMS) && defined (my_getpwent)
94 struct passwd *Perl_my_getpwent (pTHX);
103 struct group *getgrnam (char *);
104 struct group *getgrgid (Gid_t);
108 struct group *getgrent (void);
114 # if defined(_MSC_VER) || defined(__MINGW32__)
115 # include <sys/utime.h>
122 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
125 # define my_chsize PerlLIO_chsize
128 # define my_chsize PerlLIO_chsize
130 I32 my_chsize(int fd, Off_t length);
136 #else /* no flock() */
138 /* fcntl.h might not have been included, even if it exists, because
139 the current Configure only sets I_FCNTL if it's needed to pick up
140 the *_OK constants. Make sure it has been included before testing
141 the fcntl() locking constants. */
142 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
146 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
147 # define FLOCK fcntl_emulate_flock
148 # define FCNTL_EMULATE_FLOCK
149 # else /* no flock() or fcntl(F_SETLK,...) */
151 # define FLOCK lockf_emulate_flock
152 # define LOCKF_EMULATE_FLOCK
154 # endif /* no flock() or fcntl(F_SETLK,...) */
157 static int FLOCK (int, int);
160 * These are the flock() constants. Since this sytems doesn't have
161 * flock(), the values of the constants are probably not available.
175 # endif /* emulating flock() */
177 #endif /* no flock() */
180 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
182 #if defined(I_SYS_ACCESS) && !defined(R_OK)
183 # include <sys/access.h>
186 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
187 # define FD_CLOEXEC 1 /* NeXT needs this */
193 /* Missing protos on LynxOS */
194 void sethostent(int);
195 void endhostent(void);
197 void endnetent(void);
198 void setprotoent(int);
199 void endprotoent(void);
200 void setservent(int);
201 void endservent(void);
204 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
206 /* F_OK unused: if stat() cannot find it... */
208 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
209 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
210 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
213 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
214 # ifdef I_SYS_SECURITY
215 # include <sys/security.h>
219 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
222 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
226 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
228 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
232 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
233 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
234 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
237 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
239 const Uid_t ruid = getuid();
240 const Uid_t euid = geteuid();
241 const Gid_t rgid = getgid();
242 const Gid_t egid = getegid();
245 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
246 Perl_croak(aTHX_ "switching effective uid is not implemented");
249 if (setreuid(euid, ruid))
252 if (setresuid(euid, ruid, (Uid_t)-1))
255 Perl_croak(aTHX_ "entering effective uid failed");
258 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
259 Perl_croak(aTHX_ "switching effective gid is not implemented");
262 if (setregid(egid, rgid))
265 if (setresgid(egid, rgid, (Gid_t)-1))
268 Perl_croak(aTHX_ "entering effective gid failed");
271 res = access(path, mode);
274 if (setreuid(ruid, euid))
277 if (setresuid(ruid, euid, (Uid_t)-1))
280 Perl_croak(aTHX_ "leaving effective uid failed");
283 if (setregid(rgid, egid))
286 if (setresgid(rgid, egid, (Gid_t)-1))
289 Perl_croak(aTHX_ "leaving effective gid failed");
293 # define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
300 const char * const tmps = POPpconstx;
301 const I32 gimme = GIMME_V;
302 const char *mode = "r";
305 if (PL_op->op_private & OPpOPEN_IN_RAW)
307 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
309 fp = PerlProc_popen(tmps, mode);
311 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
313 PerlIO_apply_layers(aTHX_ fp,mode,type);
315 if (gimme == G_VOID) {
317 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
320 else if (gimme == G_SCALAR) {
321 ENTER_with_name("backtick");
323 PL_rs = &PL_sv_undef;
324 sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
325 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
327 LEAVE_with_name("backtick");
333 SV * const sv = newSV(79);
334 if (sv_gets(sv, fp, 0) == NULL) {
339 if (SvLEN(sv) - SvCUR(sv) > 20) {
340 SvPV_shrink_to_cur(sv);
345 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
346 TAINT; /* "I believe that this is not gratuitous!" */
349 STATUS_NATIVE_CHILD_SET(-1);
350 if (gimme == G_SCALAR)
361 tryAMAGICunTARGET(iter, -1);
363 /* Note that we only ever get here if File::Glob fails to load
364 * without at the same time croaking, for some reason, or if
365 * perl was built with PERL_EXTERNAL_GLOB */
367 ENTER_with_name("glob");
372 * The external globbing program may use things we can't control,
373 * so for security reasons we must assume the worst.
376 taint_proper(PL_no_security, "glob");
380 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
381 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
383 SAVESPTR(PL_rs); /* This is not permanent, either. */
384 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
387 *SvPVX(PL_rs) = '\n';
391 result = do_readline();
392 LEAVE_with_name("glob");
399 PL_last_in_gv = cGVOP_gv;
400 return do_readline();
411 do_join(TARG, &PL_sv_no, MARK, SP);
415 else if (SP == MARK) {
424 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
425 /* well-formed exception supplied */
427 else if (SvROK(ERRSV)) {
430 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
431 exsv = sv_mortalcopy(ERRSV);
432 sv_catpvs(exsv, "\t...caught");
435 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
448 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
450 if (SP - MARK != 1) {
452 do_join(TARG, &PL_sv_no, MARK, SP);
460 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
461 /* well-formed exception supplied */
463 else if (SvROK(ERRSV)) {
465 if (sv_isobject(exsv)) {
466 HV * const stash = SvSTASH(SvRV(exsv));
467 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
469 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
470 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
477 call_sv(MUTABLE_SV(GvCV(gv)),
478 G_SCALAR|G_EVAL|G_KEEPERR);
479 exsv = sv_mortalcopy(*PL_stack_sp--);
483 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
484 exsv = sv_mortalcopy(ERRSV);
485 sv_catpvs(exsv, "\t...propagated");
488 exsv = newSVpvs_flags("Died", SVs_TEMP);
506 GV * const gv = MUTABLE_GV(*++MARK);
508 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
509 DIE(aTHX_ PL_no_usym, "filehandle");
511 if ((io = GvIOp(gv))) {
513 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
516 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
517 "Opening dirhandle %s also as a file",
520 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
522 /* Method's args are same as ours ... */
523 /* ... except handle is replaced by the object */
524 *MARK-- = SvTIED_obj(MUTABLE_SV(io), mg);
527 ENTER_with_name("call_OPEN");
528 call_method("OPEN", G_SCALAR);
529 LEAVE_with_name("call_OPEN");
541 tmps = SvPV_const(sv, len);
542 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
545 PUSHi( (I32)PL_forkprocess );
546 else if (PL_forkprocess == 0) /* we are a new child */
553 /* These are private to this function, which is private to this file.
554 Use 0x04 rather than the next available bit, to help the compiler if the
555 architecture can generate more efficient instructions. */
556 #define MORTALIZE_NOT_NEEDED 0x04
557 #define TIED_HANDLE_ARGC_SHIFT 3
560 S_tied_handle_method(pTHX_ const char *const methname, SV **sp,
561 IO *const io, MAGIC *const mg, const U32 flags, ...)
563 U32 argc = flags >> TIED_HANDLE_ARGC_SHIFT;
565 PERL_ARGS_ASSERT_TIED_HANDLE_METHOD;
567 /* Ensure that our flag bits do not overlap. */
568 assert((MORTALIZE_NOT_NEEDED & G_WANT) == 0);
569 assert((G_WANT >> TIED_HANDLE_ARGC_SHIFT) == 0);
572 PUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
574 const U32 mortalize_not_needed = flags & MORTALIZE_NOT_NEEDED;
576 va_start(args, flags);
578 SV *const arg = va_arg(args, SV *);
579 if(mortalize_not_needed)
588 ENTER_with_name("call_tied_handle_method");
589 call_method(methname, flags & G_WANT);
590 LEAVE_with_name("call_tied_handle_method");
594 #define tied_handle_method(a,b,c,d) \
595 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR)
596 #define tied_handle_method1(a,b,c,d,e) \
597 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (1 << TIED_HANDLE_ARGC_SHIFT),e)
598 #define tied_handle_method2(a,b,c,d,e,f) \
599 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (2 << TIED_HANDLE_ARGC_SHIFT), e,f)
604 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
610 IO * const io = GvIO(gv);
612 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
614 return tied_handle_method("CLOSE", SP, io, mg);
618 PUSHs(boolSV(do_close(gv, TRUE)));
631 GV * const wgv = MUTABLE_GV(POPs);
632 GV * const rgv = MUTABLE_GV(POPs);
637 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
638 DIE(aTHX_ PL_no_usym, "filehandle");
643 do_close(rgv, FALSE);
645 do_close(wgv, FALSE);
647 if (PerlProc_pipe(fd) < 0)
650 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
651 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
652 IoOFP(rstio) = IoIFP(rstio);
653 IoIFP(wstio) = IoOFP(wstio);
654 IoTYPE(rstio) = IoTYPE_RDONLY;
655 IoTYPE(wstio) = IoTYPE_WRONLY;
657 if (!IoIFP(rstio) || !IoOFP(wstio)) {
659 PerlIO_close(IoIFP(rstio));
661 PerlLIO_close(fd[0]);
663 PerlIO_close(IoOFP(wstio));
665 PerlLIO_close(fd[1]);
668 #if defined(HAS_FCNTL) && defined(F_SETFD)
669 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
670 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
677 DIE(aTHX_ PL_no_func, "pipe");
691 gv = MUTABLE_GV(POPs);
693 if (gv && (io = GvIO(gv))
694 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
696 return tied_handle_method("FILENO", SP, io, mg);
699 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
700 /* Can't do this because people seem to do things like
701 defined(fileno($foo)) to check whether $foo is a valid fh.
708 PUSHi(PerlIO_fileno(fp));
721 anum = PerlLIO_umask(022);
722 /* setting it to 022 between the two calls to umask avoids
723 * to have a window where the umask is set to 0 -- meaning
724 * that another thread could create world-writeable files. */
726 (void)PerlLIO_umask(anum);
729 anum = PerlLIO_umask(POPi);
730 TAINT_PROPER("umask");
733 /* Only DIE if trying to restrict permissions on "user" (self).
734 * Otherwise it's harmless and more useful to just return undef
735 * since 'group' and 'other' concepts probably don't exist here. */
736 if (MAXARG >= 1 && (POPi & 0700))
737 DIE(aTHX_ "umask not implemented");
738 XPUSHs(&PL_sv_undef);
757 gv = MUTABLE_GV(POPs);
759 if (gv && (io = GvIO(gv))) {
760 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
762 /* This takes advantage of the implementation of the varargs
763 function, which I don't think that the optimiser will be able to
764 figure out. Although, as it's a static function, in theory it
766 return S_tied_handle_method(aTHX_ "BINMODE", SP, io, mg,
767 G_SCALAR|MORTALIZE_NOT_NEEDED
769 ? (1 << TIED_HANDLE_ARGC_SHIFT) : 0),
774 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
776 SETERRNO(EBADF,RMS_IFI);
783 const char *d = NULL;
786 d = SvPV_const(discp, len);
787 mode = mode_from_discipline(d, len);
788 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
789 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
790 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
811 const I32 markoff = MARK - PL_stack_base;
812 const char *methname;
813 int how = PERL_MAGIC_tied;
817 switch(SvTYPE(varsv)) {
819 methname = "TIEHASH";
820 HvEITER_set(MUTABLE_HV(varsv), 0);
823 methname = "TIEARRAY";
827 if (isGV_with_GP(varsv)) {
828 if (SvFAKE(varsv) && !(GvFLAGS(varsv) & GVf_TIEWARNED)) {
829 deprecate("tie on a handle without *");
830 GvFLAGS(varsv) |= GVf_TIEWARNED;
832 methname = "TIEHANDLE";
833 how = PERL_MAGIC_tiedscalar;
834 /* For tied filehandles, we apply tiedscalar magic to the IO
835 slot of the GP rather than the GV itself. AMS 20010812 */
837 GvIOp(varsv) = newIO();
838 varsv = MUTABLE_SV(GvIOp(varsv));
843 methname = "TIESCALAR";
844 how = PERL_MAGIC_tiedscalar;
848 if (sv_isobject(*MARK)) { /* Calls GET magic. */
849 ENTER_with_name("call_TIE");
850 PUSHSTACKi(PERLSI_MAGIC);
852 EXTEND(SP,(I32)items);
856 call_method(methname, G_SCALAR);
859 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
860 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
861 * wrong error message, and worse case, supreme action at a distance.
862 * (Sorry obfuscation writers. You're not going to be given this one.)
865 const char *name = SvPV_nomg_const(*MARK, len);
866 stash = gv_stashpvn(name, len, 0);
867 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
868 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
869 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
871 ENTER_with_name("call_TIE");
872 PUSHSTACKi(PERLSI_MAGIC);
874 EXTEND(SP,(I32)items);
878 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
884 if (sv_isobject(sv)) {
885 sv_unmagic(varsv, how);
886 /* Croak if a self-tie on an aggregate is attempted. */
887 if (varsv == SvRV(sv) &&
888 (SvTYPE(varsv) == SVt_PVAV ||
889 SvTYPE(varsv) == SVt_PVHV))
891 "Self-ties of arrays and hashes are not supported");
892 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
894 LEAVE_with_name("call_TIE");
895 SP = PL_stack_base + markoff;
905 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
906 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
908 if (isGV_with_GP(sv)) {
909 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
910 deprecate("untie on a handle without *");
911 GvFLAGS(sv) |= GVf_TIEWARNED;
913 if (!(sv = MUTABLE_SV(GvIOp(sv))))
917 if ((mg = SvTIED_mg(sv, how))) {
918 SV * const obj = SvRV(SvTIED_obj(sv, mg));
920 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
922 if (gv && isGV(gv) && (cv = GvCV(gv))) {
924 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
925 mXPUSHi(SvREFCNT(obj) - 1);
927 ENTER_with_name("call_UNTIE");
928 call_sv(MUTABLE_SV(cv), G_VOID);
929 LEAVE_with_name("call_UNTIE");
932 else if (mg && SvREFCNT(obj) > 1) {
933 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
934 "untie attempted while %"UVuf" inner references still exist",
935 (UV)SvREFCNT(obj) - 1 ) ;
939 sv_unmagic(sv, how) ;
949 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
950 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
952 if (isGV_with_GP(sv)) {
953 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
954 deprecate("tied on a handle without *");
955 GvFLAGS(sv) |= GVf_TIEWARNED;
957 if (!(sv = MUTABLE_SV(GvIOp(sv))))
961 if ((mg = SvTIED_mg(sv, how))) {
962 SV *osv = SvTIED_obj(sv, mg);
963 if (osv == mg->mg_obj)
964 osv = sv_mortalcopy(osv);
978 HV * const hv = MUTABLE_HV(POPs);
979 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
980 stash = gv_stashsv(sv, 0);
981 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
983 require_pv("AnyDBM_File.pm");
985 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
986 DIE(aTHX_ "No dbm on this machine");
996 mPUSHu(O_RDWR|O_CREAT);
1001 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1004 if (!sv_isobject(TOPs)) {
1012 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1016 if (sv_isobject(TOPs)) {
1017 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1018 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1035 struct timeval timebuf;
1036 struct timeval *tbuf = &timebuf;
1039 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1044 # if BYTEORDER & 0xf0000
1045 # define ORDERBYTE (0x88888888 - BYTEORDER)
1047 # define ORDERBYTE (0x4444 - BYTEORDER)
1053 for (i = 1; i <= 3; i++) {
1054 SV * const sv = SP[i];
1057 if (SvREADONLY(sv)) {
1059 sv_force_normal_flags(sv, 0);
1060 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1061 Perl_croak_no_modify(aTHX);
1064 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1065 SvPV_force_nolen(sv); /* force string conversion */
1072 /* little endians can use vecs directly */
1073 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1080 masksize = NFDBITS / NBBY;
1082 masksize = sizeof(long); /* documented int, everyone seems to use long */
1084 Zero(&fd_sets[0], 4, char*);
1087 # if SELECT_MIN_BITS == 1
1088 growsize = sizeof(fd_set);
1090 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1091 # undef SELECT_MIN_BITS
1092 # define SELECT_MIN_BITS __FD_SETSIZE
1094 /* If SELECT_MIN_BITS is greater than one we most probably will want
1095 * to align the sizes with SELECT_MIN_BITS/8 because for example
1096 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1097 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1098 * on (sets/tests/clears bits) is 32 bits. */
1099 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1107 timebuf.tv_sec = (long)value;
1108 value -= (NV)timebuf.tv_sec;
1109 timebuf.tv_usec = (long)(value * 1000000.0);
1114 for (i = 1; i <= 3; i++) {
1116 if (!SvOK(sv) || SvCUR(sv) == 0) {
1123 Sv_Grow(sv, growsize);
1127 while (++j <= growsize) {
1131 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1133 Newx(fd_sets[i], growsize, char);
1134 for (offset = 0; offset < growsize; offset += masksize) {
1135 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1136 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1139 fd_sets[i] = SvPVX(sv);
1143 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1144 /* Can't make just the (void*) conditional because that would be
1145 * cpp #if within cpp macro, and not all compilers like that. */
1146 nfound = PerlSock_select(
1148 (Select_fd_set_t) fd_sets[1],
1149 (Select_fd_set_t) fd_sets[2],
1150 (Select_fd_set_t) fd_sets[3],
1151 (void*) tbuf); /* Workaround for compiler bug. */
1153 nfound = PerlSock_select(
1155 (Select_fd_set_t) fd_sets[1],
1156 (Select_fd_set_t) fd_sets[2],
1157 (Select_fd_set_t) fd_sets[3],
1160 for (i = 1; i <= 3; i++) {
1163 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1165 for (offset = 0; offset < growsize; offset += masksize) {
1166 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1167 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1169 Safefree(fd_sets[i]);
1176 if (GIMME == G_ARRAY && tbuf) {
1177 value = (NV)(timebuf.tv_sec) +
1178 (NV)(timebuf.tv_usec) / 1000000.0;
1183 DIE(aTHX_ "select not implemented");
1188 =for apidoc setdefout
1190 Sets PL_defoutgv, the default file handle for output, to the passed in
1191 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1192 count of the passed in typeglob is increased by one, and the reference count
1193 of the typeglob that PL_defoutgv points to is decreased by one.
1199 Perl_setdefout(pTHX_ GV *gv)
1202 SvREFCNT_inc_simple_void(gv);
1203 SvREFCNT_dec(PL_defoutgv);
1211 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1212 GV * egv = GvEGVx(PL_defoutgv);
1216 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1218 XPUSHs(&PL_sv_undef);
1220 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1221 if (gvp && *gvp == egv) {
1222 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1226 mXPUSHs(newRV(MUTABLE_SV(egv)));
1231 if (!GvIO(newdefout))
1232 gv_IOadd(newdefout);
1233 setdefout(newdefout);
1243 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1248 if (gv && (io = GvIO(gv))) {
1249 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1251 const U32 gimme = GIMME_V;
1252 S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme);
1253 if (gimme == G_SCALAR) {
1255 SvSetMagicSV_nosteal(TARG, TOPs);
1260 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1261 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1263 SETERRNO(EBADF,RMS_IFI);
1267 sv_setpvs(TARG, " ");
1268 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1269 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1270 /* Find out how many bytes the char needs */
1271 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1274 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1275 SvCUR_set(TARG,1+len);
1284 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1287 register PERL_CONTEXT *cx;
1288 const I32 gimme = GIMME_V;
1290 PERL_ARGS_ASSERT_DOFORM;
1292 if (cv && CvCLONE(cv))
1293 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1298 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1299 PUSHFORMAT(cx, retop);
1301 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1303 setdefout(gv); /* locally select filehandle so $% et al work */
1322 gv = MUTABLE_GV(POPs);
1336 goto not_a_format_reference;
1341 tmpsv = sv_newmortal();
1342 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1343 name = SvPV_nolen_const(tmpsv);
1345 DIE(aTHX_ "Undefined format \"%s\" called", name);
1347 not_a_format_reference:
1348 DIE(aTHX_ "Not a format reference");
1350 IoFLAGS(io) &= ~IOf_DIDTOP;
1351 return doform(cv,gv,PL_op->op_next);
1357 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1358 register IO * const io = GvIOp(gv);
1363 register PERL_CONTEXT *cx;
1366 if (!io || !(ofp = IoOFP(io)))
1369 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1370 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1372 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1373 PL_formtarget != PL_toptarget)
1377 if (!IoTOP_GV(io)) {
1380 if (!IoTOP_NAME(io)) {
1382 if (!IoFMT_NAME(io))
1383 IoFMT_NAME(io) = savepv(GvNAME(gv));
1384 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1385 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1386 if ((topgv && GvFORM(topgv)) ||
1387 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1388 IoTOP_NAME(io) = savesvpv(topname);
1390 IoTOP_NAME(io) = savepvs("top");
1392 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1393 if (!topgv || !GvFORM(topgv)) {
1394 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1397 IoTOP_GV(io) = topgv;
1399 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1400 I32 lines = IoLINES_LEFT(io);
1401 const char *s = SvPVX_const(PL_formtarget);
1402 if (lines <= 0) /* Yow, header didn't even fit!!! */
1404 while (lines-- > 0) {
1405 s = strchr(s, '\n');
1411 const STRLEN save = SvCUR(PL_formtarget);
1412 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1413 do_print(PL_formtarget, ofp);
1414 SvCUR_set(PL_formtarget, save);
1415 sv_chop(PL_formtarget, s);
1416 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1419 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1420 do_print(PL_formfeed, ofp);
1421 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1423 PL_formtarget = PL_toptarget;
1424 IoFLAGS(io) |= IOf_DIDTOP;
1427 DIE(aTHX_ "bad top format reference");
1430 SV * const sv = sv_newmortal();
1432 gv_efullname4(sv, fgv, NULL, FALSE);
1433 name = SvPV_nolen_const(sv);
1435 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1437 DIE(aTHX_ "Undefined top format called");
1439 return doform(cv, gv, PL_op);
1443 POPBLOCK(cx,PL_curpm);
1445 retop = cx->blk_sub.retop;
1451 report_wrongway_fh(gv, '<');
1452 else if (ckWARN(WARN_CLOSED))
1457 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1458 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1460 if (!do_print(PL_formtarget, fp))
1463 FmLINES(PL_formtarget) = 0;
1464 SvCUR_set(PL_formtarget, 0);
1465 *SvEND(PL_formtarget) = '\0';
1466 if (IoFLAGS(io) & IOf_FLUSH)
1467 (void)PerlIO_flush(fp);
1472 PL_formtarget = PL_bodytarget;
1474 PERL_UNUSED_VAR(newsp);
1475 PERL_UNUSED_VAR(gimme);
1481 dVAR; dSP; dMARK; dORIGMARK;
1487 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1489 if (gv && (io = GvIO(gv))) {
1490 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1492 if (MARK == ORIGMARK) {
1495 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1499 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1502 call_method("PRINTF", G_SCALAR);
1509 if (!(io = GvIO(gv))) {
1511 SETERRNO(EBADF,RMS_IFI);
1514 else if (!(fp = IoOFP(io))) {
1516 report_wrongway_fh(gv, '<');
1517 else if (ckWARN(WARN_CLOSED))
1519 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1523 if (SvTAINTED(MARK[1]))
1524 TAINT_PROPER("printf");
1525 do_sprintf(sv, SP - MARK, MARK + 1);
1526 if (!do_print(sv, fp))
1529 if (IoFLAGS(io) & IOf_FLUSH)
1530 if (PerlIO_flush(fp) == EOF)
1541 PUSHs(&PL_sv_undef);
1549 const int perm = (MAXARG > 3) ? POPi : 0666;
1550 const int mode = POPi;
1551 SV * const sv = POPs;
1552 GV * const gv = MUTABLE_GV(POPs);
1555 /* Need TIEHANDLE method ? */
1556 const char * const tmps = SvPV_const(sv, len);
1557 /* FIXME? do_open should do const */
1558 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1559 IoLINES(GvIOp(gv)) = 0;
1563 PUSHs(&PL_sv_undef);
1570 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1576 Sock_size_t bufsize;
1584 bool charstart = FALSE;
1585 STRLEN charskip = 0;
1588 GV * const gv = MUTABLE_GV(*++MARK);
1589 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1590 && gv && (io = GvIO(gv)) )
1592 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1595 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1597 call_method("READ", G_SCALAR);
1607 sv_setpvs(bufsv, "");
1608 length = SvIVx(*++MARK);
1611 offset = SvIVx(*++MARK);
1615 if (!io || !IoIFP(io)) {
1617 SETERRNO(EBADF,RMS_IFI);
1620 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1621 buffer = SvPVutf8_force(bufsv, blen);
1622 /* UTF-8 may not have been set if they are all low bytes */
1627 buffer = SvPV_force(bufsv, blen);
1628 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1631 DIE(aTHX_ "Negative length");
1639 if (PL_op->op_type == OP_RECV) {
1640 char namebuf[MAXPATHLEN];
1641 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1642 bufsize = sizeof (struct sockaddr_in);
1644 bufsize = sizeof namebuf;
1646 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1650 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1651 /* 'offset' means 'flags' here */
1652 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1653 (struct sockaddr *)namebuf, &bufsize);
1656 /* MSG_TRUNC can give oversized count; quietly lose it */
1660 /* Bogus return without padding */
1661 bufsize = sizeof (struct sockaddr_in);
1663 SvCUR_set(bufsv, count);
1664 *SvEND(bufsv) = '\0';
1665 (void)SvPOK_only(bufsv);
1669 /* This should not be marked tainted if the fp is marked clean */
1670 if (!(IoFLAGS(io) & IOf_UNTAINT))
1671 SvTAINTED_on(bufsv);
1673 sv_setpvn(TARG, namebuf, bufsize);
1678 if (PL_op->op_type == OP_RECV)
1679 DIE(aTHX_ PL_no_sock_func, "recv");
1681 if (DO_UTF8(bufsv)) {
1682 /* offset adjust in characters not bytes */
1683 blen = sv_len_utf8(bufsv);
1686 if (-offset > (int)blen)
1687 DIE(aTHX_ "Offset outside string");
1690 if (DO_UTF8(bufsv)) {
1691 /* convert offset-as-chars to offset-as-bytes */
1692 if (offset >= (int)blen)
1693 offset += SvCUR(bufsv) - blen;
1695 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1698 bufsize = SvCUR(bufsv);
1699 /* Allocating length + offset + 1 isn't perfect in the case of reading
1700 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1702 (should be 2 * length + offset + 1, or possibly something longer if
1703 PL_encoding is true) */
1704 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1705 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1706 Zero(buffer+bufsize, offset-bufsize, char);
1708 buffer = buffer + offset;
1710 read_target = bufsv;
1712 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1713 concatenate it to the current buffer. */
1715 /* Truncate the existing buffer to the start of where we will be
1717 SvCUR_set(bufsv, offset);
1719 read_target = sv_newmortal();
1720 SvUPGRADE(read_target, SVt_PV);
1721 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1724 if (PL_op->op_type == OP_SYSREAD) {
1725 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1726 if (IoTYPE(io) == IoTYPE_SOCKET) {
1727 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1733 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1738 #ifdef HAS_SOCKET__bad_code_maybe
1739 if (IoTYPE(io) == IoTYPE_SOCKET) {
1740 char namebuf[MAXPATHLEN];
1741 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1742 bufsize = sizeof (struct sockaddr_in);
1744 bufsize = sizeof namebuf;
1746 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1747 (struct sockaddr *)namebuf, &bufsize);
1752 count = PerlIO_read(IoIFP(io), buffer, length);
1753 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1754 if (count == 0 && PerlIO_error(IoIFP(io)))
1758 if (IoTYPE(io) == IoTYPE_WRONLY)
1759 report_wrongway_fh(gv, '>');
1762 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1763 *SvEND(read_target) = '\0';
1764 (void)SvPOK_only(read_target);
1765 if (fp_utf8 && !IN_BYTES) {
1766 /* Look at utf8 we got back and count the characters */
1767 const char *bend = buffer + count;
1768 while (buffer < bend) {
1770 skip = UTF8SKIP(buffer);
1773 if (buffer - charskip + skip > bend) {
1774 /* partial character - try for rest of it */
1775 length = skip - (bend-buffer);
1776 offset = bend - SvPVX_const(bufsv);
1788 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1789 provided amount read (count) was what was requested (length)
1791 if (got < wanted && count == length) {
1792 length = wanted - got;
1793 offset = bend - SvPVX_const(bufsv);
1796 /* return value is character count */
1800 else if (buffer_utf8) {
1801 /* Let svcatsv upgrade the bytes we read in to utf8.
1802 The buffer is a mortal so will be freed soon. */
1803 sv_catsv_nomg(bufsv, read_target);
1806 /* This should not be marked tainted if the fp is marked clean */
1807 if (!(IoFLAGS(io) & IOf_UNTAINT))
1808 SvTAINTED_on(bufsv);
1820 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1826 STRLEN orig_blen_bytes;
1827 const int op_type = PL_op->op_type;
1831 GV *const gv = MUTABLE_GV(*++MARK);
1832 if (PL_op->op_type == OP_SYSWRITE
1833 && gv && (io = GvIO(gv))) {
1834 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1836 if (MARK == SP - 1) {
1838 mXPUSHi(sv_len(sv));
1843 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1845 call_method("WRITE", G_SCALAR);
1857 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1859 if (io && IoIFP(io))
1860 report_wrongway_fh(gv, '<');
1863 SETERRNO(EBADF,RMS_IFI);
1867 /* Do this first to trigger any overloading. */
1868 buffer = SvPV_const(bufsv, blen);
1869 orig_blen_bytes = blen;
1870 doing_utf8 = DO_UTF8(bufsv);
1872 if (PerlIO_isutf8(IoIFP(io))) {
1873 if (!SvUTF8(bufsv)) {
1874 /* We don't modify the original scalar. */
1875 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1876 buffer = (char *) tmpbuf;
1880 else if (doing_utf8) {
1881 STRLEN tmplen = blen;
1882 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1885 buffer = (char *) tmpbuf;
1889 assert((char *)result == buffer);
1890 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1894 if (op_type == OP_SYSWRITE) {
1895 Size_t length = 0; /* This length is in characters. */
1901 /* The SV is bytes, and we've had to upgrade it. */
1902 blen_chars = orig_blen_bytes;
1904 /* The SV really is UTF-8. */
1905 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1906 /* Don't call sv_len_utf8 again because it will call magic
1907 or overloading a second time, and we might get back a
1908 different result. */
1909 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1911 /* It's safe, and it may well be cached. */
1912 blen_chars = sv_len_utf8(bufsv);
1920 length = blen_chars;
1922 #if Size_t_size > IVSIZE
1923 length = (Size_t)SvNVx(*++MARK);
1925 length = (Size_t)SvIVx(*++MARK);
1927 if ((SSize_t)length < 0) {
1929 DIE(aTHX_ "Negative length");
1934 offset = SvIVx(*++MARK);
1936 if (-offset > (IV)blen_chars) {
1938 DIE(aTHX_ "Offset outside string");
1940 offset += blen_chars;
1941 } else if (offset > (IV)blen_chars) {
1943 DIE(aTHX_ "Offset outside string");
1947 if (length > blen_chars - offset)
1948 length = blen_chars - offset;
1950 /* Here we convert length from characters to bytes. */
1951 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1952 /* Either we had to convert the SV, or the SV is magical, or
1953 the SV has overloading, in which case we can't or mustn't
1954 or mustn't call it again. */
1956 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1957 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1959 /* It's a real UTF-8 SV, and it's not going to change under
1960 us. Take advantage of any cache. */
1962 I32 len_I32 = length;
1964 /* Convert the start and end character positions to bytes.
1965 Remember that the second argument to sv_pos_u2b is relative
1967 sv_pos_u2b(bufsv, &start, &len_I32);
1974 buffer = buffer+offset;
1976 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1977 if (IoTYPE(io) == IoTYPE_SOCKET) {
1978 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1984 /* See the note at doio.c:do_print about filesize limits. --jhi */
1985 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1991 const int flags = SvIVx(*++MARK);
1994 char * const sockbuf = SvPVx(*++MARK, mlen);
1995 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1996 flags, (struct sockaddr *)sockbuf, mlen);
2000 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2005 DIE(aTHX_ PL_no_sock_func, "send");
2012 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2015 #if Size_t_size > IVSIZE
2035 * in Perl 5.12 and later, the additional parameter is a bitmask:
2038 * 2 = eof() <- ARGV magic
2040 * I'll rely on the compiler's trace flow analysis to decide whether to
2041 * actually assign this out here, or punt it into the only block where it is
2042 * used. Doing it out here is DRY on the condition logic.
2047 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2053 if (PL_op->op_flags & OPf_SPECIAL) {
2054 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2058 gv = PL_last_in_gv; /* eof */
2066 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2067 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
2070 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2071 if (io && !IoIFP(io)) {
2072 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2074 IoFLAGS(io) &= ~IOf_START;
2075 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2077 sv_setpvs(GvSV(gv), "-");
2079 GvSV(gv) = newSVpvs("-");
2080 SvSETMAGIC(GvSV(gv));
2082 else if (!nextargv(gv))
2087 PUSHs(boolSV(do_eof(gv)));
2098 PL_last_in_gv = MUTABLE_GV(POPs);
2103 if (gv && (io = GvIO(gv))) {
2104 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2106 return tied_handle_method("TELL", SP, io, mg);
2111 SETERRNO(EBADF,RMS_IFI);
2116 #if LSEEKSIZE > IVSIZE
2117 PUSHn( do_tell(gv) );
2119 PUSHi( do_tell(gv) );
2127 const int whence = POPi;
2128 #if LSEEKSIZE > IVSIZE
2129 const Off_t offset = (Off_t)SvNVx(POPs);
2131 const Off_t offset = (Off_t)SvIVx(POPs);
2134 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2137 if (gv && (io = GvIO(gv))) {
2138 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2140 #if LSEEKSIZE > IVSIZE
2141 SV *const offset_sv = newSVnv((NV) offset);
2143 SV *const offset_sv = newSViv(offset);
2146 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
2151 if (PL_op->op_type == OP_SEEK)
2152 PUSHs(boolSV(do_seek(gv, offset, whence)));
2154 const Off_t sought = do_sysseek(gv, offset, whence);
2156 PUSHs(&PL_sv_undef);
2158 SV* const sv = sought ?
2159 #if LSEEKSIZE > IVSIZE
2164 : newSVpvn(zero_but_true, ZBTLEN);
2175 /* There seems to be no consensus on the length type of truncate()
2176 * and ftruncate(), both off_t and size_t have supporters. In
2177 * general one would think that when using large files, off_t is
2178 * at least as wide as size_t, so using an off_t should be okay. */
2179 /* XXX Configure probe for the length type of *truncate() needed XXX */
2182 #if Off_t_size > IVSIZE
2187 /* Checking for length < 0 is problematic as the type might or
2188 * might not be signed: if it is not, clever compilers will moan. */
2189 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2196 if (PL_op->op_flags & OPf_SPECIAL) {
2197 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2206 TAINT_PROPER("truncate");
2207 if (!(fp = IoIFP(io))) {
2213 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2215 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2222 SV * const sv = POPs;
2225 if (isGV_with_GP(sv)) {
2226 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2227 goto do_ftruncate_gv;
2229 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2230 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2231 goto do_ftruncate_gv;
2233 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2234 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2235 goto do_ftruncate_io;
2238 name = SvPV_nolen_const(sv);
2239 TAINT_PROPER("truncate");
2241 if (truncate(name, len) < 0)
2245 const int tmpfd = PerlLIO_open(name, O_RDWR);
2250 if (my_chsize(tmpfd, len) < 0)
2252 PerlLIO_close(tmpfd);
2261 SETERRNO(EBADF,RMS_IFI);
2269 SV * const argsv = POPs;
2270 const unsigned int func = POPu;
2271 const int optype = PL_op->op_type;
2272 GV * const gv = MUTABLE_GV(POPs);
2273 IO * const io = gv ? GvIOn(gv) : NULL;
2277 if (!io || !argsv || !IoIFP(io)) {
2279 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2283 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2286 s = SvPV_force(argsv, len);
2287 need = IOCPARM_LEN(func);
2289 s = Sv_Grow(argsv, need + 1);
2290 SvCUR_set(argsv, need);
2293 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2296 retval = SvIV(argsv);
2297 s = INT2PTR(char*,retval); /* ouch */
2300 TAINT_PROPER(PL_op_desc[optype]);
2302 if (optype == OP_IOCTL)
2304 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2306 DIE(aTHX_ "ioctl is not implemented");
2310 DIE(aTHX_ "fcntl is not implemented");
2312 #if defined(OS2) && defined(__EMX__)
2313 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2315 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2319 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2321 if (s[SvCUR(argsv)] != 17)
2322 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2324 s[SvCUR(argsv)] = 0; /* put our null back */
2325 SvSETMAGIC(argsv); /* Assume it has changed */
2334 PUSHp(zero_but_true, ZBTLEN);
2347 const int argtype = POPi;
2348 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2350 if (gv && (io = GvIO(gv)))
2356 /* XXX Looks to me like io is always NULL at this point */
2358 (void)PerlIO_flush(fp);
2359 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2364 SETERRNO(EBADF,RMS_IFI);
2369 DIE(aTHX_ PL_no_func, "flock()");
2379 const int protocol = POPi;
2380 const int type = POPi;
2381 const int domain = POPi;
2382 GV * const gv = MUTABLE_GV(POPs);
2383 register IO * const io = gv ? GvIOn(gv) : NULL;
2388 if (io && IoIFP(io))
2389 do_close(gv, FALSE);
2390 SETERRNO(EBADF,LIB_INVARG);
2395 do_close(gv, FALSE);
2397 TAINT_PROPER("socket");
2398 fd = PerlSock_socket(domain, type, protocol);
2401 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2402 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2403 IoTYPE(io) = IoTYPE_SOCKET;
2404 if (!IoIFP(io) || !IoOFP(io)) {
2405 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2406 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2407 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2410 #if defined(HAS_FCNTL) && defined(F_SETFD)
2411 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2415 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2420 DIE(aTHX_ PL_no_sock_func, "socket");
2426 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2428 const int protocol = POPi;
2429 const int type = POPi;
2430 const int domain = POPi;
2431 GV * const gv2 = MUTABLE_GV(POPs);
2432 GV * const gv1 = MUTABLE_GV(POPs);
2433 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2434 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2437 if (!gv1 || !gv2 || !io1 || !io2) {
2439 report_evil_fh(gv1);
2441 report_evil_fh(gv2);
2444 if (io1 && IoIFP(io1))
2445 do_close(gv1, FALSE);
2446 if (io2 && IoIFP(io2))
2447 do_close(gv2, FALSE);
2452 TAINT_PROPER("socketpair");
2453 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2455 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2456 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2457 IoTYPE(io1) = IoTYPE_SOCKET;
2458 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2459 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2460 IoTYPE(io2) = IoTYPE_SOCKET;
2461 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2462 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2463 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2464 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2465 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2466 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2467 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2470 #if defined(HAS_FCNTL) && defined(F_SETFD)
2471 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2472 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2477 DIE(aTHX_ PL_no_sock_func, "socketpair");
2485 SV * const addrsv = POPs;
2486 /* OK, so on what platform does bind modify addr? */
2488 GV * const gv = MUTABLE_GV(POPs);
2489 register IO * const io = GvIOn(gv);
2492 if (!io || !IoIFP(io))
2495 addr = SvPV_const(addrsv, len);
2496 TAINT_PROPER("bind");
2497 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2503 if (ckWARN(WARN_CLOSED))
2505 SETERRNO(EBADF,SS_IVCHAN);
2508 DIE(aTHX_ PL_no_sock_func, "bind");
2516 SV * const addrsv = POPs;
2517 GV * const gv = MUTABLE_GV(POPs);
2518 register IO * const io = GvIOn(gv);
2522 if (!io || !IoIFP(io))
2525 addr = SvPV_const(addrsv, len);
2526 TAINT_PROPER("connect");
2527 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2533 if (ckWARN(WARN_CLOSED))
2535 SETERRNO(EBADF,SS_IVCHAN);
2538 DIE(aTHX_ PL_no_sock_func, "connect");
2546 const int backlog = POPi;
2547 GV * const gv = MUTABLE_GV(POPs);
2548 register IO * const io = gv ? GvIOn(gv) : NULL;
2550 if (!gv || !io || !IoIFP(io))
2553 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2559 if (ckWARN(WARN_CLOSED))
2561 SETERRNO(EBADF,SS_IVCHAN);
2564 DIE(aTHX_ PL_no_sock_func, "listen");
2574 char namebuf[MAXPATHLEN];
2575 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2576 Sock_size_t len = sizeof (struct sockaddr_in);
2578 Sock_size_t len = sizeof namebuf;
2580 GV * const ggv = MUTABLE_GV(POPs);
2581 GV * const ngv = MUTABLE_GV(POPs);
2590 if (!gstio || !IoIFP(gstio))
2594 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2597 /* Some platforms indicate zero length when an AF_UNIX client is
2598 * not bound. Simulate a non-zero-length sockaddr structure in
2600 namebuf[0] = 0; /* sun_len */
2601 namebuf[1] = AF_UNIX; /* sun_family */
2609 do_close(ngv, FALSE);
2610 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2611 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2612 IoTYPE(nstio) = IoTYPE_SOCKET;
2613 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2614 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2615 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2616 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2619 #if defined(HAS_FCNTL) && defined(F_SETFD)
2620 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2624 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2625 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2627 #ifdef __SCO_VERSION__
2628 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2631 PUSHp(namebuf, len);
2635 if (ckWARN(WARN_CLOSED))
2636 report_evil_fh(ggv);
2637 SETERRNO(EBADF,SS_IVCHAN);
2643 DIE(aTHX_ PL_no_sock_func, "accept");
2651 const int how = POPi;
2652 GV * const gv = MUTABLE_GV(POPs);
2653 register IO * const io = GvIOn(gv);
2655 if (!io || !IoIFP(io))
2658 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2662 if (ckWARN(WARN_CLOSED))
2664 SETERRNO(EBADF,SS_IVCHAN);
2667 DIE(aTHX_ PL_no_sock_func, "shutdown");
2675 const int optype = PL_op->op_type;
2676 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2677 const unsigned int optname = (unsigned int) POPi;
2678 const unsigned int lvl = (unsigned int) POPi;
2679 GV * const gv = MUTABLE_GV(POPs);
2680 register IO * const io = GvIOn(gv);
2684 if (!io || !IoIFP(io))
2687 fd = PerlIO_fileno(IoIFP(io));
2691 (void)SvPOK_only(sv);
2695 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2702 #if defined(__SYMBIAN32__)
2703 # define SETSOCKOPT_OPTION_VALUE_T void *
2705 # define SETSOCKOPT_OPTION_VALUE_T const char *
2707 /* XXX TODO: We need to have a proper type (a Configure probe,
2708 * etc.) for what the C headers think of the third argument of
2709 * setsockopt(), the option_value read-only buffer: is it
2710 * a "char *", or a "void *", const or not. Some compilers
2711 * don't take kindly to e.g. assuming that "char *" implicitly
2712 * promotes to a "void *", or to explicitly promoting/demoting
2713 * consts to non/vice versa. The "const void *" is the SUS
2714 * definition, but that does not fly everywhere for the above
2716 SETSOCKOPT_OPTION_VALUE_T buf;
2720 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2724 aint = (int)SvIV(sv);
2725 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2728 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2737 if (ckWARN(WARN_CLOSED))
2739 SETERRNO(EBADF,SS_IVCHAN);
2744 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2752 const int optype = PL_op->op_type;
2753 GV * const gv = MUTABLE_GV(POPs);
2754 register IO * const io = GvIOn(gv);
2759 if (!io || !IoIFP(io))
2762 sv = sv_2mortal(newSV(257));
2763 (void)SvPOK_only(sv);
2767 fd = PerlIO_fileno(IoIFP(io));
2769 case OP_GETSOCKNAME:
2770 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2773 case OP_GETPEERNAME:
2774 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2776 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2778 static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
2779 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2780 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2781 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2782 sizeof(u_short) + sizeof(struct in_addr))) {
2789 #ifdef BOGUS_GETNAME_RETURN
2790 /* Interactive Unix, getpeername() and getsockname()
2791 does not return valid namelen */
2792 if (len == BOGUS_GETNAME_RETURN)
2793 len = sizeof(struct sockaddr);
2801 if (ckWARN(WARN_CLOSED))
2803 SETERRNO(EBADF,SS_IVCHAN);
2808 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2823 if (PL_op->op_flags & OPf_REF) {
2825 if (PL_op->op_type == OP_LSTAT) {
2826 if (gv != PL_defgv) {
2827 do_fstat_warning_check:
2828 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2829 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2830 } else if (PL_laststype != OP_LSTAT)
2831 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2835 if (gv != PL_defgv) {
2836 PL_laststype = OP_STAT;
2838 sv_setpvs(PL_statname, "");
2845 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2846 } else if (IoDIRP(io)) {
2848 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2850 PL_laststatval = -1;
2856 if (PL_laststatval < 0) {
2862 SV* const sv = POPs;
2863 if (isGV_with_GP(sv)) {
2864 gv = MUTABLE_GV(sv);
2866 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2867 gv = MUTABLE_GV(SvRV(sv));
2868 if (PL_op->op_type == OP_LSTAT)
2869 goto do_fstat_warning_check;
2871 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2872 io = MUTABLE_IO(SvRV(sv));
2873 if (PL_op->op_type == OP_LSTAT)
2874 goto do_fstat_warning_check;
2875 goto do_fstat_have_io;
2878 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2880 PL_laststype = PL_op->op_type;
2881 if (PL_op->op_type == OP_LSTAT)
2882 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2884 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2885 if (PL_laststatval < 0) {
2886 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2887 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2893 if (gimme != G_ARRAY) {
2894 if (gimme != G_VOID)
2895 XPUSHs(boolSV(max));
2901 mPUSHi(PL_statcache.st_dev);
2902 mPUSHi(PL_statcache.st_ino);
2903 mPUSHu(PL_statcache.st_mode);
2904 mPUSHu(PL_statcache.st_nlink);
2905 #if Uid_t_size > IVSIZE
2906 mPUSHn(PL_statcache.st_uid);
2908 # if Uid_t_sign <= 0
2909 mPUSHi(PL_statcache.st_uid);
2911 mPUSHu(PL_statcache.st_uid);
2914 #if Gid_t_size > IVSIZE
2915 mPUSHn(PL_statcache.st_gid);
2917 # if Gid_t_sign <= 0
2918 mPUSHi(PL_statcache.st_gid);
2920 mPUSHu(PL_statcache.st_gid);
2923 #ifdef USE_STAT_RDEV
2924 mPUSHi(PL_statcache.st_rdev);
2926 PUSHs(newSVpvs_flags("", SVs_TEMP));
2928 #if Off_t_size > IVSIZE
2929 mPUSHn(PL_statcache.st_size);
2931 mPUSHi(PL_statcache.st_size);
2934 mPUSHn(PL_statcache.st_atime);
2935 mPUSHn(PL_statcache.st_mtime);
2936 mPUSHn(PL_statcache.st_ctime);
2938 mPUSHi(PL_statcache.st_atime);
2939 mPUSHi(PL_statcache.st_mtime);
2940 mPUSHi(PL_statcache.st_ctime);
2942 #ifdef USE_STAT_BLOCKS
2943 mPUSHu(PL_statcache.st_blksize);
2944 mPUSHu(PL_statcache.st_blocks);
2946 PUSHs(newSVpvs_flags("", SVs_TEMP));
2947 PUSHs(newSVpvs_flags("", SVs_TEMP));
2953 #define tryAMAGICftest_MG(chr) STMT_START { \
2954 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2955 && S_try_amagic_ftest(aTHX_ chr)) \
2960 S_try_amagic_ftest(pTHX_ char chr) {
2963 SV* const arg = TOPs;
2968 if ((PL_op->op_flags & OPf_KIDS)
2971 const char tmpchr = chr;
2973 SV * const tmpsv = amagic_call(arg,
2974 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2975 ftest_amg, AMGf_unary);
2982 next = PL_op->op_next;
2983 if (next->op_type >= OP_FTRREAD &&
2984 next->op_type <= OP_FTBINARY &&
2985 next->op_private & OPpFT_STACKED
2988 /* leave the object alone */
3000 /* This macro is used by the stacked filetest operators :
3001 * if the previous filetest failed, short-circuit and pass its value.
3002 * Else, discard it from the stack and continue. --rgs
3004 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
3005 if (!SvTRUE(TOPs)) { RETURN; } \
3006 else { (void)POPs; PUTBACK; } \
3013 /* Not const, because things tweak this below. Not bool, because there's
3014 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3015 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3016 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3017 /* Giving some sort of initial value silences compilers. */
3019 int access_mode = R_OK;
3021 int access_mode = 0;
3024 /* access_mode is never used, but leaving use_access in makes the
3025 conditional compiling below much clearer. */
3028 Mode_t stat_mode = S_IRUSR;
3030 bool effective = FALSE;
3034 switch (PL_op->op_type) {
3035 case OP_FTRREAD: opchar = 'R'; break;
3036 case OP_FTRWRITE: opchar = 'W'; break;
3037 case OP_FTREXEC: opchar = 'X'; break;
3038 case OP_FTEREAD: opchar = 'r'; break;
3039 case OP_FTEWRITE: opchar = 'w'; break;
3040 case OP_FTEEXEC: opchar = 'x'; break;
3042 tryAMAGICftest_MG(opchar);
3044 STACKED_FTEST_CHECK;
3046 switch (PL_op->op_type) {
3048 #if !(defined(HAS_ACCESS) && defined(R_OK))
3054 #if defined(HAS_ACCESS) && defined(W_OK)
3059 stat_mode = S_IWUSR;
3063 #if defined(HAS_ACCESS) && defined(X_OK)
3068 stat_mode = S_IXUSR;
3072 #ifdef PERL_EFF_ACCESS
3075 stat_mode = S_IWUSR;
3079 #ifndef PERL_EFF_ACCESS
3086 #ifdef PERL_EFF_ACCESS
3091 stat_mode = S_IXUSR;
3097 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3098 const char *name = POPpx;
3100 # ifdef PERL_EFF_ACCESS
3101 result = PERL_EFF_ACCESS(name, access_mode);
3103 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3109 result = access(name, access_mode);
3111 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3122 result = my_stat_flags(0);
3126 if (cando(stat_mode, effective, &PL_statcache))
3135 const int op_type = PL_op->op_type;
3140 case OP_FTIS: opchar = 'e'; break;
3141 case OP_FTSIZE: opchar = 's'; break;
3142 case OP_FTMTIME: opchar = 'M'; break;
3143 case OP_FTCTIME: opchar = 'C'; break;
3144 case OP_FTATIME: opchar = 'A'; break;
3146 tryAMAGICftest_MG(opchar);
3148 STACKED_FTEST_CHECK;
3150 result = my_stat_flags(0);
3154 if (op_type == OP_FTIS)
3157 /* You can't dTARGET inside OP_FTIS, because you'll get
3158 "panic: pad_sv po" - the op is not flagged to have a target. */
3162 #if Off_t_size > IVSIZE
3163 PUSHn(PL_statcache.st_size);
3165 PUSHi(PL_statcache.st_size);
3169 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3172 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3175 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3189 switch (PL_op->op_type) {
3190 case OP_FTROWNED: opchar = 'O'; break;
3191 case OP_FTEOWNED: opchar = 'o'; break;
3192 case OP_FTZERO: opchar = 'z'; break;
3193 case OP_FTSOCK: opchar = 'S'; break;
3194 case OP_FTCHR: opchar = 'c'; break;
3195 case OP_FTBLK: opchar = 'b'; break;
3196 case OP_FTFILE: opchar = 'f'; break;
3197 case OP_FTDIR: opchar = 'd'; break;
3198 case OP_FTPIPE: opchar = 'p'; break;
3199 case OP_FTSUID: opchar = 'u'; break;
3200 case OP_FTSGID: opchar = 'g'; break;
3201 case OP_FTSVTX: opchar = 'k'; break;
3203 tryAMAGICftest_MG(opchar);
3205 STACKED_FTEST_CHECK;
3207 /* I believe that all these three are likely to be defined on most every
3208 system these days. */
3210 if(PL_op->op_type == OP_FTSUID) {
3211 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3217 if(PL_op->op_type == OP_FTSGID) {
3218 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3224 if(PL_op->op_type == OP_FTSVTX) {
3225 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3231 result = my_stat_flags(0);
3235 switch (PL_op->op_type) {
3237 if (PL_statcache.st_uid == PL_uid)
3241 if (PL_statcache.st_uid == PL_euid)
3245 if (PL_statcache.st_size == 0)
3249 if (S_ISSOCK(PL_statcache.st_mode))
3253 if (S_ISCHR(PL_statcache.st_mode))
3257 if (S_ISBLK(PL_statcache.st_mode))
3261 if (S_ISREG(PL_statcache.st_mode))
3265 if (S_ISDIR(PL_statcache.st_mode))
3269 if (S_ISFIFO(PL_statcache.st_mode))
3274 if (PL_statcache.st_mode & S_ISUID)
3280 if (PL_statcache.st_mode & S_ISGID)
3286 if (PL_statcache.st_mode & S_ISVTX)
3300 tryAMAGICftest_MG('l');
3301 result = my_lstat_flags(0);
3306 if (S_ISLNK(PL_statcache.st_mode))
3321 tryAMAGICftest_MG('t');
3323 STACKED_FTEST_CHECK;
3325 if (PL_op->op_flags & OPf_REF)
3327 else if (isGV_with_GP(TOPs))
3328 gv = MUTABLE_GV(POPs);
3329 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3330 gv = MUTABLE_GV(SvRV(POPs));
3333 name = SvPV_nomg(tmpsv, namelen);
3334 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3337 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3338 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3339 else if (tmpsv && SvOK(tmpsv)) {
3347 if (PerlLIO_isatty(fd))
3352 #if defined(atarist) /* this will work with atariST. Configure will
3353 make guesses for other systems. */
3354 # define FILE_base(f) ((f)->_base)
3355 # define FILE_ptr(f) ((f)->_ptr)
3356 # define FILE_cnt(f) ((f)->_cnt)
3357 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3368 register STDCHAR *s;
3374 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3376 STACKED_FTEST_CHECK;
3378 if (PL_op->op_flags & OPf_REF)
3380 else if (isGV_with_GP(TOPs))
3381 gv = MUTABLE_GV(POPs);
3382 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3383 gv = MUTABLE_GV(SvRV(POPs));
3389 if (gv == PL_defgv) {
3391 io = GvIO(PL_statgv);
3394 goto really_filename;
3399 PL_laststatval = -1;
3400 sv_setpvs(PL_statname, "");
3401 io = GvIO(PL_statgv);
3403 if (io && IoIFP(io)) {
3404 if (! PerlIO_has_base(IoIFP(io)))
3405 DIE(aTHX_ "-T and -B not implemented on filehandles");
3406 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3407 if (PL_laststatval < 0)
3409 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3410 if (PL_op->op_type == OP_FTTEXT)
3415 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3416 i = PerlIO_getc(IoIFP(io));
3418 (void)PerlIO_ungetc(IoIFP(io),i);
3420 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3422 len = PerlIO_get_bufsiz(IoIFP(io));
3423 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3424 /* sfio can have large buffers - limit to 512 */
3429 report_evil_fh(cGVOP_gv);
3430 SETERRNO(EBADF,RMS_IFI);
3438 PL_laststype = OP_STAT;
3439 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3440 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3441 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3443 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3446 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3447 if (PL_laststatval < 0) {
3448 (void)PerlIO_close(fp);
3451 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3452 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3453 (void)PerlIO_close(fp);
3455 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3456 RETPUSHNO; /* special case NFS directories */
3457 RETPUSHYES; /* null file is anything */
3462 /* now scan s to look for textiness */
3463 /* XXX ASCII dependent code */
3465 #if defined(DOSISH) || defined(USEMYBINMODE)
3466 /* ignore trailing ^Z on short files */
3467 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3471 for (i = 0; i < len; i++, s++) {
3472 if (!*s) { /* null never allowed in text */
3477 else if (!(isPRINT(*s) || isSPACE(*s)))
3480 else if (*s & 128) {
3482 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3485 /* utf8 characters don't count as odd */
3486 if (UTF8_IS_START(*s)) {
3487 int ulen = UTF8SKIP(s);
3488 if (ulen < len - i) {
3490 for (j = 1; j < ulen; j++) {
3491 if (!UTF8_IS_CONTINUATION(s[j]))
3494 --ulen; /* loop does extra increment */
3504 *s != '\n' && *s != '\r' && *s != '\b' &&
3505 *s != '\t' && *s != '\f' && *s != 27)
3510 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3521 const char *tmps = NULL;
3525 SV * const sv = POPs;
3526 if (PL_op->op_flags & OPf_SPECIAL) {
3527 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3529 else if (isGV_with_GP(sv)) {
3530 gv = MUTABLE_GV(sv);
3532 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3533 gv = MUTABLE_GV(SvRV(sv));
3536 tmps = SvPV_nolen_const(sv);
3540 if( !gv && (!tmps || !*tmps) ) {
3541 HV * const table = GvHVn(PL_envgv);
3544 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3545 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3547 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3552 deprecate("chdir('') or chdir(undef) as chdir()");
3553 tmps = SvPV_nolen_const(*svp);
3557 TAINT_PROPER("chdir");
3562 TAINT_PROPER("chdir");
3565 IO* const io = GvIO(gv);
3568 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3569 } else if (IoIFP(io)) {
3570 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3574 SETERRNO(EBADF, RMS_IFI);
3580 SETERRNO(EBADF,RMS_IFI);
3584 DIE(aTHX_ PL_no_func, "fchdir");
3588 PUSHi( PerlDir_chdir(tmps) >= 0 );
3590 /* Clear the DEFAULT element of ENV so we'll get the new value
3592 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3599 dVAR; dSP; dMARK; dTARGET;
3600 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3611 char * const tmps = POPpx;
3612 TAINT_PROPER("chroot");
3613 PUSHi( chroot(tmps) >= 0 );
3616 DIE(aTHX_ PL_no_func, "chroot");
3624 const char * const tmps2 = POPpconstx;
3625 const char * const tmps = SvPV_nolen_const(TOPs);
3626 TAINT_PROPER("rename");
3628 anum = PerlLIO_rename(tmps, tmps2);
3630 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3631 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3634 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3635 (void)UNLINK(tmps2);
3636 if (!(anum = link(tmps, tmps2)))
3637 anum = UNLINK(tmps);
3645 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3649 const int op_type = PL_op->op_type;
3653 if (op_type == OP_LINK)
3654 DIE(aTHX_ PL_no_func, "link");
3656 # ifndef HAS_SYMLINK
3657 if (op_type == OP_SYMLINK)
3658 DIE(aTHX_ PL_no_func, "symlink");
3662 const char * const tmps2 = POPpconstx;
3663 const char * const tmps = SvPV_nolen_const(TOPs);
3664 TAINT_PROPER(PL_op_desc[op_type]);
3666 # if defined(HAS_LINK)
3667 # if defined(HAS_SYMLINK)
3668 /* Both present - need to choose which. */
3669 (op_type == OP_LINK) ?
3670 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3672 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3673 PerlLIO_link(tmps, tmps2);
3676 # if defined(HAS_SYMLINK)
3677 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3678 symlink(tmps, tmps2);
3683 SETi( result >= 0 );
3690 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3701 char buf[MAXPATHLEN];
3704 #ifndef INCOMPLETE_TAINTS
3708 len = readlink(tmps, buf, sizeof(buf) - 1);
3715 RETSETUNDEF; /* just pretend it's a normal file */
3719 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3721 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3723 char * const save_filename = filename;
3728 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3730 PERL_ARGS_ASSERT_DOONELINER;
3732 Newx(cmdline, size, char);
3733 my_strlcpy(cmdline, cmd, size);
3734 my_strlcat(cmdline, " ", size);
3735 for (s = cmdline + strlen(cmdline); *filename; ) {
3739 if (s - cmdline < size)
3740 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3741 myfp = PerlProc_popen(cmdline, "r");
3745 SV * const tmpsv = sv_newmortal();
3746 /* Need to save/restore 'PL_rs' ?? */
3747 s = sv_gets(tmpsv, myfp, 0);
3748 (void)PerlProc_pclose(myfp);
3752 #ifdef HAS_SYS_ERRLIST
3757 /* you don't see this */
3758 const char * const errmsg =
3759 #ifdef HAS_SYS_ERRLIST
3767 if (instr(s, errmsg)) {
3774 #define EACCES EPERM
3776 if (instr(s, "cannot make"))
3777 SETERRNO(EEXIST,RMS_FEX);
3778 else if (instr(s, "existing file"))
3779 SETERRNO(EEXIST,RMS_FEX);
3780 else if (instr(s, "ile exists"))
3781 SETERRNO(EEXIST,RMS_FEX);
3782 else if (instr(s, "non-exist"))
3783 SETERRNO(ENOENT,RMS_FNF);
3784 else if (instr(s, "does not exist"))
3785 SETERRNO(ENOENT,RMS_FNF);
3786 else if (instr(s, "not empty"))
3787 SETERRNO(EBUSY,SS_DEVOFFLINE);
3788 else if (instr(s, "cannot access"))
3789 SETERRNO(EACCES,RMS_PRV);
3791 SETERRNO(EPERM,RMS_PRV);
3794 else { /* some mkdirs return no failure indication */
3795 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3796 if (PL_op->op_type == OP_RMDIR)
3801 SETERRNO(EACCES,RMS_PRV); /* a guess */
3810 /* This macro removes trailing slashes from a directory name.
3811 * Different operating and file systems take differently to
3812 * trailing slashes. According to POSIX 1003.1 1996 Edition
3813 * any number of trailing slashes should be allowed.
3814 * Thusly we snip them away so that even non-conforming
3815 * systems are happy.
3816 * We should probably do this "filtering" for all
3817 * the functions that expect (potentially) directory names:
3818 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3819 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3821 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3822 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3825 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3826 (tmps) = savepvn((tmps), (len)); \
3836 const int mode = (MAXARG > 1) ? POPi : 0777;
3838 TRIMSLASHES(tmps,len,copy);
3840 TAINT_PROPER("mkdir");
3842 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3846 SETi( dooneliner("mkdir", tmps) );
3847 oldumask = PerlLIO_umask(0);
3848 PerlLIO_umask(oldumask);
3849 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3864 TRIMSLASHES(tmps,len,copy);
3865 TAINT_PROPER("rmdir");
3867 SETi( PerlDir_rmdir(tmps) >= 0 );
3869 SETi( dooneliner("rmdir", tmps) );
3876 /* Directory calls. */
3880 #if defined(Direntry_t) && defined(HAS_READDIR)
3882 const char * const dirname = POPpconstx;
3883 GV * const gv = MUTABLE_GV(POPs);
3884 register IO * const io = GvIOn(gv);
3889 if ((IoIFP(io) || IoOFP(io)))
3890 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3891 "Opening filehandle %s also as a directory",
3894 PerlDir_close(IoDIRP(io));
3895 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3901 SETERRNO(EBADF,RMS_DIR);
3904 DIE(aTHX_ PL_no_dir_func, "opendir");
3910 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3911 DIE(aTHX_ PL_no_dir_func, "readdir");
3913 #if !defined(I_DIRENT) && !defined(VMS)
3914 Direntry_t *readdir (DIR *);
3920 const I32 gimme = GIMME;
3921 GV * const gv = MUTABLE_GV(POPs);
3922 register const Direntry_t *dp;
3923 register IO * const io = GvIOn(gv);
3925 if (!io || !IoDIRP(io)) {
3926 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3927 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3932 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3936 sv = newSVpvn(dp->d_name, dp->d_namlen);
3938 sv = newSVpv(dp->d_name, 0);
3940 #ifndef INCOMPLETE_TAINTS
3941 if (!(IoFLAGS(io) & IOf_UNTAINT))
3945 } while (gimme == G_ARRAY);
3947 if (!dp && gimme != G_ARRAY)
3954 SETERRNO(EBADF,RMS_ISI);
3955 if (GIMME == G_ARRAY)
3964 #if defined(HAS_TELLDIR) || defined(telldir)
3966 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3967 /* XXX netbsd still seemed to.
3968 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3969 --JHI 1999-Feb-02 */
3970 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3971 long telldir (DIR *);
3973 GV * const gv = MUTABLE_GV(POPs);
3974 register IO * const io = GvIOn(gv);
3976 if (!io || !IoDIRP(io)) {
3977 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3978 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3982 PUSHi( PerlDir_tell(IoDIRP(io)) );
3986 SETERRNO(EBADF,RMS_ISI);
3989 DIE(aTHX_ PL_no_dir_func, "telldir");
3995 #if defined(HAS_SEEKDIR) || defined(seekdir)
3997 const long along = POPl;
3998 GV * const gv = MUTABLE_GV(POPs);
3999 register IO * const io = GvIOn(gv);
4001 if (!io || !IoDIRP(io)) {
4002 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4003 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
4006 (void)PerlDir_seek(IoDIRP(io), along);
4011 SETERRNO(EBADF,RMS_ISI);
4014 DIE(aTHX_ PL_no_dir_func, "seekdir");
4020 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4022 GV * const gv = MUTABLE_GV(POPs);
4023 register IO * const io = GvIOn(gv);
4025 if (!io || !IoDIRP(io)) {
4026 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4027 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
4030 (void)PerlDir_rewind(IoDIRP(io));
4034 SETERRNO(EBADF,RMS_ISI);
4037 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4043 #if defined(Direntry_t) && defined(HAS_READDIR)
4045 GV * const gv = MUTABLE_GV(POPs);
4046 register IO * const io = GvIOn(gv);
4048 if (!io || !IoDIRP(io)) {
4049 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4050 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4053 #ifdef VOID_CLOSEDIR
4054 PerlDir_close(IoDIRP(io));
4056 if (PerlDir_close(IoDIRP(io)) < 0) {
4057 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4066 SETERRNO(EBADF,RMS_IFI);
4069 DIE(aTHX_ PL_no_dir_func, "closedir");
4073 /* Process control. */
4082 PERL_FLUSHALL_FOR_CHILD;
4083 childpid = PerlProc_fork();
4087 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4089 SvREADONLY_off(GvSV(tmpgv));
4090 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4091 SvREADONLY_on(GvSV(tmpgv));
4093 #ifdef THREADS_HAVE_PIDS
4094 PL_ppid = (IV)getppid();
4096 #ifdef PERL_USES_PL_PIDSTATUS
4097 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4103 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4108 PERL_FLUSHALL_FOR_CHILD;
4109 childpid = PerlProc_fork();
4115 DIE(aTHX_ PL_no_func, "fork");
4122 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4127 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4128 childpid = wait4pid(-1, &argflags, 0);
4130 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4135 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4136 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4137 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4139 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4144 DIE(aTHX_ PL_no_func, "wait");
4150 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4152 const int optype = POPi;
4153 const Pid_t pid = TOPi;
4157 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4158 result = wait4pid(pid, &argflags, optype);
4160 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4165 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4166 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4167 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4169 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4174 DIE(aTHX_ PL_no_func, "waitpid");
4180 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4181 #if defined(__LIBCATAMOUNT__)
4182 PL_statusvalue = -1;
4191 while (++MARK <= SP) {
4192 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4197 TAINT_PROPER("system");
4199 PERL_FLUSHALL_FOR_CHILD;
4200 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4206 if (PerlProc_pipe(pp) >= 0)
4208 while ((childpid = PerlProc_fork()) == -1) {
4209 if (errno != EAGAIN) {
4214 PerlLIO_close(pp[0]);
4215 PerlLIO_close(pp[1]);
4222 Sigsave_t ihand,qhand; /* place to save signals during system() */
4226 PerlLIO_close(pp[1]);
4228 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4229 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4232 result = wait4pid(childpid, &status, 0);
4233 } while (result == -1 && errno == EINTR);
4235 (void)rsignal_restore(SIGINT, &ihand);
4236 (void)rsignal_restore(SIGQUIT, &qhand);
4238 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4239 do_execfree(); /* free any memory child malloced on fork */
4246 while (n < sizeof(int)) {
4247 n1 = PerlLIO_read(pp[0],
4248 (void*)(((char*)&errkid)+n),
4254 PerlLIO_close(pp[0]);
4255 if (n) { /* Error */
4256 if (n != sizeof(int))
4257 DIE(aTHX_ "panic: kid popen errno read");
4258 errno = errkid; /* Propagate errno from kid */
4259 STATUS_NATIVE_CHILD_SET(-1);
4262 XPUSHi(STATUS_CURRENT);
4266 PerlLIO_close(pp[0]);
4267 #if defined(HAS_FCNTL) && defined(F_SETFD)
4268 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4271 if (PL_op->op_flags & OPf_STACKED) {
4272 SV * const really = *++MARK;
4273 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4275 else if (SP - MARK != 1)
4276 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4278 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4282 #else /* ! FORK or VMS or OS/2 */
4285 if (PL_op->op_flags & OPf_STACKED) {
4286 SV * const really = *++MARK;
4287 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4288 value = (I32)do_aspawn(really, MARK, SP);
4290 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4293 else if (SP - MARK != 1) {
4294 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4295 value = (I32)do_aspawn(NULL, MARK, SP);
4297 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4301 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4303 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4305 STATUS_NATIVE_CHILD_SET(value);
4308 XPUSHi(result ? value : STATUS_CURRENT);
4309 #endif /* !FORK or VMS or OS/2 */
4316 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4321 while (++MARK <= SP) {
4322 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4327 TAINT_PROPER("exec");
4329 PERL_FLUSHALL_FOR_CHILD;
4330 if (PL_op->op_flags & OPf_STACKED) {
4331 SV * const really = *++MARK;
4332 value = (I32)do_aexec(really, MARK, SP);
4334 else if (SP - MARK != 1)
4336 value = (I32)vms_do_aexec(NULL, MARK, SP);
4340 (void ) do_aspawn(NULL, MARK, SP);
4344 value = (I32)do_aexec(NULL, MARK, SP);
4349 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4352 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4355 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4369 # ifdef THREADS_HAVE_PIDS
4370 if (PL_ppid != 1 && getppid() == 1)
4371 /* maybe the parent process has died. Refresh ppid cache */
4375 XPUSHi( getppid() );
4379 DIE(aTHX_ PL_no_func, "getppid");
4388 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4391 pgrp = (I32)BSD_GETPGRP(pid);
4393 if (pid != 0 && pid != PerlProc_getpid())
4394 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4400 DIE(aTHX_ PL_no_func, "getpgrp()");
4420 TAINT_PROPER("setpgrp");
4422 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4424 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4425 || (pid != 0 && pid != PerlProc_getpid()))
4427 DIE(aTHX_ "setpgrp can't take arguments");
4429 SETi( setpgrp() >= 0 );
4430 #endif /* USE_BSDPGRP */
4433 DIE(aTHX_ PL_no_func, "setpgrp()");
4437 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4438 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4440 # define PRIORITY_WHICH_T(which) which
4445 #ifdef HAS_GETPRIORITY
4447 const int who = POPi;
4448 const int which = TOPi;
4449 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4452 DIE(aTHX_ PL_no_func, "getpriority()");
4458 #ifdef HAS_SETPRIORITY
4460 const int niceval = POPi;
4461 const int who = POPi;
4462 const int which = TOPi;
4463 TAINT_PROPER("setpriority");
4464 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4467 DIE(aTHX_ PL_no_func, "setpriority()");
4471 #undef PRIORITY_WHICH_T
4479 XPUSHn( time(NULL) );
4481 XPUSHi( time(NULL) );
4493 (void)PerlProc_times(&PL_timesbuf);
4495 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4496 /* struct tms, though same data */
4500 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4501 if (GIMME == G_ARRAY) {
4502 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4503 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4504 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4512 if (GIMME == G_ARRAY) {
4519 DIE(aTHX_ "times not implemented");
4521 #endif /* HAS_TIMES */
4524 /* The 32 bit int year limits the times we can represent to these
4525 boundaries with a few days wiggle room to account for time zone
4528 /* Sat Jan 3 00:00:00 -2147481748 */
4529 #define TIME_LOWER_BOUND -67768100567755200.0
4530 /* Sun Dec 29 12:00:00 2147483647 */
4531 #define TIME_UPPER_BOUND 67767976233316800.0
4540 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4541 static const char * const dayname[] =
4542 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4543 static const char * const monname[] =
4544 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4545 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4550 when = (Time64_T)now;
4553 NV input = Perl_floor(POPn);
4554 when = (Time64_T)input;
4555 if (when != input) {
4556 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4557 "%s(%.0" NVff ") too large", opname, input);
4561 if ( TIME_LOWER_BOUND > when ) {
4562 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4563 "%s(%.0" NVff ") too small", opname, when);
4566 else if( when > TIME_UPPER_BOUND ) {
4567 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4568 "%s(%.0" NVff ") too large", opname, when);
4572 if (PL_op->op_type == OP_LOCALTIME)
4573 err = S_localtime64_r(&when, &tmbuf);
4575 err = S_gmtime64_r(&when, &tmbuf);
4579 /* XXX %lld broken for quads */
4580 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4581 "%s(%.0" NVff ") failed", opname, when);
4584 if (GIMME != G_ARRAY) { /* scalar context */
4586 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4587 double year = (double)tmbuf.tm_year + 1900;
4594 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4595 dayname[tmbuf.tm_wday],
4596 monname[tmbuf.tm_mon],
4604 else { /* list context */
4610 mPUSHi(tmbuf.tm_sec);
4611 mPUSHi(tmbuf.tm_min);
4612 mPUSHi(tmbuf.tm_hour);
4613 mPUSHi(tmbuf.tm_mday);
4614 mPUSHi(tmbuf.tm_mon);
4615 mPUSHn(tmbuf.tm_year);
4616 mPUSHi(tmbuf.tm_wday);
4617 mPUSHi(tmbuf.tm_yday);
4618 mPUSHi(tmbuf.tm_isdst);
4629 anum = alarm((unsigned int)anum);
4635 DIE(aTHX_ PL_no_func, "alarm");
4646 (void)time(&lasttime);
4651 PerlProc_sleep((unsigned int)duration);
4654 XPUSHi(when - lasttime);
4658 /* Shared memory. */
4659 /* Merged with some message passing. */
4663 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4664 dVAR; dSP; dMARK; dTARGET;
4665 const int op_type = PL_op->op_type;
4670 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4673 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4676 value = (I32)(do_semop(MARK, SP) >= 0);
4679 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4695 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4696 dVAR; dSP; dMARK; dTARGET;
4697 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4704 DIE(aTHX_ "System V IPC is not implemented on this machine");
4710 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4711 dVAR; dSP; dMARK; dTARGET;
4712 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4720 PUSHp(zero_but_true, ZBTLEN);
4728 /* I can't const this further without getting warnings about the types of
4729 various arrays passed in from structures. */
4731 S_space_join_names_mortal(pTHX_ char *const *array)
4735 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4737 if (array && *array) {
4738 target = newSVpvs_flags("", SVs_TEMP);
4740 sv_catpv(target, *array);
4743 sv_catpvs(target, " ");
4746 target = sv_mortalcopy(&PL_sv_no);
4751 /* Get system info. */
4755 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4757 I32 which = PL_op->op_type;
4758 register char **elem;
4760 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4761 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4762 struct hostent *gethostbyname(Netdb_name_t);
4763 struct hostent *gethostent(void);
4765 struct hostent *hent = NULL;
4769 if (which == OP_GHBYNAME) {
4770 #ifdef HAS_GETHOSTBYNAME
4771 const char* const name = POPpbytex;
4772 hent = PerlSock_gethostbyname(name);
4774 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4777 else if (which == OP_GHBYADDR) {
4778 #ifdef HAS_GETHOSTBYADDR
4779 const int addrtype = POPi;
4780 SV * const addrsv = POPs;
4782 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4784 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4786 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4790 #ifdef HAS_GETHOSTENT
4791 hent = PerlSock_gethostent();
4793 DIE(aTHX_ PL_no_sock_func, "gethostent");
4796 #ifdef HOST_NOT_FOUND
4798 #ifdef USE_REENTRANT_API
4799 # ifdef USE_GETHOSTENT_ERRNO
4800 h_errno = PL_reentrant_buffer->_gethostent_errno;
4803 STATUS_UNIX_SET(h_errno);
4807 if (GIMME != G_ARRAY) {
4808 PUSHs(sv = sv_newmortal());
4810 if (which == OP_GHBYNAME) {
4812 sv_setpvn(sv, hent->h_addr, hent->h_length);
4815 sv_setpv(sv, (char*)hent->h_name);
4821 mPUSHs(newSVpv((char*)hent->h_name, 0));
4822 PUSHs(space_join_names_mortal(hent->h_aliases));
4823 mPUSHi(hent->h_addrtype);
4824 len = hent->h_length;
4827 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4828 mXPUSHp(*elem, len);
4832 mPUSHp(hent->h_addr, len);
4834 PUSHs(sv_mortalcopy(&PL_sv_no));
4839 DIE(aTHX_ PL_no_sock_func, "gethostent");
4845 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4847 I32 which = PL_op->op_type;
4849 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4850 struct netent *getnetbyaddr(Netdb_net_t, int);
4851 struct netent *getnetbyname(Netdb_name_t);
4852 struct netent *getnetent(void);
4854 struct netent *nent;
4856 if (which == OP_GNBYNAME){
4857 #ifdef HAS_GETNETBYNAME
4858 const char * const name = POPpbytex;
4859 nent = PerlSock_getnetbyname(name);
4861 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4864 else if (which == OP_GNBYADDR) {
4865 #ifdef HAS_GETNETBYADDR
4866 const int addrtype = POPi;
4867 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4868 nent = PerlSock_getnetbyaddr(addr, addrtype);
4870 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4874 #ifdef HAS_GETNETENT
4875 nent = PerlSock_getnetent();
4877 DIE(aTHX_ PL_no_sock_func, "getnetent");
4880 #ifdef HOST_NOT_FOUND
4882 #ifdef USE_REENTRANT_API
4883 # ifdef USE_GETNETENT_ERRNO
4884 h_errno = PL_reentrant_buffer->_getnetent_errno;
4887 STATUS_UNIX_SET(h_errno);
4892 if (GIMME != G_ARRAY) {
4893 PUSHs(sv = sv_newmortal());
4895 if (which == OP_GNBYNAME)
4896 sv_setiv(sv, (IV)nent->n_net);
4898 sv_setpv(sv, nent->n_name);
4904 mPUSHs(newSVpv(nent->n_name, 0));
4905 PUSHs(space_join_names_mortal(nent->n_aliases));
4906 mPUSHi(nent->n_addrtype);
4907 mPUSHi(nent->n_net);
4912 DIE(aTHX_ PL_no_sock_func, "getnetent");
4918 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4920 I32 which = PL_op->op_type;
4922 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4923 struct protoent *getprotobyname(Netdb_name_t);
4924 struct protoent *getprotobynumber(int);
4925 struct protoent *getprotoent(void);
4927 struct protoent *pent;
4929 if (which == OP_GPBYNAME) {
4930 #ifdef HAS_GETPROTOBYNAME
4931 const char* const name = POPpbytex;
4932 pent = PerlSock_getprotobyname(name);
4934 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4937 else if (which == OP_GPBYNUMBER) {
4938 #ifdef HAS_GETPROTOBYNUMBER
4939 const int number = POPi;
4940 pent = PerlSock_getprotobynumber(number);
4942 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4946 #ifdef HAS_GETPROTOENT
4947 pent = PerlSock_getprotoent();
4949 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4953 if (GIMME != G_ARRAY) {
4954 PUSHs(sv = sv_newmortal());
4956 if (which == OP_GPBYNAME)
4957 sv_setiv(sv, (IV)pent->p_proto);
4959 sv_setpv(sv, pent->p_name);
4965 mPUSHs(newSVpv(pent->p_name, 0));
4966 PUSHs(space_join_names_mortal(pent->p_aliases));
4967 mPUSHi(pent->p_proto);
4972 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4978 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4980 I32 which = PL_op->op_type;
4982 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4983 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4984 struct servent *getservbyport(int, Netdb_name_t);
4985 struct servent *getservent(void);
4987 struct servent *sent;
4989 if (which == OP_GSBYNAME) {
4990 #ifdef HAS_GETSERVBYNAME
4991 const char * const proto = POPpbytex;
4992 const char * const name = POPpbytex;
4993 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4995 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4998 else if (which == OP_GSBYPORT) {
4999 #ifdef HAS_GETSERVBYPORT
5000 const char * const proto = POPpbytex;
5001 unsigned short port = (unsigned short)POPu;
5003 port = PerlSock_htons(port);
5005 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5007 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5011 #ifdef HAS_GETSERVENT
5012 sent = PerlSock_getservent();
5014 DIE(aTHX_ PL_no_sock_func, "getservent");
5018 if (GIMME != G_ARRAY) {
5019 PUSHs(sv = sv_newmortal());
5021 if (which == OP_GSBYNAME) {
5023 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5025 sv_setiv(sv, (IV)(sent->s_port));
5029 sv_setpv(sv, sent->s_name);
5035 mPUSHs(newSVpv(sent->s_name, 0));
5036 PUSHs(space_join_names_mortal(sent->s_aliases));
5038 mPUSHi(PerlSock_ntohs(sent->s_port));
5040 mPUSHi(sent->s_port);
5042 mPUSHs(newSVpv(sent->s_proto, 0));
5047 DIE(aTHX_ PL_no_sock_func, "getservent");
5053 #ifdef HAS_SETHOSTENT