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 Perl_setdefout(pTHX_ GV *gv)
1163 SvREFCNT_inc_simple_void(gv);
1165 SvREFCNT_dec(PL_defoutgv);
1173 GV * const newdefout = (PL_op->op_private > 0) ? (MUTABLE_GV(POPs)) : NULL;
1174 GV * egv = GvEGV(PL_defoutgv);
1180 XPUSHs(&PL_sv_undef);
1182 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1183 if (gvp && *gvp == egv) {
1184 gv_efullname4(TARG, PL_defoutgv, NULL, TRUE);
1188 mXPUSHs(newRV(MUTABLE_SV(egv)));
1193 if (!GvIO(newdefout))
1194 gv_IOadd(newdefout);
1195 setdefout(newdefout);
1205 GV * const gv = (MAXARG==0) ? PL_stdingv : MUTABLE_GV(POPs);
1207 if (gv && (io = GvIO(gv))) {
1208 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1210 const I32 gimme = GIMME_V;
1212 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
1215 call_method("GETC", gimme);
1218 if (gimme == G_SCALAR)
1219 SvSetMagicSV_nosteal(TARG, TOPs);
1223 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1224 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1225 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1226 report_evil_fh(gv, io, PL_op->op_type);
1227 SETERRNO(EBADF,RMS_IFI);
1231 sv_setpvs(TARG, " ");
1232 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1233 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1234 /* Find out how many bytes the char needs */
1235 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1238 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1239 SvCUR_set(TARG,1+len);
1248 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1251 register PERL_CONTEXT *cx;
1252 const I32 gimme = GIMME_V;
1254 PERL_ARGS_ASSERT_DOFORM;
1259 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1260 PUSHFORMAT(cx, retop);
1262 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1264 setdefout(gv); /* locally select filehandle so $% et al work */
1281 gv = MUTABLE_GV(POPs);
1296 goto not_a_format_reference;
1301 tmpsv = sv_newmortal();
1302 gv_efullname4(tmpsv, fgv, NULL, FALSE);
1303 name = SvPV_nolen_const(tmpsv);
1305 DIE(aTHX_ "Undefined format \"%s\" called", name);
1307 not_a_format_reference:
1308 DIE(aTHX_ "Not a format reference");
1311 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1313 IoFLAGS(io) &= ~IOf_DIDTOP;
1314 return doform(cv,gv,PL_op->op_next);
1320 GV * const gv = cxstack[cxstack_ix].blk_format.gv;
1321 register IO * const io = GvIOp(gv);
1326 register PERL_CONTEXT *cx;
1328 if (!io || !(ofp = IoOFP(io)))
1331 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1332 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1334 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1335 PL_formtarget != PL_toptarget)
1339 if (!IoTOP_GV(io)) {
1342 if (!IoTOP_NAME(io)) {
1344 if (!IoFMT_NAME(io))
1345 IoFMT_NAME(io) = savepv(GvNAME(gv));
1346 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1347 topgv = gv_fetchsv(topname, 0, SVt_PVFM);
1348 if ((topgv && GvFORM(topgv)) ||
1349 !gv_fetchpvs("top", GV_NOTQUAL, SVt_PVFM))
1350 IoTOP_NAME(io) = savesvpv(topname);
1352 IoTOP_NAME(io) = savepvs("top");
1354 topgv = gv_fetchpv(IoTOP_NAME(io), 0, SVt_PVFM);
1355 if (!topgv || !GvFORM(topgv)) {
1356 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1359 IoTOP_GV(io) = topgv;
1361 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1362 I32 lines = IoLINES_LEFT(io);
1363 const char *s = SvPVX_const(PL_formtarget);
1364 if (lines <= 0) /* Yow, header didn't even fit!!! */
1366 while (lines-- > 0) {
1367 s = strchr(s, '\n');
1373 const STRLEN save = SvCUR(PL_formtarget);
1374 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1375 do_print(PL_formtarget, ofp);
1376 SvCUR_set(PL_formtarget, save);
1377 sv_chop(PL_formtarget, s);
1378 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1381 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1382 do_print(PL_formfeed, ofp);
1383 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1385 PL_formtarget = PL_toptarget;
1386 IoFLAGS(io) |= IOf_DIDTOP;
1389 DIE(aTHX_ "bad top format reference");
1392 SV * const sv = sv_newmortal();
1394 gv_efullname4(sv, fgv, NULL, FALSE);
1395 name = SvPV_nolen_const(sv);
1397 DIE(aTHX_ "Undefined top format \"%s\" called", name);
1399 DIE(aTHX_ "Undefined top format called");
1401 if (cv && CvCLONE(cv))
1402 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
1403 return doform(cv, gv, PL_op);
1407 POPBLOCK(cx,PL_curpm);
1413 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1415 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1416 else if (ckWARN(WARN_CLOSED))
1417 report_evil_fh(gv, io, PL_op->op_type);
1422 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1423 if (ckWARN(WARN_IO))
1424 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1426 if (!do_print(PL_formtarget, fp))
1429 FmLINES(PL_formtarget) = 0;
1430 SvCUR_set(PL_formtarget, 0);
1431 *SvEND(PL_formtarget) = '\0';
1432 if (IoFLAGS(io) & IOf_FLUSH)
1433 (void)PerlIO_flush(fp);
1438 PL_formtarget = PL_bodytarget;
1440 PERL_UNUSED_VAR(newsp);
1441 PERL_UNUSED_VAR(gimme);
1442 return cx->blk_sub.retop;
1447 dVAR; dSP; dMARK; dORIGMARK;
1453 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1455 if (gv && (io = GvIO(gv))) {
1456 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1458 if (MARK == ORIGMARK) {
1461 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1465 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1468 call_method("PRINTF", G_SCALAR);
1471 MARK = ORIGMARK + 1;
1479 if (!(io = GvIO(gv))) {
1480 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1481 report_evil_fh(gv, io, PL_op->op_type);
1482 SETERRNO(EBADF,RMS_IFI);
1485 else if (!(fp = IoOFP(io))) {
1486 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1488 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1489 else if (ckWARN(WARN_CLOSED))
1490 report_evil_fh(gv, io, PL_op->op_type);
1492 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1496 if (SvTAINTED(MARK[1]))
1497 TAINT_PROPER("printf");
1498 do_sprintf(sv, SP - MARK, MARK + 1);
1499 if (!do_print(sv, fp))
1502 if (IoFLAGS(io) & IOf_FLUSH)
1503 if (PerlIO_flush(fp) == EOF)
1514 PUSHs(&PL_sv_undef);
1522 const int perm = (MAXARG > 3) ? POPi : 0666;
1523 const int mode = POPi;
1524 SV * const sv = POPs;
1525 GV * const gv = MUTABLE_GV(POPs);
1528 /* Need TIEHANDLE method ? */
1529 const char * const tmps = SvPV_const(sv, len);
1530 /* FIXME? do_open should do const */
1531 if (do_open(gv, tmps, len, TRUE, mode, perm, NULL)) {
1532 IoLINES(GvIOp(gv)) = 0;
1536 PUSHs(&PL_sv_undef);
1543 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1549 Sock_size_t bufsize;
1557 bool charstart = FALSE;
1558 STRLEN charskip = 0;
1561 GV * const gv = MUTABLE_GV(*++MARK);
1562 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1563 && gv && (io = GvIO(gv)) )
1565 const MAGIC * mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1569 *MARK = SvTIED_obj(MUTABLE_SV(io), mg);
1571 call_method("READ", G_SCALAR);
1585 sv_setpvs(bufsv, "");
1586 length = SvIVx(*++MARK);
1589 offset = SvIVx(*++MARK);
1593 if (!io || !IoIFP(io)) {
1594 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1595 report_evil_fh(gv, io, PL_op->op_type);
1596 SETERRNO(EBADF,RMS_IFI);
1599 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1600 buffer = SvPVutf8_force(bufsv, blen);
1601 /* UTF-8 may not have been set if they are all low bytes */
1606 buffer = SvPV_force(bufsv, blen);
1607 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1610 DIE(aTHX_ "Negative length");
1618 if (PL_op->op_type == OP_RECV) {
1619 char namebuf[MAXPATHLEN];
1620 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1621 bufsize = sizeof (struct sockaddr_in);
1623 bufsize = sizeof namebuf;
1625 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1629 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1630 /* 'offset' means 'flags' here */
1631 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1632 (struct sockaddr *)namebuf, &bufsize);
1636 /* Bogus return without padding */
1637 bufsize = sizeof (struct sockaddr_in);
1639 SvCUR_set(bufsv, count);
1640 *SvEND(bufsv) = '\0';
1641 (void)SvPOK_only(bufsv);
1645 /* This should not be marked tainted if the fp is marked clean */
1646 if (!(IoFLAGS(io) & IOf_UNTAINT))
1647 SvTAINTED_on(bufsv);
1649 sv_setpvn(TARG, namebuf, bufsize);
1654 if (PL_op->op_type == OP_RECV)
1655 DIE(aTHX_ PL_no_sock_func, "recv");
1657 if (DO_UTF8(bufsv)) {
1658 /* offset adjust in characters not bytes */
1659 blen = sv_len_utf8(bufsv);
1662 if (-offset > (int)blen)
1663 DIE(aTHX_ "Offset outside string");
1666 if (DO_UTF8(bufsv)) {
1667 /* convert offset-as-chars to offset-as-bytes */
1668 if (offset >= (int)blen)
1669 offset += SvCUR(bufsv) - blen;
1671 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1674 bufsize = SvCUR(bufsv);
1675 /* Allocating length + offset + 1 isn't perfect in the case of reading
1676 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1678 (should be 2 * length + offset + 1, or possibly something longer if
1679 PL_encoding is true) */
1680 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1681 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1682 Zero(buffer+bufsize, offset-bufsize, char);
1684 buffer = buffer + offset;
1686 read_target = bufsv;
1688 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1689 concatenate it to the current buffer. */
1691 /* Truncate the existing buffer to the start of where we will be
1693 SvCUR_set(bufsv, offset);
1695 read_target = sv_newmortal();
1696 SvUPGRADE(read_target, SVt_PV);
1697 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1700 if (PL_op->op_type == OP_SYSREAD) {
1701 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1702 if (IoTYPE(io) == IoTYPE_SOCKET) {
1703 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1709 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1714 #ifdef HAS_SOCKET__bad_code_maybe
1715 if (IoTYPE(io) == IoTYPE_SOCKET) {
1716 char namebuf[MAXPATHLEN];
1717 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1718 bufsize = sizeof (struct sockaddr_in);
1720 bufsize = sizeof namebuf;
1722 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1723 (struct sockaddr *)namebuf, &bufsize);
1728 count = PerlIO_read(IoIFP(io), buffer, length);
1729 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1730 if (count == 0 && PerlIO_error(IoIFP(io)))
1734 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1735 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1738 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1739 *SvEND(read_target) = '\0';
1740 (void)SvPOK_only(read_target);
1741 if (fp_utf8 && !IN_BYTES) {
1742 /* Look at utf8 we got back and count the characters */
1743 const char *bend = buffer + count;
1744 while (buffer < bend) {
1746 skip = UTF8SKIP(buffer);
1749 if (buffer - charskip + skip > bend) {
1750 /* partial character - try for rest of it */
1751 length = skip - (bend-buffer);
1752 offset = bend - SvPVX_const(bufsv);
1764 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1765 provided amount read (count) was what was requested (length)
1767 if (got < wanted && count == length) {
1768 length = wanted - got;
1769 offset = bend - SvPVX_const(bufsv);
1772 /* return value is character count */
1776 else if (buffer_utf8) {
1777 /* Let svcatsv upgrade the bytes we read in to utf8.
1778 The buffer is a mortal so will be freed soon. */
1779 sv_catsv_nomg(bufsv, read_target);
1782 /* This should not be marked tainted if the fp is marked clean */
1783 if (!(IoFLAGS(io) & IOf_UNTAINT))
1784 SvTAINTED_on(bufsv);
1796 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1802 STRLEN orig_blen_bytes;
1803 const int op_type = PL_op->op_type;
1807 GV *const gv = MUTABLE_GV(*++MARK);
1808 if (PL_op->op_type == OP_SYSWRITE
1809 && gv && (io = GvIO(gv))) {
1810 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
1814 if (MARK == SP - 1) {
1816 sv = sv_2mortal(newSViv(sv_len(*SP)));
1822 *(ORIGMARK+1) = SvTIED_obj(MUTABLE_SV(io), mg);
1824 call_method("WRITE", G_SCALAR);
1840 if (!io || !IoIFP(io) || IoTYPE(io) == IoTYPE_RDONLY) {
1842 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
1843 if (io && IoIFP(io))
1844 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1846 report_evil_fh(gv, io, PL_op->op_type);
1848 SETERRNO(EBADF,RMS_IFI);
1852 /* Do this first to trigger any overloading. */
1853 buffer = SvPV_const(bufsv, blen);
1854 orig_blen_bytes = blen;
1855 doing_utf8 = DO_UTF8(bufsv);
1857 if (PerlIO_isutf8(IoIFP(io))) {
1858 if (!SvUTF8(bufsv)) {
1859 /* We don't modify the original scalar. */
1860 tmpbuf = bytes_to_utf8((const U8*) buffer, &blen);
1861 buffer = (char *) tmpbuf;
1865 else if (doing_utf8) {
1866 STRLEN tmplen = blen;
1867 U8 * const result = bytes_from_utf8((const U8*) buffer, &tmplen, &doing_utf8);
1870 buffer = (char *) tmpbuf;
1874 assert((char *)result == buffer);
1875 Perl_croak(aTHX_ "Wide character in %s", OP_DESC(PL_op));
1879 if (op_type == OP_SYSWRITE) {
1880 Size_t length = 0; /* This length is in characters. */
1886 /* The SV is bytes, and we've had to upgrade it. */
1887 blen_chars = orig_blen_bytes;
1889 /* The SV really is UTF-8. */
1890 if (SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1891 /* Don't call sv_len_utf8 again because it will call magic
1892 or overloading a second time, and we might get back a
1893 different result. */
1894 blen_chars = utf8_length((U8*)buffer, (U8*)buffer + blen);
1896 /* It's safe, and it may well be cached. */
1897 blen_chars = sv_len_utf8(bufsv);
1905 length = blen_chars;
1907 #if Size_t_size > IVSIZE
1908 length = (Size_t)SvNVx(*++MARK);
1910 length = (Size_t)SvIVx(*++MARK);
1912 if ((SSize_t)length < 0) {
1914 DIE(aTHX_ "Negative length");
1919 offset = SvIVx(*++MARK);
1921 if (-offset > (IV)blen_chars) {
1923 DIE(aTHX_ "Offset outside string");
1925 offset += blen_chars;
1926 } else if (offset >= (IV)blen_chars && blen_chars > 0) {
1928 DIE(aTHX_ "Offset outside string");
1932 if (length > blen_chars - offset)
1933 length = blen_chars - offset;
1935 /* Here we convert length from characters to bytes. */
1936 if (tmpbuf || SvGMAGICAL(bufsv) || SvAMAGIC(bufsv)) {
1937 /* Either we had to convert the SV, or the SV is magical, or
1938 the SV has overloading, in which case we can't or mustn't
1939 or mustn't call it again. */
1941 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1942 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1944 /* It's a real UTF-8 SV, and it's not going to change under
1945 us. Take advantage of any cache. */
1947 I32 len_I32 = length;
1949 /* Convert the start and end character positions to bytes.
1950 Remember that the second argument to sv_pos_u2b is relative
1952 sv_pos_u2b(bufsv, &start, &len_I32);
1959 buffer = buffer+offset;
1961 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1962 if (IoTYPE(io) == IoTYPE_SOCKET) {
1963 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1969 /* See the note at doio.c:do_print about filesize limits. --jhi */
1970 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1976 const int flags = SvIVx(*++MARK);
1979 char * const sockbuf = SvPVx(*++MARK, mlen);
1980 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1981 flags, (struct sockaddr *)sockbuf, mlen);
1985 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1990 DIE(aTHX_ PL_no_sock_func, "send");
1997 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
2000 #if Size_t_size > IVSIZE
2019 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
2021 gv = PL_last_in_gv = GvEGV(PL_argvgv);
2023 if (io && !IoIFP(io)) {
2024 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
2026 IoFLAGS(io) &= ~IOf_START;
2027 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, NULL);
2029 sv_setpvs(GvSV(gv), "-");
2032 GvSV(gv) = newSVpvs("-");
2034 SvSETMAGIC(GvSV(gv));
2036 else if (!nextargv(gv))
2041 gv = PL_last_in_gv; /* eof */
2044 gv = PL_last_in_gv = MUTABLE_GV(POPs); /* eof(FH) */
2047 IO * const io = GvIO(gv);
2049 if (io && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) {
2051 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2054 call_method("EOF", G_SCALAR);
2061 PUSHs(boolSV(!gv || do_eof(gv)));
2072 PL_last_in_gv = MUTABLE_GV(POPs);
2075 if (gv && (io = GvIO(gv))) {
2076 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2079 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2082 call_method("TELL", G_SCALAR);
2089 #if LSEEKSIZE > IVSIZE
2090 PUSHn( do_tell(gv) );
2092 PUSHi( do_tell(gv) );
2100 const int whence = POPi;
2101 #if LSEEKSIZE > IVSIZE
2102 const Off_t offset = (Off_t)SvNVx(POPs);
2104 const Off_t offset = (Off_t)SvIVx(POPs);
2107 GV * const gv = PL_last_in_gv = MUTABLE_GV(POPs);
2110 if (gv && (io = GvIO(gv))) {
2111 MAGIC * const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
2114 XPUSHs(SvTIED_obj(MUTABLE_SV(io), mg));
2115 #if LSEEKSIZE > IVSIZE
2116 mXPUSHn((NV) offset);
2123 call_method("SEEK", G_SCALAR);
2130 if (PL_op->op_type == OP_SEEK)
2131 PUSHs(boolSV(do_seek(gv, offset, whence)));
2133 const Off_t sought = do_sysseek(gv, offset, whence);
2135 PUSHs(&PL_sv_undef);
2137 SV* const sv = sought ?
2138 #if LSEEKSIZE > IVSIZE
2143 : newSVpvn(zero_but_true, ZBTLEN);
2154 /* There seems to be no consensus on the length type of truncate()
2155 * and ftruncate(), both off_t and size_t have supporters. In
2156 * general one would think that when using large files, off_t is
2157 * at least as wide as size_t, so using an off_t should be okay. */
2158 /* XXX Configure probe for the length type of *truncate() needed XXX */
2161 #if Off_t_size > IVSIZE
2166 /* Checking for length < 0 is problematic as the type might or
2167 * might not be signed: if it is not, clever compilers will moan. */
2168 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2175 if (PL_op->op_flags & OPf_SPECIAL) {
2176 tmpgv = gv_fetchsv(POPs, 0, SVt_PVIO);
2185 TAINT_PROPER("truncate");
2186 if (!(fp = IoIFP(io))) {
2192 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2194 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2201 SV * const sv = POPs;
2204 if (isGV_with_GP(sv)) {
2205 tmpgv = MUTABLE_GV(sv); /* *main::FRED for example */
2206 goto do_ftruncate_gv;
2208 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2209 tmpgv = MUTABLE_GV(SvRV(sv)); /* \*main::FRED for example */
2210 goto do_ftruncate_gv;
2212 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2213 io = MUTABLE_IO(SvRV(sv)); /* *main::FRED{IO} for example */
2214 goto do_ftruncate_io;
2217 name = SvPV_nolen_const(sv);
2218 TAINT_PROPER("truncate");
2220 if (truncate(name, len) < 0)
2224 const int tmpfd = PerlLIO_open(name, O_RDWR);
2229 if (my_chsize(tmpfd, len) < 0)
2231 PerlLIO_close(tmpfd);
2240 SETERRNO(EBADF,RMS_IFI);
2248 SV * const argsv = POPs;
2249 const unsigned int func = POPu;
2250 const int optype = PL_op->op_type;
2251 GV * const gv = MUTABLE_GV(POPs);
2252 IO * const io = gv ? GvIOn(gv) : NULL;
2256 if (!io || !argsv || !IoIFP(io)) {
2257 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2258 report_evil_fh(gv, io, PL_op->op_type);
2259 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2263 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2266 s = SvPV_force(argsv, len);
2267 need = IOCPARM_LEN(func);
2269 s = Sv_Grow(argsv, need + 1);
2270 SvCUR_set(argsv, need);
2273 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2276 retval = SvIV(argsv);
2277 s = INT2PTR(char*,retval); /* ouch */
2280 TAINT_PROPER(PL_op_desc[optype]);
2282 if (optype == OP_IOCTL)
2284 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2286 DIE(aTHX_ "ioctl is not implemented");
2290 DIE(aTHX_ "fcntl is not implemented");
2292 #if defined(OS2) && defined(__EMX__)
2293 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2295 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2299 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2301 if (s[SvCUR(argsv)] != 17)
2302 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2304 s[SvCUR(argsv)] = 0; /* put our null back */
2305 SvSETMAGIC(argsv); /* Assume it has changed */
2314 PUSHp(zero_but_true, ZBTLEN);
2327 const int argtype = POPi;
2328 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : MUTABLE_GV(POPs);
2330 if (gv && (io = GvIO(gv)))
2336 /* XXX Looks to me like io is always NULL at this point */
2338 (void)PerlIO_flush(fp);
2339 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2342 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2343 report_evil_fh(gv, io, PL_op->op_type);
2345 SETERRNO(EBADF,RMS_IFI);
2350 DIE(aTHX_ PL_no_func, "flock()");
2360 const int protocol = POPi;
2361 const int type = POPi;
2362 const int domain = POPi;
2363 GV * const gv = MUTABLE_GV(POPs);
2364 register IO * const io = gv ? GvIOn(gv) : NULL;
2368 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2369 report_evil_fh(gv, io, PL_op->op_type);
2370 if (io && IoIFP(io))
2371 do_close(gv, FALSE);
2372 SETERRNO(EBADF,LIB_INVARG);
2377 do_close(gv, FALSE);
2379 TAINT_PROPER("socket");
2380 fd = PerlSock_socket(domain, type, protocol);
2383 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2384 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2385 IoTYPE(io) = IoTYPE_SOCKET;
2386 if (!IoIFP(io) || !IoOFP(io)) {
2387 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2388 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2389 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2392 #if defined(HAS_FCNTL) && defined(F_SETFD)
2393 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2397 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2402 DIE(aTHX_ PL_no_sock_func, "socket");
2408 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2410 const int protocol = POPi;
2411 const int type = POPi;
2412 const int domain = POPi;
2413 GV * const gv2 = MUTABLE_GV(POPs);
2414 GV * const gv1 = MUTABLE_GV(POPs);
2415 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2416 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2419 if (!gv1 || !gv2 || !io1 || !io2) {
2420 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2422 report_evil_fh(gv1, io1, PL_op->op_type);
2424 report_evil_fh(gv1, io2, PL_op->op_type);
2426 if (io1 && IoIFP(io1))
2427 do_close(gv1, FALSE);
2428 if (io2 && IoIFP(io2))
2429 do_close(gv2, FALSE);
2434 do_close(gv1, FALSE);
2436 do_close(gv2, FALSE);
2438 TAINT_PROPER("socketpair");
2439 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2441 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2442 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2443 IoTYPE(io1) = IoTYPE_SOCKET;
2444 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2445 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2446 IoTYPE(io2) = IoTYPE_SOCKET;
2447 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2448 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2449 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2450 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2451 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2452 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2453 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2456 #if defined(HAS_FCNTL) && defined(F_SETFD)
2457 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2458 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2463 DIE(aTHX_ PL_no_sock_func, "socketpair");
2471 SV * const addrsv = POPs;
2472 /* OK, so on what platform does bind modify addr? */
2474 GV * const gv = MUTABLE_GV(POPs);
2475 register IO * const io = GvIOn(gv);
2478 if (!io || !IoIFP(io))
2481 addr = SvPV_const(addrsv, len);
2482 TAINT_PROPER("bind");
2483 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2489 if (ckWARN(WARN_CLOSED))
2490 report_evil_fh(gv, io, PL_op->op_type);
2491 SETERRNO(EBADF,SS_IVCHAN);
2494 DIE(aTHX_ PL_no_sock_func, "bind");
2502 SV * const addrsv = POPs;
2503 GV * const gv = MUTABLE_GV(POPs);
2504 register IO * const io = GvIOn(gv);
2508 if (!io || !IoIFP(io))
2511 addr = SvPV_const(addrsv, len);
2512 TAINT_PROPER("connect");
2513 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2519 if (ckWARN(WARN_CLOSED))
2520 report_evil_fh(gv, io, PL_op->op_type);
2521 SETERRNO(EBADF,SS_IVCHAN);
2524 DIE(aTHX_ PL_no_sock_func, "connect");
2532 const int backlog = POPi;
2533 GV * const gv = MUTABLE_GV(POPs);
2534 register IO * const io = gv ? GvIOn(gv) : NULL;
2536 if (!gv || !io || !IoIFP(io))
2539 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2545 if (ckWARN(WARN_CLOSED))
2546 report_evil_fh(gv, io, PL_op->op_type);
2547 SETERRNO(EBADF,SS_IVCHAN);
2550 DIE(aTHX_ PL_no_sock_func, "listen");
2560 char namebuf[MAXPATHLEN];
2561 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2562 Sock_size_t len = sizeof (struct sockaddr_in);
2564 Sock_size_t len = sizeof namebuf;
2566 GV * const ggv = MUTABLE_GV(POPs);
2567 GV * const ngv = MUTABLE_GV(POPs);
2576 if (!gstio || !IoIFP(gstio))
2580 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2583 /* Some platforms indicate zero length when an AF_UNIX client is
2584 * not bound. Simulate a non-zero-length sockaddr structure in
2586 namebuf[0] = 0; /* sun_len */
2587 namebuf[1] = AF_UNIX; /* sun_family */
2595 do_close(ngv, FALSE);
2596 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2597 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2598 IoTYPE(nstio) = IoTYPE_SOCKET;
2599 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2600 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2601 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2602 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2605 #if defined(HAS_FCNTL) && defined(F_SETFD)
2606 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2610 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2611 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2613 #ifdef __SCO_VERSION__
2614 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2617 PUSHp(namebuf, len);
2621 if (ckWARN(WARN_CLOSED))
2622 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2623 SETERRNO(EBADF,SS_IVCHAN);
2629 DIE(aTHX_ PL_no_sock_func, "accept");
2637 const int how = POPi;
2638 GV * const gv = MUTABLE_GV(POPs);
2639 register IO * const io = GvIOn(gv);
2641 if (!io || !IoIFP(io))
2644 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2648 if (ckWARN(WARN_CLOSED))
2649 report_evil_fh(gv, io, PL_op->op_type);
2650 SETERRNO(EBADF,SS_IVCHAN);
2653 DIE(aTHX_ PL_no_sock_func, "shutdown");
2661 const int optype = PL_op->op_type;
2662 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(newSV(257)) : POPs;
2663 const unsigned int optname = (unsigned int) POPi;
2664 const unsigned int lvl = (unsigned int) POPi;
2665 GV * const gv = MUTABLE_GV(POPs);
2666 register IO * const io = GvIOn(gv);
2670 if (!io || !IoIFP(io))
2673 fd = PerlIO_fileno(IoIFP(io));
2677 (void)SvPOK_only(sv);
2681 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2688 #if defined(__SYMBIAN32__)
2689 # define SETSOCKOPT_OPTION_VALUE_T void *
2691 # define SETSOCKOPT_OPTION_VALUE_T const char *
2693 /* XXX TODO: We need to have a proper type (a Configure probe,
2694 * etc.) for what the C headers think of the third argument of
2695 * setsockopt(), the option_value read-only buffer: is it
2696 * a "char *", or a "void *", const or not. Some compilers
2697 * don't take kindly to e.g. assuming that "char *" implicitly
2698 * promotes to a "void *", or to explicitly promoting/demoting
2699 * consts to non/vice versa. The "const void *" is the SUS
2700 * definition, but that does not fly everywhere for the above
2702 SETSOCKOPT_OPTION_VALUE_T buf;
2706 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2710 aint = (int)SvIV(sv);
2711 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2714 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2723 if (ckWARN(WARN_CLOSED))
2724 report_evil_fh(gv, io, optype);
2725 SETERRNO(EBADF,SS_IVCHAN);
2730 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2738 const int optype = PL_op->op_type;
2739 GV * const gv = MUTABLE_GV(POPs);
2740 register IO * const io = GvIOn(gv);
2745 if (!io || !IoIFP(io))
2748 sv = sv_2mortal(newSV(257));
2749 (void)SvPOK_only(sv);
2753 fd = PerlIO_fileno(IoIFP(io));
2755 case OP_GETSOCKNAME:
2756 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2759 case OP_GETPEERNAME:
2760 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2762 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2764 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";
2765 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2766 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2767 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2768 sizeof(u_short) + sizeof(struct in_addr))) {
2775 #ifdef BOGUS_GETNAME_RETURN
2776 /* Interactive Unix, getpeername() and getsockname()
2777 does not return valid namelen */
2778 if (len == BOGUS_GETNAME_RETURN)
2779 len = sizeof(struct sockaddr);
2787 if (ckWARN(WARN_CLOSED))
2788 report_evil_fh(gv, io, optype);
2789 SETERRNO(EBADF,SS_IVCHAN);
2794 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2809 if (PL_op->op_flags & OPf_REF) {
2811 if (PL_op->op_type == OP_LSTAT) {
2812 if (gv != PL_defgv) {
2813 do_fstat_warning_check:
2814 if (ckWARN(WARN_IO))
2815 Perl_warner(aTHX_ packWARN(WARN_IO),
2816 "lstat() on filehandle %s", gv ? GvENAME(gv) : "");
2817 } else if (PL_laststype != OP_LSTAT)
2818 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2822 if (gv != PL_defgv) {
2823 PL_laststype = OP_STAT;
2825 sv_setpvs(PL_statname, "");
2832 PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
2833 } else if (IoDIRP(io)) {
2835 PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache);
2837 PL_laststatval = -1;
2843 if (PL_laststatval < 0) {
2844 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2845 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2850 SV* const sv = POPs;
2851 if (isGV_with_GP(sv)) {
2852 gv = MUTABLE_GV(sv);
2854 } else if(SvROK(sv) && isGV_with_GP(SvRV(sv))) {
2855 gv = MUTABLE_GV(SvRV(sv));
2856 if (PL_op->op_type == OP_LSTAT)
2857 goto do_fstat_warning_check;
2859 } else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2860 io = MUTABLE_IO(SvRV(sv));
2861 if (PL_op->op_type == OP_LSTAT)
2862 goto do_fstat_warning_check;
2863 goto do_fstat_have_io;
2866 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2868 PL_laststype = PL_op->op_type;
2869 if (PL_op->op_type == OP_LSTAT)
2870 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2872 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2873 if (PL_laststatval < 0) {
2874 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2875 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2881 if (gimme != G_ARRAY) {
2882 if (gimme != G_VOID)
2883 XPUSHs(boolSV(max));
2889 mPUSHi(PL_statcache.st_dev);
2890 mPUSHi(PL_statcache.st_ino);
2891 mPUSHu(PL_statcache.st_mode);
2892 mPUSHu(PL_statcache.st_nlink);
2893 #if Uid_t_size > IVSIZE
2894 mPUSHn(PL_statcache.st_uid);
2896 # if Uid_t_sign <= 0
2897 mPUSHi(PL_statcache.st_uid);
2899 mPUSHu(PL_statcache.st_uid);
2902 #if Gid_t_size > IVSIZE
2903 mPUSHn(PL_statcache.st_gid);
2905 # if Gid_t_sign <= 0
2906 mPUSHi(PL_statcache.st_gid);
2908 mPUSHu(PL_statcache.st_gid);
2911 #ifdef USE_STAT_RDEV
2912 mPUSHi(PL_statcache.st_rdev);
2914 PUSHs(newSVpvs_flags("", SVs_TEMP));
2916 #if Off_t_size > IVSIZE
2917 mPUSHn(PL_statcache.st_size);
2919 mPUSHi(PL_statcache.st_size);
2922 mPUSHn(PL_statcache.st_atime);
2923 mPUSHn(PL_statcache.st_mtime);
2924 mPUSHn(PL_statcache.st_ctime);
2926 mPUSHi(PL_statcache.st_atime);
2927 mPUSHi(PL_statcache.st_mtime);
2928 mPUSHi(PL_statcache.st_ctime);
2930 #ifdef USE_STAT_BLOCKS
2931 mPUSHu(PL_statcache.st_blksize);
2932 mPUSHu(PL_statcache.st_blocks);
2934 PUSHs(newSVpvs_flags("", SVs_TEMP));
2935 PUSHs(newSVpvs_flags("", SVs_TEMP));
2941 /* This macro is used by the stacked filetest operators :
2942 * if the previous filetest failed, short-circuit and pass its value.
2943 * Else, discard it from the stack and continue. --rgs
2945 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2946 if (!SvTRUE(TOPs)) { RETURN; } \
2947 else { (void)POPs; PUTBACK; } \
2954 /* Not const, because things tweak this below. Not bool, because there's
2955 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2956 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2957 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2958 /* Giving some sort of initial value silences compilers. */
2960 int access_mode = R_OK;
2962 int access_mode = 0;
2965 /* access_mode is never used, but leaving use_access in makes the
2966 conditional compiling below much clearer. */
2969 int stat_mode = S_IRUSR;
2971 bool effective = FALSE;
2974 STACKED_FTEST_CHECK;
2976 switch (PL_op->op_type) {
2978 #if !(defined(HAS_ACCESS) && defined(R_OK))
2984 #if defined(HAS_ACCESS) && defined(W_OK)
2989 stat_mode = S_IWUSR;
2993 #if defined(HAS_ACCESS) && defined(X_OK)
2998 stat_mode = S_IXUSR;
3002 #ifdef PERL_EFF_ACCESS
3005 stat_mode = S_IWUSR;
3009 #ifndef PERL_EFF_ACCESS
3016 #ifdef PERL_EFF_ACCESS
3021 stat_mode = S_IXUSR;
3027 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
3028 const char *name = POPpx;
3030 # ifdef PERL_EFF_ACCESS
3031 result = PERL_EFF_ACCESS(name, access_mode);
3033 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
3039 result = access(name, access_mode);
3041 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
3056 if (cando(stat_mode, effective, &PL_statcache))
3065 const int op_type = PL_op->op_type;
3067 STACKED_FTEST_CHECK;
3072 if (op_type == OP_FTIS)
3075 /* You can't dTARGET inside OP_FTIS, because you'll get
3076 "panic: pad_sv po" - the op is not flagged to have a target. */
3080 #if Off_t_size > IVSIZE
3081 PUSHn(PL_statcache.st_size);
3083 PUSHi(PL_statcache.st_size);
3087 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3090 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3093 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3106 /* I believe that all these three are likely to be defined on most every
3107 system these days. */
3109 if(PL_op->op_type == OP_FTSUID)
3113 if(PL_op->op_type == OP_FTSGID)
3117 if(PL_op->op_type == OP_FTSVTX)
3121 STACKED_FTEST_CHECK;
3126 switch (PL_op->op_type) {
3128 if (PL_statcache.st_uid == PL_uid)
3132 if (PL_statcache.st_uid == PL_euid)
3136 if (PL_statcache.st_size == 0)
3140 if (S_ISSOCK(PL_statcache.st_mode))
3144 if (S_ISCHR(PL_statcache.st_mode))
3148 if (S_ISBLK(PL_statcache.st_mode))
3152 if (S_ISREG(PL_statcache.st_mode))
3156 if (S_ISDIR(PL_statcache.st_mode))
3160 if (S_ISFIFO(PL_statcache.st_mode))
3165 if (PL_statcache.st_mode & S_ISUID)
3171 if (PL_statcache.st_mode & S_ISGID)
3177 if (PL_statcache.st_mode & S_ISVTX)
3188 I32 result = my_lstat();
3192 if (S_ISLNK(PL_statcache.st_mode))
3205 STACKED_FTEST_CHECK;
3207 if (PL_op->op_flags & OPf_REF)
3209 else if (isGV(TOPs))
3210 gv = MUTABLE_GV(POPs);
3211 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3212 gv = MUTABLE_GV(SvRV(POPs));
3214 gv = gv_fetchsv(tmpsv = POPs, 0, SVt_PVIO);
3216 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3217 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3218 else if (tmpsv && SvOK(tmpsv)) {
3219 const char *tmps = SvPV_nolen_const(tmpsv);
3227 if (PerlLIO_isatty(fd))
3232 #if defined(atarist) /* this will work with atariST. Configure will
3233 make guesses for other systems. */
3234 # define FILE_base(f) ((f)->_base)
3235 # define FILE_ptr(f) ((f)->_ptr)
3236 # define FILE_cnt(f) ((f)->_cnt)
3237 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3248 register STDCHAR *s;
3254 STACKED_FTEST_CHECK;
3256 if (PL_op->op_flags & OPf_REF)
3258 else if (isGV(TOPs))
3259 gv = MUTABLE_GV(POPs);
3260 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3261 gv = MUTABLE_GV(SvRV(POPs));
3267 if (gv == PL_defgv) {
3269 io = GvIO(PL_statgv);
3272 goto really_filename;
3277 PL_laststatval = -1;
3278 sv_setpvs(PL_statname, "");
3279 io = GvIO(PL_statgv);
3281 if (io && IoIFP(io)) {
3282 if (! PerlIO_has_base(IoIFP(io)))
3283 DIE(aTHX_ "-T and -B not implemented on filehandles");
3284 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3285 if (PL_laststatval < 0)
3287 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3288 if (PL_op->op_type == OP_FTTEXT)
3293 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3294 i = PerlIO_getc(IoIFP(io));
3296 (void)PerlIO_ungetc(IoIFP(io),i);
3298 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3300 len = PerlIO_get_bufsiz(IoIFP(io));
3301 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3302 /* sfio can have large buffers - limit to 512 */
3307 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3309 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3311 SETERRNO(EBADF,RMS_IFI);
3319 PL_laststype = OP_STAT;
3320 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3321 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3322 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3324 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3327 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3328 if (PL_laststatval < 0) {
3329 (void)PerlIO_close(fp);
3332 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, NULL);
3333 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3334 (void)PerlIO_close(fp);
3336 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3337 RETPUSHNO; /* special case NFS directories */
3338 RETPUSHYES; /* null file is anything */
3343 /* now scan s to look for textiness */
3344 /* XXX ASCII dependent code */
3346 #if defined(DOSISH) || defined(USEMYBINMODE)
3347 /* ignore trailing ^Z on short files */
3348 if (len && len < (I32)sizeof(tbuf) && tbuf[len-1] == 26)
3352 for (i = 0; i < len; i++, s++) {
3353 if (!*s) { /* null never allowed in text */
3358 else if (!(isPRINT(*s) || isSPACE(*s)))
3361 else if (*s & 128) {
3363 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3366 /* utf8 characters don't count as odd */
3367 if (UTF8_IS_START(*s)) {
3368 int ulen = UTF8SKIP(s);
3369 if (ulen < len - i) {
3371 for (j = 1; j < ulen; j++) {
3372 if (!UTF8_IS_CONTINUATION(s[j]))
3375 --ulen; /* loop does extra increment */
3385 *s != '\n' && *s != '\r' && *s != '\b' &&
3386 *s != '\t' && *s != '\f' && *s != 27)
3391 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3402 const char *tmps = NULL;
3406 SV * const sv = POPs;
3407 if (PL_op->op_flags & OPf_SPECIAL) {
3408 gv = gv_fetchsv(sv, 0, SVt_PVIO);
3410 else if (isGV_with_GP(sv)) {
3411 gv = MUTABLE_GV(sv);
3413 else if (SvROK(sv) && isGV_with_GP(SvRV(sv))) {
3414 gv = MUTABLE_GV(SvRV(sv));
3417 tmps = SvPV_nolen_const(sv);
3421 if( !gv && (!tmps || !*tmps) ) {
3422 HV * const table = GvHVn(PL_envgv);
3425 if ( (svp = hv_fetchs(table, "HOME", FALSE))
3426 || (svp = hv_fetchs(table, "LOGDIR", FALSE))
3428 || (svp = hv_fetchs(table, "SYS$LOGIN", FALSE))
3433 deprecate("chdir('') or chdir(undef) as chdir()");
3434 tmps = SvPV_nolen_const(*svp);
3438 TAINT_PROPER("chdir");
3443 TAINT_PROPER("chdir");
3446 IO* const io = GvIO(gv);
3449 PUSHi(fchdir(my_dirfd(IoDIRP(io))) >= 0);
3450 } else if (IoIFP(io)) {
3451 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3454 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3455 report_evil_fh(gv, io, PL_op->op_type);
3456 SETERRNO(EBADF, RMS_IFI);
3461 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
3462 report_evil_fh(gv, io, PL_op->op_type);
3463 SETERRNO(EBADF,RMS_IFI);
3467 DIE(aTHX_ PL_no_func, "fchdir");
3471 PUSHi( PerlDir_chdir(tmps) >= 0 );
3473 /* Clear the DEFAULT element of ENV so we'll get the new value
3475 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3482 dVAR; dSP; dMARK; dTARGET;
3483 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3494 char * const tmps = POPpx;
3495 TAINT_PROPER("chroot");
3496 PUSHi( chroot(tmps) >= 0 );
3499 DIE(aTHX_ PL_no_func, "chroot");
3507 const char * const tmps2 = POPpconstx;
3508 const char * const tmps = SvPV_nolen_const(TOPs);
3509 TAINT_PROPER("rename");
3511 anum = PerlLIO_rename(tmps, tmps2);
3513 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3514 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3517 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3518 (void)UNLINK(tmps2);
3519 if (!(anum = link(tmps, tmps2)))
3520 anum = UNLINK(tmps);
3528 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3532 const int op_type = PL_op->op_type;
3536 if (op_type == OP_LINK)
3537 DIE(aTHX_ PL_no_func, "link");
3539 # ifndef HAS_SYMLINK
3540 if (op_type == OP_SYMLINK)
3541 DIE(aTHX_ PL_no_func, "symlink");
3545 const char * const tmps2 = POPpconstx;
3546 const char * const tmps = SvPV_nolen_const(TOPs);
3547 TAINT_PROPER(PL_op_desc[op_type]);
3549 # if defined(HAS_LINK)
3550 # if defined(HAS_SYMLINK)
3551 /* Both present - need to choose which. */
3552 (op_type == OP_LINK) ?
3553 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3555 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3556 PerlLIO_link(tmps, tmps2);
3559 # if defined(HAS_SYMLINK)
3560 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3561 symlink(tmps, tmps2);
3566 SETi( result >= 0 );
3573 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3584 char buf[MAXPATHLEN];
3587 #ifndef INCOMPLETE_TAINTS
3591 len = readlink(tmps, buf, sizeof(buf) - 1);
3599 RETSETUNDEF; /* just pretend it's a normal file */
3603 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3605 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3607 char * const save_filename = filename;
3612 Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
3614 PERL_ARGS_ASSERT_DOONELINER;
3616 Newx(cmdline, size, char);
3617 my_strlcpy(cmdline, cmd, size);
3618 my_strlcat(cmdline, " ", size);
3619 for (s = cmdline + strlen(cmdline); *filename; ) {
3623 if (s - cmdline < size)
3624 my_strlcpy(s, " 2>&1", size - (s - cmdline));
3625 myfp = PerlProc_popen(cmdline, "r");
3629 SV * const tmpsv = sv_newmortal();
3630 /* Need to save/restore 'PL_rs' ?? */
3631 s = sv_gets(tmpsv, myfp, 0);
3632 (void)PerlProc_pclose(myfp);
3636 #ifdef HAS_SYS_ERRLIST
3641 /* you don't see this */
3642 const char * const errmsg =
3643 #ifdef HAS_SYS_ERRLIST
3651 if (instr(s, errmsg)) {
3658 #define EACCES EPERM
3660 if (instr(s, "cannot make"))
3661 SETERRNO(EEXIST,RMS_FEX);
3662 else if (instr(s, "existing file"))
3663 SETERRNO(EEXIST,RMS_FEX);
3664 else if (instr(s, "ile exists"))
3665 SETERRNO(EEXIST,RMS_FEX);
3666 else if (instr(s, "non-exist"))
3667 SETERRNO(ENOENT,RMS_FNF);
3668 else if (instr(s, "does not exist"))
3669 SETERRNO(ENOENT,RMS_FNF);
3670 else if (instr(s, "not empty"))
3671 SETERRNO(EBUSY,SS_DEVOFFLINE);
3672 else if (instr(s, "cannot access"))
3673 SETERRNO(EACCES,RMS_PRV);
3675 SETERRNO(EPERM,RMS_PRV);
3678 else { /* some mkdirs return no failure indication */
3679 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3680 if (PL_op->op_type == OP_RMDIR)
3685 SETERRNO(EACCES,RMS_PRV); /* a guess */
3694 /* This macro removes trailing slashes from a directory name.
3695 * Different operating and file systems take differently to
3696 * trailing slashes. According to POSIX 1003.1 1996 Edition
3697 * any number of trailing slashes should be allowed.
3698 * Thusly we snip them away so that even non-conforming
3699 * systems are happy.
3700 * We should probably do this "filtering" for all
3701 * the functions that expect (potentially) directory names:
3702 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3703 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3705 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3706 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3709 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3710 (tmps) = savepvn((tmps), (len)); \
3720 const int mode = (MAXARG > 1) ? POPi : 0777;
3722 TRIMSLASHES(tmps,len,copy);
3724 TAINT_PROPER("mkdir");
3726 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3730 SETi( dooneliner("mkdir", tmps) );
3731 oldumask = PerlLIO_umask(0);
3732 PerlLIO_umask(oldumask);
3733 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3748 TRIMSLASHES(tmps,len,copy);
3749 TAINT_PROPER("rmdir");
3751 SETi( PerlDir_rmdir(tmps) >= 0 );
3753 SETi( dooneliner("rmdir", tmps) );
3760 /* Directory calls. */
3764 #if defined(Direntry_t) && defined(HAS_READDIR)
3766 const char * const dirname = POPpconstx;
3767 GV * const gv = MUTABLE_GV(POPs);
3768 register IO * const io = GvIOn(gv);
3773 if ((IoIFP(io) || IoOFP(io)) && ckWARN2(WARN_IO, WARN_DEPRECATED))
3774 Perl_warner(aTHX_ packWARN2(WARN_IO, WARN_DEPRECATED),
3775 "Opening filehandle %s also as a directory", GvENAME(gv));
3777 PerlDir_close(IoDIRP(io));
3778 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3784 SETERRNO(EBADF,RMS_DIR);
3787 DIE(aTHX_ PL_no_dir_func, "opendir");
3793 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3794 DIE(aTHX_ PL_no_dir_func, "readdir");
3796 #if !defined(I_DIRENT) && !defined(VMS)
3797 Direntry_t *readdir (DIR *);
3803 const I32 gimme = GIMME;
3804 GV * const gv = MUTABLE_GV(POPs);
3805 register const Direntry_t *dp;
3806 register IO * const io = GvIOn(gv);
3808 if (!io || !IoDIRP(io)) {
3809 if(ckWARN(WARN_IO)) {
3810 Perl_warner(aTHX_ packWARN(WARN_IO),
3811 "readdir() attempted on invalid dirhandle %s", GvENAME(gv));
3817 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3821 sv = newSVpvn(dp->d_name, dp->d_namlen);
3823 sv = newSVpv(dp->d_name, 0);
3825 #ifndef INCOMPLETE_TAINTS
3826 if (!(IoFLAGS(io) & IOf_UNTAINT))
3830 } while (gimme == G_ARRAY);
3832 if (!dp && gimme != G_ARRAY)
3839 SETERRNO(EBADF,RMS_ISI);
3840 if (GIMME == G_ARRAY)
3849 #if defined(HAS_TELLDIR) || defined(telldir)
3851 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3852 /* XXX netbsd still seemed to.
3853 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3854 --JHI 1999-Feb-02 */
3855 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3856 long telldir (DIR *);
3858 GV * const gv = MUTABLE_GV(POPs);
3859 register IO * const io = GvIOn(gv);
3861 if (!io || !IoDIRP(io)) {
3862 if(ckWARN(WARN_IO)) {
3863 Perl_warner(aTHX_ packWARN(WARN_IO),
3864 "telldir() attempted on invalid dirhandle %s", GvENAME(gv));
3869 PUSHi( PerlDir_tell(IoDIRP(io)) );
3873 SETERRNO(EBADF,RMS_ISI);
3876 DIE(aTHX_ PL_no_dir_func, "telldir");
3882 #if defined(HAS_SEEKDIR) || defined(seekdir)
3884 const long along = POPl;
3885 GV * const gv = MUTABLE_GV(POPs);
3886 register IO * const io = GvIOn(gv);
3888 if (!io || !IoDIRP(io)) {
3889 if(ckWARN(WARN_IO)) {
3890 Perl_warner(aTHX_ packWARN(WARN_IO),
3891 "seekdir() attempted on invalid dirhandle %s", GvENAME(gv));
3895 (void)PerlDir_seek(IoDIRP(io), along);
3900 SETERRNO(EBADF,RMS_ISI);
3903 DIE(aTHX_ PL_no_dir_func, "seekdir");
3909 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3911 GV * const gv = MUTABLE_GV(POPs);
3912 register IO * const io = GvIOn(gv);
3914 if (!io || !IoDIRP(io)) {
3915 if(ckWARN(WARN_IO)) {
3916 Perl_warner(aTHX_ packWARN(WARN_IO),
3917 "rewinddir() attempted on invalid dirhandle %s", GvENAME(gv));
3921 (void)PerlDir_rewind(IoDIRP(io));
3925 SETERRNO(EBADF,RMS_ISI);
3928 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3934 #if defined(Direntry_t) && defined(HAS_READDIR)
3936 GV * const gv = MUTABLE_GV(POPs);
3937 register IO * const io = GvIOn(gv);
3939 if (!io || !IoDIRP(io)) {
3940 if(ckWARN(WARN_IO)) {
3941 Perl_warner(aTHX_ packWARN(WARN_IO),
3942 "closedir() attempted on invalid dirhandle %s", GvENAME(gv));
3946 #ifdef VOID_CLOSEDIR
3947 PerlDir_close(IoDIRP(io));
3949 if (PerlDir_close(IoDIRP(io)) < 0) {
3950 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3959 SETERRNO(EBADF,RMS_IFI);
3962 DIE(aTHX_ PL_no_dir_func, "closedir");
3966 /* Process control. */
3975 PERL_FLUSHALL_FOR_CHILD;
3976 childpid = PerlProc_fork();
3980 GV * const tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
3982 SvREADONLY_off(GvSV(tmpgv));
3983 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3984 SvREADONLY_on(GvSV(tmpgv));
3986 #ifdef THREADS_HAVE_PIDS
3987 PL_ppid = (IV)getppid();
3989 #ifdef PERL_USES_PL_PIDSTATUS
3990 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3996 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4001 PERL_FLUSHALL_FOR_CHILD;
4002 childpid = PerlProc_fork();
4008 DIE(aTHX_ PL_no_func, "fork");
4015 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4020 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4021 childpid = wait4pid(-1, &argflags, 0);
4023 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4028 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4029 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4030 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4032 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4037 DIE(aTHX_ PL_no_func, "wait");
4043 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL) && !defined(__LIBCATAMOUNT__)
4045 const int optype = POPi;
4046 const Pid_t pid = TOPi;
4050 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4051 result = wait4pid(pid, &argflags, optype);
4053 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4058 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4059 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4060 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
4062 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4067 DIE(aTHX_ PL_no_func, "waitpid");
4073 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4074 #if defined(__LIBCATAMOUNT__)
4075 PL_statusvalue = -1;
4084 while (++MARK <= SP) {
4085 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4090 TAINT_PROPER("system");
4092 PERL_FLUSHALL_FOR_CHILD;
4093 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4099 if (PerlProc_pipe(pp) >= 0)
4101 while ((childpid = PerlProc_fork()) == -1) {
4102 if (errno != EAGAIN) {
4107 PerlLIO_close(pp[0]);
4108 PerlLIO_close(pp[1]);
4115 Sigsave_t ihand,qhand; /* place to save signals during system() */
4119 PerlLIO_close(pp[1]);
4121 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4122 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4125 result = wait4pid(childpid, &status, 0);
4126 } while (result == -1 && errno == EINTR);
4128 (void)rsignal_restore(SIGINT, &ihand);
4129 (void)rsignal_restore(SIGQUIT, &qhand);
4131 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4132 do_execfree(); /* free any memory child malloced on fork */
4139 while (n < sizeof(int)) {
4140 n1 = PerlLIO_read(pp[0],
4141 (void*)(((char*)&errkid)+n),
4147 PerlLIO_close(pp[0]);
4148 if (n) { /* Error */
4149 if (n != sizeof(int))
4150 DIE(aTHX_ "panic: kid popen errno read");
4151 errno = errkid; /* Propagate errno from kid */
4152 STATUS_NATIVE_CHILD_SET(-1);
4155 XPUSHi(STATUS_CURRENT);
4159 PerlLIO_close(pp[0]);
4160 #if defined(HAS_FCNTL) && defined(F_SETFD)
4161 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4164 if (PL_op->op_flags & OPf_STACKED) {
4165 SV * const really = *++MARK;
4166 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4168 else if (SP - MARK != 1)
4169 value = (I32)do_aexec5(NULL, MARK, SP, pp[1], did_pipes);
4171 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4175 #else /* ! FORK or VMS or OS/2 */
4178 if (PL_op->op_flags & OPf_STACKED) {
4179 SV * const really = *++MARK;
4180 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4181 value = (I32)do_aspawn(really, MARK, SP);
4183 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4186 else if (SP - MARK != 1) {
4187 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__) || defined(__VMS)
4188 value = (I32)do_aspawn(NULL, MARK, SP);
4190 value = (I32)do_aspawn(NULL, (void **)MARK, (void **)SP);
4194 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4196 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4198 STATUS_NATIVE_CHILD_SET(value);
4201 XPUSHi(result ? value : STATUS_CURRENT);
4202 #endif /* !FORK or VMS or OS/2 */
4209 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4214 while (++MARK <= SP) {
4215 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4220 TAINT_PROPER("exec");
4222 PERL_FLUSHALL_FOR_CHILD;
4223 if (PL_op->op_flags & OPf_STACKED) {
4224 SV * const really = *++MARK;
4225 value = (I32)do_aexec(really, MARK, SP);
4227 else if (SP - MARK != 1)
4229 value = (I32)vms_do_aexec(NULL, MARK, SP);
4233 (void ) do_aspawn(NULL, MARK, SP);
4237 value = (I32)do_aexec(NULL, MARK, SP);
4242 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4245 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4248 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4262 # ifdef THREADS_HAVE_PIDS
4263 if (PL_ppid != 1 && getppid() == 1)
4264 /* maybe the parent process has died. Refresh ppid cache */
4268 XPUSHi( getppid() );
4272 DIE(aTHX_ PL_no_func, "getppid");
4281 const Pid_t pid = (MAXARG < 1) ? 0 : SvIVx(POPs);
4284 pgrp = (I32)BSD_GETPGRP(pid);
4286 if (pid != 0 && pid != PerlProc_getpid())
4287 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4293 DIE(aTHX_ PL_no_func, "getpgrp()");
4312 TAINT_PROPER("setpgrp");
4314 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4316 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4317 || (pid != 0 && pid != PerlProc_getpid()))
4319 DIE(aTHX_ "setpgrp can't take arguments");
4321 SETi( setpgrp() >= 0 );
4322 #endif /* USE_BSDPGRP */
4325 DIE(aTHX_ PL_no_func, "setpgrp()");
4331 #ifdef HAS_GETPRIORITY
4333 const int who = POPi;
4334 const int which = TOPi;
4335 SETi( getpriority(which, who) );
4338 DIE(aTHX_ PL_no_func, "getpriority()");
4344 #ifdef HAS_SETPRIORITY
4346 const int niceval = POPi;
4347 const int who = POPi;
4348 const int which = TOPi;
4349 TAINT_PROPER("setpriority");
4350 SETi( setpriority(which, who, niceval) >= 0 );
4353 DIE(aTHX_ PL_no_func, "setpriority()");
4363 XPUSHn( time(NULL) );
4365 XPUSHi( time(NULL) );
4377 (void)PerlProc_times(&PL_timesbuf);
4379 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4380 /* struct tms, though same data */
4384 mPUSHn(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick);
4385 if (GIMME == G_ARRAY) {
4386 mPUSHn(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick);
4387 mPUSHn(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick);
4388 mPUSHn(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick);
4396 if (GIMME == G_ARRAY) {
4403 DIE(aTHX_ "times not implemented");
4405 #endif /* HAS_TIMES */
4408 #ifdef LOCALTIME_EDGECASE_BROKEN
4409 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4414 /* No workarounds in the valid range */
4415 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4416 return (localtime (tp));
4418 /* This edge case is to workaround the undefined behaviour, where the
4419 * TIMEZONE makes the time go beyond the defined range.
4420 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4421 * If there is a negative offset in TZ, like MET-1METDST, some broken
4422 * implementations of localtime () (like AIX 5.2) barf with bogus
4424 * 0x7fffffff gmtime 2038-01-19 03:14:07
4425 * 0x7fffffff localtime 1901-12-13 21:45:51
4426 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4427 * 0x3c19137f gmtime 2001-12-13 20:45:51
4428 * 0x3c19137f localtime 2001-12-13 21:45:51
4429 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4430 * Given that legal timezones are typically between GMT-12 and GMT+12
4431 * we turn back the clock 23 hours before calling the localtime
4432 * function, and add those to the return value. This will never cause
4433 * day wrapping problems, since the edge case is Tue Jan *19*
4435 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4438 if (P->tm_hour >= 24) {
4440 P->tm_mday++; /* 18 -> 19 */
4441 P->tm_wday++; /* Mon -> Tue */
4442 P->tm_yday++; /* 18 -> 19 */
4445 } /* S_my_localtime */
4453 const struct tm *tmbuf;
4454 static const char * const dayname[] =
4455 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4456 static const char * const monname[] =
4457 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4458 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4464 when = (Time_t)SvNVx(POPs);
4466 when = (Time_t)SvIVx(POPs);
4469 if (PL_op->op_type == OP_LOCALTIME)
4470 #ifdef LOCALTIME_EDGECASE_BROKEN
4471 tmbuf = S_my_localtime(aTHX_ &when);
4473 tmbuf = localtime(&when);
4476 tmbuf = gmtime(&when);
4478 if (GIMME != G_ARRAY) {
4484 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4485 dayname[tmbuf->tm_wday],
4486 monname[tmbuf->tm_mon],
4491 tmbuf->tm_year + 1900);
4497 mPUSHi(tmbuf->tm_sec);
4498 mPUSHi(tmbuf->tm_min);
4499 mPUSHi(tmbuf->tm_hour);
4500 mPUSHi(tmbuf->tm_mday);
4501 mPUSHi(tmbuf->tm_mon);
4502 mPUSHi(tmbuf->tm_year);
4503 mPUSHi(tmbuf->tm_wday);
4504 mPUSHi(tmbuf->tm_yday);
4505 mPUSHi(tmbuf->tm_isdst);
4516 anum = alarm((unsigned int)anum);
4523 DIE(aTHX_ PL_no_func, "alarm");
4534 (void)time(&lasttime);
4539 PerlProc_sleep((unsigned int)duration);
4542 XPUSHi(when - lasttime);
4546 /* Shared memory. */
4547 /* Merged with some message passing. */
4551 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4552 dVAR; dSP; dMARK; dTARGET;
4553 const int op_type = PL_op->op_type;
4558 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4561 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4564 value = (I32)(do_semop(MARK, SP) >= 0);
4567 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4583 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4584 dVAR; dSP; dMARK; dTARGET;
4585 const int anum = do_ipcget(PL_op->op_type, MARK, SP);
4592 DIE(aTHX_ "System V IPC is not implemented on this machine");
4598 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4599 dVAR; dSP; dMARK; dTARGET;
4600 const int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4608 PUSHp(zero_but_true, ZBTLEN);
4616 /* I can't const this further without getting warnings about the types of
4617 various arrays passed in from structures. */
4619 S_space_join_names_mortal(pTHX_ char *const *array)
4623 PERL_ARGS_ASSERT_SPACE_JOIN_NAMES_MORTAL;
4625 if (array && *array) {
4626 target = newSVpvs_flags("", SVs_TEMP);
4628 sv_catpv(target, *array);
4631 sv_catpvs(target, " ");
4634 target = sv_mortalcopy(&PL_sv_no);
4639 /* Get system info. */
4643 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4645 I32 which = PL_op->op_type;
4646 register char **elem;
4648 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4649 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4650 struct hostent *gethostbyname(Netdb_name_t);
4651 struct hostent *gethostent(void);
4653 struct hostent *hent;
4657 if (which == OP_GHBYNAME) {
4658 #ifdef HAS_GETHOSTBYNAME
4659 const char* const name = POPpbytex;
4660 hent = PerlSock_gethostbyname(name);
4662 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4665 else if (which == OP_GHBYADDR) {
4666 #ifdef HAS_GETHOSTBYADDR
4667 const int addrtype = POPi;
4668 SV * const addrsv = POPs;
4670 const char *addr = (char *)SvPVbyte(addrsv, addrlen);
4672 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4674 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4678 #ifdef HAS_GETHOSTENT
4679 hent = PerlSock_gethostent();
4681 DIE(aTHX_ PL_no_sock_func, "gethostent");
4684 #ifdef HOST_NOT_FOUND
4686 #ifdef USE_REENTRANT_API
4687 # ifdef USE_GETHOSTENT_ERRNO
4688 h_errno = PL_reentrant_buffer->_gethostent_errno;
4691 STATUS_UNIX_SET(h_errno);
4695 if (GIMME != G_ARRAY) {
4696 PUSHs(sv = sv_newmortal());
4698 if (which == OP_GHBYNAME) {
4700 sv_setpvn(sv, hent->h_addr, hent->h_length);
4703 sv_setpv(sv, (char*)hent->h_name);
4709 mPUSHs(newSVpv((char*)hent->h_name, 0));
4710 PUSHs(space_join_names_mortal(hent->h_aliases));
4711 mPUSHi(hent->h_addrtype);
4712 len = hent->h_length;
4715 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4716 mXPUSHp(*elem, len);
4720 mPUSHp(hent->h_addr, len);
4722 PUSHs(sv_mortalcopy(&PL_sv_no));
4727 DIE(aTHX_ PL_no_sock_func, "gethostent");
4733 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4735 I32 which = PL_op->op_type;
4737 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4738 struct netent *getnetbyaddr(Netdb_net_t, int);
4739 struct netent *getnetbyname(Netdb_name_t);
4740 struct netent *getnetent(void);
4742 struct netent *nent;
4744 if (which == OP_GNBYNAME){
4745 #ifdef HAS_GETNETBYNAME
4746 const char * const name = POPpbytex;
4747 nent = PerlSock_getnetbyname(name);
4749 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4752 else if (which == OP_GNBYADDR) {
4753 #ifdef HAS_GETNETBYADDR
4754 const int addrtype = POPi;
4755 const Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4756 nent = PerlSock_getnetbyaddr(addr, addrtype);
4758 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4762 #ifdef HAS_GETNETENT
4763 nent = PerlSock_getnetent();
4765 DIE(aTHX_ PL_no_sock_func, "getnetent");
4768 #ifdef HOST_NOT_FOUND
4770 #ifdef USE_REENTRANT_API
4771 # ifdef USE_GETNETENT_ERRNO
4772 h_errno = PL_reentrant_buffer->_getnetent_errno;
4775 STATUS_UNIX_SET(h_errno);
4780 if (GIMME != G_ARRAY) {
4781 PUSHs(sv = sv_newmortal());
4783 if (which == OP_GNBYNAME)
4784 sv_setiv(sv, (IV)nent->n_net);
4786 sv_setpv(sv, nent->n_name);
4792 mPUSHs(newSVpv(nent->n_name, 0));
4793 PUSHs(space_join_names_mortal(nent->n_aliases));
4794 mPUSHi(nent->n_addrtype);
4795 mPUSHi(nent->n_net);
4800 DIE(aTHX_ PL_no_sock_func, "getnetent");
4806 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4808 I32 which = PL_op->op_type;
4810 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4811 struct protoent *getprotobyname(Netdb_name_t);
4812 struct protoent *getprotobynumber(int);
4813 struct protoent *getprotoent(void);
4815 struct protoent *pent;
4817 if (which == OP_GPBYNAME) {
4818 #ifdef HAS_GETPROTOBYNAME
4819 const char* const name = POPpbytex;
4820 pent = PerlSock_getprotobyname(name);
4822 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4825 else if (which == OP_GPBYNUMBER) {
4826 #ifdef HAS_GETPROTOBYNUMBER
4827 const int number = POPi;
4828 pent = PerlSock_getprotobynumber(number);
4830 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4834 #ifdef HAS_GETPROTOENT
4835 pent = PerlSock_getprotoent();
4837 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4841 if (GIMME != G_ARRAY) {
4842 PUSHs(sv = sv_newmortal());
4844 if (which == OP_GPBYNAME)
4845 sv_setiv(sv, (IV)pent->p_proto);
4847 sv_setpv(sv, pent->p_name);
4853 mPUSHs(newSVpv(pent->p_name, 0));
4854 PUSHs(space_join_names_mortal(pent->p_aliases));
4855 mPUSHi(pent->p_proto);
4860 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4866 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4868 I32 which = PL_op->op_type;
4870 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4871 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4872 struct servent *getservbyport(int, Netdb_name_t);
4873 struct servent *getservent(void);
4875 struct servent *sent;
4877 if (which == OP_GSBYNAME) {
4878 #ifdef HAS_GETSERVBYNAME
4879 const char * const proto = POPpbytex;
4880 const char * const name = POPpbytex;
4881 sent = PerlSock_getservbyname(name, (proto && !*proto) ? NULL : proto);
4883 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4886 else if (which == OP_GSBYPORT) {
4887 #ifdef HAS_GETSERVBYPORT
4888 const char * const proto = POPpbytex;
4889 unsigned short port = (unsigned short)POPu;
4891 port = PerlSock_htons(port);
4893 sent = PerlSock_getservbyport(port, (proto && !*proto) ? NULL : proto);
4895 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4899 #ifdef HAS_GETSERVENT
4900 sent = PerlSock_getservent();
4902 DIE(aTHX_ PL_no_sock_func, "getservent");
4906 if (GIMME != G_ARRAY) {
4907 PUSHs(sv = sv_newmortal());
4909 if (which == OP_GSBYNAME) {
4911 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4913 sv_setiv(sv, (IV)(sent->s_port));
4917 sv_setpv(sv, sent->s_name);
4923 mPUSHs(newSVpv(sent->s_name, 0));
4924 PUSHs(space_join_names_mortal(sent->s_aliases));
4926 mPUSHi(PerlSock_ntohs(sent->s_port));
4928 mPUSHi(sent->s_port);
4930 mPUSHs(newSVpv(sent->s_proto, 0));
4935 DIE(aTHX_ PL_no_sock_func, "getservent");
4941 #ifdef HAS_SETHOSTENT
4943 PerlSock_sethostent(TOPi);
4946 DIE(aTHX_ PL_no_sock_func, "sethostent");
4952 #ifdef HAS_SETNETENT
4954 (void)PerlSock_setnetent(TOPi);
4957 DIE(aTHX_ PL_no_sock_func, "setnetent");
4963 #ifdef HAS_SETPROTOENT
4965 (void)PerlSock_setprotoent(TOPi);
4968 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4974 #ifdef HAS_SETSERVENT
4976 (void)PerlSock_setservent(TOPi);
4979 DIE(aTHX_ PL_no_sock_func, "setservent");
4985 #ifdef HAS_ENDHOSTENT
4987 PerlSock_endhostent();
4991 DIE(aTHX_ PL_no_sock_func, "endhostent");
4997 #ifdef HAS_ENDNETENT
4999 PerlSock_endnetent();
5003 DIE(aTHX_ PL_no_sock_func, "endnetent");
5009 #ifdef HAS_ENDPROTOENT
5011 PerlSock_endprotoent();
5015 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5021 #ifdef HAS_ENDSERVENT
5023 PerlSock_endservent();
5027 DIE(aTHX_ PL_no_sock_func, "endservent");
5035 I32 which = PL_op->op_type;
5037 struct passwd *pwent = NULL;
5039 * We currently support only the SysV getsp* shadow password interface.
5040 * The interface is declared in <shadow.h> and often one needs to link
5041 * with -lsecurity or some such.
5042 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5045 * AIX getpwnam() is clever enough to return the encrypted password
5046 * only if the caller (euid?) is root.
5048 * There are at least three other shadow password APIs. Many platforms
5049 * seem to contain more than one interface for accessing the shadow
5050 * password databases, possibly for compatibility reasons.
5051 * The getsp*() is by far he simplest one, the other two interfaces
5052 * are much more complicated, but also very similar to each other.
5057 * struct pr_passwd *getprpw*();
5058 * The password is in
5059 * char getprpw*(...).ufld.fd_encrypt[]
5060 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5065 * struct es_passwd *getespw*();
5066 * The password is in
5067 * char *(getespw*(...).ufld.fd_encrypt)
5068 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5071 * struct userpw *getuserpw();
5072 * The password is in
5073 * char *(getuserpw(...)).spw_upw_passwd
5074 * (but the de facto standard getpwnam() should work okay)
5076 * Mention I_PROT here so that Configure probes for it.
5078 * In HP-UX for getprpw*() the manual page claims that one should include
5079 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5080 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5081 * and pp_sys.c already includes <shadow.h> if there is such.
5083 * Note that <sys/security.h> is already probed for, but currently
5084 * it is only included in special cases.
5086 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5087 * be preferred interface, even though also the getprpw*() interface
5088 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5089 * One also needs to call set_auth_parameters() in main() before
5090 * doing anything else, whether one is using getespw*() or getprpw*().
5092 * Note that accessing the shadow databases can be magnitudes
5093 * slower than accessing the standard databases.
5098 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5099 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5100 * the pw_comment is left uninitialized. */
5101 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5107 const char* const name = POPpbytex;
5108 pwent = getpwnam(name);
5114 pwent = getpwuid(uid);
5118 # ifdef HAS_GETPWENT
5120 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5121 if (pwent) pwent = getpwnam(pwent->pw_name);
5124 DIE(aTHX_ PL_no_func, "getpwent");
5130 if (GIMME != G_ARRAY) {
5131 PUSHs(sv = sv_newmortal());
5133 if (which == OP_GPWNAM)
5134 # if Uid_t_sign <= 0
5135 sv_setiv(sv, (IV)pwent->pw_uid);
5137 sv_setuv(sv, (UV)pwent->pw_uid);
5140 sv_setpv(sv, pwent->pw_name);
5146 mPUSHs(newSVpv(pwent->pw_name, 0));
5150 /* If we have getspnam(), we try to dig up the shadow
5151 * password. If we are underprivileged, the shadow
5152 * interface will set the errno to EACCES or similar,
5153 * and return a null pointer. If this happens, we will
5154 * use the dummy password (usually "*" or "x") from the
5155 * standard password database.
5157 * In theory we could skip the shadow call completely
5158 * if euid != 0 but in practice we cannot know which
5159 * security measures are guarding the shadow databases
5160 * on a random platform.
5162 * Resist the urge to use additional shadow interfaces.
5163 * Divert the urge to writing an extension instead.
5166 /* Some AIX setups falsely(?) detect some getspnam(), which
5167 * has a different API than the Solaris/IRIX one. */
5168 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5170 const int saverrno = errno;
5171 const struct spwd * const spwent = getspnam(pwent->pw_name);
5172 /* Save and restore errno so that
5173 * underprivileged attempts seem
5174 * to have never made the unsccessful
5175 * attempt to retrieve the shadow password. */
5177 if (spwent && spwent->sp_pwdp)
5178 sv_setpv(sv, spwent->sp_pwdp);
5182 if (!SvPOK(sv)) /* Use the standard password, then. */
5183 sv_setpv(sv, pwent->pw_passwd);
5186 # ifndef INCOMPLETE_TAINTS
5187 /* passwd is tainted because user himself can diddle with it.
5188 * admittedly not much and in a very limited way, but nevertheless. */
5192 # if Uid_t_sign <= 0
5193 mPUSHi(pwent->pw_uid);
5195 mPUSHu(pwent->pw_uid);
5198 # if Uid_t_sign <= 0
5199 mPUSHi(pwent->pw_gid);
5201 mPUSHu(pwent->pw_gid);
5203 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5204 * because of the poor interface of the Perl getpw*(),
5205 * not because there's some standard/convention saying so.
5206 * A better interface would have been to return a hash,
5207 * but we are accursed by our history, alas. --jhi. */
5209 mPUSHi(pwent->pw_change);
5212 mPUSHi(pwent->pw_quota);
5215 mPUSHs(newSVpv(pwent->pw_age, 0));
5217 /* I think that you can never get this compiled, but just in case. */
5218 PUSHs(sv_mortalcopy(&PL_sv_no));
5223 /* pw_class and pw_comment are mutually exclusive--.
5224 * see the above note for pw_change, pw_quota, and pw_age. */
5226 mPUSHs(newSVpv(pwent->pw_class, 0));
5229 mPUSHs(newSVpv(pwent->pw_comment, 0));
5231 /* I think that you can never get this compiled, but just in case. */
5232 PUSHs(sv_mortalcopy(&PL_sv_no));
5237 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_gecos, 0)));
5239 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5241 # ifndef INCOMPLETE_TAINTS
5242 /* pw_gecos is tainted because user himself can diddle with it. */
5246 mPUSHs(newSVpv(pwent->pw_dir, 0));
5248 PUSHs(sv = sv_2mortal(newSVpv(pwent->pw_shell, 0)));
5249 # ifndef INCOMPLETE_TAINTS
5250 /* pw_shell is tainted because user himself can diddle with it. */
5255 mPUSHi(pwent->pw_expire);
5260 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5266 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5271 DIE(aTHX_ PL_no_func, "setpwent");
5277 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5282 DIE(aTHX_ PL_no_func, "endpwent");
5290 const I32 which = PL_op->op_type;
5291 const struct group *grent;
5293 if (which == OP_GGRNAM) {
5294 const char* const name = POPpbytex;
5295 grent = (const struct group *)getgrnam(name);
5297 else if (which == OP_GGRGID) {
5298 const Gid_t gid = POPi;
5299 grent = (const struct group *)getgrgid(gid);
5303 grent = (struct group *)getgrent();
5305 DIE(aTHX_ PL_no_func, "getgrent");
5309 if (GIMME != G_ARRAY) {
5310 SV * const sv = sv_newmortal();
5314 if (which == OP_GGRNAM)
5315 sv_setiv(sv, (IV)grent->gr_gid);
5317 sv_setpv(sv, grent->gr_name);
5323 mPUSHs(newSVpv(grent->gr_name, 0));
5326 mPUSHs(newSVpv(grent->gr_passwd, 0));
5328 PUSHs(sv_mortalcopy(&PL_sv_no));
5331 mPUSHi(grent->gr_gid);
5333 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5334 /* In UNICOS/mk (_CRAYMPP) the multithreading
5335 * versions (getgrnam_r, getgrgid_r)
5336 * seem to return an illegal pointer
5337 * as the group members list, gr_mem.
5338 * getgrent() doesn't even have a _r version
5339 * but the gr_mem is poisonous anyway.
5340 * So yes, you cannot get the list of group
5341 * members if building multithreaded in UNICOS/mk. */
5342 PUSHs(space_join_names_mortal(grent->gr_mem));
5348 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5354 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5359 DIE(aTHX_ PL_no_func, "setgrent");
5365 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5370 DIE(aTHX_ PL_no_func, "endgrent");
5380 if (!(tmps = PerlProc_getlogin()))
5382 PUSHp(tmps, strlen(tmps));
5385 DIE(aTHX_ PL_no_func, "getlogin");
5389 /* Miscellaneous. */
5394 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5395 register I32 items = SP - MARK;
5396 unsigned long a[20];
5401 while (++MARK <= SP) {
5402 if (SvTAINTED(*MARK)) {
5408 TAINT_PROPER("syscall");
5411 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5412 * or where sizeof(long) != sizeof(char*). But such machines will
5413 * not likely have syscall implemented either, so who cares?
5415 while (++MARK <= SP) {
5416 if (SvNIOK(*MARK) || !i)
5417 a[i++] = SvIV(*MARK);
5418 else if (*MARK == &PL_sv_undef)
5421 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5427 DIE(aTHX_ "Too many args to syscall");
5429 DIE(aTHX_ "Too few args to syscall");
5431 retval = syscall(a[0]);
5434 retval = syscall(a[0],a[1]);
5437 retval = syscall(a[0],a[1],a[2]);
5440 retval = syscall(a[0],a[1],a[2],a[3]);
5443 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5446 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5449 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5452 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5456 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5459 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5462 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5466 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5470 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5474 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5475 a[10],a[11],a[12],a[13]);
5477 #endif /* atarist */
5483 DIE(aTHX_ PL_no_func, "syscall");
5487 #ifdef FCNTL_EMULATE_FLOCK
5489 /* XXX Emulate flock() with fcntl().
5490 What's really needed is a good file locking module.
5494 fcntl_emulate_flock(int fd, int operation)
5498 switch (operation & ~LOCK_NB) {
5500 flock.l_type = F_RDLCK;
5503 flock.l_type = F_WRLCK;
5506 flock.l_type = F_UNLCK;
5512 flock.l_whence = SEEK_SET;
5513 flock.l_start = flock.l_len = (Off_t)0;
5515 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5518 #endif /* FCNTL_EMULATE_FLOCK */
5520 #ifdef LOCKF_EMULATE_FLOCK
5522 /* XXX Emulate flock() with lockf(). This is just to increase
5523 portability of scripts. The calls are not completely
5524 interchangeable. What's really needed is a good file
5528 /* The lockf() constants might have been defined in <unistd.h>.
5529 Unfortunately, <unistd.h> causes troubles on some mixed
5530 (BSD/POSIX) systems, such as SunOS 4.1.3.
5532 Further, the lockf() constants aren't POSIX, so they might not be
5533 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5534 just stick in the SVID values and be done with it. Sigh.
5538 # define F_ULOCK 0 /* Unlock a previously locked region */
5541 # define F_LOCK 1 /* Lock a region for exclusive use */
5544 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5547 # define F_TEST 3 /* Test a region for other processes locks */
5551 lockf_emulate_flock(int fd, int operation)
5554 const int save_errno = errno;
5557 /* flock locks entire file so for lockf we need to do the same */
5558 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5559 if (pos > 0) /* is seekable and needs to be repositioned */
5560 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5561 pos = -1; /* seek failed, so don't seek back afterwards */
5564 switch (operation) {
5566 /* LOCK_SH - get a shared lock */
5568 /* LOCK_EX - get an exclusive lock */
5570 i = lockf (fd, F_LOCK, 0);
5573 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5574 case LOCK_SH|LOCK_NB:
5575 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5576 case LOCK_EX|LOCK_NB:
5577 i = lockf (fd, F_TLOCK, 0);
5579 if ((errno == EAGAIN) || (errno == EACCES))
5580 errno = EWOULDBLOCK;
5583 /* LOCK_UN - unlock (non-blocking is a no-op) */
5585 case LOCK_UN|LOCK_NB:
5586 i = lockf (fd, F_ULOCK, 0);
5589 /* Default - can't decipher operation */
5596 if (pos > 0) /* need to restore position of the handle */
5597 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5602 #endif /* LOCKF_EMULATE_FLOCK */
5606 * c-indentation-style: bsd
5608 * indent-tabs-mode: t
5611 * ex: set ts=8 sts=4 sw=4 noet: