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 if it is gmagical, to ensure that magic
359 * is called once and only once */
360 if (SvGMAGICAL(TOPm1s)) 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) */
379 /* Note that we only ever get here if File::Glob fails to load
380 * without at the same time croaking, for some reason, or if
381 * perl was built with PERL_EXTERNAL_GLOB */
383 ENTER_with_name("glob");
388 * The external globbing program may use things we can't control,
389 * so for security reasons we must assume the worst.
392 taint_proper(PL_no_security, "glob");
396 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
397 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
399 SAVESPTR(PL_rs); /* This is not permanent, either. */
400 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
403 *SvPVX(PL_rs) = '\n';
407 result = do_readline();
408 LEAVE_with_name("glob");
415 PL_last_in_gv = cGVOP_gv;
416 return do_readline();
426 do_join(TARG, &PL_sv_no, MARK, SP);
430 else if (SP == MARK) {
439 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
440 /* well-formed exception supplied */
442 else if (SvROK(ERRSV)) {
445 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
446 exsv = sv_mortalcopy(ERRSV);
447 sv_catpvs(exsv, "\t...caught");
450 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
452 if (SvROK(exsv) && !PL_warnhook)
453 Perl_warn(aTHX_ "%"SVf, SVfARG(exsv));
464 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
466 if (SP - MARK != 1) {
468 do_join(TARG, &PL_sv_no, MARK, SP);
476 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
477 /* well-formed exception supplied */
479 else if (SvROK(ERRSV)) {
481 if (sv_isobject(exsv)) {
482 HV * const stash = SvSTASH(SvRV(exsv));
483 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
485 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
486 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
493 call_sv(MUTABLE_SV(GvCV(gv)),
494 G_SCALAR|G_EVAL|G_KEEPERR);
495 exsv = sv_mortalcopy(*PL_stack_sp--);
499 else if (SvPOK(ERRSV) && SvCUR(ERRSV)) {
500 exsv = sv_mortalcopy(ERRSV);
501 sv_catpvs(exsv, "\t...propagated");
504 exsv = newSVpvs_flags("Died", SVs_TEMP);
512 Perl_tied_method(pTHX_ const char *const methname, SV **sp, SV *const sv,
513 const MAGIC *const mg, const U32 flags, U32 argc, ...)
518 PERL_ARGS_ASSERT_TIED_METHOD;
520 /* Ensure that our flag bits do not overlap. */
521 assert((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
522 assert((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
523 assert((TIED_METHOD_SAY & G_WANT) == 0);
525 PUTBACK; /* sp is at *foot* of args, so this pops args from old stack */
526 PUSHSTACKi(PERLSI_MAGIC);
527 EXTEND(SP, argc+1); /* object + args */
529 PUSHs(SvTIED_obj(sv, mg));
530 if (flags & TIED_METHOD_ARGUMENTS_ON_STACK) {
531 Copy(orig_sp + 2, sp + 1, argc, SV*); /* copy args to new stack */
535 const U32 mortalize_not_needed
536 = flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
538 va_start(args, argc);
540 SV *const arg = va_arg(args, SV *);
541 if(mortalize_not_needed)
550 ENTER_with_name("call_tied_method");
551 if (flags & TIED_METHOD_SAY) {
552 /* local $\ = "\n" */
553 SAVEGENERICSV(PL_ors_sv);
554 PL_ors_sv = newSVpvs("\n");
556 ret_args = call_method(methname, flags & G_WANT);
561 if (ret_args) { /* copy results back to original stack */
562 EXTEND(sp, ret_args);
563 Copy(orig_sp - ret_args + 1, sp + 1, ret_args, SV*);
567 LEAVE_with_name("call_tied_method");
571 #define tied_method0(a,b,c,d) \
572 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
573 #define tied_method1(a,b,c,d,e) \
574 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
575 #define tied_method2(a,b,c,d,e,f) \
576 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
589 GV * const gv = MUTABLE_GV(*++MARK);
591 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
592 DIE(aTHX_ PL_no_usym, "filehandle");
594 if ((io = GvIOp(gv))) {
596 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
599 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
600 "Opening dirhandle %"HEKf" also as a file",
601 HEKfARG(GvENAME_HEK(gv)));
603 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
605 /* Method's args are same as ours ... */
606 /* ... except handle is replaced by the object */
607 return Perl_tied_method(aTHX_ "OPEN", mark - 1, MUTABLE_SV(io), mg,
608 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
620 tmps = SvPV_const(sv, len);
621 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
624 PUSHi( (I32)PL_forkprocess );
625 else if (PL_forkprocess == 0) /* we are a new child */
636 MAXARG == 0 || (!TOPs && !POPs) ? PL_defoutgv : MUTABLE_GV(POPs);
642 IO * const io = GvIO(gv);
644 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
646 return tied_method0("CLOSE", SP, MUTABLE_SV(io), mg);
650 PUSHs(boolSV(do_close(gv, TRUE)));
663 GV * const wgv = MUTABLE_GV(POPs);
664 GV * const rgv = MUTABLE_GV(POPs);
669 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
670 DIE(aTHX_ PL_no_usym, "filehandle");
675 do_close(rgv, FALSE);
677 do_close(wgv, FALSE);
679 if (PerlProc_pipe(fd) < 0)
682 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
683 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
684 IoOFP(rstio) = IoIFP(rstio);
685 IoIFP(wstio) = IoOFP(wstio);
686 IoTYPE(rstio) = IoTYPE_RDONLY;
687 IoTYPE(wstio) = IoTYPE_WRONLY;
689 if (!IoIFP(rstio) || !IoOFP(wstio)) {
691 PerlIO_close(IoIFP(rstio));
693 PerlLIO_close(fd[0]);
695 PerlIO_close(IoOFP(wstio));
697 PerlLIO_close(fd[1]);
700 #if defined(HAS_FCNTL) && defined(F_SETFD)
701 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
702 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
709 DIE(aTHX_ PL_no_func, "pipe");
723 gv = MUTABLE_GV(POPs);
727 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
729 return tied_method0("FILENO", SP, MUTABLE_SV(io), mg);
732 if (!io || !(fp = IoIFP(io))) {
733 /* Can't do this because people seem to do things like
734 defined(fileno($foo)) to check whether $foo is a valid fh.
741 PUSHi(PerlIO_fileno(fp));
753 if (MAXARG < 1 || (!TOPs && !POPs)) {
754 anum = PerlLIO_umask(022);
755 /* setting it to 022 between the two calls to umask avoids
756 * to have a window where the umask is set to 0 -- meaning
757 * that another thread could create world-writeable files. */
759 (void)PerlLIO_umask(anum);
762 anum = PerlLIO_umask(POPi);
763 TAINT_PROPER("umask");
766 /* Only DIE if trying to restrict permissions on "user" (self).
767 * Otherwise it's harmless and more useful to just return undef
768 * since 'group' and 'other' concepts probably don't exist here. */
769 if (MAXARG >= 1 && (TOPs||POPs) && (POPi & 0700))
770 DIE(aTHX_ "umask not implemented");
771 XPUSHs(&PL_sv_undef);
790 gv = MUTABLE_GV(POPs);
794 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
796 /* This takes advantage of the implementation of the varargs
797 function, which I don't think that the optimiser will be able to
798 figure out. Although, as it's a static function, in theory it
800 return Perl_tied_method(aTHX_ "BINMODE", SP, MUTABLE_SV(io), mg,
801 G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
802 discp ? 1 : 0, discp);
806 if (!io || !(fp = IoIFP(io))) {
808 SETERRNO(EBADF,RMS_IFI);
815 const char *d = NULL;
818 d = SvPV_const(discp, len);
819 mode = mode_from_discipline(d, len);
820 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
821 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
822 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
843 const I32 markoff = MARK - PL_stack_base;
844 const char *methname;
845 int how = PERL_MAGIC_tied;
849 switch(SvTYPE(varsv)) {
851 methname = "TIEHASH";
852 HvEITER_set(MUTABLE_HV(varsv), 0);
855 methname = "TIEARRAY";
859 if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
860 methname = "TIEHANDLE";
861 how = PERL_MAGIC_tiedscalar;
862 /* For tied filehandles, we apply tiedscalar magic to the IO
863 slot of the GP rather than the GV itself. AMS 20010812 */
865 GvIOp(varsv) = newIO();
866 varsv = MUTABLE_SV(GvIOp(varsv));
871 methname = "TIESCALAR";
872 how = PERL_MAGIC_tiedscalar;
876 if (sv_isobject(*MARK)) { /* Calls GET magic. */
877 ENTER_with_name("call_TIE");
878 PUSHSTACKi(PERLSI_MAGIC);
880 EXTEND(SP,(I32)items);
884 call_method(methname, G_SCALAR);
887 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
888 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
889 * wrong error message, and worse case, supreme action at a distance.
890 * (Sorry obfuscation writers. You're not going to be given this one.)
892 stash = gv_stashsv(*MARK, 0);
893 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
894 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
895 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
897 ENTER_with_name("call_TIE");
898 PUSHSTACKi(PERLSI_MAGIC);
900 EXTEND(SP,(I32)items);
904 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
910 if (sv_isobject(sv)) {
911 sv_unmagic(varsv, how);
912 /* Croak if a self-tie on an aggregate is attempted. */
913 if (varsv == SvRV(sv) &&
914 (SvTYPE(varsv) == SVt_PVAV ||
915 SvTYPE(varsv) == SVt_PVHV))
917 "Self-ties of arrays and hashes are not supported");
918 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
920 LEAVE_with_name("call_TIE");
921 SP = PL_stack_base + markoff;
931 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
932 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
934 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
937 if ((mg = SvTIED_mg(sv, how))) {
938 SV * const obj = SvRV(SvTIED_obj(sv, mg));
940 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
942 if (gv && isGV(gv) && (cv = GvCV(gv))) {
944 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
945 mXPUSHi(SvREFCNT(obj) - 1);
947 ENTER_with_name("call_UNTIE");
948 call_sv(MUTABLE_SV(cv), G_VOID);
949 LEAVE_with_name("call_UNTIE");
952 else if (mg && SvREFCNT(obj) > 1) {
953 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
954 "untie attempted while %"UVuf" inner references still exist",
955 (UV)SvREFCNT(obj) - 1 ) ;
959 sv_unmagic(sv, how) ;
969 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
970 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
972 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
975 if ((mg = SvTIED_mg(sv, how))) {
976 SV *osv = SvTIED_obj(sv, mg);
977 if (osv == mg->mg_obj)
978 osv = sv_mortalcopy(osv);
992 HV * const hv = MUTABLE_HV(POPs);
993 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
994 stash = gv_stashsv(sv, 0);
995 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
997 require_pv("AnyDBM_File.pm");
999 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
1000 DIE(aTHX_ "No dbm on this machine");
1010 mPUSHu(O_RDWR|O_CREAT);
1015 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1018 if (!sv_isobject(TOPs)) {
1026 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1030 if (sv_isobject(TOPs)) {
1031 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1032 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1049 struct timeval timebuf;
1050 struct timeval *tbuf = &timebuf;
1053 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1058 # if BYTEORDER & 0xf0000
1059 # define ORDERBYTE (0x88888888 - BYTEORDER)
1061 # define ORDERBYTE (0x4444 - BYTEORDER)
1067 for (i = 1; i <= 3; i++) {
1068 SV * const sv = SP[i];
1072 if (SvREADONLY(sv)) {
1074 sv_force_normal_flags(sv, 0);
1075 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1076 Perl_croak_no_modify(aTHX);
1080 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
1081 "Non-string passed as bitmask");
1082 SvPV_force_nomg_nolen(sv); /* force string conversion */
1089 /* little endians can use vecs directly */
1090 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1097 masksize = NFDBITS / NBBY;
1099 masksize = sizeof(long); /* documented int, everyone seems to use long */
1101 Zero(&fd_sets[0], 4, char*);
1104 # if SELECT_MIN_BITS == 1
1105 growsize = sizeof(fd_set);
1107 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1108 # undef SELECT_MIN_BITS
1109 # define SELECT_MIN_BITS __FD_SETSIZE
1111 /* If SELECT_MIN_BITS is greater than one we most probably will want
1112 * to align the sizes with SELECT_MIN_BITS/8 because for example
1113 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1114 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1115 * on (sets/tests/clears bits) is 32 bits. */
1116 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1124 timebuf.tv_sec = (long)value;
1125 value -= (NV)timebuf.tv_sec;
1126 timebuf.tv_usec = (long)(value * 1000000.0);
1131 for (i = 1; i <= 3; i++) {
1133 if (!SvOK(sv) || SvCUR(sv) == 0) {
1140 Sv_Grow(sv, growsize);
1144 while (++j <= growsize) {
1148 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1150 Newx(fd_sets[i], growsize, char);
1151 for (offset = 0; offset < growsize; offset += masksize) {
1152 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1153 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1156 fd_sets[i] = SvPVX(sv);
1160 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1161 /* Can't make just the (void*) conditional because that would be
1162 * cpp #if within cpp macro, and not all compilers like that. */
1163 nfound = PerlSock_select(
1165 (Select_fd_set_t) fd_sets[1],
1166 (Select_fd_set_t) fd_sets[2],
1167 (Select_fd_set_t) fd_sets[3],
1168 (void*) tbuf); /* Workaround for compiler bug. */
1170 nfound = PerlSock_select(
1172 (Select_fd_set_t) fd_sets[1],
1173 (Select_fd_set_t) fd_sets[2],
1174 (Select_fd_set_t) fd_sets[3],
1177 for (i = 1; i <= 3; i++) {
1180 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1182 for (offset = 0; offset < growsize; offset += masksize) {
1183 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1184 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1186 Safefree(fd_sets[i]);
1193 if (GIMME == G_ARRAY && tbuf) {
1194 value = (NV)(timebuf.tv_sec) +
1195 (NV)(timebuf.tv_usec) / 1000000.0;
1200 DIE(aTHX_ "select not implemented");
1205 =for apidoc setdefout
1207 Sets PL_defoutgv, the default file handle for output, to the passed in
1208 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1209 count of the passed in typeglob is increased by one, and the reference count
1210 of the typeglob that PL_defoutgv points to is decreased by one.
1216 Perl_setdefout(pTHX_ GV *gv)
1219 SvREFCNT_inc_simple_void(gv);
1220 SvREFCNT_dec(PL_defoutgv);
1228 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1229 GV * egv = GvEGVx(PL_defoutgv);
1233 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1235 XPUSHs(&PL_sv_undef);
1237 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), HEK_UTF8(GvNAME_HEK(egv)) ? -GvNAMELEN(egv) : GvNAMELEN(egv), FALSE);
1238 if (gvp && *gvp == egv) {
1239 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1243 mXPUSHs(newRV(MUTABLE_SV(egv)));
1248 if (!GvIO(newdefout))
1249 gv_IOadd(newdefout);
1250 setdefout(newdefout);
1260 MAXARG==0 || (!TOPs && !POPs) ? PL_stdingv : MUTABLE_GV(POPs);
1261 IO *const io = GvIO(gv);
1267 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1269 const U32 gimme = GIMME_V;
1270 Perl_tied_method(aTHX_ "GETC", SP, MUTABLE_SV(io), mg, gimme, 0);
1271 if (gimme == G_SCALAR) {
1273 SvSetMagicSV_nosteal(TARG, TOPs);
1278 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1279 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1281 SETERRNO(EBADF,RMS_IFI);
1285 sv_setpvs(TARG, " ");
1286 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1287 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1288 /* Find out how many bytes the char needs */
1289 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1292 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1293 SvCUR_set(TARG,1+len);
1302 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1305 register PERL_CONTEXT *cx;
1306 const I32 gimme = GIMME_V;
1308 PERL_ARGS_ASSERT_DOFORM;
1310 if (cv && CvCLONE(cv))
1311 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1316 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1317 PUSHFORMAT(cx, retop);
1319 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1321 setdefout(gv); /* locally select filehandle so $% et al work */
1340 gv = MUTABLE_GV(POPs);
1354 goto not_a_format_reference;
1358 tmpsv = sv_newmortal();
1359 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1360 if (SvPOK(tmpsv) && *SvPV_nolen_const(tmpsv))
1361 DIE(aTHX_ "Undefined format \"%"SVf"\" called", SVfARG(tmpsv));
1363 not_a_format_reference:
1364 DIE(aTHX_ "Not a format reference");
1366 IoFLAGS(io) &= ~IOf_DIDTOP;
1367 return doform(cv,gv,PL_op->op_next);
1373 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1374 register IO * const io = GvIOp(gv);
1379 register PERL_CONTEXT *cx;
1382 if (!io || !(ofp = IoOFP(io)))
1385 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1386 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1388 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1389 PL_formtarget != PL_toptarget)
1393 if (!IoTOP_GV(io)) {
1396 if (!IoTOP_NAME(io)) {
1398 if (!IoFMT_NAME(io))
1399 IoFMT_NAME(io) = savepv(GvNAME(gv));
1400 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%"HEKf"_TOP",
1401 HEKfARG(GvNAME_HEK(gv))));
1402 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1403 if ((topgv && GvFORM(topgv)) ||
1404 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1405 IoTOP_NAME(io) = savesvpv(topname);
1407 IoTOP_NAME(io) = savepvs("top");
1409 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1410 if (!topgv || !GvFORM(topgv)) {
1411 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1414 IoTOP_GV(io) = topgv;
1416 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1417 I32 lines = IoLINES_LEFT(io);
1418 const char *s = SvPVX_const(PL_formtarget);
1419 if (lines <= 0) /* Yow, header didn't even fit!!! */
1421 while (lines-- > 0) {
1422 s = strchr(s, '\n');
1428 const STRLEN save = SvCUR(PL_formtarget);
1429 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1430 do_print(PL_formtarget, ofp);
1431 SvCUR_set(PL_formtarget, save);
1432 sv_chop(PL_formtarget, s);
1433 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1436 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1437 do_print(PL_formfeed, ofp);
1438 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1440 PL_formtarget = PL_toptarget;
1441 IoFLAGS(io) |= IOf_DIDTOP;
1444 DIE(aTHX_ "bad top format reference");
1447 SV * const sv = sv_newmortal();
1448 gv_efullname4(sv, fgv, NULL, FALSE);
1449 if (SvPOK(sv) && *SvPV_nolen_const(sv))
1450 DIE(aTHX_ "Undefined top format \"%"SVf"\" called", SVfARG(sv));
1452 DIE(aTHX_ "Undefined top format called");
1454 return doform(cv, gv, PL_op);
1458 POPBLOCK(cx,PL_curpm);
1460 retop = cx->blk_sub.retop;
1466 report_wrongway_fh(gv, '<');
1472 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1473 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1475 if (!do_print(PL_formtarget, fp))
1478 FmLINES(PL_formtarget) = 0;
1479 SvCUR_set(PL_formtarget, 0);
1480 *SvEND(PL_formtarget) = '\0';
1481 if (IoFLAGS(io) & IOf_FLUSH)
1482 (void)PerlIO_flush(fp);
1487 PL_formtarget = PL_bodytarget;
1489 PERL_UNUSED_VAR(newsp);
1490 PERL_UNUSED_VAR(gimme);
1496 dVAR; dSP; dMARK; dORIGMARK;
1501 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1502 IO *const io = GvIO(gv);
1505 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1507 if (MARK == ORIGMARK) {
1510 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1513 return Perl_tied_method(aTHX_ "PRINTF", mark - 1, MUTABLE_SV(io),
1515 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1523 SETERRNO(EBADF,RMS_IFI);
1526 else if (!(fp = IoOFP(io))) {
1528 report_wrongway_fh(gv, '<');
1529 else if (ckWARN(WARN_CLOSED))
1531 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1535 do_sprintf(sv, SP - MARK, MARK + 1);
1536 if (!do_print(sv, fp))
1539 if (IoFLAGS(io) & IOf_FLUSH)
1540 if (PerlIO_flush(fp) == EOF)
1551 PUSHs(&PL_sv_undef);
1559 const int perm = (MAXARG > 3 && (TOPs || POPs)) ? POPi : 0666;
1560 const int mode = POPi;
1561 SV * const sv = POPs;
1562 GV * const gv = MUTABLE_GV(POPs);
1565 /* Need TIEHANDLE method ? */
1566 const char * const tmps = SvPV_const(sv, len);
1567 /* FIXME? do_open should do const */
1568 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1569 IoLINES(GvIOp(gv)) = 0;
1573 PUSHs(&PL_sv_undef);
1580 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1594 bool charstart = FALSE;
1595 STRLEN charskip = 0;
1598 GV * const gv = MUTABLE_GV(*++MARK);
1599 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1600 && gv && (io = GvIO(gv)) )
1602 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1604 return Perl_tied_method(aTHX_ "READ", mark - 1, MUTABLE_SV(io), mg,
1605 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1614 sv_setpvs(bufsv, "");
1615 length = SvIVx(*++MARK);
1618 offset = SvIVx(*++MARK);
1622 if (!io || !IoIFP(io)) {
1624 SETERRNO(EBADF,RMS_IFI);
1627 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1628 buffer = SvPVutf8_force(bufsv, blen);
1629 /* UTF-8 may not have been set if they are all low bytes */
1634 buffer = SvPV_force(bufsv, blen);
1635 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1638 DIE(aTHX_ "Negative length");
1646 if (PL_op->op_type == OP_RECV) {
1647 Sock_size_t bufsize;
1648 char namebuf[MAXPATHLEN];
1649 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1650 bufsize = sizeof (struct sockaddr_in);
1652 bufsize = sizeof namebuf;
1654 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1658 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1659 /* 'offset' means 'flags' here */
1660 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1661 (struct sockaddr *)namebuf, &bufsize);
1664 /* MSG_TRUNC can give oversized count; quietly lose it */
1668 /* Bogus return without padding */
1669 bufsize = sizeof (struct sockaddr_in);
1671 SvCUR_set(bufsv, count);
1672 *SvEND(bufsv) = '\0';
1673 (void)SvPOK_only(bufsv);
1677 /* This should not be marked tainted if the fp is marked clean */
1678 if (!(IoFLAGS(io) & IOf_UNTAINT))
1679 SvTAINTED_on(bufsv);
1681 sv_setpvn(TARG, namebuf, bufsize);
1686 if (DO_UTF8(bufsv)) {
1687 /* offset adjust in characters not bytes */
1688 blen = sv_len_utf8(bufsv);
1691 if (-offset > (SSize_t)blen)
1692 DIE(aTHX_ "Offset outside string");
1695 if (DO_UTF8(bufsv)) {
1696 /* convert offset-as-chars to offset-as-bytes */
1697 if (offset >= (int)blen)
1698 offset += SvCUR(bufsv) - blen;
1700 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1703 orig_size = SvCUR(bufsv);
1704 /* Allocating length + offset + 1 isn't perfect in the case of reading
1705 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1707 (should be 2 * length + offset + 1, or possibly something longer if
1708 PL_encoding is true) */
1709 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1710 if (offset > 0 && offset > (SSize_t)orig_size) { /* Zero any newly allocated space */
1711 Zero(buffer+orig_size, offset-orig_size, char);
1713 buffer = buffer + offset;
1715 read_target = bufsv;
1717 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1718 concatenate it to the current buffer. */
1720 /* Truncate the existing buffer to the start of where we will be
1722 SvCUR_set(bufsv, offset);
1724 read_target = sv_newmortal();
1725 SvUPGRADE(read_target, SVt_PV);
1726 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1729 if (PL_op->op_type == OP_SYSREAD) {
1730 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1731 if (IoTYPE(io) == IoTYPE_SOCKET) {
1732 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1738 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1743 #ifdef HAS_SOCKET__bad_code_maybe
1744 if (IoTYPE(io) == IoTYPE_SOCKET) {
1745 Sock_size_t bufsize;
1746 char namebuf[MAXPATHLEN];
1747 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1748 bufsize = sizeof (struct sockaddr_in);
1750 bufsize = sizeof namebuf;
1752 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1753 (struct sockaddr *)namebuf, &bufsize);
1758 count = PerlIO_read(IoIFP(io), buffer, length);
1759 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1760 if (count == 0 && PerlIO_error(IoIFP(io)))
1764 if (IoTYPE(io) == IoTYPE_WRONLY)
1765 report_wrongway_fh(gv, '>');
1768 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1769 *SvEND(read_target) = '\0';
1770 (void)SvPOK_only(read_target);
1771 if (fp_utf8 && !IN_BYTES) {
1772 /* Look at utf8 we got back and count the characters */
1773 const char *bend = buffer + count;
1774 while (buffer < bend) {
1776 skip = UTF8SKIP(buffer);
1779 if (buffer - charskip + skip > bend) {
1780 /* partial character - try for rest of it */
1781 length = skip - (bend-buffer);
1782 offset = bend - SvPVX_const(bufsv);
1794 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1795 provided amount read (count) was what was requested (length)
1797 if (got < wanted && count == length) {
1798 length = wanted - got;
1799 offset = bend - SvPVX_const(bufsv);
1802 /* return value is character count */
1806 else if (buffer_utf8) {
1807 /* Let svcatsv upgrade the bytes we read in to utf8.
1808 The buffer is a mortal so will be freed soon. */
1809 sv_catsv_nomg(bufsv, read_target);
1812 /* This should not be marked tainted if the fp is marked clean */
1813 if (!(IoFLAGS(io) & IOf_UNTAINT))
1814 SvTAINTED_on(bufsv);
1826 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1831 STRLEN orig_blen_bytes;
1832 const int op_type = PL_op->op_type;
1835 GV *const gv = MUTABLE_GV(*++MARK);
1836 IO *const io = GvIO(gv);
1838 if (op_type == OP_SYSWRITE && io) {
1839 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1841 if (MARK == SP - 1) {
1843 mXPUSHi(sv_len(sv));
1847 return Perl_tied_method(aTHX_ "WRITE", mark - 1, MUTABLE_SV(io), mg,
1848 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1858 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1860 if (io && IoIFP(io))
1861 report_wrongway_fh(gv, '<');
1864 SETERRNO(EBADF,RMS_IFI);
1868 /* Do this first to trigger any overloading. */
1869 buffer = SvPV_const(bufsv, blen);
1870 orig_blen_bytes = blen;
1871 doing_utf8 = DO_UTF8(bufsv);
1873 if (PerlIO_isutf8(IoIFP(io))) {
1874 if (!SvUTF8(bufsv)) {
1875 /* We don't modify the original scalar. */
1876 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1877 buffer = (char *) tmpbuf;
1881 else if (doing_utf8) {
1882 STRLEN tmplen = blen;
1883 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1886 buffer = (char *) tmpbuf;
1890 assert((char *)result == buffer);
1891 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1896 if (op_type == OP_SEND) {
1897 const int flags = SvIVx(*++MARK);
1900 char * const sockbuf = SvPVx(*++MARK, mlen);
1901 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1902 flags, (struct sockaddr *)sockbuf, mlen);
1906 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1912 Size_t length = 0; /* This length is in characters. */
1918 /* The SV is bytes, and we've had to upgrade it. */
1919 blen_chars = orig_blen_bytes;
1921 /* The SV really is UTF-8. */
1922 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1923 /* Don't call sv_len_utf8 again because it will call magic
1924 or overloading a second time, and we might get back a
1925 different result. */
1926 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1928 /* It's safe, and it may well be cached. */
1929 blen_chars = sv_len_utf8(bufsv);
1937 length = blen_chars;
1939 #if Size_t_size > IVSIZE
1940 length = (Size_t)SvNVx(*++MARK);
1942 length = (Size_t)SvIVx(*++MARK);
1944 if ((SSize_t)length < 0) {
1946 DIE(aTHX_ "Negative length");
1951 offset = SvIVx(*++MARK);
1953 if (-offset > (IV)blen_chars) {
1955 DIE(aTHX_ "Offset outside string");
1957 offset += blen_chars;
1958 } else if (offset > (IV)blen_chars) {
1960 DIE(aTHX_ "Offset outside string");
1964 if (length > blen_chars - offset)
1965 length = blen_chars - offset;
1967 /* Here we convert length from characters to bytes. */
1968 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1969 /* Either we had to convert the SV, or the SV is magical, or
1970 the SV has overloading, in which case we can't or mustn't
1971 or mustn't call it again. */
1973 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1974 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1976 /* It's a real UTF-8 SV, and it's not going to change under
1977 us. Take advantage of any cache. */
1979 I32 len_I32 = length;
1981 /* Convert the start and end character positions to bytes.
1982 Remember that the second argument to sv_pos_u2b is relative
1984 sv_pos_u2b(bufsv, &start, &len_I32);
1991 buffer = buffer+offset;
1993 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1994 if (IoTYPE(io) == IoTYPE_SOCKET) {
1995 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
2001 /* See the note at doio.c:do_print about filesize limits. --jhi */
2002 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
2011 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2014 #if Size_t_size > IVSIZE
2034 * in Perl 5.12 and later, the additional parameter is a bitmask:
2037 * 2 = eof() <- ARGV magic
2039 * I'll rely on the compiler's trace flow analysis to decide whether to
2040 * actually assign this out here, or punt it into the only block where it is
2041 * used. Doing it out here is DRY on the condition logic.
2046 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2052 if (PL_op->op_flags & OPf_SPECIAL) {
2053 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2057 gv = PL_last_in_gv; /* eof */
2065 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2066 return tied_method1("EOF", SP, MUTABLE_SV(io), mg, newSVuv(which));
2069 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2070 if (io && !IoIFP(io)) {
2071 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2073 IoFLAGS(io) &= ~IOf_START;
2074 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2076 sv_setpvs(GvSV(gv), "-");
2078 GvSV(gv) = newSVpvs("-");
2079 SvSETMAGIC(GvSV(gv));
2081 else if (!nextargv(gv))
2086 PUSHs(boolSV(do_eof(gv)));
2096 if (MAXARG != 0 && (TOPs || POPs))
2097 PL_last_in_gv = MUTABLE_GV(POPs);
2104 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2106 return tied_method0("TELL", SP, MUTABLE_SV(io), mg);
2111 SETERRNO(EBADF,RMS_IFI);
2116 #if LSEEKSIZE > IVSIZE
2117 PUSHn( do_tell(gv) );
2119 PUSHi( do_tell(gv) );
2127 const int whence = POPi;
2128 #if LSEEKSIZE > IVSIZE
2129 const Off_t offset = (Off_t)SvNVx(POPs);
2131 const Off_t offset = (Off_t)SvIVx(POPs);
2134 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2135 IO *const io = GvIO(gv);
2138 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2140 #if LSEEKSIZE > IVSIZE
2141 SV *const offset_sv = newSVnv((NV) offset);
2143 SV *const offset_sv = newSViv(offset);
2146 return tied_method2("SEEK", SP, MUTABLE_SV(io), mg, offset_sv,
2151 if (PL_op->op_type == OP_SEEK)
2152 PUSHs(boolSV(do_seek(gv, offset, whence)));
2154 const Off_t sought = do_sysseek(gv, offset, whence);
2156 PUSHs(&PL_sv_undef);
2158 SV* const sv = sought ?
2159 #if LSEEKSIZE > IVSIZE
2164 : newSVpvn(zero_but_true, ZBTLEN);
2175 /* There seems to be no consensus on the length type of truncate()
2176 * and ftruncate(), both off_t and size_t have supporters. In
2177 * general one would think that when using large files, off_t is
2178 * at least as wide as size_t, so using an off_t should be okay. */
2179 /* XXX Configure probe for the length type of *truncate() needed XXX */
2182 #if Off_t_size > IVSIZE
2187 /* Checking for length < 0 is problematic as the type might or
2188 * might not be signed: if it is not, clever compilers will moan. */
2189 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2192 SV * const sv = POPs;
2197 if ((tmpgv = PL_op->op_flags & OPf_SPECIAL
2198 ? gv_fetchsv(sv, 0, SVt_PVIO)
2199 : MAYBE_DEREF_GV(sv) )) {
2206 TAINT_PROPER("truncate");
2207 if (!(fp = IoIFP(io))) {
2213 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2215 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2221 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2222 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2223 goto do_ftruncate_io;
2226 const char * const name = SvPV_nomg_const_nolen(sv);
2227 TAINT_PROPER("truncate");
2229 if (truncate(name, len) < 0)
2233 const int tmpfd = PerlLIO_open(name, O_RDWR);
2238 if (my_chsize(tmpfd, len) < 0)
2240 PerlLIO_close(tmpfd);
2249 SETERRNO(EBADF,RMS_IFI);
2257 SV * const argsv = POPs;
2258 const unsigned int func = POPu;
2259 const int optype = PL_op->op_type;
2260 GV * const gv = MUTABLE_GV(POPs);
2261 IO * const io = gv ? GvIOn(gv) : NULL;
2265 if (!io || !argsv || !IoIFP(io)) {
2267 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2271 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2274 s = SvPV_force(argsv, len);
2275 need = IOCPARM_LEN(func);
2277 s = Sv_Grow(argsv, need + 1);
2278 SvCUR_set(argsv, need);
2281 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2284 retval = SvIV(argsv);
2285 s = INT2PTR(char*,retval); /* ouch */
2288 TAINT_PROPER(PL_op_desc[optype]);
2290 if (optype == OP_IOCTL)
2292 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2294 DIE(aTHX_ "ioctl is not implemented");
2298 DIE(aTHX_ "fcntl is not implemented");
2300 #if defined(OS2) && defined(__EMX__)
2301 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2303 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2307 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2309 if (s[SvCUR(argsv)] != 17)
2310 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2312 s[SvCUR(argsv)] = 0; /* put our null back */
2313 SvSETMAGIC(argsv); /* Assume it has changed */
2322 PUSHp(zero_but_true, ZBTLEN);
2333 const int argtype = POPi;
2334 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2335 IO *const io = GvIO(gv);
2336 PerlIO *const fp = io ? IoIFP(io) : NULL;
2338 /* XXX Looks to me like io is always NULL at this point */
2340 (void)PerlIO_flush(fp);
2341 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2346 SETERRNO(EBADF,RMS_IFI);
2351 DIE(aTHX_ PL_no_func, "flock()");
2362 const int protocol = POPi;
2363 const int type = POPi;
2364 const int domain = POPi;
2365 GV * const gv = MUTABLE_GV(POPs);
2366 register IO * const io = gv ? GvIOn(gv) : NULL;
2371 if (io && IoIFP(io))
2372 do_close(gv, FALSE);
2373 SETERRNO(EBADF,LIB_INVARG);
2378 do_close(gv, FALSE);
2380 TAINT_PROPER("socket");
2381 fd = PerlSock_socket(domain, type, protocol);
2384 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2385 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2386 IoTYPE(io) = IoTYPE_SOCKET;
2387 if (!IoIFP(io) || !IoOFP(io)) {
2388 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2389 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2390 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2393 #if defined(HAS_FCNTL) && defined(F_SETFD)
2394 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2398 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2407 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2409 const int protocol = POPi;
2410 const int type = POPi;
2411 const int domain = POPi;
2412 GV * const gv2 = MUTABLE_GV(POPs);
2413 GV * const gv1 = MUTABLE_GV(POPs);
2414 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2415 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2419 report_evil_fh(gv1);
2421 report_evil_fh(gv2);
2423 if (io1 && IoIFP(io1))
2424 do_close(gv1, FALSE);
2425 if (io2 && IoIFP(io2))
2426 do_close(gv2, FALSE);
2431 TAINT_PROPER("socketpair");
2432 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2434 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2435 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2436 IoTYPE(io1) = IoTYPE_SOCKET;
2437 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2438 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2439 IoTYPE(io2) = IoTYPE_SOCKET;
2440 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2441 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2442 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2443 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2444 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2445 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2446 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2449 #if defined(HAS_FCNTL) && defined(F_SETFD)
2450 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2451 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2456 DIE(aTHX_ PL_no_sock_func, "socketpair");
2465 SV * const addrsv = POPs;
2466 /* OK, so on what platform does bind modify addr? */
2468 GV * const gv = MUTABLE_GV(POPs);
2469 register IO * const io = GvIOn(gv);
2471 const int op_type = PL_op->op_type;
2473 if (!io || !IoIFP(io))
2476 addr = SvPV_const(addrsv, len);
2477 TAINT_PROPER(PL_op_desc[op_type]);
2478 if ((op_type == OP_BIND
2479 ? PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len)
2480 : PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len))
2488 SETERRNO(EBADF,SS_IVCHAN);
2495 const int backlog = POPi;
2496 GV * const gv = MUTABLE_GV(POPs);
2497 register IO * const io = gv ? GvIOn(gv) : NULL;
2499 if (!io || !IoIFP(io))
2502 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2509 SETERRNO(EBADF,SS_IVCHAN);
2518 char namebuf[MAXPATHLEN];
2519 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2520 Sock_size_t len = sizeof (struct sockaddr_in);
2522 Sock_size_t len = sizeof namebuf;
2524 GV * const ggv = MUTABLE_GV(POPs);
2525 GV * const ngv = MUTABLE_GV(POPs);
2534 if (!gstio || !IoIFP(gstio))
2538 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2541 /* Some platforms indicate zero length when an AF_UNIX client is
2542 * not bound. Simulate a non-zero-length sockaddr structure in
2544 namebuf[0] = 0; /* sun_len */
2545 namebuf[1] = AF_UNIX; /* sun_family */
2553 do_close(ngv, FALSE);
2554 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2555 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2556 IoTYPE(nstio) = IoTYPE_SOCKET;
2557 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2558 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2559 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2560 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2563 #if defined(HAS_FCNTL) && defined(F_SETFD)
2564 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2568 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2569 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2571 #ifdef __SCO_VERSION__
2572 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2575 PUSHp(namebuf, len);
2579 report_evil_fh(ggv);
2580 SETERRNO(EBADF,SS_IVCHAN);
2590 const int how = POPi;
2591 GV * const gv = MUTABLE_GV(POPs);
2592 register IO * const io = GvIOn(gv);
2594 if (!io || !IoIFP(io))
2597 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2602 SETERRNO(EBADF,SS_IVCHAN);
2609 const int optype = PL_op->op_type;
2610 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2611 const unsigned int optname = (unsigned int) POPi;
2612 const unsigned int lvl = (unsigned int) POPi;
2613 GV * const gv = MUTABLE_GV(POPs);
2614 register IO * const io = GvIOn(gv);
2618 if (!io || !IoIFP(io))
2621 fd = PerlIO_fileno(IoIFP(io));
2625 (void)SvPOK_only(sv);
2629 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2636 #if defined(__SYMBIAN32__)
2637 # define SETSOCKOPT_OPTION_VALUE_T void *
2639 # define SETSOCKOPT_OPTION_VALUE_T const char *
2641 /* XXX TODO: We need to have a proper type (a Configure probe,
2642 * etc.) for what the C headers think of the third argument of
2643 * setsockopt(), the option_value read-only buffer: is it
2644 * a "char *", or a "void *", const or not. Some compilers
2645 * don't take kindly to e.g. assuming that "char *" implicitly
2646 * promotes to a "void *", or to explicitly promoting/demoting
2647 * consts to non/vice versa. The "const void *" is the SUS
2648 * definition, but that does not fly everywhere for the above
2650 SETSOCKOPT_OPTION_VALUE_T buf;
2654 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2658 aint = (int)SvIV(sv);
2659 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2662 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2672 SETERRNO(EBADF,SS_IVCHAN);
2681 const int optype = PL_op->op_type;
2682 GV * const gv = MUTABLE_GV(POPs);
2683 register IO * const io = GvIOn(gv);
2688 if (!io || !IoIFP(io))
2691 sv = sv_2mortal(newSV(257));
2692 (void)SvPOK_only(sv);
2696 fd = PerlIO_fileno(IoIFP(io));
2698 case OP_GETSOCKNAME:
2699 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2702 case OP_GETPEERNAME:
2703 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2705 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2707 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";
2708 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2709 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2710 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2711 sizeof(u_short) + sizeof(struct in_addr))) {
2718 #ifdef BOGUS_GETNAME_RETURN
2719 /* Interactive Unix, getpeername() and getsockname()
2720 does not return valid namelen */
2721 if (len == BOGUS_GETNAME_RETURN)
2722 len = sizeof(struct sockaddr);
2731 SETERRNO(EBADF,SS_IVCHAN);
2750 if (PL_op->op_flags & OPf_REF ? (gv = cGVOP_gv, 1)
2751 : !!(sv=POPs, gv = MAYBE_DEREF_GV(sv))) {
2752 if (PL_op->op_type == OP_LSTAT) {
2753 if (gv != PL_defgv) {
2754 do_fstat_warning_check:
2755 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2756 "lstat() on filehandle %"SVf, SVfARG(gv
2757 ? sv_2mortal(newSVhek(GvENAME_HEK(gv)))
2759 } else if (PL_laststype != OP_LSTAT)
2760 /* diag_listed_as: The stat preceding %s wasn't an lstat */
2761 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2764 if (gv != PL_defgv) {
2765 PL_laststype = OP_STAT;
2767 sv_setpvs(PL_statname, "");
2774 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2775 } else if (IoDIRP(io)) {
2777 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2779 PL_laststatval = -1;
2785 if (PL_laststatval < 0) {
2791 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2792 io = MUTABLE_IO(SvRV(sv));
2793 if (PL_op->op_type == OP_LSTAT)
2794 goto do_fstat_warning_check;
2795 goto do_fstat_have_io;
2798 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
2800 PL_laststype = PL_op->op_type;
2801 if (PL_op->op_type == OP_LSTAT)
2802 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2804 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2805 if (PL_laststatval < 0) {
2806 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2807 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2813 if (gimme != G_ARRAY) {
2814 if (gimme != G_VOID)
2815 XPUSHs(boolSV(max));
2821 mPUSHi(PL_statcache.st_dev);
2822 #if ST_INO_SIZE > IVSIZE
2823 mPUSHn(PL_statcache.st_ino);
2825 # if ST_INO_SIGN <= 0
2826 mPUSHi(PL_statcache.st_ino);
2828 mPUSHu(PL_statcache.st_ino);
2831 mPUSHu(PL_statcache.st_mode);
2832 mPUSHu(PL_statcache.st_nlink);
2833 #if Uid_t_size > IVSIZE
2834 mPUSHn(PL_statcache.st_uid);
2836 # if Uid_t_sign <= 0
2837 mPUSHi(PL_statcache.st_uid);
2839 mPUSHu(PL_statcache.st_uid);
2842 #if Gid_t_size > IVSIZE
2843 mPUSHn(PL_statcache.st_gid);
2845 # if Gid_t_sign <= 0
2846 mPUSHi(PL_statcache.st_gid);
2848 mPUSHu(PL_statcache.st_gid);
2851 #ifdef USE_STAT_RDEV
2852 mPUSHi(PL_statcache.st_rdev);
2854 PUSHs(newSVpvs_flags("", SVs_TEMP));
2856 #if Off_t_size > IVSIZE
2857 mPUSHn(PL_statcache.st_size);
2859 mPUSHi(PL_statcache.st_size);
2862 mPUSHn(PL_statcache.st_atime);
2863 mPUSHn(PL_statcache.st_mtime);
2864 mPUSHn(PL_statcache.st_ctime);
2866 mPUSHi(PL_statcache.st_atime);
2867 mPUSHi(PL_statcache.st_mtime);
2868 mPUSHi(PL_statcache.st_ctime);
2870 #ifdef USE_STAT_BLOCKS
2871 mPUSHu(PL_statcache.st_blksize);
2872 mPUSHu(PL_statcache.st_blocks);
2874 PUSHs(newSVpvs_flags("", SVs_TEMP));
2875 PUSHs(newSVpvs_flags("", SVs_TEMP));
2881 #define tryAMAGICftest_MG(chr) STMT_START { \
2882 if ( (SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG)) \
2883 && PL_op->op_flags & OPf_KIDS \
2884 && S_try_amagic_ftest(aTHX_ chr)) \
2889 S_try_amagic_ftest(pTHX_ char chr) {
2892 SV* const arg = TOPs;
2899 const char tmpchr = chr;
2900 SV * const tmpsv = amagic_call(arg,
2901 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
2902 ftest_amg, AMGf_unary);
2909 if (PL_op->op_private & OPpFT_STACKING) {
2911 /* leave the object alone */
2923 /* This macro is used by the stacked filetest operators :
2924 * if the previous filetest failed, short-circuit and pass its value.
2925 * Else, discard it from the stack and continue. --rgs
2927 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2928 if (!SvTRUE(TOPs)) { RETURN; } \
2929 else { (void)POPs; PUTBACK; } \
2936 /* Not const, because things tweak this below. Not bool, because there's
2937 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2938 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2939 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2940 /* Giving some sort of initial value silences compilers. */
2942 int access_mode = R_OK;
2944 int access_mode = 0;
2947 /* access_mode is never used, but leaving use_access in makes the
2948 conditional compiling below much clearer. */
2951 Mode_t stat_mode = S_IRUSR;
2953 bool effective = FALSE;
2957 switch (PL_op->op_type) {
2958 case OP_FTRREAD: opchar = 'R'; break;
2959 case OP_FTRWRITE: opchar = 'W'; break;
2960 case OP_FTREXEC: opchar = 'X'; break;
2961 case OP_FTEREAD: opchar = 'r'; break;
2962 case OP_FTEWRITE: opchar = 'w'; break;
2963 case OP_FTEEXEC: opchar = 'x'; break;
2965 tryAMAGICftest_MG(opchar);
2967 STACKED_FTEST_CHECK;
2969 switch (PL_op->op_type) {
2971 #if !(defined(HAS_ACCESS) && defined(R_OK))
2977 #if defined(HAS_ACCESS) && defined(W_OK)
2982 stat_mode = S_IWUSR;
2986 #if defined(HAS_ACCESS) && defined(X_OK)
2991 stat_mode = S_IXUSR;
2995 #ifdef PERL_EFF_ACCESS
2998 stat_mode = S_IWUSR;
3002 #ifndef PERL_EFF_ACCESS
3009 #ifdef PERL_EFF_ACCESS
3014 stat_mode = S_IXUSR;
3020 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3021 const char *name = POPpx;
3023 # ifdef PERL_EFF_ACCESS
3024 result = PERL_EFF_ACCESS(name, access_mode);
3026 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3032 result = access(name, access_mode);
3034 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3045 result = my_stat_flags(0);
3049 if (cando(stat_mode, effective, &PL_statcache))
3058 const int op_type = PL_op->op_type;
3063 case OP_FTIS: opchar = 'e'; break;
3064 case OP_FTSIZE: opchar = 's'; break;
3065 case OP_FTMTIME: opchar = 'M'; break;
3066 case OP_FTCTIME: opchar = 'C'; break;
3067 case OP_FTATIME: opchar = 'A'; break;
3069 tryAMAGICftest_MG(opchar);
3071 STACKED_FTEST_CHECK;
3073 result = my_stat_flags(0);
3077 if (op_type == OP_FTIS)
3080 /* You can't dTARGET inside OP_FTIS, because you'll get
3081 "panic: pad_sv po" - the op is not flagged to have a target. */
3085 #if Off_t_size > IVSIZE
3086 PUSHn(PL_statcache.st_size);
3088 PUSHi(PL_statcache.st_size);
3092 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3095 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3098 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3112 switch (PL_op->op_type) {
3113 case OP_FTROWNED: opchar = 'O'; break;
3114 case OP_FTEOWNED: opchar = 'o'; break;
3115 case OP_FTZERO: opchar = 'z'; break;
3116 case OP_FTSOCK: opchar = 'S'; break;
3117 case OP_FTCHR: opchar = 'c'; break;
3118 case OP_FTBLK: opchar = 'b'; break;
3119 case OP_FTFILE: opchar = 'f'; break;
3120 case OP_FTDIR: opchar = 'd'; break;
3121 case OP_FTPIPE: opchar = 'p'; break;
3122 case OP_FTSUID: opchar = 'u'; break;
3123 case OP_FTSGID: opchar = 'g'; break;
3124 case OP_FTSVTX: opchar = 'k'; break;
3126 tryAMAGICftest_MG(opchar);
3128 STACKED_FTEST_CHECK;
3130 /* I believe that all these three are likely to be defined on most every
3131 system these days. */
3133 if(PL_op->op_type == OP_FTSUID) {
3134 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3140 if(PL_op->op_type == OP_FTSGID) {
3141 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3147 if(PL_op->op_type == OP_FTSVTX) {
3148 if ((PL_op->op_flags & OPf_REF) == 0 && (PL_op->op_private & OPpFT_STACKED) == 0)
3154 result = my_stat_flags(0);
3158 switch (PL_op->op_type) {
3160 if (PL_statcache.st_uid == PL_uid)
3164 if (PL_statcache.st_uid == PL_euid)
3168 if (PL_statcache.st_size == 0)
3172 if (S_ISSOCK(PL_statcache.st_mode))
3176 if (S_ISCHR(PL_statcache.st_mode))
3180 if (S_ISBLK(PL_statcache.st_mode))
3184 if (S_ISREG(PL_statcache.st_mode))
3188 if (S_ISDIR(PL_statcache.st_mode))
3192 if (S_ISFIFO(PL_statcache.st_mode))
3197 if (PL_statcache.st_mode & S_ISUID)
3203 if (PL_statcache.st_mode & S_ISGID)
3209 if (PL_statcache.st_mode & S_ISVTX)
3223 tryAMAGICftest_MG('l');
3224 STACKED_FTEST_CHECK;
3225 result = my_lstat_flags(0);
3230 if (S_ISLNK(PL_statcache.st_mode))
3245 tryAMAGICftest_MG('t');
3247 STACKED_FTEST_CHECK;
3249 if (PL_op->op_flags & OPf_REF)
3251 else if (!(gv = MAYBE_DEREF_GV_nomg(TOPs))) {
3253 name = SvPV_nomg(tmpsv, namelen);
3254 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3257 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3258 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3259 else if (tmpsv && SvOK(tmpsv)) {
3267 if (PerlLIO_isatty(fd))
3272 #if defined(atarist) /* this will work with atariST. Configure will
3273 make guesses for other systems. */
3274 # define FILE_base(f) ((f)->_base)
3275 # define FILE_ptr(f) ((f)->_ptr)
3276 # define FILE_cnt(f) ((f)->_cnt)
3277 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3288 register STDCHAR *s;
3294 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3296 STACKED_FTEST_CHECK;
3298 if (PL_op->op_flags & OPf_REF)
3300 else gv = MAYBE_DEREF_GV_nomg(TOPs);
3304 if (gv == PL_defgv) {
3306 io = GvIO(PL_statgv);
3309 goto really_filename;
3314 PL_laststatval = -1;
3315 sv_setpvs(PL_statname, "");
3316 io = GvIO(PL_statgv);
3318 if (io && IoIFP(io)) {
3319 if (! PerlIO_has_base(IoIFP(io)))
3320 DIE(aTHX_ "-T and -B not implemented on filehandles");
3321 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3322 if (PL_laststatval < 0)
3324 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3325 if (PL_op->op_type == OP_FTTEXT)
3330 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3331 i = PerlIO_getc(IoIFP(io));
3333 (void)PerlIO_ungetc(IoIFP(io),i);
3335 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3337 len = PerlIO_get_bufsiz(IoIFP(io));
3338 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3339 /* sfio can have large buffers - limit to 512 */
3344 report_evil_fh(cGVOP_gv);
3345 SETERRNO(EBADF,RMS_IFI);
3353 PL_laststype = OP_STAT;
3354 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3355 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3356 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3358 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3361 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3362 if (PL_laststatval < 0) {
3363 (void)PerlIO_close(fp);
3366 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3367 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3368 (void)PerlIO_close(fp);
3370 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3371 RETPUSHNO; /* special case NFS directories */
3372 RETPUSHYES; /* null file is anything */
3377 /* now scan s to look for textiness */
3378 /* XXX ASCII dependent code */
3380 #if defined(DOSISH) || defined(USEMYBINMODE)
3381 /* ignore trailing ^Z on short files */
3382 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3386 for (i = 0; i < len; i++, s++) {
3387 if (!*s) { /* null never allowed in text */
3392 else if (!(isPRINT(*s) || isSPACE(*s)))
3395 else if (*s & 128) {
3397 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3400 /* utf8 characters don't count as odd */
3401 if (UTF8_IS_START(*s)) {
3402 int ulen = UTF8SKIP(s);
3403 if (ulen < len - i) {
3405 for (j = 1; j < ulen; j++) {
3406 if (!UTF8_IS_CONTINUATION(s[j]))
3409 --ulen; /* loop does extra increment */
3419 *s != '\n' && *s != '\r' && *s != '\b' &&
3420 *s != '\t' && *s != '\f' && *s != 27)
3425 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3436 const char *tmps = NULL;
3440 SV * const sv = POPs;
3441 if (PL_op->op_flags & OPf_SPECIAL) {
3442 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3444 else if (!(gv = MAYBE_DEREF_GV(sv)))
3445 tmps = SvPV_nomg_const_nolen(sv);
3448 if( !gv && (!tmps || !*tmps) ) {
3449 HV * const table = GvHVn(PL_envgv);
3452 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3453 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3455 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3460 deprecate("chdir('') or chdir(undef) as chdir()");
3461 tmps = SvPV_nolen_const(*svp);
3465 TAINT_PROPER("chdir");
3470 TAINT_PROPER("chdir");
3473 IO* const io = GvIO(gv);
3476 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3477 } else if (IoIFP(io)) {
3478 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3482 SETERRNO(EBADF, RMS_IFI);
3488 SETERRNO(EBADF,RMS_IFI);
3492 DIE(aTHX_ PL_no_func, "fchdir");
3496 PUSHi( PerlDir_chdir(tmps) >= 0 );
3498 /* Clear the DEFAULT element of ENV so we'll get the new value
3500 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3507 dVAR; dSP; dMARK; dTARGET;
3508 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3519 char * const tmps = POPpx;
3520 TAINT_PROPER("chroot");
3521 PUSHi( chroot(tmps) >= 0 );
3524 DIE(aTHX_ PL_no_func, "chroot");
3532 const char * const tmps2 = POPpconstx;
3533 const char * const tmps = SvPV_nolen_const(TOPs);
3534 TAINT_PROPER("rename");
3536 anum = PerlLIO_rename(tmps, tmps2);
3538 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3539 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3542 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3543 (void)UNLINK(tmps2);
3544 if (!(anum = link(tmps, tmps2)))
3545 anum = UNLINK(tmps);
3553 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3557 const int op_type = PL_op->op_type;
3561 if (op_type == OP_LINK)
3562 DIE(aTHX_ PL_no_func, "link");
3564 # ifndef HAS_SYMLINK
3565 if (op_type == OP_SYMLINK)
3566 DIE(aTHX_ PL_no_func, "symlink");
3570 const char * const tmps2 = POPpconstx;
3571 const char * const tmps = SvPV_nolen_const(TOPs);
3572 TAINT_PROPER(PL_op_desc[op_type]);
3574 # if defined(HAS_LINK)
3575 # if defined(HAS_SYMLINK)
3576 /* Both present - need to choose which. */
3577 (op_type == OP_LINK) ?
3578 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3580 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3581 PerlLIO_link(tmps, tmps2);
3584 # if defined(HAS_SYMLINK)
3585 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3586 symlink(tmps, tmps2);
3591 SETi( result >= 0 );
3598 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3609 char buf[MAXPATHLEN];
3612 #ifndef INCOMPLETE_TAINTS
3616 len = readlink(tmps, buf, sizeof(buf) - 1);
3623 RETSETUNDEF; /* just pretend it's a normal file */
3627 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3629 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3631 char * const save_filename = filename;
3636 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3638 PERL_ARGS_ASSERT_DOONELINER;
3640 Newx(cmdline, size, char);
3641 my_strlcpy(cmdline, cmd, size);
3642 my_strlcat(cmdline, " ", size);
3643 for (s = cmdline + strlen(cmdline); *filename; ) {
3647 if (s - cmdline < size)
3648 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3649 myfp = PerlProc_popen(cmdline, "r");
3653 SV * const tmpsv = sv_newmortal();
3654 /* Need to save/restore 'PL_rs' ?? */
3655 s = sv_gets(tmpsv, myfp, 0);
3656 (void)PerlProc_pclose(myfp);
3660 #ifdef HAS_SYS_ERRLIST
3665 /* you don't see this */
3666 const char * const errmsg =
3667 #ifdef HAS_SYS_ERRLIST
3675 if (instr(s, errmsg)) {
3682 #define EACCES EPERM
3684 if (instr(s, "cannot make"))
3685 SETERRNO(EEXIST,RMS_FEX);
3686 else if (instr(s, "existing file"))
3687 SETERRNO(EEXIST,RMS_FEX);
3688 else if (instr(s, "ile exists"))
3689 SETERRNO(EEXIST,RMS_FEX);
3690 else if (instr(s, "non-exist"))
3691 SETERRNO(ENOENT,RMS_FNF);
3692 else if (instr(s, "does not exist"))
3693 SETERRNO(ENOENT,RMS_FNF);
3694 else if (instr(s, "not empty"))
3695 SETERRNO(EBUSY,SS_DEVOFFLINE);
3696 else if (instr(s, "cannot access"))
3697 SETERRNO(EACCES,RMS_PRV);
3699 SETERRNO(EPERM,RMS_PRV);
3702 else { /* some mkdirs return no failure indication */
3703 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3704 if (PL_op->op_type == OP_RMDIR)
3709 SETERRNO(EACCES,RMS_PRV); /* a guess */
3718 /* This macro removes trailing slashes from a directory name.
3719 * Different operating and file systems take differently to
3720 * trailing slashes. According to POSIX 1003.1 1996 Edition
3721 * any number of trailing slashes should be allowed.
3722 * Thusly we snip them away so that even non-conforming
3723 * systems are happy.
3724 * We should probably do this "filtering" for all
3725 * the functions that expect (potentially) directory names:
3726 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3727 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3729 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3730 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3733 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3734 (tmps) = savepvn((tmps), (len)); \
3744 const int mode = (MAXARG > 1 && (TOPs||((void)POPs,0))) ? POPi : 0777;
3746 TRIMSLASHES(tmps,len,copy);
3748 TAINT_PROPER("mkdir");
3750 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3754 SETi( dooneliner("mkdir", tmps) );
3755 oldumask = PerlLIO_umask(0);
3756 PerlLIO_umask(oldumask);
3757 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3772 TRIMSLASHES(tmps,len,copy);
3773 TAINT_PROPER("rmdir");
3775 SETi( PerlDir_rmdir(tmps) >= 0 );
3777 SETi( dooneliner("rmdir", tmps) );
3784 /* Directory calls. */
3788 #if defined(Direntry_t) && defined(HAS_READDIR)
3790 const char * const dirname = POPpconstx;
3791 GV * const gv = MUTABLE_GV(POPs);
3792 register IO * const io = GvIOn(gv);
3797 if ((IoIFP(io) || IoOFP(io)))
3798 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3799 "Opening filehandle %"HEKf" also as a directory",
3800 HEKfARG(GvENAME_HEK(gv)) );
3802 PerlDir_close(IoDIRP(io));
3803 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3809 SETERRNO(EBADF,RMS_DIR);
3812 DIE(aTHX_ PL_no_dir_func, "opendir");
3818 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3819 DIE(aTHX_ PL_no_dir_func, "readdir");
3821 #if !defined(I_DIRENT) && !defined(VMS)
3822 Direntry_t *readdir (DIR *);
3828 const I32 gimme = GIMME;
3829 GV * const gv = MUTABLE_GV(POPs);
3830 register const Direntry_t *dp;
3831 register IO * const io = GvIOn(gv);
3833 if (!io || !IoDIRP(io)) {
3834 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3835 "readdir() attempted on invalid dirhandle %"HEKf,
3836 HEKfARG(GvENAME_HEK(gv)));
3841 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3845 sv = newSVpvn(dp->d_name, dp->d_namlen);
3847 sv = newSVpv(dp->d_name, 0);
3849 #ifndef INCOMPLETE_TAINTS
3850 if (!(IoFLAGS(io) & IOf_UNTAINT))
3854 } while (gimme == G_ARRAY);
3856 if (!dp && gimme != G_ARRAY)
3863 SETERRNO(EBADF,RMS_ISI);
3864 if (GIMME == G_ARRAY)
3873 #if defined(HAS_TELLDIR) || defined(telldir)
3875 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3876 /* XXX netbsd still seemed to.
3877 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3878 --JHI 1999-Feb-02 */
3879 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3880 long telldir (DIR *);
3882 GV * const gv = MUTABLE_GV(POPs);
3883 register IO * const io = GvIOn(gv);
3885 if (!io || !IoDIRP(io)) {
3886 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3887 "telldir() attempted on invalid dirhandle %"HEKf,
3888 HEKfARG(GvENAME_HEK(gv)));
3892 PUSHi( PerlDir_tell(IoDIRP(io)) );
3896 SETERRNO(EBADF,RMS_ISI);
3899 DIE(aTHX_ PL_no_dir_func, "telldir");
3905 #if defined(HAS_SEEKDIR) || defined(seekdir)
3907 const long along = POPl;
3908 GV * const gv = MUTABLE_GV(POPs);
3909 register IO * const io = GvIOn(gv);
3911 if (!io || !IoDIRP(io)) {
3912 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3913 "seekdir() attempted on invalid dirhandle %"HEKf,
3914 HEKfARG(GvENAME_HEK(gv)));
3917 (void)PerlDir_seek(IoDIRP(io), along);
3922 SETERRNO(EBADF,RMS_ISI);
3925 DIE(aTHX_ PL_no_dir_func, "seekdir");
3931 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3933 GV * const gv = MUTABLE_GV(POPs);
3934 register IO * const io = GvIOn(gv);
3936 if (!io || !IoDIRP(io)) {
3937 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3938 "rewinddir() attempted on invalid dirhandle %"HEKf,
3939 HEKfARG(GvENAME_HEK(gv)));
3942 (void)PerlDir_rewind(IoDIRP(io));
3946 SETERRNO(EBADF,RMS_ISI);
3949 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3955 #if defined(Direntry_t) && defined(HAS_READDIR)
3957 GV * const gv = MUTABLE_GV(POPs);
3958 register IO * const io = GvIOn(gv);
3960 if (!io || !IoDIRP(io)) {
3961 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
3962 "closedir() attempted on invalid dirhandle %"HEKf,
3963 HEKfARG(GvENAME_HEK(gv)));
3966 #ifdef VOID_CLOSEDIR
3967 PerlDir_close(IoDIRP(io));
3969 if (PerlDir_close(IoDIRP(io)) < 0) {
3970 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3979 SETERRNO(EBADF,RMS_IFI);
3982 DIE(aTHX_ PL_no_dir_func, "closedir");
3986 /* Process control. */
3995 PERL_FLUSHALL_FOR_CHILD;
3996 childpid = PerlProc_fork();
4000 #ifdef THREADS_HAVE_PIDS
4001 PL_ppid = (IV)getppid();
4003 #ifdef PERL_USES_PL_PIDSTATUS
4004 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4010 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4015 PERL_FLUSHALL_FOR_CHILD;
4016 childpid = PerlProc_fork();
4022 DIE(aTHX_ PL_no_func, "fork");
4029 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4034 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4035 childpid = wait4pid(-1, &argflags, 0);
4037 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4042 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4043 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4044 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4046 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4051 DIE(aTHX_ PL_no_func, "wait");
4057 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4059 const int optype = POPi;
4060 const Pid_t pid = TOPi;
4064 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4065 result = wait4pid(pid, &argflags, optype);
4067 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4072 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4073 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4074 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4076 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4081 DIE(aTHX_ PL_no_func, "waitpid");
4087 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4088 #if defined(__LIBCATAMOUNT__)
4089 PL_statusvalue = -1;
4098 while (++MARK <= SP) {
4099 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4104 TAINT_PROPER("system");
4106 PERL_FLUSHALL_FOR_CHILD;
4107 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4113 if (PerlProc_pipe(pp) >= 0)
4115 while ((childpid = PerlProc_fork()) == -1) {
4116 if (errno != EAGAIN) {
4121 PerlLIO_close(pp[0]);
4122 PerlLIO_close(pp[1]);
4129 Sigsave_t ihand,qhand; /* place to save signals during system() */
4133 PerlLIO_close(pp[1]);
4135 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4136 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4139 result = wait4pid(childpid, &status, 0);
4140 } while (result == -1 && errno == EINTR);
4142 (void)rsignal_restore(SIGINT, &ihand);
4143 (void)rsignal_restore(SIGQUIT, &qhand);
4145 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4146 do_execfree(); /* free any memory child malloced on fork */
4153 while (n < sizeof(int)) {
4154 n1 = PerlLIO_read(pp[0],
4155 (void*)(((char*)&errkid)+n),
4161 PerlLIO_close(pp[0]);
4162 if (n) { /* Error */
4163 if (n != sizeof(int))
4164 DIE(aTHX_ "panic: kid popen errno read");
4165 errno = errkid; /* Propagate errno from kid */
4166 STATUS_NATIVE_CHILD_SET(-1);
4169 XPUSHi(STATUS_CURRENT);
4173 PerlLIO_close(pp[0]);
4174 #if defined(HAS_FCNTL) && defined(F_SETFD)
4175 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4178 if (PL_op->op_flags & OPf_STACKED) {
4179 SV * const really = *++MARK;
4180 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4182 else if (SP - MARK != 1)
4183 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4185 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4189 #else /* ! FORK or VMS or OS/2 */
4192 if (PL_op->op_flags & OPf_STACKED) {
4193 SV * const really = *++MARK;
4194 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4195 value = (I32)do_aspawn(really, MARK, SP);
4197 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4200 else if (SP - MARK != 1) {
4201 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4202 value = (I32)do_aspawn(NULL, MARK, SP);
4204 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4208 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4210 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4212 STATUS_NATIVE_CHILD_SET(value);
4215 XPUSHi(result ? value : STATUS_CURRENT);
4216 #endif /* !FORK or VMS or OS/2 */
4223 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4228 while (++MARK <= SP) {
4229 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4234 TAINT_PROPER("exec");
4236 PERL_FLUSHALL_FOR_CHILD;
4237 if (PL_op->op_flags & OPf_STACKED) {
4238 SV * const really = *++MARK;
4239 value = (I32)do_aexec(really, MARK, SP);
4241 else if (SP - MARK != 1)
4243 value = (I32)vms_do_aexec(NULL, MARK, SP);
4247 (void ) do_aspawn(NULL, MARK, SP);
4251 value = (I32)do_aexec(NULL, MARK, SP);
4256 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4259 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4262 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4276 # ifdef THREADS_HAVE_PIDS
4277 if (PL_ppid != 1 && getppid() == 1)
4278 /* maybe the parent process has died. Refresh ppid cache */
4282 XPUSHi( getppid() );
4286 DIE(aTHX_ PL_no_func, "getppid");
4296 (MAXARG < 1) ? 0 : TOPs ? SvIVx(POPs) : ((void)POPs, 0);
4299 pgrp = (I32)BSD_GETPGRP(pid);
4301 if (pid != 0 && pid != PerlProc_getpid())
4302 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4308 DIE(aTHX_ PL_no_func, "getpgrp()");
4318 pgrp = MAXARG == 2 && (TOPs||POPs) ? POPi : 0;
4319 if (MAXARG > 0) pid = TOPs && TOPi;
4325 TAINT_PROPER("setpgrp");
4327 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4329 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4330 || (pid != 0 && pid != PerlProc_getpid()))
4332 DIE(aTHX_ "setpgrp can't take arguments");
4334 SETi( setpgrp() >= 0 );
4335 #endif /* USE_BSDPGRP */
4338 DIE(aTHX_ PL_no_func, "setpgrp()");
4342 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4343 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4345 # define PRIORITY_WHICH_T(which) which
4350 #ifdef HAS_GETPRIORITY
4352 const int who = POPi;
4353 const int which = TOPi;
4354 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4357 DIE(aTHX_ PL_no_func, "getpriority()");
4363 #ifdef HAS_SETPRIORITY
4365 const int niceval = POPi;
4366 const int who = POPi;
4367 const int which = TOPi;
4368 TAINT_PROPER("setpriority");
4369 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4372 DIE(aTHX_ PL_no_func, "setpriority()");
4376 #undef PRIORITY_WHICH_T
4384 XPUSHn( time(NULL) );
4386 XPUSHi( time(NULL) );
4398 (void)PerlProc_times(&PL_timesbuf);
4400 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4401 /* struct tms, though same data */
4405 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4406 if (GIMME == G_ARRAY) {
4407 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4408 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4409 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4417 if (GIMME == G_ARRAY) {
4424 DIE(aTHX_ "times not implemented");
4426 #endif /* HAS_TIMES */
4429 /* The 32 bit int year limits the times we can represent to these
4430 boundaries with a few days wiggle room to account for time zone
4433 /* Sat Jan 3 00:00:00 -2147481748 */
4434 #define TIME_LOWER_BOUND -67768100567755200.0
4435 /* Sun Dec 29 12:00:00 2147483647 */
4436 #define TIME_UPPER_BOUND 67767976233316800.0
4445 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4446 static const char * const dayname[] =
4447 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4448 static const char * const monname[] =
4449 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4450 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4452 if (MAXARG < 1 || (!TOPs && ((void)POPs, 1))) {
4455 when = (Time64_T)now;
4458 NV input = Perl_floor(POPn);
4459 when = (Time64_T)input;
4460 if (when != input) {
4461 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4462 "%s(%.0" NVff ") too large", opname, input);
4466 if ( TIME_LOWER_BOUND > when ) {
4467 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4468 "%s(%.0" NVff ") too small", opname, when);
4471 else if( when > TIME_UPPER_BOUND ) {
4472 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4473 "%s(%.0" NVff ") too large", opname, when);
4477 if (PL_op->op_type == OP_LOCALTIME)
4478 err = S_localtime64_r(&when, &tmbuf);
4480 err = S_gmtime64_r(&when, &tmbuf);
4484 /* XXX %lld broken for quads */
4485 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4486 "%s(%.0" NVff ") failed", opname, when);
4489 if (GIMME != G_ARRAY) { /* scalar context */
4491 /* XXX newSVpvf()'s %lld type is broken, so cheat with a double */
4492 double year = (double)tmbuf.tm_year + 1900;
4499 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %.0f",
4500 dayname[tmbuf.tm_wday],
4501 monname[tmbuf.tm_mon],
4509 else { /* list context */
4515 mPUSHi(tmbuf.tm_sec);
4516 mPUSHi(tmbuf.tm_min);
4517 mPUSHi(tmbuf.tm_hour);
4518 mPUSHi(tmbuf.tm_mday);
4519 mPUSHi(tmbuf.tm_mon);
4520 mPUSHn(tmbuf.tm_year);
4521 mPUSHi(tmbuf.tm_wday);
4522 mPUSHi(tmbuf.tm_yday);
4523 mPUSHi(tmbuf.tm_isdst);
4534 anum = alarm((unsigned int)anum);
4540 DIE(aTHX_ PL_no_func, "alarm");
4551 (void)time(&lasttime);
4552 if (MAXARG < 1 || (!TOPs && !POPs))
4556 PerlProc_sleep((unsigned int)duration);
4559 XPUSHi(when - lasttime);
4563 /* Shared memory. */
4564 /* Merged with some message passing. */
4568 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4569 dVAR; dSP; dMARK; dTARGET;
4570 const int op_type = PL_op->op_type;
4575 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4578 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4581 value = (I32)(do_semop(MARK, SP) >= 0);
4584 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4592 return Perl_pp_semget(aTHX);
4600 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4601 dVAR; dSP; dMARK; dTARGET;
4602 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4609 DIE(aTHX_ "System V IPC is not implemented on this machine");
4615 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4616 dVAR; dSP; dMARK; dTARGET;
4617 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4625 PUSHp(zero_but_true, ZBTLEN);
4629 return Perl_pp_semget(aTHX);
4633 /* I can't const this further without getting warnings about the types of
4634 various arrays passed in from structures. */
4636 S_space_join_names_mortal(pTHX_ char *const *array)
4640 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4642 if (array && *array) {
4643 target = newSVpvs_flags("", SVs_TEMP);
4645 sv_catpv(target, *array);
4648 sv_catpvs(target, " ");
4651 target = sv_mortalcopy(&PL_sv_no);
4656 /* Get system info. */
4660 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4662 I32 which = PL_op->op_type;
4663 register char **elem;
4665 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4666 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4667 struct hostent *gethostbyname(Netdb_name_t);
4668 struct hostent *gethostent(void);
4670 struct hostent *hent = NULL;
4674 if (which == OP_GHBYNAME) {
4675 #ifdef HAS_GETHOSTBYNAME
4676 const char* const name = POPpbytex;
4677 hent = PerlSock_gethostbyname(name);
4679 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4682 else if (which == OP_GHBYADDR) {
4683 #ifdef HAS_GETHOSTBYADDR
4684 const int addrtype = POPi;
4685 SV * const addrsv = POPs;
4687 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4689 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4691 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4695 #ifdef HAS_GETHOSTENT
4696 hent = PerlSock_gethostent();
4698 DIE(aTHX_ PL_no_sock_func, "gethostent");
4701 #ifdef HOST_NOT_FOUND
4703 #ifdef USE_REENTRANT_API
4704 # ifdef USE_GETHOSTENT_ERRNO
4705 h_errno = PL_reentrant_buffer->_gethostent_errno;
4708 STATUS_UNIX_SET(h_errno);
4712 if (GIMME != G_ARRAY) {
4713 PUSHs(sv = sv_newmortal());
4715 if (which == OP_GHBYNAME) {
4717 sv_setpvn(sv, hent->h_addr, hent->h_length);
4720 sv_setpv(sv, (char*)hent->h_name);
4726 mPUSHs(newSVpv((char*)hent->h_name, 0));
4727 PUSHs(space_join_names_mortal(hent->h_aliases));
4728 mPUSHi(hent->h_addrtype);
4729 len = hent->h_length;
4732 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4733 mXPUSHp(*elem, len);
4737 mPUSHp(hent->h_addr, len);
4739 PUSHs(sv_mortalcopy(&PL_sv_no));
4744 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4750 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4752 I32 which = PL_op->op_type;
4754 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4755 struct netent *getnetbyaddr(Netdb_net_t, int);
4756 struct netent *getnetbyname(Netdb_name_t);
4757 struct netent *getnetent(void);
4759 struct netent *nent;
4761 if (which == OP_GNBYNAME){
4762 #ifdef HAS_GETNETBYNAME
4763 const char * const name = POPpbytex;
4764 nent = PerlSock_getnetbyname(name);
4766 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4769 else if (which == OP_GNBYADDR) {
4770 #ifdef HAS_GETNETBYADDR
4771 const int addrtype = POPi;
4772 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4773 nent = PerlSock_getnetbyaddr(addr, addrtype);
4775 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4779 #ifdef HAS_GETNETENT
4780 nent = PerlSock_getnetent();
4782 DIE(aTHX_ PL_no_sock_func, "getnetent");
4785 #ifdef HOST_NOT_FOUND
4787 #ifdef USE_REENTRANT_API
4788 # ifdef USE_GETNETENT_ERRNO
4789 h_errno = PL_reentrant_buffer->_getnetent_errno;
4792 STATUS_UNIX_SET(h_errno);
4797 if (GIMME != G_ARRAY) {
4798 PUSHs(sv = sv_newmortal());
4800 if (which == OP_GNBYNAME)
4801 sv_setiv(sv, (IV)nent->n_net);
4803 sv_setpv(sv, nent->n_name);
4809 mPUSHs(newSVpv(nent->n_name, 0));
4810 PUSHs(space_join_names_mortal(nent->n_aliases));
4811 mPUSHi(nent->n_addrtype);
4812 mPUSHi(nent->n_net);
4817 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4823 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4825 I32 which = PL_op->op_type;
4827 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4828 struct protoent *getprotobyname(Netdb_name_t);
4829 struct protoent *getprotobynumber(int);
4830 struct protoent *getprotoent(void);
4832 struct protoent *pent;
4834 if (which == OP_GPBYNAME) {
4835 #ifdef HAS_GETPROTOBYNAME
4836 const char* const name = POPpbytex;
4837 pent = PerlSock_getprotobyname(name);
4839 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4842 else if (which == OP_GPBYNUMBER) {
4843 #ifdef HAS_GETPROTOBYNUMBER
4844 const int number = POPi;
4845 pent = PerlSock_getprotobynumber(number);
4847 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4851 #ifdef HAS_GETPROTOENT
4852 pent = PerlSock_getprotoent();
4854 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4858 if (GIMME != G_ARRAY) {
4859 PUSHs(sv = sv_newmortal());
4861 if (which == OP_GPBYNAME)
4862 sv_setiv(sv, (IV)pent->p_proto);
4864 sv_setpv(sv, pent->p_name);
4870 mPUSHs(newSVpv(pent->p_name, 0));
4871 PUSHs(space_join_names_mortal(pent->p_aliases));
4872 mPUSHi(pent->p_proto);
4877 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4883 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4885 I32 which = PL_op->op_type;
4887 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4888 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4889 struct servent *getservbyport(int, Netdb_name_t);
4890 struct servent *getservent(void);
4892 struct servent *sent;
4894 if (which == OP_GSBYNAME) {
4895 #ifdef HAS_GETSERVBYNAME
4896 const char * const proto = POPpbytex;
4897 const char * const name = POPpbytex;
4898 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4900 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4903 else if (which == OP_GSBYPORT) {
4904 #ifdef HAS_GETSERVBYPORT
4905 const char * const proto = POPpbytex;
4906 unsigned short port = (unsigned short)POPu;
4908 port = PerlSock_htons(port);
4910 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4912 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4916 #ifdef HAS_GETSERVENT
4917 sent = PerlSock_getservent();
4919 DIE(aTHX_ PL_no_sock_func, "getservent");
4923 if (GIMME != G_ARRAY) {
4924 PUSHs(sv = sv_newmortal());
4926 if (which == OP_GSBYNAME) {
4928 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4930 sv_setiv(sv, (IV)(sent->s_port));
4934 sv_setpv(sv, sent->s_name);
4940 mPUSHs(newSVpv(sent->s_name, 0));
4941 PUSHs(space_join_names_mortal(sent->s_aliases));
4943 mPUSHi(PerlSock_ntohs(sent->s_port));
4945 mPUSHi(sent->s_port);
4947 mPUSHs(newSVpv(sent->s_proto, 0));
4952 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4959 const int stayopen = TOPi;
4960 switch(PL_op->op_type) {
4962 #ifdef HAS_SETHOSTENT
4963 PerlSock_sethostent(stayopen);
4965 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4968 #ifdef HAS_SETNETENT
4970 PerlSock_setnetent(stayopen);
4972 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4976 #ifdef HAS_SETPROTOENT
4977 PerlSock_setprotoent(stayopen);
4979 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4983 #ifdef HAS_SETSERVENT
4984 PerlSock_setservent(stayopen);
4986 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
4996 switch(PL_op->op_type) {
4998 #ifdef HAS_ENDHOSTENT
4999 PerlSock_endhostent();
5001 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5005 #ifdef HAS_ENDNETENT
5006 PerlSock_endnetent();
5008 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5012 #ifdef HAS_ENDPROTOENT
5013 PerlSock_endprotoent();
5015 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5019 #ifdef HAS_ENDSERVENT
5020 PerlSock_endservent();
5022 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5026 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5029 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5033 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5036 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5040 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5043 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5047 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5050 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5062 I32 which = PL_op->op_type;
5064 struct passwd *pwent = NULL;
5066 * We currently support only the SysV getsp* shadow password interface.
5067 * The interface is declared in <shadow.h> and often one needs to link
5068 * with -lsecurity or some such.
5069 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5072 * AIX getpwnam() is clever enough to return the encrypted password
5073 * only if the caller (euid?) is root.
5075 * There are at least three other shadow password APIs. Many platforms
5076 * seem to contain more than one interface for accessing the shadow
5077 * password databases, possibly for compatibility reasons.
5078 * The getsp*() is by far he simplest one, the other two interfaces
5079 * are much more complicated, but also very similar to each other.
5084 * struct pr_passwd *getprpw*();
5085 * The password is in
5086 * char getprpw*(...).ufld.fd_encrypt[]
5087 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5092 * struct es_passwd *getespw*();
5093 * The password is in
5094 * char *(getespw*(...).ufld.fd_encrypt)
5095 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5098 * struct userpw *getuserpw();
5099 * The password is in
5100 * char *(getuserpw(...)).spw_upw_passwd
5101 * (but the de facto standard getpwnam() should work okay)
5103 * Mention I_PROT here so that Configure probes for it.
5105 * In HP-UX for getprpw*() the manual page claims that one should include
5106 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5107 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5108 * and pp_sys.c already includes <shadow.h> if there is such.
5110 * Note that <sys/security.h> is already probed for, but currently
5111 * it is only included in special cases.
5113 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5114 * be preferred interface, even though also the getprpw*() interface
5115 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5116 * One also needs to call set_auth_parameters() in main() before
5117 * doing anything else, whether one is using getespw*() or getprpw*().
5119 * Note that accessing the shadow databases can be magnitudes
5120 * slower than accessing the standard databases.
5125 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5126 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5127 * the pw_comment is left uninitialized. */
5128 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5134 const char* const name = POPpbytex;
5135 pwent = getpwnam(name);
5141 pwent = getpwuid(uid);
5145 # ifdef HAS_GETPWENT
5147 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5148 if (pwent) pwent = getpwnam(pwent->pw_name);
5151 DIE(aTHX_ PL_no_func, "getpwent");
5157 if (GIMME != G_ARRAY) {
5158 PUSHs(sv = sv_newmortal());
5160 if (which == OP_GPWNAM)
5161 # if Uid_t_sign <= 0
5162 sv_setiv(sv, (IV)pwent->pw_uid);
5164 sv_setuv(sv, (UV)pwent->pw_uid);
5167 sv_setpv(sv, pwent->pw_name);
5173 mPUSHs(newSVpv(pwent->pw_name, 0));
5177 /* If we have getspnam(), we try to dig up the shadow
5178 * password. If we are underprivileged, the shadow
5179 * interface will set the errno to EACCES or similar,
5180 * and return a null pointer. If this happens, we will
5181 * use the dummy password (usually "*" or "x") from the
5182 * standard password database.
5184 * In theory we could skip the shadow call completely
5185 * if euid != 0 but in practice we cannot know which
5186 * security measures are guarding the shadow databases
5187 * on a random platform.
5189 * Resist the urge to use additional shadow interfaces.
5190 * Divert the urge to writing an extension instead.
5193 /* Some AIX setups falsely(?) detect some getspnam(), which
5194 * has a different API than the Solaris/IRIX one. */
5195 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5198 const struct spwd * const spwent = getspnam(pwent->pw_name);
5199 /* Save and restore errno so that
5200 * underprivileged attempts seem
5201 * to have never made the unsuccessful
5202 * attempt to retrieve the shadow password. */
5204 if (spwent && spwent->sp_pwdp)
5205 sv_setpv(sv, spwent->sp_pwdp);
5209 if (!SvPOK(sv)) /* Use the standard password, then. */
5210 sv_setpv(sv, pwent->pw_passwd);
5213 # ifndef INCOMPLETE_TAINTS
5214 /* passwd is tainted because user himself can diddle with it.
5215 * admittedly not much and in a very limited way, but nevertheless. */
5219 # if Uid_t_sign <= 0
5220 mPUSHi(pwent->pw_uid);
5222 mPUSHu(pwent->pw_uid);
5225 # if Uid_t_sign <= 0
5226 mPUSHi(pwent->pw_gid);
5228 mPUSHu(pwent->pw_gid);
5230 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5231 * because of the poor interface of the Perl getpw*(),
5232 * not because there's some standard/convention saying so.
5233 * A better interface would have been to return a hash,
5234 * but we are accursed by our history, alas. --jhi. */
5236 mPUSHi(pwent->pw_change);
5239 mPUSHi(pwent->pw_quota);
5242 mPUSHs(newSVpv(pwent->pw_age, 0));
5244 /* I think that you can never get this compiled, but just in case. */
5245 PUSHs(sv_mortalcopy(&PL_sv_no));
5250 /* pw_class and pw_comment are mutually exclusive--.
5251 * see the above note for pw_change, pw_quota, and pw_age. */
5253 mPUSHs(newSVpv(pwent->pw_class, 0));
5256 mPUSHs(newSVpv(pwent->pw_comment, 0));
5258 /* I think that you can never get this compiled, but just in case. */
5259 PUSHs(sv_mortalcopy(&PL_sv_no));
5264 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5266 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5268 # ifndef INCOMPLETE_TAINTS
5269 /* pw_gecos is tainted because user himself can diddle with it. */
5273 mPUSHs(newSVpv(pwent->pw_dir, 0));
5275 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5276 # ifndef INCOMPLETE_TAINTS
5277 /* pw_shell is tainted because user himself can diddle with it. */
5282 mPUSHi(pwent->pw_expire);
5287 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5295 const I32 which = PL_op->op_type;
5296 const struct group *grent;
5298 if (which == OP_GGRNAM) {
5299 const char* const name = POPpbytex;
5300 grent = (const struct group *)getgrnam(name);
5302 else if (which == OP_GGRGID) {
5303 const Gid_t gid = POPi;
5304 grent = (const struct group *)getgrgid(gid);
5308 grent = (struct group *)getgrent();
5310 DIE(aTHX_ PL_no_func, "getgrent");
5314 if (GIMME != G_ARRAY) {
5315 SV * const sv = sv_newmortal();
5319 if (which == OP_GGRNAM)
5321 sv_setiv(sv, (IV)grent->gr_gid);
5323 sv_setuv(sv, (UV)grent->gr_gid);
5326 sv_setpv(sv, grent->gr_name);
5332 mPUSHs(newSVpv(grent->gr_name, 0));
5335 mPUSHs(newSVpv(grent->gr_passwd, 0));
5337 PUSHs(sv_mortalcopy(&PL_sv_no));
5341 mPUSHi(grent->gr_gid);
5343 mPUSHu(grent->gr_gid);
5346 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5347 /* In UNICOS/mk (_CRAYMPP) the multithreading
5348 * versions (getgrnam_r, getgrgid_r)
5349 * seem to return an illegal pointer
5350 * as the group members list, gr_mem.
5351 * getgrent() doesn't even have a _r version
5352 * but the gr_mem is poisonous anyway.
5353 * So yes, you cannot get the list of group
5354 * members if building multithreaded in UNICOS/mk. */
5355 PUSHs(space_join_names_mortal(grent->gr_mem));
5361 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5371 if (!(tmps = PerlProc_getlogin()))
5373 sv_setpv_mg(TARG, tmps);
5377 DIE(aTHX_ PL_no_func, "getlogin");
5381 /* Miscellaneous. */
5386 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5387 register I32 items = SP - MARK;
5388 unsigned long a[20];
5393 while (++MARK <= SP) {
5394 if (SvTAINTED(*MARK)) {
5400 TAINT_PROPER("syscall");
5403 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5404 * or where sizeof(long) != sizeof(char*). But such machines will
5405 * not likely have syscall implemented either, so who cares?
5407 while (++MARK <= SP) {
5408 if (SvNIOK(*MARK) || !i)
5409 a[i++] = SvIV(*MARK);
5410 else if (*MARK == &PL_sv_undef)
5413 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5419 DIE(aTHX_ "Too many args to syscall");
5421 DIE(aTHX_ "Too few args to syscall");
5423 retval = syscall(a[0]);
5426 retval = syscall(a[0],a[1]);
5429 retval = syscall(a[0],a[1],a[2]);
5432 retval = syscall(a[0],a[1],a[2],a[3]);
5435 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5438 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5441 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5444 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5448 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5451 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],
5462 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5466 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5467 a[10],a[11],a[12],a[13]);
5469 #endif /* atarist */
5475 DIE(aTHX_ PL_no_func, "syscall");
5479 #ifdef FCNTL_EMULATE_FLOCK
5481 /* XXX Emulate flock() with fcntl().
5482 What's really needed is a good file locking module.
5486 fcntl_emulate_flock(int fd, int operation)
5491 switch (operation & ~LOCK_NB) {
5493 flock.l_type = F_RDLCK;
5496 flock.l_type = F_WRLCK;
5499 flock.l_type = F_UNLCK;
5505 flock.l_whence = SEEK_SET;
5506 flock.l_start = flock.l_len = (Off_t)0;
5508 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5509 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5510 errno = EWOULDBLOCK;
5514 #endif /* FCNTL_EMULATE_FLOCK */
5516 #ifdef LOCKF_EMULATE_FLOCK
5518 /* XXX Emulate flock() with lockf(). This is just to increase
5519 portability of scripts. The calls are not completely
5520 interchangeable. What's really needed is a good file
5524 /* The lockf() constants might have been defined in <unistd.h>.
5525 Unfortunately, <unistd.h> causes troubles on some mixed
5526 (BSD/POSIX) systems, such as SunOS 4.1.3.
5528 Further, the lockf() constants aren't POSIX, so they might not be
5529 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5530 just stick in the SVID values and be done with it. Sigh.
5534 # define F_ULOCK 0 /* Unlock a previously locked region */
5537 # define F_LOCK 1 /* Lock a region for exclusive use */
5540 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5543 # define F_TEST 3 /* Test a region for other processes locks */
5547 lockf_emulate_flock(int fd, int operation)
5553 /* flock locks entire file so for lockf we need to do the same */
5554 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5555 if (pos > 0) /* is seekable and needs to be repositioned */
5556 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5557 pos = -1; /* seek failed, so don't seek back afterwards */
5560 switch (operation) {
5562 /* LOCK_SH - get a shared lock */
5564 /* LOCK_EX - get an exclusive lock */
5566 i = lockf (fd, F_LOCK, 0);
5569 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5570 case LOCK_SH|LOCK_NB:
5571 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5572 case LOCK_EX|LOCK_NB:
5573 i = lockf (fd, F_TLOCK, 0);
5575 if ((errno == EAGAIN) || (errno == EACCES))
5576 errno = EWOULDBLOCK;
5579 /* LOCK_UN - unlock (non-blocking is a no-op) */
5581 case LOCK_UN|LOCK_NB:
5582 i = lockf (fd, F_ULOCK, 0);
5585 /* Default - can't decipher operation */
5592 if (pos > 0) /* need to restore position of the handle */
5593 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5598 #endif /* LOCKF_EMULATE_FLOCK */
5602 * c-indentation-style: bsd
5604 * indent-tabs-mode: t
5607 * ex: set ts=8 sts=4 sw=4 noet: