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) {
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)
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 = 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();
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(MUTABLE_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 = MUTABLE_GV(*++MARK);
518 DIE(aTHX_ PL_no_usym, "filehandle");
520 if ((io = GvIOp(gv))) {
522 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
525 Perl_ck_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
526 "Opening dirhandle %s also as a file", GvENAME(gv));
528 mg = SvTIED_mg((const 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(MUTABLE_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 : MUTABLE_GV(POPs);
568 IO * const io = GvIO(gv);
570 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
573 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
576 call_method("CLOSE", G_SCALAR);
584 PUSHs(boolSV(do_close(gv, TRUE)));
597 GV * const wgv = MUTABLE_GV(POPs);
598 GV * const rgv = MUTABLE_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");
657 gv = MUTABLE_GV(POPs);
659 if (gv && (io = GvIO(gv))
660 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
663 XPUSHs(SvTIED_obj(MUTABLE_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);
730 gv = MUTABLE_GV(POPs);
732 if (gv && (io = GvIO(gv))) {
733 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
736 XPUSHs(SvTIED_obj(MUTABLE_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(MUTABLE_HV(varsv), 0);
799 methname = "TIEARRAY";
802 if (isGV_with_GP(varsv)) {
803 methname = "TIEHANDLE";
804 how = PERL_MAGIC_tiedscalar;
805 /* For tied filehandles, we apply tiedscalar magic to the IO
806 slot of the GP rather than the GV itself. AMS 20010812 */
808 GvIOp(varsv) = newIO();
809 varsv = MUTABLE_SV(GvIOp(varsv));
814 methname = "TIESCALAR";
815 how = PERL_MAGIC_tiedscalar;
819 if (sv_isobject(*MARK)) { /* Calls GET magic. */
821 PUSHSTACKi(PERLSI_MAGIC);
823 EXTEND(SP,(I32)items);
827 call_method(methname, G_SCALAR);
830 /* Not clear why we don't call call_method here too.
831 * perhaps to get different error message ?
834 const char *name = SvPV_nomg_const(*MARK, len);
835 stash = gv_stashpvn(name, len, 0);
836 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
837 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
838 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
841 PUSHSTACKi(PERLSI_MAGIC);
843 EXTEND(SP,(I32)items);
847 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
853 if (sv_isobject(sv)) {
854 sv_unmagic(varsv, how);
855 /* Croak if a self-tie on an aggregate is attempted. */
856 if (varsv == SvRV(sv) &&
857 (SvTYPE(varsv) == SVt_PVAV ||
858 SvTYPE(varsv) == SVt_PVHV))
860 "Self-ties of arrays and hashes are not supported");
861 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
864 SP = PL_stack_base + markoff;
874 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
875 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
877 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
880 if ((mg = SvTIED_mg(sv, how))) {
881 SV * const obj = SvRV(SvTIED_obj(sv, mg));
883 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
885 if (gv && isGV(gv) && (cv = GvCV(gv))) {
887 XPUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
888 mXPUSHi(SvREFCNT(obj) - 1);
891 call_sv(MUTABLE_SV(cv), G_VOID);
895 else if (mg && SvREFCNT(obj) > 1) {
896 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
897 "untie attempted while %"UVuf" inner references still exist",
898 (UV)SvREFCNT(obj) - 1 ) ;
902 sv_unmagic(sv, how) ;
912 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
913 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
915 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
918 if ((mg = SvTIED_mg(sv, how))) {
919 SV *osv = SvTIED_obj(sv, mg);
920 if (osv == mg->mg_obj)
921 osv = sv_mortalcopy(osv);
935 HV * const hv = MUTABLE_HV(POPs);
936 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
937 stash = gv_stashsv(sv, 0);
938 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
940 require_pv("AnyDBM_File.pm");
942 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
943 DIE(aTHX_ "No dbm on this machine");
953 mPUSHu(O_RDWR|O_CREAT);
958 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
961 if (!sv_isobject(TOPs)) {
969 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
973 if (sv_isobject(TOPs)) {
974 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
975 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
992 struct timeval timebuf;
993 struct timeval *tbuf = &timebuf;
996 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1001 # if BYTEORDER & 0xf0000
1002 # define ORDERBYTE (0x88888888 - BYTEORDER)
1004 # define ORDERBYTE (0x4444 - BYTEORDER)
1010 for (i = 1; i <= 3; i++) {
1011 SV * const sv = SP[i];
1014 if (SvREADONLY(sv)) {
1016 sv_force_normal_flags(sv, 0);
1017 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1018 DIE(aTHX_ "%s", PL_no_modify);
1021 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1022 SvPV_force_nolen(sv); /* force string conversion */
1029 /* little endians can use vecs directly */
1030 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1037 masksize = NFDBITS / NBBY;
1039 masksize = sizeof(long); /* documented int, everyone seems to use long */
1041 Zero(&fd_sets[0], 4, char*);
1044 # if SELECT_MIN_BITS == 1
1045 growsize = sizeof(fd_set);
1047 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1048 # undef SELECT_MIN_BITS
1049 # define SELECT_MIN_BITS __FD_SETSIZE
1051 /* If SELECT_MIN_BITS is greater than one we most probably will want
1052 * to align the sizes with SELECT_MIN_BITS/8 because for example
1053 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1054 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1055 * on (sets/tests/clears bits) is 32 bits. */
1056 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1064 timebuf.tv_sec = (long)value;
1065 value -= (NV)timebuf.tv_sec;
1066 timebuf.tv_usec = (long)(value * 1000000.0);
1071 for (i = 1; i <= 3; i++) {
1073 if (!SvOK(sv) || SvCUR(sv) == 0) {
1080 Sv_Grow(sv, growsize);
1084 while (++j <= growsize) {
1088 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1090 Newx(fd_sets[i], growsize, char);
1091 for (offset = 0; offset < growsize; offset += masksize) {
1092 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1093 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1096 fd_sets[i] = SvPVX(sv);
1100 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1101 /* Can't make just the (void*) conditional because that would be
1102 * cpp #if within cpp macro, and not all compilers like that. */
1103 nfound = PerlSock_select(
1105 (Select_fd_set_t) fd_sets[1],
1106 (Select_fd_set_t) fd_sets[2],
1107 (Select_fd_set_t) fd_sets[3],
1108 (void*) tbuf); /* Workaround for compiler bug. */
1110 nfound = PerlSock_select(
1112 (Select_fd_set_t) fd_sets[1],
1113 (Select_fd_set_t) fd_sets[2],
1114 (Select_fd_set_t) fd_sets[3],
1117 for (i = 1; i <= 3; i++) {
1120 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1122 for (offset = 0; offset < growsize; offset += masksize) {
1123 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1124 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1126 Safefree(fd_sets[i]);
1133 if (GIMME == G_ARRAY && tbuf) {
1134 value = (NV)(timebuf.tv_sec) +
1135 (NV)(timebuf.tv_usec) / 1000000.0;
1140 DIE(aTHX_ "select not implemented");
1145 =for apidoc setdefout
1147 Sets PL_defoutgv, the default file handle for output, to the passed in
1148 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1149 count of the passed in typeglob is increased by one, and the reference count
1150 of the typeglob that PL_defoutgv points to is decreased by one.
1156 Perl_setdefout(pTHX_ GV *gv)
1159 SvREFCNT_inc_simple_void(gv);
1161 SvREFCNT_dec(PL_defoutgv);
1169 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1170 GV * egv = GvEGV(PL_defoutgv);
1176 XPUSHs(&PL_sv_undef);
1178 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1179 if (gvp && *gvp == egv) {
1180 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1184 mXPUSHs(newRV(MUTABLE_SV(egv)));
1189 if (!GvIO(newdefout))
1190 gv_IOadd(newdefout);
1191 setdefout(newdefout);
1201 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1203 if (gv && (io = GvIO(gv))) {
1204 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1206 const I32 gimme = GIMME_V;
1208 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
1211 call_method("GETC", gimme);
1214 if (gimme == G_SCALAR)
1215 SvSetMagicSV_nosteal(TARG, TOPs);
1219 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1220 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1221 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1222 report_evil_fh(gv, io, PL_op->op_type);
1223 SETERRNO(EBADF,RMS_IFI);
1227 sv_setpvs(TARG, " ");
1228 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1229 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1230 /* Find out how many bytes the char needs */
1231 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1234 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1235 SvCUR_set(TARG,1+len);
1244 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1247 register PERL_CONTEXT *cx;
1248 const I32 gimme = GIMME_V;
1250 PERL_ARGS_ASSERT_DOFORM;
1255 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1256 PUSHFORMAT(cx, retop);
1258 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1260 setdefout(gv); /* locally select filehandle so $% et al work */
1277 gv = MUTABLE_GV(POPs);
1292 goto not_a_format_reference;
1297 tmpsv = sv_newmortal();
1298 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1299 name = SvPV_nolen_const(tmpsv);
1301 DIE(aTHX_ "Undefined format \"%s\" called", name);
1303 not_a_format_reference:
1304 DIE(aTHX_ "Not a format reference");
1307 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1309 IoFLAGS(io) &= ~IOf_DIDTOP;
1310 return doform(cv,gv,PL_op->op_next);
1316 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1317 register IO * const io = GvIOp(gv);
1322 register PERL_CONTEXT *cx;
1324 if (!io || !(ofp = IoOFP(io)))
1327 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1328 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1330 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1331 PL_formtarget != PL_toptarget)
1335 if (!IoTOP_GV(io)) {
1338 if (!IoTOP_NAME(io)) {
1340 if (!IoFMT_NAME(io))
1341 IoFMT_NAME(io) = savepv(GvNAME(gv));
1342 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1343 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1344 if ((topgv && GvFORM(topgv)) ||
1345 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1346 IoTOP_NAME(io) = savesvpv(topname);
1348 IoTOP_NAME(io) = savepvs("top");
1350 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1351 if (!topgv || !GvFORM(topgv)) {
1352 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1355 IoTOP_GV(io) = topgv;
1357 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1358 I32 lines = IoLINES_LEFT(io);
1359 const char *s = SvPVX_const(PL_formtarget);
1360 if (lines <= 0) /* Yow, header didn't even fit!!! */
1362 while (lines-- > 0) {
1363 s = strchr(s, '\n');
1369 const STRLEN save = SvCUR(PL_formtarget);
1370 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1371 do_print(PL_formtarget, ofp);
1372 SvCUR_set(PL_formtarget, save);
1373 sv_chop(PL_formtarget, s);
1374 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1377 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1378 do_print(PL_formfeed, ofp);
1379 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1381 PL_formtarget = PL_toptarget;
1382 IoFLAGS(io) |= IOf_DIDTOP;
1385 DIE(aTHX_ "bad top format reference");
1388 SV * const sv = sv_newmortal();
1390 gv_efullname4(sv, fgv, NULL, FALSE);
1391 name = SvPV_nolen_const(sv);
1393 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1395 DIE(aTHX_ "Undefined top format called");
1397 if (cv && CvCLONE(cv))
1398 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1399 return doform(cv, gv, PL_op);
1403 POPBLOCK(cx,PL_curpm);
1409 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1411 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1412 else if (ckWARN(WARN_CLOSED))
1413 report_evil_fh(gv, io, PL_op->op_type);
1418 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1419 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1421 if (!do_print(PL_formtarget, fp))
1424 FmLINES(PL_formtarget) = 0;
1425 SvCUR_set(PL_formtarget, 0);
1426 *SvEND(PL_formtarget) = '\0';
1427 if (IoFLAGS(io) & IOf_FLUSH)
1428 (void)PerlIO_flush(fp);
1433 PL_formtarget = PL_bodytarget;
1435 PERL_UNUSED_VAR(newsp);
1436 PERL_UNUSED_VAR(gimme);
1437 return cx->blk_sub.retop;
1442 dVAR; dSP; dMARK; dORIGMARK;
1448 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1450 if (gv && (io = GvIO(gv))) {
1451 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1453 if (MARK == ORIGMARK) {
1456 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1460 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1463 call_method("PRINTF", G_SCALAR);
1466 MARK = ORIGMARK + 1;
1474 if (!(io = GvIO(gv))) {
1475 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1476 report_evil_fh(gv, io, PL_op->op_type);
1477 SETERRNO(EBADF,RMS_IFI);
1480 else if (!(fp = IoOFP(io))) {
1481 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1483 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1484 else if (ckWARN(WARN_CLOSED))
1485 report_evil_fh(gv, io, PL_op->op_type);
1487 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1491 if (SvTAINTED(MARK[1]))
1492 TAINT_PROPER("printf");
1493 do_sprintf(sv, SP - MARK, MARK + 1);
1494 if (!do_print(sv, fp))
1497 if (IoFLAGS(io) & IOf_FLUSH)
1498 if (PerlIO_flush(fp) == EOF)
1509 PUSHs(&PL_sv_undef);
1517 const int perm = (MAXARG > 3) ? POPi : 0666;
1518 const int mode = POPi;
1519 SV * const sv = POPs;
1520 GV * const gv = MUTABLE_GV(POPs);
1523 /* Need TIEHANDLE method ? */
1524 const char * const tmps = SvPV_const(sv, len);
1525 /* FIXME? do_open should do const */
1526 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1527 IoLINES(GvIOp(gv)) = 0;
1531 PUSHs(&PL_sv_undef);
1538 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1544 Sock_size_t bufsize;
1552 bool charstart = FALSE;
1553 STRLEN charskip = 0;
1556 GV * const gv = MUTABLE_GV(*++MARK);
1557 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1558 && gv && (io = GvIO(gv)) )
1560 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1564 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1566 call_method("READ", G_SCALAR);
1580 sv_setpvs(bufsv, "");
1581 length = SvIVx(*++MARK);
1584 offset = SvIVx(*++MARK);
1588 if (!io || !IoIFP(io)) {
1589 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1590 report_evil_fh(gv, io, PL_op->op_type);
1591 SETERRNO(EBADF,RMS_IFI);
1594 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1595 buffer = SvPVutf8_force(bufsv, blen);
1596 /* UTF-8 may not have been set if they are all low bytes */
1601 buffer = SvPV_force(bufsv, blen);
1602 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1605 DIE(aTHX_ "Negative length");
1613 if (PL_op->op_type == OP_RECV) {
1614 char namebuf[MAXPATHLEN];
1615 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1616 bufsize = sizeof (struct sockaddr_in);
1618 bufsize = sizeof namebuf;
1620 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1624 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1625 /* 'offset' means 'flags' here */
1626 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1627 (struct sockaddr *)namebuf, &bufsize);
1631 /* Bogus return without padding */
1632 bufsize = sizeof (struct sockaddr_in);
1634 SvCUR_set(bufsv, count);
1635 *SvEND(bufsv) = '\0';
1636 (void)SvPOK_only(bufsv);
1640 /* This should not be marked tainted if the fp is marked clean */
1641 if (!(IoFLAGS(io) & IOf_UNTAINT))
1642 SvTAINTED_on(bufsv);
1644 sv_setpvn(TARG, namebuf, bufsize);
1649 if (PL_op->op_type == OP_RECV)
1650 DIE(aTHX_ PL_no_sock_func, "recv");
1652 if (DO_UTF8(bufsv)) {
1653 /* offset adjust in characters not bytes */
1654 blen = sv_len_utf8(bufsv);
1657 if (-offset > (int)blen)
1658 DIE(aTHX_ "Offset outside string");
1661 if (DO_UTF8(bufsv)) {
1662 /* convert offset-as-chars to offset-as-bytes */
1663 if (offset >= (int)blen)
1664 offset += SvCUR(bufsv) - blen;
1666 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1669 bufsize = SvCUR(bufsv);
1670 /* Allocating length + offset + 1 isn't perfect in the case of reading
1671 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1673 (should be 2 * length + offset + 1, or possibly something longer if
1674 PL_encoding is true) */
1675 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1676 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1677 Zero(buffer+bufsize, offset-bufsize, char);
1679 buffer = buffer + offset;
1681 read_target = bufsv;
1683 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1684 concatenate it to the current buffer. */
1686 /* Truncate the existing buffer to the start of where we will be
1688 SvCUR_set(bufsv, offset);
1690 read_target = sv_newmortal();
1691 SvUPGRADE(read_target, SVt_PV);
1692 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1695 if (PL_op->op_type == OP_SYSREAD) {
1696 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1697 if (IoTYPE(io) == IoTYPE_SOCKET) {
1698 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1704 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1709 #ifdef HAS_SOCKET__bad_code_maybe
1710 if (IoTYPE(io) == IoTYPE_SOCKET) {
1711 char namebuf[MAXPATHLEN];
1712 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1713 bufsize = sizeof (struct sockaddr_in);
1715 bufsize = sizeof namebuf;
1717 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1718 (struct sockaddr *)namebuf, &bufsize);
1723 count = PerlIO_read(IoIFP(io), buffer, length);
1724 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1725 if (count == 0 && PerlIO_error(IoIFP(io)))
1729 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1730 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1733 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1734 *SvEND(read_target) = '\0';
1735 (void)SvPOK_only(read_target);
1736 if (fp_utf8 && !IN_BYTES) {
1737 /* Look at utf8 we got back and count the characters */
1738 const char *bend = buffer + count;
1739 while (buffer < bend) {
1741 skip = UTF8SKIP(buffer);
1744 if (buffer - charskip + skip > bend) {
1745 /* partial character - try for rest of it */
1746 length = skip - (bend-buffer);
1747 offset = bend - SvPVX_const(bufsv);
1759 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1760 provided amount read (count) was what was requested (length)
1762 if (got < wanted && count == length) {
1763 length = wanted - got;
1764 offset = bend - SvPVX_const(bufsv);
1767 /* return value is character count */
1771 else if (buffer_utf8) {
1772 /* Let svcatsv upgrade the bytes we read in to utf8.
1773 The buffer is a mortal so will be freed soon. */
1774 sv_catsv_nomg(bufsv, read_target);
1777 /* This should not be marked tainted if the fp is marked clean */
1778 if (!(IoFLAGS(io) & IOf_UNTAINT))
1779 SvTAINTED_on(bufsv);
1791 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1797 STRLEN orig_blen_bytes;
1798 const int op_type = PL_op->op_type;
1802 GV *const gv = MUTABLE_GV(*++MARK);
1803 if (PL_op->op_type == OP_SYSWRITE
1804 && gv && (io = GvIO(gv))) {
1805 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1809 if (MARK == SP - 1) {
1811 mXPUSHi(sv_len(sv));
1816 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1818 call_method("WRITE", G_SCALAR);
1834 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1836 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1837 if (io && IoIFP(io))
1838 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1840 report_evil_fh(gv, io, PL_op->op_type);
1842 SETERRNO(EBADF,RMS_IFI);
1846 /* Do this first to trigger any overloading. */
1847 buffer = SvPV_const(bufsv, blen);
1848 orig_blen_bytes = blen;
1849 doing_utf8 = DO_UTF8(bufsv);
1851 if (PerlIO_isutf8(IoIFP(io))) {
1852 if (!SvUTF8(bufsv)) {
1853 /* We don't modify the original scalar. */
1854 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1855 buffer = (char *) tmpbuf;
1859 else if (doing_utf8) {
1860 STRLEN tmplen = blen;
1861 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1864 buffer = (char *) tmpbuf;
1868 assert((char *)result == buffer);
1869 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1873 if (op_type == OP_SYSWRITE) {
1874 Size_t length = 0; /* This length is in characters. */
1880 /* The SV is bytes, and we've had to upgrade it. */
1881 blen_chars = orig_blen_bytes;
1883 /* The SV really is UTF-8. */
1884 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1885 /* Don't call sv_len_utf8 again because it will call magic
1886 or overloading a second time, and we might get back a
1887 different result. */
1888 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1890 /* It's safe, and it may well be cached. */
1891 blen_chars = sv_len_utf8(bufsv);
1899 length = blen_chars;
1901 #if Size_t_size > IVSIZE
1902 length = (Size_t)SvNVx(*++MARK);
1904 length = (Size_t)SvIVx(*++MARK);
1906 if ((SSize_t)length < 0) {
1908 DIE(aTHX_ "Negative length");
1913 offset = SvIVx(*++MARK);
1915 if (-offset > (IV)blen_chars) {
1917 DIE(aTHX_ "Offset outside string");
1919 offset += blen_chars;
1920 } else if (offset >= (IV)blen_chars) {
1922 DIE(aTHX_ "Offset outside string");
1926 if (length > blen_chars - offset)
1927 length = blen_chars - offset;
1929 /* Here we convert length from characters to bytes. */
1930 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1931 /* Either we had to convert the SV, or the SV is magical, or
1932 the SV has overloading, in which case we can't or mustn't
1933 or mustn't call it again. */
1935 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1936 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1938 /* It's a real UTF-8 SV, and it's not going to change under
1939 us. Take advantage of any cache. */
1941 I32 len_I32 = length;
1943 /* Convert the start and end character positions to bytes.
1944 Remember that the second argument to sv_pos_u2b is relative
1946 sv_pos_u2b(bufsv, &start, &len_I32);
1953 buffer = buffer+offset;
1955 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1956 if (IoTYPE(io) == IoTYPE_SOCKET) {
1957 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1963 /* See the note at doio.c:do_print about filesize limits. --jhi */
1964 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1970 const int flags = SvIVx(*++MARK);
1973 char * const sockbuf = SvPVx(*++MARK, mlen);
1974 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1975 flags, (struct sockaddr *)sockbuf, mlen);
1979 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1984 DIE(aTHX_ PL_no_sock_func, "send");
1991 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1994 #if Size_t_size > IVSIZE
2015 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2016 else if (PL_op->op_flags & OPf_SPECIAL)
2017 gv = PL_last_in_gv = GvEGV(PL_argvgv); /* eof() - ARGV magic */
2019 gv = PL_last_in_gv; /* eof */
2024 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2026 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2028 * in Perl 5.12 and later, the additional paramter is a bitmask:
2031 * 2 = eof() <- ARGV magic
2034 mPUSHi(1); /* 1 = eof(FH) - simple, explicit FH */
2035 else if (PL_op->op_flags & OPf_SPECIAL)
2036 mPUSHi(2); /* 2 = eof() - ARGV magic */
2038 mPUSHi(0); /* 0 = eof - simple, implicit FH */
2041 call_method("EOF", G_SCALAR);
2047 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2048 if (io && !IoIFP(io)) {
2049 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2051 IoFLAGS(io) &= ~IOf_START;
2052 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2054 sv_setpvs(GvSV(gv), "-");
2056 GvSV(gv) = newSVpvs("-");
2057 SvSETMAGIC(GvSV(gv));
2059 else if (!nextargv(gv))
2064 PUSHs(boolSV(do_eof(gv)));
2075 PL_last_in_gv = MUTABLE_GV(POPs);
2078 if (gv && (io = GvIO(gv))) {
2079 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2082 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2085 call_method("TELL", G_SCALAR);
2093 SETERRNO(EBADF,RMS_IFI);
2098 #if LSEEKSIZE > IVSIZE
2099 PUSHn( do_tell(gv) );
2101 PUSHi( do_tell(gv) );
2109 const int whence = POPi;
2110 #if LSEEKSIZE > IVSIZE
2111 const Off_t offset = (Off_t)SvNVx(POPs);
2113 const Off_t offset = (Off_t)SvIVx(POPs);
2116 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2119 if (gv && (io = GvIO(gv))) {
2120 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2123 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2124 #if LSEEKSIZE > IVSIZE
2125 mXPUSHn((NV) offset);
2132 call_method("SEEK", G_SCALAR);
2139 if (PL_op->op_type == OP_SEEK)
2140 PUSHs(boolSV(do_seek(gv, offset, whence)));
2142 const Off_t sought = do_sysseek(gv, offset, whence);
2144 PUSHs(&PL_sv_undef);
2146 SV* const sv = sought ?
2147 #if LSEEKSIZE > IVSIZE
2152 : newSVpvn(zero_but_true, ZBTLEN);
2163 /* There seems to be no consensus on the length type of truncate()
2164 * and ftruncate(), both off_t and size_t have supporters. In
2165 * general one would think that when using large files, off_t is
2166 * at least as wide as size_t, so using an off_t should be okay. */
2167 /* XXX Configure probe for the length type of *truncate() needed XXX */
2170 #if Off_t_size > IVSIZE
2175 /* Checking for length < 0 is problematic as the type might or
2176 * might not be signed: if it is not, clever compilers will moan. */
2177 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2184 if (PL_op->op_flags & OPf_SPECIAL) {
2185 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2194 TAINT_PROPER("truncate");
2195 if (!(fp = IoIFP(io))) {
2201 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2203 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2210 SV * const sv = POPs;
2213 if (isGV_with_GP(sv)) {
2214 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2215 goto do_ftruncate_gv;
2217 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2218 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2219 goto do_ftruncate_gv;
2221 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2222 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2223 goto do_ftruncate_io;
2226 name = SvPV_nolen_const(sv);
2227 TAINT_PROPER("truncate");
2229 if (truncate(name, len) < 0)
2233 const int tmpfd = PerlLIO_open(name, O_RDWR);
2238 if (my_chsize(tmpfd, len) < 0)
2240 PerlLIO_close(tmpfd);
2249 SETERRNO(EBADF,RMS_IFI);
2257 SV * const argsv = POPs;
2258 const unsigned int func = POPu;
2259 const int optype = PL_op->op_type;
2260 GV * const gv = MUTABLE_GV(POPs);
2261 IO * const io = gv ? GvIOn(gv) : NULL;
2265 if (!io || !argsv || !IoIFP(io)) {
2266 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2267 report_evil_fh(gv, io, PL_op->op_type);
2268 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2272 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2275 s = SvPV_force(argsv, len);
2276 need = IOCPARM_LEN(func);
2278 s = Sv_Grow(argsv, need + 1);
2279 SvCUR_set(argsv, need);
2282 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2285 retval = SvIV(argsv);
2286 s = INT2PTR(char*,retval); /* ouch */
2289 TAINT_PROPER(PL_op_desc[optype]);
2291 if (optype == OP_IOCTL)
2293 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2295 DIE(aTHX_ "ioctl is not implemented");
2299 DIE(aTHX_ "fcntl is not implemented");
2301 #if defined(OS2) && defined(__EMX__)
2302 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2304 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2308 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2310 if (s[SvCUR(argsv)] != 17)
2311 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2313 s[SvCUR(argsv)] = 0; /* put our null back */
2314 SvSETMAGIC(argsv); /* Assume it has changed */
2323 PUSHp(zero_but_true, ZBTLEN);
2336 const int argtype = POPi;
2337 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2339 if (gv && (io = GvIO(gv)))
2345 /* XXX Looks to me like io is always NULL at this point */
2347 (void)PerlIO_flush(fp);
2348 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2351 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2352 report_evil_fh(gv, io, PL_op->op_type);
2354 SETERRNO(EBADF,RMS_IFI);
2359 DIE(aTHX_ PL_no_func, "flock()");
2369 const int protocol = POPi;
2370 const int type = POPi;
2371 const int domain = POPi;
2372 GV * const gv = MUTABLE_GV(POPs);
2373 register IO * const io = gv ? GvIOn(gv) : NULL;
2377 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2378 report_evil_fh(gv, io, PL_op->op_type);
2379 if (io && IoIFP(io))
2380 do_close(gv, FALSE);
2381 SETERRNO(EBADF,LIB_INVARG);
2386 do_close(gv, FALSE);
2388 TAINT_PROPER("socket");
2389 fd = PerlSock_socket(domain, type, protocol);
2392 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2393 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2394 IoTYPE(io) = IoTYPE_SOCKET;
2395 if (!IoIFP(io) || !IoOFP(io)) {
2396 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2397 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2398 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2401 #if defined(HAS_FCNTL) && defined(F_SETFD)
2402 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2406 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2411 DIE(aTHX_ PL_no_sock_func, "socket");
2417 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2419 const int protocol = POPi;
2420 const int type = POPi;
2421 const int domain = POPi;
2422 GV * const gv2 = MUTABLE_GV(POPs);
2423 GV * const gv1 = MUTABLE_GV(POPs);
2424 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2425 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2428 if (!gv1 || !gv2 || !io1 || !io2) {
2429 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2431 report_evil_fh(gv1, io1, PL_op->op_type);
2433 report_evil_fh(gv1, io2, PL_op->op_type);
2435 if (io1 && IoIFP(io1))
2436 do_close(gv1, FALSE);
2437 if (io2 && IoIFP(io2))
2438 do_close(gv2, FALSE);
2443 do_close(gv1, FALSE);
2445 do_close(gv2, FALSE);
2447 TAINT_PROPER("socketpair");
2448 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2450 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2451 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2452 IoTYPE(io1) = IoTYPE_SOCKET;
2453 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2454 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2455 IoTYPE(io2) = IoTYPE_SOCKET;
2456 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2457 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2458 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2459 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2460 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2461 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2462 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2465 #if defined(HAS_FCNTL) && defined(F_SETFD)
2466 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2467 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2472 DIE(aTHX_ PL_no_sock_func, "socketpair");
2480 SV * const addrsv = POPs;
2481 /* OK, so on what platform does bind modify addr? */
2483 GV * const gv = MUTABLE_GV(POPs);
2484 register IO * const io = GvIOn(gv);
2487 if (!io || !IoIFP(io))
2490 addr = SvPV_const(addrsv, len);
2491 TAINT_PROPER("bind");
2492 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2498 if (ckWARN(WARN_CLOSED))
2499 report_evil_fh(gv, io, PL_op->op_type);
2500 SETERRNO(EBADF,SS_IVCHAN);
2503 DIE(aTHX_ PL_no_sock_func, "bind");
2511 SV * const addrsv = POPs;
2512 GV * const gv = MUTABLE_GV(POPs);
2513 register IO * const io = GvIOn(gv);
2517 if (!io || !IoIFP(io))
2520 addr = SvPV_const(addrsv, len);
2521 TAINT_PROPER("connect");
2522 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2528 if (ckWARN(WARN_CLOSED))
2529 report_evil_fh(gv, io, PL_op->op_type);
2530 SETERRNO(EBADF,SS_IVCHAN);
2533 DIE(aTHX_ PL_no_sock_func, "connect");
2541 const int backlog = POPi;
2542 GV * const gv = MUTABLE_GV(POPs);
2543 register IO * const io = gv ? GvIOn(gv) : NULL;
2545 if (!gv || !io || !IoIFP(io))
2548 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2554 if (ckWARN(WARN_CLOSED))
2555 report_evil_fh(gv, io, PL_op->op_type);
2556 SETERRNO(EBADF,SS_IVCHAN);
2559 DIE(aTHX_ PL_no_sock_func, "listen");
2569 char namebuf[MAXPATHLEN];
2570 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2571 Sock_size_t len = sizeof (struct sockaddr_in);
2573 Sock_size_t len = sizeof namebuf;
2575 GV * const ggv = MUTABLE_GV(POPs);
2576 GV * const ngv = MUTABLE_GV(POPs);
2585 if (!gstio || !IoIFP(gstio))
2589 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2592 /* Some platforms indicate zero length when an AF_UNIX client is
2593 * not bound. Simulate a non-zero-length sockaddr structure in
2595 namebuf[0] = 0; /* sun_len */
2596 namebuf[1] = AF_UNIX; /* sun_family */
2604 do_close(ngv, FALSE);
2605 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2606 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2607 IoTYPE(nstio) = IoTYPE_SOCKET;
2608 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2609 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2610 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2611 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2614 #if defined(HAS_FCNTL) && defined(F_SETFD)
2615 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2619 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2620 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2622 #ifdef __SCO_VERSION__
2623 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2626 PUSHp(namebuf, len);
2630 if (ckWARN(WARN_CLOSED))
2631 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2632 SETERRNO(EBADF,SS_IVCHAN);
2638 DIE(aTHX_ PL_no_sock_func, "accept");
2646 const int how = POPi;
2647 GV * const gv = MUTABLE_GV(POPs);
2648 register IO * const io = GvIOn(gv);
2650 if (!io || !IoIFP(io))
2653 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2657 if (ckWARN(WARN_CLOSED))
2658 report_evil_fh(gv, io, PL_op->op_type);
2659 SETERRNO(EBADF,SS_IVCHAN);
2662 DIE(aTHX_ PL_no_sock_func, "shutdown");
2670 const int optype = PL_op->op_type;
2671 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2672 const unsigned int optname = (unsigned int) POPi;
2673 const unsigned int lvl = (unsigned int) POPi;
2674 GV * const gv = MUTABLE_GV(POPs);
2675 register IO * const io = GvIOn(gv);
2679 if (!io || !IoIFP(io))
2682 fd = PerlIO_fileno(IoIFP(io));
2686 (void)SvPOK_only(sv);
2690 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2697 #if defined(__SYMBIAN32__)
2698 # define SETSOCKOPT_OPTION_VALUE_T void *
2700 # define SETSOCKOPT_OPTION_VALUE_T const char *
2702 /* XXX TODO: We need to have a proper type (a Configure probe,
2703 * etc.) for what the C headers think of the third argument of
2704 * setsockopt(), the option_value read-only buffer: is it
2705 * a "char *", or a "void *", const or not. Some compilers
2706 * don't take kindly to e.g. assuming that "char *" implicitly
2707 * promotes to a "void *", or to explicitly promoting/demoting
2708 * consts to non/vice versa. The "const void *" is the SUS
2709 * definition, but that does not fly everywhere for the above
2711 SETSOCKOPT_OPTION_VALUE_T buf;
2715 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2719 aint = (int)SvIV(sv);
2720 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2723 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2732 if (ckWARN(WARN_CLOSED))
2733 report_evil_fh(gv, io, optype);
2734 SETERRNO(EBADF,SS_IVCHAN);
2739 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2747 const int optype = PL_op->op_type;
2748 GV * const gv = MUTABLE_GV(POPs);
2749 register IO * const io = GvIOn(gv);
2754 if (!io || !IoIFP(io))
2757 sv = sv_2mortal(newSV(257));
2758 (void)SvPOK_only(sv);
2762 fd = PerlIO_fileno(IoIFP(io));
2764 case OP_GETSOCKNAME:
2765 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2768 case OP_GETPEERNAME:
2769 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2771 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2773 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";
2774 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2775 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2776 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2777 sizeof(u_short) + sizeof(struct in_addr))) {
2784 #ifdef BOGUS_GETNAME_RETURN
2785 /* Interactive Unix, getpeername() and getsockname()
2786 does not return valid namelen */
2787 if (len == BOGUS_GETNAME_RETURN)
2788 len = sizeof(struct sockaddr);
2796 if (ckWARN(WARN_CLOSED))
2797 report_evil_fh(gv, io, optype);
2798 SETERRNO(EBADF,SS_IVCHAN);
2803 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2818 if (PL_op->op_flags & OPf_REF) {
2820 if (PL_op->op_type == OP_LSTAT) {
2821 if (gv != PL_defgv) {
2822 do_fstat_warning_check:
2823 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2824 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2825 } else if (PL_laststype != OP_LSTAT)
2826 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2830 if (gv != PL_defgv) {
2831 PL_laststype = OP_STAT;
2833 sv_setpvs(PL_statname, "");
2840 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2841 } else if (IoDIRP(io)) {
2843 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2845 PL_laststatval = -1;
2851 if (PL_laststatval < 0) {
2852 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2853 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2858 SV* const sv = POPs;
2859 if (isGV_with_GP(sv)) {
2860 gv = MUTABLE_GV(sv);
2862 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2863 gv = MUTABLE_GV(SvRV(sv));
2864 if (PL_op->op_type == OP_LSTAT)
2865 goto do_fstat_warning_check;
2867 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2868 io = MUTABLE_IO(SvRV(sv));
2869 if (PL_op->op_type == OP_LSTAT)
2870 goto do_fstat_warning_check;
2871 goto do_fstat_have_io;
2874 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2876 PL_laststype = PL_op->op_type;
2877 if (PL_op->op_type == OP_LSTAT)
2878 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2880 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2881 if (PL_laststatval < 0) {
2882 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2883 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2889 if (gimme != G_ARRAY) {
2890 if (gimme != G_VOID)
2891 XPUSHs(boolSV(max));
2897 mPUSHi(PL_statcache.st_dev);
2898 mPUSHi(PL_statcache.st_ino);
2899 mPUSHu(PL_statcache.st_mode);
2900 mPUSHu(PL_statcache.st_nlink);
2901 #if Uid_t_size > IVSIZE
2902 mPUSHn(PL_statcache.st_uid);
2904 # if Uid_t_sign <= 0
2905 mPUSHi(PL_statcache.st_uid);
2907 mPUSHu(PL_statcache.st_uid);
2910 #if Gid_t_size > IVSIZE
2911 mPUSHn(PL_statcache.st_gid);
2913 # if Gid_t_sign <= 0
2914 mPUSHi(PL_statcache.st_gid);
2916 mPUSHu(PL_statcache.st_gid);
2919 #ifdef USE_STAT_RDEV
2920 mPUSHi(PL_statcache.st_rdev);
2922 PUSHs(newSVpvs_flags("", SVs_TEMP));
2924 #if Off_t_size > IVSIZE
2925 mPUSHn(PL_statcache.st_size);
2927 mPUSHi(PL_statcache.st_size);
2930 mPUSHn(PL_statcache.st_atime);
2931 mPUSHn(PL_statcache.st_mtime);
2932 mPUSHn(PL_statcache.st_ctime);
2934 mPUSHi(PL_statcache.st_atime);
2935 mPUSHi(PL_statcache.st_mtime);
2936 mPUSHi(PL_statcache.st_ctime);
2938 #ifdef USE_STAT_BLOCKS
2939 mPUSHu(PL_statcache.st_blksize);
2940 mPUSHu(PL_statcache.st_blocks);
2942 PUSHs(newSVpvs_flags("", SVs_TEMP));
2943 PUSHs(newSVpvs_flags("", SVs_TEMP));
2949 /* This macro is used by the stacked filetest operators :
2950 * if the previous filetest failed, short-circuit and pass its value.
2951 * Else, discard it from the stack and continue. --rgs
2953 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2954 if (!SvTRUE(TOPs)) { RETURN; } \
2955 else { (void)POPs; PUTBACK; } \
2962 /* Not const, because things tweak this below. Not bool, because there's
2963 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2964 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2965 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2966 /* Giving some sort of initial value silences compilers. */
2968 int access_mode = R_OK;
2970 int access_mode = 0;
2973 /* access_mode is never used, but leaving use_access in makes the
2974 conditional compiling below much clearer. */
2977 int stat_mode = S_IRUSR;
2979 bool effective = FALSE;
2983 switch (PL_op->op_type) {
2984 case OP_FTRREAD: opchar = 'R'; break;
2985 case OP_FTRWRITE: opchar = 'W'; break;
2986 case OP_FTREXEC: opchar = 'X'; break;
2987 case OP_FTEREAD: opchar = 'r'; break;
2988 case OP_FTEWRITE: opchar = 'w'; break;
2989 case OP_FTEEXEC: opchar = 'x'; break;
2991 tryAMAGICftest(opchar);
2993 STACKED_FTEST_CHECK;
2995 switch (PL_op->op_type) {
2997 #if !(defined(HAS_ACCESS) && defined(R_OK))
3003 #if defined(HAS_ACCESS) && defined(W_OK)
3008 stat_mode = S_IWUSR;
3012 #if defined(HAS_ACCESS) && defined(X_OK)
3017 stat_mode = S_IXUSR;
3021 #ifdef PERL_EFF_ACCESS
3024 stat_mode = S_IWUSR;
3028 #ifndef PERL_EFF_ACCESS
3035 #ifdef PERL_EFF_ACCESS
3040 stat_mode = S_IXUSR;
3046 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3047 const char *name = POPpx;
3049 # ifdef PERL_EFF_ACCESS
3050 result = PERL_EFF_ACCESS(name, access_mode);
3052 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3058 result = access(name, access_mode);
3060 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3075 if (cando(stat_mode, effective, &PL_statcache))
3084 const int op_type = PL_op->op_type;
3089 case OP_FTIS: opchar = 'e'; break;
3090 case OP_FTSIZE: opchar = 's'; break;
3091 case OP_FTMTIME: opchar = 'M'; break;
3092 case OP_FTCTIME: opchar = 'C'; break;
3093 case OP_FTATIME: opchar = 'A'; break;
3095 tryAMAGICftest(opchar);
3097 STACKED_FTEST_CHECK;
3103 if (op_type == OP_FTIS)
3106 /* You can't dTARGET inside OP_FTIS, because you'll get
3107 "panic: pad_sv po" - the op is not flagged to have a target. */
3111 #if Off_t_size > IVSIZE
3112 PUSHn(PL_statcache.st_size);
3114 PUSHi(PL_statcache.st_size);
3118 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3121 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3124 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3138 switch (PL_op->op_type) {
3139 case OP_FTROWNED: opchar = 'O'; break;
3140 case OP_FTEOWNED: opchar = 'o'; break;
3141 case OP_FTZERO: opchar = 'z'; break;
3142 case OP_FTSOCK: opchar = 'S'; break;
3143 case OP_FTCHR: opchar = 'c'; break;
3144 case OP_FTBLK: opchar = 'b'; break;
3145 case OP_FTFILE: opchar = 'f'; break;
3146 case OP_FTDIR: opchar = 'd'; break;
3147 case OP_FTPIPE: opchar = 'p'; break;
3148 case OP_FTSUID: opchar = 'u'; break;
3149 case OP_FTSGID: opchar = 'g'; break;
3150 case OP_FTSVTX: opchar = 'k'; break;
3152 tryAMAGICftest(opchar);
3154 /* I believe that all these three are likely to be defined on most every
3155 system these days. */
3157 if(PL_op->op_type == OP_FTSUID)
3161 if(PL_op->op_type == OP_FTSGID)
3165 if(PL_op->op_type == OP_FTSVTX)
3169 STACKED_FTEST_CHECK;
3175 switch (PL_op->op_type) {
3177 if (PL_statcache.st_uid == PL_uid)
3181 if (PL_statcache.st_uid == PL_euid)
3185 if (PL_statcache.st_size == 0)
3189 if (S_ISSOCK(PL_statcache.st_mode))
3193 if (S_ISCHR(PL_statcache.st_mode))
3197 if (S_ISBLK(PL_statcache.st_mode))
3201 if (S_ISREG(PL_statcache.st_mode))
3205 if (S_ISDIR(PL_statcache.st_mode))
3209 if (S_ISFIFO(PL_statcache.st_mode))
3214 if (PL_statcache.st_mode & S_ISUID)
3220 if (PL_statcache.st_mode & S_ISGID)
3226 if (PL_statcache.st_mode & S_ISVTX)
3240 tryAMAGICftest('l');
3241 result = my_lstat();
3246 if (S_ISLNK(PL_statcache.st_mode))
3259 tryAMAGICftest('t');
3261 STACKED_FTEST_CHECK;
3263 if (PL_op->op_flags & OPf_REF)
3265 else if (isGV(TOPs))
3266 gv = MUTABLE_GV(POPs);
3267 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3268 gv = MUTABLE_GV(SvRV(POPs));
3270 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3272 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3273 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3274 else if (tmpsv && SvOK(tmpsv)) {
3275 const char *tmps = SvPV_nolen_const(tmpsv);
3283 if (PerlLIO_isatty(fd))
3288 #if defined(atarist) /* this will work with atariST. Configure will
3289 make guesses for other systems. */
3290 # define FILE_base(f) ((f)->_base)
3291 # define FILE_ptr(f) ((f)->_ptr)
3292 # define FILE_cnt(f) ((f)->_cnt)
3293 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3304 register STDCHAR *s;
3310 tryAMAGICftest(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3312 STACKED_FTEST_CHECK;
3314 if (PL_op->op_flags & OPf_REF)
3316 else if (isGV(TOPs))
3317 gv = MUTABLE_GV(POPs);
3318 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3319 gv = MUTABLE_GV(SvRV(POPs));
3325 if (gv == PL_defgv) {
3327 io = GvIO(PL_statgv);
3330 goto really_filename;
3335 PL_laststatval = -1;
3336 sv_setpvs(PL_statname, "");
3337 io = GvIO(PL_statgv);
3339 if (io && IoIFP(io)) {
3340 if (! PerlIO_has_base(IoIFP(io)))
3341 DIE(aTHX_ "-T and -B not implemented on filehandles");
3342 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3343 if (PL_laststatval < 0)
3345 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3346 if (PL_op->op_type == OP_FTTEXT)
3351 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3352 i = PerlIO_getc(IoIFP(io));
3354 (void)PerlIO_ungetc(IoIFP(io),i);
3356 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3358 len = PerlIO_get_bufsiz(IoIFP(io));
3359 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3360 /* sfio can have large buffers - limit to 512 */
3365 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3367 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3369 SETERRNO(EBADF,RMS_IFI);
3377 PL_laststype = OP_STAT;
3378 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3379 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3380 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3382 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3385 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3386 if (PL_laststatval < 0) {
3387 (void)PerlIO_close(fp);
3390 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3391 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3392 (void)PerlIO_close(fp);
3394 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3395 RETPUSHNO; /* special case NFS directories */
3396 RETPUSHYES; /* null file is anything */
3401 /* now scan s to look for textiness */
3402 /* XXX ASCII dependent code */
3404 #if defined(DOSISH) || defined(USEMYBINMODE)
3405 /* ignore trailing ^Z on short files */
3406 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3410 for (i = 0; i < len; i++, s++) {
3411 if (!*s) { /* null never allowed in text */
3416 else if (!(isPRINT(*s) || isSPACE(*s)))
3419 else if (*s & 128) {
3421 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3424 /* utf8 characters don't count as odd */
3425 if (UTF8_IS_START(*s)) {
3426 int ulen = UTF8SKIP(s);
3427 if (ulen < len - i) {
3429 for (j = 1; j < ulen; j++) {
3430 if (!UTF8_IS_CONTINUATION(s[j]))
3433 --ulen; /* loop does extra increment */
3443 *s != '\n' && *s != '\r' && *s != '\b' &&
3444 *s != '\t' && *s != '\f' && *s != 27)
3449 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3460 const char *tmps = NULL;
3464 SV * const sv = POPs;
3465 if (PL_op->op_flags & OPf_SPECIAL) {
3466 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3468 else if (isGV_with_GP(sv)) {
3469 gv = MUTABLE_GV(sv);
3471 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3472 gv = MUTABLE_GV(SvRV(sv));
3475 tmps = SvPV_nolen_const(sv);
3479 if( !gv && (!tmps || !*tmps) ) {
3480 HV * const table = GvHVn(PL_envgv);
3483 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3484 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3486 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3491 deprecate("chdir('') or chdir(undef) as chdir()");
3492 tmps = SvPV_nolen_const(*svp);
3496 TAINT_PROPER("chdir");
3501 TAINT_PROPER("chdir");
3504 IO* const io = GvIO(gv);
3507 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3508 } else if (IoIFP(io)) {
3509 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3512 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3513 report_evil_fh(gv, io, PL_op->op_type);
3514 SETERRNO(EBADF, RMS_IFI);
3519 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3520 report_evil_fh(gv, io, PL_op->op_type);
3521 SETERRNO(EBADF,RMS_IFI);
3525 DIE(aTHX_ PL_no_func, "fchdir");
3529 PUSHi( PerlDir_chdir(tmps) >= 0 );
3531 /* Clear the DEFAULT element of ENV so we'll get the new value
3533 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3540 dVAR; dSP; dMARK; dTARGET;
3541 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3552 char * const tmps = POPpx;
3553 TAINT_PROPER("chroot");
3554 PUSHi( chroot(tmps) >= 0 );
3557 DIE(aTHX_ PL_no_func, "chroot");
3565 const char * const tmps2 = POPpconstx;
3566 const char * const tmps = SvPV_nolen_const(TOPs);
3567 TAINT_PROPER("rename");
3569 anum = PerlLIO_rename(tmps, tmps2);
3571 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3572 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3575 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3576 (void)UNLINK(tmps2);
3577 if (!(anum = link(tmps, tmps2)))
3578 anum = UNLINK(tmps);
3586 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3590 const int op_type = PL_op->op_type;
3594 if (op_type == OP_LINK)
3595 DIE(aTHX_ PL_no_func, "link");
3597 # ifndef HAS_SYMLINK
3598 if (op_type == OP_SYMLINK)
3599 DIE(aTHX_ PL_no_func, "symlink");
3603 const char * const tmps2 = POPpconstx;
3604 const char * const tmps = SvPV_nolen_const(TOPs);
3605 TAINT_PROPER(PL_op_desc[op_type]);
3607 # if defined(HAS_LINK)
3608 # if defined(HAS_SYMLINK)
3609 /* Both present - need to choose which. */
3610 (op_type == OP_LINK) ?
3611 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3613 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3614 PerlLIO_link(tmps, tmps2);
3617 # if defined(HAS_SYMLINK)
3618 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3619 symlink(tmps, tmps2);
3624 SETi( result >= 0 );
3631 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3642 char buf[MAXPATHLEN];
3645 #ifndef INCOMPLETE_TAINTS
3649 len = readlink(tmps, buf, sizeof(buf) - 1);
3657 RETSETUNDEF; /* just pretend it's a normal file */
3661 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3663 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3665 char * const save_filename = filename;
3670 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3672 PERL_ARGS_ASSERT_DOONELINER;
3674 Newx(cmdline, size, char);
3675 my_strlcpy(cmdline, cmd, size);
3676 my_strlcat(cmdline, " ", size);
3677 for (s = cmdline + strlen(cmdline); *filename; ) {
3681 if (s - cmdline < size)
3682 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3683 myfp = PerlProc_popen(cmdline, "r");
3687 SV * const tmpsv = sv_newmortal();
3688 /* Need to save/restore 'PL_rs' ?? */
3689 s = sv_gets(tmpsv, myfp, 0);
3690 (void)PerlProc_pclose(myfp);
3694 #ifdef HAS_SYS_ERRLIST
3699 /* you don't see this */
3700 const char * const errmsg =
3701 #ifdef HAS_SYS_ERRLIST
3709 if (instr(s, errmsg)) {
3716 #define EACCES EPERM
3718 if (instr(s, "cannot make"))
3719 SETERRNO(EEXIST,RMS_FEX);
3720 else if (instr(s, "existing file"))
3721 SETERRNO(EEXIST,RMS_FEX);
3722 else if (instr(s, "ile exists"))
3723 SETERRNO(EEXIST,RMS_FEX);
3724 else if (instr(s, "non-exist"))
3725 SETERRNO(ENOENT,RMS_FNF);
3726 else if (instr(s, "does not exist"))
3727 SETERRNO(ENOENT,RMS_FNF);
3728 else if (instr(s, "not empty"))
3729 SETERRNO(EBUSY,SS_DEVOFFLINE);
3730 else if (instr(s, "cannot access"))
3731 SETERRNO(EACCES,RMS_PRV);
3733 SETERRNO(EPERM,RMS_PRV);
3736 else { /* some mkdirs return no failure indication */
3737 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3738 if (PL_op->op_type == OP_RMDIR)
3743 SETERRNO(EACCES,RMS_PRV); /* a guess */
3752 /* This macro removes trailing slashes from a directory name.
3753 * Different operating and file systems take differently to
3754 * trailing slashes. According to POSIX 1003.1 1996 Edition
3755 * any number of trailing slashes should be allowed.
3756 * Thusly we snip them away so that even non-conforming
3757 * systems are happy.
3758 * We should probably do this "filtering" for all
3759 * the functions that expect (potentially) directory names:
3760 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3761 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3763 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3764 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3767 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3768 (tmps) = savepvn((tmps), (len)); \
3778 const int mode = (MAXARG > 1) ? POPi : 0777;
3780 TRIMSLASHES(tmps,len,copy);
3782 TAINT_PROPER("mkdir");
3784 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3788 SETi( dooneliner("mkdir", tmps) );
3789 oldumask = PerlLIO_umask(0);
3790 PerlLIO_umask(oldumask);
3791 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3806 TRIMSLASHES(tmps,len,copy);
3807 TAINT_PROPER("rmdir");
3809 SETi( PerlDir_rmdir(tmps) >= 0 );
3811 SETi( dooneliner("rmdir", tmps) );
3818 /* Directory calls. */
3822 #if defined(Direntry_t) && defined(HAS_READDIR)
3824 const char * const dirname = POPpconstx;
3825 GV * const gv = MUTABLE_GV(POPs);
3826 register IO * const io = GvIOn(gv);
3831 if ((IoIFP(io) || IoOFP(io)))
3832 Perl_ck_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3833 "Opening filehandle %s also as a directory", GvENAME(gv));
3835 PerlDir_close(IoDIRP(io));
3836 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3842 SETERRNO(EBADF,RMS_DIR);
3845 DIE(aTHX_ PL_no_dir_func, "opendir");
3851 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3852 DIE(aTHX_ PL_no_dir_func, "readdir");
3854 #if !defined(I_DIRENT) && !defined(VMS)
3855 Direntry_t *readdir (DIR *);
3861 const I32 gimme = GIMME;
3862 GV * const gv = MUTABLE_GV(POPs);
3863 register const Direntry_t *dp;
3864 register IO * const io = GvIOn(gv);
3866 if (!io || !IoDIRP(io)) {
3867 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3868 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3873 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3877 sv = newSVpvn(dp->d_name, dp->d_namlen);
3879 sv = newSVpv(dp->d_name, 0);
3881 #ifndef INCOMPLETE_TAINTS
3882 if (!(IoFLAGS(io) & IOf_UNTAINT))
3886 } while (gimme == G_ARRAY);
3888 if (!dp && gimme != G_ARRAY)
3895 SETERRNO(EBADF,RMS_ISI);
3896 if (GIMME == G_ARRAY)
3905 #if defined(HAS_TELLDIR) || defined(telldir)
3907 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3908 /* XXX netbsd still seemed to.
3909 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3910 --JHI 1999-Feb-02 */
3911 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3912 long telldir (DIR *);
3914 GV * const gv = MUTABLE_GV(POPs);
3915 register IO * const io = GvIOn(gv);
3917 if (!io || !IoDIRP(io)) {
3918 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3919 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3923 PUSHi( PerlDir_tell(IoDIRP(io)) );
3927 SETERRNO(EBADF,RMS_ISI);
3930 DIE(aTHX_ PL_no_dir_func, "telldir");
3936 #if defined(HAS_SEEKDIR) || defined(seekdir)
3938 const long along = POPl;
3939 GV * const gv = MUTABLE_GV(POPs);
3940 register IO * const io = GvIOn(gv);
3942 if (!io || !IoDIRP(io)) {
3943 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3944 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3947 (void)PerlDir_seek(IoDIRP(io), along);
3952 SETERRNO(EBADF,RMS_ISI);
3955 DIE(aTHX_ PL_no_dir_func, "seekdir");
3961 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3963 GV * const gv = MUTABLE_GV(POPs);
3964 register IO * const io = GvIOn(gv);
3966 if (!io || !IoDIRP(io)) {
3967 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3968 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3971 (void)PerlDir_rewind(IoDIRP(io));
3975 SETERRNO(EBADF,RMS_ISI);
3978 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3984 #if defined(Direntry_t) && defined(HAS_READDIR)
3986 GV * const gv = MUTABLE_GV(POPs);
3987 register IO * const io = GvIOn(gv);
3989 if (!io || !IoDIRP(io)) {
3990 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3991 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3994 #ifdef VOID_CLOSEDIR
3995 PerlDir_close(IoDIRP(io));
3997 if (PerlDir_close(IoDIRP(io)) < 0) {
3998 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4007 SETERRNO(EBADF,RMS_IFI);
4010 DIE(aTHX_ PL_no_dir_func, "closedir");
4014 /* Process control. */
4023 PERL_FLUSHALL_FOR_CHILD;
4024 childpid = PerlProc_fork();
4028 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4030 SvREADONLY_off(GvSV(tmpgv));
4031 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4032 SvREADONLY_on(GvSV(tmpgv));
4034 #ifdef THREADS_HAVE_PIDS
4035 PL_ppid = (IV)getppid();
4037 #ifdef PERL_USES_PL_PIDSTATUS
4038 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4044 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4049 PERL_FLUSHALL_FOR_CHILD;
4050 childpid = PerlProc_fork();
4056 DIE(aTHX_ PL_no_func, "fork");
4063 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4068 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4069 childpid = wait4pid(-1, &argflags, 0);
4071 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4076 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4077 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4078 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4080 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4085 DIE(aTHX_ PL_no_func, "wait");
4091 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4093 const int optype = POPi;
4094 const Pid_t pid = TOPi;
4098 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4099 result = wait4pid(pid, &argflags, optype);
4101 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4106 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4107 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4108 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4110 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4115 DIE(aTHX_ PL_no_func, "waitpid");
4121 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4122 #if defined(__LIBCATAMOUNT__)
4123 PL_statusvalue = -1;
4132 while (++MARK <= SP) {
4133 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4138 TAINT_PROPER("system");
4140 PERL_FLUSHALL_FOR_CHILD;
4141 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4147 if (PerlProc_pipe(pp) >= 0)
4149 while ((childpid = PerlProc_fork()) == -1) {
4150 if (errno != EAGAIN) {
4155 PerlLIO_close(pp[0]);
4156 PerlLIO_close(pp[1]);
4163 Sigsave_t ihand,qhand; /* place to save signals during system() */
4167 PerlLIO_close(pp[1]);
4169 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4170 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4173 result = wait4pid(childpid, &status, 0);
4174 } while (result == -1 && errno == EINTR);
4176 (void)rsignal_restore(SIGINT, &ihand);
4177 (void)rsignal_restore(SIGQUIT, &qhand);
4179 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4180 do_execfree(); /* free any memory child malloced on fork */
4187 while (n < sizeof(int)) {
4188 n1 = PerlLIO_read(pp[0],
4189 (void*)(((char*)&errkid)+n),
4195 PerlLIO_close(pp[0]);
4196 if (n) { /* Error */
4197 if (n != sizeof(int))
4198 DIE(aTHX_ "panic: kid popen errno read");
4199 errno = errkid; /* Propagate errno from kid */
4200 STATUS_NATIVE_CHILD_SET(-1);
4203 XPUSHi(STATUS_CURRENT);
4207 PerlLIO_close(pp[0]);
4208 #if defined(HAS_FCNTL) && defined(F_SETFD)
4209 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4212 if (PL_op->op_flags & OPf_STACKED) {
4213 SV * const really = *++MARK;
4214 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4216 else if (SP - MARK != 1)
4217 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4219 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4223 #else /* ! FORK or VMS or OS/2 */
4226 if (PL_op->op_flags & OPf_STACKED) {
4227 SV * const really = *++MARK;
4228 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4229 value = (I32)do_aspawn(really, MARK, SP);
4231 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4234 else if (SP - MARK != 1) {
4235 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4236 value = (I32)do_aspawn(NULL, MARK, SP);
4238 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4242 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4244 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4246 STATUS_NATIVE_CHILD_SET(value);
4249 XPUSHi(result ? value : STATUS_CURRENT);
4250 #endif /* !FORK or VMS or OS/2 */
4257 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4262 while (++MARK <= SP) {
4263 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4268 TAINT_PROPER("exec");
4270 PERL_FLUSHALL_FOR_CHILD;
4271 if (PL_op->op_flags & OPf_STACKED) {
4272 SV * const really = *++MARK;
4273 value = (I32)do_aexec(really, MARK, SP);
4275 else if (SP - MARK != 1)
4277 value = (I32)vms_do_aexec(NULL, MARK, SP);
4281 (void ) do_aspawn(NULL, MARK, SP);
4285 value = (I32)do_aexec(NULL, MARK, SP);
4290 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4293 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4296 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4310 # ifdef THREADS_HAVE_PIDS
4311 if (PL_ppid != 1 && getppid() == 1)
4312 /* maybe the parent process has died. Refresh ppid cache */
4316 XPUSHi( getppid() );
4320 DIE(aTHX_ PL_no_func, "getppid");
4329 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4332 pgrp = (I32)BSD_GETPGRP(pid);
4334 if (pid != 0 && pid != PerlProc_getpid())
4335 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4341 DIE(aTHX_ PL_no_func, "getpgrp()");
4361 TAINT_PROPER("setpgrp");
4363 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4365 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4366 || (pid != 0 && pid != PerlProc_getpid()))
4368 DIE(aTHX_ "setpgrp can't take arguments");
4370 SETi( setpgrp() >= 0 );
4371 #endif /* USE_BSDPGRP */
4374 DIE(aTHX_ PL_no_func, "setpgrp()");
4380 #ifdef HAS_GETPRIORITY
4382 const int who = POPi;
4383 const int which = TOPi;
4384 SETi( getpriority(which, who) );
4387 DIE(aTHX_ PL_no_func, "getpriority()");
4393 #ifdef HAS_SETPRIORITY
4395 const int niceval = POPi;
4396 const int who = POPi;
4397 const int which = TOPi;
4398 TAINT_PROPER("setpriority");
4399 SETi( setpriority(which, who, niceval) >= 0 );
4402 DIE(aTHX_ PL_no_func, "setpriority()");
4412 XPUSHn( time(NULL) );
4414 XPUSHi( time(NULL) );
4426 (void)PerlProc_times(&PL_timesbuf);
4428 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4429 /* struct tms, though same data */
4433 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4434 if (GIMME == G_ARRAY) {
4435 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4436 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4437 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4445 if (GIMME == G_ARRAY) {
4452 DIE(aTHX_ "times not implemented");
4454 #endif /* HAS_TIMES */
4464 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4465 static const char * const dayname[] =
4466 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4467 static const char * const monname[] =
4468 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4469 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4474 when = (Time64_T)now;
4477 double input = Perl_floor(POPn);
4478 when = (Time64_T)input;
4479 if (when != input) {
4480 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4481 "%s(%.0f) too large", opname, input);
4485 if (PL_op->op_type == OP_LOCALTIME)
4486 err = S_localtime64_r(&when, &tmbuf);
4488 err = S_gmtime64_r(&when, &tmbuf);
4491 /* XXX %lld broken for quads */
4492 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4493 "%s(%.0f) failed", opname, (double)when);
4496 if (GIMME != G_ARRAY) { /* scalar context */
4498 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4499 double year = (double)tmbuf.tm_year + 1900;
4506 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4507 dayname[tmbuf.tm_wday],
4508 monname[tmbuf.tm_mon],
4516 else { /* list context */
4522 mPUSHi(tmbuf.tm_sec);
4523 mPUSHi(tmbuf.tm_min);
4524 mPUSHi(tmbuf.tm_hour);
4525 mPUSHi(tmbuf.tm_mday);
4526 mPUSHi(tmbuf.tm_mon);
4527 mPUSHn(tmbuf.tm_year);
4528 mPUSHi(tmbuf.tm_wday);
4529 mPUSHi(tmbuf.tm_yday);
4530 mPUSHi(tmbuf.tm_isdst);
4541 anum = alarm((unsigned int)anum);
4548 DIE(aTHX_ PL_no_func, "alarm");
4559 (void)time(&lasttime);
4564 PerlProc_sleep((unsigned int)duration);
4567 XPUSHi(when - lasttime);
4571 /* Shared memory. */
4572 /* Merged with some message passing. */
4576 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4577 dVAR; dSP; dMARK; dTARGET;
4578 const int op_type = PL_op->op_type;
4583 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4586 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4589 value = (I32)(do_semop(MARK, SP) >= 0);
4592 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4608 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4609 dVAR; dSP; dMARK; dTARGET;
4610 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4617 DIE(aTHX_ "System V IPC is not implemented on this machine");
4623 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4624 dVAR; dSP; dMARK; dTARGET;
4625 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4633 PUSHp(zero_but_true, ZBTLEN);
4641 /* I can't const this further without getting warnings about the types of
4642 various arrays passed in from structures. */
4644 S_space_join_names_mortal(pTHX_ char *const *array)
4648 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4650 if (array && *array) {
4651 target = newSVpvs_flags("", SVs_TEMP);
4653 sv_catpv(target, *array);
4656 sv_catpvs(target, " ");
4659 target = sv_mortalcopy(&PL_sv_no);
4664 /* Get system info. */
4668 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4670 I32 which = PL_op->op_type;
4671 register char **elem;
4673 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4674 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4675 struct hostent *gethostbyname(Netdb_name_t);
4676 struct hostent *gethostent(void);
4678 struct hostent *hent;
4682 if (which == OP_GHBYNAME) {
4683 #ifdef HAS_GETHOSTBYNAME
4684 const char* const name = POPpbytex;
4685 hent = PerlSock_gethostbyname(name);
4687 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4690 else if (which == OP_GHBYADDR) {
4691 #ifdef HAS_GETHOSTBYADDR
4692 const int addrtype = POPi;
4693 SV * const addrsv = POPs;
4695 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4697 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4699 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4703 #ifdef HAS_GETHOSTENT
4704 hent = PerlSock_gethostent();
4706 DIE(aTHX_ PL_no_sock_func, "gethostent");
4709 #ifdef HOST_NOT_FOUND
4711 #ifdef USE_REENTRANT_API
4712 # ifdef USE_GETHOSTENT_ERRNO
4713 h_errno = PL_reentrant_buffer->_gethostent_errno;
4716 STATUS_UNIX_SET(h_errno);
4720 if (GIMME != G_ARRAY) {
4721 PUSHs(sv = sv_newmortal());
4723 if (which == OP_GHBYNAME) {
4725 sv_setpvn(sv, hent->h_addr, hent->h_length);
4728 sv_setpv(sv, (char*)hent->h_name);
4734 mPUSHs(newSVpv((char*)hent->h_name, 0));
4735 PUSHs(space_join_names_mortal(hent->h_aliases));
4736 mPUSHi(hent->h_addrtype);
4737 len = hent->h_length;
4740 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4741 mXPUSHp(*elem, len);
4745 mPUSHp(hent->h_addr, len);
4747 PUSHs(sv_mortalcopy(&PL_sv_no));
4752 DIE(aTHX_ PL_no_sock_func, "gethostent");
4758 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4760 I32 which = PL_op->op_type;
4762 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4763 struct netent *getnetbyaddr(Netdb_net_t, int);
4764 struct netent *getnetbyname(Netdb_name_t);
4765 struct netent *getnetent(void);
4767 struct netent *nent;
4769 if (which == OP_GNBYNAME){
4770 #ifdef HAS_GETNETBYNAME
4771 const char * const name = POPpbytex;
4772 nent = PerlSock_getnetbyname(name);
4774 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4777 else if (which == OP_GNBYADDR) {
4778 #ifdef HAS_GETNETBYADDR
4779 const int addrtype = POPi;
4780 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4781 nent = PerlSock_getnetbyaddr(addr, addrtype);
4783 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4787 #ifdef HAS_GETNETENT
4788 nent = PerlSock_getnetent();
4790 DIE(aTHX_ PL_no_sock_func, "getnetent");
4793 #ifdef HOST_NOT_FOUND
4795 #ifdef USE_REENTRANT_API
4796 # ifdef USE_GETNETENT_ERRNO
4797 h_errno = PL_reentrant_buffer->_getnetent_errno;
4800 STATUS_UNIX_SET(h_errno);
4805 if (GIMME != G_ARRAY) {
4806 PUSHs(sv = sv_newmortal());
4808 if (which == OP_GNBYNAME)
4809 sv_setiv(sv, (IV)nent->n_net);
4811 sv_setpv(sv, nent->n_name);
4817 mPUSHs(newSVpv(nent->n_name, 0));
4818 PUSHs(space_join_names_mortal(nent->n_aliases));
4819 mPUSHi(nent->n_addrtype);
4820 mPUSHi(nent->n_net);
4825 DIE(aTHX_ PL_no_sock_func, "getnetent");
4831 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4833 I32 which = PL_op->op_type;
4835 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4836 struct protoent *getprotobyname(Netdb_name_t);
4837 struct protoent *getprotobynumber(int);
4838 struct protoent *getprotoent(void);
4840 struct protoent *pent;
4842 if (which == OP_GPBYNAME) {
4843 #ifdef HAS_GETPROTOBYNAME
4844 const char* const name = POPpbytex;
4845 pent = PerlSock_getprotobyname(name);
4847 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4850 else if (which == OP_GPBYNUMBER) {
4851 #ifdef HAS_GETPROTOBYNUMBER
4852 const int number = POPi;
4853 pent = PerlSock_getprotobynumber(number);
4855 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4859 #ifdef HAS_GETPROTOENT
4860 pent = PerlSock_getprotoent();
4862 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4866 if (GIMME != G_ARRAY) {
4867 PUSHs(sv = sv_newmortal());
4869 if (which == OP_GPBYNAME)
4870 sv_setiv(sv, (IV)pent->p_proto);
4872 sv_setpv(sv, pent->p_name);
4878 mPUSHs(newSVpv(pent->p_name, 0));
4879 PUSHs(space_join_names_mortal(pent->p_aliases));
4880 mPUSHi(pent->p_proto);
4885 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4891 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4893 I32 which = PL_op->op_type;
4895 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4896 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4897 struct servent *getservbyport(int, Netdb_name_t);
4898 struct servent *getservent(void);
4900 struct servent *sent;
4902 if (which == OP_GSBYNAME) {
4903 #ifdef HAS_GETSERVBYNAME
4904 const char * const proto = POPpbytex;
4905 const char * const name = POPpbytex;
4906 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4908 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4911 else if (which == OP_GSBYPORT) {
4912 #ifdef HAS_GETSERVBYPORT
4913 const char * const proto = POPpbytex;
4914 unsigned short port = (unsigned short)POPu;
4916 port = PerlSock_htons(port);
4918 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4920 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4924 #ifdef HAS_GETSERVENT
4925 sent = PerlSock_getservent();
4927 DIE(aTHX_ PL_no_sock_func, "getservent");
4931 if (GIMME != G_ARRAY) {
4932 PUSHs(sv = sv_newmortal());
4934 if (which == OP_GSBYNAME) {
4936 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4938 sv_setiv(sv, (IV)(sent->s_port));
4942 sv_setpv(sv, sent->s_name);
4948 mPUSHs(newSVpv(sent->s_name, 0));
4949 PUSHs(space_join_names_mortal(sent->s_aliases));
4951 mPUSHi(PerlSock_ntohs(sent->s_port));
4953 mPUSHi(sent->s_port);
4955 mPUSHs(newSVpv(sent->s_proto, 0));
4960 DIE(aTHX_ PL_no_sock_func, "getservent");
4966 #ifdef HAS_SETHOSTENT
4968 PerlSock_sethostent(TOPi);
4971 DIE(aTHX_ PL_no_sock_func, "sethostent");
4977 #ifdef HAS_SETNETENT
4979 (void)PerlSock_setnetent(TOPi);
4982 DIE(aTHX_ PL_no_sock_func, "setnetent");
4988 #ifdef HAS_SETPROTOENT
4990 (void)PerlSock_setprotoent(TOPi);
4993 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4999 #ifdef HAS_SETSERVENT
5001 (void)PerlSock_setservent(TOPi);
5004 DIE(aTHX_ PL_no_sock_func, "setservent");
5010 #ifdef HAS_ENDHOSTENT
5012 PerlSock_endhostent();
5016 DIE(aTHX_ PL_no_sock_func, "endhostent");
5022 #ifdef HAS_ENDNETENT
5024 PerlSock_endnetent();
5028 DIE(aTHX_ PL_no_sock_func, "endnetent");
5034 #ifdef HAS_ENDPROTOENT
5036 PerlSock_endprotoent();
5040 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5046 #ifdef HAS_ENDSERVENT
5048 PerlSock_endservent();
5052 DIE(aTHX_ PL_no_sock_func, "endservent");
5060 I32 which = PL_op->op_type;
5062 struct passwd *pwent = NULL;
5064 * We currently support only the SysV getsp* shadow password interface.
5065 * The interface is declared in <shadow.h> and often one needs to link
5066 * with -lsecurity or some such.
5067 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5070 * AIX getpwnam() is clever enough to return the encrypted password
5071 * only if the caller (euid?) is root.
5073 * There are at least three other shadow password APIs. Many platforms
5074 * seem to contain more than one interface for accessing the shadow
5075 * password databases, possibly for compatibility reasons.
5076 * The getsp*() is by far he simplest one, the other two interfaces
5077 * are much more complicated, but also very similar to each other.
5082 * struct pr_passwd *getprpw*();
5083 * The password is in
5084 * char getprpw*(...).ufld.fd_encrypt[]
5085 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5090 * struct es_passwd *getespw*();
5091 * The password is in
5092 * char *(getespw*(...).ufld.fd_encrypt)
5093 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5096 * struct userpw *getuserpw();
5097 * The password is in
5098 * char *(getuserpw(...)).spw_upw_passwd
5099 * (but the de facto standard getpwnam() should work okay)
5101 * Mention I_PROT here so that Configure probes for it.
5103 * In HP-UX for getprpw*() the manual page claims that one should include
5104 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5105 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5106 * and pp_sys.c already includes <shadow.h> if there is such.
5108 * Note that <sys/security.h> is already probed for, but currently
5109 * it is only included in special cases.
5111 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5112 * be preferred interface, even though also the getprpw*() interface
5113 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5114 * One also needs to call set_auth_parameters() in main() before
5115 * doing anything else, whether one is using getespw*() or getprpw*().
5117 * Note that accessing the shadow databases can be magnitudes
5118 * slower than accessing the standard databases.
5123 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5124 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5125 * the pw_comment is left uninitialized. */
5126 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5132 const char* const name = POPpbytex;
5133 pwent = getpwnam(name);
5139 pwent = getpwuid(uid);
5143 # ifdef HAS_GETPWENT
5145 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5146 if (pwent) pwent = getpwnam(pwent->pw_name);
5149 DIE(aTHX_ PL_no_func, "getpwent");
5155 if (GIMME != G_ARRAY) {
5156 PUSHs(sv = sv_newmortal());
5158 if (which == OP_GPWNAM)
5159 # if Uid_t_sign <= 0
5160 sv_setiv(sv, (IV)pwent->pw_uid);
5162 sv_setuv(sv, (UV)pwent->pw_uid);
5165 sv_setpv(sv, pwent->pw_name);
5171 mPUSHs(newSVpv(pwent->pw_name, 0));
5175 /* If we have getspnam(), we try to dig up the shadow
5176 * password. If we are underprivileged, the shadow
5177 * interface will set the errno to EACCES or similar,
5178 * and return a null pointer. If this happens, we will
5179 * use the dummy password (usually "*" or "x") from the
5180 * standard password database.
5182 * In theory we could skip the shadow call completely
5183 * if euid != 0 but in practice we cannot know which
5184 * security measures are guarding the shadow databases
5185 * on a random platform.
5187 * Resist the urge to use additional shadow interfaces.
5188 * Divert the urge to writing an extension instead.
5191 /* Some AIX setups falsely(?) detect some getspnam(), which
5192 * has a different API than the Solaris/IRIX one. */
5193 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5196 const struct spwd * const spwent = getspnam(pwent->pw_name);
5197 /* Save and restore errno so that
5198 * underprivileged attempts seem
5199 * to have never made the unsccessful
5200 * attempt to retrieve the shadow password. */
5202 if (spwent && spwent->sp_pwdp)
5203 sv_setpv(sv, spwent->sp_pwdp);
5207 if (!SvPOK(sv)) /* Use the standard password, then. */
5208 sv_setpv(sv, pwent->pw_passwd);
5211 # ifndef INCOMPLETE_TAINTS
5212 /* passwd is tainted because user himself can diddle with it.
5213 * admittedly not much and in a very limited way, but nevertheless. */
5217 # if Uid_t_sign <= 0
5218 mPUSHi(pwent->pw_uid);
5220 mPUSHu(pwent->pw_uid);
5223 # if Uid_t_sign <= 0
5224 mPUSHi(pwent->pw_gid);
5226 mPUSHu(pwent->pw_gid);
5228 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5229 * because of the poor interface of the Perl getpw*(),
5230 * not because there's some standard/convention saying so.
5231 * A better interface would have been to return a hash,
5232 * but we are accursed by our history, alas. --jhi. */
5234 mPUSHi(pwent->pw_change);
5237 mPUSHi(pwent->pw_quota);
5240 mPUSHs(newSVpv(pwent->pw_age, 0));
5242 /* I think that you can never get this compiled, but just in case. */
5243 PUSHs(sv_mortalcopy(&PL_sv_no));
5248 /* pw_class and pw_comment are mutually exclusive--.
5249 * see the above note for pw_change, pw_quota, and pw_age. */
5251 mPUSHs(newSVpv(pwent->pw_class, 0));
5254 mPUSHs(newSVpv(pwent->pw_comment, 0));
5256 /* I think that you can never get this compiled, but just in case. */
5257 PUSHs(sv_mortalcopy(&PL_sv_no));
5262 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5264 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5266 # ifndef INCOMPLETE_TAINTS
5267 /* pw_gecos is tainted because user himself can diddle with it. */
5271 mPUSHs(newSVpv(pwent->pw_dir, 0));
5273 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5274 # ifndef INCOMPLETE_TAINTS
5275 /* pw_shell is tainted because user himself can diddle with it. */
5280 mPUSHi(pwent->pw_expire);
5285 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5291 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5296 DIE(aTHX_ PL_no_func, "setpwent");
5302 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5307 DIE(aTHX_ PL_no_func, "endpwent");
5315 const I32 which = PL_op->op_type;
5316 const struct group *grent;
5318 if (which == OP_GGRNAM) {
5319 const char* const name = POPpbytex;
5320 grent = (const struct group *)getgrnam(name);
5322 else if (which == OP_GGRGID) {
5323 const Gid_t gid = POPi;
5324 grent = (const struct group *)getgrgid(gid);
5328 grent = (struct group *)getgrent();
5330 DIE(aTHX_ PL_no_func, "getgrent");
5334 if (GIMME != G_ARRAY) {
5335 SV * const sv = sv_newmortal();
5339 if (which == OP_GGRNAM)
5341 sv_setiv(sv, (IV)grent->gr_gid);
5343 sv_setuv(sv, (UV)grent->gr_gid);
5346 sv_setpv(sv, grent->gr_name);
5352 mPUSHs(newSVpv(grent->gr_name, 0));
5355 mPUSHs(newSVpv(grent->gr_passwd, 0));
5357 PUSHs(sv_mortalcopy(&PL_sv_no));
5361 mPUSHi(grent->gr_gid);
5363 mPUSHu(grent->gr_gid);
5366 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5367 /* In UNICOS/mk (_CRAYMPP) the multithreading
5368 * versions (getgrnam_r, getgrgid_r)
5369 * seem to return an illegal pointer
5370 * as the group members list, gr_mem.
5371 * getgrent() doesn't even have a _r version
5372 * but the gr_mem is poisonous anyway.
5373 * So yes, you cannot get the list of group
5374 * members if building multithreaded in UNICOS/mk. */
5375 PUSHs(space_join_names_mortal(grent->gr_mem));
5381 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5387 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5392 DIE(aTHX_ PL_no_func, "setgrent");
5398 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5403 DIE(aTHX_ PL_no_func, "endgrent");
5413 if (!(tmps = PerlProc_getlogin()))
5415 PUSHp(tmps, strlen(tmps));
5418 DIE(aTHX_ PL_no_func, "getlogin");
5422 /* Miscellaneous. */
5427 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5428 register I32 items = SP - MARK;
5429 unsigned long a[20];
5434 while (++MARK <= SP) {
5435 if (SvTAINTED(*MARK)) {
5441 TAINT_PROPER("syscall");
5444 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5445 * or where sizeof(long) != sizeof(char*). But such machines will
5446 * not likely have syscall implemented either, so who cares?
5448 while (++MARK <= SP) {
5449 if (SvNIOK(*MARK) || !i)
5450 a[i++] = SvIV(*MARK);
5451 else if (*MARK == &PL_sv_undef)
5454 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5460 DIE(aTHX_ "Too many args to syscall");
5462 DIE(aTHX_ "Too few args to syscall");
5464 retval = syscall(a[0]);
5467 retval = syscall(a[0],a[1]);
5470 retval = syscall(a[0],a[1],a[2]);
5473 retval = syscall(a[0],a[1],a[2],a[3]);
5476 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5479 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5482 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5485 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5489 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5492 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5495 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5499 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5503 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5507 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5508 a[10],a[11],a[12],a[13]);
5510 #endif /* atarist */
5516 DIE(aTHX_ PL_no_func, "syscall");
5520 #ifdef FCNTL_EMULATE_FLOCK
5522 /* XXX Emulate flock() with fcntl().
5523 What's really needed is a good file locking module.
5527 fcntl_emulate_flock(int fd, int operation)
5531 switch (operation & ~LOCK_NB) {
5533 flock.l_type = F_RDLCK;
5536 flock.l_type = F_WRLCK;
5539 flock.l_type = F_UNLCK;
5545 flock.l_whence = SEEK_SET;
5546 flock.l_start = flock.l_len = (Off_t)0;
5548 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5551 #endif /* FCNTL_EMULATE_FLOCK */
5553 #ifdef LOCKF_EMULATE_FLOCK
5555 /* XXX Emulate flock() with lockf(). This is just to increase
5556 portability of scripts. The calls are not completely
5557 interchangeable. What's really needed is a good file
5561 /* The lockf() constants might have been defined in <unistd.h>.
5562 Unfortunately, <unistd.h> causes troubles on some mixed
5563 (BSD/POSIX) systems, such as SunOS 4.1.3.
5565 Further, the lockf() constants aren't POSIX, so they might not be
5566 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5567 just stick in the SVID values and be done with it. Sigh.
5571 # define F_ULOCK 0 /* Unlock a previously locked region */
5574 # define F_LOCK 1 /* Lock a region for exclusive use */
5577 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5580 # define F_TEST 3 /* Test a region for other processes locks */
5584 lockf_emulate_flock(int fd, int operation)
5590 /* flock locks entire file so for lockf we need to do the same */
5591 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5592 if (pos > 0) /* is seekable and needs to be repositioned */
5593 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5594 pos = -1; /* seek failed, so don't seek back afterwards */
5597 switch (operation) {
5599 /* LOCK_SH - get a shared lock */
5601 /* LOCK_EX - get an exclusive lock */
5603 i = lockf (fd, F_LOCK, 0);
5606 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5607 case LOCK_SH|LOCK_NB:
5608 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5609 case LOCK_EX|LOCK_NB:
5610 i = lockf (fd, F_TLOCK, 0);
5612 if ((errno == EAGAIN) || (errno == EACCES))
5613 errno = EWOULDBLOCK;
5616 /* LOCK_UN - unlock (non-blocking is a no-op) */
5618 case LOCK_UN|LOCK_NB:
5619 i = lockf (fd, F_ULOCK, 0);
5622 /* Default - can't decipher operation */
5629 if (pos > 0) /* need to restore position of the handle */
5630 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5635 #endif /* LOCKF_EMULATE_FLOCK */
5639 * c-indentation-style: bsd
5641 * indent-tabs-mode: t
5644 * ex: set ts=8 sts=4 sw=4 noet: