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));
516 GV * const gv = MUTABLE_GV(*++MARK);
519 DIE(aTHX_ PL_no_usym, "filehandle");
521 if ((io = GvIOp(gv))) {
523 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
526 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
527 "Opening dirhandle %s also as a file",
530 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
532 /* Method's args are same as ours ... */
533 /* ... except handle is replaced by the object */
534 *MARK-- = SvTIED_obj(MUTABLE_SV(io), mg);
538 call_method("OPEN", G_SCALAR);
552 tmps = SvPV_const(sv, len);
553 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
556 PUSHi( (I32)PL_forkprocess );
557 else if (PL_forkprocess == 0) /* we are a new child */
567 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
570 IO * const io = GvIO(gv);
572 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
575 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
578 call_method("CLOSE", G_SCALAR);
586 PUSHs(boolSV(do_close(gv, TRUE)));
599 GV * const wgv = MUTABLE_GV(POPs);
600 GV * const rgv = MUTABLE_GV(POPs);
605 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
606 DIE(aTHX_ PL_no_usym, "filehandle");
611 do_close(rgv, FALSE);
613 do_close(wgv, FALSE);
615 if (PerlProc_pipe(fd) < 0)
618 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
619 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
620 IoOFP(rstio) = IoIFP(rstio);
621 IoIFP(wstio) = IoOFP(wstio);
622 IoTYPE(rstio) = IoTYPE_RDONLY;
623 IoTYPE(wstio) = IoTYPE_WRONLY;
625 if (!IoIFP(rstio) || !IoOFP(wstio)) {
627 PerlIO_close(IoIFP(rstio));
629 PerlLIO_close(fd[0]);
631 PerlIO_close(IoOFP(wstio));
633 PerlLIO_close(fd[1]);
636 #if defined(HAS_FCNTL) && defined(F_SETFD)
637 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
638 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
645 DIE(aTHX_ PL_no_func, "pipe");
659 gv = MUTABLE_GV(POPs);
661 if (gv && (io = GvIO(gv))
662 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
665 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
668 call_method("FILENO", G_SCALAR);
674 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
675 /* Can't do this because people seem to do things like
676 defined(fileno($foo)) to check whether $foo is a valid fh.
677 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
678 report_evil_fh(gv, io, PL_op->op_type);
683 PUSHi(PerlIO_fileno(fp));
696 anum = PerlLIO_umask(022);
697 /* setting it to 022 between the two calls to umask avoids
698 * to have a window where the umask is set to 0 -- meaning
699 * that another thread could create world-writeable files. */
701 (void)PerlLIO_umask(anum);
704 anum = PerlLIO_umask(POPi);
705 TAINT_PROPER("umask");
708 /* Only DIE if trying to restrict permissions on "user" (self).
709 * Otherwise it's harmless and more useful to just return undef
710 * since 'group' and 'other' concepts probably don't exist here. */
711 if (MAXARG >= 1 && (POPi & 0700))
712 DIE(aTHX_ "umask not implemented");
713 XPUSHs(&PL_sv_undef);
732 gv = MUTABLE_GV(POPs);
734 if (gv && (io = GvIO(gv))) {
735 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
738 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
743 call_method("BINMODE", G_SCALAR);
751 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
752 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
753 report_evil_fh(gv, io, PL_op->op_type);
754 SETERRNO(EBADF,RMS_IFI);
761 const char *d = NULL;
764 d = SvPV_const(discp, len);
765 mode = mode_from_discipline(d, len);
766 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
767 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
768 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
789 const I32 markoff = MARK - PL_stack_base;
790 const char *methname;
791 int how = PERL_MAGIC_tied;
795 switch(SvTYPE(varsv)) {
797 methname = "TIEHASH";
798 HvEITER_set(MUTABLE_HV(varsv), 0);
801 methname = "TIEARRAY";
804 if (isGV_with_GP(varsv)) {
805 methname = "TIEHANDLE";
806 how = PERL_MAGIC_tiedscalar;
807 /* For tied filehandles, we apply tiedscalar magic to the IO
808 slot of the GP rather than the GV itself. AMS 20010812 */
810 GvIOp(varsv) = newIO();
811 varsv = MUTABLE_SV(GvIOp(varsv));
816 methname = "TIESCALAR";
817 how = PERL_MAGIC_tiedscalar;
821 if (sv_isobject(*MARK)) { /* Calls GET magic. */
823 PUSHSTACKi(PERLSI_MAGIC);
825 EXTEND(SP,(I32)items);
829 call_method(methname, G_SCALAR);
832 /* Not clear why we don't call call_method here too.
833 * perhaps to get different error message ?
836 const char *name = SvPV_nomg_const(*MARK, len);
837 stash = gv_stashpvn(name, len, 0);
838 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
839 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
840 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
843 PUSHSTACKi(PERLSI_MAGIC);
845 EXTEND(SP,(I32)items);
849 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
855 if (sv_isobject(sv)) {
856 sv_unmagic(varsv, how);
857 /* Croak if a self-tie on an aggregate is attempted. */
858 if (varsv == SvRV(sv) &&
859 (SvTYPE(varsv) == SVt_PVAV ||
860 SvTYPE(varsv) == SVt_PVHV))
862 "Self-ties of arrays and hashes are not supported");
863 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
866 SP = PL_stack_base + markoff;
876 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
877 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
879 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
882 if ((mg = SvTIED_mg(sv, how))) {
883 SV * const obj = SvRV(SvTIED_obj(sv, mg));
885 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
887 if (gv && isGV(gv) && (cv = GvCV(gv))) {
889 XPUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
890 mXPUSHi(SvREFCNT(obj) - 1);
893 call_sv(MUTABLE_SV(cv), G_VOID);
897 else if (mg && SvREFCNT(obj) > 1) {
898 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
899 "untie attempted while %"UVuf" inner references still exist",
900 (UV)SvREFCNT(obj) - 1 ) ;
904 sv_unmagic(sv, how) ;
914 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
915 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
917 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
920 if ((mg = SvTIED_mg(sv, how))) {
921 SV *osv = SvTIED_obj(sv, mg);
922 if (osv == mg->mg_obj)
923 osv = sv_mortalcopy(osv);
937 HV * const hv = MUTABLE_HV(POPs);
938 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
939 stash = gv_stashsv(sv, 0);
940 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
942 require_pv("AnyDBM_File.pm");
944 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
945 DIE(aTHX_ "No dbm on this machine");
955 mPUSHu(O_RDWR|O_CREAT);
960 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
963 if (!sv_isobject(TOPs)) {
971 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
975 if (sv_isobject(TOPs)) {
976 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
977 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
994 struct timeval timebuf;
995 struct timeval *tbuf = &timebuf;
998 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1003 # if BYTEORDER & 0xf0000
1004 # define ORDERBYTE (0x88888888 - BYTEORDER)
1006 # define ORDERBYTE (0x4444 - BYTEORDER)
1012 for (i = 1; i <= 3; i++) {
1013 SV * const sv = SP[i];
1016 if (SvREADONLY(sv)) {
1018 sv_force_normal_flags(sv, 0);
1019 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1020 DIE(aTHX_ "%s", PL_no_modify);
1023 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1024 SvPV_force_nolen(sv); /* force string conversion */
1031 /* little endians can use vecs directly */
1032 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1039 masksize = NFDBITS / NBBY;
1041 masksize = sizeof(long); /* documented int, everyone seems to use long */
1043 Zero(&fd_sets[0], 4, char*);
1046 # if SELECT_MIN_BITS == 1
1047 growsize = sizeof(fd_set);
1049 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1050 # undef SELECT_MIN_BITS
1051 # define SELECT_MIN_BITS __FD_SETSIZE
1053 /* If SELECT_MIN_BITS is greater than one we most probably will want
1054 * to align the sizes with SELECT_MIN_BITS/8 because for example
1055 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1056 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1057 * on (sets/tests/clears bits) is 32 bits. */
1058 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1066 timebuf.tv_sec = (long)value;
1067 value -= (NV)timebuf.tv_sec;
1068 timebuf.tv_usec = (long)(value * 1000000.0);
1073 for (i = 1; i <= 3; i++) {
1075 if (!SvOK(sv) || SvCUR(sv) == 0) {
1082 Sv_Grow(sv, growsize);
1086 while (++j <= growsize) {
1090 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1092 Newx(fd_sets[i], growsize, char);
1093 for (offset = 0; offset < growsize; offset += masksize) {
1094 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1095 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1098 fd_sets[i] = SvPVX(sv);
1102 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1103 /* Can't make just the (void*) conditional because that would be
1104 * cpp #if within cpp macro, and not all compilers like that. */
1105 nfound = PerlSock_select(
1107 (Select_fd_set_t) fd_sets[1],
1108 (Select_fd_set_t) fd_sets[2],
1109 (Select_fd_set_t) fd_sets[3],
1110 (void*) tbuf); /* Workaround for compiler bug. */
1112 nfound = PerlSock_select(
1114 (Select_fd_set_t) fd_sets[1],
1115 (Select_fd_set_t) fd_sets[2],
1116 (Select_fd_set_t) fd_sets[3],
1119 for (i = 1; i <= 3; i++) {
1122 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1124 for (offset = 0; offset < growsize; offset += masksize) {
1125 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1126 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1128 Safefree(fd_sets[i]);
1135 if (GIMME == G_ARRAY && tbuf) {
1136 value = (NV)(timebuf.tv_sec) +
1137 (NV)(timebuf.tv_usec) / 1000000.0;
1142 DIE(aTHX_ "select not implemented");
1147 =for apidoc setdefout
1149 Sets PL_defoutgv, the default file handle for output, to the passed in
1150 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1151 count of the passed in typeglob is increased by one, and the reference count
1152 of the typeglob that PL_defoutgv points to is decreased by one.
1158 Perl_setdefout(pTHX_ GV *gv)
1161 SvREFCNT_inc_simple_void(gv);
1163 SvREFCNT_dec(PL_defoutgv);
1171 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1172 GV * egv = GvEGV(PL_defoutgv);
1178 XPUSHs(&PL_sv_undef);
1180 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1181 if (gvp && *gvp == egv) {
1182 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1186 mXPUSHs(newRV(MUTABLE_SV(egv)));
1191 if (!GvIO(newdefout))
1192 gv_IOadd(newdefout);
1193 setdefout(newdefout);
1203 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1205 if (gv && (io = GvIO(gv))) {
1206 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1208 const I32 gimme = GIMME_V;
1210 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
1213 call_method("GETC", gimme);
1216 if (gimme == G_SCALAR)
1217 SvSetMagicSV_nosteal(TARG, TOPs);
1221 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1222 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1223 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1224 report_evil_fh(gv, io, PL_op->op_type);
1225 SETERRNO(EBADF,RMS_IFI);
1229 sv_setpvs(TARG, " ");
1230 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1231 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1232 /* Find out how many bytes the char needs */
1233 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1236 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1237 SvCUR_set(TARG,1+len);
1246 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1249 register PERL_CONTEXT *cx;
1250 const I32 gimme = GIMME_V;
1252 PERL_ARGS_ASSERT_DOFORM;
1257 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1258 PUSHFORMAT(cx, retop);
1260 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1262 setdefout(gv); /* locally select filehandle so $% et al work */
1279 gv = MUTABLE_GV(POPs);
1294 goto not_a_format_reference;
1299 tmpsv = sv_newmortal();
1300 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1301 name = SvPV_nolen_const(tmpsv);
1303 DIE(aTHX_ "Undefined format \"%s\" called", name);
1305 not_a_format_reference:
1306 DIE(aTHX_ "Not a format reference");
1309 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1311 IoFLAGS(io) &= ~IOf_DIDTOP;
1312 return doform(cv,gv,PL_op->op_next);
1318 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1319 register IO * const io = GvIOp(gv);
1324 register PERL_CONTEXT *cx;
1326 if (!io || !(ofp = IoOFP(io)))
1329 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1330 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1332 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1333 PL_formtarget != PL_toptarget)
1337 if (!IoTOP_GV(io)) {
1340 if (!IoTOP_NAME(io)) {
1342 if (!IoFMT_NAME(io))
1343 IoFMT_NAME(io) = savepv(GvNAME(gv));
1344 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1345 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1346 if ((topgv && GvFORM(topgv)) ||
1347 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1348 IoTOP_NAME(io) = savesvpv(topname);
1350 IoTOP_NAME(io) = savepvs("top");
1352 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1353 if (!topgv || !GvFORM(topgv)) {
1354 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1357 IoTOP_GV(io) = topgv;
1359 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1360 I32 lines = IoLINES_LEFT(io);
1361 const char *s = SvPVX_const(PL_formtarget);
1362 if (lines <= 0) /* Yow, header didn't even fit!!! */
1364 while (lines-- > 0) {
1365 s = strchr(s, '\n');
1371 const STRLEN save = SvCUR(PL_formtarget);
1372 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1373 do_print(PL_formtarget, ofp);
1374 SvCUR_set(PL_formtarget, save);
1375 sv_chop(PL_formtarget, s);
1376 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1379 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1380 do_print(PL_formfeed, ofp);
1381 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1383 PL_formtarget = PL_toptarget;
1384 IoFLAGS(io) |= IOf_DIDTOP;
1387 DIE(aTHX_ "bad top format reference");
1390 SV * const sv = sv_newmortal();
1392 gv_efullname4(sv, fgv, NULL, FALSE);
1393 name = SvPV_nolen_const(sv);
1395 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1397 DIE(aTHX_ "Undefined top format called");
1399 if (cv && CvCLONE(cv))
1400 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1401 return doform(cv, gv, PL_op);
1405 POPBLOCK(cx,PL_curpm);
1411 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1413 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1414 else if (ckWARN(WARN_CLOSED))
1415 report_evil_fh(gv, io, PL_op->op_type);
1420 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1421 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1423 if (!do_print(PL_formtarget, fp))
1426 FmLINES(PL_formtarget) = 0;
1427 SvCUR_set(PL_formtarget, 0);
1428 *SvEND(PL_formtarget) = '\0';
1429 if (IoFLAGS(io) & IOf_FLUSH)
1430 (void)PerlIO_flush(fp);
1435 PL_formtarget = PL_bodytarget;
1437 PERL_UNUSED_VAR(newsp);
1438 PERL_UNUSED_VAR(gimme);
1439 return cx->blk_sub.retop;
1444 dVAR; dSP; dMARK; dORIGMARK;
1450 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1452 if (gv && (io = GvIO(gv))) {
1453 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1455 if (MARK == ORIGMARK) {
1458 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1462 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1465 call_method("PRINTF", G_SCALAR);
1468 MARK = ORIGMARK + 1;
1476 if (!(io = GvIO(gv))) {
1477 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1478 report_evil_fh(gv, io, PL_op->op_type);
1479 SETERRNO(EBADF,RMS_IFI);
1482 else if (!(fp = IoOFP(io))) {
1483 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1485 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1486 else if (ckWARN(WARN_CLOSED))
1487 report_evil_fh(gv, io, PL_op->op_type);
1489 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1493 if (SvTAINTED(MARK[1]))
1494 TAINT_PROPER("printf");
1495 do_sprintf(sv, SP - MARK, MARK + 1);
1496 if (!do_print(sv, fp))
1499 if (IoFLAGS(io) & IOf_FLUSH)
1500 if (PerlIO_flush(fp) == EOF)
1511 PUSHs(&PL_sv_undef);
1519 const int perm = (MAXARG > 3) ? POPi : 0666;
1520 const int mode = POPi;
1521 SV * const sv = POPs;
1522 GV * const gv = MUTABLE_GV(POPs);
1525 /* Need TIEHANDLE method ? */
1526 const char * const tmps = SvPV_const(sv, len);
1527 /* FIXME? do_open should do const */
1528 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1529 IoLINES(GvIOp(gv)) = 0;
1533 PUSHs(&PL_sv_undef);
1540 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1546 Sock_size_t bufsize;
1554 bool charstart = FALSE;
1555 STRLEN charskip = 0;
1558 GV * const gv = MUTABLE_GV(*++MARK);
1559 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1560 && gv && (io = GvIO(gv)) )
1562 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1566 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1568 call_method("READ", G_SCALAR);
1582 sv_setpvs(bufsv, "");
1583 length = SvIVx(*++MARK);
1586 offset = SvIVx(*++MARK);
1590 if (!io || !IoIFP(io)) {
1591 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1592 report_evil_fh(gv, io, PL_op->op_type);
1593 SETERRNO(EBADF,RMS_IFI);
1596 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1597 buffer = SvPVutf8_force(bufsv, blen);
1598 /* UTF-8 may not have been set if they are all low bytes */
1603 buffer = SvPV_force(bufsv, blen);
1604 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1607 DIE(aTHX_ "Negative length");
1615 if (PL_op->op_type == OP_RECV) {
1616 char namebuf[MAXPATHLEN];
1617 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1618 bufsize = sizeof (struct sockaddr_in);
1620 bufsize = sizeof namebuf;
1622 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1626 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1627 /* 'offset' means 'flags' here */
1628 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1629 (struct sockaddr *)namebuf, &bufsize);
1633 /* Bogus return without padding */
1634 bufsize = sizeof (struct sockaddr_in);
1636 SvCUR_set(bufsv, count);
1637 *SvEND(bufsv) = '\0';
1638 (void)SvPOK_only(bufsv);
1642 /* This should not be marked tainted if the fp is marked clean */
1643 if (!(IoFLAGS(io) & IOf_UNTAINT))
1644 SvTAINTED_on(bufsv);
1646 sv_setpvn(TARG, namebuf, bufsize);
1651 if (PL_op->op_type == OP_RECV)
1652 DIE(aTHX_ PL_no_sock_func, "recv");
1654 if (DO_UTF8(bufsv)) {
1655 /* offset adjust in characters not bytes */
1656 blen = sv_len_utf8(bufsv);
1659 if (-offset > (int)blen)
1660 DIE(aTHX_ "Offset outside string");
1663 if (DO_UTF8(bufsv)) {
1664 /* convert offset-as-chars to offset-as-bytes */
1665 if (offset >= (int)blen)
1666 offset += SvCUR(bufsv) - blen;
1668 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1671 bufsize = SvCUR(bufsv);
1672 /* Allocating length + offset + 1 isn't perfect in the case of reading
1673 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1675 (should be 2 * length + offset + 1, or possibly something longer if
1676 PL_encoding is true) */
1677 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1678 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1679 Zero(buffer+bufsize, offset-bufsize, char);
1681 buffer = buffer + offset;
1683 read_target = bufsv;
1685 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1686 concatenate it to the current buffer. */
1688 /* Truncate the existing buffer to the start of where we will be
1690 SvCUR_set(bufsv, offset);
1692 read_target = sv_newmortal();
1693 SvUPGRADE(read_target, SVt_PV);
1694 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1697 if (PL_op->op_type == OP_SYSREAD) {
1698 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1699 if (IoTYPE(io) == IoTYPE_SOCKET) {
1700 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1706 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1711 #ifdef HAS_SOCKET__bad_code_maybe
1712 if (IoTYPE(io) == IoTYPE_SOCKET) {
1713 char namebuf[MAXPATHLEN];
1714 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1715 bufsize = sizeof (struct sockaddr_in);
1717 bufsize = sizeof namebuf;
1719 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1720 (struct sockaddr *)namebuf, &bufsize);
1725 count = PerlIO_read(IoIFP(io), buffer, length);
1726 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1727 if (count == 0 && PerlIO_error(IoIFP(io)))
1731 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1732 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1735 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1736 *SvEND(read_target) = '\0';
1737 (void)SvPOK_only(read_target);
1738 if (fp_utf8 && !IN_BYTES) {
1739 /* Look at utf8 we got back and count the characters */
1740 const char *bend = buffer + count;
1741 while (buffer < bend) {
1743 skip = UTF8SKIP(buffer);
1746 if (buffer - charskip + skip > bend) {
1747 /* partial character - try for rest of it */
1748 length = skip - (bend-buffer);
1749 offset = bend - SvPVX_const(bufsv);
1761 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1762 provided amount read (count) was what was requested (length)
1764 if (got < wanted && count == length) {
1765 length = wanted - got;
1766 offset = bend - SvPVX_const(bufsv);
1769 /* return value is character count */
1773 else if (buffer_utf8) {
1774 /* Let svcatsv upgrade the bytes we read in to utf8.
1775 The buffer is a mortal so will be freed soon. */
1776 sv_catsv_nomg(bufsv, read_target);
1779 /* This should not be marked tainted if the fp is marked clean */
1780 if (!(IoFLAGS(io) & IOf_UNTAINT))
1781 SvTAINTED_on(bufsv);
1793 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1799 STRLEN orig_blen_bytes;
1800 const int op_type = PL_op->op_type;
1804 GV *const gv = MUTABLE_GV(*++MARK);
1805 if (PL_op->op_type == OP_SYSWRITE
1806 && gv && (io = GvIO(gv))) {
1807 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1811 if (MARK == SP - 1) {
1813 mXPUSHi(sv_len(sv));
1818 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1820 call_method("WRITE", G_SCALAR);
1836 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1838 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1839 if (io && IoIFP(io))
1840 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1842 report_evil_fh(gv, io, PL_op->op_type);
1844 SETERRNO(EBADF,RMS_IFI);
1848 /* Do this first to trigger any overloading. */
1849 buffer = SvPV_const(bufsv, blen);
1850 orig_blen_bytes = blen;
1851 doing_utf8 = DO_UTF8(bufsv);
1853 if (PerlIO_isutf8(IoIFP(io))) {
1854 if (!SvUTF8(bufsv)) {
1855 /* We don't modify the original scalar. */
1856 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1857 buffer = (char *) tmpbuf;
1861 else if (doing_utf8) {
1862 STRLEN tmplen = blen;
1863 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1866 buffer = (char *) tmpbuf;
1870 assert((char *)result == buffer);
1871 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1875 if (op_type == OP_SYSWRITE) {
1876 Size_t length = 0; /* This length is in characters. */
1882 /* The SV is bytes, and we've had to upgrade it. */
1883 blen_chars = orig_blen_bytes;
1885 /* The SV really is UTF-8. */
1886 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1887 /* Don't call sv_len_utf8 again because it will call magic
1888 or overloading a second time, and we might get back a
1889 different result. */
1890 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1892 /* It's safe, and it may well be cached. */
1893 blen_chars = sv_len_utf8(bufsv);
1901 length = blen_chars;
1903 #if Size_t_size > IVSIZE
1904 length = (Size_t)SvNVx(*++MARK);
1906 length = (Size_t)SvIVx(*++MARK);
1908 if ((SSize_t)length < 0) {
1910 DIE(aTHX_ "Negative length");
1915 offset = SvIVx(*++MARK);
1917 if (-offset > (IV)blen_chars) {
1919 DIE(aTHX_ "Offset outside string");
1921 offset += blen_chars;
1922 } else if (offset > (IV)blen_chars) {
1924 DIE(aTHX_ "Offset outside string");
1928 if (length > blen_chars - offset)
1929 length = blen_chars - offset;
1931 /* Here we convert length from characters to bytes. */
1932 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1933 /* Either we had to convert the SV, or the SV is magical, or
1934 the SV has overloading, in which case we can't or mustn't
1935 or mustn't call it again. */
1937 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1938 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1940 /* It's a real UTF-8 SV, and it's not going to change under
1941 us. Take advantage of any cache. */
1943 I32 len_I32 = length;
1945 /* Convert the start and end character positions to bytes.
1946 Remember that the second argument to sv_pos_u2b is relative
1948 sv_pos_u2b(bufsv, &start, &len_I32);
1955 buffer = buffer+offset;
1957 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1958 if (IoTYPE(io) == IoTYPE_SOCKET) {
1959 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1965 /* See the note at doio.c:do_print about filesize limits. --jhi */
1966 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1972 const int flags = SvIVx(*++MARK);
1975 char * const sockbuf = SvPVx(*++MARK, mlen);
1976 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1977 flags, (struct sockaddr *)sockbuf, mlen);
1981 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1986 DIE(aTHX_ PL_no_sock_func, "send");
1993 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1996 #if Size_t_size > IVSIZE
2017 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2018 else if (PL_op->op_flags & OPf_SPECIAL)
2019 gv = PL_last_in_gv = GvEGV(PL_argvgv); /* eof() - ARGV magic */
2021 gv = PL_last_in_gv; /* eof */
2026 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2028 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2030 * in Perl 5.12 and later, the additional paramter is a bitmask:
2033 * 2 = eof() <- ARGV magic
2036 mPUSHi(1); /* 1 = eof(FH) - simple, explicit FH */
2037 else if (PL_op->op_flags & OPf_SPECIAL)
2038 mPUSHi(2); /* 2 = eof() - ARGV magic */
2040 mPUSHi(0); /* 0 = eof - simple, implicit FH */
2043 call_method("EOF", G_SCALAR);
2049 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2050 if (io && !IoIFP(io)) {
2051 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2053 IoFLAGS(io) &= ~IOf_START;
2054 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2056 sv_setpvs(GvSV(gv), "-");
2058 GvSV(gv) = newSVpvs("-");
2059 SvSETMAGIC(GvSV(gv));
2061 else if (!nextargv(gv))
2066 PUSHs(boolSV(do_eof(gv)));
2077 PL_last_in_gv = MUTABLE_GV(POPs);
2080 if (gv && (io = GvIO(gv))) {
2081 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2084 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2087 call_method("TELL", G_SCALAR);
2095 SETERRNO(EBADF,RMS_IFI);
2100 #if LSEEKSIZE > IVSIZE
2101 PUSHn( do_tell(gv) );
2103 PUSHi( do_tell(gv) );
2111 const int whence = POPi;
2112 #if LSEEKSIZE > IVSIZE
2113 const Off_t offset = (Off_t)SvNVx(POPs);
2115 const Off_t offset = (Off_t)SvIVx(POPs);
2118 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2121 if (gv && (io = GvIO(gv))) {
2122 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2125 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2126 #if LSEEKSIZE > IVSIZE
2127 mXPUSHn((NV) offset);
2134 call_method("SEEK", G_SCALAR);
2141 if (PL_op->op_type == OP_SEEK)
2142 PUSHs(boolSV(do_seek(gv, offset, whence)));
2144 const Off_t sought = do_sysseek(gv, offset, whence);
2146 PUSHs(&PL_sv_undef);
2148 SV* const sv = sought ?
2149 #if LSEEKSIZE > IVSIZE
2154 : newSVpvn(zero_but_true, ZBTLEN);
2165 /* There seems to be no consensus on the length type of truncate()
2166 * and ftruncate(), both off_t and size_t have supporters. In
2167 * general one would think that when using large files, off_t is
2168 * at least as wide as size_t, so using an off_t should be okay. */
2169 /* XXX Configure probe for the length type of *truncate() needed XXX */
2172 #if Off_t_size > IVSIZE
2177 /* Checking for length < 0 is problematic as the type might or
2178 * might not be signed: if it is not, clever compilers will moan. */
2179 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2186 if (PL_op->op_flags & OPf_SPECIAL) {
2187 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2196 TAINT_PROPER("truncate");
2197 if (!(fp = IoIFP(io))) {
2203 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2205 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2212 SV * const sv = POPs;
2215 if (isGV_with_GP(sv)) {
2216 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2217 goto do_ftruncate_gv;
2219 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2220 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2221 goto do_ftruncate_gv;
2223 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2224 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2225 goto do_ftruncate_io;
2228 name = SvPV_nolen_const(sv);
2229 TAINT_PROPER("truncate");
2231 if (truncate(name, len) < 0)
2235 const int tmpfd = PerlLIO_open(name, O_RDWR);
2240 if (my_chsize(tmpfd, len) < 0)
2242 PerlLIO_close(tmpfd);
2251 SETERRNO(EBADF,RMS_IFI);
2259 SV * const argsv = POPs;
2260 const unsigned int func = POPu;
2261 const int optype = PL_op->op_type;
2262 GV * const gv = MUTABLE_GV(POPs);
2263 IO * const io = gv ? GvIOn(gv) : NULL;
2267 if (!io || !argsv || !IoIFP(io)) {
2268 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2269 report_evil_fh(gv, io, PL_op->op_type);
2270 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2274 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2277 s = SvPV_force(argsv, len);
2278 need = IOCPARM_LEN(func);
2280 s = Sv_Grow(argsv, need + 1);
2281 SvCUR_set(argsv, need);
2284 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2287 retval = SvIV(argsv);
2288 s = INT2PTR(char*,retval); /* ouch */
2291 TAINT_PROPER(PL_op_desc[optype]);
2293 if (optype == OP_IOCTL)
2295 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2297 DIE(aTHX_ "ioctl is not implemented");
2301 DIE(aTHX_ "fcntl is not implemented");
2303 #if defined(OS2) && defined(__EMX__)
2304 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2306 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2310 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2312 if (s[SvCUR(argsv)] != 17)
2313 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2315 s[SvCUR(argsv)] = 0; /* put our null back */
2316 SvSETMAGIC(argsv); /* Assume it has changed */
2325 PUSHp(zero_but_true, ZBTLEN);
2338 const int argtype = POPi;
2339 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2341 if (gv && (io = GvIO(gv)))
2347 /* XXX Looks to me like io is always NULL at this point */
2349 (void)PerlIO_flush(fp);
2350 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2353 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2354 report_evil_fh(gv, io, PL_op->op_type);
2356 SETERRNO(EBADF,RMS_IFI);
2361 DIE(aTHX_ PL_no_func, "flock()");
2371 const int protocol = POPi;
2372 const int type = POPi;
2373 const int domain = POPi;
2374 GV * const gv = MUTABLE_GV(POPs);
2375 register IO * const io = gv ? GvIOn(gv) : NULL;
2379 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2380 report_evil_fh(gv, io, PL_op->op_type);
2381 if (io && IoIFP(io))
2382 do_close(gv, FALSE);
2383 SETERRNO(EBADF,LIB_INVARG);
2388 do_close(gv, FALSE);
2390 TAINT_PROPER("socket");
2391 fd = PerlSock_socket(domain, type, protocol);
2394 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2395 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2396 IoTYPE(io) = IoTYPE_SOCKET;
2397 if (!IoIFP(io) || !IoOFP(io)) {
2398 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2399 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2400 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2403 #if defined(HAS_FCNTL) && defined(F_SETFD)
2404 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2408 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2413 DIE(aTHX_ PL_no_sock_func, "socket");
2419 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2421 const int protocol = POPi;
2422 const int type = POPi;
2423 const int domain = POPi;
2424 GV * const gv2 = MUTABLE_GV(POPs);
2425 GV * const gv1 = MUTABLE_GV(POPs);
2426 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2427 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2430 if (!gv1 || !gv2 || !io1 || !io2) {
2431 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2433 report_evil_fh(gv1, io1, PL_op->op_type);
2435 report_evil_fh(gv1, io2, PL_op->op_type);
2437 if (io1 && IoIFP(io1))
2438 do_close(gv1, FALSE);
2439 if (io2 && IoIFP(io2))
2440 do_close(gv2, FALSE);
2445 do_close(gv1, FALSE);
2447 do_close(gv2, FALSE);
2449 TAINT_PROPER("socketpair");
2450 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2452 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2453 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2454 IoTYPE(io1) = IoTYPE_SOCKET;
2455 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2456 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2457 IoTYPE(io2) = IoTYPE_SOCKET;
2458 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2459 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2460 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2461 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2462 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2463 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2464 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2467 #if defined(HAS_FCNTL) && defined(F_SETFD)
2468 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2469 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2474 DIE(aTHX_ PL_no_sock_func, "socketpair");
2482 SV * const addrsv = POPs;
2483 /* OK, so on what platform does bind modify addr? */
2485 GV * const gv = MUTABLE_GV(POPs);
2486 register IO * const io = GvIOn(gv);
2489 if (!io || !IoIFP(io))
2492 addr = SvPV_const(addrsv, len);
2493 TAINT_PROPER("bind");
2494 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2500 if (ckWARN(WARN_CLOSED))
2501 report_evil_fh(gv, io, PL_op->op_type);
2502 SETERRNO(EBADF,SS_IVCHAN);
2505 DIE(aTHX_ PL_no_sock_func, "bind");
2513 SV * const addrsv = POPs;
2514 GV * const gv = MUTABLE_GV(POPs);
2515 register IO * const io = GvIOn(gv);
2519 if (!io || !IoIFP(io))
2522 addr = SvPV_const(addrsv, len);
2523 TAINT_PROPER("connect");
2524 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2530 if (ckWARN(WARN_CLOSED))
2531 report_evil_fh(gv, io, PL_op->op_type);
2532 SETERRNO(EBADF,SS_IVCHAN);
2535 DIE(aTHX_ PL_no_sock_func, "connect");
2543 const int backlog = POPi;
2544 GV * const gv = MUTABLE_GV(POPs);
2545 register IO * const io = gv ? GvIOn(gv) : NULL;
2547 if (!gv || !io || !IoIFP(io))
2550 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2556 if (ckWARN(WARN_CLOSED))
2557 report_evil_fh(gv, io, PL_op->op_type);
2558 SETERRNO(EBADF,SS_IVCHAN);
2561 DIE(aTHX_ PL_no_sock_func, "listen");
2571 char namebuf[MAXPATHLEN];
2572 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2573 Sock_size_t len = sizeof (struct sockaddr_in);
2575 Sock_size_t len = sizeof namebuf;
2577 GV * const ggv = MUTABLE_GV(POPs);
2578 GV * const ngv = MUTABLE_GV(POPs);
2587 if (!gstio || !IoIFP(gstio))
2591 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2594 /* Some platforms indicate zero length when an AF_UNIX client is
2595 * not bound. Simulate a non-zero-length sockaddr structure in
2597 namebuf[0] = 0; /* sun_len */
2598 namebuf[1] = AF_UNIX; /* sun_family */
2606 do_close(ngv, FALSE);
2607 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2608 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2609 IoTYPE(nstio) = IoTYPE_SOCKET;
2610 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2611 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2612 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2613 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2616 #if defined(HAS_FCNTL) && defined(F_SETFD)
2617 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2621 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2622 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2624 #ifdef __SCO_VERSION__
2625 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2628 PUSHp(namebuf, len);
2632 if (ckWARN(WARN_CLOSED))
2633 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2634 SETERRNO(EBADF,SS_IVCHAN);
2640 DIE(aTHX_ PL_no_sock_func, "accept");
2648 const int how = POPi;
2649 GV * const gv = MUTABLE_GV(POPs);
2650 register IO * const io = GvIOn(gv);
2652 if (!io || !IoIFP(io))
2655 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2659 if (ckWARN(WARN_CLOSED))
2660 report_evil_fh(gv, io, PL_op->op_type);
2661 SETERRNO(EBADF,SS_IVCHAN);
2664 DIE(aTHX_ PL_no_sock_func, "shutdown");
2672 const int optype = PL_op->op_type;
2673 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2674 const unsigned int optname = (unsigned int) POPi;
2675 const unsigned int lvl = (unsigned int) POPi;
2676 GV * const gv = MUTABLE_GV(POPs);
2677 register IO * const io = GvIOn(gv);
2681 if (!io || !IoIFP(io))
2684 fd = PerlIO_fileno(IoIFP(io));
2688 (void)SvPOK_only(sv);
2692 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2699 #if defined(__SYMBIAN32__)
2700 # define SETSOCKOPT_OPTION_VALUE_T void *
2702 # define SETSOCKOPT_OPTION_VALUE_T const char *
2704 /* XXX TODO: We need to have a proper type (a Configure probe,
2705 * etc.) for what the C headers think of the third argument of
2706 * setsockopt(), the option_value read-only buffer: is it
2707 * a "char *", or a "void *", const or not. Some compilers
2708 * don't take kindly to e.g. assuming that "char *" implicitly
2709 * promotes to a "void *", or to explicitly promoting/demoting
2710 * consts to non/vice versa. The "const void *" is the SUS
2711 * definition, but that does not fly everywhere for the above
2713 SETSOCKOPT_OPTION_VALUE_T buf;
2717 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2721 aint = (int)SvIV(sv);
2722 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2725 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2734 if (ckWARN(WARN_CLOSED))
2735 report_evil_fh(gv, io, optype);
2736 SETERRNO(EBADF,SS_IVCHAN);
2741 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2749 const int optype = PL_op->op_type;
2750 GV * const gv = MUTABLE_GV(POPs);
2751 register IO * const io = GvIOn(gv);
2756 if (!io || !IoIFP(io))
2759 sv = sv_2mortal(newSV(257));
2760 (void)SvPOK_only(sv);
2764 fd = PerlIO_fileno(IoIFP(io));
2766 case OP_GETSOCKNAME:
2767 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2770 case OP_GETPEERNAME:
2771 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2773 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2775 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";
2776 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2777 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2778 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2779 sizeof(u_short) + sizeof(struct in_addr))) {
2786 #ifdef BOGUS_GETNAME_RETURN
2787 /* Interactive Unix, getpeername() and getsockname()
2788 does not return valid namelen */
2789 if (len == BOGUS_GETNAME_RETURN)
2790 len = sizeof(struct sockaddr);
2798 if (ckWARN(WARN_CLOSED))
2799 report_evil_fh(gv, io, optype);
2800 SETERRNO(EBADF,SS_IVCHAN);
2805 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2820 if (PL_op->op_flags & OPf_REF) {
2822 if (PL_op->op_type == OP_LSTAT) {
2823 if (gv != PL_defgv) {
2824 do_fstat_warning_check:
2825 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2826 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2827 } else if (PL_laststype != OP_LSTAT)
2828 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2832 if (gv != PL_defgv) {
2833 PL_laststype = OP_STAT;
2835 sv_setpvs(PL_statname, "");
2842 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2843 } else if (IoDIRP(io)) {
2845 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2847 PL_laststatval = -1;
2853 if (PL_laststatval < 0) {
2854 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2855 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2860 SV* const sv = POPs;
2861 if (isGV_with_GP(sv)) {
2862 gv = MUTABLE_GV(sv);
2864 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2865 gv = MUTABLE_GV(SvRV(sv));
2866 if (PL_op->op_type == OP_LSTAT)
2867 goto do_fstat_warning_check;
2869 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2870 io = MUTABLE_IO(SvRV(sv));
2871 if (PL_op->op_type == OP_LSTAT)
2872 goto do_fstat_warning_check;
2873 goto do_fstat_have_io;
2876 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2878 PL_laststype = PL_op->op_type;
2879 if (PL_op->op_type == OP_LSTAT)
2880 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2882 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2883 if (PL_laststatval < 0) {
2884 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2885 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2891 if (gimme != G_ARRAY) {
2892 if (gimme != G_VOID)
2893 XPUSHs(boolSV(max));
2899 mPUSHi(PL_statcache.st_dev);
2900 mPUSHi(PL_statcache.st_ino);
2901 mPUSHu(PL_statcache.st_mode);
2902 mPUSHu(PL_statcache.st_nlink);
2903 #if Uid_t_size > IVSIZE
2904 mPUSHn(PL_statcache.st_uid);
2906 # if Uid_t_sign <= 0
2907 mPUSHi(PL_statcache.st_uid);
2909 mPUSHu(PL_statcache.st_uid);
2912 #if Gid_t_size > IVSIZE
2913 mPUSHn(PL_statcache.st_gid);
2915 # if Gid_t_sign <= 0
2916 mPUSHi(PL_statcache.st_gid);
2918 mPUSHu(PL_statcache.st_gid);
2921 #ifdef USE_STAT_RDEV
2922 mPUSHi(PL_statcache.st_rdev);
2924 PUSHs(newSVpvs_flags("", SVs_TEMP));
2926 #if Off_t_size > IVSIZE
2927 mPUSHn(PL_statcache.st_size);
2929 mPUSHi(PL_statcache.st_size);
2932 mPUSHn(PL_statcache.st_atime);
2933 mPUSHn(PL_statcache.st_mtime);
2934 mPUSHn(PL_statcache.st_ctime);
2936 mPUSHi(PL_statcache.st_atime);
2937 mPUSHi(PL_statcache.st_mtime);
2938 mPUSHi(PL_statcache.st_ctime);
2940 #ifdef USE_STAT_BLOCKS
2941 mPUSHu(PL_statcache.st_blksize);
2942 mPUSHu(PL_statcache.st_blocks);
2944 PUSHs(newSVpvs_flags("", SVs_TEMP));
2945 PUSHs(newSVpvs_flags("", SVs_TEMP));
2951 /* This macro is used by the stacked filetest operators :
2952 * if the previous filetest failed, short-circuit and pass its value.
2953 * Else, discard it from the stack and continue. --rgs
2955 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2956 if (!SvTRUE(TOPs)) { RETURN; } \
2957 else { (void)POPs; PUTBACK; } \
2964 /* Not const, because things tweak this below. Not bool, because there's
2965 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2966 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2967 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2968 /* Giving some sort of initial value silences compilers. */
2970 int access_mode = R_OK;
2972 int access_mode = 0;
2975 /* access_mode is never used, but leaving use_access in makes the
2976 conditional compiling below much clearer. */
2979 int stat_mode = S_IRUSR;
2981 bool effective = FALSE;
2985 switch (PL_op->op_type) {
2986 case OP_FTRREAD: opchar = 'R'; break;
2987 case OP_FTRWRITE: opchar = 'W'; break;
2988 case OP_FTREXEC: opchar = 'X'; break;
2989 case OP_FTEREAD: opchar = 'r'; break;
2990 case OP_FTEWRITE: opchar = 'w'; break;
2991 case OP_FTEEXEC: opchar = 'x'; break;
2993 tryAMAGICftest(opchar);
2995 STACKED_FTEST_CHECK;
2997 switch (PL_op->op_type) {
2999 #if !(defined(HAS_ACCESS) && defined(R_OK))
3005 #if defined(HAS_ACCESS) && defined(W_OK)
3010 stat_mode = S_IWUSR;
3014 #if defined(HAS_ACCESS) && defined(X_OK)
3019 stat_mode = S_IXUSR;
3023 #ifdef PERL_EFF_ACCESS
3026 stat_mode = S_IWUSR;
3030 #ifndef PERL_EFF_ACCESS
3037 #ifdef PERL_EFF_ACCESS
3042 stat_mode = S_IXUSR;
3048 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3049 const char *name = POPpx;
3051 # ifdef PERL_EFF_ACCESS
3052 result = PERL_EFF_ACCESS(name, access_mode);
3054 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3060 result = access(name, access_mode);
3062 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3077 if (cando(stat_mode, effective, &PL_statcache))
3086 const int op_type = PL_op->op_type;
3091 case OP_FTIS: opchar = 'e'; break;
3092 case OP_FTSIZE: opchar = 's'; break;
3093 case OP_FTMTIME: opchar = 'M'; break;
3094 case OP_FTCTIME: opchar = 'C'; break;
3095 case OP_FTATIME: opchar = 'A'; break;
3097 tryAMAGICftest(opchar);
3099 STACKED_FTEST_CHECK;
3105 if (op_type == OP_FTIS)
3108 /* You can't dTARGET inside OP_FTIS, because you'll get
3109 "panic: pad_sv po" - the op is not flagged to have a target. */
3113 #if Off_t_size > IVSIZE
3114 PUSHn(PL_statcache.st_size);
3116 PUSHi(PL_statcache.st_size);
3120 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3123 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3126 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3140 switch (PL_op->op_type) {
3141 case OP_FTROWNED: opchar = 'O'; break;
3142 case OP_FTEOWNED: opchar = 'o'; break;
3143 case OP_FTZERO: opchar = 'z'; break;
3144 case OP_FTSOCK: opchar = 'S'; break;
3145 case OP_FTCHR: opchar = 'c'; break;
3146 case OP_FTBLK: opchar = 'b'; break;
3147 case OP_FTFILE: opchar = 'f'; break;
3148 case OP_FTDIR: opchar = 'd'; break;
3149 case OP_FTPIPE: opchar = 'p'; break;
3150 case OP_FTSUID: opchar = 'u'; break;
3151 case OP_FTSGID: opchar = 'g'; break;
3152 case OP_FTSVTX: opchar = 'k'; break;
3154 tryAMAGICftest(opchar);
3156 /* I believe that all these three are likely to be defined on most every
3157 system these days. */
3159 if(PL_op->op_type == OP_FTSUID)
3163 if(PL_op->op_type == OP_FTSGID)
3167 if(PL_op->op_type == OP_FTSVTX)
3171 STACKED_FTEST_CHECK;
3177 switch (PL_op->op_type) {
3179 if (PL_statcache.st_uid == PL_uid)
3183 if (PL_statcache.st_uid == PL_euid)
3187 if (PL_statcache.st_size == 0)
3191 if (S_ISSOCK(PL_statcache.st_mode))
3195 if (S_ISCHR(PL_statcache.st_mode))
3199 if (S_ISBLK(PL_statcache.st_mode))
3203 if (S_ISREG(PL_statcache.st_mode))
3207 if (S_ISDIR(PL_statcache.st_mode))
3211 if (S_ISFIFO(PL_statcache.st_mode))
3216 if (PL_statcache.st_mode & S_ISUID)
3222 if (PL_statcache.st_mode & S_ISGID)
3228 if (PL_statcache.st_mode & S_ISVTX)
3242 tryAMAGICftest('l');
3243 result = my_lstat();
3248 if (S_ISLNK(PL_statcache.st_mode))
3261 tryAMAGICftest('t');
3263 STACKED_FTEST_CHECK;
3265 if (PL_op->op_flags & OPf_REF)
3267 else if (isGV(TOPs))
3268 gv = MUTABLE_GV(POPs);
3269 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3270 gv = MUTABLE_GV(SvRV(POPs));
3272 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3274 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3275 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3276 else if (tmpsv && SvOK(tmpsv)) {
3277 const char *tmps = SvPV_nolen_const(tmpsv);
3285 if (PerlLIO_isatty(fd))
3290 #if defined(atarist) /* this will work with atariST. Configure will
3291 make guesses for other systems. */
3292 # define FILE_base(f) ((f)->_base)
3293 # define FILE_ptr(f) ((f)->_ptr)
3294 # define FILE_cnt(f) ((f)->_cnt)
3295 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3306 register STDCHAR *s;
3312 tryAMAGICftest(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3314 STACKED_FTEST_CHECK;
3316 if (PL_op->op_flags & OPf_REF)
3318 else if (isGV(TOPs))
3319 gv = MUTABLE_GV(POPs);
3320 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3321 gv = MUTABLE_GV(SvRV(POPs));
3327 if (gv == PL_defgv) {
3329 io = GvIO(PL_statgv);
3332 goto really_filename;
3337 PL_laststatval = -1;
3338 sv_setpvs(PL_statname, "");
3339 io = GvIO(PL_statgv);
3341 if (io && IoIFP(io)) {
3342 if (! PerlIO_has_base(IoIFP(io)))
3343 DIE(aTHX_ "-T and -B not implemented on filehandles");
3344 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3345 if (PL_laststatval < 0)
3347 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3348 if (PL_op->op_type == OP_FTTEXT)
3353 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3354 i = PerlIO_getc(IoIFP(io));
3356 (void)PerlIO_ungetc(IoIFP(io),i);
3358 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3360 len = PerlIO_get_bufsiz(IoIFP(io));
3361 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3362 /* sfio can have large buffers - limit to 512 */
3367 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3369 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3371 SETERRNO(EBADF,RMS_IFI);
3379 PL_laststype = OP_STAT;
3380 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3381 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3382 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3384 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3387 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3388 if (PL_laststatval < 0) {
3389 (void)PerlIO_close(fp);
3392 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3393 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3394 (void)PerlIO_close(fp);
3396 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3397 RETPUSHNO; /* special case NFS directories */
3398 RETPUSHYES; /* null file is anything */
3403 /* now scan s to look for textiness */
3404 /* XXX ASCII dependent code */
3406 #if defined(DOSISH) || defined(USEMYBINMODE)
3407 /* ignore trailing ^Z on short files */
3408 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3412 for (i = 0; i < len; i++, s++) {
3413 if (!*s) { /* null never allowed in text */
3418 else if (!(isPRINT(*s) || isSPACE(*s)))
3421 else if (*s & 128) {
3423 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3426 /* utf8 characters don't count as odd */
3427 if (UTF8_IS_START(*s)) {
3428 int ulen = UTF8SKIP(s);
3429 if (ulen < len - i) {
3431 for (j = 1; j < ulen; j++) {
3432 if (!UTF8_IS_CONTINUATION(s[j]))
3435 --ulen; /* loop does extra increment */
3445 *s != '\n' && *s != '\r' && *s != '\b' &&
3446 *s != '\t' && *s != '\f' && *s != 27)
3451 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3462 const char *tmps = NULL;
3466 SV * const sv = POPs;
3467 if (PL_op->op_flags & OPf_SPECIAL) {
3468 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3470 else if (isGV_with_GP(sv)) {
3471 gv = MUTABLE_GV(sv);
3473 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3474 gv = MUTABLE_GV(SvRV(sv));
3477 tmps = SvPV_nolen_const(sv);
3481 if( !gv && (!tmps || !*tmps) ) {
3482 HV * const table = GvHVn(PL_envgv);
3485 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3486 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3488 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3493 deprecate("chdir('') or chdir(undef) as chdir()");
3494 tmps = SvPV_nolen_const(*svp);
3498 TAINT_PROPER("chdir");
3503 TAINT_PROPER("chdir");
3506 IO* const io = GvIO(gv);
3509 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3510 } else if (IoIFP(io)) {
3511 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3514 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3515 report_evil_fh(gv, io, PL_op->op_type);
3516 SETERRNO(EBADF, RMS_IFI);
3521 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3522 report_evil_fh(gv, io, PL_op->op_type);
3523 SETERRNO(EBADF,RMS_IFI);
3527 DIE(aTHX_ PL_no_func, "fchdir");
3531 PUSHi( PerlDir_chdir(tmps) >= 0 );
3533 /* Clear the DEFAULT element of ENV so we'll get the new value
3535 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3542 dVAR; dSP; dMARK; dTARGET;
3543 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3554 char * const tmps = POPpx;
3555 TAINT_PROPER("chroot");
3556 PUSHi( chroot(tmps) >= 0 );
3559 DIE(aTHX_ PL_no_func, "chroot");
3567 const char * const tmps2 = POPpconstx;
3568 const char * const tmps = SvPV_nolen_const(TOPs);
3569 TAINT_PROPER("rename");
3571 anum = PerlLIO_rename(tmps, tmps2);
3573 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3574 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3577 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3578 (void)UNLINK(tmps2);
3579 if (!(anum = link(tmps, tmps2)))
3580 anum = UNLINK(tmps);
3588 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3592 const int op_type = PL_op->op_type;
3596 if (op_type == OP_LINK)
3597 DIE(aTHX_ PL_no_func, "link");
3599 # ifndef HAS_SYMLINK
3600 if (op_type == OP_SYMLINK)
3601 DIE(aTHX_ PL_no_func, "symlink");
3605 const char * const tmps2 = POPpconstx;
3606 const char * const tmps = SvPV_nolen_const(TOPs);
3607 TAINT_PROPER(PL_op_desc[op_type]);
3609 # if defined(HAS_LINK)
3610 # if defined(HAS_SYMLINK)
3611 /* Both present - need to choose which. */
3612 (op_type == OP_LINK) ?
3613 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3615 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3616 PerlLIO_link(tmps, tmps2);
3619 # if defined(HAS_SYMLINK)
3620 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3621 symlink(tmps, tmps2);
3626 SETi( result >= 0 );
3633 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3644 char buf[MAXPATHLEN];
3647 #ifndef INCOMPLETE_TAINTS
3651 len = readlink(tmps, buf, sizeof(buf) - 1);
3659 RETSETUNDEF; /* just pretend it's a normal file */
3663 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3665 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3667 char * const save_filename = filename;
3672 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3674 PERL_ARGS_ASSERT_DOONELINER;
3676 Newx(cmdline, size, char);
3677 my_strlcpy(cmdline, cmd, size);
3678 my_strlcat(cmdline, " ", size);
3679 for (s = cmdline + strlen(cmdline); *filename; ) {
3683 if (s - cmdline < size)
3684 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3685 myfp = PerlProc_popen(cmdline, "r");
3689 SV * const tmpsv = sv_newmortal();
3690 /* Need to save/restore 'PL_rs' ?? */
3691 s = sv_gets(tmpsv, myfp, 0);
3692 (void)PerlProc_pclose(myfp);
3696 #ifdef HAS_SYS_ERRLIST
3701 /* you don't see this */
3702 const char * const errmsg =
3703 #ifdef HAS_SYS_ERRLIST
3711 if (instr(s, errmsg)) {
3718 #define EACCES EPERM
3720 if (instr(s, "cannot make"))
3721 SETERRNO(EEXIST,RMS_FEX);
3722 else if (instr(s, "existing file"))
3723 SETERRNO(EEXIST,RMS_FEX);
3724 else if (instr(s, "ile exists"))
3725 SETERRNO(EEXIST,RMS_FEX);
3726 else if (instr(s, "non-exist"))
3727 SETERRNO(ENOENT,RMS_FNF);
3728 else if (instr(s, "does not exist"))
3729 SETERRNO(ENOENT,RMS_FNF);
3730 else if (instr(s, "not empty"))
3731 SETERRNO(EBUSY,SS_DEVOFFLINE);
3732 else if (instr(s, "cannot access"))
3733 SETERRNO(EACCES,RMS_PRV);
3735 SETERRNO(EPERM,RMS_PRV);
3738 else { /* some mkdirs return no failure indication */
3739 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3740 if (PL_op->op_type == OP_RMDIR)
3745 SETERRNO(EACCES,RMS_PRV); /* a guess */
3754 /* This macro removes trailing slashes from a directory name.
3755 * Different operating and file systems take differently to
3756 * trailing slashes. According to POSIX 1003.1 1996 Edition
3757 * any number of trailing slashes should be allowed.
3758 * Thusly we snip them away so that even non-conforming
3759 * systems are happy.
3760 * We should probably do this "filtering" for all
3761 * the functions that expect (potentially) directory names:
3762 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3763 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3765 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3766 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3769 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3770 (tmps) = savepvn((tmps), (len)); \
3780 const int mode = (MAXARG > 1) ? POPi : 0777;
3782 TRIMSLASHES(tmps,len,copy);
3784 TAINT_PROPER("mkdir");
3786 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3790 SETi( dooneliner("mkdir", tmps) );
3791 oldumask = PerlLIO_umask(0);
3792 PerlLIO_umask(oldumask);
3793 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3808 TRIMSLASHES(tmps,len,copy);
3809 TAINT_PROPER("rmdir");
3811 SETi( PerlDir_rmdir(tmps) >= 0 );
3813 SETi( dooneliner("rmdir", tmps) );
3820 /* Directory calls. */
3824 #if defined(Direntry_t) && defined(HAS_READDIR)
3826 const char * const dirname = POPpconstx;
3827 GV * const gv = MUTABLE_GV(POPs);
3828 register IO * const io = GvIOn(gv);
3833 if ((IoIFP(io) || IoOFP(io)))
3834 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3835 "Opening filehandle %s also as a directory",
3838 PerlDir_close(IoDIRP(io));
3839 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3845 SETERRNO(EBADF,RMS_DIR);
3848 DIE(aTHX_ PL_no_dir_func, "opendir");
3854 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3855 DIE(aTHX_ PL_no_dir_func, "readdir");
3857 #if !defined(I_DIRENT) && !defined(VMS)
3858 Direntry_t *readdir (DIR *);
3864 const I32 gimme = GIMME;
3865 GV * const gv = MUTABLE_GV(POPs);
3866 register const Direntry_t *dp;
3867 register IO * const io = GvIOn(gv);
3869 if (!io || !IoDIRP(io)) {
3870 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3871 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3876 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3880 sv = newSVpvn(dp->d_name, dp->d_namlen);
3882 sv = newSVpv(dp->d_name, 0);
3884 #ifndef INCOMPLETE_TAINTS
3885 if (!(IoFLAGS(io) & IOf_UNTAINT))
3889 } while (gimme == G_ARRAY);
3891 if (!dp && gimme != G_ARRAY)
3898 SETERRNO(EBADF,RMS_ISI);
3899 if (GIMME == G_ARRAY)
3908 #if defined(HAS_TELLDIR) || defined(telldir)
3910 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3911 /* XXX netbsd still seemed to.
3912 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3913 --JHI 1999-Feb-02 */
3914 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3915 long telldir (DIR *);
3917 GV * const gv = MUTABLE_GV(POPs);
3918 register IO * const io = GvIOn(gv);
3920 if (!io || !IoDIRP(io)) {
3921 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3922 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3926 PUSHi( PerlDir_tell(IoDIRP(io)) );
3930 SETERRNO(EBADF,RMS_ISI);
3933 DIE(aTHX_ PL_no_dir_func, "telldir");
3939 #if defined(HAS_SEEKDIR) || defined(seekdir)
3941 const long along = POPl;
3942 GV * const gv = MUTABLE_GV(POPs);
3943 register IO * const io = GvIOn(gv);
3945 if (!io || !IoDIRP(io)) {
3946 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3947 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3950 (void)PerlDir_seek(IoDIRP(io), along);
3955 SETERRNO(EBADF,RMS_ISI);
3958 DIE(aTHX_ PL_no_dir_func, "seekdir");
3964 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3966 GV * const gv = MUTABLE_GV(POPs);
3967 register IO * const io = GvIOn(gv);
3969 if (!io || !IoDIRP(io)) {
3970 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3971 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3974 (void)PerlDir_rewind(IoDIRP(io));
3978 SETERRNO(EBADF,RMS_ISI);
3981 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3987 #if defined(Direntry_t) && defined(HAS_READDIR)
3989 GV * const gv = MUTABLE_GV(POPs);
3990 register IO * const io = GvIOn(gv);
3992 if (!io || !IoDIRP(io)) {
3993 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3994 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3997 #ifdef VOID_CLOSEDIR
3998 PerlDir_close(IoDIRP(io));
4000 if (PerlDir_close(IoDIRP(io)) < 0) {
4001 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4010 SETERRNO(EBADF,RMS_IFI);
4013 DIE(aTHX_ PL_no_dir_func, "closedir");
4017 /* Process control. */
4026 PERL_FLUSHALL_FOR_CHILD;
4027 childpid = PerlProc_fork();
4031 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4033 SvREADONLY_off(GvSV(tmpgv));
4034 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4035 SvREADONLY_on(GvSV(tmpgv));
4037 #ifdef THREADS_HAVE_PIDS
4038 PL_ppid = (IV)getppid();
4040 #ifdef PERL_USES_PL_PIDSTATUS
4041 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4047 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4052 PERL_FLUSHALL_FOR_CHILD;
4053 childpid = PerlProc_fork();
4059 DIE(aTHX_ PL_no_func, "fork");
4066 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4071 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4072 childpid = wait4pid(-1, &argflags, 0);
4074 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4079 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4080 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4081 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4083 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4088 DIE(aTHX_ PL_no_func, "wait");
4094 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4096 const int optype = POPi;
4097 const Pid_t pid = TOPi;
4101 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4102 result = wait4pid(pid, &argflags, optype);
4104 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4109 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4110 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4111 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4113 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4118 DIE(aTHX_ PL_no_func, "waitpid");
4124 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4125 #if defined(__LIBCATAMOUNT__)
4126 PL_statusvalue = -1;
4135 while (++MARK <= SP) {
4136 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4141 TAINT_PROPER("system");
4143 PERL_FLUSHALL_FOR_CHILD;
4144 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4150 if (PerlProc_pipe(pp) >= 0)
4152 while ((childpid = PerlProc_fork()) == -1) {
4153 if (errno != EAGAIN) {
4158 PerlLIO_close(pp[0]);
4159 PerlLIO_close(pp[1]);
4166 Sigsave_t ihand,qhand; /* place to save signals during system() */
4170 PerlLIO_close(pp[1]);
4172 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4173 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4176 result = wait4pid(childpid, &status, 0);
4177 } while (result == -1 && errno == EINTR);
4179 (void)rsignal_restore(SIGINT, &ihand);
4180 (void)rsignal_restore(SIGQUIT, &qhand);
4182 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4183 do_execfree(); /* free any memory child malloced on fork */
4190 while (n < sizeof(int)) {
4191 n1 = PerlLIO_read(pp[0],
4192 (void*)(((char*)&errkid)+n),
4198 PerlLIO_close(pp[0]);
4199 if (n) { /* Error */
4200 if (n != sizeof(int))
4201 DIE(aTHX_ "panic: kid popen errno read");
4202 errno = errkid; /* Propagate errno from kid */
4203 STATUS_NATIVE_CHILD_SET(-1);
4206 XPUSHi(STATUS_CURRENT);
4210 PerlLIO_close(pp[0]);
4211 #if defined(HAS_FCNTL) && defined(F_SETFD)
4212 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4215 if (PL_op->op_flags & OPf_STACKED) {
4216 SV * const really = *++MARK;
4217 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4219 else if (SP - MARK != 1)
4220 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4222 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4226 #else /* ! FORK or VMS or OS/2 */
4229 if (PL_op->op_flags & OPf_STACKED) {
4230 SV * const really = *++MARK;
4231 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4232 value = (I32)do_aspawn(really, MARK, SP);
4234 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4237 else if (SP - MARK != 1) {
4238 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4239 value = (I32)do_aspawn(NULL, MARK, SP);
4241 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4245 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4247 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4249 STATUS_NATIVE_CHILD_SET(value);
4252 XPUSHi(result ? value : STATUS_CURRENT);
4253 #endif /* !FORK or VMS or OS/2 */
4260 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4265 while (++MARK <= SP) {
4266 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4271 TAINT_PROPER("exec");
4273 PERL_FLUSHALL_FOR_CHILD;
4274 if (PL_op->op_flags & OPf_STACKED) {
4275 SV * const really = *++MARK;
4276 value = (I32)do_aexec(really, MARK, SP);
4278 else if (SP - MARK != 1)
4280 value = (I32)vms_do_aexec(NULL, MARK, SP);
4284 (void ) do_aspawn(NULL, MARK, SP);
4288 value = (I32)do_aexec(NULL, MARK, SP);
4293 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4296 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4299 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4313 # ifdef THREADS_HAVE_PIDS
4314 if (PL_ppid != 1 && getppid() == 1)
4315 /* maybe the parent process has died. Refresh ppid cache */
4319 XPUSHi( getppid() );
4323 DIE(aTHX_ PL_no_func, "getppid");
4332 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4335 pgrp = (I32)BSD_GETPGRP(pid);
4337 if (pid != 0 && pid != PerlProc_getpid())
4338 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4344 DIE(aTHX_ PL_no_func, "getpgrp()");
4364 TAINT_PROPER("setpgrp");
4366 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4368 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4369 || (pid != 0 && pid != PerlProc_getpid()))
4371 DIE(aTHX_ "setpgrp can't take arguments");
4373 SETi( setpgrp() >= 0 );
4374 #endif /* USE_BSDPGRP */
4377 DIE(aTHX_ PL_no_func, "setpgrp()");
4383 #ifdef HAS_GETPRIORITY
4385 const int who = POPi;
4386 const int which = TOPi;
4387 SETi( getpriority(which, who) );
4390 DIE(aTHX_ PL_no_func, "getpriority()");
4396 #ifdef HAS_SETPRIORITY
4398 const int niceval = POPi;
4399 const int who = POPi;
4400 const int which = TOPi;
4401 TAINT_PROPER("setpriority");
4402 SETi( setpriority(which, who, niceval) >= 0 );
4405 DIE(aTHX_ PL_no_func, "setpriority()");
4415 XPUSHn( time(NULL) );
4417 XPUSHi( time(NULL) );
4429 (void)PerlProc_times(&PL_timesbuf);
4431 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4432 /* struct tms, though same data */
4436 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4437 if (GIMME == G_ARRAY) {
4438 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4439 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4440 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4448 if (GIMME == G_ARRAY) {
4455 DIE(aTHX_ "times not implemented");
4457 #endif /* HAS_TIMES */
4467 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4468 static const char * const dayname[] =
4469 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4470 static const char * const monname[] =
4471 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4472 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4477 when = (Time64_T)now;
4480 double input = Perl_floor(POPn);
4481 when = (Time64_T)input;
4482 if (when != input) {
4483 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4484 "%s(%.0f) too large", opname, input);
4488 if (PL_op->op_type == OP_LOCALTIME)
4489 err = S_localtime64_r(&when, &tmbuf);
4491 err = S_gmtime64_r(&when, &tmbuf);
4494 /* XXX %lld broken for quads */
4495 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4496 "%s(%.0f) failed", opname, (double)when);
4499 if (GIMME != G_ARRAY) { /* scalar context */
4501 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4502 double year = (double)tmbuf.tm_year + 1900;
4509 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4510 dayname[tmbuf.tm_wday],
4511 monname[tmbuf.tm_mon],
4519 else { /* list context */
4525 mPUSHi(tmbuf.tm_sec);
4526 mPUSHi(tmbuf.tm_min);
4527 mPUSHi(tmbuf.tm_hour);
4528 mPUSHi(tmbuf.tm_mday);
4529 mPUSHi(tmbuf.tm_mon);
4530 mPUSHn(tmbuf.tm_year);
4531 mPUSHi(tmbuf.tm_wday);
4532 mPUSHi(tmbuf.tm_yday);
4533 mPUSHi(tmbuf.tm_isdst);
4544 anum = alarm((unsigned int)anum);
4551 DIE(aTHX_ PL_no_func, "alarm");
4562 (void)time(&lasttime);
4567 PerlProc_sleep((unsigned int)duration);
4570 XPUSHi(when - lasttime);
4574 /* Shared memory. */
4575 /* Merged with some message passing. */
4579 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4580 dVAR; dSP; dMARK; dTARGET;
4581 const int op_type = PL_op->op_type;
4586 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4589 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4592 value = (I32)(do_semop(MARK, SP) >= 0);
4595 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4611 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4612 dVAR; dSP; dMARK; dTARGET;
4613 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4620 DIE(aTHX_ "System V IPC is not implemented on this machine");
4626 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4627 dVAR; dSP; dMARK; dTARGET;
4628 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4636 PUSHp(zero_but_true, ZBTLEN);
4644 /* I can't const this further without getting warnings about the types of
4645 various arrays passed in from structures. */
4647 S_space_join_names_mortal(pTHX_ char *const *array)
4651 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4653 if (array && *array) {
4654 target = newSVpvs_flags("", SVs_TEMP);
4656 sv_catpv(target, *array);
4659 sv_catpvs(target, " ");
4662 target = sv_mortalcopy(&PL_sv_no);
4667 /* Get system info. */
4671 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4673 I32 which = PL_op->op_type;
4674 register char **elem;
4676 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4677 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4678 struct hostent *gethostbyname(Netdb_name_t);
4679 struct hostent *gethostent(void);
4681 struct hostent *hent;
4685 if (which == OP_GHBYNAME) {
4686 #ifdef HAS_GETHOSTBYNAME
4687 const char* const name = POPpbytex;
4688 hent = PerlSock_gethostbyname(name);
4690 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4693 else if (which == OP_GHBYADDR) {
4694 #ifdef HAS_GETHOSTBYADDR
4695 const int addrtype = POPi;
4696 SV * const addrsv = POPs;
4698 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4700 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4702 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4706 #ifdef HAS_GETHOSTENT
4707 hent = PerlSock_gethostent();
4709 DIE(aTHX_ PL_no_sock_func, "gethostent");
4712 #ifdef HOST_NOT_FOUND
4714 #ifdef USE_REENTRANT_API
4715 # ifdef USE_GETHOSTENT_ERRNO
4716 h_errno = PL_reentrant_buffer->_gethostent_errno;
4719 STATUS_UNIX_SET(h_errno);
4723 if (GIMME != G_ARRAY) {
4724 PUSHs(sv = sv_newmortal());
4726 if (which == OP_GHBYNAME) {
4728 sv_setpvn(sv, hent->h_addr, hent->h_length);
4731 sv_setpv(sv, (char*)hent->h_name);
4737 mPUSHs(newSVpv((char*)hent->h_name, 0));
4738 PUSHs(space_join_names_mortal(hent->h_aliases));
4739 mPUSHi(hent->h_addrtype);
4740 len = hent->h_length;
4743 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4744 mXPUSHp(*elem, len);
4748 mPUSHp(hent->h_addr, len);
4750 PUSHs(sv_mortalcopy(&PL_sv_no));
4755 DIE(aTHX_ PL_no_sock_func, "gethostent");
4761 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4763 I32 which = PL_op->op_type;
4765 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4766 struct netent *getnetbyaddr(Netdb_net_t, int);
4767 struct netent *getnetbyname(Netdb_name_t);
4768 struct netent *getnetent(void);
4770 struct netent *nent;
4772 if (which == OP_GNBYNAME){
4773 #ifdef HAS_GETNETBYNAME
4774 const char * const name = POPpbytex;
4775 nent = PerlSock_getnetbyname(name);
4777 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4780 else if (which == OP_GNBYADDR) {
4781 #ifdef HAS_GETNETBYADDR
4782 const int addrtype = POPi;
4783 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4784 nent = PerlSock_getnetbyaddr(addr, addrtype);
4786 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4790 #ifdef HAS_GETNETENT
4791 nent = PerlSock_getnetent();
4793 DIE(aTHX_ PL_no_sock_func, "getnetent");
4796 #ifdef HOST_NOT_FOUND
4798 #ifdef USE_REENTRANT_API
4799 # ifdef USE_GETNETENT_ERRNO
4800 h_errno = PL_reentrant_buffer->_getnetent_errno;
4803 STATUS_UNIX_SET(h_errno);
4808 if (GIMME != G_ARRAY) {
4809 PUSHs(sv = sv_newmortal());
4811 if (which == OP_GNBYNAME)
4812 sv_setiv(sv, (IV)nent->n_net);
4814 sv_setpv(sv, nent->n_name);
4820 mPUSHs(newSVpv(nent->n_name, 0));
4821 PUSHs(space_join_names_mortal(nent->n_aliases));
4822 mPUSHi(nent->n_addrtype);
4823 mPUSHi(nent->n_net);
4828 DIE(aTHX_ PL_no_sock_func, "getnetent");
4834 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4836 I32 which = PL_op->op_type;
4838 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4839 struct protoent *getprotobyname(Netdb_name_t);
4840 struct protoent *getprotobynumber(int);
4841 struct protoent *getprotoent(void);
4843 struct protoent *pent;
4845 if (which == OP_GPBYNAME) {
4846 #ifdef HAS_GETPROTOBYNAME
4847 const char* const name = POPpbytex;
4848 pent = PerlSock_getprotobyname(name);
4850 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4853 else if (which == OP_GPBYNUMBER) {
4854 #ifdef HAS_GETPROTOBYNUMBER
4855 const int number = POPi;
4856 pent = PerlSock_getprotobynumber(number);
4858 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4862 #ifdef HAS_GETPROTOENT
4863 pent = PerlSock_getprotoent();
4865 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4869 if (GIMME != G_ARRAY) {
4870 PUSHs(sv = sv_newmortal());
4872 if (which == OP_GPBYNAME)
4873 sv_setiv(sv, (IV)pent->p_proto);
4875 sv_setpv(sv, pent->p_name);
4881 mPUSHs(newSVpv(pent->p_name, 0));
4882 PUSHs(space_join_names_mortal(pent->p_aliases));
4883 mPUSHi(pent->p_proto);
4888 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4894 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4896 I32 which = PL_op->op_type;
4898 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4899 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4900 struct servent *getservbyport(int, Netdb_name_t);
4901 struct servent *getservent(void);
4903 struct servent *sent;
4905 if (which == OP_GSBYNAME) {
4906 #ifdef HAS_GETSERVBYNAME
4907 const char * const proto = POPpbytex;
4908 const char * const name = POPpbytex;
4909 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4911 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4914 else if (which == OP_GSBYPORT) {
4915 #ifdef HAS_GETSERVBYPORT
4916 const char * const proto = POPpbytex;
4917 unsigned short port = (unsigned short)POPu;
4919 port = PerlSock_htons(port);
4921 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4923 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4927 #ifdef HAS_GETSERVENT
4928 sent = PerlSock_getservent();
4930 DIE(aTHX_ PL_no_sock_func, "getservent");
4934 if (GIMME != G_ARRAY) {
4935 PUSHs(sv = sv_newmortal());
4937 if (which == OP_GSBYNAME) {
4939 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4941 sv_setiv(sv, (IV)(sent->s_port));
4945 sv_setpv(sv, sent->s_name);
4951 mPUSHs(newSVpv(sent->s_name, 0));
4952 PUSHs(space_join_names_mortal(sent->s_aliases));
4954 mPUSHi(PerlSock_ntohs(sent->s_port));
4956 mPUSHi(sent->s_port);
4958 mPUSHs(newSVpv(sent->s_proto, 0));
4963 DIE(aTHX_ PL_no_sock_func, "getservent");
4969 #ifdef HAS_SETHOSTENT
4971 PerlSock_sethostent(TOPi);
4974 DIE(aTHX_ PL_no_sock_func, "sethostent");
4980 #ifdef HAS_SETNETENT
4982 (void)PerlSock_setnetent(TOPi);
4985 DIE(aTHX_ PL_no_sock_func, "setnetent");
4991 #ifdef HAS_SETPROTOENT
4993 (void)PerlSock_setprotoent(TOPi);
4996 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5002 #ifdef HAS_SETSERVENT
5004 (void)PerlSock_setservent(TOPi);
5007 DIE(aTHX_ PL_no_sock_func, "setservent");
5013 #ifdef HAS_ENDHOSTENT
5015 PerlSock_endhostent();
5019 DIE(aTHX_ PL_no_sock_func, "endhostent");
5025 #ifdef HAS_ENDNETENT
5027 PerlSock_endnetent();
5031 DIE(aTHX_ PL_no_sock_func, "endnetent");
5037 #ifdef HAS_ENDPROTOENT
5039 PerlSock_endprotoent();
5043 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5049 #ifdef HAS_ENDSERVENT
5051 PerlSock_endservent();
5055 DIE(aTHX_ PL_no_sock_func, "endservent");
5063 I32 which = PL_op->op_type;
5065 struct passwd *pwent = NULL;
5067 * We currently support only the SysV getsp* shadow password interface.
5068 * The interface is declared in <shadow.h> and often one needs to link
5069 * with -lsecurity or some such.
5070 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5073 * AIX getpwnam() is clever enough to return the encrypted password
5074 * only if the caller (euid?) is root.
5076 * There are at least three other shadow password APIs. Many platforms
5077 * seem to contain more than one interface for accessing the shadow
5078 * password databases, possibly for compatibility reasons.
5079 * The getsp*() is by far he simplest one, the other two interfaces
5080 * are much more complicated, but also very similar to each other.
5085 * struct pr_passwd *getprpw*();
5086 * The password is in
5087 * char getprpw*(...).ufld.fd_encrypt[]
5088 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5093 * struct es_passwd *getespw*();
5094 * The password is in
5095 * char *(getespw*(...).ufld.fd_encrypt)
5096 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5099 * struct userpw *getuserpw();
5100 * The password is in
5101 * char *(getuserpw(...)).spw_upw_passwd
5102 * (but the de facto standard getpwnam() should work okay)
5104 * Mention I_PROT here so that Configure probes for it.
5106 * In HP-UX for getprpw*() the manual page claims that one should include
5107 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5108 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5109 * and pp_sys.c already includes <shadow.h> if there is such.
5111 * Note that <sys/security.h> is already probed for, but currently
5112 * it is only included in special cases.
5114 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5115 * be preferred interface, even though also the getprpw*() interface
5116 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5117 * One also needs to call set_auth_parameters() in main() before
5118 * doing anything else, whether one is using getespw*() or getprpw*().
5120 * Note that accessing the shadow databases can be magnitudes
5121 * slower than accessing the standard databases.
5126 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5127 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5128 * the pw_comment is left uninitialized. */
5129 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5135 const char* const name = POPpbytex;
5136 pwent = getpwnam(name);
5142 pwent = getpwuid(uid);
5146 # ifdef HAS_GETPWENT
5148 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5149 if (pwent) pwent = getpwnam(pwent->pw_name);
5152 DIE(aTHX_ PL_no_func, "getpwent");
5158 if (GIMME != G_ARRAY) {
5159 PUSHs(sv = sv_newmortal());
5161 if (which == OP_GPWNAM)
5162 # if Uid_t_sign <= 0
5163 sv_setiv(sv, (IV)pwent->pw_uid);
5165 sv_setuv(sv, (UV)pwent->pw_uid);
5168 sv_setpv(sv, pwent->pw_name);
5174 mPUSHs(newSVpv(pwent->pw_name, 0));
5178 /* If we have getspnam(), we try to dig up the shadow
5179 * password. If we are underprivileged, the shadow
5180 * interface will set the errno to EACCES or similar,
5181 * and return a null pointer. If this happens, we will
5182 * use the dummy password (usually "*" or "x") from the
5183 * standard password database.
5185 * In theory we could skip the shadow call completely
5186 * if euid != 0 but in practice we cannot know which
5187 * security measures are guarding the shadow databases
5188 * on a random platform.
5190 * Resist the urge to use additional shadow interfaces.
5191 * Divert the urge to writing an extension instead.
5194 /* Some AIX setups falsely(?) detect some getspnam(), which
5195 * has a different API than the Solaris/IRIX one. */
5196 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5199 const struct spwd * const spwent = getspnam(pwent->pw_name);
5200 /* Save and restore errno so that
5201 * underprivileged attempts seem
5202 * to have never made the unsccessful
5203 * attempt to retrieve the shadow password. */
5205 if (spwent && spwent->sp_pwdp)
5206 sv_setpv(sv, spwent->sp_pwdp);
5210 if (!SvPOK(sv)) /* Use the standard password, then. */
5211 sv_setpv(sv, pwent->pw_passwd);
5214 # ifndef INCOMPLETE_TAINTS
5215 /* passwd is tainted because user himself can diddle with it.
5216 * admittedly not much and in a very limited way, but nevertheless. */
5220 # if Uid_t_sign <= 0
5221 mPUSHi(pwent->pw_uid);
5223 mPUSHu(pwent->pw_uid);
5226 # if Uid_t_sign <= 0
5227 mPUSHi(pwent->pw_gid);
5229 mPUSHu(pwent->pw_gid);
5231 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5232 * because of the poor interface of the Perl getpw*(),
5233 * not because there's some standard/convention saying so.
5234 * A better interface would have been to return a hash,
5235 * but we are accursed by our history, alas. --jhi. */
5237 mPUSHi(pwent->pw_change);
5240 mPUSHi(pwent->pw_quota);
5243 mPUSHs(newSVpv(pwent->pw_age, 0));
5245 /* I think that you can never get this compiled, but just in case. */
5246 PUSHs(sv_mortalcopy(&PL_sv_no));
5251 /* pw_class and pw_comment are mutually exclusive--.
5252 * see the above note for pw_change, pw_quota, and pw_age. */
5254 mPUSHs(newSVpv(pwent->pw_class, 0));
5257 mPUSHs(newSVpv(pwent->pw_comment, 0));
5259 /* I think that you can never get this compiled, but just in case. */
5260 PUSHs(sv_mortalcopy(&PL_sv_no));
5265 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5267 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5269 # ifndef INCOMPLETE_TAINTS
5270 /* pw_gecos is tainted because user himself can diddle with it. */
5274 mPUSHs(newSVpv(pwent->pw_dir, 0));
5276 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5277 # ifndef INCOMPLETE_TAINTS
5278 /* pw_shell is tainted because user himself can diddle with it. */
5283 mPUSHi(pwent->pw_expire);
5288 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5294 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5299 DIE(aTHX_ PL_no_func, "setpwent");
5305 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5310 DIE(aTHX_ PL_no_func, "endpwent");
5318 const I32 which = PL_op->op_type;
5319 const struct group *grent;
5321 if (which == OP_GGRNAM) {
5322 const char* const name = POPpbytex;
5323 grent = (const struct group *)getgrnam(name);
5325 else if (which == OP_GGRGID) {
5326 const Gid_t gid = POPi;
5327 grent = (const struct group *)getgrgid(gid);
5331 grent = (struct group *)getgrent();
5333 DIE(aTHX_ PL_no_func, "getgrent");
5337 if (GIMME != G_ARRAY) {
5338 SV * const sv = sv_newmortal();
5342 if (which == OP_GGRNAM)
5344 sv_setiv(sv, (IV)grent->gr_gid);
5346 sv_setuv(sv, (UV)grent->gr_gid);
5349 sv_setpv(sv, grent->gr_name);
5355 mPUSHs(newSVpv(grent->gr_name, 0));
5358 mPUSHs(newSVpv(grent->gr_passwd, 0));
5360 PUSHs(sv_mortalcopy(&PL_sv_no));
5364 mPUSHi(grent->gr_gid);
5366 mPUSHu(grent->gr_gid);
5369 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5370 /* In UNICOS/mk (_CRAYMPP) the multithreading
5371 * versions (getgrnam_r, getgrgid_r)
5372 * seem to return an illegal pointer
5373 * as the group members list, gr_mem.
5374 * getgrent() doesn't even have a _r version
5375 * but the gr_mem is poisonous anyway.
5376 * So yes, you cannot get the list of group
5377 * members if building multithreaded in UNICOS/mk. */
5378 PUSHs(space_join_names_mortal(grent->gr_mem));
5384 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5390 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5395 DIE(aTHX_ PL_no_func, "setgrent");
5401 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5406 DIE(aTHX_ PL_no_func, "endgrent");
5416 if (!(tmps = PerlProc_getlogin()))
5418 PUSHp(tmps, strlen(tmps));
5421 DIE(aTHX_ PL_no_func, "getlogin");
5425 /* Miscellaneous. */
5430 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5431 register I32 items = SP - MARK;
5432 unsigned long a[20];
5437 while (++MARK <= SP) {
5438 if (SvTAINTED(*MARK)) {
5444 TAINT_PROPER("syscall");
5447 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5448 * or where sizeof(long) != sizeof(char*). But such machines will
5449 * not likely have syscall implemented either, so who cares?
5451 while (++MARK <= SP) {
5452 if (SvNIOK(*MARK) || !i)
5453 a[i++] = SvIV(*MARK);
5454 else if (*MARK == &PL_sv_undef)
5457 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5463 DIE(aTHX_ "Too many args to syscall");
5465 DIE(aTHX_ "Too few args to syscall");
5467 retval = syscall(a[0]);
5470 retval = syscall(a[0],a[1]);
5473 retval = syscall(a[0],a[1],a[2]);
5476 retval = syscall(a[0],a[1],a[2],a[3]);
5479 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5482 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5485 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5488 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5492 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5495 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5498 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5502 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5506 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5510 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5511 a[10],a[11],a[12],a[13]);
5513 #endif /* atarist */
5519 DIE(aTHX_ PL_no_func, "syscall");
5523 #ifdef FCNTL_EMULATE_FLOCK
5525 /* XXX Emulate flock() with fcntl().
5526 What's really needed is a good file locking module.
5530 fcntl_emulate_flock(int fd, int operation)
5534 switch (operation & ~LOCK_NB) {
5536 flock.l_type = F_RDLCK;
5539 flock.l_type = F_WRLCK;
5542 flock.l_type = F_UNLCK;
5548 flock.l_whence = SEEK_SET;
5549 flock.l_start = flock.l_len = (Off_t)0;
5551 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5554 #endif /* FCNTL_EMULATE_FLOCK */
5556 #ifdef LOCKF_EMULATE_FLOCK
5558 /* XXX Emulate flock() with lockf(). This is just to increase
5559 portability of scripts. The calls are not completely
5560 interchangeable. What's really needed is a good file
5564 /* The lockf() constants might have been defined in <unistd.h>.
5565 Unfortunately, <unistd.h> causes troubles on some mixed
5566 (BSD/POSIX) systems, such as SunOS 4.1.3.
5568 Further, the lockf() constants aren't POSIX, so they might not be
5569 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5570 just stick in the SVID values and be done with it. Sigh.
5574 # define F_ULOCK 0 /* Unlock a previously locked region */
5577 # define F_LOCK 1 /* Lock a region for exclusive use */
5580 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5583 # define F_TEST 3 /* Test a region for other processes locks */
5587 lockf_emulate_flock(int fd, int operation)
5593 /* flock locks entire file so for lockf we need to do the same */
5594 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5595 if (pos > 0) /* is seekable and needs to be repositioned */
5596 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5597 pos = -1; /* seek failed, so don't seek back afterwards */
5600 switch (operation) {
5602 /* LOCK_SH - get a shared lock */
5604 /* LOCK_EX - get an exclusive lock */
5606 i = lockf (fd, F_LOCK, 0);
5609 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5610 case LOCK_SH|LOCK_NB:
5611 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5612 case LOCK_EX|LOCK_NB:
5613 i = lockf (fd, F_TLOCK, 0);
5615 if ((errno == EAGAIN) || (errno == EACCES))
5616 errno = EWOULDBLOCK;
5619 /* LOCK_UN - unlock (non-blocking is a no-op) */
5621 case LOCK_UN|LOCK_NB:
5622 i = lockf (fd, F_ULOCK, 0);
5625 /* Default - can't decipher operation */
5632 if (pos > 0) /* need to restore position of the handle */
5633 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5638 #endif /* LOCKF_EMULATE_FLOCK */
5642 * c-indentation-style: bsd
5644 * indent-tabs-mode: t
5647 * ex: set ts=8 sts=4 sw=4 noet: