3 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 * 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * But only a short way ahead its floor and the walls on either side were
13 * cloven by a great fissure, out of which the red glare came, now leaping
14 * up, now dying down into darkness; and all the while far below there was
15 * a rumour and a trouble as of great engines throbbing and labouring.
17 * [p.945 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
20 /* This file contains system pp ("push/pop") functions that
21 * execute the opcodes that make up a perl program. A typical pp function
22 * expects to find its arguments on the stack, and usually pushes its
23 * results onto the stack, hence the 'pp' terminology. Each OP structure
24 * contains a pointer to the relevant pp_foo() function.
26 * By 'system', we mean ops which interact with the OS, such as pp_open().
30 #define PERL_IN_PP_SYS_C
36 /* Shadow password support for solaris - pdo@cs.umd.edu
37 * Not just Solaris: at least HP-UX, IRIX, Linux.
38 * The API is from SysV.
40 * There are at least two more shadow interfaces,
41 * see the comments in pp_gpwent().
45 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
46 * and another MAXINT from "perl.h" <- <sys/param.h>. */
53 # include <sys/wait.h>
57 # include <sys/resource.h>
66 # include <sys/select.h>
70 /* XXX Configure test needed.
71 h_errno might not be a simple 'int', especially for multi-threaded
72 applications, see "extern int errno in perl.h". Creating such
73 a test requires taking into account the differences between
74 compiling multithreaded and singlethreaded ($ccflags et al).
75 HOST_NOT_FOUND is typically defined in <netdb.h>.
77 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
86 struct passwd *getpwnam (char *);
87 struct passwd *getpwuid (Uid_t);
92 struct passwd *getpwent (void);
93 #elif defined (VMS) && defined (my_getpwent)
94 struct passwd *Perl_my_getpwent (pTHX);
103 struct group *getgrnam (char *);
104 struct group *getgrgid (Gid_t);
108 struct group *getgrent (void);
114 # if defined(_MSC_VER) || defined(__MINGW32__)
115 # include <sys/utime.h>
122 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
125 # define my_chsize PerlLIO_chsize
128 # define my_chsize PerlLIO_chsize
130 I32 my_chsize(int fd, Off_t length);
136 #else /* no flock() */
138 /* fcntl.h might not have been included, even if it exists, because
139 the current Configure only sets I_FCNTL if it's needed to pick up
140 the *_OK constants. Make sure it has been included before testing
141 the fcntl() locking constants. */
142 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
146 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
147 # define FLOCK fcntl_emulate_flock
148 # define FCNTL_EMULATE_FLOCK
149 # else /* no flock() or fcntl(F_SETLK,...) */
151 # define FLOCK lockf_emulate_flock
152 # define LOCKF_EMULATE_FLOCK
154 # endif /* no flock() or fcntl(F_SETLK,...) */
157 static int FLOCK (int, int);
160 * These are the flock() constants. Since this sytems doesn't have
161 * flock(), the values of the constants are probably not available.
175 # endif /* emulating flock() */
177 #endif /* no flock() */
180 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
182 #if defined(I_SYS_ACCESS) && !defined(R_OK)
183 # include <sys/access.h>
186 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
187 # define FD_CLOEXEC 1 /* NeXT needs this */
193 /* Missing protos on LynxOS */
194 void sethostent(int);
195 void endhostent(void);
197 void endnetent(void);
198 void setprotoent(int);
199 void endprotoent(void);
200 void setservent(int);
201 void endservent(void);
204 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
206 /* F_OK unused: if stat() cannot find it... */
208 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
209 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
210 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
213 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
214 # ifdef I_SYS_SECURITY
215 # include <sys/security.h>
219 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
222 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
226 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
228 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
232 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
233 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
234 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
237 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
239 const Uid_t ruid = getuid();
240 const Uid_t euid = geteuid();
241 const Gid_t rgid = getgid();
242 const Gid_t egid = getegid();
245 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
246 Perl_croak(aTHX_ "switching effective uid is not implemented");
249 if (setreuid(euid, ruid))
252 if (setresuid(euid, ruid, (Uid_t)-1))
255 Perl_croak(aTHX_ "entering effective uid failed");
258 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
259 Perl_croak(aTHX_ "switching effective gid is not implemented");
262 if (setregid(egid, rgid))
265 if (setresgid(egid, rgid, (Gid_t)-1))
268 Perl_croak(aTHX_ "entering effective gid failed");
271 res = access(path, mode);
274 if (setreuid(ruid, euid))
277 if (setresuid(ruid, euid, (Uid_t)-1))
280 Perl_croak(aTHX_ "leaving effective uid failed");
283 if (setregid(rgid, egid))
286 if (setresgid(rgid, egid, (Gid_t)-1))
289 Perl_croak(aTHX_ "leaving effective gid failed");
293 # define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
300 const char * const tmps = POPpconstx;
301 const I32 gimme = GIMME_V;
302 const char *mode = "r";
305 if (PL_op->op_private & OPpOPEN_IN_RAW)
307 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
309 fp = PerlProc_popen(tmps, mode);
311 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
313 PerlIO_apply_layers(aTHX_ fp,mode,type);
315 if (gimme == G_VOID) {
317 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
320 else if (gimme == G_SCALAR) {
321 ENTER_with_name("backtick");
323 PL_rs = &PL_sv_undef;
324 sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
325 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
327 LEAVE_with_name("backtick");
333 SV * const sv = newSV(79);
334 if (sv_gets(sv, fp, 0) == NULL) {
339 if (SvLEN(sv) - SvCUR(sv) > 20) {
340 SvPV_shrink_to_cur(sv);
345 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
346 TAINT; /* "I believe that this is not gratuitous!" */
349 STATUS_NATIVE_CHILD_SET(-1);
350 if (gimme == G_SCALAR)
361 tryAMAGICunTARGET(iter, -1);
363 /* Note that we only ever get here if File::Glob fails to load
364 * without at the same time croaking, for some reason, or if
365 * perl was built with PERL_EXTERNAL_GLOB */
367 ENTER_with_name("glob");
372 * The external globbing program may use things we can't control,
373 * so for security reasons we must assume the worst.
376 taint_proper(PL_no_security, "glob");
380 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
381 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
383 SAVESPTR(PL_rs); /* This is not permanent, either. */
384 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
387 *SvPVX(PL_rs) = '\n';
391 result = do_readline();
392 LEAVE_with_name("glob");
399 PL_last_in_gv = cGVOP_gv;
400 return do_readline();
411 do_join(TARG, &PL_sv_no, MARK, SP);
415 else if (SP == MARK) {
424 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
425 /* well-formed exception supplied */
427 else if (SvROK(ERRSV)) {
430 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
431 exsv = sv_mortalcopy(ERRSV);
432 sv_catpvs(exsv, "\t...caught");
435 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
448 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
450 if (SP - MARK != 1) {
452 do_join(TARG, &PL_sv_no, MARK, SP);
460 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
461 /* well-formed exception supplied */
463 else if (SvROK(ERRSV)) {
465 if (sv_isobject(exsv)) {
466 HV * const stash = SvSTASH(SvRV(exsv));
467 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
469 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
470 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
477 call_sv(MUTABLE_SV(GvCV(gv)),
478 G_SCALAR|G_EVAL|G_KEEPERR);
479 exsv = sv_mortalcopy(*PL_stack_sp--);
483 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
484 exsv = sv_mortalcopy(ERRSV);
485 sv_catpvs(exsv, "\t...propagated");
488 exsv = newSVpvs_flags("Died", SVs_TEMP);
507 GV * const gv = MUTABLE_GV(*++MARK);
510 DIE(aTHX_ PL_no_usym, "filehandle");
512 if ((io = GvIOp(gv))) {
514 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
517 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
518 "Opening dirhandle %s also as a file",
521 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
523 /* Method's args are same as ours ... */
524 /* ... except handle is replaced by the object */
525 *MARK-- = SvTIED_obj(MUTABLE_SV(io), mg);
528 ENTER_with_name("call_OPEN");
529 call_method("OPEN", G_SCALAR);
530 LEAVE_with_name("call_OPEN");
543 tmps = SvPV_const(sv, len);
544 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
547 PUSHi( (I32)PL_forkprocess );
548 else if (PL_forkprocess == 0) /* we are a new child */
558 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
561 IO * const io = GvIO(gv);
563 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
566 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
568 ENTER_with_name("call_CLOSE");
569 call_method("CLOSE", G_SCALAR);
570 LEAVE_with_name("call_CLOSE");
577 PUSHs(boolSV(do_close(gv, TRUE)));
590 GV * const wgv = MUTABLE_GV(POPs);
591 GV * const rgv = MUTABLE_GV(POPs);
596 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
597 DIE(aTHX_ PL_no_usym, "filehandle");
602 do_close(rgv, FALSE);
604 do_close(wgv, FALSE);
606 if (PerlProc_pipe(fd) < 0)
609 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
610 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
611 IoOFP(rstio) = IoIFP(rstio);
612 IoIFP(wstio) = IoOFP(wstio);
613 IoTYPE(rstio) = IoTYPE_RDONLY;
614 IoTYPE(wstio) = IoTYPE_WRONLY;
616 if (!IoIFP(rstio) || !IoOFP(wstio)) {
618 PerlIO_close(IoIFP(rstio));
620 PerlLIO_close(fd[0]);
622 PerlIO_close(IoOFP(wstio));
624 PerlLIO_close(fd[1]);
627 #if defined(HAS_FCNTL) && defined(F_SETFD)
628 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
629 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
636 DIE(aTHX_ PL_no_func, "pipe");
651 gv = MUTABLE_GV(POPs);
653 if (gv && (io = GvIO(gv))
654 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
657 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
659 ENTER_with_name("call_FILENO");
660 call_method("FILENO", G_SCALAR);
661 LEAVE_with_name("call_FILENO");
666 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
667 /* Can't do this because people seem to do things like
668 defined(fileno($foo)) to check whether $foo is a valid fh.
669 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
670 report_evil_fh(gv, io, PL_op->op_type);
675 PUSHi(PerlIO_fileno(fp));
688 anum = PerlLIO_umask(022);
689 /* setting it to 022 between the two calls to umask avoids
690 * to have a window where the umask is set to 0 -- meaning
691 * that another thread could create world-writeable files. */
693 (void)PerlLIO_umask(anum);
696 anum = PerlLIO_umask(POPi);
697 TAINT_PROPER("umask");
700 /* Only DIE if trying to restrict permissions on "user" (self).
701 * Otherwise it's harmless and more useful to just return undef
702 * since 'group' and 'other' concepts probably don't exist here. */
703 if (MAXARG >= 1 && (POPi & 0700))
704 DIE(aTHX_ "umask not implemented");
705 XPUSHs(&PL_sv_undef);
724 gv = MUTABLE_GV(POPs);
726 if (gv && (io = GvIO(gv))) {
727 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
730 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
734 ENTER_with_name("call_BINMODE");
735 call_method("BINMODE", G_SCALAR);
736 LEAVE_with_name("call_BINMODE");
743 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
744 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
745 report_evil_fh(gv, io, PL_op->op_type);
746 SETERRNO(EBADF,RMS_IFI);
753 const char *d = NULL;
756 d = SvPV_const(discp, len);
757 mode = mode_from_discipline(d, len);
758 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
759 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
760 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
781 const I32 markoff = MARK - PL_stack_base;
782 const char *methname;
783 int how = PERL_MAGIC_tied;
787 switch(SvTYPE(varsv)) {
789 methname = "TIEHASH";
790 HvEITER_set(MUTABLE_HV(varsv), 0);
793 methname = "TIEARRAY";
796 if (isGV_with_GP(varsv)) {
797 methname = "TIEHANDLE";
798 how = PERL_MAGIC_tiedscalar;
799 /* For tied filehandles, we apply tiedscalar magic to the IO
800 slot of the GP rather than the GV itself. AMS 20010812 */
802 GvIOp(varsv) = newIO();
803 varsv = MUTABLE_SV(GvIOp(varsv));
808 methname = "TIESCALAR";
809 how = PERL_MAGIC_tiedscalar;
813 if (sv_isobject(*MARK)) { /* Calls GET magic. */
814 ENTER_with_name("call_TIE");
815 PUSHSTACKi(PERLSI_MAGIC);
817 EXTEND(SP,(I32)items);
821 call_method(methname, G_SCALAR);
824 /* Not clear why we don't call call_method here too.
825 * perhaps to get different error message ?
828 const char *name = SvPV_nomg_const(*MARK, len);
829 stash = gv_stashpvn(name, len, 0);
830 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
831 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
832 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
834 ENTER_with_name("call_TIE");
835 PUSHSTACKi(PERLSI_MAGIC);
837 EXTEND(SP,(I32)items);
841 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
847 if (sv_isobject(sv)) {
848 sv_unmagic(varsv, how);
849 /* Croak if a self-tie on an aggregate is attempted. */
850 if (varsv == SvRV(sv) &&
851 (SvTYPE(varsv) == SVt_PVAV ||
852 SvTYPE(varsv) == SVt_PVHV))
854 "Self-ties of arrays and hashes are not supported");
855 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
857 LEAVE_with_name("call_TIE");
858 SP = PL_stack_base + markoff;
868 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
869 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
871 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
874 if ((mg = SvTIED_mg(sv, how))) {
875 SV * const obj = SvRV(SvTIED_obj(sv, mg));
877 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
879 if (gv && isGV(gv) && (cv = GvCV(gv))) {
881 XPUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
882 mXPUSHi(SvREFCNT(obj) - 1);
884 ENTER_with_name("call_UNTIE");
885 call_sv(MUTABLE_SV(cv), G_VOID);
886 LEAVE_with_name("call_UNTIE");
889 else if (mg && SvREFCNT(obj) > 1) {
890 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
891 "untie attempted while %"UVuf" inner references still exist",
892 (UV)SvREFCNT(obj) - 1 ) ;
896 sv_unmagic(sv, how) ;
906 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
907 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
909 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
912 if ((mg = SvTIED_mg(sv, how))) {
913 SV *osv = SvTIED_obj(sv, mg);
914 if (osv == mg->mg_obj)
915 osv = sv_mortalcopy(osv);
929 HV * const hv = MUTABLE_HV(POPs);
930 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
931 stash = gv_stashsv(sv, 0);
932 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
934 require_pv("AnyDBM_File.pm");
936 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
937 DIE(aTHX_ "No dbm on this machine");
947 mPUSHu(O_RDWR|O_CREAT);
952 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
955 if (!sv_isobject(TOPs)) {
963 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
967 if (sv_isobject(TOPs)) {
968 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
969 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
986 struct timeval timebuf;
987 struct timeval *tbuf = &timebuf;
990 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
995 # if BYTEORDER & 0xf0000
996 # define ORDERBYTE (0x88888888 - BYTEORDER)
998 # define ORDERBYTE (0x4444 - BYTEORDER)
1004 for (i = 1; i <= 3; i++) {
1005 SV * const sv = SP[i];
1008 if (SvREADONLY(sv)) {
1010 sv_force_normal_flags(sv, 0);
1011 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1012 DIE(aTHX_ "%s", PL_no_modify);
1015 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1016 SvPV_force_nolen(sv); /* force string conversion */
1023 /* little endians can use vecs directly */
1024 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1031 masksize = NFDBITS / NBBY;
1033 masksize = sizeof(long); /* documented int, everyone seems to use long */
1035 Zero(&fd_sets[0], 4, char*);
1038 # if SELECT_MIN_BITS == 1
1039 growsize = sizeof(fd_set);
1041 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1042 # undef SELECT_MIN_BITS
1043 # define SELECT_MIN_BITS __FD_SETSIZE
1045 /* If SELECT_MIN_BITS is greater than one we most probably will want
1046 * to align the sizes with SELECT_MIN_BITS/8 because for example
1047 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1048 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1049 * on (sets/tests/clears bits) is 32 bits. */
1050 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1058 timebuf.tv_sec = (long)value;
1059 value -= (NV)timebuf.tv_sec;
1060 timebuf.tv_usec = (long)(value * 1000000.0);
1065 for (i = 1; i <= 3; i++) {
1067 if (!SvOK(sv) || SvCUR(sv) == 0) {
1074 Sv_Grow(sv, growsize);
1078 while (++j <= growsize) {
1082 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1084 Newx(fd_sets[i], growsize, char);
1085 for (offset = 0; offset < growsize; offset += masksize) {
1086 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1087 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1090 fd_sets[i] = SvPVX(sv);
1094 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1095 /* Can't make just the (void*) conditional because that would be
1096 * cpp #if within cpp macro, and not all compilers like that. */
1097 nfound = PerlSock_select(
1099 (Select_fd_set_t) fd_sets[1],
1100 (Select_fd_set_t) fd_sets[2],
1101 (Select_fd_set_t) fd_sets[3],
1102 (void*) tbuf); /* Workaround for compiler bug. */
1104 nfound = PerlSock_select(
1106 (Select_fd_set_t) fd_sets[1],
1107 (Select_fd_set_t) fd_sets[2],
1108 (Select_fd_set_t) fd_sets[3],
1111 for (i = 1; i <= 3; i++) {
1114 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1116 for (offset = 0; offset < growsize; offset += masksize) {
1117 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1118 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1120 Safefree(fd_sets[i]);
1127 if (GIMME == G_ARRAY && tbuf) {
1128 value = (NV)(timebuf.tv_sec) +
1129 (NV)(timebuf.tv_usec) / 1000000.0;
1134 DIE(aTHX_ "select not implemented");
1140 =for apidoc setdefout
1142 Sets PL_defoutgv, the default file handle for output, to the passed in
1143 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1144 count of the passed in typeglob is increased by one, and the reference count
1145 of the typeglob that PL_defoutgv points to is decreased by one.
1151 Perl_setdefout(pTHX_ GV *gv)
1154 SvREFCNT_inc_simple_void(gv);
1155 SvREFCNT_dec(PL_defoutgv);
1163 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1164 GV * egv = GvEGVx(PL_defoutgv);
1168 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1170 XPUSHs(&PL_sv_undef);
1172 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1173 if (gvp && *gvp == egv) {
1174 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1178 mXPUSHs(newRV(MUTABLE_SV(egv)));
1183 if (!GvIO(newdefout))
1184 gv_IOadd(newdefout);
1185 setdefout(newdefout);
1195 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1197 if (gv && (io = GvIO(gv))) {
1198 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1200 const I32 gimme = GIMME_V;
1202 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
1205 call_method("GETC", gimme);
1208 if (gimme == G_SCALAR)
1209 SvSetMagicSV_nosteal(TARG, TOPs);
1213 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1214 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1215 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1216 report_evil_fh(gv, io, PL_op->op_type);
1217 SETERRNO(EBADF,RMS_IFI);
1221 sv_setpvs(TARG, " ");
1222 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1223 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1224 /* Find out how many bytes the char needs */
1225 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1228 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1229 SvCUR_set(TARG,1+len);
1238 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1241 register PERL_CONTEXT *cx;
1242 const I32 gimme = GIMME_V;
1244 PERL_ARGS_ASSERT_DOFORM;
1249 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1250 PUSHFORMAT(cx, retop);
1252 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1254 setdefout(gv); /* locally select filehandle so $% et al work */
1271 gv = MUTABLE_GV(POPs);
1286 goto not_a_format_reference;
1291 tmpsv = sv_newmortal();
1292 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1293 name = SvPV_nolen_const(tmpsv);
1295 DIE(aTHX_ "Undefined format \"%s\" called", name);
1297 not_a_format_reference:
1298 DIE(aTHX_ "Not a format reference");
1301 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1303 IoFLAGS(io) &= ~IOf_DIDTOP;
1304 return doform(cv,gv,PL_op->op_next);
1310 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1311 register IO * const io = GvIOp(gv);
1316 register PERL_CONTEXT *cx;
1318 if (!io || !(ofp = IoOFP(io)))
1321 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1322 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1324 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1325 PL_formtarget != PL_toptarget)
1329 if (!IoTOP_GV(io)) {
1332 if (!IoTOP_NAME(io)) {
1334 if (!IoFMT_NAME(io))
1335 IoFMT_NAME(io) = savepv(GvNAME(gv));
1336 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1337 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1338 if ((topgv && GvFORM(topgv)) ||
1339 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1340 IoTOP_NAME(io) = savesvpv(topname);
1342 IoTOP_NAME(io) = savepvs("top");
1344 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1345 if (!topgv || !GvFORM(topgv)) {
1346 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1349 IoTOP_GV(io) = topgv;
1351 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1352 I32 lines = IoLINES_LEFT(io);
1353 const char *s = SvPVX_const(PL_formtarget);
1354 if (lines <= 0) /* Yow, header didn't even fit!!! */
1356 while (lines-- > 0) {
1357 s = strchr(s, '\n');
1363 const STRLEN save = SvCUR(PL_formtarget);
1364 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1365 do_print(PL_formtarget, ofp);
1366 SvCUR_set(PL_formtarget, save);
1367 sv_chop(PL_formtarget, s);
1368 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1371 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1372 do_print(PL_formfeed, ofp);
1373 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1375 PL_formtarget = PL_toptarget;
1376 IoFLAGS(io) |= IOf_DIDTOP;
1379 DIE(aTHX_ "bad top format reference");
1382 SV * const sv = sv_newmortal();
1384 gv_efullname4(sv, fgv, NULL, FALSE);
1385 name = SvPV_nolen_const(sv);
1387 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1389 DIE(aTHX_ "Undefined top format called");
1391 if (cv && CvCLONE(cv))
1392 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1393 return doform(cv, gv, PL_op);
1397 POPBLOCK(cx,PL_curpm);
1403 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1405 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1406 else if (ckWARN(WARN_CLOSED))
1407 report_evil_fh(gv, io, PL_op->op_type);
1412 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1413 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1415 if (!do_print(PL_formtarget, fp))
1418 FmLINES(PL_formtarget) = 0;
1419 SvCUR_set(PL_formtarget, 0);
1420 *SvEND(PL_formtarget) = '\0';
1421 if (IoFLAGS(io) & IOf_FLUSH)
1422 (void)PerlIO_flush(fp);
1427 PL_formtarget = PL_bodytarget;
1429 PERL_UNUSED_VAR(newsp);
1430 PERL_UNUSED_VAR(gimme);
1431 return cx->blk_sub.retop;
1436 dVAR; dSP; dMARK; dORIGMARK;
1442 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1444 if (gv && (io = GvIO(gv))) {
1445 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1447 if (MARK == ORIGMARK) {
1450 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1454 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1457 call_method("PRINTF", G_SCALAR);
1460 MARK = ORIGMARK + 1;
1468 if (!(io = GvIO(gv))) {
1469 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1470 report_evil_fh(gv, io, PL_op->op_type);
1471 SETERRNO(EBADF,RMS_IFI);
1474 else if (!(fp = IoOFP(io))) {
1475 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1477 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1478 else if (ckWARN(WARN_CLOSED))
1479 report_evil_fh(gv, io, PL_op->op_type);
1481 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1485 if (SvTAINTED(MARK[1]))
1486 TAINT_PROPER("printf");
1487 do_sprintf(sv, SP - MARK, MARK + 1);
1488 if (!do_print(sv, fp))
1491 if (IoFLAGS(io) & IOf_FLUSH)
1492 if (PerlIO_flush(fp) == EOF)
1503 PUSHs(&PL_sv_undef);
1511 const int perm = (MAXARG > 3) ? POPi : 0666;
1512 const int mode = POPi;
1513 SV * const sv = POPs;
1514 GV * const gv = MUTABLE_GV(POPs);
1517 /* Need TIEHANDLE method ? */
1518 const char * const tmps = SvPV_const(sv, len);
1519 /* FIXME? do_open should do const */
1520 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1521 IoLINES(GvIOp(gv)) = 0;
1525 PUSHs(&PL_sv_undef);
1532 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1538 Sock_size_t bufsize;
1546 bool charstart = FALSE;
1547 STRLEN charskip = 0;
1550 GV * const gv = MUTABLE_GV(*++MARK);
1551 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1552 && gv && (io = GvIO(gv)) )
1554 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1558 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1560 call_method("READ", G_SCALAR);
1574 sv_setpvs(bufsv, "");
1575 length = SvIVx(*++MARK);
1578 offset = SvIVx(*++MARK);
1582 if (!io || !IoIFP(io)) {
1583 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1584 report_evil_fh(gv, io, PL_op->op_type);
1585 SETERRNO(EBADF,RMS_IFI);
1588 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1589 buffer = SvPVutf8_force(bufsv, blen);
1590 /* UTF-8 may not have been set if they are all low bytes */
1595 buffer = SvPV_force(bufsv, blen);
1596 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1599 DIE(aTHX_ "Negative length");
1607 if (PL_op->op_type == OP_RECV) {
1608 char namebuf[MAXPATHLEN];
1609 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1610 bufsize = sizeof (struct sockaddr_in);
1612 bufsize = sizeof namebuf;
1614 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1618 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1619 /* 'offset' means 'flags' here */
1620 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1621 (struct sockaddr *)namebuf, &bufsize);
1625 /* Bogus return without padding */
1626 bufsize = sizeof (struct sockaddr_in);
1628 SvCUR_set(bufsv, count);
1629 *SvEND(bufsv) = '\0';
1630 (void)SvPOK_only(bufsv);
1634 /* This should not be marked tainted if the fp is marked clean */
1635 if (!(IoFLAGS(io) & IOf_UNTAINT))
1636 SvTAINTED_on(bufsv);
1638 sv_setpvn(TARG, namebuf, bufsize);
1643 if (PL_op->op_type == OP_RECV)
1644 DIE(aTHX_ PL_no_sock_func, "recv");
1646 if (DO_UTF8(bufsv)) {
1647 /* offset adjust in characters not bytes */
1648 blen = sv_len_utf8(bufsv);
1651 if (-offset > (int)blen)
1652 DIE(aTHX_ "Offset outside string");
1655 if (DO_UTF8(bufsv)) {
1656 /* convert offset-as-chars to offset-as-bytes */
1657 if (offset >= (int)blen)
1658 offset += SvCUR(bufsv) - blen;
1660 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1663 bufsize = SvCUR(bufsv);
1664 /* Allocating length + offset + 1 isn't perfect in the case of reading
1665 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1667 (should be 2 * length + offset + 1, or possibly something longer if
1668 PL_encoding is true) */
1669 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1670 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1671 Zero(buffer+bufsize, offset-bufsize, char);
1673 buffer = buffer + offset;
1675 read_target = bufsv;
1677 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1678 concatenate it to the current buffer. */
1680 /* Truncate the existing buffer to the start of where we will be
1682 SvCUR_set(bufsv, offset);
1684 read_target = sv_newmortal();
1685 SvUPGRADE(read_target, SVt_PV);
1686 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1689 if (PL_op->op_type == OP_SYSREAD) {
1690 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1691 if (IoTYPE(io) == IoTYPE_SOCKET) {
1692 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1698 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1703 #ifdef HAS_SOCKET__bad_code_maybe
1704 if (IoTYPE(io) == IoTYPE_SOCKET) {
1705 char namebuf[MAXPATHLEN];
1706 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1707 bufsize = sizeof (struct sockaddr_in);
1709 bufsize = sizeof namebuf;
1711 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1712 (struct sockaddr *)namebuf, &bufsize);
1717 count = PerlIO_read(IoIFP(io), buffer, length);
1718 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1719 if (count == 0 && PerlIO_error(IoIFP(io)))
1723 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1724 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1727 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1728 *SvEND(read_target) = '\0';
1729 (void)SvPOK_only(read_target);
1730 if (fp_utf8 && !IN_BYTES) {
1731 /* Look at utf8 we got back and count the characters */
1732 const char *bend = buffer + count;
1733 while (buffer < bend) {
1735 skip = UTF8SKIP(buffer);
1738 if (buffer - charskip + skip > bend) {
1739 /* partial character - try for rest of it */
1740 length = skip - (bend-buffer);
1741 offset = bend - SvPVX_const(bufsv);
1753 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1754 provided amount read (count) was what was requested (length)
1756 if (got < wanted && count == length) {
1757 length = wanted - got;
1758 offset = bend - SvPVX_const(bufsv);
1761 /* return value is character count */
1765 else if (buffer_utf8) {
1766 /* Let svcatsv upgrade the bytes we read in to utf8.
1767 The buffer is a mortal so will be freed soon. */
1768 sv_catsv_nomg(bufsv, read_target);
1771 /* This should not be marked tainted if the fp is marked clean */
1772 if (!(IoFLAGS(io) & IOf_UNTAINT))
1773 SvTAINTED_on(bufsv);
1785 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1791 STRLEN orig_blen_bytes;
1792 const int op_type = PL_op->op_type;
1796 GV *const gv = MUTABLE_GV(*++MARK);
1797 if (PL_op->op_type == OP_SYSWRITE
1798 && gv && (io = GvIO(gv))) {
1799 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1803 if (MARK == SP - 1) {
1805 mXPUSHi(sv_len(sv));
1810 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1812 call_method("WRITE", G_SCALAR);
1828 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1830 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1831 if (io && IoIFP(io))
1832 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1834 report_evil_fh(gv, io, PL_op->op_type);
1836 SETERRNO(EBADF,RMS_IFI);
1840 /* Do this first to trigger any overloading. */
1841 buffer = SvPV_const(bufsv, blen);
1842 orig_blen_bytes = blen;
1843 doing_utf8 = DO_UTF8(bufsv);
1845 if (PerlIO_isutf8(IoIFP(io))) {
1846 if (!SvUTF8(bufsv)) {
1847 /* We don't modify the original scalar. */
1848 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1849 buffer = (char *) tmpbuf;
1853 else if (doing_utf8) {
1854 STRLEN tmplen = blen;
1855 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1858 buffer = (char *) tmpbuf;
1862 assert((char *)result == buffer);
1863 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1867 if (op_type == OP_SYSWRITE) {
1868 Size_t length = 0; /* This length is in characters. */
1874 /* The SV is bytes, and we've had to upgrade it. */
1875 blen_chars = orig_blen_bytes;
1877 /* The SV really is UTF-8. */
1878 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1879 /* Don't call sv_len_utf8 again because it will call magic
1880 or overloading a second time, and we might get back a
1881 different result. */
1882 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1884 /* It's safe, and it may well be cached. */
1885 blen_chars = sv_len_utf8(bufsv);
1893 length = blen_chars;
1895 #if Size_t_size > IVSIZE
1896 length = (Size_t)SvNVx(*++MARK);
1898 length = (Size_t)SvIVx(*++MARK);
1900 if ((SSize_t)length < 0) {
1902 DIE(aTHX_ "Negative length");
1907 offset = SvIVx(*++MARK);
1909 if (-offset > (IV)blen_chars) {
1911 DIE(aTHX_ "Offset outside string");
1913 offset += blen_chars;
1914 } else if (offset > (IV)blen_chars) {
1916 DIE(aTHX_ "Offset outside string");
1920 if (length > blen_chars - offset)
1921 length = blen_chars - offset;
1923 /* Here we convert length from characters to bytes. */
1924 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1925 /* Either we had to convert the SV, or the SV is magical, or
1926 the SV has overloading, in which case we can't or mustn't
1927 or mustn't call it again. */
1929 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1930 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1932 /* It's a real UTF-8 SV, and it's not going to change under
1933 us. Take advantage of any cache. */
1935 I32 len_I32 = length;
1937 /* Convert the start and end character positions to bytes.
1938 Remember that the second argument to sv_pos_u2b is relative
1940 sv_pos_u2b(bufsv, &start, &len_I32);
1947 buffer = buffer+offset;
1949 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1950 if (IoTYPE(io) == IoTYPE_SOCKET) {
1951 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1957 /* See the note at doio.c:do_print about filesize limits. --jhi */
1958 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1964 const int flags = SvIVx(*++MARK);
1967 char * const sockbuf = SvPVx(*++MARK, mlen);
1968 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1969 flags, (struct sockaddr *)sockbuf, mlen);
1973 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1978 DIE(aTHX_ PL_no_sock_func, "send");
1985 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1988 #if Size_t_size > IVSIZE
2009 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2010 else if (PL_op->op_flags & OPf_SPECIAL)
2011 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2013 gv = PL_last_in_gv; /* eof */
2018 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2020 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2022 * in Perl 5.12 and later, the additional paramter is a bitmask:
2025 * 2 = eof() <- ARGV magic
2028 mPUSHi(1); /* 1 = eof(FH) - simple, explicit FH */
2029 else if (PL_op->op_flags & OPf_SPECIAL)
2030 mPUSHi(2); /* 2 = eof() - ARGV magic */
2032 mPUSHi(0); /* 0 = eof - simple, implicit FH */
2035 call_method("EOF", G_SCALAR);
2041 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2042 if (io && !IoIFP(io)) {
2043 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2045 IoFLAGS(io) &= ~IOf_START;
2046 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2048 sv_setpvs(GvSV(gv), "-");
2050 GvSV(gv) = newSVpvs("-");
2051 SvSETMAGIC(GvSV(gv));
2053 else if (!nextargv(gv))
2058 PUSHs(boolSV(do_eof(gv)));
2069 PL_last_in_gv = MUTABLE_GV(POPs);
2072 if (gv && (io = GvIO(gv))) {
2073 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2076 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2079 call_method("TELL", G_SCALAR);
2087 SETERRNO(EBADF,RMS_IFI);
2092 #if LSEEKSIZE > IVSIZE
2093 PUSHn( do_tell(gv) );
2095 PUSHi( do_tell(gv) );
2103 const int whence = POPi;
2104 #if LSEEKSIZE > IVSIZE
2105 const Off_t offset = (Off_t)SvNVx(POPs);
2107 const Off_t offset = (Off_t)SvIVx(POPs);
2110 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2113 if (gv && (io = GvIO(gv))) {
2114 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2117 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2118 #if LSEEKSIZE > IVSIZE
2119 mXPUSHn((NV) offset);
2126 call_method("SEEK", G_SCALAR);
2133 if (PL_op->op_type == OP_SEEK)
2134 PUSHs(boolSV(do_seek(gv, offset, whence)));
2136 const Off_t sought = do_sysseek(gv, offset, whence);
2138 PUSHs(&PL_sv_undef);
2140 SV* const sv = sought ?
2141 #if LSEEKSIZE > IVSIZE
2146 : newSVpvn(zero_but_true, ZBTLEN);
2157 /* There seems to be no consensus on the length type of truncate()
2158 * and ftruncate(), both off_t and size_t have supporters. In
2159 * general one would think that when using large files, off_t is
2160 * at least as wide as size_t, so using an off_t should be okay. */
2161 /* XXX Configure probe for the length type of *truncate() needed XXX */
2164 #if Off_t_size > IVSIZE
2169 /* Checking for length < 0 is problematic as the type might or
2170 * might not be signed: if it is not, clever compilers will moan. */
2171 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2178 if (PL_op->op_flags & OPf_SPECIAL) {
2179 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2188 TAINT_PROPER("truncate");
2189 if (!(fp = IoIFP(io))) {
2195 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2197 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2204 SV * const sv = POPs;
2207 if (isGV_with_GP(sv)) {
2208 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2209 goto do_ftruncate_gv;
2211 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2212 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2213 goto do_ftruncate_gv;
2215 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2216 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2217 goto do_ftruncate_io;
2220 name = SvPV_nolen_const(sv);
2221 TAINT_PROPER("truncate");
2223 if (truncate(name, len) < 0)
2227 const int tmpfd = PerlLIO_open(name, O_RDWR);
2232 if (my_chsize(tmpfd, len) < 0)
2234 PerlLIO_close(tmpfd);
2243 SETERRNO(EBADF,RMS_IFI);
2251 SV * const argsv = POPs;
2252 const unsigned int func = POPu;
2253 const int optype = PL_op->op_type;
2254 GV * const gv = MUTABLE_GV(POPs);
2255 IO * const io = gv ? GvIOn(gv) : NULL;
2259 if (!io || !argsv || !IoIFP(io)) {
2260 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2261 report_evil_fh(gv, io, PL_op->op_type);
2262 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2266 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2269 s = SvPV_force(argsv, len);
2270 need = IOCPARM_LEN(func);
2272 s = Sv_Grow(argsv, need + 1);
2273 SvCUR_set(argsv, need);
2276 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2279 retval = SvIV(argsv);
2280 s = INT2PTR(char*,retval); /* ouch */
2283 TAINT_PROPER(PL_op_desc[optype]);
2285 if (optype == OP_IOCTL)
2287 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2289 DIE(aTHX_ "ioctl is not implemented");
2293 DIE(aTHX_ "fcntl is not implemented");
2295 #if defined(OS2) && defined(__EMX__)
2296 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2298 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2302 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2304 if (s[SvCUR(argsv)] != 17)
2305 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2307 s[SvCUR(argsv)] = 0; /* put our null back */
2308 SvSETMAGIC(argsv); /* Assume it has changed */
2317 PUSHp(zero_but_true, ZBTLEN);
2330 const int argtype = POPi;
2331 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2333 if (gv && (io = GvIO(gv)))
2339 /* XXX Looks to me like io is always NULL at this point */
2341 (void)PerlIO_flush(fp);
2342 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2345 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2346 report_evil_fh(gv, io, PL_op->op_type);
2348 SETERRNO(EBADF,RMS_IFI);
2353 DIE(aTHX_ PL_no_func, "flock()");
2364 const int protocol = POPi;
2365 const int type = POPi;
2366 const int domain = POPi;
2367 GV * const gv = MUTABLE_GV(POPs);
2368 register IO * const io = gv ? GvIOn(gv) : NULL;
2372 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2373 report_evil_fh(gv, io, PL_op->op_type);
2374 if (io && IoIFP(io))
2375 do_close(gv, FALSE);
2376 SETERRNO(EBADF,LIB_INVARG);
2381 do_close(gv, FALSE);
2383 TAINT_PROPER("socket");
2384 fd = PerlSock_socket(domain, type, protocol);
2387 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2388 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2389 IoTYPE(io) = IoTYPE_SOCKET;
2390 if (!IoIFP(io) || !IoOFP(io)) {
2391 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2392 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2393 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2396 #if defined(HAS_FCNTL) && defined(F_SETFD)
2397 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2401 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2406 DIE(aTHX_ PL_no_sock_func, "socket");
2413 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2415 const int protocol = POPi;
2416 const int type = POPi;
2417 const int domain = POPi;
2418 GV * const gv2 = MUTABLE_GV(POPs);
2419 GV * const gv1 = MUTABLE_GV(POPs);
2420 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2421 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2424 if (!gv1 || !gv2 || !io1 || !io2) {
2425 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2427 report_evil_fh(gv1, io1, PL_op->op_type);
2429 report_evil_fh(gv1, io2, PL_op->op_type);
2431 if (io1 && IoIFP(io1))
2432 do_close(gv1, FALSE);
2433 if (io2 && IoIFP(io2))
2434 do_close(gv2, FALSE);
2439 do_close(gv1, FALSE);
2441 do_close(gv2, FALSE);
2443 TAINT_PROPER("socketpair");
2444 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2446 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2447 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2448 IoTYPE(io1) = IoTYPE_SOCKET;
2449 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2450 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2451 IoTYPE(io2) = IoTYPE_SOCKET;
2452 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2453 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2454 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2455 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2456 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2457 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2458 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2461 #if defined(HAS_FCNTL) && defined(F_SETFD)
2462 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2463 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2468 DIE(aTHX_ PL_no_sock_func, "socketpair");
2477 SV * const addrsv = POPs;
2478 /* OK, so on what platform does bind modify addr? */
2480 GV * const gv = MUTABLE_GV(POPs);
2481 register IO * const io = GvIOn(gv);
2484 if (!io || !IoIFP(io))
2487 addr = SvPV_const(addrsv, len);
2488 TAINT_PROPER("bind");
2489 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2495 if (ckWARN(WARN_CLOSED))
2496 report_evil_fh(gv, io, PL_op->op_type);
2497 SETERRNO(EBADF,SS_IVCHAN);
2500 DIE(aTHX_ PL_no_sock_func, "bind");
2509 SV * const addrsv = POPs;
2510 GV * const gv = MUTABLE_GV(POPs);
2511 register IO * const io = GvIOn(gv);
2515 if (!io || !IoIFP(io))
2518 addr = SvPV_const(addrsv, len);
2519 TAINT_PROPER("connect");
2520 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2526 if (ckWARN(WARN_CLOSED))
2527 report_evil_fh(gv, io, PL_op->op_type);
2528 SETERRNO(EBADF,SS_IVCHAN);
2531 DIE(aTHX_ PL_no_sock_func, "connect");
2540 const int backlog = POPi;
2541 GV * const gv = MUTABLE_GV(POPs);
2542 register IO * const io = gv ? GvIOn(gv) : NULL;
2544 if (!gv || !io || !IoIFP(io))
2547 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2553 if (ckWARN(WARN_CLOSED))
2554 report_evil_fh(gv, io, PL_op->op_type);
2555 SETERRNO(EBADF,SS_IVCHAN);
2558 DIE(aTHX_ PL_no_sock_func, "listen");
2569 char namebuf[MAXPATHLEN];
2570 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2571 Sock_size_t len = sizeof (struct sockaddr_in);
2573 Sock_size_t len = sizeof namebuf;
2575 GV * const ggv = MUTABLE_GV(POPs);
2576 GV * const ngv = MUTABLE_GV(POPs);
2585 if (!gstio || !IoIFP(gstio))
2589 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2592 /* Some platforms indicate zero length when an AF_UNIX client is
2593 * not bound. Simulate a non-zero-length sockaddr structure in
2595 namebuf[0] = 0; /* sun_len */
2596 namebuf[1] = AF_UNIX; /* sun_family */
2604 do_close(ngv, FALSE);
2605 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2606 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2607 IoTYPE(nstio) = IoTYPE_SOCKET;
2608 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2609 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2610 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2611 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2614 #if defined(HAS_FCNTL) && defined(F_SETFD)
2615 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2619 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2620 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2622 #ifdef __SCO_VERSION__
2623 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2626 PUSHp(namebuf, len);
2630 if (ckWARN(WARN_CLOSED))
2631 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2632 SETERRNO(EBADF,SS_IVCHAN);
2638 DIE(aTHX_ PL_no_sock_func, "accept");
2647 const int how = POPi;
2648 GV * const gv = MUTABLE_GV(POPs);
2649 register IO * const io = GvIOn(gv);
2651 if (!io || !IoIFP(io))
2654 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2658 if (ckWARN(WARN_CLOSED))
2659 report_evil_fh(gv, io, PL_op->op_type);
2660 SETERRNO(EBADF,SS_IVCHAN);
2663 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]);
2750 const int optype = PL_op->op_type;
2751 GV * const gv = MUTABLE_GV(POPs);
2752 register IO * const io = GvIOn(gv);
2757 if (!io || !IoIFP(io))
2760 sv = sv_2mortal(newSV(257));
2761 (void)SvPOK_only(sv);
2765 fd = PerlIO_fileno(IoIFP(io));
2767 case OP_GETSOCKNAME:
2768 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2771 case OP_GETPEERNAME:
2772 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2774 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2776 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";
2777 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2778 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2779 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2780 sizeof(u_short) + sizeof(struct in_addr))) {
2787 #ifdef BOGUS_GETNAME_RETURN
2788 /* Interactive Unix, getpeername() and getsockname()
2789 does not return valid namelen */
2790 if (len == BOGUS_GETNAME_RETURN)
2791 len = sizeof(struct sockaddr);
2799 if (ckWARN(WARN_CLOSED))
2800 report_evil_fh(gv, io, optype);
2801 SETERRNO(EBADF,SS_IVCHAN);
2806 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2822 if (PL_op->op_flags & OPf_REF) {
2824 if (PL_op->op_type == OP_LSTAT) {
2825 if (gv != PL_defgv) {
2826 do_fstat_warning_check:
2827 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2828 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2829 } else if (PL_laststype != OP_LSTAT)
2830 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2834 if (gv != PL_defgv) {
2835 PL_laststype = OP_STAT;
2837 sv_setpvs(PL_statname, "");
2844 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2845 } else if (IoDIRP(io)) {
2847 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2849 PL_laststatval = -1;
2855 if (PL_laststatval < 0) {
2856 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2857 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2862 SV* const sv = POPs;
2863 if (isGV_with_GP(sv)) {
2864 gv = MUTABLE_GV(sv);
2866 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2867 gv = MUTABLE_GV(SvRV(sv));
2868 if (PL_op->op_type == OP_LSTAT)
2869 goto do_fstat_warning_check;
2871 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2872 io = MUTABLE_IO(SvRV(sv));
2873 if (PL_op->op_type == OP_LSTAT)
2874 goto do_fstat_warning_check;
2875 goto do_fstat_have_io;
2878 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2880 PL_laststype = PL_op->op_type;
2881 if (PL_op->op_type == OP_LSTAT)
2882 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2884 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2885 if (PL_laststatval < 0) {
2886 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2887 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2893 if (gimme != G_ARRAY) {
2894 if (gimme != G_VOID)
2895 XPUSHs(boolSV(max));
2901 mPUSHi(PL_statcache.st_dev);
2902 mPUSHi(PL_statcache.st_ino);
2903 mPUSHu(PL_statcache.st_mode);
2904 mPUSHu(PL_statcache.st_nlink);
2905 #if Uid_t_size > IVSIZE
2906 mPUSHn(PL_statcache.st_uid);
2908 # if Uid_t_sign <= 0
2909 mPUSHi(PL_statcache.st_uid);
2911 mPUSHu(PL_statcache.st_uid);
2914 #if Gid_t_size > IVSIZE
2915 mPUSHn(PL_statcache.st_gid);
2917 # if Gid_t_sign <= 0
2918 mPUSHi(PL_statcache.st_gid);
2920 mPUSHu(PL_statcache.st_gid);
2923 #ifdef USE_STAT_RDEV
2924 mPUSHi(PL_statcache.st_rdev);
2926 PUSHs(newSVpvs_flags("", SVs_TEMP));
2928 #if Off_t_size > IVSIZE
2929 mPUSHn(PL_statcache.st_size);
2931 mPUSHi(PL_statcache.st_size);
2934 mPUSHn(PL_statcache.st_atime);
2935 mPUSHn(PL_statcache.st_mtime);
2936 mPUSHn(PL_statcache.st_ctime);
2938 mPUSHi(PL_statcache.st_atime);
2939 mPUSHi(PL_statcache.st_mtime);
2940 mPUSHi(PL_statcache.st_ctime);
2942 #ifdef USE_STAT_BLOCKS
2943 mPUSHu(PL_statcache.st_blksize);
2944 mPUSHu(PL_statcache.st_blocks);
2946 PUSHs(newSVpvs_flags("", SVs_TEMP));
2947 PUSHs(newSVpvs_flags("", SVs_TEMP));
2953 /* This macro is used by the stacked filetest operators :
2954 * if the previous filetest failed, short-circuit and pass its value.
2955 * Else, discard it from the stack and continue. --rgs
2957 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2958 if (!SvTRUE(TOPs)) { RETURN; } \
2959 else { (void)POPs; PUTBACK; } \
2966 /* Not const, because things tweak this below. Not bool, because there's
2967 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2968 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2969 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2970 /* Giving some sort of initial value silences compilers. */
2972 int access_mode = R_OK;
2974 int access_mode = 0;
2977 /* access_mode is never used, but leaving use_access in makes the
2978 conditional compiling below much clearer. */
2981 int stat_mode = S_IRUSR;
2983 bool effective = FALSE;
2987 switch (PL_op->op_type) {
2988 case OP_FTRREAD: opchar = 'R'; break;
2989 case OP_FTRWRITE: opchar = 'W'; break;
2990 case OP_FTREXEC: opchar = 'X'; break;
2991 case OP_FTEREAD: opchar = 'r'; break;
2992 case OP_FTEWRITE: opchar = 'w'; break;
2993 case OP_FTEEXEC: opchar = 'x'; break;
2995 tryAMAGICftest(opchar);
2997 STACKED_FTEST_CHECK;
2999 switch (PL_op->op_type) {
3001 #if !(defined(HAS_ACCESS) && defined(R_OK))
3007 #if defined(HAS_ACCESS) && defined(W_OK)
3012 stat_mode = S_IWUSR;
3016 #if defined(HAS_ACCESS) && defined(X_OK)
3021 stat_mode = S_IXUSR;
3025 #ifdef PERL_EFF_ACCESS
3028 stat_mode = S_IWUSR;
3032 #ifndef PERL_EFF_ACCESS
3039 #ifdef PERL_EFF_ACCESS
3044 stat_mode = S_IXUSR;
3050 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3051 const char *name = POPpx;
3053 # ifdef PERL_EFF_ACCESS
3054 result = PERL_EFF_ACCESS(name, access_mode);
3056 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3062 result = access(name, access_mode);
3064 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3079 if (cando(stat_mode, effective, &PL_statcache))
3088 const int op_type = PL_op->op_type;
3093 case OP_FTIS: opchar = 'e'; break;
3094 case OP_FTSIZE: opchar = 's'; break;
3095 case OP_FTMTIME: opchar = 'M'; break;
3096 case OP_FTCTIME: opchar = 'C'; break;
3097 case OP_FTATIME: opchar = 'A'; break;
3099 tryAMAGICftest(opchar);
3101 STACKED_FTEST_CHECK;
3107 if (op_type == OP_FTIS)
3110 /* You can't dTARGET inside OP_FTIS, because you'll get
3111 "panic: pad_sv po" - the op is not flagged to have a target. */
3115 #if Off_t_size > IVSIZE
3116 PUSHn(PL_statcache.st_size);
3118 PUSHi(PL_statcache.st_size);
3122 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3125 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3128 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3142 switch (PL_op->op_type) {
3143 case OP_FTROWNED: opchar = 'O'; break;
3144 case OP_FTEOWNED: opchar = 'o'; break;
3145 case OP_FTZERO: opchar = 'z'; break;
3146 case OP_FTSOCK: opchar = 'S'; break;
3147 case OP_FTCHR: opchar = 'c'; break;
3148 case OP_FTBLK: opchar = 'b'; break;
3149 case OP_FTFILE: opchar = 'f'; break;
3150 case OP_FTDIR: opchar = 'd'; break;
3151 case OP_FTPIPE: opchar = 'p'; break;
3152 case OP_FTSUID: opchar = 'u'; break;
3153 case OP_FTSGID: opchar = 'g'; break;
3154 case OP_FTSVTX: opchar = 'k'; break;
3156 tryAMAGICftest(opchar);
3158 /* I believe that all these three are likely to be defined on most every
3159 system these days. */
3161 if(PL_op->op_type == OP_FTSUID)
3165 if(PL_op->op_type == OP_FTSGID)
3169 if(PL_op->op_type == OP_FTSVTX)
3173 STACKED_FTEST_CHECK;
3179 switch (PL_op->op_type) {
3181 if (PL_statcache.st_uid == PL_uid)
3185 if (PL_statcache.st_uid == PL_euid)
3189 if (PL_statcache.st_size == 0)
3193 if (S_ISSOCK(PL_statcache.st_mode))
3197 if (S_ISCHR(PL_statcache.st_mode))
3201 if (S_ISBLK(PL_statcache.st_mode))
3205 if (S_ISREG(PL_statcache.st_mode))
3209 if (S_ISDIR(PL_statcache.st_mode))
3213 if (S_ISFIFO(PL_statcache.st_mode))
3218 if (PL_statcache.st_mode & S_ISUID)
3224 if (PL_statcache.st_mode & S_ISGID)
3230 if (PL_statcache.st_mode & S_ISVTX)
3244 tryAMAGICftest('l');
3245 result = my_lstat();
3250 if (S_ISLNK(PL_statcache.st_mode))
3263 tryAMAGICftest('t');
3265 STACKED_FTEST_CHECK;
3267 if (PL_op->op_flags & OPf_REF)
3269 else if (isGV(TOPs))
3270 gv = MUTABLE_GV(POPs);
3271 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3272 gv = MUTABLE_GV(SvRV(POPs));
3274 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3276 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3277 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3278 else if (tmpsv && SvOK(tmpsv)) {
3279 const char *tmps = SvPV_nolen_const(tmpsv);
3287 if (PerlLIO_isatty(fd))
3292 #if defined(atarist) /* this will work with atariST. Configure will
3293 make guesses for other systems. */
3294 # define FILE_base(f) ((f)->_base)
3295 # define FILE_ptr(f) ((f)->_ptr)
3296 # define FILE_cnt(f) ((f)->_cnt)
3297 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3308 register STDCHAR *s;
3314 tryAMAGICftest(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3316 STACKED_FTEST_CHECK;
3318 if (PL_op->op_flags & OPf_REF)
3320 else if (isGV(TOPs))
3321 gv = MUTABLE_GV(POPs);
3322 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3323 gv = MUTABLE_GV(SvRV(POPs));
3329 if (gv == PL_defgv) {
3331 io = GvIO(PL_statgv);
3334 goto really_filename;
3339 PL_laststatval = -1;
3340 sv_setpvs(PL_statname, "");
3341 io = GvIO(PL_statgv);
3343 if (io && IoIFP(io)) {
3344 if (! PerlIO_has_base(IoIFP(io)))
3345 DIE(aTHX_ "-T and -B not implemented on filehandles");
3346 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3347 if (PL_laststatval < 0)
3349 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3350 if (PL_op->op_type == OP_FTTEXT)
3355 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3356 i = PerlIO_getc(IoIFP(io));
3358 (void)PerlIO_ungetc(IoIFP(io),i);
3360 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3362 len = PerlIO_get_bufsiz(IoIFP(io));
3363 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3364 /* sfio can have large buffers - limit to 512 */
3369 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3371 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3373 SETERRNO(EBADF,RMS_IFI);
3381 PL_laststype = OP_STAT;
3382 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3383 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3384 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3386 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3389 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3390 if (PL_laststatval < 0) {
3391 (void)PerlIO_close(fp);
3394 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3395 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3396 (void)PerlIO_close(fp);
3398 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3399 RETPUSHNO; /* special case NFS directories */
3400 RETPUSHYES; /* null file is anything */
3405 /* now scan s to look for textiness */
3406 /* XXX ASCII dependent code */
3408 #if defined(DOSISH) || defined(USEMYBINMODE)
3409 /* ignore trailing ^Z on short files */
3410 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3414 for (i = 0; i < len; i++, s++) {
3415 if (!*s) { /* null never allowed in text */
3420 else if (!(isPRINT(*s) || isSPACE(*s)))
3423 else if (*s & 128) {
3425 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3428 /* utf8 characters don't count as odd */
3429 if (UTF8_IS_START(*s)) {
3430 int ulen = UTF8SKIP(s);
3431 if (ulen < len - i) {
3433 for (j = 1; j < ulen; j++) {
3434 if (!UTF8_IS_CONTINUATION(s[j]))
3437 --ulen; /* loop does extra increment */
3447 *s != '\n' && *s != '\r' && *s != '\b' &&
3448 *s != '\t' && *s != '\f' && *s != 27)
3453 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3464 const char *tmps = NULL;
3468 SV * const sv = POPs;
3469 if (PL_op->op_flags & OPf_SPECIAL) {
3470 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3472 else if (isGV_with_GP(sv)) {
3473 gv = MUTABLE_GV(sv);
3475 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3476 gv = MUTABLE_GV(SvRV(sv));
3479 tmps = SvPV_nolen_const(sv);
3483 if( !gv && (!tmps || !*tmps) ) {
3484 HV * const table = GvHVn(PL_envgv);
3487 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3488 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3490 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3495 deprecate("chdir('') or chdir(undef) as chdir()");
3496 tmps = SvPV_nolen_const(*svp);
3500 TAINT_PROPER("chdir");
3505 TAINT_PROPER("chdir");
3508 IO* const io = GvIO(gv);
3511 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3512 } else if (IoIFP(io)) {
3513 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3516 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3517 report_evil_fh(gv, io, PL_op->op_type);
3518 SETERRNO(EBADF, RMS_IFI);
3523 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3524 report_evil_fh(gv, io, PL_op->op_type);
3525 SETERRNO(EBADF,RMS_IFI);
3529 DIE(aTHX_ PL_no_func, "fchdir");
3533 PUSHi( PerlDir_chdir(tmps) >= 0 );
3535 /* Clear the DEFAULT element of ENV so we'll get the new value
3537 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3544 dVAR; dSP; dMARK; dTARGET;
3545 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3556 char * const tmps = POPpx;
3557 TAINT_PROPER("chroot");
3558 PUSHi( chroot(tmps) >= 0 );
3561 DIE(aTHX_ PL_no_func, "chroot");
3570 const char * const tmps2 = POPpconstx;
3571 const char * const tmps = SvPV_nolen_const(TOPs);
3572 TAINT_PROPER("rename");
3574 anum = PerlLIO_rename(tmps, tmps2);
3576 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3577 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3580 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3581 (void)UNLINK(tmps2);
3582 if (!(anum = link(tmps, tmps2)))
3583 anum = UNLINK(tmps);
3591 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3595 const int op_type = PL_op->op_type;
3599 if (op_type == OP_LINK)
3600 DIE(aTHX_ PL_no_func, "link");
3602 # ifndef HAS_SYMLINK
3603 if (op_type == OP_SYMLINK)
3604 DIE(aTHX_ PL_no_func, "symlink");
3608 const char * const tmps2 = POPpconstx;
3609 const char * const tmps = SvPV_nolen_const(TOPs);
3610 TAINT_PROPER(PL_op_desc[op_type]);
3612 # if defined(HAS_LINK)
3613 # if defined(HAS_SYMLINK)
3614 /* Both present - need to choose which. */
3615 (op_type == OP_LINK) ?
3616 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3618 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3619 PerlLIO_link(tmps, tmps2);
3622 # if defined(HAS_SYMLINK)
3623 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3624 symlink(tmps, tmps2);
3629 SETi( result >= 0 );
3636 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3648 char buf[MAXPATHLEN];
3651 #ifndef INCOMPLETE_TAINTS
3655 len = readlink(tmps, buf, sizeof(buf) - 1);
3663 RETSETUNDEF; /* just pretend it's a normal file */
3667 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3669 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3671 char * const save_filename = filename;
3676 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3678 PERL_ARGS_ASSERT_DOONELINER;
3680 Newx(cmdline, size, char);
3681 my_strlcpy(cmdline, cmd, size);
3682 my_strlcat(cmdline, " ", size);
3683 for (s = cmdline + strlen(cmdline); *filename; ) {
3687 if (s - cmdline < size)
3688 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3689 myfp = PerlProc_popen(cmdline, "r");
3693 SV * const tmpsv = sv_newmortal();
3694 /* Need to save/restore 'PL_rs' ?? */
3695 s = sv_gets(tmpsv, myfp, 0);
3696 (void)PerlProc_pclose(myfp);
3700 #ifdef HAS_SYS_ERRLIST
3705 /* you don't see this */
3706 const char * const errmsg =
3707 #ifdef HAS_SYS_ERRLIST
3715 if (instr(s, errmsg)) {
3722 #define EACCES EPERM
3724 if (instr(s, "cannot make"))
3725 SETERRNO(EEXIST,RMS_FEX);
3726 else if (instr(s, "existing file"))
3727 SETERRNO(EEXIST,RMS_FEX);
3728 else if (instr(s, "ile exists"))
3729 SETERRNO(EEXIST,RMS_FEX);
3730 else if (instr(s, "non-exist"))
3731 SETERRNO(ENOENT,RMS_FNF);
3732 else if (instr(s, "does not exist"))
3733 SETERRNO(ENOENT,RMS_FNF);
3734 else if (instr(s, "not empty"))
3735 SETERRNO(EBUSY,SS_DEVOFFLINE);
3736 else if (instr(s, "cannot access"))
3737 SETERRNO(EACCES,RMS_PRV);
3739 SETERRNO(EPERM,RMS_PRV);
3742 else { /* some mkdirs return no failure indication */
3743 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3744 if (PL_op->op_type == OP_RMDIR)
3749 SETERRNO(EACCES,RMS_PRV); /* a guess */
3758 /* This macro removes trailing slashes from a directory name.
3759 * Different operating and file systems take differently to
3760 * trailing slashes. According to POSIX 1003.1 1996 Edition
3761 * any number of trailing slashes should be allowed.
3762 * Thusly we snip them away so that even non-conforming
3763 * systems are happy.
3764 * We should probably do this "filtering" for all
3765 * the functions that expect (potentially) directory names:
3766 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3767 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3769 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3770 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3773 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3774 (tmps) = savepvn((tmps), (len)); \
3784 const int mode = (MAXARG > 1) ? POPi : 0777;
3786 TRIMSLASHES(tmps,len,copy);
3788 TAINT_PROPER("mkdir");
3790 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3794 SETi( dooneliner("mkdir", tmps) );
3795 oldumask = PerlLIO_umask(0);
3796 PerlLIO_umask(oldumask);
3797 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3812 TRIMSLASHES(tmps,len,copy);
3813 TAINT_PROPER("rmdir");
3815 SETi( PerlDir_rmdir(tmps) >= 0 );
3817 SETi( dooneliner("rmdir", tmps) );
3824 /* Directory calls. */
3828 #if defined(Direntry_t) && defined(HAS_READDIR)
3830 const char * const dirname = POPpconstx;
3831 GV * const gv = MUTABLE_GV(POPs);
3832 register IO * const io = GvIOn(gv);
3837 if ((IoIFP(io) || IoOFP(io)))
3838 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3839 "Opening filehandle %s also as a directory",
3842 PerlDir_close(IoDIRP(io));
3843 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3849 SETERRNO(EBADF,RMS_DIR);
3852 DIE(aTHX_ PL_no_dir_func, "opendir");
3859 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3860 DIE(aTHX_ PL_no_dir_func, "readdir");
3863 #if !defined(I_DIRENT) && !defined(VMS)
3864 Direntry_t *readdir (DIR *);
3870 const I32 gimme = GIMME;
3871 GV * const gv = MUTABLE_GV(POPs);
3872 register const Direntry_t *dp;
3873 register IO * const io = GvIOn(gv);
3875 if (!io || !IoDIRP(io)) {
3876 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3877 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3882 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3886 sv = newSVpvn(dp->d_name, dp->d_namlen);
3888 sv = newSVpv(dp->d_name, 0);
3890 #ifndef INCOMPLETE_TAINTS
3891 if (!(IoFLAGS(io) & IOf_UNTAINT))
3895 } while (gimme == G_ARRAY);
3897 if (!dp && gimme != G_ARRAY)
3904 SETERRNO(EBADF,RMS_ISI);
3905 if (GIMME == G_ARRAY)
3914 #if defined(HAS_TELLDIR) || defined(telldir)
3916 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3917 /* XXX netbsd still seemed to.
3918 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3919 --JHI 1999-Feb-02 */
3920 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3921 long telldir (DIR *);
3923 GV * const gv = MUTABLE_GV(POPs);
3924 register IO * const io = GvIOn(gv);
3926 if (!io || !IoDIRP(io)) {
3927 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3928 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3932 PUSHi( PerlDir_tell(IoDIRP(io)) );
3936 SETERRNO(EBADF,RMS_ISI);
3939 DIE(aTHX_ PL_no_dir_func, "telldir");
3946 #if defined(HAS_SEEKDIR) || defined(seekdir)
3948 const long along = POPl;
3949 GV * const gv = MUTABLE_GV(POPs);
3950 register IO * const io = GvIOn(gv);
3952 if (!io || !IoDIRP(io)) {
3953 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3954 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3957 (void)PerlDir_seek(IoDIRP(io), along);
3962 SETERRNO(EBADF,RMS_ISI);
3965 DIE(aTHX_ PL_no_dir_func, "seekdir");
3972 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3974 GV * const gv = MUTABLE_GV(POPs);
3975 register IO * const io = GvIOn(gv);
3977 if (!io || !IoDIRP(io)) {
3978 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3979 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3982 (void)PerlDir_rewind(IoDIRP(io));
3986 SETERRNO(EBADF,RMS_ISI);
3989 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3996 #if defined(Direntry_t) && defined(HAS_READDIR)
3998 GV * const gv = MUTABLE_GV(POPs);
3999 register IO * const io = GvIOn(gv);
4001 if (!io || !IoDIRP(io)) {
4002 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4003 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
4006 #ifdef VOID_CLOSEDIR
4007 PerlDir_close(IoDIRP(io));
4009 if (PerlDir_close(IoDIRP(io)) < 0) {
4010 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4019 SETERRNO(EBADF,RMS_IFI);
4022 DIE(aTHX_ PL_no_dir_func, "closedir");
4027 /* Process control. */
4036 PERL_FLUSHALL_FOR_CHILD;
4037 childpid = PerlProc_fork();
4041 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4043 SvREADONLY_off(GvSV(tmpgv));
4044 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4045 SvREADONLY_on(GvSV(tmpgv));
4047 #ifdef THREADS_HAVE_PIDS
4048 PL_ppid = (IV)getppid();
4050 #ifdef PERL_USES_PL_PIDSTATUS
4051 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4057 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4062 PERL_FLUSHALL_FOR_CHILD;
4063 childpid = PerlProc_fork();
4069 DIE(aTHX_ PL_no_func, "fork");
4077 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4082 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4083 childpid = wait4pid(-1, &argflags, 0);
4085 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4090 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4091 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4092 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4094 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4099 DIE(aTHX_ PL_no_func, "wait");
4106 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4108 const int optype = POPi;
4109 const Pid_t pid = TOPi;
4113 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4114 result = wait4pid(pid, &argflags, optype);
4116 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4121 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4122 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4123 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4125 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4130 DIE(aTHX_ PL_no_func, "waitpid");
4137 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4138 #if defined(__LIBCATAMOUNT__)
4139 PL_statusvalue = -1;
4148 while (++MARK <= SP) {
4149 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4154 TAINT_PROPER("system");
4156 PERL_FLUSHALL_FOR_CHILD;
4157 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4163 if (PerlProc_pipe(pp) >= 0)
4165 while ((childpid = PerlProc_fork()) == -1) {
4166 if (errno != EAGAIN) {
4171 PerlLIO_close(pp[0]);
4172 PerlLIO_close(pp[1]);
4179 Sigsave_t ihand,qhand; /* place to save signals during system() */
4183 PerlLIO_close(pp[1]);
4185 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4186 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4189 result = wait4pid(childpid, &status, 0);
4190 } while (result == -1 && errno == EINTR);
4192 (void)rsignal_restore(SIGINT, &ihand);
4193 (void)rsignal_restore(SIGQUIT, &qhand);
4195 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4196 do_execfree(); /* free any memory child malloced on fork */
4203 while (n < sizeof(int)) {
4204 n1 = PerlLIO_read(pp[0],
4205 (void*)(((char*)&errkid)+n),
4211 PerlLIO_close(pp[0]);
4212 if (n) { /* Error */
4213 if (n != sizeof(int))
4214 DIE(aTHX_ "panic: kid popen errno read");
4215 errno = errkid; /* Propagate errno from kid */
4216 STATUS_NATIVE_CHILD_SET(-1);
4219 XPUSHi(STATUS_CURRENT);
4223 PerlLIO_close(pp[0]);
4224 #if defined(HAS_FCNTL) && defined(F_SETFD)
4225 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4228 if (PL_op->op_flags & OPf_STACKED) {
4229 SV * const really = *++MARK;
4230 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4232 else if (SP - MARK != 1)
4233 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4235 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4239 #else /* ! FORK or VMS or OS/2 */
4242 if (PL_op->op_flags & OPf_STACKED) {
4243 SV * const really = *++MARK;
4244 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4245 value = (I32)do_aspawn(really, MARK, SP);
4247 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4250 else if (SP - MARK != 1) {
4251 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4252 value = (I32)do_aspawn(NULL, MARK, SP);
4254 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4258 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4260 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4262 STATUS_NATIVE_CHILD_SET(value);
4265 XPUSHi(result ? value : STATUS_CURRENT);
4266 #endif /* !FORK or VMS or OS/2 */
4273 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4278 while (++MARK <= SP) {
4279 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4284 TAINT_PROPER("exec");
4286 PERL_FLUSHALL_FOR_CHILD;
4287 if (PL_op->op_flags & OPf_STACKED) {
4288 SV * const really = *++MARK;
4289 value = (I32)do_aexec(really, MARK, SP);
4291 else if (SP - MARK != 1)
4293 value = (I32)vms_do_aexec(NULL, MARK, SP);
4297 (void ) do_aspawn(NULL, MARK, SP);
4301 value = (I32)do_aexec(NULL, MARK, SP);
4306 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4309 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4312 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4326 # ifdef THREADS_HAVE_PIDS
4327 if (PL_ppid != 1 && getppid() == 1)
4328 /* maybe the parent process has died. Refresh ppid cache */
4332 XPUSHi( getppid() );
4336 DIE(aTHX_ PL_no_func, "getppid");
4346 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4349 pgrp = (I32)BSD_GETPGRP(pid);
4351 if (pid != 0 && pid != PerlProc_getpid())
4352 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4358 DIE(aTHX_ PL_no_func, "getpgrp()");
4379 TAINT_PROPER("setpgrp");
4381 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4383 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4384 || (pid != 0 && pid != PerlProc_getpid()))
4386 DIE(aTHX_ "setpgrp can't take arguments");
4388 SETi( setpgrp() >= 0 );
4389 #endif /* USE_BSDPGRP */
4392 DIE(aTHX_ PL_no_func, "setpgrp()");
4399 #ifdef HAS_GETPRIORITY
4401 const int who = POPi;
4402 const int which = TOPi;
4403 SETi( getpriority(which, who) );
4406 DIE(aTHX_ PL_no_func, "getpriority()");
4413 #ifdef HAS_SETPRIORITY
4415 const int niceval = POPi;
4416 const int who = POPi;
4417 const int which = TOPi;
4418 TAINT_PROPER("setpriority");
4419 SETi( setpriority(which, who, niceval) >= 0 );
4422 DIE(aTHX_ PL_no_func, "setpriority()");
4433 XPUSHn( time(NULL) );
4435 XPUSHi( time(NULL) );
4447 (void)PerlProc_times(&PL_timesbuf);
4449 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4450 /* struct tms, though same data */
4454 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4455 if (GIMME == G_ARRAY) {
4456 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4457 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4458 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4466 if (GIMME == G_ARRAY) {
4473 DIE(aTHX_ "times not implemented");
4476 #endif /* HAS_TIMES */
4479 /* The 32 bit int year limits the times we can represent to these
4480 boundaries with a few days wiggle room to account for time zone
4483 /* Sat Jan 3 00:00:00 -2147481748 */
4484 #define TIME_LOWER_BOUND -67768100567755200.0
4485 /* Sun Dec 29 12:00:00 2147483647 */
4486 #define TIME_UPPER_BOUND 67767976233316800.0
4495 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4496 static const char * const dayname[] =
4497 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4498 static const char * const monname[] =
4499 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4500 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4505 when = (Time64_T)now;
4508 NV input = Perl_floor(POPn);
4509 when = (Time64_T)input;
4510 if (when != input) {
4511 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4512 "%s(%.0" NVff ") too large", opname, input);
4516 if ( TIME_LOWER_BOUND > when ) {
4517 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4518 "%s(%.0" NVff ") too small", opname, when);
4521 else if( when > TIME_UPPER_BOUND ) {
4522 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4523 "%s(%.0" NVff ") too large", opname, when);
4527 if (PL_op->op_type == OP_LOCALTIME)
4528 err = S_localtime64_r(&when, &tmbuf);
4530 err = S_gmtime64_r(&when, &tmbuf);
4534 /* XXX %lld broken for quads */
4535 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4536 "%s(%.0" NVff ") failed", opname, when);
4539 if (GIMME != G_ARRAY) { /* scalar context */
4541 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4542 double year = (double)tmbuf.tm_year + 1900;
4549 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4550 dayname[tmbuf.tm_wday],
4551 monname[tmbuf.tm_mon],
4559 else { /* list context */
4565 mPUSHi(tmbuf.tm_sec);
4566 mPUSHi(tmbuf.tm_min);
4567 mPUSHi(tmbuf.tm_hour);
4568 mPUSHi(tmbuf.tm_mday);
4569 mPUSHi(tmbuf.tm_mon);
4570 mPUSHn(tmbuf.tm_year);
4571 mPUSHi(tmbuf.tm_wday);
4572 mPUSHi(tmbuf.tm_yday);
4573 mPUSHi(tmbuf.tm_isdst);
4584 anum = alarm((unsigned int)anum);
4591 DIE(aTHX_ PL_no_func, "alarm");
4603 (void)time(&lasttime);
4608 PerlProc_sleep((unsigned int)duration);
4611 XPUSHi(when - lasttime);
4615 /* Shared memory. */
4616 /* Merged with some message passing. */
4620 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4621 dVAR; dSP; dMARK; dTARGET;
4622 const int op_type = PL_op->op_type;
4627 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4630 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4633 value = (I32)(do_semop(MARK, SP) >= 0);
4636 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4652 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4653 dVAR; dSP; dMARK; dTARGET;
4654 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4661 DIE(aTHX_ "System V IPC is not implemented on this machine");
4668 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4669 dVAR; dSP; dMARK; dTARGET;
4670 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4678 PUSHp(zero_but_true, ZBTLEN);
4686 /* I can't const this further without getting warnings about the types of
4687 various arrays passed in from structures. */
4689 S_space_join_names_mortal(pTHX_ char *const *array)
4693 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4695 if (array && *array) {
4696 target = newSVpvs_flags("", SVs_TEMP);
4698 sv_catpv(target, *array);
4701 sv_catpvs(target, " ");
4704 target = sv_mortalcopy(&PL_sv_no);
4709 /* Get system info. */
4713 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4715 I32 which = PL_op->op_type;
4716 register char **elem;
4718 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4719 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4720 struct hostent *gethostbyname(Netdb_name_t);
4721 struct hostent *gethostent(void);
4723 struct hostent *hent = NULL;
4727 if (which == OP_GHBYNAME) {
4728 #ifdef HAS_GETHOSTBYNAME
4729 const char* const name = POPpbytex;
4730 hent = PerlSock_gethostbyname(name);
4732 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4735 else if (which == OP_GHBYADDR) {
4736 #ifdef HAS_GETHOSTBYADDR
4737 const int addrtype = POPi;
4738 SV * const addrsv = POPs;
4740 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4742 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4744 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4748 #ifdef HAS_GETHOSTENT
4749 hent = PerlSock_gethostent();
4751 DIE(aTHX_ PL_no_sock_func, "gethostent");
4754 #ifdef HOST_NOT_FOUND
4756 #ifdef USE_REENTRANT_API
4757 # ifdef USE_GETHOSTENT_ERRNO
4758 h_errno = PL_reentrant_buffer->_gethostent_errno;
4761 STATUS_UNIX_SET(h_errno);
4765 if (GIMME != G_ARRAY) {
4766 PUSHs(sv = sv_newmortal());
4768 if (which == OP_GHBYNAME) {
4770 sv_setpvn(sv, hent->h_addr, hent->h_length);
4773 sv_setpv(sv, (char*)hent->h_name);
4779 mPUSHs(newSVpv((char*)hent->h_name, 0));
4780 PUSHs(space_join_names_mortal(hent->h_aliases));
4781 mPUSHi(hent->h_addrtype);
4782 len = hent->h_length;
4785 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4786 mXPUSHp(*elem, len);
4790 mPUSHp(hent->h_addr, len);
4792 PUSHs(sv_mortalcopy(&PL_sv_no));
4797 DIE(aTHX_ PL_no_sock_func, "gethostent");
4804 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4806 I32 which = PL_op->op_type;
4808 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4809 struct netent *getnetbyaddr(Netdb_net_t, int);
4810 struct netent *getnetbyname(Netdb_name_t);
4811 struct netent *getnetent(void);
4813 struct netent *nent;
4815 if (which == OP_GNBYNAME){
4816 #ifdef HAS_GETNETBYNAME
4817 const char * const name = POPpbytex;
4818 nent = PerlSock_getnetbyname(name);
4820 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4823 else if (which == OP_GNBYADDR) {
4824 #ifdef HAS_GETNETBYADDR
4825 const int addrtype = POPi;
4826 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4827 nent = PerlSock_getnetbyaddr(addr, addrtype);
4829 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4833 #ifdef HAS_GETNETENT
4834 nent = PerlSock_getnetent();
4836 DIE(aTHX_ PL_no_sock_func, "getnetent");
4839 #ifdef HOST_NOT_FOUND
4841 #ifdef USE_REENTRANT_API
4842 # ifdef USE_GETNETENT_ERRNO
4843 h_errno = PL_reentrant_buffer->_getnetent_errno;
4846 STATUS_UNIX_SET(h_errno);
4851 if (GIMME != G_ARRAY) {
4852 PUSHs(sv = sv_newmortal());
4854 if (which == OP_GNBYNAME)
4855 sv_setiv(sv, (IV)nent->n_net);
4857 sv_setpv(sv, nent->n_name);
4863 mPUSHs(newSVpv(nent->n_name, 0));
4864 PUSHs(space_join_names_mortal(nent->n_aliases));
4865 mPUSHi(nent->n_addrtype);
4866 mPUSHi(nent->n_net);
4871 DIE(aTHX_ PL_no_sock_func, "getnetent");
4878 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4880 I32 which = PL_op->op_type;
4882 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4883 struct protoent *getprotobyname(Netdb_name_t);
4884 struct protoent *getprotobynumber(int);
4885 struct protoent *getprotoent(void);
4887 struct protoent *pent;
4889 if (which == OP_GPBYNAME) {
4890 #ifdef HAS_GETPROTOBYNAME
4891 const char* const name = POPpbytex;
4892 pent = PerlSock_getprotobyname(name);
4894 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4897 else if (which == OP_GPBYNUMBER) {
4898 #ifdef HAS_GETPROTOBYNUMBER
4899 const int number = POPi;
4900 pent = PerlSock_getprotobynumber(number);
4902 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4906 #ifdef HAS_GETPROTOENT
4907 pent = PerlSock_getprotoent();
4909 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4913 if (GIMME != G_ARRAY) {
4914 PUSHs(sv = sv_newmortal());
4916 if (which == OP_GPBYNAME)
4917 sv_setiv(sv, (IV)pent->p_proto);
4919 sv_setpv(sv, pent->p_name);
4925 mPUSHs(newSVpv(pent->p_name, 0));
4926 PUSHs(space_join_names_mortal(pent->p_aliases));
4927 mPUSHi(pent->p_proto);
4932 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4939 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4941 I32 which = PL_op->op_type;
4943 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4944 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4945 struct servent *getservbyport(int, Netdb_name_t);
4946 struct servent *getservent(void);
4948 struct servent *sent;
4950 if (which == OP_GSBYNAME) {
4951 #ifdef HAS_GETSERVBYNAME
4952 const char * const proto = POPpbytex;
4953 const char * const name = POPpbytex;
4954 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4956 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4959 else if (which == OP_GSBYPORT) {
4960 #ifdef HAS_GETSERVBYPORT
4961 const char * const proto = POPpbytex;
4962 unsigned short port = (unsigned short)POPu;
4964 port = PerlSock_htons(port);
4966 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4968 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4972 #ifdef HAS_GETSERVENT
4973 sent = PerlSock_getservent();
4975 DIE(aTHX_ PL_no_sock_func, "getservent");
4979 if (GIMME != G_ARRAY) {
4980 PUSHs(sv = sv_newmortal());
4982 if (which == OP_GSBYNAME) {
4984 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4986 sv_setiv(sv, (IV)(sent->s_port));
4990 sv_setpv(sv, sent->s_name);
4996 mPUSHs(newSVpv(sent->s_name, 0));
4997 PUSHs(space_join_names_mortal(sent->s_aliases));
4999 mPUSHi(PerlSock_ntohs(sent->s_port));
5001 mPUSHi(sent->s_port);
5003 mPUSHs(newSVpv(sent->s_proto, 0));
5008 DIE(aTHX_ PL_no_sock_func, "getservent");
5015 #ifdef HAS_SETHOSTENT
5017 PerlSock_sethostent(TOPi);
5020 DIE(aTHX_ PL_no_sock_func, "sethostent");
5027 #ifdef HAS_SETNETENT
5029 (void)PerlSock_setnetent(TOPi);
5032 DIE(aTHX_ PL_no_sock_func, "setnetent");
5039 #ifdef HAS_SETPROTOENT
5041 (void)PerlSock_setprotoent(TOPi);
5044 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5051 #ifdef HAS_SETSERVENT
5053 (void)PerlSock_setservent(TOPi);
5056 DIE(aTHX_ PL_no_sock_func, "setservent");
5063 #ifdef HAS_ENDHOSTENT
5065 PerlSock_endhostent();
5069 DIE(aTHX_ PL_no_sock_func, "endhostent");
5076 #ifdef HAS_ENDNETENT
5078 PerlSock_endnetent();
5082 DIE(aTHX_ PL_no_sock_func, "endnetent");
5089 #ifdef HAS_ENDPROTOENT
5091 PerlSock_endprotoent();
5095 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5102 #ifdef HAS_ENDSERVENT
5104 PerlSock_endservent();
5108 DIE(aTHX_ PL_no_sock_func, "endservent");
5117 I32 which = PL_op->op_type;
5119 struct passwd *pwent = NULL;
5121 * We currently support only the SysV getsp* shadow password interface.
5122 * The interface is declared in <shadow.h> and often one needs to link
5123 * with -lsecurity or some such.
5124 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5127 * AIX getpwnam() is clever enough to return the encrypted password
5128 * only if the caller (euid?) is root.
5130 * There are at least three other shadow password APIs. Many platforms
5131 * seem to contain more than one interface for accessing the shadow
5132 * password databases, possibly for compatibility reasons.
5133 * The getsp*() is by far he simplest one, the other two interfaces
5134 * are much more complicated, but also very similar to each other.
5139 * struct pr_passwd *getprpw*();
5140 * The password is in
5141 * char getprpw*(...).ufld.fd_encrypt[]
5142 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5147 * struct es_passwd *getespw*();
5148 * The password is in
5149 * char *(getespw*(...).ufld.fd_encrypt)
5150 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5153 * struct userpw *getuserpw();
5154 * The password is in
5155 * char *(getuserpw(...)).spw_upw_passwd
5156 * (but the de facto standard getpwnam() should work okay)
5158 * Mention I_PROT here so that Configure probes for it.
5160 * In HP-UX for getprpw*() the manual page claims that one should include
5161 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5162 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5163 * and pp_sys.c already includes <shadow.h> if there is such.
5165 * Note that <sys/security.h> is already probed for, but currently
5166 * it is only included in special cases.
5168 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5169 * be preferred interface, even though also the getprpw*() interface
5170 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5171 * One also needs to call set_auth_parameters() in main() before
5172 * doing anything else, whether one is using getespw*() or getprpw*().
5174 * Note that accessing the shadow databases can be magnitudes
5175 * slower than accessing the standard databases.
5180 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5181 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5182 * the pw_comment is left uninitialized. */
5183 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5189 const char* const name = POPpbytex;
5190 pwent = getpwnam(name);
5196 pwent = getpwuid(uid);
5200 # ifdef HAS_GETPWENT
5202 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5203 if (pwent) pwent = getpwnam(pwent->pw_name);
5206 DIE(aTHX_ PL_no_func, "getpwent");
5212 if (GIMME != G_ARRAY) {
5213 PUSHs(sv = sv_newmortal());
5215 if (which == OP_GPWNAM)
5216 # if Uid_t_sign <= 0
5217 sv_setiv(sv, (IV)pwent->pw_uid);
5219 sv_setuv(sv, (UV)pwent->pw_uid);
5222 sv_setpv(sv, pwent->pw_name);
5228 mPUSHs(newSVpv(pwent->pw_name, 0));
5232 /* If we have getspnam(), we try to dig up the shadow
5233 * password. If we are underprivileged, the shadow
5234 * interface will set the errno to EACCES or similar,
5235 * and return a null pointer. If this happens, we will
5236 * use the dummy password (usually "*" or "x") from the
5237 * standard password database.
5239 * In theory we could skip the shadow call completely
5240 * if euid != 0 but in practice we cannot know which
5241 * security measures are guarding the shadow databases
5242 * on a random platform.
5244 * Resist the urge to use additional shadow interfaces.
5245 * Divert the urge to writing an extension instead.
5248 /* Some AIX setups falsely(?) detect some getspnam(), which
5249 * has a different API than the Solaris/IRIX one. */
5250 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5253 const struct spwd * const spwent = getspnam(pwent->pw_name);
5254 /* Save and restore errno so that
5255 * underprivileged attempts seem
5256 * to have never made the unsccessful
5257 * attempt to retrieve the shadow password. */
5259 if (spwent && spwent->sp_pwdp)
5260 sv_setpv(sv, spwent->sp_pwdp);
5264 if (!SvPOK(sv)) /* Use the standard password, then. */
5265 sv_setpv(sv, pwent->pw_passwd);
5268 # ifndef INCOMPLETE_TAINTS
5269 /* passwd is tainted because user himself can diddle with it.
5270 * admittedly not much and in a very limited way, but nevertheless. */
5274 # if Uid_t_sign <= 0
5275 mPUSHi(pwent->pw_uid);
5277 mPUSHu(pwent->pw_uid);
5280 # if Uid_t_sign <= 0
5281 mPUSHi(pwent->pw_gid);
5283 mPUSHu(pwent->pw_gid);
5285 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5286 * because of the poor interface of the Perl getpw*(),
5287 * not because there's some standard/convention saying so.
5288 * A better interface would have been to return a hash,
5289 * but we are accursed by our history, alas. --jhi. */
5291 mPUSHi(pwent->pw_change);
5294 mPUSHi(pwent->pw_quota);
5297 mPUSHs(newSVpv(pwent->pw_age, 0));
5299 /* I think that you can never get this compiled, but just in case. */
5300 PUSHs(sv_mortalcopy(&PL_sv_no));
5305 /* pw_class and pw_comment are mutually exclusive--.
5306 * see the above note for pw_change, pw_quota, and pw_age. */
5308 mPUSHs(newSVpv(pwent->pw_class, 0));
5311 mPUSHs(newSVpv(pwent->pw_comment, 0));
5313 /* I think that you can never get this compiled, but just in case. */
5314 PUSHs(sv_mortalcopy(&PL_sv_no));
5319 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5321 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5323 # ifndef INCOMPLETE_TAINTS
5324 /* pw_gecos is tainted because user himself can diddle with it. */
5328 mPUSHs(newSVpv(pwent->pw_dir, 0));
5330 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5331 # ifndef INCOMPLETE_TAINTS
5332 /* pw_shell is tainted because user himself can diddle with it. */
5337 mPUSHi(pwent->pw_expire);
5342 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5349 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5354 DIE(aTHX_ PL_no_func, "setpwent");
5361 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5366 DIE(aTHX_ PL_no_func, "endpwent");
5375 const I32 which = PL_op->op_type;
5376 const struct group *grent;
5378 if (which == OP_GGRNAM) {
5379 const char* const name = POPpbytex;
5380 grent = (const struct group *)getgrnam(name);
5382 else if (which == OP_GGRGID) {
5383 const Gid_t gid = POPi;
5384 grent = (const struct group *)getgrgid(gid);
5388 grent = (struct group *)getgrent();
5390 DIE(aTHX_ PL_no_func, "getgrent");
5394 if (GIMME != G_ARRAY) {
5395 SV * const sv = sv_newmortal();
5399 if (which == OP_GGRNAM)
5401 sv_setiv(sv, (IV)grent->gr_gid);
5403 sv_setuv(sv, (UV)grent->gr_gid);
5406 sv_setpv(sv, grent->gr_name);
5412 mPUSHs(newSVpv(grent->gr_name, 0));
5415 mPUSHs(newSVpv(grent->gr_passwd, 0));
5417 PUSHs(sv_mortalcopy(&PL_sv_no));
5421 mPUSHi(grent->gr_gid);
5423 mPUSHu(grent->gr_gid);
5426 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5427 /* In UNICOS/mk (_CRAYMPP) the multithreading
5428 * versions (getgrnam_r, getgrgid_r)
5429 * seem to return an illegal pointer
5430 * as the group members list, gr_mem.
5431 * getgrent() doesn't even have a _r version
5432 * but the gr_mem is poisonous anyway.
5433 * So yes, you cannot get the list of group
5434 * members if building multithreaded in UNICOS/mk. */
5435 PUSHs(space_join_names_mortal(grent->gr_mem));
5441 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5448 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5453 DIE(aTHX_ PL_no_func, "setgrent");
5460 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5465 DIE(aTHX_ PL_no_func, "endgrent");
5476 if (!(tmps = PerlProc_getlogin()))
5478 PUSHp(tmps, strlen(tmps));
5481 DIE(aTHX_ PL_no_func, "getlogin");
5486 /* Miscellaneous. */
5491 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5492 register I32 items = SP - MARK;
5493 unsigned long a[20];
5498 while (++MARK <= SP) {
5499 if (SvTAINTED(*MARK)) {
5505 TAINT_PROPER("syscall");
5508 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5509 * or where sizeof(long) != sizeof(char*). But such machines will
5510 * not likely have syscall implemented either, so who cares?
5512 while (++MARK <= SP) {
5513 if (SvNIOK(*MARK) || !i)
5514 a[i++] = SvIV(*MARK);
5515 else if (*MARK == &PL_sv_undef)
5518 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5524 DIE(aTHX_ "Too many args to syscall");
5526 DIE(aTHX_ "Too few args to syscall");
5528 retval = syscall(a[0]);
5531 retval = syscall(a[0],a[1]);
5534 retval = syscall(a[0],a[1],a[2]);
5537 retval = syscall(a[0],a[1],a[2],a[3]);
5540 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5543 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5546 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5549 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5553 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5556 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5559 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5563 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5567 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5571 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5572 a[10],a[11],a[12],a[13]);
5574 #endif /* atarist */
5580 DIE(aTHX_ PL_no_func, "syscall");
5585 #ifdef FCNTL_EMULATE_FLOCK
5587 /* XXX Emulate flock() with fcntl().
5588 What's really needed is a good file locking module.
5592 fcntl_emulate_flock(int fd, int operation)
5597 switch (operation & ~LOCK_NB) {
5599 flock.l_type = F_RDLCK;
5602 flock.l_type = F_WRLCK;
5605 flock.l_type = F_UNLCK;
5611 flock.l_whence = SEEK_SET;
5612 flock.l_start = flock.l_len = (Off_t)0;
5614 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5615 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5616 errno = EWOULDBLOCK;
5620 #endif /* FCNTL_EMULATE_FLOCK */
5622 #ifdef LOCKF_EMULATE_FLOCK
5624 /* XXX Emulate flock() with lockf(). This is just to increase
5625 portability of scripts. The calls are not completely
5626 interchangeable. What's really needed is a good file
5630 /* The lockf() constants might have been defined in <unistd.h>.
5631 Unfortunately, <unistd.h> causes troubles on some mixed
5632 (BSD/POSIX) systems, such as SunOS 4.1.3.
5634 Further, the lockf() constants aren't POSIX, so they might not be
5635 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5636 just stick in the SVID values and be done with it. Sigh.
5640 # define F_ULOCK 0 /* Unlock a previously locked region */
5643 # define F_LOCK 1 /* Lock a region for exclusive use */
5646 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5649 # define F_TEST 3 /* Test a region for other processes locks */
5653 lockf_emulate_flock(int fd, int operation)
5659 /* flock locks entire file so for lockf we need to do the same */
5660 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5661 if (pos > 0) /* is seekable and needs to be repositioned */
5662 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5663 pos = -1; /* seek failed, so don't seek back afterwards */
5666 switch (operation) {
5668 /* LOCK_SH - get a shared lock */
5670 /* LOCK_EX - get an exclusive lock */
5672 i = lockf (fd, F_LOCK, 0);
5675 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5676 case LOCK_SH|LOCK_NB:
5677 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5678 case LOCK_EX|LOCK_NB:
5679 i = lockf (fd, F_TLOCK, 0);
5681 if ((errno == EAGAIN) || (errno == EACCES))
5682 errno = EWOULDBLOCK;
5685 /* LOCK_UN - unlock (non-blocking is a no-op) */
5687 case LOCK_UN|LOCK_NB:
5688 i = lockf (fd, F_ULOCK, 0);
5691 /* Default - can't decipher operation */
5698 if (pos > 0) /* need to restore position of the handle */
5699 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5704 #endif /* LOCKF_EMULATE_FLOCK */
5708 * c-indentation-style: bsd
5710 * indent-tabs-mode: t
5713 * ex: set ts=8 sts=4 sw=4 noet: