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
2030 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
2032 gv = PL_last_in_gv = GvEGV(PL_argvgv);
2034 if (io && !IoIFP(io)) {
2035 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2037 IoFLAGS(io) &= ~IOf_START;
2038 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2040 sv_setpvs(GvSV(gv), "-");
2043 GvSV(gv) = newSVpvs("-");
2045 SvSETMAGIC(GvSV(gv));
2047 else if (!nextargv(gv))
2052 gv = PL_last_in_gv; /* eof */
2055 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2058 IO * const io = GvIO(gv);
2060 if (io && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2062 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2065 call_method("EOF", G_SCALAR);
2072 PUSHs(boolSV(!gv || do_eof(gv)));
2083 PL_last_in_gv = MUTABLE_GV(POPs);
2086 if (gv && (io = GvIO(gv))) {
2087 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2090 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2093 call_method("TELL", G_SCALAR);
2100 #if LSEEKSIZE > IVSIZE
2101 PUSHn( do_tell(gv) );
2103 PUSHi( do_tell(gv) );
2111 const int whence = POPi;
2112 #if LSEEKSIZE > IVSIZE
2113 const Off_t offset = (Off_t)SvNVx(POPs);
2115 const Off_t offset = (Off_t)SvIVx(POPs);
2118 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2121 if (gv && (io = GvIO(gv))) {
2122 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2125 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2126 #if LSEEKSIZE > IVSIZE
2127 mXPUSHn((NV) offset);
2134 call_method("SEEK", G_SCALAR);
2141 if (PL_op->op_type == OP_SEEK)
2142 PUSHs(boolSV(do_seek(gv, offset, whence)));
2144 const Off_t sought = do_sysseek(gv, offset, whence);
2146 PUSHs(&PL_sv_undef);
2148 SV* const sv = sought ?
2149 #if LSEEKSIZE > IVSIZE
2154 : newSVpvn(zero_but_true, ZBTLEN);
2165 /* There seems to be no consensus on the length type of truncate()
2166 * and ftruncate(), both off_t and size_t have supporters. In
2167 * general one would think that when using large files, off_t is
2168 * at least as wide as size_t, so using an off_t should be okay. */
2169 /* XXX Configure probe for the length type of *truncate() needed XXX */
2172 #if Off_t_size > IVSIZE
2177 /* Checking for length < 0 is problematic as the type might or
2178 * might not be signed: if it is not, clever compilers will moan. */
2179 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2186 if (PL_op->op_flags & OPf_SPECIAL) {
2187 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2196 TAINT_PROPER("truncate");
2197 if (!(fp = IoIFP(io))) {
2203 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2205 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2212 SV * const sv = POPs;
2215 if (isGV_with_GP(sv)) {
2216 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2217 goto do_ftruncate_gv;
2219 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2220 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2221 goto do_ftruncate_gv;
2223 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2224 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2225 goto do_ftruncate_io;
2228 name = SvPV_nolen_const(sv);
2229 TAINT_PROPER("truncate");
2231 if (truncate(name, len) < 0)
2235 const int tmpfd = PerlLIO_open(name, O_RDWR);
2240 if (my_chsize(tmpfd, len) < 0)
2242 PerlLIO_close(tmpfd);
2251 SETERRNO(EBADF,RMS_IFI);
2259 SV * const argsv = POPs;
2260 const unsigned int func = POPu;
2261 const int optype = PL_op->op_type;
2262 GV * const gv = MUTABLE_GV(POPs);
2263 IO * const io = gv ? GvIOn(gv) : NULL;
2267 if (!io || !argsv || !IoIFP(io)) {
2268 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2269 report_evil_fh(gv, io, PL_op->op_type);
2270 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2274 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2277 s = SvPV_force(argsv, len);
2278 need = IOCPARM_LEN(func);
2280 s = Sv_Grow(argsv, need + 1);
2281 SvCUR_set(argsv, need);
2284 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2287 retval = SvIV(argsv);
2288 s = INT2PTR(char*,retval); /* ouch */
2291 TAINT_PROPER(PL_op_desc[optype]);
2293 if (optype == OP_IOCTL)
2295 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2297 DIE(aTHX_ "ioctl is not implemented");
2301 DIE(aTHX_ "fcntl is not implemented");
2303 #if defined(OS2) && defined(__EMX__)
2304 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2306 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2310 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2312 if (s[SvCUR(argsv)] != 17)
2313 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2315 s[SvCUR(argsv)] = 0; /* put our null back */
2316 SvSETMAGIC(argsv); /* Assume it has changed */
2325 PUSHp(zero_but_true, ZBTLEN);
2338 const int argtype = POPi;
2339 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2341 if (gv && (io = GvIO(gv)))
2347 /* XXX Looks to me like io is always NULL at this point */
2349 (void)PerlIO_flush(fp);
2350 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2353 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2354 report_evil_fh(gv, io, PL_op->op_type);
2356 SETERRNO(EBADF,RMS_IFI);
2361 DIE(aTHX_ PL_no_func, "flock()");
2371 const int protocol = POPi;
2372 const int type = POPi;
2373 const int domain = POPi;
2374 GV * const gv = MUTABLE_GV(POPs);
2375 register IO * const io = gv ? GvIOn(gv) : NULL;
2379 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2380 report_evil_fh(gv, io, PL_op->op_type);
2381 if (io && IoIFP(io))
2382 do_close(gv, FALSE);
2383 SETERRNO(EBADF,LIB_INVARG);
2388 do_close(gv, FALSE);
2390 TAINT_PROPER("socket");
2391 fd = PerlSock_socket(domain, type, protocol);
2394 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2395 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2396 IoTYPE(io) = IoTYPE_SOCKET;
2397 if (!IoIFP(io) || !IoOFP(io)) {
2398 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2399 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2400 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2403 #if defined(HAS_FCNTL) && defined(F_SETFD)
2404 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2408 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2413 DIE(aTHX_ PL_no_sock_func, "socket");
2419 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2421 const int protocol = POPi;
2422 const int type = POPi;
2423 const int domain = POPi;
2424 GV * const gv2 = MUTABLE_GV(POPs);
2425 GV * const gv1 = MUTABLE_GV(POPs);
2426 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2427 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2430 if (!gv1 || !gv2 || !io1 || !io2) {
2431 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2433 report_evil_fh(gv1, io1, PL_op->op_type);
2435 report_evil_fh(gv1, io2, PL_op->op_type);
2437 if (io1 && IoIFP(io1))
2438 do_close(gv1, FALSE);
2439 if (io2 && IoIFP(io2))
2440 do_close(gv2, FALSE);
2445 do_close(gv1, FALSE);
2447 do_close(gv2, FALSE);
2449 TAINT_PROPER("socketpair");
2450 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2452 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2453 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2454 IoTYPE(io1) = IoTYPE_SOCKET;
2455 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2456 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2457 IoTYPE(io2) = IoTYPE_SOCKET;
2458 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2459 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2460 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2461 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2462 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2463 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2464 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2467 #if defined(HAS_FCNTL) && defined(F_SETFD)
2468 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2469 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2474 DIE(aTHX_ PL_no_sock_func, "socketpair");
2482 SV * const addrsv = POPs;
2483 /* OK, so on what platform does bind modify addr? */
2485 GV * const gv = MUTABLE_GV(POPs);
2486 register IO * const io = GvIOn(gv);
2489 if (!io || !IoIFP(io))
2492 addr = SvPV_const(addrsv, len);
2493 TAINT_PROPER("bind");
2494 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2500 if (ckWARN(WARN_CLOSED))
2501 report_evil_fh(gv, io, PL_op->op_type);
2502 SETERRNO(EBADF,SS_IVCHAN);
2505 DIE(aTHX_ PL_no_sock_func, "bind");
2513 SV * const addrsv = POPs;
2514 GV * const gv = MUTABLE_GV(POPs);
2515 register IO * const io = GvIOn(gv);
2519 if (!io || !IoIFP(io))
2522 addr = SvPV_const(addrsv, len);
2523 TAINT_PROPER("connect");
2524 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2530 if (ckWARN(WARN_CLOSED))
2531 report_evil_fh(gv, io, PL_op->op_type);
2532 SETERRNO(EBADF,SS_IVCHAN);
2535 DIE(aTHX_ PL_no_sock_func, "connect");
2543 const int backlog = POPi;
2544 GV * const gv = MUTABLE_GV(POPs);
2545 register IO * const io = gv ? GvIOn(gv) : NULL;
2547 if (!gv || !io || !IoIFP(io))
2550 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2556 if (ckWARN(WARN_CLOSED))
2557 report_evil_fh(gv, io, PL_op->op_type);
2558 SETERRNO(EBADF,SS_IVCHAN);
2561 DIE(aTHX_ PL_no_sock_func, "listen");
2571 char namebuf[MAXPATHLEN];
2572 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2573 Sock_size_t len = sizeof (struct sockaddr_in);
2575 Sock_size_t len = sizeof namebuf;
2577 GV * const ggv = MUTABLE_GV(POPs);
2578 GV * const ngv = MUTABLE_GV(POPs);
2587 if (!gstio || !IoIFP(gstio))
2591 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2594 /* Some platforms indicate zero length when an AF_UNIX client is
2595 * not bound. Simulate a non-zero-length sockaddr structure in
2597 namebuf[0] = 0; /* sun_len */
2598 namebuf[1] = AF_UNIX; /* sun_family */
2606 do_close(ngv, FALSE);
2607 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2608 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2609 IoTYPE(nstio) = IoTYPE_SOCKET;
2610 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2611 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2612 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2613 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2616 #if defined(HAS_FCNTL) && defined(F_SETFD)
2617 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2621 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2622 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2624 #ifdef __SCO_VERSION__
2625 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2628 PUSHp(namebuf, len);
2632 if (ckWARN(WARN_CLOSED))
2633 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2634 SETERRNO(EBADF,SS_IVCHAN);
2640 DIE(aTHX_ PL_no_sock_func, "accept");
2648 const int how = POPi;
2649 GV * const gv = MUTABLE_GV(POPs);
2650 register IO * const io = GvIOn(gv);
2652 if (!io || !IoIFP(io))
2655 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2659 if (ckWARN(WARN_CLOSED))
2660 report_evil_fh(gv, io, PL_op->op_type);
2661 SETERRNO(EBADF,SS_IVCHAN);
2664 DIE(aTHX_ PL_no_sock_func, "shutdown");
2672 const int optype = PL_op->op_type;
2673 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2674 const unsigned int optname = (unsigned int) POPi;
2675 const unsigned int lvl = (unsigned int) POPi;
2676 GV * const gv = MUTABLE_GV(POPs);
2677 register IO * const io = GvIOn(gv);
2681 if (!io || !IoIFP(io))
2684 fd = PerlIO_fileno(IoIFP(io));
2688 (void)SvPOK_only(sv);
2692 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2699 #if defined(__SYMBIAN32__)
2700 # define SETSOCKOPT_OPTION_VALUE_T void *
2702 # define SETSOCKOPT_OPTION_VALUE_T const char *
2704 /* XXX TODO: We need to have a proper type (a Configure probe,
2705 * etc.) for what the C headers think of the third argument of
2706 * setsockopt(), the option_value read-only buffer: is it
2707 * a "char *", or a "void *", const or not. Some compilers
2708 * don't take kindly to e.g. assuming that "char *" implicitly
2709 * promotes to a "void *", or to explicitly promoting/demoting
2710 * consts to non/vice versa. The "const void *" is the SUS
2711 * definition, but that does not fly everywhere for the above
2713 SETSOCKOPT_OPTION_VALUE_T buf;
2717 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2721 aint = (int)SvIV(sv);
2722 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2725 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2734 if (ckWARN(WARN_CLOSED))
2735 report_evil_fh(gv, io, optype);
2736 SETERRNO(EBADF,SS_IVCHAN);
2741 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2749 const int optype = PL_op->op_type;
2750 GV * const gv = MUTABLE_GV(POPs);
2751 register IO * const io = GvIOn(gv);
2756 if (!io || !IoIFP(io))
2759 sv = sv_2mortal(newSV(257));
2760 (void)SvPOK_only(sv);
2764 fd = PerlIO_fileno(IoIFP(io));
2766 case OP_GETSOCKNAME:
2767 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2770 case OP_GETPEERNAME:
2771 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2773 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2775 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";
2776 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2777 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2778 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2779 sizeof(u_short) + sizeof(struct in_addr))) {
2786 #ifdef BOGUS_GETNAME_RETURN
2787 /* Interactive Unix, getpeername() and getsockname()
2788 does not return valid namelen */
2789 if (len == BOGUS_GETNAME_RETURN)
2790 len = sizeof(struct sockaddr);
2798 if (ckWARN(WARN_CLOSED))
2799 report_evil_fh(gv, io, optype);
2800 SETERRNO(EBADF,SS_IVCHAN);
2805 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2820 if (PL_op->op_flags & OPf_REF) {
2822 if (PL_op->op_type == OP_LSTAT) {
2823 if (gv != PL_defgv) {
2824 do_fstat_warning_check:
2825 if (ckWARN(WARN_IO))
2826 Perl_warner(aTHX_ packWARN(WARN_IO),
2827 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2828 } else if (PL_laststype != OP_LSTAT)
2829 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2833 if (gv != PL_defgv) {
2834 PL_laststype = OP_STAT;
2836 sv_setpvs(PL_statname, "");
2843 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2844 } else if (IoDIRP(io)) {
2846 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2848 PL_laststatval = -1;
2854 if (PL_laststatval < 0) {
2855 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2856 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2861 SV* const sv = POPs;
2862 if (isGV_with_GP(sv)) {
2863 gv = MUTABLE_GV(sv);
2865 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2866 gv = MUTABLE_GV(SvRV(sv));
2867 if (PL_op->op_type == OP_LSTAT)
2868 goto do_fstat_warning_check;
2870 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2871 io = MUTABLE_IO(SvRV(sv));
2872 if (PL_op->op_type == OP_LSTAT)
2873 goto do_fstat_warning_check;
2874 goto do_fstat_have_io;
2877 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2879 PL_laststype = PL_op->op_type;
2880 if (PL_op->op_type == OP_LSTAT)
2881 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2883 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2884 if (PL_laststatval < 0) {
2885 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2886 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2892 if (gimme != G_ARRAY) {
2893 if (gimme != G_VOID)
2894 XPUSHs(boolSV(max));
2900 mPUSHi(PL_statcache.st_dev);
2901 mPUSHi(PL_statcache.st_ino);
2902 mPUSHu(PL_statcache.st_mode);
2903 mPUSHu(PL_statcache.st_nlink);
2904 #if Uid_t_size > IVSIZE
2905 mPUSHn(PL_statcache.st_uid);
2907 # if Uid_t_sign <= 0
2908 mPUSHi(PL_statcache.st_uid);
2910 mPUSHu(PL_statcache.st_uid);
2913 #if Gid_t_size > IVSIZE
2914 mPUSHn(PL_statcache.st_gid);
2916 # if Gid_t_sign <= 0
2917 mPUSHi(PL_statcache.st_gid);
2919 mPUSHu(PL_statcache.st_gid);
2922 #ifdef USE_STAT_RDEV
2923 mPUSHi(PL_statcache.st_rdev);
2925 PUSHs(newSVpvs_flags("", SVs_TEMP));
2927 #if Off_t_size > IVSIZE
2928 mPUSHn(PL_statcache.st_size);
2930 mPUSHi(PL_statcache.st_size);
2933 mPUSHn(PL_statcache.st_atime);
2934 mPUSHn(PL_statcache.st_mtime);
2935 mPUSHn(PL_statcache.st_ctime);
2937 mPUSHi(PL_statcache.st_atime);
2938 mPUSHi(PL_statcache.st_mtime);
2939 mPUSHi(PL_statcache.st_ctime);
2941 #ifdef USE_STAT_BLOCKS
2942 mPUSHu(PL_statcache.st_blksize);
2943 mPUSHu(PL_statcache.st_blocks);
2945 PUSHs(newSVpvs_flags("", SVs_TEMP));
2946 PUSHs(newSVpvs_flags("", SVs_TEMP));
2952 /* This macro is used by the stacked filetest operators :
2953 * if the previous filetest failed, short-circuit and pass its value.
2954 * Else, discard it from the stack and continue. --rgs
2956 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2957 if (!SvTRUE(TOPs)) { RETURN; } \
2958 else { (void)POPs; PUTBACK; } \
2965 /* Not const, because things tweak this below. Not bool, because there's
2966 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2967 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2968 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2969 /* Giving some sort of initial value silences compilers. */
2971 int access_mode = R_OK;
2973 int access_mode = 0;
2976 /* access_mode is never used, but leaving use_access in makes the
2977 conditional compiling below much clearer. */
2980 int stat_mode = S_IRUSR;
2982 bool effective = FALSE;
2985 STACKED_FTEST_CHECK;
2987 switch (PL_op->op_type) {
2989 #if !(defined(HAS_ACCESS) && defined(R_OK))
2995 #if defined(HAS_ACCESS) && defined(W_OK)
3000 stat_mode = S_IWUSR;
3004 #if defined(HAS_ACCESS) && defined(X_OK)
3009 stat_mode = S_IXUSR;
3013 #ifdef PERL_EFF_ACCESS
3016 stat_mode = S_IWUSR;
3020 #ifndef PERL_EFF_ACCESS
3027 #ifdef PERL_EFF_ACCESS
3032 stat_mode = S_IXUSR;
3038 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3039 const char *name = POPpx;
3041 # ifdef PERL_EFF_ACCESS
3042 result = PERL_EFF_ACCESS(name, access_mode);
3044 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3050 result = access(name, access_mode);
3052 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3067 if (cando(stat_mode, effective, &PL_statcache))
3076 const int op_type = PL_op->op_type;
3078 STACKED_FTEST_CHECK;
3083 if (op_type == OP_FTIS)
3086 /* You can't dTARGET inside OP_FTIS, because you'll get
3087 "panic: pad_sv po" - the op is not flagged to have a target. */
3091 #if Off_t_size > IVSIZE
3092 PUSHn(PL_statcache.st_size);
3094 PUSHi(PL_statcache.st_size);
3098 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3101 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3104 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3117 /* I believe that all these three are likely to be defined on most every
3118 system these days. */
3120 if(PL_op->op_type == OP_FTSUID)
3124 if(PL_op->op_type == OP_FTSGID)
3128 if(PL_op->op_type == OP_FTSVTX)
3132 STACKED_FTEST_CHECK;
3137 switch (PL_op->op_type) {
3139 if (PL_statcache.st_uid == PL_uid)
3143 if (PL_statcache.st_uid == PL_euid)
3147 if (PL_statcache.st_size == 0)
3151 if (S_ISSOCK(PL_statcache.st_mode))
3155 if (S_ISCHR(PL_statcache.st_mode))
3159 if (S_ISBLK(PL_statcache.st_mode))
3163 if (S_ISREG(PL_statcache.st_mode))
3167 if (S_ISDIR(PL_statcache.st_mode))
3171 if (S_ISFIFO(PL_statcache.st_mode))
3176 if (PL_statcache.st_mode & S_ISUID)
3182 if (PL_statcache.st_mode & S_ISGID)
3188 if (PL_statcache.st_mode & S_ISVTX)
3199 I32 result = my_lstat();
3203 if (S_ISLNK(PL_statcache.st_mode))
3216 STACKED_FTEST_CHECK;
3218 if (PL_op->op_flags & OPf_REF)
3220 else if (isGV(TOPs))
3221 gv = MUTABLE_GV(POPs);
3222 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3223 gv = MUTABLE_GV(SvRV(POPs));
3225 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3227 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3228 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3229 else if (tmpsv && SvOK(tmpsv)) {
3230 const char *tmps = SvPV_nolen_const(tmpsv);
3238 if (PerlLIO_isatty(fd))
3243 #if defined(atarist) /* this will work with atariST. Configure will
3244 make guesses for other systems. */
3245 # define FILE_base(f) ((f)->_base)
3246 # define FILE_ptr(f) ((f)->_ptr)
3247 # define FILE_cnt(f) ((f)->_cnt)
3248 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3259 register STDCHAR *s;
3265 STACKED_FTEST_CHECK;
3267 if (PL_op->op_flags & OPf_REF)
3269 else if (isGV(TOPs))
3270 gv = MUTABLE_GV(POPs);
3271 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3272 gv = MUTABLE_GV(SvRV(POPs));
3278 if (gv == PL_defgv) {
3280 io = GvIO(PL_statgv);
3283 goto really_filename;
3288 PL_laststatval = -1;
3289 sv_setpvs(PL_statname, "");
3290 io = GvIO(PL_statgv);
3292 if (io && IoIFP(io)) {
3293 if (! PerlIO_has_base(IoIFP(io)))
3294 DIE(aTHX_ "-T and -B not implemented on filehandles");
3295 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3296 if (PL_laststatval < 0)
3298 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3299 if (PL_op->op_type == OP_FTTEXT)
3304 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3305 i = PerlIO_getc(IoIFP(io));
3307 (void)PerlIO_ungetc(IoIFP(io),i);
3309 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3311 len = PerlIO_get_bufsiz(IoIFP(io));
3312 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3313 /* sfio can have large buffers - limit to 512 */
3318 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3320 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3322 SETERRNO(EBADF,RMS_IFI);
3330 PL_laststype = OP_STAT;
3331 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3332 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3333 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3335 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3338 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3339 if (PL_laststatval < 0) {
3340 (void)PerlIO_close(fp);
3343 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3344 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3345 (void)PerlIO_close(fp);
3347 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3348 RETPUSHNO; /* special case NFS directories */
3349 RETPUSHYES; /* null file is anything */
3354 /* now scan s to look for textiness */
3355 /* XXX ASCII dependent code */
3357 #if defined(DOSISH) || defined(USEMYBINMODE)
3358 /* ignore trailing ^Z on short files */
3359 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3363 for (i = 0; i < len; i++, s++) {
3364 if (!*s) { /* null never allowed in text */
3369 else if (!(isPRINT(*s) || isSPACE(*s)))
3372 else if (*s & 128) {
3374 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3377 /* utf8 characters don't count as odd */
3378 if (UTF8_IS_START(*s)) {
3379 int ulen = UTF8SKIP(s);
3380 if (ulen < len - i) {
3382 for (j = 1; j < ulen; j++) {
3383 if (!UTF8_IS_CONTINUATION(s[j]))
3386 --ulen; /* loop does extra increment */
3396 *s != '\n' && *s != '\r' && *s != '\b' &&
3397 *s != '\t' && *s != '\f' && *s != 27)
3402 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3413 const char *tmps = NULL;
3417 SV * const sv = POPs;
3418 if (PL_op->op_flags & OPf_SPECIAL) {
3419 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3421 else if (isGV_with_GP(sv)) {
3422 gv = MUTABLE_GV(sv);
3424 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3425 gv = MUTABLE_GV(SvRV(sv));
3428 tmps = SvPV_nolen_const(sv);
3432 if( !gv && (!tmps || !*tmps) ) {
3433 HV * const table = GvHVn(PL_envgv);
3436 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3437 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3439 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3444 deprecate("chdir('') or chdir(undef) as chdir()");
3445 tmps = SvPV_nolen_const(*svp);
3449 TAINT_PROPER("chdir");
3454 TAINT_PROPER("chdir");
3457 IO* const io = GvIO(gv);
3460 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3461 } else if (IoIFP(io)) {
3462 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3465 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3466 report_evil_fh(gv, io, PL_op->op_type);
3467 SETERRNO(EBADF, RMS_IFI);
3472 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3473 report_evil_fh(gv, io, PL_op->op_type);
3474 SETERRNO(EBADF,RMS_IFI);
3478 DIE(aTHX_ PL_no_func, "fchdir");
3482 PUSHi( PerlDir_chdir(tmps) >= 0 );
3484 /* Clear the DEFAULT element of ENV so we'll get the new value
3486 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3493 dVAR; dSP; dMARK; dTARGET;
3494 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3505 char * const tmps = POPpx;
3506 TAINT_PROPER("chroot");
3507 PUSHi( chroot(tmps) >= 0 );
3510 DIE(aTHX_ PL_no_func, "chroot");
3518 const char * const tmps2 = POPpconstx;
3519 const char * const tmps = SvPV_nolen_const(TOPs);
3520 TAINT_PROPER("rename");
3522 anum = PerlLIO_rename(tmps, tmps2);
3524 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3525 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3528 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3529 (void)UNLINK(tmps2);
3530 if (!(anum = link(tmps, tmps2)))
3531 anum = UNLINK(tmps);
3539 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3543 const int op_type = PL_op->op_type;
3547 if (op_type == OP_LINK)
3548 DIE(aTHX_ PL_no_func, "link");
3550 # ifndef HAS_SYMLINK
3551 if (op_type == OP_SYMLINK)
3552 DIE(aTHX_ PL_no_func, "symlink");
3556 const char * const tmps2 = POPpconstx;
3557 const char * const tmps = SvPV_nolen_const(TOPs);
3558 TAINT_PROPER(PL_op_desc[op_type]);
3560 # if defined(HAS_LINK)
3561 # if defined(HAS_SYMLINK)
3562 /* Both present - need to choose which. */
3563 (op_type == OP_LINK) ?
3564 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3566 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3567 PerlLIO_link(tmps, tmps2);
3570 # if defined(HAS_SYMLINK)
3571 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3572 symlink(tmps, tmps2);
3577 SETi( result >= 0 );
3584 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3595 char buf[MAXPATHLEN];
3598 #ifndef INCOMPLETE_TAINTS
3602 len = readlink(tmps, buf, sizeof(buf) - 1);
3610 RETSETUNDEF; /* just pretend it's a normal file */
3614 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3616 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3618 char * const save_filename = filename;
3623 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3625 PERL_ARGS_ASSERT_DOONELINER;
3627 Newx(cmdline, size, char);
3628 my_strlcpy(cmdline, cmd, size);
3629 my_strlcat(cmdline, " ", size);
3630 for (s = cmdline + strlen(cmdline); *filename; ) {
3634 if (s - cmdline < size)
3635 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3636 myfp = PerlProc_popen(cmdline, "r");
3640 SV * const tmpsv = sv_newmortal();
3641 /* Need to save/restore 'PL_rs' ?? */
3642 s = sv_gets(tmpsv, myfp, 0);
3643 (void)PerlProc_pclose(myfp);
3647 #ifdef HAS_SYS_ERRLIST
3652 /* you don't see this */
3653 const char * const errmsg =
3654 #ifdef HAS_SYS_ERRLIST
3662 if (instr(s, errmsg)) {
3669 #define EACCES EPERM
3671 if (instr(s, "cannot make"))
3672 SETERRNO(EEXIST,RMS_FEX);
3673 else if (instr(s, "existing file"))
3674 SETERRNO(EEXIST,RMS_FEX);
3675 else if (instr(s, "ile exists"))
3676 SETERRNO(EEXIST,RMS_FEX);
3677 else if (instr(s, "non-exist"))
3678 SETERRNO(ENOENT,RMS_FNF);
3679 else if (instr(s, "does not exist"))
3680 SETERRNO(ENOENT,RMS_FNF);
3681 else if (instr(s, "not empty"))
3682 SETERRNO(EBUSY,SS_DEVOFFLINE);
3683 else if (instr(s, "cannot access"))
3684 SETERRNO(EACCES,RMS_PRV);
3686 SETERRNO(EPERM,RMS_PRV);
3689 else { /* some mkdirs return no failure indication */
3690 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3691 if (PL_op->op_type == OP_RMDIR)
3696 SETERRNO(EACCES,RMS_PRV); /* a guess */
3705 /* This macro removes trailing slashes from a directory name.
3706 * Different operating and file systems take differently to
3707 * trailing slashes. According to POSIX 1003.1 1996 Edition
3708 * any number of trailing slashes should be allowed.
3709 * Thusly we snip them away so that even non-conforming
3710 * systems are happy.
3711 * We should probably do this "filtering" for all
3712 * the functions that expect (potentially) directory names:
3713 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3714 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3716 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3717 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3720 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3721 (tmps) = savepvn((tmps), (len)); \
3731 const int mode = (MAXARG > 1) ? POPi : 0777;
3733 TRIMSLASHES(tmps,len,copy);
3735 TAINT_PROPER("mkdir");
3737 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3741 SETi( dooneliner("mkdir", tmps) );
3742 oldumask = PerlLIO_umask(0);
3743 PerlLIO_umask(oldumask);
3744 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3759 TRIMSLASHES(tmps,len,copy);
3760 TAINT_PROPER("rmdir");
3762 SETi( PerlDir_rmdir(tmps) >= 0 );
3764 SETi( dooneliner("rmdir", tmps) );
3771 /* Directory calls. */
3775 #if defined(Direntry_t) && defined(HAS_READDIR)
3777 const char * const dirname = POPpconstx;
3778 GV * const gv = MUTABLE_GV(POPs);
3779 register IO * const io = GvIOn(gv);
3784 if ((IoIFP(io) || IoOFP(io)) && ckWARN2(WARN_IO, WARN_DEPRECATED))
3785 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3786 "Opening filehandle %s also as a directory", GvENAME(gv));
3788 PerlDir_close(IoDIRP(io));
3789 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3795 SETERRNO(EBADF,RMS_DIR);
3798 DIE(aTHX_ PL_no_dir_func, "opendir");
3804 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3805 DIE(aTHX_ PL_no_dir_func, "readdir");
3807 #if !defined(I_DIRENT) && !defined(VMS)
3808 Direntry_t *readdir (DIR *);
3814 const I32 gimme = GIMME;
3815 GV * const gv = MUTABLE_GV(POPs);
3816 register const Direntry_t *dp;
3817 register IO * const io = GvIOn(gv);
3819 if (!io || !IoDIRP(io)) {
3820 if(ckWARN(WARN_IO)) {
3821 Perl_warner(aTHX_ packWARN(WARN_IO),
3822 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3828 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3832 sv = newSVpvn(dp->d_name, dp->d_namlen);
3834 sv = newSVpv(dp->d_name, 0);
3836 #ifndef INCOMPLETE_TAINTS
3837 if (!(IoFLAGS(io) & IOf_UNTAINT))
3841 } while (gimme == G_ARRAY);
3843 if (!dp && gimme != G_ARRAY)
3850 SETERRNO(EBADF,RMS_ISI);
3851 if (GIMME == G_ARRAY)
3860 #if defined(HAS_TELLDIR) || defined(telldir)
3862 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3863 /* XXX netbsd still seemed to.
3864 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3865 --JHI 1999-Feb-02 */
3866 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3867 long telldir (DIR *);
3869 GV * const gv = MUTABLE_GV(POPs);
3870 register IO * const io = GvIOn(gv);
3872 if (!io || !IoDIRP(io)) {
3873 if(ckWARN(WARN_IO)) {
3874 Perl_warner(aTHX_ packWARN(WARN_IO),
3875 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3880 PUSHi( PerlDir_tell(IoDIRP(io)) );
3884 SETERRNO(EBADF,RMS_ISI);
3887 DIE(aTHX_ PL_no_dir_func, "telldir");
3893 #if defined(HAS_SEEKDIR) || defined(seekdir)
3895 const long along = POPl;
3896 GV * const gv = MUTABLE_GV(POPs);
3897 register IO * const io = GvIOn(gv);
3899 if (!io || !IoDIRP(io)) {
3900 if(ckWARN(WARN_IO)) {
3901 Perl_warner(aTHX_ packWARN(WARN_IO),
3902 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3906 (void)PerlDir_seek(IoDIRP(io), along);
3911 SETERRNO(EBADF,RMS_ISI);
3914 DIE(aTHX_ PL_no_dir_func, "seekdir");
3920 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3922 GV * const gv = MUTABLE_GV(POPs);
3923 register IO * const io = GvIOn(gv);
3925 if (!io || !IoDIRP(io)) {
3926 if(ckWARN(WARN_IO)) {
3927 Perl_warner(aTHX_ packWARN(WARN_IO),
3928 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3932 (void)PerlDir_rewind(IoDIRP(io));
3936 SETERRNO(EBADF,RMS_ISI);
3939 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3945 #if defined(Direntry_t) && defined(HAS_READDIR)
3947 GV * const gv = MUTABLE_GV(POPs);
3948 register IO * const io = GvIOn(gv);
3950 if (!io || !IoDIRP(io)) {
3951 if(ckWARN(WARN_IO)) {
3952 Perl_warner(aTHX_ packWARN(WARN_IO),
3953 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3957 #ifdef VOID_CLOSEDIR
3958 PerlDir_close(IoDIRP(io));
3960 if (PerlDir_close(IoDIRP(io)) < 0) {
3961 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3970 SETERRNO(EBADF,RMS_IFI);
3973 DIE(aTHX_ PL_no_dir_func, "closedir");
3977 /* Process control. */
3986 PERL_FLUSHALL_FOR_CHILD;
3987 childpid = PerlProc_fork();
3991 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
3993 SvREADONLY_off(GvSV(tmpgv));
3994 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3995 SvREADONLY_on(GvSV(tmpgv));
3997 #ifdef THREADS_HAVE_PIDS
3998 PL_ppid = (IV)getppid();
4000 #ifdef PERL_USES_PL_PIDSTATUS
4001 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4007 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4012 PERL_FLUSHALL_FOR_CHILD;
4013 childpid = PerlProc_fork();
4019 DIE(aTHX_ PL_no_func, "fork");
4026 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4031 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4032 childpid = wait4pid(-1, &argflags, 0);
4034 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4039 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4040 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4041 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4043 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4048 DIE(aTHX_ PL_no_func, "wait");
4054 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4056 const int optype = POPi;
4057 const Pid_t pid = TOPi;
4061 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4062 result = wait4pid(pid, &argflags, optype);
4064 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4069 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4070 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4071 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4073 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4078 DIE(aTHX_ PL_no_func, "waitpid");
4084 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4085 #if defined(__LIBCATAMOUNT__)
4086 PL_statusvalue = -1;
4095 while (++MARK <= SP) {
4096 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4101 TAINT_PROPER("system");
4103 PERL_FLUSHALL_FOR_CHILD;
4104 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4110 if (PerlProc_pipe(pp) >= 0)
4112 while ((childpid = PerlProc_fork()) == -1) {
4113 if (errno != EAGAIN) {
4118 PerlLIO_close(pp[0]);
4119 PerlLIO_close(pp[1]);
4126 Sigsave_t ihand,qhand; /* place to save signals during system() */
4130 PerlLIO_close(pp[1]);
4132 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4133 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4136 result = wait4pid(childpid, &status, 0);
4137 } while (result == -1 && errno == EINTR);
4139 (void)rsignal_restore(SIGINT, &ihand);
4140 (void)rsignal_restore(SIGQUIT, &qhand);
4142 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4143 do_execfree(); /* free any memory child malloced on fork */
4150 while (n < sizeof(int)) {
4151 n1 = PerlLIO_read(pp[0],
4152 (void*)(((char*)&errkid)+n),
4158 PerlLIO_close(pp[0]);
4159 if (n) { /* Error */
4160 if (n != sizeof(int))
4161 DIE(aTHX_ "panic: kid popen errno read");
4162 errno = errkid; /* Propagate errno from kid */
4163 STATUS_NATIVE_CHILD_SET(-1);
4166 XPUSHi(STATUS_CURRENT);
4170 PerlLIO_close(pp[0]);
4171 #if defined(HAS_FCNTL) && defined(F_SETFD)
4172 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4175 if (PL_op->op_flags & OPf_STACKED) {
4176 SV * const really = *++MARK;
4177 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4179 else if (SP - MARK != 1)
4180 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4182 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4186 #else /* ! FORK or VMS or OS/2 */
4189 if (PL_op->op_flags & OPf_STACKED) {
4190 SV * const really = *++MARK;
4191 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4192 value = (I32)do_aspawn(really, MARK, SP);
4194 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4197 else if (SP - MARK != 1) {
4198 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4199 value = (I32)do_aspawn(NULL, MARK, SP);
4201 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4205 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4207 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4209 STATUS_NATIVE_CHILD_SET(value);
4212 XPUSHi(result ? value : STATUS_CURRENT);
4213 #endif /* !FORK or VMS or OS/2 */
4220 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4225 while (++MARK <= SP) {
4226 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4231 TAINT_PROPER("exec");
4233 PERL_FLUSHALL_FOR_CHILD;
4234 if (PL_op->op_flags & OPf_STACKED) {
4235 SV * const really = *++MARK;
4236 value = (I32)do_aexec(really, MARK, SP);
4238 else if (SP - MARK != 1)
4240 value = (I32)vms_do_aexec(NULL, MARK, SP);
4244 (void ) do_aspawn(NULL, MARK, SP);
4248 value = (I32)do_aexec(NULL, MARK, SP);
4253 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4256 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4259 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4273 # ifdef THREADS_HAVE_PIDS
4274 if (PL_ppid != 1 && getppid() == 1)
4275 /* maybe the parent process has died. Refresh ppid cache */
4279 XPUSHi( getppid() );
4283 DIE(aTHX_ PL_no_func, "getppid");
4292 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4295 pgrp = (I32)BSD_GETPGRP(pid);
4297 if (pid != 0 && pid != PerlProc_getpid())
4298 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4304 DIE(aTHX_ PL_no_func, "getpgrp()");
4323 TAINT_PROPER("setpgrp");
4325 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4327 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4328 || (pid != 0 && pid != PerlProc_getpid()))
4330 DIE(aTHX_ "setpgrp can't take arguments");
4332 SETi( setpgrp() >= 0 );
4333 #endif /* USE_BSDPGRP */
4336 DIE(aTHX_ PL_no_func, "setpgrp()");
4342 #ifdef HAS_GETPRIORITY
4344 const int who = POPi;
4345 const int which = TOPi;
4346 SETi( getpriority(which, who) );
4349 DIE(aTHX_ PL_no_func, "getpriority()");
4355 #ifdef HAS_SETPRIORITY
4357 const int niceval = POPi;
4358 const int who = POPi;
4359 const int which = TOPi;
4360 TAINT_PROPER("setpriority");
4361 SETi( setpriority(which, who, niceval) >= 0 );
4364 DIE(aTHX_ PL_no_func, "setpriority()");
4374 XPUSHn( time(NULL) );
4376 XPUSHi( time(NULL) );
4388 (void)PerlProc_times(&PL_timesbuf);
4390 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4391 /* struct tms, though same data */
4395 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4396 if (GIMME == G_ARRAY) {
4397 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4398 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4399 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4407 if (GIMME == G_ARRAY) {
4414 DIE(aTHX_ "times not implemented");
4416 #endif /* HAS_TIMES */
4419 #ifdef LOCALTIME_EDGECASE_BROKEN
4420 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4425 /* No workarounds in the valid range */
4426 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4427 return (localtime (tp));
4429 /* This edge case is to workaround the undefined behaviour, where the
4430 * TIMEZONE makes the time go beyond the defined range.
4431 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4432 * If there is a negative offset in TZ, like MET-1METDST, some broken
4433 * implementations of localtime () (like AIX 5.2) barf with bogus
4435 * 0x7fffffff gmtime 2038-01-19 03:14:07
4436 * 0x7fffffff localtime 1901-12-13 21:45:51
4437 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4438 * 0x3c19137f gmtime 2001-12-13 20:45:51
4439 * 0x3c19137f localtime 2001-12-13 21:45:51
4440 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4441 * Given that legal timezones are typically between GMT-12 and GMT+12
4442 * we turn back the clock 23 hours before calling the localtime
4443 * function, and add those to the return value. This will never cause
4444 * day wrapping problems, since the edge case is Tue Jan *19*
4446 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4449 if (P->tm_hour >= 24) {
4451 P->tm_mday++; /* 18 -> 19 */
4452 P->tm_wday++; /* Mon -> Tue */
4453 P->tm_yday++; /* 18 -> 19 */
4456 } /* S_my_localtime */
4464 const struct tm *tmbuf;
4465 static const char * const dayname[] =
4466 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4467 static const char * const monname[] =
4468 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4469 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4475 when = (Time_t)SvNVx(POPs);
4477 when = (Time_t)SvIVx(POPs);
4480 if (PL_op->op_type == OP_LOCALTIME)
4481 #ifdef LOCALTIME_EDGECASE_BROKEN
4482 tmbuf = S_my_localtime(aTHX_ &when);
4484 tmbuf = localtime(&when);
4487 tmbuf = gmtime(&when);
4489 if (GIMME != G_ARRAY) {
4495 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4496 dayname[tmbuf->tm_wday],
4497 monname[tmbuf->tm_mon],
4502 tmbuf->tm_year + 1900);
4508 mPUSHi(tmbuf->tm_sec);
4509 mPUSHi(tmbuf->tm_min);
4510 mPUSHi(tmbuf->tm_hour);
4511 mPUSHi(tmbuf->tm_mday);
4512 mPUSHi(tmbuf->tm_mon);
4513 mPUSHi(tmbuf->tm_year);
4514 mPUSHi(tmbuf->tm_wday);
4515 mPUSHi(tmbuf->tm_yday);
4516 mPUSHi(tmbuf->tm_isdst);
4527 anum = alarm((unsigned int)anum);
4534 DIE(aTHX_ PL_no_func, "alarm");
4545 (void)time(&lasttime);
4550 PerlProc_sleep((unsigned int)duration);
4553 XPUSHi(when - lasttime);
4557 /* Shared memory. */
4558 /* Merged with some message passing. */
4562 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4563 dVAR; dSP; dMARK; dTARGET;
4564 const int op_type = PL_op->op_type;
4569 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4572 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4575 value = (I32)(do_semop(MARK, SP) >= 0);
4578 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4594 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4595 dVAR; dSP; dMARK; dTARGET;
4596 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4603 DIE(aTHX_ "System V IPC is not implemented on this machine");
4609 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4610 dVAR; dSP; dMARK; dTARGET;
4611 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4619 PUSHp(zero_but_true, ZBTLEN);
4627 /* I can't const this further without getting warnings about the types of
4628 various arrays passed in from structures. */
4630 S_space_join_names_mortal(pTHX_ char *const *array)
4634 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4636 if (array && *array) {
4637 target = newSVpvs_flags("", SVs_TEMP);
4639 sv_catpv(target, *array);
4642 sv_catpvs(target, " ");
4645 target = sv_mortalcopy(&PL_sv_no);
4650 /* Get system info. */
4654 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4656 I32 which = PL_op->op_type;
4657 register char **elem;
4659 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4660 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4661 struct hostent *gethostbyname(Netdb_name_t);
4662 struct hostent *gethostent(void);
4664 struct hostent *hent;
4668 if (which == OP_GHBYNAME) {
4669 #ifdef HAS_GETHOSTBYNAME
4670 const char* const name = POPpbytex;
4671 hent = PerlSock_gethostbyname(name);
4673 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4676 else if (which == OP_GHBYADDR) {
4677 #ifdef HAS_GETHOSTBYADDR
4678 const int addrtype = POPi;
4679 SV * const addrsv = POPs;
4681 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4683 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4685 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4689 #ifdef HAS_GETHOSTENT
4690 hent = PerlSock_gethostent();
4692 DIE(aTHX_ PL_no_sock_func, "gethostent");
4695 #ifdef HOST_NOT_FOUND
4697 #ifdef USE_REENTRANT_API
4698 # ifdef USE_GETHOSTENT_ERRNO
4699 h_errno = PL_reentrant_buffer->_gethostent_errno;
4702 STATUS_UNIX_SET(h_errno);
4706 if (GIMME != G_ARRAY) {
4707 PUSHs(sv = sv_newmortal());
4709 if (which == OP_GHBYNAME) {
4711 sv_setpvn(sv, hent->h_addr, hent->h_length);
4714 sv_setpv(sv, (char*)hent->h_name);
4720 mPUSHs(newSVpv((char*)hent->h_name, 0));
4721 PUSHs(space_join_names_mortal(hent->h_aliases));
4722 mPUSHi(hent->h_addrtype);
4723 len = hent->h_length;
4726 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4727 mXPUSHp(*elem, len);
4731 mPUSHp(hent->h_addr, len);
4733 PUSHs(sv_mortalcopy(&PL_sv_no));
4738 DIE(aTHX_ PL_no_sock_func, "gethostent");
4744 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4746 I32 which = PL_op->op_type;
4748 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4749 struct netent *getnetbyaddr(Netdb_net_t, int);
4750 struct netent *getnetbyname(Netdb_name_t);
4751 struct netent *getnetent(void);
4753 struct netent *nent;
4755 if (which == OP_GNBYNAME){
4756 #ifdef HAS_GETNETBYNAME
4757 const char * const name = POPpbytex;
4758 nent = PerlSock_getnetbyname(name);
4760 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4763 else if (which == OP_GNBYADDR) {
4764 #ifdef HAS_GETNETBYADDR
4765 const int addrtype = POPi;
4766 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4767 nent = PerlSock_getnetbyaddr(addr, addrtype);
4769 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4773 #ifdef HAS_GETNETENT
4774 nent = PerlSock_getnetent();
4776 DIE(aTHX_ PL_no_sock_func, "getnetent");
4779 #ifdef HOST_NOT_FOUND
4781 #ifdef USE_REENTRANT_API
4782 # ifdef USE_GETNETENT_ERRNO
4783 h_errno = PL_reentrant_buffer->_getnetent_errno;
4786 STATUS_UNIX_SET(h_errno);
4791 if (GIMME != G_ARRAY) {
4792 PUSHs(sv = sv_newmortal());
4794 if (which == OP_GNBYNAME)
4795 sv_setiv(sv, (IV)nent->n_net);
4797 sv_setpv(sv, nent->n_name);
4803 mPUSHs(newSVpv(nent->n_name, 0));
4804 PUSHs(space_join_names_mortal(nent->n_aliases));
4805 mPUSHi(nent->n_addrtype);
4806 mPUSHi(nent->n_net);
4811 DIE(aTHX_ PL_no_sock_func, "getnetent");
4817 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4819 I32 which = PL_op->op_type;
4821 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4822 struct protoent *getprotobyname(Netdb_name_t);
4823 struct protoent *getprotobynumber(int);
4824 struct protoent *getprotoent(void);
4826 struct protoent *pent;
4828 if (which == OP_GPBYNAME) {
4829 #ifdef HAS_GETPROTOBYNAME
4830 const char* const name = POPpbytex;
4831 pent = PerlSock_getprotobyname(name);
4833 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4836 else if (which == OP_GPBYNUMBER) {
4837 #ifdef HAS_GETPROTOBYNUMBER
4838 const int number = POPi;
4839 pent = PerlSock_getprotobynumber(number);
4841 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4845 #ifdef HAS_GETPROTOENT
4846 pent = PerlSock_getprotoent();
4848 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4852 if (GIMME != G_ARRAY) {
4853 PUSHs(sv = sv_newmortal());
4855 if (which == OP_GPBYNAME)
4856 sv_setiv(sv, (IV)pent->p_proto);
4858 sv_setpv(sv, pent->p_name);
4864 mPUSHs(newSVpv(pent->p_name, 0));
4865 PUSHs(space_join_names_mortal(pent->p_aliases));
4866 mPUSHi(pent->p_proto);
4871 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4877 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4879 I32 which = PL_op->op_type;
4881 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4882 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4883 struct servent *getservbyport(int, Netdb_name_t);
4884 struct servent *getservent(void);
4886 struct servent *sent;
4888 if (which == OP_GSBYNAME) {
4889 #ifdef HAS_GETSERVBYNAME
4890 const char * const proto = POPpbytex;
4891 const char * const name = POPpbytex;
4892 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4894 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4897 else if (which == OP_GSBYPORT) {
4898 #ifdef HAS_GETSERVBYPORT
4899 const char * const proto = POPpbytex;
4900 unsigned short port = (unsigned short)POPu;
4902 port = PerlSock_htons(port);
4904 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4906 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4910 #ifdef HAS_GETSERVENT
4911 sent = PerlSock_getservent();
4913 DIE(aTHX_ PL_no_sock_func, "getservent");
4917 if (GIMME != G_ARRAY) {
4918 PUSHs(sv = sv_newmortal());
4920 if (which == OP_GSBYNAME) {
4922 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4924 sv_setiv(sv, (IV)(sent->s_port));
4928 sv_setpv(sv, sent->s_name);
4934 mPUSHs(newSVpv(sent->s_name, 0));
4935 PUSHs(space_join_names_mortal(sent->s_aliases));
4937 mPUSHi(PerlSock_ntohs(sent->s_port));
4939 mPUSHi(sent->s_port);
4941 mPUSHs(newSVpv(sent->s_proto, 0));
4946 DIE(aTHX_ PL_no_sock_func, "getservent");
4952 #ifdef HAS_SETHOSTENT
4954 PerlSock_sethostent(TOPi);
4957 DIE(aTHX_ PL_no_sock_func, "sethostent");
4963 #ifdef HAS_SETNETENT
4965 (void)PerlSock_setnetent(TOPi);
4968 DIE(aTHX_ PL_no_sock_func, "setnetent");
4974 #ifdef HAS_SETPROTOENT
4976 (void)PerlSock_setprotoent(TOPi);
4979 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4985 #ifdef HAS_SETSERVENT
4987 (void)PerlSock_setservent(TOPi);
4990 DIE(aTHX_ PL_no_sock_func, "setservent");
4996 #ifdef HAS_ENDHOSTENT
4998 PerlSock_endhostent();
5002 DIE(aTHX_ PL_no_sock_func, "endhostent");
5008 #ifdef HAS_ENDNETENT
5010 PerlSock_endnetent();
5014 DIE(aTHX_ PL_no_sock_func, "endnetent");
5020 #ifdef HAS_ENDPROTOENT
5022 PerlSock_endprotoent();
5026 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5032 #ifdef HAS_ENDSERVENT
5034 PerlSock_endservent();
5038 DIE(aTHX_ PL_no_sock_func, "endservent");
5046 I32 which = PL_op->op_type;
5048 struct passwd *pwent = NULL;
5050 * We currently support only the SysV getsp* shadow password interface.
5051 * The interface is declared in <shadow.h> and often one needs to link
5052 * with -lsecurity or some such.
5053 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5056 * AIX getpwnam() is clever enough to return the encrypted password
5057 * only if the caller (euid?) is root.
5059 * There are at least three other shadow password APIs. Many platforms
5060 * seem to contain more than one interface for accessing the shadow
5061 * password databases, possibly for compatibility reasons.
5062 * The getsp*() is by far he simplest one, the other two interfaces
5063 * are much more complicated, but also very similar to each other.
5068 * struct pr_passwd *getprpw*();
5069 * The password is in
5070 * char getprpw*(...).ufld.fd_encrypt[]
5071 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5076 * struct es_passwd *getespw*();
5077 * The password is in
5078 * char *(getespw*(...).ufld.fd_encrypt)
5079 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5082 * struct userpw *getuserpw();
5083 * The password is in
5084 * char *(getuserpw(...)).spw_upw_passwd
5085 * (but the de facto standard getpwnam() should work okay)
5087 * Mention I_PROT here so that Configure probes for it.
5089 * In HP-UX for getprpw*() the manual page claims that one should include
5090 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5091 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5092 * and pp_sys.c already includes <shadow.h> if there is such.
5094 * Note that <sys/security.h> is already probed for, but currently
5095 * it is only included in special cases.
5097 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5098 * be preferred interface, even though also the getprpw*() interface
5099 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5100 * One also needs to call set_auth_parameters() in main() before
5101 * doing anything else, whether one is using getespw*() or getprpw*().
5103 * Note that accessing the shadow databases can be magnitudes
5104 * slower than accessing the standard databases.
5109 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5110 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5111 * the pw_comment is left uninitialized. */
5112 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5118 const char* const name = POPpbytex;
5119 pwent = getpwnam(name);
5125 pwent = getpwuid(uid);
5129 # ifdef HAS_GETPWENT
5131 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5132 if (pwent) pwent = getpwnam(pwent->pw_name);
5135 DIE(aTHX_ PL_no_func, "getpwent");
5141 if (GIMME != G_ARRAY) {
5142 PUSHs(sv = sv_newmortal());
5144 if (which == OP_GPWNAM)
5145 # if Uid_t_sign <= 0
5146 sv_setiv(sv, (IV)pwent->pw_uid);
5148 sv_setuv(sv, (UV)pwent->pw_uid);
5151 sv_setpv(sv, pwent->pw_name);
5157 mPUSHs(newSVpv(pwent->pw_name, 0));
5161 /* If we have getspnam(), we try to dig up the shadow
5162 * password. If we are underprivileged, the shadow
5163 * interface will set the errno to EACCES or similar,
5164 * and return a null pointer. If this happens, we will
5165 * use the dummy password (usually "*" or "x") from the
5166 * standard password database.
5168 * In theory we could skip the shadow call completely
5169 * if euid != 0 but in practice we cannot know which
5170 * security measures are guarding the shadow databases
5171 * on a random platform.
5173 * Resist the urge to use additional shadow interfaces.
5174 * Divert the urge to writing an extension instead.
5177 /* Some AIX setups falsely(?) detect some getspnam(), which
5178 * has a different API than the Solaris/IRIX one. */
5179 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5181 const int saverrno = errno;
5182 const struct spwd * const spwent = getspnam(pwent->pw_name);
5183 /* Save and restore errno so that
5184 * underprivileged attempts seem
5185 * to have never made the unsccessful
5186 * attempt to retrieve the shadow password. */
5188 if (spwent && spwent->sp_pwdp)
5189 sv_setpv(sv, spwent->sp_pwdp);
5193 if (!SvPOK(sv)) /* Use the standard password, then. */
5194 sv_setpv(sv, pwent->pw_passwd);
5197 # ifndef INCOMPLETE_TAINTS
5198 /* passwd is tainted because user himself can diddle with it.
5199 * admittedly not much and in a very limited way, but nevertheless. */
5203 # if Uid_t_sign <= 0
5204 mPUSHi(pwent->pw_uid);
5206 mPUSHu(pwent->pw_uid);
5209 # if Uid_t_sign <= 0
5210 mPUSHi(pwent->pw_gid);
5212 mPUSHu(pwent->pw_gid);
5214 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5215 * because of the poor interface of the Perl getpw*(),
5216 * not because there's some standard/convention saying so.
5217 * A better interface would have been to return a hash,
5218 * but we are accursed by our history, alas. --jhi. */
5220 mPUSHi(pwent->pw_change);
5223 mPUSHi(pwent->pw_quota);
5226 mPUSHs(newSVpv(pwent->pw_age, 0));
5228 /* I think that you can never get this compiled, but just in case. */
5229 PUSHs(sv_mortalcopy(&PL_sv_no));
5234 /* pw_class and pw_comment are mutually exclusive--.
5235 * see the above note for pw_change, pw_quota, and pw_age. */
5237 mPUSHs(newSVpv(pwent->pw_class, 0));
5240 mPUSHs(newSVpv(pwent->pw_comment, 0));
5242 /* I think that you can never get this compiled, but just in case. */
5243 PUSHs(sv_mortalcopy(&PL_sv_no));
5248 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5250 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5252 # ifndef INCOMPLETE_TAINTS
5253 /* pw_gecos is tainted because user himself can diddle with it. */
5257 mPUSHs(newSVpv(pwent->pw_dir, 0));
5259 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5260 # ifndef INCOMPLETE_TAINTS
5261 /* pw_shell is tainted because user himself can diddle with it. */
5266 mPUSHi(pwent->pw_expire);
5271 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5277 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5282 DIE(aTHX_ PL_no_func, "setpwent");
5288 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5293 DIE(aTHX_ PL_no_func, "endpwent");
5301 const I32 which = PL_op->op_type;
5302 const struct group *grent;
5304 if (which == OP_GGRNAM) {
5305 const char* const name = POPpbytex;
5306 grent = (const struct group *)getgrnam(name);
5308 else if (which == OP_GGRGID) {
5309 const Gid_t gid = POPi;
5310 grent = (const struct group *)getgrgid(gid);
5314 grent = (struct group *)getgrent();
5316 DIE(aTHX_ PL_no_func, "getgrent");
5320 if (GIMME != G_ARRAY) {
5321 SV * const sv = sv_newmortal();
5325 if (which == OP_GGRNAM)
5326 sv_setiv(sv, (IV)grent->gr_gid);
5328 sv_setpv(sv, grent->gr_name);
5334 mPUSHs(newSVpv(grent->gr_name, 0));
5337 mPUSHs(newSVpv(grent->gr_passwd, 0));
5339 PUSHs(sv_mortalcopy(&PL_sv_no));
5342 mPUSHi(grent->gr_gid);
5344 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5345 /* In UNICOS/mk (_CRAYMPP) the multithreading
5346 * versions (getgrnam_r, getgrgid_r)
5347 * seem to return an illegal pointer
5348 * as the group members list, gr_mem.
5349 * getgrent() doesn't even have a _r version
5350 * but the gr_mem is poisonous anyway.
5351 * So yes, you cannot get the list of group
5352 * members if building multithreaded in UNICOS/mk. */
5353 PUSHs(space_join_names_mortal(grent->gr_mem));
5359 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5365 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5370 DIE(aTHX_ PL_no_func, "setgrent");
5376 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5381 DIE(aTHX_ PL_no_func, "endgrent");
5391 if (!(tmps = PerlProc_getlogin()))
5393 PUSHp(tmps, strlen(tmps));
5396 DIE(aTHX_ PL_no_func, "getlogin");
5400 /* Miscellaneous. */
5405 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5406 register I32 items = SP - MARK;
5407 unsigned long a[20];
5412 while (++MARK <= SP) {
5413 if (SvTAINTED(*MARK)) {
5419 TAINT_PROPER("syscall");
5422 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5423 * or where sizeof(long) != sizeof(char*). But such machines will
5424 * not likely have syscall implemented either, so who cares?
5426 while (++MARK <= SP) {
5427 if (SvNIOK(*MARK) || !i)
5428 a[i++] = SvIV(*MARK);
5429 else if (*MARK == &PL_sv_undef)
5432 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5438 DIE(aTHX_ "Too many args to syscall");
5440 DIE(aTHX_ "Too few args to syscall");
5442 retval = syscall(a[0]);
5445 retval = syscall(a[0],a[1]);
5448 retval = syscall(a[0],a[1],a[2]);
5451 retval = syscall(a[0],a[1],a[2],a[3]);
5454 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5457 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5460 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5463 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5467 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5470 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5473 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5477 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5481 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5485 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5486 a[10],a[11],a[12],a[13]);
5488 #endif /* atarist */
5494 DIE(aTHX_ PL_no_func, "syscall");
5498 #ifdef FCNTL_EMULATE_FLOCK
5500 /* XXX Emulate flock() with fcntl().
5501 What's really needed is a good file locking module.
5505 fcntl_emulate_flock(int fd, int operation)
5509 switch (operation & ~LOCK_NB) {
5511 flock.l_type = F_RDLCK;
5514 flock.l_type = F_WRLCK;
5517 flock.l_type = F_UNLCK;
5523 flock.l_whence = SEEK_SET;
5524 flock.l_start = flock.l_len = (Off_t)0;
5526 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5529 #endif /* FCNTL_EMULATE_FLOCK */
5531 #ifdef LOCKF_EMULATE_FLOCK
5533 /* XXX Emulate flock() with lockf(). This is just to increase
5534 portability of scripts. The calls are not completely
5535 interchangeable. What's really needed is a good file
5539 /* The lockf() constants might have been defined in <unistd.h>.
5540 Unfortunately, <unistd.h> causes troubles on some mixed
5541 (BSD/POSIX) systems, such as SunOS 4.1.3.
5543 Further, the lockf() constants aren't POSIX, so they might not be
5544 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5545 just stick in the SVID values and be done with it. Sigh.
5549 # define F_ULOCK 0 /* Unlock a previously locked region */
5552 # define F_LOCK 1 /* Lock a region for exclusive use */
5555 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5558 # define F_TEST 3 /* Test a region for other processes locks */
5562 lockf_emulate_flock(int fd, int operation)
5565 const int save_errno = errno;
5568 /* flock locks entire file so for lockf we need to do the same */
5569 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5570 if (pos > 0) /* is seekable and needs to be repositioned */
5571 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5572 pos = -1; /* seek failed, so don't seek back afterwards */
5575 switch (operation) {
5577 /* LOCK_SH - get a shared lock */
5579 /* LOCK_EX - get an exclusive lock */
5581 i = lockf (fd, F_LOCK, 0);
5584 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5585 case LOCK_SH|LOCK_NB:
5586 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5587 case LOCK_EX|LOCK_NB:
5588 i = lockf (fd, F_TLOCK, 0);
5590 if ((errno == EAGAIN) || (errno == EACCES))
5591 errno = EWOULDBLOCK;
5594 /* LOCK_UN - unlock (non-blocking is a no-op) */
5596 case LOCK_UN|LOCK_NB:
5597 i = lockf (fd, F_ULOCK, 0);
5600 /* Default - can't decipher operation */
5607 if (pos > 0) /* need to restore position of the handle */
5608 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5613 #endif /* LOCKF_EMULATE_FLOCK */
5617 * c-indentation-style: bsd
5619 * indent-tabs-mode: t
5622 * ex: set ts=8 sts=4 sw=4 noet: