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
35 /* Shadow password support for solaris - pdo@cs.umd.edu
36 * Not just Solaris: at least HP-UX, IRIX, Linux.
37 * The API is from SysV.
39 * There are at least two more shadow interfaces,
40 * see the comments in pp_gpwent().
44 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
45 * and another MAXINT from "perl.h" <- <sys/param.h>. */
52 # include <sys/resource.h>
61 # include <sys/select.h>
65 /* XXX Configure test needed.
66 h_errno might not be a simple 'int', especially for multi-threaded
67 applications, see "extern int errno in perl.h". Creating such
68 a test requires taking into account the differences between
69 compiling multithreaded and singlethreaded ($ccflags et al).
70 HOST_NOT_FOUND is typically defined in <netdb.h>.
72 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
81 struct passwd *getpwnam (char *);
82 struct passwd *getpwuid (Uid_t);
87 struct passwd *getpwent (void);
88 #elif defined (VMS) && defined (my_getpwent)
89 struct passwd *Perl_my_getpwent (pTHX);
98 struct group *getgrnam (char *);
99 struct group *getgrgid (Gid_t);
103 struct group *getgrent (void);
109 # if defined(_MSC_VER) || defined(__MINGW32__)
110 # include <sys/utime.h>
117 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
120 # define my_chsize PerlLIO_chsize
123 # define my_chsize PerlLIO_chsize
125 I32 my_chsize(int fd, Off_t length);
131 #else /* no flock() */
133 /* fcntl.h might not have been included, even if it exists, because
134 the current Configure only sets I_FCNTL if it's needed to pick up
135 the *_OK constants. Make sure it has been included before testing
136 the fcntl() locking constants. */
137 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
141 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
142 # define FLOCK fcntl_emulate_flock
143 # define FCNTL_EMULATE_FLOCK
144 # else /* no flock() or fcntl(F_SETLK,...) */
146 # define FLOCK lockf_emulate_flock
147 # define LOCKF_EMULATE_FLOCK
149 # endif /* no flock() or fcntl(F_SETLK,...) */
152 static int FLOCK (int, int);
155 * These are the flock() constants. Since this sytems doesn't have
156 * flock(), the values of the constants are probably not available.
170 # endif /* emulating flock() */
172 #endif /* no flock() */
175 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
177 #if defined(I_SYS_ACCESS) && !defined(R_OK)
178 # include <sys/access.h>
184 /* Missing protos on LynxOS */
185 void sethostent(int);
186 void endhostent(void);
188 void endnetent(void);
189 void setprotoent(int);
190 void endprotoent(void);
191 void setservent(int);
192 void endservent(void);
196 # include "amigaos4/amigaio.h"
199 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
201 /* F_OK unused: if stat() cannot find it... */
203 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
204 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
205 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
208 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
209 # ifdef I_SYS_SECURITY
210 # include <sys/security.h>
214 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
217 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
221 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
223 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
227 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
228 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
229 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
232 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
234 const Uid_t ruid = getuid();
235 const Uid_t euid = geteuid();
236 const Gid_t rgid = getgid();
237 const Gid_t egid = getegid();
240 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
241 Perl_croak(aTHX_ "switching effective uid is not implemented");
244 if (setreuid(euid, ruid))
247 if (setresuid(euid, ruid, (Uid_t)-1))
250 /* diag_listed_as: entering effective %s failed */
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 /* diag_listed_as: entering effective %s failed */
265 Perl_croak(aTHX_ "entering effective gid failed");
268 res = access(path, mode);
271 if (setreuid(ruid, euid))
274 if (setresuid(ruid, euid, (Uid_t)-1))
277 /* diag_listed_as: leaving effective %s failed */
278 Perl_croak(aTHX_ "leaving effective uid failed");
281 if (setregid(rgid, egid))
284 if (setresgid(rgid, egid, (Gid_t)-1))
287 /* diag_listed_as: leaving effective %s failed */
288 Perl_croak(aTHX_ "leaving effective gid failed");
292 # define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
299 const char * const tmps = POPpconstx;
300 const I32 gimme = GIMME_V;
301 const char *mode = "r";
304 if (PL_op->op_private & OPpOPEN_IN_RAW)
306 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
308 fp = PerlProc_popen(tmps, mode);
310 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
312 PerlIO_apply_layers(aTHX_ fp,mode,type);
314 if (gimme == G_VOID) {
316 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
319 else if (gimme == G_SCALAR) {
320 ENTER_with_name("backtick");
322 PL_rs = &PL_sv_undef;
323 sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
324 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
326 LEAVE_with_name("backtick");
332 SV * const sv = newSV(79);
333 if (sv_gets(sv, fp, 0) == NULL) {
338 if (SvLEN(sv) - SvCUR(sv) > 20) {
339 SvPV_shrink_to_cur(sv);
344 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
345 TAINT; /* "I believe that this is not gratuitous!" */
348 STATUS_NATIVE_CHILD_SET(-1);
349 if (gimme == G_SCALAR)
360 GV * const gv = (PL_op->op_flags & OPf_SPECIAL) ? NULL : (GV *)POPs;
364 /* make a copy of the pattern if it is gmagical, to ensure that magic
365 * is called once and only once */
366 if (SvGMAGICAL(TOPs)) TOPs = sv_2mortal(newSVsv(TOPs));
368 tryAMAGICunTARGETlist(iter_amg, (PL_op->op_flags & OPf_SPECIAL));
370 if (PL_op->op_flags & OPf_SPECIAL) {
371 /* call Perl-level glob function instead. Stack args are:
373 * and following OPs should be: gv(CORE::GLOBAL::glob), entersub
382 /* Note that we only ever get here if File::Glob fails to load
383 * without at the same time croaking, for some reason, or if
384 * perl was built with PERL_EXTERNAL_GLOB */
386 ENTER_with_name("glob");
391 * The external globbing program may use things we can't control,
392 * so for security reasons we must assume the worst.
395 taint_proper(PL_no_security, "glob");
399 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
402 SAVESPTR(PL_rs); /* This is not permanent, either. */
403 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
406 *SvPVX(PL_rs) = '\n';
410 result = do_readline();
411 LEAVE_with_name("glob");
417 PL_last_in_gv = cGVOP_gv;
418 return do_readline();
428 do_join(TARG, &PL_sv_no, MARK, SP);
432 else if (SP == MARK) {
439 if (SvGMAGICAL(exsv)) exsv = sv_mortalcopy(exsv);
442 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
443 /* well-formed exception supplied */
446 SV * const errsv = ERRSV;
449 if (SvGMAGICAL(errsv)) {
450 exsv = sv_newmortal();
451 sv_setsv_nomg(exsv, errsv);
455 else if (SvPOKp(errsv) ? SvCUR(errsv) : SvNIOKp(errsv)) {
456 exsv = sv_newmortal();
457 sv_setsv_nomg(exsv, errsv);
458 sv_catpvs(exsv, "\t...caught");
461 exsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
464 if (SvROK(exsv) && !PL_warnhook)
465 Perl_warn(aTHX_ "%"SVf, SVfARG(exsv));
477 VMSISH_HUSHED || (PL_curcop->op_private & OPpHUSH_VMSISH);
479 if (SP - MARK != 1) {
481 do_join(TARG, &PL_sv_no, MARK, SP);
489 if (SvROK(exsv) || (SvPV_const(exsv, len), len)) {
490 /* well-formed exception supplied */
493 SV * const errsv = ERRSV;
497 if (sv_isobject(exsv)) {
498 HV * const stash = SvSTASH(SvRV(exsv));
499 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
501 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
502 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
509 call_sv(MUTABLE_SV(GvCV(gv)),
510 G_SCALAR|G_EVAL|G_KEEPERR);
511 exsv = sv_mortalcopy(*PL_stack_sp--);
515 else if (SvPOK(errsv) && SvCUR(errsv)) {
516 exsv = sv_mortalcopy(errsv);
517 sv_catpvs(exsv, "\t...propagated");
520 exsv = newSVpvs_flags("Died", SVs_TEMP);
524 NOT_REACHED; /* NOTREACHED */
525 return NULL; /* avoid missing return from non-void function warning */
531 Perl_tied_method(pTHX_ SV *methname, SV **sp, SV *const sv,
532 const MAGIC *const mg, const U32 flags, U32 argc, ...)
537 PERL_ARGS_ASSERT_TIED_METHOD;
539 /* Ensure that our flag bits do not overlap. */
540 STATIC_ASSERT_STMT((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
541 STATIC_ASSERT_STMT((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
542 STATIC_ASSERT_STMT((TIED_METHOD_SAY & G_WANT) == 0);
544 PUTBACK; /* sp is at *foot* of args, so this pops args from old stack */
545 PUSHSTACKi(PERLSI_MAGIC);
546 EXTEND(SP, argc+1); /* object + args */
548 PUSHs(SvTIED_obj(sv, mg));
549 if (flags & TIED_METHOD_ARGUMENTS_ON_STACK) {
550 Copy(orig_sp + 2, sp + 1, argc, SV*); /* copy args to new stack */
554 const U32 mortalize_not_needed
555 = flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
557 va_start(args, argc);
559 SV *const arg = va_arg(args, SV *);
560 if(mortalize_not_needed)
569 ENTER_with_name("call_tied_method");
570 if (flags & TIED_METHOD_SAY) {
571 /* local $\ = "\n" */
572 SAVEGENERICSV(PL_ors_sv);
573 PL_ors_sv = newSVpvs("\n");
575 ret_args = call_sv(methname, (flags & G_WANT)|G_METHOD_NAMED);
580 if (ret_args) { /* copy results back to original stack */
581 EXTEND(sp, ret_args);
582 Copy(orig_sp - ret_args + 1, sp + 1, ret_args, SV*);
586 LEAVE_with_name("call_tied_method");
590 #define tied_method0(a,b,c,d) \
591 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
592 #define tied_method1(a,b,c,d,e) \
593 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
594 #define tied_method2(a,b,c,d,e,f) \
595 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
608 GV * const gv = MUTABLE_GV(*++MARK);
610 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
611 DIE(aTHX_ PL_no_usym, "filehandle");
613 if ((io = GvIOp(gv))) {
615 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
618 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
619 "Opening dirhandle %"HEKf" also as a file",
620 HEKfARG(GvENAME_HEK(gv)));
622 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
624 /* Method's args are same as ours ... */
625 /* ... except handle is replaced by the object */
626 return Perl_tied_method(aTHX_ SV_CONST(OPEN), mark - 1, MUTABLE_SV(io), mg,
627 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
639 tmps = SvPV_const(sv, len);
640 ok = do_open6(gv, tmps, len, NULL, MARK+1, (SP-MARK));
643 PUSHi( (I32)PL_forkprocess );
644 else if (PL_forkprocess == 0) /* we are a new child */
655 MAXARG == 0 || (!TOPs && !POPs) ? PL_defoutgv : MUTABLE_GV(POPs);
661 IO * const io = GvIO(gv);
663 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
665 return tied_method0(SV_CONST(CLOSE), SP, MUTABLE_SV(io), mg);
669 PUSHs(boolSV(do_close(gv, TRUE)));
681 GV * const wgv = MUTABLE_GV(POPs);
682 GV * const rgv = MUTABLE_GV(POPs);
684 assert (isGV_with_GP(rgv));
685 assert (isGV_with_GP(wgv));
688 do_close(rgv, FALSE);
692 do_close(wgv, FALSE);
694 if (PerlProc_pipe(fd) < 0)
697 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
698 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
699 IoOFP(rstio) = IoIFP(rstio);
700 IoIFP(wstio) = IoOFP(wstio);
701 IoTYPE(rstio) = IoTYPE_RDONLY;
702 IoTYPE(wstio) = IoTYPE_WRONLY;
704 if (!IoIFP(rstio) || !IoOFP(wstio)) {
706 PerlIO_close(IoIFP(rstio));
708 PerlLIO_close(fd[0]);
710 PerlIO_close(IoOFP(wstio));
712 PerlLIO_close(fd[1]);
715 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
716 /* ensure close-on-exec */
717 if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
718 (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
726 DIE(aTHX_ PL_no_func, "pipe");
740 gv = MUTABLE_GV(POPs);
744 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
746 return tied_method0(SV_CONST(FILENO), SP, MUTABLE_SV(io), mg);
749 if (io && IoDIRP(io)) {
750 #if defined(HAS_DIRFD) || defined(HAS_DIR_DD_FD)
751 PUSHi(my_dirfd(IoDIRP(io)));
753 #elif defined(ENOTSUP)
754 errno = ENOTSUP; /* Operation not supported */
756 #elif defined(EOPNOTSUPP)
757 errno = EOPNOTSUPP; /* Operation not supported on socket */
760 errno = EINVAL; /* Invalid argument */
765 if (!io || !(fp = IoIFP(io))) {
766 /* Can't do this because people seem to do things like
767 defined(fileno($foo)) to check whether $foo is a valid fh.
774 PUSHi(PerlIO_fileno(fp));
785 if (MAXARG < 1 || (!TOPs && !POPs)) {
786 anum = PerlLIO_umask(022);
787 /* setting it to 022 between the two calls to umask avoids
788 * to have a window where the umask is set to 0 -- meaning
789 * that another thread could create world-writeable files. */
791 (void)PerlLIO_umask(anum);
794 anum = PerlLIO_umask(POPi);
795 TAINT_PROPER("umask");
798 /* Only DIE if trying to restrict permissions on "user" (self).
799 * Otherwise it's harmless and more useful to just return undef
800 * since 'group' and 'other' concepts probably don't exist here. */
801 if (MAXARG >= 1 && (TOPs||POPs) && (POPi & 0700))
802 DIE(aTHX_ "umask not implemented");
803 XPUSHs(&PL_sv_undef);
822 gv = MUTABLE_GV(POPs);
826 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
828 /* This takes advantage of the implementation of the varargs
829 function, which I don't think that the optimiser will be able to
830 figure out. Although, as it's a static function, in theory it
832 return Perl_tied_method(aTHX_ SV_CONST(BINMODE), SP, MUTABLE_SV(io), mg,
833 G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
834 discp ? 1 : 0, discp);
838 if (!io || !(fp = IoIFP(io))) {
840 SETERRNO(EBADF,RMS_IFI);
847 const char *d = NULL;
850 d = SvPV_const(discp, len);
851 mode = mode_from_discipline(d, len);
852 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
853 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
854 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
875 const I32 markoff = MARK - PL_stack_base;
876 const char *methname;
877 int how = PERL_MAGIC_tied;
881 switch(SvTYPE(varsv)) {
885 methname = "TIEHASH";
886 if (HvLAZYDEL(varsv) && (entry = HvEITER((HV *)varsv))) {
887 HvLAZYDEL_off(varsv);
888 hv_free_ent((HV *)varsv, entry);
890 HvEITER_set(MUTABLE_HV(varsv), 0);
894 methname = "TIEARRAY";
895 if (!AvREAL(varsv)) {
897 Perl_croak(aTHX_ "Cannot tie unreifiable array");
898 av_clear((AV *)varsv);
905 if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
906 methname = "TIEHANDLE";
907 how = PERL_MAGIC_tiedscalar;
908 /* For tied filehandles, we apply tiedscalar magic to the IO
909 slot of the GP rather than the GV itself. AMS 20010812 */
911 GvIOp(varsv) = newIO();
912 varsv = MUTABLE_SV(GvIOp(varsv));
915 if (SvTYPE(varsv) == SVt_PVLV && LvTYPE(varsv) == 'y') {
916 vivify_defelem(varsv);
917 varsv = LvTARG(varsv);
921 methname = "TIESCALAR";
922 how = PERL_MAGIC_tiedscalar;
926 if (sv_isobject(*MARK)) { /* Calls GET magic. */
927 ENTER_with_name("call_TIE");
928 PUSHSTACKi(PERLSI_MAGIC);
930 EXTEND(SP,(I32)items);
934 call_method(methname, G_SCALAR);
937 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
938 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
939 * wrong error message, and worse case, supreme action at a distance.
940 * (Sorry obfuscation writers. You're not going to be given this one.)
942 stash = gv_stashsv(*MARK, 0);
943 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
944 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
945 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
947 ENTER_with_name("call_TIE");
948 PUSHSTACKi(PERLSI_MAGIC);
950 EXTEND(SP,(I32)items);
954 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
960 if (sv_isobject(sv)) {
961 sv_unmagic(varsv, how);
962 /* Croak if a self-tie on an aggregate is attempted. */
963 if (varsv == SvRV(sv) &&
964 (SvTYPE(varsv) == SVt_PVAV ||
965 SvTYPE(varsv) == SVt_PVHV))
967 "Self-ties of arrays and hashes are not supported");
968 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
970 LEAVE_with_name("call_TIE");
971 SP = PL_stack_base + markoff;
977 /* also used for: pp_dbmclose() */
984 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
985 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
987 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
990 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y' &&
991 !(sv = defelem_target(sv, NULL))) RETPUSHUNDEF;
993 if ((mg = SvTIED_mg(sv, how))) {
994 SV * const obj = SvRV(SvTIED_obj(sv, mg));
996 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
998 if (gv && isGV(gv) && (cv = GvCV(gv))) {
1000 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
1001 mXPUSHi(SvREFCNT(obj) - 1);
1003 ENTER_with_name("call_UNTIE");
1004 call_sv(MUTABLE_SV(cv), G_VOID);
1005 LEAVE_with_name("call_UNTIE");
1008 else if (mg && SvREFCNT(obj) > 1) {
1009 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
1010 "untie attempted while %"UVuf" inner references still exist",
1011 (UV)SvREFCNT(obj) - 1 ) ;
1015 sv_unmagic(sv, how) ;
1024 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
1025 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
1027 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
1030 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y' &&
1031 !(sv = defelem_target(sv, NULL))) goto ret_undef;
1033 if ((mg = SvTIED_mg(sv, how))) {
1034 SETs(SvTIED_obj(sv, mg));
1035 return NORMAL; /* PUTBACK not needed, pp_tied never moves SP */
1049 HV * const hv = MUTABLE_HV(POPs);
1050 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
1051 stash = gv_stashsv(sv, 0);
1052 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
1054 require_pv("AnyDBM_File.pm");
1056 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
1057 DIE(aTHX_ "No dbm on this machine");
1067 mPUSHu(O_RDWR|O_CREAT);
1071 if (!SvOK(right)) right = &PL_sv_no;
1075 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1078 if (!sv_isobject(TOPs)) {
1086 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1088 if (sv_isobject(TOPs))
1093 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1094 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1111 struct timeval timebuf;
1112 struct timeval *tbuf = &timebuf;
1115 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1120 # if BYTEORDER & 0xf0000
1121 # define ORDERBYTE (0x88888888 - BYTEORDER)
1123 # define ORDERBYTE (0x4444 - BYTEORDER)
1129 for (i = 1; i <= 3; i++) {
1130 SV * const sv = SP[i];
1134 if (SvREADONLY(sv)) {
1135 if (!(SvPOK(sv) && SvCUR(sv) == 0))
1136 Perl_croak_no_modify();
1138 else if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
1141 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
1142 "Non-string passed as bitmask");
1143 SvPV_force_nomg_nolen(sv); /* force string conversion */
1150 /* little endians can use vecs directly */
1151 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1158 masksize = NFDBITS / NBBY;
1160 masksize = sizeof(long); /* documented int, everyone seems to use long */
1162 Zero(&fd_sets[0], 4, char*);
1165 # if SELECT_MIN_BITS == 1
1166 growsize = sizeof(fd_set);
1168 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1169 # undef SELECT_MIN_BITS
1170 # define SELECT_MIN_BITS __FD_SETSIZE
1172 /* If SELECT_MIN_BITS is greater than one we most probably will want
1173 * to align the sizes with SELECT_MIN_BITS/8 because for example
1174 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1175 * UNIX, Solaris, Darwin) the smallest quantum select() operates
1176 * on (sets/tests/clears bits) is 32 bits. */
1177 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1183 value = SvNV_nomg(sv);
1186 timebuf.tv_sec = (long)value;
1187 value -= (NV)timebuf.tv_sec;
1188 timebuf.tv_usec = (long)(value * 1000000.0);
1193 for (i = 1; i <= 3; i++) {
1195 if (!SvOK(sv) || SvCUR(sv) == 0) {
1202 Sv_Grow(sv, growsize);
1206 while (++j <= growsize) {
1210 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1212 Newx(fd_sets[i], growsize, char);
1213 for (offset = 0; offset < growsize; offset += masksize) {
1214 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1215 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1218 fd_sets[i] = SvPVX(sv);
1222 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1223 /* Can't make just the (void*) conditional because that would be
1224 * cpp #if within cpp macro, and not all compilers like that. */
1225 nfound = PerlSock_select(
1227 (Select_fd_set_t) fd_sets[1],
1228 (Select_fd_set_t) fd_sets[2],
1229 (Select_fd_set_t) fd_sets[3],
1230 (void*) tbuf); /* Workaround for compiler bug. */
1232 nfound = PerlSock_select(
1234 (Select_fd_set_t) fd_sets[1],
1235 (Select_fd_set_t) fd_sets[2],
1236 (Select_fd_set_t) fd_sets[3],
1239 for (i = 1; i <= 3; i++) {
1242 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1244 for (offset = 0; offset < growsize; offset += masksize) {
1245 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1246 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1248 Safefree(fd_sets[i]);
1255 if (GIMME_V == G_ARRAY && tbuf) {
1256 value = (NV)(timebuf.tv_sec) +
1257 (NV)(timebuf.tv_usec) / 1000000.0;
1262 DIE(aTHX_ "select not implemented");
1270 =for apidoc setdefout
1272 Sets C<PL_defoutgv>, the default file handle for output, to the passed in
1273 typeglob. As C<PL_defoutgv> "owns" a reference on its typeglob, the reference
1274 count of the passed in typeglob is increased by one, and the reference count
1275 of the typeglob that C<PL_defoutgv> points to is decreased by one.
1281 Perl_setdefout(pTHX_ GV *gv)
1283 PERL_ARGS_ASSERT_SETDEFOUT;
1284 SvREFCNT_inc_simple_void_NN(gv);
1285 SvREFCNT_dec(PL_defoutgv);
1293 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1294 GV * egv = GvEGVx(PL_defoutgv);
1299 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1300 gvp = hv && HvENAME(hv)
1301 ? (GV**)hv_fetch(hv, GvNAME(egv), HEK_UTF8(GvNAME_HEK(egv)) ? -GvNAMELEN(egv) : GvNAMELEN(egv), FALSE)
1303 if (gvp && *gvp == egv) {
1304 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1308 mXPUSHs(newRV(MUTABLE_SV(egv)));
1312 if (!GvIO(newdefout))
1313 gv_IOadd(newdefout);
1314 setdefout(newdefout);
1324 MAXARG==0 || (!TOPs && !POPs) ? PL_stdingv : MUTABLE_GV(POPs);
1325 IO *const io = GvIO(gv);
1331 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1333 const U32 gimme = GIMME_V;
1334 Perl_tied_method(aTHX_ SV_CONST(GETC), SP, MUTABLE_SV(io), mg, gimme, 0);
1335 if (gimme == G_SCALAR) {
1337 SvSetMagicSV_nosteal(TARG, TOPs);
1342 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1343 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1345 SETERRNO(EBADF,RMS_IFI);
1349 sv_setpvs(TARG, " ");
1350 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1351 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1352 /* Find out how many bytes the char needs */
1353 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1356 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1357 SvCUR_set(TARG,1+len);
1361 else SvUTF8_off(TARG);
1367 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1370 const I32 gimme = GIMME_V;
1372 PERL_ARGS_ASSERT_DOFORM;
1375 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1380 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1381 PUSHFORMAT(cx, retop);
1382 if (CvDEPTH(cv) >= 2) {
1383 PERL_STACK_OVERFLOW_CHECK();
1384 pad_push(CvPADLIST(cv), CvDEPTH(cv));
1387 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv));
1389 setdefout(gv); /* locally select filehandle so $% et al work */
1407 gv = MUTABLE_GV(POPs);
1424 tmpsv = sv_newmortal();
1425 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1426 DIE(aTHX_ "Undefined format \"%"SVf"\" called", SVfARG(tmpsv));
1428 IoFLAGS(io) &= ~IOf_DIDTOP;
1429 RETURNOP(doform(cv,gv,PL_op->op_next));
1435 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1436 IO * const io = GvIOp(gv);
1443 bool is_return = cBOOL(PL_op->op_type == OP_RETURN);
1445 if (is_return || !io || !(ofp = IoOFP(io)))
1448 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1449 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1451 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1452 PL_formtarget != PL_toptarget)
1456 if (!IoTOP_GV(io)) {
1459 if (!IoTOP_NAME(io)) {
1461 if (!IoFMT_NAME(io))
1462 IoFMT_NAME(io) = savepv(GvNAME(gv));
1463 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%"HEKf"_TOP",
1464 HEKfARG(GvNAME_HEK(gv))));
1465 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1466 if ((topgv && GvFORM(topgv)) ||
1467 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1468 IoTOP_NAME(io) = savesvpv(topname);
1470 IoTOP_NAME(io) = savepvs("top");
1472 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1473 if (!topgv || !GvFORM(topgv)) {
1474 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1477 IoTOP_GV(io) = topgv;
1479 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1480 I32 lines = IoLINES_LEFT(io);
1481 const char *s = SvPVX_const(PL_formtarget);
1482 if (lines <= 0) /* Yow, header didn't even fit!!! */
1484 while (lines-- > 0) {
1485 s = strchr(s, '\n');
1491 const STRLEN save = SvCUR(PL_formtarget);
1492 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1493 do_print(PL_formtarget, ofp);
1494 SvCUR_set(PL_formtarget, save);
1495 sv_chop(PL_formtarget, s);
1496 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1499 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1500 do_print(GvSV(gv_fetchpvs("\f", GV_ADD, SVt_PV)), ofp);
1501 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1503 PL_formtarget = PL_toptarget;
1504 IoFLAGS(io) |= IOf_DIDTOP;
1506 assert(fgv); /* IoTOP_GV(io) should have been set above */
1509 SV * const sv = sv_newmortal();
1510 gv_efullname4(sv, fgv, NULL, FALSE);
1511 DIE(aTHX_ "Undefined top format \"%"SVf"\" called", SVfARG(sv));
1513 return doform(cv, gv, PL_op);
1517 POPBLOCK(cx,PL_curpm);
1518 retop = cx->blk_sub.retop;
1520 SP = newsp; /* ignore retval of formline */
1524 /* XXX the semantics of doing 'return' in a format aren't documented.
1525 * Currently we ignore any args to 'return' and just return
1526 * a single undef in both scalar and list contexts
1528 PUSHs(&PL_sv_undef);
1529 else if (!io || !(fp = IoOFP(io))) {
1530 if (io && IoIFP(io))
1531 report_wrongway_fh(gv, '<');
1537 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1538 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1540 if (!do_print(PL_formtarget, fp))
1543 FmLINES(PL_formtarget) = 0;
1544 SvCUR_set(PL_formtarget, 0);
1545 *SvEND(PL_formtarget) = '\0';
1546 if (IoFLAGS(io) & IOf_FLUSH)
1547 (void)PerlIO_flush(fp);
1551 PL_formtarget = PL_bodytarget;
1552 PERL_UNUSED_VAR(gimme);
1558 dSP; dMARK; dORIGMARK;
1562 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1563 IO *const io = GvIO(gv);
1565 /* Treat empty list as "" */
1566 if (MARK == SP) XPUSHs(&PL_sv_no);
1569 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1571 if (MARK == ORIGMARK) {
1574 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1577 return Perl_tied_method(aTHX_ SV_CONST(PRINTF), mark - 1, MUTABLE_SV(io),
1579 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1586 SETERRNO(EBADF,RMS_IFI);
1589 else if (!(fp = IoOFP(io))) {
1591 report_wrongway_fh(gv, '<');
1592 else if (ckWARN(WARN_CLOSED))
1594 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1598 SV *sv = sv_newmortal();
1599 do_sprintf(sv, SP - MARK, MARK + 1);
1600 if (!do_print(sv, fp))
1603 if (IoFLAGS(io) & IOf_FLUSH)
1604 if (PerlIO_flush(fp) == EOF)
1613 PUSHs(&PL_sv_undef);
1620 const int perm = (MAXARG > 3 && (TOPs || POPs)) ? POPi : 0666;
1621 const int mode = POPi;
1622 SV * const sv = POPs;
1623 GV * const gv = MUTABLE_GV(POPs);
1626 /* Need TIEHANDLE method ? */
1627 const char * const tmps = SvPV_const(sv, len);
1628 if (do_open_raw(gv, tmps, len, mode, perm)) {
1629 IoLINES(GvIOp(gv)) = 0;
1633 PUSHs(&PL_sv_undef);
1639 /* also used for: pp_read() and pp_recv() (where supported) */
1643 dSP; dMARK; dORIGMARK; dTARGET;
1657 bool charstart = FALSE;
1658 STRLEN charskip = 0;
1660 GV * const gv = MUTABLE_GV(*++MARK);
1663 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1664 && gv && (io = GvIO(gv)) )
1666 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1668 return Perl_tied_method(aTHX_ SV_CONST(READ), mark - 1, MUTABLE_SV(io), mg,
1669 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1678 sv_setpvs(bufsv, "");
1679 length = SvIVx(*++MARK);
1681 DIE(aTHX_ "Negative length");
1684 offset = SvIVx(*++MARK);
1688 if (!io || !IoIFP(io)) {
1690 SETERRNO(EBADF,RMS_IFI);
1694 /* Note that fd can here validly be -1, don't check it yet. */
1695 fd = PerlIO_fileno(IoIFP(io));
1697 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1698 if (PL_op->op_type == OP_SYSREAD || PL_op->op_type == OP_RECV) {
1699 Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED),
1700 "%s() is deprecated on :utf8 handles",
1703 buffer = SvPVutf8_force(bufsv, blen);
1704 /* UTF-8 may not have been set if they are all low bytes */
1709 buffer = SvPV_force(bufsv, blen);
1710 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1712 if (DO_UTF8(bufsv)) {
1713 blen = sv_len_utf8_nomg(bufsv);
1722 if (PL_op->op_type == OP_RECV) {
1723 Sock_size_t bufsize;
1724 char namebuf[MAXPATHLEN];
1726 SETERRNO(EBADF,SS_IVCHAN);
1729 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(__QNXNTO__)
1730 bufsize = sizeof (struct sockaddr_in);
1732 bufsize = sizeof namebuf;
1734 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1738 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1739 /* 'offset' means 'flags' here */
1740 count = PerlSock_recvfrom(fd, buffer, length, offset,
1741 (struct sockaddr *)namebuf, &bufsize);
1744 /* MSG_TRUNC can give oversized count; quietly lose it */
1747 SvCUR_set(bufsv, count);
1748 *SvEND(bufsv) = '\0';
1749 (void)SvPOK_only(bufsv);
1753 /* This should not be marked tainted if the fp is marked clean */
1754 if (!(IoFLAGS(io) & IOf_UNTAINT))
1755 SvTAINTED_on(bufsv);
1757 #if defined(__CYGWIN__)
1758 /* recvfrom() on cygwin doesn't set bufsize at all for
1759 connected sockets, leaving us with trash in the returned
1760 name, so use the same test as the Win32 code to check if it
1761 wasn't set, and set it [perl #118843] */
1762 if (bufsize == sizeof namebuf)
1765 sv_setpvn(TARG, namebuf, bufsize);
1771 if (-offset > (SSize_t)blen)
1772 DIE(aTHX_ "Offset outside string");
1775 if (DO_UTF8(bufsv)) {
1776 /* convert offset-as-chars to offset-as-bytes */
1777 if (offset >= (SSize_t)blen)
1778 offset += SvCUR(bufsv) - blen;
1780 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1784 /* Reestablish the fd in case it shifted from underneath us. */
1785 fd = PerlIO_fileno(IoIFP(io));
1787 orig_size = SvCUR(bufsv);
1788 /* Allocating length + offset + 1 isn't perfect in the case of reading
1789 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1791 (should be 2 * length + offset + 1, or possibly something longer if
1792 IN_ENCODING Is true) */
1793 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1794 if (offset > 0 && offset > (SSize_t)orig_size) { /* Zero any newly allocated space */
1795 Zero(buffer+orig_size, offset-orig_size, char);
1797 buffer = buffer + offset;
1799 read_target = bufsv;
1801 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1802 concatenate it to the current buffer. */
1804 /* Truncate the existing buffer to the start of where we will be
1806 SvCUR_set(bufsv, offset);
1808 read_target = sv_newmortal();
1809 SvUPGRADE(read_target, SVt_PV);
1810 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1813 if (PL_op->op_type == OP_SYSREAD) {
1814 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1815 if (IoTYPE(io) == IoTYPE_SOCKET) {
1817 SETERRNO(EBADF,SS_IVCHAN);
1821 count = PerlSock_recv(fd, buffer, length, 0);
1827 SETERRNO(EBADF,RMS_IFI);
1831 count = PerlLIO_read(fd, buffer, length);
1836 count = PerlIO_read(IoIFP(io), buffer, length);
1837 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1838 if (count == 0 && PerlIO_error(IoIFP(io)))
1842 if (IoTYPE(io) == IoTYPE_WRONLY)
1843 report_wrongway_fh(gv, '>');
1846 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1847 *SvEND(read_target) = '\0';
1848 (void)SvPOK_only(read_target);
1849 if (fp_utf8 && !IN_BYTES) {
1850 /* Look at utf8 we got back and count the characters */
1851 const char *bend = buffer + count;
1852 while (buffer < bend) {
1854 skip = UTF8SKIP(buffer);
1857 if (buffer - charskip + skip > bend) {
1858 /* partial character - try for rest of it */
1859 length = skip - (bend-buffer);
1860 offset = bend - SvPVX_const(bufsv);
1872 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1873 provided amount read (count) was what was requested (length)
1875 if (got < wanted && count == length) {
1876 length = wanted - got;
1877 offset = bend - SvPVX_const(bufsv);
1880 /* return value is character count */
1884 else if (buffer_utf8) {
1885 /* Let svcatsv upgrade the bytes we read in to utf8.
1886 The buffer is a mortal so will be freed soon. */
1887 sv_catsv_nomg(bufsv, read_target);
1890 /* This should not be marked tainted if the fp is marked clean */
1891 if (!(IoFLAGS(io) & IOf_UNTAINT))
1892 SvTAINTED_on(bufsv);
1903 /* also used for: pp_send() where defined */
1907 dSP; dMARK; dORIGMARK; dTARGET;
1912 STRLEN orig_blen_bytes;
1913 const int op_type = PL_op->op_type;
1916 GV *const gv = MUTABLE_GV(*++MARK);
1917 IO *const io = GvIO(gv);
1920 if (op_type == OP_SYSWRITE && io) {
1921 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1923 if (MARK == SP - 1) {
1925 mXPUSHi(sv_len(sv));
1929 return Perl_tied_method(aTHX_ SV_CONST(WRITE), mark - 1, MUTABLE_SV(io), mg,
1930 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1940 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1942 if (io && IoIFP(io))
1943 report_wrongway_fh(gv, '<');
1946 SETERRNO(EBADF,RMS_IFI);
1949 fd = PerlIO_fileno(IoIFP(io));
1951 SETERRNO(EBADF,SS_IVCHAN);
1956 /* Do this first to trigger any overloading. */
1957 buffer = SvPV_const(bufsv, blen);
1958 orig_blen_bytes = blen;
1959 doing_utf8 = DO_UTF8(bufsv);
1961 if (PerlIO_isutf8(IoIFP(io))) {
1962 Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED),
1963 "%s() is deprecated on :utf8 handles",
1965 if (!SvUTF8(bufsv)) {
1966 /* We don't modify the original scalar. */
1967 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1968 buffer = (char *) tmpbuf;
1972 else if (doing_utf8) {
1973 STRLEN tmplen = blen;
1974 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1977 buffer = (char *) tmpbuf;
1981 assert((char *)result == buffer);
1982 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1987 if (op_type == OP_SEND) {
1988 const int flags = SvIVx(*++MARK);
1991 char * const sockbuf = SvPVx(*++MARK, mlen);
1992 retval = PerlSock_sendto(fd, buffer, blen,
1993 flags, (struct sockaddr *)sockbuf, mlen);
1996 retval = PerlSock_send(fd, buffer, blen, flags);
2002 Size_t length = 0; /* This length is in characters. */
2008 /* The SV is bytes, and we've had to upgrade it. */
2009 blen_chars = orig_blen_bytes;
2011 /* The SV really is UTF-8. */
2012 /* Don't call sv_len_utf8 on a magical or overloaded
2013 scalar, as we might get back a different result. */
2014 blen_chars = sv_or_pv_len_utf8(bufsv, buffer, blen);
2021 length = blen_chars;
2023 #if Size_t_size > IVSIZE
2024 length = (Size_t)SvNVx(*++MARK);
2026 length = (Size_t)SvIVx(*++MARK);
2028 if ((SSize_t)length < 0) {
2030 DIE(aTHX_ "Negative length");
2035 offset = SvIVx(*++MARK);
2037 if (-offset > (IV)blen_chars) {
2039 DIE(aTHX_ "Offset outside string");
2041 offset += blen_chars;
2042 } else if (offset > (IV)blen_chars) {
2044 DIE(aTHX_ "Offset outside string");
2048 if (length > blen_chars - offset)
2049 length = blen_chars - offset;
2051 /* Here we convert length from characters to bytes. */
2052 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
2053 /* Either we had to convert the SV, or the SV is magical, or
2054 the SV has overloading, in which case we can't or mustn't
2055 or mustn't call it again. */
2057 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
2058 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
2060 /* It's a real UTF-8 SV, and it's not going to change under
2061 us. Take advantage of any cache. */
2063 I32 len_I32 = length;
2065 /* Convert the start and end character positions to bytes.
2066 Remember that the second argument to sv_pos_u2b is relative
2068 sv_pos_u2b(bufsv, &start, &len_I32);
2075 buffer = buffer+offset;
2077 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
2078 if (IoTYPE(io) == IoTYPE_SOCKET) {
2079 retval = PerlSock_send(fd, buffer, length, 0);
2084 /* See the note at doio.c:do_print about filesize limits. --jhi */
2085 retval = PerlLIO_write(fd, buffer, length);
2093 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2096 #if Size_t_size > IVSIZE
2116 * in Perl 5.12 and later, the additional parameter is a bitmask:
2119 * 2 = eof() <- ARGV magic
2121 * I'll rely on the compiler's trace flow analysis to decide whether to
2122 * actually assign this out here, or punt it into the only block where it is
2123 * used. Doing it out here is DRY on the condition logic.
2128 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2134 if (PL_op->op_flags & OPf_SPECIAL) {
2135 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2139 gv = PL_last_in_gv; /* eof */
2147 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2148 return tied_method1(SV_CONST(EOF), SP, MUTABLE_SV(io), mg, newSVuv(which));
2151 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2152 if (io && !IoIFP(io)) {
2153 if ((IoFLAGS(io) & IOf_START) && av_tindex(GvAVn(gv)) < 0) {
2156 IoFLAGS(io) &= ~IOf_START;
2157 do_open6(gv, "-", 1, NULL, NULL, 0);
2165 *svp = newSVpvs("-");
2167 else if (!nextargv(gv, FALSE))
2172 PUSHs(boolSV(do_eof(gv)));
2182 if (MAXARG != 0 && (TOPs || POPs))
2183 PL_last_in_gv = MUTABLE_GV(POPs);
2190 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2192 return tied_method0(SV_CONST(TELL), SP, MUTABLE_SV(io), mg);
2197 SETERRNO(EBADF,RMS_IFI);
2202 #if LSEEKSIZE > IVSIZE
2203 PUSHn( do_tell(gv) );
2205 PUSHi( do_tell(gv) );
2211 /* also used for: pp_seek() */
2216 const int whence = POPi;
2217 #if LSEEKSIZE > IVSIZE
2218 const Off_t offset = (Off_t)SvNVx(POPs);
2220 const Off_t offset = (Off_t)SvIVx(POPs);
2223 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2224 IO *const io = GvIO(gv);
2227 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2229 #if LSEEKSIZE > IVSIZE
2230 SV *const offset_sv = newSVnv((NV) offset);
2232 SV *const offset_sv = newSViv(offset);
2235 return tied_method2(SV_CONST(SEEK), SP, MUTABLE_SV(io), mg, offset_sv,
2240 if (PL_op->op_type == OP_SEEK)
2241 PUSHs(boolSV(do_seek(gv, offset, whence)));
2243 const Off_t sought = do_sysseek(gv, offset, whence);
2245 PUSHs(&PL_sv_undef);
2247 SV* const sv = sought ?
2248 #if LSEEKSIZE > IVSIZE
2253 : newSVpvn(zero_but_true, ZBTLEN);
2263 /* There seems to be no consensus on the length type of truncate()
2264 * and ftruncate(), both off_t and size_t have supporters. In
2265 * general one would think that when using large files, off_t is
2266 * at least as wide as size_t, so using an off_t should be okay. */
2267 /* XXX Configure probe for the length type of *truncate() needed XXX */
2270 #if Off_t_size > IVSIZE
2275 /* Checking for length < 0 is problematic as the type might or
2276 * might not be signed: if it is not, clever compilers will moan. */
2277 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2280 SV * const sv = POPs;
2285 if (PL_op->op_flags & OPf_SPECIAL
2286 ? (tmpgv = gv_fetchsv(sv, 0, SVt_PVIO), 1)
2287 : !!(tmpgv = MAYBE_DEREF_GV(sv)) ) {
2294 TAINT_PROPER("truncate");
2295 if (!(fp = IoIFP(io))) {
2299 int fd = PerlIO_fileno(fp);
2301 SETERRNO(EBADF,RMS_IFI);
2305 SETERRNO(EINVAL, LIB_INVARG);
2310 if (ftruncate(fd, len) < 0)
2312 if (my_chsize(fd, len) < 0)
2320 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2321 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2322 goto do_ftruncate_io;
2325 const char * const name = SvPV_nomg_const_nolen(sv);
2326 TAINT_PROPER("truncate");
2328 if (truncate(name, len) < 0)
2335 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
2336 mode |= O_LARGEFILE; /* Transparently largefiley. */
2339 /* On open(), the Win32 CRT tries to seek around text
2340 * files using 32-bit offsets, which causes the open()
2341 * to fail on large files, so open in binary mode.
2345 tmpfd = PerlLIO_open(name, mode);
2350 if (my_chsize(tmpfd, len) < 0)
2352 PerlLIO_close(tmpfd);
2361 SETERRNO(EBADF,RMS_IFI);
2367 /* also used for: pp_fcntl() */
2372 SV * const argsv = POPs;
2373 const unsigned int func = POPu;
2375 GV * const gv = MUTABLE_GV(POPs);
2376 IO * const io = GvIOn(gv);
2382 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2386 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2389 s = SvPV_force(argsv, len);
2390 need = IOCPARM_LEN(func);
2392 s = Sv_Grow(argsv, need + 1);
2393 SvCUR_set(argsv, need);
2396 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2399 retval = SvIV(argsv);
2400 s = INT2PTR(char*,retval); /* ouch */
2403 optype = PL_op->op_type;
2404 TAINT_PROPER(PL_op_desc[optype]);
2406 if (optype == OP_IOCTL)
2408 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2410 DIE(aTHX_ "ioctl is not implemented");
2414 DIE(aTHX_ "fcntl is not implemented");
2416 #if defined(OS2) && defined(__EMX__)
2417 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2419 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2423 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2425 if (s[SvCUR(argsv)] != 17)
2426 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2428 s[SvCUR(argsv)] = 0; /* put our null back */
2429 SvSETMAGIC(argsv); /* Assume it has changed */
2438 PUSHp(zero_but_true, ZBTLEN);
2449 const int argtype = POPi;
2450 GV * const gv = MUTABLE_GV(POPs);
2451 IO *const io = GvIO(gv);
2452 PerlIO *const fp = io ? IoIFP(io) : NULL;
2454 /* XXX Looks to me like io is always NULL at this point */
2456 (void)PerlIO_flush(fp);
2457 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2462 SETERRNO(EBADF,RMS_IFI);
2467 DIE(aTHX_ PL_no_func, "flock");
2478 const int protocol = POPi;
2479 const int type = POPi;
2480 const int domain = POPi;
2481 GV * const gv = MUTABLE_GV(POPs);
2482 IO * const io = GvIOn(gv);
2486 do_close(gv, FALSE);
2488 TAINT_PROPER("socket");
2489 fd = PerlSock_socket(domain, type, protocol);
2491 SETERRNO(EBADF,RMS_IFI);
2494 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2495 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2496 IoTYPE(io) = IoTYPE_SOCKET;
2497 if (!IoIFP(io) || !IoOFP(io)) {
2498 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2499 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2500 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2503 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2504 /* ensure close-on-exec */
2505 if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
2515 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2518 const int protocol = POPi;
2519 const int type = POPi;
2520 const int domain = POPi;
2522 GV * const gv2 = MUTABLE_GV(POPs);
2523 IO * const io2 = GvIOn(gv2);
2524 GV * const gv1 = MUTABLE_GV(POPs);
2525 IO * const io1 = GvIOn(gv1);
2528 do_close(gv1, FALSE);
2530 do_close(gv2, FALSE);
2532 TAINT_PROPER("socketpair");
2533 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2535 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2536 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2537 IoTYPE(io1) = IoTYPE_SOCKET;
2538 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2539 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2540 IoTYPE(io2) = IoTYPE_SOCKET;
2541 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2542 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2543 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2544 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2545 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2546 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2547 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2550 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2551 /* ensure close-on-exec */
2552 if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
2553 (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
2559 DIE(aTHX_ PL_no_sock_func, "socketpair");
2565 /* also used for: pp_connect() */
2570 SV * const addrsv = POPs;
2571 /* OK, so on what platform does bind modify addr? */
2573 GV * const gv = MUTABLE_GV(POPs);
2574 IO * const io = GvIOn(gv);
2581 fd = PerlIO_fileno(IoIFP(io));
2585 addr = SvPV_const(addrsv, len);
2586 op_type = PL_op->op_type;
2587 TAINT_PROPER(PL_op_desc[op_type]);
2588 if ((op_type == OP_BIND
2589 ? PerlSock_bind(fd, (struct sockaddr *)addr, len)
2590 : PerlSock_connect(fd, (struct sockaddr *)addr, len))
2598 SETERRNO(EBADF,SS_IVCHAN);
2605 const int backlog = POPi;
2606 GV * const gv = MUTABLE_GV(POPs);
2607 IO * const io = GvIOn(gv);
2612 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2619 SETERRNO(EBADF,SS_IVCHAN);
2627 char namebuf[MAXPATHLEN];
2628 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(__QNXNTO__)
2629 Sock_size_t len = sizeof (struct sockaddr_in);
2631 Sock_size_t len = sizeof namebuf;
2633 GV * const ggv = MUTABLE_GV(POPs);
2634 GV * const ngv = MUTABLE_GV(POPs);
2637 IO * const gstio = GvIO(ggv);
2638 if (!gstio || !IoIFP(gstio))
2642 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2645 /* Some platforms indicate zero length when an AF_UNIX client is
2646 * not bound. Simulate a non-zero-length sockaddr structure in
2648 namebuf[0] = 0; /* sun_len */
2649 namebuf[1] = AF_UNIX; /* sun_family */
2657 do_close(ngv, FALSE);
2658 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2659 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2660 IoTYPE(nstio) = IoTYPE_SOCKET;
2661 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2662 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2663 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2664 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2667 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2668 /* ensure close-on-exec */
2669 if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
2673 #ifdef __SCO_VERSION__
2674 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2677 PUSHp(namebuf, len);
2681 report_evil_fh(ggv);
2682 SETERRNO(EBADF,SS_IVCHAN);
2692 const int how = POPi;
2693 GV * const gv = MUTABLE_GV(POPs);
2694 IO * const io = GvIOn(gv);
2699 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2704 SETERRNO(EBADF,SS_IVCHAN);
2709 /* also used for: pp_gsockopt() */
2714 const int optype = PL_op->op_type;
2715 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2716 const unsigned int optname = (unsigned int) POPi;
2717 const unsigned int lvl = (unsigned int) POPi;
2718 GV * const gv = MUTABLE_GV(POPs);
2719 IO * const io = GvIOn(gv);
2726 fd = PerlIO_fileno(IoIFP(io));
2732 (void)SvPOK_only(sv);
2736 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2739 /* XXX Configure test: does getsockopt set the length properly? */
2748 #if defined(__SYMBIAN32__)
2749 # define SETSOCKOPT_OPTION_VALUE_T void *
2751 # define SETSOCKOPT_OPTION_VALUE_T const char *
2753 /* XXX TODO: We need to have a proper type (a Configure probe,
2754 * etc.) for what the C headers think of the third argument of
2755 * setsockopt(), the option_value read-only buffer: is it
2756 * a "char *", or a "void *", const or not. Some compilers
2757 * don't take kindly to e.g. assuming that "char *" implicitly
2758 * promotes to a "void *", or to explicitly promoting/demoting
2759 * consts to non/vice versa. The "const void *" is the SUS
2760 * definition, but that does not fly everywhere for the above
2762 SETSOCKOPT_OPTION_VALUE_T buf;
2766 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2770 aint = (int)SvIV(sv);
2771 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2774 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2784 SETERRNO(EBADF,SS_IVCHAN);
2791 /* also used for: pp_getsockname() */
2796 const int optype = PL_op->op_type;
2797 GV * const gv = MUTABLE_GV(POPs);
2798 IO * const io = GvIOn(gv);
2806 sv = sv_2mortal(newSV(257));
2807 (void)SvPOK_only(sv);
2811 fd = PerlIO_fileno(IoIFP(io));
2815 case OP_GETSOCKNAME:
2816 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2819 case OP_GETPEERNAME:
2820 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2822 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2824 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";
2825 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2826 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2827 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2828 sizeof(u_short) + sizeof(struct in_addr))) {
2835 #ifdef BOGUS_GETNAME_RETURN
2836 /* Interactive Unix, getpeername() and getsockname()
2837 does not return valid namelen */
2838 if (len == BOGUS_GETNAME_RETURN)
2839 len = sizeof(struct sockaddr);
2848 SETERRNO(EBADF,SS_IVCHAN);
2857 /* also used for: pp_lstat() */
2868 if (PL_op->op_flags & OPf_REF ? (gv = cGVOP_gv, 1)
2869 : !!(sv=POPs, gv = MAYBE_DEREF_GV(sv))) {
2870 if (PL_op->op_type == OP_LSTAT) {
2871 if (gv != PL_defgv) {
2872 do_fstat_warning_check:
2873 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2874 "lstat() on filehandle%s%"SVf,
2877 ? sv_2mortal(newSVhek(GvENAME_HEK(gv)))
2879 } else if (PL_laststype != OP_LSTAT)
2880 /* diag_listed_as: The stat preceding %s wasn't an lstat */
2881 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2884 if (gv != PL_defgv) {
2888 PL_laststype = OP_STAT;
2889 PL_statgv = gv ? gv : (GV *)io;
2890 sv_setpvs(PL_statname, "");
2896 int fd = PerlIO_fileno(IoIFP(io));
2898 PL_laststatval = -1;
2899 SETERRNO(EBADF,RMS_IFI);
2901 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
2904 } else if (IoDIRP(io)) {
2906 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2909 PL_laststatval = -1;
2912 else PL_laststatval = -1;
2913 if (PL_laststatval < 0 && !havefp) report_evil_fh(gv);
2916 if (PL_laststatval < 0) {
2922 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2923 io = MUTABLE_IO(SvRV(sv));
2924 if (PL_op->op_type == OP_LSTAT)
2925 goto do_fstat_warning_check;
2926 goto do_fstat_have_io;
2929 SvTAINTED_off(PL_statname); /* previous tainting irrelevant */
2930 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
2932 PL_laststype = PL_op->op_type;
2933 file = SvPV_nolen_const(PL_statname);
2934 if (PL_op->op_type == OP_LSTAT)
2935 PL_laststatval = PerlLIO_lstat(file, &PL_statcache);
2937 PL_laststatval = PerlLIO_stat(file, &PL_statcache);
2938 if (PL_laststatval < 0) {
2939 if (ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
2940 /* PL_warn_nl is constant */
2941 GCC_DIAG_IGNORE(-Wformat-nonliteral);
2942 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2950 if (gimme != G_ARRAY) {
2951 if (gimme != G_VOID)
2952 XPUSHs(boolSV(max));
2958 mPUSHi(PL_statcache.st_dev);
2959 #if ST_INO_SIZE > IVSIZE
2960 mPUSHn(PL_statcache.st_ino);
2962 # if ST_INO_SIGN <= 0
2963 mPUSHi(PL_statcache.st_ino);
2965 mPUSHu(PL_statcache.st_ino);
2968 mPUSHu(PL_statcache.st_mode);
2969 mPUSHu(PL_statcache.st_nlink);
2971 sv_setuid(PUSHmortal, PL_statcache.st_uid);
2972 sv_setgid(PUSHmortal, PL_statcache.st_gid);
2974 #ifdef USE_STAT_RDEV
2975 mPUSHi(PL_statcache.st_rdev);
2977 PUSHs(newSVpvs_flags("", SVs_TEMP));
2979 #if Off_t_size > IVSIZE
2980 mPUSHn(PL_statcache.st_size);
2982 mPUSHi(PL_statcache.st_size);
2985 mPUSHn(PL_statcache.st_atime);
2986 mPUSHn(PL_statcache.st_mtime);
2987 mPUSHn(PL_statcache.st_ctime);
2989 mPUSHi(PL_statcache.st_atime);
2990 mPUSHi(PL_statcache.st_mtime);
2991 mPUSHi(PL_statcache.st_ctime);
2993 #ifdef USE_STAT_BLOCKS
2994 mPUSHu(PL_statcache.st_blksize);
2995 mPUSHu(PL_statcache.st_blocks);
2997 PUSHs(newSVpvs_flags("", SVs_TEMP));
2998 PUSHs(newSVpvs_flags("", SVs_TEMP));
3004 /* All filetest ops avoid manipulating the perl stack pointer in their main
3005 bodies (since commit d2c4d2d1e22d3125), and return using either
3006 S_ft_return_false() or S_ft_return_true(). These two helper functions are
3007 the only two which manipulate the perl stack. To ensure that no stack
3008 manipulation macros are used, the filetest ops avoid defining a local copy
3009 of the stack pointer with dSP. */
3011 /* If the next filetest is stacked up with this one
3012 (PL_op->op_private & OPpFT_STACKING), we leave
3013 the original argument on the stack for success,
3014 and skip the stacked operators on failure.
3015 The next few macros/functions take care of this.
3019 S_ft_return_false(pTHX_ SV *ret) {
3023 if (PL_op->op_flags & OPf_REF) XPUSHs(ret);
3027 if (PL_op->op_private & OPpFT_STACKING) {
3028 while (OP_IS_FILETEST(next->op_type)
3029 && next->op_private & OPpFT_STACKED)
3030 next = next->op_next;
3035 PERL_STATIC_INLINE OP *
3036 S_ft_return_true(pTHX_ SV *ret) {
3038 if (PL_op->op_flags & OPf_REF)
3039 XPUSHs(PL_op->op_private & OPpFT_STACKING ? (SV *)cGVOP_gv : (ret));
3040 else if (!(PL_op->op_private & OPpFT_STACKING))
3046 #define FT_RETURNNO return S_ft_return_false(aTHX_ &PL_sv_no)
3047 #define FT_RETURNUNDEF return S_ft_return_false(aTHX_ &PL_sv_undef)
3048 #define FT_RETURNYES return S_ft_return_true(aTHX_ &PL_sv_yes)
3050 #define tryAMAGICftest_MG(chr) STMT_START { \
3051 if ( (SvFLAGS(*PL_stack_sp) & (SVf_ROK|SVs_GMG)) \
3052 && PL_op->op_flags & OPf_KIDS) { \
3053 OP *next = S_try_amagic_ftest(aTHX_ chr); \
3054 if (next) return next; \
3059 S_try_amagic_ftest(pTHX_ char chr) {
3060 SV *const arg = *PL_stack_sp;
3063 if (!(PL_op->op_private & OPpFT_STACKING)) SvGETMAGIC(arg);
3067 const char tmpchr = chr;
3068 SV * const tmpsv = amagic_call(arg,
3069 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
3070 ftest_amg, AMGf_unary);
3075 return SvTRUE(tmpsv)
3076 ? S_ft_return_true(aTHX_ tmpsv) : S_ft_return_false(aTHX_ tmpsv);
3082 /* also used for: pp_fteexec() pp_fteread() pp_ftewrite() pp_ftrexec()
3088 /* Not const, because things tweak this below. Not bool, because there's
3089 no guarantee that OPpFT_ACCESS is <= CHAR_MAX */
3090 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3091 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3092 /* Giving some sort of initial value silences compilers. */
3094 int access_mode = R_OK;
3096 int access_mode = 0;
3099 /* access_mode is never used, but leaving use_access in makes the
3100 conditional compiling below much clearer. */
3103 Mode_t stat_mode = S_IRUSR;
3105 bool effective = FALSE;
3108 switch (PL_op->op_type) {
3109 case OP_FTRREAD: opchar = 'R'; break;
3110 case OP_FTRWRITE: opchar = 'W'; break;
3111 case OP_FTREXEC: opchar = 'X'; break;
3112 case OP_FTEREAD: opchar = 'r'; break;
3113 case OP_FTEWRITE: opchar = 'w'; break;
3114 case OP_FTEEXEC: opchar = 'x'; break;
3116 tryAMAGICftest_MG(opchar);
3118 switch (PL_op->op_type) {
3120 #if !(defined(HAS_ACCESS) && defined(R_OK))
3126 #if defined(HAS_ACCESS) && defined(W_OK)
3131 stat_mode = S_IWUSR;
3135 #if defined(HAS_ACCESS) && defined(X_OK)
3140 stat_mode = S_IXUSR;
3144 #ifdef PERL_EFF_ACCESS
3147 stat_mode = S_IWUSR;
3151 #ifndef PERL_EFF_ACCESS
3158 #ifdef PERL_EFF_ACCESS
3163 stat_mode = S_IXUSR;
3169 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3170 const char *name = SvPV_nolen(*PL_stack_sp);
3172 # ifdef PERL_EFF_ACCESS
3173 result = PERL_EFF_ACCESS(name, access_mode);
3175 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3181 result = access(name, access_mode);
3183 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3194 result = my_stat_flags(0);
3197 if (cando(stat_mode, effective, &PL_statcache))
3203 /* also used for: pp_ftatime() pp_ftctime() pp_ftmtime() pp_ftsize() */
3208 const int op_type = PL_op->op_type;
3212 case OP_FTIS: opchar = 'e'; break;
3213 case OP_FTSIZE: opchar = 's'; break;
3214 case OP_FTMTIME: opchar = 'M'; break;
3215 case OP_FTCTIME: opchar = 'C'; break;
3216 case OP_FTATIME: opchar = 'A'; break;
3218 tryAMAGICftest_MG(opchar);
3220 result = my_stat_flags(0);
3223 if (op_type == OP_FTIS)
3226 /* You can't dTARGET inside OP_FTIS, because you'll get
3227 "panic: pad_sv po" - the op is not flagged to have a target. */
3231 #if Off_t_size > IVSIZE
3232 sv_setnv(TARG, (NV)PL_statcache.st_size);
3234 sv_setiv(TARG, (IV)PL_statcache.st_size);
3239 ((NV)PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3243 ((NV)PL_basetime - PL_statcache.st_atime) / 86400.0 );
3247 ((NV)PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3251 return SvTRUE_nomg(TARG)
3252 ? S_ft_return_true(aTHX_ TARG) : S_ft_return_false(aTHX_ TARG);
3257 /* also used for: pp_ftblk() pp_ftchr() pp_ftdir() pp_fteowned()
3258 * pp_ftfile() pp_ftpipe() pp_ftsgid() pp_ftsock()
3259 * pp_ftsuid() pp_ftsvtx() pp_ftzero() */
3266 switch (PL_op->op_type) {
3267 case OP_FTROWNED: opchar = 'O'; break;
3268 case OP_FTEOWNED: opchar = 'o'; break;
3269 case OP_FTZERO: opchar = 'z'; break;
3270 case OP_FTSOCK: opchar = 'S'; break;
3271 case OP_FTCHR: opchar = 'c'; break;
3272 case OP_FTBLK: opchar = 'b'; break;
3273 case OP_FTFILE: opchar = 'f'; break;
3274 case OP_FTDIR: opchar = 'd'; break;
3275 case OP_FTPIPE: opchar = 'p'; break;
3276 case OP_FTSUID: opchar = 'u'; break;
3277 case OP_FTSGID: opchar = 'g'; break;
3278 case OP_FTSVTX: opchar = 'k'; break;
3280 tryAMAGICftest_MG(opchar);
3282 /* I believe that all these three are likely to be defined on most every
3283 system these days. */
3285 if(PL_op->op_type == OP_FTSUID) {
3290 if(PL_op->op_type == OP_FTSGID) {
3295 if(PL_op->op_type == OP_FTSVTX) {
3300 result = my_stat_flags(0);
3303 switch (PL_op->op_type) {
3305 if (PL_statcache.st_uid == PerlProc_getuid())
3309 if (PL_statcache.st_uid == PerlProc_geteuid())
3313 if (PL_statcache.st_size == 0)
3317 if (S_ISSOCK(PL_statcache.st_mode))
3321 if (S_ISCHR(PL_statcache.st_mode))
3325 if (S_ISBLK(PL_statcache.st_mode))
3329 if (S_ISREG(PL_statcache.st_mode))
3333 if (S_ISDIR(PL_statcache.st_mode))
3337 if (S_ISFIFO(PL_statcache.st_mode))
3342 if (PL_statcache.st_mode & S_ISUID)
3348 if (PL_statcache.st_mode & S_ISGID)
3354 if (PL_statcache.st_mode & S_ISVTX)
3366 tryAMAGICftest_MG('l');
3367 result = my_lstat_flags(0);
3371 if (S_ISLNK(PL_statcache.st_mode))
3384 tryAMAGICftest_MG('t');
3386 if (PL_op->op_flags & OPf_REF)
3389 SV *tmpsv = *PL_stack_sp;
3390 if (!(gv = MAYBE_DEREF_GV_nomg(tmpsv))) {
3391 name = SvPV_nomg(tmpsv, namelen);
3392 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3396 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3397 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3398 else if (name && isDIGIT(*name) && grok_atoUV(name, &uv, NULL) && uv <= PERL_INT_MAX)
3403 SETERRNO(EBADF,RMS_IFI);
3406 if (PerlLIO_isatty(fd))
3412 /* also used for: pp_ftbinary() */
3426 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3428 if (PL_op->op_flags & OPf_REF)
3430 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
3435 gv = MAYBE_DEREF_GV_nomg(sv);
3439 if (gv == PL_defgv) {
3441 io = SvTYPE(PL_statgv) == SVt_PVIO
3445 goto really_filename;
3450 sv_setpvs(PL_statname, "");
3451 io = GvIO(PL_statgv);
3453 PL_laststatval = -1;
3454 PL_laststype = OP_STAT;
3455 if (io && IoIFP(io)) {
3457 if (! PerlIO_has_base(IoIFP(io)))
3458 DIE(aTHX_ "-T and -B not implemented on filehandles");
3459 fd = PerlIO_fileno(IoIFP(io));
3461 SETERRNO(EBADF,RMS_IFI);
3464 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
3465 if (PL_laststatval < 0)
3467 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3468 if (PL_op->op_type == OP_FTTEXT)
3473 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3474 i = PerlIO_getc(IoIFP(io));
3476 (void)PerlIO_ungetc(IoIFP(io),i);
3478 /* null file is anything */
3481 len = PerlIO_get_bufsiz(IoIFP(io));
3482 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3483 /* sfio can have large buffers - limit to 512 */
3488 SETERRNO(EBADF,RMS_IFI);
3490 SETERRNO(EBADF,RMS_IFI);
3499 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3501 file = SvPVX_const(PL_statname);
3503 if (!(fp = PerlIO_open(file, "r"))) {
3505 PL_laststatval = -1;
3506 PL_laststype = OP_STAT;
3508 if (ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
3509 /* PL_warn_nl is constant */
3510 GCC_DIAG_IGNORE(-Wformat-nonliteral);
3511 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3516 PL_laststype = OP_STAT;
3517 fd = PerlIO_fileno(fp);
3519 (void)PerlIO_close(fp);
3520 SETERRNO(EBADF,RMS_IFI);
3523 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
3524 if (PL_laststatval < 0) {
3525 (void)PerlIO_close(fp);
3526 SETERRNO(EBADF,RMS_IFI);
3529 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3530 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3531 (void)PerlIO_close(fp);
3533 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3534 FT_RETURNNO; /* special case NFS directories */
3535 FT_RETURNYES; /* null file is anything */
3540 /* now scan s to look for textiness */
3542 #if defined(DOSISH) || defined(USEMYBINMODE)
3543 /* ignore trailing ^Z on short files */
3544 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3549 if (! is_invariant_string((U8 *) s, len)) {
3552 /* Here contains a variant under UTF-8 . See if the entire string is
3553 * UTF-8. But the buffer may end in a partial character, so consider
3554 * it UTF-8 if the first non-UTF8 char is an ending partial */
3555 if (is_utf8_string_loc((U8 *) s, len, &ep)
3556 || ep + UTF8SKIP(ep) > (U8 *) (s + len))
3558 if (PL_op->op_type == OP_FTTEXT) {
3567 /* Here, is not UTF-8 or is entirely ASCII. Look through the buffer for
3568 * things that wouldn't be in ASCII text or rich ASCII text. Count these
3570 for (i = 0; i < len; i++, s++) {
3571 if (!*s) { /* null never allowed in text */
3575 #ifdef USE_LOCALE_CTYPE
3576 if (IN_LC_RUNTIME(LC_CTYPE)) {
3577 if ( isPRINT_LC(*s) || isSPACE_LC(*s)) {
3584 /* VT occurs so rarely in text, that we consider it odd */
3585 || (isSPACE_A(*s) && *s != VT_NATIVE)
3587 /* But there is a fair amount of backspaces and escapes in
3590 || *s == ESC_NATIVE)
3597 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3608 const char *tmps = NULL;
3612 SV * const sv = POPs;
3613 if (PL_op->op_flags & OPf_SPECIAL) {
3614 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3616 if (ckWARN(WARN_UNOPENED)) {
3617 Perl_warner(aTHX_ packWARN(WARN_UNOPENED),
3618 "chdir() on unopened filehandle %" SVf, sv);
3620 SETERRNO(EBADF,RMS_IFI);
3622 TAINT_PROPER("chdir");
3626 else if (!(gv = MAYBE_DEREF_GV(sv)))
3627 tmps = SvPV_nomg_const_nolen(sv);
3630 HV * const table = GvHVn(PL_envgv);
3633 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3634 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3636 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3640 tmps = SvPV_nolen_const(*svp);
3644 SETERRNO(EINVAL, LIB_INVARG);
3645 TAINT_PROPER("chdir");
3650 TAINT_PROPER("chdir");
3653 IO* const io = GvIO(gv);
3656 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3657 } else if (IoIFP(io)) {
3658 int fd = PerlIO_fileno(IoIFP(io));
3662 PUSHi(fchdir(fd) >= 0);
3672 DIE(aTHX_ PL_no_func, "fchdir");
3676 PUSHi( PerlDir_chdir(tmps) >= 0 );
3678 /* Clear the DEFAULT element of ENV so we'll get the new value
3680 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3687 SETERRNO(EBADF,RMS_IFI);
3694 /* also used for: pp_chmod() pp_kill() pp_unlink() pp_utime() */
3698 dSP; dMARK; dTARGET;
3699 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3710 char * const tmps = POPpx;
3711 TAINT_PROPER("chroot");
3712 PUSHi( chroot(tmps) >= 0 );
3715 DIE(aTHX_ PL_no_func, "chroot");
3723 const char * const tmps2 = POPpconstx;
3724 const char * const tmps = SvPV_nolen_const(TOPs);
3725 TAINT_PROPER("rename");
3727 anum = PerlLIO_rename(tmps, tmps2);
3729 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3730 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3733 if (PerlProc_geteuid() || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3734 (void)UNLINK(tmps2);
3735 if (!(anum = link(tmps, tmps2)))
3736 anum = UNLINK(tmps);
3745 /* also used for: pp_symlink() */
3747 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3751 const int op_type = PL_op->op_type;
3755 if (op_type == OP_LINK)
3756 DIE(aTHX_ PL_no_func, "link");
3758 # ifndef HAS_SYMLINK
3759 if (op_type == OP_SYMLINK)
3760 DIE(aTHX_ PL_no_func, "symlink");
3764 const char * const tmps2 = POPpconstx;
3765 const char * const tmps = SvPV_nolen_const(TOPs);
3766 TAINT_PROPER(PL_op_desc[op_type]);
3768 # if defined(HAS_LINK)
3769 # if defined(HAS_SYMLINK)
3770 /* Both present - need to choose which. */
3771 (op_type == OP_LINK) ?
3772 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3774 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3775 PerlLIO_link(tmps, tmps2);
3778 # if defined(HAS_SYMLINK)
3779 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3780 symlink(tmps, tmps2);
3785 SETi( result >= 0 );
3790 /* also used for: pp_symlink() */
3795 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3805 char buf[MAXPATHLEN];
3810 /* NOTE: if the length returned by readlink() is sizeof(buf) - 1,
3811 * it is impossible to know whether the result was truncated. */
3812 len = readlink(tmps, buf, sizeof(buf) - 1);
3821 RETSETUNDEF; /* just pretend it's a normal file */
3825 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3827 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3829 char * const save_filename = filename;
3834 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3836 PERL_ARGS_ASSERT_DOONELINER;
3838 Newx(cmdline, size, char);
3839 my_strlcpy(cmdline, cmd, size);
3840 my_strlcat(cmdline, " ", size);
3841 for (s = cmdline + strlen(cmdline); *filename; ) {
3845 if (s - cmdline < size)
3846 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3847 myfp = PerlProc_popen(cmdline, "r");
3851 SV * const tmpsv = sv_newmortal();
3852 /* Need to save/restore 'PL_rs' ?? */
3853 s = sv_gets(tmpsv, myfp, 0);
3854 (void)PerlProc_pclose(myfp);
3858 #ifdef HAS_SYS_ERRLIST
3863 /* you don't see this */
3864 const char * const errmsg = Strerror(e) ;
3867 if (instr(s, errmsg)) {
3874 #define EACCES EPERM
3876 if (instr(s, "cannot make"))
3877 SETERRNO(EEXIST,RMS_FEX);
3878 else if (instr(s, "existing file"))
3879 SETERRNO(EEXIST,RMS_FEX);
3880 else if (instr(s, "ile exists"))
3881 SETERRNO(EEXIST,RMS_FEX);
3882 else if (instr(s, "non-exist"))
3883 SETERRNO(ENOENT,RMS_FNF);
3884 else if (instr(s, "does not exist"))
3885 SETERRNO(ENOENT,RMS_FNF);
3886 else if (instr(s, "not empty"))
3887 SETERRNO(EBUSY,SS_DEVOFFLINE);
3888 else if (instr(s, "cannot access"))
3889 SETERRNO(EACCES,RMS_PRV);
3891 SETERRNO(EPERM,RMS_PRV);
3894 else { /* some mkdirs return no failure indication */
3895 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3896 if (PL_op->op_type == OP_RMDIR)
3901 SETERRNO(EACCES,RMS_PRV); /* a guess */
3910 /* This macro removes trailing slashes from a directory name.
3911 * Different operating and file systems take differently to
3912 * trailing slashes. According to POSIX 1003.1 1996 Edition
3913 * any number of trailing slashes should be allowed.
3914 * Thusly we snip them away so that even non-conforming
3915 * systems are happy.
3916 * We should probably do this "filtering" for all
3917 * the functions that expect (potentially) directory names:
3918 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3919 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3921 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3922 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3925 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3926 (tmps) = savepvn((tmps), (len)); \
3936 const unsigned int mode = (MAXARG > 1 && (TOPs||((void)POPs,0))) ? POPu : 0777;
3938 TRIMSLASHES(tmps,len,copy);
3940 TAINT_PROPER("mkdir");
3942 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3946 SETi( dooneliner("mkdir", tmps) );
3947 oldumask = PerlLIO_umask(0);
3948 PerlLIO_umask(oldumask);
3949 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3964 TRIMSLASHES(tmps,len,copy);
3965 TAINT_PROPER("rmdir");
3967 SETi( PerlDir_rmdir(tmps) >= 0 );
3969 SETi( dooneliner("rmdir", tmps) );
3976 /* Directory calls. */
3980 #if defined(Direntry_t) && defined(HAS_READDIR)
3982 const char * const dirname = POPpconstx;
3983 GV * const gv = MUTABLE_GV(POPs);
3984 IO * const io = GvIOn(gv);
3986 if ((IoIFP(io) || IoOFP(io)))
3987 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3988 "Opening filehandle %"HEKf" also as a directory",
3989 HEKfARG(GvENAME_HEK(gv)) );
3991 PerlDir_close(IoDIRP(io));
3992 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3998 SETERRNO(EBADF,RMS_DIR);
4001 DIE(aTHX_ PL_no_dir_func, "opendir");
4007 #if !defined(Direntry_t) || !defined(HAS_READDIR)
4008 DIE(aTHX_ PL_no_dir_func, "readdir");
4010 #if !defined(I_DIRENT) && !defined(VMS)
4011 Direntry_t *readdir (DIR *);
4016 const I32 gimme = GIMME_V;
4017 GV * const gv = MUTABLE_GV(POPs);
4018 const Direntry_t *dp;
4019 IO * const io = GvIOn(gv);
4022 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4023 "readdir() attempted on invalid dirhandle %"HEKf,
4024 HEKfARG(GvENAME_HEK(gv)));
4029 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
4033 sv = newSVpvn(dp->d_name, dp->d_namlen);
4035 sv = newSVpv(dp->d_name, 0);
4037 if (!(IoFLAGS(io) & IOf_UNTAINT))
4040 } while (gimme == G_ARRAY);
4042 if (!dp && gimme != G_ARRAY)
4049 SETERRNO(EBADF,RMS_ISI);
4050 if (gimme == G_ARRAY)
4059 #if defined(HAS_TELLDIR) || defined(telldir)
4061 /* XXX does _anyone_ need this? --AD 2/20/1998 */
4062 /* XXX netbsd still seemed to.
4063 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
4064 --JHI 1999-Feb-02 */
4065 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
4066 long telldir (DIR *);
4068 GV * const gv = MUTABLE_GV(POPs);
4069 IO * const io = GvIOn(gv);
4072 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4073 "telldir() attempted on invalid dirhandle %"HEKf,
4074 HEKfARG(GvENAME_HEK(gv)));
4078 PUSHi( PerlDir_tell(IoDIRP(io)) );
4082 SETERRNO(EBADF,RMS_ISI);
4085 DIE(aTHX_ PL_no_dir_func, "telldir");
4091 #if defined(HAS_SEEKDIR) || defined(seekdir)
4093 const long along = POPl;
4094 GV * const gv = MUTABLE_GV(POPs);
4095 IO * const io = GvIOn(gv);
4098 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4099 "seekdir() attempted on invalid dirhandle %"HEKf,
4100 HEKfARG(GvENAME_HEK(gv)));
4103 (void)PerlDir_seek(IoDIRP(io), along);
4108 SETERRNO(EBADF,RMS_ISI);
4111 DIE(aTHX_ PL_no_dir_func, "seekdir");
4117 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4119 GV * const gv = MUTABLE_GV(POPs);
4120 IO * const io = GvIOn(gv);
4123 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4124 "rewinddir() attempted on invalid dirhandle %"HEKf,
4125 HEKfARG(GvENAME_HEK(gv)));
4128 (void)PerlDir_rewind(IoDIRP(io));
4132 SETERRNO(EBADF,RMS_ISI);
4135 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4141 #if defined(Direntry_t) && defined(HAS_READDIR)
4143 GV * const gv = MUTABLE_GV(POPs);
4144 IO * const io = GvIOn(gv);
4147 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4148 "closedir() attempted on invalid dirhandle %"HEKf,
4149 HEKfARG(GvENAME_HEK(gv)));
4152 #ifdef VOID_CLOSEDIR
4153 PerlDir_close(IoDIRP(io));
4155 if (PerlDir_close(IoDIRP(io)) < 0) {
4156 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4165 SETERRNO(EBADF,RMS_IFI);
4168 DIE(aTHX_ PL_no_dir_func, "closedir");
4172 /* Process control. */
4179 #ifdef HAS_SIGPROCMASK
4180 sigset_t oldmask, newmask;
4184 PERL_FLUSHALL_FOR_CHILD;
4185 #ifdef HAS_SIGPROCMASK
4186 sigfillset(&newmask);
4187 sigprocmask(SIG_SETMASK, &newmask, &oldmask);
4189 childpid = PerlProc_fork();
4190 if (childpid == 0) {
4194 for (sig = 1; sig < SIG_SIZE; sig++)
4195 PL_psig_pend[sig] = 0;
4197 #ifdef HAS_SIGPROCMASK
4200 sigprocmask(SIG_SETMASK, &oldmask, NULL);
4207 #ifdef PERL_USES_PL_PIDSTATUS
4208 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4214 # if (defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)) || defined(__amigaos4__)
4219 PERL_FLUSHALL_FOR_CHILD;
4220 childpid = PerlProc_fork();
4226 DIE(aTHX_ PL_no_func, "fork");
4233 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4238 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4239 childpid = wait4pid(-1, &argflags, 0);
4241 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4246 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4247 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4248 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4250 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4255 DIE(aTHX_ PL_no_func, "wait");
4261 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4263 const int optype = POPi;
4264 const Pid_t pid = TOPi;
4268 result = amigaos_waitpid(aTHX_ optype, pid, &argflags);
4269 STATUS_NATIVE_CHILD_SET((result >= 0) ? argflags : -1);
4270 result = result == 0 ? pid : -1;
4274 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4275 result = wait4pid(pid, &argflags, optype);
4277 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4282 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4283 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4284 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4286 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4288 # endif /* __amigaos4__ */
4292 DIE(aTHX_ PL_no_func, "waitpid");
4298 dSP; dMARK; dORIGMARK; dTARGET;
4299 #if defined(__LIBCATAMOUNT__)
4300 PL_statusvalue = -1;
4305 # ifdef __amigaos4__
4313 while (++MARK <= SP) {
4314 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4319 TAINT_PROPER("system");
4321 PERL_FLUSHALL_FOR_CHILD;
4322 #if (defined(HAS_FORK) || defined(__amigaos4__)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4324 #if defined(__amigaos4__)
4325 struct UserData userdata;
4332 bool child_success = FALSE;
4333 #ifdef HAS_SIGPROCMASK
4334 sigset_t newset, oldset;
4337 if (PerlProc_pipe(pp) >= 0)
4339 #if defined(__amigaos4__)
4340 amigaos_fork_set_userdata(aTHX_
4346 pthread_create(&proc,NULL,amigaos_system_child,(void *)&userdata);
4347 child_success = proc > 0;
4349 #ifdef HAS_SIGPROCMASK
4350 sigemptyset(&newset);
4351 sigaddset(&newset, SIGCHLD);
4352 sigprocmask(SIG_BLOCK, &newset, &oldset);
4354 while ((childpid = PerlProc_fork()) == -1) {
4355 if (errno != EAGAIN) {
4360 PerlLIO_close(pp[0]);
4361 PerlLIO_close(pp[1]);
4363 #ifdef HAS_SIGPROCMASK
4364 sigprocmask(SIG_SETMASK, &oldset, NULL);
4370 child_success = childpid > 0;
4372 if (child_success) {
4373 Sigsave_t ihand,qhand; /* place to save signals during system() */
4376 #ifndef __amigaos4__
4378 PerlLIO_close(pp[1]);
4381 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4382 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4385 result = pthread_join(proc, (void **)&status);
4388 result = wait4pid(childpid, &status, 0);
4389 } while (result == -1 && errno == EINTR);
4392 #ifdef HAS_SIGPROCMASK
4393 sigprocmask(SIG_SETMASK, &oldset, NULL);
4395 (void)rsignal_restore(SIGINT, &ihand);
4396 (void)rsignal_restore(SIGQUIT, &qhand);
4398 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4399 do_execfree(); /* free any memory child malloced on fork */
4406 while (n < sizeof(int)) {
4407 n1 = PerlLIO_read(pp[0],
4408 (void*)(((char*)&errkid)+n),
4414 PerlLIO_close(pp[0]);
4415 if (n) { /* Error */
4416 if (n != sizeof(int))
4417 DIE(aTHX_ "panic: kid popen errno read, n=%u", n);
4418 errno = errkid; /* Propagate errno from kid */
4419 #if defined(__amigaos4__)
4420 /* The pipe always has something in it
4421 * so n alone is not enough. */
4425 STATUS_NATIVE_CHILD_SET(-1);
4429 XPUSHi(STATUS_CURRENT);
4432 #ifndef __amigaos4__
4433 #ifdef HAS_SIGPROCMASK
4434 sigprocmask(SIG_SETMASK, &oldset, NULL);
4437 PerlLIO_close(pp[0]);
4438 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
4439 if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0)
4443 if (PL_op->op_flags & OPf_STACKED) {
4444 SV * const really = *++MARK;
4445 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4447 else if (SP - MARK != 1)
4448 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4450 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4452 #endif /* __amigaos4__ */
4455 #else /* ! FORK or VMS or OS/2 */
4458 if (PL_op->op_flags & OPf_STACKED) {
4459 SV * const really = *++MARK;
4460 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4461 value = (I32)do_aspawn(really, MARK, SP);
4463 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4466 else if (SP - MARK != 1) {
4467 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4468 value = (I32)do_aspawn(NULL, MARK, SP);
4470 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4474 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4476 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4478 STATUS_NATIVE_CHILD_SET(value);
4481 XPUSHi(result ? value : STATUS_CURRENT);
4482 #endif /* !FORK or VMS or OS/2 */
4489 dSP; dMARK; dORIGMARK; dTARGET;
4491 #if defined(__amigaos4__)
4497 while (++MARK <= SP) {
4498 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4503 TAINT_PROPER("exec");
4505 #if defined(__amigaos4__)
4506 /* Make sure redirection behaves after exec. Yes, in AmigaOS the
4507 * original process continues after exec, since processes are more
4509 amigaos_stdio_save(aTHX_ &store);
4511 PERL_FLUSHALL_FOR_CHILD;
4512 if (PL_op->op_flags & OPf_STACKED) {
4513 SV * const really = *++MARK;
4514 value = (I32)do_aexec(really, MARK, SP);
4516 else if (SP - MARK != 1)
4518 value = (I32)vms_do_aexec(NULL, MARK, SP);
4520 value = (I32)do_aexec(NULL, MARK, SP);
4524 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4526 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4530 #if defined(__amigaos4__)
4531 amigaos_stdio_restore(aTHX_ &store);
4532 STATUS_NATIVE_CHILD_SET(value);
4533 PL_exit_flags |= PERL_EXIT_EXPECTED;
4534 if (value != -1) my_exit(value);
4545 XPUSHi( getppid() );
4548 DIE(aTHX_ PL_no_func, "getppid");
4558 (MAXARG < 1) ? 0 : TOPs ? SvIVx(POPs) : ((void)POPs, 0);
4561 pgrp = (I32)BSD_GETPGRP(pid);
4563 if (pid != 0 && pid != PerlProc_getpid())
4564 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4570 DIE(aTHX_ PL_no_func, "getpgrp");
4580 pgrp = MAXARG == 2 && (TOPs||POPs) ? POPi : 0;
4581 if (MAXARG > 0) pid = TOPs ? TOPi : 0;
4588 TAINT_PROPER("setpgrp");
4590 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4592 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4593 || (pid != 0 && pid != PerlProc_getpid()))
4595 DIE(aTHX_ "setpgrp can't take arguments");
4597 SETi( setpgrp() >= 0 );
4598 #endif /* USE_BSDPGRP */
4601 DIE(aTHX_ PL_no_func, "setpgrp");
4605 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4606 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4608 # define PRIORITY_WHICH_T(which) which
4613 #ifdef HAS_GETPRIORITY
4615 const int who = POPi;
4616 const int which = TOPi;
4617 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4620 DIE(aTHX_ PL_no_func, "getpriority");
4626 #ifdef HAS_SETPRIORITY
4628 const int niceval = POPi;
4629 const int who = POPi;
4630 const int which = TOPi;
4631 TAINT_PROPER("setpriority");
4632 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4635 DIE(aTHX_ PL_no_func, "setpriority");
4639 #undef PRIORITY_WHICH_T
4647 XPUSHn( time(NULL) );
4649 XPUSHi( time(NULL) );
4658 struct tms timesbuf;
4661 (void)PerlProc_times(×buf);
4663 mPUSHn(((NV)timesbuf.tms_utime)/(NV)PL_clocktick);
4664 if (GIMME_V == G_ARRAY) {
4665 mPUSHn(((NV)timesbuf.tms_stime)/(NV)PL_clocktick);
4666 mPUSHn(((NV)timesbuf.tms_cutime)/(NV)PL_clocktick);
4667 mPUSHn(((NV)timesbuf.tms_cstime)/(NV)PL_clocktick);
4675 if (GIMME_V == G_ARRAY) {
4682 DIE(aTHX_ "times not implemented");
4684 #endif /* HAS_TIMES */
4687 /* The 32 bit int year limits the times we can represent to these
4688 boundaries with a few days wiggle room to account for time zone
4691 /* Sat Jan 3 00:00:00 -2147481748 */
4692 #define TIME_LOWER_BOUND -67768100567755200.0
4693 /* Sun Dec 29 12:00:00 2147483647 */
4694 #define TIME_UPPER_BOUND 67767976233316800.0
4697 /* also used for: pp_localtime() */
4705 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4706 static const char * const dayname[] =
4707 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4708 static const char * const monname[] =
4709 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4710 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4712 if (MAXARG < 1 || (!TOPs && ((void)POPs, 1))) {
4715 when = (Time64_T)now;
4718 NV input = Perl_floor(POPn);
4719 const bool pl_isnan = Perl_isnan(input);
4720 when = (Time64_T)input;
4721 if (UNLIKELY(pl_isnan || when != input)) {
4722 /* diag_listed_as: gmtime(%f) too large */
4723 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4724 "%s(%.0" NVff ") too large", opname, input);
4732 if ( TIME_LOWER_BOUND > when ) {
4733 /* diag_listed_as: gmtime(%f) too small */
4734 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4735 "%s(%.0" NVff ") too small", opname, when);
4738 else if( when > TIME_UPPER_BOUND ) {
4739 /* diag_listed_as: gmtime(%f) too small */
4740 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4741 "%s(%.0" NVff ") too large", opname, when);
4745 if (PL_op->op_type == OP_LOCALTIME)
4746 err = Perl_localtime64_r(&when, &tmbuf);
4748 err = Perl_gmtime64_r(&when, &tmbuf);
4752 /* diag_listed_as: gmtime(%f) failed */
4753 /* XXX %lld broken for quads */
4755 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4756 "%s(%.0" NVff ") failed", opname, when);
4759 if (GIMME_V != G_ARRAY) { /* scalar context */
4766 Perl_sv_setpvf_mg(aTHX_ TARG, "%s %s %2d %02d:%02d:%02d %"IVdf,
4767 dayname[tmbuf.tm_wday],
4768 monname[tmbuf.tm_mon],
4773 (IV)tmbuf.tm_year + 1900);
4776 else { /* list context */
4782 mPUSHi(tmbuf.tm_sec);
4783 mPUSHi(tmbuf.tm_min);
4784 mPUSHi(tmbuf.tm_hour);
4785 mPUSHi(tmbuf.tm_mday);
4786 mPUSHi(tmbuf.tm_mon);
4787 mPUSHn(tmbuf.tm_year);
4788 mPUSHi(tmbuf.tm_wday);
4789 mPUSHi(tmbuf.tm_yday);
4790 mPUSHi(tmbuf.tm_isdst);
4799 /* alarm() takes an unsigned int number of seconds, and return the
4800 * unsigned int number of seconds remaining in the previous alarm
4801 * (alarms don't stack). Therefore negative return values are not
4805 /* Note that while the C library function alarm() as such has
4806 * no errors defined (or in other words, properly behaving client
4807 * code shouldn't expect any), alarm() being obsoleted by
4808 * setitimer() and often being implemented in terms of
4809 * setitimer(), can fail. */
4810 /* diag_listed_as: %s() with negative argument */
4811 Perl_ck_warner_d(aTHX_ packWARN(WARN_MISC),
4812 "alarm() with negative argument");
4813 SETERRNO(EINVAL, LIB_INVARG);
4817 unsigned int retval = alarm(anum);
4818 if ((int)retval < 0) /* Strictly speaking "cannot happen". */
4824 DIE(aTHX_ PL_no_func, "alarm");
4835 (void)time(&lasttime);
4836 if (MAXARG < 1 || (!TOPs && !POPs))
4841 /* diag_listed_as: %s() with negative argument */
4842 Perl_ck_warner_d(aTHX_ packWARN(WARN_MISC),
4843 "sleep() with negative argument");
4844 SETERRNO(EINVAL, LIB_INVARG);
4848 PerlProc_sleep((unsigned int)duration);
4852 XPUSHi(when - lasttime);
4856 /* Shared memory. */
4857 /* Merged with some message passing. */
4859 /* also used for: pp_msgrcv() pp_msgsnd() pp_semop() pp_shmread() */
4863 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4864 dSP; dMARK; dTARGET;
4865 const int op_type = PL_op->op_type;
4870 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4873 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4876 value = (I32)(do_semop(MARK, SP) >= 0);
4879 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4887 return Perl_pp_semget(aTHX);
4893 /* also used for: pp_msgget() pp_shmget() */
4897 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4898 dSP; dMARK; dTARGET;
4899 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4906 DIE(aTHX_ "System V IPC is not implemented on this machine");
4910 /* also used for: pp_msgctl() pp_shmctl() */
4914 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4915 dSP; dMARK; dTARGET;
4916 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4924 PUSHp(zero_but_true, ZBTLEN);
4928 return Perl_pp_semget(aTHX);
4932 /* I can't const this further without getting warnings about the types of
4933 various arrays passed in from structures. */
4935 S_space_join_names_mortal(pTHX_ char *const *array)
4939 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4942 target = newSVpvs_flags("", SVs_TEMP);
4944 sv_catpv(target, *array);
4947 sv_catpvs(target, " ");
4950 target = sv_mortalcopy(&PL_sv_no);
4955 /* Get system info. */
4957 /* also used for: pp_ghbyaddr() pp_ghbyname() */
4961 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4963 I32 which = PL_op->op_type;
4966 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4967 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4968 struct hostent *gethostbyname(Netdb_name_t);
4969 struct hostent *gethostent(void);
4971 struct hostent *hent = NULL;
4975 if (which == OP_GHBYNAME) {
4976 #ifdef HAS_GETHOSTBYNAME
4977 const char* const name = POPpbytex;
4978 hent = PerlSock_gethostbyname(name);
4980 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4983 else if (which == OP_GHBYADDR) {
4984 #ifdef HAS_GETHOSTBYADDR
4985 const int addrtype = POPi;
4986 SV * const addrsv = POPs;
4988 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4990 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4992 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4996 #ifdef HAS_GETHOSTENT
4997 hent = PerlSock_gethostent();
4999 DIE(aTHX_ PL_no_sock_func, "gethostent");
5002 #ifdef HOST_NOT_FOUND
5004 #ifdef USE_REENTRANT_API
5005 # ifdef USE_GETHOSTENT_ERRNO
5006 h_errno = PL_reentrant_buffer->_gethostent_errno;
5009 STATUS_UNIX_SET(h_errno);
5013 if (GIMME_V != G_ARRAY) {
5014 PUSHs(sv = sv_newmortal());
5016 if (which == OP_GHBYNAME) {
5018 sv_setpvn(sv, hent->h_addr, hent->h_length);
5021 sv_setpv(sv, (char*)hent->h_name);
5027 mPUSHs(newSVpv((char*)hent->h_name, 0));
5028 PUSHs(space_join_names_mortal(hent->h_aliases));
5029 mPUSHi(hent->h_addrtype);
5030 len = hent->h_length;
5033 for (elem = hent->h_addr_list; elem && *elem; elem++) {
5034 mXPUSHp(*elem, len);
5038 mPUSHp(hent->h_addr, len);
5040 PUSHs(sv_mortalcopy(&PL_sv_no));
5045 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5049 /* also used for: pp_gnbyaddr() pp_gnbyname() */
5053 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
5055 I32 which = PL_op->op_type;
5057 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
5058 struct netent *getnetbyaddr(Netdb_net_t, int);
5059 struct netent *getnetbyname(Netdb_name_t);
5060 struct netent *getnetent(void);
5062 struct netent *nent;
5064 if (which == OP_GNBYNAME){
5065 #ifdef HAS_GETNETBYNAME
5066 const char * const name = POPpbytex;
5067 nent = PerlSock_getnetbyname(name);
5069 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
5072 else if (which == OP_GNBYADDR) {
5073 #ifdef HAS_GETNETBYADDR
5074 const int addrtype = POPi;
5075 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
5076 nent = PerlSock_getnetbyaddr(addr, addrtype);
5078 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
5082 #ifdef HAS_GETNETENT
5083 nent = PerlSock_getnetent();
5085 DIE(aTHX_ PL_no_sock_func, "getnetent");
5088 #ifdef HOST_NOT_FOUND
5090 #ifdef USE_REENTRANT_API
5091 # ifdef USE_GETNETENT_ERRNO
5092 h_errno = PL_reentrant_buffer->_getnetent_errno;
5095 STATUS_UNIX_SET(h_errno);
5100 if (GIMME_V != G_ARRAY) {
5101 PUSHs(sv = sv_newmortal());
5103 if (which == OP_GNBYNAME)
5104 sv_setiv(sv, (IV)nent->n_net);
5106 sv_setpv(sv, nent->n_name);
5112 mPUSHs(newSVpv(nent->n_name, 0));
5113 PUSHs(space_join_names_mortal(nent->n_aliases));
5114 mPUSHi(nent->n_addrtype);
5115 mPUSHi(nent->n_net);
5120 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5125 /* also used for: pp_gpbyname() pp_gpbynumber() */
5129 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
5131 I32 which = PL_op->op_type;
5133 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
5134 struct protoent *getprotobyname(Netdb_name_t);
5135 struct protoent *getprotobynumber(int);
5136 struct protoent *getprotoent(void);
5138 struct protoent *pent;
5140 if (which == OP_GPBYNAME) {
5141 #ifdef HAS_GETPROTOBYNAME
5142 const char* const name = POPpbytex;
5143 pent = PerlSock_getprotobyname(name);
5145 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
5148 else if (which == OP_GPBYNUMBER) {
5149 #ifdef HAS_GETPROTOBYNUMBER
5150 const int number = POPi;
5151 pent = PerlSock_getprotobynumber(number);
5153 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5157 #ifdef HAS_GETPROTOENT
5158 pent = PerlSock_getprotoent();
5160 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5164 if (GIMME_V != G_ARRAY) {
5165 PUSHs(sv = sv_newmortal());
5167 if (which == OP_GPBYNAME)
5168 sv_setiv(sv, (IV)pent->p_proto);
5170 sv_setpv(sv, pent->p_name);
5176 mPUSHs(newSVpv(pent->p_name, 0));
5177 PUSHs(space_join_names_mortal(pent->p_aliases));
5178 mPUSHi(pent->p_proto);
5183 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5188 /* also used for: pp_gsbyname() pp_gsbyport() */
5192 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5194 I32 which = PL_op->op_type;
5196 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5197 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5198 struct servent *getservbyport(int, Netdb_name_t);
5199 struct servent *getservent(void);
5201 struct servent *sent;
5203 if (which == OP_GSBYNAME) {
5204 #ifdef HAS_GETSERVBYNAME
5205 const char * const proto = POPpbytex;
5206 const char * const name = POPpbytex;
5207 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5209 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5212 else if (which == OP_GSBYPORT) {
5213 #ifdef HAS_GETSERVBYPORT
5214 const char * const proto = POPpbytex;
5215 unsigned short port = (unsigned short)POPu;
5216 port = PerlSock_htons(port);
5217 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5219 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5223 #ifdef HAS_GETSERVENT
5224 sent = PerlSock_getservent();
5226 DIE(aTHX_ PL_no_sock_func, "getservent");
5230 if (GIMME_V != G_ARRAY) {
5231 PUSHs(sv = sv_newmortal());
5233 if (which == OP_GSBYNAME) {
5234 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5237 sv_setpv(sv, sent->s_name);
5243 mPUSHs(newSVpv(sent->s_name, 0));
5244 PUSHs(space_join_names_mortal(sent->s_aliases));
5245 mPUSHi(PerlSock_ntohs(sent->s_port));
5246 mPUSHs(newSVpv(sent->s_proto, 0));
5251 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5256 /* also used for: pp_snetent() pp_sprotoent() pp_sservent() */
5261 const int stayopen = TOPi;
5262 switch(PL_op->op_type) {
5264 #ifdef HAS_SETHOSTENT
5265 PerlSock_sethostent(stayopen);
5267 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5270 #ifdef HAS_SETNETENT
5272 PerlSock_setnetent(stayopen);
5274 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5278 #ifdef HAS_SETPROTOENT
5279 PerlSock_setprotoent(stayopen);
5281 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5285 #ifdef HAS_SETSERVENT
5286 PerlSock_setservent(stayopen);
5288 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5296 /* also used for: pp_egrent() pp_enetent() pp_eprotoent() pp_epwent()
5297 * pp_eservent() pp_sgrent() pp_spwent() */
5302 switch(PL_op->op_type) {
5304 #ifdef HAS_ENDHOSTENT
5305 PerlSock_endhostent();
5307 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5311 #ifdef HAS_ENDNETENT
5312 PerlSock_endnetent();
5314 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5318 #ifdef HAS_ENDPROTOENT
5319 PerlSock_endprotoent();
5321 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5325 #ifdef HAS_ENDSERVENT
5326 PerlSock_endservent();
5328 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5332 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5335 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5339 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5342 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5346 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5349 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5353 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5356 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5365 /* also used for: pp_gpwnam() pp_gpwuid() */
5371 I32 which = PL_op->op_type;
5373 struct passwd *pwent = NULL;
5375 * We currently support only the SysV getsp* shadow password interface.
5376 * The interface is declared in <shadow.h> and often one needs to link
5377 * with -lsecurity or some such.
5378 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5381 * AIX getpwnam() is clever enough to return the encrypted password
5382 * only if the caller (euid?) is root.
5384 * There are at least three other shadow password APIs. Many platforms
5385 * seem to contain more than one interface for accessing the shadow
5386 * password databases, possibly for compatibility reasons.
5387 * The getsp*() is by far he simplest one, the other two interfaces
5388 * are much more complicated, but also very similar to each other.
5393 * struct pr_passwd *getprpw*();
5394 * The password is in
5395 * char getprpw*(...).ufld.fd_encrypt[]
5396 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5401 * struct es_passwd *getespw*();
5402 * The password is in
5403 * char *(getespw*(...).ufld.fd_encrypt)
5404 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5407 * struct userpw *getuserpw();
5408 * The password is in
5409 * char *(getuserpw(...)).spw_upw_passwd
5410 * (but the de facto standard getpwnam() should work okay)
5412 * Mention I_PROT here so that Configure probes for it.
5414 * In HP-UX for getprpw*() the manual page claims that one should include
5415 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5416 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5417 * and pp_sys.c already includes <shadow.h> if there is such.
5419 * Note that <sys/security.h> is already probed for, but currently
5420 * it is only included in special cases.
5422 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5423 * be preferred interface, even though also the getprpw*() interface
5424 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5425 * One also needs to call set_auth_parameters() in main() before
5426 * doing anything else, whether one is using getespw*() or getprpw*().
5428 * Note that accessing the shadow databases can be magnitudes
5429 * slower than accessing the standard databases.
5434 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5435 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5436 * the pw_comment is left uninitialized. */
5437 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5443 const char* const name = POPpbytex;
5444 pwent = getpwnam(name);
5450 pwent = getpwuid(uid);
5454 # ifdef HAS_GETPWENT
5456 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5457 if (pwent) pwent = getpwnam(pwent->pw_name);
5460 DIE(aTHX_ PL_no_func, "getpwent");
5466 if (GIMME_V != G_ARRAY) {
5467 PUSHs(sv = sv_newmortal());
5469 if (which == OP_GPWNAM)
5470 sv_setuid(sv, pwent->pw_uid);
5472 sv_setpv(sv, pwent->pw_name);
5478 mPUSHs(newSVpv(pwent->pw_name, 0));
5482 /* If we have getspnam(), we try to dig up the shadow
5483 * password. If we are underprivileged, the shadow
5484 * interface will set the errno to EACCES or similar,
5485 * and return a null pointer. If this happens, we will
5486 * use the dummy password (usually "*" or "x") from the
5487 * standard password database.
5489 * In theory we could skip the shadow call completely
5490 * if euid != 0 but in practice we cannot know which
5491 * security measures are guarding the shadow databases
5492 * on a random platform.
5494 * Resist the urge to use additional shadow interfaces.
5495 * Divert the urge to writing an extension instead.
5498 /* Some AIX setups falsely(?) detect some getspnam(), which
5499 * has a different API than the Solaris/IRIX one. */
5500 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5503 const struct spwd * const spwent = getspnam(pwent->pw_name);
5504 /* Save and restore errno so that
5505 * underprivileged attempts seem
5506 * to have never made the unsuccessful
5507 * attempt to retrieve the shadow password. */
5509 if (spwent && spwent->sp_pwdp)
5510 sv_setpv(sv, spwent->sp_pwdp);
5514 if (!SvPOK(sv)) /* Use the standard password, then. */
5515 sv_setpv(sv, pwent->pw_passwd);
5518 /* passwd is tainted because user himself can diddle with it.
5519 * admittedly not much and in a very limited way, but nevertheless. */
5522 sv_setuid(PUSHmortal, pwent->pw_uid);
5523 sv_setgid(PUSHmortal, pwent->pw_gid);
5525 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5526 * because of the poor interface of the Perl getpw*(),
5527 * not because there's some standard/convention saying so.
5528 * A better interface would have been to return a hash,
5529 * but we are accursed by our history, alas. --jhi. */
5531 mPUSHi(pwent->pw_change);
5534 mPUSHi(pwent->pw_quota);
5537 mPUSHs(newSVpv(pwent->pw_age, 0));
5539 /* I think that you can never get this compiled, but just in case. */
5540 PUSHs(sv_mortalcopy(&PL_sv_no));
5545 /* pw_class and pw_comment are mutually exclusive--.
5546 * see the above note for pw_change, pw_quota, and pw_age. */
5548 mPUSHs(newSVpv(pwent->pw_class, 0));
5551 mPUSHs(newSVpv(pwent->pw_comment, 0));
5553 /* I think that you can never get this compiled, but just in case. */
5554 PUSHs(sv_mortalcopy(&PL_sv_no));
5559 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5561 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5563 /* pw_gecos is tainted because user himself can diddle with it. */
5566 mPUSHs(newSVpv(pwent->pw_dir, 0));
5568 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5569 /* pw_shell is tainted because user himself can diddle with it. */
5573 mPUSHi(pwent->pw_expire);
5578 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5583 /* also used for: pp_ggrgid() pp_ggrnam() */
5589 const I32 which = PL_op->op_type;
5590 const struct group *grent;
5592 if (which == OP_GGRNAM) {
5593 const char* const name = POPpbytex;
5594 grent = (const struct group *)getgrnam(name);
5596 else if (which == OP_GGRGID) {
5598 const Gid_t gid = POPu;
5599 #elif Gid_t_sign == -1
5600 const Gid_t gid = POPi;
5602 # error "Unexpected Gid_t_sign"
5604 grent = (const struct group *)getgrgid(gid);
5608 grent = (struct group *)getgrent();
5610 DIE(aTHX_ PL_no_func, "getgrent");
5614 if (GIMME_V != G_ARRAY) {
5615 SV * const sv = sv_newmortal();
5619 if (which == OP_GGRNAM)
5620 sv_setgid(sv, grent->gr_gid);
5622 sv_setpv(sv, grent->gr_name);
5628 mPUSHs(newSVpv(grent->gr_name, 0));
5631 mPUSHs(newSVpv(grent->gr_passwd, 0));
5633 PUSHs(sv_mortalcopy(&PL_sv_no));
5636 sv_setgid(PUSHmortal, grent->gr_gid);
5638 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5639 /* In UNICOS/mk (_CRAYMPP) the multithreading
5640 * versions (getgrnam_r, getgrgid_r)
5641 * seem to return an illegal pointer
5642 * as the group members list, gr_mem.
5643 * getgrent() doesn't even have a _r version
5644 * but the gr_mem is poisonous anyway.
5645 * So yes, you cannot get the list of group
5646 * members if building multithreaded in UNICOS/mk. */
5647 PUSHs(space_join_names_mortal(grent->gr_mem));
5653 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5663 if (!(tmps = PerlProc_getlogin()))
5665 sv_setpv_mg(TARG, tmps);
5669 DIE(aTHX_ PL_no_func, "getlogin");
5673 /* Miscellaneous. */
5678 dSP; dMARK; dORIGMARK; dTARGET;
5679 I32 items = SP - MARK;
5680 unsigned long a[20];
5685 while (++MARK <= SP) {
5686 if (SvTAINTED(*MARK)) {
5692 TAINT_PROPER("syscall");
5695 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5696 * or where sizeof(long) != sizeof(char*). But such machines will
5697 * not likely have syscall implemented either, so who cares?
5699 while (++MARK <= SP) {
5700 if (SvNIOK(*MARK) || !i)
5701 a[i++] = SvIV(*MARK);
5702 else if (*MARK == &PL_sv_undef)
5705 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5711 DIE(aTHX_ "Too many args to syscall");
5713 DIE(aTHX_ "Too few args to syscall");
5715 retval = syscall(a[0]);
5718 retval = syscall(a[0],a[1]);
5721 retval = syscall(a[0],a[1],a[2]);
5724 retval = syscall(a[0],a[1],a[2],a[3]);
5727 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5730 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5733 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5736 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5743 DIE(aTHX_ PL_no_func, "syscall");
5747 #ifdef FCNTL_EMULATE_FLOCK
5749 /* XXX Emulate flock() with fcntl().
5750 What's really needed is a good file locking module.
5754 fcntl_emulate_flock(int fd, int operation)
5759 switch (operation & ~LOCK_NB) {
5761 flock.l_type = F_RDLCK;
5764 flock.l_type = F_WRLCK;
5767 flock.l_type = F_UNLCK;
5773 flock.l_whence = SEEK_SET;
5774 flock.l_start = flock.l_len = (Off_t)0;
5776 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5777 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5778 errno = EWOULDBLOCK;
5782 #endif /* FCNTL_EMULATE_FLOCK */
5784 #ifdef LOCKF_EMULATE_FLOCK
5786 /* XXX Emulate flock() with lockf(). This is just to increase
5787 portability of scripts. The calls are not completely
5788 interchangeable. What's really needed is a good file
5792 /* The lockf() constants might have been defined in <unistd.h>.
5793 Unfortunately, <unistd.h> causes troubles on some mixed
5794 (BSD/POSIX) systems, such as SunOS 4.1.3.
5796 Further, the lockf() constants aren't POSIX, so they might not be
5797 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5798 just stick in the SVID values and be done with it. Sigh.
5802 # define F_ULOCK 0 /* Unlock a previously locked region */
5805 # define F_LOCK 1 /* Lock a region for exclusive use */
5808 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5811 # define F_TEST 3 /* Test a region for other processes locks */
5815 lockf_emulate_flock(int fd, int operation)
5821 /* flock locks entire file so for lockf we need to do the same */
5822 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5823 if (pos > 0) /* is seekable and needs to be repositioned */
5824 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5825 pos = -1; /* seek failed, so don't seek back afterwards */
5828 switch (operation) {
5830 /* LOCK_SH - get a shared lock */
5832 /* LOCK_EX - get an exclusive lock */
5834 i = lockf (fd, F_LOCK, 0);
5837 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5838 case LOCK_SH|LOCK_NB:
5839 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5840 case LOCK_EX|LOCK_NB:
5841 i = lockf (fd, F_TLOCK, 0);
5843 if ((errno == EAGAIN) || (errno == EACCES))
5844 errno = EWOULDBLOCK;
5847 /* LOCK_UN - unlock (non-blocking is a no-op) */
5849 case LOCK_UN|LOCK_NB:
5850 i = lockf (fd, F_ULOCK, 0);
5853 /* Default - can't decipher operation */
5860 if (pos > 0) /* need to restore position of the handle */
5861 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5866 #endif /* LOCKF_EMULATE_FLOCK */
5869 * ex: set ts=8 sts=4 sw=4 et: