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
34 /* Shadow password support for solaris - pdo@cs.umd.edu
35 * Not just Solaris: at least HP-UX, IRIX, Linux.
36 * The API is from SysV.
38 * There are at least two more shadow interfaces,
39 * see the comments in pp_gpwent().
43 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
44 * and another MAXINT from "perl.h" <- <sys/param.h>. */
51 # include <sys/wait.h>
55 # include <sys/resource.h>
64 # include <sys/select.h>
68 /* XXX Configure test needed.
69 h_errno might not be a simple 'int', especially for multi-threaded
70 applications, see "extern int errno in perl.h". Creating such
71 a test requires taking into account the differences between
72 compiling multithreaded and singlethreaded ($ccflags et al).
73 HOST_NOT_FOUND is typically defined in <netdb.h>.
75 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
84 struct passwd *getpwnam (char *);
85 struct passwd *getpwuid (Uid_t);
90 struct passwd *getpwent (void);
91 #elif defined (VMS) && defined (my_getpwent)
92 struct passwd *Perl_my_getpwent (pTHX);
101 struct group *getgrnam (char *);
102 struct group *getgrgid (Gid_t);
106 struct group *getgrent (void);
112 # if defined(_MSC_VER) || defined(__MINGW32__)
113 # include <sys/utime.h>
120 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
123 # define my_chsize PerlLIO_chsize
126 # define my_chsize PerlLIO_chsize
128 I32 my_chsize(int fd, Off_t length);
134 #else /* no flock() */
136 /* fcntl.h might not have been included, even if it exists, because
137 the current Configure only sets I_FCNTL if it's needed to pick up
138 the *_OK constants. Make sure it has been included before testing
139 the fcntl() locking constants. */
140 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
144 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
145 # define FLOCK fcntl_emulate_flock
146 # define FCNTL_EMULATE_FLOCK
147 # else /* no flock() or fcntl(F_SETLK,...) */
149 # define FLOCK lockf_emulate_flock
150 # define LOCKF_EMULATE_FLOCK
152 # endif /* no flock() or fcntl(F_SETLK,...) */
155 static int FLOCK (int, int);
158 * These are the flock() constants. Since this sytems doesn't have
159 * flock(), the values of the constants are probably not available.
173 # endif /* emulating flock() */
175 #endif /* no flock() */
178 static const char zero_but_true[ZBTLEN + 1] = "0 but true";
180 #if defined(I_SYS_ACCESS) && !defined(R_OK)
181 # include <sys/access.h>
184 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
185 # define FD_CLOEXEC 1 /* NeXT needs this */
191 /* Missing protos on LynxOS */
192 void sethostent(int);
193 void endhostent(void);
195 void endnetent(void);
196 void setprotoent(int);
197 void endprotoent(void);
198 void setservent(int);
199 void endservent(void);
202 #undef PERL_EFF_ACCESS /* EFFective uid/gid ACCESS */
204 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
205 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
206 * available in the 32bit environment, which could warrant Configure
207 * checks in the future.
210 #define LOCALTIME_EDGECASE_BROKEN
213 /* F_OK unused: if stat() cannot find it... */
215 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
216 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
217 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
220 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
221 # ifdef I_SYS_SECURITY
222 # include <sys/security.h>
226 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
229 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
233 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
235 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
239 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
240 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
241 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
244 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
246 const Uid_t ruid = getuid();
247 const Uid_t euid = geteuid();
248 const Gid_t rgid = getgid();
249 const Gid_t egid = getegid();
253 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
254 Perl_croak(aTHX_ "switching effective uid is not implemented");
257 if (setreuid(euid, ruid))
260 if (setresuid(euid, ruid, (Uid_t)-1))
263 Perl_croak(aTHX_ "entering effective uid failed");
266 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
267 Perl_croak(aTHX_ "switching effective gid is not implemented");
270 if (setregid(egid, rgid))
273 if (setresgid(egid, rgid, (Gid_t)-1))
276 Perl_croak(aTHX_ "entering effective gid failed");
279 res = access(path, mode);
282 if (setreuid(ruid, euid))
285 if (setresuid(ruid, euid, (Uid_t)-1))
288 Perl_croak(aTHX_ "leaving effective uid failed");
291 if (setregid(rgid, egid))
294 if (setresgid(rgid, egid, (Gid_t)-1))
297 Perl_croak(aTHX_ "leaving effective gid failed");
302 # define PERL_EFF_ACCESS(p,f) (S_emulate_eaccess(aTHX_ (p), (f)))
309 const char * const tmps = POPpconstx;
310 const I32 gimme = GIMME_V;
311 const char *mode = "r";
314 if (PL_op->op_private & OPpOPEN_IN_RAW)
316 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
318 fp = PerlProc_popen(tmps, mode);
320 const char * const type = Perl_PerlIO_context_layers(aTHX_ NULL);
322 PerlIO_apply_layers(aTHX_ fp,mode,type);
324 if (gimme == G_VOID) {
326 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
329 else if (gimme == G_SCALAR) {
332 PL_rs = &PL_sv_undef;
333 sv_setpvs(TARG, ""); /* note that this preserves previous buffer */
334 while (sv_gets(TARG, fp, SvCUR(TARG)) != NULL)
342 SV * const sv = newSV(79);
343 if (sv_gets(sv, fp, 0) == NULL) {
348 if (SvLEN(sv) - SvCUR(sv) > 20) {
349 SvPV_shrink_to_cur(sv);
354 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
355 TAINT; /* "I believe that this is not gratuitous!" */
358 STATUS_NATIVE_CHILD_SET(-1);
359 if (gimme == G_SCALAR)
370 tryAMAGICunTARGET(iter, -1);
372 /* Note that we only ever get here if File::Glob fails to load
373 * without at the same time croaking, for some reason, or if
374 * perl was built with PERL_EXTERNAL_GLOB */
381 * The external globbing program may use things we can't control,
382 * so for security reasons we must assume the worst.
385 taint_proper(PL_no_security, "glob");
389 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
390 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
392 SAVESPTR(PL_rs); /* This is not permanent, either. */
393 PL_rs = newSVpvs_flags("\000", SVs_TEMP);
396 *SvPVX(PL_rs) = '\n';
400 result = do_readline();
408 PL_last_in_gv = cGVOP_gv;
409 return do_readline();
420 do_join(TARG, &PL_sv_no, MARK, SP);
424 else if (SP == MARK) {
432 tmps = SvPV_const(tmpsv, len);
433 if ((!tmps || !len) && PL_errgv) {
434 SV * const error = ERRSV;
435 SvUPGRADE(error, SVt_PV);
436 if (SvPOK(error) && SvCUR(error))
437 sv_catpvs(error, "\t...caught");
439 tmps = SvPV_const(tmpsv, len);
442 tmpsv = newSVpvs_flags("Warning: something's wrong", SVs_TEMP);
444 Perl_warn(aTHX_ "%"SVf, SVfARG(tmpsv));
456 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
458 if (SP - MARK != 1) {
460 do_join(TARG, &PL_sv_no, MARK, SP);
462 tmps = SvPV_const(tmpsv, len);
468 tmps = SvROK(tmpsv) ? (const char *)NULL : SvPV_const(tmpsv, len);
471 SV * const error = ERRSV;
472 SvUPGRADE(error, SVt_PV);
473 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
475 SvSetSV(error,tmpsv);
476 else if (sv_isobject(error)) {
477 HV * const stash = SvSTASH(SvRV(error));
478 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
480 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
481 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
488 call_sv(MUTABLE_SV(GvCV(gv)),
489 G_SCALAR|G_EVAL|G_KEEPERR);
490 sv_setsv(error,*PL_stack_sp--);
496 if (SvPOK(error) && SvCUR(error))
497 sv_catpvs(error, "\t...propagated");
500 tmps = SvPV_const(tmpsv, len);
506 tmpsv = newSVpvs_flags("Died", SVs_TEMP);
508 DIE(aTHX_ "%"SVf, SVfARG(tmpsv));
524 GV * const gv = MUTABLE_GV(*++MARK);
527 DIE(aTHX_ PL_no_usym, "filehandle");
529 if ((io = GvIOp(gv))) {
531 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
533 if (IoDIRP(io) && ckWARN2(WARN_IO, WARN_DEPRECATED))
534 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
535 "Opening dirhandle %s also as a file", GvENAME(gv));
537 mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
539 /* Method's args are same as ours ... */
540 /* ... except handle is replaced by the object */
541 *MARK-- = SvTIED_obj(MUTABLE_SV(io), mg);
545 call_method("OPEN", G_SCALAR);
559 tmps = SvPV_const(sv, len);
560 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, NULL, MARK+1, (SP-MARK));
563 PUSHi( (I32)PL_forkprocess );
564 else if (PL_forkprocess == 0) /* we are a new child */
574 GV * const gv = (MAXARG == 0) ? PL_defoutgv : MUTABLE_GV(POPs);
577 IO * const io = GvIO(gv);
579 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
582 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
585 call_method("CLOSE", G_SCALAR);
593 PUSHs(boolSV(do_close(gv, TRUE)));
606 GV * const wgv = MUTABLE_GV(POPs);
607 GV * const rgv = MUTABLE_GV(POPs);
612 if (!isGV_with_GP(rgv) || !isGV_with_GP(wgv))
613 DIE(aTHX_ PL_no_usym, "filehandle");
618 do_close(rgv, FALSE);
620 do_close(wgv, FALSE);
622 if (PerlProc_pipe(fd) < 0)
625 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
626 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
627 IoOFP(rstio) = IoIFP(rstio);
628 IoIFP(wstio) = IoOFP(wstio);
629 IoTYPE(rstio) = IoTYPE_RDONLY;
630 IoTYPE(wstio) = IoTYPE_WRONLY;
632 if (!IoIFP(rstio) || !IoOFP(wstio)) {
634 PerlIO_close(IoIFP(rstio));
636 PerlLIO_close(fd[0]);
638 PerlIO_close(IoOFP(wstio));
640 PerlLIO_close(fd[1]);
643 #if defined(HAS_FCNTL) && defined(F_SETFD)
644 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
645 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
652 DIE(aTHX_ PL_no_func, "pipe");
666 gv = MUTABLE_GV(POPs);
668 if (gv && (io = GvIO(gv))
669 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
672 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
675 call_method("FILENO", G_SCALAR);
681 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
682 /* Can't do this because people seem to do things like
683 defined(fileno($foo)) to check whether $foo is a valid fh.
684 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
685 report_evil_fh(gv, io, PL_op->op_type);
690 PUSHi(PerlIO_fileno(fp));
703 anum = PerlLIO_umask(022);
704 /* setting it to 022 between the two calls to umask avoids
705 * to have a window where the umask is set to 0 -- meaning
706 * that another thread could create world-writeable files. */
708 (void)PerlLIO_umask(anum);
711 anum = PerlLIO_umask(POPi);
712 TAINT_PROPER("umask");
715 /* Only DIE if trying to restrict permissions on "user" (self).
716 * Otherwise it's harmless and more useful to just return undef
717 * since 'group' and 'other' concepts probably don't exist here. */
718 if (MAXARG >= 1 && (POPi & 0700))
719 DIE(aTHX_ "umask not implemented");
720 XPUSHs(&PL_sv_undef);
739 gv = MUTABLE_GV(POPs);
741 if (gv && (io = GvIO(gv))) {
742 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
745 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
750 call_method("BINMODE", G_SCALAR);
758 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
759 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
760 report_evil_fh(gv, io, PL_op->op_type);
761 SETERRNO(EBADF,RMS_IFI);
768 const char *d = NULL;
771 d = SvPV_const(discp, len);
772 mode = mode_from_discipline(d, len);
773 if (PerlIO_binmode(aTHX_ fp, IoTYPE(io), mode, d)) {
774 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
775 if (!PerlIO_binmode(aTHX_ IoOFP(io), IoTYPE(io), mode, d)) {
796 const I32 markoff = MARK - PL_stack_base;
797 const char *methname;
798 int how = PERL_MAGIC_tied;
802 switch(SvTYPE(varsv)) {
804 methname = "TIEHASH";
805 HvEITER_set(MUTABLE_HV(varsv), 0);
808 methname = "TIEARRAY";
811 if (isGV_with_GP(varsv)) {
812 #ifdef GV_UNIQUE_CHECK
813 if (GvUNIQUE((const GV *)varsv)) {
814 Perl_croak(aTHX_ "Attempt to tie unique GV");
817 methname = "TIEHANDLE";
818 how = PERL_MAGIC_tiedscalar;
819 /* For tied filehandles, we apply tiedscalar magic to the IO
820 slot of the GP rather than the GV itself. AMS 20010812 */
822 GvIOp(varsv) = newIO();
823 varsv = MUTABLE_SV(GvIOp(varsv));
828 methname = "TIESCALAR";
829 how = PERL_MAGIC_tiedscalar;
833 if (sv_isobject(*MARK)) { /* Calls GET magic. */
835 PUSHSTACKi(PERLSI_MAGIC);
837 EXTEND(SP,(I32)items);
841 call_method(methname, G_SCALAR);
844 /* Not clear why we don't call call_method here too.
845 * perhaps to get different error message ?
848 const char *name = SvPV_nomg_const(*MARK, len);
849 stash = gv_stashpvn(name, len, 0);
850 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
851 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
852 methname, SVfARG(SvOK(*MARK) ? *MARK : &PL_sv_no));
855 PUSHSTACKi(PERLSI_MAGIC);
857 EXTEND(SP,(I32)items);
861 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
867 if (sv_isobject(sv)) {
868 sv_unmagic(varsv, how);
869 /* Croak if a self-tie on an aggregate is attempted. */
870 if (varsv == SvRV(sv) &&
871 (SvTYPE(varsv) == SVt_PVAV ||
872 SvTYPE(varsv) == SVt_PVHV))
874 "Self-ties of arrays and hashes are not supported");
875 sv_magic(varsv, (SvRV(sv) == varsv ? NULL : sv), how, NULL, 0);
878 SP = PL_stack_base + markoff;
888 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
889 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
891 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
894 if ((mg = SvTIED_mg(sv, how))) {
895 SV * const obj = SvRV(SvTIED_obj(sv, mg));
897 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
899 if (gv && isGV(gv) && (cv = GvCV(gv))) {
901 XPUSHs(SvTIED_obj(MUTABLE_SV(gv), mg));
902 mXPUSHi(SvREFCNT(obj) - 1);
905 call_sv(MUTABLE_SV(cv), G_VOID);
909 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
910 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
911 "untie attempted while %"UVuf" inner references still exist",
912 (UV)SvREFCNT(obj) - 1 ) ;
916 sv_unmagic(sv, how) ;
926 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
927 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
929 if (isGV_with_GP(sv) && !(sv = MUTABLE_SV(GvIOp(sv))))
932 if ((mg = SvTIED_mg(sv, how))) {
933 SV *osv = SvTIED_obj(sv, mg);
934 if (osv == mg->mg_obj)
935 osv = sv_mortalcopy(osv);
949 HV * const hv = MUTABLE_HV(POPs);
950 SV * const sv = newSVpvs_flags("AnyDBM_File", SVs_TEMP);
951 stash = gv_stashsv(sv, 0);
952 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
954 require_pv("AnyDBM_File.pm");
956 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH")))
957 DIE(aTHX_ "No dbm on this machine");
967 mPUSHu(O_RDWR|O_CREAT);
972 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
975 if (!sv_isobject(TOPs)) {
983 call_sv(MUTABLE_SV(GvCV(gv)), G_SCALAR);
987 if (sv_isobject(TOPs)) {
988 sv_unmagic(MUTABLE_SV(hv), PERL_MAGIC_tied);
989 sv_magic(MUTABLE_SV(hv), TOPs, PERL_MAGIC_tied, NULL, 0);
1006 struct timeval timebuf;
1007 struct timeval *tbuf = &timebuf;
1010 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1015 # if BYTEORDER & 0xf0000
1016 # define ORDERBYTE (0x88888888 - BYTEORDER)
1018 # define ORDERBYTE (0x4444 - BYTEORDER)
1024 for (i = 1; i <= 3; i++) {
1025 SV * const sv = SP[i];
1028 if (SvREADONLY(sv)) {
1030 sv_force_normal_flags(sv, 0);
1031 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1032 DIE(aTHX_ "%s", PL_no_modify);
1035 if (ckWARN(WARN_MISC))
1036 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1037 SvPV_force_nolen(sv); /* force string conversion */
1044 /* little endians can use vecs directly */
1045 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1052 masksize = NFDBITS / NBBY;
1054 masksize = sizeof(long); /* documented int, everyone seems to use long */
1056 Zero(&fd_sets[0], 4, char*);
1059 # if SELECT_MIN_BITS == 1
1060 growsize = sizeof(fd_set);
1062 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1063 # undef SELECT_MIN_BITS
1064 # define SELECT_MIN_BITS __FD_SETSIZE
1066 /* If SELECT_MIN_BITS is greater than one we most probably will want
1067 * to align the sizes with SELECT_MIN_BITS/8 because for example
1068 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1069 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1070 * on (sets/tests/clears bits) is 32 bits. */
1071 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1079 timebuf.tv_sec = (long)value;
1080 value -= (NV)timebuf.tv_sec;
1081 timebuf.tv_usec = (long)(value * 1000000.0);
1086 for (i = 1; i <= 3; i++) {
1088 if (!SvOK(sv) || SvCUR(sv) == 0) {
1095 Sv_Grow(sv, growsize);
1099 while (++j <= growsize) {
1103 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1105 Newx(fd_sets[i], growsize, char);
1106 for (offset = 0; offset < growsize; offset += masksize) {
1107 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1108 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1111 fd_sets[i] = SvPVX(sv);
1115 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1116 /* Can't make just the (void*) conditional because that would be
1117 * cpp #if within cpp macro, and not all compilers like that. */
1118 nfound = PerlSock_select(
1120 (Select_fd_set_t) fd_sets[1],
1121 (Select_fd_set_t) fd_sets[2],
1122 (Select_fd_set_t) fd_sets[3],
1123 (void*) tbuf); /* Workaround for compiler bug. */
1125 nfound = PerlSock_select(
1127 (Select_fd_set_t) fd_sets[1],
1128 (Select_fd_set_t) fd_sets[2],
1129 (Select_fd_set_t) fd_sets[3],
1132 for (i = 1; i <= 3; i++) {
1135 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1137 for (offset = 0; offset < growsize; offset += masksize) {
1138 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1139 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1141 Safefree(fd_sets[i]);
1148 if (GIMME == G_ARRAY && tbuf) {
1149 value = (NV)(timebuf.tv_sec) +
1150 (NV)(timebuf.tv_usec) / 1000000.0;
1155 DIE(aTHX_ "select not implemented");
1160 =for apidoc setdefout
1162 Sets PL_defoutgv, the default file handle for output, to the passed in
1163 typeglob. As PL_defoutgv "owns" a reference on its typeglob, the reference
1164 count of the passed in typeglob is increased by one, and the reference count
1165 of the typeglob that PL_defoutgv points to is decreased by one.
1171 Perl_setdefout(pTHX_ GV *gv)
1174 SvREFCNT_inc_simple_void(gv);
1176 SvREFCNT_dec(PL_defoutgv);
1184 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1185 GV * egv = GvEGV(PL_defoutgv);
1191 XPUSHs(&PL_sv_undef);
1193 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1194 if (gvp && *gvp == egv) {
1195 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1199 mXPUSHs(newRV(MUTABLE_SV(egv)));
1204 if (!GvIO(newdefout))
1205 gv_IOadd(newdefout);
1206 setdefout(newdefout);
1216 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1218 if (gv && (io = GvIO(gv))) {
1219 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1221 const I32 gimme = GIMME_V;
1223 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
1226 call_method("GETC", gimme);
1229 if (gimme == G_SCALAR)
1230 SvSetMagicSV_nosteal(TARG, TOPs);
1234 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1235 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1236 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1237 report_evil_fh(gv, io, PL_op->op_type);
1238 SETERRNO(EBADF,RMS_IFI);
1242 sv_setpvs(TARG, " ");
1243 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1244 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1245 /* Find out how many bytes the char needs */
1246 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1249 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1250 SvCUR_set(TARG,1+len);
1259 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1262 register PERL_CONTEXT *cx;
1263 const I32 gimme = GIMME_V;
1265 PERL_ARGS_ASSERT_DOFORM;
1270 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1271 PUSHFORMAT(cx, retop);
1273 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1275 setdefout(gv); /* locally select filehandle so $% et al work */
1292 gv = MUTABLE_GV(POPs);
1307 goto not_a_format_reference;
1312 tmpsv = sv_newmortal();
1313 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1314 name = SvPV_nolen_const(tmpsv);
1316 DIE(aTHX_ "Undefined format \"%s\" called", name);
1318 not_a_format_reference:
1319 DIE(aTHX_ "Not a format reference");
1322 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1324 IoFLAGS(io) &= ~IOf_DIDTOP;
1325 return doform(cv,gv,PL_op->op_next);
1331 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1332 register IO * const io = GvIOp(gv);
1337 register PERL_CONTEXT *cx;
1339 if (!io || !(ofp = IoOFP(io)))
1342 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1343 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1345 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1346 PL_formtarget != PL_toptarget)
1350 if (!IoTOP_GV(io)) {
1353 if (!IoTOP_NAME(io)) {
1355 if (!IoFMT_NAME(io))
1356 IoFMT_NAME(io) = savepv(GvNAME(gv));
1357 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1358 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1359 if ((topgv && GvFORM(topgv)) ||
1360 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1361 IoTOP_NAME(io) = savesvpv(topname);
1363 IoTOP_NAME(io) = savepvs("top");
1365 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1366 if (!topgv || !GvFORM(topgv)) {
1367 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1370 IoTOP_GV(io) = topgv;
1372 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1373 I32 lines = IoLINES_LEFT(io);
1374 const char *s = SvPVX_const(PL_formtarget);
1375 if (lines <= 0) /* Yow, header didn't even fit!!! */
1377 while (lines-- > 0) {
1378 s = strchr(s, '\n');
1384 const STRLEN save = SvCUR(PL_formtarget);
1385 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1386 do_print(PL_formtarget, ofp);
1387 SvCUR_set(PL_formtarget, save);
1388 sv_chop(PL_formtarget, s);
1389 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1392 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1393 do_print(PL_formfeed, ofp);
1394 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1396 PL_formtarget = PL_toptarget;
1397 IoFLAGS(io) |= IOf_DIDTOP;
1400 DIE(aTHX_ "bad top format reference");
1403 SV * const sv = sv_newmortal();
1405 gv_efullname4(sv, fgv, NULL, FALSE);
1406 name = SvPV_nolen_const(sv);
1408 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1410 DIE(aTHX_ "Undefined top format called");
1412 if (cv && CvCLONE(cv))
1413 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1414 return doform(cv, gv, PL_op);
1418 POPBLOCK(cx,PL_curpm);
1424 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1426 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1427 else if (ckWARN(WARN_CLOSED))
1428 report_evil_fh(gv, io, PL_op->op_type);
1433 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1434 if (ckWARN(WARN_IO))
1435 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1437 if (!do_print(PL_formtarget, fp))
1440 FmLINES(PL_formtarget) = 0;
1441 SvCUR_set(PL_formtarget, 0);
1442 *SvEND(PL_formtarget) = '\0';
1443 if (IoFLAGS(io) & IOf_FLUSH)
1444 (void)PerlIO_flush(fp);
1449 PL_formtarget = PL_bodytarget;
1451 PERL_UNUSED_VAR(newsp);
1452 PERL_UNUSED_VAR(gimme);
1453 return cx->blk_sub.retop;
1458 dVAR; dSP; dMARK; dORIGMARK;
1464 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1466 if (gv && (io = GvIO(gv))) {
1467 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1469 if (MARK == ORIGMARK) {
1472 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1476 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1479 call_method("PRINTF", G_SCALAR);
1482 MARK = ORIGMARK + 1;
1490 if (!(io = GvIO(gv))) {
1491 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1492 report_evil_fh(gv, io, PL_op->op_type);
1493 SETERRNO(EBADF,RMS_IFI);
1496 else if (!(fp = IoOFP(io))) {
1497 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1499 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1500 else if (ckWARN(WARN_CLOSED))
1501 report_evil_fh(gv, io, PL_op->op_type);
1503 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1507 if (SvTAINTED(MARK[1]))
1508 TAINT_PROPER("printf");
1509 do_sprintf(sv, SP - MARK, MARK + 1);
1510 if (!do_print(sv, fp))
1513 if (IoFLAGS(io) & IOf_FLUSH)
1514 if (PerlIO_flush(fp) == EOF)
1525 PUSHs(&PL_sv_undef);
1533 const int perm = (MAXARG > 3) ? POPi : 0666;
1534 const int mode = POPi;
1535 SV * const sv = POPs;
1536 GV * const gv = MUTABLE_GV(POPs);
1539 /* Need TIEHANDLE method ? */
1540 const char * const tmps = SvPV_const(sv, len);
1541 /* FIXME? do_open should do const */
1542 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1543 IoLINES(GvIOp(gv)) = 0;
1547 PUSHs(&PL_sv_undef);
1554 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1560 Sock_size_t bufsize;
1568 bool charstart = FALSE;
1569 STRLEN charskip = 0;
1572 GV * const gv = MUTABLE_GV(*++MARK);
1573 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1574 && gv && (io = GvIO(gv)) )
1576 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1580 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1582 call_method("READ", G_SCALAR);
1596 sv_setpvs(bufsv, "");
1597 length = SvIVx(*++MARK);
1600 offset = SvIVx(*++MARK);
1604 if (!io || !IoIFP(io)) {
1605 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1606 report_evil_fh(gv, io, PL_op->op_type);
1607 SETERRNO(EBADF,RMS_IFI);
1610 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1611 buffer = SvPVutf8_force(bufsv, blen);
1612 /* UTF-8 may not have been set if they are all low bytes */
1617 buffer = SvPV_force(bufsv, blen);
1618 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1621 DIE(aTHX_ "Negative length");
1629 if (PL_op->op_type == OP_RECV) {
1630 char namebuf[MAXPATHLEN];
1631 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1632 bufsize = sizeof (struct sockaddr_in);
1634 bufsize = sizeof namebuf;
1636 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1640 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1641 /* 'offset' means 'flags' here */
1642 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1643 (struct sockaddr *)namebuf, &bufsize);
1647 /* Bogus return without padding */
1648 bufsize = sizeof (struct sockaddr_in);
1650 SvCUR_set(bufsv, count);
1651 *SvEND(bufsv) = '\0';
1652 (void)SvPOK_only(bufsv);
1656 /* This should not be marked tainted if the fp is marked clean */
1657 if (!(IoFLAGS(io) & IOf_UNTAINT))
1658 SvTAINTED_on(bufsv);
1660 sv_setpvn(TARG, namebuf, bufsize);
1665 if (PL_op->op_type == OP_RECV)
1666 DIE(aTHX_ PL_no_sock_func, "recv");
1668 if (DO_UTF8(bufsv)) {
1669 /* offset adjust in characters not bytes */
1670 blen = sv_len_utf8(bufsv);
1673 if (-offset > (int)blen)
1674 DIE(aTHX_ "Offset outside string");
1677 if (DO_UTF8(bufsv)) {
1678 /* convert offset-as-chars to offset-as-bytes */
1679 if (offset >= (int)blen)
1680 offset += SvCUR(bufsv) - blen;
1682 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1685 bufsize = SvCUR(bufsv);
1686 /* Allocating length + offset + 1 isn't perfect in the case of reading
1687 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1689 (should be 2 * length + offset + 1, or possibly something longer if
1690 PL_encoding is true) */
1691 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1692 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1693 Zero(buffer+bufsize, offset-bufsize, char);
1695 buffer = buffer + offset;
1697 read_target = bufsv;
1699 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1700 concatenate it to the current buffer. */
1702 /* Truncate the existing buffer to the start of where we will be
1704 SvCUR_set(bufsv, offset);
1706 read_target = sv_newmortal();
1707 SvUPGRADE(read_target, SVt_PV);
1708 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1711 if (PL_op->op_type == OP_SYSREAD) {
1712 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1713 if (IoTYPE(io) == IoTYPE_SOCKET) {
1714 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1720 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1725 #ifdef HAS_SOCKET__bad_code_maybe
1726 if (IoTYPE(io) == IoTYPE_SOCKET) {
1727 char namebuf[MAXPATHLEN];
1728 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1729 bufsize = sizeof (struct sockaddr_in);
1731 bufsize = sizeof namebuf;
1733 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1734 (struct sockaddr *)namebuf, &bufsize);
1739 count = PerlIO_read(IoIFP(io), buffer, length);
1740 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1741 if (count == 0 && PerlIO_error(IoIFP(io)))
1745 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1746 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1749 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1750 *SvEND(read_target) = '\0';
1751 (void)SvPOK_only(read_target);
1752 if (fp_utf8 && !IN_BYTES) {
1753 /* Look at utf8 we got back and count the characters */
1754 const char *bend = buffer + count;
1755 while (buffer < bend) {
1757 skip = UTF8SKIP(buffer);
1760 if (buffer - charskip + skip > bend) {
1761 /* partial character - try for rest of it */
1762 length = skip - (bend-buffer);
1763 offset = bend - SvPVX_const(bufsv);
1775 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1776 provided amount read (count) was what was requested (length)
1778 if (got < wanted && count == length) {
1779 length = wanted - got;
1780 offset = bend - SvPVX_const(bufsv);
1783 /* return value is character count */
1787 else if (buffer_utf8) {
1788 /* Let svcatsv upgrade the bytes we read in to utf8.
1789 The buffer is a mortal so will be freed soon. */
1790 sv_catsv_nomg(bufsv, read_target);
1793 /* This should not be marked tainted if the fp is marked clean */
1794 if (!(IoFLAGS(io) & IOf_UNTAINT))
1795 SvTAINTED_on(bufsv);
1807 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1813 STRLEN orig_blen_bytes;
1814 const int op_type = PL_op->op_type;
1818 GV *const gv = MUTABLE_GV(*++MARK);
1819 if (PL_op->op_type == OP_SYSWRITE
1820 && gv && (io = GvIO(gv))) {
1821 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1825 if (MARK == SP - 1) {
1827 sv = sv_2mortal(newSViv(sv_len(*SP)));
1833 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1835 call_method("WRITE", G_SCALAR);
1851 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1853 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1854 if (io && IoIFP(io))
1855 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1857 report_evil_fh(gv, io, PL_op->op_type);
1859 SETERRNO(EBADF,RMS_IFI);
1863 /* Do this first to trigger any overloading. */
1864 buffer = SvPV_const(bufsv, blen);
1865 orig_blen_bytes = blen;
1866 doing_utf8 = DO_UTF8(bufsv);
1868 if (PerlIO_isutf8(IoIFP(io))) {
1869 if (!SvUTF8(bufsv)) {
1870 /* We don't modify the original scalar. */
1871 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1872 buffer = (char *) tmpbuf;
1876 else if (doing_utf8) {
1877 STRLEN tmplen = blen;
1878 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1881 buffer = (char *) tmpbuf;
1885 assert((char *)result == buffer);
1886 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1890 if (op_type == OP_SYSWRITE) {
1891 Size_t length = 0; /* This length is in characters. */
1897 /* The SV is bytes, and we've had to upgrade it. */
1898 blen_chars = orig_blen_bytes;
1900 /* The SV really is UTF-8. */
1901 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1902 /* Don't call sv_len_utf8 again because it will call magic
1903 or overloading a second time, and we might get back a
1904 different result. */
1905 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1907 /* It's safe, and it may well be cached. */
1908 blen_chars = sv_len_utf8(bufsv);
1916 length = blen_chars;
1918 #if Size_t_size > IVSIZE
1919 length = (Size_t)SvNVx(*++MARK);
1921 length = (Size_t)SvIVx(*++MARK);
1923 if ((SSize_t)length < 0) {
1925 DIE(aTHX_ "Negative length");
1930 offset = SvIVx(*++MARK);
1932 if (-offset > (IV)blen_chars) {
1934 DIE(aTHX_ "Offset outside string");
1936 offset += blen_chars;
1937 } else if (offset >= (IV)blen_chars && blen_chars > 0) {
1939 DIE(aTHX_ "Offset outside string");
1943 if (length > blen_chars - offset)
1944 length = blen_chars - offset;
1946 /* Here we convert length from characters to bytes. */
1947 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1948 /* Either we had to convert the SV, or the SV is magical, or
1949 the SV has overloading, in which case we can't or mustn't
1950 or mustn't call it again. */
1952 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1953 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1955 /* It's a real UTF-8 SV, and it's not going to change under
1956 us. Take advantage of any cache. */
1958 I32 len_I32 = length;
1960 /* Convert the start and end character positions to bytes.
1961 Remember that the second argument to sv_pos_u2b is relative
1963 sv_pos_u2b(bufsv, &start, &len_I32);
1970 buffer = buffer+offset;
1972 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1973 if (IoTYPE(io) == IoTYPE_SOCKET) {
1974 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1980 /* See the note at doio.c:do_print about filesize limits. --jhi */
1981 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1987 const int flags = SvIVx(*++MARK);
1990 char * const sockbuf = SvPVx(*++MARK, mlen);
1991 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1992 flags, (struct sockaddr *)sockbuf, mlen);
1996 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
2001 DIE(aTHX_ PL_no_sock_func, "send");
2008 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2011 #if Size_t_size > IVSIZE
2032 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2033 else if (PL_op->op_flags & OPf_SPECIAL)
2034 gv = PL_last_in_gv = GvEGV(PL_argvgv); /* eof() - ARGV magic */
2036 gv = PL_last_in_gv; /* eof */
2041 if ((io = GvIO(gv)) && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2043 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2045 * in Perl 5.12 and later, the additional paramter is a bitmask:
2048 * 2 = eof() <- ARGV magic
2051 mPUSHi(1); /* 1 = eof(FH) - simple, explicit FH */
2052 else if (PL_op->op_flags & OPf_SPECIAL)
2053 mPUSHi(2); /* 2 = eof() - ARGV magic */
2055 mPUSHi(0); /* 0 = eof - simple, implicit FH */
2058 call_method("EOF", G_SCALAR);
2064 if (!MAXARG && (PL_op->op_flags & OPf_SPECIAL)) { /* eof() */
2065 if (io && !IoIFP(io)) {
2066 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2068 IoFLAGS(io) &= ~IOf_START;
2069 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2071 sv_setpvs(GvSV(gv), "-");
2073 GvSV(gv) = newSVpvs("-");
2074 SvSETMAGIC(GvSV(gv));
2076 else if (!nextargv(gv))
2081 PUSHs(boolSV(do_eof(gv)));
2092 PL_last_in_gv = MUTABLE_GV(POPs);
2095 if (gv && (io = GvIO(gv))) {
2096 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2099 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2102 call_method("TELL", G_SCALAR);
2109 #if LSEEKSIZE > IVSIZE
2110 PUSHn( do_tell(gv) );
2112 PUSHi( do_tell(gv) );
2120 const int whence = POPi;
2121 #if LSEEKSIZE > IVSIZE
2122 const Off_t offset = (Off_t)SvNVx(POPs);
2124 const Off_t offset = (Off_t)SvIVx(POPs);
2127 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2130 if (gv && (io = GvIO(gv))) {
2131 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2134 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2135 #if LSEEKSIZE > IVSIZE
2136 mXPUSHn((NV) offset);
2143 call_method("SEEK", G_SCALAR);
2150 if (PL_op->op_type == OP_SEEK)
2151 PUSHs(boolSV(do_seek(gv, offset, whence)));
2153 const Off_t sought = do_sysseek(gv, offset, whence);
2155 PUSHs(&PL_sv_undef);
2157 SV* const sv = sought ?
2158 #if LSEEKSIZE > IVSIZE
2163 : newSVpvn(zero_but_true, ZBTLEN);
2174 /* There seems to be no consensus on the length type of truncate()
2175 * and ftruncate(), both off_t and size_t have supporters. In
2176 * general one would think that when using large files, off_t is
2177 * at least as wide as size_t, so using an off_t should be okay. */
2178 /* XXX Configure probe for the length type of *truncate() needed XXX */
2181 #if Off_t_size > IVSIZE
2186 /* Checking for length < 0 is problematic as the type might or
2187 * might not be signed: if it is not, clever compilers will moan. */
2188 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2195 if (PL_op->op_flags & OPf_SPECIAL) {
2196 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2205 TAINT_PROPER("truncate");
2206 if (!(fp = IoIFP(io))) {
2212 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2214 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2221 SV * const sv = POPs;
2224 if (isGV_with_GP(sv)) {
2225 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2226 goto do_ftruncate_gv;
2228 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2229 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2230 goto do_ftruncate_gv;
2232 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2233 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2234 goto do_ftruncate_io;
2237 name = SvPV_nolen_const(sv);
2238 TAINT_PROPER("truncate");
2240 if (truncate(name, len) < 0)
2244 const int tmpfd = PerlLIO_open(name, O_RDWR);
2249 if (my_chsize(tmpfd, len) < 0)
2251 PerlLIO_close(tmpfd);
2260 SETERRNO(EBADF,RMS_IFI);
2268 SV * const argsv = POPs;
2269 const unsigned int func = POPu;
2270 const int optype = PL_op->op_type;
2271 GV * const gv = MUTABLE_GV(POPs);
2272 IO * const io = gv ? GvIOn(gv) : NULL;
2276 if (!io || !argsv || !IoIFP(io)) {
2277 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2278 report_evil_fh(gv, io, PL_op->op_type);
2279 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2283 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2286 s = SvPV_force(argsv, len);
2287 need = IOCPARM_LEN(func);
2289 s = Sv_Grow(argsv, need + 1);
2290 SvCUR_set(argsv, need);
2293 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2296 retval = SvIV(argsv);
2297 s = INT2PTR(char*,retval); /* ouch */
2300 TAINT_PROPER(PL_op_desc[optype]);
2302 if (optype == OP_IOCTL)
2304 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2306 DIE(aTHX_ "ioctl is not implemented");
2310 DIE(aTHX_ "fcntl is not implemented");
2312 #if defined(OS2) && defined(__EMX__)
2313 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2315 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2319 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2321 if (s[SvCUR(argsv)] != 17)
2322 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2324 s[SvCUR(argsv)] = 0; /* put our null back */
2325 SvSETMAGIC(argsv); /* Assume it has changed */
2334 PUSHp(zero_but_true, ZBTLEN);
2347 const int argtype = POPi;
2348 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2350 if (gv && (io = GvIO(gv)))
2356 /* XXX Looks to me like io is always NULL at this point */
2358 (void)PerlIO_flush(fp);
2359 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2362 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2363 report_evil_fh(gv, io, PL_op->op_type);
2365 SETERRNO(EBADF,RMS_IFI);
2370 DIE(aTHX_ PL_no_func, "flock()");
2380 const int protocol = POPi;
2381 const int type = POPi;
2382 const int domain = POPi;
2383 GV * const gv = MUTABLE_GV(POPs);
2384 register IO * const io = gv ? GvIOn(gv) : NULL;
2388 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2389 report_evil_fh(gv, io, PL_op->op_type);
2390 if (io && IoIFP(io))
2391 do_close(gv, FALSE);
2392 SETERRNO(EBADF,LIB_INVARG);
2397 do_close(gv, FALSE);
2399 TAINT_PROPER("socket");
2400 fd = PerlSock_socket(domain, type, protocol);
2403 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2404 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2405 IoTYPE(io) = IoTYPE_SOCKET;
2406 if (!IoIFP(io) || !IoOFP(io)) {
2407 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2408 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2409 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2412 #if defined(HAS_FCNTL) && defined(F_SETFD)
2413 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2417 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2422 DIE(aTHX_ PL_no_sock_func, "socket");
2428 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2430 const int protocol = POPi;
2431 const int type = POPi;
2432 const int domain = POPi;
2433 GV * const gv2 = MUTABLE_GV(POPs);
2434 GV * const gv1 = MUTABLE_GV(POPs);
2435 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2436 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2439 if (!gv1 || !gv2 || !io1 || !io2) {
2440 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2442 report_evil_fh(gv1, io1, PL_op->op_type);
2444 report_evil_fh(gv1, io2, PL_op->op_type);
2446 if (io1 && IoIFP(io1))
2447 do_close(gv1, FALSE);
2448 if (io2 && IoIFP(io2))
2449 do_close(gv2, FALSE);
2454 do_close(gv1, FALSE);
2456 do_close(gv2, FALSE);
2458 TAINT_PROPER("socketpair");
2459 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2461 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2462 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2463 IoTYPE(io1) = IoTYPE_SOCKET;
2464 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2465 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2466 IoTYPE(io2) = IoTYPE_SOCKET;
2467 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2468 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2469 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2470 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2471 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2472 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2473 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2476 #if defined(HAS_FCNTL) && defined(F_SETFD)
2477 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2478 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2483 DIE(aTHX_ PL_no_sock_func, "socketpair");
2491 SV * const addrsv = POPs;
2492 /* OK, so on what platform does bind modify addr? */
2494 GV * const gv = MUTABLE_GV(POPs);
2495 register IO * const io = GvIOn(gv);
2498 if (!io || !IoIFP(io))
2501 addr = SvPV_const(addrsv, len);
2502 TAINT_PROPER("bind");
2503 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2509 if (ckWARN(WARN_CLOSED))
2510 report_evil_fh(gv, io, PL_op->op_type);
2511 SETERRNO(EBADF,SS_IVCHAN);
2514 DIE(aTHX_ PL_no_sock_func, "bind");
2522 SV * const addrsv = POPs;
2523 GV * const gv = MUTABLE_GV(POPs);
2524 register IO * const io = GvIOn(gv);
2528 if (!io || !IoIFP(io))
2531 addr = SvPV_const(addrsv, len);
2532 TAINT_PROPER("connect");
2533 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2539 if (ckWARN(WARN_CLOSED))
2540 report_evil_fh(gv, io, PL_op->op_type);
2541 SETERRNO(EBADF,SS_IVCHAN);
2544 DIE(aTHX_ PL_no_sock_func, "connect");
2552 const int backlog = POPi;
2553 GV * const gv = MUTABLE_GV(POPs);
2554 register IO * const io = gv ? GvIOn(gv) : NULL;
2556 if (!gv || !io || !IoIFP(io))
2559 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2565 if (ckWARN(WARN_CLOSED))
2566 report_evil_fh(gv, io, PL_op->op_type);
2567 SETERRNO(EBADF,SS_IVCHAN);
2570 DIE(aTHX_ PL_no_sock_func, "listen");
2580 char namebuf[MAXPATHLEN];
2581 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2582 Sock_size_t len = sizeof (struct sockaddr_in);
2584 Sock_size_t len = sizeof namebuf;
2586 GV * const ggv = MUTABLE_GV(POPs);
2587 GV * const ngv = MUTABLE_GV(POPs);
2596 if (!gstio || !IoIFP(gstio))
2600 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2603 /* Some platforms indicate zero length when an AF_UNIX client is
2604 * not bound. Simulate a non-zero-length sockaddr structure in
2606 namebuf[0] = 0; /* sun_len */
2607 namebuf[1] = AF_UNIX; /* sun_family */
2615 do_close(ngv, FALSE);
2616 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2617 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2618 IoTYPE(nstio) = IoTYPE_SOCKET;
2619 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2620 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2621 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2622 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2625 #if defined(HAS_FCNTL) && defined(F_SETFD)
2626 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2630 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2631 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2633 #ifdef __SCO_VERSION__
2634 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2637 PUSHp(namebuf, len);
2641 if (ckWARN(WARN_CLOSED))
2642 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2643 SETERRNO(EBADF,SS_IVCHAN);
2649 DIE(aTHX_ PL_no_sock_func, "accept");
2657 const int how = POPi;
2658 GV * const gv = MUTABLE_GV(POPs);
2659 register IO * const io = GvIOn(gv);
2661 if (!io || !IoIFP(io))
2664 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2668 if (ckWARN(WARN_CLOSED))
2669 report_evil_fh(gv, io, PL_op->op_type);
2670 SETERRNO(EBADF,SS_IVCHAN);
2673 DIE(aTHX_ PL_no_sock_func, "shutdown");
2681 const int optype = PL_op->op_type;
2682 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2683 const unsigned int optname = (unsigned int) POPi;
2684 const unsigned int lvl = (unsigned int) POPi;
2685 GV * const gv = MUTABLE_GV(POPs);
2686 register IO * const io = GvIOn(gv);
2690 if (!io || !IoIFP(io))
2693 fd = PerlIO_fileno(IoIFP(io));
2697 (void)SvPOK_only(sv);
2701 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2708 #if defined(__SYMBIAN32__)
2709 # define SETSOCKOPT_OPTION_VALUE_T void *
2711 # define SETSOCKOPT_OPTION_VALUE_T const char *
2713 /* XXX TODO: We need to have a proper type (a Configure probe,
2714 * etc.) for what the C headers think of the third argument of
2715 * setsockopt(), the option_value read-only buffer: is it
2716 * a "char *", or a "void *", const or not. Some compilers
2717 * don't take kindly to e.g. assuming that "char *" implicitly
2718 * promotes to a "void *", or to explicitly promoting/demoting
2719 * consts to non/vice versa. The "const void *" is the SUS
2720 * definition, but that does not fly everywhere for the above
2722 SETSOCKOPT_OPTION_VALUE_T buf;
2726 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2730 aint = (int)SvIV(sv);
2731 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2734 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2743 if (ckWARN(WARN_CLOSED))
2744 report_evil_fh(gv, io, optype);
2745 SETERRNO(EBADF,SS_IVCHAN);
2750 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2758 const int optype = PL_op->op_type;
2759 GV * const gv = MUTABLE_GV(POPs);
2760 register IO * const io = GvIOn(gv);
2765 if (!io || !IoIFP(io))
2768 sv = sv_2mortal(newSV(257));
2769 (void)SvPOK_only(sv);
2773 fd = PerlIO_fileno(IoIFP(io));
2775 case OP_GETSOCKNAME:
2776 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2779 case OP_GETPEERNAME:
2780 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2782 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2784 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";
2785 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2786 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2787 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2788 sizeof(u_short) + sizeof(struct in_addr))) {
2795 #ifdef BOGUS_GETNAME_RETURN
2796 /* Interactive Unix, getpeername() and getsockname()
2797 does not return valid namelen */
2798 if (len == BOGUS_GETNAME_RETURN)
2799 len = sizeof(struct sockaddr);
2807 if (ckWARN(WARN_CLOSED))
2808 report_evil_fh(gv, io, optype);
2809 SETERRNO(EBADF,SS_IVCHAN);
2814 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2829 if (PL_op->op_flags & OPf_REF) {
2831 if (PL_op->op_type == OP_LSTAT) {
2832 if (gv != PL_defgv) {
2833 do_fstat_warning_check:
2834 if (ckWARN(WARN_IO))
2835 Perl_warner(aTHX_ packWARN(WARN_IO),
2836 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2837 } else if (PL_laststype != OP_LSTAT)
2838 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2842 if (gv != PL_defgv) {
2843 PL_laststype = OP_STAT;
2845 sv_setpvs(PL_statname, "");
2852 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2853 } else if (IoDIRP(io)) {
2855 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2857 PL_laststatval = -1;
2863 if (PL_laststatval < 0) {
2864 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2865 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2870 SV* const sv = POPs;
2871 if (isGV_with_GP(sv)) {
2872 gv = MUTABLE_GV(sv);
2874 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2875 gv = MUTABLE_GV(SvRV(sv));
2876 if (PL_op->op_type == OP_LSTAT)
2877 goto do_fstat_warning_check;
2879 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2880 io = MUTABLE_IO(SvRV(sv));
2881 if (PL_op->op_type == OP_LSTAT)
2882 goto do_fstat_warning_check;
2883 goto do_fstat_have_io;
2886 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2888 PL_laststype = PL_op->op_type;
2889 if (PL_op->op_type == OP_LSTAT)
2890 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2892 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2893 if (PL_laststatval < 0) {
2894 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2895 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2901 if (gimme != G_ARRAY) {
2902 if (gimme != G_VOID)
2903 XPUSHs(boolSV(max));
2909 mPUSHi(PL_statcache.st_dev);
2910 mPUSHi(PL_statcache.st_ino);
2911 mPUSHu(PL_statcache.st_mode);
2912 mPUSHu(PL_statcache.st_nlink);
2913 #if Uid_t_size > IVSIZE
2914 mPUSHn(PL_statcache.st_uid);
2916 # if Uid_t_sign <= 0
2917 mPUSHi(PL_statcache.st_uid);
2919 mPUSHu(PL_statcache.st_uid);
2922 #if Gid_t_size > IVSIZE
2923 mPUSHn(PL_statcache.st_gid);
2925 # if Gid_t_sign <= 0
2926 mPUSHi(PL_statcache.st_gid);
2928 mPUSHu(PL_statcache.st_gid);
2931 #ifdef USE_STAT_RDEV
2932 mPUSHi(PL_statcache.st_rdev);
2934 PUSHs(newSVpvs_flags("", SVs_TEMP));
2936 #if Off_t_size > IVSIZE
2937 mPUSHn(PL_statcache.st_size);
2939 mPUSHi(PL_statcache.st_size);
2942 mPUSHn(PL_statcache.st_atime);
2943 mPUSHn(PL_statcache.st_mtime);
2944 mPUSHn(PL_statcache.st_ctime);
2946 mPUSHi(PL_statcache.st_atime);
2947 mPUSHi(PL_statcache.st_mtime);
2948 mPUSHi(PL_statcache.st_ctime);
2950 #ifdef USE_STAT_BLOCKS
2951 mPUSHu(PL_statcache.st_blksize);
2952 mPUSHu(PL_statcache.st_blocks);
2954 PUSHs(newSVpvs_flags("", SVs_TEMP));
2955 PUSHs(newSVpvs_flags("", SVs_TEMP));
2961 /* This macro is used by the stacked filetest operators :
2962 * if the previous filetest failed, short-circuit and pass its value.
2963 * Else, discard it from the stack and continue. --rgs
2965 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2966 if (!SvTRUE(TOPs)) { RETURN; } \
2967 else { (void)POPs; PUTBACK; } \
2974 /* Not const, because things tweak this below. Not bool, because there's
2975 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2976 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2977 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2978 /* Giving some sort of initial value silences compilers. */
2980 int access_mode = R_OK;
2982 int access_mode = 0;
2985 /* access_mode is never used, but leaving use_access in makes the
2986 conditional compiling below much clearer. */
2989 int stat_mode = S_IRUSR;
2991 bool effective = FALSE;
2994 STACKED_FTEST_CHECK;
2996 switch (PL_op->op_type) {
2998 #if !(defined(HAS_ACCESS) && defined(R_OK))
3004 #if defined(HAS_ACCESS) && defined(W_OK)
3009 stat_mode = S_IWUSR;
3013 #if defined(HAS_ACCESS) && defined(X_OK)
3018 stat_mode = S_IXUSR;
3022 #ifdef PERL_EFF_ACCESS
3025 stat_mode = S_IWUSR;
3029 #ifndef PERL_EFF_ACCESS
3036 #ifdef PERL_EFF_ACCESS
3041 stat_mode = S_IXUSR;
3047 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3048 const char *name = POPpx;
3050 # ifdef PERL_EFF_ACCESS
3051 result = PERL_EFF_ACCESS(name, access_mode);
3053 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3059 result = access(name, access_mode);
3061 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3076 if (cando(stat_mode, effective, &PL_statcache))
3085 const int op_type = PL_op->op_type;
3087 STACKED_FTEST_CHECK;
3092 if (op_type == OP_FTIS)
3095 /* You can't dTARGET inside OP_FTIS, because you'll get
3096 "panic: pad_sv po" - the op is not flagged to have a target. */
3100 #if Off_t_size > IVSIZE
3101 PUSHn(PL_statcache.st_size);
3103 PUSHi(PL_statcache.st_size);
3107 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3110 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3113 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3126 /* I believe that all these three are likely to be defined on most every
3127 system these days. */
3129 if(PL_op->op_type == OP_FTSUID)
3133 if(PL_op->op_type == OP_FTSGID)
3137 if(PL_op->op_type == OP_FTSVTX)
3141 STACKED_FTEST_CHECK;
3146 switch (PL_op->op_type) {
3148 if (PL_statcache.st_uid == PL_uid)
3152 if (PL_statcache.st_uid == PL_euid)
3156 if (PL_statcache.st_size == 0)
3160 if (S_ISSOCK(PL_statcache.st_mode))
3164 if (S_ISCHR(PL_statcache.st_mode))
3168 if (S_ISBLK(PL_statcache.st_mode))
3172 if (S_ISREG(PL_statcache.st_mode))
3176 if (S_ISDIR(PL_statcache.st_mode))
3180 if (S_ISFIFO(PL_statcache.st_mode))
3185 if (PL_statcache.st_mode & S_ISUID)
3191 if (PL_statcache.st_mode & S_ISGID)
3197 if (PL_statcache.st_mode & S_ISVTX)
3208 I32 result = my_lstat();
3212 if (S_ISLNK(PL_statcache.st_mode))
3225 STACKED_FTEST_CHECK;
3227 if (PL_op->op_flags & OPf_REF)
3229 else if (isGV(TOPs))
3230 gv = MUTABLE_GV(POPs);
3231 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3232 gv = MUTABLE_GV(SvRV(POPs));
3234 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3236 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3237 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3238 else if (tmpsv && SvOK(tmpsv)) {
3239 const char *tmps = SvPV_nolen_const(tmpsv);
3247 if (PerlLIO_isatty(fd))
3252 #if defined(atarist) /* this will work with atariST. Configure will
3253 make guesses for other systems. */
3254 # define FILE_base(f) ((f)->_base)
3255 # define FILE_ptr(f) ((f)->_ptr)
3256 # define FILE_cnt(f) ((f)->_cnt)
3257 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3268 register STDCHAR *s;
3274 STACKED_FTEST_CHECK;
3276 if (PL_op->op_flags & OPf_REF)
3278 else if (isGV(TOPs))
3279 gv = MUTABLE_GV(POPs);
3280 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3281 gv = MUTABLE_GV(SvRV(POPs));
3287 if (gv == PL_defgv) {
3289 io = GvIO(PL_statgv);
3292 goto really_filename;
3297 PL_laststatval = -1;
3298 sv_setpvs(PL_statname, "");
3299 io = GvIO(PL_statgv);
3301 if (io && IoIFP(io)) {
3302 if (! PerlIO_has_base(IoIFP(io)))
3303 DIE(aTHX_ "-T and -B not implemented on filehandles");
3304 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3305 if (PL_laststatval < 0)
3307 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3308 if (PL_op->op_type == OP_FTTEXT)
3313 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3314 i = PerlIO_getc(IoIFP(io));
3316 (void)PerlIO_ungetc(IoIFP(io),i);
3318 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3320 len = PerlIO_get_bufsiz(IoIFP(io));
3321 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3322 /* sfio can have large buffers - limit to 512 */
3327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3329 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3331 SETERRNO(EBADF,RMS_IFI);
3339 PL_laststype = OP_STAT;
3340 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3341 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3342 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3344 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3347 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3348 if (PL_laststatval < 0) {
3349 (void)PerlIO_close(fp);
3352 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3353 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3354 (void)PerlIO_close(fp);
3356 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3357 RETPUSHNO; /* special case NFS directories */
3358 RETPUSHYES; /* null file is anything */
3363 /* now scan s to look for textiness */
3364 /* XXX ASCII dependent code */
3366 #if defined(DOSISH) || defined(USEMYBINMODE)
3367 /* ignore trailing ^Z on short files */
3368 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3372 for (i = 0; i < len; i++, s++) {
3373 if (!*s) { /* null never allowed in text */
3378 else if (!(isPRINT(*s) || isSPACE(*s)))
3381 else if (*s & 128) {
3383 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3386 /* utf8 characters don't count as odd */
3387 if (UTF8_IS_START(*s)) {
3388 int ulen = UTF8SKIP(s);
3389 if (ulen < len - i) {
3391 for (j = 1; j < ulen; j++) {
3392 if (!UTF8_IS_CONTINUATION(s[j]))
3395 --ulen; /* loop does extra increment */
3405 *s != '\n' && *s != '\r' && *s != '\b' &&
3406 *s != '\t' && *s != '\f' && *s != 27)
3411 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3422 const char *tmps = NULL;
3426 SV * const sv = POPs;
3427 if (PL_op->op_flags & OPf_SPECIAL) {
3428 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3430 else if (isGV_with_GP(sv)) {
3431 gv = MUTABLE_GV(sv);
3433 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3434 gv = MUTABLE_GV(SvRV(sv));
3437 tmps = SvPV_nolen_const(sv);
3441 if( !gv && (!tmps || !*tmps) ) {
3442 HV * const table = GvHVn(PL_envgv);
3445 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3446 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3448 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3453 deprecate("chdir('') or chdir(undef) as chdir()");
3454 tmps = SvPV_nolen_const(*svp);
3458 TAINT_PROPER("chdir");
3463 TAINT_PROPER("chdir");
3466 IO* const io = GvIO(gv);
3469 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3470 } else if (IoIFP(io)) {
3471 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3474 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3475 report_evil_fh(gv, io, PL_op->op_type);
3476 SETERRNO(EBADF, RMS_IFI);
3481 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3482 report_evil_fh(gv, io, PL_op->op_type);
3483 SETERRNO(EBADF,RMS_IFI);
3487 DIE(aTHX_ PL_no_func, "fchdir");
3491 PUSHi( PerlDir_chdir(tmps) >= 0 );
3493 /* Clear the DEFAULT element of ENV so we'll get the new value
3495 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3502 dVAR; dSP; dMARK; dTARGET;
3503 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3514 char * const tmps = POPpx;
3515 TAINT_PROPER("chroot");
3516 PUSHi( chroot(tmps) >= 0 );
3519 DIE(aTHX_ PL_no_func, "chroot");
3527 const char * const tmps2 = POPpconstx;
3528 const char * const tmps = SvPV_nolen_const(TOPs);
3529 TAINT_PROPER("rename");
3531 anum = PerlLIO_rename(tmps, tmps2);
3533 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3534 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3537 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3538 (void)UNLINK(tmps2);
3539 if (!(anum = link(tmps, tmps2)))
3540 anum = UNLINK(tmps);
3548 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3552 const int op_type = PL_op->op_type;
3556 if (op_type == OP_LINK)
3557 DIE(aTHX_ PL_no_func, "link");
3559 # ifndef HAS_SYMLINK
3560 if (op_type == OP_SYMLINK)
3561 DIE(aTHX_ PL_no_func, "symlink");
3565 const char * const tmps2 = POPpconstx;
3566 const char * const tmps = SvPV_nolen_const(TOPs);
3567 TAINT_PROPER(PL_op_desc[op_type]);
3569 # if defined(HAS_LINK)
3570 # if defined(HAS_SYMLINK)
3571 /* Both present - need to choose which. */
3572 (op_type == OP_LINK) ?
3573 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3575 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3576 PerlLIO_link(tmps, tmps2);
3579 # if defined(HAS_SYMLINK)
3580 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3581 symlink(tmps, tmps2);
3586 SETi( result >= 0 );
3593 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3604 char buf[MAXPATHLEN];
3607 #ifndef INCOMPLETE_TAINTS
3611 len = readlink(tmps, buf, sizeof(buf) - 1);
3619 RETSETUNDEF; /* just pretend it's a normal file */
3623 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3625 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3627 char * const save_filename = filename;
3632 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3634 PERL_ARGS_ASSERT_DOONELINER;
3636 Newx(cmdline, size, char);
3637 my_strlcpy(cmdline, cmd, size);
3638 my_strlcat(cmdline, " ", size);
3639 for (s = cmdline + strlen(cmdline); *filename; ) {
3643 if (s - cmdline < size)
3644 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3645 myfp = PerlProc_popen(cmdline, "r");
3649 SV * const tmpsv = sv_newmortal();
3650 /* Need to save/restore 'PL_rs' ?? */
3651 s = sv_gets(tmpsv, myfp, 0);
3652 (void)PerlProc_pclose(myfp);
3656 #ifdef HAS_SYS_ERRLIST
3661 /* you don't see this */
3662 const char * const errmsg =
3663 #ifdef HAS_SYS_ERRLIST
3671 if (instr(s, errmsg)) {
3678 #define EACCES EPERM
3680 if (instr(s, "cannot make"))
3681 SETERRNO(EEXIST,RMS_FEX);
3682 else if (instr(s, "existing file"))
3683 SETERRNO(EEXIST,RMS_FEX);
3684 else if (instr(s, "ile exists"))
3685 SETERRNO(EEXIST,RMS_FEX);
3686 else if (instr(s, "non-exist"))
3687 SETERRNO(ENOENT,RMS_FNF);
3688 else if (instr(s, "does not exist"))
3689 SETERRNO(ENOENT,RMS_FNF);
3690 else if (instr(s, "not empty"))
3691 SETERRNO(EBUSY,SS_DEVOFFLINE);
3692 else if (instr(s, "cannot access"))
3693 SETERRNO(EACCES,RMS_PRV);
3695 SETERRNO(EPERM,RMS_PRV);
3698 else { /* some mkdirs return no failure indication */
3699 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3700 if (PL_op->op_type == OP_RMDIR)
3705 SETERRNO(EACCES,RMS_PRV); /* a guess */
3714 /* This macro removes trailing slashes from a directory name.
3715 * Different operating and file systems take differently to
3716 * trailing slashes. According to POSIX 1003.1 1996 Edition
3717 * any number of trailing slashes should be allowed.
3718 * Thusly we snip them away so that even non-conforming
3719 * systems are happy.
3720 * We should probably do this "filtering" for all
3721 * the functions that expect (potentially) directory names:
3722 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3723 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3725 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3726 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3729 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3730 (tmps) = savepvn((tmps), (len)); \
3740 const int mode = (MAXARG > 1) ? POPi : 0777;
3742 TRIMSLASHES(tmps,len,copy);
3744 TAINT_PROPER("mkdir");
3746 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3750 SETi( dooneliner("mkdir", tmps) );
3751 oldumask = PerlLIO_umask(0);
3752 PerlLIO_umask(oldumask);
3753 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3768 TRIMSLASHES(tmps,len,copy);
3769 TAINT_PROPER("rmdir");
3771 SETi( PerlDir_rmdir(tmps) >= 0 );
3773 SETi( dooneliner("rmdir", tmps) );
3780 /* Directory calls. */
3784 #if defined(Direntry_t) && defined(HAS_READDIR)
3786 const char * const dirname = POPpconstx;
3787 GV * const gv = MUTABLE_GV(POPs);
3788 register IO * const io = GvIOn(gv);
3793 if ((IoIFP(io) || IoOFP(io)) && ckWARN2(WARN_IO, WARN_DEPRECATED))
3794 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3795 "Opening filehandle %s also as a directory", GvENAME(gv));
3797 PerlDir_close(IoDIRP(io));
3798 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3804 SETERRNO(EBADF,RMS_DIR);
3807 DIE(aTHX_ PL_no_dir_func, "opendir");
3813 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3814 DIE(aTHX_ PL_no_dir_func, "readdir");
3816 #if !defined(I_DIRENT) && !defined(VMS)
3817 Direntry_t *readdir (DIR *);
3823 const I32 gimme = GIMME;
3824 GV * const gv = MUTABLE_GV(POPs);
3825 register const Direntry_t *dp;
3826 register IO * const io = GvIOn(gv);
3828 if (!io || !IoDIRP(io)) {
3829 if(ckWARN(WARN_IO)) {
3830 Perl_warner(aTHX_ packWARN(WARN_IO),
3831 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3837 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3841 sv = newSVpvn(dp->d_name, dp->d_namlen);
3843 sv = newSVpv(dp->d_name, 0);
3845 #ifndef INCOMPLETE_TAINTS
3846 if (!(IoFLAGS(io) & IOf_UNTAINT))
3850 } while (gimme == G_ARRAY);
3852 if (!dp && gimme != G_ARRAY)
3859 SETERRNO(EBADF,RMS_ISI);
3860 if (GIMME == G_ARRAY)
3869 #if defined(HAS_TELLDIR) || defined(telldir)
3871 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3872 /* XXX netbsd still seemed to.
3873 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3874 --JHI 1999-Feb-02 */
3875 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3876 long telldir (DIR *);
3878 GV * const gv = MUTABLE_GV(POPs);
3879 register IO * const io = GvIOn(gv);
3881 if (!io || !IoDIRP(io)) {
3882 if(ckWARN(WARN_IO)) {
3883 Perl_warner(aTHX_ packWARN(WARN_IO),
3884 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3889 PUSHi( PerlDir_tell(IoDIRP(io)) );
3893 SETERRNO(EBADF,RMS_ISI);
3896 DIE(aTHX_ PL_no_dir_func, "telldir");
3902 #if defined(HAS_SEEKDIR) || defined(seekdir)
3904 const long along = POPl;
3905 GV * const gv = MUTABLE_GV(POPs);
3906 register IO * const io = GvIOn(gv);
3908 if (!io || !IoDIRP(io)) {
3909 if(ckWARN(WARN_IO)) {
3910 Perl_warner(aTHX_ packWARN(WARN_IO),
3911 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3915 (void)PerlDir_seek(IoDIRP(io), along);
3920 SETERRNO(EBADF,RMS_ISI);
3923 DIE(aTHX_ PL_no_dir_func, "seekdir");
3929 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3931 GV * const gv = MUTABLE_GV(POPs);
3932 register IO * const io = GvIOn(gv);
3934 if (!io || !IoDIRP(io)) {
3935 if(ckWARN(WARN_IO)) {
3936 Perl_warner(aTHX_ packWARN(WARN_IO),
3937 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3941 (void)PerlDir_rewind(IoDIRP(io));
3945 SETERRNO(EBADF,RMS_ISI);
3948 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3954 #if defined(Direntry_t) && defined(HAS_READDIR)
3956 GV * const gv = MUTABLE_GV(POPs);
3957 register IO * const io = GvIOn(gv);
3959 if (!io || !IoDIRP(io)) {
3960 if(ckWARN(WARN_IO)) {
3961 Perl_warner(aTHX_ packWARN(WARN_IO),
3962 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3966 #ifdef VOID_CLOSEDIR
3967 PerlDir_close(IoDIRP(io));
3969 if (PerlDir_close(IoDIRP(io)) < 0) {
3970 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3979 SETERRNO(EBADF,RMS_IFI);
3982 DIE(aTHX_ PL_no_dir_func, "closedir");
3986 /* Process control. */
3995 PERL_FLUSHALL_FOR_CHILD;
3996 childpid = PerlProc_fork();
4000 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
4002 SvREADONLY_off(GvSV(tmpgv));
4003 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4004 SvREADONLY_on(GvSV(tmpgv));
4006 #ifdef THREADS_HAVE_PIDS
4007 PL_ppid = (IV)getppid();
4009 #ifdef PERL_USES_PL_PIDSTATUS
4010 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4016 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4021 PERL_FLUSHALL_FOR_CHILD;
4022 childpid = PerlProc_fork();
4028 DIE(aTHX_ PL_no_func, "fork");
4035 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4040 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4041 childpid = wait4pid(-1, &argflags, 0);
4043 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4048 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4049 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4050 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4052 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4057 DIE(aTHX_ PL_no_func, "wait");
4063 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4065 const int optype = POPi;
4066 const Pid_t pid = TOPi;
4070 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4071 result = wait4pid(pid, &argflags, optype);
4073 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4078 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4079 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4080 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4082 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4087 DIE(aTHX_ PL_no_func, "waitpid");
4093 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4094 #if defined(__LIBCATAMOUNT__)
4095 PL_statusvalue = -1;
4104 while (++MARK <= SP) {
4105 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4110 TAINT_PROPER("system");
4112 PERL_FLUSHALL_FOR_CHILD;
4113 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4119 if (PerlProc_pipe(pp) >= 0)
4121 while ((childpid = PerlProc_fork()) == -1) {
4122 if (errno != EAGAIN) {
4127 PerlLIO_close(pp[0]);
4128 PerlLIO_close(pp[1]);
4135 Sigsave_t ihand,qhand; /* place to save signals during system() */
4139 PerlLIO_close(pp[1]);
4141 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4142 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4145 result = wait4pid(childpid, &status, 0);
4146 } while (result == -1 && errno == EINTR);
4148 (void)rsignal_restore(SIGINT, &ihand);
4149 (void)rsignal_restore(SIGQUIT, &qhand);
4151 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4152 do_execfree(); /* free any memory child malloced on fork */
4159 while (n < sizeof(int)) {
4160 n1 = PerlLIO_read(pp[0],
4161 (void*)(((char*)&errkid)+n),
4167 PerlLIO_close(pp[0]);
4168 if (n) { /* Error */
4169 if (n != sizeof(int))
4170 DIE(aTHX_ "panic: kid popen errno read");
4171 errno = errkid; /* Propagate errno from kid */
4172 STATUS_NATIVE_CHILD_SET(-1);
4175 XPUSHi(STATUS_CURRENT);
4179 PerlLIO_close(pp[0]);
4180 #if defined(HAS_FCNTL) && defined(F_SETFD)
4181 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4184 if (PL_op->op_flags & OPf_STACKED) {
4185 SV * const really = *++MARK;
4186 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4188 else if (SP - MARK != 1)
4189 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4191 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4195 #else /* ! FORK or VMS or OS/2 */
4198 if (PL_op->op_flags & OPf_STACKED) {
4199 SV * const really = *++MARK;
4200 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4201 value = (I32)do_aspawn(really, MARK, SP);
4203 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4206 else if (SP - MARK != 1) {
4207 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4208 value = (I32)do_aspawn(NULL, MARK, SP);
4210 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4214 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4216 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4218 STATUS_NATIVE_CHILD_SET(value);
4221 XPUSHi(result ? value : STATUS_CURRENT);
4222 #endif /* !FORK or VMS or OS/2 */
4229 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4234 while (++MARK <= SP) {
4235 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4240 TAINT_PROPER("exec");
4242 PERL_FLUSHALL_FOR_CHILD;
4243 if (PL_op->op_flags & OPf_STACKED) {
4244 SV * const really = *++MARK;
4245 value = (I32)do_aexec(really, MARK, SP);
4247 else if (SP - MARK != 1)
4249 value = (I32)vms_do_aexec(NULL, MARK, SP);
4253 (void ) do_aspawn(NULL, MARK, SP);
4257 value = (I32)do_aexec(NULL, MARK, SP);
4262 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4265 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4268 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4282 # ifdef THREADS_HAVE_PIDS
4283 if (PL_ppid != 1 && getppid() == 1)
4284 /* maybe the parent process has died. Refresh ppid cache */
4288 XPUSHi( getppid() );
4292 DIE(aTHX_ PL_no_func, "getppid");
4301 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4304 pgrp = (I32)BSD_GETPGRP(pid);
4306 if (pid != 0 && pid != PerlProc_getpid())
4307 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4313 DIE(aTHX_ PL_no_func, "getpgrp()");
4332 TAINT_PROPER("setpgrp");
4334 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4336 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4337 || (pid != 0 && pid != PerlProc_getpid()))
4339 DIE(aTHX_ "setpgrp can't take arguments");
4341 SETi( setpgrp() >= 0 );
4342 #endif /* USE_BSDPGRP */
4345 DIE(aTHX_ PL_no_func, "setpgrp()");
4351 #ifdef HAS_GETPRIORITY
4353 const int who = POPi;
4354 const int which = TOPi;
4355 SETi( getpriority(which, who) );
4358 DIE(aTHX_ PL_no_func, "getpriority()");
4364 #ifdef HAS_SETPRIORITY
4366 const int niceval = POPi;
4367 const int who = POPi;
4368 const int which = TOPi;
4369 TAINT_PROPER("setpriority");
4370 SETi( setpriority(which, who, niceval) >= 0 );
4373 DIE(aTHX_ PL_no_func, "setpriority()");
4383 XPUSHn( time(NULL) );
4385 XPUSHi( time(NULL) );
4397 (void)PerlProc_times(&PL_timesbuf);
4399 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4400 /* struct tms, though same data */
4404 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4405 if (GIMME == G_ARRAY) {
4406 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4407 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4408 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4416 if (GIMME == G_ARRAY) {
4423 DIE(aTHX_ "times not implemented");
4425 #endif /* HAS_TIMES */
4428 #ifdef LOCALTIME_EDGECASE_BROKEN
4429 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4434 /* No workarounds in the valid range */
4435 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4436 return (localtime (tp));
4438 /* This edge case is to workaround the undefined behaviour, where the
4439 * TIMEZONE makes the time go beyond the defined range.
4440 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4441 * If there is a negative offset in TZ, like MET-1METDST, some broken
4442 * implementations of localtime () (like AIX 5.2) barf with bogus
4444 * 0x7fffffff gmtime 2038-01-19 03:14:07
4445 * 0x7fffffff localtime 1901-12-13 21:45:51
4446 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4447 * 0x3c19137f gmtime 2001-12-13 20:45:51
4448 * 0x3c19137f localtime 2001-12-13 21:45:51
4449 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4450 * Given that legal timezones are typically between GMT-12 and GMT+12
4451 * we turn back the clock 23 hours before calling the localtime
4452 * function, and add those to the return value. This will never cause
4453 * day wrapping problems, since the edge case is Tue Jan *19*
4455 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4458 if (P->tm_hour >= 24) {
4460 P->tm_mday++; /* 18 -> 19 */
4461 P->tm_wday++; /* Mon -> Tue */
4462 P->tm_yday++; /* 18 -> 19 */
4465 } /* S_my_localtime */
4473 const struct tm *tmbuf;
4474 static const char * const dayname[] =
4475 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4476 static const char * const monname[] =
4477 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4478 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4484 when = (Time_t)SvNVx(POPs);
4486 when = (Time_t)SvIVx(POPs);
4489 if (PL_op->op_type == OP_LOCALTIME)
4490 #ifdef LOCALTIME_EDGECASE_BROKEN
4491 tmbuf = S_my_localtime(aTHX_ &when);
4493 tmbuf = localtime(&when);
4496 tmbuf = gmtime(&when);
4498 if (GIMME != G_ARRAY) {
4504 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4505 dayname[tmbuf->tm_wday],
4506 monname[tmbuf->tm_mon],
4511 tmbuf->tm_year + 1900);
4517 mPUSHi(tmbuf->tm_sec);
4518 mPUSHi(tmbuf->tm_min);
4519 mPUSHi(tmbuf->tm_hour);
4520 mPUSHi(tmbuf->tm_mday);
4521 mPUSHi(tmbuf->tm_mon);
4522 mPUSHi(tmbuf->tm_year);
4523 mPUSHi(tmbuf->tm_wday);
4524 mPUSHi(tmbuf->tm_yday);
4525 mPUSHi(tmbuf->tm_isdst);
4536 anum = alarm((unsigned int)anum);
4543 DIE(aTHX_ PL_no_func, "alarm");
4554 (void)time(&lasttime);
4559 PerlProc_sleep((unsigned int)duration);
4562 XPUSHi(when - lasttime);
4566 /* Shared memory. */
4567 /* Merged with some message passing. */
4571 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4572 dVAR; dSP; dMARK; dTARGET;
4573 const int op_type = PL_op->op_type;
4578 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4581 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4584 value = (I32)(do_semop(MARK, SP) >= 0);
4587 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4603 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4604 dVAR; dSP; dMARK; dTARGET;
4605 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4612 DIE(aTHX_ "System V IPC is not implemented on this machine");
4618 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4619 dVAR; dSP; dMARK; dTARGET;
4620 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4628 PUSHp(zero_but_true, ZBTLEN);
4636 /* I can't const this further without getting warnings about the types of
4637 various arrays passed in from structures. */
4639 S_space_join_names_mortal(pTHX_ char *const *array)
4643 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4645 if (array && *array) {
4646 target = newSVpvs_flags("", SVs_TEMP);
4648 sv_catpv(target, *array);
4651 sv_catpvs(target, " ");
4654 target = sv_mortalcopy(&PL_sv_no);
4659 /* Get system info. */
4663 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4665 I32 which = PL_op->op_type;
4666 register char **elem;
4668 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4669 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4670 struct hostent *gethostbyname(Netdb_name_t);
4671 struct hostent *gethostent(void);
4673 struct hostent *hent;
4677 if (which == OP_GHBYNAME) {
4678 #ifdef HAS_GETHOSTBYNAME
4679 const char* const name = POPpbytex;
4680 hent = PerlSock_gethostbyname(name);
4682 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4685 else if (which == OP_GHBYADDR) {
4686 #ifdef HAS_GETHOSTBYADDR
4687 const int addrtype = POPi;
4688 SV * const addrsv = POPs;
4690 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4692 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4694 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4698 #ifdef HAS_GETHOSTENT
4699 hent = PerlSock_gethostent();
4701 DIE(aTHX_ PL_no_sock_func, "gethostent");
4704 #ifdef HOST_NOT_FOUND
4706 #ifdef USE_REENTRANT_API
4707 # ifdef USE_GETHOSTENT_ERRNO
4708 h_errno = PL_reentrant_buffer->_gethostent_errno;
4711 STATUS_UNIX_SET(h_errno);
4715 if (GIMME != G_ARRAY) {
4716 PUSHs(sv = sv_newmortal());
4718 if (which == OP_GHBYNAME) {
4720 sv_setpvn(sv, hent->h_addr, hent->h_length);
4723 sv_setpv(sv, (char*)hent->h_name);
4729 mPUSHs(newSVpv((char*)hent->h_name, 0));
4730 PUSHs(space_join_names_mortal(hent->h_aliases));
4731 mPUSHi(hent->h_addrtype);
4732 len = hent->h_length;
4735 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4736 mXPUSHp(*elem, len);
4740 mPUSHp(hent->h_addr, len);
4742 PUSHs(sv_mortalcopy(&PL_sv_no));
4747 DIE(aTHX_ PL_no_sock_func, "gethostent");
4753 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4755 I32 which = PL_op->op_type;
4757 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4758 struct netent *getnetbyaddr(Netdb_net_t, int);
4759 struct netent *getnetbyname(Netdb_name_t);
4760 struct netent *getnetent(void);
4762 struct netent *nent;
4764 if (which == OP_GNBYNAME){
4765 #ifdef HAS_GETNETBYNAME
4766 const char * const name = POPpbytex;
4767 nent = PerlSock_getnetbyname(name);
4769 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4772 else if (which == OP_GNBYADDR) {
4773 #ifdef HAS_GETNETBYADDR
4774 const int addrtype = POPi;
4775 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4776 nent = PerlSock_getnetbyaddr(addr, addrtype);
4778 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4782 #ifdef HAS_GETNETENT
4783 nent = PerlSock_getnetent();
4785 DIE(aTHX_ PL_no_sock_func, "getnetent");
4788 #ifdef HOST_NOT_FOUND
4790 #ifdef USE_REENTRANT_API
4791 # ifdef USE_GETNETENT_ERRNO
4792 h_errno = PL_reentrant_buffer->_getnetent_errno;
4795 STATUS_UNIX_SET(h_errno);
4800 if (GIMME != G_ARRAY) {
4801 PUSHs(sv = sv_newmortal());
4803 if (which == OP_GNBYNAME)
4804 sv_setiv(sv, (IV)nent->n_net);
4806 sv_setpv(sv, nent->n_name);
4812 mPUSHs(newSVpv(nent->n_name, 0));
4813 PUSHs(space_join_names_mortal(nent->n_aliases));
4814 mPUSHi(nent->n_addrtype);
4815 mPUSHi(nent->n_net);
4820 DIE(aTHX_ PL_no_sock_func, "getnetent");
4826 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4828 I32 which = PL_op->op_type;
4830 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4831 struct protoent *getprotobyname(Netdb_name_t);
4832 struct protoent *getprotobynumber(int);
4833 struct protoent *getprotoent(void);
4835 struct protoent *pent;
4837 if (which == OP_GPBYNAME) {
4838 #ifdef HAS_GETPROTOBYNAME
4839 const char* const name = POPpbytex;
4840 pent = PerlSock_getprotobyname(name);
4842 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4845 else if (which == OP_GPBYNUMBER) {
4846 #ifdef HAS_GETPROTOBYNUMBER
4847 const int number = POPi;
4848 pent = PerlSock_getprotobynumber(number);
4850 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4854 #ifdef HAS_GETPROTOENT
4855 pent = PerlSock_getprotoent();
4857 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4861 if (GIMME != G_ARRAY) {
4862 PUSHs(sv = sv_newmortal());
4864 if (which == OP_GPBYNAME)
4865 sv_setiv(sv, (IV)pent->p_proto);
4867 sv_setpv(sv, pent->p_name);
4873 mPUSHs(newSVpv(pent->p_name, 0));
4874 PUSHs(space_join_names_mortal(pent->p_aliases));
4875 mPUSHi(pent->p_proto);
4880 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4886 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4888 I32 which = PL_op->op_type;
4890 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4891 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4892 struct servent *getservbyport(int, Netdb_name_t);
4893 struct servent *getservent(void);
4895 struct servent *sent;
4897 if (which == OP_GSBYNAME) {
4898 #ifdef HAS_GETSERVBYNAME
4899 const char * const proto = POPpbytex;
4900 const char * const name = POPpbytex;
4901 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4903 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4906 else if (which == OP_GSBYPORT) {
4907 #ifdef HAS_GETSERVBYPORT
4908 const char * const proto = POPpbytex;
4909 unsigned short port = (unsigned short)POPu;
4911 port = PerlSock_htons(port);
4913 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4915 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4919 #ifdef HAS_GETSERVENT
4920 sent = PerlSock_getservent();
4922 DIE(aTHX_ PL_no_sock_func, "getservent");
4926 if (GIMME != G_ARRAY) {
4927 PUSHs(sv = sv_newmortal());
4929 if (which == OP_GSBYNAME) {
4931 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4933 sv_setiv(sv, (IV)(sent->s_port));
4937 sv_setpv(sv, sent->s_name);
4943 mPUSHs(newSVpv(sent->s_name, 0));
4944 PUSHs(space_join_names_mortal(sent->s_aliases));
4946 mPUSHi(PerlSock_ntohs(sent->s_port));
4948 mPUSHi(sent->s_port);
4950 mPUSHs(newSVpv(sent->s_proto, 0));
4955 DIE(aTHX_ PL_no_sock_func, "getservent");
4961 #ifdef HAS_SETHOSTENT
4963 PerlSock_sethostent(TOPi);
4966 DIE(aTHX_ PL_no_sock_func, "sethostent");
4972 #ifdef HAS_SETNETENT
4974 (void)PerlSock_setnetent(TOPi);
4977 DIE(aTHX_ PL_no_sock_func, "setnetent");
4983 #ifdef HAS_SETPROTOENT
4985 (void)PerlSock_setprotoent(TOPi);
4988 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4994 #ifdef HAS_SETSERVENT
4996 (void)PerlSock_setservent(TOPi);
4999 DIE(aTHX_ PL_no_sock_func, "setservent");
5005 #ifdef HAS_ENDHOSTENT
5007 PerlSock_endhostent();
5011 DIE(aTHX_ PL_no_sock_func, "endhostent");
5017 #ifdef HAS_ENDNETENT
5019 PerlSock_endnetent();
5023 DIE(aTHX_ PL_no_sock_func, "endnetent");
5029 #ifdef HAS_ENDPROTOENT
5031 PerlSock_endprotoent();
5035 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5041 #ifdef HAS_ENDSERVENT
5043 PerlSock_endservent();
5047 DIE(aTHX_ PL_no_sock_func, "endservent");
5055 I32 which = PL_op->op_type;
5057 struct passwd *pwent = NULL;
5059 * We currently support only the SysV getsp* shadow password interface.
5060 * The interface is declared in <shadow.h> and often one needs to link
5061 * with -lsecurity or some such.
5062 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5065 * AIX getpwnam() is clever enough to return the encrypted password
5066 * only if the caller (euid?) is root.
5068 * There are at least three other shadow password APIs. Many platforms
5069 * seem to contain more than one interface for accessing the shadow
5070 * password databases, possibly for compatibility reasons.
5071 * The getsp*() is by far he simplest one, the other two interfaces
5072 * are much more complicated, but also very similar to each other.
5077 * struct pr_passwd *getprpw*();
5078 * The password is in
5079 * char getprpw*(...).ufld.fd_encrypt[]
5080 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5085 * struct es_passwd *getespw*();
5086 * The password is in
5087 * char *(getespw*(...).ufld.fd_encrypt)
5088 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5091 * struct userpw *getuserpw();
5092 * The password is in
5093 * char *(getuserpw(...)).spw_upw_passwd
5094 * (but the de facto standard getpwnam() should work okay)
5096 * Mention I_PROT here so that Configure probes for it.
5098 * In HP-UX for getprpw*() the manual page claims that one should include
5099 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5100 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5101 * and pp_sys.c already includes <shadow.h> if there is such.
5103 * Note that <sys/security.h> is already probed for, but currently
5104 * it is only included in special cases.
5106 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5107 * be preferred interface, even though also the getprpw*() interface
5108 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5109 * One also needs to call set_auth_parameters() in main() before
5110 * doing anything else, whether one is using getespw*() or getprpw*().
5112 * Note that accessing the shadow databases can be magnitudes
5113 * slower than accessing the standard databases.
5118 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5119 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5120 * the pw_comment is left uninitialized. */
5121 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5127 const char* const name = POPpbytex;
5128 pwent = getpwnam(name);
5134 pwent = getpwuid(uid);
5138 # ifdef HAS_GETPWENT
5140 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5141 if (pwent) pwent = getpwnam(pwent->pw_name);
5144 DIE(aTHX_ PL_no_func, "getpwent");
5150 if (GIMME != G_ARRAY) {
5151 PUSHs(sv = sv_newmortal());
5153 if (which == OP_GPWNAM)
5154 # if Uid_t_sign <= 0
5155 sv_setiv(sv, (IV)pwent->pw_uid);
5157 sv_setuv(sv, (UV)pwent->pw_uid);
5160 sv_setpv(sv, pwent->pw_name);
5166 mPUSHs(newSVpv(pwent->pw_name, 0));
5170 /* If we have getspnam(), we try to dig up the shadow
5171 * password. If we are underprivileged, the shadow
5172 * interface will set the errno to EACCES or similar,
5173 * and return a null pointer. If this happens, we will
5174 * use the dummy password (usually "*" or "x") from the
5175 * standard password database.
5177 * In theory we could skip the shadow call completely
5178 * if euid != 0 but in practice we cannot know which
5179 * security measures are guarding the shadow databases
5180 * on a random platform.
5182 * Resist the urge to use additional shadow interfaces.
5183 * Divert the urge to writing an extension instead.
5186 /* Some AIX setups falsely(?) detect some getspnam(), which
5187 * has a different API than the Solaris/IRIX one. */
5188 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5191 const struct spwd * const spwent = getspnam(pwent->pw_name);
5192 /* Save and restore errno so that
5193 * underprivileged attempts seem
5194 * to have never made the unsccessful
5195 * attempt to retrieve the shadow password. */
5197 if (spwent && spwent->sp_pwdp)
5198 sv_setpv(sv, spwent->sp_pwdp);
5202 if (!SvPOK(sv)) /* Use the standard password, then. */
5203 sv_setpv(sv, pwent->pw_passwd);
5206 # ifndef INCOMPLETE_TAINTS
5207 /* passwd is tainted because user himself can diddle with it.
5208 * admittedly not much and in a very limited way, but nevertheless. */
5212 # if Uid_t_sign <= 0
5213 mPUSHi(pwent->pw_uid);
5215 mPUSHu(pwent->pw_uid);
5218 # if Uid_t_sign <= 0
5219 mPUSHi(pwent->pw_gid);
5221 mPUSHu(pwent->pw_gid);
5223 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5224 * because of the poor interface of the Perl getpw*(),
5225 * not because there's some standard/convention saying so.
5226 * A better interface would have been to return a hash,
5227 * but we are accursed by our history, alas. --jhi. */
5229 mPUSHi(pwent->pw_change);
5232 mPUSHi(pwent->pw_quota);
5235 mPUSHs(newSVpv(pwent->pw_age, 0));
5237 /* I think that you can never get this compiled, but just in case. */
5238 PUSHs(sv_mortalcopy(&PL_sv_no));
5243 /* pw_class and pw_comment are mutually exclusive--.
5244 * see the above note for pw_change, pw_quota, and pw_age. */
5246 mPUSHs(newSVpv(pwent->pw_class, 0));
5249 mPUSHs(newSVpv(pwent->pw_comment, 0));
5251 /* I think that you can never get this compiled, but just in case. */
5252 PUSHs(sv_mortalcopy(&PL_sv_no));
5257 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5259 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5261 # ifndef INCOMPLETE_TAINTS
5262 /* pw_gecos is tainted because user himself can diddle with it. */
5266 mPUSHs(newSVpv(pwent->pw_dir, 0));
5268 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5269 # ifndef INCOMPLETE_TAINTS
5270 /* pw_shell is tainted because user himself can diddle with it. */
5275 mPUSHi(pwent->pw_expire);
5280 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5286 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5291 DIE(aTHX_ PL_no_func, "setpwent");
5297 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5302 DIE(aTHX_ PL_no_func, "endpwent");
5310 const I32 which = PL_op->op_type;
5311 const struct group *grent;
5313 if (which == OP_GGRNAM) {
5314 const char* const name = POPpbytex;
5315 grent = (const struct group *)getgrnam(name);
5317 else if (which == OP_GGRGID) {
5318 const Gid_t gid = POPi;
5319 grent = (const struct group *)getgrgid(gid);
5323 grent = (struct group *)getgrent();
5325 DIE(aTHX_ PL_no_func, "getgrent");
5329 if (GIMME != G_ARRAY) {
5330 SV * const sv = sv_newmortal();
5334 if (which == OP_GGRNAM)
5335 sv_setiv(sv, (IV)grent->gr_gid);
5337 sv_setpv(sv, grent->gr_name);
5343 mPUSHs(newSVpv(grent->gr_name, 0));
5346 mPUSHs(newSVpv(grent->gr_passwd, 0));
5348 PUSHs(sv_mortalcopy(&PL_sv_no));
5351 mPUSHi(grent->gr_gid);
5353 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5354 /* In UNICOS/mk (_CRAYMPP) the multithreading
5355 * versions (getgrnam_r, getgrgid_r)
5356 * seem to return an illegal pointer
5357 * as the group members list, gr_mem.
5358 * getgrent() doesn't even have a _r version
5359 * but the gr_mem is poisonous anyway.
5360 * So yes, you cannot get the list of group
5361 * members if building multithreaded in UNICOS/mk. */
5362 PUSHs(space_join_names_mortal(grent->gr_mem));
5368 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5374 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5379 DIE(aTHX_ PL_no_func, "setgrent");
5385 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5390 DIE(aTHX_ PL_no_func, "endgrent");
5400 if (!(tmps = PerlProc_getlogin()))
5402 PUSHp(tmps, strlen(tmps));
5405 DIE(aTHX_ PL_no_func, "getlogin");
5409 /* Miscellaneous. */
5414 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5415 register I32 items = SP - MARK;
5416 unsigned long a[20];
5421 while (++MARK <= SP) {
5422 if (SvTAINTED(*MARK)) {
5428 TAINT_PROPER("syscall");
5431 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5432 * or where sizeof(long) != sizeof(char*). But such machines will
5433 * not likely have syscall implemented either, so who cares?
5435 while (++MARK <= SP) {
5436 if (SvNIOK(*MARK) || !i)
5437 a[i++] = SvIV(*MARK);
5438 else if (*MARK == &PL_sv_undef)
5441 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5447 DIE(aTHX_ "Too many args to syscall");
5449 DIE(aTHX_ "Too few args to syscall");
5451 retval = syscall(a[0]);
5454 retval = syscall(a[0],a[1]);
5457 retval = syscall(a[0],a[1],a[2]);
5460 retval = syscall(a[0],a[1],a[2],a[3]);
5463 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5466 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5469 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5472 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5476 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5479 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5482 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5486 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5490 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5494 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5495 a[10],a[11],a[12],a[13]);
5497 #endif /* atarist */
5503 DIE(aTHX_ PL_no_func, "syscall");
5507 #ifdef FCNTL_EMULATE_FLOCK
5509 /* XXX Emulate flock() with fcntl().
5510 What's really needed is a good file locking module.
5514 fcntl_emulate_flock(int fd, int operation)
5518 switch (operation & ~LOCK_NB) {
5520 flock.l_type = F_RDLCK;
5523 flock.l_type = F_WRLCK;
5526 flock.l_type = F_UNLCK;
5532 flock.l_whence = SEEK_SET;
5533 flock.l_start = flock.l_len = (Off_t)0;
5535 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5538 #endif /* FCNTL_EMULATE_FLOCK */
5540 #ifdef LOCKF_EMULATE_FLOCK
5542 /* XXX Emulate flock() with lockf(). This is just to increase
5543 portability of scripts. The calls are not completely
5544 interchangeable. What's really needed is a good file
5548 /* The lockf() constants might have been defined in <unistd.h>.
5549 Unfortunately, <unistd.h> causes troubles on some mixed
5550 (BSD/POSIX) systems, such as SunOS 4.1.3.
5552 Further, the lockf() constants aren't POSIX, so they might not be
5553 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5554 just stick in the SVID values and be done with it. Sigh.
5558 # define F_ULOCK 0 /* Unlock a previously locked region */
5561 # define F_LOCK 1 /* Lock a region for exclusive use */
5564 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5567 # define F_TEST 3 /* Test a region for other processes locks */
5571 lockf_emulate_flock(int fd, int operation)
5577 /* flock locks entire file so for lockf we need to do the same */
5578 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5579 if (pos > 0) /* is seekable and needs to be repositioned */
5580 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5581 pos = -1; /* seek failed, so don't seek back afterwards */
5584 switch (operation) {
5586 /* LOCK_SH - get a shared lock */
5588 /* LOCK_EX - get an exclusive lock */
5590 i = lockf (fd, F_LOCK, 0);
5593 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5594 case LOCK_SH|LOCK_NB:
5595 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5596 case LOCK_EX|LOCK_NB:
5597 i = lockf (fd, F_TLOCK, 0);
5599 if ((errno == EAGAIN) || (errno == EACCES))
5600 errno = EWOULDBLOCK;
5603 /* LOCK_UN - unlock (non-blocking is a no-op) */
5605 case LOCK_UN|LOCK_NB:
5606 i = lockf (fd, F_ULOCK, 0);
5609 /* Default - can't decipher operation */
5616 if (pos > 0) /* need to restore position of the handle */
5617 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5622 #endif /* LOCKF_EMULATE_FLOCK */
5626 * c-indentation-style: bsd
5628 * indent-tabs-mode: t
5631 * ex: set ts=8 sts=4 sw=4 noet: