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.
702 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
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))) {
775 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
777 SETERRNO(EBADF,RMS_IFI);
784 const char *d = NULL;
787 d = SvPV_const(discp, len);
788 mode = mode_from_discipline(d, len);
789 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
790 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
791 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
812 const I32 markoff = MARK - PL_stack_base;
813 const char *methname;
814 int how = PERL_MAGIC_tied;
818 switch(SvTYPE(varsv)) {
820 methname = "TIEHASH";
821 HvEITER_set(MUTABLE_HV(varsv), 0);
824 methname = "TIEARRAY";
828 if (isGV_with_GP(varsv)) {
829 if (SvFAKE(varsv) && !(GvFLAGS(varsv) & GVf_TIEWARNED)) {
830 deprecate("tie on a handle without *");
831 GvFLAGS(varsv) |= GVf_TIEWARNED;
833 methname = "TIEHANDLE";
834 how = PERL_MAGIC_tiedscalar;
835 /* For tied filehandles, we apply tiedscalar magic to the IO
836 slot of the GP rather than the GV itself. AMS 20010812 */
838 GvIOp(varsv) = newIO();
839 varsv = MUTABLE_SV(GvIOp(varsv));
844 methname = "TIESCALAR";
845 how = PERL_MAGIC_tiedscalar;
849 if (sv_isobject(*MARK)) { /* Calls GET magic. */
850 ENTER_with_name("call_TIE");
851 PUSHSTACKi(PERLSI_MAGIC);
853 EXTEND(SP,(I32)items);
857 call_method(methname, G_SCALAR);
860 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
861 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
862 * wrong error message, and worse case, supreme action at a distance.
863 * (Sorry obfuscation writers. You're not going to be given this one.)
866 const char *name = SvPV_nomg_const(*MARK, len);
867 stash = gv_stashpvn(name, len, 0);
868 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
869 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
870 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
872 ENTER_with_name("call_TIE");
873 PUSHSTACKi(PERLSI_MAGIC);
875 EXTEND(SP,(I32)items);
879 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
885 if (sv_isobject(sv)) {
886 sv_unmagic(varsv, how);
887 /* Croak if a self-tie on an aggregate is attempted. */
888 if (varsv == SvRV(sv) &&
889 (SvTYPE(varsv) == SVt_PVAV ||
890 SvTYPE(varsv) == SVt_PVHV))
892 "Self-ties of arrays and hashes are not supported");
893 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
895 LEAVE_with_name("call_TIE");
896 SP = PL_stack_base + markoff;
906 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
907 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
909 if (isGV_with_GP(sv)) {
910 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
911 deprecate("untie on a handle without *");
912 GvFLAGS(sv) |= GVf_TIEWARNED;
914 if (!(sv = MUTABLE_SV(GvIOp(sv))))
918 if ((mg = SvTIED_mg(sv, how))) {
919 SV * const obj = SvRV(SvTIED_obj(sv, mg));
921 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
923 if (gv && isGV(gv) && (cv = GvCV(gv))) {
925 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
926 mXPUSHi(SvREFCNT(obj) - 1);
928 ENTER_with_name("call_UNTIE");
929 call_sv(MUTABLE_SV(cv), G_VOID);
930 LEAVE_with_name("call_UNTIE");
933 else if (mg && SvREFCNT(obj) > 1) {
934 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
935 "untie attempted while %"UVuf" inner references still exist",
936 (UV)SvREFCNT(obj) - 1 ) ;
940 sv_unmagic(sv, how) ;
950 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
951 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
953 if (isGV_with_GP(sv)) {
954 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
955 deprecate("tied on a handle without *");
956 GvFLAGS(sv) |= GVf_TIEWARNED;
958 if (!(sv = MUTABLE_SV(GvIOp(sv))))
962 if ((mg = SvTIED_mg(sv, how))) {
963 SV *osv = SvTIED_obj(sv, mg);
964 if (osv == mg->mg_obj)
965 osv = sv_mortalcopy(osv);
979 HV * const hv = MUTABLE_HV(POPs);
980 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
981 stash = gv_stashsv(sv, 0);
982 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
984 require_pv("AnyDBM_File.pm");
986 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
987 DIE(aTHX_ "No dbm on this machine");
997 mPUSHu(O_RDWR|O_CREAT);
1002 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1005 if (!sv_isobject(TOPs)) {
1013 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1017 if (sv_isobject(TOPs)) {
1018 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1019 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1036 struct timeval timebuf;
1037 struct timeval *tbuf = &timebuf;
1040 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1045 # if BYTEORDER & 0xf0000
1046 # define ORDERBYTE (0x88888888 - BYTEORDER)
1048 # define ORDERBYTE (0x4444 - BYTEORDER)
1054 for (i = 1; i <= 3; i++) {
1055 SV * const sv = SP[i];
1058 if (SvREADONLY(sv)) {
1060 sv_force_normal_flags(sv, 0);
1061 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1062 Perl_croak_no_modify(aTHX);
1065 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1066 SvPV_force_nolen(sv); /* force string conversion */
1073 /* little endians can use vecs directly */
1074 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1081 masksize = NFDBITS / NBBY;
1083 masksize = sizeof(long); /* documented int, everyone seems to use long */
1085 Zero(&fd_sets[0], 4, char*);
1088 # if SELECT_MIN_BITS == 1
1089 growsize = sizeof(fd_set);
1091 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1092 # undef SELECT_MIN_BITS
1093 # define SELECT_MIN_BITS __FD_SETSIZE
1095 /* If SELECT_MIN_BITS is greater than one we most probably will want
1096 * to align the sizes with SELECT_MIN_BITS/8 because for example
1097 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1098 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1099 * on (sets/tests/clears bits) is 32 bits. */
1100 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1108 timebuf.tv_sec = (long)value;
1109 value -= (NV)timebuf.tv_sec;
1110 timebuf.tv_usec = (long)(value * 1000000.0);
1115 for (i = 1; i <= 3; i++) {
1117 if (!SvOK(sv) || SvCUR(sv) == 0) {
1124 Sv_Grow(sv, growsize);
1128 while (++j <= growsize) {
1132 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1134 Newx(fd_sets[i], growsize, char);
1135 for (offset = 0; offset < growsize; offset += masksize) {
1136 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1137 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1140 fd_sets[i] = SvPVX(sv);
1144 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1145 /* Can't make just the (void*) conditional because that would be
1146 * cpp #if within cpp macro, and not all compilers like that. */
1147 nfound = PerlSock_select(
1149 (Select_fd_set_t) fd_sets[1],
1150 (Select_fd_set_t) fd_sets[2],
1151 (Select_fd_set_t) fd_sets[3],
1152 (void*) tbuf); /* Workaround for compiler bug. */
1154 nfound = PerlSock_select(
1156 (Select_fd_set_t) fd_sets[1],
1157 (Select_fd_set_t) fd_sets[2],
1158 (Select_fd_set_t) fd_sets[3],
1161 for (i = 1; i <= 3; i++) {
1164 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1166 for (offset = 0; offset < growsize; offset += masksize) {
1167 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1168 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1170 Safefree(fd_sets[i]);
1177 if (GIMME == G_ARRAY && tbuf) {
1178 value = (NV)(timebuf.tv_sec) +
1179 (NV)(timebuf.tv_usec) / 1000000.0;
1184 DIE(aTHX_ "select not implemented");
1189 =for apidoc setdefout
1191 Sets PL_defoutgv, the default file handle for output, to the passed in
1192 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1193 count of the passed in typeglob is increased by one, and the reference count
1194 of the typeglob that PL_defoutgv points to is decreased by one.
1200 Perl_setdefout(pTHX_ GV *gv)
1203 SvREFCNT_inc_simple_void(gv);
1204 SvREFCNT_dec(PL_defoutgv);
1212 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1213 GV * egv = GvEGVx(PL_defoutgv);
1217 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1219 XPUSHs(&PL_sv_undef);
1221 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1222 if (gvp && *gvp == egv) {
1223 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1227 mXPUSHs(newRV(MUTABLE_SV(egv)));
1232 if (!GvIO(newdefout))
1233 gv_IOadd(newdefout);
1234 setdefout(newdefout);
1244 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1249 if (gv && (io = GvIO(gv))) {
1250 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1252 const U32 gimme = GIMME_V;
1253 S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme);
1254 if (gimme == G_SCALAR) {
1256 SvSetMagicSV_nosteal(TARG, TOPs);
1261 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1262 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1263 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1265 SETERRNO(EBADF,RMS_IFI);
1269 sv_setpvs(TARG, " ");
1270 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1271 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1272 /* Find out how many bytes the char needs */
1273 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1276 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1277 SvCUR_set(TARG,1+len);
1286 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1289 register PERL_CONTEXT *cx;
1290 const I32 gimme = GIMME_V;
1292 PERL_ARGS_ASSERT_DOFORM;
1294 if (cv && CvCLONE(cv))
1295 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1300 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1301 PUSHFORMAT(cx, retop);
1303 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1305 setdefout(gv); /* locally select filehandle so $% et al work */
1324 gv = MUTABLE_GV(POPs);
1338 goto not_a_format_reference;
1343 tmpsv = sv_newmortal();
1344 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1345 name = SvPV_nolen_const(tmpsv);
1347 DIE(aTHX_ "Undefined format \"%s\" called", name);
1349 not_a_format_reference:
1350 DIE(aTHX_ "Not a format reference");
1352 IoFLAGS(io) &= ~IOf_DIDTOP;
1353 return doform(cv,gv,PL_op->op_next);
1359 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1360 register IO * const io = GvIOp(gv);
1365 register PERL_CONTEXT *cx;
1368 if (!io || !(ofp = IoOFP(io)))
1371 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1372 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1374 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1375 PL_formtarget != PL_toptarget)
1379 if (!IoTOP_GV(io)) {
1382 if (!IoTOP_NAME(io)) {
1384 if (!IoFMT_NAME(io))
1385 IoFMT_NAME(io) = savepv(GvNAME(gv));
1386 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1387 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1388 if ((topgv && GvFORM(topgv)) ||
1389 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1390 IoTOP_NAME(io) = savesvpv(topname);
1392 IoTOP_NAME(io) = savepvs("top");
1394 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1395 if (!topgv || !GvFORM(topgv)) {
1396 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1399 IoTOP_GV(io) = topgv;
1401 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1402 I32 lines = IoLINES_LEFT(io);
1403 const char *s = SvPVX_const(PL_formtarget);
1404 if (lines <= 0) /* Yow, header didn't even fit!!! */
1406 while (lines-- > 0) {
1407 s = strchr(s, '\n');
1413 const STRLEN save = SvCUR(PL_formtarget);
1414 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1415 do_print(PL_formtarget, ofp);
1416 SvCUR_set(PL_formtarget, save);
1417 sv_chop(PL_formtarget, s);
1418 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1421 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1422 do_print(PL_formfeed, ofp);
1423 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1425 PL_formtarget = PL_toptarget;
1426 IoFLAGS(io) |= IOf_DIDTOP;
1429 DIE(aTHX_ "bad top format reference");
1432 SV * const sv = sv_newmortal();
1434 gv_efullname4(sv, fgv, NULL, FALSE);
1435 name = SvPV_nolen_const(sv);
1437 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1439 DIE(aTHX_ "Undefined top format called");
1441 return doform(cv, gv, PL_op);
1445 POPBLOCK(cx,PL_curpm);
1447 retop = cx->blk_sub.retop;
1452 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1454 report_wrongway_fh(gv, '<');
1455 else if (ckWARN(WARN_CLOSED))
1461 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1462 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1464 if (!do_print(PL_formtarget, fp))
1467 FmLINES(PL_formtarget) = 0;
1468 SvCUR_set(PL_formtarget, 0);
1469 *SvEND(PL_formtarget) = '\0';
1470 if (IoFLAGS(io) & IOf_FLUSH)
1471 (void)PerlIO_flush(fp);
1476 PL_formtarget = PL_bodytarget;
1478 PERL_UNUSED_VAR(newsp);
1479 PERL_UNUSED_VAR(gimme);
1485 dVAR; dSP; dMARK; dORIGMARK;
1491 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1493 if (gv && (io = GvIO(gv))) {
1494 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1496 if (MARK == ORIGMARK) {
1499 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1503 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1506 call_method("PRINTF", G_SCALAR);
1513 if (!(io = GvIO(gv))) {
1514 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1516 SETERRNO(EBADF,RMS_IFI);
1519 else if (!(fp = IoOFP(io))) {
1520 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1522 report_wrongway_fh(gv, '<');
1523 else if (ckWARN(WARN_CLOSED))
1526 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1530 if (SvTAINTED(MARK[1]))
1531 TAINT_PROPER("printf");
1532 do_sprintf(sv, SP - MARK, MARK + 1);
1533 if (!do_print(sv, fp))
1536 if (IoFLAGS(io) & IOf_FLUSH)
1537 if (PerlIO_flush(fp) == EOF)
1548 PUSHs(&PL_sv_undef);
1556 const int perm = (MAXARG > 3) ? POPi : 0666;
1557 const int mode = POPi;
1558 SV * const sv = POPs;
1559 GV * const gv = MUTABLE_GV(POPs);
1562 /* Need TIEHANDLE method ? */
1563 const char * const tmps = SvPV_const(sv, len);
1564 /* FIXME? do_open should do const */
1565 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1566 IoLINES(GvIOp(gv)) = 0;
1570 PUSHs(&PL_sv_undef);
1577 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1583 Sock_size_t bufsize;
1591 bool charstart = FALSE;
1592 STRLEN charskip = 0;
1595 GV * const gv = MUTABLE_GV(*++MARK);
1596 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1597 && gv && (io = GvIO(gv)) )
1599 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1602 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1604 call_method("READ", G_SCALAR);
1614 sv_setpvs(bufsv, "");
1615 length = SvIVx(*++MARK);
1618 offset = SvIVx(*++MARK);
1622 if (!io || !IoIFP(io)) {
1623 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1625 SETERRNO(EBADF,RMS_IFI);
1628 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1629 buffer = SvPVutf8_force(bufsv, blen);
1630 /* UTF-8 may not have been set if they are all low bytes */
1635 buffer = SvPV_force(bufsv, blen);
1636 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1639 DIE(aTHX_ "Negative length");
1647 if (PL_op->op_type == OP_RECV) {
1648 char namebuf[MAXPATHLEN];
1649 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1650 bufsize = sizeof (struct sockaddr_in);
1652 bufsize = sizeof namebuf;
1654 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1658 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1659 /* 'offset' means 'flags' here */
1660 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1661 (struct sockaddr *)namebuf, &bufsize);
1664 /* MSG_TRUNC can give oversized count; quietly lose it */
1668 /* Bogus return without padding */
1669 bufsize = sizeof (struct sockaddr_in);
1671 SvCUR_set(bufsv, count);
1672 *SvEND(bufsv) = '\0';
1673 (void)SvPOK_only(bufsv);
1677 /* This should not be marked tainted if the fp is marked clean */
1678 if (!(IoFLAGS(io) & IOf_UNTAINT))
1679 SvTAINTED_on(bufsv);
1681 sv_setpvn(TARG, namebuf, bufsize);
1686 if (PL_op->op_type == OP_RECV)
1687 DIE(aTHX_ PL_no_sock_func, "recv");
1689 if (DO_UTF8(bufsv)) {
1690 /* offset adjust in characters not bytes */
1691 blen = sv_len_utf8(bufsv);
1694 if (-offset > (int)blen)
1695 DIE(aTHX_ "Offset outside string");
1698 if (DO_UTF8(bufsv)) {
1699 /* convert offset-as-chars to offset-as-bytes */
1700 if (offset >= (int)blen)
1701 offset += SvCUR(bufsv) - blen;
1703 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1706 bufsize = SvCUR(bufsv);
1707 /* Allocating length + offset + 1 isn't perfect in the case of reading
1708 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1710 (should be 2 * length + offset + 1, or possibly something longer if
1711 PL_encoding is true) */
1712 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1713 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1714 Zero(buffer+bufsize, offset-bufsize, char);
1716 buffer = buffer + offset;
1718 read_target = bufsv;
1720 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1721 concatenate it to the current buffer. */
1723 /* Truncate the existing buffer to the start of where we will be
1725 SvCUR_set(bufsv, offset);
1727 read_target = sv_newmortal();
1728 SvUPGRADE(read_target, SVt_PV);
1729 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1732 if (PL_op->op_type == OP_SYSREAD) {
1733 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1734 if (IoTYPE(io) == IoTYPE_SOCKET) {
1735 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1741 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1746 #ifdef HAS_SOCKET__bad_code_maybe
1747 if (IoTYPE(io) == IoTYPE_SOCKET) {
1748 char namebuf[MAXPATHLEN];
1749 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1750 bufsize = sizeof (struct sockaddr_in);
1752 bufsize = sizeof namebuf;
1754 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1755 (struct sockaddr *)namebuf, &bufsize);
1760 count = PerlIO_read(IoIFP(io), buffer, length);
1761 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1762 if (count == 0 && PerlIO_error(IoIFP(io)))
1766 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1767 report_wrongway_fh(gv, '>');
1770 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1771 *SvEND(read_target) = '\0';
1772 (void)SvPOK_only(read_target);
1773 if (fp_utf8 && !IN_BYTES) {
1774 /* Look at utf8 we got back and count the characters */
1775 const char *bend = buffer + count;
1776 while (buffer < bend) {
1778 skip = UTF8SKIP(buffer);
1781 if (buffer - charskip + skip > bend) {
1782 /* partial character - try for rest of it */
1783 length = skip - (bend-buffer);
1784 offset = bend - SvPVX_const(bufsv);
1796 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1797 provided amount read (count) was what was requested (length)
1799 if (got < wanted && count == length) {
1800 length = wanted - got;
1801 offset = bend - SvPVX_const(bufsv);
1804 /* return value is character count */
1808 else if (buffer_utf8) {
1809 /* Let svcatsv upgrade the bytes we read in to utf8.
1810 The buffer is a mortal so will be freed soon. */
1811 sv_catsv_nomg(bufsv, read_target);
1814 /* This should not be marked tainted if the fp is marked clean */
1815 if (!(IoFLAGS(io) & IOf_UNTAINT))
1816 SvTAINTED_on(bufsv);
1828 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1834 STRLEN orig_blen_bytes;
1835 const int op_type = PL_op->op_type;
1839 GV *const gv = MUTABLE_GV(*++MARK);
1840 if (PL_op->op_type == OP_SYSWRITE
1841 && gv && (io = GvIO(gv))) {
1842 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1844 if (MARK == SP - 1) {
1846 mXPUSHi(sv_len(sv));
1851 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1853 call_method("WRITE", G_SCALAR);
1865 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1867 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1868 if (io && IoIFP(io))
1869 report_wrongway_fh(gv, '<');
1873 SETERRNO(EBADF,RMS_IFI);
1877 /* Do this first to trigger any overloading. */
1878 buffer = SvPV_const(bufsv, blen);
1879 orig_blen_bytes = blen;
1880 doing_utf8 = DO_UTF8(bufsv);
1882 if (PerlIO_isutf8(IoIFP(io))) {
1883 if (!SvUTF8(bufsv)) {
1884 /* We don't modify the original scalar. */
1885 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1886 buffer = (char *) tmpbuf;
1890 else if (doing_utf8) {
1891 STRLEN tmplen = blen;
1892 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1895 buffer = (char *) tmpbuf;
1899 assert((char *)result == buffer);
1900 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1904 if (op_type == OP_SYSWRITE) {
1905 Size_t length = 0; /* This length is in characters. */
1911 /* The SV is bytes, and we've had to upgrade it. */
1912 blen_chars = orig_blen_bytes;
1914 /* The SV really is UTF-8. */
1915 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1916 /* Don't call sv_len_utf8 again because it will call magic
1917 or overloading a second time, and we might get back a
1918 different result. */
1919 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1921 /* It's safe, and it may well be cached. */
1922 blen_chars = sv_len_utf8(bufsv);
1930 length = blen_chars;
1932 #if Size_t_size > IVSIZE
1933 length = (Size_t)SvNVx(*++MARK);
1935 length = (Size_t)SvIVx(*++MARK);
1937 if ((SSize_t)length < 0) {
1939 DIE(aTHX_ "Negative length");
1944 offset = SvIVx(*++MARK);
1946 if (-offset > (IV)blen_chars) {
1948 DIE(aTHX_ "Offset outside string");
1950 offset += blen_chars;
1951 } else if (offset > (IV)blen_chars) {
1953 DIE(aTHX_ "Offset outside string");
1957 if (length > blen_chars - offset)
1958 length = blen_chars - offset;
1960 /* Here we convert length from characters to bytes. */
1961 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1962 /* Either we had to convert the SV, or the SV is magical, or
1963 the SV has overloading, in which case we can't or mustn't
1964 or mustn't call it again. */
1966 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1967 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1969 /* It's a real UTF-8 SV, and it's not going to change under
1970 us. Take advantage of any cache. */
1972 I32 len_I32 = length;
1974 /* Convert the start and end character positions to bytes.
1975 Remember that the second argument to sv_pos_u2b is relative
1977 sv_pos_u2b(bufsv, &start, &len_I32);
1984 buffer = buffer+offset;
1986 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1987 if (IoTYPE(io) == IoTYPE_SOCKET) {
1988 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1994 /* See the note at doio.c:do_print about filesize limits. --jhi */
1995 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
2001 const int flags = SvIVx(*++MARK);
2004 char * const sockbuf = SvPVx(*++MARK, mlen);
2005 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
2006 flags, (struct sockaddr *)sockbuf, mlen);
2010 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2015 DIE(aTHX_ PL_no_sock_func, "send");
2022 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2025 #if Size_t_size > IVSIZE
2045 * in Perl 5.12 and later, the additional parameter is a bitmask:
2048 * 2 = eof() <- ARGV magic
2050 * I'll rely on the compiler's trace flow analysis to decide whether to
2051 * actually assign this out here, or punt it into the only block where it is
2052 * used. Doing it out here is DRY on the condition logic.
2057 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2063 if (PL_op->op_flags & OPf_SPECIAL) {
2064 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2068 gv = PL_last_in_gv; /* eof */
2076 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2077 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
2080 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2081 if (io && !IoIFP(io)) {
2082 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2084 IoFLAGS(io) &= ~IOf_START;
2085 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2087 sv_setpvs(GvSV(gv), "-");
2089 GvSV(gv) = newSVpvs("-");
2090 SvSETMAGIC(GvSV(gv));
2092 else if (!nextargv(gv))
2097 PUSHs(boolSV(do_eof(gv)));
2108 PL_last_in_gv = MUTABLE_GV(POPs);
2113 if (gv && (io = GvIO(gv))) {
2114 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2116 return tied_handle_method("TELL", SP, io, mg);
2121 SETERRNO(EBADF,RMS_IFI);
2126 #if LSEEKSIZE > IVSIZE
2127 PUSHn( do_tell(gv) );
2129 PUSHi( do_tell(gv) );
2137 const int whence = POPi;
2138 #if LSEEKSIZE > IVSIZE
2139 const Off_t offset = (Off_t)SvNVx(POPs);
2141 const Off_t offset = (Off_t)SvIVx(POPs);
2144 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2147 if (gv && (io = GvIO(gv))) {
2148 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2150 #if LSEEKSIZE > IVSIZE
2151 SV *const offset_sv = newSVnv((NV) offset);
2153 SV *const offset_sv = newSViv(offset);
2156 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
2161 if (PL_op->op_type == OP_SEEK)
2162 PUSHs(boolSV(do_seek(gv, offset, whence)));
2164 const Off_t sought = do_sysseek(gv, offset, whence);
2166 PUSHs(&PL_sv_undef);
2168 SV* const sv = sought ?
2169 #if LSEEKSIZE > IVSIZE
2174 : newSVpvn(zero_but_true, ZBTLEN);
2185 /* There seems to be no consensus on the length type of truncate()
2186 * and ftruncate(), both off_t and size_t have supporters. In
2187 * general one would think that when using large files, off_t is
2188 * at least as wide as size_t, so using an off_t should be okay. */
2189 /* XXX Configure probe for the length type of *truncate() needed XXX */
2192 #if Off_t_size > IVSIZE
2197 /* Checking for length < 0 is problematic as the type might or
2198 * might not be signed: if it is not, clever compilers will moan. */
2199 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2206 if (PL_op->op_flags & OPf_SPECIAL) {
2207 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2216 TAINT_PROPER("truncate");
2217 if (!(fp = IoIFP(io))) {
2223 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2225 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2232 SV * const sv = POPs;
2235 if (isGV_with_GP(sv)) {
2236 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2237 goto do_ftruncate_gv;
2239 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2240 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2241 goto do_ftruncate_gv;
2243 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2244 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2245 goto do_ftruncate_io;
2248 name = SvPV_nolen_const(sv);
2249 TAINT_PROPER("truncate");
2251 if (truncate(name, len) < 0)
2255 const int tmpfd = PerlLIO_open(name, O_RDWR);
2260 if (my_chsize(tmpfd, len) < 0)
2262 PerlLIO_close(tmpfd);
2271 SETERRNO(EBADF,RMS_IFI);
2279 SV * const argsv = POPs;
2280 const unsigned int func = POPu;
2281 const int optype = PL_op->op_type;
2282 GV * const gv = MUTABLE_GV(POPs);
2283 IO * const io = gv ? GvIOn(gv) : NULL;
2287 if (!io || !argsv || !IoIFP(io)) {
2288 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2290 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2294 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2297 s = SvPV_force(argsv, len);
2298 need = IOCPARM_LEN(func);
2300 s = Sv_Grow(argsv, need + 1);
2301 SvCUR_set(argsv, need);
2304 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2307 retval = SvIV(argsv);
2308 s = INT2PTR(char*,retval); /* ouch */
2311 TAINT_PROPER(PL_op_desc[optype]);
2313 if (optype == OP_IOCTL)
2315 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2317 DIE(aTHX_ "ioctl is not implemented");
2321 DIE(aTHX_ "fcntl is not implemented");
2323 #if defined(OS2) && defined(__EMX__)
2324 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2326 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2330 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2332 if (s[SvCUR(argsv)] != 17)
2333 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2335 s[SvCUR(argsv)] = 0; /* put our null back */
2336 SvSETMAGIC(argsv); /* Assume it has changed */
2345 PUSHp(zero_but_true, ZBTLEN);
2358 const int argtype = POPi;
2359 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2361 if (gv && (io = GvIO(gv)))
2367 /* XXX Looks to me like io is always NULL at this point */
2369 (void)PerlIO_flush(fp);
2370 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2373 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2376 SETERRNO(EBADF,RMS_IFI);
2381 DIE(aTHX_ PL_no_func, "flock()");
2391 const int protocol = POPi;
2392 const int type = POPi;
2393 const int domain = POPi;
2394 GV * const gv = MUTABLE_GV(POPs);
2395 register IO * const io = gv ? GvIOn(gv) : NULL;
2399 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2401 if (io && IoIFP(io))
2402 do_close(gv, FALSE);
2403 SETERRNO(EBADF,LIB_INVARG);
2408 do_close(gv, FALSE);
2410 TAINT_PROPER("socket");
2411 fd = PerlSock_socket(domain, type, protocol);
2414 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2415 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2416 IoTYPE(io) = IoTYPE_SOCKET;
2417 if (!IoIFP(io) || !IoOFP(io)) {
2418 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2419 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2420 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2423 #if defined(HAS_FCNTL) && defined(F_SETFD)
2424 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2428 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2433 DIE(aTHX_ PL_no_sock_func, "socket");
2439 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2441 const int protocol = POPi;
2442 const int type = POPi;
2443 const int domain = POPi;
2444 GV * const gv2 = MUTABLE_GV(POPs);
2445 GV * const gv1 = MUTABLE_GV(POPs);
2446 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2447 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2450 if (!gv1 || !gv2 || !io1 || !io2) {
2451 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2453 report_evil_fh(gv1);
2455 report_evil_fh(gv2);
2459 if (io1 && IoIFP(io1))
2460 do_close(gv1, FALSE);
2461 if (io2 && IoIFP(io2))
2462 do_close(gv2, FALSE);
2467 TAINT_PROPER("socketpair");
2468 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2470 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2471 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2472 IoTYPE(io1) = IoTYPE_SOCKET;
2473 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2474 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2475 IoTYPE(io2) = IoTYPE_SOCKET;
2476 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2477 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2478 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2479 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2480 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2481 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2482 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2485 #if defined(HAS_FCNTL) && defined(F_SETFD)
2486 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2487 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2492 DIE(aTHX_ PL_no_sock_func, "socketpair");
2500 SV * const addrsv = POPs;
2501 /* OK, so on what platform does bind modify addr? */
2503 GV * const gv = MUTABLE_GV(POPs);
2504 register IO * const io = GvIOn(gv);
2507 if (!io || !IoIFP(io))
2510 addr = SvPV_const(addrsv, len);
2511 TAINT_PROPER("bind");
2512 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2518 if (ckWARN(WARN_CLOSED))
2520 SETERRNO(EBADF,SS_IVCHAN);
2523 DIE(aTHX_ PL_no_sock_func, "bind");
2531 SV * const addrsv = POPs;
2532 GV * const gv = MUTABLE_GV(POPs);
2533 register IO * const io = GvIOn(gv);
2537 if (!io || !IoIFP(io))
2540 addr = SvPV_const(addrsv, len);
2541 TAINT_PROPER("connect");
2542 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2548 if (ckWARN(WARN_CLOSED))
2550 SETERRNO(EBADF,SS_IVCHAN);
2553 DIE(aTHX_ PL_no_sock_func, "connect");
2561 const int backlog = POPi;
2562 GV * const gv = MUTABLE_GV(POPs);
2563 register IO * const io = gv ? GvIOn(gv) : NULL;
2565 if (!gv || !io || !IoIFP(io))
2568 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2574 if (ckWARN(WARN_CLOSED))
2576 SETERRNO(EBADF,SS_IVCHAN);
2579 DIE(aTHX_ PL_no_sock_func, "listen");
2589 char namebuf[MAXPATHLEN];
2590 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2591 Sock_size_t len = sizeof (struct sockaddr_in);
2593 Sock_size_t len = sizeof namebuf;
2595 GV * const ggv = MUTABLE_GV(POPs);
2596 GV * const ngv = MUTABLE_GV(POPs);
2605 if (!gstio || !IoIFP(gstio))
2609 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2612 /* Some platforms indicate zero length when an AF_UNIX client is
2613 * not bound. Simulate a non-zero-length sockaddr structure in
2615 namebuf[0] = 0; /* sun_len */
2616 namebuf[1] = AF_UNIX; /* sun_family */
2624 do_close(ngv, FALSE);
2625 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2626 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2627 IoTYPE(nstio) = IoTYPE_SOCKET;
2628 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2629 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2630 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2631 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2634 #if defined(HAS_FCNTL) && defined(F_SETFD)
2635 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2639 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2640 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2642 #ifdef __SCO_VERSION__
2643 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2646 PUSHp(namebuf, len);
2650 if (ckWARN(WARN_CLOSED))
2651 report_evil_fh(ggv);
2652 SETERRNO(EBADF,SS_IVCHAN);
2658 DIE(aTHX_ PL_no_sock_func, "accept");
2666 const int how = POPi;
2667 GV * const gv = MUTABLE_GV(POPs);
2668 register IO * const io = GvIOn(gv);
2670 if (!io || !IoIFP(io))
2673 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2677 if (ckWARN(WARN_CLOSED))
2679 SETERRNO(EBADF,SS_IVCHAN);
2682 DIE(aTHX_ PL_no_sock_func, "shutdown");
2690 const int optype = PL_op->op_type;
2691 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2692 const unsigned int optname = (unsigned int) POPi;
2693 const unsigned int lvl = (unsigned int) POPi;
2694 GV * const gv = MUTABLE_GV(POPs);
2695 register IO * const io = GvIOn(gv);
2699 if (!io || !IoIFP(io))
2702 fd = PerlIO_fileno(IoIFP(io));
2706 (void)SvPOK_only(sv);
2710 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2717 #if defined(__SYMBIAN32__)
2718 # define SETSOCKOPT_OPTION_VALUE_T void *
2720 # define SETSOCKOPT_OPTION_VALUE_T const char *
2722 /* XXX TODO: We need to have a proper type (a Configure probe,
2723 * etc.) for what the C headers think of the third argument of
2724 * setsockopt(), the option_value read-only buffer: is it
2725 * a "char *", or a "void *", const or not. Some compilers
2726 * don't take kindly to e.g. assuming that "char *" implicitly
2727 * promotes to a "void *", or to explicitly promoting/demoting
2728 * consts to non/vice versa. The "const void *" is the SUS
2729 * definition, but that does not fly everywhere for the above
2731 SETSOCKOPT_OPTION_VALUE_T buf;
2735 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2739 aint = (int)SvIV(sv);
2740 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2743 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2752 if (ckWARN(WARN_CLOSED))
2754 SETERRNO(EBADF,SS_IVCHAN);
2759 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2767 const int optype = PL_op->op_type;
2768 GV * const gv = MUTABLE_GV(POPs);
2769 register IO * const io = GvIOn(gv);
2774 if (!io || !IoIFP(io))
2777 sv = sv_2mortal(newSV(257));
2778 (void)SvPOK_only(sv);
2782 fd = PerlIO_fileno(IoIFP(io));
2784 case OP_GETSOCKNAME:
2785 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2788 case OP_GETPEERNAME:
2789 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2791 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2793 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";
2794 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2795 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2796 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2797 sizeof(u_short) + sizeof(struct in_addr))) {
2804 #ifdef BOGUS_GETNAME_RETURN
2805 /* Interactive Unix, getpeername() and getsockname()
2806 does not return valid namelen */
2807 if (len == BOGUS_GETNAME_RETURN)
2808 len = sizeof(struct sockaddr);
2816 if (ckWARN(WARN_CLOSED))
2818 SETERRNO(EBADF,SS_IVCHAN);
2823 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2838 if (PL_op->op_flags & OPf_REF) {
2840 if (PL_op->op_type == OP_LSTAT) {
2841 if (gv != PL_defgv) {
2842 do_fstat_warning_check:
2843 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2844 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2845 } else if (PL_laststype != OP_LSTAT)
2846 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2850 if (gv != PL_defgv) {
2851 PL_laststype = OP_STAT;
2853 sv_setpvs(PL_statname, "");
2860 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2861 } else if (IoDIRP(io)) {
2863 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2865 PL_laststatval = -1;
2871 if (PL_laststatval < 0) {
2872 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2878 SV* const sv = POPs;
2879 if (isGV_with_GP(sv)) {
2880 gv = MUTABLE_GV(sv);
2882 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2883 gv = MUTABLE_GV(SvRV(sv));
2884 if (PL_op->op_type == OP_LSTAT)
2885 goto do_fstat_warning_check;
2887 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2888 io = MUTABLE_IO(SvRV(sv));
2889 if (PL_op->op_type == OP_LSTAT)
2890 goto do_fstat_warning_check;
2891 goto do_fstat_have_io;
2894 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2896 PL_laststype = PL_op->op_type;
2897 if (PL_op->op_type == OP_LSTAT)
2898 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2900 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2901 if (PL_laststatval < 0) {
2902 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2903 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2909 if (gimme != G_ARRAY) {
2910 if (gimme != G_VOID)
2911 XPUSHs(boolSV(max));
2917 mPUSHi(PL_statcache.st_dev);
2918 mPUSHi(PL_statcache.st_ino);
2919 mPUSHu(PL_statcache.st_mode);
2920 mPUSHu(PL_statcache.st_nlink);
2921 #if Uid_t_size > IVSIZE
2922 mPUSHn(PL_statcache.st_uid);
2924 # if Uid_t_sign <= 0
2925 mPUSHi(PL_statcache.st_uid);
2927 mPUSHu(PL_statcache.st_uid);
2930 #if Gid_t_size > IVSIZE
2931 mPUSHn(PL_statcache.st_gid);
2933 # if Gid_t_sign <= 0
2934 mPUSHi(PL_statcache.st_gid);
2936 mPUSHu(PL_statcache.st_gid);
2939 #ifdef USE_STAT_RDEV
2940 mPUSHi(PL_statcache.st_rdev);
2942 PUSHs(newSVpvs_flags("", SVs_TEMP));
2944 #if Off_t_size > IVSIZE
2945 mPUSHn(PL_statcache.st_size);
2947 mPUSHi(PL_statcache.st_size);
2950 mPUSHn(PL_statcache.st_atime);
2951 mPUSHn(PL_statcache.st_mtime);
2952 mPUSHn(PL_statcache.st_ctime);
2954 mPUSHi(PL_statcache.st_atime);
2955 mPUSHi(PL_statcache.st_mtime);
2956 mPUSHi(PL_statcache.st_ctime);
2958 #ifdef USE_STAT_BLOCKS
2959 mPUSHu(PL_statcache.st_blksize);
2960 mPUSHu(PL_statcache.st_blocks);
2962 PUSHs(newSVpvs_flags("", SVs_TEMP));
2963 PUSHs(newSVpvs_flags("", SVs_TEMP));
2969 #define tryAMAGICftest_MG(chr) STMT_START { \
2970 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2971 && S_try_amagic_ftest(aTHX_ chr)) \
2976 S_try_amagic_ftest(pTHX_ char chr) {
2979 SV* const arg = TOPs;
2984 if ((PL_op->op_flags & OPf_KIDS)
2987 const char tmpchr = chr;
2989 SV * const tmpsv = amagic_call(arg,
2990 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2991 ftest_amg, AMGf_unary);
2998 next = PL_op->op_next;
2999 if (next->op_type >= OP_FTRREAD &&
3000 next->op_type <= OP_FTBINARY &&
3001 next->op_private & OPpFT_STACKED
3004 /* leave the object alone */
3016 /* This macro is used by the stacked filetest operators :
3017 * if the previous filetest failed, short-circuit and pass its value.
3018 * Else, discard it from the stack and continue. --rgs
3020 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
3021 if (!SvTRUE(TOPs)) { RETURN; } \
3022 else { (void)POPs; PUTBACK; } \
3029 /* Not const, because things tweak this below. Not bool, because there's
3030 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3031 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3032 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3033 /* Giving some sort of initial value silences compilers. */
3035 int access_mode = R_OK;
3037 int access_mode = 0;
3040 /* access_mode is never used, but leaving use_access in makes the
3041 conditional compiling below much clearer. */
3044 Mode_t stat_mode = S_IRUSR;
3046 bool effective = FALSE;
3050 switch (PL_op->op_type) {
3051 case OP_FTRREAD: opchar = 'R'; break;
3052 case OP_FTRWRITE: opchar = 'W'; break;
3053 case OP_FTREXEC: opchar = 'X'; break;
3054 case OP_FTEREAD: opchar = 'r'; break;
3055 case OP_FTEWRITE: opchar = 'w'; break;
3056 case OP_FTEEXEC: opchar = 'x'; break;
3058 tryAMAGICftest_MG(opchar);
3060 STACKED_FTEST_CHECK;
3062 switch (PL_op->op_type) {
3064 #if !(defined(HAS_ACCESS) && defined(R_OK))
3070 #if defined(HAS_ACCESS) && defined(W_OK)
3075 stat_mode = S_IWUSR;
3079 #if defined(HAS_ACCESS) && defined(X_OK)
3084 stat_mode = S_IXUSR;
3088 #ifdef PERL_EFF_ACCESS
3091 stat_mode = S_IWUSR;
3095 #ifndef PERL_EFF_ACCESS
3102 #ifdef PERL_EFF_ACCESS
3107 stat_mode = S_IXUSR;
3113 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3114 const char *name = POPpx;
3116 # ifdef PERL_EFF_ACCESS
3117 result = PERL_EFF_ACCESS(name, access_mode);
3119 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3125 result = access(name, access_mode);
3127 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3138 result = my_stat_flags(0);
3142 if (cando(stat_mode, effective, &PL_statcache))
3151 const int op_type = PL_op->op_type;
3156 case OP_FTIS: opchar = 'e'; break;
3157 case OP_FTSIZE: opchar = 's'; break;
3158 case OP_FTMTIME: opchar = 'M'; break;
3159 case OP_FTCTIME: opchar = 'C'; break;
3160 case OP_FTATIME: opchar = 'A'; break;
3162 tryAMAGICftest_MG(opchar);
3164 STACKED_FTEST_CHECK;
3166 result = my_stat_flags(0);
3170 if (op_type == OP_FTIS)
3173 /* You can't dTARGET inside OP_FTIS, because you'll get
3174 "panic: pad_sv po" - the op is not flagged to have a target. */
3178 #if Off_t_size > IVSIZE
3179 PUSHn(PL_statcache.st_size);
3181 PUSHi(PL_statcache.st_size);
3185 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3188 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3191 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3205 switch (PL_op->op_type) {
3206 case OP_FTROWNED: opchar = 'O'; break;
3207 case OP_FTEOWNED: opchar = 'o'; break;
3208 case OP_FTZERO: opchar = 'z'; break;
3209 case OP_FTSOCK: opchar = 'S'; break;
3210 case OP_FTCHR: opchar = 'c'; break;
3211 case OP_FTBLK: opchar = 'b'; break;
3212 case OP_FTFILE: opchar = 'f'; break;
3213 case OP_FTDIR: opchar = 'd'; break;
3214 case OP_FTPIPE: opchar = 'p'; break;
3215 case OP_FTSUID: opchar = 'u'; break;
3216 case OP_FTSGID: opchar = 'g'; break;
3217 case OP_FTSVTX: opchar = 'k'; break;
3219 tryAMAGICftest_MG(opchar);
3221 STACKED_FTEST_CHECK;
3223 /* I believe that all these three are likely to be defined on most every
3224 system these days. */
3226 if(PL_op->op_type == OP_FTSUID) {
3227 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3233 if(PL_op->op_type == OP_FTSGID) {
3234 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3240 if(PL_op->op_type == OP_FTSVTX) {
3241 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3247 result = my_stat_flags(0);
3251 switch (PL_op->op_type) {
3253 if (PL_statcache.st_uid == PL_uid)
3257 if (PL_statcache.st_uid == PL_euid)
3261 if (PL_statcache.st_size == 0)
3265 if (S_ISSOCK(PL_statcache.st_mode))
3269 if (S_ISCHR(PL_statcache.st_mode))
3273 if (S_ISBLK(PL_statcache.st_mode))
3277 if (S_ISREG(PL_statcache.st_mode))
3281 if (S_ISDIR(PL_statcache.st_mode))
3285 if (S_ISFIFO(PL_statcache.st_mode))
3290 if (PL_statcache.st_mode & S_ISUID)
3296 if (PL_statcache.st_mode & S_ISGID)
3302 if (PL_statcache.st_mode & S_ISVTX)
3316 tryAMAGICftest_MG('l');
3317 result = my_lstat_flags(0);
3322 if (S_ISLNK(PL_statcache.st_mode))
3337 tryAMAGICftest_MG('t');
3339 STACKED_FTEST_CHECK;
3341 if (PL_op->op_flags & OPf_REF)
3343 else if (isGV_with_GP(TOPs))
3344 gv = MUTABLE_GV(POPs);
3345 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3346 gv = MUTABLE_GV(SvRV(POPs));
3349 name = SvPV_nomg(tmpsv, namelen);
3350 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3353 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3354 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3355 else if (tmpsv && SvOK(tmpsv)) {
3363 if (PerlLIO_isatty(fd))
3368 #if defined(atarist) /* this will work with atariST. Configure will
3369 make guesses for other systems. */
3370 # define FILE_base(f) ((f)->_base)
3371 # define FILE_ptr(f) ((f)->_ptr)
3372 # define FILE_cnt(f) ((f)->_cnt)
3373 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3384 register STDCHAR *s;
3390 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3392 STACKED_FTEST_CHECK;
3394 if (PL_op->op_flags & OPf_REF)
3396 else if (isGV_with_GP(TOPs))
3397 gv = MUTABLE_GV(POPs);
3398 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3399 gv = MUTABLE_GV(SvRV(POPs));
3405 if (gv == PL_defgv) {
3407 io = GvIO(PL_statgv);
3410 goto really_filename;
3415 PL_laststatval = -1;
3416 sv_setpvs(PL_statname, "");
3417 io = GvIO(PL_statgv);
3419 if (io && IoIFP(io)) {
3420 if (! PerlIO_has_base(IoIFP(io)))
3421 DIE(aTHX_ "-T and -B not implemented on filehandles");
3422 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3423 if (PL_laststatval < 0)
3425 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3426 if (PL_op->op_type == OP_FTTEXT)
3431 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3432 i = PerlIO_getc(IoIFP(io));
3434 (void)PerlIO_ungetc(IoIFP(io),i);
3436 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3438 len = PerlIO_get_bufsiz(IoIFP(io));
3439 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3440 /* sfio can have large buffers - limit to 512 */
3445 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3449 SETERRNO(EBADF,RMS_IFI);
3457 PL_laststype = OP_STAT;
3458 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3459 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3460 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3462 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3465 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3466 if (PL_laststatval < 0) {
3467 (void)PerlIO_close(fp);
3470 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3471 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3472 (void)PerlIO_close(fp);
3474 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3475 RETPUSHNO; /* special case NFS directories */
3476 RETPUSHYES; /* null file is anything */
3481 /* now scan s to look for textiness */
3482 /* XXX ASCII dependent code */
3484 #if defined(DOSISH) || defined(USEMYBINMODE)
3485 /* ignore trailing ^Z on short files */
3486 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3490 for (i = 0; i < len; i++, s++) {
3491 if (!*s) { /* null never allowed in text */
3496 else if (!(isPRINT(*s) || isSPACE(*s)))
3499 else if (*s & 128) {
3501 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3504 /* utf8 characters don't count as odd */
3505 if (UTF8_IS_START(*s)) {
3506 int ulen = UTF8SKIP(s);
3507 if (ulen < len - i) {
3509 for (j = 1; j < ulen; j++) {
3510 if (!UTF8_IS_CONTINUATION(s[j]))
3513 --ulen; /* loop does extra increment */
3523 *s != '\n' && *s != '\r' && *s != '\b' &&
3524 *s != '\t' && *s != '\f' && *s != 27)
3529 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3540 const char *tmps = NULL;
3544 SV * const sv = POPs;
3545 if (PL_op->op_flags & OPf_SPECIAL) {
3546 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3548 else if (isGV_with_GP(sv)) {
3549 gv = MUTABLE_GV(sv);
3551 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3552 gv = MUTABLE_GV(SvRV(sv));
3555 tmps = SvPV_nolen_const(sv);
3559 if( !gv && (!tmps || !*tmps) ) {
3560 HV * const table = GvHVn(PL_envgv);
3563 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3564 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3566 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3571 deprecate("chdir('') or chdir(undef) as chdir()");
3572 tmps = SvPV_nolen_const(*svp);
3576 TAINT_PROPER("chdir");
3581 TAINT_PROPER("chdir");
3584 IO* const io = GvIO(gv);
3587 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3588 } else if (IoIFP(io)) {
3589 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3592 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3594 SETERRNO(EBADF, RMS_IFI);
3599 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3601 SETERRNO(EBADF,RMS_IFI);
3605 DIE(aTHX_ PL_no_func, "fchdir");
3609 PUSHi( PerlDir_chdir(tmps) >= 0 );
3611 /* Clear the DEFAULT element of ENV so we'll get the new value
3613 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3620 dVAR; dSP; dMARK; dTARGET;
3621 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3632 char * const tmps = POPpx;
3633 TAINT_PROPER("chroot");
3634 PUSHi( chroot(tmps) >= 0 );
3637 DIE(aTHX_ PL_no_func, "chroot");
3645 const char * const tmps2 = POPpconstx;
3646 const char * const tmps = SvPV_nolen_const(TOPs);
3647 TAINT_PROPER("rename");
3649 anum = PerlLIO_rename(tmps, tmps2);
3651 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3652 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3655 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3656 (void)UNLINK(tmps2);
3657 if (!(anum = link(tmps, tmps2)))
3658 anum = UNLINK(tmps);
3666 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3670 const int op_type = PL_op->op_type;
3674 if (op_type == OP_LINK)
3675 DIE(aTHX_ PL_no_func, "link");
3677 # ifndef HAS_SYMLINK
3678 if (op_type == OP_SYMLINK)
3679 DIE(aTHX_ PL_no_func, "symlink");
3683 const char * const tmps2 = POPpconstx;
3684 const char * const tmps = SvPV_nolen_const(TOPs);
3685 TAINT_PROPER(PL_op_desc[op_type]);
3687 # if defined(HAS_LINK)
3688 # if defined(HAS_SYMLINK)
3689 /* Both present - need to choose which. */
3690 (op_type == OP_LINK) ?
3691 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3693 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3694 PerlLIO_link(tmps, tmps2);
3697 # if defined(HAS_SYMLINK)
3698 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3699 symlink(tmps, tmps2);
3704 SETi( result >= 0 );
3711 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3722 char buf[MAXPATHLEN];
3725 #ifndef INCOMPLETE_TAINTS
3729 len = readlink(tmps, buf, sizeof(buf) - 1);
3736 RETSETUNDEF; /* just pretend it's a normal file */
3740 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3742 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3744 char * const save_filename = filename;
3749 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3751 PERL_ARGS_ASSERT_DOONELINER;
3753 Newx(cmdline, size, char);
3754 my_strlcpy(cmdline, cmd, size);
3755 my_strlcat(cmdline, " ", size);
3756 for (s = cmdline + strlen(cmdline); *filename; ) {
3760 if (s - cmdline < size)
3761 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3762 myfp = PerlProc_popen(cmdline, "r");
3766 SV * const tmpsv = sv_newmortal();
3767 /* Need to save/restore 'PL_rs' ?? */
3768 s = sv_gets(tmpsv, myfp, 0);
3769 (void)PerlProc_pclose(myfp);
3773 #ifdef HAS_SYS_ERRLIST
3778 /* you don't see this */
3779 const char * const errmsg =
3780 #ifdef HAS_SYS_ERRLIST
3788 if (instr(s, errmsg)) {
3795 #define EACCES EPERM
3797 if (instr(s, "cannot make"))
3798 SETERRNO(EEXIST,RMS_FEX);
3799 else if (instr(s, "existing file"))
3800 SETERRNO(EEXIST,RMS_FEX);
3801 else if (instr(s, "ile exists"))
3802 SETERRNO(EEXIST,RMS_FEX);
3803 else if (instr(s, "non-exist"))
3804 SETERRNO(ENOENT,RMS_FNF);
3805 else if (instr(s, "does not exist"))
3806 SETERRNO(ENOENT,RMS_FNF);
3807 else if (instr(s, "not empty"))
3808 SETERRNO(EBUSY,SS_DEVOFFLINE);
3809 else if (instr(s, "cannot access"))
3810 SETERRNO(EACCES,RMS_PRV);
3812 SETERRNO(EPERM,RMS_PRV);
3815 else { /* some mkdirs return no failure indication */
3816 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3817 if (PL_op->op_type == OP_RMDIR)
3822 SETERRNO(EACCES,RMS_PRV); /* a guess */
3831 /* This macro removes trailing slashes from a directory name.
3832 * Different operating and file systems take differently to
3833 * trailing slashes. According to POSIX 1003.1 1996 Edition
3834 * any number of trailing slashes should be allowed.
3835 * Thusly we snip them away so that even non-conforming
3836 * systems are happy.
3837 * We should probably do this "filtering" for all
3838 * the functions that expect (potentially) directory names:
3839 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3840 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3842 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3843 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3846 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3847 (tmps) = savepvn((tmps), (len)); \
3857 const int mode = (MAXARG > 1) ? POPi : 0777;
3859 TRIMSLASHES(tmps,len,copy);
3861 TAINT_PROPER("mkdir");
3863 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3867 SETi( dooneliner("mkdir", tmps) );
3868 oldumask = PerlLIO_umask(0);
3869 PerlLIO_umask(oldumask);
3870 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3885 TRIMSLASHES(tmps,len,copy);
3886 TAINT_PROPER("rmdir");
3888 SETi( PerlDir_rmdir(tmps) >= 0 );
3890 SETi( dooneliner("rmdir", tmps) );
3897 /* Directory calls. */
3901 #if defined(Direntry_t) && defined(HAS_READDIR)
3903 const char * const dirname = POPpconstx;
3904 GV * const gv = MUTABLE_GV(POPs);
3905 register IO * const io = GvIOn(gv);
3910 if ((IoIFP(io) || IoOFP(io)))
3911 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3912 "Opening filehandle %s also as a directory",
3915 PerlDir_close(IoDIRP(io));
3916 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3922 SETERRNO(EBADF,RMS_DIR);
3925 DIE(aTHX_ PL_no_dir_func, "opendir");
3931 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3932 DIE(aTHX_ PL_no_dir_func, "readdir");
3934 #if !defined(I_DIRENT) && !defined(VMS)
3935 Direntry_t *readdir (DIR *);
3941 const I32 gimme = GIMME;
3942 GV * const gv = MUTABLE_GV(POPs);
3943 register const Direntry_t *dp;
3944 register IO * const io = GvIOn(gv);
3946 if (!io || !IoDIRP(io)) {
3947 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3948 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3953 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3957 sv = newSVpvn(dp->d_name, dp->d_namlen);
3959 sv = newSVpv(dp->d_name, 0);
3961 #ifndef INCOMPLETE_TAINTS
3962 if (!(IoFLAGS(io) & IOf_UNTAINT))
3966 } while (gimme == G_ARRAY);
3968 if (!dp && gimme != G_ARRAY)
3975 SETERRNO(EBADF,RMS_ISI);
3976 if (GIMME == G_ARRAY)
3985 #if defined(HAS_TELLDIR) || defined(telldir)
3987 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3988 /* XXX netbsd still seemed to.
3989 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3990 --JHI 1999-Feb-02 */
3991 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3992 long telldir (DIR *);
3994 GV * const gv = MUTABLE_GV(POPs);
3995 register IO * const io = GvIOn(gv);
3997 if (!io || !IoDIRP(io)) {
3998 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3999 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
4003 PUSHi( PerlDir_tell(IoDIRP(io)) );
4007 SETERRNO(EBADF,RMS_ISI);
4010 DIE(aTHX_ PL_no_dir_func, "telldir");
4016 #if defined(HAS_SEEKDIR) || defined(seekdir)
4018 const long along = POPl;
4019 GV * const gv = MUTABLE_GV(POPs);
4020 register IO * const io = GvIOn(gv);
4022 if (!io || !IoDIRP(io)) {
4023 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4024 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
4027 (void)PerlDir_seek(IoDIRP(io), along);
4032 SETERRNO(EBADF,RMS_ISI);
4035 DIE(aTHX_ PL_no_dir_func, "seekdir");
4041 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4043 GV * const gv = MUTABLE_GV(POPs);
4044 register IO * const io = GvIOn(gv);
4046 if (!io || !IoDIRP(io)) {
4047 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4048 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
4051 (void)PerlDir_rewind(IoDIRP(io));
4055 SETERRNO(EBADF,RMS_ISI);
4058 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4064 #if defined(Direntry_t) && defined(HAS_READDIR)
4066 GV * const gv = MUTABLE_GV(POPs);
4067 register IO * const io = GvIOn(gv);
4069 if (!io || !IoDIRP(io)) {
4070 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4071 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4074 #ifdef VOID_CLOSEDIR
4075 PerlDir_close(IoDIRP(io));
4077 if (PerlDir_close(IoDIRP(io)) < 0) {
4078 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4087 SETERRNO(EBADF,RMS_IFI);
4090 DIE(aTHX_ PL_no_dir_func, "closedir");
4094 /* Process control. */
4103 PERL_FLUSHALL_FOR_CHILD;
4104 childpid = PerlProc_fork();
4108 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4110 SvREADONLY_off(GvSV(tmpgv));
4111 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4112 SvREADONLY_on(GvSV(tmpgv));
4114 #ifdef THREADS_HAVE_PIDS
4115 PL_ppid = (IV)getppid();
4117 #ifdef PERL_USES_PL_PIDSTATUS
4118 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4124 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4129 PERL_FLUSHALL_FOR_CHILD;
4130 childpid = PerlProc_fork();
4136 DIE(aTHX_ PL_no_func, "fork");
4143 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4148 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4149 childpid = wait4pid(-1, &argflags, 0);
4151 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4156 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4157 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4158 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4160 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4165 DIE(aTHX_ PL_no_func, "wait");
4171 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4173 const int optype = POPi;
4174 const Pid_t pid = TOPi;
4178 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4179 result = wait4pid(pid, &argflags, optype);
4181 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4186 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4187 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4188 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4190 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4195 DIE(aTHX_ PL_no_func, "waitpid");
4201 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4202 #if defined(__LIBCATAMOUNT__)
4203 PL_statusvalue = -1;
4212 while (++MARK <= SP) {
4213 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4218 TAINT_PROPER("system");
4220 PERL_FLUSHALL_FOR_CHILD;
4221 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4227 if (PerlProc_pipe(pp) >= 0)
4229 while ((childpid = PerlProc_fork()) == -1) {
4230 if (errno != EAGAIN) {
4235 PerlLIO_close(pp[0]);
4236 PerlLIO_close(pp[1]);
4243 Sigsave_t ihand,qhand; /* place to save signals during system() */
4247 PerlLIO_close(pp[1]);
4249 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4250 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4253 result = wait4pid(childpid, &status, 0);
4254 } while (result == -1 && errno == EINTR);
4256 (void)rsignal_restore(SIGINT, &ihand);
4257 (void)rsignal_restore(SIGQUIT, &qhand);
4259 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4260 do_execfree(); /* free any memory child malloced on fork */
4267 while (n < sizeof(int)) {
4268 n1 = PerlLIO_read(pp[0],
4269 (void*)(((char*)&errkid)+n),
4275 PerlLIO_close(pp[0]);
4276 if (n) { /* Error */
4277 if (n != sizeof(int))
4278 DIE(aTHX_ "panic: kid popen errno read");
4279 errno = errkid; /* Propagate errno from kid */
4280 STATUS_NATIVE_CHILD_SET(-1);
4283 XPUSHi(STATUS_CURRENT);
4287 PerlLIO_close(pp[0]);
4288 #if defined(HAS_FCNTL) && defined(F_SETFD)
4289 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4292 if (PL_op->op_flags & OPf_STACKED) {
4293 SV * const really = *++MARK;
4294 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4296 else if (SP - MARK != 1)
4297 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4299 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4303 #else /* ! FORK or VMS or OS/2 */
4306 if (PL_op->op_flags & OPf_STACKED) {
4307 SV * const really = *++MARK;
4308 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4309 value = (I32)do_aspawn(really, MARK, SP);
4311 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4314 else if (SP - MARK != 1) {
4315 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4316 value = (I32)do_aspawn(NULL, MARK, SP);
4318 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4322 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4324 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4326 STATUS_NATIVE_CHILD_SET(value);
4329 XPUSHi(result ? value : STATUS_CURRENT);
4330 #endif /* !FORK or VMS or OS/2 */
4337 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4342 while (++MARK <= SP) {
4343 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4348 TAINT_PROPER("exec");
4350 PERL_FLUSHALL_FOR_CHILD;
4351 if (PL_op->op_flags & OPf_STACKED) {
4352 SV * const really = *++MARK;
4353 value = (I32)do_aexec(really, MARK, SP);
4355 else if (SP - MARK != 1)
4357 value = (I32)vms_do_aexec(NULL, MARK, SP);
4361 (void ) do_aspawn(NULL, MARK, SP);
4365 value = (I32)do_aexec(NULL, MARK, SP);
4370 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4373 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4376 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4390 # ifdef THREADS_HAVE_PIDS
4391 if (PL_ppid != 1 && getppid() == 1)
4392 /* maybe the parent process has died. Refresh ppid cache */
4396 XPUSHi( getppid() );
4400 DIE(aTHX_ PL_no_func, "getppid");
4409 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4412 pgrp = (I32)BSD_GETPGRP(pid);
4414 if (pid != 0 && pid != PerlProc_getpid())
4415 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4421 DIE(aTHX_ PL_no_func, "getpgrp()");
4441 TAINT_PROPER("setpgrp");
4443 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4445 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4446 || (pid != 0 && pid != PerlProc_getpid()))
4448 DIE(aTHX_ "setpgrp can't take arguments");
4450 SETi( setpgrp() >= 0 );
4451 #endif /* USE_BSDPGRP */
4454 DIE(aTHX_ PL_no_func, "setpgrp()");
4458 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4459 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4461 # define PRIORITY_WHICH_T(which) which
4466 #ifdef HAS_GETPRIORITY
4468 const int who = POPi;
4469 const int which = TOPi;
4470 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4473 DIE(aTHX_ PL_no_func, "getpriority()");
4479 #ifdef HAS_SETPRIORITY
4481 const int niceval = POPi;
4482 const int who = POPi;
4483 const int which = TOPi;
4484 TAINT_PROPER("setpriority");
4485 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4488 DIE(aTHX_ PL_no_func, "setpriority()");
4492 #undef PRIORITY_WHICH_T
4500 XPUSHn( time(NULL) );
4502 XPUSHi( time(NULL) );
4514 (void)PerlProc_times(&PL_timesbuf);
4516 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4517 /* struct tms, though same data */
4521 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4522 if (GIMME == G_ARRAY) {
4523 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4524 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4525 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4533 if (GIMME == G_ARRAY) {
4540 DIE(aTHX_ "times not implemented");
4542 #endif /* HAS_TIMES */
4545 /* The 32 bit int year limits the times we can represent to these
4546 boundaries with a few days wiggle room to account for time zone
4549 /* Sat Jan 3 00:00:00 -2147481748 */
4550 #define TIME_LOWER_BOUND -67768100567755200.0
4551 /* Sun Dec 29 12:00:00 2147483647 */
4552 #define TIME_UPPER_BOUND 67767976233316800.0
4561 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4562 static const char * const dayname[] =
4563 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4564 static const char * const monname[] =
4565 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4566 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4571 when = (Time64_T)now;
4574 NV input = Perl_floor(POPn);
4575 when = (Time64_T)input;
4576 if (when != input) {
4577 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4578 "%s(%.0" NVff ") too large", opname, input);
4582 if ( TIME_LOWER_BOUND > when ) {
4583 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4584 "%s(%.0" NVff ") too small", opname, when);
4587 else if( when > TIME_UPPER_BOUND ) {
4588 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4589 "%s(%.0" NVff ") too large", opname, when);
4593 if (PL_op->op_type == OP_LOCALTIME)
4594 err = S_localtime64_r(&when, &tmbuf);
4596 err = S_gmtime64_r(&when, &tmbuf);
4600 /* XXX %lld broken for quads */
4601 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4602 "%s(%.0" NVff ") failed", opname, when);
4605 if (GIMME != G_ARRAY) { /* scalar context */
4607 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4608 double year = (double)tmbuf.tm_year + 1900;
4615 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4616 dayname[tmbuf.tm_wday],
4617 monname[tmbuf.tm_mon],
4625 else { /* list context */
4631 mPUSHi(tmbuf.tm_sec);
4632 mPUSHi(tmbuf.tm_min);
4633 mPUSHi(tmbuf.tm_hour);
4634 mPUSHi(tmbuf.tm_mday);
4635 mPUSHi(tmbuf.tm_mon);
4636 mPUSHn(tmbuf.tm_year);
4637 mPUSHi(tmbuf.tm_wday);
4638 mPUSHi(tmbuf.tm_yday);
4639 mPUSHi(tmbuf.tm_isdst);
4650 anum = alarm((unsigned int)anum);
4656 DIE(aTHX_ PL_no_func, "alarm");
4667 (void)time(&lasttime);
4672 PerlProc_sleep((unsigned int)duration);
4675 XPUSHi(when - lasttime);
4679 /* Shared memory. */
4680 /* Merged with some message passing. */
4684 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4685 dVAR; dSP; dMARK; dTARGET;
4686 const int op_type = PL_op->op_type;
4691 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4694 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4697 value = (I32)(do_semop(MARK, SP) >= 0);
4700 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4716 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4717 dVAR; dSP; dMARK; dTARGET;
4718 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4725 DIE(aTHX_ "System V IPC is not implemented on this machine");
4731 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4732 dVAR; dSP; dMARK; dTARGET;
4733 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4741 PUSHp(zero_but_true, ZBTLEN);
4749 /* I can't const this further without getting warnings about the types of
4750 various arrays passed in from structures. */
4752 S_space_join_names_mortal(pTHX_ char *const *array)
4756 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4758 if (array && *array) {
4759 target = newSVpvs_flags("", SVs_TEMP);
4761 sv_catpv(target, *array);
4764 sv_catpvs(target, " ");
4767 target = sv_mortalcopy(&PL_sv_no);
4772 /* Get system info. */
4776 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4778 I32 which = PL_op->op_type;
4779 register char **elem;
4781 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4782 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4783 struct hostent *gethostbyname(Netdb_name_t);
4784 struct hostent *gethostent(void);
4786 struct hostent *hent = NULL;
4790 if (which == OP_GHBYNAME) {
4791 #ifdef HAS_GETHOSTBYNAME
4792 const char* const name = POPpbytex;
4793 hent = PerlSock_gethostbyname(name);
4795 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4798 else if (which == OP_GHBYADDR) {
4799 #ifdef HAS_GETHOSTBYADDR
4800 const int addrtype = POPi;
4801 SV * const addrsv = POPs;
4803 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4805 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4807 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4811 #ifdef HAS_GETHOSTENT
4812 hent = PerlSock_gethostent();
4814 DIE(aTHX_ PL_no_sock_func, "gethostent");
4817 #ifdef HOST_NOT_FOUND
4819 #ifdef USE_REENTRANT_API
4820 # ifdef USE_GETHOSTENT_ERRNO
4821 h_errno = PL_reentrant_buffer->_gethostent_errno;
4824 STATUS_UNIX_SET(h_errno);
4828 if (GIMME != G_ARRAY) {
4829 PUSHs(sv = sv_newmortal());
4831 if (which == OP_GHBYNAME) {
4833 sv_setpvn(sv, hent->h_addr, hent->h_length);
4836 sv_setpv(sv, (char*)hent->h_name);
4842 mPUSHs(newSVpv((char*)hent->h_name, 0));
4843 PUSHs(space_join_names_mortal(hent->h_aliases));
4844 mPUSHi(hent->h_addrtype);
4845 len = hent->h_length;
4848 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4849 mXPUSHp(*elem, len);
4853 mPUSHp(hent->h_addr, len);
4855 PUSHs(sv_mortalcopy(&PL_sv_no));
4860 DIE(aTHX_ PL_no_sock_func, "gethostent");
4866 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4868 I32 which = PL_op->op_type;
4870 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4871 struct netent *getnetbyaddr(Netdb_net_t, int);
4872 struct netent *getnetbyname(Netdb_name_t);
4873 struct netent *getnetent(void);
4875 struct netent *nent;
4877 if (which == OP_GNBYNAME){
4878 #ifdef HAS_GETNETBYNAME
4879 const char * const name = POPpbytex;
4880 nent = PerlSock_getnetbyname(name);
4882 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4885 else if (which == OP_GNBYADDR) {
4886 #ifdef HAS_GETNETBYADDR
4887 const int addrtype = POPi;
4888 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4889 nent = PerlSock_getnetbyaddr(addr, addrtype);
4891 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4895 #ifdef HAS_GETNETENT
4896 nent = PerlSock_getnetent();
4898 DIE(aTHX_ PL_no_sock_func, "getnetent");
4901 #ifdef HOST_NOT_FOUND
4903 #ifdef USE_REENTRANT_API
4904 # ifdef USE_GETNETENT_ERRNO
4905 h_errno = PL_reentrant_buffer->_getnetent_errno;
4908 STATUS_UNIX_SET(h_errno);
4913 if (GIMME != G_ARRAY) {
4914 PUSHs(sv = sv_newmortal());
4916 if (which == OP_GNBYNAME)
4917 sv_setiv(sv, (IV)nent->n_net);
4919 sv_setpv(sv, nent->n_name);
4925 mPUSHs(newSVpv(nent->n_name, 0));
4926 PUSHs(space_join_names_mortal(nent->n_aliases));
4927 mPUSHi(nent->n_addrtype);
4928 mPUSHi(nent->n_net);
4933 DIE(aTHX_ PL_no_sock_func, "getnetent");
4939 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4941 I32 which = PL_op->op_type;
4943 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4944 struct protoent *getprotobyname(Netdb_name_t);
4945 struct protoent *getprotobynumber(int);
4946 struct protoent *getprotoent(void);
4948 struct protoent *pent;
4950 if (which == OP_GPBYNAME) {
4951 #ifdef HAS_GETPROTOBYNAME
4952 const char* const name = POPpbytex;
4953 pent = PerlSock_getprotobyname(name);
4955 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4958 else if (which == OP_GPBYNUMBER) {
4959 #ifdef HAS_GETPROTOBYNUMBER
4960 const int number = POPi;
4961 pent = PerlSock_getprotobynumber(number);
4963 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4967 #ifdef HAS_GETPROTOENT
4968 pent = PerlSock_getprotoent();
4970 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4974 if (GIMME != G_ARRAY) {
4975 PUSHs(sv = sv_newmortal());
4977 if (which == OP_GPBYNAME)
4978 sv_setiv(sv, (IV)pent->p_proto);
4980 sv_setpv(sv, pent->p_name);
4986 mPUSHs(newSVpv(pent->p_name, 0));
4987 PUSHs(space_join_names_mortal(pent->p_aliases));
4988 mPUSHi(pent->p_proto);
4993 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4999 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5001 I32 which = PL_op->op_type;
5003 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5004 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5005 struct servent *getservbyport(int, Netdb_name_t);
5006 struct servent *getservent(void);
5008 struct servent *sent;
5010 if (which == OP_GSBYNAME) {
5011 #ifdef HAS_GETSERVBYNAME
5012 const char * const proto = POPpbytex;
5013 const char * const name = POPpbytex;
5014 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5016 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5019 else if (which == OP_GSBYPORT) {
5020 #ifdef HAS_GETSERVBYPORT
5021 const char * const proto = POPpbytex;
5022 unsigned short port = (unsigned short)POPu;
5024 port = PerlSock_htons(port);
5026 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5028 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5032 #ifdef HAS_GETSERVENT
5033 sent = PerlSock_getservent();
5035 DIE(aTHX_ PL_no_sock_func, "getservent");
5039 if (GIMME != G_ARRAY) {
5040 PUSHs(sv = sv_newmortal());
5042 if (which == OP_GSBYNAME) {
5044 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5046 sv_setiv(sv, (IV)(sent->s_port));
5050 sv_setpv(sv, sent->s_name);
5056 mPUSHs(newSVpv(sent->s_name, 0));
5057 PUSHs(space_join_names_mortal(sent->s_aliases));
5059 mPUSHi(PerlSock_ntohs(sent->s_port));
5061 mPUSHi(sent->s_port);
5063 mPUSHs(newSVpv(sent->s_proto, 0));