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);
507 GV * const gv = MUTABLE_GV(*++MARK);
510 DIE(aTHX_ PL_no_usym, "filehandle");
512 if ((io = GvIOp(gv))) {
514 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
517 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
518 "Opening dirhandle %s also as a file",
521 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
523 /* Method's args are same as ours ... */
524 /* ... except handle is replaced by the object */
525 *MARK-- = SvTIED_obj(MUTABLE_SV(io), mg);
528 ENTER_with_name("call_OPEN");
529 call_method("OPEN", G_SCALAR);
530 LEAVE_with_name("call_OPEN");
543 tmps = SvPV_const(sv, len);
544 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
547 PUSHi( (I32)PL_forkprocess );
548 else if (PL_forkprocess == 0) /* we are a new child */
555 /* This is private to this function, which is private to this file.
556 Use 0x04 rather than the next available bit, to help the compiler if the
557 architecture can generate more efficient instructions. */
558 #define MORTALIZE_NOT_NEEDED 0x04
561 S_tied_handle_method(pTHX_ const char *const methname, SV **sp,
562 IO *const io, MAGIC *const mg, const U32 flags,
563 unsigned int argc, ...)
565 PERL_ARGS_ASSERT_TIED_HANDLE_METHOD;
567 assert((MORTALIZE_NOT_NEEDED & G_WANT) == 0);
570 PUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
572 const U32 mortalize_not_needed = flags & MORTALIZE_NOT_NEEDED;
574 va_start(args, argc);
576 SV *const arg = va_arg(args, SV *);
577 if(mortalize_not_needed)
586 ENTER_with_name("call_tied_handle_method");
587 call_method(methname, flags & G_WANT);
588 LEAVE_with_name("call_tied_handle_method");
592 #define tied_handle_method(a,b,c,d) \
593 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR,0)
594 #define tied_handle_method1(a,b,c,d,e) \
595 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
596 #define tied_handle_method2(a,b,c,d,e,f) \
597 S_tied_handle_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
602 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
608 IO * const io = GvIO(gv);
610 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
612 return tied_handle_method("CLOSE", SP, io, mg);
616 PUSHs(boolSV(do_close(gv, TRUE)));
629 GV * const wgv = MUTABLE_GV(POPs);
630 GV * const rgv = MUTABLE_GV(POPs);
635 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
636 DIE(aTHX_ PL_no_usym, "filehandle");
641 do_close(rgv, FALSE);
643 do_close(wgv, FALSE);
645 if (PerlProc_pipe(fd) < 0)
648 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
649 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
650 IoOFP(rstio) = IoIFP(rstio);
651 IoIFP(wstio) = IoOFP(wstio);
652 IoTYPE(rstio) = IoTYPE_RDONLY;
653 IoTYPE(wstio) = IoTYPE_WRONLY;
655 if (!IoIFP(rstio) || !IoOFP(wstio)) {
657 PerlIO_close(IoIFP(rstio));
659 PerlLIO_close(fd[0]);
661 PerlIO_close(IoOFP(wstio));
663 PerlLIO_close(fd[1]);
666 #if defined(HAS_FCNTL) && defined(F_SETFD)
667 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
668 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
675 DIE(aTHX_ PL_no_func, "pipe");
690 gv = MUTABLE_GV(POPs);
692 if (gv && (io = GvIO(gv))
693 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
695 return tied_handle_method("FILENO", SP, io, mg);
698 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
699 /* Can't do this because people seem to do things like
700 defined(fileno($foo)) to check whether $foo is a valid fh.
701 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
702 report_evil_fh(gv, io, PL_op->op_type);
707 PUSHi(PerlIO_fileno(fp));
720 anum = PerlLIO_umask(022);
721 /* setting it to 022 between the two calls to umask avoids
722 * to have a window where the umask is set to 0 -- meaning
723 * that another thread could create world-writeable files. */
725 (void)PerlLIO_umask(anum);
728 anum = PerlLIO_umask(POPi);
729 TAINT_PROPER("umask");
732 /* Only DIE if trying to restrict permissions on "user" (self).
733 * Otherwise it's harmless and more useful to just return undef
734 * since 'group' and 'other' concepts probably don't exist here. */
735 if (MAXARG >= 1 && (POPi & 0700))
736 DIE(aTHX_ "umask not implemented");
737 XPUSHs(&PL_sv_undef);
756 gv = MUTABLE_GV(POPs);
758 if (gv && (io = GvIO(gv))) {
759 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
761 /* This takes advantage of the implementation of the varargs
762 function, which I don't think that the optimiser will be able to
763 figure out. Although, as it's a static function, in theory it
765 return S_tied_handle_method(aTHX_ "BINMODE", SP, io, mg,
766 G_SCALAR|MORTALIZE_NOT_NEEDED,
767 discp ? 1 : 0, discp);
771 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
772 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
773 report_evil_fh(gv, io, PL_op->op_type);
774 SETERRNO(EBADF,RMS_IFI);
781 const char *d = NULL;
784 d = SvPV_const(discp, len);
785 mode = mode_from_discipline(d, len);
786 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
787 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
788 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
809 const I32 markoff = MARK - PL_stack_base;
810 const char *methname;
811 int how = PERL_MAGIC_tied;
815 switch(SvTYPE(varsv)) {
817 methname = "TIEHASH";
818 HvEITER_set(MUTABLE_HV(varsv), 0);
821 methname = "TIEARRAY";
824 if (isGV_with_GP(varsv)) {
825 methname = "TIEHANDLE";
826 how = PERL_MAGIC_tiedscalar;
827 /* For tied filehandles, we apply tiedscalar magic to the IO
828 slot of the GP rather than the GV itself. AMS 20010812 */
830 GvIOp(varsv) = newIO();
831 varsv = MUTABLE_SV(GvIOp(varsv));
836 methname = "TIESCALAR";
837 how = PERL_MAGIC_tiedscalar;
841 if (sv_isobject(*MARK)) { /* Calls GET magic. */
842 ENTER_with_name("call_TIE");
843 PUSHSTACKi(PERLSI_MAGIC);
845 EXTEND(SP,(I32)items);
849 call_method(methname, G_SCALAR);
852 /* Not clear why we don't call call_method here too.
853 * perhaps to get different error message ?
856 const char *name = SvPV_nomg_const(*MARK, len);
857 stash = gv_stashpvn(name, len, 0);
858 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
859 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
860 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
862 ENTER_with_name("call_TIE");
863 PUSHSTACKi(PERLSI_MAGIC);
865 EXTEND(SP,(I32)items);
869 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
875 if (sv_isobject(sv)) {
876 sv_unmagic(varsv, how);
877 /* Croak if a self-tie on an aggregate is attempted. */
878 if (varsv == SvRV(sv) &&
879 (SvTYPE(varsv) == SVt_PVAV ||
880 SvTYPE(varsv) == SVt_PVHV))
882 "Self-ties of arrays and hashes are not supported");
883 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
885 LEAVE_with_name("call_TIE");
886 SP = PL_stack_base + markoff;
896 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
897 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
899 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
902 if ((mg = SvTIED_mg(sv, how))) {
903 SV * const obj = SvRV(SvTIED_obj(sv, mg));
905 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
907 if (gv && isGV(gv) && (cv = GvCV(gv))) {
909 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
910 mXPUSHi(SvREFCNT(obj) - 1);
912 ENTER_with_name("call_UNTIE");
913 call_sv(MUTABLE_SV(cv), G_VOID);
914 LEAVE_with_name("call_UNTIE");
917 else if (mg && SvREFCNT(obj) > 1) {
918 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
919 "untie attempted while %"UVuf" inner references still exist",
920 (UV)SvREFCNT(obj) - 1 ) ;
924 sv_unmagic(sv, how) ;
934 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
935 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
937 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
940 if ((mg = SvTIED_mg(sv, how))) {
941 SV *osv = SvTIED_obj(sv, mg);
942 if (osv == mg->mg_obj)
943 osv = sv_mortalcopy(osv);
957 HV * const hv = MUTABLE_HV(POPs);
958 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
959 stash = gv_stashsv(sv, 0);
960 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
962 require_pv("AnyDBM_File.pm");
964 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
965 DIE(aTHX_ "No dbm on this machine");
975 mPUSHu(O_RDWR|O_CREAT);
980 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
983 if (!sv_isobject(TOPs)) {
991 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
995 if (sv_isobject(TOPs)) {
996 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
997 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1014 struct timeval timebuf;
1015 struct timeval *tbuf = &timebuf;
1018 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1023 # if BYTEORDER & 0xf0000
1024 # define ORDERBYTE (0x88888888 - BYTEORDER)
1026 # define ORDERBYTE (0x4444 - BYTEORDER)
1032 for (i = 1; i <= 3; i++) {
1033 SV * const sv = SP[i];
1036 if (SvREADONLY(sv)) {
1038 sv_force_normal_flags(sv, 0);
1039 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1040 DIE(aTHX_ "%s", PL_no_modify);
1043 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1044 SvPV_force_nolen(sv); /* force string conversion */
1051 /* little endians can use vecs directly */
1052 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1059 masksize = NFDBITS / NBBY;
1061 masksize = sizeof(long); /* documented int, everyone seems to use long */
1063 Zero(&fd_sets[0], 4, char*);
1066 # if SELECT_MIN_BITS == 1
1067 growsize = sizeof(fd_set);
1069 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1070 # undef SELECT_MIN_BITS
1071 # define SELECT_MIN_BITS __FD_SETSIZE
1073 /* If SELECT_MIN_BITS is greater than one we most probably will want
1074 * to align the sizes with SELECT_MIN_BITS/8 because for example
1075 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1076 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1077 * on (sets/tests/clears bits) is 32 bits. */
1078 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1086 timebuf.tv_sec = (long)value;
1087 value -= (NV)timebuf.tv_sec;
1088 timebuf.tv_usec = (long)(value * 1000000.0);
1093 for (i = 1; i <= 3; i++) {
1095 if (!SvOK(sv) || SvCUR(sv) == 0) {
1102 Sv_Grow(sv, growsize);
1106 while (++j <= growsize) {
1110 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1112 Newx(fd_sets[i], growsize, char);
1113 for (offset = 0; offset < growsize; offset += masksize) {
1114 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1115 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1118 fd_sets[i] = SvPVX(sv);
1122 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1123 /* Can't make just the (void*) conditional because that would be
1124 * cpp #if within cpp macro, and not all compilers like that. */
1125 nfound = PerlSock_select(
1127 (Select_fd_set_t) fd_sets[1],
1128 (Select_fd_set_t) fd_sets[2],
1129 (Select_fd_set_t) fd_sets[3],
1130 (void*) tbuf); /* Workaround for compiler bug. */
1132 nfound = PerlSock_select(
1134 (Select_fd_set_t) fd_sets[1],
1135 (Select_fd_set_t) fd_sets[2],
1136 (Select_fd_set_t) fd_sets[3],
1139 for (i = 1; i <= 3; i++) {
1142 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1144 for (offset = 0; offset < growsize; offset += masksize) {
1145 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1146 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1148 Safefree(fd_sets[i]);
1155 if (GIMME == G_ARRAY && tbuf) {
1156 value = (NV)(timebuf.tv_sec) +
1157 (NV)(timebuf.tv_usec) / 1000000.0;
1162 DIE(aTHX_ "select not implemented");
1168 =for apidoc setdefout
1170 Sets PL_defoutgv, the default file handle for output, to the passed in
1171 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1172 count of the passed in typeglob is increased by one, and the reference count
1173 of the typeglob that PL_defoutgv points to is decreased by one.
1179 Perl_setdefout(pTHX_ GV *gv)
1182 SvREFCNT_inc_simple_void(gv);
1183 SvREFCNT_dec(PL_defoutgv);
1191 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1192 GV * egv = GvEGVx(PL_defoutgv);
1196 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1198 XPUSHs(&PL_sv_undef);
1200 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1201 if (gvp && *gvp == egv) {
1202 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1206 mXPUSHs(newRV(MUTABLE_SV(egv)));
1211 if (!GvIO(newdefout))
1212 gv_IOadd(newdefout);
1213 setdefout(newdefout);
1223 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1228 if (gv && (io = GvIO(gv))) {
1229 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1231 const U32 gimme = GIMME_V;
1232 S_tied_handle_method(aTHX_ "GETC", SP, io, mg, gimme, 0);
1233 if (gimme == G_SCALAR) {
1235 SvSetMagicSV_nosteal(TARG, TOPs);
1240 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1241 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1242 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1243 report_evil_fh(gv, io, PL_op->op_type);
1244 SETERRNO(EBADF,RMS_IFI);
1248 sv_setpvs(TARG, " ");
1249 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1250 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1251 /* Find out how many bytes the char needs */
1252 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1255 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1256 SvCUR_set(TARG,1+len);
1265 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1268 register PERL_CONTEXT *cx;
1269 const I32 gimme = GIMME_V;
1271 PERL_ARGS_ASSERT_DOFORM;
1276 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1277 PUSHFORMAT(cx, retop);
1279 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1281 setdefout(gv); /* locally select filehandle so $% et al work */
1300 gv = MUTABLE_GV(POPs);
1314 goto not_a_format_reference;
1319 tmpsv = sv_newmortal();
1320 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1321 name = SvPV_nolen_const(tmpsv);
1323 DIE(aTHX_ "Undefined format \"%s\" called", name);
1325 not_a_format_reference:
1326 DIE(aTHX_ "Not a format reference");
1329 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1331 IoFLAGS(io) &= ~IOf_DIDTOP;
1332 return doform(cv,gv,PL_op->op_next);
1338 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1339 register IO * const io = GvIOp(gv);
1344 register PERL_CONTEXT *cx;
1346 if (!io || !(ofp = IoOFP(io)))
1349 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1350 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1352 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1353 PL_formtarget != PL_toptarget)
1357 if (!IoTOP_GV(io)) {
1360 if (!IoTOP_NAME(io)) {
1362 if (!IoFMT_NAME(io))
1363 IoFMT_NAME(io) = savepv(GvNAME(gv));
1364 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1365 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1366 if ((topgv && GvFORM(topgv)) ||
1367 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1368 IoTOP_NAME(io) = savesvpv(topname);
1370 IoTOP_NAME(io) = savepvs("top");
1372 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1373 if (!topgv || !GvFORM(topgv)) {
1374 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1377 IoTOP_GV(io) = topgv;
1379 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1380 I32 lines = IoLINES_LEFT(io);
1381 const char *s = SvPVX_const(PL_formtarget);
1382 if (lines <= 0) /* Yow, header didn't even fit!!! */
1384 while (lines-- > 0) {
1385 s = strchr(s, '\n');
1391 const STRLEN save = SvCUR(PL_formtarget);
1392 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1393 do_print(PL_formtarget, ofp);
1394 SvCUR_set(PL_formtarget, save);
1395 sv_chop(PL_formtarget, s);
1396 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1399 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1400 do_print(PL_formfeed, ofp);
1401 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1403 PL_formtarget = PL_toptarget;
1404 IoFLAGS(io) |= IOf_DIDTOP;
1407 DIE(aTHX_ "bad top format reference");
1410 SV * const sv = sv_newmortal();
1412 gv_efullname4(sv, fgv, NULL, FALSE);
1413 name = SvPV_nolen_const(sv);
1415 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1417 DIE(aTHX_ "Undefined top format called");
1419 if (cv && CvCLONE(cv))
1420 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1421 return doform(cv, gv, PL_op);
1425 POPBLOCK(cx,PL_curpm);
1431 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1433 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1434 else if (ckWARN(WARN_CLOSED))
1435 report_evil_fh(gv, io, PL_op->op_type);
1440 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1441 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1443 if (!do_print(PL_formtarget, fp))
1446 FmLINES(PL_formtarget) = 0;
1447 SvCUR_set(PL_formtarget, 0);
1448 *SvEND(PL_formtarget) = '\0';
1449 if (IoFLAGS(io) & IOf_FLUSH)
1450 (void)PerlIO_flush(fp);
1455 PL_formtarget = PL_bodytarget;
1457 PERL_UNUSED_VAR(newsp);
1458 PERL_UNUSED_VAR(gimme);
1459 return cx->blk_sub.retop;
1464 dVAR; dSP; dMARK; dORIGMARK;
1470 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1472 if (gv && (io = GvIO(gv))) {
1473 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1475 if (MARK == ORIGMARK) {
1478 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1482 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1485 call_method("PRINTF", G_SCALAR);
1488 MARK = ORIGMARK + 1;
1496 if (!(io = GvIO(gv))) {
1497 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1498 report_evil_fh(gv, io, PL_op->op_type);
1499 SETERRNO(EBADF,RMS_IFI);
1502 else if (!(fp = IoOFP(io))) {
1503 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1505 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1506 else if (ckWARN(WARN_CLOSED))
1507 report_evil_fh(gv, io, PL_op->op_type);
1509 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1513 if (SvTAINTED(MARK[1]))
1514 TAINT_PROPER("printf");
1515 do_sprintf(sv, SP - MARK, MARK + 1);
1516 if (!do_print(sv, fp))
1519 if (IoFLAGS(io) & IOf_FLUSH)
1520 if (PerlIO_flush(fp) == EOF)
1531 PUSHs(&PL_sv_undef);
1539 const int perm = (MAXARG > 3) ? POPi : 0666;
1540 const int mode = POPi;
1541 SV * const sv = POPs;
1542 GV * const gv = MUTABLE_GV(POPs);
1545 /* Need TIEHANDLE method ? */
1546 const char * const tmps = SvPV_const(sv, len);
1547 /* FIXME? do_open should do const */
1548 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1549 IoLINES(GvIOp(gv)) = 0;
1553 PUSHs(&PL_sv_undef);
1560 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1566 Sock_size_t bufsize;
1574 bool charstart = FALSE;
1575 STRLEN charskip = 0;
1578 GV * const gv = MUTABLE_GV(*++MARK);
1579 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1580 && gv && (io = GvIO(gv)) )
1582 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1586 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1588 call_method("READ", G_SCALAR);
1602 sv_setpvs(bufsv, "");
1603 length = SvIVx(*++MARK);
1606 offset = SvIVx(*++MARK);
1610 if (!io || !IoIFP(io)) {
1611 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1612 report_evil_fh(gv, io, PL_op->op_type);
1613 SETERRNO(EBADF,RMS_IFI);
1616 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1617 buffer = SvPVutf8_force(bufsv, blen);
1618 /* UTF-8 may not have been set if they are all low bytes */
1623 buffer = SvPV_force(bufsv, blen);
1624 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1627 DIE(aTHX_ "Negative length");
1635 if (PL_op->op_type == OP_RECV) {
1636 char namebuf[MAXPATHLEN];
1637 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1638 bufsize = sizeof (struct sockaddr_in);
1640 bufsize = sizeof namebuf;
1642 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1646 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1647 /* 'offset' means 'flags' here */
1648 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1649 (struct sockaddr *)namebuf, &bufsize);
1653 /* Bogus return without padding */
1654 bufsize = sizeof (struct sockaddr_in);
1656 SvCUR_set(bufsv, count);
1657 *SvEND(bufsv) = '\0';
1658 (void)SvPOK_only(bufsv);
1662 /* This should not be marked tainted if the fp is marked clean */
1663 if (!(IoFLAGS(io) & IOf_UNTAINT))
1664 SvTAINTED_on(bufsv);
1666 sv_setpvn(TARG, namebuf, bufsize);
1671 if (PL_op->op_type == OP_RECV)
1672 DIE(aTHX_ PL_no_sock_func, "recv");
1674 if (DO_UTF8(bufsv)) {
1675 /* offset adjust in characters not bytes */
1676 blen = sv_len_utf8(bufsv);
1679 if (-offset > (int)blen)
1680 DIE(aTHX_ "Offset outside string");
1683 if (DO_UTF8(bufsv)) {
1684 /* convert offset-as-chars to offset-as-bytes */
1685 if (offset >= (int)blen)
1686 offset += SvCUR(bufsv) - blen;
1688 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1691 bufsize = SvCUR(bufsv);
1692 /* Allocating length + offset + 1 isn't perfect in the case of reading
1693 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1695 (should be 2 * length + offset + 1, or possibly something longer if
1696 PL_encoding is true) */
1697 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1698 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1699 Zero(buffer+bufsize, offset-bufsize, char);
1701 buffer = buffer + offset;
1703 read_target = bufsv;
1705 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1706 concatenate it to the current buffer. */
1708 /* Truncate the existing buffer to the start of where we will be
1710 SvCUR_set(bufsv, offset);
1712 read_target = sv_newmortal();
1713 SvUPGRADE(read_target, SVt_PV);
1714 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1717 if (PL_op->op_type == OP_SYSREAD) {
1718 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1719 if (IoTYPE(io) == IoTYPE_SOCKET) {
1720 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1726 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1731 #ifdef HAS_SOCKET__bad_code_maybe
1732 if (IoTYPE(io) == IoTYPE_SOCKET) {
1733 char namebuf[MAXPATHLEN];
1734 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1735 bufsize = sizeof (struct sockaddr_in);
1737 bufsize = sizeof namebuf;
1739 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1740 (struct sockaddr *)namebuf, &bufsize);
1745 count = PerlIO_read(IoIFP(io), buffer, length);
1746 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1747 if (count == 0 && PerlIO_error(IoIFP(io)))
1751 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1752 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1755 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1756 *SvEND(read_target) = '\0';
1757 (void)SvPOK_only(read_target);
1758 if (fp_utf8 && !IN_BYTES) {
1759 /* Look at utf8 we got back and count the characters */
1760 const char *bend = buffer + count;
1761 while (buffer < bend) {
1763 skip = UTF8SKIP(buffer);
1766 if (buffer - charskip + skip > bend) {
1767 /* partial character - try for rest of it */
1768 length = skip - (bend-buffer);
1769 offset = bend - SvPVX_const(bufsv);
1781 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1782 provided amount read (count) was what was requested (length)
1784 if (got < wanted && count == length) {
1785 length = wanted - got;
1786 offset = bend - SvPVX_const(bufsv);
1789 /* return value is character count */
1793 else if (buffer_utf8) {
1794 /* Let svcatsv upgrade the bytes we read in to utf8.
1795 The buffer is a mortal so will be freed soon. */
1796 sv_catsv_nomg(bufsv, read_target);
1799 /* This should not be marked tainted if the fp is marked clean */
1800 if (!(IoFLAGS(io) & IOf_UNTAINT))
1801 SvTAINTED_on(bufsv);
1813 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1819 STRLEN orig_blen_bytes;
1820 const int op_type = PL_op->op_type;
1824 GV *const gv = MUTABLE_GV(*++MARK);
1825 if (PL_op->op_type == OP_SYSWRITE
1826 && gv && (io = GvIO(gv))) {
1827 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1831 if (MARK == SP - 1) {
1833 mXPUSHi(sv_len(sv));
1838 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1840 call_method("WRITE", G_SCALAR);
1856 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1858 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1859 if (io && IoIFP(io))
1860 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1862 report_evil_fh(gv, io, PL_op->op_type);
1864 SETERRNO(EBADF,RMS_IFI);
1868 /* Do this first to trigger any overloading. */
1869 buffer = SvPV_const(bufsv, blen);
1870 orig_blen_bytes = blen;
1871 doing_utf8 = DO_UTF8(bufsv);
1873 if (PerlIO_isutf8(IoIFP(io))) {
1874 if (!SvUTF8(bufsv)) {
1875 /* We don't modify the original scalar. */
1876 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1877 buffer = (char *) tmpbuf;
1881 else if (doing_utf8) {
1882 STRLEN tmplen = blen;
1883 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1886 buffer = (char *) tmpbuf;
1890 assert((char *)result == buffer);
1891 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1895 if (op_type == OP_SYSWRITE) {
1896 Size_t length = 0; /* This length is in characters. */
1902 /* The SV is bytes, and we've had to upgrade it. */
1903 blen_chars = orig_blen_bytes;
1905 /* The SV really is UTF-8. */
1906 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1907 /* Don't call sv_len_utf8 again because it will call magic
1908 or overloading a second time, and we might get back a
1909 different result. */
1910 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1912 /* It's safe, and it may well be cached. */
1913 blen_chars = sv_len_utf8(bufsv);
1921 length = blen_chars;
1923 #if Size_t_size > IVSIZE
1924 length = (Size_t)SvNVx(*++MARK);
1926 length = (Size_t)SvIVx(*++MARK);
1928 if ((SSize_t)length < 0) {
1930 DIE(aTHX_ "Negative length");
1935 offset = SvIVx(*++MARK);
1937 if (-offset > (IV)blen_chars) {
1939 DIE(aTHX_ "Offset outside string");
1941 offset += blen_chars;
1942 } else if (offset > (IV)blen_chars) {
1944 DIE(aTHX_ "Offset outside string");
1948 if (length > blen_chars - offset)
1949 length = blen_chars - offset;
1951 /* Here we convert length from characters to bytes. */
1952 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1953 /* Either we had to convert the SV, or the SV is magical, or
1954 the SV has overloading, in which case we can't or mustn't
1955 or mustn't call it again. */
1957 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1958 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1960 /* It's a real UTF-8 SV, and it's not going to change under
1961 us. Take advantage of any cache. */
1963 I32 len_I32 = length;
1965 /* Convert the start and end character positions to bytes.
1966 Remember that the second argument to sv_pos_u2b is relative
1968 sv_pos_u2b(bufsv, &start, &len_I32);
1975 buffer = buffer+offset;
1977 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1978 if (IoTYPE(io) == IoTYPE_SOCKET) {
1979 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1985 /* See the note at doio.c:do_print about filesize limits. --jhi */
1986 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1992 const int flags = SvIVx(*++MARK);
1995 char * const sockbuf = SvPVx(*++MARK, mlen);
1996 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1997 flags, (struct sockaddr *)sockbuf, mlen);
2001 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2006 DIE(aTHX_ PL_no_sock_func, "send");
2013 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2016 #if Size_t_size > IVSIZE
2036 * in Perl 5.12 and later, the additional parameter is a bitmask:
2039 * 2 = eof() <- ARGV magic
2041 * I'll rely on the compiler's trace flow analysis to decide whether to
2042 * actually assign this out here, or punt it into the only block where it is
2043 * used. Doing it out here is DRY on the condition logic.
2048 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2054 if (PL_op->op_flags & OPf_SPECIAL) {
2055 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2059 gv = PL_last_in_gv; /* eof */
2067 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2068 return tied_handle_method1("EOF", SP, io, mg, newSVuv(which));
2071 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2072 if (io && !IoIFP(io)) {
2073 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2075 IoFLAGS(io) &= ~IOf_START;
2076 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2078 sv_setpvs(GvSV(gv), "-");
2080 GvSV(gv) = newSVpvs("-");
2081 SvSETMAGIC(GvSV(gv));
2083 else if (!nextargv(gv))
2088 PUSHs(boolSV(do_eof(gv)));
2099 PL_last_in_gv = MUTABLE_GV(POPs);
2104 if (gv && (io = GvIO(gv))) {
2105 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2107 return tied_handle_method("TELL", SP, io, mg);
2112 SETERRNO(EBADF,RMS_IFI);
2117 #if LSEEKSIZE > IVSIZE
2118 PUSHn( do_tell(gv) );
2120 PUSHi( do_tell(gv) );
2128 const int whence = POPi;
2129 #if LSEEKSIZE > IVSIZE
2130 const Off_t offset = (Off_t)SvNVx(POPs);
2132 const Off_t offset = (Off_t)SvIVx(POPs);
2135 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2138 if (gv && (io = GvIO(gv))) {
2139 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2141 #if LSEEKSIZE > IVSIZE
2142 SV *const offset_sv = newSVnv((NV) offset);
2144 SV *const offset_sv = newSViv(offset);
2147 return tied_handle_method2("SEEK", SP, io, mg, offset_sv,
2152 if (PL_op->op_type == OP_SEEK)
2153 PUSHs(boolSV(do_seek(gv, offset, whence)));
2155 const Off_t sought = do_sysseek(gv, offset, whence);
2157 PUSHs(&PL_sv_undef);
2159 SV* const sv = sought ?
2160 #if LSEEKSIZE > IVSIZE
2165 : newSVpvn(zero_but_true, ZBTLEN);
2176 /* There seems to be no consensus on the length type of truncate()
2177 * and ftruncate(), both off_t and size_t have supporters. In
2178 * general one would think that when using large files, off_t is
2179 * at least as wide as size_t, so using an off_t should be okay. */
2180 /* XXX Configure probe for the length type of *truncate() needed XXX */
2183 #if Off_t_size > IVSIZE
2188 /* Checking for length < 0 is problematic as the type might or
2189 * might not be signed: if it is not, clever compilers will moan. */
2190 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2197 if (PL_op->op_flags & OPf_SPECIAL) {
2198 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2207 TAINT_PROPER("truncate");
2208 if (!(fp = IoIFP(io))) {
2214 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2216 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2223 SV * const sv = POPs;
2226 if (isGV_with_GP(sv)) {
2227 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2228 goto do_ftruncate_gv;
2230 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2231 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2232 goto do_ftruncate_gv;
2234 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2235 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2236 goto do_ftruncate_io;
2239 name = SvPV_nolen_const(sv);
2240 TAINT_PROPER("truncate");
2242 if (truncate(name, len) < 0)
2246 const int tmpfd = PerlLIO_open(name, O_RDWR);
2251 if (my_chsize(tmpfd, len) < 0)
2253 PerlLIO_close(tmpfd);
2262 SETERRNO(EBADF,RMS_IFI);
2270 SV * const argsv = POPs;
2271 const unsigned int func = POPu;
2272 const int optype = PL_op->op_type;
2273 GV * const gv = MUTABLE_GV(POPs);
2274 IO * const io = gv ? GvIOn(gv) : NULL;
2278 if (!io || !argsv || !IoIFP(io)) {
2279 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2280 report_evil_fh(gv, io, PL_op->op_type);
2281 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2285 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2288 s = SvPV_force(argsv, len);
2289 need = IOCPARM_LEN(func);
2291 s = Sv_Grow(argsv, need + 1);
2292 SvCUR_set(argsv, need);
2295 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2298 retval = SvIV(argsv);
2299 s = INT2PTR(char*,retval); /* ouch */
2302 TAINT_PROPER(PL_op_desc[optype]);
2304 if (optype == OP_IOCTL)
2306 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2308 DIE(aTHX_ "ioctl is not implemented");
2312 DIE(aTHX_ "fcntl is not implemented");
2314 #if defined(OS2) && defined(__EMX__)
2315 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2317 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2321 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2323 if (s[SvCUR(argsv)] != 17)
2324 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2326 s[SvCUR(argsv)] = 0; /* put our null back */
2327 SvSETMAGIC(argsv); /* Assume it has changed */
2336 PUSHp(zero_but_true, ZBTLEN);
2349 const int argtype = POPi;
2350 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2352 if (gv && (io = GvIO(gv)))
2358 /* XXX Looks to me like io is always NULL at this point */
2360 (void)PerlIO_flush(fp);
2361 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2364 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2365 report_evil_fh(gv, io, PL_op->op_type);
2367 SETERRNO(EBADF,RMS_IFI);
2372 DIE(aTHX_ PL_no_func, "flock()");
2383 const int protocol = POPi;
2384 const int type = POPi;
2385 const int domain = POPi;
2386 GV * const gv = MUTABLE_GV(POPs);
2387 register IO * const io = gv ? GvIOn(gv) : NULL;
2391 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2392 report_evil_fh(gv, io, PL_op->op_type);
2393 if (io && IoIFP(io))
2394 do_close(gv, FALSE);
2395 SETERRNO(EBADF,LIB_INVARG);
2400 do_close(gv, FALSE);
2402 TAINT_PROPER("socket");
2403 fd = PerlSock_socket(domain, type, protocol);
2406 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2407 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2408 IoTYPE(io) = IoTYPE_SOCKET;
2409 if (!IoIFP(io) || !IoOFP(io)) {
2410 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2411 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2412 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2415 #if defined(HAS_FCNTL) && defined(F_SETFD)
2416 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2420 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2425 DIE(aTHX_ PL_no_sock_func, "socket");
2432 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2434 const int protocol = POPi;
2435 const int type = POPi;
2436 const int domain = POPi;
2437 GV * const gv2 = MUTABLE_GV(POPs);
2438 GV * const gv1 = MUTABLE_GV(POPs);
2439 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2440 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2443 if (!gv1 || !gv2 || !io1 || !io2) {
2444 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2446 report_evil_fh(gv1, io1, PL_op->op_type);
2448 report_evil_fh(gv1, io2, PL_op->op_type);
2450 if (io1 && IoIFP(io1))
2451 do_close(gv1, FALSE);
2452 if (io2 && IoIFP(io2))
2453 do_close(gv2, FALSE);
2458 do_close(gv1, FALSE);
2460 do_close(gv2, FALSE);
2462 TAINT_PROPER("socketpair");
2463 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2465 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2466 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2467 IoTYPE(io1) = IoTYPE_SOCKET;
2468 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2469 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2470 IoTYPE(io2) = IoTYPE_SOCKET;
2471 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2472 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2473 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2474 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2475 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2476 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2477 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2480 #if defined(HAS_FCNTL) && defined(F_SETFD)
2481 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2482 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2487 DIE(aTHX_ PL_no_sock_func, "socketpair");
2496 SV * const addrsv = POPs;
2497 /* OK, so on what platform does bind modify addr? */
2499 GV * const gv = MUTABLE_GV(POPs);
2500 register IO * const io = GvIOn(gv);
2503 if (!io || !IoIFP(io))
2506 addr = SvPV_const(addrsv, len);
2507 TAINT_PROPER("bind");
2508 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2514 if (ckWARN(WARN_CLOSED))
2515 report_evil_fh(gv, io, PL_op->op_type);
2516 SETERRNO(EBADF,SS_IVCHAN);
2519 DIE(aTHX_ PL_no_sock_func, "bind");
2528 SV * const addrsv = POPs;
2529 GV * const gv = MUTABLE_GV(POPs);
2530 register IO * const io = GvIOn(gv);
2534 if (!io || !IoIFP(io))
2537 addr = SvPV_const(addrsv, len);
2538 TAINT_PROPER("connect");
2539 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2545 if (ckWARN(WARN_CLOSED))
2546 report_evil_fh(gv, io, PL_op->op_type);
2547 SETERRNO(EBADF,SS_IVCHAN);
2550 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");
2588 char namebuf[MAXPATHLEN];
2589 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2590 Sock_size_t len = sizeof (struct sockaddr_in);
2592 Sock_size_t len = sizeof namebuf;
2594 GV * const ggv = MUTABLE_GV(POPs);
2595 GV * const ngv = MUTABLE_GV(POPs);
2604 if (!gstio || !IoIFP(gstio))
2608 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2611 /* Some platforms indicate zero length when an AF_UNIX client is
2612 * not bound. Simulate a non-zero-length sockaddr structure in
2614 namebuf[0] = 0; /* sun_len */
2615 namebuf[1] = AF_UNIX; /* sun_family */
2623 do_close(ngv, FALSE);
2624 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2625 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2626 IoTYPE(nstio) = IoTYPE_SOCKET;
2627 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2628 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2629 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2630 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2633 #if defined(HAS_FCNTL) && defined(F_SETFD)
2634 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2638 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2639 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2641 #ifdef __SCO_VERSION__
2642 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2645 PUSHp(namebuf, len);
2649 if (ckWARN(WARN_CLOSED))
2650 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2651 SETERRNO(EBADF,SS_IVCHAN);
2657 DIE(aTHX_ PL_no_sock_func, "accept");
2666 const int how = POPi;
2667 GV * const gv = MUTABLE_GV(POPs);
2668 register IO * const io = GvIOn(gv);
2670 if (!io || !IoIFP(io))
2673 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2677 if (ckWARN(WARN_CLOSED))
2678 report_evil_fh(gv, io, PL_op->op_type);
2679 SETERRNO(EBADF,SS_IVCHAN);
2682 DIE(aTHX_ PL_no_sock_func, "shutdown");
2691 const int optype = PL_op->op_type;
2692 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2693 const unsigned int optname = (unsigned int) POPi;
2694 const unsigned int lvl = (unsigned int) POPi;
2695 GV * const gv = MUTABLE_GV(POPs);
2696 register IO * const io = GvIOn(gv);
2700 if (!io || !IoIFP(io))
2703 fd = PerlIO_fileno(IoIFP(io));
2707 (void)SvPOK_only(sv);
2711 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2718 #if defined(__SYMBIAN32__)
2719 # define SETSOCKOPT_OPTION_VALUE_T void *
2721 # define SETSOCKOPT_OPTION_VALUE_T const char *
2723 /* XXX TODO: We need to have a proper type (a Configure probe,
2724 * etc.) for what the C headers think of the third argument of
2725 * setsockopt(), the option_value read-only buffer: is it
2726 * a "char *", or a "void *", const or not. Some compilers
2727 * don't take kindly to e.g. assuming that "char *" implicitly
2728 * promotes to a "void *", or to explicitly promoting/demoting
2729 * consts to non/vice versa. The "const void *" is the SUS
2730 * definition, but that does not fly everywhere for the above
2732 SETSOCKOPT_OPTION_VALUE_T buf;
2736 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2740 aint = (int)SvIV(sv);
2741 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2744 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2753 if (ckWARN(WARN_CLOSED))
2754 report_evil_fh(gv, io, optype);
2755 SETERRNO(EBADF,SS_IVCHAN);
2760 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2769 const int optype = PL_op->op_type;
2770 GV * const gv = MUTABLE_GV(POPs);
2771 register IO * const io = GvIOn(gv);
2776 if (!io || !IoIFP(io))
2779 sv = sv_2mortal(newSV(257));
2780 (void)SvPOK_only(sv);
2784 fd = PerlIO_fileno(IoIFP(io));
2786 case OP_GETSOCKNAME:
2787 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2790 case OP_GETPEERNAME:
2791 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2793 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2795 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";
2796 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2797 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2798 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2799 sizeof(u_short) + sizeof(struct in_addr))) {
2806 #ifdef BOGUS_GETNAME_RETURN
2807 /* Interactive Unix, getpeername() and getsockname()
2808 does not return valid namelen */
2809 if (len == BOGUS_GETNAME_RETURN)
2810 len = sizeof(struct sockaddr);
2818 if (ckWARN(WARN_CLOSED))
2819 report_evil_fh(gv, io, optype);
2820 SETERRNO(EBADF,SS_IVCHAN);
2825 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2841 if (PL_op->op_flags & OPf_REF) {
2843 if (PL_op->op_type == OP_LSTAT) {
2844 if (gv != PL_defgv) {
2845 do_fstat_warning_check:
2846 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2847 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2848 } else if (PL_laststype != OP_LSTAT)
2849 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2853 if (gv != PL_defgv) {
2854 PL_laststype = OP_STAT;
2856 sv_setpvs(PL_statname, "");
2863 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2864 } else if (IoDIRP(io)) {
2866 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2868 PL_laststatval = -1;
2874 if (PL_laststatval < 0) {
2875 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2876 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2881 SV* const sv = POPs;
2882 if (isGV_with_GP(sv)) {
2883 gv = MUTABLE_GV(sv);
2885 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2886 gv = MUTABLE_GV(SvRV(sv));
2887 if (PL_op->op_type == OP_LSTAT)
2888 goto do_fstat_warning_check;
2890 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2891 io = MUTABLE_IO(SvRV(sv));
2892 if (PL_op->op_type == OP_LSTAT)
2893 goto do_fstat_warning_check;
2894 goto do_fstat_have_io;
2897 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2899 PL_laststype = PL_op->op_type;
2900 if (PL_op->op_type == OP_LSTAT)
2901 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2903 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2904 if (PL_laststatval < 0) {
2905 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2906 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2912 if (gimme != G_ARRAY) {
2913 if (gimme != G_VOID)
2914 XPUSHs(boolSV(max));
2920 mPUSHi(PL_statcache.st_dev);
2921 mPUSHi(PL_statcache.st_ino);
2922 mPUSHu(PL_statcache.st_mode);
2923 mPUSHu(PL_statcache.st_nlink);
2924 #if Uid_t_size > IVSIZE
2925 mPUSHn(PL_statcache.st_uid);
2927 # if Uid_t_sign <= 0
2928 mPUSHi(PL_statcache.st_uid);
2930 mPUSHu(PL_statcache.st_uid);
2933 #if Gid_t_size > IVSIZE
2934 mPUSHn(PL_statcache.st_gid);
2936 # if Gid_t_sign <= 0
2937 mPUSHi(PL_statcache.st_gid);
2939 mPUSHu(PL_statcache.st_gid);
2942 #ifdef USE_STAT_RDEV
2943 mPUSHi(PL_statcache.st_rdev);
2945 PUSHs(newSVpvs_flags("", SVs_TEMP));
2947 #if Off_t_size > IVSIZE
2948 mPUSHn(PL_statcache.st_size);
2950 mPUSHi(PL_statcache.st_size);
2953 mPUSHn(PL_statcache.st_atime);
2954 mPUSHn(PL_statcache.st_mtime);
2955 mPUSHn(PL_statcache.st_ctime);
2957 mPUSHi(PL_statcache.st_atime);
2958 mPUSHi(PL_statcache.st_mtime);
2959 mPUSHi(PL_statcache.st_ctime);
2961 #ifdef USE_STAT_BLOCKS
2962 mPUSHu(PL_statcache.st_blksize);
2963 mPUSHu(PL_statcache.st_blocks);
2965 PUSHs(newSVpvs_flags("", SVs_TEMP));
2966 PUSHs(newSVpvs_flags("", SVs_TEMP));
2972 #define tryAMAGICftest_MG(chr) STMT_START { \
2973 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2974 && S_try_amagic_ftest(aTHX_ chr)) \
2979 S_try_amagic_ftest(pTHX_ char chr) {
2982 SV* const arg = TOPs;
2987 if ((PL_op->op_flags & OPf_KIDS)
2990 const char tmpchr = chr;
2992 SV * const tmpsv = amagic_call(arg,
2993 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2994 ftest_amg, AMGf_unary);
3001 next = PL_op->op_next;
3002 if (next->op_type >= OP_FTRREAD &&
3003 next->op_type <= OP_FTBINARY &&
3004 next->op_private & OPpFT_STACKED
3007 /* leave the object alone */
3019 /* This macro is used by the stacked filetest operators :
3020 * if the previous filetest failed, short-circuit and pass its value.
3021 * Else, discard it from the stack and continue. --rgs
3023 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
3024 if (!SvTRUE(TOPs)) { RETURN; } \
3025 else { (void)POPs; PUTBACK; } \
3032 /* Not const, because things tweak this below. Not bool, because there's
3033 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
3034 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3035 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3036 /* Giving some sort of initial value silences compilers. */
3038 int access_mode = R_OK;
3040 int access_mode = 0;
3043 /* access_mode is never used, but leaving use_access in makes the
3044 conditional compiling below much clearer. */
3047 int stat_mode = S_IRUSR;
3049 bool effective = FALSE;
3053 switch (PL_op->op_type) {
3054 case OP_FTRREAD: opchar = 'R'; break;
3055 case OP_FTRWRITE: opchar = 'W'; break;
3056 case OP_FTREXEC: opchar = 'X'; break;
3057 case OP_FTEREAD: opchar = 'r'; break;
3058 case OP_FTEWRITE: opchar = 'w'; break;
3059 case OP_FTEEXEC: opchar = 'x'; break;
3061 tryAMAGICftest_MG(opchar);
3063 STACKED_FTEST_CHECK;
3065 switch (PL_op->op_type) {
3067 #if !(defined(HAS_ACCESS) && defined(R_OK))
3073 #if defined(HAS_ACCESS) && defined(W_OK)
3078 stat_mode = S_IWUSR;
3082 #if defined(HAS_ACCESS) && defined(X_OK)
3087 stat_mode = S_IXUSR;
3091 #ifdef PERL_EFF_ACCESS
3094 stat_mode = S_IWUSR;
3098 #ifndef PERL_EFF_ACCESS
3105 #ifdef PERL_EFF_ACCESS
3110 stat_mode = S_IXUSR;
3116 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3117 const char *name = POPpx;
3119 # ifdef PERL_EFF_ACCESS
3120 result = PERL_EFF_ACCESS(name, access_mode);
3122 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3128 result = access(name, access_mode);
3130 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3145 if (cando(stat_mode, effective, &PL_statcache))
3154 const int op_type = PL_op->op_type;
3159 case OP_FTIS: opchar = 'e'; break;
3160 case OP_FTSIZE: opchar = 's'; break;
3161 case OP_FTMTIME: opchar = 'M'; break;
3162 case OP_FTCTIME: opchar = 'C'; break;
3163 case OP_FTATIME: opchar = 'A'; break;
3165 tryAMAGICftest_MG(opchar);
3167 STACKED_FTEST_CHECK;
3173 if (op_type == OP_FTIS)
3176 /* You can't dTARGET inside OP_FTIS, because you'll get
3177 "panic: pad_sv po" - the op is not flagged to have a target. */
3181 #if Off_t_size > IVSIZE
3182 PUSHn(PL_statcache.st_size);
3184 PUSHi(PL_statcache.st_size);
3188 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3191 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3194 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3208 switch (PL_op->op_type) {
3209 case OP_FTROWNED: opchar = 'O'; break;
3210 case OP_FTEOWNED: opchar = 'o'; break;
3211 case OP_FTZERO: opchar = 'z'; break;
3212 case OP_FTSOCK: opchar = 'S'; break;
3213 case OP_FTCHR: opchar = 'c'; break;
3214 case OP_FTBLK: opchar = 'b'; break;
3215 case OP_FTFILE: opchar = 'f'; break;
3216 case OP_FTDIR: opchar = 'd'; break;
3217 case OP_FTPIPE: opchar = 'p'; break;
3218 case OP_FTSUID: opchar = 'u'; break;
3219 case OP_FTSGID: opchar = 'g'; break;
3220 case OP_FTSVTX: opchar = 'k'; break;
3222 tryAMAGICftest_MG(opchar);
3224 /* I believe that all these three are likely to be defined on most every
3225 system these days. */
3227 if(PL_op->op_type == OP_FTSUID)
3231 if(PL_op->op_type == OP_FTSGID)
3235 if(PL_op->op_type == OP_FTSVTX)
3239 STACKED_FTEST_CHECK;
3245 switch (PL_op->op_type) {
3247 if (PL_statcache.st_uid == PL_uid)
3251 if (PL_statcache.st_uid == PL_euid)
3255 if (PL_statcache.st_size == 0)
3259 if (S_ISSOCK(PL_statcache.st_mode))
3263 if (S_ISCHR(PL_statcache.st_mode))
3267 if (S_ISBLK(PL_statcache.st_mode))
3271 if (S_ISREG(PL_statcache.st_mode))
3275 if (S_ISDIR(PL_statcache.st_mode))
3279 if (S_ISFIFO(PL_statcache.st_mode))
3284 if (PL_statcache.st_mode & S_ISUID)
3290 if (PL_statcache.st_mode & S_ISGID)
3296 if (PL_statcache.st_mode & S_ISVTX)
3310 tryAMAGICftest_MG('l');
3311 result = my_lstat();
3316 if (S_ISLNK(PL_statcache.st_mode))
3329 tryAMAGICftest_MG('t');
3331 STACKED_FTEST_CHECK;
3333 if (PL_op->op_flags & OPf_REF)
3335 else if (isGV(TOPs))
3336 gv = MUTABLE_GV(POPs);
3337 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3338 gv = MUTABLE_GV(SvRV(POPs));
3340 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3342 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3343 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3344 else if (tmpsv && SvOK(tmpsv)) {
3345 const char *tmps = SvPV_nolen_const(tmpsv);
3353 if (PerlLIO_isatty(fd))
3358 #if defined(atarist) /* this will work with atariST. Configure will
3359 make guesses for other systems. */
3360 # define FILE_base(f) ((f)->_base)
3361 # define FILE_ptr(f) ((f)->_ptr)
3362 # define FILE_cnt(f) ((f)->_cnt)
3363 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3374 register STDCHAR *s;
3380 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3382 STACKED_FTEST_CHECK;
3384 if (PL_op->op_flags & OPf_REF)
3386 else if (isGV(TOPs))
3387 gv = MUTABLE_GV(POPs);
3388 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3389 gv = MUTABLE_GV(SvRV(POPs));
3395 if (gv == PL_defgv) {
3397 io = GvIO(PL_statgv);
3400 goto really_filename;
3405 PL_laststatval = -1;
3406 sv_setpvs(PL_statname, "");
3407 io = GvIO(PL_statgv);
3409 if (io && IoIFP(io)) {
3410 if (! PerlIO_has_base(IoIFP(io)))
3411 DIE(aTHX_ "-T and -B not implemented on filehandles");
3412 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3413 if (PL_laststatval < 0)
3415 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3416 if (PL_op->op_type == OP_FTTEXT)
3421 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3422 i = PerlIO_getc(IoIFP(io));
3424 (void)PerlIO_ungetc(IoIFP(io),i);
3426 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3428 len = PerlIO_get_bufsiz(IoIFP(io));
3429 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3430 /* sfio can have large buffers - limit to 512 */
3435 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3437 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3439 SETERRNO(EBADF,RMS_IFI);
3447 PL_laststype = OP_STAT;
3448 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3449 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3450 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3452 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3455 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3456 if (PL_laststatval < 0) {
3457 (void)PerlIO_close(fp);
3460 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3461 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3462 (void)PerlIO_close(fp);
3464 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3465 RETPUSHNO; /* special case NFS directories */
3466 RETPUSHYES; /* null file is anything */
3471 /* now scan s to look for textiness */
3472 /* XXX ASCII dependent code */
3474 #if defined(DOSISH) || defined(USEMYBINMODE)
3475 /* ignore trailing ^Z on short files */
3476 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3480 for (i = 0; i < len; i++, s++) {
3481 if (!*s) { /* null never allowed in text */
3486 else if (!(isPRINT(*s) || isSPACE(*s)))
3489 else if (*s & 128) {
3491 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3494 /* utf8 characters don't count as odd */
3495 if (UTF8_IS_START(*s)) {
3496 int ulen = UTF8SKIP(s);
3497 if (ulen < len - i) {
3499 for (j = 1; j < ulen; j++) {
3500 if (!UTF8_IS_CONTINUATION(s[j]))
3503 --ulen; /* loop does extra increment */
3513 *s != '\n' && *s != '\r' && *s != '\b' &&
3514 *s != '\t' && *s != '\f' && *s != 27)
3519 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3530 const char *tmps = NULL;
3534 SV * const sv = POPs;
3535 if (PL_op->op_flags & OPf_SPECIAL) {
3536 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3538 else if (isGV_with_GP(sv)) {
3539 gv = MUTABLE_GV(sv);
3541 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3542 gv = MUTABLE_GV(SvRV(sv));
3545 tmps = SvPV_nolen_const(sv);
3549 if( !gv && (!tmps || !*tmps) ) {
3550 HV * const table = GvHVn(PL_envgv);
3553 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3554 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3556 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3561 deprecate("chdir('') or chdir(undef) as chdir()");
3562 tmps = SvPV_nolen_const(*svp);
3566 TAINT_PROPER("chdir");
3571 TAINT_PROPER("chdir");
3574 IO* const io = GvIO(gv);
3577 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3578 } else if (IoIFP(io)) {
3579 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3582 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3583 report_evil_fh(gv, io, PL_op->op_type);
3584 SETERRNO(EBADF, RMS_IFI);
3589 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3590 report_evil_fh(gv, io, PL_op->op_type);
3591 SETERRNO(EBADF,RMS_IFI);
3595 DIE(aTHX_ PL_no_func, "fchdir");
3599 PUSHi( PerlDir_chdir(tmps) >= 0 );
3601 /* Clear the DEFAULT element of ENV so we'll get the new value
3603 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3610 dVAR; dSP; dMARK; dTARGET;
3611 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3622 char * const tmps = POPpx;
3623 TAINT_PROPER("chroot");
3624 PUSHi( chroot(tmps) >= 0 );
3627 DIE(aTHX_ PL_no_func, "chroot");
3636 const char * const tmps2 = POPpconstx;
3637 const char * const tmps = SvPV_nolen_const(TOPs);
3638 TAINT_PROPER("rename");
3640 anum = PerlLIO_rename(tmps, tmps2);
3642 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3643 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3646 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3647 (void)UNLINK(tmps2);
3648 if (!(anum = link(tmps, tmps2)))
3649 anum = UNLINK(tmps);
3657 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3661 const int op_type = PL_op->op_type;
3665 if (op_type == OP_LINK)
3666 DIE(aTHX_ PL_no_func, "link");
3668 # ifndef HAS_SYMLINK
3669 if (op_type == OP_SYMLINK)
3670 DIE(aTHX_ PL_no_func, "symlink");
3674 const char * const tmps2 = POPpconstx;
3675 const char * const tmps = SvPV_nolen_const(TOPs);
3676 TAINT_PROPER(PL_op_desc[op_type]);
3678 # if defined(HAS_LINK)
3679 # if defined(HAS_SYMLINK)
3680 /* Both present - need to choose which. */
3681 (op_type == OP_LINK) ?
3682 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3684 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3685 PerlLIO_link(tmps, tmps2);
3688 # if defined(HAS_SYMLINK)
3689 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3690 symlink(tmps, tmps2);
3695 SETi( result >= 0 );
3702 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3714 char buf[MAXPATHLEN];
3717 #ifndef INCOMPLETE_TAINTS
3721 len = readlink(tmps, buf, sizeof(buf) - 1);
3728 RETSETUNDEF; /* just pretend it's a normal file */
3732 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3734 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3736 char * const save_filename = filename;
3741 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3743 PERL_ARGS_ASSERT_DOONELINER;
3745 Newx(cmdline, size, char);
3746 my_strlcpy(cmdline, cmd, size);
3747 my_strlcat(cmdline, " ", size);
3748 for (s = cmdline + strlen(cmdline); *filename; ) {
3752 if (s - cmdline < size)
3753 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3754 myfp = PerlProc_popen(cmdline, "r");
3758 SV * const tmpsv = sv_newmortal();
3759 /* Need to save/restore 'PL_rs' ?? */
3760 s = sv_gets(tmpsv, myfp, 0);
3761 (void)PerlProc_pclose(myfp);
3765 #ifdef HAS_SYS_ERRLIST
3770 /* you don't see this */
3771 const char * const errmsg =
3772 #ifdef HAS_SYS_ERRLIST
3780 if (instr(s, errmsg)) {
3787 #define EACCES EPERM
3789 if (instr(s, "cannot make"))
3790 SETERRNO(EEXIST,RMS_FEX);
3791 else if (instr(s, "existing file"))
3792 SETERRNO(EEXIST,RMS_FEX);
3793 else if (instr(s, "ile exists"))
3794 SETERRNO(EEXIST,RMS_FEX);
3795 else if (instr(s, "non-exist"))
3796 SETERRNO(ENOENT,RMS_FNF);
3797 else if (instr(s, "does not exist"))
3798 SETERRNO(ENOENT,RMS_FNF);
3799 else if (instr(s, "not empty"))
3800 SETERRNO(EBUSY,SS_DEVOFFLINE);
3801 else if (instr(s, "cannot access"))
3802 SETERRNO(EACCES,RMS_PRV);
3804 SETERRNO(EPERM,RMS_PRV);
3807 else { /* some mkdirs return no failure indication */
3808 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3809 if (PL_op->op_type == OP_RMDIR)
3814 SETERRNO(EACCES,RMS_PRV); /* a guess */
3823 /* This macro removes trailing slashes from a directory name.
3824 * Different operating and file systems take differently to
3825 * trailing slashes. According to POSIX 1003.1 1996 Edition
3826 * any number of trailing slashes should be allowed.
3827 * Thusly we snip them away so that even non-conforming
3828 * systems are happy.
3829 * We should probably do this "filtering" for all
3830 * the functions that expect (potentially) directory names:
3831 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3832 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3834 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3835 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3838 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3839 (tmps) = savepvn((tmps), (len)); \
3849 const int mode = (MAXARG > 1) ? POPi : 0777;
3851 TRIMSLASHES(tmps,len,copy);
3853 TAINT_PROPER("mkdir");
3855 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3859 SETi( dooneliner("mkdir", tmps) );
3860 oldumask = PerlLIO_umask(0);
3861 PerlLIO_umask(oldumask);
3862 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3877 TRIMSLASHES(tmps,len,copy);
3878 TAINT_PROPER("rmdir");
3880 SETi( PerlDir_rmdir(tmps) >= 0 );
3882 SETi( dooneliner("rmdir", tmps) );
3889 /* Directory calls. */
3893 #if defined(Direntry_t) && defined(HAS_READDIR)
3895 const char * const dirname = POPpconstx;
3896 GV * const gv = MUTABLE_GV(POPs);
3897 register IO * const io = GvIOn(gv);
3902 if ((IoIFP(io) || IoOFP(io)))
3903 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3904 "Opening filehandle %s also as a directory",
3907 PerlDir_close(IoDIRP(io));
3908 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3914 SETERRNO(EBADF,RMS_DIR);
3917 DIE(aTHX_ PL_no_dir_func, "opendir");
3924 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3925 DIE(aTHX_ PL_no_dir_func, "readdir");
3928 #if !defined(I_DIRENT) && !defined(VMS)
3929 Direntry_t *readdir (DIR *);
3935 const I32 gimme = GIMME;
3936 GV * const gv = MUTABLE_GV(POPs);
3937 register const Direntry_t *dp;
3938 register IO * const io = GvIOn(gv);
3940 if (!io || !IoDIRP(io)) {
3941 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3942 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3947 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3951 sv = newSVpvn(dp->d_name, dp->d_namlen);
3953 sv = newSVpv(dp->d_name, 0);
3955 #ifndef INCOMPLETE_TAINTS
3956 if (!(IoFLAGS(io) & IOf_UNTAINT))
3960 } while (gimme == G_ARRAY);
3962 if (!dp && gimme != G_ARRAY)
3969 SETERRNO(EBADF,RMS_ISI);
3970 if (GIMME == G_ARRAY)
3979 #if defined(HAS_TELLDIR) || defined(telldir)
3981 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3982 /* XXX netbsd still seemed to.
3983 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3984 --JHI 1999-Feb-02 */
3985 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3986 long telldir (DIR *);
3988 GV * const gv = MUTABLE_GV(POPs);
3989 register IO * const io = GvIOn(gv);
3991 if (!io || !IoDIRP(io)) {
3992 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3993 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3997 PUSHi( PerlDir_tell(IoDIRP(io)) );
4001 SETERRNO(EBADF,RMS_ISI);
4004 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");
4037 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4039 GV * const gv = MUTABLE_GV(POPs);
4040 register IO * const io = GvIOn(gv);
4042 if (!io || !IoDIRP(io)) {
4043 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4044 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
4047 (void)PerlDir_rewind(IoDIRP(io));
4051 SETERRNO(EBADF,RMS_ISI);
4054 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4061 #if defined(Direntry_t) && defined(HAS_READDIR)
4063 GV * const gv = MUTABLE_GV(POPs);
4064 register IO * const io = GvIOn(gv);
4066 if (!io || !IoDIRP(io)) {
4067 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4068 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4071 #ifdef VOID_CLOSEDIR
4072 PerlDir_close(IoDIRP(io));
4074 if (PerlDir_close(IoDIRP(io)) < 0) {
4075 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4084 SETERRNO(EBADF,RMS_IFI);
4087 DIE(aTHX_ PL_no_dir_func, "closedir");
4092 /* Process control. */
4101 PERL_FLUSHALL_FOR_CHILD;
4102 childpid = PerlProc_fork();
4106 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4108 SvREADONLY_off(GvSV(tmpgv));
4109 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4110 SvREADONLY_on(GvSV(tmpgv));
4112 #ifdef THREADS_HAVE_PIDS
4113 PL_ppid = (IV)getppid();
4115 #ifdef PERL_USES_PL_PIDSTATUS
4116 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4122 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4127 PERL_FLUSHALL_FOR_CHILD;
4128 childpid = PerlProc_fork();
4134 DIE(aTHX_ PL_no_func, "fork");
4142 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4147 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4148 childpid = wait4pid(-1, &argflags, 0);
4150 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4155 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4156 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4157 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4159 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4164 DIE(aTHX_ PL_no_func, "wait");
4171 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4173 const int optype = POPi;
4174 const Pid_t pid = TOPi;
4178 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4179 result = wait4pid(pid, &argflags, optype);
4181 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4186 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4187 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4188 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4190 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4195 DIE(aTHX_ PL_no_func, "waitpid");
4202 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4203 #if defined(__LIBCATAMOUNT__)
4204 PL_statusvalue = -1;
4213 while (++MARK <= SP) {
4214 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4219 TAINT_PROPER("system");
4221 PERL_FLUSHALL_FOR_CHILD;
4222 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4228 if (PerlProc_pipe(pp) >= 0)
4230 while ((childpid = PerlProc_fork()) == -1) {
4231 if (errno != EAGAIN) {
4236 PerlLIO_close(pp[0]);
4237 PerlLIO_close(pp[1]);
4244 Sigsave_t ihand,qhand; /* place to save signals during system() */
4248 PerlLIO_close(pp[1]);
4250 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4251 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4254 result = wait4pid(childpid, &status, 0);
4255 } while (result == -1 && errno == EINTR);
4257 (void)rsignal_restore(SIGINT, &ihand);
4258 (void)rsignal_restore(SIGQUIT, &qhand);
4260 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4261 do_execfree(); /* free any memory child malloced on fork */
4268 while (n < sizeof(int)) {
4269 n1 = PerlLIO_read(pp[0],
4270 (void*)(((char*)&errkid)+n),
4276 PerlLIO_close(pp[0]);
4277 if (n) { /* Error */
4278 if (n != sizeof(int))
4279 DIE(aTHX_ "panic: kid popen errno read");
4280 errno = errkid; /* Propagate errno from kid */
4281 STATUS_NATIVE_CHILD_SET(-1);
4284 XPUSHi(STATUS_CURRENT);
4288 PerlLIO_close(pp[0]);
4289 #if defined(HAS_FCNTL) && defined(F_SETFD)
4290 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4293 if (PL_op->op_flags & OPf_STACKED) {
4294 SV * const really = *++MARK;
4295 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4297 else if (SP - MARK != 1)
4298 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4300 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4304 #else /* ! FORK or VMS or OS/2 */
4307 if (PL_op->op_flags & OPf_STACKED) {
4308 SV * const really = *++MARK;
4309 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4310 value = (I32)do_aspawn(really, MARK, SP);
4312 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4315 else if (SP - MARK != 1) {
4316 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4317 value = (I32)do_aspawn(NULL, MARK, SP);
4319 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4323 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4325 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4327 STATUS_NATIVE_CHILD_SET(value);
4330 XPUSHi(result ? value : STATUS_CURRENT);
4331 #endif /* !FORK or VMS or OS/2 */
4338 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4343 while (++MARK <= SP) {
4344 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4349 TAINT_PROPER("exec");
4351 PERL_FLUSHALL_FOR_CHILD;
4352 if (PL_op->op_flags & OPf_STACKED) {
4353 SV * const really = *++MARK;
4354 value = (I32)do_aexec(really, MARK, SP);
4356 else if (SP - MARK != 1)
4358 value = (I32)vms_do_aexec(NULL, MARK, SP);
4362 (void ) do_aspawn(NULL, MARK, SP);
4366 value = (I32)do_aexec(NULL, MARK, SP);
4371 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4374 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4377 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4391 # ifdef THREADS_HAVE_PIDS
4392 if (PL_ppid != 1 && getppid() == 1)
4393 /* maybe the parent process has died. Refresh ppid cache */
4397 XPUSHi( getppid() );
4401 DIE(aTHX_ PL_no_func, "getppid");
4411 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4414 pgrp = (I32)BSD_GETPGRP(pid);
4416 if (pid != 0 && pid != PerlProc_getpid())
4417 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4423 DIE(aTHX_ PL_no_func, "getpgrp()");
4444 TAINT_PROPER("setpgrp");
4446 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4448 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4449 || (pid != 0 && pid != PerlProc_getpid()))
4451 DIE(aTHX_ "setpgrp can't take arguments");
4453 SETi( setpgrp() >= 0 );
4454 #endif /* USE_BSDPGRP */
4457 DIE(aTHX_ PL_no_func, "setpgrp()");
4464 #ifdef HAS_GETPRIORITY
4466 const int who = POPi;
4467 const int which = TOPi;
4468 SETi( getpriority(which, who) );
4471 DIE(aTHX_ PL_no_func, "getpriority()");
4478 #ifdef HAS_SETPRIORITY
4480 const int niceval = POPi;
4481 const int who = POPi;
4482 const int which = TOPi;
4483 TAINT_PROPER("setpriority");
4484 SETi( setpriority(which, who, niceval) >= 0 );
4487 DIE(aTHX_ PL_no_func, "setpriority()");
4498 XPUSHn( time(NULL) );
4500 XPUSHi( time(NULL) );
4512 (void)PerlProc_times(&PL_timesbuf);
4514 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4515 /* struct tms, though same data */
4519 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4520 if (GIMME == G_ARRAY) {
4521 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4522 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4523 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4531 if (GIMME == G_ARRAY) {
4538 DIE(aTHX_ "times not implemented");
4541 #endif /* HAS_TIMES */
4544 /* The 32 bit int year limits the times we can represent to these
4545 boundaries with a few days wiggle room to account for time zone
4548 /* Sat Jan 3 00:00:00 -2147481748 */
4549 #define TIME_LOWER_BOUND -67768100567755200.0
4550 /* Sun Dec 29 12:00:00 2147483647 */
4551 #define TIME_UPPER_BOUND 67767976233316800.0
4560 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4561 static const char * const dayname[] =
4562 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4563 static const char * const monname[] =
4564 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4565 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4570 when = (Time64_T)now;
4573 NV input = Perl_floor(POPn);
4574 when = (Time64_T)input;
4575 if (when != input) {
4576 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4577 "%s(%.0" NVff ") too large", opname, input);
4581 if ( TIME_LOWER_BOUND > when ) {
4582 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4583 "%s(%.0" NVff ") too small", opname, when);
4586 else if( when > TIME_UPPER_BOUND ) {
4587 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4588 "%s(%.0" NVff ") too large", opname, when);
4592 if (PL_op->op_type == OP_LOCALTIME)
4593 err = S_localtime64_r(&when, &tmbuf);
4595 err = S_gmtime64_r(&when, &tmbuf);
4599 /* XXX %lld broken for quads */
4600 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4601 "%s(%.0" NVff ") failed", opname, when);
4604 if (GIMME != G_ARRAY) { /* scalar context */
4606 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4607 double year = (double)tmbuf.tm_year + 1900;
4614 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4615 dayname[tmbuf.tm_wday],
4616 monname[tmbuf.tm_mon],
4624 else { /* list context */
4630 mPUSHi(tmbuf.tm_sec);
4631 mPUSHi(tmbuf.tm_min);
4632 mPUSHi(tmbuf.tm_hour);
4633 mPUSHi(tmbuf.tm_mday);
4634 mPUSHi(tmbuf.tm_mon);
4635 mPUSHn(tmbuf.tm_year);
4636 mPUSHi(tmbuf.tm_wday);
4637 mPUSHi(tmbuf.tm_yday);
4638 mPUSHi(tmbuf.tm_isdst);
4649 anum = alarm((unsigned int)anum);
4655 DIE(aTHX_ PL_no_func, "alarm");
4667 (void)time(&lasttime);
4672 PerlProc_sleep((unsigned int)duration);
4675 XPUSHi(when - lasttime);
4679 /* Shared memory. */
4680 /* Merged with some message passing. */
4684 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4685 dVAR; dSP; dMARK; dTARGET;
4686 const int op_type = PL_op->op_type;
4691 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4694 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4697 value = (I32)(do_semop(MARK, SP) >= 0);
4700 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4716 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4717 dVAR; dSP; dMARK; dTARGET;
4718 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4725 DIE(aTHX_ "System V IPC is not implemented on this machine");
4732 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4733 dVAR; dSP; dMARK; dTARGET;
4734 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4742 PUSHp(zero_but_true, ZBTLEN);
4750 /* I can't const this further without getting warnings about the types of
4751 various arrays passed in from structures. */
4753 S_space_join_names_mortal(pTHX_ char *const *array)
4757 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4759 if (array && *array) {
4760 target = newSVpvs_flags("", SVs_TEMP);
4762 sv_catpv(target, *array);
4765 sv_catpvs(target, " ");
4768 target = sv_mortalcopy(&PL_sv_no);
4773 /* Get system info. */
4777 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4779 I32 which = PL_op->op_type;
4780 register char **elem;
4782 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4783 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4784 struct hostent *gethostbyname(Netdb_name_t);
4785 struct hostent *gethostent(void);
4787 struct hostent *hent = NULL;
4791 if (which == OP_GHBYNAME) {
4792 #ifdef HAS_GETHOSTBYNAME
4793 const char* const name = POPpbytex;
4794 hent = PerlSock_gethostbyname(name);
4796 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4799 else if (which == OP_GHBYADDR) {
4800 #ifdef HAS_GETHOSTBYADDR
4801 const int addrtype = POPi;
4802 SV * const addrsv = POPs;
4804 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4806 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4808 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4812 #ifdef HAS_GETHOSTENT
4813 hent = PerlSock_gethostent();
4815 DIE(aTHX_ PL_no_sock_func, "gethostent");
4818 #ifdef HOST_NOT_FOUND
4820 #ifdef USE_REENTRANT_API
4821 # ifdef USE_GETHOSTENT_ERRNO
4822 h_errno = PL_reentrant_buffer->_gethostent_errno;
4825 STATUS_UNIX_SET(h_errno);
4829 if (GIMME != G_ARRAY) {
4830 PUSHs(sv = sv_newmortal());
4832 if (which == OP_GHBYNAME) {
4834 sv_setpvn(sv, hent->h_addr, hent->h_length);
4837 sv_setpv(sv, (char*)hent->h_name);
4843 mPUSHs(newSVpv((char*)hent->h_name, 0));
4844 PUSHs(space_join_names_mortal(hent->h_aliases));
4845 mPUSHi(hent->h_addrtype);
4846 len = hent->h_length;
4849 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4850 mXPUSHp(*elem, len);
4854 mPUSHp(hent->h_addr, len);
4856 PUSHs(sv_mortalcopy(&PL_sv_no));
4861 DIE(aTHX_ PL_no_sock_func, "gethostent");
4868 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4870 I32 which = PL_op->op_type;
4872 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4873 struct netent *getnetbyaddr(Netdb_net_t, int);
4874 struct netent *getnetbyname(Netdb_name_t);
4875 struct netent *getnetent(void);
4877 struct netent *nent;
4879 if (which == OP_GNBYNAME){
4880 #ifdef HAS_GETNETBYNAME
4881 const char * const name = POPpbytex;
4882 nent = PerlSock_getnetbyname(name);
4884 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4887 else if (which == OP_GNBYADDR) {
4888 #ifdef HAS_GETNETBYADDR
4889 const int addrtype = POPi;
4890 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4891 nent = PerlSock_getnetbyaddr(addr, addrtype);
4893 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4897 #ifdef HAS_GETNETENT
4898 nent = PerlSock_getnetent();
4900 DIE(aTHX_ PL_no_sock_func, "getnetent");
4903 #ifdef HOST_NOT_FOUND
4905 #ifdef USE_REENTRANT_API
4906 # ifdef USE_GETNETENT_ERRNO
4907 h_errno = PL_reentrant_buffer->_getnetent_errno;
4910 STATUS_UNIX_SET(h_errno);
4915 if (GIMME != G_ARRAY) {
4916 PUSHs(sv = sv_newmortal());
4918 if (which == OP_GNBYNAME)
4919 sv_setiv(sv, (IV)nent->n_net);
4921 sv_setpv(sv, nent->n_name);
4927 mPUSHs(newSVpv(nent->n_name, 0));
4928 PUSHs(space_join_names_mortal(nent->n_aliases));
4929 mPUSHi(nent->n_addrtype);
4930 mPUSHi(nent->n_net);
4935 DIE(aTHX_ PL_no_sock_func, "getnetent");
4942 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4944 I32 which = PL_op->op_type;
4946 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4947 struct protoent *getprotobyname(Netdb_name_t);
4948 struct protoent *getprotobynumber(int);
4949 struct protoent *getprotoent(void);
4951 struct protoent *pent;
4953 if (which == OP_GPBYNAME) {
4954 #ifdef HAS_GETPROTOBYNAME
4955 const char* const name = POPpbytex;
4956 pent = PerlSock_getprotobyname(name);
4958 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4961 else if (which == OP_GPBYNUMBER) {
4962 #ifdef HAS_GETPROTOBYNUMBER
4963 const int number = POPi;
4964 pent = PerlSock_getprotobynumber(number);
4966 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4970 #ifdef HAS_GETPROTOENT
4971 pent = PerlSock_getprotoent();
4973 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4977 if (GIMME != G_ARRAY) {
4978 PUSHs(sv = sv_newmortal());
4980 if (which == OP_GPBYNAME)
4981 sv_setiv(sv, (IV)pent->p_proto);
4983 sv_setpv(sv, pent->p_name);
4989 mPUSHs(newSVpv(pent->p_name, 0));
4990 PUSHs(space_join_names_mortal(pent->p_aliases));
4991 mPUSHi(pent->p_proto);
4996 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5003 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5005 I32 which = PL_op->op_type;
5007 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5008 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5009 struct servent *getservbyport(int, Netdb_name_t);
5010 struct servent *getservent(void);
5012 struct servent *sent;
5014 if (which == OP_GSBYNAME) {
5015 #ifdef HAS_GETSERVBYNAME
5016 const char * const proto = POPpbytex;
5017 const char * const name = POPpbytex;
5018 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5020 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5023 else if (which == OP_GSBYPORT) {
5024 #ifdef HAS_GETSERVBYPORT
5025 const char * const proto = POPpbytex;
5026 unsigned short port = (unsigned short)POPu;
5028 port = PerlSock_htons(port);
5030 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5032 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5036 #ifdef HAS_GETSERVENT
5037 sent = PerlSock_getservent();
5039 DIE(aTHX_ PL_no_sock_func, "getservent");
5043 if (GIMME != G_ARRAY) {
5044 PUSHs(sv = sv_newmortal());
5046 if (which == OP_GSBYNAME) {
5048 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5050 sv_setiv(sv, (IV)(sent->s_port));
5054 sv_setpv(sv, sent->s_name);
5060 mPUSHs(newSVpv(sent->s_name, 0));
5061 PUSHs(space_join_names_mortal(sent->s_aliases));
5063 mPUSHi(PerlSock_ntohs(sent->s_port));
5065 mPUSHi(sent->s_port);
5067 mPUSHs(newSVpv(sent->s_proto, 0));
5072 DIE(aTHX_ PL_no_sock_func, "getservent");
5079 #ifdef HAS_SETHOSTENT
5081 PerlSock_sethostent(TOPi);
5084 DIE(aTHX_ PL_no_sock_func, "sethostent");
5091 #ifdef HAS_SETNETENT
5093 (void)PerlSock_setnetent(TOPi);
5096 DIE(aTHX_ PL_no_sock_func, "setnetent");
5103 #ifdef HAS_SETPROTOENT
5105 (void)PerlSock_setprotoent(TOPi);
5108 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5115 #ifdef HAS_SETSERVENT
5117 (void)PerlSock_setservent(TOPi);
5120 DIE(aTHX_ PL_no_sock_func, "setservent");
5127 #ifdef HAS_ENDHOSTENT
5129 PerlSock_endhostent();
5133 DIE(aTHX_ PL_no_sock_func, "endhostent");
5140 #ifdef HAS_ENDNETENT
5142 PerlSock_endnetent();
5146 DIE(aTHX_ PL_no_sock_func, "endnetent");
5153 #ifdef HAS_ENDPROTOENT
5155 PerlSock_endprotoent();
5159 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5166 #ifdef HAS_ENDSERVENT
5168 PerlSock_endservent();
5172 DIE(aTHX_ PL_no_sock_func, "endservent");
5181 I32 which = PL_op->op_type;
5183 struct passwd *pwent = NULL;
5185 * We currently support only the SysV getsp* shadow password interface.
5186 * The interface is declared in <shadow.h> and often one needs to link
5187 * with -lsecurity or some such.
5188 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5191 * AIX getpwnam() is clever enough to return the encrypted password
5192 * only if the caller (euid?) is root.
5194 * There are at least three other shadow password APIs. Many platforms
5195 * seem to contain more than one interface for accessing the shadow
5196 * password databases, possibly for compatibility reasons.
5197 * The getsp*() is by far he simplest one, the other two interfaces
5198 * are much more complicated, but also very similar to each other.
5203 * struct pr_passwd *getprpw*();
5204 * The password is in
5205 * char getprpw*(...).ufld.fd_encrypt[]
5206 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5211 * struct es_passwd *getespw*();
5212 * The password is in
5213 * char *(getespw*(...).ufld.fd_encrypt)
5214 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5217 * struct userpw *getuserpw();
5218 * The password is in
5219 * char *(getuserpw(...)).spw_upw_passwd
5220 * (but the de facto standard getpwnam() should work okay)
5222 * Mention I_PROT here so that Configure probes for it.
5224 * In HP-UX for getprpw*() the manual page claims that one should include
5225 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5226 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5227 * and pp_sys.c already includes <shadow.h> if there is such.
5229 * Note that <sys/security.h> is already probed for, but currently
5230 * it is only included in special cases.
5232 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5233 * be preferred interface, even though also the getprpw*() interface
5234 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5235 * One also needs to call set_auth_parameters() in main() before
5236 * doing anything else, whether one is using getespw*() or getprpw*().
5238 * Note that accessing the shadow databases can be magnitudes
5239 * slower than accessing the standard databases.
5244 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5245 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5246 * the pw_comment is left uninitialized. */
5247 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5253 const char* const name = POPpbytex;
5254 pwent = getpwnam(name);
5260 pwent = getpwuid(uid);
5264 # ifdef HAS_GETPWENT
5266 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5267 if (pwent) pwent = getpwnam(pwent->pw_name);
5270 DIE(aTHX_ PL_no_func, "getpwent");
5276 if (GIMME != G_ARRAY) {
5277 PUSHs(sv = sv_newmortal());
5279 if (which == OP_GPWNAM)
5280 # if Uid_t_sign <= 0
5281 sv_setiv(sv, (IV)pwent->pw_uid);
5283 sv_setuv(sv, (UV)pwent->pw_uid);
5286 sv_setpv(sv, pwent->pw_name);
5292 mPUSHs(newSVpv(pwent->pw_name, 0));
5296 /* If we have getspnam(), we try to dig up the shadow
5297 * password. If we are underprivileged, the shadow
5298 * interface will set the errno to EACCES or similar,
5299 * and return a null pointer. If this happens, we will
5300 * use the dummy password (usually "*" or "x") from the
5301 * standard password database.
5303 * In theory we could skip the shadow call completely
5304 * if euid != 0 but in practice we cannot know which
5305 * security measures are guarding the shadow databases
5306 * on a random platform.
5308 * Resist the urge to use additional shadow interfaces.
5309 * Divert the urge to writing an extension instead.
5312 /* Some AIX setups falsely(?) detect some getspnam(), which
5313 * has a different API than the Solaris/IRIX one. */
5314 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5317 const struct spwd * const spwent = getspnam(pwent->pw_name);
5318 /* Save and restore errno so that
5319 * underprivileged attempts seem
5320 * to have never made the unsccessful
5321 * attempt to retrieve the shadow password. */
5323 if (spwent && spwent->sp_pwdp)
5324 sv_setpv(sv, spwent->sp_pwdp);
5328 if (!SvPOK(sv)) /* Use the standard password, then. */
5329 sv_setpv(sv, pwent->pw_passwd);
5332 # ifndef INCOMPLETE_TAINTS
5333 /* passwd is tainted because user himself can diddle with it.
5334 * admittedly not much and in a very limited way, but nevertheless. */
5338 # if Uid_t_sign <= 0
5339 mPUSHi(pwent->pw_uid);
5341 mPUSHu(pwent->pw_uid);
5344 # if Uid_t_sign <= 0
5345 mPUSHi(pwent->pw_gid);
5347 mPUSHu(pwent->pw_gid);
5349 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5350 * because of the poor interface of the Perl getpw*(),
5351 * not because there's some standard/convention saying so.
5352 * A better interface would have been to return a hash,
5353 * but we are accursed by our history, alas. --jhi. */
5355 mPUSHi(pwent->pw_change);
5358 mPUSHi(pwent->pw_quota);
5361 mPUSHs(newSVpv(pwent->pw_age, 0));
5363 /* I think that you can never get this compiled, but just in case. */
5364 PUSHs(sv_mortalcopy(&PL_sv_no));
5369 /* pw_class and pw_comment are mutually exclusive--.
5370 * see the above note for pw_change, pw_quota, and pw_age. */
5372 mPUSHs(newSVpv(pwent->pw_class, 0));
5375 mPUSHs(newSVpv(pwent->pw_comment, 0));
5377 /* I think that you can never get this compiled, but just in case. */
5378 PUSHs(sv_mortalcopy(&PL_sv_no));
5383 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5385 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5387 # ifndef INCOMPLETE_TAINTS
5388 /* pw_gecos is tainted because user himself can diddle with it. */
5392 mPUSHs(newSVpv(pwent->pw_dir, 0));
5394 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5395 # ifndef INCOMPLETE_TAINTS
5396 /* pw_shell is tainted because user himself can diddle with it. */
5401 mPUSHi(pwent->pw_expire);
5406 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5413 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5418 DIE(aTHX_ PL_no_func, "setpwent");
5425 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5430 DIE(aTHX_ PL_no_func, "endpwent");
5439 const I32 which = PL_op->op_type;
5440 const struct group *grent;
5442 if (which == OP_GGRNAM) {
5443 const char* const name = POPpbytex;
5444 grent = (const struct group *)getgrnam(name);
5446 else if (which == OP_GGRGID) {
5447 const Gid_t gid = POPi;
5448 grent = (const struct group *)getgrgid(gid);
5452 grent = (struct group *)getgrent();
5454 DIE(aTHX_ PL_no_func, "getgrent");
5458 if (GIMME != G_ARRAY) {
5459 SV * const sv = sv_newmortal();
5463 if (which == OP_GGRNAM)
5465 sv_setiv(sv, (IV)grent->gr_gid);
5467 sv_setuv(sv, (UV)grent->gr_gid);
5470 sv_setpv(sv, grent->gr_name);
5476 mPUSHs(newSVpv(grent->gr_name, 0));
5479 mPUSHs(newSVpv(grent->gr_passwd, 0));
5481 PUSHs(sv_mortalcopy(&PL_sv_no));
5485 mPUSHi(grent->gr_gid);
5487 mPUSHu(grent->gr_gid);
5490 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5491 /* In UNICOS/mk (_CRAYMPP) the multithreading
5492 * versions (getgrnam_r, getgrgid_r)
5493 * seem to return an illegal pointer
5494 * as the group members list, gr_mem.
5495 * getgrent() doesn't even have a _r version
5496 * but the gr_mem is poisonous anyway.
5497 * So yes, you cannot get the list of group
5498 * members if building multithreaded in UNICOS/mk. */
5499 PUSHs(space_join_names_mortal(grent->gr_mem));
5505 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5512 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5517 DIE(aTHX_ PL_no_func, "setgrent");
5524 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5529 DIE(aTHX_ PL_no_func, "endgrent");
5540 if (!(tmps = PerlProc_getlogin()))
5542 PUSHp(tmps, strlen(tmps));
5545 DIE(aTHX_ PL_no_func, "getlogin");
5550 /* Miscellaneous. */
5555 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5556 register I32 items = SP - MARK;
5557 unsigned long a[20];
5562 while (++MARK <= SP) {
5563 if (SvTAINTED(*MARK)) {
5569 TAINT_PROPER("syscall");
5572 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5573 * or where sizeof(long) != sizeof(char*). But such machines will
5574 * not likely have syscall implemented either, so who cares?
5576 while (++MARK <= SP) {
5577 if (SvNIOK(*MARK) || !i)
5578 a[i++] = SvIV(*MARK);
5579 else if (*MARK == &PL_sv_undef)
5582 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5588 DIE(aTHX_ "Too many args to syscall");
5590 DIE(aTHX_ "Too few args to syscall");
5592 retval = syscall(a[0]);
5595 retval = syscall(a[0],a[1]);
5598 retval = syscall(a[0],a[1],a[2]);
5601 retval = syscall(a[0],a[1],a[2],a[3]);
5604 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5607 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5610 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5613 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5617 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5620 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5623 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5627 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5631 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5635 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5636 a[10],a[11],a[12],a[13]);
5638 #endif /* atarist */
5644 DIE(aTHX_ PL_no_func, "syscall");
5649 #ifdef FCNTL_EMULATE_FLOCK
5651 /* XXX Emulate flock() with fcntl().
5652 What's really needed is a good file locking module.
5656 fcntl_emulate_flock(int fd, int operation)
5661 switch (operation & ~LOCK_NB) {
5663 flock.l_type = F_RDLCK;
5666 flock.l_type = F_WRLCK;
5669 flock.l_type = F_UNLCK;
5675 flock.l_whence = SEEK_SET;
5676 flock.l_start = flock.l_len = (Off_t)0;
5678 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5679 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5680 errno = EWOULDBLOCK;
5684 #endif /* FCNTL_EMULATE_FLOCK */
5686 #ifdef LOCKF_EMULATE_FLOCK
5688 /* XXX Emulate flock() with lockf(). This is just to increase
5689 portability of scripts. The calls are not completely
5690 interchangeable. What's really needed is a good file
5694 /* The lockf() constants might have been defined in <unistd.h>.
5695 Unfortunately, <unistd.h> causes troubles on some mixed
5696 (BSD/POSIX) systems, such as SunOS 4.1.3.
5698 Further, the lockf() constants aren't POSIX, so they might not be
5699 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5700 just stick in the SVID values and be done with it. Sigh.
5704 # define F_ULOCK 0 /* Unlock a previously locked region */
5707 # define F_LOCK 1 /* Lock a region for exclusive use */
5710 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5713 # define F_TEST 3 /* Test a region for other processes locks */
5717 lockf_emulate_flock(int fd, int operation)
5723 /* flock locks entire file so for lockf we need to do the same */
5724 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5725 if (pos > 0) /* is seekable and needs to be repositioned */
5726 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5727 pos = -1; /* seek failed, so don't seek back afterwards */
5730 switch (operation) {
5732 /* LOCK_SH - get a shared lock */
5734 /* LOCK_EX - get an exclusive lock */
5736 i = lockf (fd, F_LOCK, 0);
5739 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5740 case LOCK_SH|LOCK_NB:
5741 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5742 case LOCK_EX|LOCK_NB:
5743 i = lockf (fd, F_TLOCK, 0);
5745 if ((errno == EAGAIN) || (errno == EACCES))
5746 errno = EWOULDBLOCK;
5749 /* LOCK_UN - unlock (non-blocking is a no-op) */
5751 case LOCK_UN|LOCK_NB:
5752 i = lockf (fd, F_ULOCK, 0);
5755 /* Default - can't decipher operation */
5762 if (pos > 0) /* need to restore position of the handle */
5763 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5768 #endif /* LOCKF_EMULATE_FLOCK */
5772 * c-indentation-style: bsd
5774 * indent-tabs-mode: t
5777 * ex: set ts=8 sts=4 sw=4 noet: