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;
1281 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1282 PUSHFORMAT(cx, retop);
1284 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1286 setdefout(gv); /* locally select filehandle so $% et al work */
1305 gv = MUTABLE_GV(POPs);
1319 goto not_a_format_reference;
1324 tmpsv = sv_newmortal();
1325 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1326 name = SvPV_nolen_const(tmpsv);
1328 DIE(aTHX_ "Undefined format \"%s\" called", name);
1330 not_a_format_reference:
1331 DIE(aTHX_ "Not a format reference");
1334 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
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 if (cv && CvCLONE(cv))
1425 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1426 return doform(cv, gv, PL_op);
1430 POPBLOCK(cx,PL_curpm);
1436 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1438 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1439 else if (ckWARN(WARN_CLOSED))
1440 report_evil_fh(gv, io, PL_op->op_type);
1445 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1446 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1448 if (!do_print(PL_formtarget, fp))
1451 FmLINES(PL_formtarget) = 0;
1452 SvCUR_set(PL_formtarget, 0);
1453 *SvEND(PL_formtarget) = '\0';
1454 if (IoFLAGS(io) & IOf_FLUSH)
1455 (void)PerlIO_flush(fp);
1460 PL_formtarget = PL_bodytarget;
1462 PERL_UNUSED_VAR(newsp);
1463 PERL_UNUSED_VAR(gimme);
1464 return cx->blk_sub.retop;
1469 dVAR; dSP; dMARK; dORIGMARK;
1475 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1477 if (gv && (io = GvIO(gv))) {
1478 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1480 if (MARK == ORIGMARK) {
1483 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1487 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1490 call_method("PRINTF", G_SCALAR);
1493 MARK = ORIGMARK + 1;
1501 if (!(io = GvIO(gv))) {
1502 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1503 report_evil_fh(gv, io, PL_op->op_type);
1504 SETERRNO(EBADF,RMS_IFI);
1507 else if (!(fp = IoOFP(io))) {
1508 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1510 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1511 else if (ckWARN(WARN_CLOSED))
1512 report_evil_fh(gv, io, PL_op->op_type);
1514 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1518 if (SvTAINTED(MARK[1]))
1519 TAINT_PROPER("printf");
1520 do_sprintf(sv, SP - MARK, MARK + 1);
1521 if (!do_print(sv, fp))
1524 if (IoFLAGS(io) & IOf_FLUSH)
1525 if (PerlIO_flush(fp) == EOF)
1536 PUSHs(&PL_sv_undef);
1544 const int perm = (MAXARG > 3) ? POPi : 0666;
1545 const int mode = POPi;
1546 SV * const sv = POPs;
1547 GV * const gv = MUTABLE_GV(POPs);
1550 /* Need TIEHANDLE method ? */
1551 const char * const tmps = SvPV_const(sv, len);
1552 /* FIXME? do_open should do const */
1553 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1554 IoLINES(GvIOp(gv)) = 0;
1558 PUSHs(&PL_sv_undef);
1565 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1571 Sock_size_t bufsize;
1579 bool charstart = FALSE;
1580 STRLEN charskip = 0;
1583 GV * const gv = MUTABLE_GV(*++MARK);
1584 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1585 && gv && (io = GvIO(gv)) )
1587 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1591 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1593 call_method("READ", G_SCALAR);
1607 sv_setpvs(bufsv, "");
1608 length = SvIVx(*++MARK);
1611 offset = SvIVx(*++MARK);
1615 if (!io || !IoIFP(io)) {
1616 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1617 report_evil_fh(gv, io, PL_op->op_type);
1618 SETERRNO(EBADF,RMS_IFI);
1621 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1622 buffer = SvPVutf8_force(bufsv, blen);
1623 /* UTF-8 may not have been set if they are all low bytes */
1628 buffer = SvPV_force(bufsv, blen);
1629 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1632 DIE(aTHX_ "Negative length");
1640 if (PL_op->op_type == OP_RECV) {
1641 char namebuf[MAXPATHLEN];
1642 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1643 bufsize = sizeof (struct sockaddr_in);
1645 bufsize = sizeof namebuf;
1647 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1651 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1652 /* 'offset' means 'flags' here */
1653 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1654 (struct sockaddr *)namebuf, &bufsize);
1658 /* Bogus return without padding */
1659 bufsize = sizeof (struct sockaddr_in);
1661 SvCUR_set(bufsv, count);
1662 *SvEND(bufsv) = '\0';
1663 (void)SvPOK_only(bufsv);
1667 /* This should not be marked tainted if the fp is marked clean */
1668 if (!(IoFLAGS(io) & IOf_UNTAINT))
1669 SvTAINTED_on(bufsv);
1671 sv_setpvn(TARG, namebuf, bufsize);
1676 if (PL_op->op_type == OP_RECV)
1677 DIE(aTHX_ PL_no_sock_func, "recv");
1679 if (DO_UTF8(bufsv)) {
1680 /* offset adjust in characters not bytes */
1681 blen = sv_len_utf8(bufsv);
1684 if (-offset > (int)blen)
1685 DIE(aTHX_ "Offset outside string");
1688 if (DO_UTF8(bufsv)) {
1689 /* convert offset-as-chars to offset-as-bytes */
1690 if (offset >= (int)blen)
1691 offset += SvCUR(bufsv) - blen;
1693 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1696 bufsize = SvCUR(bufsv);
1697 /* Allocating length + offset + 1 isn't perfect in the case of reading
1698 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1700 (should be 2 * length + offset + 1, or possibly something longer if
1701 PL_encoding is true) */
1702 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1703 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1704 Zero(buffer+bufsize, offset-bufsize, char);
1706 buffer = buffer + offset;
1708 read_target = bufsv;
1710 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1711 concatenate it to the current buffer. */
1713 /* Truncate the existing buffer to the start of where we will be
1715 SvCUR_set(bufsv, offset);
1717 read_target = sv_newmortal();
1718 SvUPGRADE(read_target, SVt_PV);
1719 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1722 if (PL_op->op_type == OP_SYSREAD) {
1723 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1724 if (IoTYPE(io) == IoTYPE_SOCKET) {
1725 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1731 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1736 #ifdef HAS_SOCKET__bad_code_maybe
1737 if (IoTYPE(io) == IoTYPE_SOCKET) {
1738 char namebuf[MAXPATHLEN];
1739 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1740 bufsize = sizeof (struct sockaddr_in);
1742 bufsize = sizeof namebuf;
1744 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1745 (struct sockaddr *)namebuf, &bufsize);
1750 count = PerlIO_read(IoIFP(io), buffer, length);
1751 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1752 if (count == 0 && PerlIO_error(IoIFP(io)))
1756 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1757 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1760 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1761 *SvEND(read_target) = '\0';
1762 (void)SvPOK_only(read_target);
1763 if (fp_utf8 && !IN_BYTES) {
1764 /* Look at utf8 we got back and count the characters */
1765 const char *bend = buffer + count;
1766 while (buffer < bend) {
1768 skip = UTF8SKIP(buffer);
1771 if (buffer - charskip + skip > bend) {
1772 /* partial character - try for rest of it */
1773 length = skip - (bend-buffer);
1774 offset = bend - SvPVX_const(bufsv);
1786 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1787 provided amount read (count) was what was requested (length)
1789 if (got < wanted && count == length) {
1790 length = wanted - got;
1791 offset = bend - SvPVX_const(bufsv);
1794 /* return value is character count */
1798 else if (buffer_utf8) {
1799 /* Let svcatsv upgrade the bytes we read in to utf8.
1800 The buffer is a mortal so will be freed soon. */
1801 sv_catsv_nomg(bufsv, read_target);
1804 /* This should not be marked tainted if the fp is marked clean */
1805 if (!(IoFLAGS(io) & IOf_UNTAINT))
1806 SvTAINTED_on(bufsv);
1818 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1824 STRLEN orig_blen_bytes;
1825 const int op_type = PL_op->op_type;
1829 GV *const gv = MUTABLE_GV(*++MARK);
1830 if (PL_op->op_type == OP_SYSWRITE
1831 && gv && (io = GvIO(gv))) {
1832 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1836 if (MARK == SP - 1) {
1838 mXPUSHi(sv_len(sv));
1843 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1845 call_method("WRITE", G_SCALAR);
1861 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1863 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1864 if (io && IoIFP(io))
1865 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1867 report_evil_fh(gv, io, PL_op->op_type);
1869 SETERRNO(EBADF,RMS_IFI);
1873 /* Do this first to trigger any overloading. */
1874 buffer = SvPV_const(bufsv, blen);
1875 orig_blen_bytes = blen;
1876 doing_utf8 = DO_UTF8(bufsv);
1878 if (PerlIO_isutf8(IoIFP(io))) {
1879 if (!SvUTF8(bufsv)) {
1880 /* We don't modify the original scalar. */
1881 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1882 buffer = (char *) tmpbuf;
1886 else if (doing_utf8) {
1887 STRLEN tmplen = blen;
1888 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1891 buffer = (char *) tmpbuf;
1895 assert((char *)result == buffer);
1896 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1900 if (op_type == OP_SYSWRITE) {
1901 Size_t length = 0; /* This length is in characters. */
1907 /* The SV is bytes, and we've had to upgrade it. */
1908 blen_chars = orig_blen_bytes;
1910 /* The SV really is UTF-8. */
1911 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1912 /* Don't call sv_len_utf8 again because it will call magic
1913 or overloading a second time, and we might get back a
1914 different result. */
1915 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1917 /* It's safe, and it may well be cached. */
1918 blen_chars = sv_len_utf8(bufsv);
1926 length = blen_chars;
1928 #if Size_t_size > IVSIZE
1929 length = (Size_t)SvNVx(*++MARK);
1931 length = (Size_t)SvIVx(*++MARK);
1933 if ((SSize_t)length < 0) {
1935 DIE(aTHX_ "Negative length");
1940 offset = SvIVx(*++MARK);
1942 if (-offset > (IV)blen_chars) {
1944 DIE(aTHX_ "Offset outside string");
1946 offset += blen_chars;
1947 } else if (offset > (IV)blen_chars) {
1949 DIE(aTHX_ "Offset outside string");
1953 if (length > blen_chars - offset)
1954 length = blen_chars - offset;
1956 /* Here we convert length from characters to bytes. */
1957 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1958 /* Either we had to convert the SV, or the SV is magical, or
1959 the SV has overloading, in which case we can't or mustn't
1960 or mustn't call it again. */
1962 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1963 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1965 /* It's a real UTF-8 SV, and it's not going to change under
1966 us. Take advantage of any cache. */
1968 I32 len_I32 = length;
1970 /* Convert the start and end character positions to bytes.
1971 Remember that the second argument to sv_pos_u2b is relative
1973 sv_pos_u2b(bufsv, &start, &len_I32);
1980 buffer = buffer+offset;
1982 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1983 if (IoTYPE(io) == IoTYPE_SOCKET) {
1984 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1990 /* See the note at doio.c:do_print about filesize limits. --jhi */
1991 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1997 const int flags = SvIVx(*++MARK);
2000 char * const sockbuf = SvPVx(*++MARK, mlen);
2001 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
2002 flags, (struct sockaddr *)sockbuf, mlen);
2006 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2011 DIE(aTHX_ PL_no_sock_func, "send");
2018 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2021 #if Size_t_size > IVSIZE
2041 * in Perl 5.12 and later, the additional parameter is a bitmask:
2044 * 2 = eof() <- ARGV magic
2046 * I'll rely on the compiler's trace flow analysis to decide whether to
2047 * actually assign this out here, or punt it into the only block where it is
2048 * used. Doing it out here is DRY on the condition logic.
2053 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2059 if (PL_op->op_flags & OPf_SPECIAL) {
2060 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2064 gv = PL_last_in_gv; /* eof */
2072 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2073 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
2076 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2077 if (io && !IoIFP(io)) {
2078 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2080 IoFLAGS(io) &= ~IOf_START;
2081 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2083 sv_setpvs(GvSV(gv), "-");
2085 GvSV(gv) = newSVpvs("-");
2086 SvSETMAGIC(GvSV(gv));
2088 else if (!nextargv(gv))
2093 PUSHs(boolSV(do_eof(gv)));
2104 PL_last_in_gv = MUTABLE_GV(POPs);
2109 if (gv && (io = GvIO(gv))) {
2110 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2112 return tied_handle_method("TELL", SP, io, mg);
2117 SETERRNO(EBADF,RMS_IFI);
2122 #if LSEEKSIZE > IVSIZE
2123 PUSHn( do_tell(gv) );
2125 PUSHi( do_tell(gv) );
2133 const int whence = POPi;
2134 #if LSEEKSIZE > IVSIZE
2135 const Off_t offset = (Off_t)SvNVx(POPs);
2137 const Off_t offset = (Off_t)SvIVx(POPs);
2140 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2143 if (gv && (io = GvIO(gv))) {
2144 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2146 #if LSEEKSIZE > IVSIZE
2147 SV *const offset_sv = newSVnv((NV) offset);
2149 SV *const offset_sv = newSViv(offset);
2152 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
2157 if (PL_op->op_type == OP_SEEK)
2158 PUSHs(boolSV(do_seek(gv, offset, whence)));
2160 const Off_t sought = do_sysseek(gv, offset, whence);
2162 PUSHs(&PL_sv_undef);
2164 SV* const sv = sought ?
2165 #if LSEEKSIZE > IVSIZE
2170 : newSVpvn(zero_but_true, ZBTLEN);
2181 /* There seems to be no consensus on the length type of truncate()
2182 * and ftruncate(), both off_t and size_t have supporters. In
2183 * general one would think that when using large files, off_t is
2184 * at least as wide as size_t, so using an off_t should be okay. */
2185 /* XXX Configure probe for the length type of *truncate() needed XXX */
2188 #if Off_t_size > IVSIZE
2193 /* Checking for length < 0 is problematic as the type might or
2194 * might not be signed: if it is not, clever compilers will moan. */
2195 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2202 if (PL_op->op_flags & OPf_SPECIAL) {
2203 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2212 TAINT_PROPER("truncate");
2213 if (!(fp = IoIFP(io))) {
2219 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2221 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2228 SV * const sv = POPs;
2231 if (isGV_with_GP(sv)) {
2232 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2233 goto do_ftruncate_gv;
2235 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2236 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2237 goto do_ftruncate_gv;
2239 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2240 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2241 goto do_ftruncate_io;
2244 name = SvPV_nolen_const(sv);
2245 TAINT_PROPER("truncate");
2247 if (truncate(name, len) < 0)
2251 const int tmpfd = PerlLIO_open(name, O_RDWR);
2256 if (my_chsize(tmpfd, len) < 0)
2258 PerlLIO_close(tmpfd);
2267 SETERRNO(EBADF,RMS_IFI);
2275 SV * const argsv = POPs;
2276 const unsigned int func = POPu;
2277 const int optype = PL_op->op_type;
2278 GV * const gv = MUTABLE_GV(POPs);
2279 IO * const io = gv ? GvIOn(gv) : NULL;
2283 if (!io || !argsv || !IoIFP(io)) {
2284 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2285 report_evil_fh(gv, io, PL_op->op_type);
2286 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2290 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2293 s = SvPV_force(argsv, len);
2294 need = IOCPARM_LEN(func);
2296 s = Sv_Grow(argsv, need + 1);
2297 SvCUR_set(argsv, need);
2300 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2303 retval = SvIV(argsv);
2304 s = INT2PTR(char*,retval); /* ouch */
2307 TAINT_PROPER(PL_op_desc[optype]);
2309 if (optype == OP_IOCTL)
2311 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2313 DIE(aTHX_ "ioctl is not implemented");
2317 DIE(aTHX_ "fcntl is not implemented");
2319 #if defined(OS2) && defined(__EMX__)
2320 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2322 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2326 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2328 if (s[SvCUR(argsv)] != 17)
2329 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2331 s[SvCUR(argsv)] = 0; /* put our null back */
2332 SvSETMAGIC(argsv); /* Assume it has changed */
2341 PUSHp(zero_but_true, ZBTLEN);
2354 const int argtype = POPi;
2355 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2357 if (gv && (io = GvIO(gv)))
2363 /* XXX Looks to me like io is always NULL at this point */
2365 (void)PerlIO_flush(fp);
2366 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2369 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2370 report_evil_fh(gv, io, PL_op->op_type);
2372 SETERRNO(EBADF,RMS_IFI);
2377 DIE(aTHX_ PL_no_func, "flock()");
2387 const int protocol = POPi;
2388 const int type = POPi;
2389 const int domain = POPi;
2390 GV * const gv = MUTABLE_GV(POPs);
2391 register IO * const io = gv ? GvIOn(gv) : NULL;
2395 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2396 report_evil_fh(gv, io, PL_op->op_type);
2397 if (io && IoIFP(io))
2398 do_close(gv, FALSE);
2399 SETERRNO(EBADF,LIB_INVARG);
2404 do_close(gv, FALSE);
2406 TAINT_PROPER("socket");
2407 fd = PerlSock_socket(domain, type, protocol);
2410 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2411 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2412 IoTYPE(io) = IoTYPE_SOCKET;
2413 if (!IoIFP(io) || !IoOFP(io)) {
2414 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2415 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2416 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2419 #if defined(HAS_FCNTL) && defined(F_SETFD)
2420 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2424 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2429 DIE(aTHX_ PL_no_sock_func, "socket");
2435 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2437 const int protocol = POPi;
2438 const int type = POPi;
2439 const int domain = POPi;
2440 GV * const gv2 = MUTABLE_GV(POPs);
2441 GV * const gv1 = MUTABLE_GV(POPs);
2442 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2443 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2446 if (!gv1 || !gv2 || !io1 || !io2) {
2447 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2449 report_evil_fh(gv1, io1, PL_op->op_type);
2451 report_evil_fh(gv1, io2, PL_op->op_type);
2453 if (io1 && IoIFP(io1))
2454 do_close(gv1, FALSE);
2455 if (io2 && IoIFP(io2))
2456 do_close(gv2, FALSE);
2461 do_close(gv1, FALSE);
2463 do_close(gv2, FALSE);
2465 TAINT_PROPER("socketpair");
2466 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2468 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2469 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2470 IoTYPE(io1) = IoTYPE_SOCKET;
2471 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2472 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2473 IoTYPE(io2) = IoTYPE_SOCKET;
2474 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2475 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2476 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2477 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2478 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2479 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2480 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2483 #if defined(HAS_FCNTL) && defined(F_SETFD)
2484 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2485 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2490 DIE(aTHX_ PL_no_sock_func, "socketpair");
2498 SV * const addrsv = POPs;
2499 /* OK, so on what platform does bind modify addr? */
2501 GV * const gv = MUTABLE_GV(POPs);
2502 register IO * const io = GvIOn(gv);
2505 if (!io || !IoIFP(io))
2508 addr = SvPV_const(addrsv, len);
2509 TAINT_PROPER("bind");
2510 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2516 if (ckWARN(WARN_CLOSED))
2517 report_evil_fh(gv, io, PL_op->op_type);
2518 SETERRNO(EBADF,SS_IVCHAN);
2521 DIE(aTHX_ PL_no_sock_func, "bind");
2529 SV * const addrsv = POPs;
2530 GV * const gv = MUTABLE_GV(POPs);
2531 register IO * const io = GvIOn(gv);
2535 if (!io || !IoIFP(io))
2538 addr = SvPV_const(addrsv, len);
2539 TAINT_PROPER("connect");
2540 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2546 if (ckWARN(WARN_CLOSED))
2547 report_evil_fh(gv, io, PL_op->op_type);
2548 SETERRNO(EBADF,SS_IVCHAN);
2551 DIE(aTHX_ PL_no_sock_func, "connect");
2559 const int backlog = POPi;
2560 GV * const gv = MUTABLE_GV(POPs);
2561 register IO * const io = gv ? GvIOn(gv) : NULL;
2563 if (!gv || !io || !IoIFP(io))
2566 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2572 if (ckWARN(WARN_CLOSED))
2573 report_evil_fh(gv, io, PL_op->op_type);
2574 SETERRNO(EBADF,SS_IVCHAN);
2577 DIE(aTHX_ PL_no_sock_func, "listen");
2587 char namebuf[MAXPATHLEN];
2588 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2589 Sock_size_t len = sizeof (struct sockaddr_in);
2591 Sock_size_t len = sizeof namebuf;
2593 GV * const ggv = MUTABLE_GV(POPs);
2594 GV * const ngv = MUTABLE_GV(POPs);
2603 if (!gstio || !IoIFP(gstio))
2607 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2610 /* Some platforms indicate zero length when an AF_UNIX client is
2611 * not bound. Simulate a non-zero-length sockaddr structure in
2613 namebuf[0] = 0; /* sun_len */
2614 namebuf[1] = AF_UNIX; /* sun_family */
2622 do_close(ngv, FALSE);
2623 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2624 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2625 IoTYPE(nstio) = IoTYPE_SOCKET;
2626 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2627 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2628 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2629 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2632 #if defined(HAS_FCNTL) && defined(F_SETFD)
2633 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2637 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2638 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2640 #ifdef __SCO_VERSION__
2641 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2644 PUSHp(namebuf, len);
2648 if (ckWARN(WARN_CLOSED))
2649 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2650 SETERRNO(EBADF,SS_IVCHAN);
2656 DIE(aTHX_ PL_no_sock_func, "accept");
2664 const int how = POPi;
2665 GV * const gv = MUTABLE_GV(POPs);
2666 register IO * const io = GvIOn(gv);
2668 if (!io || !IoIFP(io))
2671 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2675 if (ckWARN(WARN_CLOSED))
2676 report_evil_fh(gv, io, PL_op->op_type);
2677 SETERRNO(EBADF,SS_IVCHAN);
2680 DIE(aTHX_ PL_no_sock_func, "shutdown");
2688 const int optype = PL_op->op_type;
2689 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2690 const unsigned int optname = (unsigned int) POPi;
2691 const unsigned int lvl = (unsigned int) POPi;
2692 GV * const gv = MUTABLE_GV(POPs);
2693 register IO * const io = GvIOn(gv);
2697 if (!io || !IoIFP(io))
2700 fd = PerlIO_fileno(IoIFP(io));
2704 (void)SvPOK_only(sv);
2708 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2715 #if defined(__SYMBIAN32__)
2716 # define SETSOCKOPT_OPTION_VALUE_T void *
2718 # define SETSOCKOPT_OPTION_VALUE_T const char *
2720 /* XXX TODO: We need to have a proper type (a Configure probe,
2721 * etc.) for what the C headers think of the third argument of
2722 * setsockopt(), the option_value read-only buffer: is it
2723 * a "char *", or a "void *", const or not. Some compilers
2724 * don't take kindly to e.g. assuming that "char *" implicitly
2725 * promotes to a "void *", or to explicitly promoting/demoting
2726 * consts to non/vice versa. The "const void *" is the SUS
2727 * definition, but that does not fly everywhere for the above
2729 SETSOCKOPT_OPTION_VALUE_T buf;
2733 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2737 aint = (int)SvIV(sv);
2738 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2741 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2750 if (ckWARN(WARN_CLOSED))
2751 report_evil_fh(gv, io, optype);
2752 SETERRNO(EBADF,SS_IVCHAN);
2757 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2765 const int optype = PL_op->op_type;
2766 GV * const gv = MUTABLE_GV(POPs);
2767 register IO * const io = GvIOn(gv);
2772 if (!io || !IoIFP(io))
2775 sv = sv_2mortal(newSV(257));
2776 (void)SvPOK_only(sv);
2780 fd = PerlIO_fileno(IoIFP(io));
2782 case OP_GETSOCKNAME:
2783 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2786 case OP_GETPEERNAME:
2787 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2789 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2791 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";
2792 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2793 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2794 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2795 sizeof(u_short) + sizeof(struct in_addr))) {
2802 #ifdef BOGUS_GETNAME_RETURN
2803 /* Interactive Unix, getpeername() and getsockname()
2804 does not return valid namelen */
2805 if (len == BOGUS_GETNAME_RETURN)
2806 len = sizeof(struct sockaddr);
2814 if (ckWARN(WARN_CLOSED))
2815 report_evil_fh(gv, io, optype);
2816 SETERRNO(EBADF,SS_IVCHAN);
2821 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2836 if (PL_op->op_flags & OPf_REF) {
2838 if (PL_op->op_type == OP_LSTAT) {
2839 if (gv != PL_defgv) {
2840 do_fstat_warning_check:
2841 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2842 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2843 } else if (PL_laststype != OP_LSTAT)
2844 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2848 if (gv != PL_defgv) {
2849 PL_laststype = OP_STAT;
2851 sv_setpvs(PL_statname, "");
2858 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2859 } else if (IoDIRP(io)) {
2861 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2863 PL_laststatval = -1;
2869 if (PL_laststatval < 0) {
2870 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2871 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2876 SV* const sv = POPs;
2877 if (isGV_with_GP(sv)) {
2878 gv = MUTABLE_GV(sv);
2880 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2881 gv = MUTABLE_GV(SvRV(sv));
2882 if (PL_op->op_type == OP_LSTAT)
2883 goto do_fstat_warning_check;
2885 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2886 io = MUTABLE_IO(SvRV(sv));
2887 if (PL_op->op_type == OP_LSTAT)
2888 goto do_fstat_warning_check;
2889 goto do_fstat_have_io;
2892 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2894 PL_laststype = PL_op->op_type;
2895 if (PL_op->op_type == OP_LSTAT)
2896 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2898 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2899 if (PL_laststatval < 0) {
2900 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2901 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2907 if (gimme != G_ARRAY) {
2908 if (gimme != G_VOID)
2909 XPUSHs(boolSV(max));
2915 mPUSHi(PL_statcache.st_dev);
2916 mPUSHi(PL_statcache.st_ino);
2917 mPUSHu(PL_statcache.st_mode);
2918 mPUSHu(PL_statcache.st_nlink);
2919 #if Uid_t_size > IVSIZE
2920 mPUSHn(PL_statcache.st_uid);
2922 # if Uid_t_sign <= 0
2923 mPUSHi(PL_statcache.st_uid);
2925 mPUSHu(PL_statcache.st_uid);
2928 #if Gid_t_size > IVSIZE
2929 mPUSHn(PL_statcache.st_gid);
2931 # if Gid_t_sign <= 0
2932 mPUSHi(PL_statcache.st_gid);
2934 mPUSHu(PL_statcache.st_gid);
2937 #ifdef USE_STAT_RDEV
2938 mPUSHi(PL_statcache.st_rdev);
2940 PUSHs(newSVpvs_flags("", SVs_TEMP));
2942 #if Off_t_size > IVSIZE
2943 mPUSHn(PL_statcache.st_size);
2945 mPUSHi(PL_statcache.st_size);
2948 mPUSHn(PL_statcache.st_atime);
2949 mPUSHn(PL_statcache.st_mtime);
2950 mPUSHn(PL_statcache.st_ctime);
2952 mPUSHi(PL_statcache.st_atime);
2953 mPUSHi(PL_statcache.st_mtime);
2954 mPUSHi(PL_statcache.st_ctime);
2956 #ifdef USE_STAT_BLOCKS
2957 mPUSHu(PL_statcache.st_blksize);
2958 mPUSHu(PL_statcache.st_blocks);
2960 PUSHs(newSVpvs_flags("", SVs_TEMP));
2961 PUSHs(newSVpvs_flags("", SVs_TEMP));
2967 #define tryAMAGICftest_MG(chr) STMT_START { \
2968 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2969 && S_try_amagic_ftest(aTHX_ chr)) \
2974 S_try_amagic_ftest(pTHX_ char chr) {
2977 SV* const arg = TOPs;
2982 if ((PL_op->op_flags & OPf_KIDS)
2985 const char tmpchr = chr;
2987 SV * const tmpsv = amagic_call(arg,
2988 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2989 ftest_amg, AMGf_unary);
2996 next = PL_op->op_next;
2997 if (next->op_type >= OP_FTRREAD &&
2998 next->op_type <= OP_FTBINARY &&
2999 next->op_private & OPpFT_STACKED
3002 /* leave the object alone */
3014 /* This macro is used by the stacked filetest operators :
3015 * if the previous filetest failed, short-circuit and pass its value.
3016 * Else, discard it from the stack and continue. --rgs
3018 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
3019 if (!SvTRUE(TOPs)) { RETURN; } \
3020 else { (void)POPs; PUTBACK; } \
3027 /* Not const, because things tweak this below. Not bool, because there's
3028 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3029 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3030 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3031 /* Giving some sort of initial value silences compilers. */
3033 int access_mode = R_OK;
3035 int access_mode = 0;
3038 /* access_mode is never used, but leaving use_access in makes the
3039 conditional compiling below much clearer. */
3042 int stat_mode = S_IRUSR;
3044 bool effective = FALSE;
3048 switch (PL_op->op_type) {
3049 case OP_FTRREAD: opchar = 'R'; break;
3050 case OP_FTRWRITE: opchar = 'W'; break;
3051 case OP_FTREXEC: opchar = 'X'; break;
3052 case OP_FTEREAD: opchar = 'r'; break;
3053 case OP_FTEWRITE: opchar = 'w'; break;
3054 case OP_FTEEXEC: opchar = 'x'; break;
3056 tryAMAGICftest_MG(opchar);
3058 STACKED_FTEST_CHECK;
3060 switch (PL_op->op_type) {
3062 #if !(defined(HAS_ACCESS) && defined(R_OK))
3068 #if defined(HAS_ACCESS) && defined(W_OK)
3073 stat_mode = S_IWUSR;
3077 #if defined(HAS_ACCESS) && defined(X_OK)
3082 stat_mode = S_IXUSR;
3086 #ifdef PERL_EFF_ACCESS
3089 stat_mode = S_IWUSR;
3093 #ifndef PERL_EFF_ACCESS
3100 #ifdef PERL_EFF_ACCESS
3105 stat_mode = S_IXUSR;
3111 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3112 const char *name = POPpx;
3114 # ifdef PERL_EFF_ACCESS
3115 result = PERL_EFF_ACCESS(name, access_mode);
3117 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3123 result = access(name, access_mode);
3125 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3136 result = my_stat_flags(0);
3140 if (cando(stat_mode, effective, &PL_statcache))
3149 const int op_type = PL_op->op_type;
3154 case OP_FTIS: opchar = 'e'; break;
3155 case OP_FTSIZE: opchar = 's'; break;
3156 case OP_FTMTIME: opchar = 'M'; break;
3157 case OP_FTCTIME: opchar = 'C'; break;
3158 case OP_FTATIME: opchar = 'A'; break;
3160 tryAMAGICftest_MG(opchar);
3162 STACKED_FTEST_CHECK;
3164 result = my_stat_flags(0);
3168 if (op_type == OP_FTIS)
3171 /* You can't dTARGET inside OP_FTIS, because you'll get
3172 "panic: pad_sv po" - the op is not flagged to have a target. */
3176 #if Off_t_size > IVSIZE
3177 PUSHn(PL_statcache.st_size);
3179 PUSHi(PL_statcache.st_size);
3183 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3186 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3189 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3203 switch (PL_op->op_type) {
3204 case OP_FTROWNED: opchar = 'O'; break;
3205 case OP_FTEOWNED: opchar = 'o'; break;
3206 case OP_FTZERO: opchar = 'z'; break;
3207 case OP_FTSOCK: opchar = 'S'; break;
3208 case OP_FTCHR: opchar = 'c'; break;
3209 case OP_FTBLK: opchar = 'b'; break;
3210 case OP_FTFILE: opchar = 'f'; break;
3211 case OP_FTDIR: opchar = 'd'; break;
3212 case OP_FTPIPE: opchar = 'p'; break;
3213 case OP_FTSUID: opchar = 'u'; break;
3214 case OP_FTSGID: opchar = 'g'; break;
3215 case OP_FTSVTX: opchar = 'k'; break;
3217 tryAMAGICftest_MG(opchar);
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) {
3228 if(PL_op->op_type == OP_FTSGID) {
3234 if(PL_op->op_type == OP_FTSVTX) {
3240 STACKED_FTEST_CHECK;
3242 result = my_stat_flags(0);
3246 switch (PL_op->op_type) {
3248 if (PL_statcache.st_uid == PL_uid)
3252 if (PL_statcache.st_uid == PL_euid)
3256 if (PL_statcache.st_size == 0)
3260 if (S_ISSOCK(PL_statcache.st_mode))
3264 if (S_ISCHR(PL_statcache.st_mode))
3268 if (S_ISBLK(PL_statcache.st_mode))
3272 if (S_ISREG(PL_statcache.st_mode))
3276 if (S_ISDIR(PL_statcache.st_mode))
3280 if (S_ISFIFO(PL_statcache.st_mode))
3285 if (PL_statcache.st_mode & S_ISUID)
3291 if (PL_statcache.st_mode & S_ISGID)
3297 if (PL_statcache.st_mode & S_ISVTX)
3311 tryAMAGICftest_MG('l');
3312 result = my_lstat_flags(0);
3317 if (S_ISLNK(PL_statcache.st_mode))
3332 tryAMAGICftest_MG('t');
3334 STACKED_FTEST_CHECK;
3336 if (PL_op->op_flags & OPf_REF)
3338 else if (isGV(TOPs))
3339 gv = MUTABLE_GV(POPs);
3340 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3341 gv = MUTABLE_GV(SvRV(POPs));
3344 name = SvPV_nomg(tmpsv, namelen);
3345 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3348 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3349 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3350 else if (tmpsv && SvOK(tmpsv)) {
3358 if (PerlLIO_isatty(fd))
3363 #if defined(atarist) /* this will work with atariST. Configure will
3364 make guesses for other systems. */
3365 # define FILE_base(f) ((f)->_base)
3366 # define FILE_ptr(f) ((f)->_ptr)
3367 # define FILE_cnt(f) ((f)->_cnt)
3368 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3379 register STDCHAR *s;
3385 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3387 STACKED_FTEST_CHECK;
3389 if (PL_op->op_flags & OPf_REF)
3391 else if (isGV(TOPs))
3392 gv = MUTABLE_GV(POPs);
3393 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3394 gv = MUTABLE_GV(SvRV(POPs));
3400 if (gv == PL_defgv) {
3402 io = GvIO(PL_statgv);
3405 goto really_filename;
3410 PL_laststatval = -1;
3411 sv_setpvs(PL_statname, "");
3412 io = GvIO(PL_statgv);
3414 if (io && IoIFP(io)) {
3415 if (! PerlIO_has_base(IoIFP(io)))
3416 DIE(aTHX_ "-T and -B not implemented on filehandles");
3417 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3418 if (PL_laststatval < 0)
3420 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3421 if (PL_op->op_type == OP_FTTEXT)
3426 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3427 i = PerlIO_getc(IoIFP(io));
3429 (void)PerlIO_ungetc(IoIFP(io),i);
3431 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3433 len = PerlIO_get_bufsiz(IoIFP(io));
3434 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3435 /* sfio can have large buffers - limit to 512 */
3440 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3442 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3444 SETERRNO(EBADF,RMS_IFI);
3452 PL_laststype = OP_STAT;
3453 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3454 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3455 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3457 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3460 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3461 if (PL_laststatval < 0) {
3462 (void)PerlIO_close(fp);
3465 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3466 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3467 (void)PerlIO_close(fp);
3469 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3470 RETPUSHNO; /* special case NFS directories */
3471 RETPUSHYES; /* null file is anything */
3476 /* now scan s to look for textiness */
3477 /* XXX ASCII dependent code */
3479 #if defined(DOSISH) || defined(USEMYBINMODE)
3480 /* ignore trailing ^Z on short files */
3481 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3485 for (i = 0; i < len; i++, s++) {
3486 if (!*s) { /* null never allowed in text */
3491 else if (!(isPRINT(*s) || isSPACE(*s)))
3494 else if (*s & 128) {
3496 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3499 /* utf8 characters don't count as odd */
3500 if (UTF8_IS_START(*s)) {
3501 int ulen = UTF8SKIP(s);
3502 if (ulen < len - i) {
3504 for (j = 1; j < ulen; j++) {
3505 if (!UTF8_IS_CONTINUATION(s[j]))
3508 --ulen; /* loop does extra increment */
3518 *s != '\n' && *s != '\r' && *s != '\b' &&
3519 *s != '\t' && *s != '\f' && *s != 27)
3524 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3535 const char *tmps = NULL;
3539 SV * const sv = POPs;
3540 if (PL_op->op_flags & OPf_SPECIAL) {
3541 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3543 else if (isGV_with_GP(sv)) {
3544 gv = MUTABLE_GV(sv);
3546 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3547 gv = MUTABLE_GV(SvRV(sv));
3550 tmps = SvPV_nolen_const(sv);
3554 if( !gv && (!tmps || !*tmps) ) {
3555 HV * const table = GvHVn(PL_envgv);
3558 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3559 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3561 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3566 deprecate("chdir('') or chdir(undef) as chdir()");
3567 tmps = SvPV_nolen_const(*svp);
3571 TAINT_PROPER("chdir");
3576 TAINT_PROPER("chdir");
3579 IO* const io = GvIO(gv);
3582 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3583 } else if (IoIFP(io)) {
3584 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3587 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3588 report_evil_fh(gv, io, PL_op->op_type);
3589 SETERRNO(EBADF, RMS_IFI);
3594 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3595 report_evil_fh(gv, io, PL_op->op_type);
3596 SETERRNO(EBADF,RMS_IFI);
3600 DIE(aTHX_ PL_no_func, "fchdir");
3604 PUSHi( PerlDir_chdir(tmps) >= 0 );
3606 /* Clear the DEFAULT element of ENV so we'll get the new value
3608 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3615 dVAR; dSP; dMARK; dTARGET;
3616 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3627 char * const tmps = POPpx;
3628 TAINT_PROPER("chroot");
3629 PUSHi( chroot(tmps) >= 0 );
3632 DIE(aTHX_ PL_no_func, "chroot");
3640 const char * const tmps2 = POPpconstx;
3641 const char * const tmps = SvPV_nolen_const(TOPs);
3642 TAINT_PROPER("rename");
3644 anum = PerlLIO_rename(tmps, tmps2);
3646 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3647 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3650 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3651 (void)UNLINK(tmps2);
3652 if (!(anum = link(tmps, tmps2)))
3653 anum = UNLINK(tmps);
3661 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3665 const int op_type = PL_op->op_type;
3669 if (op_type == OP_LINK)
3670 DIE(aTHX_ PL_no_func, "link");
3672 # ifndef HAS_SYMLINK
3673 if (op_type == OP_SYMLINK)
3674 DIE(aTHX_ PL_no_func, "symlink");
3678 const char * const tmps2 = POPpconstx;
3679 const char * const tmps = SvPV_nolen_const(TOPs);
3680 TAINT_PROPER(PL_op_desc[op_type]);
3682 # if defined(HAS_LINK)
3683 # if defined(HAS_SYMLINK)
3684 /* Both present - need to choose which. */
3685 (op_type == OP_LINK) ?
3686 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3688 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3689 PerlLIO_link(tmps, tmps2);
3692 # if defined(HAS_SYMLINK)
3693 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3694 symlink(tmps, tmps2);
3699 SETi( result >= 0 );
3706 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3717 char buf[MAXPATHLEN];
3720 #ifndef INCOMPLETE_TAINTS
3724 len = readlink(tmps, buf, sizeof(buf) - 1);
3731 RETSETUNDEF; /* just pretend it's a normal file */
3735 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3737 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3739 char * const save_filename = filename;
3744 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3746 PERL_ARGS_ASSERT_DOONELINER;
3748 Newx(cmdline, size, char);
3749 my_strlcpy(cmdline, cmd, size);
3750 my_strlcat(cmdline, " ", size);
3751 for (s = cmdline + strlen(cmdline); *filename; ) {
3755 if (s - cmdline < size)
3756 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3757 myfp = PerlProc_popen(cmdline, "r");
3761 SV * const tmpsv = sv_newmortal();
3762 /* Need to save/restore 'PL_rs' ?? */
3763 s = sv_gets(tmpsv, myfp, 0);
3764 (void)PerlProc_pclose(myfp);
3768 #ifdef HAS_SYS_ERRLIST
3773 /* you don't see this */
3774 const char * const errmsg =
3775 #ifdef HAS_SYS_ERRLIST
3783 if (instr(s, errmsg)) {
3790 #define EACCES EPERM
3792 if (instr(s, "cannot make"))
3793 SETERRNO(EEXIST,RMS_FEX);
3794 else if (instr(s, "existing file"))
3795 SETERRNO(EEXIST,RMS_FEX);
3796 else if (instr(s, "ile exists"))
3797 SETERRNO(EEXIST,RMS_FEX);
3798 else if (instr(s, "non-exist"))
3799 SETERRNO(ENOENT,RMS_FNF);
3800 else if (instr(s, "does not exist"))
3801 SETERRNO(ENOENT,RMS_FNF);
3802 else if (instr(s, "not empty"))
3803 SETERRNO(EBUSY,SS_DEVOFFLINE);
3804 else if (instr(s, "cannot access"))
3805 SETERRNO(EACCES,RMS_PRV);
3807 SETERRNO(EPERM,RMS_PRV);
3810 else { /* some mkdirs return no failure indication */
3811 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3812 if (PL_op->op_type == OP_RMDIR)
3817 SETERRNO(EACCES,RMS_PRV); /* a guess */
3826 /* This macro removes trailing slashes from a directory name.
3827 * Different operating and file systems take differently to
3828 * trailing slashes. According to POSIX 1003.1 1996 Edition
3829 * any number of trailing slashes should be allowed.
3830 * Thusly we snip them away so that even non-conforming
3831 * systems are happy.
3832 * We should probably do this "filtering" for all
3833 * the functions that expect (potentially) directory names:
3834 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3835 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3837 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3838 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3841 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3842 (tmps) = savepvn((tmps), (len)); \
3852 const int mode = (MAXARG > 1) ? POPi : 0777;
3854 TRIMSLASHES(tmps,len,copy);
3856 TAINT_PROPER("mkdir");
3858 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3862 SETi( dooneliner("mkdir", tmps) );
3863 oldumask = PerlLIO_umask(0);
3864 PerlLIO_umask(oldumask);
3865 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3880 TRIMSLASHES(tmps,len,copy);
3881 TAINT_PROPER("rmdir");
3883 SETi( PerlDir_rmdir(tmps) >= 0 );
3885 SETi( dooneliner("rmdir", tmps) );
3892 /* Directory calls. */
3896 #if defined(Direntry_t) && defined(HAS_READDIR)
3898 const char * const dirname = POPpconstx;
3899 GV * const gv = MUTABLE_GV(POPs);
3900 register IO * const io = GvIOn(gv);
3905 if ((IoIFP(io) || IoOFP(io)))
3906 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3907 "Opening filehandle %s also as a directory",
3910 PerlDir_close(IoDIRP(io));
3911 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3917 SETERRNO(EBADF,RMS_DIR);
3920 DIE(aTHX_ PL_no_dir_func, "opendir");
3926 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3927 DIE(aTHX_ PL_no_dir_func, "readdir");
3929 #if !defined(I_DIRENT) && !defined(VMS)
3930 Direntry_t *readdir (DIR *);
3936 const I32 gimme = GIMME;
3937 GV * const gv = MUTABLE_GV(POPs);
3938 register const Direntry_t *dp;
3939 register IO * const io = GvIOn(gv);
3941 if (!io || !IoDIRP(io)) {
3942 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3943 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3948 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3952 sv = newSVpvn(dp->d_name, dp->d_namlen);
3954 sv = newSVpv(dp->d_name, 0);
3956 #ifndef INCOMPLETE_TAINTS
3957 if (!(IoFLAGS(io) & IOf_UNTAINT))
3961 } while (gimme == G_ARRAY);
3963 if (!dp && gimme != G_ARRAY)
3970 SETERRNO(EBADF,RMS_ISI);
3971 if (GIMME == G_ARRAY)
3980 #if defined(HAS_TELLDIR) || defined(telldir)
3982 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3983 /* XXX netbsd still seemed to.
3984 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3985 --JHI 1999-Feb-02 */
3986 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3987 long telldir (DIR *);
3989 GV * const gv = MUTABLE_GV(POPs);
3990 register IO * const io = GvIOn(gv);
3992 if (!io || !IoDIRP(io)) {
3993 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3994 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3998 PUSHi( PerlDir_tell(IoDIRP(io)) );
4002 SETERRNO(EBADF,RMS_ISI);
4005 DIE(aTHX_ PL_no_dir_func, "telldir");
4011 #if defined(HAS_SEEKDIR) || defined(seekdir)
4013 const long along = POPl;
4014 GV * const gv = MUTABLE_GV(POPs);
4015 register IO * const io = GvIOn(gv);
4017 if (!io || !IoDIRP(io)) {
4018 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4019 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
4022 (void)PerlDir_seek(IoDIRP(io), along);
4027 SETERRNO(EBADF,RMS_ISI);
4030 DIE(aTHX_ PL_no_dir_func, "seekdir");
4036 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4038 GV * const gv = MUTABLE_GV(POPs);
4039 register IO * const io = GvIOn(gv);
4041 if (!io || !IoDIRP(io)) {
4042 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4043 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
4046 (void)PerlDir_rewind(IoDIRP(io));
4050 SETERRNO(EBADF,RMS_ISI);
4053 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4059 #if defined(Direntry_t) && defined(HAS_READDIR)
4061 GV * const gv = MUTABLE_GV(POPs);
4062 register IO * const io = GvIOn(gv);
4064 if (!io || !IoDIRP(io)) {
4065 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4066 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4069 #ifdef VOID_CLOSEDIR
4070 PerlDir_close(IoDIRP(io));
4072 if (PerlDir_close(IoDIRP(io)) < 0) {
4073 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4082 SETERRNO(EBADF,RMS_IFI);
4085 DIE(aTHX_ PL_no_dir_func, "closedir");
4089 /* Process control. */
4098 PERL_FLUSHALL_FOR_CHILD;
4099 childpid = PerlProc_fork();
4103 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4105 SvREADONLY_off(GvSV(tmpgv));
4106 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4107 SvREADONLY_on(GvSV(tmpgv));
4109 #ifdef THREADS_HAVE_PIDS
4110 PL_ppid = (IV)getppid();
4112 #ifdef PERL_USES_PL_PIDSTATUS
4113 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4119 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4124 PERL_FLUSHALL_FOR_CHILD;
4125 childpid = PerlProc_fork();
4131 DIE(aTHX_ PL_no_func, "fork");
4138 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4143 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4144 childpid = wait4pid(-1, &argflags, 0);
4146 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4151 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4152 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4153 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4155 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4160 DIE(aTHX_ PL_no_func, "wait");
4166 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4168 const int optype = POPi;
4169 const Pid_t pid = TOPi;
4173 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4174 result = wait4pid(pid, &argflags, optype);
4176 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4181 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4182 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4183 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4185 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4190 DIE(aTHX_ PL_no_func, "waitpid");
4196 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4197 #if defined(__LIBCATAMOUNT__)
4198 PL_statusvalue = -1;
4207 while (++MARK <= SP) {
4208 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4213 TAINT_PROPER("system");
4215 PERL_FLUSHALL_FOR_CHILD;
4216 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4222 if (PerlProc_pipe(pp) >= 0)
4224 while ((childpid = PerlProc_fork()) == -1) {
4225 if (errno != EAGAIN) {
4230 PerlLIO_close(pp[0]);
4231 PerlLIO_close(pp[1]);
4238 Sigsave_t ihand,qhand; /* place to save signals during system() */
4242 PerlLIO_close(pp[1]);
4244 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4245 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4248 result = wait4pid(childpid, &status, 0);
4249 } while (result == -1 && errno == EINTR);
4251 (void)rsignal_restore(SIGINT, &ihand);
4252 (void)rsignal_restore(SIGQUIT, &qhand);
4254 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4255 do_execfree(); /* free any memory child malloced on fork */
4262 while (n < sizeof(int)) {
4263 n1 = PerlLIO_read(pp[0],
4264 (void*)(((char*)&errkid)+n),
4270 PerlLIO_close(pp[0]);
4271 if (n) { /* Error */
4272 if (n != sizeof(int))
4273 DIE(aTHX_ "panic: kid popen errno read");
4274 errno = errkid; /* Propagate errno from kid */
4275 STATUS_NATIVE_CHILD_SET(-1);
4278 XPUSHi(STATUS_CURRENT);
4282 PerlLIO_close(pp[0]);
4283 #if defined(HAS_FCNTL) && defined(F_SETFD)
4284 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4287 if (PL_op->op_flags & OPf_STACKED) {
4288 SV * const really = *++MARK;
4289 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4291 else if (SP - MARK != 1)
4292 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4294 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4298 #else /* ! FORK or VMS or OS/2 */
4301 if (PL_op->op_flags & OPf_STACKED) {
4302 SV * const really = *++MARK;
4303 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4304 value = (I32)do_aspawn(really, MARK, SP);
4306 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4309 else if (SP - MARK != 1) {
4310 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4311 value = (I32)do_aspawn(NULL, MARK, SP);
4313 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4317 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4319 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4321 STATUS_NATIVE_CHILD_SET(value);
4324 XPUSHi(result ? value : STATUS_CURRENT);
4325 #endif /* !FORK or VMS or OS/2 */
4332 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4337 while (++MARK <= SP) {
4338 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4343 TAINT_PROPER("exec");
4345 PERL_FLUSHALL_FOR_CHILD;
4346 if (PL_op->op_flags & OPf_STACKED) {
4347 SV * const really = *++MARK;
4348 value = (I32)do_aexec(really, MARK, SP);
4350 else if (SP - MARK != 1)
4352 value = (I32)vms_do_aexec(NULL, MARK, SP);
4356 (void ) do_aspawn(NULL, MARK, SP);
4360 value = (I32)do_aexec(NULL, MARK, SP);
4365 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4368 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4371 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4385 # ifdef THREADS_HAVE_PIDS
4386 if (PL_ppid != 1 && getppid() == 1)
4387 /* maybe the parent process has died. Refresh ppid cache */
4391 XPUSHi( getppid() );
4395 DIE(aTHX_ PL_no_func, "getppid");
4404 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4407 pgrp = (I32)BSD_GETPGRP(pid);
4409 if (pid != 0 && pid != PerlProc_getpid())
4410 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4416 DIE(aTHX_ PL_no_func, "getpgrp()");
4436 TAINT_PROPER("setpgrp");
4438 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4440 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4441 || (pid != 0 && pid != PerlProc_getpid()))
4443 DIE(aTHX_ "setpgrp can't take arguments");
4445 SETi( setpgrp() >= 0 );
4446 #endif /* USE_BSDPGRP */
4449 DIE(aTHX_ PL_no_func, "setpgrp()");
4455 #ifdef HAS_GETPRIORITY
4457 const int who = POPi;
4458 const int which = TOPi;
4459 SETi( getpriority(which, who) );
4462 DIE(aTHX_ PL_no_func, "getpriority()");
4468 #ifdef HAS_SETPRIORITY
4470 const int niceval = POPi;
4471 const int who = POPi;
4472 const int which = TOPi;
4473 TAINT_PROPER("setpriority");
4474 SETi( setpriority(which, who, niceval) >= 0 );
4477 DIE(aTHX_ PL_no_func, "setpriority()");
4487 XPUSHn( time(NULL) );
4489 XPUSHi( time(NULL) );
4501 (void)PerlProc_times(&PL_timesbuf);
4503 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4504 /* struct tms, though same data */
4508 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4509 if (GIMME == G_ARRAY) {
4510 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4511 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4512 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4520 if (GIMME == G_ARRAY) {
4527 DIE(aTHX_ "times not implemented");
4529 #endif /* HAS_TIMES */
4532 /* The 32 bit int year limits the times we can represent to these
4533 boundaries with a few days wiggle room to account for time zone
4536 /* Sat Jan 3 00:00:00 -2147481748 */
4537 #define TIME_LOWER_BOUND -67768100567755200.0
4538 /* Sun Dec 29 12:00:00 2147483647 */
4539 #define TIME_UPPER_BOUND 67767976233316800.0
4548 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4549 static const char * const dayname[] =
4550 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4551 static const char * const monname[] =
4552 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4553 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4558 when = (Time64_T)now;
4561 NV input = Perl_floor(POPn);
4562 when = (Time64_T)input;
4563 if (when != input) {
4564 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4565 "%s(%.0" NVff ") too large", opname, input);
4569 if ( TIME_LOWER_BOUND > when ) {
4570 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4571 "%s(%.0" NVff ") too small", opname, when);
4574 else if( when > TIME_UPPER_BOUND ) {
4575 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4576 "%s(%.0" NVff ") too large", opname, when);
4580 if (PL_op->op_type == OP_LOCALTIME)
4581 err = S_localtime64_r(&when, &tmbuf);
4583 err = S_gmtime64_r(&when, &tmbuf);
4587 /* XXX %lld broken for quads */
4588 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4589 "%s(%.0" NVff ") failed", opname, when);
4592 if (GIMME != G_ARRAY) { /* scalar context */
4594 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4595 double year = (double)tmbuf.tm_year + 1900;
4602 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4603 dayname[tmbuf.tm_wday],
4604 monname[tmbuf.tm_mon],
4612 else { /* list context */
4618 mPUSHi(tmbuf.tm_sec);
4619 mPUSHi(tmbuf.tm_min);
4620 mPUSHi(tmbuf.tm_hour);
4621 mPUSHi(tmbuf.tm_mday);
4622 mPUSHi(tmbuf.tm_mon);
4623 mPUSHn(tmbuf.tm_year);
4624 mPUSHi(tmbuf.tm_wday);
4625 mPUSHi(tmbuf.tm_yday);
4626 mPUSHi(tmbuf.tm_isdst);
4637 anum = alarm((unsigned int)anum);
4643 DIE(aTHX_ PL_no_func, "alarm");
4654 (void)time(&lasttime);
4659 PerlProc_sleep((unsigned int)duration);
4662 XPUSHi(when - lasttime);
4666 /* Shared memory. */
4667 /* Merged with some message passing. */
4671 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4672 dVAR; dSP; dMARK; dTARGET;
4673 const int op_type = PL_op->op_type;
4678 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4681 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4684 value = (I32)(do_semop(MARK, SP) >= 0);
4687 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4703 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4704 dVAR; dSP; dMARK; dTARGET;
4705 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4712 DIE(aTHX_ "System V IPC is not implemented on this machine");
4718 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4719 dVAR; dSP; dMARK; dTARGET;
4720 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4728 PUSHp(zero_but_true, ZBTLEN);
4736 /* I can't const this further without getting warnings about the types of
4737 various arrays passed in from structures. */
4739 S_space_join_names_mortal(pTHX_ char *const *array)
4743 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4745 if (array && *array) {
4746 target = newSVpvs_flags("", SVs_TEMP);
4748 sv_catpv(target, *array);
4751 sv_catpvs(target, " ");
4754 target = sv_mortalcopy(&PL_sv_no);
4759 /* Get system info. */
4763 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4765 I32 which = PL_op->op_type;
4766 register char **elem;
4768 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4769 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4770 struct hostent *gethostbyname(Netdb_name_t);
4771 struct hostent *gethostent(void);
4773 struct hostent *hent = NULL;
4777 if (which == OP_GHBYNAME) {
4778 #ifdef HAS_GETHOSTBYNAME
4779 const char* const name = POPpbytex;
4780 hent = PerlSock_gethostbyname(name);
4782 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4785 else if (which == OP_GHBYADDR) {
4786 #ifdef HAS_GETHOSTBYADDR
4787 const int addrtype = POPi;
4788 SV * const addrsv = POPs;
4790 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4792 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4794 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4798 #ifdef HAS_GETHOSTENT
4799 hent = PerlSock_gethostent();
4801 DIE(aTHX_ PL_no_sock_func, "gethostent");
4804 #ifdef HOST_NOT_FOUND
4806 #ifdef USE_REENTRANT_API
4807 # ifdef USE_GETHOSTENT_ERRNO
4808 h_errno = PL_reentrant_buffer->_gethostent_errno;
4811 STATUS_UNIX_SET(h_errno);
4815 if (GIMME != G_ARRAY) {
4816 PUSHs(sv = sv_newmortal());
4818 if (which == OP_GHBYNAME) {
4820 sv_setpvn(sv, hent->h_addr, hent->h_length);
4823 sv_setpv(sv, (char*)hent->h_name);
4829 mPUSHs(newSVpv((char*)hent->h_name, 0));
4830 PUSHs(space_join_names_mortal(hent->h_aliases));
4831 mPUSHi(hent->h_addrtype);
4832 len = hent->h_length;
4835 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4836 mXPUSHp(*elem, len);
4840 mPUSHp(hent->h_addr, len);
4842 PUSHs(sv_mortalcopy(&PL_sv_no));
4847 DIE(aTHX_ PL_no_sock_func, "gethostent");
4853 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4855 I32 which = PL_op->op_type;
4857 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4858 struct netent *getnetbyaddr(Netdb_net_t, int);
4859 struct netent *getnetbyname(Netdb_name_t);
4860 struct netent *getnetent(void);
4862 struct netent *nent;
4864 if (which == OP_GNBYNAME){
4865 #ifdef HAS_GETNETBYNAME
4866 const char * const name = POPpbytex;
4867 nent = PerlSock_getnetbyname(name);
4869 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4872 else if (which == OP_GNBYADDR) {
4873 #ifdef HAS_GETNETBYADDR
4874 const int addrtype = POPi;
4875 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4876 nent = PerlSock_getnetbyaddr(addr, addrtype);
4878 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4882 #ifdef HAS_GETNETENT
4883 nent = PerlSock_getnetent();
4885 DIE(aTHX_ PL_no_sock_func, "getnetent");
4888 #ifdef HOST_NOT_FOUND
4890 #ifdef USE_REENTRANT_API
4891 # ifdef USE_GETNETENT_ERRNO
4892 h_errno = PL_reentrant_buffer->_getnetent_errno;
4895 STATUS_UNIX_SET(h_errno);
4900 if (GIMME != G_ARRAY) {
4901 PUSHs(sv = sv_newmortal());
4903 if (which == OP_GNBYNAME)
4904 sv_setiv(sv, (IV)nent->n_net);
4906 sv_setpv(sv, nent->n_name);
4912 mPUSHs(newSVpv(nent->n_name, 0));
4913 PUSHs(space_join_names_mortal(nent->n_aliases));
4914 mPUSHi(nent->n_addrtype);
4915 mPUSHi(nent->n_net);
4920 DIE(aTHX_ PL_no_sock_func, "getnetent");
4926 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4928 I32 which = PL_op->op_type;
4930 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4931 struct protoent *getprotobyname(Netdb_name_t);
4932 struct protoent *getprotobynumber(int);
4933 struct protoent *getprotoent(void);
4935 struct protoent *pent;
4937 if (which == OP_GPBYNAME) {
4938 #ifdef HAS_GETPROTOBYNAME
4939 const char* const name = POPpbytex;
4940 pent = PerlSock_getprotobyname(name);
4942 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4945 else if (which == OP_GPBYNUMBER) {
4946 #ifdef HAS_GETPROTOBYNUMBER
4947 const int number = POPi;
4948 pent = PerlSock_getprotobynumber(number);
4950 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4954 #ifdef HAS_GETPROTOENT
4955 pent = PerlSock_getprotoent();
4957 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4961 if (GIMME != G_ARRAY) {
4962 PUSHs(sv = sv_newmortal());
4964 if (which == OP_GPBYNAME)
4965 sv_setiv(sv, (IV)pent->p_proto);
4967 sv_setpv(sv, pent->p_name);
4973 mPUSHs(newSVpv(pent->p_name, 0));
4974 PUSHs(space_join_names_mortal(pent->p_aliases));
4975 mPUSHi(pent->p_proto);
4980 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4986 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4988 I32 which = PL_op->op_type;
4990 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4991 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4992 struct servent *getservbyport(int, Netdb_name_t);
4993 struct servent *getservent(void);
4995 struct servent *sent;
4997 if (which == OP_GSBYNAME) {
4998 #ifdef HAS_GETSERVBYNAME
4999 const char * const proto = POPpbytex;
5000 const char * const name = POPpbytex;
5001 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5003 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5006 else if (which == OP_GSBYPORT) {
5007 #ifdef HAS_GETSERVBYPORT
5008 const char * const proto = POPpbytex;
5009 unsigned short port = (unsigned short)POPu;
5011 port = PerlSock_htons(port);
5013 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5015 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5019 #ifdef HAS_GETSERVENT
5020 sent = PerlSock_getservent();
5022 DIE(aTHX_ PL_no_sock_func, "getservent");
5026 if (GIMME != G_ARRAY) {
5027 PUSHs(sv = sv_newmortal());
5029 if (which == OP_GSBYNAME) {
5031 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5033 sv_setiv(sv, (IV)(sent->s_port));
5037 sv_setpv(sv, sent->s_name);
5043 mPUSHs(newSVpv(sent->s_name, 0));
5044 PUSHs(space_join_names_mortal(sent->s_aliases));
5046 mPUSHi(PerlSock_ntohs(sent->s_port));
5048 mPUSHi(sent->s_port);
5050 mPUSHs(newSVpv(sent->s_proto, 0));
5055 DIE(aTHX_ PL_no_sock_func, "getservent");
5061 #ifdef HAS_SETHOSTENT
5063 PerlSock_sethostent(TOPi);
5066 DIE(aTHX_ PL_no_sock_func, "sethostent");
5072 #ifdef HAS_SETNETENT
5074 (void)PerlSock_setnetent(TOPi);
5077 DIE(aTHX_ PL_no_sock_func, "setnetent");
5083 #ifdef HAS_SETPROTOENT
5085 (void)PerlSock_setprotoent(TOPi);
5088 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5094 #ifdef HAS_SETSERVENT
5096 (void)PerlSock_setservent(TOPi);
5099 DIE(aTHX_ PL_no_sock_func, "setservent");
5105 #ifdef HAS_ENDHOSTENT
5107 PerlSock_endhostent();
5111 DIE(aTHX_ PL_no_sock_func, "endhostent");
5117 #ifdef HAS_ENDNETENT
5119 PerlSock_endnetent();
5123 DIE(aTHX_ PL_no_sock_func, "endnetent");
5129 #ifdef HAS_ENDPROTOENT
5131 PerlSock_endprotoent();
5135 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5141 #ifdef HAS_ENDSERVENT
5143 PerlSock_endservent();
5147 DIE(aTHX_ PL_no_sock_func, "endservent");
5155 I32 which = PL_op->op_type;
5157 struct passwd *pwent = NULL;
5159 * We currently support only the SysV getsp* shadow password interface.
5160 * The interface is declared in <shadow.h> and often one needs to link
5161 * with -lsecurity or some such.
5162 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5165 * AIX getpwnam() is clever enough to return the encrypted password
5166 * only if the caller (euid?) is root.
5168 * There are at least three other shadow password APIs. Many platforms
5169 * seem to contain more than one interface for accessing the shadow
5170 * password databases, possibly for compatibility reasons.
5171 * The getsp*() is by far he simplest one, the other two interfaces
5172 * are much more complicated, but also very similar to each other.
5177 * struct pr_passwd *getprpw*();
5178 * The password is in
5179 * char getprpw*(...).ufld.fd_encrypt[]
5180 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5185 * struct es_passwd *getespw*();
5186 * The password is in
5187 * char *(getespw*(...).ufld.fd_encrypt)
5188 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5191 * struct userpw *getuserpw();
5192 * The password is in
5193 * char *(getuserpw(...)).spw_upw_passwd
5194 * (but the de facto standard getpwnam() should work okay)
5196 * Mention I_PROT here so that Configure probes for it.
5198 * In HP-UX for getprpw*() the manual page claims that one should include
5199 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5200 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5201 * and pp_sys.c already includes <shadow.h> if there is such.
5203 * Note that <sys/security.h> is already probed for, but currently
5204 * it is only included in special cases.
5206 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5207 * be preferred interface, even though also the getprpw*() interface
5208 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5209 * One also needs to call set_auth_parameters() in main() before
5210 * doing anything else, whether one is using getespw*() or getprpw*().
5212 * Note that accessing the shadow databases can be magnitudes
5213 * slower than accessing the standard databases.
5218 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5219 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5220 * the pw_comment is left uninitialized. */
5221 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5227 const char* const name = POPpbytex;
5228 pwent = getpwnam(name);
5234 pwent = getpwuid(uid);
5238 # ifdef HAS_GETPWENT
5240 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5241 if (pwent) pwent = getpwnam(pwent->pw_name);
5244 DIE(aTHX_ PL_no_func, "getpwent");
5250 if (GIMME != G_ARRAY) {
5251 PUSHs(sv = sv_newmortal());
5253 if (which == OP_GPWNAM)
5254 # if Uid_t_sign <= 0
5255 sv_setiv(sv, (IV)pwent->pw_uid);
5257 sv_setuv(sv, (UV)pwent->pw_uid);
5260 sv_setpv(sv, pwent->pw_name);
5266 mPUSHs(newSVpv(pwent->pw_name, 0));
5270 /* If we have getspnam(), we try to dig up the shadow
5271 * password. If we are underprivileged, the shadow
5272 * interface will set the errno to EACCES or similar,
5273 * and return a null pointer. If this happens, we will
5274 * use the dummy password (usually "*" or "x") from the
5275 * standard password database.
5277 * In theory we could skip the shadow call completely
5278 * if euid != 0 but in practice we cannot know which
5279 * security measures are guarding the shadow databases
5280 * on a random platform.
5282 * Resist the urge to use additional shadow interfaces.
5283 * Divert the urge to writing an extension instead.
5286 /* Some AIX setups falsely(?) detect some getspnam(), which
5287 * has a different API than the Solaris/IRIX one. */
5288 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5291 const struct spwd * const spwent = getspnam(pwent->pw_name);
5292 /* Save and restore errno so that
5293 * underprivileged attempts seem
5294 * to have never made the unsccessful
5295 * attempt to retrieve the shadow password. */
5297 if (spwent && spwent->sp_pwdp)
5298 sv_setpv(sv, spwent->sp_pwdp);
5302 if (!SvPOK(sv)) /* Use the standard password, then. */
5303 sv_setpv(sv, pwent->pw_passwd);
5306 # ifndef INCOMPLETE_TAINTS
5307 /* passwd is tainted because user himself can diddle with it.
5308 * admittedly not much and in a very limited way, but nevertheless. */
5312 # if Uid_t_sign <= 0
5313 mPUSHi(pwent->pw_uid);
5315 mPUSHu(pwent->pw_uid);
5318 # if Uid_t_sign <= 0
5319 mPUSHi(pwent->pw_gid);
5321 mPUSHu(pwent->pw_gid);
5323 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5324 * because of the poor interface of the Perl getpw*(),
5325 * not because there's some standard/convention saying so.
5326 * A better interface would have been to return a hash,
5327 * but we are accursed by our history, alas. --jhi. */
5329 mPUSHi(pwent->pw_change);
5332 mPUSHi(pwent->pw_quota);
5335 mPUSHs(newSVpv(pwent->pw_age, 0));
5337 /* I think that you can never get this compiled, but just in case. */
5338 PUSHs(sv_mortalcopy(&PL_sv_no));
5343 /* pw_class and pw_comment are mutually exclusive--.
5344 * see the above note for pw_change, pw_quota, and pw_age. */
5346 mPUSHs(newSVpv(pwent->pw_class, 0));
5349 mPUSHs(newSVpv(pwent->pw_comment, 0));
5351 /* I think that you can never get this compiled, but just in case. */
5352 PUSHs(sv_mortalcopy(&PL_sv_no));
5357 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5359 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5361 # ifndef INCOMPLETE_TAINTS
5362 /* pw_gecos is tainted because user himself can diddle with it. */
5366 mPUSHs(newSVpv(pwent->pw_dir, 0));
5368 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5369 # ifndef INCOMPLETE_TAINTS
5370 /* pw_shell is tainted because user himself can diddle with it. */
5375 mPUSHi(pwent->pw_expire);
5380 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5386 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5391 DIE(aTHX_ PL_no_func, "setpwent");
5397 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5402 DIE(aTHX_ PL_no_func, "endpwent");
5410 const I32 which = PL_op->op_type;
5411 const struct group *grent;
5413 if (which == OP_GGRNAM) {
5414 const char* const name = POPpbytex;
5415 grent = (const struct group *)getgrnam(name);
5417 else if (which == OP_GGRGID) {
5418 const Gid_t gid = POPi;
5419 grent = (const struct group *)getgrgid(gid);
5423 grent = (struct group *)getgrent();
5425 DIE(aTHX_ PL_no_func, "getgrent");
5429 if (GIMME != G_ARRAY) {
5430 SV * const sv = sv_newmortal();
5434 if (which == OP_GGRNAM)
5436 sv_setiv(sv, (IV)grent->gr_gid);
5438 sv_setuv(sv, (UV)grent->gr_gid);
5441 sv_setpv(sv, grent->gr_name);
5447 mPUSHs(newSVpv(grent->gr_name, 0));
5450 mPUSHs(newSVpv(grent->gr_passwd, 0));
5452 PUSHs(sv_mortalcopy(&PL_sv_no));
5456 mPUSHi(grent->gr_gid);
5458 mPUSHu(grent->gr_gid);
5461 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5462 /* In UNICOS/mk (_CRAYMPP) the multithreading
5463 * versions (getgrnam_r, getgrgid_r)
5464 * seem to return an illegal pointer
5465 * as the group members list, gr_mem.
5466 * getgrent() doesn't even have a _r version
5467 * but the gr_mem is poisonous anyway.
5468 * So yes, you cannot get the list of group
5469 * members if building multithreaded in UNICOS/mk. */
5470 PUSHs(space_join_names_mortal(grent->gr_mem));
5476 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5482 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5487 DIE(aTHX_ PL_no_func, "setgrent");
5493 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5498 DIE(aTHX_ PL_no_func, "endgrent");
5508 if (!(tmps = PerlProc_getlogin()))
5510 PUSHp(tmps, strlen(tmps));
5513 DIE(aTHX_ PL_no_func, "getlogin");
5517 /* Miscellaneous. */
5522 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5523 register I32 items = SP - MARK;
5524 unsigned long a[20];
5529 while (++MARK <= SP) {
5530 if (SvTAINTED(*MARK)) {
5536 TAINT_PROPER("syscall");
5539 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5540 * or where sizeof(long) != sizeof(char*). But such machines will
5541 * not likely have syscall implemented either, so who cares?
5543 while (++MARK <= SP) {
5544 if (SvNIOK(*MARK) || !i)
5545 a[i++] = SvIV(*MARK);
5546 else if (*MARK == &PL_sv_undef)
5549 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5555 DIE(aTHX_ "Too many args to syscall");
5557 DIE(aTHX_ "Too few args to syscall");
5559 retval = syscall(a[0]);
5562 retval = syscall(a[0],a[1]);
5565 retval = syscall(a[0],a[1],a[2]);
5568 retval = syscall(a[0],a[1],a[2],a[3]);
5571 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5574 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5577 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5580 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5584 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5587 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5590 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5594 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5598 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5602 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5603 a[10],a[11],a[12],a[13]);
5605 #endif /* atarist */
5611 DIE(aTHX_ PL_no_func, "syscall");
5615 #ifdef FCNTL_EMULATE_FLOCK
5617 /* XXX Emulate flock() with fcntl().
5618 What's really needed is a good file locking module.
5622 fcntl_emulate_flock(int fd, int operation)
5627 switch (operation & ~LOCK_NB) {
5629 flock.l_type = F_RDLCK;
5632 flock.l_type = F_WRLCK;
5635 flock.l_type = F_UNLCK;
5641 flock.l_whence = SEEK_SET;
5642 flock.l_start = flock.l_len = (Off_t)0;
5644 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5645 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5646 errno = EWOULDBLOCK;
5650 #endif /* FCNTL_EMULATE_FLOCK */
5652 #ifdef LOCKF_EMULATE_FLOCK
5654 /* XXX Emulate flock() with lockf(). This is just to increase
5655 portability of scripts. The calls are not completely
5656 interchangeable. What's really needed is a good file
5660 /* The lockf() constants might have been defined in <unistd.h>.
5661 Unfortunately, <unistd.h> causes troubles on some mixed
5662 (BSD/POSIX) systems, such as SunOS 4.1.3.
5664 Further, the lockf() constants aren't POSIX, so they might not be
5665 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5666 just stick in the SVID values and be done with it. Sigh.
5670 # define F_ULOCK 0 /* Unlock a previously locked region */
5673 # define F_LOCK 1 /* Lock a region for exclusive use */
5676 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5679 # define F_TEST 3 /* Test a region for other processes locks */
5683 lockf_emulate_flock(int fd, int operation)
5689 /* flock locks entire file so for lockf we need to do the same */
5690 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5691 if (pos > 0) /* is seekable and needs to be repositioned */
5692 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5693 pos = -1; /* seek failed, so don't seek back afterwards */
5696 switch (operation) {
5698 /* LOCK_SH - get a shared lock */
5700 /* LOCK_EX - get an exclusive lock */
5702 i = lockf (fd, F_LOCK, 0);
5705 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5706 case LOCK_SH|LOCK_NB:
5707 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5708 case LOCK_EX|LOCK_NB:
5709 i = lockf (fd, F_TLOCK, 0);
5711 if ((errno == EAGAIN) || (errno == EACCES))
5712 errno = EWOULDBLOCK;
5715 /* LOCK_UN - unlock (non-blocking is a no-op) */
5717 case LOCK_UN|LOCK_NB:
5718 i = lockf (fd, F_ULOCK, 0);
5721 /* Default - can't decipher operation */
5728 if (pos > 0) /* need to restore position of the handle */
5729 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5734 #endif /* LOCKF_EMULATE_FLOCK */
5738 * c-indentation-style: bsd
5740 * indent-tabs-mode: t
5743 * ex: set ts=8 sts=4 sw=4 noet: