3 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 * 2004, 2005, 2006, 2007 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.
18 /* This file contains system pp ("push/pop") functions that
19 * execute the opcodes that make up a perl program. A typical pp function
20 * expects to find its arguments on the stack, and usually pushes its
21 * results onto the stack, hence the 'pp' terminology. Each OP structure
22 * contains a pointer to the relevant pp_foo() function.
24 * By 'system', we mean ops which interact with the OS, such as pp_open().
28 #define PERL_IN_PP_SYS_C
34 /* Shadow password support for solaris - pdo@cs.umd.edu
35 * Not just Solaris: at least HP-UX, IRIX, Linux.
36 * The API is from SysV.
38 * There are at least two more shadow interfaces,
39 * see the comments in pp_gpwent().
43 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
44 * and another MAXINT from "perl.h" <- <sys/param.h>. */
51 # include <sys/wait.h>
55 # include <sys/resource.h>
64 # include <sys/select.h>
68 /* XXX Configure test needed.
69 h_errno might not be a simple 'int', especially for multi-threaded
70 applications, see "extern int errno in perl.h". Creating such
71 a test requires taking into account the differences between
72 compiling multithreaded and singlethreaded ($ccflags et al).
73 HOST_NOT_FOUND is typically defined in <netdb.h>.
75 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
84 struct passwd *getpwnam (char *);
85 struct passwd *getpwuid (Uid_t);
90 struct passwd *getpwent (void);
91 #elif defined (VMS) && defined (my_getpwent)
92 struct passwd *Perl_my_getpwent (pTHX);
101 struct group *getgrnam (char *);
102 struct group *getgrgid (Gid_t);
106 struct group *getgrent (void);
112 # if defined(_MSC_VER) || defined(__MINGW32__)
113 # include <sys/utime.h>
120 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
123 # define my_chsize PerlLIO_chsize
126 # define my_chsize PerlLIO_chsize
128 I32 my_chsize(int fd, Off_t length);
134 #else /* no flock() */
136 /* fcntl.h might not have been included, even if it exists, because
137 the current Configure only sets I_FCNTL if it's needed to pick up
138 the *_OK constants. Make sure it has been included before testing
139 the fcntl() locking constants. */
140 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
144 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
145 # define FLOCK fcntl_emulate_flock
146 # define FCNTL_EMULATE_FLOCK
147 # else /* no flock() or fcntl(F_SETLK,...) */
149 # define FLOCK lockf_emulate_flock
150 # define LOCKF_EMULATE_FLOCK
152 # endif /* no flock() or fcntl(F_SETLK,...) */
155 static int FLOCK (int, int);
158 * These are the flock() constants. Since this sytems doesn't have
159 * flock(), the values of the constants are probably not available.
173 # endif /* emulating flock() */
175 #endif /* no flock() */
178 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
180 #if defined(I_SYS_ACCESS) && !defined(R_OK)
181 # include <sys/access.h>
184 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
185 # define FD_CLOEXEC 1 /* NeXT needs this */
191 /* Missing protos on LynxOS */
192 void sethostent(int);
193 void endhostent(void);
195 void endnetent(void);
196 void setprotoent(int);
197 void endprotoent(void);
198 void setservent(int);
199 void endservent(void);
202 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
204 /* F_OK unused: if stat() cannot find it... */
206 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
207 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
208 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
211 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
212 # ifdef I_SYS_SECURITY
213 # include <sys/security.h>
217 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
220 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
224 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
226 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
230 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
231 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
232 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
235 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
237 const Uid_t ruid = getuid();
238 const Uid_t euid = geteuid();
239 const Gid_t rgid = getgid();
240 const Gid_t egid = getegid();
244 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
245 Perl_croak(aTHX_ "switching effective uid is not implemented");
248 if (setreuid(euid, ruid))
251 if (setresuid(euid, ruid, (Uid_t)-1))
254 Perl_croak(aTHX_ "entering effective uid failed");
257 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
258 Perl_croak(aTHX_ "switching effective gid is not implemented");
261 if (setregid(egid, rgid))
264 if (setresgid(egid, rgid, (Gid_t)-1))
267 Perl_croak(aTHX_ "entering effective gid failed");
270 res = access(path, mode);
273 if (setreuid(ruid, euid))
276 if (setresuid(ruid, euid, (Uid_t)-1))
279 Perl_croak(aTHX_ "leaving effective uid failed");
282 if (setregid(rgid, egid))
285 if (setresgid(rgid, egid, (Gid_t)-1))
288 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) {
323 PL_rs = &PL_sv_undef;
324 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
325 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
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 */
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 = (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();
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) {
423 tmps = SvPV_const(tmpsv, len);
424 if ((!tmps || !len) && PL_errgv) {
425 SV * const error = ERRSV;
426 SvUPGRADE(error, SVt_PV);
427 if (SvPOK(error) && SvCUR(error))
428 sv_catpvs(error, "\t...caught");
430 tmps = SvPV_const(tmpsv, len);
433 tmpsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
435 Perl_warn(aTHX_ "%"SVf, SVfARG(tmpsv));
447 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
449 if (SP - MARK != 1) {
451 do_join(TARG, &PL_sv_no, MARK, SP);
453 tmps = SvPV_const(tmpsv, len);
459 tmps = SvROK(tmpsv) ? (const char *)NULL : SvPV_const(tmpsv, len);
462 SV * const error = ERRSV;
463 SvUPGRADE(error, SVt_PV);
464 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
466 SvSetSV(error,tmpsv);
467 else if (sv_isobject(error)) {
468 HV * const stash = SvSTASH(SvRV(error));
469 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
471 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
472 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
479 call_sv((SV*)GvCV(gv),
480 G_SCALAR|G_EVAL|G_KEEPERR);
481 sv_setsv(error,*PL_stack_sp--);
487 if (SvPOK(error) && SvCUR(error))
488 sv_catpvs(error, "\t...propagated");
491 tmps = SvPV_const(tmpsv, len);
497 tmpsv = newSVpvs_flags("Died", SVs_TEMP);
499 DIE(aTHX_ "%"SVf, SVfARG(tmpsv));
515 GV * const gv = (GV *)*++MARK;
518 DIE(aTHX_ PL_no_usym, "filehandle");
520 if ((io = GvIOp(gv))) {
522 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
524 if (IoDIRP(io) && ckWARN2(WARN_IO, WARN_DEPRECATED))
525 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
526 "Opening dirhandle %s also as a file", GvENAME(gv));
528 mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
530 /* Method's args are same as ours ... */
531 /* ... except handle is replaced by the object */
532 *MARK-- = SvTIED_obj((SV*)io, mg);
536 call_method("OPEN", G_SCALAR);
550 tmps = SvPV_const(sv, len);
551 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
554 PUSHi( (I32)PL_forkprocess );
555 else if (PL_forkprocess == 0) /* we are a new child */
565 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
568 IO * const io = GvIO(gv);
570 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
573 XPUSHs(SvTIED_obj((SV*)io, mg));
576 call_method("CLOSE", G_SCALAR);
584 PUSHs(boolSV(do_close(gv, TRUE)));
597 GV * const wgv = (GV*)POPs;
598 GV * const rgv = (GV*)POPs;
603 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
604 DIE(aTHX_ PL_no_usym, "filehandle");
609 do_close(rgv, FALSE);
611 do_close(wgv, FALSE);
613 if (PerlProc_pipe(fd) < 0)
616 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
617 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
618 IoOFP(rstio) = IoIFP(rstio);
619 IoIFP(wstio) = IoOFP(wstio);
620 IoTYPE(rstio) = IoTYPE_RDONLY;
621 IoTYPE(wstio) = IoTYPE_WRONLY;
623 if (!IoIFP(rstio) || !IoOFP(wstio)) {
625 PerlIO_close(IoIFP(rstio));
627 PerlLIO_close(fd[0]);
629 PerlIO_close(IoOFP(wstio));
631 PerlLIO_close(fd[1]);
634 #if defined(HAS_FCNTL) && defined(F_SETFD)
635 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
636 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
643 DIE(aTHX_ PL_no_func, "pipe");
659 if (gv && (io = GvIO(gv))
660 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
663 XPUSHs(SvTIED_obj((SV*)io, mg));
666 call_method("FILENO", G_SCALAR);
672 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
673 /* Can't do this because people seem to do things like
674 defined(fileno($foo)) to check whether $foo is a valid fh.
675 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
676 report_evil_fh(gv, io, PL_op->op_type);
681 PUSHi(PerlIO_fileno(fp));
694 anum = PerlLIO_umask(022);
695 /* setting it to 022 between the two calls to umask avoids
696 * to have a window where the umask is set to 0 -- meaning
697 * that another thread could create world-writeable files. */
699 (void)PerlLIO_umask(anum);
702 anum = PerlLIO_umask(POPi);
703 TAINT_PROPER("umask");
706 /* Only DIE if trying to restrict permissions on "user" (self).
707 * Otherwise it's harmless and more useful to just return undef
708 * since 'group' and 'other' concepts probably don't exist here. */
709 if (MAXARG >= 1 && (POPi & 0700))
710 DIE(aTHX_ "umask not implemented");
711 XPUSHs(&PL_sv_undef);
732 if (gv && (io = GvIO(gv))) {
733 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
736 XPUSHs(SvTIED_obj((SV*)io, mg));
741 call_method("BINMODE", G_SCALAR);
749 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
750 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
751 report_evil_fh(gv, io, PL_op->op_type);
752 SETERRNO(EBADF,RMS_IFI);
759 const char *d = NULL;
762 d = SvPV_const(discp, len);
763 mode = mode_from_discipline(d, len);
764 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
765 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
766 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
787 const I32 markoff = MARK - PL_stack_base;
788 const char *methname;
789 int how = PERL_MAGIC_tied;
793 switch(SvTYPE(varsv)) {
795 methname = "TIEHASH";
796 HvEITER_set((HV *)varsv, 0);
799 methname = "TIEARRAY";
802 if (isGV_with_GP(varsv)) {
803 #ifdef GV_UNIQUE_CHECK
804 if (GvUNIQUE((GV*)varsv)) {
805 Perl_croak(aTHX_ "Attempt to tie unique GV");
808 methname = "TIEHANDLE";
809 how = PERL_MAGIC_tiedscalar;
810 /* For tied filehandles, we apply tiedscalar magic to the IO
811 slot of the GP rather than the GV itself. AMS 20010812 */
813 GvIOp(varsv) = newIO();
814 varsv = (SV *)GvIOp(varsv);
819 methname = "TIESCALAR";
820 how = PERL_MAGIC_tiedscalar;
824 if (sv_isobject(*MARK)) { /* Calls GET magic. */
826 PUSHSTACKi(PERLSI_MAGIC);
828 EXTEND(SP,(I32)items);
832 call_method(methname, G_SCALAR);
835 /* Not clear why we don't call call_method here too.
836 * perhaps to get different error message ?
839 const char *name = SvPV_nomg_const(*MARK, len);
840 stash = gv_stashpvn(name, len, 0);
841 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
842 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
843 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
846 PUSHSTACKi(PERLSI_MAGIC);
848 EXTEND(SP,(I32)items);
852 call_sv((SV*)GvCV(gv), G_SCALAR);
858 if (sv_isobject(sv)) {
859 sv_unmagic(varsv, how);
860 /* Croak if a self-tie on an aggregate is attempted. */
861 if (varsv == SvRV(sv) &&
862 (SvTYPE(varsv) == SVt_PVAV ||
863 SvTYPE(varsv) == SVt_PVHV))
865 "Self-ties of arrays and hashes are not supported");
866 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
869 SP = PL_stack_base + markoff;
879 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
880 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
882 if (isGV_with_GP(sv) && !(sv = (SV *)GvIOp(sv)))
885 if ((mg = SvTIED_mg(sv, how))) {
886 SV * const obj = SvRV(SvTIED_obj(sv, mg));
888 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
890 if (gv && isGV(gv) && (cv = GvCV(gv))) {
892 XPUSHs(SvTIED_obj((SV*)gv, mg));
893 mXPUSHi(SvREFCNT(obj) - 1);
896 call_sv((SV *)cv, G_VOID);
900 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
901 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
902 "untie attempted while %"UVuf" inner references still exist",
903 (UV)SvREFCNT(obj) - 1 ) ;
907 sv_unmagic(sv, how) ;
917 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
918 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
920 if (isGV_with_GP(sv) && !(sv = (SV *)GvIOp(sv)))
923 if ((mg = SvTIED_mg(sv, how))) {
924 SV *osv = SvTIED_obj(sv, mg);
925 if (osv == mg->mg_obj)
926 osv = sv_mortalcopy(osv);
940 HV * const hv = (HV*)POPs;
941 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
942 stash = gv_stashsv(sv, 0);
943 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
945 require_pv("AnyDBM_File.pm");
947 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
948 DIE(aTHX_ "No dbm on this machine");
958 mPUSHu(O_RDWR|O_CREAT);
963 call_sv((SV*)GvCV(gv), G_SCALAR);
966 if (!sv_isobject(TOPs)) {
974 call_sv((SV*)GvCV(gv), G_SCALAR);
978 if (sv_isobject(TOPs)) {
979 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
980 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, NULL, 0);
997 struct timeval timebuf;
998 struct timeval *tbuf = &timebuf;
1001 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1006 # if BYTEORDER & 0xf0000
1007 # define ORDERBYTE (0x88888888 - BYTEORDER)
1009 # define ORDERBYTE (0x4444 - BYTEORDER)
1015 for (i = 1; i <= 3; i++) {
1016 SV * const sv = SP[i];
1019 if (SvREADONLY(sv)) {
1021 sv_force_normal_flags(sv, 0);
1022 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1023 DIE(aTHX_ PL_no_modify);
1026 if (ckWARN(WARN_MISC))
1027 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1028 SvPV_force_nolen(sv); /* force string conversion */
1035 /* little endians can use vecs directly */
1036 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1043 masksize = NFDBITS / NBBY;
1045 masksize = sizeof(long); /* documented int, everyone seems to use long */
1047 Zero(&fd_sets[0], 4, char*);
1050 # if SELECT_MIN_BITS == 1
1051 growsize = sizeof(fd_set);
1053 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1054 # undef SELECT_MIN_BITS
1055 # define SELECT_MIN_BITS __FD_SETSIZE
1057 /* If SELECT_MIN_BITS is greater than one we most probably will want
1058 * to align the sizes with SELECT_MIN_BITS/8 because for example
1059 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1060 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1061 * on (sets/tests/clears bits) is 32 bits. */
1062 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1070 timebuf.tv_sec = (long)value;
1071 value -= (NV)timebuf.tv_sec;
1072 timebuf.tv_usec = (long)(value * 1000000.0);
1077 for (i = 1; i <= 3; i++) {
1079 if (!SvOK(sv) || SvCUR(sv) == 0) {
1086 Sv_Grow(sv, growsize);
1090 while (++j <= growsize) {
1094 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1096 Newx(fd_sets[i], growsize, char);
1097 for (offset = 0; offset < growsize; offset += masksize) {
1098 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1099 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1102 fd_sets[i] = SvPVX(sv);
1106 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1107 /* Can't make just the (void*) conditional because that would be
1108 * cpp #if within cpp macro, and not all compilers like that. */
1109 nfound = PerlSock_select(
1111 (Select_fd_set_t) fd_sets[1],
1112 (Select_fd_set_t) fd_sets[2],
1113 (Select_fd_set_t) fd_sets[3],
1114 (void*) tbuf); /* Workaround for compiler bug. */
1116 nfound = PerlSock_select(
1118 (Select_fd_set_t) fd_sets[1],
1119 (Select_fd_set_t) fd_sets[2],
1120 (Select_fd_set_t) fd_sets[3],
1123 for (i = 1; i <= 3; i++) {
1126 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1128 for (offset = 0; offset < growsize; offset += masksize) {
1129 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1130 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1132 Safefree(fd_sets[i]);
1139 if (GIMME == G_ARRAY && tbuf) {
1140 value = (NV)(timebuf.tv_sec) +
1141 (NV)(timebuf.tv_usec) / 1000000.0;
1146 DIE(aTHX_ "select not implemented");
1151 Perl_setdefout(pTHX_ GV *gv)
1154 SvREFCNT_inc_simple_void(gv);
1156 SvREFCNT_dec(PL_defoutgv);
1164 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : NULL;
1165 GV * egv = GvEGV(PL_defoutgv);
1171 XPUSHs(&PL_sv_undef);
1173 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1174 if (gvp && *gvp == egv) {
1175 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1179 mXPUSHs(newRV((SV*)egv));
1184 if (!GvIO(newdefout))
1185 gv_IOadd(newdefout);
1186 setdefout(newdefout);
1196 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1198 if (gv && (io = GvIO(gv))) {
1199 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1201 const I32 gimme = GIMME_V;
1203 XPUSHs(SvTIED_obj((SV*)io, mg));
1206 call_method("GETC", gimme);
1209 if (gimme == G_SCALAR)
1210 SvSetMagicSV_nosteal(TARG, TOPs);
1214 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1215 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1216 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1217 report_evil_fh(gv, io, PL_op->op_type);
1218 SETERRNO(EBADF,RMS_IFI);
1222 sv_setpvn(TARG, " ", 1);
1223 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1224 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1225 /* Find out how many bytes the char needs */
1226 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1229 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1230 SvCUR_set(TARG,1+len);
1239 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1242 register PERL_CONTEXT *cx;
1243 const I32 gimme = GIMME_V;
1245 PERL_ARGS_ASSERT_DOFORM;
1250 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1251 PUSHFORMAT(cx, retop);
1253 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1255 setdefout(gv); /* locally select filehandle so $% et al work */
1287 goto not_a_format_reference;
1292 tmpsv = sv_newmortal();
1293 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1294 name = SvPV_nolen_const(tmpsv);
1296 DIE(aTHX_ "Undefined format \"%s\" called", name);
1298 not_a_format_reference:
1299 DIE(aTHX_ "Not a format reference");
1302 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1304 IoFLAGS(io) &= ~IOf_DIDTOP;
1305 return doform(cv,gv,PL_op->op_next);
1311 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1312 register IO * const io = GvIOp(gv);
1317 register PERL_CONTEXT *cx;
1319 if (!io || !(ofp = IoOFP(io)))
1322 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1323 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1325 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1326 PL_formtarget != PL_toptarget)
1330 if (!IoTOP_GV(io)) {
1333 if (!IoTOP_NAME(io)) {
1335 if (!IoFMT_NAME(io))
1336 IoFMT_NAME(io) = savepv(GvNAME(gv));
1337 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1338 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1339 if ((topgv && GvFORM(topgv)) ||
1340 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1341 IoTOP_NAME(io) = savesvpv(topname);
1343 IoTOP_NAME(io) = savepvs("top");
1345 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1346 if (!topgv || !GvFORM(topgv)) {
1347 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1350 IoTOP_GV(io) = topgv;
1352 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1353 I32 lines = IoLINES_LEFT(io);
1354 const char *s = SvPVX_const(PL_formtarget);
1355 if (lines <= 0) /* Yow, header didn't even fit!!! */
1357 while (lines-- > 0) {
1358 s = strchr(s, '\n');
1364 const STRLEN save = SvCUR(PL_formtarget);
1365 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1366 do_print(PL_formtarget, ofp);
1367 SvCUR_set(PL_formtarget, save);
1368 sv_chop(PL_formtarget, s);
1369 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1372 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1373 do_print(PL_formfeed, ofp);
1374 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1376 PL_formtarget = PL_toptarget;
1377 IoFLAGS(io) |= IOf_DIDTOP;
1380 DIE(aTHX_ "bad top format reference");
1383 SV * const sv = sv_newmortal();
1385 gv_efullname4(sv, fgv, NULL, FALSE);
1386 name = SvPV_nolen_const(sv);
1388 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1390 DIE(aTHX_ "Undefined top format called");
1392 if (cv && CvCLONE(cv))
1393 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1394 return doform(cv, gv, PL_op);
1398 POPBLOCK(cx,PL_curpm);
1404 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1406 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1407 else if (ckWARN(WARN_CLOSED))
1408 report_evil_fh(gv, io, PL_op->op_type);
1413 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1414 if (ckWARN(WARN_IO))
1415 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1417 if (!do_print(PL_formtarget, fp))
1420 FmLINES(PL_formtarget) = 0;
1421 SvCUR_set(PL_formtarget, 0);
1422 *SvEND(PL_formtarget) = '\0';
1423 if (IoFLAGS(io) & IOf_FLUSH)
1424 (void)PerlIO_flush(fp);
1429 PL_formtarget = PL_bodytarget;
1431 PERL_UNUSED_VAR(newsp);
1432 PERL_UNUSED_VAR(gimme);
1433 return cx->blk_sub.retop;
1438 dVAR; dSP; dMARK; dORIGMARK;
1443 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1445 if (gv && (io = GvIO(gv))) {
1446 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1448 if (MARK == ORIGMARK) {
1451 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1455 *MARK = SvTIED_obj((SV*)io, mg);
1458 call_method("PRINTF", G_SCALAR);
1461 MARK = ORIGMARK + 1;
1469 if (!(io = GvIO(gv))) {
1470 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1471 report_evil_fh(gv, io, PL_op->op_type);
1472 SETERRNO(EBADF,RMS_IFI);
1475 else if (!(fp = IoOFP(io))) {
1476 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1478 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1479 else if (ckWARN(WARN_CLOSED))
1480 report_evil_fh(gv, io, PL_op->op_type);
1482 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1486 if (SvTAINTED(MARK[1]))
1487 TAINT_PROPER("printf");
1488 do_sprintf(sv, SP - MARK, MARK + 1);
1489 if (!do_print(sv, fp))
1492 if (IoFLAGS(io) & IOf_FLUSH)
1493 if (PerlIO_flush(fp) == EOF)
1504 PUSHs(&PL_sv_undef);
1512 const int perm = (MAXARG > 3) ? POPi : 0666;
1513 const int mode = POPi;
1514 SV * const sv = POPs;
1515 GV * const gv = (GV *)POPs;
1518 /* Need TIEHANDLE method ? */
1519 const char * const tmps = SvPV_const(sv, len);
1520 /* FIXME? do_open should do const */
1521 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1522 IoLINES(GvIOp(gv)) = 0;
1526 PUSHs(&PL_sv_undef);
1533 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1539 Sock_size_t bufsize;
1547 bool charstart = FALSE;
1548 STRLEN charskip = 0;
1551 GV * const gv = (GV*)*++MARK;
1552 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1553 && gv && (io = GvIO(gv)) )
1555 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1559 *MARK = SvTIED_obj((SV*)io, mg);
1561 call_method("READ", G_SCALAR);
1575 sv_setpvn(bufsv, "", 0);
1576 length = SvIVx(*++MARK);
1579 offset = SvIVx(*++MARK);
1583 if (!io || !IoIFP(io)) {
1584 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1585 report_evil_fh(gv, io, PL_op->op_type);
1586 SETERRNO(EBADF,RMS_IFI);
1589 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1590 buffer = SvPVutf8_force(bufsv, blen);
1591 /* UTF-8 may not have been set if they are all low bytes */
1596 buffer = SvPV_force(bufsv, blen);
1597 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1600 DIE(aTHX_ "Negative length");
1608 if (PL_op->op_type == OP_RECV) {
1609 char namebuf[MAXPATHLEN];
1610 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1611 bufsize = sizeof (struct sockaddr_in);
1613 bufsize = sizeof namebuf;
1615 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1619 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1620 /* 'offset' means 'flags' here */
1621 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1622 (struct sockaddr *)namebuf, &bufsize);
1626 /* Bogus return without padding */
1627 bufsize = sizeof (struct sockaddr_in);
1629 SvCUR_set(bufsv, count);
1630 *SvEND(bufsv) = '\0';
1631 (void)SvPOK_only(bufsv);
1635 /* This should not be marked tainted if the fp is marked clean */
1636 if (!(IoFLAGS(io) & IOf_UNTAINT))
1637 SvTAINTED_on(bufsv);
1639 sv_setpvn(TARG, namebuf, bufsize);
1644 if (PL_op->op_type == OP_RECV)
1645 DIE(aTHX_ PL_no_sock_func, "recv");
1647 if (DO_UTF8(bufsv)) {
1648 /* offset adjust in characters not bytes */
1649 blen = sv_len_utf8(bufsv);
1652 if (-offset > (int)blen)
1653 DIE(aTHX_ "Offset outside string");
1656 if (DO_UTF8(bufsv)) {
1657 /* convert offset-as-chars to offset-as-bytes */
1658 if (offset >= (int)blen)
1659 offset += SvCUR(bufsv) - blen;
1661 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1664 bufsize = SvCUR(bufsv);
1665 /* Allocating length + offset + 1 isn't perfect in the case of reading
1666 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1668 (should be 2 * length + offset + 1, or possibly something longer if
1669 PL_encoding is true) */
1670 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1671 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1672 Zero(buffer+bufsize, offset-bufsize, char);
1674 buffer = buffer + offset;
1676 read_target = bufsv;
1678 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1679 concatenate it to the current buffer. */
1681 /* Truncate the existing buffer to the start of where we will be
1683 SvCUR_set(bufsv, offset);
1685 read_target = sv_newmortal();
1686 SvUPGRADE(read_target, SVt_PV);
1687 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1690 if (PL_op->op_type == OP_SYSREAD) {
1691 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1692 if (IoTYPE(io) == IoTYPE_SOCKET) {
1693 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1699 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1704 #ifdef HAS_SOCKET__bad_code_maybe
1705 if (IoTYPE(io) == IoTYPE_SOCKET) {
1706 char namebuf[MAXPATHLEN];
1707 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1708 bufsize = sizeof (struct sockaddr_in);
1710 bufsize = sizeof namebuf;
1712 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1713 (struct sockaddr *)namebuf, &bufsize);
1718 count = PerlIO_read(IoIFP(io), buffer, length);
1719 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1720 if (count == 0 && PerlIO_error(IoIFP(io)))
1724 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1725 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1728 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1729 *SvEND(read_target) = '\0';
1730 (void)SvPOK_only(read_target);
1731 if (fp_utf8 && !IN_BYTES) {
1732 /* Look at utf8 we got back and count the characters */
1733 const char *bend = buffer + count;
1734 while (buffer < bend) {
1736 skip = UTF8SKIP(buffer);
1739 if (buffer - charskip + skip > bend) {
1740 /* partial character - try for rest of it */
1741 length = skip - (bend-buffer);
1742 offset = bend - SvPVX_const(bufsv);
1754 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1755 provided amount read (count) was what was requested (length)
1757 if (got < wanted && count == length) {
1758 length = wanted - got;
1759 offset = bend - SvPVX_const(bufsv);
1762 /* return value is character count */
1766 else if (buffer_utf8) {
1767 /* Let svcatsv upgrade the bytes we read in to utf8.
1768 The buffer is a mortal so will be freed soon. */
1769 sv_catsv_nomg(bufsv, read_target);
1772 /* This should not be marked tainted if the fp is marked clean */
1773 if (!(IoFLAGS(io) & IOf_UNTAINT))
1774 SvTAINTED_on(bufsv);
1786 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1792 STRLEN orig_blen_bytes;
1793 const int op_type = PL_op->op_type;
1797 GV *const gv = (GV*)*++MARK;
1798 if (PL_op->op_type == OP_SYSWRITE
1799 && gv && (io = GvIO(gv))) {
1800 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1804 if (MARK == SP - 1) {
1806 sv = sv_2mortal(newSViv(sv_len(*SP)));
1812 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1814 call_method("WRITE", G_SCALAR);
1830 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1832 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1833 if (io && IoIFP(io))
1834 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1836 report_evil_fh(gv, io, PL_op->op_type);
1838 SETERRNO(EBADF,RMS_IFI);
1842 /* Do this first to trigger any overloading. */
1843 buffer = SvPV_const(bufsv, blen);
1844 orig_blen_bytes = blen;
1845 doing_utf8 = DO_UTF8(bufsv);
1847 if (PerlIO_isutf8(IoIFP(io))) {
1848 if (!SvUTF8(bufsv)) {
1849 /* We don't modify the original scalar. */
1850 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1851 buffer = (char *) tmpbuf;
1855 else if (doing_utf8) {
1856 STRLEN tmplen = blen;
1857 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1860 buffer = (char *) tmpbuf;
1864 assert((char *)result == buffer);
1865 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1869 if (op_type == OP_SYSWRITE) {
1870 Size_t length = 0; /* This length is in characters. */
1876 /* The SV is bytes, and we've had to upgrade it. */
1877 blen_chars = orig_blen_bytes;
1879 /* The SV really is UTF-8. */
1880 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1881 /* Don't call sv_len_utf8 again because it will call magic
1882 or overloading a second time, and we might get back a
1883 different result. */
1884 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1886 /* It's safe, and it may well be cached. */
1887 blen_chars = sv_len_utf8(bufsv);
1895 length = blen_chars;
1897 #if Size_t_size > IVSIZE
1898 length = (Size_t)SvNVx(*++MARK);
1900 length = (Size_t)SvIVx(*++MARK);
1902 if ((SSize_t)length < 0) {
1904 DIE(aTHX_ "Negative length");
1909 offset = SvIVx(*++MARK);
1911 if (-offset > (IV)blen_chars) {
1913 DIE(aTHX_ "Offset outside string");
1915 offset += blen_chars;
1916 } else if (offset >= (IV)blen_chars && blen_chars > 0) {
1918 DIE(aTHX_ "Offset outside string");
1922 if (length > blen_chars - offset)
1923 length = blen_chars - offset;
1925 /* Here we convert length from characters to bytes. */
1926 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1927 /* Either we had to convert the SV, or the SV is magical, or
1928 the SV has overloading, in which case we can't or mustn't
1929 or mustn't call it again. */
1931 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1932 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1934 /* It's a real UTF-8 SV, and it's not going to change under
1935 us. Take advantage of any cache. */
1937 I32 len_I32 = length;
1939 /* Convert the start and end character positions to bytes.
1940 Remember that the second argument to sv_pos_u2b is relative
1942 sv_pos_u2b(bufsv, &start, &len_I32);
1949 buffer = buffer+offset;
1951 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1952 if (IoTYPE(io) == IoTYPE_SOCKET) {
1953 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1959 /* See the note at doio.c:do_print about filesize limits. --jhi */
1960 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1966 const int flags = SvIVx(*++MARK);
1969 char * const sockbuf = SvPVx(*++MARK, mlen);
1970 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1971 flags, (struct sockaddr *)sockbuf, mlen);
1975 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1980 DIE(aTHX_ PL_no_sock_func, "send");
1987 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1990 #if Size_t_size > IVSIZE
2009 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
2011 gv = PL_last_in_gv = GvEGV(PL_argvgv);
2013 if (io && !IoIFP(io)) {
2014 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2016 IoFLAGS(io) &= ~IOf_START;
2017 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2019 sv_setpvn(GvSV(gv), "-", 1);
2022 GvSV(gv) = newSVpvn("-", 1);
2024 SvSETMAGIC(GvSV(gv));
2026 else if (!nextargv(gv))
2031 gv = PL_last_in_gv; /* eof */
2034 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
2037 IO * const io = GvIO(gv);
2039 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
2041 XPUSHs(SvTIED_obj((SV*)io, mg));
2044 call_method("EOF", G_SCALAR);
2051 PUSHs(boolSV(!gv || do_eof(gv)));
2062 PL_last_in_gv = (GV*)POPs;
2065 if (gv && (io = GvIO(gv))) {
2066 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
2069 XPUSHs(SvTIED_obj((SV*)io, mg));
2072 call_method("TELL", G_SCALAR);
2079 #if LSEEKSIZE > IVSIZE
2080 PUSHn( do_tell(gv) );
2082 PUSHi( do_tell(gv) );
2090 const int whence = POPi;
2091 #if LSEEKSIZE > IVSIZE
2092 const Off_t offset = (Off_t)SvNVx(POPs);
2094 const Off_t offset = (Off_t)SvIVx(POPs);
2097 GV * const gv = PL_last_in_gv = (GV*)POPs;
2100 if (gv && (io = GvIO(gv))) {
2101 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
2104 XPUSHs(SvTIED_obj((SV*)io, mg));
2105 #if LSEEKSIZE > IVSIZE
2106 mXPUSHn((NV) offset);
2113 call_method("SEEK", G_SCALAR);
2120 if (PL_op->op_type == OP_SEEK)
2121 PUSHs(boolSV(do_seek(gv, offset, whence)));
2123 const Off_t sought = do_sysseek(gv, offset, whence);
2125 PUSHs(&PL_sv_undef);
2127 SV* const sv = sought ?
2128 #if LSEEKSIZE > IVSIZE
2133 : newSVpvn(zero_but_true, ZBTLEN);
2144 /* There seems to be no consensus on the length type of truncate()
2145 * and ftruncate(), both off_t and size_t have supporters. In
2146 * general one would think that when using large files, off_t is
2147 * at least as wide as size_t, so using an off_t should be okay. */
2148 /* XXX Configure probe for the length type of *truncate() needed XXX */
2151 #if Off_t_size > IVSIZE
2156 /* Checking for length < 0 is problematic as the type might or
2157 * might not be signed: if it is not, clever compilers will moan. */
2158 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2165 if (PL_op->op_flags & OPf_SPECIAL) {
2166 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2175 TAINT_PROPER("truncate");
2176 if (!(fp = IoIFP(io))) {
2182 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2184 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2191 SV * const sv = POPs;
2194 if (isGV_with_GP(sv)) {
2195 tmpgv = (GV*)sv; /* *main::FRED for example */
2196 goto do_ftruncate_gv;
2198 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2199 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2200 goto do_ftruncate_gv;
2202 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2203 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2204 goto do_ftruncate_io;
2207 name = SvPV_nolen_const(sv);
2208 TAINT_PROPER("truncate");
2210 if (truncate(name, len) < 0)
2214 const int tmpfd = PerlLIO_open(name, O_RDWR);
2219 if (my_chsize(tmpfd, len) < 0)
2221 PerlLIO_close(tmpfd);
2230 SETERRNO(EBADF,RMS_IFI);
2238 SV * const argsv = POPs;
2239 const unsigned int func = POPu;
2240 const int optype = PL_op->op_type;
2241 GV * const gv = (GV*)POPs;
2242 IO * const io = gv ? GvIOn(gv) : NULL;
2246 if (!io || !argsv || !IoIFP(io)) {
2247 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2248 report_evil_fh(gv, io, PL_op->op_type);
2249 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2253 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2256 s = SvPV_force(argsv, len);
2257 need = IOCPARM_LEN(func);
2259 s = Sv_Grow(argsv, need + 1);
2260 SvCUR_set(argsv, need);
2263 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2266 retval = SvIV(argsv);
2267 s = INT2PTR(char*,retval); /* ouch */
2270 TAINT_PROPER(PL_op_desc[optype]);
2272 if (optype == OP_IOCTL)
2274 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2276 DIE(aTHX_ "ioctl is not implemented");
2280 DIE(aTHX_ "fcntl is not implemented");
2282 #if defined(OS2) && defined(__EMX__)
2283 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2285 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2289 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2291 if (s[SvCUR(argsv)] != 17)
2292 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2294 s[SvCUR(argsv)] = 0; /* put our null back */
2295 SvSETMAGIC(argsv); /* Assume it has changed */
2304 PUSHp(zero_but_true, ZBTLEN);
2317 const int argtype = POPi;
2318 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
2320 if (gv && (io = GvIO(gv)))
2326 /* XXX Looks to me like io is always NULL at this point */
2328 (void)PerlIO_flush(fp);
2329 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2332 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2333 report_evil_fh(gv, io, PL_op->op_type);
2335 SETERRNO(EBADF,RMS_IFI);
2340 DIE(aTHX_ PL_no_func, "flock()");
2350 const int protocol = POPi;
2351 const int type = POPi;
2352 const int domain = POPi;
2353 GV * const gv = (GV*)POPs;
2354 register IO * const io = gv ? GvIOn(gv) : NULL;
2358 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2359 report_evil_fh(gv, io, PL_op->op_type);
2360 if (io && IoIFP(io))
2361 do_close(gv, FALSE);
2362 SETERRNO(EBADF,LIB_INVARG);
2367 do_close(gv, FALSE);
2369 TAINT_PROPER("socket");
2370 fd = PerlSock_socket(domain, type, protocol);
2373 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2374 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2375 IoTYPE(io) = IoTYPE_SOCKET;
2376 if (!IoIFP(io) || !IoOFP(io)) {
2377 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2378 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2379 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2382 #if defined(HAS_FCNTL) && defined(F_SETFD)
2383 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2387 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2392 DIE(aTHX_ PL_no_sock_func, "socket");
2398 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2400 const int protocol = POPi;
2401 const int type = POPi;
2402 const int domain = POPi;
2403 GV * const gv2 = (GV*)POPs;
2404 GV * const gv1 = (GV*)POPs;
2405 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2406 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2409 if (!gv1 || !gv2 || !io1 || !io2) {
2410 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2412 report_evil_fh(gv1, io1, PL_op->op_type);
2414 report_evil_fh(gv1, io2, PL_op->op_type);
2416 if (io1 && IoIFP(io1))
2417 do_close(gv1, FALSE);
2418 if (io2 && IoIFP(io2))
2419 do_close(gv2, FALSE);
2424 do_close(gv1, FALSE);
2426 do_close(gv2, FALSE);
2428 TAINT_PROPER("socketpair");
2429 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2431 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2432 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2433 IoTYPE(io1) = IoTYPE_SOCKET;
2434 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2435 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2436 IoTYPE(io2) = IoTYPE_SOCKET;
2437 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2438 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2439 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2440 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2441 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2442 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2443 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2446 #if defined(HAS_FCNTL) && defined(F_SETFD)
2447 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2448 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2453 DIE(aTHX_ PL_no_sock_func, "socketpair");
2461 SV * const addrsv = POPs;
2462 /* OK, so on what platform does bind modify addr? */
2464 GV * const gv = (GV*)POPs;
2465 register IO * const io = GvIOn(gv);
2468 if (!io || !IoIFP(io))
2471 addr = SvPV_const(addrsv, len);
2472 TAINT_PROPER("bind");
2473 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2479 if (ckWARN(WARN_CLOSED))
2480 report_evil_fh(gv, io, PL_op->op_type);
2481 SETERRNO(EBADF,SS_IVCHAN);
2484 DIE(aTHX_ PL_no_sock_func, "bind");
2492 SV * const addrsv = POPs;
2493 GV * const gv = (GV*)POPs;
2494 register IO * const io = GvIOn(gv);
2498 if (!io || !IoIFP(io))
2501 addr = SvPV_const(addrsv, len);
2502 TAINT_PROPER("connect");
2503 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2509 if (ckWARN(WARN_CLOSED))
2510 report_evil_fh(gv, io, PL_op->op_type);
2511 SETERRNO(EBADF,SS_IVCHAN);
2514 DIE(aTHX_ PL_no_sock_func, "connect");
2522 const int backlog = POPi;
2523 GV * const gv = (GV*)POPs;
2524 register IO * const io = gv ? GvIOn(gv) : NULL;
2526 if (!gv || !io || !IoIFP(io))
2529 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2535 if (ckWARN(WARN_CLOSED))
2536 report_evil_fh(gv, io, PL_op->op_type);
2537 SETERRNO(EBADF,SS_IVCHAN);
2540 DIE(aTHX_ PL_no_sock_func, "listen");
2550 char namebuf[MAXPATHLEN];
2551 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2552 Sock_size_t len = sizeof (struct sockaddr_in);
2554 Sock_size_t len = sizeof namebuf;
2556 GV * const ggv = (GV*)POPs;
2557 GV * const ngv = (GV*)POPs;
2566 if (!gstio || !IoIFP(gstio))
2570 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2573 /* Some platforms indicate zero length when an AF_UNIX client is
2574 * not bound. Simulate a non-zero-length sockaddr structure in
2576 namebuf[0] = 0; /* sun_len */
2577 namebuf[1] = AF_UNIX; /* sun_family */
2585 do_close(ngv, FALSE);
2586 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2587 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2588 IoTYPE(nstio) = IoTYPE_SOCKET;
2589 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2590 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2591 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2592 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2595 #if defined(HAS_FCNTL) && defined(F_SETFD)
2596 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2600 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2601 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2603 #ifdef __SCO_VERSION__
2604 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2607 PUSHp(namebuf, len);
2611 if (ckWARN(WARN_CLOSED))
2612 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2613 SETERRNO(EBADF,SS_IVCHAN);
2619 DIE(aTHX_ PL_no_sock_func, "accept");
2627 const int how = POPi;
2628 GV * const gv = (GV*)POPs;
2629 register IO * const io = GvIOn(gv);
2631 if (!io || !IoIFP(io))
2634 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2638 if (ckWARN(WARN_CLOSED))
2639 report_evil_fh(gv, io, PL_op->op_type);
2640 SETERRNO(EBADF,SS_IVCHAN);
2643 DIE(aTHX_ PL_no_sock_func, "shutdown");
2651 const int optype = PL_op->op_type;
2652 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2653 const unsigned int optname = (unsigned int) POPi;
2654 const unsigned int lvl = (unsigned int) POPi;
2655 GV * const gv = (GV*)POPs;
2656 register IO * const io = GvIOn(gv);
2660 if (!io || !IoIFP(io))
2663 fd = PerlIO_fileno(IoIFP(io));
2667 (void)SvPOK_only(sv);
2671 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2678 #if defined(__SYMBIAN32__)
2679 # define SETSOCKOPT_OPTION_VALUE_T void *
2681 # define SETSOCKOPT_OPTION_VALUE_T const char *
2683 /* XXX TODO: We need to have a proper type (a Configure probe,
2684 * etc.) for what the C headers think of the third argument of
2685 * setsockopt(), the option_value read-only buffer: is it
2686 * a "char *", or a "void *", const or not. Some compilers
2687 * don't take kindly to e.g. assuming that "char *" implicitly
2688 * promotes to a "void *", or to explicitly promoting/demoting
2689 * consts to non/vice versa. The "const void *" is the SUS
2690 * definition, but that does not fly everywhere for the above
2692 SETSOCKOPT_OPTION_VALUE_T buf;
2696 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2700 aint = (int)SvIV(sv);
2701 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2704 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2713 if (ckWARN(WARN_CLOSED))
2714 report_evil_fh(gv, io, optype);
2715 SETERRNO(EBADF,SS_IVCHAN);
2720 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2728 const int optype = PL_op->op_type;
2729 GV * const gv = (GV*)POPs;
2730 register IO * const io = GvIOn(gv);
2735 if (!io || !IoIFP(io))
2738 sv = sv_2mortal(newSV(257));
2739 (void)SvPOK_only(sv);
2743 fd = PerlIO_fileno(IoIFP(io));
2745 case OP_GETSOCKNAME:
2746 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2749 case OP_GETPEERNAME:
2750 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2752 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2754 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";
2755 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2756 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2757 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2758 sizeof(u_short) + sizeof(struct in_addr))) {
2765 #ifdef BOGUS_GETNAME_RETURN
2766 /* Interactive Unix, getpeername() and getsockname()
2767 does not return valid namelen */
2768 if (len == BOGUS_GETNAME_RETURN)
2769 len = sizeof(struct sockaddr);
2777 if (ckWARN(WARN_CLOSED))
2778 report_evil_fh(gv, io, optype);
2779 SETERRNO(EBADF,SS_IVCHAN);
2784 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2799 if (PL_op->op_flags & OPf_REF) {
2801 if (PL_op->op_type == OP_LSTAT) {
2802 if (gv != PL_defgv) {
2803 do_fstat_warning_check:
2804 if (ckWARN(WARN_IO))
2805 Perl_warner(aTHX_ packWARN(WARN_IO),
2806 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2807 } else if (PL_laststype != OP_LSTAT)
2808 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2812 if (gv != PL_defgv) {
2813 PL_laststype = OP_STAT;
2815 sv_setpvn(PL_statname, "", 0);
2822 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2823 } else if (IoDIRP(io)) {
2825 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2827 PL_laststatval = -1;
2833 if (PL_laststatval < 0) {
2834 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2835 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2840 SV* const sv = POPs;
2841 if (isGV_with_GP(sv)) {
2844 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2846 if (PL_op->op_type == OP_LSTAT)
2847 goto do_fstat_warning_check;
2849 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2851 if (PL_op->op_type == OP_LSTAT)
2852 goto do_fstat_warning_check;
2853 goto do_fstat_have_io;
2856 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2858 PL_laststype = PL_op->op_type;
2859 if (PL_op->op_type == OP_LSTAT)
2860 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2862 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2863 if (PL_laststatval < 0) {
2864 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2865 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2871 if (gimme != G_ARRAY) {
2872 if (gimme != G_VOID)
2873 XPUSHs(boolSV(max));
2879 mPUSHi(PL_statcache.st_dev);
2880 mPUSHi(PL_statcache.st_ino);
2881 mPUSHu(PL_statcache.st_mode);
2882 mPUSHu(PL_statcache.st_nlink);
2883 #if Uid_t_size > IVSIZE
2884 mPUSHn(PL_statcache.st_uid);
2886 # if Uid_t_sign <= 0
2887 mPUSHi(PL_statcache.st_uid);
2889 mPUSHu(PL_statcache.st_uid);
2892 #if Gid_t_size > IVSIZE
2893 mPUSHn(PL_statcache.st_gid);
2895 # if Gid_t_sign <= 0
2896 mPUSHi(PL_statcache.st_gid);
2898 mPUSHu(PL_statcache.st_gid);
2901 #ifdef USE_STAT_RDEV
2902 mPUSHi(PL_statcache.st_rdev);
2904 PUSHs(newSVpvs_flags("", SVs_TEMP));
2906 #if Off_t_size > IVSIZE
2907 mPUSHn(PL_statcache.st_size);
2909 mPUSHi(PL_statcache.st_size);
2912 mPUSHn(PL_statcache.st_atime);
2913 mPUSHn(PL_statcache.st_mtime);
2914 mPUSHn(PL_statcache.st_ctime);
2916 mPUSHi(PL_statcache.st_atime);
2917 mPUSHi(PL_statcache.st_mtime);
2918 mPUSHi(PL_statcache.st_ctime);
2920 #ifdef USE_STAT_BLOCKS
2921 mPUSHu(PL_statcache.st_blksize);
2922 mPUSHu(PL_statcache.st_blocks);
2924 PUSHs(newSVpvs_flags("", SVs_TEMP));
2925 PUSHs(newSVpvs_flags("", SVs_TEMP));
2931 /* This macro is used by the stacked filetest operators :
2932 * if the previous filetest failed, short-circuit and pass its value.
2933 * Else, discard it from the stack and continue. --rgs
2935 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2936 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2937 else { (void)POPs; PUTBACK; } \
2944 /* Not const, because things tweak this below. Not bool, because there's
2945 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2946 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2947 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2948 /* Giving some sort of initial value silences compilers. */
2950 int access_mode = R_OK;
2952 int access_mode = 0;
2955 /* access_mode is never used, but leaving use_access in makes the
2956 conditional compiling below much clearer. */
2959 int stat_mode = S_IRUSR;
2961 bool effective = FALSE;
2964 STACKED_FTEST_CHECK;
2966 switch (PL_op->op_type) {
2968 #if !(defined(HAS_ACCESS) && defined(R_OK))
2974 #if defined(HAS_ACCESS) && defined(W_OK)
2979 stat_mode = S_IWUSR;
2983 #if defined(HAS_ACCESS) && defined(X_OK)
2988 stat_mode = S_IXUSR;
2992 #ifdef PERL_EFF_ACCESS
2995 stat_mode = S_IWUSR;
2999 #ifndef PERL_EFF_ACCESS
3006 #ifdef PERL_EFF_ACCESS
3011 stat_mode = S_IXUSR;
3017 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3018 const char *name = POPpx;
3020 # ifdef PERL_EFF_ACCESS
3021 result = PERL_EFF_ACCESS(name, access_mode);
3023 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3029 result = access(name, access_mode);
3031 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3046 if (cando(stat_mode, effective, &PL_statcache))
3055 const int op_type = PL_op->op_type;
3057 STACKED_FTEST_CHECK;
3062 if (op_type == OP_FTIS)
3065 /* You can't dTARGET inside OP_FTIS, because you'll get
3066 "panic: pad_sv po" - the op is not flagged to have a target. */
3070 #if Off_t_size > IVSIZE
3071 PUSHn(PL_statcache.st_size);
3073 PUSHi(PL_statcache.st_size);
3077 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3080 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3083 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3096 /* I believe that all these three are likely to be defined on most every
3097 system these days. */
3099 if(PL_op->op_type == OP_FTSUID)
3103 if(PL_op->op_type == OP_FTSGID)
3107 if(PL_op->op_type == OP_FTSVTX)
3111 STACKED_FTEST_CHECK;
3116 switch (PL_op->op_type) {
3118 if (PL_statcache.st_uid == PL_uid)
3122 if (PL_statcache.st_uid == PL_euid)
3126 if (PL_statcache.st_size == 0)
3130 if (S_ISSOCK(PL_statcache.st_mode))
3134 if (S_ISCHR(PL_statcache.st_mode))
3138 if (S_ISBLK(PL_statcache.st_mode))
3142 if (S_ISREG(PL_statcache.st_mode))
3146 if (S_ISDIR(PL_statcache.st_mode))
3150 if (S_ISFIFO(PL_statcache.st_mode))
3155 if (PL_statcache.st_mode & S_ISUID)
3161 if (PL_statcache.st_mode & S_ISGID)
3167 if (PL_statcache.st_mode & S_ISVTX)
3178 I32 result = my_lstat();
3182 if (S_ISLNK(PL_statcache.st_mode))
3195 STACKED_FTEST_CHECK;
3197 if (PL_op->op_flags & OPf_REF)
3199 else if (isGV(TOPs))
3201 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3202 gv = (GV*)SvRV(POPs);
3204 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3206 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3207 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3208 else if (tmpsv && SvOK(tmpsv)) {
3209 const char *tmps = SvPV_nolen_const(tmpsv);
3217 if (PerlLIO_isatty(fd))
3222 #if defined(atarist) /* this will work with atariST. Configure will
3223 make guesses for other systems. */
3224 # define FILE_base(f) ((f)->_base)
3225 # define FILE_ptr(f) ((f)->_ptr)
3226 # define FILE_cnt(f) ((f)->_cnt)
3227 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3238 register STDCHAR *s;
3244 STACKED_FTEST_CHECK;
3246 if (PL_op->op_flags & OPf_REF)
3248 else if (isGV(TOPs))
3250 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3251 gv = (GV*)SvRV(POPs);
3257 if (gv == PL_defgv) {
3259 io = GvIO(PL_statgv);
3262 goto really_filename;
3267 PL_laststatval = -1;
3268 sv_setpvn(PL_statname, "", 0);
3269 io = GvIO(PL_statgv);
3271 if (io && IoIFP(io)) {
3272 if (! PerlIO_has_base(IoIFP(io)))
3273 DIE(aTHX_ "-T and -B not implemented on filehandles");
3274 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3275 if (PL_laststatval < 0)
3277 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3278 if (PL_op->op_type == OP_FTTEXT)
3283 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3284 i = PerlIO_getc(IoIFP(io));
3286 (void)PerlIO_ungetc(IoIFP(io),i);
3288 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3290 len = PerlIO_get_bufsiz(IoIFP(io));
3291 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3292 /* sfio can have large buffers - limit to 512 */
3297 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3299 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3301 SETERRNO(EBADF,RMS_IFI);
3309 PL_laststype = OP_STAT;
3310 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3311 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3312 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3314 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3317 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3318 if (PL_laststatval < 0) {
3319 (void)PerlIO_close(fp);
3322 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3323 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3324 (void)PerlIO_close(fp);
3326 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3327 RETPUSHNO; /* special case NFS directories */
3328 RETPUSHYES; /* null file is anything */
3333 /* now scan s to look for textiness */
3334 /* XXX ASCII dependent code */
3336 #if defined(DOSISH) || defined(USEMYBINMODE)
3337 /* ignore trailing ^Z on short files */
3338 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3342 for (i = 0; i < len; i++, s++) {
3343 if (!*s) { /* null never allowed in text */
3348 else if (!(isPRINT(*s) || isSPACE(*s)))
3351 else if (*s & 128) {
3353 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3356 /* utf8 characters don't count as odd */
3357 if (UTF8_IS_START(*s)) {
3358 int ulen = UTF8SKIP(s);
3359 if (ulen < len - i) {
3361 for (j = 1; j < ulen; j++) {
3362 if (!UTF8_IS_CONTINUATION(s[j]))
3365 --ulen; /* loop does extra increment */
3375 *s != '\n' && *s != '\r' && *s != '\b' &&
3376 *s != '\t' && *s != '\f' && *s != 27)
3381 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3392 const char *tmps = NULL;
3396 SV * const sv = POPs;
3397 if (PL_op->op_flags & OPf_SPECIAL) {
3398 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3400 else if (isGV_with_GP(sv)) {
3403 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3407 tmps = SvPV_nolen_const(sv);
3411 if( !gv && (!tmps || !*tmps) ) {
3412 HV * const table = GvHVn(PL_envgv);
3415 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3416 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3418 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3423 deprecate("chdir('') or chdir(undef) as chdir()");
3424 tmps = SvPV_nolen_const(*svp);
3428 TAINT_PROPER("chdir");
3433 TAINT_PROPER("chdir");
3436 IO* const io = GvIO(gv);
3439 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3440 } else if (IoIFP(io)) {
3441 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3444 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3445 report_evil_fh(gv, io, PL_op->op_type);
3446 SETERRNO(EBADF, RMS_IFI);
3451 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3452 report_evil_fh(gv, io, PL_op->op_type);
3453 SETERRNO(EBADF,RMS_IFI);
3457 DIE(aTHX_ PL_no_func, "fchdir");
3461 PUSHi( PerlDir_chdir(tmps) >= 0 );
3463 /* Clear the DEFAULT element of ENV so we'll get the new value
3465 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3472 dVAR; dSP; dMARK; dTARGET;
3473 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3484 char * const tmps = POPpx;
3485 TAINT_PROPER("chroot");
3486 PUSHi( chroot(tmps) >= 0 );
3489 DIE(aTHX_ PL_no_func, "chroot");
3497 const char * const tmps2 = POPpconstx;
3498 const char * const tmps = SvPV_nolen_const(TOPs);
3499 TAINT_PROPER("rename");
3501 anum = PerlLIO_rename(tmps, tmps2);
3503 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3504 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3507 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3508 (void)UNLINK(tmps2);
3509 if (!(anum = link(tmps, tmps2)))
3510 anum = UNLINK(tmps);
3518 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3522 const int op_type = PL_op->op_type;
3526 if (op_type == OP_LINK)
3527 DIE(aTHX_ PL_no_func, "link");
3529 # ifndef HAS_SYMLINK
3530 if (op_type == OP_SYMLINK)
3531 DIE(aTHX_ PL_no_func, "symlink");
3535 const char * const tmps2 = POPpconstx;
3536 const char * const tmps = SvPV_nolen_const(TOPs);
3537 TAINT_PROPER(PL_op_desc[op_type]);
3539 # if defined(HAS_LINK)
3540 # if defined(HAS_SYMLINK)
3541 /* Both present - need to choose which. */
3542 (op_type == OP_LINK) ?
3543 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3545 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3546 PerlLIO_link(tmps, tmps2);
3549 # if defined(HAS_SYMLINK)
3550 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3551 symlink(tmps, tmps2);
3556 SETi( result >= 0 );
3563 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3574 char buf[MAXPATHLEN];
3577 #ifndef INCOMPLETE_TAINTS
3581 len = readlink(tmps, buf, sizeof(buf) - 1);
3589 RETSETUNDEF; /* just pretend it's a normal file */
3593 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3595 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3597 char * const save_filename = filename;
3602 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3604 PERL_ARGS_ASSERT_DOONELINER;
3606 Newx(cmdline, size, char);
3607 my_strlcpy(cmdline, cmd, size);
3608 my_strlcat(cmdline, " ", size);
3609 for (s = cmdline + strlen(cmdline); *filename; ) {
3613 if (s - cmdline < size)
3614 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3615 myfp = PerlProc_popen(cmdline, "r");
3619 SV * const tmpsv = sv_newmortal();
3620 /* Need to save/restore 'PL_rs' ?? */
3621 s = sv_gets(tmpsv, myfp, 0);
3622 (void)PerlProc_pclose(myfp);
3626 #ifdef HAS_SYS_ERRLIST
3631 /* you don't see this */
3632 const char * const errmsg =
3633 #ifdef HAS_SYS_ERRLIST
3641 if (instr(s, errmsg)) {
3648 #define EACCES EPERM
3650 if (instr(s, "cannot make"))
3651 SETERRNO(EEXIST,RMS_FEX);
3652 else if (instr(s, "existing file"))
3653 SETERRNO(EEXIST,RMS_FEX);
3654 else if (instr(s, "ile exists"))
3655 SETERRNO(EEXIST,RMS_FEX);
3656 else if (instr(s, "non-exist"))
3657 SETERRNO(ENOENT,RMS_FNF);
3658 else if (instr(s, "does not exist"))
3659 SETERRNO(ENOENT,RMS_FNF);
3660 else if (instr(s, "not empty"))
3661 SETERRNO(EBUSY,SS_DEVOFFLINE);
3662 else if (instr(s, "cannot access"))
3663 SETERRNO(EACCES,RMS_PRV);
3665 SETERRNO(EPERM,RMS_PRV);
3668 else { /* some mkdirs return no failure indication */
3669 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3670 if (PL_op->op_type == OP_RMDIR)
3675 SETERRNO(EACCES,RMS_PRV); /* a guess */
3684 /* This macro removes trailing slashes from a directory name.
3685 * Different operating and file systems take differently to
3686 * trailing slashes. According to POSIX 1003.1 1996 Edition
3687 * any number of trailing slashes should be allowed.
3688 * Thusly we snip them away so that even non-conforming
3689 * systems are happy.
3690 * We should probably do this "filtering" for all
3691 * the functions that expect (potentially) directory names:
3692 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3693 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3695 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3696 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3699 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3700 (tmps) = savepvn((tmps), (len)); \
3710 const int mode = (MAXARG > 1) ? POPi : 0777;
3712 TRIMSLASHES(tmps,len,copy);
3714 TAINT_PROPER("mkdir");
3716 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3720 SETi( dooneliner("mkdir", tmps) );
3721 oldumask = PerlLIO_umask(0);
3722 PerlLIO_umask(oldumask);
3723 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3738 TRIMSLASHES(tmps,len,copy);
3739 TAINT_PROPER("rmdir");
3741 SETi( PerlDir_rmdir(tmps) >= 0 );
3743 SETi( dooneliner("rmdir", tmps) );
3750 /* Directory calls. */
3754 #if defined(Direntry_t) && defined(HAS_READDIR)
3756 const char * const dirname = POPpconstx;
3757 GV * const gv = (GV*)POPs;
3758 register IO * const io = GvIOn(gv);
3763 if ((IoIFP(io) || IoOFP(io)) && ckWARN2(WARN_IO, WARN_DEPRECATED))
3764 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3765 "Opening filehandle %s also as a directory", GvENAME(gv));
3767 PerlDir_close(IoDIRP(io));
3768 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3774 SETERRNO(EBADF,RMS_DIR);
3777 DIE(aTHX_ PL_no_dir_func, "opendir");
3783 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3784 DIE(aTHX_ PL_no_dir_func, "readdir");
3786 #if !defined(I_DIRENT) && !defined(VMS)
3787 Direntry_t *readdir (DIR *);
3793 const I32 gimme = GIMME;
3794 GV * const gv = (GV *)POPs;
3795 register const Direntry_t *dp;
3796 register IO * const io = GvIOn(gv);
3798 if (!io || !IoDIRP(io)) {
3799 if(ckWARN(WARN_IO)) {
3800 Perl_warner(aTHX_ packWARN(WARN_IO),
3801 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3807 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3811 sv = newSVpvn(dp->d_name, dp->d_namlen);
3813 sv = newSVpv(dp->d_name, 0);
3815 #ifndef INCOMPLETE_TAINTS
3816 if (!(IoFLAGS(io) & IOf_UNTAINT))
3820 } while (gimme == G_ARRAY);
3822 if (!dp && gimme != G_ARRAY)
3829 SETERRNO(EBADF,RMS_ISI);
3830 if (GIMME == G_ARRAY)
3839 #if defined(HAS_TELLDIR) || defined(telldir)
3841 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3842 /* XXX netbsd still seemed to.
3843 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3844 --JHI 1999-Feb-02 */
3845 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3846 long telldir (DIR *);
3848 GV * const gv = (GV*)POPs;
3849 register IO * const io = GvIOn(gv);
3851 if (!io || !IoDIRP(io)) {
3852 if(ckWARN(WARN_IO)) {
3853 Perl_warner(aTHX_ packWARN(WARN_IO),
3854 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3859 PUSHi( PerlDir_tell(IoDIRP(io)) );
3863 SETERRNO(EBADF,RMS_ISI);
3866 DIE(aTHX_ PL_no_dir_func, "telldir");
3872 #if defined(HAS_SEEKDIR) || defined(seekdir)
3874 const long along = POPl;
3875 GV * const gv = (GV*)POPs;
3876 register IO * const io = GvIOn(gv);
3878 if (!io || !IoDIRP(io)) {
3879 if(ckWARN(WARN_IO)) {
3880 Perl_warner(aTHX_ packWARN(WARN_IO),
3881 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3885 (void)PerlDir_seek(IoDIRP(io), along);
3890 SETERRNO(EBADF,RMS_ISI);
3893 DIE(aTHX_ PL_no_dir_func, "seekdir");
3899 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3901 GV * const gv = (GV*)POPs;
3902 register IO * const io = GvIOn(gv);
3904 if (!io || !IoDIRP(io)) {
3905 if(ckWARN(WARN_IO)) {
3906 Perl_warner(aTHX_ packWARN(WARN_IO),
3907 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3911 (void)PerlDir_rewind(IoDIRP(io));
3915 SETERRNO(EBADF,RMS_ISI);
3918 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3924 #if defined(Direntry_t) && defined(HAS_READDIR)
3926 GV * const gv = (GV*)POPs;
3927 register IO * const io = GvIOn(gv);
3929 if (!io || !IoDIRP(io)) {
3930 if(ckWARN(WARN_IO)) {
3931 Perl_warner(aTHX_ packWARN(WARN_IO),
3932 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3936 #ifdef VOID_CLOSEDIR
3937 PerlDir_close(IoDIRP(io));
3939 if (PerlDir_close(IoDIRP(io)) < 0) {
3940 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3949 SETERRNO(EBADF,RMS_IFI);
3952 DIE(aTHX_ PL_no_dir_func, "closedir");
3956 /* Process control. */
3965 PERL_FLUSHALL_FOR_CHILD;
3966 childpid = PerlProc_fork();
3970 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
3972 SvREADONLY_off(GvSV(tmpgv));
3973 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3974 SvREADONLY_on(GvSV(tmpgv));
3976 #ifdef THREADS_HAVE_PIDS
3977 PL_ppid = (IV)getppid();
3979 #ifdef PERL_USES_PL_PIDSTATUS
3980 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3986 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3991 PERL_FLUSHALL_FOR_CHILD;
3992 childpid = PerlProc_fork();
3998 DIE(aTHX_ PL_no_func, "fork");
4005 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4010 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4011 childpid = wait4pid(-1, &argflags, 0);
4013 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4018 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4019 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4020 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4022 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4027 DIE(aTHX_ PL_no_func, "wait");
4033 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4035 const int optype = POPi;
4036 const Pid_t pid = TOPi;
4040 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4041 result = wait4pid(pid, &argflags, optype);
4043 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4048 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4049 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4050 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4052 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4057 DIE(aTHX_ PL_no_func, "waitpid");
4063 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4064 #if defined(__LIBCATAMOUNT__)
4065 PL_statusvalue = -1;
4074 while (++MARK <= SP) {
4075 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4080 TAINT_PROPER("system");
4082 PERL_FLUSHALL_FOR_CHILD;
4083 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4089 if (PerlProc_pipe(pp) >= 0)
4091 while ((childpid = PerlProc_fork()) == -1) {
4092 if (errno != EAGAIN) {
4097 PerlLIO_close(pp[0]);
4098 PerlLIO_close(pp[1]);
4105 Sigsave_t ihand,qhand; /* place to save signals during system() */
4109 PerlLIO_close(pp[1]);
4111 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4112 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4115 result = wait4pid(childpid, &status, 0);
4116 } while (result == -1 && errno == EINTR);
4118 (void)rsignal_restore(SIGINT, &ihand);
4119 (void)rsignal_restore(SIGQUIT, &qhand);
4121 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4122 do_execfree(); /* free any memory child malloced on fork */
4129 while (n < sizeof(int)) {
4130 n1 = PerlLIO_read(pp[0],
4131 (void*)(((char*)&errkid)+n),
4137 PerlLIO_close(pp[0]);
4138 if (n) { /* Error */
4139 if (n != sizeof(int))
4140 DIE(aTHX_ "panic: kid popen errno read");
4141 errno = errkid; /* Propagate errno from kid */
4142 STATUS_NATIVE_CHILD_SET(-1);
4145 XPUSHi(STATUS_CURRENT);
4149 PerlLIO_close(pp[0]);
4150 #if defined(HAS_FCNTL) && defined(F_SETFD)
4151 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4154 if (PL_op->op_flags & OPf_STACKED) {
4155 SV * const really = *++MARK;
4156 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4158 else if (SP - MARK != 1)
4159 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4161 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4165 #else /* ! FORK or VMS or OS/2 */
4168 if (PL_op->op_flags & OPf_STACKED) {
4169 SV * const really = *++MARK;
4170 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4171 value = (I32)do_aspawn(really, MARK, SP);
4173 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4176 else if (SP - MARK != 1) {
4177 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4178 value = (I32)do_aspawn(NULL, MARK, SP);
4180 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4184 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4186 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4188 STATUS_NATIVE_CHILD_SET(value);
4191 XPUSHi(result ? value : STATUS_CURRENT);
4192 #endif /* !FORK or VMS or OS/2 */
4199 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4204 while (++MARK <= SP) {
4205 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4210 TAINT_PROPER("exec");
4212 PERL_FLUSHALL_FOR_CHILD;
4213 if (PL_op->op_flags & OPf_STACKED) {
4214 SV * const really = *++MARK;
4215 value = (I32)do_aexec(really, MARK, SP);
4217 else if (SP - MARK != 1)
4219 value = (I32)vms_do_aexec(NULL, MARK, SP);
4223 (void ) do_aspawn(NULL, MARK, SP);
4227 value = (I32)do_aexec(NULL, MARK, SP);
4232 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4235 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4238 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4252 # ifdef THREADS_HAVE_PIDS
4253 if (PL_ppid != 1 && getppid() == 1)
4254 /* maybe the parent process has died. Refresh ppid cache */
4258 XPUSHi( getppid() );
4262 DIE(aTHX_ PL_no_func, "getppid");
4271 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4274 pgrp = (I32)BSD_GETPGRP(pid);
4276 if (pid != 0 && pid != PerlProc_getpid())
4277 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4283 DIE(aTHX_ PL_no_func, "getpgrp()");
4302 TAINT_PROPER("setpgrp");
4304 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4306 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4307 || (pid != 0 && pid != PerlProc_getpid()))
4309 DIE(aTHX_ "setpgrp can't take arguments");
4311 SETi( setpgrp() >= 0 );
4312 #endif /* USE_BSDPGRP */
4315 DIE(aTHX_ PL_no_func, "setpgrp()");
4321 #ifdef HAS_GETPRIORITY
4323 const int who = POPi;
4324 const int which = TOPi;
4325 SETi( getpriority(which, who) );
4328 DIE(aTHX_ PL_no_func, "getpriority()");
4334 #ifdef HAS_SETPRIORITY
4336 const int niceval = POPi;
4337 const int who = POPi;
4338 const int which = TOPi;
4339 TAINT_PROPER("setpriority");
4340 SETi( setpriority(which, who, niceval) >= 0 );
4343 DIE(aTHX_ PL_no_func, "setpriority()");
4353 XPUSHn( time(NULL) );
4355 XPUSHi( time(NULL) );
4367 (void)PerlProc_times(&PL_timesbuf);
4369 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4370 /* struct tms, though same data */
4374 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4375 if (GIMME == G_ARRAY) {
4376 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4377 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4378 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4386 if (GIMME == G_ARRAY) {
4393 DIE(aTHX_ "times not implemented");
4395 #endif /* HAS_TIMES */
4405 static const char * const dayname[] =
4406 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4407 static const char * const monname[] =
4408 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4409 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4414 when = (Time64_T)now;
4417 when = (Time64_T)SvNVx(POPs);
4419 if (PL_op->op_type == OP_LOCALTIME)
4420 err = localtime64_r(&when, &tmbuf);
4422 err = gmtime64_r(&when, &tmbuf);
4425 char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4426 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
4427 "%s under/overflowed the year", opname);
4430 if (GIMME != G_ARRAY) { /* scalar context */
4437 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %lld",
4438 dayname[tmbuf.tm_wday],
4439 monname[tmbuf.tm_mon],
4444 tmbuf.tm_year + 1900);
4447 else { /* list context */
4453 mPUSHi(tmbuf.tm_sec);
4454 mPUSHi(tmbuf.tm_min);
4455 mPUSHi(tmbuf.tm_hour);
4456 mPUSHi(tmbuf.tm_mday);
4457 mPUSHi(tmbuf.tm_mon);
4458 mPUSHi(tmbuf.tm_year);
4459 mPUSHi(tmbuf.tm_wday);
4460 mPUSHi(tmbuf.tm_yday);
4461 mPUSHi(tmbuf.tm_isdst);
4472 anum = alarm((unsigned int)anum);
4479 DIE(aTHX_ PL_no_func, "alarm");
4490 (void)time(&lasttime);
4495 PerlProc_sleep((unsigned int)duration);
4498 XPUSHi(when - lasttime);
4502 /* Shared memory. */
4503 /* Merged with some message passing. */
4507 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4508 dVAR; dSP; dMARK; dTARGET;
4509 const int op_type = PL_op->op_type;
4514 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4517 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4520 value = (I32)(do_semop(MARK, SP) >= 0);
4523 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4539 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4540 dVAR; dSP; dMARK; dTARGET;
4541 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4548 DIE(aTHX_ "System V IPC is not implemented on this machine");
4554 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4555 dVAR; dSP; dMARK; dTARGET;
4556 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4564 PUSHp(zero_but_true, ZBTLEN);
4572 /* I can't const this further without getting warnings about the types of
4573 various arrays passed in from structures. */
4575 S_space_join_names_mortal(pTHX_ char *const *array)
4579 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4581 if (array && *array) {
4582 target = newSVpvs_flags("", SVs_TEMP);
4584 sv_catpv(target, *array);
4587 sv_catpvs(target, " ");
4590 target = sv_mortalcopy(&PL_sv_no);
4595 /* Get system info. */
4599 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4601 I32 which = PL_op->op_type;
4602 register char **elem;
4604 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4605 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4606 struct hostent *gethostbyname(Netdb_name_t);
4607 struct hostent *gethostent(void);
4609 struct hostent *hent;
4613 if (which == OP_GHBYNAME) {
4614 #ifdef HAS_GETHOSTBYNAME
4615 const char* const name = POPpbytex;
4616 hent = PerlSock_gethostbyname(name);
4618 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4621 else if (which == OP_GHBYADDR) {
4622 #ifdef HAS_GETHOSTBYADDR
4623 const int addrtype = POPi;
4624 SV * const addrsv = POPs;
4626 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4628 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4630 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4634 #ifdef HAS_GETHOSTENT
4635 hent = PerlSock_gethostent();
4637 DIE(aTHX_ PL_no_sock_func, "gethostent");
4640 #ifdef HOST_NOT_FOUND
4642 #ifdef USE_REENTRANT_API
4643 # ifdef USE_GETHOSTENT_ERRNO
4644 h_errno = PL_reentrant_buffer->_gethostent_errno;
4647 STATUS_UNIX_SET(h_errno);
4651 if (GIMME != G_ARRAY) {
4652 PUSHs(sv = sv_newmortal());
4654 if (which == OP_GHBYNAME) {
4656 sv_setpvn(sv, hent->h_addr, hent->h_length);
4659 sv_setpv(sv, (char*)hent->h_name);
4665 mPUSHs(newSVpv((char*)hent->h_name, 0));
4666 PUSHs(space_join_names_mortal(hent->h_aliases));
4667 mPUSHi(hent->h_addrtype);
4668 len = hent->h_length;
4671 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4672 mXPUSHp(*elem, len);
4676 mPUSHp(hent->h_addr, len);
4678 PUSHs(sv_mortalcopy(&PL_sv_no));
4683 DIE(aTHX_ PL_no_sock_func, "gethostent");
4689 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4691 I32 which = PL_op->op_type;
4693 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4694 struct netent *getnetbyaddr(Netdb_net_t, int);
4695 struct netent *getnetbyname(Netdb_name_t);
4696 struct netent *getnetent(void);
4698 struct netent *nent;
4700 if (which == OP_GNBYNAME){
4701 #ifdef HAS_GETNETBYNAME
4702 const char * const name = POPpbytex;
4703 nent = PerlSock_getnetbyname(name);
4705 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4708 else if (which == OP_GNBYADDR) {
4709 #ifdef HAS_GETNETBYADDR
4710 const int addrtype = POPi;
4711 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4712 nent = PerlSock_getnetbyaddr(addr, addrtype);
4714 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4718 #ifdef HAS_GETNETENT
4719 nent = PerlSock_getnetent();
4721 DIE(aTHX_ PL_no_sock_func, "getnetent");
4724 #ifdef HOST_NOT_FOUND
4726 #ifdef USE_REENTRANT_API
4727 # ifdef USE_GETNETENT_ERRNO
4728 h_errno = PL_reentrant_buffer->_getnetent_errno;
4731 STATUS_UNIX_SET(h_errno);
4736 if (GIMME != G_ARRAY) {
4737 PUSHs(sv = sv_newmortal());
4739 if (which == OP_GNBYNAME)
4740 sv_setiv(sv, (IV)nent->n_net);
4742 sv_setpv(sv, nent->n_name);
4748 mPUSHs(newSVpv(nent->n_name, 0));
4749 PUSHs(space_join_names_mortal(nent->n_aliases));
4750 mPUSHi(nent->n_addrtype);
4751 mPUSHi(nent->n_net);
4756 DIE(aTHX_ PL_no_sock_func, "getnetent");
4762 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4764 I32 which = PL_op->op_type;
4766 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4767 struct protoent *getprotobyname(Netdb_name_t);
4768 struct protoent *getprotobynumber(int);
4769 struct protoent *getprotoent(void);
4771 struct protoent *pent;
4773 if (which == OP_GPBYNAME) {
4774 #ifdef HAS_GETPROTOBYNAME
4775 const char* const name = POPpbytex;
4776 pent = PerlSock_getprotobyname(name);
4778 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4781 else if (which == OP_GPBYNUMBER) {
4782 #ifdef HAS_GETPROTOBYNUMBER
4783 const int number = POPi;
4784 pent = PerlSock_getprotobynumber(number);
4786 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4790 #ifdef HAS_GETPROTOENT
4791 pent = PerlSock_getprotoent();
4793 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4797 if (GIMME != G_ARRAY) {
4798 PUSHs(sv = sv_newmortal());
4800 if (which == OP_GPBYNAME)
4801 sv_setiv(sv, (IV)pent->p_proto);
4803 sv_setpv(sv, pent->p_name);
4809 mPUSHs(newSVpv(pent->p_name, 0));
4810 PUSHs(space_join_names_mortal(pent->p_aliases));
4811 mPUSHi(pent->p_proto);
4816 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4822 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4824 I32 which = PL_op->op_type;
4826 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4827 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4828 struct servent *getservbyport(int, Netdb_name_t);
4829 struct servent *getservent(void);
4831 struct servent *sent;
4833 if (which == OP_GSBYNAME) {
4834 #ifdef HAS_GETSERVBYNAME
4835 const char * const proto = POPpbytex;
4836 const char * const name = POPpbytex;
4837 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4839 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4842 else if (which == OP_GSBYPORT) {
4843 #ifdef HAS_GETSERVBYPORT
4844 const char * const proto = POPpbytex;
4845 unsigned short port = (unsigned short)POPu;
4847 port = PerlSock_htons(port);
4849 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4851 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4855 #ifdef HAS_GETSERVENT
4856 sent = PerlSock_getservent();
4858 DIE(aTHX_ PL_no_sock_func, "getservent");
4862 if (GIMME != G_ARRAY) {
4863 PUSHs(sv = sv_newmortal());
4865 if (which == OP_GSBYNAME) {
4867 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4869 sv_setiv(sv, (IV)(sent->s_port));
4873 sv_setpv(sv, sent->s_name);
4879 mPUSHs(newSVpv(sent->s_name, 0));
4880 PUSHs(space_join_names_mortal(sent->s_aliases));
4882 mPUSHi(PerlSock_ntohs(sent->s_port));
4884 mPUSHi(sent->s_port);
4886 mPUSHs(newSVpv(sent->s_proto, 0));
4891 DIE(aTHX_ PL_no_sock_func, "getservent");
4897 #ifdef HAS_SETHOSTENT
4899 PerlSock_sethostent(TOPi);
4902 DIE(aTHX_ PL_no_sock_func, "sethostent");
4908 #ifdef HAS_SETNETENT
4910 (void)PerlSock_setnetent(TOPi);
4913 DIE(aTHX_ PL_no_sock_func, "setnetent");
4919 #ifdef HAS_SETPROTOENT
4921 (void)PerlSock_setprotoent(TOPi);
4924 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4930 #ifdef HAS_SETSERVENT
4932 (void)PerlSock_setservent(TOPi);
4935 DIE(aTHX_ PL_no_sock_func, "setservent");
4941 #ifdef HAS_ENDHOSTENT
4943 PerlSock_endhostent();
4947 DIE(aTHX_ PL_no_sock_func, "endhostent");
4953 #ifdef HAS_ENDNETENT
4955 PerlSock_endnetent();
4959 DIE(aTHX_ PL_no_sock_func, "endnetent");
4965 #ifdef HAS_ENDPROTOENT
4967 PerlSock_endprotoent();
4971 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4977 #ifdef HAS_ENDSERVENT
4979 PerlSock_endservent();
4983 DIE(aTHX_ PL_no_sock_func, "endservent");
4991 I32 which = PL_op->op_type;
4993 struct passwd *pwent = NULL;
4995 * We currently support only the SysV getsp* shadow password interface.
4996 * The interface is declared in <shadow.h> and often one needs to link
4997 * with -lsecurity or some such.
4998 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5001 * AIX getpwnam() is clever enough to return the encrypted password
5002 * only if the caller (euid?) is root.
5004 * There are at least three other shadow password APIs. Many platforms
5005 * seem to contain more than one interface for accessing the shadow
5006 * password databases, possibly for compatibility reasons.
5007 * The getsp*() is by far he simplest one, the other two interfaces
5008 * are much more complicated, but also very similar to each other.
5013 * struct pr_passwd *getprpw*();
5014 * The password is in
5015 * char getprpw*(...).ufld.fd_encrypt[]
5016 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5021 * struct es_passwd *getespw*();
5022 * The password is in
5023 * char *(getespw*(...).ufld.fd_encrypt)
5024 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5027 * struct userpw *getuserpw();
5028 * The password is in
5029 * char *(getuserpw(...)).spw_upw_passwd
5030 * (but the de facto standard getpwnam() should work okay)
5032 * Mention I_PROT here so that Configure probes for it.
5034 * In HP-UX for getprpw*() the manual page claims that one should include
5035 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5036 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5037 * and pp_sys.c already includes <shadow.h> if there is such.
5039 * Note that <sys/security.h> is already probed for, but currently
5040 * it is only included in special cases.
5042 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5043 * be preferred interface, even though also the getprpw*() interface
5044 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5045 * One also needs to call set_auth_parameters() in main() before
5046 * doing anything else, whether one is using getespw*() or getprpw*().
5048 * Note that accessing the shadow databases can be magnitudes
5049 * slower than accessing the standard databases.
5054 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5055 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5056 * the pw_comment is left uninitialized. */
5057 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5063 const char* const name = POPpbytex;
5064 pwent = getpwnam(name);
5070 pwent = getpwuid(uid);
5074 # ifdef HAS_GETPWENT
5076 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5077 if (pwent) pwent = getpwnam(pwent->pw_name);
5080 DIE(aTHX_ PL_no_func, "getpwent");<