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);
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");
542 tmps = SvPV_const(sv, len);
543 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
546 PUSHi( (I32)PL_forkprocess );
547 else if (PL_forkprocess == 0) /* we are a new child */
554 /* These are private to this function, which is private to this file.
555 Use 0x04 rather than the next available bit, to help the compiler if the
556 architecture can generate more efficient instructions. */
557 #define MORTALIZE_NOT_NEEDED 0x04
558 #define TIED_HANDLE_ARGC_SHIFT 3
561 S_tied_handle_method(pTHX_ const char *const methname, SV **sp,
562 IO *const io, MAGIC *const mg, const U32 flags, ...)
564 U32 argc = flags >> TIED_HANDLE_ARGC_SHIFT;
566 PERL_ARGS_ASSERT_TIED_HANDLE_METHOD;
568 /* Ensure that our flag bits do not overlap. */
569 assert((MORTALIZE_NOT_NEEDED & G_WANT) == 0);
570 assert((G_WANT >> TIED_HANDLE_ARGC_SHIFT) == 0);
573 PUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
575 const U32 mortalize_not_needed = flags & MORTALIZE_NOT_NEEDED;
577 va_start(args, flags);
579 SV *const arg = va_arg(args, SV *);
580 if(mortalize_not_needed)
589 ENTER_with_name("call_tied_handle_method");
590 call_method(methname, flags & G_WANT);
591 LEAVE_with_name("call_tied_handle_method");
595 #define tied_handle_method(a,b,c,d) \
596 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR)
597 #define tied_handle_method1(a,b,c,d,e) \
598 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (1 << TIED_HANDLE_ARGC_SHIFT),e)
599 #define tied_handle_method2(a,b,c,d,e,f) \
600 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR | (2 << TIED_HANDLE_ARGC_SHIFT), e,f)
605 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
611 IO * const io = GvIO(gv);
613 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
615 return tied_handle_method("CLOSE", SP, io, mg);
619 PUSHs(boolSV(do_close(gv, TRUE)));
632 GV * const wgv = MUTABLE_GV(POPs);
633 GV * const rgv = MUTABLE_GV(POPs);
638 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
639 DIE(aTHX_ PL_no_usym, "filehandle");
644 do_close(rgv, FALSE);
646 do_close(wgv, FALSE);
648 if (PerlProc_pipe(fd) < 0)
651 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
652 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
653 IoOFP(rstio) = IoIFP(rstio);
654 IoIFP(wstio) = IoOFP(wstio);
655 IoTYPE(rstio) = IoTYPE_RDONLY;
656 IoTYPE(wstio) = IoTYPE_WRONLY;
658 if (!IoIFP(rstio) || !IoOFP(wstio)) {
660 PerlIO_close(IoIFP(rstio));
662 PerlLIO_close(fd[0]);
664 PerlIO_close(IoOFP(wstio));
666 PerlLIO_close(fd[1]);
669 #if defined(HAS_FCNTL) && defined(F_SETFD)
670 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
671 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
678 DIE(aTHX_ PL_no_func, "pipe");
692 gv = MUTABLE_GV(POPs);
694 if (gv && (io = GvIO(gv))
695 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
697 return tied_handle_method("FILENO", SP, io, mg);
700 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
701 /* Can't do this because people seem to do things like
702 defined(fileno($foo)) to check whether $foo is a valid fh.
703 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
704 report_evil_fh(gv, io, PL_op->op_type);
709 PUSHi(PerlIO_fileno(fp));
722 anum = PerlLIO_umask(022);
723 /* setting it to 022 between the two calls to umask avoids
724 * to have a window where the umask is set to 0 -- meaning
725 * that another thread could create world-writeable files. */
727 (void)PerlLIO_umask(anum);
730 anum = PerlLIO_umask(POPi);
731 TAINT_PROPER("umask");
734 /* Only DIE if trying to restrict permissions on "user" (self).
735 * Otherwise it's harmless and more useful to just return undef
736 * since 'group' and 'other' concepts probably don't exist here. */
737 if (MAXARG >= 1 && (POPi & 0700))
738 DIE(aTHX_ "umask not implemented");
739 XPUSHs(&PL_sv_undef);
758 gv = MUTABLE_GV(POPs);
760 if (gv && (io = GvIO(gv))) {
761 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
763 /* This takes advantage of the implementation of the varargs
764 function, which I don't think that the optimiser will be able to
765 figure out. Although, as it's a static function, in theory it
767 return S_tied_handle_method(aTHX_ "BINMODE", SP, io, mg,
768 G_SCALAR|MORTALIZE_NOT_NEEDED
770 ? (1 << TIED_HANDLE_ARGC_SHIFT) : 0),
775 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
776 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
777 report_evil_fh(gv, io, PL_op->op_type);
778 SETERRNO(EBADF,RMS_IFI);
785 const char *d = NULL;
788 d = SvPV_const(discp, len);
789 mode = mode_from_discipline(d, len);
790 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
791 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
792 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
813 const I32 markoff = MARK - PL_stack_base;
814 const char *methname;
815 int how = PERL_MAGIC_tied;
819 switch(SvTYPE(varsv)) {
821 methname = "TIEHASH";
822 HvEITER_set(MUTABLE_HV(varsv), 0);
825 methname = "TIEARRAY";
828 if (isGV_with_GP(varsv)) {
829 methname = "TIEHANDLE";
830 how = PERL_MAGIC_tiedscalar;
831 /* For tied filehandles, we apply tiedscalar magic to the IO
832 slot of the GP rather than the GV itself. AMS 20010812 */
834 GvIOp(varsv) = newIO();
835 varsv = MUTABLE_SV(GvIOp(varsv));
840 methname = "TIESCALAR";
841 how = PERL_MAGIC_tiedscalar;
845 if (sv_isobject(*MARK)) { /* Calls GET magic. */
846 ENTER_with_name("call_TIE");
847 PUSHSTACKi(PERLSI_MAGIC);
849 EXTEND(SP,(I32)items);
853 call_method(methname, G_SCALAR);
856 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
857 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
858 * wrong error message, and worse case, supreme action at a distance.
859 * (Sorry obfuscation writers. You're not going to be given this one.)
862 const char *name = SvPV_nomg_const(*MARK, len);
863 stash = gv_stashpvn(name, len, 0);
864 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
865 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
866 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
868 ENTER_with_name("call_TIE");
869 PUSHSTACKi(PERLSI_MAGIC);
871 EXTEND(SP,(I32)items);
875 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
881 if (sv_isobject(sv)) {
882 sv_unmagic(varsv, how);
883 /* Croak if a self-tie on an aggregate is attempted. */
884 if (varsv == SvRV(sv) &&
885 (SvTYPE(varsv) == SVt_PVAV ||
886 SvTYPE(varsv) == SVt_PVHV))
888 "Self-ties of arrays and hashes are not supported");
889 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
891 LEAVE_with_name("call_TIE");
892 SP = PL_stack_base + markoff;
902 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
903 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
905 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
908 if ((mg = SvTIED_mg(sv, how))) {
909 SV * const obj = SvRV(SvTIED_obj(sv, mg));
911 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
913 if (gv && isGV(gv) && (cv = GvCV(gv))) {
915 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
916 mXPUSHi(SvREFCNT(obj) - 1);
918 ENTER_with_name("call_UNTIE");
919 call_sv(MUTABLE_SV(cv), G_VOID);
920 LEAVE_with_name("call_UNTIE");
923 else if (mg && SvREFCNT(obj) > 1) {
924 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
925 "untie attempted while %"UVuf" inner references still exist",
926 (UV)SvREFCNT(obj) - 1 ) ;
930 sv_unmagic(sv, how) ;
940 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
941 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
943 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
946 if ((mg = SvTIED_mg(sv, how))) {
947 SV *osv = SvTIED_obj(sv, mg);
948 if (osv == mg->mg_obj)
949 osv = sv_mortalcopy(osv);
963 HV * const hv = MUTABLE_HV(POPs);
964 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
965 stash = gv_stashsv(sv, 0);
966 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
968 require_pv("AnyDBM_File.pm");
970 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
971 DIE(aTHX_ "No dbm on this machine");
981 mPUSHu(O_RDWR|O_CREAT);
986 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
989 if (!sv_isobject(TOPs)) {
997 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1001 if (sv_isobject(TOPs)) {
1002 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1003 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1020 struct timeval timebuf;
1021 struct timeval *tbuf = &timebuf;
1024 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1029 # if BYTEORDER & 0xf0000
1030 # define ORDERBYTE (0x88888888 - BYTEORDER)
1032 # define ORDERBYTE (0x4444 - BYTEORDER)
1038 for (i = 1; i <= 3; i++) {
1039 SV * const sv = SP[i];
1042 if (SvREADONLY(sv)) {
1044 sv_force_normal_flags(sv, 0);
1045 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1046 Perl_croak_no_modify(aTHX);
1049 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1050 SvPV_force_nolen(sv); /* force string conversion */
1057 /* little endians can use vecs directly */
1058 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1065 masksize = NFDBITS / NBBY;
1067 masksize = sizeof(long); /* documented int, everyone seems to use long */
1069 Zero(&fd_sets[0], 4, char*);
1072 # if SELECT_MIN_BITS == 1
1073 growsize = sizeof(fd_set);
1075 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1076 # undef SELECT_MIN_BITS
1077 # define SELECT_MIN_BITS __FD_SETSIZE
1079 /* If SELECT_MIN_BITS is greater than one we most probably will want
1080 * to align the sizes with SELECT_MIN_BITS/8 because for example
1081 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1082 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1083 * on (sets/tests/clears bits) is 32 bits. */
1084 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1092 timebuf.tv_sec = (long)value;
1093 value -= (NV)timebuf.tv_sec;
1094 timebuf.tv_usec = (long)(value * 1000000.0);
1099 for (i = 1; i <= 3; i++) {
1101 if (!SvOK(sv) || SvCUR(sv) == 0) {
1108 Sv_Grow(sv, growsize);
1112 while (++j <= growsize) {
1116 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1118 Newx(fd_sets[i], growsize, char);
1119 for (offset = 0; offset < growsize; offset += masksize) {
1120 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1121 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1124 fd_sets[i] = SvPVX(sv);
1128 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1129 /* Can't make just the (void*) conditional because that would be
1130 * cpp #if within cpp macro, and not all compilers like that. */
1131 nfound = PerlSock_select(
1133 (Select_fd_set_t) fd_sets[1],
1134 (Select_fd_set_t) fd_sets[2],
1135 (Select_fd_set_t) fd_sets[3],
1136 (void*) tbuf); /* Workaround for compiler bug. */
1138 nfound = PerlSock_select(
1140 (Select_fd_set_t) fd_sets[1],
1141 (Select_fd_set_t) fd_sets[2],
1142 (Select_fd_set_t) fd_sets[3],
1145 for (i = 1; i <= 3; i++) {
1148 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1150 for (offset = 0; offset < growsize; offset += masksize) {
1151 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1152 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1154 Safefree(fd_sets[i]);
1161 if (GIMME == G_ARRAY && tbuf) {
1162 value = (NV)(timebuf.tv_sec) +
1163 (NV)(timebuf.tv_usec) / 1000000.0;
1168 DIE(aTHX_ "select not implemented");
1173 =for apidoc setdefout
1175 Sets PL_defoutgv, the default file handle for output, to the passed in
1176 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1177 count of the passed in typeglob is increased by one, and the reference count
1178 of the typeglob that PL_defoutgv points to is decreased by one.
1184 Perl_setdefout(pTHX_ GV *gv)
1187 SvREFCNT_inc_simple_void(gv);
1188 SvREFCNT_dec(PL_defoutgv);
1196 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1197 GV * egv = GvEGVx(PL_defoutgv);
1201 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1203 XPUSHs(&PL_sv_undef);
1205 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1206 if (gvp && *gvp == egv) {
1207 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1211 mXPUSHs(newRV(MUTABLE_SV(egv)));
1216 if (!GvIO(newdefout))
1217 gv_IOadd(newdefout);
1218 setdefout(newdefout);
1228 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1233 if (gv && (io = GvIO(gv))) {
1234 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1236 const U32 gimme = GIMME_V;
1237 S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme);
1238 if (gimme == G_SCALAR) {
1240 SvSetMagicSV_nosteal(TARG, TOPs);
1245 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1246 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1247 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1248 report_evil_fh(gv, io, PL_op->op_type);
1249 SETERRNO(EBADF,RMS_IFI);
1253 sv_setpvs(TARG, " ");
1254 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1255 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1256 /* Find out how many bytes the char needs */
1257 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1260 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1261 SvCUR_set(TARG,1+len);
1270 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1273 register PERL_CONTEXT *cx;
1274 const I32 gimme = GIMME_V;
1276 PERL_ARGS_ASSERT_DOFORM;
1278 if (cv && CvCLONE(cv))
1279 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1284 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1285 PUSHFORMAT(cx, retop);
1287 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1289 setdefout(gv); /* locally select filehandle so $% et al work */
1308 gv = MUTABLE_GV(POPs);
1322 goto not_a_format_reference;
1327 tmpsv = sv_newmortal();
1328 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1329 name = SvPV_nolen_const(tmpsv);
1331 DIE(aTHX_ "Undefined format \"%s\" called", name);
1333 not_a_format_reference:
1334 DIE(aTHX_ "Not a format reference");
1336 IoFLAGS(io) &= ~IOf_DIDTOP;
1337 return doform(cv,gv,PL_op->op_next);
1343 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1344 register IO * const io = GvIOp(gv);
1349 register PERL_CONTEXT *cx;
1351 if (!io || !(ofp = IoOFP(io)))
1354 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1355 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1357 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1358 PL_formtarget != PL_toptarget)
1362 if (!IoTOP_GV(io)) {
1365 if (!IoTOP_NAME(io)) {
1367 if (!IoFMT_NAME(io))
1368 IoFMT_NAME(io) = savepv(GvNAME(gv));
1369 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1370 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1371 if ((topgv && GvFORM(topgv)) ||
1372 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1373 IoTOP_NAME(io) = savesvpv(topname);
1375 IoTOP_NAME(io) = savepvs("top");
1377 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1378 if (!topgv || !GvFORM(topgv)) {
1379 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1382 IoTOP_GV(io) = topgv;
1384 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1385 I32 lines = IoLINES_LEFT(io);
1386 const char *s = SvPVX_const(PL_formtarget);
1387 if (lines <= 0) /* Yow, header didn't even fit!!! */
1389 while (lines-- > 0) {
1390 s = strchr(s, '\n');
1396 const STRLEN save = SvCUR(PL_formtarget);
1397 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1398 do_print(PL_formtarget, ofp);
1399 SvCUR_set(PL_formtarget, save);
1400 sv_chop(PL_formtarget, s);
1401 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1404 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1405 do_print(PL_formfeed, ofp);
1406 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1408 PL_formtarget = PL_toptarget;
1409 IoFLAGS(io) |= IOf_DIDTOP;
1412 DIE(aTHX_ "bad top format reference");
1415 SV * const sv = sv_newmortal();
1417 gv_efullname4(sv, fgv, NULL, FALSE);
1418 name = SvPV_nolen_const(sv);
1420 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1422 DIE(aTHX_ "Undefined top format called");
1424 return doform(cv, gv, PL_op);
1428 POPBLOCK(cx,PL_curpm);
1434 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1436 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1437 else if (ckWARN(WARN_CLOSED))
1438 report_evil_fh(gv, io, PL_op->op_type);
1443 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1444 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1446 if (!do_print(PL_formtarget, fp))
1449 FmLINES(PL_formtarget) = 0;
1450 SvCUR_set(PL_formtarget, 0);
1451 *SvEND(PL_formtarget) = '\0';
1452 if (IoFLAGS(io) & IOf_FLUSH)
1453 (void)PerlIO_flush(fp);
1458 PL_formtarget = PL_bodytarget;
1460 PERL_UNUSED_VAR(newsp);
1461 PERL_UNUSED_VAR(gimme);
1462 return cx->blk_sub.retop;
1467 dVAR; dSP; dMARK; dORIGMARK;
1473 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1475 if (gv && (io = GvIO(gv))) {
1476 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1478 if (MARK == ORIGMARK) {
1481 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1485 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1488 call_method("PRINTF", G_SCALAR);
1491 MARK = ORIGMARK + 1;
1499 if (!(io = GvIO(gv))) {
1500 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1501 report_evil_fh(gv, io, PL_op->op_type);
1502 SETERRNO(EBADF,RMS_IFI);
1505 else if (!(fp = IoOFP(io))) {
1506 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1508 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1509 else if (ckWARN(WARN_CLOSED))
1510 report_evil_fh(gv, io, PL_op->op_type);
1512 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1516 if (SvTAINTED(MARK[1]))
1517 TAINT_PROPER("printf");
1518 do_sprintf(sv, SP - MARK, MARK + 1);
1519 if (!do_print(sv, fp))
1522 if (IoFLAGS(io) & IOf_FLUSH)
1523 if (PerlIO_flush(fp) == EOF)
1534 PUSHs(&PL_sv_undef);
1542 const int perm = (MAXARG > 3) ? POPi : 0666;
1543 const int mode = POPi;
1544 SV * const sv = POPs;
1545 GV * const gv = MUTABLE_GV(POPs);
1548 /* Need TIEHANDLE method ? */
1549 const char * const tmps = SvPV_const(sv, len);
1550 /* FIXME? do_open should do const */
1551 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1552 IoLINES(GvIOp(gv)) = 0;
1556 PUSHs(&PL_sv_undef);
1563 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1569 Sock_size_t bufsize;
1577 bool charstart = FALSE;
1578 STRLEN charskip = 0;
1581 GV * const gv = MUTABLE_GV(*++MARK);
1582 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1583 && gv && (io = GvIO(gv)) )
1585 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1589 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1591 call_method("READ", G_SCALAR);
1605 sv_setpvs(bufsv, "");
1606 length = SvIVx(*++MARK);
1609 offset = SvIVx(*++MARK);
1613 if (!io || !IoIFP(io)) {
1614 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1615 report_evil_fh(gv, io, PL_op->op_type);
1616 SETERRNO(EBADF,RMS_IFI);
1619 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1620 buffer = SvPVutf8_force(bufsv, blen);
1621 /* UTF-8 may not have been set if they are all low bytes */
1626 buffer = SvPV_force(bufsv, blen);
1627 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1630 DIE(aTHX_ "Negative length");
1638 if (PL_op->op_type == OP_RECV) {
1639 char namebuf[MAXPATHLEN];
1640 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1641 bufsize = sizeof (struct sockaddr_in);
1643 bufsize = sizeof namebuf;
1645 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1649 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1650 /* 'offset' means 'flags' here */
1651 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1652 (struct sockaddr *)namebuf, &bufsize);
1656 /* Bogus return without padding */
1657 bufsize = sizeof (struct sockaddr_in);
1659 SvCUR_set(bufsv, count);
1660 *SvEND(bufsv) = '\0';
1661 (void)SvPOK_only(bufsv);
1665 /* This should not be marked tainted if the fp is marked clean */
1666 if (!(IoFLAGS(io) & IOf_UNTAINT))
1667 SvTAINTED_on(bufsv);
1669 sv_setpvn(TARG, namebuf, bufsize);
1674 if (PL_op->op_type == OP_RECV)
1675 DIE(aTHX_ PL_no_sock_func, "recv");
1677 if (DO_UTF8(bufsv)) {
1678 /* offset adjust in characters not bytes */
1679 blen = sv_len_utf8(bufsv);
1682 if (-offset > (int)blen)
1683 DIE(aTHX_ "Offset outside string");
1686 if (DO_UTF8(bufsv)) {
1687 /* convert offset-as-chars to offset-as-bytes */
1688 if (offset >= (int)blen)
1689 offset += SvCUR(bufsv) - blen;
1691 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1694 bufsize = SvCUR(bufsv);
1695 /* Allocating length + offset + 1 isn't perfect in the case of reading
1696 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1698 (should be 2 * length + offset + 1, or possibly something longer if
1699 PL_encoding is true) */
1700 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1701 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1702 Zero(buffer+bufsize, offset-bufsize, char);
1704 buffer = buffer + offset;
1706 read_target = bufsv;
1708 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1709 concatenate it to the current buffer. */
1711 /* Truncate the existing buffer to the start of where we will be
1713 SvCUR_set(bufsv, offset);
1715 read_target = sv_newmortal();
1716 SvUPGRADE(read_target, SVt_PV);
1717 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1720 if (PL_op->op_type == OP_SYSREAD) {
1721 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1722 if (IoTYPE(io) == IoTYPE_SOCKET) {
1723 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1729 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1734 #ifdef HAS_SOCKET__bad_code_maybe
1735 if (IoTYPE(io) == IoTYPE_SOCKET) {
1736 char namebuf[MAXPATHLEN];
1737 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1738 bufsize = sizeof (struct sockaddr_in);
1740 bufsize = sizeof namebuf;
1742 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1743 (struct sockaddr *)namebuf, &bufsize);
1748 count = PerlIO_read(IoIFP(io), buffer, length);
1749 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1750 if (count == 0 && PerlIO_error(IoIFP(io)))
1754 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1755 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1758 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1759 *SvEND(read_target) = '\0';
1760 (void)SvPOK_only(read_target);
1761 if (fp_utf8 && !IN_BYTES) {
1762 /* Look at utf8 we got back and count the characters */
1763 const char *bend = buffer + count;
1764 while (buffer < bend) {
1766 skip = UTF8SKIP(buffer);
1769 if (buffer - charskip + skip > bend) {
1770 /* partial character - try for rest of it */
1771 length = skip - (bend-buffer);
1772 offset = bend - SvPVX_const(bufsv);
1784 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1785 provided amount read (count) was what was requested (length)
1787 if (got < wanted && count == length) {
1788 length = wanted - got;
1789 offset = bend - SvPVX_const(bufsv);
1792 /* return value is character count */
1796 else if (buffer_utf8) {
1797 /* Let svcatsv upgrade the bytes we read in to utf8.
1798 The buffer is a mortal so will be freed soon. */
1799 sv_catsv_nomg(bufsv, read_target);
1802 /* This should not be marked tainted if the fp is marked clean */
1803 if (!(IoFLAGS(io) & IOf_UNTAINT))
1804 SvTAINTED_on(bufsv);
1816 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1822 STRLEN orig_blen_bytes;
1823 const int op_type = PL_op->op_type;
1827 GV *const gv = MUTABLE_GV(*++MARK);
1828 if (PL_op->op_type == OP_SYSWRITE
1829 && gv && (io = GvIO(gv))) {
1830 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1834 if (MARK == SP - 1) {
1836 mXPUSHi(sv_len(sv));
1841 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1843 call_method("WRITE", G_SCALAR);
1859 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1861 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1862 if (io && IoIFP(io))
1863 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1865 report_evil_fh(gv, io, PL_op->op_type);
1867 SETERRNO(EBADF,RMS_IFI);
1871 /* Do this first to trigger any overloading. */
1872 buffer = SvPV_const(bufsv, blen);
1873 orig_blen_bytes = blen;
1874 doing_utf8 = DO_UTF8(bufsv);
1876 if (PerlIO_isutf8(IoIFP(io))) {
1877 if (!SvUTF8(bufsv)) {
1878 /* We don't modify the original scalar. */
1879 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1880 buffer = (char *) tmpbuf;
1884 else if (doing_utf8) {
1885 STRLEN tmplen = blen;
1886 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1889 buffer = (char *) tmpbuf;
1893 assert((char *)result == buffer);
1894 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1898 if (op_type == OP_SYSWRITE) {
1899 Size_t length = 0; /* This length is in characters. */
1905 /* The SV is bytes, and we've had to upgrade it. */
1906 blen_chars = orig_blen_bytes;
1908 /* The SV really is UTF-8. */
1909 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1910 /* Don't call sv_len_utf8 again because it will call magic
1911 or overloading a second time, and we might get back a
1912 different result. */
1913 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1915 /* It's safe, and it may well be cached. */
1916 blen_chars = sv_len_utf8(bufsv);
1924 length = blen_chars;
1926 #if Size_t_size > IVSIZE
1927 length = (Size_t)SvNVx(*++MARK);
1929 length = (Size_t)SvIVx(*++MARK);
1931 if ((SSize_t)length < 0) {
1933 DIE(aTHX_ "Negative length");
1938 offset = SvIVx(*++MARK);
1940 if (-offset > (IV)blen_chars) {
1942 DIE(aTHX_ "Offset outside string");
1944 offset += blen_chars;
1945 } else if (offset > (IV)blen_chars) {
1947 DIE(aTHX_ "Offset outside string");
1951 if (length > blen_chars - offset)
1952 length = blen_chars - offset;
1954 /* Here we convert length from characters to bytes. */
1955 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1956 /* Either we had to convert the SV, or the SV is magical, or
1957 the SV has overloading, in which case we can't or mustn't
1958 or mustn't call it again. */
1960 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1961 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1963 /* It's a real UTF-8 SV, and it's not going to change under
1964 us. Take advantage of any cache. */
1966 I32 len_I32 = length;
1968 /* Convert the start and end character positions to bytes.
1969 Remember that the second argument to sv_pos_u2b is relative
1971 sv_pos_u2b(bufsv, &start, &len_I32);
1978 buffer = buffer+offset;
1980 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1981 if (IoTYPE(io) == IoTYPE_SOCKET) {
1982 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1988 /* See the note at doio.c:do_print about filesize limits. --jhi */
1989 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1995 const int flags = SvIVx(*++MARK);
1998 char * const sockbuf = SvPVx(*++MARK, mlen);
1999 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
2000 flags, (struct sockaddr *)sockbuf, mlen);
2004 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2009 DIE(aTHX_ PL_no_sock_func, "send");
2016 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2019 #if Size_t_size > IVSIZE
2039 * in Perl 5.12 and later, the additional parameter is a bitmask:
2042 * 2 = eof() <- ARGV magic
2044 * I'll rely on the compiler's trace flow analysis to decide whether to
2045 * actually assign this out here, or punt it into the only block where it is
2046 * used. Doing it out here is DRY on the condition logic.
2051 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2057 if (PL_op->op_flags & OPf_SPECIAL) {
2058 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2062 gv = PL_last_in_gv; /* eof */
2070 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2071 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
2074 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2075 if (io && !IoIFP(io)) {
2076 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2078 IoFLAGS(io) &= ~IOf_START;
2079 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2081 sv_setpvs(GvSV(gv), "-");
2083 GvSV(gv) = newSVpvs("-");
2084 SvSETMAGIC(GvSV(gv));
2086 else if (!nextargv(gv))
2091 PUSHs(boolSV(do_eof(gv)));
2102 PL_last_in_gv = MUTABLE_GV(POPs);
2107 if (gv && (io = GvIO(gv))) {
2108 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2110 return tied_handle_method("TELL", SP, io, mg);
2115 SETERRNO(EBADF,RMS_IFI);
2120 #if LSEEKSIZE > IVSIZE
2121 PUSHn( do_tell(gv) );
2123 PUSHi( do_tell(gv) );
2131 const int whence = POPi;
2132 #if LSEEKSIZE > IVSIZE
2133 const Off_t offset = (Off_t)SvNVx(POPs);
2135 const Off_t offset = (Off_t)SvIVx(POPs);
2138 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2141 if (gv && (io = GvIO(gv))) {
2142 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2144 #if LSEEKSIZE > IVSIZE
2145 SV *const offset_sv = newSVnv((NV) offset);
2147 SV *const offset_sv = newSViv(offset);
2150 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
2155 if (PL_op->op_type == OP_SEEK)
2156 PUSHs(boolSV(do_seek(gv, offset, whence)));
2158 const Off_t sought = do_sysseek(gv, offset, whence);
2160 PUSHs(&PL_sv_undef);
2162 SV* const sv = sought ?
2163 #if LSEEKSIZE > IVSIZE
2168 : newSVpvn(zero_but_true, ZBTLEN);
2179 /* There seems to be no consensus on the length type of truncate()
2180 * and ftruncate(), both off_t and size_t have supporters. In
2181 * general one would think that when using large files, off_t is
2182 * at least as wide as size_t, so using an off_t should be okay. */
2183 /* XXX Configure probe for the length type of *truncate() needed XXX */
2186 #if Off_t_size > IVSIZE
2191 /* Checking for length < 0 is problematic as the type might or
2192 * might not be signed: if it is not, clever compilers will moan. */
2193 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2200 if (PL_op->op_flags & OPf_SPECIAL) {
2201 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2210 TAINT_PROPER("truncate");
2211 if (!(fp = IoIFP(io))) {
2217 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2219 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2226 SV * const sv = POPs;
2229 if (isGV_with_GP(sv)) {
2230 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2231 goto do_ftruncate_gv;
2233 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2234 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2235 goto do_ftruncate_gv;
2237 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2238 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2239 goto do_ftruncate_io;
2242 name = SvPV_nolen_const(sv);
2243 TAINT_PROPER("truncate");
2245 if (truncate(name, len) < 0)
2249 const int tmpfd = PerlLIO_open(name, O_RDWR);
2254 if (my_chsize(tmpfd, len) < 0)
2256 PerlLIO_close(tmpfd);
2265 SETERRNO(EBADF,RMS_IFI);
2273 SV * const argsv = POPs;
2274 const unsigned int func = POPu;
2275 const int optype = PL_op->op_type;
2276 GV * const gv = MUTABLE_GV(POPs);
2277 IO * const io = gv ? GvIOn(gv) : NULL;
2281 if (!io || !argsv || !IoIFP(io)) {
2282 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2283 report_evil_fh(gv, io, PL_op->op_type);
2284 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2288 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2291 s = SvPV_force(argsv, len);
2292 need = IOCPARM_LEN(func);
2294 s = Sv_Grow(argsv, need + 1);
2295 SvCUR_set(argsv, need);
2298 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2301 retval = SvIV(argsv);
2302 s = INT2PTR(char*,retval); /* ouch */
2305 TAINT_PROPER(PL_op_desc[optype]);
2307 if (optype == OP_IOCTL)
2309 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2311 DIE(aTHX_ "ioctl is not implemented");
2315 DIE(aTHX_ "fcntl is not implemented");
2317 #if defined(OS2) && defined(__EMX__)
2318 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2320 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2324 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2326 if (s[SvCUR(argsv)] != 17)
2327 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2329 s[SvCUR(argsv)] = 0; /* put our null back */
2330 SvSETMAGIC(argsv); /* Assume it has changed */
2339 PUSHp(zero_but_true, ZBTLEN);
2352 const int argtype = POPi;
2353 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2355 if (gv && (io = GvIO(gv)))
2361 /* XXX Looks to me like io is always NULL at this point */
2363 (void)PerlIO_flush(fp);
2364 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2367 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2368 report_evil_fh(gv, io, PL_op->op_type);
2370 SETERRNO(EBADF,RMS_IFI);
2375 DIE(aTHX_ PL_no_func, "flock()");
2385 const int protocol = POPi;
2386 const int type = POPi;
2387 const int domain = POPi;
2388 GV * const gv = MUTABLE_GV(POPs);
2389 register IO * const io = gv ? GvIOn(gv) : NULL;
2393 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2394 report_evil_fh(gv, io, PL_op->op_type);
2395 if (io && IoIFP(io))
2396 do_close(gv, FALSE);
2397 SETERRNO(EBADF,LIB_INVARG);
2402 do_close(gv, FALSE);
2404 TAINT_PROPER("socket");
2405 fd = PerlSock_socket(domain, type, protocol);
2408 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2409 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2410 IoTYPE(io) = IoTYPE_SOCKET;
2411 if (!IoIFP(io) || !IoOFP(io)) {
2412 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2413 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2414 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2417 #if defined(HAS_FCNTL) && defined(F_SETFD)
2418 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2422 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2427 DIE(aTHX_ PL_no_sock_func, "socket");
2433 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2435 const int protocol = POPi;
2436 const int type = POPi;
2437 const int domain = POPi;
2438 GV * const gv2 = MUTABLE_GV(POPs);
2439 GV * const gv1 = MUTABLE_GV(POPs);
2440 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2441 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2444 if (!gv1 || !gv2 || !io1 || !io2) {
2445 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2447 report_evil_fh(gv1, io1, PL_op->op_type);
2449 report_evil_fh(gv1, io2, PL_op->op_type);
2451 if (io1 && IoIFP(io1))
2452 do_close(gv1, FALSE);
2453 if (io2 && IoIFP(io2))
2454 do_close(gv2, FALSE);
2459 do_close(gv1, FALSE);
2461 do_close(gv2, FALSE);
2463 TAINT_PROPER("socketpair");
2464 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2466 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2467 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2468 IoTYPE(io1) = IoTYPE_SOCKET;
2469 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2470 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2471 IoTYPE(io2) = IoTYPE_SOCKET;
2472 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2473 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2474 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2475 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2476 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2477 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2478 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2481 #if defined(HAS_FCNTL) && defined(F_SETFD)
2482 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2483 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2488 DIE(aTHX_ PL_no_sock_func, "socketpair");
2496 SV * const addrsv = POPs;
2497 /* OK, so on what platform does bind modify addr? */
2499 GV * const gv = MUTABLE_GV(POPs);
2500 register IO * const io = GvIOn(gv);
2503 if (!io || !IoIFP(io))
2506 addr = SvPV_const(addrsv, len);
2507 TAINT_PROPER("bind");
2508 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2514 if (ckWARN(WARN_CLOSED))
2515 report_evil_fh(gv, io, PL_op->op_type);
2516 SETERRNO(EBADF,SS_IVCHAN);
2519 DIE(aTHX_ PL_no_sock_func, "bind");
2527 SV * const addrsv = POPs;
2528 GV * const gv = MUTABLE_GV(POPs);
2529 register IO * const io = GvIOn(gv);
2533 if (!io || !IoIFP(io))
2536 addr = SvPV_const(addrsv, len);
2537 TAINT_PROPER("connect");
2538 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2544 if (ckWARN(WARN_CLOSED))
2545 report_evil_fh(gv, io, PL_op->op_type);
2546 SETERRNO(EBADF,SS_IVCHAN);
2549 DIE(aTHX_ PL_no_sock_func, "connect");
2557 const int backlog = POPi;
2558 GV * const gv = MUTABLE_GV(POPs);
2559 register IO * const io = gv ? GvIOn(gv) : NULL;
2561 if (!gv || !io || !IoIFP(io))
2564 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2570 if (ckWARN(WARN_CLOSED))
2571 report_evil_fh(gv, io, PL_op->op_type);
2572 SETERRNO(EBADF,SS_IVCHAN);
2575 DIE(aTHX_ PL_no_sock_func, "listen");
2585 char namebuf[MAXPATHLEN];
2586 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2587 Sock_size_t len = sizeof (struct sockaddr_in);
2589 Sock_size_t len = sizeof namebuf;
2591 GV * const ggv = MUTABLE_GV(POPs);
2592 GV * const ngv = MUTABLE_GV(POPs);
2601 if (!gstio || !IoIFP(gstio))
2605 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2608 /* Some platforms indicate zero length when an AF_UNIX client is
2609 * not bound. Simulate a non-zero-length sockaddr structure in
2611 namebuf[0] = 0; /* sun_len */
2612 namebuf[1] = AF_UNIX; /* sun_family */
2620 do_close(ngv, FALSE);
2621 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2622 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2623 IoTYPE(nstio) = IoTYPE_SOCKET;
2624 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2625 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2626 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2627 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2630 #if defined(HAS_FCNTL) && defined(F_SETFD)
2631 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2635 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2636 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2638 #ifdef __SCO_VERSION__
2639 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2642 PUSHp(namebuf, len);
2646 if (ckWARN(WARN_CLOSED))
2647 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2648 SETERRNO(EBADF,SS_IVCHAN);
2654 DIE(aTHX_ PL_no_sock_func, "accept");
2662 const int how = POPi;
2663 GV * const gv = MUTABLE_GV(POPs);
2664 register IO * const io = GvIOn(gv);
2666 if (!io || !IoIFP(io))
2669 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2673 if (ckWARN(WARN_CLOSED))
2674 report_evil_fh(gv, io, PL_op->op_type);
2675 SETERRNO(EBADF,SS_IVCHAN);
2678 DIE(aTHX_ PL_no_sock_func, "shutdown");
2686 const int optype = PL_op->op_type;
2687 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2688 const unsigned int optname = (unsigned int) POPi;
2689 const unsigned int lvl = (unsigned int) POPi;
2690 GV * const gv = MUTABLE_GV(POPs);
2691 register IO * const io = GvIOn(gv);
2695 if (!io || !IoIFP(io))
2698 fd = PerlIO_fileno(IoIFP(io));
2702 (void)SvPOK_only(sv);
2706 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2713 #if defined(__SYMBIAN32__)
2714 # define SETSOCKOPT_OPTION_VALUE_T void *
2716 # define SETSOCKOPT_OPTION_VALUE_T const char *
2718 /* XXX TODO: We need to have a proper type (a Configure probe,
2719 * etc.) for what the C headers think of the third argument of
2720 * setsockopt(), the option_value read-only buffer: is it
2721 * a "char *", or a "void *", const or not. Some compilers
2722 * don't take kindly to e.g. assuming that "char *" implicitly
2723 * promotes to a "void *", or to explicitly promoting/demoting
2724 * consts to non/vice versa. The "const void *" is the SUS
2725 * definition, but that does not fly everywhere for the above
2727 SETSOCKOPT_OPTION_VALUE_T buf;
2731 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2735 aint = (int)SvIV(sv);
2736 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2739 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2748 if (ckWARN(WARN_CLOSED))
2749 report_evil_fh(gv, io, optype);
2750 SETERRNO(EBADF,SS_IVCHAN);
2755 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2763 const int optype = PL_op->op_type;
2764 GV * const gv = MUTABLE_GV(POPs);
2765 register IO * const io = GvIOn(gv);
2770 if (!io || !IoIFP(io))
2773 sv = sv_2mortal(newSV(257));
2774 (void)SvPOK_only(sv);
2778 fd = PerlIO_fileno(IoIFP(io));
2780 case OP_GETSOCKNAME:
2781 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2784 case OP_GETPEERNAME:
2785 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2787 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2789 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";
2790 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2791 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2792 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2793 sizeof(u_short) + sizeof(struct in_addr))) {
2800 #ifdef BOGUS_GETNAME_RETURN
2801 /* Interactive Unix, getpeername() and getsockname()
2802 does not return valid namelen */
2803 if (len == BOGUS_GETNAME_RETURN)
2804 len = sizeof(struct sockaddr);
2812 if (ckWARN(WARN_CLOSED))
2813 report_evil_fh(gv, io, optype);
2814 SETERRNO(EBADF,SS_IVCHAN);
2819 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2834 if (PL_op->op_flags & OPf_REF) {
2836 if (PL_op->op_type == OP_LSTAT) {
2837 if (gv != PL_defgv) {
2838 do_fstat_warning_check:
2839 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2840 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2841 } else if (PL_laststype != OP_LSTAT)
2842 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2846 if (gv != PL_defgv) {
2847 PL_laststype = OP_STAT;
2849 sv_setpvs(PL_statname, "");
2856 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2857 } else if (IoDIRP(io)) {
2859 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2861 PL_laststatval = -1;
2867 if (PL_laststatval < 0) {
2868 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2869 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2874 SV* const sv = POPs;
2875 if (isGV_with_GP(sv)) {
2876 gv = MUTABLE_GV(sv);
2878 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2879 gv = MUTABLE_GV(SvRV(sv));
2880 if (PL_op->op_type == OP_LSTAT)
2881 goto do_fstat_warning_check;
2883 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2884 io = MUTABLE_IO(SvRV(sv));
2885 if (PL_op->op_type == OP_LSTAT)
2886 goto do_fstat_warning_check;
2887 goto do_fstat_have_io;
2890 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2892 PL_laststype = PL_op->op_type;
2893 if (PL_op->op_type == OP_LSTAT)
2894 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2896 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2897 if (PL_laststatval < 0) {
2898 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2899 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2905 if (gimme != G_ARRAY) {
2906 if (gimme != G_VOID)
2907 XPUSHs(boolSV(max));
2913 mPUSHi(PL_statcache.st_dev);
2914 mPUSHi(PL_statcache.st_ino);
2915 mPUSHu(PL_statcache.st_mode);
2916 mPUSHu(PL_statcache.st_nlink);
2917 #if Uid_t_size > IVSIZE
2918 mPUSHn(PL_statcache.st_uid);
2920 # if Uid_t_sign <= 0
2921 mPUSHi(PL_statcache.st_uid);
2923 mPUSHu(PL_statcache.st_uid);
2926 #if Gid_t_size > IVSIZE
2927 mPUSHn(PL_statcache.st_gid);
2929 # if Gid_t_sign <= 0
2930 mPUSHi(PL_statcache.st_gid);
2932 mPUSHu(PL_statcache.st_gid);
2935 #ifdef USE_STAT_RDEV
2936 mPUSHi(PL_statcache.st_rdev);
2938 PUSHs(newSVpvs_flags("", SVs_TEMP));
2940 #if Off_t_size > IVSIZE
2941 mPUSHn(PL_statcache.st_size);
2943 mPUSHi(PL_statcache.st_size);
2946 mPUSHn(PL_statcache.st_atime);
2947 mPUSHn(PL_statcache.st_mtime);
2948 mPUSHn(PL_statcache.st_ctime);
2950 mPUSHi(PL_statcache.st_atime);
2951 mPUSHi(PL_statcache.st_mtime);
2952 mPUSHi(PL_statcache.st_ctime);
2954 #ifdef USE_STAT_BLOCKS
2955 mPUSHu(PL_statcache.st_blksize);
2956 mPUSHu(PL_statcache.st_blocks);
2958 PUSHs(newSVpvs_flags("", SVs_TEMP));
2959 PUSHs(newSVpvs_flags("", SVs_TEMP));
2965 #define tryAMAGICftest_MG(chr) STMT_START { \
2966 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2967 && S_try_amagic_ftest(aTHX_ chr)) \
2972 S_try_amagic_ftest(pTHX_ char chr) {
2975 SV* const arg = TOPs;
2980 if ((PL_op->op_flags & OPf_KIDS)
2983 const char tmpchr = chr;
2985 SV * const tmpsv = amagic_call(arg,
2986 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2987 ftest_amg, AMGf_unary);
2994 next = PL_op->op_next;
2995 if (next->op_type >= OP_FTRREAD &&
2996 next->op_type <= OP_FTBINARY &&
2997 next->op_private & OPpFT_STACKED
3000 /* leave the object alone */
3012 /* This macro is used by the stacked filetest operators :
3013 * if the previous filetest failed, short-circuit and pass its value.
3014 * Else, discard it from the stack and continue. --rgs
3016 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
3017 if (!SvTRUE(TOPs)) { RETURN; } \
3018 else { (void)POPs; PUTBACK; } \
3025 /* Not const, because things tweak this below. Not bool, because there's
3026 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3027 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3028 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3029 /* Giving some sort of initial value silences compilers. */
3031 int access_mode = R_OK;
3033 int access_mode = 0;
3036 /* access_mode is never used, but leaving use_access in makes the
3037 conditional compiling below much clearer. */
3040 int stat_mode = S_IRUSR;
3042 bool effective = FALSE;
3046 switch (PL_op->op_type) {
3047 case OP_FTRREAD: opchar = 'R'; break;
3048 case OP_FTRWRITE: opchar = 'W'; break;
3049 case OP_FTREXEC: opchar = 'X'; break;
3050 case OP_FTEREAD: opchar = 'r'; break;
3051 case OP_FTEWRITE: opchar = 'w'; break;
3052 case OP_FTEEXEC: opchar = 'x'; break;
3054 tryAMAGICftest_MG(opchar);
3056 STACKED_FTEST_CHECK;
3058 switch (PL_op->op_type) {
3060 #if !(defined(HAS_ACCESS) && defined(R_OK))
3066 #if defined(HAS_ACCESS) && defined(W_OK)
3071 stat_mode = S_IWUSR;
3075 #if defined(HAS_ACCESS) && defined(X_OK)
3080 stat_mode = S_IXUSR;
3084 #ifdef PERL_EFF_ACCESS
3087 stat_mode = S_IWUSR;
3091 #ifndef PERL_EFF_ACCESS
3098 #ifdef PERL_EFF_ACCESS
3103 stat_mode = S_IXUSR;
3109 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3110 const char *name = POPpx;
3112 # ifdef PERL_EFF_ACCESS
3113 result = PERL_EFF_ACCESS(name, access_mode);
3115 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3121 result = access(name, access_mode);
3123 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3134 result = my_stat_flags(0);
3138 if (cando(stat_mode, effective, &PL_statcache))
3147 const int op_type = PL_op->op_type;
3152 case OP_FTIS: opchar = 'e'; break;
3153 case OP_FTSIZE: opchar = 's'; break;
3154 case OP_FTMTIME: opchar = 'M'; break;
3155 case OP_FTCTIME: opchar = 'C'; break;
3156 case OP_FTATIME: opchar = 'A'; break;
3158 tryAMAGICftest_MG(opchar);
3160 STACKED_FTEST_CHECK;
3162 result = my_stat_flags(0);
3166 if (op_type == OP_FTIS)
3169 /* You can't dTARGET inside OP_FTIS, because you'll get
3170 "panic: pad_sv po" - the op is not flagged to have a target. */
3174 #if Off_t_size > IVSIZE
3175 PUSHn(PL_statcache.st_size);
3177 PUSHi(PL_statcache.st_size);
3181 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3184 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3187 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3201 switch (PL_op->op_type) {
3202 case OP_FTROWNED: opchar = 'O'; break;
3203 case OP_FTEOWNED: opchar = 'o'; break;
3204 case OP_FTZERO: opchar = 'z'; break;
3205 case OP_FTSOCK: opchar = 'S'; break;
3206 case OP_FTCHR: opchar = 'c'; break;
3207 case OP_FTBLK: opchar = 'b'; break;
3208 case OP_FTFILE: opchar = 'f'; break;
3209 case OP_FTDIR: opchar = 'd'; break;
3210 case OP_FTPIPE: opchar = 'p'; break;
3211 case OP_FTSUID: opchar = 'u'; break;
3212 case OP_FTSGID: opchar = 'g'; break;
3213 case OP_FTSVTX: opchar = 'k'; break;
3215 tryAMAGICftest_MG(opchar);
3217 STACKED_FTEST_CHECK;
3219 /* I believe that all these three are likely to be defined on most every
3220 system these days. */
3222 if(PL_op->op_type == OP_FTSUID) {
3223 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3229 if(PL_op->op_type == OP_FTSGID) {
3230 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3236 if(PL_op->op_type == OP_FTSVTX) {
3237 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3243 result = my_stat_flags(0);
3247 switch (PL_op->op_type) {
3249 if (PL_statcache.st_uid == PL_uid)
3253 if (PL_statcache.st_uid == PL_euid)
3257 if (PL_statcache.st_size == 0)
3261 if (S_ISSOCK(PL_statcache.st_mode))
3265 if (S_ISCHR(PL_statcache.st_mode))
3269 if (S_ISBLK(PL_statcache.st_mode))
3273 if (S_ISREG(PL_statcache.st_mode))
3277 if (S_ISDIR(PL_statcache.st_mode))
3281 if (S_ISFIFO(PL_statcache.st_mode))
3286 if (PL_statcache.st_mode & S_ISUID)
3292 if (PL_statcache.st_mode & S_ISGID)
3298 if (PL_statcache.st_mode & S_ISVTX)
3312 tryAMAGICftest_MG('l');
3313 result = my_lstat_flags(0);
3318 if (S_ISLNK(PL_statcache.st_mode))
3333 tryAMAGICftest_MG('t');
3335 STACKED_FTEST_CHECK;
3337 if (PL_op->op_flags & OPf_REF)
3339 else if (isGV(TOPs))
3340 gv = MUTABLE_GV(POPs);
3341 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3342 gv = MUTABLE_GV(SvRV(POPs));
3345 name = SvPV_nomg(tmpsv, namelen);
3346 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3349 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3350 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3351 else if (tmpsv && SvOK(tmpsv)) {
3359 if (PerlLIO_isatty(fd))
3364 #if defined(atarist) /* this will work with atariST. Configure will
3365 make guesses for other systems. */
3366 # define FILE_base(f) ((f)->_base)
3367 # define FILE_ptr(f) ((f)->_ptr)
3368 # define FILE_cnt(f) ((f)->_cnt)
3369 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3380 register STDCHAR *s;
3386 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3388 STACKED_FTEST_CHECK;
3390 if (PL_op->op_flags & OPf_REF)
3392 else if (isGV(TOPs))
3393 gv = MUTABLE_GV(POPs);
3394 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3395 gv = MUTABLE_GV(SvRV(POPs));
3401 if (gv == PL_defgv) {
3403 io = GvIO(PL_statgv);
3406 goto really_filename;
3411 PL_laststatval = -1;
3412 sv_setpvs(PL_statname, "");
3413 io = GvIO(PL_statgv);
3415 if (io && IoIFP(io)) {
3416 if (! PerlIO_has_base(IoIFP(io)))
3417 DIE(aTHX_ "-T and -B not implemented on filehandles");
3418 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3419 if (PL_laststatval < 0)
3421 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3422 if (PL_op->op_type == OP_FTTEXT)
3427 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3428 i = PerlIO_getc(IoIFP(io));
3430 (void)PerlIO_ungetc(IoIFP(io),i);
3432 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3434 len = PerlIO_get_bufsiz(IoIFP(io));
3435 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3436 /* sfio can have large buffers - limit to 512 */
3441 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3443 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3445 SETERRNO(EBADF,RMS_IFI);
3453 PL_laststype = OP_STAT;
3454 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3455 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3456 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3458 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3461 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3462 if (PL_laststatval < 0) {
3463 (void)PerlIO_close(fp);
3466 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3467 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3468 (void)PerlIO_close(fp);
3470 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3471 RETPUSHNO; /* special case NFS directories */
3472 RETPUSHYES; /* null file is anything */
3477 /* now scan s to look for textiness */
3478 /* XXX ASCII dependent code */
3480 #if defined(DOSISH) || defined(USEMYBINMODE)
3481 /* ignore trailing ^Z on short files */
3482 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3486 for (i = 0; i < len; i++, s++) {
3487 if (!*s) { /* null never allowed in text */
3492 else if (!(isPRINT(*s) || isSPACE(*s)))
3495 else if (*s & 128) {
3497 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3500 /* utf8 characters don't count as odd */
3501 if (UTF8_IS_START(*s)) {
3502 int ulen = UTF8SKIP(s);
3503 if (ulen < len - i) {
3505 for (j = 1; j < ulen; j++) {
3506 if (!UTF8_IS_CONTINUATION(s[j]))
3509 --ulen; /* loop does extra increment */
3519 *s != '\n' && *s != '\r' && *s != '\b' &&
3520 *s != '\t' && *s != '\f' && *s != 27)
3525 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3536 const char *tmps = NULL;
3540 SV * const sv = POPs;
3541 if (PL_op->op_flags & OPf_SPECIAL) {
3542 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3544 else if (isGV_with_GP(sv)) {
3545 gv = MUTABLE_GV(sv);
3547 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3548 gv = MUTABLE_GV(SvRV(sv));
3551 tmps = SvPV_nolen_const(sv);
3555 if( !gv && (!tmps || !*tmps) ) {
3556 HV * const table = GvHVn(PL_envgv);
3559 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3560 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3562 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3567 deprecate("chdir('') or chdir(undef) as chdir()");
3568 tmps = SvPV_nolen_const(*svp);
3572 TAINT_PROPER("chdir");
3577 TAINT_PROPER("chdir");
3580 IO* const io = GvIO(gv);
3583 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3584 } else if (IoIFP(io)) {
3585 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3588 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3589 report_evil_fh(gv, io, PL_op->op_type);
3590 SETERRNO(EBADF, RMS_IFI);
3595 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3596 report_evil_fh(gv, io, PL_op->op_type);
3597 SETERRNO(EBADF,RMS_IFI);
3601 DIE(aTHX_ PL_no_func, "fchdir");
3605 PUSHi( PerlDir_chdir(tmps) >= 0 );
3607 /* Clear the DEFAULT element of ENV so we'll get the new value
3609 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3616 dVAR; dSP; dMARK; dTARGET;
3617 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3628 char * const tmps = POPpx;
3629 TAINT_PROPER("chroot");
3630 PUSHi( chroot(tmps) >= 0 );
3633 DIE(aTHX_ PL_no_func, "chroot");
3641 const char * const tmps2 = POPpconstx;
3642 const char * const tmps = SvPV_nolen_const(TOPs);
3643 TAINT_PROPER("rename");
3645 anum = PerlLIO_rename(tmps, tmps2);
3647 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3648 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3651 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3652 (void)UNLINK(tmps2);
3653 if (!(anum = link(tmps, tmps2)))
3654 anum = UNLINK(tmps);
3662 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3666 const int op_type = PL_op->op_type;
3670 if (op_type == OP_LINK)
3671 DIE(aTHX_ PL_no_func, "link");
3673 # ifndef HAS_SYMLINK
3674 if (op_type == OP_SYMLINK)
3675 DIE(aTHX_ PL_no_func, "symlink");
3679 const char * const tmps2 = POPpconstx;
3680 const char * const tmps = SvPV_nolen_const(TOPs);
3681 TAINT_PROPER(PL_op_desc[op_type]);
3683 # if defined(HAS_LINK)
3684 # if defined(HAS_SYMLINK)
3685 /* Both present - need to choose which. */
3686 (op_type == OP_LINK) ?
3687 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3689 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3690 PerlLIO_link(tmps, tmps2);
3693 # if defined(HAS_SYMLINK)
3694 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3695 symlink(tmps, tmps2);
3700 SETi( result >= 0 );
3707 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3718 char buf[MAXPATHLEN];
3721 #ifndef INCOMPLETE_TAINTS
3725 len = readlink(tmps, buf, sizeof(buf) - 1);
3732 RETSETUNDEF; /* just pretend it's a normal file */
3736 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3738 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3740 char * const save_filename = filename;
3745 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3747 PERL_ARGS_ASSERT_DOONELINER;
3749 Newx(cmdline, size, char);
3750 my_strlcpy(cmdline, cmd, size);
3751 my_strlcat(cmdline, " ", size);
3752 for (s = cmdline + strlen(cmdline); *filename; ) {
3756 if (s - cmdline < size)
3757 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3758 myfp = PerlProc_popen(cmdline, "r");
3762 SV * const tmpsv = sv_newmortal();
3763 /* Need to save/restore 'PL_rs' ?? */
3764 s = sv_gets(tmpsv, myfp, 0);
3765 (void)PerlProc_pclose(myfp);
3769 #ifdef HAS_SYS_ERRLIST
3774 /* you don't see this */
3775 const char * const errmsg =
3776 #ifdef HAS_SYS_ERRLIST
3784 if (instr(s, errmsg)) {
3791 #define EACCES EPERM
3793 if (instr(s, "cannot make"))
3794 SETERRNO(EEXIST,RMS_FEX);
3795 else if (instr(s, "existing file"))
3796 SETERRNO(EEXIST,RMS_FEX);
3797 else if (instr(s, "ile exists"))
3798 SETERRNO(EEXIST,RMS_FEX);
3799 else if (instr(s, "non-exist"))
3800 SETERRNO(ENOENT,RMS_FNF);
3801 else if (instr(s, "does not exist"))
3802 SETERRNO(ENOENT,RMS_FNF);
3803 else if (instr(s, "not empty"))
3804 SETERRNO(EBUSY,SS_DEVOFFLINE);
3805 else if (instr(s, "cannot access"))
3806 SETERRNO(EACCES,RMS_PRV);
3808 SETERRNO(EPERM,RMS_PRV);
3811 else { /* some mkdirs return no failure indication */
3812 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3813 if (PL_op->op_type == OP_RMDIR)
3818 SETERRNO(EACCES,RMS_PRV); /* a guess */
3827 /* This macro removes trailing slashes from a directory name.
3828 * Different operating and file systems take differently to
3829 * trailing slashes. According to POSIX 1003.1 1996 Edition
3830 * any number of trailing slashes should be allowed.
3831 * Thusly we snip them away so that even non-conforming
3832 * systems are happy.
3833 * We should probably do this "filtering" for all
3834 * the functions that expect (potentially) directory names:
3835 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3836 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3838 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3839 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3842 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3843 (tmps) = savepvn((tmps), (len)); \
3853 const int mode = (MAXARG > 1) ? POPi : 0777;
3855 TRIMSLASHES(tmps,len,copy);
3857 TAINT_PROPER("mkdir");
3859 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3863 SETi( dooneliner("mkdir", tmps) );
3864 oldumask = PerlLIO_umask(0);
3865 PerlLIO_umask(oldumask);
3866 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3881 TRIMSLASHES(tmps,len,copy);
3882 TAINT_PROPER("rmdir");
3884 SETi( PerlDir_rmdir(tmps) >= 0 );
3886 SETi( dooneliner("rmdir", tmps) );
3893 /* Directory calls. */
3897 #if defined(Direntry_t) && defined(HAS_READDIR)
3899 const char * const dirname = POPpconstx;
3900 GV * const gv = MUTABLE_GV(POPs);
3901 register IO * const io = GvIOn(gv);
3906 if ((IoIFP(io) || IoOFP(io)))
3907 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3908 "Opening filehandle %s also as a directory",
3911 PerlDir_close(IoDIRP(io));
3912 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3918 SETERRNO(EBADF,RMS_DIR);
3921 DIE(aTHX_ PL_no_dir_func, "opendir");
3927 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3928 DIE(aTHX_ PL_no_dir_func, "readdir");
3930 #if !defined(I_DIRENT) && !defined(VMS)
3931 Direntry_t *readdir (DIR *);
3937 const I32 gimme = GIMME;
3938 GV * const gv = MUTABLE_GV(POPs);
3939 register const Direntry_t *dp;
3940 register IO * const io = GvIOn(gv);
3942 if (!io || !IoDIRP(io)) {
3943 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3944 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3949 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3953 sv = newSVpvn(dp->d_name, dp->d_namlen);
3955 sv = newSVpv(dp->d_name, 0);
3957 #ifndef INCOMPLETE_TAINTS
3958 if (!(IoFLAGS(io) & IOf_UNTAINT))
3962 } while (gimme == G_ARRAY);
3964 if (!dp && gimme != G_ARRAY)
3971 SETERRNO(EBADF,RMS_ISI);
3972 if (GIMME == G_ARRAY)
3981 #if defined(HAS_TELLDIR) || defined(telldir)
3983 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3984 /* XXX netbsd still seemed to.
3985 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3986 --JHI 1999-Feb-02 */
3987 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3988 long telldir (DIR *);
3990 GV * const gv = MUTABLE_GV(POPs);
3991 register IO * const io = GvIOn(gv);
3993 if (!io || !IoDIRP(io)) {
3994 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3995 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3999 PUSHi( PerlDir_tell(IoDIRP(io)) );
4003 SETERRNO(EBADF,RMS_ISI);
4006 DIE(aTHX_ PL_no_dir_func, "telldir");
4012 #if defined(HAS_SEEKDIR) || defined(seekdir)
4014 const long along = POPl;
4015 GV * const gv = MUTABLE_GV(POPs);
4016 register IO * const io = GvIOn(gv);
4018 if (!io || !IoDIRP(io)) {
4019 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4020 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
4023 (void)PerlDir_seek(IoDIRP(io), along);
4028 SETERRNO(EBADF,RMS_ISI);
4031 DIE(aTHX_ PL_no_dir_func, "seekdir");
4037 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4039 GV * const gv = MUTABLE_GV(POPs);
4040 register IO * const io = GvIOn(gv);
4042 if (!io || !IoDIRP(io)) {
4043 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4044 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
4047 (void)PerlDir_rewind(IoDIRP(io));
4051 SETERRNO(EBADF,RMS_ISI);
4054 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4060 #if defined(Direntry_t) && defined(HAS_READDIR)
4062 GV * const gv = MUTABLE_GV(POPs);
4063 register IO * const io = GvIOn(gv);
4065 if (!io || !IoDIRP(io)) {
4066 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4067 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4070 #ifdef VOID_CLOSEDIR
4071 PerlDir_close(IoDIRP(io));
4073 if (PerlDir_close(IoDIRP(io)) < 0) {
4074 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4083 SETERRNO(EBADF,RMS_IFI);
4086 DIE(aTHX_ PL_no_dir_func, "closedir");
4090 /* Process control. */
4099 PERL_FLUSHALL_FOR_CHILD;
4100 childpid = PerlProc_fork();
4104 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4106 SvREADONLY_off(GvSV(tmpgv));
4107 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4108 SvREADONLY_on(GvSV(tmpgv));
4110 #ifdef THREADS_HAVE_PIDS
4111 PL_ppid = (IV)getppid();
4113 #ifdef PERL_USES_PL_PIDSTATUS
4114 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4120 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4125 PERL_FLUSHALL_FOR_CHILD;
4126 childpid = PerlProc_fork();
4132 DIE(aTHX_ PL_no_func, "fork");
4139 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4144 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4145 childpid = wait4pid(-1, &argflags, 0);
4147 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4152 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4153 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4154 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4156 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4161 DIE(aTHX_ PL_no_func, "wait");
4167 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4169 const int optype = POPi;
4170 const Pid_t pid = TOPi;
4174 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4175 result = wait4pid(pid, &argflags, optype);
4177 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4182 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4183 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4184 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4186 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4191 DIE(aTHX_ PL_no_func, "waitpid");
4197 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4198 #if defined(__LIBCATAMOUNT__)
4199 PL_statusvalue = -1;
4208 while (++MARK <= SP) {
4209 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4214 TAINT_PROPER("system");
4216 PERL_FLUSHALL_FOR_CHILD;
4217 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4223 if (PerlProc_pipe(pp) >= 0)
4225 while ((childpid = PerlProc_fork()) == -1) {
4226 if (errno != EAGAIN) {
4231 PerlLIO_close(pp[0]);
4232 PerlLIO_close(pp[1]);
4239 Sigsave_t ihand,qhand; /* place to save signals during system() */
4243 PerlLIO_close(pp[1]);
4245 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4246 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4249 result = wait4pid(childpid, &status, 0);
4250 } while (result == -1 && errno == EINTR);
4252 (void)rsignal_restore(SIGINT, &ihand);
4253 (void)rsignal_restore(SIGQUIT, &qhand);
4255 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4256 do_execfree(); /* free any memory child malloced on fork */
4263 while (n < sizeof(int)) {
4264 n1 = PerlLIO_read(pp[0],
4265 (void*)(((char*)&errkid)+n),
4271 PerlLIO_close(pp[0]);
4272 if (n) { /* Error */
4273 if (n != sizeof(int))
4274 DIE(aTHX_ "panic: kid popen errno read");
4275 errno = errkid; /* Propagate errno from kid */
4276 STATUS_NATIVE_CHILD_SET(-1);
4279 XPUSHi(STATUS_CURRENT);
4283 PerlLIO_close(pp[0]);
4284 #if defined(HAS_FCNTL) && defined(F_SETFD)
4285 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4288 if (PL_op->op_flags & OPf_STACKED) {
4289 SV * const really = *++MARK;
4290 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4292 else if (SP - MARK != 1)
4293 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4295 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4299 #else /* ! FORK or VMS or OS/2 */
4302 if (PL_op->op_flags & OPf_STACKED) {
4303 SV * const really = *++MARK;
4304 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4305 value = (I32)do_aspawn(really, MARK, SP);
4307 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4310 else if (SP - MARK != 1) {
4311 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4312 value = (I32)do_aspawn(NULL, MARK, SP);
4314 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4318 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4320 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4322 STATUS_NATIVE_CHILD_SET(value);
4325 XPUSHi(result ? value : STATUS_CURRENT);
4326 #endif /* !FORK or VMS or OS/2 */
4333 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4338 while (++MARK <= SP) {
4339 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4344 TAINT_PROPER("exec");
4346 PERL_FLUSHALL_FOR_CHILD;
4347 if (PL_op->op_flags & OPf_STACKED) {
4348 SV * const really = *++MARK;
4349 value = (I32)do_aexec(really, MARK, SP);
4351 else if (SP - MARK != 1)
4353 value = (I32)vms_do_aexec(NULL, MARK, SP);
4357 (void ) do_aspawn(NULL, MARK, SP);
4361 value = (I32)do_aexec(NULL, MARK, SP);
4366 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4369 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4372 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4386 # ifdef THREADS_HAVE_PIDS
4387 if (PL_ppid != 1 && getppid() == 1)
4388 /* maybe the parent process has died. Refresh ppid cache */
4392 XPUSHi( getppid() );
4396 DIE(aTHX_ PL_no_func, "getppid");
4405 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4408 pgrp = (I32)BSD_GETPGRP(pid);
4410 if (pid != 0 && pid != PerlProc_getpid())
4411 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4417 DIE(aTHX_ PL_no_func, "getpgrp()");
4437 TAINT_PROPER("setpgrp");
4439 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4441 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4442 || (pid != 0 && pid != PerlProc_getpid()))
4444 DIE(aTHX_ "setpgrp can't take arguments");
4446 SETi( setpgrp() >= 0 );
4447 #endif /* USE_BSDPGRP */
4450 DIE(aTHX_ PL_no_func, "setpgrp()");
4455 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4457 # define PRIORITY_WHICH_T(which) which
4462 #ifdef HAS_GETPRIORITY
4464 const int who = POPi;
4465 const int which = TOPi;
4466 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4469 DIE(aTHX_ PL_no_func, "getpriority()");
4475 #ifdef HAS_SETPRIORITY
4477 const int niceval = POPi;
4478 const int who = POPi;
4479 const int which = TOPi;
4480 TAINT_PROPER("setpriority");
4481 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4484 DIE(aTHX_ PL_no_func, "setpriority()");
4488 #undef PRIORITY_WHICH_T
4496 XPUSHn( time(NULL) );
4498 XPUSHi( time(NULL) );
4510 (void)PerlProc_times(&PL_timesbuf);
4512 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4513 /* struct tms, though same data */
4517 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4518 if (GIMME == G_ARRAY) {
4519 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4520 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4521 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4529 if (GIMME == G_ARRAY) {
4536 DIE(aTHX_ "times not implemented");
4538 #endif /* HAS_TIMES */
4541 /* The 32 bit int year limits the times we can represent to these
4542 boundaries with a few days wiggle room to account for time zone
4545 /* Sat Jan 3 00:00:00 -2147481748 */
4546 #define TIME_LOWER_BOUND -67768100567755200.0
4547 /* Sun Dec 29 12:00:00 2147483647 */
4548 #define TIME_UPPER_BOUND 67767976233316800.0
4557 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4558 static const char * const dayname[] =
4559 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4560 static const char * const monname[] =
4561 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4562 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4567 when = (Time64_T)now;
4570 NV input = Perl_floor(POPn);
4571 when = (Time64_T)input;
4572 if (when != input) {
4573 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4574 "%s(%.0" NVff ") too large", opname, input);
4578 if ( TIME_LOWER_BOUND > when ) {
4579 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4580 "%s(%.0" NVff ") too small", opname, when);
4583 else if( when > TIME_UPPER_BOUND ) {
4584 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4585 "%s(%.0" NVff ") too large", opname, when);
4589 if (PL_op->op_type == OP_LOCALTIME)
4590 err = S_localtime64_r(&when, &tmbuf);
4592 err = S_gmtime64_r(&when, &tmbuf);
4596 /* XXX %lld broken for quads */
4597 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4598 "%s(%.0" NVff ") failed", opname, when);
4601 if (GIMME != G_ARRAY) { /* scalar context */
4603 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4604 double year = (double)tmbuf.tm_year + 1900;
4611 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4612 dayname[tmbuf.tm_wday],
4613 monname[tmbuf.tm_mon],
4621 else { /* list context */
4627 mPUSHi(tmbuf.tm_sec);
4628 mPUSHi(tmbuf.tm_min);
4629 mPUSHi(tmbuf.tm_hour);
4630 mPUSHi(tmbuf.tm_mday);
4631 mPUSHi(tmbuf.tm_mon);
4632 mPUSHn(tmbuf.tm_year);
4633 mPUSHi(tmbuf.tm_wday);
4634 mPUSHi(tmbuf.tm_yday);
4635 mPUSHi(tmbuf.tm_isdst);
4646 anum = alarm((unsigned int)anum);
4652 DIE(aTHX_ PL_no_func, "alarm");
4663 (void)time(&lasttime);
4668 PerlProc_sleep((unsigned int)duration);
4671 XPUSHi(when - lasttime);
4675 /* Shared memory. */
4676 /* Merged with some message passing. */
4680 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4681 dVAR; dSP; dMARK; dTARGET;
4682 const int op_type = PL_op->op_type;
4687 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4690 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4693 value = (I32)(do_semop(MARK, SP) >= 0);
4696 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4712 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4713 dVAR; dSP; dMARK; dTARGET;
4714 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4721 DIE(aTHX_ "System V IPC is not implemented on this machine");
4727 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4728 dVAR; dSP; dMARK; dTARGET;
4729 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4737 PUSHp(zero_but_true, ZBTLEN);
4745 /* I can't const this further without getting warnings about the types of
4746 various arrays passed in from structures. */
4748 S_space_join_names_mortal(pTHX_ char *const *array)
4752 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4754 if (array && *array) {
4755 target = newSVpvs_flags("", SVs_TEMP);
4757 sv_catpv(target, *array);
4760 sv_catpvs(target, " ");
4763 target = sv_mortalcopy(&PL_sv_no);
4768 /* Get system info. */
4772 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4774 I32 which = PL_op->op_type;
4775 register char **elem;
4777 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4778 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4779 struct hostent *gethostbyname(Netdb_name_t);
4780 struct hostent *gethostent(void);
4782 struct hostent *hent = NULL;
4786 if (which == OP_GHBYNAME) {
4787 #ifdef HAS_GETHOSTBYNAME
4788 const char* const name = POPpbytex;
4789 hent = PerlSock_gethostbyname(name);
4791 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4794 else if (which == OP_GHBYADDR) {
4795 #ifdef HAS_GETHOSTBYADDR
4796 const int addrtype = POPi;
4797 SV * const addrsv = POPs;
4799 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4801 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4803 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4807 #ifdef HAS_GETHOSTENT
4808 hent = PerlSock_gethostent();
4810 DIE(aTHX_ PL_no_sock_func, "gethostent");
4813 #ifdef HOST_NOT_FOUND
4815 #ifdef USE_REENTRANT_API
4816 # ifdef USE_GETHOSTENT_ERRNO
4817 h_errno = PL_reentrant_buffer->_gethostent_errno;
4820 STATUS_UNIX_SET(h_errno);
4824 if (GIMME != G_ARRAY) {
4825 PUSHs(sv = sv_newmortal());
4827 if (which == OP_GHBYNAME) {
4829 sv_setpvn(sv, hent->h_addr, hent->h_length);
4832 sv_setpv(sv, (char*)hent->h_name);
4838 mPUSHs(newSVpv((char*)hent->h_name, 0));
4839 PUSHs(space_join_names_mortal(hent->h_aliases));
4840 mPUSHi(hent->h_addrtype);
4841 len = hent->h_length;
4844 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4845 mXPUSHp(*elem, len);
4849 mPUSHp(hent->h_addr, len);
4851 PUSHs(sv_mortalcopy(&PL_sv_no));
4856 DIE(aTHX_ PL_no_sock_func, "gethostent");
4862 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4864 I32 which = PL_op->op_type;
4866 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4867 struct netent *getnetbyaddr(Netdb_net_t, int);
4868 struct netent *getnetbyname(Netdb_name_t);
4869 struct netent *getnetent(void);
4871 struct netent *nent;
4873 if (which == OP_GNBYNAME){
4874 #ifdef HAS_GETNETBYNAME
4875 const char * const name = POPpbytex;
4876 nent = PerlSock_getnetbyname(name);
4878 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4881 else if (which == OP_GNBYADDR) {
4882 #ifdef HAS_GETNETBYADDR
4883 const int addrtype = POPi;
4884 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4885 nent = PerlSock_getnetbyaddr(addr, addrtype);
4887 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4891 #ifdef HAS_GETNETENT
4892 nent = PerlSock_getnetent();
4894 DIE(aTHX_ PL_no_sock_func, "getnetent");
4897 #ifdef HOST_NOT_FOUND
4899 #ifdef USE_REENTRANT_API
4900 # ifdef USE_GETNETENT_ERRNO
4901 h_errno = PL_reentrant_buffer->_getnetent_errno;
4904 STATUS_UNIX_SET(h_errno);
4909 if (GIMME != G_ARRAY) {
4910 PUSHs(sv = sv_newmortal());
4912 if (which == OP_GNBYNAME)
4913 sv_setiv(sv, (IV)nent->n_net);
4915 sv_setpv(sv, nent->n_name);
4921 mPUSHs(newSVpv(nent->n_name, 0));
4922 PUSHs(space_join_names_mortal(nent->n_aliases));
4923 mPUSHi(nent->n_addrtype);
4924 mPUSHi(nent->n_net);
4929 DIE(aTHX_ PL_no_sock_func, "getnetent");
4935 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4937 I32 which = PL_op->op_type;
4939 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4940 struct protoent *getprotobyname(Netdb_name_t);
4941 struct protoent *getprotobynumber(int);
4942 struct protoent *getprotoent(void);
4944 struct protoent *pent;
4946 if (which == OP_GPBYNAME) {
4947 #ifdef HAS_GETPROTOBYNAME
4948 const char* const name = POPpbytex;
4949 pent = PerlSock_getprotobyname(name);
4951 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4954 else if (which == OP_GPBYNUMBER) {
4955 #ifdef HAS_GETPROTOBYNUMBER
4956 const int number = POPi;
4957 pent = PerlSock_getprotobynumber(number);
4959 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4963 #ifdef HAS_GETPROTOENT
4964 pent = PerlSock_getprotoent();
4966 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4970 if (GIMME != G_ARRAY) {
4971 PUSHs(sv = sv_newmortal());
4973 if (which == OP_GPBYNAME)
4974 sv_setiv(sv, (IV)pent->p_proto);
4976 sv_setpv(sv, pent->p_name);
4982 mPUSHs(newSVpv(pent->p_name, 0));
4983 PUSHs(space_join_names_mortal(pent->p_aliases));
4984 mPUSHi(pent->p_proto);
4989 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4995 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4997 I32 which = PL_op->op_type;
4999 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5000 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5001 struct servent *getservbyport(int, Netdb_name_t);
5002 struct servent *getservent(void);
5004 struct servent *sent;
5006 if (which == OP_GSBYNAME) {
5007 #ifdef HAS_GETSERVBYNAME
5008 const char * const proto = POPpbytex;
5009 const char * const name = POPpbytex;
5010 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5012 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5015 else if (which == OP_GSBYPORT) {
5016 #ifdef HAS_GETSERVBYPORT
5017 const char * const proto = POPpbytex;
5018 unsigned short port = (unsigned short)POPu;
5020 port = PerlSock_htons(port);
5022 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5024 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5028 #ifdef HAS_GETSERVENT
5029 sent = PerlSock_getservent();
5031 DIE(aTHX_ PL_no_sock_func, "getservent");
5035 if (GIMME != G_ARRAY) {
5036 PUSHs(sv = sv_newmortal());
5038 if (which == OP_GSBYNAME) {
5040 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5042 sv_setiv(sv, (IV)(sent->s_port));
5046 sv_setpv(sv, sent->s_name);
5052 mPUSHs(newSVpv(sent->s_name, 0));
5053 PUSHs(space_join_names_mortal(sent->s_aliases));
5055 mPUSHi(PerlSock_ntohs(sent->s_port));
5057 mPUSHi(sent->s_port);
5059 mPUSHs(newSVpv(sent->s_proto, 0));
5064 DIE(aTHX_ PL_no_sock_func, "getservent");
5070 #ifdef HAS_SETHOSTENT
5072 PerlSock_sethostent(TOPi);
5075 DIE(aTHX_ PL_no_sock_func, "sethostent");
5081 #ifdef HAS_SETNETENT
5083 (void)PerlSock_setnetent(TOPi);
5086 DIE(aTHX_ PL_no_sock_func, "setnetent");
5092 #ifdef HAS_SETPROTOENT
5094 (void)PerlSock_setprotoent(TOPi);
5097 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5103 #ifdef HAS_SETSERVENT
5105 (void)PerlSock_setservent(TOPi);
5108 DIE(aTHX_ PL_no_sock_func, "setservent");
5114 #ifdef HAS_ENDHOSTENT
5116 PerlSock_endhostent();
5120 DIE(aTHX_ PL_no_sock_func, "endhostent");
5126 #ifdef HAS_ENDNETENT
5128 PerlSock_endnetent();
5132 DIE(aTHX_ PL_no_sock_func, "endnetent");
5138 #ifdef HAS_ENDPROTOENT
5140 PerlSock_endprotoent();
5144 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5150 #ifdef HAS_ENDSERVENT
5152 PerlSock_endservent();
5156 DIE(aTHX_ PL_no_sock_func, "endservent");
5164 I32 which = PL_op->op_type;
5166 struct passwd *pwent = NULL;
5168 * We currently support only the SysV getsp* shadow password interface.
5169 * The interface is declared in <shadow.h> and often one needs to link
5170 * with -lsecurity or some such.
5171 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5174 * AIX getpwnam() is clever enough to return the encrypted password
5175 * only if the caller (euid?) is root.
5177 * There are at least three other shadow password APIs. Many platforms
5178 * seem to contain more than one interface for accessing the shadow
5179 * password databases, possibly for compatibility reasons.
5180 * The getsp*() is by far he simplest one, the other two interfaces
5181 * are much more complicated, but also very similar to each other.
5186 * struct pr_passwd *getprpw*();
5187 * The password is in
5188 * char getprpw*(...).ufld.fd_encrypt[]
5189 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5194 * struct es_passwd *getespw*();
5195 * The password is in
5196 * char *(getespw*(...).ufld.fd_encrypt)
5197 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5200 * struct userpw *getuserpw();
5201 * The password is in
5202 * char *(getuserpw(...)).spw_upw_passwd
5203 * (but the de facto standard getpwnam() should work okay)
5205 * Mention I_PROT here so that Configure probes for it.
5207 * In HP-UX for getprpw*() the manual page claims that one should include
5208 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5209 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5210 * and pp_sys.c already includes <shadow.h> if there is such.
5212 * Note that <sys/security.h> is already probed for, but currently
5213 * it is only included in special cases.
5215 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5216 * be preferred interface, even though also the getprpw*() interface
5217 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5218 * One also needs to call set_auth_parameters() in main() before
5219 * doing anything else, whether one is using getespw*() or getprpw*().
5221 * Note that accessing the shadow databases can be magnitudes
5222 * slower than accessing the standard databases.
5227 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5228 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5229 * the pw_comment is left uninitialized. */
5230 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5236 const char* const name = POPpbytex;
5237 pwent = getpwnam(name);
5243 pwent = getpwuid(uid);
5247 # ifdef HAS_GETPWENT
5249 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5250 if (pwent) pwent = getpwnam(pwent->pw_name);
5253 DIE(aTHX_ PL_no_func, "getpwent");
5259 if (GIMME != G_ARRAY) {
5260 PUSHs(sv = sv_newmortal());
5262 if (which == OP_GPWNAM)
5263 # if Uid_t_sign <= 0
5264 sv_setiv(sv, (IV)pwent->pw_uid);
5266 sv_setuv(sv, (UV)pwent->pw_uid);
5269 sv_setpv(sv, pwent->pw_name);
5275 mPUSHs(newSVpv(pwent->pw_name, 0));
5279 /* If we have getspnam(), we try to dig up the shadow
5280 * password. If we are underprivileged, the shadow
5281 * interface will set the errno to EACCES or similar,
5282 * and return a null pointer. If this happens, we will
5283 * use the dummy password (usually "*" or "x") from the
5284 * standard password database.
5286 * In theory we could skip the shadow call completely
5287 * if euid != 0 but in practice we cannot know which
5288 * security measures are guarding the shadow databases
5289 * on a random platform.
5291 * Resist the urge to use additional shadow interfaces.
5292 * Divert the urge to writing an extension instead.
5295 /* Some AIX setups falsely(?) detect some getspnam(), which
5296 * has a different API than the Solaris/IRIX one. */
5297 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5300 const struct spwd * const spwent = getspnam(pwent->pw_name);
5301 /* Save and restore errno so that
5302 * underprivileged attempts seem
5303 * to have never made the unsccessful
5304 * attempt to retrieve the shadow password. */
5306 if (spwent && spwent->sp_pwdp)
5307 sv_setpv(sv, spwent->sp_pwdp);
5311 if (!SvPOK(sv)) /* Use the standard password, then. */
5312 sv_setpv(sv, pwent->pw_passwd);
5315 # ifndef INCOMPLETE_TAINTS
5316 /* passwd is tainted because user himself can diddle with it.
5317 * admittedly not much and in a very limited way, but nevertheless. */
5321 # if Uid_t_sign <= 0
5322 mPUSHi(pwent->pw_uid);
5324 mPUSHu(pwent->pw_uid);
5327 # if Uid_t_sign <= 0
5328 mPUSHi(pwent->pw_gid);
5330 mPUSHu(pwent->pw_gid);
5332 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5333 * because of the poor interface of the Perl getpw*(),
5334 * not because there's some standard/convention saying so.
5335 * A better interface would have been to return a hash,
5336 * but we are accursed by our history, alas. --jhi. */
5338 mPUSHi(pwent->pw_change);
5341 mPUSHi(pwent->pw_quota);
5344 mPUSHs(newSVpv(pwent->pw_age, 0));
5346 /* I think that you can never get this compiled, but just in case. */
5347 PUSHs(sv_mortalcopy(&PL_sv_no));
5352 /* pw_class and pw_comment are mutually exclusive--.
5353 * see the above note for pw_change, pw_quota, and pw_age. */
5355 mPUSHs(newSVpv(pwent->pw_class, 0));
5358 mPUSHs(newSVpv(pwent->pw_comment, 0));
5360 /* I think that you can never get this compiled, but just in case. */
5361 PUSHs(sv_mortalcopy(&PL_sv_no));
5366 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5368 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5370 # ifndef INCOMPLETE_TAINTS
5371 /* pw_gecos is tainted because user himself can diddle with it. */
5375 mPUSHs(newSVpv(pwent->pw_dir, 0));
5377 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5378 # ifndef INCOMPLETE_TAINTS
5379 /* pw_shell is tainted because user himself can diddle with it. */
5384 mPUSHi(pwent->pw_expire);
5389 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5395 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5400 DIE(aTHX_ PL_no_func, "setpwent");
5406 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5411 DIE(aTHX_ PL_no_func, "endpwent");
5419 const I32 which = PL_op->op_type;
5420 const struct group *grent;
5422 if (which == OP_GGRNAM) {
5423 const char* const name = POPpbytex;
5424 grent = (const struct group *)getgrnam(name);
5426 else if (which == OP_GGRGID) {
5427 const Gid_t gid = POPi;
5428 grent = (const struct group *)getgrgid(gid);
5432 grent = (struct group *)getgrent();
5434 DIE(aTHX_ PL_no_func, "getgrent");
5438 if (GIMME != G_ARRAY) {
5439 SV * const sv = sv_newmortal();
5443 if (which == OP_GGRNAM)
5445 sv_setiv(sv, (IV)grent->gr_gid);
5447 sv_setuv(sv, (UV)grent->gr_gid);
5450 sv_setpv(sv, grent->gr_name);
5456 mPUSHs(newSVpv(grent->gr_name, 0));
5459 mPUSHs(newSVpv(grent->gr_passwd, 0));
5461 PUSHs(sv_mortalcopy(&PL_sv_no));
5465 mPUSHi(grent->gr_gid);
5467 mPUSHu(grent->gr_gid);
5470 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5471 /* In UNICOS/mk (_CRAYMPP) the multithreading
5472 * versions (getgrnam_r, getgrgid_r)
5473 * seem to return an illegal pointer
5474 * as the group members list, gr_mem.
5475 * getgrent() doesn't even have a _r version
5476 * but the gr_mem is poisonous anyway.
5477 * So yes, you cannot get the list of group
5478 * members if building multithreaded in UNICOS/mk. */
5479 PUSHs(space_join_names_mortal(grent->gr_mem));
5485 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5491 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5496 DIE(aTHX_ PL_no_func, "setgrent");
5502 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5507 DIE(aTHX_ PL_no_func, "endgrent");
5517 if (!(tmps = PerlProc_getlogin()))
5519 PUSHp(tmps, strlen(tmps));
5522 DIE(aTHX_ PL_no_func, "getlogin");
5526 /* Miscellaneous. */
5531 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5532 register I32 items = SP - MARK;
5533 unsigned long a[20];
5538 while (++MARK <= SP) {
5539 if (SvTAINTED(*MARK)) {
5545 TAINT_PROPER("syscall");
5548 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5549 * or where sizeof(long) != sizeof(char*). But such machines will
5550 * not likely have syscall implemented either, so who cares?
5552 while (++MARK <= SP) {
5553 if (SvNIOK(*MARK) || !i)
5554 a[i++] = SvIV(*MARK);
5555 else if (*MARK == &PL_sv_undef)
5558 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5564 DIE(aTHX_ "Too many args to syscall");
5566 DIE(aTHX_ "Too few args to syscall");
5568 retval = syscall(a[0]);
5571 retval = syscall(a[0],a[1]);
5574 retval = syscall(a[0],a[1],a[2]);
5577 retval = syscall(a[0],a[1],a[2],a[3]);
5580 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5583 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5586 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5589 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5593 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5596 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5599 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5603 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5607 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5611 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5612 a[10],a[11],a[12],a[13]);
5614 #endif /* atarist */
5620 DIE(aTHX_ PL_no_func, "syscall");
5624 #ifdef FCNTL_EMULATE_FLOCK
5626 /* XXX Emulate flock() with fcntl().
5627 What's really needed is a good file locking module.
5631 fcntl_emulate_flock(int fd, int operation)
5636 switch (operation & ~LOCK_NB) {
5638 flock.l_type = F_RDLCK;
5641 flock.l_type = F_WRLCK;
5644 flock.l_type = F_UNLCK;
5650 flock.l_whence = SEEK_SET;
5651 flock.l_start = flock.l_len = (Off_t)0;
5653 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5654 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5655 errno = EWOULDBLOCK;
5659 #endif /* FCNTL_EMULATE_FLOCK */
5661 #ifdef LOCKF_EMULATE_FLOCK
5663 /* XXX Emulate flock() with lockf(). This is just to increase
5664 portability of scripts. The calls are not completely
5665 interchangeable. What's really needed is a good file
5669 /* The lockf() constants might have been defined in <unistd.h>.
5670 Unfortunately, <unistd.h> causes troubles on some mixed
5671 (BSD/POSIX) systems, such as SunOS 4.1.3.
5673 Further, the lockf() constants aren't POSIX, so they might not be
5674 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5675 just stick in the SVID values and be done with it. Sigh.
5679 # define F_ULOCK 0 /* Unlock a previously locked region */
5682 # define F_LOCK 1 /* Lock a region for exclusive use */
5685 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5688 # define F_TEST 3 /* Test a region for other processes locks */
5692 lockf_emulate_flock(int fd, int operation)
5698 /* flock locks entire file so for lockf we need to do the same */
5699 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5700 if (pos > 0) /* is seekable and needs to be repositioned */
5701 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5702 pos = -1; /* seek failed, so don't seek back afterwards */
5705 switch (operation) {
5707 /* LOCK_SH - get a shared lock */
5709 /* LOCK_EX - get an exclusive lock */
5711 i = lockf (fd, F_LOCK, 0);
5714 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5715 case LOCK_SH|LOCK_NB:
5716 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5717 case LOCK_EX|LOCK_NB:
5718 i = lockf (fd, F_TLOCK, 0);
5720 if ((errno == EAGAIN) || (errno == EACCES))
5721 errno = EWOULDBLOCK;
5724 /* LOCK_UN - unlock (non-blocking is a no-op) */
5726 case LOCK_UN|LOCK_NB:
5727 i = lockf (fd, F_ULOCK, 0);
5730 /* Default - can't decipher operation */
5737 if (pos > 0) /* need to restore position of the handle */
5738 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5743 #endif /* LOCKF_EMULATE_FLOCK */
5747 * c-indentation-style: bsd
5749 * indent-tabs-mode: t
5752 * ex: set ts=8 sts=4 sw=4 noet: