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 U8 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 SvPVCLEAR(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, ...)
538 PERL_ARGS_ASSERT_TIED_METHOD;
540 /* Ensure that our flag bits do not overlap. */
541 STATIC_ASSERT_STMT((TIED_METHOD_MORTALIZE_NOT_NEEDED & G_WANT) == 0);
542 STATIC_ASSERT_STMT((TIED_METHOD_ARGUMENTS_ON_STACK & G_WANT) == 0);
543 STATIC_ASSERT_STMT((TIED_METHOD_SAY & G_WANT) == 0);
545 PUTBACK; /* sp is at *foot* of args, so this pops args from old stack */
546 PUSHSTACKi(PERLSI_MAGIC);
547 /* extend for object + args. If argc might wrap/truncate when cast
548 * to SSize_t and incremented, set to -1, which will trigger a panic in
550 * The weird way this is written is because g++ is dumb enough to
551 * warn "comparison is always false" on something like:
553 * sizeof(a) >= sizeof(b) && a >= B_t_MAX -1
555 * (where the LH condition is false)
558 (argc > (sizeof(argc) >= sizeof(SSize_t) ? SSize_t_MAX - 1 : argc))
559 ? -1 : (SSize_t)argc + 1;
560 EXTEND(SP, extend_size);
562 PUSHs(SvTIED_obj(sv, mg));
563 if (flags & TIED_METHOD_ARGUMENTS_ON_STACK) {
564 Copy(orig_sp + 2, sp + 1, argc, SV*); /* copy args to new stack */
568 const U32 mortalize_not_needed
569 = flags & TIED_METHOD_MORTALIZE_NOT_NEEDED;
571 va_start(args, argc);
573 SV *const arg = va_arg(args, SV *);
574 if(mortalize_not_needed)
583 ENTER_with_name("call_tied_method");
584 if (flags & TIED_METHOD_SAY) {
585 /* local $\ = "\n" */
586 SAVEGENERICSV(PL_ors_sv);
587 PL_ors_sv = newSVpvs("\n");
589 ret_args = call_sv(methname, (flags & G_WANT)|G_METHOD_NAMED);
594 if (ret_args) { /* copy results back to original stack */
595 EXTEND(sp, ret_args);
596 Copy(orig_sp - ret_args + 1, sp + 1, ret_args, SV*);
600 LEAVE_with_name("call_tied_method");
604 #define tied_method0(a,b,c,d) \
605 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,0)
606 #define tied_method1(a,b,c,d,e) \
607 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,1,e)
608 #define tied_method2(a,b,c,d,e,f) \
609 Perl_tied_method(aTHX_ a,b,c,d,G_SCALAR,2,e,f)
622 GV * const gv = MUTABLE_GV(*++MARK);
624 if (!isGV(gv) && !(SvTYPE(gv) == SVt_PVLV && isGV_with_GP(gv)))
625 DIE(aTHX_ PL_no_usym, "filehandle");
627 if ((io = GvIOp(gv))) {
629 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
632 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
633 "Opening dirhandle %" HEKf " also as a file. This will be a fatal error in Perl 5.28",
634 HEKfARG(GvENAME_HEK(gv)));
636 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
638 /* Method's args are same as ours ... */
639 /* ... except handle is replaced by the object */
640 return Perl_tied_method(aTHX_ SV_CONST(OPEN), mark - 1, MUTABLE_SV(io), mg,
641 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
653 tmps = SvPV_const(sv, len);
654 ok = do_open6(gv, tmps, len, NULL, MARK+1, (SP-MARK));
657 PUSHi( (I32)PL_forkprocess );
658 else if (PL_forkprocess == 0) /* we are a new child */
669 MAXARG == 0 || (!TOPs && !POPs) ? PL_defoutgv : MUTABLE_GV(POPs);
675 IO * const io = GvIO(gv);
677 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
679 return tied_method0(SV_CONST(CLOSE), SP, MUTABLE_SV(io), mg);
683 PUSHs(boolSV(do_close(gv, TRUE)));
695 GV * const wgv = MUTABLE_GV(POPs);
696 GV * const rgv = MUTABLE_GV(POPs);
700 do_close(rgv, FALSE);
704 do_close(wgv, FALSE);
706 if (PerlProc_pipe(fd) < 0)
709 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r" PIPE_OPEN_MODE);
710 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w" PIPE_OPEN_MODE);
711 IoOFP(rstio) = IoIFP(rstio);
712 IoIFP(wstio) = IoOFP(wstio);
713 IoTYPE(rstio) = IoTYPE_RDONLY;
714 IoTYPE(wstio) = IoTYPE_WRONLY;
716 if (!IoIFP(rstio) || !IoOFP(wstio)) {
718 PerlIO_close(IoIFP(rstio));
720 PerlLIO_close(fd[0]);
722 PerlIO_close(IoOFP(wstio));
724 PerlLIO_close(fd[1]);
727 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
728 /* ensure close-on-exec */
729 if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
730 (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
738 DIE(aTHX_ PL_no_func, "pipe");
752 gv = MUTABLE_GV(POPs);
756 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
758 return tied_method0(SV_CONST(FILENO), SP, MUTABLE_SV(io), mg);
761 if (io && IoDIRP(io)) {
762 #if defined(HAS_DIRFD) || defined(HAS_DIR_DD_FD)
763 PUSHi(my_dirfd(IoDIRP(io)));
765 #elif defined(ENOTSUP)
766 errno = ENOTSUP; /* Operation not supported */
768 #elif defined(EOPNOTSUPP)
769 errno = EOPNOTSUPP; /* Operation not supported on socket */
772 errno = EINVAL; /* Invalid argument */
777 if (!io || !(fp = IoIFP(io))) {
778 /* Can't do this because people seem to do things like
779 defined(fileno($foo)) to check whether $foo is a valid fh.
786 PUSHi(PerlIO_fileno(fp));
797 if (MAXARG < 1 || (!TOPs && !POPs)) {
798 anum = PerlLIO_umask(022);
799 /* setting it to 022 between the two calls to umask avoids
800 * to have a window where the umask is set to 0 -- meaning
801 * that another thread could create world-writeable files. */
803 (void)PerlLIO_umask(anum);
806 anum = PerlLIO_umask(POPi);
807 TAINT_PROPER("umask");
810 /* Only DIE if trying to restrict permissions on "user" (self).
811 * Otherwise it's harmless and more useful to just return undef
812 * since 'group' and 'other' concepts probably don't exist here. */
813 if (MAXARG >= 1 && (TOPs||POPs) && (POPi & 0700))
814 DIE(aTHX_ "umask not implemented");
815 XPUSHs(&PL_sv_undef);
834 gv = MUTABLE_GV(POPs);
838 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
840 /* This takes advantage of the implementation of the varargs
841 function, which I don't think that the optimiser will be able to
842 figure out. Although, as it's a static function, in theory it
844 return Perl_tied_method(aTHX_ SV_CONST(BINMODE), SP, MUTABLE_SV(io), mg,
845 G_SCALAR|TIED_METHOD_MORTALIZE_NOT_NEEDED,
846 discp ? 1 : 0, discp);
850 if (!io || !(fp = IoIFP(io))) {
852 SETERRNO(EBADF,RMS_IFI);
859 const char *d = NULL;
862 d = SvPV_const(discp, len);
863 mode = mode_from_discipline(d, len);
864 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
865 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
866 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
887 const I32 markoff = MARK - PL_stack_base;
888 const char *methname;
889 int how = PERL_MAGIC_tied;
893 switch(SvTYPE(varsv)) {
897 methname = "TIEHASH";
898 if (HvLAZYDEL(varsv) && (entry = HvEITER((HV *)varsv))) {
899 HvLAZYDEL_off(varsv);
900 hv_free_ent((HV *)varsv, entry);
902 HvEITER_set(MUTABLE_HV(varsv), 0);
906 methname = "TIEARRAY";
907 if (!AvREAL(varsv)) {
909 Perl_croak(aTHX_ "Cannot tie unreifiable array");
910 av_clear((AV *)varsv);
917 if (isGV_with_GP(varsv) && !SvFAKE(varsv)) {
918 methname = "TIEHANDLE";
919 how = PERL_MAGIC_tiedscalar;
920 /* For tied filehandles, we apply tiedscalar magic to the IO
921 slot of the GP rather than the GV itself. AMS 20010812 */
923 GvIOp(varsv) = newIO();
924 varsv = MUTABLE_SV(GvIOp(varsv));
927 if (SvTYPE(varsv) == SVt_PVLV && LvTYPE(varsv) == 'y') {
928 vivify_defelem(varsv);
929 varsv = LvTARG(varsv);
933 methname = "TIESCALAR";
934 how = PERL_MAGIC_tiedscalar;
938 if (sv_isobject(*MARK)) { /* Calls GET magic. */
939 ENTER_with_name("call_TIE");
940 PUSHSTACKi(PERLSI_MAGIC);
942 EXTEND(SP,(I32)items);
946 call_method(methname, G_SCALAR);
949 /* Can't use call_method here, else this: fileno FOO; tie @a, "FOO"
950 * will attempt to invoke IO::File::TIEARRAY, with (best case) the
951 * wrong error message, and worse case, supreme action at a distance.
952 * (Sorry obfuscation writers. You're not going to be given this one.)
954 stash = gv_stashsv(*MARK, 0);
957 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%" SVf "\"",
958 methname, SVfARG(*MARK));
959 else if (isGV(*MARK)) {
960 /* If the glob doesn't name an existing package, using
961 * SVfARG(*MARK) would yield "*Foo::Bar" or *main::Foo. So
962 * generate the name for the error message explicitly. */
963 SV *stashname = sv_2mortal(newSV(0));
964 gv_fullname4(stashname, (GV *) *MARK, NULL, FALSE);
965 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%" SVf "\"",
966 methname, SVfARG(stashname));
969 SV *stashname = !SvPOK(*MARK) ? &PL_sv_no
970 : SvCUR(*MARK) ? *MARK
971 : sv_2mortal(newSVpvs("main"));
972 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%" SVf "\""
973 " (perhaps you forgot to load \"%" SVf "\"?)",
974 methname, SVfARG(stashname), SVfARG(stashname));
977 else if (!(gv = gv_fetchmethod(stash, methname))) {
978 /* The effective name can only be NULL for stashes that have
979 * been deleted from the symbol table, which this one can't
980 * be, since we just looked it up by name.
982 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%" HEKf "\"",
983 methname, HvENAME_HEK_NN(stash));
985 ENTER_with_name("call_TIE");
986 PUSHSTACKi(PERLSI_MAGIC);
988 EXTEND(SP,(I32)items);
992 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
998 if (sv_isobject(sv)) {
999 sv_unmagic(varsv, how);
1000 /* Croak if a self-tie on an aggregate is attempted. */
1001 if (varsv == SvRV(sv) &&
1002 (SvTYPE(varsv) == SVt_PVAV ||
1003 SvTYPE(varsv) == SVt_PVHV))
1005 "Self-ties of arrays and hashes are not supported");
1006 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
1008 LEAVE_with_name("call_TIE");
1009 SP = PL_stack_base + markoff;
1015 /* also used for: pp_dbmclose() */
1022 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
1023 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
1025 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
1028 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y' &&
1029 !(sv = defelem_target(sv, NULL))) RETPUSHUNDEF;
1031 if ((mg = SvTIED_mg(sv, how))) {
1032 SV * const obj = SvRV(SvTIED_obj(sv, mg));
1034 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
1036 if (gv && isGV(gv) && (cv = GvCV(gv))) {
1038 PUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
1039 mXPUSHi(SvREFCNT(obj) - 1);
1041 ENTER_with_name("call_UNTIE");
1042 call_sv(MUTABLE_SV(cv), G_VOID);
1043 LEAVE_with_name("call_UNTIE");
1046 else if (mg && SvREFCNT(obj) > 1) {
1047 Perl_ck_warner(aTHX_ packWARN(WARN_UNTIE),
1048 "untie attempted while %" UVuf " inner references still exist",
1049 (UV)SvREFCNT(obj) - 1 ) ;
1053 sv_unmagic(sv, how) ;
1062 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
1063 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
1065 if (isGV_with_GP(sv) && !SvFAKE(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
1068 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y' &&
1069 !(sv = defelem_target(sv, NULL))) goto ret_undef;
1071 if ((mg = SvTIED_mg(sv, how))) {
1072 SETs(SvTIED_obj(sv, mg));
1073 return NORMAL; /* PUTBACK not needed, pp_tied never moves SP */
1087 HV * const hv = MUTABLE_HV(POPs);
1088 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
1089 stash = gv_stashsv(sv, 0);
1090 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
1092 require_pv("AnyDBM_File.pm");
1094 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
1095 DIE(aTHX_ "No dbm on this machine");
1105 mPUSHu(O_RDWR|O_CREAT);
1109 if (!SvOK(right)) right = &PL_sv_no;
1113 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1116 if (!sv_isobject(TOPs)) {
1124 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
1126 if (sv_isobject(TOPs))
1131 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
1132 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1149 struct timeval timebuf;
1150 struct timeval *tbuf = &timebuf;
1153 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1158 # if BYTEORDER & 0xf0000
1159 # define ORDERBYTE (0x88888888 - BYTEORDER)
1161 # define ORDERBYTE (0x4444 - BYTEORDER)
1167 for (i = 1; i <= 3; i++) {
1168 SV * const sv = SP[i];
1172 if (SvREADONLY(sv)) {
1173 if (!(SvPOK(sv) && SvCUR(sv) == 0))
1174 Perl_croak_no_modify();
1176 else if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
1179 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
1180 "Non-string passed as bitmask");
1181 SvPV_force_nomg_nolen(sv); /* force string conversion */
1188 /* little endians can use vecs directly */
1189 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1196 masksize = NFDBITS / NBBY;
1198 masksize = sizeof(long); /* documented int, everyone seems to use long */
1200 Zero(&fd_sets[0], 4, char*);
1203 # if SELECT_MIN_BITS == 1
1204 growsize = sizeof(fd_set);
1206 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1207 # undef SELECT_MIN_BITS
1208 # define SELECT_MIN_BITS __FD_SETSIZE
1210 /* If SELECT_MIN_BITS is greater than one we most probably will want
1211 * to align the sizes with SELECT_MIN_BITS/8 because for example
1212 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1213 * UNIX, Solaris, Darwin) the smallest quantum select() operates
1214 * on (sets/tests/clears bits) is 32 bits. */
1215 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1221 value = SvNV_nomg(sv);
1224 timebuf.tv_sec = (long)value;
1225 value -= (NV)timebuf.tv_sec;
1226 timebuf.tv_usec = (long)(value * 1000000.0);
1231 for (i = 1; i <= 3; i++) {
1233 if (!SvOK(sv) || SvCUR(sv) == 0) {
1240 Sv_Grow(sv, growsize);
1244 while (++j <= growsize) {
1248 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1250 Newx(fd_sets[i], growsize, char);
1251 for (offset = 0; offset < growsize; offset += masksize) {
1252 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1253 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1256 fd_sets[i] = SvPVX(sv);
1260 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1261 /* Can't make just the (void*) conditional because that would be
1262 * cpp #if within cpp macro, and not all compilers like that. */
1263 nfound = PerlSock_select(
1265 (Select_fd_set_t) fd_sets[1],
1266 (Select_fd_set_t) fd_sets[2],
1267 (Select_fd_set_t) fd_sets[3],
1268 (void*) tbuf); /* Workaround for compiler bug. */
1270 nfound = PerlSock_select(
1272 (Select_fd_set_t) fd_sets[1],
1273 (Select_fd_set_t) fd_sets[2],
1274 (Select_fd_set_t) fd_sets[3],
1277 for (i = 1; i <= 3; i++) {
1280 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1282 for (offset = 0; offset < growsize; offset += masksize) {
1283 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1284 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1286 Safefree(fd_sets[i]);
1293 if (GIMME_V == G_ARRAY && tbuf) {
1294 value = (NV)(timebuf.tv_sec) +
1295 (NV)(timebuf.tv_usec) / 1000000.0;
1300 DIE(aTHX_ "select not implemented");
1308 =for apidoc setdefout
1310 Sets C<PL_defoutgv>, the default file handle for output, to the passed in
1311 typeglob. As C<PL_defoutgv> "owns" a reference on its typeglob, the reference
1312 count of the passed in typeglob is increased by one, and the reference count
1313 of the typeglob that C<PL_defoutgv> points to is decreased by one.
1319 Perl_setdefout(pTHX_ GV *gv)
1321 GV *oldgv = PL_defoutgv;
1323 PERL_ARGS_ASSERT_SETDEFOUT;
1325 SvREFCNT_inc_simple_void_NN(gv);
1327 SvREFCNT_dec(oldgv);
1334 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1335 GV * egv = GvEGVx(PL_defoutgv);
1340 hv = isGV_with_GP(egv) ? GvSTASH(egv) : NULL;
1341 gvp = hv && HvENAME(hv)
1342 ? (GV**)hv_fetch(hv, GvNAME(egv), HEK_UTF8(GvNAME_HEK(egv)) ? -GvNAMELEN(egv) : GvNAMELEN(egv), FALSE)
1344 if (gvp && *gvp == egv) {
1345 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1349 mXPUSHs(newRV(MUTABLE_SV(egv)));
1353 if (!GvIO(newdefout))
1354 gv_IOadd(newdefout);
1355 setdefout(newdefout);
1365 MAXARG==0 || (!TOPs && !POPs) ? PL_stdingv : MUTABLE_GV(POPs);
1366 IO *const io = GvIO(gv);
1372 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1374 const U8 gimme = GIMME_V;
1375 Perl_tied_method(aTHX_ SV_CONST(GETC), SP, MUTABLE_SV(io), mg, gimme, 0);
1376 if (gimme == G_SCALAR) {
1378 SvSetMagicSV_nosteal(TARG, TOPs);
1383 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1384 if (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1386 SETERRNO(EBADF,RMS_IFI);
1390 sv_setpvs(TARG, " ");
1391 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1392 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1393 /* Find out how many bytes the char needs */
1394 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1397 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1398 SvCUR_set(TARG,1+len);
1402 else SvUTF8_off(TARG);
1408 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1411 const U8 gimme = GIMME_V;
1413 PERL_ARGS_ASSERT_DOFORM;
1416 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1418 cx = cx_pushblock(CXt_FORMAT, gimme, PL_stack_sp, PL_savestack_ix);
1419 cx_pushformat(cx, cv, retop, gv);
1420 if (CvDEPTH(cv) >= 2)
1421 pad_push(CvPADLIST(cv), CvDEPTH(cv));
1422 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv));
1424 setdefout(gv); /* locally select filehandle so $% et al work */
1441 gv = MUTABLE_GV(POPs);
1458 SV * const tmpsv = sv_newmortal();
1459 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1460 DIE(aTHX_ "Undefined format \"%" SVf "\" called", SVfARG(tmpsv));
1462 IoFLAGS(io) &= ~IOf_DIDTOP;
1463 RETURNOP(doform(cv,gv,PL_op->op_next));
1469 GV * const gv = CX_CUR()->blk_format.gv;
1470 IO * const io = GvIOp(gv);
1475 bool is_return = cBOOL(PL_op->op_type == OP_RETURN);
1477 if (is_return || !io || !(ofp = IoOFP(io)))
1480 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1481 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1483 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1484 PL_formtarget != PL_toptarget)
1488 if (!IoTOP_GV(io)) {
1491 if (!IoTOP_NAME(io)) {
1493 if (!IoFMT_NAME(io))
1494 IoFMT_NAME(io) = savepv(GvNAME(gv));
1495 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%" HEKf "_TOP",
1496 HEKfARG(GvNAME_HEK(gv))));
1497 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1498 if ((topgv && GvFORM(topgv)) ||
1499 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1500 IoTOP_NAME(io) = savesvpv(topname);
1502 IoTOP_NAME(io) = savepvs("top");
1504 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1505 if (!topgv || !GvFORM(topgv)) {
1506 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1509 IoTOP_GV(io) = topgv;
1511 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1512 I32 lines = IoLINES_LEFT(io);
1513 const char *s = SvPVX_const(PL_formtarget);
1514 if (lines <= 0) /* Yow, header didn't even fit!!! */
1516 while (lines-- > 0) {
1517 s = strchr(s, '\n');
1523 const STRLEN save = SvCUR(PL_formtarget);
1524 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1525 do_print(PL_formtarget, ofp);
1526 SvCUR_set(PL_formtarget, save);
1527 sv_chop(PL_formtarget, s);
1528 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1531 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1532 do_print(GvSV(gv_fetchpvs("\f", GV_ADD, SVt_PV)), ofp);
1533 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1535 PL_formtarget = PL_toptarget;
1536 IoFLAGS(io) |= IOf_DIDTOP;
1538 assert(fgv); /* IoTOP_GV(io) should have been set above */
1541 SV * const sv = sv_newmortal();
1542 gv_efullname4(sv, fgv, NULL, FALSE);
1543 DIE(aTHX_ "Undefined top format \"%" SVf "\" called", SVfARG(sv));
1545 return doform(cv, gv, PL_op);
1550 assert(CxTYPE(cx) == CXt_FORMAT);
1551 SP = PL_stack_base + cx->blk_oldsp; /* ignore retval of formline */
1555 retop = cx->blk_sub.retop;
1559 /* XXX the semantics of doing 'return' in a format aren't documented.
1560 * Currently we ignore any args to 'return' and just return
1561 * a single undef in both scalar and list contexts
1563 PUSHs(&PL_sv_undef);
1564 else if (!io || !(fp = IoOFP(io))) {
1565 if (io && IoIFP(io))
1566 report_wrongway_fh(gv, '<');
1572 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1573 Perl_ck_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1575 if (!do_print(PL_formtarget, fp))
1578 FmLINES(PL_formtarget) = 0;
1579 SvCUR_set(PL_formtarget, 0);
1580 *SvEND(PL_formtarget) = '\0';
1581 if (IoFLAGS(io) & IOf_FLUSH)
1582 (void)PerlIO_flush(fp);
1586 PL_formtarget = PL_bodytarget;
1592 dSP; dMARK; dORIGMARK;
1596 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1597 IO *const io = GvIO(gv);
1599 /* Treat empty list as "" */
1600 if (MARK == SP) XPUSHs(&PL_sv_no);
1603 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1605 if (MARK == ORIGMARK) {
1608 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1611 return Perl_tied_method(aTHX_ SV_CONST(PRINTF), mark - 1, MUTABLE_SV(io),
1613 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1620 SETERRNO(EBADF,RMS_IFI);
1623 else if (!(fp = IoOFP(io))) {
1625 report_wrongway_fh(gv, '<');
1626 else if (ckWARN(WARN_CLOSED))
1628 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1632 SV *sv = sv_newmortal();
1633 do_sprintf(sv, SP - MARK, MARK + 1);
1634 if (!do_print(sv, fp))
1637 if (IoFLAGS(io) & IOf_FLUSH)
1638 if (PerlIO_flush(fp) == EOF)
1647 PUSHs(&PL_sv_undef);
1654 const int perm = (MAXARG > 3 && (TOPs || POPs)) ? POPi : 0666;
1655 const int mode = POPi;
1656 SV * const sv = POPs;
1657 GV * const gv = MUTABLE_GV(POPs);
1660 /* Need TIEHANDLE method ? */
1661 const char * const tmps = SvPV_const(sv, len);
1662 if (do_open_raw(gv, tmps, len, mode, perm)) {
1663 IoLINES(GvIOp(gv)) = 0;
1667 PUSHs(&PL_sv_undef);
1673 /* also used for: pp_read() and pp_recv() (where supported) */
1677 dSP; dMARK; dORIGMARK; dTARGET;
1691 bool charstart = FALSE;
1692 STRLEN charskip = 0;
1694 GV * const gv = MUTABLE_GV(*++MARK);
1697 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1698 && gv && (io = GvIO(gv)) )
1700 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1702 return Perl_tied_method(aTHX_ SV_CONST(READ), mark - 1, MUTABLE_SV(io), mg,
1703 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1713 length = SvIVx(*++MARK);
1715 DIE(aTHX_ "Negative length");
1718 offset = SvIVx(*++MARK);
1722 if (!io || !IoIFP(io)) {
1724 SETERRNO(EBADF,RMS_IFI);
1728 /* Note that fd can here validly be -1, don't check it yet. */
1729 fd = PerlIO_fileno(IoIFP(io));
1731 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1732 if (PL_op->op_type == OP_SYSREAD || PL_op->op_type == OP_RECV) {
1733 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
1734 "%s() is deprecated on :utf8 handles. "
1735 "This will be a fatal error in Perl 5.30",
1738 buffer = SvPVutf8_force(bufsv, blen);
1739 /* UTF-8 may not have been set if they are all low bytes */
1744 buffer = SvPV_force(bufsv, blen);
1745 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1747 if (DO_UTF8(bufsv)) {
1748 blen = sv_len_utf8_nomg(bufsv);
1757 if (PL_op->op_type == OP_RECV) {
1758 Sock_size_t bufsize;
1759 char namebuf[MAXPATHLEN];
1761 SETERRNO(EBADF,SS_IVCHAN);
1764 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(__QNXNTO__)
1765 bufsize = sizeof (struct sockaddr_in);
1767 bufsize = sizeof namebuf;
1769 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1773 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1774 /* 'offset' means 'flags' here */
1775 count = PerlSock_recvfrom(fd, buffer, length, offset,
1776 (struct sockaddr *)namebuf, &bufsize);
1779 /* MSG_TRUNC can give oversized count; quietly lose it */
1782 SvCUR_set(bufsv, count);
1783 *SvEND(bufsv) = '\0';
1784 (void)SvPOK_only(bufsv);
1788 /* This should not be marked tainted if the fp is marked clean */
1789 if (!(IoFLAGS(io) & IOf_UNTAINT))
1790 SvTAINTED_on(bufsv);
1792 #if defined(__CYGWIN__)
1793 /* recvfrom() on cygwin doesn't set bufsize at all for
1794 connected sockets, leaving us with trash in the returned
1795 name, so use the same test as the Win32 code to check if it
1796 wasn't set, and set it [perl #118843] */
1797 if (bufsize == sizeof namebuf)
1800 sv_setpvn(TARG, namebuf, bufsize);
1806 if (-offset > (SSize_t)blen)
1807 DIE(aTHX_ "Offset outside string");
1810 if (DO_UTF8(bufsv)) {
1811 /* convert offset-as-chars to offset-as-bytes */
1812 if (offset >= (SSize_t)blen)
1813 offset += SvCUR(bufsv) - blen;
1815 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1819 /* Reestablish the fd in case it shifted from underneath us. */
1820 fd = PerlIO_fileno(IoIFP(io));
1822 orig_size = SvCUR(bufsv);
1823 /* Allocating length + offset + 1 isn't perfect in the case of reading
1824 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1826 (should be 2 * length + offset + 1, or possibly something longer if
1827 IN_ENCODING Is true) */
1828 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1829 if (offset > 0 && offset > (SSize_t)orig_size) { /* Zero any newly allocated space */
1830 Zero(buffer+orig_size, offset-orig_size, char);
1832 buffer = buffer + offset;
1834 read_target = bufsv;
1836 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1837 concatenate it to the current buffer. */
1839 /* Truncate the existing buffer to the start of where we will be
1841 SvCUR_set(bufsv, offset);
1843 read_target = sv_newmortal();
1844 SvUPGRADE(read_target, SVt_PV);
1845 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1848 if (PL_op->op_type == OP_SYSREAD) {
1849 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1850 if (IoTYPE(io) == IoTYPE_SOCKET) {
1852 SETERRNO(EBADF,SS_IVCHAN);
1856 count = PerlSock_recv(fd, buffer, length, 0);
1862 SETERRNO(EBADF,RMS_IFI);
1866 count = PerlLIO_read(fd, buffer, length);
1871 count = PerlIO_read(IoIFP(io), buffer, length);
1872 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1873 if (count == 0 && PerlIO_error(IoIFP(io)))
1877 if (IoTYPE(io) == IoTYPE_WRONLY)
1878 report_wrongway_fh(gv, '>');
1881 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1882 *SvEND(read_target) = '\0';
1883 (void)SvPOK_only(read_target);
1884 if (fp_utf8 && !IN_BYTES) {
1885 /* Look at utf8 we got back and count the characters */
1886 const char *bend = buffer + count;
1887 while (buffer < bend) {
1889 skip = UTF8SKIP(buffer);
1892 if (buffer - charskip + skip > bend) {
1893 /* partial character - try for rest of it */
1894 length = skip - (bend-buffer);
1895 offset = bend - SvPVX_const(bufsv);
1907 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1908 provided amount read (count) was what was requested (length)
1910 if (got < wanted && count == length) {
1911 length = wanted - got;
1912 offset = bend - SvPVX_const(bufsv);
1915 /* return value is character count */
1919 else if (buffer_utf8) {
1920 /* Let svcatsv upgrade the bytes we read in to utf8.
1921 The buffer is a mortal so will be freed soon. */
1922 sv_catsv_nomg(bufsv, read_target);
1925 /* This should not be marked tainted if the fp is marked clean */
1926 if (!(IoFLAGS(io) & IOf_UNTAINT))
1927 SvTAINTED_on(bufsv);
1938 /* also used for: pp_send() where defined */
1942 dSP; dMARK; dORIGMARK; dTARGET;
1947 STRLEN orig_blen_bytes;
1948 const int op_type = PL_op->op_type;
1951 GV *const gv = MUTABLE_GV(*++MARK);
1952 IO *const io = GvIO(gv);
1955 if (op_type == OP_SYSWRITE && io) {
1956 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1958 if (MARK == SP - 1) {
1960 mXPUSHi(sv_len(sv));
1964 return Perl_tied_method(aTHX_ SV_CONST(WRITE), mark - 1, MUTABLE_SV(io), mg,
1965 G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK,
1975 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1977 if (io && IoIFP(io))
1978 report_wrongway_fh(gv, '<');
1981 SETERRNO(EBADF,RMS_IFI);
1984 fd = PerlIO_fileno(IoIFP(io));
1986 SETERRNO(EBADF,SS_IVCHAN);
1991 /* Do this first to trigger any overloading. */
1992 buffer = SvPV_const(bufsv, blen);
1993 orig_blen_bytes = blen;
1994 doing_utf8 = DO_UTF8(bufsv);
1996 if (PerlIO_isutf8(IoIFP(io))) {
1997 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
1998 "%s() is deprecated on :utf8 handles. "
1999 "This will be a fatal error in Perl 5.30",
2001 if (!SvUTF8(bufsv)) {
2002 /* We don't modify the original scalar. */
2003 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
2004 buffer = (char *) tmpbuf;
2008 else if (doing_utf8) {
2009 STRLEN tmplen = blen;
2010 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
2013 buffer = (char *) tmpbuf;
2017 assert((char *)result == buffer);
2018 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
2023 if (op_type == OP_SEND) {
2024 const int flags = SvIVx(*++MARK);
2027 char * const sockbuf = SvPVx(*++MARK, mlen);
2028 retval = PerlSock_sendto(fd, buffer, blen,
2029 flags, (struct sockaddr *)sockbuf, mlen);
2032 retval = PerlSock_send(fd, buffer, blen, flags);
2038 Size_t length = 0; /* This length is in characters. */
2044 /* The SV is bytes, and we've had to upgrade it. */
2045 blen_chars = orig_blen_bytes;
2047 /* The SV really is UTF-8. */
2048 /* Don't call sv_len_utf8 on a magical or overloaded
2049 scalar, as we might get back a different result. */
2050 blen_chars = sv_or_pv_len_utf8(bufsv, buffer, blen);
2057 length = blen_chars;
2059 #if Size_t_size > IVSIZE
2060 length = (Size_t)SvNVx(*++MARK);
2062 length = (Size_t)SvIVx(*++MARK);
2064 if ((SSize_t)length < 0) {
2066 DIE(aTHX_ "Negative length");
2071 offset = SvIVx(*++MARK);
2073 if (-offset > (IV)blen_chars) {
2075 DIE(aTHX_ "Offset outside string");
2077 offset += blen_chars;
2078 } else if (offset > (IV)blen_chars) {
2080 DIE(aTHX_ "Offset outside string");
2084 if (length > blen_chars - offset)
2085 length = blen_chars - offset;
2087 /* Here we convert length from characters to bytes. */
2088 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
2089 /* Either we had to convert the SV, or the SV is magical, or
2090 the SV has overloading, in which case we can't or mustn't
2091 or mustn't call it again. */
2093 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
2094 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
2096 /* It's a real UTF-8 SV, and it's not going to change under
2097 us. Take advantage of any cache. */
2099 I32 len_I32 = length;
2101 /* Convert the start and end character positions to bytes.
2102 Remember that the second argument to sv_pos_u2b is relative
2104 sv_pos_u2b(bufsv, &start, &len_I32);
2111 buffer = buffer+offset;
2113 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
2114 if (IoTYPE(io) == IoTYPE_SOCKET) {
2115 retval = PerlSock_send(fd, buffer, length, 0);
2120 /* See the note at doio.c:do_print about filesize limits. --jhi */
2121 retval = PerlLIO_write(fd, buffer, length);
2129 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2132 #if Size_t_size > IVSIZE
2152 * in Perl 5.12 and later, the additional parameter is a bitmask:
2155 * 2 = eof() <- ARGV magic
2157 * I'll rely on the compiler's trace flow analysis to decide whether to
2158 * actually assign this out here, or punt it into the only block where it is
2159 * used. Doing it out here is DRY on the condition logic.
2164 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2170 if (PL_op->op_flags & OPf_SPECIAL) {
2171 gv = PL_last_in_gv = GvEGVx(PL_argvgv); /* eof() - ARGV magic */
2175 gv = PL_last_in_gv; /* eof */
2183 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2184 return tied_method1(SV_CONST(EOF), SP, MUTABLE_SV(io), mg, newSVuv(which));
2187 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2188 if (io && !IoIFP(io)) {
2189 if ((IoFLAGS(io) & IOf_START) && av_tindex(GvAVn(gv)) < 0) {
2192 IoFLAGS(io) &= ~IOf_START;
2193 do_open6(gv, "-", 1, NULL, NULL, 0);
2201 *svp = newSVpvs("-");
2203 else if (!nextargv(gv, FALSE))
2208 PUSHs(boolSV(do_eof(gv)));
2218 if (MAXARG != 0 && (TOPs || POPs))
2219 PL_last_in_gv = MUTABLE_GV(POPs);
2226 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2228 return tied_method0(SV_CONST(TELL), SP, MUTABLE_SV(io), mg);
2233 SETERRNO(EBADF,RMS_IFI);
2238 #if LSEEKSIZE > IVSIZE
2239 PUSHn( do_tell(gv) );
2241 PUSHi( do_tell(gv) );
2247 /* also used for: pp_seek() */
2252 const int whence = POPi;
2253 #if LSEEKSIZE > IVSIZE
2254 const Off_t offset = (Off_t)SvNVx(POPs);
2256 const Off_t offset = (Off_t)SvIVx(POPs);
2259 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2260 IO *const io = GvIO(gv);
2263 const MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2265 #if LSEEKSIZE > IVSIZE
2266 SV *const offset_sv = newSVnv((NV) offset);
2268 SV *const offset_sv = newSViv(offset);
2271 return tied_method2(SV_CONST(SEEK), SP, MUTABLE_SV(io), mg, offset_sv,
2276 if (PL_op->op_type == OP_SEEK)
2277 PUSHs(boolSV(do_seek(gv, offset, whence)));
2279 const Off_t sought = do_sysseek(gv, offset, whence);
2281 PUSHs(&PL_sv_undef);
2283 SV* const sv = sought ?
2284 #if LSEEKSIZE > IVSIZE
2289 : newSVpvn(zero_but_true, ZBTLEN);
2299 /* There seems to be no consensus on the length type of truncate()
2300 * and ftruncate(), both off_t and size_t have supporters. In
2301 * general one would think that when using large files, off_t is
2302 * at least as wide as size_t, so using an off_t should be okay. */
2303 /* XXX Configure probe for the length type of *truncate() needed XXX */
2306 #if Off_t_size > IVSIZE
2311 /* Checking for length < 0 is problematic as the type might or
2312 * might not be signed: if it is not, clever compilers will moan. */
2313 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2316 SV * const sv = POPs;
2321 if (PL_op->op_flags & OPf_SPECIAL
2322 ? (tmpgv = gv_fetchsv(sv, 0, SVt_PVIO), 1)
2323 : !!(tmpgv = MAYBE_DEREF_GV(sv)) ) {
2330 TAINT_PROPER("truncate");
2331 if (!(fp = IoIFP(io))) {
2335 int fd = PerlIO_fileno(fp);
2337 SETERRNO(EBADF,RMS_IFI);
2341 SETERRNO(EINVAL, LIB_INVARG);
2346 if (ftruncate(fd, len) < 0)
2348 if (my_chsize(fd, len) < 0)
2356 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2357 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2358 goto do_ftruncate_io;
2361 const char * const name = SvPV_nomg_const_nolen(sv);
2362 TAINT_PROPER("truncate");
2364 if (truncate(name, len) < 0)
2371 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
2372 mode |= O_LARGEFILE; /* Transparently largefiley. */
2375 /* On open(), the Win32 CRT tries to seek around text
2376 * files using 32-bit offsets, which causes the open()
2377 * to fail on large files, so open in binary mode.
2381 tmpfd = PerlLIO_open(name, mode);
2386 if (my_chsize(tmpfd, len) < 0)
2388 PerlLIO_close(tmpfd);
2397 SETERRNO(EBADF,RMS_IFI);
2403 /* also used for: pp_fcntl() */
2408 SV * const argsv = POPs;
2409 const unsigned int func = POPu;
2411 GV * const gv = MUTABLE_GV(POPs);
2412 IO * const io = GvIOn(gv);
2418 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2422 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2425 s = SvPV_force(argsv, len);
2426 need = IOCPARM_LEN(func);
2428 s = Sv_Grow(argsv, need + 1);
2429 SvCUR_set(argsv, need);
2432 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2435 retval = SvIV(argsv);
2436 s = INT2PTR(char*,retval); /* ouch */
2439 optype = PL_op->op_type;
2440 TAINT_PROPER(PL_op_desc[optype]);
2442 if (optype == OP_IOCTL)
2444 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2446 DIE(aTHX_ "ioctl is not implemented");
2450 DIE(aTHX_ "fcntl is not implemented");
2452 #if defined(OS2) && defined(__EMX__)
2453 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2455 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2459 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2461 if (s[SvCUR(argsv)] != 17)
2462 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2464 s[SvCUR(argsv)] = 0; /* put our null back */
2465 SvSETMAGIC(argsv); /* Assume it has changed */
2474 PUSHp(zero_but_true, ZBTLEN);
2485 const int argtype = POPi;
2486 GV * const gv = MUTABLE_GV(POPs);
2487 IO *const io = GvIO(gv);
2488 PerlIO *const fp = io ? IoIFP(io) : NULL;
2490 /* XXX Looks to me like io is always NULL at this point */
2492 (void)PerlIO_flush(fp);
2493 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2498 SETERRNO(EBADF,RMS_IFI);
2503 DIE(aTHX_ PL_no_func, "flock");
2514 const int protocol = POPi;
2515 const int type = POPi;
2516 const int domain = POPi;
2517 GV * const gv = MUTABLE_GV(POPs);
2518 IO * const io = GvIOn(gv);
2522 do_close(gv, FALSE);
2524 TAINT_PROPER("socket");
2525 fd = PerlSock_socket(domain, type, protocol);
2529 IoIFP(io) = PerlIO_fdopen(fd, "r" SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2530 IoOFP(io) = PerlIO_fdopen(fd, "w" SOCKET_OPEN_MODE);
2531 IoTYPE(io) = IoTYPE_SOCKET;
2532 if (!IoIFP(io) || !IoOFP(io)) {
2533 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2534 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2535 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2538 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2539 /* ensure close-on-exec */
2540 if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
2550 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2553 const int protocol = POPi;
2554 const int type = POPi;
2555 const int domain = POPi;
2557 GV * const gv2 = MUTABLE_GV(POPs);
2558 IO * const io2 = GvIOn(gv2);
2559 GV * const gv1 = MUTABLE_GV(POPs);
2560 IO * const io1 = GvIOn(gv1);
2563 do_close(gv1, FALSE);
2565 do_close(gv2, FALSE);
2567 TAINT_PROPER("socketpair");
2568 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2570 IoIFP(io1) = PerlIO_fdopen(fd[0], "r" SOCKET_OPEN_MODE);
2571 IoOFP(io1) = PerlIO_fdopen(fd[0], "w" SOCKET_OPEN_MODE);
2572 IoTYPE(io1) = IoTYPE_SOCKET;
2573 IoIFP(io2) = PerlIO_fdopen(fd[1], "r" SOCKET_OPEN_MODE);
2574 IoOFP(io2) = PerlIO_fdopen(fd[1], "w" SOCKET_OPEN_MODE);
2575 IoTYPE(io2) = IoTYPE_SOCKET;
2576 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2577 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2578 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2579 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2580 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2581 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2582 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2585 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2586 /* ensure close-on-exec */
2587 if ((fd[0] > PL_maxsysfd && fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) ||
2588 (fd[1] > PL_maxsysfd && fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0))
2594 DIE(aTHX_ PL_no_sock_func, "socketpair");
2600 /* also used for: pp_connect() */
2605 SV * const addrsv = POPs;
2606 /* OK, so on what platform does bind modify addr? */
2608 GV * const gv = MUTABLE_GV(POPs);
2609 IO * const io = GvIOn(gv);
2616 fd = PerlIO_fileno(IoIFP(io));
2620 addr = SvPV_const(addrsv, len);
2621 op_type = PL_op->op_type;
2622 TAINT_PROPER(PL_op_desc[op_type]);
2623 if ((op_type == OP_BIND
2624 ? PerlSock_bind(fd, (struct sockaddr *)addr, len)
2625 : PerlSock_connect(fd, (struct sockaddr *)addr, len))
2633 SETERRNO(EBADF,SS_IVCHAN);
2640 const int backlog = POPi;
2641 GV * const gv = MUTABLE_GV(POPs);
2642 IO * const io = GvIOn(gv);
2647 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2654 SETERRNO(EBADF,SS_IVCHAN);
2662 char namebuf[MAXPATHLEN];
2663 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(__QNXNTO__)
2664 Sock_size_t len = sizeof (struct sockaddr_in);
2666 Sock_size_t len = sizeof namebuf;
2668 GV * const ggv = MUTABLE_GV(POPs);
2669 GV * const ngv = MUTABLE_GV(POPs);
2672 IO * const gstio = GvIO(ggv);
2673 if (!gstio || !IoIFP(gstio))
2677 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2680 /* Some platforms indicate zero length when an AF_UNIX client is
2681 * not bound. Simulate a non-zero-length sockaddr structure in
2683 namebuf[0] = 0; /* sun_len */
2684 namebuf[1] = AF_UNIX; /* sun_family */
2692 do_close(ngv, FALSE);
2693 IoIFP(nstio) = PerlIO_fdopen(fd, "r" SOCKET_OPEN_MODE);
2694 IoOFP(nstio) = PerlIO_fdopen(fd, "w" SOCKET_OPEN_MODE);
2695 IoTYPE(nstio) = IoTYPE_SOCKET;
2696 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2697 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2698 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2699 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2702 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
2703 /* ensure close-on-exec */
2704 if (fd > PL_maxsysfd && fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
2708 #ifdef __SCO_VERSION__
2709 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2712 PUSHp(namebuf, len);
2716 report_evil_fh(ggv);
2717 SETERRNO(EBADF,SS_IVCHAN);
2727 const int how = POPi;
2728 GV * const gv = MUTABLE_GV(POPs);
2729 IO * const io = GvIOn(gv);
2734 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2739 SETERRNO(EBADF,SS_IVCHAN);
2744 /* also used for: pp_gsockopt() */
2749 const int optype = PL_op->op_type;
2750 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2751 const unsigned int optname = (unsigned int) POPi;
2752 const unsigned int lvl = (unsigned int) POPi;
2753 GV * const gv = MUTABLE_GV(POPs);
2754 IO * const io = GvIOn(gv);
2761 fd = PerlIO_fileno(IoIFP(io));
2767 (void)SvPOK_only(sv);
2771 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2774 /* XXX Configure test: does getsockopt set the length properly? */
2783 #if defined(__SYMBIAN32__)
2784 # define SETSOCKOPT_OPTION_VALUE_T void *
2786 # define SETSOCKOPT_OPTION_VALUE_T const char *
2788 /* XXX TODO: We need to have a proper type (a Configure probe,
2789 * etc.) for what the C headers think of the third argument of
2790 * setsockopt(), the option_value read-only buffer: is it
2791 * a "char *", or a "void *", const or not. Some compilers
2792 * don't take kindly to e.g. assuming that "char *" implicitly
2793 * promotes to a "void *", or to explicitly promoting/demoting
2794 * consts to non/vice versa. The "const void *" is the SUS
2795 * definition, but that does not fly everywhere for the above
2797 SETSOCKOPT_OPTION_VALUE_T buf;
2801 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2805 aint = (int)SvIV(sv);
2806 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2809 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2819 SETERRNO(EBADF,SS_IVCHAN);
2826 /* also used for: pp_getsockname() */
2831 const int optype = PL_op->op_type;
2832 GV * const gv = MUTABLE_GV(POPs);
2833 IO * const io = GvIOn(gv);
2841 sv = sv_2mortal(newSV(257));
2842 (void)SvPOK_only(sv);
2846 fd = PerlIO_fileno(IoIFP(io));
2850 case OP_GETSOCKNAME:
2851 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2854 case OP_GETPEERNAME:
2855 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2857 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2859 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";
2860 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2861 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2862 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2863 sizeof(u_short) + sizeof(struct in_addr))) {
2870 #ifdef BOGUS_GETNAME_RETURN
2871 /* Interactive Unix, getpeername() and getsockname()
2872 does not return valid namelen */
2873 if (len == BOGUS_GETNAME_RETURN)
2874 len = sizeof(struct sockaddr);
2883 SETERRNO(EBADF,SS_IVCHAN);
2892 /* also used for: pp_lstat() */
2903 if (PL_op->op_flags & OPf_REF ? (gv = cGVOP_gv, 1)
2904 : !!(sv=POPs, gv = MAYBE_DEREF_GV(sv))) {
2905 if (PL_op->op_type == OP_LSTAT) {
2906 if (gv != PL_defgv) {
2907 do_fstat_warning_check:
2908 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
2909 "lstat() on filehandle%s%" SVf,
2912 ? sv_2mortal(newSVhek(GvENAME_HEK(gv)))
2914 } else if (PL_laststype != OP_LSTAT)
2915 /* diag_listed_as: The stat preceding %s wasn't an lstat */
2916 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2919 if (gv != PL_defgv) {
2923 PL_laststype = OP_STAT;
2924 PL_statgv = gv ? gv : (GV *)io;
2925 SvPVCLEAR(PL_statname);
2931 int fd = PerlIO_fileno(IoIFP(io));
2933 PL_laststatval = -1;
2934 SETERRNO(EBADF,RMS_IFI);
2936 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
2939 } else if (IoDIRP(io)) {
2941 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2944 PL_laststatval = -1;
2947 else PL_laststatval = -1;
2948 if (PL_laststatval < 0 && !havefp) report_evil_fh(gv);
2951 if (PL_laststatval < 0) {
2957 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2958 io = MUTABLE_IO(SvRV(sv));
2959 if (PL_op->op_type == OP_LSTAT)
2960 goto do_fstat_warning_check;
2961 goto do_fstat_have_io;
2964 SvTAINTED_off(PL_statname); /* previous tainting irrelevant */
2965 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
2967 PL_laststype = PL_op->op_type;
2968 file = SvPV_nolen_const(PL_statname);
2969 if (PL_op->op_type == OP_LSTAT)
2970 PL_laststatval = PerlLIO_lstat(file, &PL_statcache);
2972 PL_laststatval = PerlLIO_stat(file, &PL_statcache);
2973 if (PL_laststatval < 0) {
2974 if (ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
2975 /* PL_warn_nl is constant */
2976 GCC_DIAG_IGNORE(-Wformat-nonliteral);
2977 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2985 if (gimme != G_ARRAY) {
2986 if (gimme != G_VOID)
2987 XPUSHs(boolSV(max));
2993 mPUSHi(PL_statcache.st_dev);
2994 #if ST_INO_SIZE > IVSIZE
2995 mPUSHn(PL_statcache.st_ino);
2997 # if ST_INO_SIGN <= 0
2998 mPUSHi(PL_statcache.st_ino);
3000 mPUSHu(PL_statcache.st_ino);
3003 mPUSHu(PL_statcache.st_mode);
3004 mPUSHu(PL_statcache.st_nlink);
3006 sv_setuid(PUSHmortal, PL_statcache.st_uid);
3007 sv_setgid(PUSHmortal, PL_statcache.st_gid);
3009 #ifdef USE_STAT_RDEV
3010 mPUSHi(PL_statcache.st_rdev);
3012 PUSHs(newSVpvs_flags("", SVs_TEMP));
3014 #if Off_t_size > IVSIZE
3015 mPUSHn(PL_statcache.st_size);
3017 mPUSHi(PL_statcache.st_size);
3020 mPUSHn(PL_statcache.st_atime);
3021 mPUSHn(PL_statcache.st_mtime);
3022 mPUSHn(PL_statcache.st_ctime);
3024 mPUSHi(PL_statcache.st_atime);
3025 mPUSHi(PL_statcache.st_mtime);
3026 mPUSHi(PL_statcache.st_ctime);
3028 #ifdef USE_STAT_BLOCKS
3029 mPUSHu(PL_statcache.st_blksize);
3030 mPUSHu(PL_statcache.st_blocks);
3032 PUSHs(newSVpvs_flags("", SVs_TEMP));
3033 PUSHs(newSVpvs_flags("", SVs_TEMP));
3039 /* All filetest ops avoid manipulating the perl stack pointer in their main
3040 bodies (since commit d2c4d2d1e22d3125), and return using either
3041 S_ft_return_false() or S_ft_return_true(). These two helper functions are
3042 the only two which manipulate the perl stack. To ensure that no stack
3043 manipulation macros are used, the filetest ops avoid defining a local copy
3044 of the stack pointer with dSP. */
3046 /* If the next filetest is stacked up with this one
3047 (PL_op->op_private & OPpFT_STACKING), we leave
3048 the original argument on the stack for success,
3049 and skip the stacked operators on failure.
3050 The next few macros/functions take care of this.
3054 S_ft_return_false(pTHX_ SV *ret) {
3058 if (PL_op->op_flags & OPf_REF) XPUSHs(ret);
3062 if (PL_op->op_private & OPpFT_STACKING) {
3063 while (OP_IS_FILETEST(next->op_type)
3064 && next->op_private & OPpFT_STACKED)
3065 next = next->op_next;
3070 PERL_STATIC_INLINE OP *
3071 S_ft_return_true(pTHX_ SV *ret) {
3073 if (PL_op->op_flags & OPf_REF)
3074 XPUSHs(PL_op->op_private & OPpFT_STACKING ? (SV *)cGVOP_gv : (ret));
3075 else if (!(PL_op->op_private & OPpFT_STACKING))
3081 #define FT_RETURNNO return S_ft_return_false(aTHX_ &PL_sv_no)
3082 #define FT_RETURNUNDEF return S_ft_return_false(aTHX_ &PL_sv_undef)
3083 #define FT_RETURNYES return S_ft_return_true(aTHX_ &PL_sv_yes)
3085 #define tryAMAGICftest_MG(chr) STMT_START { \
3086 if ( (SvFLAGS(*PL_stack_sp) & (SVf_ROK|SVs_GMG)) \
3087 && PL_op->op_flags & OPf_KIDS) { \
3088 OP *next = S_try_amagic_ftest(aTHX_ chr); \
3089 if (next) return next; \
3094 S_try_amagic_ftest(pTHX_ char chr) {
3095 SV *const arg = *PL_stack_sp;
3098 if (!(PL_op->op_private & OPpFT_STACKING)) SvGETMAGIC(arg);
3102 const char tmpchr = chr;
3103 SV * const tmpsv = amagic_call(arg,
3104 newSVpvn_flags(&tmpchr, 1, SVs_TEMP),
3105 ftest_amg, AMGf_unary);
3110 return SvTRUE(tmpsv)
3111 ? S_ft_return_true(aTHX_ tmpsv) : S_ft_return_false(aTHX_ tmpsv);
3117 /* also used for: pp_fteexec() pp_fteread() pp_ftewrite() pp_ftrexec()
3123 /* Not const, because things tweak this below. Not bool, because there's
3124 no guarantee that OPpFT_ACCESS is <= CHAR_MAX */
3125 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3126 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
3127 /* Giving some sort of initial value silences compilers. */
3129 int access_mode = R_OK;
3131 int access_mode = 0;
3134 /* access_mode is never used, but leaving use_access in makes the
3135 conditional compiling below much clearer. */
3138 Mode_t stat_mode = S_IRUSR;
3140 bool effective = FALSE;
3143 switch (PL_op->op_type) {
3144 case OP_FTRREAD: opchar = 'R'; break;
3145 case OP_FTRWRITE: opchar = 'W'; break;
3146 case OP_FTREXEC: opchar = 'X'; break;
3147 case OP_FTEREAD: opchar = 'r'; break;
3148 case OP_FTEWRITE: opchar = 'w'; break;
3149 case OP_FTEEXEC: opchar = 'x'; break;
3151 tryAMAGICftest_MG(opchar);
3153 switch (PL_op->op_type) {
3155 #if !(defined(HAS_ACCESS) && defined(R_OK))
3161 #if defined(HAS_ACCESS) && defined(W_OK)
3166 stat_mode = S_IWUSR;
3170 #if defined(HAS_ACCESS) && defined(X_OK)
3175 stat_mode = S_IXUSR;
3179 #ifdef PERL_EFF_ACCESS
3182 stat_mode = S_IWUSR;
3186 #ifndef PERL_EFF_ACCESS
3193 #ifdef PERL_EFF_ACCESS
3198 stat_mode = S_IXUSR;
3204 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3205 const char *name = SvPV_nolen(*PL_stack_sp);
3207 # ifdef PERL_EFF_ACCESS
3208 result = PERL_EFF_ACCESS(name, access_mode);
3210 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3216 result = access(name, access_mode);
3218 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3229 result = my_stat_flags(0);
3232 if (cando(stat_mode, effective, &PL_statcache))
3238 /* also used for: pp_ftatime() pp_ftctime() pp_ftmtime() pp_ftsize() */
3243 const int op_type = PL_op->op_type;
3247 case OP_FTIS: opchar = 'e'; break;
3248 case OP_FTSIZE: opchar = 's'; break;
3249 case OP_FTMTIME: opchar = 'M'; break;
3250 case OP_FTCTIME: opchar = 'C'; break;
3251 case OP_FTATIME: opchar = 'A'; break;
3253 tryAMAGICftest_MG(opchar);
3255 result = my_stat_flags(0);
3258 if (op_type == OP_FTIS)
3261 /* You can't dTARGET inside OP_FTIS, because you'll get
3262 "panic: pad_sv po" - the op is not flagged to have a target. */
3266 #if Off_t_size > IVSIZE
3267 sv_setnv(TARG, (NV)PL_statcache.st_size);
3269 sv_setiv(TARG, (IV)PL_statcache.st_size);
3274 ((NV)PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3278 ((NV)PL_basetime - PL_statcache.st_atime) / 86400.0 );
3282 ((NV)PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3286 return SvTRUE_nomg(TARG)
3287 ? S_ft_return_true(aTHX_ TARG) : S_ft_return_false(aTHX_ TARG);
3292 /* also used for: pp_ftblk() pp_ftchr() pp_ftdir() pp_fteowned()
3293 * pp_ftfile() pp_ftpipe() pp_ftsgid() pp_ftsock()
3294 * pp_ftsuid() pp_ftsvtx() pp_ftzero() */
3301 switch (PL_op->op_type) {
3302 case OP_FTROWNED: opchar = 'O'; break;
3303 case OP_FTEOWNED: opchar = 'o'; break;
3304 case OP_FTZERO: opchar = 'z'; break;
3305 case OP_FTSOCK: opchar = 'S'; break;
3306 case OP_FTCHR: opchar = 'c'; break;
3307 case OP_FTBLK: opchar = 'b'; break;
3308 case OP_FTFILE: opchar = 'f'; break;
3309 case OP_FTDIR: opchar = 'd'; break;
3310 case OP_FTPIPE: opchar = 'p'; break;
3311 case OP_FTSUID: opchar = 'u'; break;
3312 case OP_FTSGID: opchar = 'g'; break;
3313 case OP_FTSVTX: opchar = 'k'; break;
3315 tryAMAGICftest_MG(opchar);
3317 /* I believe that all these three are likely to be defined on most every
3318 system these days. */
3320 if(PL_op->op_type == OP_FTSUID) {
3325 if(PL_op->op_type == OP_FTSGID) {
3330 if(PL_op->op_type == OP_FTSVTX) {
3335 result = my_stat_flags(0);
3338 switch (PL_op->op_type) {
3340 if (PL_statcache.st_uid == PerlProc_getuid())
3344 if (PL_statcache.st_uid == PerlProc_geteuid())
3348 if (PL_statcache.st_size == 0)
3352 if (S_ISSOCK(PL_statcache.st_mode))
3356 if (S_ISCHR(PL_statcache.st_mode))
3360 if (S_ISBLK(PL_statcache.st_mode))
3364 if (S_ISREG(PL_statcache.st_mode))
3368 if (S_ISDIR(PL_statcache.st_mode))
3372 if (S_ISFIFO(PL_statcache.st_mode))
3377 if (PL_statcache.st_mode & S_ISUID)
3383 if (PL_statcache.st_mode & S_ISGID)
3389 if (PL_statcache.st_mode & S_ISVTX)
3401 tryAMAGICftest_MG('l');
3402 result = my_lstat_flags(0);
3406 if (S_ISLNK(PL_statcache.st_mode))
3419 tryAMAGICftest_MG('t');
3421 if (PL_op->op_flags & OPf_REF)
3424 SV *tmpsv = *PL_stack_sp;
3425 if (!(gv = MAYBE_DEREF_GV_nomg(tmpsv))) {
3426 name = SvPV_nomg(tmpsv, namelen);
3427 gv = gv_fetchpvn_flags(name, namelen, SvUTF8(tmpsv), SVt_PVIO);
3431 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3432 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3433 else if (name && isDIGIT(*name) && grok_atoUV(name, &uv, NULL) && uv <= PERL_INT_MAX)
3438 SETERRNO(EBADF,RMS_IFI);
3441 if (PerlLIO_isatty(fd))
3447 /* also used for: pp_ftbinary() */
3461 tryAMAGICftest_MG(PL_op->op_type == OP_FTTEXT ? 'T' : 'B');
3463 if (PL_op->op_flags & OPf_REF)
3465 else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
3470 gv = MAYBE_DEREF_GV_nomg(sv);
3474 if (gv == PL_defgv) {
3476 io = SvTYPE(PL_statgv) == SVt_PVIO
3480 goto really_filename;
3485 SvPVCLEAR(PL_statname);
3486 io = GvIO(PL_statgv);
3488 PL_laststatval = -1;
3489 PL_laststype = OP_STAT;
3490 if (io && IoIFP(io)) {
3492 if (! PerlIO_has_base(IoIFP(io)))
3493 DIE(aTHX_ "-T and -B not implemented on filehandles");
3494 fd = PerlIO_fileno(IoIFP(io));
3496 SETERRNO(EBADF,RMS_IFI);
3499 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
3500 if (PL_laststatval < 0)
3502 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3503 if (PL_op->op_type == OP_FTTEXT)
3508 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3509 i = PerlIO_getc(IoIFP(io));
3511 (void)PerlIO_ungetc(IoIFP(io),i);
3513 /* null file is anything */
3516 len = PerlIO_get_bufsiz(IoIFP(io));
3517 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3518 /* sfio can have large buffers - limit to 512 */
3523 SETERRNO(EBADF,RMS_IFI);
3525 SETERRNO(EBADF,RMS_IFI);
3534 sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
3536 file = SvPVX_const(PL_statname);
3538 if (!(fp = PerlIO_open(file, "r"))) {
3540 PL_laststatval = -1;
3541 PL_laststype = OP_STAT;
3543 if (ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
3544 /* PL_warn_nl is constant */
3545 GCC_DIAG_IGNORE(-Wformat-nonliteral);
3546 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3551 PL_laststype = OP_STAT;
3552 fd = PerlIO_fileno(fp);
3554 (void)PerlIO_close(fp);
3555 SETERRNO(EBADF,RMS_IFI);
3558 PL_laststatval = PerlLIO_fstat(fd, &PL_statcache);
3559 if (PL_laststatval < 0) {
3561 (void)PerlIO_close(fp);
3565 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3566 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3567 (void)PerlIO_close(fp);
3569 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3570 FT_RETURNNO; /* special case NFS directories */
3571 FT_RETURNYES; /* null file is anything */
3576 /* now scan s to look for textiness */
3578 #if defined(DOSISH) || defined(USEMYBINMODE)
3579 /* ignore trailing ^Z on short files */
3580 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3585 if (! is_utf8_invariant_string((U8 *) s, len)) {
3587 /* Here contains a variant under UTF-8 . See if the entire string is
3589 if (is_utf8_fixed_width_buf_flags((U8 *) s, len, 0)) {
3590 if (PL_op->op_type == OP_FTTEXT) {
3599 /* Here, is not UTF-8 or is entirely ASCII. Look through the buffer for
3600 * things that wouldn't be in ASCII text or rich ASCII text. Count these
3602 for (i = 0; i < len; i++, s++) {
3603 if (!*s) { /* null never allowed in text */
3607 #ifdef USE_LOCALE_CTYPE
3608 if (IN_LC_RUNTIME(LC_CTYPE)) {
3609 if ( isPRINT_LC(*s) || isSPACE_LC(*s)) {
3616 /* VT occurs so rarely in text, that we consider it odd */
3617 || (isSPACE_A(*s) && *s != VT_NATIVE)
3619 /* But there is a fair amount of backspaces and escapes in
3622 || *s == ESC_NATIVE)
3629 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3640 const char *tmps = NULL;
3644 SV * const sv = POPs;
3645 if (PL_op->op_flags & OPf_SPECIAL) {
3646 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3648 if (ckWARN(WARN_UNOPENED)) {
3649 Perl_warner(aTHX_ packWARN(WARN_UNOPENED),
3650 "chdir() on unopened filehandle %" SVf, sv);
3652 SETERRNO(EBADF,RMS_IFI);
3654 TAINT_PROPER("chdir");
3658 else if (!(gv = MAYBE_DEREF_GV(sv)))
3659 tmps = SvPV_nomg_const_nolen(sv);
3662 HV * const table = GvHVn(PL_envgv);
3666 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3667 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3669 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3673 tmps = SvPV_nolen_const(*svp);
3677 SETERRNO(EINVAL, LIB_INVARG);
3678 TAINT_PROPER("chdir");
3683 TAINT_PROPER("chdir");
3686 IO* const io = GvIO(gv);
3689 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3690 } else if (IoIFP(io)) {
3691 int fd = PerlIO_fileno(IoIFP(io));
3695 PUSHi(fchdir(fd) >= 0);
3705 DIE(aTHX_ PL_no_func, "fchdir");
3709 PUSHi( PerlDir_chdir(tmps) >= 0 );
3711 /* Clear the DEFAULT element of ENV so we'll get the new value
3713 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3720 SETERRNO(EBADF,RMS_IFI);
3727 /* also used for: pp_chmod() pp_kill() pp_unlink() pp_utime() */
3731 dSP; dMARK; dTARGET;
3732 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3743 char * const tmps = POPpx;
3744 TAINT_PROPER("chroot");
3745 PUSHi( chroot(tmps) >= 0 );
3748 DIE(aTHX_ PL_no_func, "chroot");
3759 const char * const tmps2 = POPpconstx;
3760 const char * const tmps = SvPV_nolen_const(TOPs);
3761 TAINT_PROPER("rename");
3763 anum = PerlLIO_rename(tmps, tmps2);
3765 if (!(anum = PerlLIO_stat(tmps, &statbuf))) {
3766 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3769 if (PerlProc_geteuid() || PerlLIO_stat(tmps2, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
3770 (void)UNLINK(tmps2);
3771 if (!(anum = link(tmps, tmps2)))
3772 anum = UNLINK(tmps);
3781 /* also used for: pp_symlink() */
3783 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3787 const int op_type = PL_op->op_type;
3791 if (op_type == OP_LINK)
3792 DIE(aTHX_ PL_no_func, "link");
3794 # ifndef HAS_SYMLINK
3795 if (op_type == OP_SYMLINK)
3796 DIE(aTHX_ PL_no_func, "symlink");
3800 const char * const tmps2 = POPpconstx;
3801 const char * const tmps = SvPV_nolen_const(TOPs);
3802 TAINT_PROPER(PL_op_desc[op_type]);
3804 # if defined(HAS_LINK)
3805 # if defined(HAS_SYMLINK)
3806 /* Both present - need to choose which. */
3807 (op_type == OP_LINK) ?
3808 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3810 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3811 PerlLIO_link(tmps, tmps2);
3814 # if defined(HAS_SYMLINK)
3815 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3816 symlink(tmps, tmps2);
3821 SETi( result >= 0 );
3826 /* also used for: pp_symlink() */
3831 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3841 char buf[MAXPATHLEN];
3846 /* NOTE: if the length returned by readlink() is sizeof(buf) - 1,
3847 * it is impossible to know whether the result was truncated. */
3848 len = readlink(tmps, buf, sizeof(buf) - 1);
3857 RETSETUNDEF; /* just pretend it's a normal file */
3861 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3863 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3865 char * const save_filename = filename;
3870 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3872 PERL_ARGS_ASSERT_DOONELINER;
3874 Newx(cmdline, size, char);
3875 my_strlcpy(cmdline, cmd, size);
3876 my_strlcat(cmdline, " ", size);
3877 for (s = cmdline + strlen(cmdline); *filename; ) {
3881 if (s - cmdline < size)
3882 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3883 myfp = PerlProc_popen(cmdline, "r");
3887 SV * const tmpsv = sv_newmortal();
3888 /* Need to save/restore 'PL_rs' ?? */
3889 s = sv_gets(tmpsv, myfp, 0);
3890 (void)PerlProc_pclose(myfp);
3894 #ifdef HAS_SYS_ERRLIST
3899 /* you don't see this */
3900 const char * const errmsg = Strerror(e) ;
3903 if (instr(s, errmsg)) {
3910 #define EACCES EPERM
3912 if (instr(s, "cannot make"))
3913 SETERRNO(EEXIST,RMS_FEX);
3914 else if (instr(s, "existing file"))
3915 SETERRNO(EEXIST,RMS_FEX);
3916 else if (instr(s, "ile exists"))
3917 SETERRNO(EEXIST,RMS_FEX);
3918 else if (instr(s, "non-exist"))
3919 SETERRNO(ENOENT,RMS_FNF);
3920 else if (instr(s, "does not exist"))
3921 SETERRNO(ENOENT,RMS_FNF);
3922 else if (instr(s, "not empty"))
3923 SETERRNO(EBUSY,SS_DEVOFFLINE);
3924 else if (instr(s, "cannot access"))
3925 SETERRNO(EACCES,RMS_PRV);
3927 SETERRNO(EPERM,RMS_PRV);
3930 else { /* some mkdirs return no failure indication */
3932 anum = (PerlLIO_stat(save_filename, &statbuf) >= 0);
3933 if (PL_op->op_type == OP_RMDIR)
3938 SETERRNO(EACCES,RMS_PRV); /* a guess */
3947 /* This macro removes trailing slashes from a directory name.
3948 * Different operating and file systems take differently to
3949 * trailing slashes. According to POSIX 1003.1 1996 Edition
3950 * any number of trailing slashes should be allowed.
3951 * Thusly we snip them away so that even non-conforming
3952 * systems are happy.
3953 * We should probably do this "filtering" for all
3954 * the functions that expect (potentially) directory names:
3955 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3956 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3958 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3959 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3962 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3963 (tmps) = savepvn((tmps), (len)); \
3973 const unsigned int mode = (MAXARG > 1 && (TOPs||((void)POPs,0))) ? POPu : 0777;
3975 TRIMSLASHES(tmps,len,copy);
3977 TAINT_PROPER("mkdir");
3979 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3983 SETi( dooneliner("mkdir", tmps) );
3984 oldumask = PerlLIO_umask(0);
3985 PerlLIO_umask(oldumask);
3986 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
4001 TRIMSLASHES(tmps,len,copy);
4002 TAINT_PROPER("rmdir");
4004 SETi( PerlDir_rmdir(tmps) >= 0 );
4006 SETi( dooneliner("rmdir", tmps) );
4013 /* Directory calls. */
4017 #if defined(Direntry_t) && defined(HAS_READDIR)
4019 const char * const dirname = POPpconstx;
4020 GV * const gv = MUTABLE_GV(POPs);
4021 IO * const io = GvIOn(gv);
4023 if ((IoIFP(io) || IoOFP(io)))
4024 Perl_ck_warner_d(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
4025 "Opening filehandle %" HEKf " also as a directory. This will be a fatal error in Perl 5.28",
4026 HEKfARG(GvENAME_HEK(gv)) );
4028 PerlDir_close(IoDIRP(io));
4029 if (!(IoDIRP(io) = PerlDir_open(dirname)))
4035 SETERRNO(EBADF,RMS_DIR);
4038 DIE(aTHX_ PL_no_dir_func, "opendir");
4044 #if !defined(Direntry_t) || !defined(HAS_READDIR)
4045 DIE(aTHX_ PL_no_dir_func, "readdir");
4047 #if !defined(I_DIRENT) && !defined(VMS)
4048 Direntry_t *readdir (DIR *);
4053 const U8 gimme = GIMME_V;
4054 GV * const gv = MUTABLE_GV(POPs);
4055 const Direntry_t *dp;
4056 IO * const io = GvIOn(gv);
4059 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4060 "readdir() attempted on invalid dirhandle %" HEKf,
4061 HEKfARG(GvENAME_HEK(gv)));
4066 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
4070 sv = newSVpvn(dp->d_name, dp->d_namlen);
4072 sv = newSVpv(dp->d_name, 0);
4074 if (!(IoFLAGS(io) & IOf_UNTAINT))
4077 } while (gimme == G_ARRAY);
4079 if (!dp && gimme != G_ARRAY)
4086 SETERRNO(EBADF,RMS_ISI);
4087 if (gimme == G_ARRAY)
4096 #if defined(HAS_TELLDIR) || defined(telldir)
4098 /* XXX does _anyone_ need this? --AD 2/20/1998 */
4099 /* XXX netbsd still seemed to.
4100 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
4101 --JHI 1999-Feb-02 */
4102 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
4103 long telldir (DIR *);
4105 GV * const gv = MUTABLE_GV(POPs);
4106 IO * const io = GvIOn(gv);
4109 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4110 "telldir() attempted on invalid dirhandle %" HEKf,
4111 HEKfARG(GvENAME_HEK(gv)));
4115 PUSHi( PerlDir_tell(IoDIRP(io)) );
4119 SETERRNO(EBADF,RMS_ISI);
4122 DIE(aTHX_ PL_no_dir_func, "telldir");
4128 #if defined(HAS_SEEKDIR) || defined(seekdir)
4130 const long along = POPl;
4131 GV * const gv = MUTABLE_GV(POPs);
4132 IO * const io = GvIOn(gv);
4135 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4136 "seekdir() attempted on invalid dirhandle %" HEKf,
4137 HEKfARG(GvENAME_HEK(gv)));
4140 (void)PerlDir_seek(IoDIRP(io), along);
4145 SETERRNO(EBADF,RMS_ISI);
4148 DIE(aTHX_ PL_no_dir_func, "seekdir");
4154 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4156 GV * const gv = MUTABLE_GV(POPs);
4157 IO * const io = GvIOn(gv);
4160 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4161 "rewinddir() attempted on invalid dirhandle %" HEKf,
4162 HEKfARG(GvENAME_HEK(gv)));
4165 (void)PerlDir_rewind(IoDIRP(io));
4169 SETERRNO(EBADF,RMS_ISI);
4172 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4178 #if defined(Direntry_t) && defined(HAS_READDIR)
4180 GV * const gv = MUTABLE_GV(POPs);
4181 IO * const io = GvIOn(gv);
4184 Perl_ck_warner(aTHX_ packWARN(WARN_IO),
4185 "closedir() attempted on invalid dirhandle %" HEKf,
4186 HEKfARG(GvENAME_HEK(gv)));
4189 #ifdef VOID_CLOSEDIR
4190 PerlDir_close(IoDIRP(io));
4192 if (PerlDir_close(IoDIRP(io)) < 0) {
4193 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4202 SETERRNO(EBADF,RMS_IFI);
4205 DIE(aTHX_ PL_no_dir_func, "closedir");
4209 /* Process control. */
4216 #ifdef HAS_SIGPROCMASK
4217 sigset_t oldmask, newmask;
4221 PERL_FLUSHALL_FOR_CHILD;
4222 #ifdef HAS_SIGPROCMASK
4223 sigfillset(&newmask);
4224 sigprocmask(SIG_SETMASK, &newmask, &oldmask);
4226 childpid = PerlProc_fork();
4227 if (childpid == 0) {
4231 for (sig = 1; sig < SIG_SIZE; sig++)
4232 PL_psig_pend[sig] = 0;
4234 #ifdef HAS_SIGPROCMASK
4237 sigprocmask(SIG_SETMASK, &oldmask, NULL);
4244 #ifdef PERL_USES_PL_PIDSTATUS
4245 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4251 # if (defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)) || defined(__amigaos4__)
4256 PERL_FLUSHALL_FOR_CHILD;
4257 childpid = PerlProc_fork();
4263 DIE(aTHX_ PL_no_func, "fork");
4270 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4275 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4276 childpid = wait4pid(-1, &argflags, 0);
4278 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4283 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4284 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4285 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4287 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4292 DIE(aTHX_ PL_no_func, "wait");
4298 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
4300 const int optype = POPi;
4301 const Pid_t pid = TOPi;
4305 result = amigaos_waitpid(aTHX_ optype, pid, &argflags);
4306 STATUS_NATIVE_CHILD_SET((result >= 0) ? argflags : -1);
4307 result = result == 0 ? pid : -1;
4311 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4312 result = wait4pid(pid, &argflags, optype);
4314 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4319 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4320 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4321 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4323 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4325 # endif /* __amigaos4__ */
4329 DIE(aTHX_ PL_no_func, "waitpid");
4335 dSP; dMARK; dORIGMARK; dTARGET;
4336 #if defined(__LIBCATAMOUNT__)
4337 PL_statusvalue = -1;
4342 # ifdef __amigaos4__
4350 while (++MARK <= SP) {
4351 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4356 TAINT_PROPER("system");
4358 PERL_FLUSHALL_FOR_CHILD;
4359 #if (defined(HAS_FORK) || defined(__amigaos4__)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4362 struct UserData userdata;
4369 bool child_success = FALSE;
4370 #ifdef HAS_SIGPROCMASK
4371 sigset_t newset, oldset;
4374 if (PerlProc_pipe(pp) >= 0)
4377 amigaos_fork_set_userdata(aTHX_
4383 pthread_create(&proc,NULL,amigaos_system_child,(void *)&userdata);
4384 child_success = proc > 0;
4386 #ifdef HAS_SIGPROCMASK
4387 sigemptyset(&newset);
4388 sigaddset(&newset, SIGCHLD);
4389 sigprocmask(SIG_BLOCK, &newset, &oldset);
4391 while ((childpid = PerlProc_fork()) == -1) {
4392 if (errno != EAGAIN) {
4397 PerlLIO_close(pp[0]);
4398 PerlLIO_close(pp[1]);
4400 #ifdef HAS_SIGPROCMASK
4401 sigprocmask(SIG_SETMASK, &oldset, NULL);
4407 child_success = childpid > 0;
4409 if (child_success) {
4410 Sigsave_t ihand,qhand; /* place to save signals during system() */
4413 #ifndef __amigaos4__
4415 PerlLIO_close(pp[1]);
4418 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4419 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4422 result = pthread_join(proc, (void **)&status);
4425 result = wait4pid(childpid, &status, 0);
4426 } while (result == -1 && errno == EINTR);
4429 #ifdef HAS_SIGPROCMASK
4430 sigprocmask(SIG_SETMASK, &oldset, NULL);
4432 (void)rsignal_restore(SIGINT, &ihand);
4433 (void)rsignal_restore(SIGQUIT, &qhand);
4435 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4436 do_execfree(); /* free any memory child malloced on fork */
4442 while (n < sizeof(int)) {
4443 const SSize_t n1 = PerlLIO_read(pp[0],
4444 (void*)(((char*)&errkid)+n),
4450 PerlLIO_close(pp[0]);
4451 if (n) { /* Error */
4452 if (n != sizeof(int))
4453 DIE(aTHX_ "panic: kid popen errno read, n=%u", n);
4454 errno = errkid; /* Propagate errno from kid */
4456 /* The pipe always has something in it
4457 * so n alone is not enough. */
4461 STATUS_NATIVE_CHILD_SET(-1);
4465 XPUSHi(STATUS_CURRENT);
4468 #ifndef __amigaos4__
4469 #ifdef HAS_SIGPROCMASK
4470 sigprocmask(SIG_SETMASK, &oldset, NULL);
4473 PerlLIO_close(pp[0]);
4474 #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC)
4475 if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0)
4479 if (PL_op->op_flags & OPf_STACKED) {
4480 SV * const really = *++MARK;
4481 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4483 else if (SP - MARK != 1)
4484 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4486 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4488 #endif /* __amigaos4__ */
4491 #else /* ! FORK or VMS or OS/2 */
4494 if (PL_op->op_flags & OPf_STACKED) {
4495 SV * const really = *++MARK;
4496 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4497 value = (I32)do_aspawn(really, MARK, SP);
4499 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4502 else if (SP - MARK != 1) {
4503 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4504 value = (I32)do_aspawn(NULL, MARK, SP);
4506 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4510 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4512 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4514 STATUS_NATIVE_CHILD_SET(value);
4517 XPUSHi(result ? value : STATUS_CURRENT);
4518 #endif /* !FORK or VMS or OS/2 */
4525 dSP; dMARK; dORIGMARK; dTARGET;
4530 while (++MARK <= SP) {
4531 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4536 TAINT_PROPER("exec");
4539 PERL_FLUSHALL_FOR_CHILD;
4540 if (PL_op->op_flags & OPf_STACKED) {
4541 SV * const really = *++MARK;
4542 value = (I32)do_aexec(really, MARK, SP);
4544 else if (SP - MARK != 1)
4546 value = (I32)vms_do_aexec(NULL, MARK, SP);
4548 value = (I32)do_aexec(NULL, MARK, SP);
4552 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4554 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4566 XPUSHi( getppid() );
4569 DIE(aTHX_ PL_no_func, "getppid");
4579 (MAXARG < 1) ? 0 : TOPs ? SvIVx(POPs) : ((void)POPs, 0);
4582 pgrp = (I32)BSD_GETPGRP(pid);
4584 if (pid != 0 && pid != PerlProc_getpid())
4585 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4591 DIE(aTHX_ PL_no_func, "getpgrp");
4601 pgrp = MAXARG == 2 && (TOPs||POPs) ? POPi : 0;
4602 if (MAXARG > 0) pid = TOPs ? TOPi : 0;
4609 TAINT_PROPER("setpgrp");
4611 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4613 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4614 || (pid != 0 && pid != PerlProc_getpid()))
4616 DIE(aTHX_ "setpgrp can't take arguments");
4618 SETi( setpgrp() >= 0 );
4619 #endif /* USE_BSDPGRP */
4622 DIE(aTHX_ PL_no_func, "setpgrp");
4626 #if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || (__GLIBC__ > 2))
4627 # define PRIORITY_WHICH_T(which) (__priority_which_t)which
4629 # define PRIORITY_WHICH_T(which) which
4634 #ifdef HAS_GETPRIORITY
4636 const int who = POPi;
4637 const int which = TOPi;
4638 SETi( getpriority(PRIORITY_WHICH_T(which), who) );
4641 DIE(aTHX_ PL_no_func, "getpriority");
4647 #ifdef HAS_SETPRIORITY
4649 const int niceval = POPi;
4650 const int who = POPi;
4651 const int which = TOPi;
4652 TAINT_PROPER("setpriority");
4653 SETi( setpriority(PRIORITY_WHICH_T(which), who, niceval) >= 0 );
4656 DIE(aTHX_ PL_no_func, "setpriority");
4660 #undef PRIORITY_WHICH_T
4668 XPUSHn( time(NULL) );
4670 XPUSHi( time(NULL) );
4679 struct tms timesbuf;
4682 (void)PerlProc_times(×buf);
4684 mPUSHn(((NV)timesbuf.tms_utime)/(NV)PL_clocktick);
4685 if (GIMME_V == G_ARRAY) {
4686 mPUSHn(((NV)timesbuf.tms_stime)/(NV)PL_clocktick);
4687 mPUSHn(((NV)timesbuf.tms_cutime)/(NV)PL_clocktick);
4688 mPUSHn(((NV)timesbuf.tms_cstime)/(NV)PL_clocktick);
4696 if (GIMME_V == G_ARRAY) {
4703 DIE(aTHX_ "times not implemented");
4705 #endif /* HAS_TIMES */
4708 /* The 32 bit int year limits the times we can represent to these
4709 boundaries with a few days wiggle room to account for time zone
4712 /* Sat Jan 3 00:00:00 -2147481748 */
4713 #define TIME_LOWER_BOUND -67768100567755200.0
4714 /* Sun Dec 29 12:00:00 2147483647 */
4715 #define TIME_UPPER_BOUND 67767976233316800.0
4718 /* also used for: pp_localtime() */
4726 const char *opname = PL_op->op_type == OP_LOCALTIME ? "localtime" : "gmtime";
4727 static const char * const dayname[] =
4728 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4729 static const char * const monname[] =
4730 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4731 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4733 if (MAXARG < 1 || (!TOPs && ((void)POPs, 1))) {
4736 when = (Time64_T)now;
4739 NV input = Perl_floor(POPn);
4740 const bool pl_isnan = Perl_isnan(input);
4741 when = (Time64_T)input;
4742 if (UNLIKELY(pl_isnan || when != input)) {
4743 /* diag_listed_as: gmtime(%f) too large */
4744 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4745 "%s(%.0" NVff ") too large", opname, input);
4753 if ( TIME_LOWER_BOUND > when ) {
4754 /* diag_listed_as: gmtime(%f) too small */
4755 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4756 "%s(%.0" NVff ") too small", opname, when);
4759 else if( when > TIME_UPPER_BOUND ) {
4760 /* diag_listed_as: gmtime(%f) too small */
4761 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4762 "%s(%.0" NVff ") too large", opname, when);
4766 if (PL_op->op_type == OP_LOCALTIME)
4767 err = Perl_localtime64_r(&when, &tmbuf);
4769 err = Perl_gmtime64_r(&when, &tmbuf);
4773 /* diag_listed_as: gmtime(%f) failed */
4774 /* XXX %lld broken for quads */
4776 Perl_ck_warner(aTHX_ packWARN(WARN_OVERFLOW),
4777 "%s(%.0" NVff ") failed", opname, when);
4780 if (GIMME_V != G_ARRAY) { /* scalar context */
4787 Perl_sv_setpvf_mg(aTHX_ TARG, "%s %s %2d %02d:%02d:%02d %" IVdf,
4788 dayname[tmbuf.tm_wday],
4789 monname[tmbuf.tm_mon],
4794 (IV)tmbuf.tm_year + 1900);
4797 else { /* list context */
4803 mPUSHi(tmbuf.tm_sec);
4804 mPUSHi(tmbuf.tm_min);
4805 mPUSHi(tmbuf.tm_hour);
4806 mPUSHi(tmbuf.tm_mday);
4807 mPUSHi(tmbuf.tm_mon);
4808 mPUSHn(tmbuf.tm_year);
4809 mPUSHi(tmbuf.tm_wday);
4810 mPUSHi(tmbuf.tm_yday);
4811 mPUSHi(tmbuf.tm_isdst);
4820 /* alarm() takes an unsigned int number of seconds, and return the
4821 * unsigned int number of seconds remaining in the previous alarm
4822 * (alarms don't stack). Therefore negative return values are not
4826 /* Note that while the C library function alarm() as such has
4827 * no errors defined (or in other words, properly behaving client
4828 * code shouldn't expect any), alarm() being obsoleted by
4829 * setitimer() and often being implemented in terms of
4830 * setitimer(), can fail. */
4831 /* diag_listed_as: %s() with negative argument */
4832 Perl_ck_warner_d(aTHX_ packWARN(WARN_MISC),
4833 "alarm() with negative argument");
4834 SETERRNO(EINVAL, LIB_INVARG);
4838 unsigned int retval = alarm(anum);
4839 if ((int)retval < 0) /* Strictly speaking "cannot happen". */
4845 DIE(aTHX_ PL_no_func, "alarm");
4855 (void)time(&lasttime);
4856 if (MAXARG < 1 || (!TOPs && !POPs))
4859 const I32 duration = POPi;
4861 /* diag_listed_as: %s() with negative argument */
4862 Perl_ck_warner_d(aTHX_ packWARN(WARN_MISC),
4863 "sleep() with negative argument");
4864 SETERRNO(EINVAL, LIB_INVARG);
4868 PerlProc_sleep((unsigned int)duration);
4872 XPUSHi(when - lasttime);
4876 /* Shared memory. */
4877 /* Merged with some message passing. */
4879 /* also used for: pp_msgrcv() pp_msgsnd() pp_semop() pp_shmread() */
4883 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4884 dSP; dMARK; dTARGET;
4885 const int op_type = PL_op->op_type;
4890 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4893 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4896 value = (I32)(do_semop(MARK, SP) >= 0);
4899 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4907 return Perl_pp_semget(aTHX);
4913 /* also used for: pp_msgget() pp_shmget() */
4917 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4918 dSP; dMARK; dTARGET;
4919 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4926 DIE(aTHX_ "System V IPC is not implemented on this machine");
4930 /* also used for: pp_msgctl() pp_shmctl() */
4934 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4935 dSP; dMARK; dTARGET;
4936 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4944 PUSHp(zero_but_true, ZBTLEN);
4948 return Perl_pp_semget(aTHX);
4952 /* I can't const this further without getting warnings about the types of
4953 various arrays passed in from structures. */
4955 S_space_join_names_mortal(pTHX_ char *const *array)
4959 if (array && *array) {
4960 target = newSVpvs_flags("", SVs_TEMP);
4962 sv_catpv(target, *array);
4965 sv_catpvs(target, " ");
4968 target = sv_mortalcopy(&PL_sv_no);
4973 /* Get system info. */
4975 /* also used for: pp_ghbyaddr() pp_ghbyname() */
4979 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4981 I32 which = PL_op->op_type;
4984 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4985 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4986 struct hostent *gethostbyname(Netdb_name_t);
4987 struct hostent *gethostent(void);
4989 struct hostent *hent = NULL;
4993 if (which == OP_GHBYNAME) {
4994 #ifdef HAS_GETHOSTBYNAME
4995 const char* const name = POPpbytex;
4996 hent = PerlSock_gethostbyname(name);
4998 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
5001 else if (which == OP_GHBYADDR) {
5002 #ifdef HAS_GETHOSTBYADDR
5003 const int addrtype = POPi;
5004 SV * const addrsv = POPs;
5006 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
5008 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
5010 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
5014 #ifdef HAS_GETHOSTENT
5015 hent = PerlSock_gethostent();
5017 DIE(aTHX_ PL_no_sock_func, "gethostent");
5020 #ifdef HOST_NOT_FOUND
5022 #ifdef USE_REENTRANT_API
5023 # ifdef USE_GETHOSTENT_ERRNO
5024 h_errno = PL_reentrant_buffer->_gethostent_errno;
5027 STATUS_UNIX_SET(h_errno);
5031 if (GIMME_V != G_ARRAY) {
5032 PUSHs(sv = sv_newmortal());
5034 if (which == OP_GHBYNAME) {
5036 sv_setpvn(sv, hent->h_addr, hent->h_length);
5039 sv_setpv(sv, (char*)hent->h_name);
5045 mPUSHs(newSVpv((char*)hent->h_name, 0));
5046 PUSHs(space_join_names_mortal(hent->h_aliases));
5047 mPUSHi(hent->h_addrtype);
5048 len = hent->h_length;
5051 for (elem = hent->h_addr_list; elem && *elem; elem++) {
5052 mXPUSHp(*elem, len);
5056 mPUSHp(hent->h_addr, len);
5058 PUSHs(sv_mortalcopy(&PL_sv_no));
5063 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5067 /* also used for: pp_gnbyaddr() pp_gnbyname() */
5071 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
5073 I32 which = PL_op->op_type;
5075 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
5076 struct netent *getnetbyaddr(Netdb_net_t, int);
5077 struct netent *getnetbyname(Netdb_name_t);
5078 struct netent *getnetent(void);
5080 struct netent *nent;
5082 if (which == OP_GNBYNAME){
5083 #ifdef HAS_GETNETBYNAME
5084 const char * const name = POPpbytex;
5085 nent = PerlSock_getnetbyname(name);
5087 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
5090 else if (which == OP_GNBYADDR) {
5091 #ifdef HAS_GETNETBYADDR
5092 const int addrtype = POPi;
5093 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
5094 nent = PerlSock_getnetbyaddr(addr, addrtype);
5096 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
5100 #ifdef HAS_GETNETENT
5101 nent = PerlSock_getnetent();
5103 DIE(aTHX_ PL_no_sock_func, "getnetent");
5106 #ifdef HOST_NOT_FOUND
5108 #ifdef USE_REENTRANT_API
5109 # ifdef USE_GETNETENT_ERRNO
5110 h_errno = PL_reentrant_buffer->_getnetent_errno;
5113 STATUS_UNIX_SET(h_errno);
5118 if (GIMME_V != G_ARRAY) {
5119 PUSHs(sv = sv_newmortal());
5121 if (which == OP_GNBYNAME)
5122 sv_setiv(sv, (IV)nent->n_net);
5124 sv_setpv(sv, nent->n_name);
5130 mPUSHs(newSVpv(nent->n_name, 0));
5131 PUSHs(space_join_names_mortal(nent->n_aliases));
5132 mPUSHi(nent->n_addrtype);
5133 mPUSHi(nent->n_net);
5138 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5143 /* also used for: pp_gpbyname() pp_gpbynumber() */
5147 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
5149 I32 which = PL_op->op_type;
5151 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
5152 struct protoent *getprotobyname(Netdb_name_t);
5153 struct protoent *getprotobynumber(int);
5154 struct protoent *getprotoent(void);
5156 struct protoent *pent;
5158 if (which == OP_GPBYNAME) {
5159 #ifdef HAS_GETPROTOBYNAME
5160 const char* const name = POPpbytex;
5161 pent = PerlSock_getprotobyname(name);
5163 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
5166 else if (which == OP_GPBYNUMBER) {
5167 #ifdef HAS_GETPROTOBYNUMBER
5168 const int number = POPi;
5169 pent = PerlSock_getprotobynumber(number);
5171 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5175 #ifdef HAS_GETPROTOENT
5176 pent = PerlSock_getprotoent();
5178 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5182 if (GIMME_V != G_ARRAY) {
5183 PUSHs(sv = sv_newmortal());
5185 if (which == OP_GPBYNAME)
5186 sv_setiv(sv, (IV)pent->p_proto);
5188 sv_setpv(sv, pent->p_name);
5194 mPUSHs(newSVpv(pent->p_name, 0));
5195 PUSHs(space_join_names_mortal(pent->p_aliases));
5196 mPUSHi(pent->p_proto);
5201 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5206 /* also used for: pp_gsbyname() pp_gsbyport() */
5210 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5212 I32 which = PL_op->op_type;
5214 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5215 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5216 struct servent *getservbyport(int, Netdb_name_t);
5217 struct servent *getservent(void);
5219 struct servent *sent;
5221 if (which == OP_GSBYNAME) {
5222 #ifdef HAS_GETSERVBYNAME
5223 const char * const proto = POPpbytex;
5224 const char * const name = POPpbytex;
5225 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
5227 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5230 else if (which == OP_GSBYPORT) {
5231 #ifdef HAS_GETSERVBYPORT
5232 const char * const proto = POPpbytex;
5233 unsigned short port = (unsigned short)POPu;
5234 port = PerlSock_htons(port);
5235 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
5237 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5241 #ifdef HAS_GETSERVENT
5242 sent = PerlSock_getservent();
5244 DIE(aTHX_ PL_no_sock_func, "getservent");
5248 if (GIMME_V != G_ARRAY) {
5249 PUSHs(sv = sv_newmortal());
5251 if (which == OP_GSBYNAME) {
5252 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5255 sv_setpv(sv, sent->s_name);
5261 mPUSHs(newSVpv(sent->s_name, 0));
5262 PUSHs(space_join_names_mortal(sent->s_aliases));
5263 mPUSHi(PerlSock_ntohs(sent->s_port));
5264 mPUSHs(newSVpv(sent->s_proto, 0));
5269 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5274 /* also used for: pp_snetent() pp_sprotoent() pp_sservent() */
5279 const int stayopen = TOPi;
5280 switch(PL_op->op_type) {
5282 #ifdef HAS_SETHOSTENT
5283 PerlSock_sethostent(stayopen);
5285 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5288 #ifdef HAS_SETNETENT
5290 PerlSock_setnetent(stayopen);
5292 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5296 #ifdef HAS_SETPROTOENT
5297 PerlSock_setprotoent(stayopen);
5299 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5303 #ifdef HAS_SETSERVENT
5304 PerlSock_setservent(stayopen);
5306 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5314 /* also used for: pp_egrent() pp_enetent() pp_eprotoent() pp_epwent()
5315 * pp_eservent() pp_sgrent() pp_spwent() */
5320 switch(PL_op->op_type) {
5322 #ifdef HAS_ENDHOSTENT
5323 PerlSock_endhostent();
5325 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5329 #ifdef HAS_ENDNETENT
5330 PerlSock_endnetent();
5332 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5336 #ifdef HAS_ENDPROTOENT
5337 PerlSock_endprotoent();
5339 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5343 #ifdef HAS_ENDSERVENT
5344 PerlSock_endservent();
5346 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
5350 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5353 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5357 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5360 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5364 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5367 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5371 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5374 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5383 /* also used for: pp_gpwnam() pp_gpwuid() */
5389 I32 which = PL_op->op_type;
5391 struct passwd *pwent = NULL;
5393 * We currently support only the SysV getsp* shadow password interface.
5394 * The interface is declared in <shadow.h> and often one needs to link
5395 * with -lsecurity or some such.
5396 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5399 * AIX getpwnam() is clever enough to return the encrypted password
5400 * only if the caller (euid?) is root.
5402 * There are at least three other shadow password APIs. Many platforms
5403 * seem to contain more than one interface for accessing the shadow
5404 * password databases, possibly for compatibility reasons.
5405 * The getsp*() is by far he simplest one, the other two interfaces
5406 * are much more complicated, but also very similar to each other.
5411 * struct pr_passwd *getprpw*();
5412 * The password is in
5413 * char getprpw*(...).ufld.fd_encrypt[]
5414 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5419 * struct es_passwd *getespw*();
5420 * The password is in
5421 * char *(getespw*(...).ufld.fd_encrypt)
5422 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5425 * struct userpw *getuserpw();
5426 * The password is in
5427 * char *(getuserpw(...)).spw_upw_passwd
5428 * (but the de facto standard getpwnam() should work okay)
5430 * Mention I_PROT here so that Configure probes for it.
5432 * In HP-UX for getprpw*() the manual page claims that one should include
5433 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5434 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5435 * and pp_sys.c already includes <shadow.h> if there is such.
5437 * Note that <sys/security.h> is already probed for, but currently
5438 * it is only included in special cases.
5440 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5441 * be preferred interface, even though also the getprpw*() interface
5442 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5443 * One also needs to call set_auth_parameters() in main() before
5444 * doing anything else, whether one is using getespw*() or getprpw*().
5446 * Note that accessing the shadow databases can be magnitudes
5447 * slower than accessing the standard databases.
5452 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5453 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5454 * the pw_comment is left uninitialized. */
5455 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5461 const char* const name = POPpbytex;
5462 pwent = getpwnam(name);
5468 pwent = getpwuid(uid);
5472 # ifdef HAS_GETPWENT
5474 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5475 if (pwent) pwent = getpwnam(pwent->pw_name);
5478 DIE(aTHX_ PL_no_func, "getpwent");
5484 if (GIMME_V != G_ARRAY) {
5485 PUSHs(sv = sv_newmortal());
5487 if (which == OP_GPWNAM)
5488 sv_setuid(sv, pwent->pw_uid);
5490 sv_setpv(sv, pwent->pw_name);
5496 mPUSHs(newSVpv(pwent->pw_name, 0));
5500 /* If we have getspnam(), we try to dig up the shadow
5501 * password. If we are underprivileged, the shadow
5502 * interface will set the errno to EACCES or similar,
5503 * and return a null pointer. If this happens, we will
5504 * use the dummy password (usually "*" or "x") from the
5505 * standard password database.
5507 * In theory we could skip the shadow call completely
5508 * if euid != 0 but in practice we cannot know which
5509 * security measures are guarding the shadow databases
5510 * on a random platform.
5512 * Resist the urge to use additional shadow interfaces.
5513 * Divert the urge to writing an extension instead.
5516 /* Some AIX setups falsely(?) detect some getspnam(), which
5517 * has a different API than the Solaris/IRIX one. */
5518 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5521 const struct spwd * const spwent = getspnam(pwent->pw_name);
5522 /* Save and restore errno so that
5523 * underprivileged attempts seem
5524 * to have never made the unsuccessful
5525 * attempt to retrieve the shadow password. */
5527 if (spwent && spwent->sp_pwdp)
5528 sv_setpv(sv, spwent->sp_pwdp);
5532 if (!SvPOK(sv)) /* Use the standard password, then. */
5533 sv_setpv(sv, pwent->pw_passwd);
5536 /* passwd is tainted because user himself can diddle with it.
5537 * admittedly not much and in a very limited way, but nevertheless. */
5540 sv_setuid(PUSHmortal, pwent->pw_uid);
5541 sv_setgid(PUSHmortal, pwent->pw_gid);
5543 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5544 * because of the poor interface of the Perl getpw*(),
5545 * not because there's some standard/convention saying so.
5546 * A better interface would have been to return a hash,
5547 * but we are accursed by our history, alas. --jhi. */
5549 mPUSHi(pwent->pw_change);
5552 mPUSHi(pwent->pw_quota);
5555 mPUSHs(newSVpv(pwent->pw_age, 0));
5557 /* I think that you can never get this compiled, but just in case. */
5558 PUSHs(sv_mortalcopy(&PL_sv_no));
5563 /* pw_class and pw_comment are mutually exclusive--.
5564 * see the above note for pw_change, pw_quota, and pw_age. */
5566 mPUSHs(newSVpv(pwent->pw_class, 0));
5569 mPUSHs(newSVpv(pwent->pw_comment, 0));
5571 /* I think that you can never get this compiled, but just in case. */
5572 PUSHs(sv_mortalcopy(&PL_sv_no));
5577 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5579 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5581 /* pw_gecos is tainted because user himself can diddle with it. */
5584 mPUSHs(newSVpv(pwent->pw_dir, 0));
5586 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5587 /* pw_shell is tainted because user himself can diddle with it. */
5591 mPUSHi(pwent->pw_expire);
5596 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5601 /* also used for: pp_ggrgid() pp_ggrnam() */
5607 const I32 which = PL_op->op_type;
5608 const struct group *grent;
5610 if (which == OP_GGRNAM) {
5611 const char* const name = POPpbytex;
5612 grent = (const struct group *)getgrnam(name);
5614 else if (which == OP_GGRGID) {
5616 const Gid_t gid = POPu;
5617 #elif Gid_t_sign == -1
5618 const Gid_t gid = POPi;
5620 # error "Unexpected Gid_t_sign"
5622 grent = (const struct group *)getgrgid(gid);
5626 grent = (struct group *)getgrent();
5628 DIE(aTHX_ PL_no_func, "getgrent");
5632 if (GIMME_V != G_ARRAY) {
5633 SV * const sv = sv_newmortal();
5637 if (which == OP_GGRNAM)
5638 sv_setgid(sv, grent->gr_gid);
5640 sv_setpv(sv, grent->gr_name);
5646 mPUSHs(newSVpv(grent->gr_name, 0));
5649 mPUSHs(newSVpv(grent->gr_passwd, 0));
5651 PUSHs(sv_mortalcopy(&PL_sv_no));
5654 sv_setgid(PUSHmortal, grent->gr_gid);
5656 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5657 /* In UNICOS/mk (_CRAYMPP) the multithreading
5658 * versions (getgrnam_r, getgrgid_r)
5659 * seem to return an illegal pointer
5660 * as the group members list, gr_mem.
5661 * getgrent() doesn't even have a _r version
5662 * but the gr_mem is poisonous anyway.
5663 * So yes, you cannot get the list of group
5664 * members if building multithreaded in UNICOS/mk. */
5665 PUSHs(space_join_names_mortal(grent->gr_mem));
5671 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5681 if (!(tmps = PerlProc_getlogin()))
5683 sv_setpv_mg(TARG, tmps);
5687 DIE(aTHX_ PL_no_func, "getlogin");
5691 /* Miscellaneous. */
5696 dSP; dMARK; dORIGMARK; dTARGET;
5697 I32 items = SP - MARK;
5698 unsigned long a[20];
5703 while (++MARK <= SP) {
5704 if (SvTAINTED(*MARK)) {
5710 TAINT_PROPER("syscall");
5713 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5714 * or where sizeof(long) != sizeof(char*). But such machines will
5715 * not likely have syscall implemented either, so who cares?
5717 while (++MARK <= SP) {
5718 if (SvNIOK(*MARK) || !i)
5719 a[i++] = SvIV(*MARK);
5720 else if (*MARK == &PL_sv_undef)
5723 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5729 DIE(aTHX_ "Too many args to syscall");
5731 DIE(aTHX_ "Too few args to syscall");
5733 retval = syscall(a[0]);
5736 retval = syscall(a[0],a[1]);
5739 retval = syscall(a[0],a[1],a[2]);
5742 retval = syscall(a[0],a[1],a[2],a[3]);
5745 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5748 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5751 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5754 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5761 DIE(aTHX_ PL_no_func, "syscall");
5765 #ifdef FCNTL_EMULATE_FLOCK
5767 /* XXX Emulate flock() with fcntl().
5768 What's really needed is a good file locking module.
5772 fcntl_emulate_flock(int fd, int operation)
5777 switch (operation & ~LOCK_NB) {
5779 flock.l_type = F_RDLCK;
5782 flock.l_type = F_WRLCK;
5785 flock.l_type = F_UNLCK;
5791 flock.l_whence = SEEK_SET;
5792 flock.l_start = flock.l_len = (Off_t)0;
5794 res = fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5795 if (res == -1 && ((errno == EAGAIN) || (errno == EACCES)))
5796 errno = EWOULDBLOCK;
5800 #endif /* FCNTL_EMULATE_FLOCK */
5802 #ifdef LOCKF_EMULATE_FLOCK
5804 /* XXX Emulate flock() with lockf(). This is just to increase
5805 portability of scripts. The calls are not completely
5806 interchangeable. What's really needed is a good file
5810 /* The lockf() constants might have been defined in <unistd.h>.
5811 Unfortunately, <unistd.h> causes troubles on some mixed
5812 (BSD/POSIX) systems, such as SunOS 4.1.3.
5814 Further, the lockf() constants aren't POSIX, so they might not be
5815 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5816 just stick in the SVID values and be done with it. Sigh.
5820 # define F_ULOCK 0 /* Unlock a previously locked region */
5823 # define F_LOCK 1 /* Lock a region for exclusive use */
5826 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5829 # define F_TEST 3 /* Test a region for other processes locks */
5833 lockf_emulate_flock(int fd, int operation)
5839 /* flock locks entire file so for lockf we need to do the same */
5840 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5841 if (pos > 0) /* is seekable and needs to be repositioned */
5842 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5843 pos = -1; /* seek failed, so don't seek back afterwards */
5846 switch (operation) {
5848 /* LOCK_SH - get a shared lock */
5850 /* LOCK_EX - get an exclusive lock */
5852 i = lockf (fd, F_LOCK, 0);
5855 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5856 case LOCK_SH|LOCK_NB:
5857 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5858 case LOCK_EX|LOCK_NB:
5859 i = lockf (fd, F_TLOCK, 0);
5861 if ((errno == EAGAIN) || (errno == EACCES))
5862 errno = EWOULDBLOCK;
5865 /* LOCK_UN - unlock (non-blocking is a no-op) */
5867 case LOCK_UN|LOCK_NB:
5868 i = lockf (fd, F_ULOCK, 0);
5871 /* Default - can't decipher operation */
5878 if (pos > 0) /* need to restore position of the handle */
5879 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5884 #endif /* LOCKF_EMULATE_FLOCK */
5887 * ex: set ts=8 sts=4 sw=4 et: