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/resource.h>
62 # include <sys/select.h>
66 /* XXX Configure test needed.
67 h_errno might not be a simple 'int', especially for multi-threaded
68 applications, see "extern int errno in perl.h". Creating such
69 a test requires taking into account the differences between
70 compiling multithreaded and singlethreaded ($ccflags et al).
71 HOST_NOT_FOUND is typically defined in <netdb.h>.
73 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
82 struct passwd *getpwnam (char *);
83 struct passwd *getpwuid (Uid_t);
88 struct passwd *getpwent (void);
89 #elif defined (VMS) && defined (my_getpwent)
90 struct passwd *Perl_my_getpwent (pTHX);
99 struct group *getgrnam (char *);
100 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
110 # if defined(_MSC_VER) || defined(__MINGW32__)
111 # include <sys/utime.h>
118 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
121 # define my_chsize PerlLIO_chsize
124 # define my_chsize PerlLIO_chsize
126 I32 my_chsize(int fd, Off_t length);
132 #else /* no flock() */
134 /* fcntl.h might not have been included, even if it exists, because
135 the current Configure only sets I_FCNTL if it's needed to pick up
136 the *_OK constants. Make sure it has been included before testing
137 the fcntl() locking constants. */
138 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
142 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
143 # define FLOCK fcntl_emulate_flock
144 # define FCNTL_EMULATE_FLOCK
145 # else /* no flock() or fcntl(F_SETLK,...) */
147 # define FLOCK lockf_emulate_flock
148 # define LOCKF_EMULATE_FLOCK
150 # endif /* no flock() or fcntl(F_SETLK,...) */
153 static int FLOCK (int, int);
156 * These are the flock() constants. Since this sytems doesn't have
157 * flock(), the values of the constants are probably not available.
171 # endif /* emulating flock() */
173 #endif /* no flock() */
176 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
178 #if defined(I_SYS_ACCESS) && !defined(R_OK)
179 # include <sys/access.h>
182 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
183 # define FD_CLOEXEC 1 /* NeXT needs this */
189 /* Missing protos on LynxOS */
190 void sethostent(int);
191 void endhostent(void);
193 void endnetent(void);
194 void setprotoent(int);
195 void endprotoent(void);
196 void setservent(int);
197 void endservent(void);
200 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
202 /* F_OK unused: if stat() cannot find it... */
204 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
205 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
206 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
209 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
210 # ifdef I_SYS_SECURITY
211 # include <sys/security.h>
215 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
218 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
222 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
224 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
228 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
229 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
230 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
233 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
235 const Uid_t ruid = getuid();
236 const Uid_t euid = geteuid();
237 const Gid_t rgid = getgid();
238 const Gid_t egid = getegid();
241 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
242 Perl_croak(aTHX_ "switching effective uid is not implemented");
245 if (setreuid(euid, ruid))
248 if (setresuid(euid, ruid, (Uid_t)-1))
251 Perl_croak(aTHX_ "entering effective uid failed");
254 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
255 Perl_croak(aTHX_ "switching effective gid is not implemented");
258 if (setregid(egid, rgid))
261 if (setresgid(egid, rgid, (Gid_t)-1))
264 Perl_croak(aTHX_ "entering effective gid failed");
267 res = access(path, mode);
270 if (setreuid(ruid, euid))
273 if (setresuid(ruid, euid, (Uid_t)-1))
276 Perl_croak(aTHX_ "leaving effective uid failed");
279 if (setregid(rgid, egid))
282 if (setresgid(rgid, egid, (Gid_t)-1))
285 Perl_croak(aTHX_ "leaving effective gid failed");
289 # define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
296 const char * const tmps = POPpconstx;
297 const I32 gimme = GIMME_V;
298 const char *mode = "r";
301 if (PL_op->op_private & OPpOPEN_IN_RAW)
303 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
305 fp = PerlProc_popen(tmps, mode);
307 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
309 PerlIO_apply_layers(aTHX_ fp,mode,type);
311 if (gimme == G_VOID) {
313 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
316 else if (gimme == G_SCALAR) {
317 ENTER_with_name("backtick");
319 PL_rs = &PL_sv_undef;
320 sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
321 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
323 LEAVE_with_name("backtick");
329 SV * const sv = newSV(79);
330 if (sv_gets(sv, fp, 0) == NULL) {
335 if (SvLEN(sv) - SvCUR(sv) > 20) {
336 SvPV_shrink_to_cur(sv);
341 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
342 TAINT; /* "I believe that this is not gratuitous!" */
345 STATUS_NATIVE_CHILD_SET(-1);
346 if (gimme == G_SCALAR)
358 /* make a copy of the pattern, to ensure that magic is called once
360 TOPm1s = sv_2mortal(newSVsv(TOPm1s));
362 tryAMAGICunTARGET(iter_amg, -1, (PL_op->op_flags & OPf_SPECIAL));
364 if (PL_op->op_flags & OPf_SPECIAL) {
365 /* call Perl-level glob function instead. Stack args are:
366 * MARK, wildcard, csh_glob context index
367 * and following OPs should be: gv(CORE::GLOBAL::glob), entersub
371 /* stack args are: wildcard, gv(_GEN_n) */
374 /* Note that we only ever get here if File::Glob fails to load
375 * without at the same time croaking, for some reason, or if
376 * perl was built with PERL_EXTERNAL_GLOB */
378 ENTER_with_name("glob");
383 * The external globbing program may use things we can't control,
384 * so for security reasons we must assume the worst.
387 taint_proper(PL_no_security, "glob");
391 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
392 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
394 SAVESPTR(PL_rs); /* This is not permanent, either. */
395 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
398 *SvPVX(PL_rs) = '\n';
402 result = do_readline();
403 LEAVE_with_name("glob");
410 PL_last_in_gv = cGVOP_gv;
411 return do_readline();
421 do_join(TARG, &PL_sv_no, MARK, SP);
425 else if (SP == MARK) {
434 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
435 /* well-formed exception supplied */
437 else if (SvROK(ERRSV)) {
440 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
441 exsv = sv_mortalcopy(ERRSV);
442 sv_catpvs(exsv, "\t...caught");
445 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
447 if (SvROK(exsv) && !PL_warnhook)
448 Perl_warn(aTHX_ "%"SVf, SVfARG(exsv));
459 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
461 if (SP - MARK != 1) {
463 do_join(TARG, &PL_sv_no, MARK, SP);
471 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
472 /* well-formed exception supplied */
474 else if (SvROK(ERRSV)) {
476 if (sv_isobject(exsv)) {
477 HV * const stash = SvSTASH(SvRV(exsv));
478 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
480 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
481 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
488 call_sv(MUTABLE_SV(GvCV(gv)),
489 G_SCALAR|G_EVAL|G_KEEPERR);
490 exsv = sv_mortalcopy(*PL_stack_sp--);
494 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
495 exsv = sv_mortalcopy(ERRSV);
496 sv_catpvs(exsv, "\t...propagated");
499 exsv = newSVpvs_flags("Died", SVs_TEMP);
507 Perl_tied_method(pTHX_ const char *const methname, SV **sp, SV *const sv,
508 const MAGIC *const mg, const U32 flags, U32 argc, ...)
513 PERL_ARGS_ASSERT_TIED_METHOD;
515 /* Ensure that our flag bits do not overlap. */
516 assert((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
517 assert((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
518 assert((TIED_METHOD_SAY & G_WANT) == 0);
520 PUTBACK; /* sp is at *foot* of args, so this pops args from old stack */
521 PUSHSTACKi(PERLSI_MAGIC);
522 EXTEND(SP, argc+1); /* object + args */
524 PUSHs(SvTIED_obj(sv, mg));
525 if (flags & TIED_METHOD_ARGUMENTS_ON_STACK) {
526 Copy(orig_sp + 2, sp + 1, argc, SV*); /* copy args to new stack */
530 const U32 mortalize_not_needed
531 = flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
533 va_start(args, argc);
535 SV *const arg = va_arg(args, SV *);
536 if(mortalize_not_needed)
545 ENTER_with_name("call_tied_method");
546 if (flags & TIED_METHOD_SAY) {
547 /* local $\ = "\n" */
548 SAVEGENERICSV(PL_ors_sv);
549 PL_ors_sv = newSVpvs("\n");
551 ret_args = call_method(methname, flags & G_WANT);
556 if (ret_args) { /* copy results back to original stack */
557 EXTEND(sp, ret_args);
558 Copy(orig_sp - ret_args + 1, sp + 1, ret_args, SV*);
562 LEAVE_with_name("call_tied_method");
566 #define tied_method0(a,b,c,d) \
567 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
568 #define tied_method1(a,b,c,d,e) \
569 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
570 #define tied_method2(a,b,c,d,e,f) \
571 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
584 GV * const gv = MUTABLE_GV(*++MARK);
586 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
587 DIE(aTHX_ PL_no_usym, "filehandle");
589 if ((io = GvIOp(gv))) {
591 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
594 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
595 "Opening dirhandle %"HEKf" also as a file",
596 HEKfARG(GvENAME_HEK(gv)));
598 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
600 /* Method's args are same as ours ... */
601 /* ... except handle is replaced by the object */
602 return Perl_tied_method(aTHX_ "OPEN", mark - 1, MUTABLE_SV(io), mg,
603 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
615 tmps = SvPV_const(sv, len);
616 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
619 PUSHi( (I32)PL_forkprocess );
620 else if (PL_forkprocess == 0) /* we are a new child */
631 MAXARG == 0 || (!TOPs && !POPs) ? PL_defoutgv : MUTABLE_GV(POPs);
637 IO * const io = GvIO(gv);
639 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
641 return tied_method0("CLOSE", SP, MUTABLE_SV(io), mg);
645 PUSHs(boolSV(do_close(gv, TRUE)));
658 GV * const wgv = MUTABLE_GV(POPs);
659 GV * const rgv = MUTABLE_GV(POPs);
664 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
665 DIE(aTHX_ PL_no_usym, "filehandle");
670 do_close(rgv, FALSE);
672 do_close(wgv, FALSE);
674 if (PerlProc_pipe(fd) < 0)
677 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
678 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
679 IoOFP(rstio) = IoIFP(rstio);
680 IoIFP(wstio) = IoOFP(wstio);
681 IoTYPE(rstio) = IoTYPE_RDONLY;
682 IoTYPE(wstio) = IoTYPE_WRONLY;
684 if (!IoIFP(rstio) || !IoOFP(wstio)) {
686 PerlIO_close(IoIFP(rstio));
688 PerlLIO_close(fd[0]);
690 PerlIO_close(IoOFP(wstio));
692 PerlLIO_close(fd[1]);
695 #if defined(HAS_FCNTL) && defined(F_SETFD)
696 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
697 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
704 DIE(aTHX_ PL_no_func, "pipe");
718 gv = MUTABLE_GV(POPs);
722 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
724 return tied_method0("FILENO", SP, MUTABLE_SV(io), mg);
727 if (!io || !(fp = IoIFP(io))) {
728 /* Can't do this because people seem to do things like
729 defined(fileno($foo)) to check whether $foo is a valid fh.
736 PUSHi(PerlIO_fileno(fp));
748 if (MAXARG < 1 || (!TOPs && !POPs)) {
749 anum = PerlLIO_umask(022);
750 /* setting it to 022 between the two calls to umask avoids
751 * to have a window where the umask is set to 0 -- meaning
752 * that another thread could create world-writeable files. */
754 (void)PerlLIO_umask(anum);
757 anum = PerlLIO_umask(POPi);
758 TAINT_PROPER("umask");
761 /* Only DIE if trying to restrict permissions on "user" (self).
762 * Otherwise it's harmless and more useful to just return undef
763 * since 'group' and 'other' concepts probably don't exist here. */
764 if (MAXARG >= 1 && (TOPs||POPs) && (POPi & 0700))
765 DIE(aTHX_ "umask not implemented");
766 XPUSHs(&PL_sv_undef);
785 gv = MUTABLE_GV(POPs);
789 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
791 /* This takes advantage of the implementation of the varargs
792 function, which I don't think that the optimiser will be able to
793 figure out. Although, as it's a static function, in theory it
795 return Perl_tied_method(aTHX_ "BINMODE", SP, MUTABLE_SV(io), mg,
796 G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
797 discp ? 1 : 0, discp);
801 if (!io || !(fp = IoIFP(io))) {
803 SETERRNO(EBADF,RMS_IFI);
810 const char *d = NULL;
813 d = SvPV_const(discp, len);
814 mode = mode_from_discipline(d, len);
815 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
816 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
817 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
838 const I32 markoff = MARK - PL_stack_base;
839 const char *methname;
840 int how = PERL_MAGIC_tied;
844 switch(SvTYPE(varsv)) {
846 methname = "TIEHASH";
847 HvEITER_set(MUTABLE_HV(varsv), 0);
850 methname = "TIEARRAY";
854 if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
855 methname = "TIEHANDLE";
856 how = PERL_MAGIC_tiedscalar;
857 /* For tied filehandles, we apply tiedscalar magic to the IO
858 slot of the GP rather than the GV itself. AMS 20010812 */
860 GvIOp(varsv) = newIO();
861 varsv = MUTABLE_SV(GvIOp(varsv));
866 methname = "TIESCALAR";
867 how = PERL_MAGIC_tiedscalar;
871 if (sv_isobject(*MARK)) { /* Calls GET magic. */
872 ENTER_with_name("call_TIE");
873 PUSHSTACKi(PERLSI_MAGIC);
875 EXTEND(SP,(I32)items);
879 call_method(methname, G_SCALAR);
882 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
883 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
884 * wrong error message, and worse case, supreme action at a distance.
885 * (Sorry obfuscation writers. You're not going to be given this one.)
887 stash = gv_stashsv(*MARK, 0);
888 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
889 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
890 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
892 ENTER_with_name("call_TIE");
893 PUSHSTACKi(PERLSI_MAGIC);
895 EXTEND(SP,(I32)items);
899 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
905 if (sv_isobject(sv)) {
906 sv_unmagic(varsv, how);
907 /* Croak if a self-tie on an aggregate is attempted. */
908 if (varsv == SvRV(sv) &&
909 (SvTYPE(varsv) == SVt_PVAV ||
910 SvTYPE(varsv) == SVt_PVHV))
912 "Self-ties of arrays and hashes are not supported");
913 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
915 LEAVE_with_name("call_TIE");
916 SP = PL_stack_base + markoff;
926 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
927 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
929 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
932 if ((mg = SvTIED_mg(sv, how))) {
933 SV * const obj = SvRV(SvTIED_obj(sv, mg));
935 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
937 if (gv && isGV(gv) && (cv = GvCV(gv))) {
939 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
940 mXPUSHi(SvREFCNT(obj) - 1);
942 ENTER_with_name("call_UNTIE");
943 call_sv(MUTABLE_SV(cv), G_VOID);
944 LEAVE_with_name("call_UNTIE");
947 else if (mg && SvREFCNT(obj) > 1) {
948 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
949 "untie attempted while %"UVuf" inner references still exist",
950 (UV)SvREFCNT(obj) - 1 ) ;
954 sv_unmagic(sv, how) ;
964 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
965 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
967 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
970 if ((mg = SvTIED_mg(sv, how))) {
971 SV *osv = SvTIED_obj(sv, mg);
972 if (osv == mg->mg_obj)
973 osv = sv_mortalcopy(osv);
987 HV * const hv = MUTABLE_HV(POPs);
988 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
989 stash = gv_stashsv(sv, 0);
990 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
992 require_pv("AnyDBM_File.pm");
994 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
995 DIE(aTHX_ "No dbm on this machine");
1005 mPUSHu(O_RDWR|O_CREAT);
1010 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1013 if (!sv_isobject(TOPs)) {
1021 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1025 if (sv_isobject(TOPs)) {
1026 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1027 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1044 struct timeval timebuf;
1045 struct timeval *tbuf = &timebuf;
1048 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1053 # if BYTEORDER & 0xf0000
1054 # define ORDERBYTE (0x88888888 - BYTEORDER)
1056 # define ORDERBYTE (0x4444 - BYTEORDER)
1062 for (i = 1; i <= 3; i++) {
1063 SV * const sv = SP[i];
1066 if (SvREADONLY(sv)) {
1068 sv_force_normal_flags(sv, 0);
1069 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1070 Perl_croak_no_modify(aTHX);
1073 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1074 SvPV_force_nolen(sv); /* force string conversion */
1081 /* little endians can use vecs directly */
1082 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1089 masksize = NFDBITS / NBBY;
1091 masksize = sizeof(long); /* documented int, everyone seems to use long */
1093 Zero(&fd_sets[0], 4, char*);
1096 # if SELECT_MIN_BITS == 1
1097 growsize = sizeof(fd_set);
1099 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1100 # undef SELECT_MIN_BITS
1101 # define SELECT_MIN_BITS __FD_SETSIZE
1103 /* If SELECT_MIN_BITS is greater than one we most probably will want
1104 * to align the sizes with SELECT_MIN_BITS/8 because for example
1105 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1106 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1107 * on (sets/tests/clears bits) is 32 bits. */
1108 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1116 timebuf.tv_sec = (long)value;
1117 value -= (NV)timebuf.tv_sec;
1118 timebuf.tv_usec = (long)(value * 1000000.0);
1123 for (i = 1; i <= 3; i++) {
1125 if (!SvOK(sv) || SvCUR(sv) == 0) {
1132 Sv_Grow(sv, growsize);
1136 while (++j <= growsize) {
1140 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1142 Newx(fd_sets[i], growsize, char);
1143 for (offset = 0; offset < growsize; offset += masksize) {
1144 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1145 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1148 fd_sets[i] = SvPVX(sv);
1152 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1153 /* Can't make just the (void*) conditional because that would be
1154 * cpp #if within cpp macro, and not all compilers like that. */
1155 nfound = PerlSock_select(
1157 (Select_fd_set_t) fd_sets[1],
1158 (Select_fd_set_t) fd_sets[2],
1159 (Select_fd_set_t) fd_sets[3],
1160 (void*) tbuf); /* Workaround for compiler bug. */
1162 nfound = PerlSock_select(
1164 (Select_fd_set_t) fd_sets[1],
1165 (Select_fd_set_t) fd_sets[2],
1166 (Select_fd_set_t) fd_sets[3],
1169 for (i = 1; i <= 3; i++) {
1172 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1174 for (offset = 0; offset < growsize; offset += masksize) {
1175 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1176 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1178 Safefree(fd_sets[i]);
1185 if (GIMME == G_ARRAY && tbuf) {
1186 value = (NV)(timebuf.tv_sec) +
1187 (NV)(timebuf.tv_usec) / 1000000.0;
1192 DIE(aTHX_ "select not implemented");
1197 =for apidoc setdefout
1199 Sets PL_defoutgv, the default file handle for output, to the passed in
1200 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1201 count of the passed in typeglob is increased by one, and the reference count
1202 of the typeglob that PL_defoutgv points to is decreased by one.
1208 Perl_setdefout(pTHX_ GV *gv)
1211 SvREFCNT_inc_simple_void(gv);
1212 SvREFCNT_dec(PL_defoutgv);
1220 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1221 GV * egv = GvEGVx(PL_defoutgv);
1225 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1227 XPUSHs(&PL_sv_undef);
1229 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), HEK_UTF8(GvNAME_HEK(egv)) ? -GvNAMELEN(egv) : GvNAMELEN(egv), FALSE);
1230 if (gvp && *gvp == egv) {
1231 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1235 mXPUSHs(newRV(MUTABLE_SV(egv)));
1240 if (!GvIO(newdefout))
1241 gv_IOadd(newdefout);
1242 setdefout(newdefout);
1252 MAXARG==0 || (!TOPs && !POPs) ? PL_stdingv : MUTABLE_GV(POPs);
1253 IO *const io = GvIO(gv);
1259 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1261 const U32 gimme = GIMME_V;
1262 Perl_tied_method(aTHX_ "GETC", SP, MUTABLE_SV(io), mg, gimme, 0);
1263 if (gimme == G_SCALAR) {
1265 SvSetMagicSV_nosteal(TARG, TOPs);
1270 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1271 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1273 SETERRNO(EBADF,RMS_IFI);
1277 sv_setpvs(TARG, " ");
1278 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1279 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1280 /* Find out how many bytes the char needs */
1281 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1284 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1285 SvCUR_set(TARG,1+len);
1294 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1297 register PERL_CONTEXT *cx;
1298 const I32 gimme = GIMME_V;
1300 PERL_ARGS_ASSERT_DOFORM;
1302 if (cv && CvCLONE(cv))
1303 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1308 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1309 PUSHFORMAT(cx, retop);
1311 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1313 setdefout(gv); /* locally select filehandle so $% et al work */
1332 gv = MUTABLE_GV(POPs);
1346 goto not_a_format_reference;
1350 tmpsv = sv_newmortal();
1351 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1352 if (SvPOK(tmpsv) && *SvPV_nolen_const(tmpsv))
1353 DIE(aTHX_ "Undefined format \"%"SVf"\" called", SVfARG(tmpsv));
1355 not_a_format_reference:
1356 DIE(aTHX_ "Not a format reference");
1358 IoFLAGS(io) &= ~IOf_DIDTOP;
1359 return doform(cv,gv,PL_op->op_next);
1365 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1366 register IO * const io = GvIOp(gv);
1371 register PERL_CONTEXT *cx;
1374 if (!io || !(ofp = IoOFP(io)))
1377 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1378 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1380 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1381 PL_formtarget != PL_toptarget)
1385 if (!IoTOP_GV(io)) {
1388 if (!IoTOP_NAME(io)) {
1390 if (!IoFMT_NAME(io))
1391 IoFMT_NAME(io) = savepv(GvNAME(gv));
1392 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%"HEKf"_TOP",
1393 HEKfARG(GvNAME_HEK(gv))));
1394 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1395 if ((topgv && GvFORM(topgv)) ||
1396 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1397 IoTOP_NAME(io) = savesvpv(topname);
1399 IoTOP_NAME(io) = savepvs("top");
1401 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1402 if (!topgv || !GvFORM(topgv)) {
1403 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1406 IoTOP_GV(io) = topgv;
1408 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1409 I32 lines = IoLINES_LEFT(io);
1410 const char *s = SvPVX_const(PL_formtarget);
1411 if (lines <= 0) /* Yow, header didn't even fit!!! */
1413 while (lines-- > 0) {
1414 s = strchr(s, '\n');
1420 const STRLEN save = SvCUR(PL_formtarget);
1421 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1422 do_print(PL_formtarget, ofp);
1423 SvCUR_set(PL_formtarget, save);
1424 sv_chop(PL_formtarget, s);
1425 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1428 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1429 do_print(PL_formfeed, ofp);
1430 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1432 PL_formtarget = PL_toptarget;
1433 IoFLAGS(io) |= IOf_DIDTOP;
1436 DIE(aTHX_ "bad top format reference");
1439 SV * const sv = sv_newmortal();
1440 gv_efullname4(sv, fgv, NULL, FALSE);
1441 if (SvPOK(sv) && *SvPV_nolen_const(sv))
1442 DIE(aTHX_ "Undefined top format \"%"SVf"\" called", SVfARG(sv));
1444 DIE(aTHX_ "Undefined top format called");
1446 return doform(cv, gv, PL_op);
1450 POPBLOCK(cx,PL_curpm);
1452 retop = cx->blk_sub.retop;
1458 report_wrongway_fh(gv, '<');
1464 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1465 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1467 if (!do_print(PL_formtarget, fp))
1470 FmLINES(PL_formtarget) = 0;
1471 SvCUR_set(PL_formtarget, 0);
1472 *SvEND(PL_formtarget) = '\0';
1473 if (IoFLAGS(io) & IOf_FLUSH)
1474 (void)PerlIO_flush(fp);
1479 PL_formtarget = PL_bodytarget;
1481 PERL_UNUSED_VAR(newsp);
1482 PERL_UNUSED_VAR(gimme);
1488 dVAR; dSP; dMARK; dORIGMARK;
1493 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1494 IO *const io = GvIO(gv);
1497 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1499 if (MARK == ORIGMARK) {
1502 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1505 return Perl_tied_method(aTHX_ "PRINTF", mark - 1, MUTABLE_SV(io),
1507 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1515 SETERRNO(EBADF,RMS_IFI);
1518 else if (!(fp = IoOFP(io))) {
1520 report_wrongway_fh(gv, '<');
1521 else if (ckWARN(WARN_CLOSED))
1523 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1527 do_sprintf(sv, SP - MARK, MARK + 1);
1528 if (!do_print(sv, fp))
1531 if (IoFLAGS(io) & IOf_FLUSH)
1532 if (PerlIO_flush(fp) == EOF)
1543 PUSHs(&PL_sv_undef);
1551 const int perm = (MAXARG > 3 && (TOPs || POPs)) ? POPi : 0666;
1552 const int mode = POPi;
1553 SV * const sv = POPs;
1554 GV * const gv = MUTABLE_GV(POPs);
1557 /* Need TIEHANDLE method ? */
1558 const char * const tmps = SvPV_const(sv, len);
1559 /* FIXME? do_open should do const */
1560 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1561 IoLINES(GvIOp(gv)) = 0;
1565 PUSHs(&PL_sv_undef);
1572 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1586 bool charstart = FALSE;
1587 STRLEN charskip = 0;
1590 GV * const gv = MUTABLE_GV(*++MARK);
1591 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1592 && gv && (io = GvIO(gv)) )
1594 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1596 return Perl_tied_method(aTHX_ "READ", mark - 1, MUTABLE_SV(io), mg,
1597 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1606 sv_setpvs(bufsv, "");
1607 length = SvIVx(*++MARK);
1610 offset = SvIVx(*++MARK);
1614 if (!io || !IoIFP(io)) {
1616 SETERRNO(EBADF,RMS_IFI);
1619 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1620 buffer = SvPVutf8_force(bufsv, blen);
1621 /* UTF-8 may not have been set if they are all low bytes */
1626 buffer = SvPV_force(bufsv, blen);
1627 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1630 DIE(aTHX_ "Negative length");
1638 if (PL_op->op_type == OP_RECV) {
1639 Sock_size_t bufsize;
1640 char namebuf[MAXPATHLEN];
1641 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1642 bufsize = sizeof (struct sockaddr_in);
1644 bufsize = sizeof namebuf;
1646 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1650 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1651 /* 'offset' means 'flags' here */
1652 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1653 (struct sockaddr *)namebuf, &bufsize);
1656 /* MSG_TRUNC can give oversized count; quietly lose it */
1660 /* Bogus return without padding */
1661 bufsize = sizeof (struct sockaddr_in);
1663 SvCUR_set(bufsv, count);
1664 *SvEND(bufsv) = '\0';
1665 (void)SvPOK_only(bufsv);
1669 /* This should not be marked tainted if the fp is marked clean */
1670 if (!(IoFLAGS(io) & IOf_UNTAINT))
1671 SvTAINTED_on(bufsv);
1673 sv_setpvn(TARG, namebuf, bufsize);
1678 if (DO_UTF8(bufsv)) {
1679 /* offset adjust in characters not bytes */
1680 blen = sv_len_utf8(bufsv);
1683 if (-offset > (SSize_t)blen)
1684 DIE(aTHX_ "Offset outside string");
1687 if (DO_UTF8(bufsv)) {
1688 /* convert offset-as-chars to offset-as-bytes */
1689 if (offset >= (int)blen)
1690 offset += SvCUR(bufsv) - blen;
1692 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1695 orig_size = SvCUR(bufsv);
1696 /* Allocating length + offset + 1 isn't perfect in the case of reading
1697 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1699 (should be 2 * length + offset + 1, or possibly something longer if
1700 PL_encoding is true) */
1701 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1702 if (offset > 0 && offset > (SSize_t)orig_size) { /* Zero any newly allocated space */
1703 Zero(buffer+orig_size, offset-orig_size, char);
1705 buffer = buffer + offset;
1707 read_target = bufsv;
1709 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1710 concatenate it to the current buffer. */
1712 /* Truncate the existing buffer to the start of where we will be
1714 SvCUR_set(bufsv, offset);
1716 read_target = sv_newmortal();
1717 SvUPGRADE(read_target, SVt_PV);
1718 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1721 if (PL_op->op_type == OP_SYSREAD) {
1722 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1723 if (IoTYPE(io) == IoTYPE_SOCKET) {
1724 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1730 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1735 #ifdef HAS_SOCKET__bad_code_maybe
1736 if (IoTYPE(io) == IoTYPE_SOCKET) {
1737 Sock_size_t bufsize;
1738 char namebuf[MAXPATHLEN];
1739 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1740 bufsize = sizeof (struct sockaddr_in);
1742 bufsize = sizeof namebuf;
1744 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1745 (struct sockaddr *)namebuf, &bufsize);
1750 count = PerlIO_read(IoIFP(io), buffer, length);
1751 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1752 if (count == 0 && PerlIO_error(IoIFP(io)))
1756 if (IoTYPE(io) == IoTYPE_WRONLY)
1757 report_wrongway_fh(gv, '>');
1760 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1761 *SvEND(read_target) = '\0';
1762 (void)SvPOK_only(read_target);
1763 if (fp_utf8 && !IN_BYTES) {
1764 /* Look at utf8 we got back and count the characters */
1765 const char *bend = buffer + count;
1766 while (buffer < bend) {
1768 skip = UTF8SKIP(buffer);
1771 if (buffer - charskip + skip > bend) {
1772 /* partial character - try for rest of it */
1773 length = skip - (bend-buffer);
1774 offset = bend - SvPVX_const(bufsv);
1786 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1787 provided amount read (count) was what was requested (length)
1789 if (got < wanted && count == length) {
1790 length = wanted - got;
1791 offset = bend - SvPVX_const(bufsv);
1794 /* return value is character count */
1798 else if (buffer_utf8) {
1799 /* Let svcatsv upgrade the bytes we read in to utf8.
1800 The buffer is a mortal so will be freed soon. */
1801 sv_catsv_nomg(bufsv, read_target);
1804 /* This should not be marked tainted if the fp is marked clean */
1805 if (!(IoFLAGS(io) & IOf_UNTAINT))
1806 SvTAINTED_on(bufsv);
1818 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1823 STRLEN orig_blen_bytes;
1824 const int op_type = PL_op->op_type;
1827 GV *const gv = MUTABLE_GV(*++MARK);
1828 IO *const io = GvIO(gv);
1830 if (op_type == OP_SYSWRITE && io) {
1831 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1833 if (MARK == SP - 1) {
1835 mXPUSHi(sv_len(sv));
1839 return Perl_tied_method(aTHX_ "WRITE", mark - 1, MUTABLE_SV(io), mg,
1840 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1850 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1852 if (io && IoIFP(io))
1853 report_wrongway_fh(gv, '<');
1856 SETERRNO(EBADF,RMS_IFI);
1860 /* Do this first to trigger any overloading. */
1861 buffer = SvPV_const(bufsv, blen);
1862 orig_blen_bytes = blen;
1863 doing_utf8 = DO_UTF8(bufsv);
1865 if (PerlIO_isutf8(IoIFP(io))) {
1866 if (!SvUTF8(bufsv)) {
1867 /* We don't modify the original scalar. */
1868 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1869 buffer = (char *) tmpbuf;
1873 else if (doing_utf8) {
1874 STRLEN tmplen = blen;
1875 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1878 buffer = (char *) tmpbuf;
1882 assert((char *)result == buffer);
1883 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1888 if (op_type == OP_SEND) {
1889 const int flags = SvIVx(*++MARK);
1892 char * const sockbuf = SvPVx(*++MARK, mlen);
1893 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1894 flags, (struct sockaddr *)sockbuf, mlen);
1898 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1904 Size_t length = 0; /* This length is in characters. */
1910 /* The SV is bytes, and we've had to upgrade it. */
1911 blen_chars = orig_blen_bytes;
1913 /* The SV really is UTF-8. */
1914 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1915 /* Don't call sv_len_utf8 again because it will call magic
1916 or overloading a second time, and we might get back a
1917 different result. */
1918 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1920 /* It's safe, and it may well be cached. */
1921 blen_chars = sv_len_utf8(bufsv);
1929 length = blen_chars;
1931 #if Size_t_size > IVSIZE
1932 length = (Size_t)SvNVx(*++MARK);
1934 length = (Size_t)SvIVx(*++MARK);
1936 if ((SSize_t)length < 0) {
1938 DIE(aTHX_ "Negative length");
1943 offset = SvIVx(*++MARK);
1945 if (-offset > (IV)blen_chars) {
1947 DIE(aTHX_ "Offset outside string");
1949 offset += blen_chars;
1950 } else if (offset > (IV)blen_chars) {
1952 DIE(aTHX_ "Offset outside string");
1956 if (length > blen_chars - offset)
1957 length = blen_chars - offset;
1959 /* Here we convert length from characters to bytes. */
1960 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1961 /* Either we had to convert the SV, or the SV is magical, or
1962 the SV has overloading, in which case we can't or mustn't
1963 or mustn't call it again. */
1965 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1966 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1968 /* It's a real UTF-8 SV, and it's not going to change under
1969 us. Take advantage of any cache. */
1971 I32 len_I32 = length;
1973 /* Convert the start and end character positions to bytes.
1974 Remember that the second argument to sv_pos_u2b is relative
1976 sv_pos_u2b(bufsv, &start, &len_I32);
1983 buffer = buffer+offset;
1985 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1986 if (IoTYPE(io) == IoTYPE_SOCKET) {
1987 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1993 /* See the note at doio.c:do_print about filesize limits. --jhi */
1994 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
2003 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2006 #if Size_t_size > IVSIZE
2026 * in Perl 5.12 and later, the additional parameter is a bitmask:
2029 * 2 = eof() <- ARGV magic
2031 * I'll rely on the compiler's trace flow analysis to decide whether to
2032 * actually assign this out here, or punt it into the only block where it is
2033 * used. Doing it out here is DRY on the condition logic.
2038 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2044 if (PL_op->op_flags & OPf_SPECIAL) {
2045 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2049 gv = PL_last_in_gv; /* eof */
2057 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2058 return tied_method1("EOF", SP, MUTABLE_SV(io), mg, newSVuv(which));
2061 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2062 if (io && !IoIFP(io)) {
2063 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2065 IoFLAGS(io) &= ~IOf_START;
2066 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2068 sv_setpvs(GvSV(gv), "-");
2070 GvSV(gv) = newSVpvs("-");
2071 SvSETMAGIC(GvSV(gv));
2073 else if (!nextargv(gv))
2078 PUSHs(boolSV(do_eof(gv)));
2088 if (MAXARG != 0 && (TOPs || POPs))
2089 PL_last_in_gv = MUTABLE_GV(POPs);
2096 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2098 return tied_method0("TELL", SP, MUTABLE_SV(io), mg);
2103 SETERRNO(EBADF,RMS_IFI);
2108 #if LSEEKSIZE > IVSIZE
2109 PUSHn( do_tell(gv) );
2111 PUSHi( do_tell(gv) );
2119 const int whence = POPi;
2120 #if LSEEKSIZE > IVSIZE
2121 const Off_t offset = (Off_t)SvNVx(POPs);
2123 const Off_t offset = (Off_t)SvIVx(POPs);
2126 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2127 IO *const io = GvIO(gv);
2130 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2132 #if LSEEKSIZE > IVSIZE
2133 SV *const offset_sv = newSVnv((NV) offset);
2135 SV *const offset_sv = newSViv(offset);
2138 return tied_method2("SEEK", SP, MUTABLE_SV(io), mg, offset_sv,
2143 if (PL_op->op_type == OP_SEEK)
2144 PUSHs(boolSV(do_seek(gv, offset, whence)));
2146 const Off_t sought = do_sysseek(gv, offset, whence);
2148 PUSHs(&PL_sv_undef);
2150 SV* const sv = sought ?
2151 #if LSEEKSIZE > IVSIZE
2156 : newSVpvn(zero_but_true, ZBTLEN);
2167 /* There seems to be no consensus on the length type of truncate()
2168 * and ftruncate(), both off_t and size_t have supporters. In
2169 * general one would think that when using large files, off_t is
2170 * at least as wide as size_t, so using an off_t should be okay. */
2171 /* XXX Configure probe for the length type of *truncate() needed XXX */
2174 #if Off_t_size > IVSIZE
2179 /* Checking for length < 0 is problematic as the type might or
2180 * might not be signed: if it is not, clever compilers will moan. */
2181 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2184 SV * const sv = POPs;
2189 if ((tmpgv = PL_op->op_flags & OPf_SPECIAL
2190 ? gv_fetchsv(sv, 0, SVt_PVIO)
2191 : MAYBE_DEREF_GV(sv) )) {
2198 TAINT_PROPER("truncate");
2199 if (!(fp = IoIFP(io))) {
2205 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2207 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2213 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2214 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2215 goto do_ftruncate_io;
2218 const char * const name = SvPV_nomg_const_nolen(sv);
2219 TAINT_PROPER("truncate");
2221 if (truncate(name, len) < 0)
2225 const int tmpfd = PerlLIO_open(name, O_RDWR);
2230 if (my_chsize(tmpfd, len) < 0)
2232 PerlLIO_close(tmpfd);
2241 SETERRNO(EBADF,RMS_IFI);
2249 SV * const argsv = POPs;
2250 const unsigned int func = POPu;
2251 const int optype = PL_op->op_type;
2252 GV * const gv = MUTABLE_GV(POPs);
2253 IO * const io = gv ? GvIOn(gv) : NULL;
2257 if (!io || !argsv || !IoIFP(io)) {
2259 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2263 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2266 s = SvPV_force(argsv, len);
2267 need = IOCPARM_LEN(func);
2269 s = Sv_Grow(argsv, need + 1);
2270 SvCUR_set(argsv, need);
2273 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2276 retval = SvIV(argsv);
2277 s = INT2PTR(char*,retval); /* ouch */
2280 TAINT_PROPER(PL_op_desc[optype]);
2282 if (optype == OP_IOCTL)
2284 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2286 DIE(aTHX_ "ioctl is not implemented");
2290 DIE(aTHX_ "fcntl is not implemented");
2292 #if defined(OS2) && defined(__EMX__)
2293 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2295 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2299 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2301 if (s[SvCUR(argsv)] != 17)
2302 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2304 s[SvCUR(argsv)] = 0; /* put our null back */
2305 SvSETMAGIC(argsv); /* Assume it has changed */
2314 PUSHp(zero_but_true, ZBTLEN);
2325 const int argtype = POPi;
2326 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2327 IO *const io = GvIO(gv);
2328 PerlIO *const fp = io ? IoIFP(io) : NULL;
2330 /* XXX Looks to me like io is always NULL at this point */
2332 (void)PerlIO_flush(fp);
2333 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2338 SETERRNO(EBADF,RMS_IFI);
2343 DIE(aTHX_ PL_no_func, "flock()");
2354 const int protocol = POPi;
2355 const int type = POPi;
2356 const int domain = POPi;
2357 GV * const gv = MUTABLE_GV(POPs);
2358 register IO * const io = gv ? GvIOn(gv) : NULL;
2363 if (io && IoIFP(io))
2364 do_close(gv, FALSE);
2365 SETERRNO(EBADF,LIB_INVARG);
2370 do_close(gv, FALSE);
2372 TAINT_PROPER("socket");
2373 fd = PerlSock_socket(domain, type, protocol);
2376 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2377 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2378 IoTYPE(io) = IoTYPE_SOCKET;
2379 if (!IoIFP(io) || !IoOFP(io)) {
2380 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2381 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2382 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2385 #if defined(HAS_FCNTL) && defined(F_SETFD)
2386 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2390 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2399 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2401 const int protocol = POPi;
2402 const int type = POPi;
2403 const int domain = POPi;
2404 GV * const gv2 = MUTABLE_GV(POPs);
2405 GV * const gv1 = MUTABLE_GV(POPs);
2406 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2407 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2411 report_evil_fh(gv1);
2413 report_evil_fh(gv2);
2415 if (io1 && IoIFP(io1))
2416 do_close(gv1, FALSE);
2417 if (io2 && IoIFP(io2))
2418 do_close(gv2, FALSE);
2423 TAINT_PROPER("socketpair");
2424 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2426 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2427 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2428 IoTYPE(io1) = IoTYPE_SOCKET;
2429 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2430 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2431 IoTYPE(io2) = IoTYPE_SOCKET;
2432 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2433 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2434 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2435 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2436 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2437 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2438 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2441 #if defined(HAS_FCNTL) && defined(F_SETFD)
2442 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2443 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2448 DIE(aTHX_ PL_no_sock_func, "socketpair");
2457 SV * const addrsv = POPs;
2458 /* OK, so on what platform does bind modify addr? */
2460 GV * const gv = MUTABLE_GV(POPs);
2461 register IO * const io = GvIOn(gv);
2463 const int op_type = PL_op->op_type;
2465 if (!io || !IoIFP(io))
2468 addr = SvPV_const(addrsv, len);
2469 TAINT_PROPER(PL_op_desc[op_type]);
2470 if ((op_type == OP_BIND
2471 ? PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len)
2472 : PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len))
2480 SETERRNO(EBADF,SS_IVCHAN);
2487 const int backlog = POPi;
2488 GV * const gv = MUTABLE_GV(POPs);
2489 register IO * const io = gv ? GvIOn(gv) : NULL;
2491 if (!io || !IoIFP(io))
2494 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2501 SETERRNO(EBADF,SS_IVCHAN);
2510 char namebuf[MAXPATHLEN];
2511 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2512 Sock_size_t len = sizeof (struct sockaddr_in);
2514 Sock_size_t len = sizeof namebuf;
2516 GV * const ggv = MUTABLE_GV(POPs);
2517 GV * const ngv = MUTABLE_GV(POPs);
2526 if (!gstio || !IoIFP(gstio))
2530 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2533 /* Some platforms indicate zero length when an AF_UNIX client is
2534 * not bound. Simulate a non-zero-length sockaddr structure in
2536 namebuf[0] = 0; /* sun_len */
2537 namebuf[1] = AF_UNIX; /* sun_family */
2545 do_close(ngv, FALSE);
2546 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2547 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2548 IoTYPE(nstio) = IoTYPE_SOCKET;
2549 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2550 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2551 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2552 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2555 #if defined(HAS_FCNTL) && defined(F_SETFD)
2556 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2560 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2561 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2563 #ifdef __SCO_VERSION__
2564 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2567 PUSHp(namebuf, len);
2571 report_evil_fh(ggv);
2572 SETERRNO(EBADF,SS_IVCHAN);
2582 const int how = POPi;
2583 GV * const gv = MUTABLE_GV(POPs);
2584 register IO * const io = GvIOn(gv);
2586 if (!io || !IoIFP(io))
2589 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2594 SETERRNO(EBADF,SS_IVCHAN);
2601 const int optype = PL_op->op_type;
2602 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2603 const unsigned int optname = (unsigned int) POPi;
2604 const unsigned int lvl = (unsigned int) POPi;
2605 GV * const gv = MUTABLE_GV(POPs);
2606 register IO * const io = GvIOn(gv);
2610 if (!io || !IoIFP(io))
2613 fd = PerlIO_fileno(IoIFP(io));
2617 (void)SvPOK_only(sv);
2621 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2628 #if defined(__SYMBIAN32__)
2629 # define SETSOCKOPT_OPTION_VALUE_T void *
2631 # define SETSOCKOPT_OPTION_VALUE_T const char *
2633 /* XXX TODO: We need to have a proper type (a Configure probe,
2634 * etc.) for what the C headers think of the third argument of
2635 * setsockopt(), the option_value read-only buffer: is it
2636 * a "char *", or a "void *", const or not. Some compilers
2637 * don't take kindly to e.g. assuming that "char *" implicitly
2638 * promotes to a "void *", or to explicitly promoting/demoting
2639 * consts to non/vice versa. The "const void *" is the SUS
2640 * definition, but that does not fly everywhere for the above
2642 SETSOCKOPT_OPTION_VALUE_T buf;
2646 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2650 aint = (int)SvIV(sv);
2651 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2654 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2664 SETERRNO(EBADF,SS_IVCHAN);
2673 const int optype = PL_op->op_type;
2674 GV * const gv = MUTABLE_GV(POPs);
2675 register IO * const io = GvIOn(gv);
2680 if (!io || !IoIFP(io))
2683 sv = sv_2mortal(newSV(257));
2684 (void)SvPOK_only(sv);
2688 fd = PerlIO_fileno(IoIFP(io));
2690 case OP_GETSOCKNAME:
2691 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2694 case OP_GETPEERNAME:
2695 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2697 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2699 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";
2700 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2701 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2702 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2703 sizeof(u_short) + sizeof(struct in_addr))) {
2710 #ifdef BOGUS_GETNAME_RETURN
2711 /* Interactive Unix, getpeername() and getsockname()
2712 does not return valid namelen */
2713 if (len == BOGUS_GETNAME_RETURN)
2714 len = sizeof(struct sockaddr);
2723 SETERRNO(EBADF,SS_IVCHAN);
2742 if (PL_op->op_flags & OPf_REF ? (gv = cGVOP_gv, 1)
2743 : !!(sv=POPs, gv = MAYBE_DEREF_GV(sv))) {
2744 if (PL_op->op_type == OP_LSTAT) {
2745 if (gv != PL_defgv) {
2746 do_fstat_warning_check:
2747 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2748 "lstat() on filehandle %"SVf, SVfARG(gv
2749 ? sv_2mortal(newSVhek(GvENAME_HEK(gv)))
2751 } else if (PL_laststype != OP_LSTAT)
2752 /* diag_listed_as: The stat preceding %s wasn't an lstat */
2753 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2756 if (gv != PL_defgv) {
2757 PL_laststype = OP_STAT;
2759 sv_setpvs(PL_statname, "");
2766 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2767 } else if (IoDIRP(io)) {
2769 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2771 PL_laststatval = -1;
2777 if (PL_laststatval < 0) {
2783 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2784 io = MUTABLE_IO(SvRV(sv));
2785 if (PL_op->op_type == OP_LSTAT)
2786 goto do_fstat_warning_check;
2787 goto do_fstat_have_io;
2790 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
2792 PL_laststype = PL_op->op_type;
2793 if (PL_op->op_type == OP_LSTAT)
2794 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2796 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2797 if (PL_laststatval < 0) {
2798 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2799 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2805 if (gimme != G_ARRAY) {
2806 if (gimme != G_VOID)
2807 XPUSHs(boolSV(max));
2813 mPUSHi(PL_statcache.st_dev);
2814 #if ST_INO_SIZE > IVSIZE
2815 mPUSHn(PL_statcache.st_ino);
2817 # if ST_INO_SIGN <= 0
2818 mPUSHi(PL_statcache.st_ino);
2820 mPUSHu(PL_statcache.st_ino);
2823 mPUSHu(PL_statcache.st_mode);
2824 mPUSHu(PL_statcache.st_nlink);
2825 #if Uid_t_size > IVSIZE
2826 mPUSHn(PL_statcache.st_uid);
2828 # if Uid_t_sign <= 0
2829 mPUSHi(PL_statcache.st_uid);
2831 mPUSHu(PL_statcache.st_uid);
2834 #if Gid_t_size > IVSIZE
2835 mPUSHn(PL_statcache.st_gid);
2837 # if Gid_t_sign <= 0
2838 mPUSHi(PL_statcache.st_gid);
2840 mPUSHu(PL_statcache.st_gid);
2843 #ifdef USE_STAT_RDEV
2844 mPUSHi(PL_statcache.st_rdev);
2846 PUSHs(newSVpvs_flags("", SVs_TEMP));
2848 #if Off_t_size > IVSIZE
2849 mPUSHn(PL_statcache.st_size);
2851 mPUSHi(PL_statcache.st_size);
2854 mPUSHn(PL_statcache.st_atime);
2855 mPUSHn(PL_statcache.st_mtime);
2856 mPUSHn(PL_statcache.st_ctime);
2858 mPUSHi(PL_statcache.st_atime);
2859 mPUSHi(PL_statcache.st_mtime);
2860 mPUSHi(PL_statcache.st_ctime);
2862 #ifdef USE_STAT_BLOCKS
2863 mPUSHu(PL_statcache.st_blksize);
2864 mPUSHu(PL_statcache.st_blocks);
2866 PUSHs(newSVpvs_flags("", SVs_TEMP));
2867 PUSHs(newSVpvs_flags("", SVs_TEMP));
2873 #define tryAMAGICftest_MG(chr) STMT_START { \
2874 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2875 && PL_op->op_flags & OPf_KIDS \
2876 && S_try_amagic_ftest(aTHX_ chr)) \
2881 S_try_amagic_ftest(pTHX_ char chr) {
2884 SV* const arg = TOPs;
2891 const char tmpchr = chr;
2892 SV * const tmpsv = amagic_call(arg,
2893 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2894 ftest_amg, AMGf_unary);
2901 if (PL_op->op_private & OPpFT_STACKING) {
2903 /* leave the object alone */
2915 /* This macro is used by the stacked filetest operators :
2916 * if the previous filetest failed, short-circuit and pass its value.
2917 * Else, discard it from the stack and continue. --rgs
2919 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2920 if (!SvTRUE(TOPs)) { RETURN; } \
2921 else { (void)POPs; PUTBACK; } \
2928 /* Not const, because things tweak this below. Not bool, because there's
2929 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2930 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2931 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2932 /* Giving some sort of initial value silences compilers. */
2934 int access_mode = R_OK;
2936 int access_mode = 0;
2939 /* access_mode is never used, but leaving use_access in makes the
2940 conditional compiling below much clearer. */
2943 Mode_t stat_mode = S_IRUSR;
2945 bool effective = FALSE;
2949 switch (PL_op->op_type) {
2950 case OP_FTRREAD: opchar = 'R'; break;
2951 case OP_FTRWRITE: opchar = 'W'; break;
2952 case OP_FTREXEC: opchar = 'X'; break;
2953 case OP_FTEREAD: opchar = 'r'; break;
2954 case OP_FTEWRITE: opchar = 'w'; break;
2955 case OP_FTEEXEC: opchar = 'x'; break;
2957 tryAMAGICftest_MG(opchar);
2959 STACKED_FTEST_CHECK;
2961 switch (PL_op->op_type) {
2963 #if !(defined(HAS_ACCESS) && defined(R_OK))
2969 #if defined(HAS_ACCESS) && defined(W_OK)
2974 stat_mode = S_IWUSR;
2978 #if defined(HAS_ACCESS) && defined(X_OK)
2983 stat_mode = S_IXUSR;
2987 #ifdef PERL_EFF_ACCESS
2990 stat_mode = S_IWUSR;
2994 #ifndef PERL_EFF_ACCESS
3001 #ifdef PERL_EFF_ACCESS
3006 stat_mode = S_IXUSR;
3012 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3013 const char *name = POPpx;
3015 # ifdef PERL_EFF_ACCESS
3016 result = PERL_EFF_ACCESS(name, access_mode);
3018 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3024 result = access(name, access_mode);
3026 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3037 result = my_stat_flags(0);
3041 if (cando(stat_mode, effective, &PL_statcache))
3050 const int op_type = PL_op->op_type;
3055 case OP_FTIS: opchar = 'e'; break;
3056 case OP_FTSIZE: opchar = 's'; break;
3057 case OP_FTMTIME: opchar = 'M'; break;
3058 case OP_FTCTIME: opchar = 'C'; break;
3059 case OP_FTATIME: opchar = 'A'; break;
3061 tryAMAGICftest_MG(opchar);
3063 STACKED_FTEST_CHECK;
3065 result = my_stat_flags(0);
3069 if (op_type == OP_FTIS)
3072 /* You can't dTARGET inside OP_FTIS, because you'll get
3073 "panic: pad_sv po" - the op is not flagged to have a target. */
3077 #if Off_t_size > IVSIZE
3078 PUSHn(PL_statcache.st_size);
3080 PUSHi(PL_statcache.st_size);
3084 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3087 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3090 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3104 switch (PL_op->op_type) {
3105 case OP_FTROWNED: opchar = 'O'; break;
3106 case OP_FTEOWNED: opchar = 'o'; break;
3107 case OP_FTZERO: opchar = 'z'; break;
3108 case OP_FTSOCK: opchar = 'S'; break;
3109 case OP_FTCHR: opchar = 'c'; break;
3110 case OP_FTBLK: opchar = 'b'; break;
3111 case OP_FTFILE: opchar = 'f'; break;
3112 case OP_FTDIR: opchar = 'd'; break;
3113 case OP_FTPIPE: opchar = 'p'; break;
3114 case OP_FTSUID: opchar = 'u'; break;
3115 case OP_FTSGID: opchar = 'g'; break;
3116 case OP_FTSVTX: opchar = 'k'; break;
3118 tryAMAGICftest_MG(opchar);
3120 STACKED_FTEST_CHECK;
3122 /* I believe that all these three are likely to be defined on most every
3123 system these days. */
3125 if(PL_op->op_type == OP_FTSUID) {
3126 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3132 if(PL_op->op_type == OP_FTSGID) {
3133 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3139 if(PL_op->op_type == OP_FTSVTX) {
3140 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3146 result = my_stat_flags(0);
3150 switch (PL_op->op_type) {
3152 if (PL_statcache.st_uid == PL_uid)
3156 if (PL_statcache.st_uid == PL_euid)
3160 if (PL_statcache.st_size == 0)
3164 if (S_ISSOCK(PL_statcache.st_mode))
3168 if (S_ISCHR(PL_statcache.st_mode))
3172 if (S_ISBLK(PL_statcache.st_mode))
3176 if (S_ISREG(PL_statcache.st_mode))
3180 if (S_ISDIR(PL_statcache.st_mode))
3184 if (S_ISFIFO(PL_statcache.st_mode))
3189 if (PL_statcache.st_mode & S_ISUID)
3195 if (PL_statcache.st_mode & S_ISGID)
3201 if (PL_statcache.st_mode & S_ISVTX)
3215 tryAMAGICftest_MG('l');
3216 STACKED_FTEST_CHECK;
3217 result = my_lstat_flags(0);
3222 if (S_ISLNK(PL_statcache.st_mode))
3237 tryAMAGICftest_MG('t');
3239 STACKED_FTEST_CHECK;
3241 if (PL_op->op_flags & OPf_REF)
3243 else if (!(gv = MAYBE_DEREF_GV_nomg(TOPs))) {
3245 name = SvPV_nomg(tmpsv, namelen);
3246 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3249 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3250 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3251 else if (tmpsv && SvOK(tmpsv)) {
3259 if (PerlLIO_isatty(fd))
3264 #if defined(atarist) /* this will work with atariST. Configure will
3265 make guesses for other systems. */
3266 # define FILE_base(f) ((f)->_base)
3267 # define FILE_ptr(f) ((f)->_ptr)
3268 # define FILE_cnt(f) ((f)->_cnt)
3269 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3280 register STDCHAR *s;
3286 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3288 STACKED_FTEST_CHECK;
3290 if (PL_op->op_flags & OPf_REF)
3292 else gv = MAYBE_DEREF_GV_nomg(TOPs);
3296 if (gv == PL_defgv) {
3298 io = GvIO(PL_statgv);
3301 goto really_filename;
3306 PL_laststatval = -1;
3307 sv_setpvs(PL_statname, "");
3308 io = GvIO(PL_statgv);
3310 if (io && IoIFP(io)) {
3311 if (! PerlIO_has_base(IoIFP(io)))
3312 DIE(aTHX_ "-T and -B not implemented on filehandles");
3313 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3314 if (PL_laststatval < 0)
3316 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3317 if (PL_op->op_type == OP_FTTEXT)
3322 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3323 i = PerlIO_getc(IoIFP(io));
3325 (void)PerlIO_ungetc(IoIFP(io),i);
3327 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3329 len = PerlIO_get_bufsiz(IoIFP(io));
3330 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3331 /* sfio can have large buffers - limit to 512 */
3336 report_evil_fh(cGVOP_gv);
3337 SETERRNO(EBADF,RMS_IFI);
3345 PL_laststype = OP_STAT;
3346 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3347 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3348 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3350 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3353 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3354 if (PL_laststatval < 0) {
3355 (void)PerlIO_close(fp);
3358 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3359 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3360 (void)PerlIO_close(fp);
3362 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3363 RETPUSHNO; /* special case NFS directories */
3364 RETPUSHYES; /* null file is anything */
3369 /* now scan s to look for textiness */
3370 /* XXX ASCII dependent code */
3372 #if defined(DOSISH) || defined(USEMYBINMODE)
3373 /* ignore trailing ^Z on short files */
3374 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3378 for (i = 0; i < len; i++, s++) {
3379 if (!*s) { /* null never allowed in text */
3384 else if (!(isPRINT(*s) || isSPACE(*s)))
3387 else if (*s & 128) {
3389 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3392 /* utf8 characters don't count as odd */
3393 if (UTF8_IS_START(*s)) {
3394 int ulen = UTF8SKIP(s);
3395 if (ulen < len - i) {
3397 for (j = 1; j < ulen; j++) {
3398 if (!UTF8_IS_CONTINUATION(s[j]))
3401 --ulen; /* loop does extra increment */
3411 *s != '\n' && *s != '\r' && *s != '\b' &&
3412 *s != '\t' && *s != '\f' && *s != 27)
3417 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3428 const char *tmps = NULL;
3432 SV * const sv = POPs;
3433 if (PL_op->op_flags & OPf_SPECIAL) {
3434 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3436 else if (!(gv = MAYBE_DEREF_GV(sv)))
3437 tmps = SvPV_nomg_const_nolen(sv);
3440 if( !gv && (!tmps || !*tmps) ) {
3441 HV * const table = GvHVn(PL_envgv);
3444 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3445 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3447 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3452 deprecate("chdir('') or chdir(undef) as chdir()");
3453 tmps = SvPV_nolen_const(*svp);
3457 TAINT_PROPER("chdir");
3462 TAINT_PROPER("chdir");
3465 IO* const io = GvIO(gv);
3468 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3469 } else if (IoIFP(io)) {
3470 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3474 SETERRNO(EBADF, RMS_IFI);
3480 SETERRNO(EBADF,RMS_IFI);
3484 DIE(aTHX_ PL_no_func, "fchdir");
3488 PUSHi( PerlDir_chdir(tmps) >= 0 );
3490 /* Clear the DEFAULT element of ENV so we'll get the new value
3492 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3499 dVAR; dSP; dMARK; dTARGET;
3500 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3511 char * const tmps = POPpx;
3512 TAINT_PROPER("chroot");
3513 PUSHi( chroot(tmps) >= 0 );
3516 DIE(aTHX_ PL_no_func, "chroot");
3524 const char * const tmps2 = POPpconstx;
3525 const char * const tmps = SvPV_nolen_const(TOPs);
3526 TAINT_PROPER("rename");
3528 anum = PerlLIO_rename(tmps, tmps2);
3530 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3531 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3534 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3535 (void)UNLINK(tmps2);
3536 if (!(anum = link(tmps, tmps2)))
3537 anum = UNLINK(tmps);
3545 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3549 const int op_type = PL_op->op_type;
3553 if (op_type == OP_LINK)
3554 DIE(aTHX_ PL_no_func, "link");
3556 # ifndef HAS_SYMLINK
3557 if (op_type == OP_SYMLINK)
3558 DIE(aTHX_ PL_no_func, "symlink");
3562 const char * const tmps2 = POPpconstx;
3563 const char * const tmps = SvPV_nolen_const(TOPs);
3564 TAINT_PROPER(PL_op_desc[op_type]);
3566 # if defined(HAS_LINK)
3567 # if defined(HAS_SYMLINK)
3568 /* Both present - need to choose which. */
3569 (op_type == OP_LINK) ?
3570 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3572 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3573 PerlLIO_link(tmps, tmps2);
3576 # if defined(HAS_SYMLINK)
3577 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3578 symlink(tmps, tmps2);
3583 SETi( result >= 0 );
3590 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3601 char buf[MAXPATHLEN];
3604 #ifndef INCOMPLETE_TAINTS
3608 len = readlink(tmps, buf, sizeof(buf) - 1);
3615 RETSETUNDEF; /* just pretend it's a normal file */
3619 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3621 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3623 char * const save_filename = filename;
3628 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3630 PERL_ARGS_ASSERT_DOONELINER;
3632 Newx(cmdline, size, char);
3633 my_strlcpy(cmdline, cmd, size);
3634 my_strlcat(cmdline, " ", size);
3635 for (s = cmdline + strlen(cmdline); *filename; ) {
3639 if (s - cmdline < size)
3640 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3641 myfp = PerlProc_popen(cmdline, "r");
3645 SV * const tmpsv = sv_newmortal();
3646 /* Need to save/restore 'PL_rs' ?? */
3647 s = sv_gets(tmpsv, myfp, 0);
3648 (void)PerlProc_pclose(myfp);
3652 #ifdef HAS_SYS_ERRLIST
3657 /* you don't see this */
3658 const char * const errmsg =
3659 #ifdef HAS_SYS_ERRLIST
3667 if (instr(s, errmsg)) {
3674 #define EACCES EPERM
3676 if (instr(s, "cannot make"))
3677 SETERRNO(EEXIST,RMS_FEX);
3678 else if (instr(s, "existing file"))
3679 SETERRNO(EEXIST,RMS_FEX);
3680 else if (instr(s, "ile exists"))
3681 SETERRNO(EEXIST,RMS_FEX);
3682 else if (instr(s, "non-exist"))
3683 SETERRNO(ENOENT,RMS_FNF);
3684 else if (instr(s, "does not exist"))
3685 SETERRNO(ENOENT,RMS_FNF);
3686 else if (instr(s, "not empty"))
3687 SETERRNO(EBUSY,SS_DEVOFFLINE);
3688 else if (instr(s, "cannot access"))
3689 SETERRNO(EACCES,RMS_PRV);
3691 SETERRNO(EPERM,RMS_PRV);
3694 else { /* some mkdirs return no failure indication */
3695 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3696 if (PL_op->op_type == OP_RMDIR)
3701 SETERRNO(EACCES,RMS_PRV); /* a guess */
3710 /* This macro removes trailing slashes from a directory name.
3711 * Different operating and file systems take differently to
3712 * trailing slashes. According to POSIX 1003.1 1996 Edition
3713 * any number of trailing slashes should be allowed.
3714 * Thusly we snip them away so that even non-conforming
3715 * systems are happy.
3716 * We should probably do this "filtering" for all
3717 * the functions that expect (potentially) directory names:
3718 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3719 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3721 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3722 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3725 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3726 (tmps) = savepvn((tmps), (len)); \
3736 const int mode = (MAXARG > 1 && (TOPs||((void)POPs,0))) ? POPi : 0777;
3738 TRIMSLASHES(tmps,len,copy);
3740 TAINT_PROPER("mkdir");
3742 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3746 SETi( dooneliner("mkdir", tmps) );
3747 oldumask = PerlLIO_umask(0);
3748 PerlLIO_umask(oldumask);
3749 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3764 TRIMSLASHES(tmps,len,copy);
3765 TAINT_PROPER("rmdir");
3767 SETi( PerlDir_rmdir(tmps) >= 0 );
3769 SETi( dooneliner("rmdir", tmps) );
3776 /* Directory calls. */
3780 #if defined(Direntry_t) && defined(HAS_READDIR)
3782 const char * const dirname = POPpconstx;
3783 GV * const gv = MUTABLE_GV(POPs);
3784 register IO * const io = GvIOn(gv);
3789 if ((IoIFP(io) || IoOFP(io)))
3790 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3791 "Opening filehandle %"HEKf" also as a directory",
3792 HEKfARG(GvENAME_HEK(gv)) );
3794 PerlDir_close(IoDIRP(io));
3795 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3801 SETERRNO(EBADF,RMS_DIR);
3804 DIE(aTHX_ PL_no_dir_func, "opendir");
3810 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3811 DIE(aTHX_ PL_no_dir_func, "readdir");
3813 #if !defined(I_DIRENT) && !defined(VMS)
3814 Direntry_t *readdir (DIR *);
3820 const I32 gimme = GIMME;
3821 GV * const gv = MUTABLE_GV(POPs);
3822 register const Direntry_t *dp;
3823 register IO * const io = GvIOn(gv);
3825 if (!io || !IoDIRP(io)) {
3826 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3827 "readdir() attempted on invalid dirhandle %"HEKf,
3828 HEKfARG(GvENAME_HEK(gv)));
3833 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3837 sv = newSVpvn(dp->d_name, dp->d_namlen);
3839 sv = newSVpv(dp->d_name, 0);
3841 #ifndef INCOMPLETE_TAINTS
3842 if (!(IoFLAGS(io) & IOf_UNTAINT))
3846 } while (gimme == G_ARRAY);
3848 if (!dp && gimme != G_ARRAY)
3855 SETERRNO(EBADF,RMS_ISI);
3856 if (GIMME == G_ARRAY)
3865 #if defined(HAS_TELLDIR) || defined(telldir)
3867 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3868 /* XXX netbsd still seemed to.
3869 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3870 --JHI 1999-Feb-02 */
3871 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3872 long telldir (DIR *);
3874 GV * const gv = MUTABLE_GV(POPs);
3875 register IO * const io = GvIOn(gv);
3877 if (!io || !IoDIRP(io)) {
3878 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3879 "telldir() attempted on invalid dirhandle %"HEKf,
3880 HEKfARG(GvENAME_HEK(gv)));
3884 PUSHi( PerlDir_tell(IoDIRP(io)) );
3888 SETERRNO(EBADF,RMS_ISI);
3891 DIE(aTHX_ PL_no_dir_func, "telldir");
3897 #if defined(HAS_SEEKDIR) || defined(seekdir)
3899 const long along = POPl;
3900 GV * const gv = MUTABLE_GV(POPs);
3901 register IO * const io = GvIOn(gv);
3903 if (!io || !IoDIRP(io)) {
3904 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3905 "seekdir() attempted on invalid dirhandle %"HEKf,
3906 HEKfARG(GvENAME_HEK(gv)));
3909 (void)PerlDir_seek(IoDIRP(io), along);
3914 SETERRNO(EBADF,RMS_ISI);
3917 DIE(aTHX_ PL_no_dir_func, "seekdir");
3923 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3925 GV * const gv = MUTABLE_GV(POPs);
3926 register IO * const io = GvIOn(gv);
3928 if (!io || !IoDIRP(io)) {
3929 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3930 "rewinddir() attempted on invalid dirhandle %"HEKf,
3931 HEKfARG(GvENAME_HEK(gv)));
3934 (void)PerlDir_rewind(IoDIRP(io));
3938 SETERRNO(EBADF,RMS_ISI);
3941 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3947 #if defined(Direntry_t) && defined(HAS_READDIR)
3949 GV * const gv = MUTABLE_GV(POPs);
3950 register IO * const io = GvIOn(gv);
3952 if (!io || !IoDIRP(io)) {
3953 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3954 "closedir() attempted on invalid dirhandle %"HEKf,
3955 HEKfARG(GvENAME_HEK(gv)));
3958 #ifdef VOID_CLOSEDIR
3959 PerlDir_close(IoDIRP(io));
3961 if (PerlDir_close(IoDIRP(io)) < 0) {
3962 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3971 SETERRNO(EBADF,RMS_IFI);
3974 DIE(aTHX_ PL_no_dir_func, "closedir");
3978 /* Process control. */
3987 PERL_FLUSHALL_FOR_CHILD;
3988 childpid = PerlProc_fork();
3992 #ifdef THREADS_HAVE_PIDS
3993 PL_ppid = (IV)getppid();
3995 #ifdef PERL_USES_PL_PIDSTATUS
3996 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4002 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4007 PERL_FLUSHALL_FOR_CHILD;
4008 childpid = PerlProc_fork();
4014 DIE(aTHX_ PL_no_func, "fork");
4021 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4026 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4027 childpid = wait4pid(-1, &argflags, 0);
4029 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4034 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4035 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4036 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4038 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4043 DIE(aTHX_ PL_no_func, "wait");
4049 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4051 const int optype = POPi;
4052 const Pid_t pid = TOPi;
4056 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4057 result = wait4pid(pid, &argflags, optype);
4059 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4064 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4065 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4066 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4068 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4073 DIE(aTHX_ PL_no_func, "waitpid");
4079 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4080 #if defined(__LIBCATAMOUNT__)
4081 PL_statusvalue = -1;
4090 while (++MARK <= SP) {
4091 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4096 TAINT_PROPER("system");
4098 PERL_FLUSHALL_FOR_CHILD;
4099 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4105 if (PerlProc_pipe(pp) >= 0)
4107 while ((childpid = PerlProc_fork()) == -1) {
4108 if (errno != EAGAIN) {
4113 PerlLIO_close(pp[0]);
4114 PerlLIO_close(pp[1]);
4121 Sigsave_t ihand,qhand; /* place to save signals during system() */
4125 PerlLIO_close(pp[1]);
4127 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4128 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4131 result = wait4pid(childpid, &status, 0);
4132 } while (result == -1 && errno == EINTR);
4134 (void)rsignal_restore(SIGINT, &ihand);
4135 (void)rsignal_restore(SIGQUIT, &qhand);
4137 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4138 do_execfree(); /* free any memory child malloced on fork */
4145 while (n < sizeof(int)) {
4146 n1 = PerlLIO_read(pp[0],
4147 (void*)(((char*)&errkid)+n),
4153 PerlLIO_close(pp[0]);
4154 if (n) { /* Error */
4155 if (n != sizeof(int))
4156 DIE(aTHX_ "panic: kid popen errno read");
4157 errno = errkid; /* Propagate errno from kid */
4158 STATUS_NATIVE_CHILD_SET(-1);
4161 XPUSHi(STATUS_CURRENT);
4165 PerlLIO_close(pp[0]);
4166 #if defined(HAS_FCNTL) && defined(F_SETFD)
4167 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4170 if (PL_op->op_flags & OPf_STACKED) {
4171 SV * const really = *++MARK;
4172 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4174 else if (SP - MARK != 1)
4175 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4177 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4181 #else /* ! FORK or VMS or OS/2 */
4184 if (PL_op->op_flags & OPf_STACKED) {
4185 SV * const really = *++MARK;
4186 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4187 value = (I32)do_aspawn(really, MARK, SP);
4189 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4192 else if (SP - MARK != 1) {
4193 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4194 value = (I32)do_aspawn(NULL, MARK, SP);
4196 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4200 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4202 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4204 STATUS_NATIVE_CHILD_SET(value);
4207 XPUSHi(result ? value : STATUS_CURRENT);
4208 #endif /* !FORK or VMS or OS/2 */
4215 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4220 while (++MARK <= SP) {
4221 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4226 TAINT_PROPER("exec");
4228 PERL_FLUSHALL_FOR_CHILD;
4229 if (PL_op->op_flags & OPf_STACKED) {
4230 SV * const really = *++MARK;
4231 value = (I32)do_aexec(really, MARK, SP);
4233 else if (SP - MARK != 1)
4235 value = (I32)vms_do_aexec(NULL, MARK, SP);
4239 (void ) do_aspawn(NULL, MARK, SP);
4243 value = (I32)do_aexec(NULL, MARK, SP);
4248 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4251 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4254 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4268 # ifdef THREADS_HAVE_PIDS
4269 if (PL_ppid != 1 && getppid() == 1)
4270 /* maybe the parent process has died. Refresh ppid cache */
4274 XPUSHi( getppid() );
4278 DIE(aTHX_ PL_no_func, "getppid");
4288 (MAXARG < 1) ? 0 : TOPs ? SvIVx(POPs) : ((void)POPs, 0);
4291 pgrp = (I32)BSD_GETPGRP(pid);
4293 if (pid != 0 && pid != PerlProc_getpid())
4294 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4300 DIE(aTHX_ PL_no_func, "getpgrp()");
4310 pgrp = MAXARG == 2 && (TOPs||POPs) ? POPi : 0;
4311 if (MAXARG > 0) pid = TOPs && TOPi;
4317 TAINT_PROPER("setpgrp");
4319 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4321 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4322 || (pid != 0 && pid != PerlProc_getpid()))
4324 DIE(aTHX_ "setpgrp can't take arguments");
4326 SETi( setpgrp() >= 0 );
4327 #endif /* USE_BSDPGRP */
4330 DIE(aTHX_ PL_no_func, "setpgrp()");
4334 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4335 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4337 # define PRIORITY_WHICH_T(which) which
4342 #ifdef HAS_GETPRIORITY
4344 const int who = POPi;
4345 const int which = TOPi;
4346 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4349 DIE(aTHX_ PL_no_func, "getpriority()");
4355 #ifdef HAS_SETPRIORITY
4357 const int niceval = POPi;
4358 const int who = POPi;
4359 const int which = TOPi;
4360 TAINT_PROPER("setpriority");
4361 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4364 DIE(aTHX_ PL_no_func, "setpriority()");
4368 #undef PRIORITY_WHICH_T
4376 XPUSHn( time(NULL) );
4378 XPUSHi( time(NULL) );
4390 (void)PerlProc_times(&PL_timesbuf);
4392 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4393 /* struct tms, though same data */
4397 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4398 if (GIMME == G_ARRAY) {
4399 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4400 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4401 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4409 if (GIMME == G_ARRAY) {
4416 DIE(aTHX_ "times not implemented");
4418 #endif /* HAS_TIMES */
4421 /* The 32 bit int year limits the times we can represent to these
4422 boundaries with a few days wiggle room to account for time zone
4425 /* Sat Jan 3 00:00:00 -2147481748 */
4426 #define TIME_LOWER_BOUND -67768100567755200.0
4427 /* Sun Dec 29 12:00:00 2147483647 */
4428 #define TIME_UPPER_BOUND 67767976233316800.0
4437 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4438 static const char * const dayname[] =
4439 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4440 static const char * const monname[] =
4441 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4442 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4444 if (MAXARG < 1 || (!TOPs && ((void)POPs, 1))) {
4447 when = (Time64_T)now;
4450 NV input = Perl_floor(POPn);
4451 when = (Time64_T)input;
4452 if (when != input) {
4453 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4454 "%s(%.0" NVff ") too large", opname, input);
4458 if ( TIME_LOWER_BOUND > when ) {
4459 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4460 "%s(%.0" NVff ") too small", opname, when);
4463 else if( when > TIME_UPPER_BOUND ) {
4464 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4465 "%s(%.0" NVff ") too large", opname, when);
4469 if (PL_op->op_type == OP_LOCALTIME)
4470 err = S_localtime64_r(&when, &tmbuf);
4472 err = S_gmtime64_r(&when, &tmbuf);
4476 /* XXX %lld broken for quads */
4477 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4478 "%s(%.0" NVff ") failed", opname, when);
4481 if (GIMME != G_ARRAY) { /* scalar context */
4483 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4484 double year = (double)tmbuf.tm_year + 1900;
4491 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4492 dayname[tmbuf.tm_wday],
4493 monname[tmbuf.tm_mon],
4501 else { /* list context */
4507 mPUSHi(tmbuf.tm_sec);
4508 mPUSHi(tmbuf.tm_min);
4509 mPUSHi(tmbuf.tm_hour);
4510 mPUSHi(tmbuf.tm_mday);
4511 mPUSHi(tmbuf.tm_mon);
4512 mPUSHn(tmbuf.tm_year);
4513 mPUSHi(tmbuf.tm_wday);
4514 mPUSHi(tmbuf.tm_yday);
4515 mPUSHi(tmbuf.tm_isdst);
4526 anum = alarm((unsigned int)anum);
4532 DIE(aTHX_ PL_no_func, "alarm");
4543 (void)time(&lasttime);
4544 if (MAXARG < 1 || (!TOPs && !POPs))
4548 PerlProc_sleep((unsigned int)duration);
4551 XPUSHi(when - lasttime);
4555 /* Shared memory. */
4556 /* Merged with some message passing. */
4560 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4561 dVAR; dSP; dMARK; dTARGET;
4562 const int op_type = PL_op->op_type;
4567 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4570 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4573 value = (I32)(do_semop(MARK, SP) >= 0);
4576 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4584 return Perl_pp_semget(aTHX);
4592 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4593 dVAR; dSP; dMARK; dTARGET;
4594 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4601 DIE(aTHX_ "System V IPC is not implemented on this machine");
4607 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4608 dVAR; dSP; dMARK; dTARGET;
4609 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4617 PUSHp(zero_but_true, ZBTLEN);
4621 return Perl_pp_semget(aTHX);
4625 /* I can't const this further without getting warnings about the types of
4626 various arrays passed in from structures. */
4628 S_space_join_names_mortal(pTHX_ char *const *array)
4632 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4634 if (array && *array) {
4635 target = newSVpvs_flags("", SVs_TEMP);
4637 sv_catpv(target, *array);
4640 sv_catpvs(target, " ");
4643 target = sv_mortalcopy(&PL_sv_no);
4648 /* Get system info. */
4652 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4654 I32 which = PL_op->op_type;
4655 register char **elem;
4657 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4658 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4659 struct hostent *gethostbyname(Netdb_name_t);
4660 struct hostent *gethostent(void);
4662 struct hostent *hent = NULL;
4666 if (which == OP_GHBYNAME) {
4667 #ifdef HAS_GETHOSTBYNAME
4668 const char* const name = POPpbytex;
4669 hent = PerlSock_gethostbyname(name);
4671 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4674 else if (which == OP_GHBYADDR) {
4675 #ifdef HAS_GETHOSTBYADDR
4676 const int addrtype = POPi;
4677 SV * const addrsv = POPs;
4679 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4681 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4683 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4687 #ifdef HAS_GETHOSTENT
4688 hent = PerlSock_gethostent();
4690 DIE(aTHX_ PL_no_sock_func, "gethostent");
4693 #ifdef HOST_NOT_FOUND
4695 #ifdef USE_REENTRANT_API
4696 # ifdef USE_GETHOSTENT_ERRNO
4697 h_errno = PL_reentrant_buffer->_gethostent_errno;
4700 STATUS_UNIX_SET(h_errno);
4704 if (GIMME != G_ARRAY) {
4705 PUSHs(sv = sv_newmortal());
4707 if (which == OP_GHBYNAME) {
4709 sv_setpvn(sv, hent->h_addr, hent->h_length);
4712 sv_setpv(sv, (char*)hent->h_name);
4718 mPUSHs(newSVpv((char*)hent->h_name, 0));
4719 PUSHs(space_join_names_mortal(hent->h_aliases));
4720 mPUSHi(hent->h_addrtype);
4721 len = hent->h_length;
4724 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4725 mXPUSHp(*elem, len);
4729 mPUSHp(hent->h_addr, len);
4731 PUSHs(sv_mortalcopy(&PL_sv_no));
4736 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4742 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4744 I32 which = PL_op->op_type;
4746 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4747 struct netent *getnetbyaddr(Netdb_net_t, int);
4748 struct netent *getnetbyname(Netdb_name_t);
4749 struct netent *getnetent(void);
4751 struct netent *nent;
4753 if (which == OP_GNBYNAME){
4754 #ifdef HAS_GETNETBYNAME
4755 const char * const name = POPpbytex;
4756 nent = PerlSock_getnetbyname(name);
4758 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4761 else if (which == OP_GNBYADDR) {
4762 #ifdef HAS_GETNETBYADDR
4763 const int addrtype = POPi;
4764 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4765 nent = PerlSock_getnetbyaddr(addr, addrtype);
4767 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4771 #ifdef HAS_GETNETENT
4772 nent = PerlSock_getnetent();
4774 DIE(aTHX_ PL_no_sock_func, "getnetent");
4777 #ifdef HOST_NOT_FOUND
4779 #ifdef USE_REENTRANT_API
4780 # ifdef USE_GETNETENT_ERRNO
4781 h_errno = PL_reentrant_buffer->_getnetent_errno;
4784 STATUS_UNIX_SET(h_errno);
4789 if (GIMME != G_ARRAY) {
4790 PUSHs(sv = sv_newmortal());
4792 if (which == OP_GNBYNAME)
4793 sv_setiv(sv, (IV)nent->n_net);
4795 sv_setpv(sv, nent->n_name);
4801 mPUSHs(newSVpv(nent->n_name, 0));
4802 PUSHs(space_join_names_mortal(nent->n_aliases));
4803 mPUSHi(nent->n_addrtype);
4804 mPUSHi(nent->n_net);
4809 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4815 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4817 I32 which = PL_op->op_type;
4819 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4820 struct protoent *getprotobyname(Netdb_name_t);
4821 struct protoent *getprotobynumber(int);
4822 struct protoent *getprotoent(void);
4824 struct protoent *pent;
4826 if (which == OP_GPBYNAME) {
4827 #ifdef HAS_GETPROTOBYNAME
4828 const char* const name = POPpbytex;
4829 pent = PerlSock_getprotobyname(name);
4831 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4834 else if (which == OP_GPBYNUMBER) {
4835 #ifdef HAS_GETPROTOBYNUMBER
4836 const int number = POPi;
4837 pent = PerlSock_getprotobynumber(number);
4839 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4843 #ifdef HAS_GETPROTOENT
4844 pent = PerlSock_getprotoent();
4846 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4850 if (GIMME != G_ARRAY) {
4851 PUSHs(sv = sv_newmortal());
4853 if (which == OP_GPBYNAME)
4854 sv_setiv(sv, (IV)pent->p_proto);
4856 sv_setpv(sv, pent->p_name);
4862 mPUSHs(newSVpv(pent->p_name, 0));
4863 PUSHs(space_join_names_mortal(pent->p_aliases));
4864 mPUSHi(pent->p_proto);
4869 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4875 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4877 I32 which = PL_op->op_type;
4879 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4880 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4881 struct servent *getservbyport(int, Netdb_name_t);
4882 struct servent *getservent(void);
4884 struct servent *sent;
4886 if (which == OP_GSBYNAME) {
4887 #ifdef HAS_GETSERVBYNAME
4888 const char * const proto = POPpbytex;
4889 const char * const name = POPpbytex;
4890 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4892 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4895 else if (which == OP_GSBYPORT) {
4896 #ifdef HAS_GETSERVBYPORT
4897 const char * const proto = POPpbytex;
4898 unsigned short port = (unsigned short)POPu;
4900 port = PerlSock_htons(port);
4902 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4904 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4908 #ifdef HAS_GETSERVENT
4909 sent = PerlSock_getservent();
4911 DIE(aTHX_ PL_no_sock_func, "getservent");
4915 if (GIMME != G_ARRAY) {
4916 PUSHs(sv = sv_newmortal());
4918 if (which == OP_GSBYNAME) {
4920 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4922 sv_setiv(sv, (IV)(sent->s_port));
4926 sv_setpv(sv, sent->s_name);
4932 mPUSHs(newSVpv(sent->s_name, 0));
4933 PUSHs(space_join_names_mortal(sent->s_aliases));
4935 mPUSHi(PerlSock_ntohs(sent->s_port));
4937 mPUSHi(sent->s_port);
4939 mPUSHs(newSVpv(sent->s_proto, 0));
4944 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4951 const int stayopen = TOPi;
4952 switch(PL_op->op_type) {
4954 #ifdef HAS_SETHOSTENT
4955 PerlSock_sethostent(stayopen);
4957 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4960 #ifdef HAS_SETNETENT
4962 PerlSock_setnetent(stayopen);
4964 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4968 #ifdef HAS_SETPROTOENT
4969 PerlSock_setprotoent(stayopen);
4971 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4975 #ifdef HAS_SETSERVENT
4976 PerlSock_setservent(stayopen);
4978 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4988 switch(PL_op->op_type) {
4990 #ifdef HAS_ENDHOSTENT
4991 PerlSock_endhostent();
4993 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4997 #ifdef HAS_ENDNETENT
4998 PerlSock_endnetent();
5000 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5004 #ifdef HAS_ENDPROTOENT
5005 PerlSock_endprotoent();
5007 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5011 #ifdef HAS_ENDSERVENT
5012 PerlSock_endservent();
5014 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5018 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5021 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5025 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5028 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5032 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5035 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5039 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5042 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5054 I32 which = PL_op->op_type;
5056 struct passwd *pwent = NULL;
5058 * We currently support only the SysV getsp* shadow password interface.
5059 * The interface is declared in <shadow.h> and often one needs to link
5060 * with -lsecurity or some such.
5061 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5064 * AIX getpwnam() is clever enough to return the encrypted password
5065 * only if the caller (euid?) is root.
5067 * There are at least three other shadow password APIs. Many platforms
5068 * seem to contain more than one interface for accessing the shadow
5069 * password databases, possibly for compatibility reasons.
5070 * The getsp*() is by far he simplest one, the other two interfaces
5071 * are much more complicated, but also very similar to each other.
5076 * struct pr_passwd *getprpw*();
5077 * The password is in
5078 * char getprpw*(...).ufld.fd_encrypt[]
5079 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5084 * struct es_passwd *getespw*();
5085 * The password is in
5086 * char *(getespw*(...).ufld.fd_encrypt)
5087 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5090 * struct userpw *getuserpw();
5091 * The password is in
5092 * char *(getuserpw(...)).spw_upw_passwd
5093 * (but the de facto standard getpwnam() should work okay)
5095 * Mention I_PROT here so that Configure probes for it.
5097 * In HP-UX for getprpw*() the manual page claims that one should include
5098 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5099 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5100 * and pp_sys.c already includes <shadow.h> if there is such.
5102 * Note that <sys/security.h> is already probed for, but currently
5103 * it is only included in special cases.
5105 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5106 * be preferred interface, even though also the getprpw*() interface
5107 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5108 * One also needs to call set_auth_parameters() in main() before
5109 * doing anything else, whether one is using getespw*() or getprpw*().
5111 * Note that accessing the shadow databases can be magnitudes
5112 * slower than accessing the standard databases.
5117 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5118 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5119 * the pw_comment is left uninitialized. */
5120 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5126 const char* const name = POPpbytex;
5127 pwent = getpwnam(name);
5133 pwent = getpwuid(uid);
5137 # ifdef HAS_GETPWENT
5139 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5140 if (pwent) pwent = getpwnam(pwent->pw_name);
5143 DIE(aTHX_ PL_no_func, "getpwent");
5149 if (GIMME != G_ARRAY) {
5150 PUSHs(sv = sv_newmortal());
5152 if (which == OP_GPWNAM)
5153 # if Uid_t_sign <= 0
5154 sv_setiv(sv, (IV)pwent->pw_uid);
5156 sv_setuv(sv, (UV)pwent->pw_uid);
5159 sv_setpv(sv, pwent->pw_name);
5165 mPUSHs(newSVpv(pwent->pw_name, 0));
5169 /* If we have getspnam(), we try to dig up the shadow
5170 * password. If we are underprivileged, the shadow
5171 * interface will set the errno to EACCES or similar,
5172 * and return a null pointer. If this happens, we will
5173 * use the dummy password (usually "*" or "x") from the
5174 * standard password database.
5176 * In theory we could skip the shadow call completely
5177 * if euid != 0 but in practice we cannot know which
5178 * security measures are guarding the shadow databases
5179 * on a random platform.
5181 * Resist the urge to use additional shadow interfaces.
5182 * Divert the urge to writing an extension instead.
5185 /* Some AIX setups falsely(?) detect some getspnam(), which
5186 * has a different API than the Solaris/IRIX one. */
5187 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5190 const struct spwd * const spwent = getspnam(pwent->pw_name);
5191 /* Save and restore errno so that
5192 * underprivileged attempts seem
5193 * to have never made the unsuccessful
5194 * attempt to retrieve the shadow password. */
5196 if (spwent && spwent->sp_pwdp)
5197 sv_setpv(sv, spwent->sp_pwdp);
5201 if (!SvPOK(sv)) /* Use the standard password, then. */
5202 sv_setpv(sv, pwent->pw_passwd);
5205 # ifndef INCOMPLETE_TAINTS
5206 /* passwd is tainted because user himself can diddle with it.
5207 * admittedly not much and in a very limited way, but nevertheless. */
5211 # if Uid_t_sign <= 0
5212 mPUSHi(pwent->pw_uid);
5214 mPUSHu(pwent->pw_uid);
5217 # if Uid_t_sign <= 0
5218 mPUSHi(pwent->pw_gid);
5220 mPUSHu(pwent->pw_gid);
5222 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5223 * because of the poor interface of the Perl getpw*(),
5224 * not because there's some standard/convention saying so.
5225 * A better interface would have been to return a hash,
5226 * but we are accursed by our history, alas. --jhi. */
5228 mPUSHi(pwent->pw_change);
5231 mPUSHi(pwent->pw_quota);
5234 mPUSHs(newSVpv(pwent->pw_age, 0));
5236 /* I think that you can never get this compiled, but just in case. */
5237 PUSHs(sv_mortalcopy(&PL_sv_no));
5242 /* pw_class and pw_comment are mutually exclusive--.
5243 * see the above note for pw_change, pw_quota, and pw_age. */
5245 mPUSHs(newSVpv(pwent->pw_class, 0));
5248 mPUSHs(newSVpv(pwent->pw_comment, 0));
5250 /* I think that you can never get this compiled, but just in case. */
5251 PUSHs(sv_mortalcopy(&PL_sv_no));
5256 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5258 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5260 # ifndef INCOMPLETE_TAINTS
5261 /* pw_gecos is tainted because user himself can diddle with it. */
5265 mPUSHs(newSVpv(pwent->pw_dir, 0));
5267 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5268 # ifndef INCOMPLETE_TAINTS
5269 /* pw_shell is tainted because user himself can diddle with it. */
5274 mPUSHi(pwent->pw_expire);
5279 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5287 const I32 which = PL_op->op_type;
5288 const struct group *grent;
5290 if (which == OP_GGRNAM) {
5291 const char* const name = POPpbytex;
5292 grent = (const struct group *)getgrnam(name);
5294 else if (which == OP_GGRGID) {
5295 const Gid_t gid = POPi;
5296 grent = (const struct group *)getgrgid(gid);
5300 grent = (struct group *)getgrent();
5302 DIE(aTHX_ PL_no_func, "getgrent");
5306 if (GIMME != G_ARRAY) {
5307 SV * const sv = sv_newmortal();
5311 if (which == OP_GGRNAM)
5313 sv_setiv(sv, (IV)grent->gr_gid);
5315 sv_setuv(sv, (UV)grent->gr_gid);
5318 sv_setpv(sv, grent->gr_name);
5324 mPUSHs(newSVpv(grent->gr_name, 0));
5327 mPUSHs(newSVpv(grent->gr_passwd, 0));
5329 PUSHs(sv_mortalcopy(&PL_sv_no));
5333 mPUSHi(grent->gr_gid);
5335 mPUSHu(grent->gr_gid);
5338 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5339 /* In UNICOS/mk (_CRAYMPP) the multithreading
5340 * versions (getgrnam_r, getgrgid_r)
5341 * seem to return an illegal pointer
5342 * as the group members list, gr_mem.
5343 * getgrent() doesn't even have a _r version
5344 * but the gr_mem is poisonous anyway.
5345 * So yes, you cannot get the list of group
5346 * members if building multithreaded in UNICOS/mk. */
5347 PUSHs(space_join_names_mortal(grent->gr_mem));
5353 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5363 if (!(tmps = PerlProc_getlogin()))
5365 sv_setpv_mg(TARG, tmps);
5369 DIE(aTHX_ PL_no_func, "getlogin");
5373 /* Miscellaneous. */
5378 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5379 register I32 items = SP - MARK;
5380 unsigned long a[20];
5385 while (++MARK <= SP) {
5386 if (SvTAINTED(*MARK)) {
5392 TAINT_PROPER("syscall");
5395 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5396 * or where sizeof(long) != sizeof(char*). But such machines will
5397 * not likely have syscall implemented either, so who cares?
5399 while (++MARK <= SP) {
5400 if (SvNIOK(*MARK) || !i)
5401 a[i++] = SvIV(*MARK);
5402 else if (*MARK == &PL_sv_undef)
5405 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5411 DIE(aTHX_ "Too many args to syscall");
5413 DIE(aTHX_ "Too few args to syscall");
5415 retval = syscall(a[0]);
5418 retval = syscall(a[0],a[1]);
5421 retval = syscall(a[0],a[1],a[2]);
5424 retval = syscall(a[0],a[1],a[2],a[3]);
5427 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5430 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5433 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5436 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5440 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5443 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5446 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5450 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5454 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5458 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5459 a[10],a[11],a[12],a[13]);
5461 #endif /* atarist */
5467 DIE(aTHX_ PL_no_func, "syscall");
5471 #ifdef FCNTL_EMULATE_FLOCK
5473 /* XXX Emulate flock() with fcntl().
5474 What's really needed is a good file locking module.
5478 fcntl_emulate_flock(int fd, int operation)
5483 switch (operation & ~LOCK_NB) {
5485 flock.l_type = F_RDLCK;
5488 flock.l_type = F_WRLCK;
5491 flock.l_type = F_UNLCK;
5497 flock.l_whence = SEEK_SET;
5498 flock.l_start = flock.l_len = (Off_t)0;
5500 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5501 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5502 errno = EWOULDBLOCK;
5506 #endif /* FCNTL_EMULATE_FLOCK */
5508 #ifdef LOCKF_EMULATE_FLOCK
5510 /* XXX Emulate flock() with lockf(). This is just to increase
5511 portability of scripts. The calls are not completely
5512 interchangeable. What's really needed is a good file
5516 /* The lockf() constants might have been defined in <unistd.h>.
5517 Unfortunately, <unistd.h> causes troubles on some mixed
5518 (BSD/POSIX) systems, such as SunOS 4.1.3.
5520 Further, the lockf() constants aren't POSIX, so they might not be
5521 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5522 just stick in the SVID values and be done with it. Sigh.
5526 # define F_ULOCK 0 /* Unlock a previously locked region */
5529 # define F_LOCK 1 /* Lock a region for exclusive use */
5532 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5535 # define F_TEST 3 /* Test a region for other processes locks */
5539 lockf_emulate_flock(int fd, int operation)
5545 /* flock locks entire file so for lockf we need to do the same */
5546 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5547 if (pos > 0) /* is seekable and needs to be repositioned */
5548 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5549 pos = -1; /* seek failed, so don't seek back afterwards */
5552 switch (operation) {
5554 /* LOCK_SH - get a shared lock */
5556 /* LOCK_EX - get an exclusive lock */
5558 i = lockf (fd, F_LOCK, 0);
5561 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5562 case LOCK_SH|LOCK_NB:
5563 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5564 case LOCK_EX|LOCK_NB:
5565 i = lockf (fd, F_TLOCK, 0);
5567 if ((errno == EAGAIN) || (errno == EACCES))
5568 errno = EWOULDBLOCK;
5571 /* LOCK_UN - unlock (non-blocking is a no-op) */
5573 case LOCK_UN|LOCK_NB:
5574 i = lockf (fd, F_ULOCK, 0);
5577 /* Default - can't decipher operation */
5584 if (pos > 0) /* need to restore position of the handle */
5585 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5590 #endif /* LOCKF_EMULATE_FLOCK */
5594 * c-indentation-style: bsd
5596 * indent-tabs-mode: t
5599 * ex: set ts=8 sts=4 sw=4 noet: