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)
362 /* make a copy of the pattern, to ensure that magic is called once
364 TOPm1s = sv_2mortal(newSVsv(TOPm1s));
366 tryAMAGICunTARGET(iter_amg, -1, (PL_op->op_flags & OPf_SPECIAL));
368 if (PL_op->op_flags & OPf_SPECIAL) {
369 /* call Perl-level glob function instead. Stack args are:
370 * MARK, wildcard, csh_glob context index
371 * and following OPs should be: gv(CORE::GLOBAL::glob), entersub
375 /* stack args are: wildcard, gv(_GEN_n) */
378 /* Note that we only ever get here if File::Glob fails to load
379 * without at the same time croaking, for some reason, or if
380 * perl was built with PERL_EXTERNAL_GLOB */
382 ENTER_with_name("glob");
387 * The external globbing program may use things we can't control,
388 * so for security reasons we must assume the worst.
391 taint_proper(PL_no_security, "glob");
395 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
396 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
398 SAVESPTR(PL_rs); /* This is not permanent, either. */
399 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
402 *SvPVX(PL_rs) = '\n';
406 result = do_readline();
407 LEAVE_with_name("glob");
414 PL_last_in_gv = cGVOP_gv;
415 return do_readline();
426 do_join(TARG, &PL_sv_no, MARK, SP);
430 else if (SP == MARK) {
439 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
440 /* well-formed exception supplied */
442 else if (SvROK(ERRSV)) {
445 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
446 exsv = sv_mortalcopy(ERRSV);
447 sv_catpvs(exsv, "\t...caught");
450 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
452 if (SvROK(exsv) && !PL_warnhook)
453 Perl_warn(aTHX_ "%"SVf, SVfARG(exsv));
465 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
467 if (SP - MARK != 1) {
469 do_join(TARG, &PL_sv_no, MARK, SP);
477 if (SvROK(exsv) || (pv = SvPV_const(exsv, len), len)) {
478 /* well-formed exception supplied */
480 else if (SvROK(ERRSV)) {
482 if (sv_isobject(exsv)) {
483 HV * const stash = SvSTASH(SvRV(exsv));
484 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
486 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
487 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
494 call_sv(MUTABLE_SV(GvCV(gv)),
495 G_SCALAR|G_EVAL|G_KEEPERR);
496 exsv = sv_mortalcopy(*PL_stack_sp--);
500 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
501 exsv = sv_mortalcopy(ERRSV);
502 sv_catpvs(exsv, "\t...propagated");
505 exsv = newSVpvs_flags("Died", SVs_TEMP);
513 Perl_tied_method(pTHX_ const char *const methname, SV **sp, SV *const sv,
514 const MAGIC *const mg, const U32 flags, U32 argc, ...)
516 PERL_ARGS_ASSERT_TIED_METHOD;
518 /* Ensure that our flag bits do not overlap. */
519 assert((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
520 assert((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
521 assert((TIED_METHOD_SAY & G_WANT) == 0);
524 PUSHs(SvTIED_obj(sv, mg));
525 if (flags & TIED_METHOD_ARGUMENTS_ON_STACK)
528 const U32 mortalize_not_needed
529 = flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
531 va_start(args, argc);
533 SV *const arg = va_arg(args, SV *);
534 if(mortalize_not_needed)
543 ENTER_with_name("call_tied_method");
544 if (flags & TIED_METHOD_SAY) {
545 /* local $\ = "\n" */
546 SAVEGENERICSV(PL_ors_sv);
547 PL_ors_sv = newSVpvs("\n");
549 call_method(methname, flags & G_WANT);
550 LEAVE_with_name("call_tied_method");
554 #define tied_method0(a,b,c,d) \
555 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
556 #define tied_method1(a,b,c,d,e) \
557 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
558 #define tied_method2(a,b,c,d,e,f) \
559 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
572 GV * const gv = MUTABLE_GV(*++MARK);
574 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
575 DIE(aTHX_ PL_no_usym, "filehandle");
577 if ((io = GvIOp(gv))) {
579 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
582 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
583 "Opening dirhandle %s also as a file",
586 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
588 /* Method's args are same as ours ... */
589 /* ... except handle is replaced by the object */
590 return Perl_tied_method(aTHX_ "OPEN", mark - 1, MUTABLE_SV(io), mg,
591 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
603 tmps = SvPV_const(sv, len);
604 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
607 PUSHi( (I32)PL_forkprocess );
608 else if (PL_forkprocess == 0) /* we are a new child */
618 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
624 IO * const io = GvIO(gv);
626 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
628 return tied_method0("CLOSE", SP, MUTABLE_SV(io), mg);
632 PUSHs(boolSV(do_close(gv, TRUE)));
645 GV * const wgv = MUTABLE_GV(POPs);
646 GV * const rgv = MUTABLE_GV(POPs);
651 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
652 DIE(aTHX_ PL_no_usym, "filehandle");
657 do_close(rgv, FALSE);
659 do_close(wgv, FALSE);
661 if (PerlProc_pipe(fd) < 0)
664 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
665 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
666 IoOFP(rstio) = IoIFP(rstio);
667 IoIFP(wstio) = IoOFP(wstio);
668 IoTYPE(rstio) = IoTYPE_RDONLY;
669 IoTYPE(wstio) = IoTYPE_WRONLY;
671 if (!IoIFP(rstio) || !IoOFP(wstio)) {
673 PerlIO_close(IoIFP(rstio));
675 PerlLIO_close(fd[0]);
677 PerlIO_close(IoOFP(wstio));
679 PerlLIO_close(fd[1]);
682 #if defined(HAS_FCNTL) && defined(F_SETFD)
683 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
684 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
691 DIE(aTHX_ PL_no_func, "pipe");
705 gv = MUTABLE_GV(POPs);
709 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
711 return tied_method0("FILENO", SP, MUTABLE_SV(io), mg);
714 if (!io || !(fp = IoIFP(io))) {
715 /* Can't do this because people seem to do things like
716 defined(fileno($foo)) to check whether $foo is a valid fh.
723 PUSHi(PerlIO_fileno(fp));
736 anum = PerlLIO_umask(022);
737 /* setting it to 022 between the two calls to umask avoids
738 * to have a window where the umask is set to 0 -- meaning
739 * that another thread could create world-writeable files. */
741 (void)PerlLIO_umask(anum);
744 anum = PerlLIO_umask(POPi);
745 TAINT_PROPER("umask");
748 /* Only DIE if trying to restrict permissions on "user" (self).
749 * Otherwise it's harmless and more useful to just return undef
750 * since 'group' and 'other' concepts probably don't exist here. */
751 if (MAXARG >= 1 && (POPi & 0700))
752 DIE(aTHX_ "umask not implemented");
753 XPUSHs(&PL_sv_undef);
772 gv = MUTABLE_GV(POPs);
776 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
778 /* This takes advantage of the implementation of the varargs
779 function, which I don't think that the optimiser will be able to
780 figure out. Although, as it's a static function, in theory it
782 return Perl_tied_method(aTHX_ "BINMODE", SP, MUTABLE_SV(io), mg,
783 G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
784 discp ? 1 : 0, discp);
788 if (!io || !(fp = IoIFP(io))) {
790 SETERRNO(EBADF,RMS_IFI);
797 const char *d = NULL;
800 d = SvPV_const(discp, len);
801 mode = mode_from_discipline(d, len);
802 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
803 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
804 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
825 const I32 markoff = MARK - PL_stack_base;
826 const char *methname;
827 int how = PERL_MAGIC_tied;
831 switch(SvTYPE(varsv)) {
833 methname = "TIEHASH";
834 HvEITER_set(MUTABLE_HV(varsv), 0);
837 methname = "TIEARRAY";
841 if (isGV_with_GP(varsv)) {
842 if (SvFAKE(varsv) && !(GvFLAGS(varsv) & GVf_TIEWARNED)) {
843 deprecate("tie on a handle without *");
844 GvFLAGS(varsv) |= GVf_TIEWARNED;
846 methname = "TIEHANDLE";
847 how = PERL_MAGIC_tiedscalar;
848 /* For tied filehandles, we apply tiedscalar magic to the IO
849 slot of the GP rather than the GV itself. AMS 20010812 */
851 GvIOp(varsv) = newIO();
852 varsv = MUTABLE_SV(GvIOp(varsv));
857 methname = "TIESCALAR";
858 how = PERL_MAGIC_tiedscalar;
862 if (sv_isobject(*MARK)) { /* Calls GET magic. */
863 ENTER_with_name("call_TIE");
864 PUSHSTACKi(PERLSI_MAGIC);
866 EXTEND(SP,(I32)items);
870 call_method(methname, G_SCALAR);
873 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
874 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
875 * wrong error message, and worse case, supreme action at a distance.
876 * (Sorry obfuscation writers. You're not going to be given this one.)
879 const char *name = SvPV_nomg_const(*MARK, len);
880 stash = gv_stashpvn(name, len, 0);
881 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
882 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
883 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
885 ENTER_with_name("call_TIE");
886 PUSHSTACKi(PERLSI_MAGIC);
888 EXTEND(SP,(I32)items);
892 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
898 if (sv_isobject(sv)) {
899 sv_unmagic(varsv, how);
900 /* Croak if a self-tie on an aggregate is attempted. */
901 if (varsv == SvRV(sv) &&
902 (SvTYPE(varsv) == SVt_PVAV ||
903 SvTYPE(varsv) == SVt_PVHV))
905 "Self-ties of arrays and hashes are not supported");
906 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
908 LEAVE_with_name("call_TIE");
909 SP = PL_stack_base + markoff;
919 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
920 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
922 if (isGV_with_GP(sv)) {
923 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
924 deprecate("untie on a handle without *");
925 GvFLAGS(sv) |= GVf_TIEWARNED;
927 if (!(sv = MUTABLE_SV(GvIOp(sv))))
931 if ((mg = SvTIED_mg(sv, how))) {
932 SV * const obj = SvRV(SvTIED_obj(sv, mg));
934 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
936 if (gv && isGV(gv) && (cv = GvCV(gv))) {
938 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
939 mXPUSHi(SvREFCNT(obj) - 1);
941 ENTER_with_name("call_UNTIE");
942 call_sv(MUTABLE_SV(cv), G_VOID);
943 LEAVE_with_name("call_UNTIE");
946 else if (mg && SvREFCNT(obj) > 1) {
947 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
948 "untie attempted while %"UVuf" inner references still exist",
949 (UV)SvREFCNT(obj) - 1 ) ;
953 sv_unmagic(sv, how) ;
963 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
964 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
966 if (isGV_with_GP(sv)) {
967 if (SvFAKE(sv) && !(GvFLAGS(sv) & GVf_TIEWARNED)) {
968 deprecate("tied on a handle without *");
969 GvFLAGS(sv) |= GVf_TIEWARNED;
971 if (!(sv = MUTABLE_SV(GvIOp(sv))))
975 if ((mg = SvTIED_mg(sv, how))) {
976 SV *osv = SvTIED_obj(sv, mg);
977 if (osv == mg->mg_obj)
978 osv = sv_mortalcopy(osv);
992 HV * const hv = MUTABLE_HV(POPs);
993 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
994 stash = gv_stashsv(sv, 0);
995 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
997 require_pv("AnyDBM_File.pm");
999 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
1000 DIE(aTHX_ "No dbm on this machine");
1010 mPUSHu(O_RDWR|O_CREAT);
1015 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1018 if (!sv_isobject(TOPs)) {
1026 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1030 if (sv_isobject(TOPs)) {
1031 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1032 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1049 struct timeval timebuf;
1050 struct timeval *tbuf = &timebuf;
1053 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1058 # if BYTEORDER & 0xf0000
1059 # define ORDERBYTE (0x88888888 - BYTEORDER)
1061 # define ORDERBYTE (0x4444 - BYTEORDER)
1067 for (i = 1; i <= 3; i++) {
1068 SV * const sv = SP[i];
1071 if (SvREADONLY(sv)) {
1073 sv_force_normal_flags(sv, 0);
1074 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1075 Perl_croak_no_modify(aTHX);
1078 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1079 SvPV_force_nolen(sv); /* force string conversion */
1086 /* little endians can use vecs directly */
1087 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1094 masksize = NFDBITS / NBBY;
1096 masksize = sizeof(long); /* documented int, everyone seems to use long */
1098 Zero(&fd_sets[0], 4, char*);
1101 # if SELECT_MIN_BITS == 1
1102 growsize = sizeof(fd_set);
1104 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1105 # undef SELECT_MIN_BITS
1106 # define SELECT_MIN_BITS __FD_SETSIZE
1108 /* If SELECT_MIN_BITS is greater than one we most probably will want
1109 * to align the sizes with SELECT_MIN_BITS/8 because for example
1110 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1111 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1112 * on (sets/tests/clears bits) is 32 bits. */
1113 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1121 timebuf.tv_sec = (long)value;
1122 value -= (NV)timebuf.tv_sec;
1123 timebuf.tv_usec = (long)(value * 1000000.0);
1128 for (i = 1; i <= 3; i++) {
1130 if (!SvOK(sv) || SvCUR(sv) == 0) {
1137 Sv_Grow(sv, growsize);
1141 while (++j <= growsize) {
1145 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1147 Newx(fd_sets[i], growsize, char);
1148 for (offset = 0; offset < growsize; offset += masksize) {
1149 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1150 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1153 fd_sets[i] = SvPVX(sv);
1157 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1158 /* Can't make just the (void*) conditional because that would be
1159 * cpp #if within cpp macro, and not all compilers like that. */
1160 nfound = PerlSock_select(
1162 (Select_fd_set_t) fd_sets[1],
1163 (Select_fd_set_t) fd_sets[2],
1164 (Select_fd_set_t) fd_sets[3],
1165 (void*) tbuf); /* Workaround for compiler bug. */
1167 nfound = PerlSock_select(
1169 (Select_fd_set_t) fd_sets[1],
1170 (Select_fd_set_t) fd_sets[2],
1171 (Select_fd_set_t) fd_sets[3],
1174 for (i = 1; i <= 3; i++) {
1177 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1179 for (offset = 0; offset < growsize; offset += masksize) {
1180 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1181 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1183 Safefree(fd_sets[i]);
1190 if (GIMME == G_ARRAY && tbuf) {
1191 value = (NV)(timebuf.tv_sec) +
1192 (NV)(timebuf.tv_usec) / 1000000.0;
1197 DIE(aTHX_ "select not implemented");
1202 =for apidoc setdefout
1204 Sets PL_defoutgv, the default file handle for output, to the passed in
1205 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1206 count of the passed in typeglob is increased by one, and the reference count
1207 of the typeglob that PL_defoutgv points to is decreased by one.
1213 Perl_setdefout(pTHX_ GV *gv)
1216 SvREFCNT_inc_simple_void(gv);
1217 SvREFCNT_dec(PL_defoutgv);
1225 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1226 GV * egv = GvEGVx(PL_defoutgv);
1230 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1232 XPUSHs(&PL_sv_undef);
1234 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1235 if (gvp && *gvp == egv) {
1236 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1240 mXPUSHs(newRV(MUTABLE_SV(egv)));
1245 if (!GvIO(newdefout))
1246 gv_IOadd(newdefout);
1247 setdefout(newdefout);
1256 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1257 IO *const io = GvIO(gv);
1263 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1265 const U32 gimme = GIMME_V;
1266 Perl_tied_method(aTHX_ "GETC", SP, MUTABLE_SV(io), mg, gimme, 0);
1267 if (gimme == G_SCALAR) {
1269 SvSetMagicSV_nosteal(TARG, TOPs);
1274 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1275 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1277 SETERRNO(EBADF,RMS_IFI);
1281 sv_setpvs(TARG, " ");
1282 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1283 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1284 /* Find out how many bytes the char needs */
1285 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1288 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1289 SvCUR_set(TARG,1+len);
1298 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1301 register PERL_CONTEXT *cx;
1302 const I32 gimme = GIMME_V;
1304 PERL_ARGS_ASSERT_DOFORM;
1306 if (cv && CvCLONE(cv))
1307 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1312 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1313 PUSHFORMAT(cx, retop);
1315 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1317 setdefout(gv); /* locally select filehandle so $% et al work */
1336 gv = MUTABLE_GV(POPs);
1350 goto not_a_format_reference;
1355 tmpsv = sv_newmortal();
1356 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1357 name = SvPV_nolen_const(tmpsv);
1359 DIE(aTHX_ "Undefined format \"%s\" called", name);
1361 not_a_format_reference:
1362 DIE(aTHX_ "Not a format reference");
1364 IoFLAGS(io) &= ~IOf_DIDTOP;
1365 return doform(cv,gv,PL_op->op_next);
1371 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1372 register IO * const io = GvIOp(gv);
1377 register PERL_CONTEXT *cx;
1380 if (!io || !(ofp = IoOFP(io)))
1383 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1384 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1386 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1387 PL_formtarget != PL_toptarget)
1391 if (!IoTOP_GV(io)) {
1394 if (!IoTOP_NAME(io)) {
1396 if (!IoFMT_NAME(io))
1397 IoFMT_NAME(io) = savepv(GvNAME(gv));
1398 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1399 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1400 if ((topgv && GvFORM(topgv)) ||
1401 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1402 IoTOP_NAME(io) = savesvpv(topname);
1404 IoTOP_NAME(io) = savepvs("top");
1406 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1407 if (!topgv || !GvFORM(topgv)) {
1408 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1411 IoTOP_GV(io) = topgv;
1413 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1414 I32 lines = IoLINES_LEFT(io);
1415 const char *s = SvPVX_const(PL_formtarget);
1416 if (lines <= 0) /* Yow, header didn't even fit!!! */
1418 while (lines-- > 0) {
1419 s = strchr(s, '\n');
1425 const STRLEN save = SvCUR(PL_formtarget);
1426 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1427 do_print(PL_formtarget, ofp);
1428 SvCUR_set(PL_formtarget, save);
1429 sv_chop(PL_formtarget, s);
1430 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1433 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1434 do_print(PL_formfeed, ofp);
1435 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1437 PL_formtarget = PL_toptarget;
1438 IoFLAGS(io) |= IOf_DIDTOP;
1441 DIE(aTHX_ "bad top format reference");
1444 SV * const sv = sv_newmortal();
1446 gv_efullname4(sv, fgv, NULL, FALSE);
1447 name = SvPV_nolen_const(sv);
1449 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1451 DIE(aTHX_ "Undefined top format called");
1453 return doform(cv, gv, PL_op);
1457 POPBLOCK(cx,PL_curpm);
1459 retop = cx->blk_sub.retop;
1465 report_wrongway_fh(gv, '<');
1471 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1472 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1474 if (!do_print(PL_formtarget, fp))
1477 FmLINES(PL_formtarget) = 0;
1478 SvCUR_set(PL_formtarget, 0);
1479 *SvEND(PL_formtarget) = '\0';
1480 if (IoFLAGS(io) & IOf_FLUSH)
1481 (void)PerlIO_flush(fp);
1486 PL_formtarget = PL_bodytarget;
1488 PERL_UNUSED_VAR(newsp);
1489 PERL_UNUSED_VAR(gimme);
1495 dVAR; dSP; dMARK; dORIGMARK;
1500 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1501 IO *const io = GvIO(gv);
1504 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1506 if (MARK == ORIGMARK) {
1509 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1512 return Perl_tied_method(aTHX_ "PRINTF", mark - 1, MUTABLE_SV(io),
1514 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1522 SETERRNO(EBADF,RMS_IFI);
1525 else if (!(fp = IoOFP(io))) {
1527 report_wrongway_fh(gv, '<');
1528 else if (ckWARN(WARN_CLOSED))
1530 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1534 if (SvTAINTED(MARK[1]))
1535 TAINT_PROPER("printf");
1536 do_sprintf(sv, SP - MARK, MARK + 1);
1537 if (!do_print(sv, fp))
1540 if (IoFLAGS(io) & IOf_FLUSH)
1541 if (PerlIO_flush(fp) == EOF)
1552 PUSHs(&PL_sv_undef);
1560 const int perm = (MAXARG > 3) ? POPi : 0666;
1561 const int mode = POPi;
1562 SV * const sv = POPs;
1563 GV * const gv = MUTABLE_GV(POPs);
1566 /* Need TIEHANDLE method ? */
1567 const char * const tmps = SvPV_const(sv, len);
1568 /* FIXME? do_open should do const */
1569 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1570 IoLINES(GvIOp(gv)) = 0;
1574 PUSHs(&PL_sv_undef);
1581 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1587 Sock_size_t bufsize;
1595 bool charstart = FALSE;
1596 STRLEN charskip = 0;
1599 GV * const gv = MUTABLE_GV(*++MARK);
1600 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1601 && gv && (io = GvIO(gv)) )
1603 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1605 return Perl_tied_method(aTHX_ "READ", mark - 1, MUTABLE_SV(io), mg,
1606 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1615 sv_setpvs(bufsv, "");
1616 length = SvIVx(*++MARK);
1619 offset = SvIVx(*++MARK);
1623 if (!io || !IoIFP(io)) {
1625 SETERRNO(EBADF,RMS_IFI);
1628 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1629 buffer = SvPVutf8_force(bufsv, blen);
1630 /* UTF-8 may not have been set if they are all low bytes */
1635 buffer = SvPV_force(bufsv, blen);
1636 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1639 DIE(aTHX_ "Negative length");
1647 if (PL_op->op_type == OP_RECV) {
1648 char namebuf[MAXPATHLEN];
1649 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1650 bufsize = sizeof (struct sockaddr_in);
1652 bufsize = sizeof namebuf;
1654 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1658 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1659 /* 'offset' means 'flags' here */
1660 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1661 (struct sockaddr *)namebuf, &bufsize);
1664 /* MSG_TRUNC can give oversized count; quietly lose it */
1668 /* Bogus return without padding */
1669 bufsize = sizeof (struct sockaddr_in);
1671 SvCUR_set(bufsv, count);
1672 *SvEND(bufsv) = '\0';
1673 (void)SvPOK_only(bufsv);
1677 /* This should not be marked tainted if the fp is marked clean */
1678 if (!(IoFLAGS(io) & IOf_UNTAINT))
1679 SvTAINTED_on(bufsv);
1681 sv_setpvn(TARG, namebuf, bufsize);
1686 if (DO_UTF8(bufsv)) {
1687 /* offset adjust in characters not bytes */
1688 blen = sv_len_utf8(bufsv);
1691 if (-offset > (int)blen)
1692 DIE(aTHX_ "Offset outside string");
1695 if (DO_UTF8(bufsv)) {
1696 /* convert offset-as-chars to offset-as-bytes */
1697 if (offset >= (int)blen)
1698 offset += SvCUR(bufsv) - blen;
1700 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1703 bufsize = SvCUR(bufsv);
1704 /* Allocating length + offset + 1 isn't perfect in the case of reading
1705 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1707 (should be 2 * length + offset + 1, or possibly something longer if
1708 PL_encoding is true) */
1709 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1710 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1711 Zero(buffer+bufsize, offset-bufsize, char);
1713 buffer = buffer + offset;
1715 read_target = bufsv;
1717 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1718 concatenate it to the current buffer. */
1720 /* Truncate the existing buffer to the start of where we will be
1722 SvCUR_set(bufsv, offset);
1724 read_target = sv_newmortal();
1725 SvUPGRADE(read_target, SVt_PV);
1726 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1729 if (PL_op->op_type == OP_SYSREAD) {
1730 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1731 if (IoTYPE(io) == IoTYPE_SOCKET) {
1732 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1738 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1743 #ifdef HAS_SOCKET__bad_code_maybe
1744 if (IoTYPE(io) == IoTYPE_SOCKET) {
1745 char namebuf[MAXPATHLEN];
1746 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1747 bufsize = sizeof (struct sockaddr_in);
1749 bufsize = sizeof namebuf;
1751 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1752 (struct sockaddr *)namebuf, &bufsize);
1757 count = PerlIO_read(IoIFP(io), buffer, length);
1758 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1759 if (count == 0 && PerlIO_error(IoIFP(io)))
1763 if (IoTYPE(io) == IoTYPE_WRONLY)
1764 report_wrongway_fh(gv, '>');
1767 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1768 *SvEND(read_target) = '\0';
1769 (void)SvPOK_only(read_target);
1770 if (fp_utf8 && !IN_BYTES) {
1771 /* Look at utf8 we got back and count the characters */
1772 const char *bend = buffer + count;
1773 while (buffer < bend) {
1775 skip = UTF8SKIP(buffer);
1778 if (buffer - charskip + skip > bend) {
1779 /* partial character - try for rest of it */
1780 length = skip - (bend-buffer);
1781 offset = bend - SvPVX_const(bufsv);
1793 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1794 provided amount read (count) was what was requested (length)
1796 if (got < wanted && count == length) {
1797 length = wanted - got;
1798 offset = bend - SvPVX_const(bufsv);
1801 /* return value is character count */
1805 else if (buffer_utf8) {
1806 /* Let svcatsv upgrade the bytes we read in to utf8.
1807 The buffer is a mortal so will be freed soon. */
1808 sv_catsv_nomg(bufsv, read_target);
1811 /* This should not be marked tainted if the fp is marked clean */
1812 if (!(IoFLAGS(io) & IOf_UNTAINT))
1813 SvTAINTED_on(bufsv);
1825 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1830 STRLEN orig_blen_bytes;
1831 const int op_type = PL_op->op_type;
1834 GV *const gv = MUTABLE_GV(*++MARK);
1835 IO *const io = GvIO(gv);
1837 if (op_type == OP_SYSWRITE && io) {
1838 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1840 if (MARK == SP - 1) {
1842 mXPUSHi(sv_len(sv));
1846 return Perl_tied_method(aTHX_ "WRITE", mark - 1, MUTABLE_SV(io), mg,
1847 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1857 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1859 if (io && IoIFP(io))
1860 report_wrongway_fh(gv, '<');
1863 SETERRNO(EBADF,RMS_IFI);
1867 /* Do this first to trigger any overloading. */
1868 buffer = SvPV_const(bufsv, blen);
1869 orig_blen_bytes = blen;
1870 doing_utf8 = DO_UTF8(bufsv);
1872 if (PerlIO_isutf8(IoIFP(io))) {
1873 if (!SvUTF8(bufsv)) {
1874 /* We don't modify the original scalar. */
1875 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1876 buffer = (char *) tmpbuf;
1880 else if (doing_utf8) {
1881 STRLEN tmplen = blen;
1882 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1885 buffer = (char *) tmpbuf;
1889 assert((char *)result == buffer);
1890 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1895 if (op_type == OP_SEND) {
1896 const int flags = SvIVx(*++MARK);
1899 char * const sockbuf = SvPVx(*++MARK, mlen);
1900 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1901 flags, (struct sockaddr *)sockbuf, mlen);
1905 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1911 Size_t length = 0; /* This length is in characters. */
1917 /* The SV is bytes, and we've had to upgrade it. */
1918 blen_chars = orig_blen_bytes;
1920 /* The SV really is UTF-8. */
1921 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1922 /* Don't call sv_len_utf8 again because it will call magic
1923 or overloading a second time, and we might get back a
1924 different result. */
1925 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1927 /* It's safe, and it may well be cached. */
1928 blen_chars = sv_len_utf8(bufsv);
1936 length = blen_chars;
1938 #if Size_t_size > IVSIZE
1939 length = (Size_t)SvNVx(*++MARK);
1941 length = (Size_t)SvIVx(*++MARK);
1943 if ((SSize_t)length < 0) {
1945 DIE(aTHX_ "Negative length");
1950 offset = SvIVx(*++MARK);
1952 if (-offset > (IV)blen_chars) {
1954 DIE(aTHX_ "Offset outside string");
1956 offset += blen_chars;
1957 } else if (offset > (IV)blen_chars) {
1959 DIE(aTHX_ "Offset outside string");
1963 if (length > blen_chars - offset)
1964 length = blen_chars - offset;
1966 /* Here we convert length from characters to bytes. */
1967 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1968 /* Either we had to convert the SV, or the SV is magical, or
1969 the SV has overloading, in which case we can't or mustn't
1970 or mustn't call it again. */
1972 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1973 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1975 /* It's a real UTF-8 SV, and it's not going to change under
1976 us. Take advantage of any cache. */
1978 I32 len_I32 = length;
1980 /* Convert the start and end character positions to bytes.
1981 Remember that the second argument to sv_pos_u2b is relative
1983 sv_pos_u2b(bufsv, &start, &len_I32);
1990 buffer = buffer+offset;
1992 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1993 if (IoTYPE(io) == IoTYPE_SOCKET) {
1994 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
2000 /* See the note at doio.c:do_print about filesize limits. --jhi */
2001 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
2010 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2013 #if Size_t_size > IVSIZE
2033 * in Perl 5.12 and later, the additional parameter is a bitmask:
2036 * 2 = eof() <- ARGV magic
2038 * I'll rely on the compiler's trace flow analysis to decide whether to
2039 * actually assign this out here, or punt it into the only block where it is
2040 * used. Doing it out here is DRY on the condition logic.
2045 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2051 if (PL_op->op_flags & OPf_SPECIAL) {
2052 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2056 gv = PL_last_in_gv; /* eof */
2064 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2065 return tied_method1("EOF", SP, MUTABLE_SV(io), mg, newSVuv(which));
2068 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2069 if (io && !IoIFP(io)) {
2070 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2072 IoFLAGS(io) &= ~IOf_START;
2073 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2075 sv_setpvs(GvSV(gv), "-");
2077 GvSV(gv) = newSVpvs("-");
2078 SvSETMAGIC(GvSV(gv));
2080 else if (!nextargv(gv))
2085 PUSHs(boolSV(do_eof(gv)));
2096 PL_last_in_gv = MUTABLE_GV(POPs);
2103 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2105 return tied_method0("TELL", SP, MUTABLE_SV(io), mg);
2110 SETERRNO(EBADF,RMS_IFI);
2115 #if LSEEKSIZE > IVSIZE
2116 PUSHn( do_tell(gv) );
2118 PUSHi( do_tell(gv) );
2126 const int whence = POPi;
2127 #if LSEEKSIZE > IVSIZE
2128 const Off_t offset = (Off_t)SvNVx(POPs);
2130 const Off_t offset = (Off_t)SvIVx(POPs);
2133 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2134 IO *const io = GvIO(gv);
2137 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2139 #if LSEEKSIZE > IVSIZE
2140 SV *const offset_sv = newSVnv((NV) offset);
2142 SV *const offset_sv = newSViv(offset);
2145 return tied_method2("SEEK", SP, MUTABLE_SV(io), mg, offset_sv,
2150 if (PL_op->op_type == OP_SEEK)
2151 PUSHs(boolSV(do_seek(gv, offset, whence)));
2153 const Off_t sought = do_sysseek(gv, offset, whence);
2155 PUSHs(&PL_sv_undef);
2157 SV* const sv = sought ?
2158 #if LSEEKSIZE > IVSIZE
2163 : newSVpvn(zero_but_true, ZBTLEN);
2174 /* There seems to be no consensus on the length type of truncate()
2175 * and ftruncate(), both off_t and size_t have supporters. In
2176 * general one would think that when using large files, off_t is
2177 * at least as wide as size_t, so using an off_t should be okay. */
2178 /* XXX Configure probe for the length type of *truncate() needed XXX */
2181 #if Off_t_size > IVSIZE
2186 /* Checking for length < 0 is problematic as the type might or
2187 * might not be signed: if it is not, clever compilers will moan. */
2188 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2195 if (PL_op->op_flags & OPf_SPECIAL) {
2196 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2205 TAINT_PROPER("truncate");
2206 if (!(fp = IoIFP(io))) {
2212 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2214 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2221 SV * const sv = POPs;
2224 if (isGV_with_GP(sv)) {
2225 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2226 goto do_ftruncate_gv;
2228 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2229 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2230 goto do_ftruncate_gv;
2232 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2233 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2234 goto do_ftruncate_io;
2237 name = SvPV_nolen_const(sv);
2238 TAINT_PROPER("truncate");
2240 if (truncate(name, len) < 0)
2244 const int tmpfd = PerlLIO_open(name, O_RDWR);
2249 if (my_chsize(tmpfd, len) < 0)
2251 PerlLIO_close(tmpfd);
2260 SETERRNO(EBADF,RMS_IFI);
2268 SV * const argsv = POPs;
2269 const unsigned int func = POPu;
2270 const int optype = PL_op->op_type;
2271 GV * const gv = MUTABLE_GV(POPs);
2272 IO * const io = gv ? GvIOn(gv) : NULL;
2276 if (!io || !argsv || !IoIFP(io)) {
2278 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2282 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2285 s = SvPV_force(argsv, len);
2286 need = IOCPARM_LEN(func);
2288 s = Sv_Grow(argsv, need + 1);
2289 SvCUR_set(argsv, need);
2292 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2295 retval = SvIV(argsv);
2296 s = INT2PTR(char*,retval); /* ouch */
2299 TAINT_PROPER(PL_op_desc[optype]);
2301 if (optype == OP_IOCTL)
2303 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2305 DIE(aTHX_ "ioctl is not implemented");
2309 DIE(aTHX_ "fcntl is not implemented");
2311 #if defined(OS2) && defined(__EMX__)
2312 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2314 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2318 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2320 if (s[SvCUR(argsv)] != 17)
2321 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2323 s[SvCUR(argsv)] = 0; /* put our null back */
2324 SvSETMAGIC(argsv); /* Assume it has changed */
2333 PUSHp(zero_but_true, ZBTLEN);
2344 const int argtype = POPi;
2345 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2346 IO *const io = GvIO(gv);
2347 PerlIO *const fp = io ? IoIFP(io) : NULL;
2349 /* XXX Looks to me like io is always NULL at this point */
2351 (void)PerlIO_flush(fp);
2352 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2357 SETERRNO(EBADF,RMS_IFI);
2362 DIE(aTHX_ PL_no_func, "flock()");
2373 const int protocol = POPi;
2374 const int type = POPi;
2375 const int domain = POPi;
2376 GV * const gv = MUTABLE_GV(POPs);
2377 register IO * const io = gv ? GvIOn(gv) : NULL;
2382 if (io && IoIFP(io))
2383 do_close(gv, FALSE);
2384 SETERRNO(EBADF,LIB_INVARG);
2389 do_close(gv, FALSE);
2391 TAINT_PROPER("socket");
2392 fd = PerlSock_socket(domain, type, protocol);
2395 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2396 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2397 IoTYPE(io) = IoTYPE_SOCKET;
2398 if (!IoIFP(io) || !IoOFP(io)) {
2399 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2400 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2401 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2404 #if defined(HAS_FCNTL) && defined(F_SETFD)
2405 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2409 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2418 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2420 const int protocol = POPi;
2421 const int type = POPi;
2422 const int domain = POPi;
2423 GV * const gv2 = MUTABLE_GV(POPs);
2424 GV * const gv1 = MUTABLE_GV(POPs);
2425 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2426 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2430 report_evil_fh(gv1);
2432 report_evil_fh(gv2);
2434 if (io1 && IoIFP(io1))
2435 do_close(gv1, FALSE);
2436 if (io2 && IoIFP(io2))
2437 do_close(gv2, FALSE);
2442 TAINT_PROPER("socketpair");
2443 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2445 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2446 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2447 IoTYPE(io1) = IoTYPE_SOCKET;
2448 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2449 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2450 IoTYPE(io2) = IoTYPE_SOCKET;
2451 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2452 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2453 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2454 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2455 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2456 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2457 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2460 #if defined(HAS_FCNTL) && defined(F_SETFD)
2461 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2462 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2467 DIE(aTHX_ PL_no_sock_func, "socketpair");
2476 SV * const addrsv = POPs;
2477 /* OK, so on what platform does bind modify addr? */
2479 GV * const gv = MUTABLE_GV(POPs);
2480 register IO * const io = GvIOn(gv);
2482 const int op_type = PL_op->op_type;
2484 if (!io || !IoIFP(io))
2487 addr = SvPV_const(addrsv, len);
2488 TAINT_PROPER(PL_op_desc[op_type]);
2489 if ((op_type == OP_BIND
2490 ? PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len)
2491 : PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len))
2499 SETERRNO(EBADF,SS_IVCHAN);
2506 const int backlog = POPi;
2507 GV * const gv = MUTABLE_GV(POPs);
2508 register IO * const io = gv ? GvIOn(gv) : NULL;
2510 if (!io || !IoIFP(io))
2513 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2520 SETERRNO(EBADF,SS_IVCHAN);
2529 char namebuf[MAXPATHLEN];
2530 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2531 Sock_size_t len = sizeof (struct sockaddr_in);
2533 Sock_size_t len = sizeof namebuf;
2535 GV * const ggv = MUTABLE_GV(POPs);
2536 GV * const ngv = MUTABLE_GV(POPs);
2545 if (!gstio || !IoIFP(gstio))
2549 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2552 /* Some platforms indicate zero length when an AF_UNIX client is
2553 * not bound. Simulate a non-zero-length sockaddr structure in
2555 namebuf[0] = 0; /* sun_len */
2556 namebuf[1] = AF_UNIX; /* sun_family */
2564 do_close(ngv, FALSE);
2565 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2566 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2567 IoTYPE(nstio) = IoTYPE_SOCKET;
2568 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2569 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2570 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2571 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2574 #if defined(HAS_FCNTL) && defined(F_SETFD)
2575 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2579 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2580 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2582 #ifdef __SCO_VERSION__
2583 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2586 PUSHp(namebuf, len);
2590 report_evil_fh(ggv);
2591 SETERRNO(EBADF,SS_IVCHAN);
2601 const int how = POPi;
2602 GV * const gv = MUTABLE_GV(POPs);
2603 register IO * const io = GvIOn(gv);
2605 if (!io || !IoIFP(io))
2608 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2613 SETERRNO(EBADF,SS_IVCHAN);
2620 const int optype = PL_op->op_type;
2621 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2622 const unsigned int optname = (unsigned int) POPi;
2623 const unsigned int lvl = (unsigned int) POPi;
2624 GV * const gv = MUTABLE_GV(POPs);
2625 register IO * const io = GvIOn(gv);
2629 if (!io || !IoIFP(io))
2632 fd = PerlIO_fileno(IoIFP(io));
2636 (void)SvPOK_only(sv);
2640 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2647 #if defined(__SYMBIAN32__)
2648 # define SETSOCKOPT_OPTION_VALUE_T void *
2650 # define SETSOCKOPT_OPTION_VALUE_T const char *
2652 /* XXX TODO: We need to have a proper type (a Configure probe,
2653 * etc.) for what the C headers think of the third argument of
2654 * setsockopt(), the option_value read-only buffer: is it
2655 * a "char *", or a "void *", const or not. Some compilers
2656 * don't take kindly to e.g. assuming that "char *" implicitly
2657 * promotes to a "void *", or to explicitly promoting/demoting
2658 * consts to non/vice versa. The "const void *" is the SUS
2659 * definition, but that does not fly everywhere for the above
2661 SETSOCKOPT_OPTION_VALUE_T buf;
2665 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2669 aint = (int)SvIV(sv);
2670 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2673 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2683 SETERRNO(EBADF,SS_IVCHAN);
2692 const int optype = PL_op->op_type;
2693 GV * const gv = MUTABLE_GV(POPs);
2694 register IO * const io = GvIOn(gv);
2699 if (!io || !IoIFP(io))
2702 sv = sv_2mortal(newSV(257));
2703 (void)SvPOK_only(sv);
2707 fd = PerlIO_fileno(IoIFP(io));
2709 case OP_GETSOCKNAME:
2710 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2713 case OP_GETPEERNAME:
2714 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2716 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2718 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";
2719 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2720 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2721 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2722 sizeof(u_short) + sizeof(struct in_addr))) {
2729 #ifdef BOGUS_GETNAME_RETURN
2730 /* Interactive Unix, getpeername() and getsockname()
2731 does not return valid namelen */
2732 if (len == BOGUS_GETNAME_RETURN)
2733 len = sizeof(struct sockaddr);
2742 SETERRNO(EBADF,SS_IVCHAN);
2760 if (PL_op->op_flags & OPf_REF) {
2762 if (PL_op->op_type == OP_LSTAT) {
2763 if (gv != PL_defgv) {
2764 do_fstat_warning_check:
2765 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2766 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2767 } else if (PL_laststype != OP_LSTAT)
2768 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2772 if (gv != PL_defgv) {
2773 PL_laststype = OP_STAT;
2775 sv_setpvs(PL_statname, "");
2782 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2783 } else if (IoDIRP(io)) {
2785 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2787 PL_laststatval = -1;
2793 if (PL_laststatval < 0) {
2799 SV* const sv = POPs;
2800 if (isGV_with_GP(sv)) {
2801 gv = MUTABLE_GV(sv);
2803 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2804 gv = MUTABLE_GV(SvRV(sv));
2805 if (PL_op->op_type == OP_LSTAT)
2806 goto do_fstat_warning_check;
2808 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2809 io = MUTABLE_IO(SvRV(sv));
2810 if (PL_op->op_type == OP_LSTAT)
2811 goto do_fstat_warning_check;
2812 goto do_fstat_have_io;
2815 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2817 PL_laststype = PL_op->op_type;
2818 if (PL_op->op_type == OP_LSTAT)
2819 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2821 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2822 if (PL_laststatval < 0) {
2823 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2824 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2830 if (gimme != G_ARRAY) {
2831 if (gimme != G_VOID)
2832 XPUSHs(boolSV(max));
2838 mPUSHi(PL_statcache.st_dev);
2839 mPUSHi(PL_statcache.st_ino);
2840 mPUSHu(PL_statcache.st_mode);
2841 mPUSHu(PL_statcache.st_nlink);
2842 #if Uid_t_size > IVSIZE
2843 mPUSHn(PL_statcache.st_uid);
2845 # if Uid_t_sign <= 0
2846 mPUSHi(PL_statcache.st_uid);
2848 mPUSHu(PL_statcache.st_uid);
2851 #if Gid_t_size > IVSIZE
2852 mPUSHn(PL_statcache.st_gid);
2854 # if Gid_t_sign <= 0
2855 mPUSHi(PL_statcache.st_gid);
2857 mPUSHu(PL_statcache.st_gid);
2860 #ifdef USE_STAT_RDEV
2861 mPUSHi(PL_statcache.st_rdev);
2863 PUSHs(newSVpvs_flags("", SVs_TEMP));
2865 #if Off_t_size > IVSIZE
2866 mPUSHn(PL_statcache.st_size);
2868 mPUSHi(PL_statcache.st_size);
2871 mPUSHn(PL_statcache.st_atime);
2872 mPUSHn(PL_statcache.st_mtime);
2873 mPUSHn(PL_statcache.st_ctime);
2875 mPUSHi(PL_statcache.st_atime);
2876 mPUSHi(PL_statcache.st_mtime);
2877 mPUSHi(PL_statcache.st_ctime);
2879 #ifdef USE_STAT_BLOCKS
2880 mPUSHu(PL_statcache.st_blksize);
2881 mPUSHu(PL_statcache.st_blocks);
2883 PUSHs(newSVpvs_flags("", SVs_TEMP));
2884 PUSHs(newSVpvs_flags("", SVs_TEMP));
2890 #define tryAMAGICftest_MG(chr) STMT_START { \
2891 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2892 && S_try_amagic_ftest(aTHX_ chr)) \
2897 S_try_amagic_ftest(pTHX_ char chr) {
2900 SV* const arg = TOPs;
2905 if ((PL_op->op_flags & OPf_KIDS)
2908 const char tmpchr = chr;
2910 SV * const tmpsv = amagic_call(arg,
2911 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2912 ftest_amg, AMGf_unary);
2919 next = PL_op->op_next;
2920 if (next->op_type >= OP_FTRREAD &&
2921 next->op_type <= OP_FTBINARY &&
2922 next->op_private & OPpFT_STACKED
2925 /* leave the object alone */
2937 /* This macro is used by the stacked filetest operators :
2938 * if the previous filetest failed, short-circuit and pass its value.
2939 * Else, discard it from the stack and continue. --rgs
2941 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2942 if (!SvTRUE(TOPs)) { RETURN; } \
2943 else { (void)POPs; PUTBACK; } \
2950 /* Not const, because things tweak this below. Not bool, because there's
2951 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2952 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2953 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2954 /* Giving some sort of initial value silences compilers. */
2956 int access_mode = R_OK;
2958 int access_mode = 0;
2961 /* access_mode is never used, but leaving use_access in makes the
2962 conditional compiling below much clearer. */
2965 Mode_t stat_mode = S_IRUSR;
2967 bool effective = FALSE;
2971 switch (PL_op->op_type) {
2972 case OP_FTRREAD: opchar = 'R'; break;
2973 case OP_FTRWRITE: opchar = 'W'; break;
2974 case OP_FTREXEC: opchar = 'X'; break;
2975 case OP_FTEREAD: opchar = 'r'; break;
2976 case OP_FTEWRITE: opchar = 'w'; break;
2977 case OP_FTEEXEC: opchar = 'x'; break;
2979 tryAMAGICftest_MG(opchar);
2981 STACKED_FTEST_CHECK;
2983 switch (PL_op->op_type) {
2985 #if !(defined(HAS_ACCESS) && defined(R_OK))
2991 #if defined(HAS_ACCESS) && defined(W_OK)
2996 stat_mode = S_IWUSR;
3000 #if defined(HAS_ACCESS) && defined(X_OK)
3005 stat_mode = S_IXUSR;
3009 #ifdef PERL_EFF_ACCESS
3012 stat_mode = S_IWUSR;
3016 #ifndef PERL_EFF_ACCESS
3023 #ifdef PERL_EFF_ACCESS
3028 stat_mode = S_IXUSR;
3034 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3035 const char *name = POPpx;
3037 # ifdef PERL_EFF_ACCESS
3038 result = PERL_EFF_ACCESS(name, access_mode);
3040 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3046 result = access(name, access_mode);
3048 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3059 result = my_stat_flags(0);
3063 if (cando(stat_mode, effective, &PL_statcache))
3072 const int op_type = PL_op->op_type;
3077 case OP_FTIS: opchar = 'e'; break;
3078 case OP_FTSIZE: opchar = 's'; break;
3079 case OP_FTMTIME: opchar = 'M'; break;
3080 case OP_FTCTIME: opchar = 'C'; break;
3081 case OP_FTATIME: opchar = 'A'; break;
3083 tryAMAGICftest_MG(opchar);
3085 STACKED_FTEST_CHECK;
3087 result = my_stat_flags(0);
3091 if (op_type == OP_FTIS)
3094 /* You can't dTARGET inside OP_FTIS, because you'll get
3095 "panic: pad_sv po" - the op is not flagged to have a target. */
3099 #if Off_t_size > IVSIZE
3100 PUSHn(PL_statcache.st_size);
3102 PUSHi(PL_statcache.st_size);
3106 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3109 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3112 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3126 switch (PL_op->op_type) {
3127 case OP_FTROWNED: opchar = 'O'; break;
3128 case OP_FTEOWNED: opchar = 'o'; break;
3129 case OP_FTZERO: opchar = 'z'; break;
3130 case OP_FTSOCK: opchar = 'S'; break;
3131 case OP_FTCHR: opchar = 'c'; break;
3132 case OP_FTBLK: opchar = 'b'; break;
3133 case OP_FTFILE: opchar = 'f'; break;
3134 case OP_FTDIR: opchar = 'd'; break;
3135 case OP_FTPIPE: opchar = 'p'; break;
3136 case OP_FTSUID: opchar = 'u'; break;
3137 case OP_FTSGID: opchar = 'g'; break;
3138 case OP_FTSVTX: opchar = 'k'; break;
3140 tryAMAGICftest_MG(opchar);
3142 STACKED_FTEST_CHECK;
3144 /* I believe that all these three are likely to be defined on most every
3145 system these days. */
3147 if(PL_op->op_type == OP_FTSUID) {
3148 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3154 if(PL_op->op_type == OP_FTSGID) {
3155 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3161 if(PL_op->op_type == OP_FTSVTX) {
3162 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3168 result = my_stat_flags(0);
3172 switch (PL_op->op_type) {
3174 if (PL_statcache.st_uid == PL_uid)
3178 if (PL_statcache.st_uid == PL_euid)
3182 if (PL_statcache.st_size == 0)
3186 if (S_ISSOCK(PL_statcache.st_mode))
3190 if (S_ISCHR(PL_statcache.st_mode))
3194 if (S_ISBLK(PL_statcache.st_mode))
3198 if (S_ISREG(PL_statcache.st_mode))
3202 if (S_ISDIR(PL_statcache.st_mode))
3206 if (S_ISFIFO(PL_statcache.st_mode))
3211 if (PL_statcache.st_mode & S_ISUID)
3217 if (PL_statcache.st_mode & S_ISGID)
3223 if (PL_statcache.st_mode & S_ISVTX)
3237 tryAMAGICftest_MG('l');
3238 result = my_lstat_flags(0);
3243 if (S_ISLNK(PL_statcache.st_mode))
3258 tryAMAGICftest_MG('t');
3260 STACKED_FTEST_CHECK;
3262 if (PL_op->op_flags & OPf_REF)
3264 else if (isGV_with_GP(TOPs))
3265 gv = MUTABLE_GV(POPs);
3266 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3267 gv = MUTABLE_GV(SvRV(POPs));
3270 name = SvPV_nomg(tmpsv, namelen);
3271 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3274 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3275 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3276 else if (tmpsv && SvOK(tmpsv)) {
3284 if (PerlLIO_isatty(fd))
3289 #if defined(atarist) /* this will work with atariST. Configure will
3290 make guesses for other systems. */
3291 # define FILE_base(f) ((f)->_base)
3292 # define FILE_ptr(f) ((f)->_ptr)
3293 # define FILE_cnt(f) ((f)->_cnt)
3294 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3305 register STDCHAR *s;
3311 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3313 STACKED_FTEST_CHECK;
3315 if (PL_op->op_flags & OPf_REF)
3317 else if (isGV_with_GP(TOPs))
3318 gv = MUTABLE_GV(POPs);
3319 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3320 gv = MUTABLE_GV(SvRV(POPs));
3326 if (gv == PL_defgv) {
3328 io = GvIO(PL_statgv);
3331 goto really_filename;
3336 PL_laststatval = -1;
3337 sv_setpvs(PL_statname, "");
3338 io = GvIO(PL_statgv);
3340 if (io && IoIFP(io)) {
3341 if (! PerlIO_has_base(IoIFP(io)))
3342 DIE(aTHX_ "-T and -B not implemented on filehandles");
3343 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3344 if (PL_laststatval < 0)
3346 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3347 if (PL_op->op_type == OP_FTTEXT)
3352 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3353 i = PerlIO_getc(IoIFP(io));
3355 (void)PerlIO_ungetc(IoIFP(io),i);
3357 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3359 len = PerlIO_get_bufsiz(IoIFP(io));
3360 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3361 /* sfio can have large buffers - limit to 512 */
3366 report_evil_fh(cGVOP_gv);
3367 SETERRNO(EBADF,RMS_IFI);
3375 PL_laststype = OP_STAT;
3376 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3377 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3378 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3380 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3383 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3384 if (PL_laststatval < 0) {
3385 (void)PerlIO_close(fp);
3388 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3389 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3390 (void)PerlIO_close(fp);
3392 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3393 RETPUSHNO; /* special case NFS directories */
3394 RETPUSHYES; /* null file is anything */
3399 /* now scan s to look for textiness */
3400 /* XXX ASCII dependent code */
3402 #if defined(DOSISH) || defined(USEMYBINMODE)
3403 /* ignore trailing ^Z on short files */
3404 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3408 for (i = 0; i < len; i++, s++) {
3409 if (!*s) { /* null never allowed in text */
3414 else if (!(isPRINT(*s) || isSPACE(*s)))
3417 else if (*s & 128) {
3419 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3422 /* utf8 characters don't count as odd */
3423 if (UTF8_IS_START(*s)) {
3424 int ulen = UTF8SKIP(s);
3425 if (ulen < len - i) {
3427 for (j = 1; j < ulen; j++) {
3428 if (!UTF8_IS_CONTINUATION(s[j]))
3431 --ulen; /* loop does extra increment */
3441 *s != '\n' && *s != '\r' && *s != '\b' &&
3442 *s != '\t' && *s != '\f' && *s != 27)
3447 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3458 const char *tmps = NULL;
3462 SV * const sv = POPs;
3463 if (PL_op->op_flags & OPf_SPECIAL) {
3464 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3466 else if (isGV_with_GP(sv)) {
3467 gv = MUTABLE_GV(sv);
3469 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3470 gv = MUTABLE_GV(SvRV(sv));
3473 tmps = SvPV_nolen_const(sv);
3477 if( !gv && (!tmps || !*tmps) ) {
3478 HV * const table = GvHVn(PL_envgv);
3481 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3482 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3484 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3489 deprecate("chdir('') or chdir(undef) as chdir()");
3490 tmps = SvPV_nolen_const(*svp);
3494 TAINT_PROPER("chdir");
3499 TAINT_PROPER("chdir");
3502 IO* const io = GvIO(gv);
3505 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3506 } else if (IoIFP(io)) {
3507 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3511 SETERRNO(EBADF, RMS_IFI);
3517 SETERRNO(EBADF,RMS_IFI);
3521 DIE(aTHX_ PL_no_func, "fchdir");
3525 PUSHi( PerlDir_chdir(tmps) >= 0 );
3527 /* Clear the DEFAULT element of ENV so we'll get the new value
3529 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3536 dVAR; dSP; dMARK; dTARGET;
3537 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3548 char * const tmps = POPpx;
3549 TAINT_PROPER("chroot");
3550 PUSHi( chroot(tmps) >= 0 );
3553 DIE(aTHX_ PL_no_func, "chroot");
3561 const char * const tmps2 = POPpconstx;
3562 const char * const tmps = SvPV_nolen_const(TOPs);
3563 TAINT_PROPER("rename");
3565 anum = PerlLIO_rename(tmps, tmps2);
3567 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3568 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3571 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3572 (void)UNLINK(tmps2);
3573 if (!(anum = link(tmps, tmps2)))
3574 anum = UNLINK(tmps);
3582 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3586 const int op_type = PL_op->op_type;
3590 if (op_type == OP_LINK)
3591 DIE(aTHX_ PL_no_func, "link");
3593 # ifndef HAS_SYMLINK
3594 if (op_type == OP_SYMLINK)
3595 DIE(aTHX_ PL_no_func, "symlink");
3599 const char * const tmps2 = POPpconstx;
3600 const char * const tmps = SvPV_nolen_const(TOPs);
3601 TAINT_PROPER(PL_op_desc[op_type]);
3603 # if defined(HAS_LINK)
3604 # if defined(HAS_SYMLINK)
3605 /* Both present - need to choose which. */
3606 (op_type == OP_LINK) ?
3607 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3609 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3610 PerlLIO_link(tmps, tmps2);
3613 # if defined(HAS_SYMLINK)
3614 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3615 symlink(tmps, tmps2);
3620 SETi( result >= 0 );
3627 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3638 char buf[MAXPATHLEN];
3641 #ifndef INCOMPLETE_TAINTS
3645 len = readlink(tmps, buf, sizeof(buf) - 1);
3652 RETSETUNDEF; /* just pretend it's a normal file */
3656 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3658 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3660 char * const save_filename = filename;
3665 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3667 PERL_ARGS_ASSERT_DOONELINER;
3669 Newx(cmdline, size, char);
3670 my_strlcpy(cmdline, cmd, size);
3671 my_strlcat(cmdline, " ", size);
3672 for (s = cmdline + strlen(cmdline); *filename; ) {
3676 if (s - cmdline < size)
3677 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3678 myfp = PerlProc_popen(cmdline, "r");
3682 SV * const tmpsv = sv_newmortal();
3683 /* Need to save/restore 'PL_rs' ?? */
3684 s = sv_gets(tmpsv, myfp, 0);
3685 (void)PerlProc_pclose(myfp);
3689 #ifdef HAS_SYS_ERRLIST
3694 /* you don't see this */
3695 const char * const errmsg =
3696 #ifdef HAS_SYS_ERRLIST
3704 if (instr(s, errmsg)) {
3711 #define EACCES EPERM
3713 if (instr(s, "cannot make"))
3714 SETERRNO(EEXIST,RMS_FEX);
3715 else if (instr(s, "existing file"))
3716 SETERRNO(EEXIST,RMS_FEX);
3717 else if (instr(s, "ile exists"))
3718 SETERRNO(EEXIST,RMS_FEX);
3719 else if (instr(s, "non-exist"))
3720 SETERRNO(ENOENT,RMS_FNF);
3721 else if (instr(s, "does not exist"))
3722 SETERRNO(ENOENT,RMS_FNF);
3723 else if (instr(s, "not empty"))
3724 SETERRNO(EBUSY,SS_DEVOFFLINE);
3725 else if (instr(s, "cannot access"))
3726 SETERRNO(EACCES,RMS_PRV);
3728 SETERRNO(EPERM,RMS_PRV);
3731 else { /* some mkdirs return no failure indication */
3732 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3733 if (PL_op->op_type == OP_RMDIR)
3738 SETERRNO(EACCES,RMS_PRV); /* a guess */
3747 /* This macro removes trailing slashes from a directory name.
3748 * Different operating and file systems take differently to
3749 * trailing slashes. According to POSIX 1003.1 1996 Edition
3750 * any number of trailing slashes should be allowed.
3751 * Thusly we snip them away so that even non-conforming
3752 * systems are happy.
3753 * We should probably do this "filtering" for all
3754 * the functions that expect (potentially) directory names:
3755 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3756 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3758 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3759 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3762 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3763 (tmps) = savepvn((tmps), (len)); \
3773 const int mode = (MAXARG > 1) ? POPi : 0777;
3775 TRIMSLASHES(tmps,len,copy);
3777 TAINT_PROPER("mkdir");
3779 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3783 SETi( dooneliner("mkdir", tmps) );
3784 oldumask = PerlLIO_umask(0);
3785 PerlLIO_umask(oldumask);
3786 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3801 TRIMSLASHES(tmps,len,copy);
3802 TAINT_PROPER("rmdir");
3804 SETi( PerlDir_rmdir(tmps) >= 0 );
3806 SETi( dooneliner("rmdir", tmps) );
3813 /* Directory calls. */
3817 #if defined(Direntry_t) && defined(HAS_READDIR)
3819 const char * const dirname = POPpconstx;
3820 GV * const gv = MUTABLE_GV(POPs);
3821 register IO * const io = GvIOn(gv);
3826 if ((IoIFP(io) || IoOFP(io)))
3827 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3828 "Opening filehandle %s also as a directory",
3831 PerlDir_close(IoDIRP(io));
3832 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3838 SETERRNO(EBADF,RMS_DIR);
3841 DIE(aTHX_ PL_no_dir_func, "opendir");
3847 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3848 DIE(aTHX_ PL_no_dir_func, "readdir");
3850 #if !defined(I_DIRENT) && !defined(VMS)
3851 Direntry_t *readdir (DIR *);
3857 const I32 gimme = GIMME;
3858 GV * const gv = MUTABLE_GV(POPs);
3859 register const Direntry_t *dp;
3860 register IO * const io = GvIOn(gv);
3862 if (!io || !IoDIRP(io)) {
3863 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3864 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3869 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3873 sv = newSVpvn(dp->d_name, dp->d_namlen);
3875 sv = newSVpv(dp->d_name, 0);
3877 #ifndef INCOMPLETE_TAINTS
3878 if (!(IoFLAGS(io) & IOf_UNTAINT))
3882 } while (gimme == G_ARRAY);
3884 if (!dp && gimme != G_ARRAY)
3891 SETERRNO(EBADF,RMS_ISI);
3892 if (GIMME == G_ARRAY)
3901 #if defined(HAS_TELLDIR) || defined(telldir)
3903 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3904 /* XXX netbsd still seemed to.
3905 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3906 --JHI 1999-Feb-02 */
3907 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3908 long telldir (DIR *);
3910 GV * const gv = MUTABLE_GV(POPs);
3911 register IO * const io = GvIOn(gv);
3913 if (!io || !IoDIRP(io)) {
3914 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3915 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3919 PUSHi( PerlDir_tell(IoDIRP(io)) );
3923 SETERRNO(EBADF,RMS_ISI);
3926 DIE(aTHX_ PL_no_dir_func, "telldir");
3932 #if defined(HAS_SEEKDIR) || defined(seekdir)
3934 const long along = POPl;
3935 GV * const gv = MUTABLE_GV(POPs);
3936 register IO * const io = GvIOn(gv);
3938 if (!io || !IoDIRP(io)) {
3939 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3940 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3943 (void)PerlDir_seek(IoDIRP(io), along);
3948 SETERRNO(EBADF,RMS_ISI);
3951 DIE(aTHX_ PL_no_dir_func, "seekdir");
3957 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3959 GV * const gv = MUTABLE_GV(POPs);
3960 register IO * const io = GvIOn(gv);
3962 if (!io || !IoDIRP(io)) {
3963 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3964 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3967 (void)PerlDir_rewind(IoDIRP(io));
3971 SETERRNO(EBADF,RMS_ISI);
3974 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3980 #if defined(Direntry_t) && defined(HAS_READDIR)
3982 GV * const gv = MUTABLE_GV(POPs);
3983 register IO * const io = GvIOn(gv);
3985 if (!io || !IoDIRP(io)) {
3986 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3987 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3990 #ifdef VOID_CLOSEDIR
3991 PerlDir_close(IoDIRP(io));
3993 if (PerlDir_close(IoDIRP(io)) < 0) {
3994 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4003 SETERRNO(EBADF,RMS_IFI);
4006 DIE(aTHX_ PL_no_dir_func, "closedir");
4010 /* Process control. */
4019 PERL_FLUSHALL_FOR_CHILD;
4020 childpid = PerlProc_fork();
4024 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4026 SvREADONLY_off(GvSV(tmpgv));
4027 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4028 SvREADONLY_on(GvSV(tmpgv));
4030 #ifdef THREADS_HAVE_PIDS
4031 PL_ppid = (IV)getppid();
4033 #ifdef PERL_USES_PL_PIDSTATUS
4034 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4040 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4045 PERL_FLUSHALL_FOR_CHILD;
4046 childpid = PerlProc_fork();
4052 DIE(aTHX_ PL_no_func, "fork");
4059 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4064 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4065 childpid = wait4pid(-1, &argflags, 0);
4067 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4072 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4073 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4074 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4076 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4081 DIE(aTHX_ PL_no_func, "wait");
4087 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4089 const int optype = POPi;
4090 const Pid_t pid = TOPi;
4094 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4095 result = wait4pid(pid, &argflags, optype);
4097 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4102 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4103 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4104 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4106 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4111 DIE(aTHX_ PL_no_func, "waitpid");
4117 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4118 #if defined(__LIBCATAMOUNT__)
4119 PL_statusvalue = -1;
4128 while (++MARK <= SP) {
4129 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4134 TAINT_PROPER("system");
4136 PERL_FLUSHALL_FOR_CHILD;
4137 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4143 if (PerlProc_pipe(pp) >= 0)
4145 while ((childpid = PerlProc_fork()) == -1) {
4146 if (errno != EAGAIN) {
4151 PerlLIO_close(pp[0]);
4152 PerlLIO_close(pp[1]);
4159 Sigsave_t ihand,qhand; /* place to save signals during system() */
4163 PerlLIO_close(pp[1]);
4165 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4166 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4169 result = wait4pid(childpid, &status, 0);
4170 } while (result == -1 && errno == EINTR);
4172 (void)rsignal_restore(SIGINT, &ihand);
4173 (void)rsignal_restore(SIGQUIT, &qhand);
4175 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4176 do_execfree(); /* free any memory child malloced on fork */
4183 while (n < sizeof(int)) {
4184 n1 = PerlLIO_read(pp[0],
4185 (void*)(((char*)&errkid)+n),
4191 PerlLIO_close(pp[0]);
4192 if (n) { /* Error */
4193 if (n != sizeof(int))
4194 DIE(aTHX_ "panic: kid popen errno read");
4195 errno = errkid; /* Propagate errno from kid */
4196 STATUS_NATIVE_CHILD_SET(-1);
4199 XPUSHi(STATUS_CURRENT);
4203 PerlLIO_close(pp[0]);
4204 #if defined(HAS_FCNTL) && defined(F_SETFD)
4205 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4208 if (PL_op->op_flags & OPf_STACKED) {
4209 SV * const really = *++MARK;
4210 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4212 else if (SP - MARK != 1)
4213 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4215 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4219 #else /* ! FORK or VMS or OS/2 */
4222 if (PL_op->op_flags & OPf_STACKED) {
4223 SV * const really = *++MARK;
4224 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4225 value = (I32)do_aspawn(really, MARK, SP);
4227 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4230 else if (SP - MARK != 1) {
4231 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4232 value = (I32)do_aspawn(NULL, MARK, SP);
4234 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4238 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4240 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4242 STATUS_NATIVE_CHILD_SET(value);
4245 XPUSHi(result ? value : STATUS_CURRENT);
4246 #endif /* !FORK or VMS or OS/2 */
4253 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4258 while (++MARK <= SP) {
4259 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4264 TAINT_PROPER("exec");
4266 PERL_FLUSHALL_FOR_CHILD;
4267 if (PL_op->op_flags & OPf_STACKED) {
4268 SV * const really = *++MARK;
4269 value = (I32)do_aexec(really, MARK, SP);
4271 else if (SP - MARK != 1)
4273 value = (I32)vms_do_aexec(NULL, MARK, SP);
4277 (void ) do_aspawn(NULL, MARK, SP);
4281 value = (I32)do_aexec(NULL, MARK, SP);
4286 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4289 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4292 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4306 # ifdef THREADS_HAVE_PIDS
4307 if (PL_ppid != 1 && getppid() == 1)
4308 /* maybe the parent process has died. Refresh ppid cache */
4312 XPUSHi( getppid() );
4316 DIE(aTHX_ PL_no_func, "getppid");
4325 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4328 pgrp = (I32)BSD_GETPGRP(pid);
4330 if (pid != 0 && pid != PerlProc_getpid())
4331 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4337 DIE(aTHX_ PL_no_func, "getpgrp()");
4357 TAINT_PROPER("setpgrp");
4359 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4361 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4362 || (pid != 0 && pid != PerlProc_getpid()))
4364 DIE(aTHX_ "setpgrp can't take arguments");
4366 SETi( setpgrp() >= 0 );
4367 #endif /* USE_BSDPGRP */
4370 DIE(aTHX_ PL_no_func, "setpgrp()");
4374 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4375 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4377 # define PRIORITY_WHICH_T(which) which
4382 #ifdef HAS_GETPRIORITY
4384 const int who = POPi;
4385 const int which = TOPi;
4386 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4389 DIE(aTHX_ PL_no_func, "getpriority()");
4395 #ifdef HAS_SETPRIORITY
4397 const int niceval = POPi;
4398 const int who = POPi;
4399 const int which = TOPi;
4400 TAINT_PROPER("setpriority");
4401 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4404 DIE(aTHX_ PL_no_func, "setpriority()");
4408 #undef PRIORITY_WHICH_T
4416 XPUSHn( time(NULL) );
4418 XPUSHi( time(NULL) );
4430 (void)PerlProc_times(&PL_timesbuf);
4432 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4433 /* struct tms, though same data */
4437 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4438 if (GIMME == G_ARRAY) {
4439 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4440 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4441 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4449 if (GIMME == G_ARRAY) {
4456 DIE(aTHX_ "times not implemented");
4458 #endif /* HAS_TIMES */
4461 /* The 32 bit int year limits the times we can represent to these
4462 boundaries with a few days wiggle room to account for time zone
4465 /* Sat Jan 3 00:00:00 -2147481748 */
4466 #define TIME_LOWER_BOUND -67768100567755200.0
4467 /* Sun Dec 29 12:00:00 2147483647 */
4468 #define TIME_UPPER_BOUND 67767976233316800.0
4477 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4478 static const char * const dayname[] =
4479 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4480 static const char * const monname[] =
4481 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4482 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4487 when = (Time64_T)now;
4490 NV input = Perl_floor(POPn);
4491 when = (Time64_T)input;
4492 if (when != input) {
4493 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4494 "%s(%.0" NVff ") too large", opname, input);
4498 if ( TIME_LOWER_BOUND > when ) {
4499 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4500 "%s(%.0" NVff ") too small", opname, when);
4503 else if( when > TIME_UPPER_BOUND ) {
4504 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4505 "%s(%.0" NVff ") too large", opname, when);
4509 if (PL_op->op_type == OP_LOCALTIME)
4510 err = S_localtime64_r(&when, &tmbuf);
4512 err = S_gmtime64_r(&when, &tmbuf);
4516 /* XXX %lld broken for quads */
4517 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4518 "%s(%.0" NVff ") failed", opname, when);
4521 if (GIMME != G_ARRAY) { /* scalar context */
4523 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4524 double year = (double)tmbuf.tm_year + 1900;
4531 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4532 dayname[tmbuf.tm_wday],
4533 monname[tmbuf.tm_mon],
4541 else { /* list context */
4547 mPUSHi(tmbuf.tm_sec);
4548 mPUSHi(tmbuf.tm_min);
4549 mPUSHi(tmbuf.tm_hour);
4550 mPUSHi(tmbuf.tm_mday);
4551 mPUSHi(tmbuf.tm_mon);
4552 mPUSHn(tmbuf.tm_year);
4553 mPUSHi(tmbuf.tm_wday);
4554 mPUSHi(tmbuf.tm_yday);
4555 mPUSHi(tmbuf.tm_isdst);
4566 anum = alarm((unsigned int)anum);
4572 DIE(aTHX_ PL_no_func, "alarm");
4583 (void)time(&lasttime);
4588 PerlProc_sleep((unsigned int)duration);
4591 XPUSHi(when - lasttime);
4595 /* Shared memory. */
4596 /* Merged with some message passing. */
4600 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4601 dVAR; dSP; dMARK; dTARGET;
4602 const int op_type = PL_op->op_type;
4607 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4610 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4613 value = (I32)(do_semop(MARK, SP) >= 0);
4616 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4624 return Perl_pp_semget(aTHX);
4632 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4633 dVAR; dSP; dMARK; dTARGET;
4634 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4641 DIE(aTHX_ "System V IPC is not implemented on this machine");
4647 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4648 dVAR; dSP; dMARK; dTARGET;
4649 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4657 PUSHp(zero_but_true, ZBTLEN);
4661 return Perl_pp_semget(aTHX);
4665 /* I can't const this further without getting warnings about the types of
4666 various arrays passed in from structures. */
4668 S_space_join_names_mortal(pTHX_ char *const *array)
4672 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4674 if (array && *array) {
4675 target = newSVpvs_flags("", SVs_TEMP);
4677 sv_catpv(target, *array);
4680 sv_catpvs(target, " ");
4683 target = sv_mortalcopy(&PL_sv_no);
4688 /* Get system info. */
4692 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4694 I32 which = PL_op->op_type;
4695 register char **elem;
4697 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4698 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4699 struct hostent *gethostbyname(Netdb_name_t);
4700 struct hostent *gethostent(void);
4702 struct hostent *hent = NULL;
4706 if (which == OP_GHBYNAME) {
4707 #ifdef HAS_GETHOSTBYNAME
4708 const char* const name = POPpbytex;
4709 hent = PerlSock_gethostbyname(name);
4711 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4714 else if (which == OP_GHBYADDR) {
4715 #ifdef HAS_GETHOSTBYADDR
4716 const int addrtype = POPi;
4717 SV * const addrsv = POPs;
4719 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4721 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4723 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4727 #ifdef HAS_GETHOSTENT
4728 hent = PerlSock_gethostent();
4730 DIE(aTHX_ PL_no_sock_func, "gethostent");
4733 #ifdef HOST_NOT_FOUND
4735 #ifdef USE_REENTRANT_API
4736 # ifdef USE_GETHOSTENT_ERRNO
4737 h_errno = PL_reentrant_buffer->_gethostent_errno;
4740 STATUS_UNIX_SET(h_errno);
4744 if (GIMME != G_ARRAY) {
4745 PUSHs(sv = sv_newmortal());
4747 if (which == OP_GHBYNAME) {
4749 sv_setpvn(sv, hent->h_addr, hent->h_length);
4752 sv_setpv(sv, (char*)hent->h_name);
4758 mPUSHs(newSVpv((char*)hent->h_name, 0));
4759 PUSHs(space_join_names_mortal(hent->h_aliases));
4760 mPUSHi(hent->h_addrtype);
4761 len = hent->h_length;
4764 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4765 mXPUSHp(*elem, len);
4769 mPUSHp(hent->h_addr, len);
4771 PUSHs(sv_mortalcopy(&PL_sv_no));
4776 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4782 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4784 I32 which = PL_op->op_type;
4786 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4787 struct netent *getnetbyaddr(Netdb_net_t, int);
4788 struct netent *getnetbyname(Netdb_name_t);
4789 struct netent *getnetent(void);
4791 struct netent *nent;
4793 if (which == OP_GNBYNAME){
4794 #ifdef HAS_GETNETBYNAME
4795 const char * const name = POPpbytex;
4796 nent = PerlSock_getnetbyname(name);
4798 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4801 else if (which == OP_GNBYADDR) {
4802 #ifdef HAS_GETNETBYADDR
4803 const int addrtype = POPi;
4804 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4805 nent = PerlSock_getnetbyaddr(addr, addrtype);
4807 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4811 #ifdef HAS_GETNETENT
4812 nent = PerlSock_getnetent();
4814 DIE(aTHX_ PL_no_sock_func, "getnetent");
4817 #ifdef HOST_NOT_FOUND
4819 #ifdef USE_REENTRANT_API
4820 # ifdef USE_GETNETENT_ERRNO
4821 h_errno = PL_reentrant_buffer->_getnetent_errno;
4824 STATUS_UNIX_SET(h_errno);
4829 if (GIMME != G_ARRAY) {
4830 PUSHs(sv = sv_newmortal());
4832 if (which == OP_GNBYNAME)
4833 sv_setiv(sv, (IV)nent->n_net);
4835 sv_setpv(sv, nent->n_name);
4841 mPUSHs(newSVpv(nent->n_name, 0));
4842 PUSHs(space_join_names_mortal(nent->n_aliases));
4843 mPUSHi(nent->n_addrtype);
4844 mPUSHi(nent->n_net);
4849 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4855 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4857 I32 which = PL_op->op_type;
4859 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4860 struct protoent *getprotobyname(Netdb_name_t);
4861 struct protoent *getprotobynumber(int);
4862 struct protoent *getprotoent(void);
4864 struct protoent *pent;
4866 if (which == OP_GPBYNAME) {
4867 #ifdef HAS_GETPROTOBYNAME
4868 const char* const name = POPpbytex;
4869 pent = PerlSock_getprotobyname(name);
4871 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4874 else if (which == OP_GPBYNUMBER) {
4875 #ifdef HAS_GETPROTOBYNUMBER
4876 const int number = POPi;
4877 pent = PerlSock_getprotobynumber(number);
4879 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4883 #ifdef HAS_GETPROTOENT
4884 pent = PerlSock_getprotoent();
4886 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4890 if (GIMME != G_ARRAY) {
4891 PUSHs(sv = sv_newmortal());
4893 if (which == OP_GPBYNAME)
4894 sv_setiv(sv, (IV)pent->p_proto);
4896 sv_setpv(sv, pent->p_name);
4902 mPUSHs(newSVpv(pent->p_name, 0));
4903 PUSHs(space_join_names_mortal(pent->p_aliases));
4904 mPUSHi(pent->p_proto);
4909 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4915 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4917 I32 which = PL_op->op_type;
4919 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4920 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4921 struct servent *getservbyport(int, Netdb_name_t);
4922 struct servent *getservent(void);
4924 struct servent *sent;
4926 if (which == OP_GSBYNAME) {
4927 #ifdef HAS_GETSERVBYNAME
4928 const char * const proto = POPpbytex;
4929 const char * const name = POPpbytex;
4930 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4932 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4935 else if (which == OP_GSBYPORT) {
4936 #ifdef HAS_GETSERVBYPORT
4937 const char * const proto = POPpbytex;
4938 unsigned short port = (unsigned short)POPu;
4940 port = PerlSock_htons(port);
4942 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4944 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4948 #ifdef HAS_GETSERVENT
4949 sent = PerlSock_getservent();
4951 DIE(aTHX_ PL_no_sock_func, "getservent");
4955 if (GIMME != G_ARRAY) {
4956 PUSHs(sv = sv_newmortal());
4958 if (which == OP_GSBYNAME) {
4960 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4962 sv_setiv(sv, (IV)(sent->s_port));
4966 sv_setpv(sv, sent->s_name);
4972 mPUSHs(newSVpv(sent->s_name, 0));
4973 PUSHs(space_join_names_mortal(sent->s_aliases));
4975 mPUSHi(PerlSock_ntohs(sent->s_port));
4977 mPUSHi(sent->s_port);
4979 mPUSHs(newSVpv(sent->s_proto, 0));
4984 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4991 const int stayopen = TOPi;
4992 switch(PL_op->op_type) {
4994 #ifdef HAS_SETHOSTENT
4995 PerlSock_sethostent(stayopen);
4997 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5000 #ifdef HAS_SETNETENT
5002 PerlSock_setnetent(stayopen);
5004 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5008 #ifdef HAS_SETPROTOENT
5009 PerlSock_setprotoent(stayopen);
5011 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5015 #ifdef HAS_SETSERVENT
5016 PerlSock_setservent(stayopen);
5018 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5028 switch(PL_op->op_type) {
5030 #ifdef HAS_ENDHOSTENT
5031 PerlSock_endhostent();
5033 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5037 #ifdef HAS_ENDNETENT
5038 PerlSock_endnetent();
5040 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5044 #ifdef HAS_ENDPROTOENT
5045 PerlSock_endprotoent();
5047 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5051 #ifdef HAS_ENDSERVENT
5052 PerlSock_endservent();
5054 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5058 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5061 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5065 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5068 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5072 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5075 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5079 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5082 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5094 I32 which = PL_op->op_type;
5096 struct passwd *pwent = NULL;
5098 * We currently support only the SysV getsp* shadow password interface.
5099 * The interface is declared in <shadow.h> and often one needs to link
5100 * with -lsecurity or some such.
5101 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5104 * AIX getpwnam() is clever enough to return the encrypted password
5105 * only if the caller (euid?) is root.
5107 * There are at least three other shadow password APIs. Many platforms
5108 * seem to contain more than one interface for accessing the shadow
5109 * password databases, possibly for compatibility reasons.
5110 * The getsp*() is by far he simplest one, the other two interfaces
5111 * are much more complicated, but also very similar to each other.
5116 * struct pr_passwd *getprpw*();
5117 * The password is in
5118 * char getprpw*(...).ufld.fd_encrypt[]
5119 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5124 * struct es_passwd *getespw*();
5125 * The password is in
5126 * char *(getespw*(...).ufld.fd_encrypt)
5127 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5130 * struct userpw *getuserpw();
5131 * The password is in
5132 * char *(getuserpw(...)).spw_upw_passwd
5133 * (but the de facto standard getpwnam() should work okay)
5135 * Mention I_PROT here so that Configure probes for it.
5137 * In HP-UX for getprpw*() the manual page claims that one should include
5138 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5139 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5140 * and pp_sys.c already includes <shadow.h> if there is such.
5142 * Note that <sys/security.h> is already probed for, but currently
5143 * it is only included in special cases.
5145 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5146 * be preferred interface, even though also the getprpw*() interface
5147 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5148 * One also needs to call set_auth_parameters() in main() before
5149 * doing anything else, whether one is using getespw*() or getprpw*().
5151 * Note that accessing the shadow databases can be magnitudes
5152 * slower than accessing the standard databases.
5157 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5158 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5159 * the pw_comment is left uninitialized. */
5160 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5166 const char* const name = POPpbytex;
5167 pwent = getpwnam(name);
5173 pwent = getpwuid(uid);
5177 # ifdef HAS_GETPWENT
5179 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5180 if (pwent) pwent = getpwnam(pwent->pw_name);
5183 DIE(aTHX_ PL_no_func, "getpwent");
5189 if (GIMME != G_ARRAY) {
5190 PUSHs(sv = sv_newmortal());
5192 if (which == OP_GPWNAM)
5193 # if Uid_t_sign <= 0
5194 sv_setiv(sv, (IV)pwent->pw_uid);
5196 sv_setuv(sv, (UV)pwent->pw_uid);
5199 sv_setpv(sv, pwent->pw_name);
5205 mPUSHs(newSVpv(pwent->pw_name, 0));
5209 /* If we have getspnam(), we try to dig up the shadow
5210 * password. If we are underprivileged, the shadow
5211 * interface will set the errno to EACCES or similar,
5212 * and return a null pointer. If this happens, we will
5213 * use the dummy password (usually "*" or "x") from the
5214 * standard password database.
5216 * In theory we could skip the shadow call completely
5217 * if euid != 0 but in practice we cannot know which
5218 * security measures are guarding the shadow databases
5219 * on a random platform.
5221 * Resist the urge to use additional shadow interfaces.
5222 * Divert the urge to writing an extension instead.
5225 /* Some AIX setups falsely(?) detect some getspnam(), which
5226 * has a different API than the Solaris/IRIX one. */
5227 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5230 const struct spwd * const spwent = getspnam(pwent->pw_name);
5231 /* Save and restore errno so that
5232 * underprivileged attempts seem
5233 * to have never made the unsuccessful
5234 * attempt to retrieve the shadow password. */
5236 if (spwent && spwent->sp_pwdp)
5237 sv_setpv(sv, spwent->sp_pwdp);
5241 if (!SvPOK(sv)) /* Use the standard password, then. */
5242 sv_setpv(sv, pwent->pw_passwd);
5245 # ifndef INCOMPLETE_TAINTS
5246 /* passwd is tainted because user himself can diddle with it.
5247 * admittedly not much and in a very limited way, but nevertheless. */
5251 # if Uid_t_sign <= 0
5252 mPUSHi(pwent->pw_uid);
5254 mPUSHu(pwent->pw_uid);
5257 # if Uid_t_sign <= 0
5258 mPUSHi(pwent->pw_gid);
5260 mPUSHu(pwent->pw_gid);
5262 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5263 * because of the poor interface of the Perl getpw*(),
5264 * not because there's some standard/convention saying so.
5265 * A better interface would have been to return a hash,
5266 * but we are accursed by our history, alas. --jhi. */
5268 mPUSHi(pwent->pw_change);
5271 mPUSHi(pwent->pw_quota);
5274 mPUSHs(newSVpv(pwent->pw_age, 0));
5276 /* I think that you can never get this compiled, but just in case. */
5277 PUSHs(sv_mortalcopy(&PL_sv_no));
5282 /* pw_class and pw_comment are mutually exclusive--.
5283 * see the above note for pw_change, pw_quota, and pw_age. */
5285 mPUSHs(newSVpv(pwent->pw_class, 0));
5288 mPUSHs(newSVpv(pwent->pw_comment, 0));
5290 /* I think that you can never get this compiled, but just in case. */
5291 PUSHs(sv_mortalcopy(&PL_sv_no));
5296 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5298 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5300 # ifndef INCOMPLETE_TAINTS
5301 /* pw_gecos is tainted because user himself can diddle with it. */
5305 mPUSHs(newSVpv(pwent->pw_dir, 0));
5307 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5308 # ifndef INCOMPLETE_TAINTS
5309 /* pw_shell is tainted because user himself can diddle with it. */
5314 mPUSHi(pwent->pw_expire);
5319 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5327 const I32 which = PL_op->op_type;
5328 const struct group *grent;
5330 if (which == OP_GGRNAM) {
5331 const char* const name = POPpbytex;
5332 grent = (const struct group *)getgrnam(name);
5334 else if (which == OP_GGRGID) {
5335 const Gid_t gid = POPi;
5336 grent = (const struct group *)getgrgid(gid);
5340 grent = (struct group *)getgrent();
5342 DIE(aTHX_ PL_no_func, "getgrent");
5346 if (GIMME != G_ARRAY) {
5347 SV * const sv = sv_newmortal();
5351 if (which == OP_GGRNAM)
5353 sv_setiv(sv, (IV)grent->gr_gid);
5355 sv_setuv(sv, (UV)grent->gr_gid);
5358 sv_setpv(sv, grent->gr_name);
5364 mPUSHs(newSVpv(grent->gr_name, 0));
5367 mPUSHs(newSVpv(grent->gr_passwd, 0));
5369 PUSHs(sv_mortalcopy(&PL_sv_no));
5373 mPUSHi(grent->gr_gid);
5375 mPUSHu(grent->gr_gid);
5378 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5379 /* In UNICOS/mk (_CRAYMPP) the multithreading
5380 * versions (getgrnam_r, getgrgid_r)
5381 * seem to return an illegal pointer
5382 * as the group members list, gr_mem.
5383 * getgrent() doesn't even have a _r version
5384 * but the gr_mem is poisonous anyway.
5385 * So yes, you cannot get the list of group
5386 * members if building multithreaded in UNICOS/mk. */
5387 PUSHs(space_join_names_mortal(grent->gr_mem));
5393 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5403 if (!(tmps = PerlProc_getlogin()))
5405 sv_setpv_mg(TARG, tmps);
5409 DIE(aTHX_ PL_no_func, "getlogin");
5413 /* Miscellaneous. */
5418 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5419 register I32 items = SP - MARK;
5420 unsigned long a[20];
5425 while (++MARK <= SP) {
5426 if (SvTAINTED(*MARK)) {
5432 TAINT_PROPER("syscall");
5435 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5436 * or where sizeof(long) != sizeof(char*). But such machines will
5437 * not likely have syscall implemented either, so who cares?
5439 while (++MARK <= SP) {
5440 if (SvNIOK(*MARK) || !i)
5441 a[i++] = SvIV(*MARK);
5442 else if (*MARK == &PL_sv_undef)
5445 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5451 DIE(aTHX_ "Too many args to syscall");
5453 DIE(aTHX_ "Too few args to syscall");
5455 retval = syscall(a[0]);
5458 retval = syscall(a[0],a[1]);
5461 retval = syscall(a[0],a[1],a[2]);
5464 retval = syscall(a[0],a[1],a[2],a[3]);
5467 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5470 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5473 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5476 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5480 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5483 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5486 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5490 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5494 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5498 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5499 a[10],a[11],a[12],a[13]);
5501 #endif /* atarist */
5507 DIE(aTHX_ PL_no_func, "syscall");
5511 #ifdef FCNTL_EMULATE_FLOCK
5513 /* XXX Emulate flock() with fcntl().
5514 What's really needed is a good file locking module.
5518 fcntl_emulate_flock(int fd, int operation)
5523 switch (operation & ~LOCK_NB) {
5525 flock.l_type = F_RDLCK;
5528 flock.l_type = F_WRLCK;
5531 flock.l_type = F_UNLCK;
5537 flock.l_whence = SEEK_SET;
5538 flock.l_start = flock.l_len = (Off_t)0;
5540 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5541 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5542 errno = EWOULDBLOCK;
5546 #endif /* FCNTL_EMULATE_FLOCK */
5548 #ifdef LOCKF_EMULATE_FLOCK
5550 /* XXX Emulate flock() with lockf(). This is just to increase
5551 portability of scripts. The calls are not completely
5552 interchangeable. What's really needed is a good file
5556 /* The lockf() constants might have been defined in <unistd.h>.
5557 Unfortunately, <unistd.h> causes troubles on some mixed
5558 (BSD/POSIX) systems, such as SunOS 4.1.3.
5560 Further, the lockf() constants aren't POSIX, so they might not be
5561 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5562 just stick in the SVID values and be done with it. Sigh.
5566 # define F_ULOCK 0 /* Unlock a previously locked region */
5569 # define F_LOCK 1 /* Lock a region for exclusive use */
5572 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5575 # define F_TEST 3 /* Test a region for other processes locks */
5579 lockf_emulate_flock(int fd, int operation)
5585 /* flock locks entire file so for lockf we need to do the same */
5586 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5587 if (pos > 0) /* is seekable and needs to be repositioned */
5588 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5589 pos = -1; /* seek failed, so don't seek back afterwards */
5592 switch (operation) {
5594 /* LOCK_SH - get a shared lock */
5596 /* LOCK_EX - get an exclusive lock */
5598 i = lockf (fd, F_LOCK, 0);
5601 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5602 case LOCK_SH|LOCK_NB:
5603 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5604 case LOCK_EX|LOCK_NB:
5605 i = lockf (fd, F_TLOCK, 0);
5607 if ((errno == EAGAIN) || (errno == EACCES))
5608 errno = EWOULDBLOCK;
5611 /* LOCK_UN - unlock (non-blocking is a no-op) */
5613 case LOCK_UN|LOCK_NB:
5614 i = lockf (fd, F_ULOCK, 0);
5617 /* Default - can't decipher operation */
5624 if (pos > 0) /* need to restore position of the handle */
5625 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5630 #endif /* LOCKF_EMULATE_FLOCK */
5634 * c-indentation-style: bsd
5636 * indent-tabs-mode: t
5639 * ex: set ts=8 sts=4 sw=4 noet: