3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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.
18 /* This file contains system pp ("push/pop") functions that
19 * execute the opcodes that make up a perl program. A typical pp function
20 * expects to find its arguments on the stack, and usually pushes its
21 * results onto the stack, hence the 'pp' terminology. Each OP structure
22 * contains a pointer to the relevant pp_foo() function.
24 * By 'system', we mean ops which interact with the OS, such as pp_open().
28 #define PERL_IN_PP_SYS_C
32 /* Shadow password support for solaris - pdo@cs.umd.edu
33 * Not just Solaris: at least HP-UX, IRIX, Linux.
34 * The API is from SysV.
36 * There are at least two more shadow interfaces,
37 * see the comments in pp_gpwent().
41 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
42 * and another MAXINT from "perl.h" <- <sys/param.h>. */
50 extern "C" int syscall(unsigned long,...);
55 # include <sys/wait.h>
59 # include <sys/resource.h>
68 # include <sys/select.h>
72 /* XXX Configure test needed.
73 h_errno might not be a simple 'int', especially for multi-threaded
74 applications, see "extern int errno in perl.h". Creating such
75 a test requires taking into account the differences between
76 compiling multithreaded and singlethreaded ($ccflags et al).
77 HOST_NOT_FOUND is typically defined in <netdb.h>.
79 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
88 struct passwd *getpwnam (char *);
89 struct passwd *getpwuid (Uid_t);
94 struct passwd *getpwent (void);
95 #elif defined (VMS) && defined (my_getpwent)
96 struct passwd *Perl_my_getpwent (void);
105 struct group *getgrnam (char *);
106 struct group *getgrgid (Gid_t);
110 struct group *getgrent (void);
116 # if defined(_MSC_VER) || defined(__MINGW32__)
117 # include <sys/utime.h>
124 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
127 # define my_chsize PerlLIO_chsize
132 #else /* no flock() */
134 /* fcntl.h might not have been included, even if it exists, because
135 the current Configure only sets I_FCNTL if it's needed to pick up
136 the *_OK constants. Make sure it has been included before testing
137 the fcntl() locking constants. */
138 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
142 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
143 # define FLOCK fcntl_emulate_flock
144 # define FCNTL_EMULATE_FLOCK
145 # else /* no flock() or fcntl(F_SETLK,...) */
147 # define FLOCK lockf_emulate_flock
148 # define LOCKF_EMULATE_FLOCK
150 # endif /* no flock() or fcntl(F_SETLK,...) */
153 static int FLOCK (int, int);
156 * These are the flock() constants. Since this sytems doesn't have
157 * flock(), the values of the constants are probably not available.
171 # endif /* emulating flock() */
173 #endif /* no flock() */
176 static char zero_but_true[ZBTLEN + 1] = "0 but true";
178 #if defined(I_SYS_ACCESS) && !defined(R_OK)
179 # include <sys/access.h>
182 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
183 # define FD_CLOEXEC 1 /* NeXT needs this */
189 /* Missing protos on LynxOS */
190 void sethostent(int);
191 void endhostent(void);
193 void endnetent(void);
194 void setprotoent(int);
195 void endprotoent(void);
196 void setservent(int);
197 void endservent(void);
200 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
201 #undef PERL_EFF_ACCESS_W_OK
202 #undef PERL_EFF_ACCESS_X_OK
204 /* F_OK unused: if stat() cannot find it... */
206 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
207 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
208 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
209 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
210 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
213 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
214 # ifdef I_SYS_SECURITY
215 # include <sys/security.h>
219 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
220 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
221 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
225 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
226 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
230 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
232 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
234 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 Uid_t ruid = getuid();
245 Uid_t euid = geteuid();
246 Gid_t rgid = getgid();
247 Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
301 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
302 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
305 #if !defined(PERL_EFF_ACCESS_R_OK)
306 /* With it or without it: anyway you get a warning: either that
307 it is unused, or it is declared static and never defined.
310 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
312 Perl_croak(aTHX_ "switching effective uid is not implemented");
328 if (PL_op->op_private & OPpOPEN_IN_RAW)
330 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
332 fp = PerlProc_popen(tmps, mode);
335 if (PL_curcop->cop_io) {
336 type = SvPV_nolen(PL_curcop->cop_io);
339 PerlIO_apply_layers(aTHX_ fp,mode,type);
341 if (gimme == G_VOID) {
343 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
347 else if (gimme == G_SCALAR) {
349 PL_rs = &PL_sv_undef;
350 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
351 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
363 if (sv_gets(sv, fp, 0) == Nullch) {
367 XPUSHs(sv_2mortal(sv));
368 if (SvLEN(sv) - SvCUR(sv) > 20) {
369 SvLEN_set(sv, SvCUR(sv)+1);
370 Renew(SvPVX(sv), SvLEN(sv), char);
375 STATUS_NATIVE_SET(PerlProc_pclose(fp));
376 TAINT; /* "I believe that this is not gratuitous!" */
379 STATUS_NATIVE_SET(-1);
380 if (gimme == G_SCALAR)
390 tryAMAGICunTARGET(iter, -1);
392 /* Note that we only ever get here if File::Glob fails to load
393 * without at the same time croaking, for some reason, or if
394 * perl was built with PERL_EXTERNAL_GLOB */
401 * The external globbing program may use things we can't control,
402 * so for security reasons we must assume the worst.
405 taint_proper(PL_no_security, "glob");
409 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
410 PL_last_in_gv = (GV*)*PL_stack_sp--;
412 SAVESPTR(PL_rs); /* This is not permanent, either. */
413 PL_rs = sv_2mortal(newSVpvn("\000", 1));
416 *SvPVX(PL_rs) = '\n';
420 result = do_readline();
427 PL_last_in_gv = cGVOP_gv;
428 return do_readline();
437 if (SP - MARK != 1) {
439 do_join(TARG, &PL_sv_no, MARK, SP);
446 tmps = SvPV(tmpsv, len);
447 if ((!tmps || !len) && PL_errgv) {
449 (void)SvUPGRADE(error, SVt_PV);
450 if (SvPOK(error) && SvCUR(error))
451 sv_catpv(error, "\t...caught");
453 tmps = SvPV(tmpsv, len);
456 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
458 Perl_warn(aTHX_ "%"SVf, tmpsv);
470 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
472 if (SP - MARK != 1) {
474 do_join(TARG, &PL_sv_no, MARK, SP);
476 tmps = SvPV(tmpsv, len);
482 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
486 (void)SvUPGRADE(error, SVt_PV);
487 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
489 SvSetSV(error,tmpsv);
490 else if (sv_isobject(error)) {
491 HV *stash = SvSTASH(SvRV(error));
492 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
494 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
495 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
502 call_sv((SV*)GvCV(gv),
503 G_SCALAR|G_EVAL|G_KEEPERR);
504 sv_setsv(error,*PL_stack_sp--);
507 DIE(aTHX_ Nullformat);
510 if (SvPOK(error) && SvCUR(error))
511 sv_catpv(error, "\t...propagated");
513 tmps = SvPV(tmpsv, len);
517 tmpsv = sv_2mortal(newSVpvn("Died", 4));
519 DIE(aTHX_ "%"SVf, tmpsv);
539 DIE(aTHX_ PL_no_usym, "filehandle");
540 if ((io = GvIOp(gv)))
541 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
543 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
544 /* Method's args are same as ours ... */
545 /* ... except handle is replaced by the object */
546 *MARK-- = SvTIED_obj((SV*)io, mg);
550 call_method("OPEN", G_SCALAR);
563 tmps = SvPV(sv, len);
564 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
567 PUSHi( (I32)PL_forkprocess );
568 else if (PL_forkprocess == 0) /* we are a new child */
587 if (gv && (io = GvIO(gv))
588 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
591 XPUSHs(SvTIED_obj((SV*)io, mg));
594 call_method("CLOSE", G_SCALAR);
600 PUSHs(boolSV(do_close(gv, TRUE)));
620 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
621 DIE(aTHX_ PL_no_usym, "filehandle");
626 do_close(rgv, FALSE);
628 do_close(wgv, FALSE);
630 if (PerlProc_pipe(fd) < 0)
633 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
634 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
635 IoOFP(rstio) = IoIFP(rstio);
636 IoIFP(wstio) = IoOFP(wstio);
637 IoTYPE(rstio) = IoTYPE_RDONLY;
638 IoTYPE(wstio) = IoTYPE_WRONLY;
640 if (!IoIFP(rstio) || !IoOFP(wstio)) {
641 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
642 else PerlLIO_close(fd[0]);
643 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
644 else PerlLIO_close(fd[1]);
647 #if defined(HAS_FCNTL) && defined(F_SETFD)
648 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
649 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
656 DIE(aTHX_ PL_no_func, "pipe");
672 if (gv && (io = GvIO(gv))
673 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
676 XPUSHs(SvTIED_obj((SV*)io, mg));
679 call_method("FILENO", G_SCALAR);
685 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
686 /* Can't do this because people seem to do things like
687 defined(fileno($foo)) to check whether $foo is a valid fh.
688 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
689 report_evil_fh(gv, io, PL_op->op_type);
694 PUSHi(PerlIO_fileno(fp));
705 anum = PerlLIO_umask(0);
706 (void)PerlLIO_umask(anum);
709 anum = PerlLIO_umask(POPi);
710 TAINT_PROPER("umask");
713 /* Only DIE if trying to restrict permissions on `user' (self).
714 * Otherwise it's harmless and more useful to just return undef
715 * since 'group' and 'other' concepts probably don't exist here. */
716 if (MAXARG >= 1 && (POPi & 0700))
717 DIE(aTHX_ "umask not implemented");
718 XPUSHs(&PL_sv_undef);
740 if (gv && (io = GvIO(gv))
741 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
744 XPUSHs(SvTIED_obj((SV*)io, mg));
749 call_method("BINMODE", G_SCALAR);
756 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
757 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
758 report_evil_fh(gv, io, PL_op->op_type);
759 SETERRNO(EBADF,RMS_IFI);
764 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
765 (discp) ? SvPV_nolen(discp) : Nullch)) {
766 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
767 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
768 mode_from_discipline(discp),
769 (discp) ? SvPV_nolen(discp) : Nullch)) {
791 I32 markoff = MARK - PL_stack_base;
793 int how = PERL_MAGIC_tied;
797 switch(SvTYPE(varsv)) {
799 methname = "TIEHASH";
800 HvEITER((HV *)varsv) = Null(HE *);
803 methname = "TIEARRAY";
806 #ifdef GV_UNIQUE_CHECK
807 if (GvUNIQUE((GV*)varsv)) {
808 Perl_croak(aTHX_ "Attempt to tie unique GV");
811 methname = "TIEHANDLE";
812 how = PERL_MAGIC_tiedscalar;
813 /* For tied filehandles, we apply tiedscalar magic to the IO
814 slot of the GP rather than the GV itself. AMS 20010812 */
816 GvIOp(varsv) = newIO();
817 varsv = (SV *)GvIOp(varsv);
820 methname = "TIESCALAR";
821 how = PERL_MAGIC_tiedscalar;
825 if (sv_isobject(*MARK)) {
827 PUSHSTACKi(PERLSI_MAGIC);
829 EXTEND(SP,(I32)items);
833 call_method(methname, G_SCALAR);
836 /* Not clear why we don't call call_method here too.
837 * perhaps to get different error message ?
839 stash = gv_stashsv(*MARK, FALSE);
840 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
841 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
845 PUSHSTACKi(PERLSI_MAGIC);
847 EXTEND(SP,(I32)items);
851 call_sv((SV*)GvCV(gv), G_SCALAR);
857 if (sv_isobject(sv)) {
858 sv_unmagic(varsv, how);
859 /* Croak if a self-tie on an aggregate is attempted. */
860 if (varsv == SvRV(sv) &&
861 (SvTYPE(varsv) == SVt_PVAV ||
862 SvTYPE(varsv) == SVt_PVHV))
864 "Self-ties of arrays and hashes are not supported");
865 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
868 SP = PL_stack_base + markoff;
878 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
879 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
881 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
884 if ((mg = SvTIED_mg(sv, how))) {
885 SV *obj = SvRV(SvTIED_obj(sv, mg));
889 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
890 isGV(gv) && (cv = GvCV(gv))) {
892 XPUSHs(SvTIED_obj((SV*)gv, mg));
893 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
896 call_sv((SV *)cv, G_VOID);
900 else if (ckWARN(WARN_UNTIE)) {
901 if (mg && SvREFCNT(obj) > 1)
902 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
903 "untie attempted while %"UVuf" inner references still exist",
904 (UV)SvREFCNT(obj) - 1 ) ;
908 sv_unmagic(sv, how) ;
917 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
918 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
920 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
923 if ((mg = SvTIED_mg(sv, how))) {
924 SV *osv = SvTIED_obj(sv, mg);
925 if (osv == mg->mg_obj)
926 osv = sv_mortalcopy(osv);
944 sv = sv_mortalcopy(&PL_sv_no);
945 sv_setpv(sv, "AnyDBM_File");
946 stash = gv_stashsv(sv, FALSE);
947 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
949 require_pv("AnyDBM_File.pm");
951 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
952 DIE(aTHX_ "No dbm on this machine");
962 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
964 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
967 call_sv((SV*)GvCV(gv), G_SCALAR);
970 if (!sv_isobject(TOPs)) {
975 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
978 call_sv((SV*)GvCV(gv), G_SCALAR);
982 if (sv_isobject(TOPs)) {
983 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
984 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1006 struct timeval timebuf;
1007 struct timeval *tbuf = &timebuf;
1011 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1016 # if BYTEORDER & 0xf0000
1017 # define ORDERBYTE (0x88888888 - BYTEORDER)
1019 # define ORDERBYTE (0x4444 - BYTEORDER)
1025 for (i = 1; i <= 3; i++) {
1033 /* little endians can use vecs directly */
1034 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1041 masksize = NFDBITS / NBBY;
1043 masksize = sizeof(long); /* documented int, everyone seems to use long */
1045 Zero(&fd_sets[0], 4, char*);
1048 # if SELECT_MIN_BITS == 1
1049 growsize = sizeof(fd_set);
1051 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1052 # undef SELECT_MIN_BITS
1053 # define SELECT_MIN_BITS __FD_SETSIZE
1055 /* If SELECT_MIN_BITS is greater than one we most probably will want
1056 * to align the sizes with SELECT_MIN_BITS/8 because for example
1057 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1058 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1059 * on (sets/tests/clears bits) is 32 bits. */
1060 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1068 timebuf.tv_sec = (long)value;
1069 value -= (NV)timebuf.tv_sec;
1070 timebuf.tv_usec = (long)(value * 1000000.0);
1073 tbuf = Null(struct timeval*);
1075 for (i = 1; i <= 3; i++) {
1081 else if (!SvPOK(sv))
1082 SvPV_force(sv,n_a); /* force string conversion */
1085 Sv_Grow(sv, growsize);
1089 while (++j <= growsize) {
1093 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1095 New(403, fd_sets[i], growsize, char);
1096 for (offset = 0; offset < growsize; offset += masksize) {
1097 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1098 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1101 fd_sets[i] = SvPVX(sv);
1105 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1106 /* Can't make just the (void*) conditional because that would be
1107 * cpp #if within cpp macro, and not all compilers like that. */
1108 nfound = PerlSock_select(
1110 (Select_fd_set_t) fd_sets[1],
1111 (Select_fd_set_t) fd_sets[2],
1112 (Select_fd_set_t) fd_sets[3],
1113 (void*) tbuf); /* Workaround for compiler bug. */
1115 nfound = PerlSock_select(
1117 (Select_fd_set_t) fd_sets[1],
1118 (Select_fd_set_t) fd_sets[2],
1119 (Select_fd_set_t) fd_sets[3],
1122 for (i = 1; i <= 3; i++) {
1125 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1127 for (offset = 0; offset < growsize; offset += masksize) {
1128 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1129 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1131 Safefree(fd_sets[i]);
1138 if (GIMME == G_ARRAY && tbuf) {
1139 value = (NV)(timebuf.tv_sec) +
1140 (NV)(timebuf.tv_usec) / 1000000.0;
1141 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1142 sv_setnv(sv, value);
1146 DIE(aTHX_ "select not implemented");
1151 Perl_setdefout(pTHX_ GV *gv)
1154 (void)SvREFCNT_inc(gv);
1156 SvREFCNT_dec(PL_defoutgv);
1163 GV *newdefout, *egv;
1166 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1168 egv = GvEGV(PL_defoutgv);
1173 XPUSHs(&PL_sv_undef);
1175 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1176 if (gvp && *gvp == egv) {
1177 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1181 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1186 if (!GvIO(newdefout))
1187 gv_IOadd(newdefout);
1188 setdefout(newdefout);
1206 if (gv && (io = GvIO(gv))
1207 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1209 I32 gimme = GIMME_V;
1211 XPUSHs(SvTIED_obj((SV*)io, mg));
1214 call_method("GETC", gimme);
1217 if (gimme == G_SCALAR)
1218 SvSetMagicSV_nosteal(TARG, TOPs);
1221 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1222 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1223 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1224 report_evil_fh(gv, io, PL_op->op_type);
1225 SETERRNO(EBADF,RMS_IFI);
1229 sv_setpv(TARG, " ");
1230 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1231 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1232 /* Find out how many bytes the char needs */
1233 Size_t len = UTF8SKIP(SvPVX(TARG));
1236 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1237 SvCUR_set(TARG,1+len);
1247 return pp_sysread();
1251 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1253 register PERL_CONTEXT *cx;
1254 I32 gimme = GIMME_V;
1259 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1261 cx->blk_sub.retop = retop;
1262 PAD_SET_CUR(CvPADLIST(cv), 1);
1264 setdefout(gv); /* locally select filehandle so $% et al work */
1297 SV *tmpsv = sv_newmortal();
1298 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1299 name = SvPV_nolen(tmpsv);
1302 DIE(aTHX_ "Undefined format \"%s\" called", name);
1303 DIE(aTHX_ "Not a format reference");
1306 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1308 IoFLAGS(io) &= ~IOf_DIDTOP;
1309 return doform(cv,gv,PL_op->op_next);
1315 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1316 register IO *io = GvIOp(gv);
1317 PerlIO *ofp = IoOFP(io);
1321 register PERL_CONTEXT *cx;
1323 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1324 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1327 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1328 PL_formtarget != PL_toptarget)
1332 if (!IoTOP_GV(io)) {
1336 if (!IoTOP_NAME(io)) {
1337 if (!IoFMT_NAME(io))
1338 IoFMT_NAME(io) = savepv(GvNAME(gv));
1339 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1340 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1341 if ((topgv && GvFORM(topgv)) ||
1342 !gv_fetchpv("top",FALSE,SVt_PVFM))
1343 IoTOP_NAME(io) = savepv(SvPVX(topname));
1345 IoTOP_NAME(io) = savepv("top");
1347 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1348 if (!topgv || !GvFORM(topgv)) {
1349 IoLINES_LEFT(io) = 100000000;
1352 IoTOP_GV(io) = topgv;
1354 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1355 I32 lines = IoLINES_LEFT(io);
1356 char *s = SvPVX(PL_formtarget);
1357 if (lines <= 0) /* Yow, header didn't even fit!!! */
1359 while (lines-- > 0) {
1360 s = strchr(s, '\n');
1366 STRLEN save = SvCUR(PL_formtarget);
1367 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1368 do_print(PL_formtarget, ofp);
1369 SvCUR_set(PL_formtarget, save);
1370 sv_chop(PL_formtarget, s);
1371 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1374 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1375 do_print(PL_formfeed, ofp);
1376 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1378 PL_formtarget = PL_toptarget;
1379 IoFLAGS(io) |= IOf_DIDTOP;
1382 DIE(aTHX_ "bad top format reference");
1387 SV *sv = sv_newmortal();
1388 gv_efullname4(sv, fgv, Nullch, FALSE);
1389 name = SvPV_nolen(sv);
1392 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1395 DIE(aTHX_ "Undefined top format called");
1399 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1400 return doform(cv,gv,PL_op);
1404 POPBLOCK(cx,PL_curpm);
1410 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1412 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1413 else if (ckWARN(WARN_CLOSED))
1414 report_evil_fh(gv, io, PL_op->op_type);
1419 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1420 if (ckWARN(WARN_IO))
1421 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1423 if (!do_print(PL_formtarget, fp))
1426 FmLINES(PL_formtarget) = 0;
1427 SvCUR_set(PL_formtarget, 0);
1428 *SvEND(PL_formtarget) = '\0';
1429 if (IoFLAGS(io) & IOf_FLUSH)
1430 (void)PerlIO_flush(fp);
1435 PL_formtarget = PL_bodytarget;
1437 return cx->blk_sub.retop;
1442 dSP; dMARK; dORIGMARK;
1449 if (PL_op->op_flags & OPf_STACKED)
1454 if (gv && (io = GvIO(gv))
1455 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1457 if (MARK == ORIGMARK) {
1460 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1464 *MARK = SvTIED_obj((SV*)io, mg);
1467 call_method("PRINTF", G_SCALAR);
1470 MARK = ORIGMARK + 1;
1477 if (!(io = GvIO(gv))) {
1478 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1479 report_evil_fh(gv, io, PL_op->op_type);
1480 SETERRNO(EBADF,RMS_IFI);
1483 else if (!(fp = IoOFP(io))) {
1484 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1486 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1487 else if (ckWARN(WARN_CLOSED))
1488 report_evil_fh(gv, io, PL_op->op_type);
1490 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1494 do_sprintf(sv, SP - MARK, MARK + 1);
1495 if (!do_print(sv, fp))
1498 if (IoFLAGS(io) & IOf_FLUSH)
1499 if (PerlIO_flush(fp) == EOF)
1510 PUSHs(&PL_sv_undef);
1531 /* Need TIEHANDLE method ? */
1533 tmps = SvPV(sv, len);
1534 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1535 IoLINES(GvIOp(gv)) = 0;
1539 PUSHs(&PL_sv_undef);
1546 dSP; dMARK; dORIGMARK; dTARGET;
1553 Sock_size_t bufsize;
1560 bool charstart = FALSE;
1561 STRLEN charskip = 0;
1565 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1566 && gv && (io = GvIO(gv))
1567 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1572 *MARK = SvTIED_obj((SV*)io, mg);
1574 call_method("READ", G_SCALAR);
1587 sv_setpvn(bufsv, "", 0);
1588 length = SvIVx(*++MARK);
1591 offset = SvIVx(*++MARK);
1595 if (!io || !IoIFP(io)) {
1596 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1597 report_evil_fh(gv, io, PL_op->op_type);
1598 SETERRNO(EBADF,RMS_IFI);
1601 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1602 buffer = SvPVutf8_force(bufsv, blen);
1603 /* UTF-8 may not have been set if they are all low bytes */
1607 buffer = SvPV_force(bufsv, blen);
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 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1676 if (offset > bufsize) { /* Zero any newly allocated space */
1677 Zero(buffer+bufsize, offset-bufsize, char);
1679 buffer = buffer + offset;
1681 if (PL_op->op_type == OP_SYSREAD) {
1682 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1683 if (IoTYPE(io) == IoTYPE_SOCKET) {
1684 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1690 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1695 #ifdef HAS_SOCKET__bad_code_maybe
1696 if (IoTYPE(io) == IoTYPE_SOCKET) {
1697 char namebuf[MAXPATHLEN];
1698 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1699 bufsize = sizeof (struct sockaddr_in);
1701 bufsize = sizeof namebuf;
1703 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1704 (struct sockaddr *)namebuf, &bufsize);
1709 count = PerlIO_read(IoIFP(io), buffer, length);
1710 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1711 if (count == 0 && PerlIO_error(IoIFP(io)))
1715 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1716 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1719 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1720 *SvEND(bufsv) = '\0';
1721 (void)SvPOK_only(bufsv);
1722 if (fp_utf8 && !IN_BYTES) {
1723 /* Look at utf8 we got back and count the characters */
1724 char *bend = buffer + count;
1725 while (buffer < bend) {
1727 skip = UTF8SKIP(buffer);
1730 if (buffer - charskip + skip > bend) {
1731 /* partial character - try for rest of it */
1732 length = skip - (bend-buffer);
1733 offset = bend - SvPVX(bufsv);
1745 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1746 provided amount read (count) was what was requested (length)
1748 if (got < wanted && count == length) {
1749 length = wanted - got;
1750 offset = bend - SvPVX(bufsv);
1753 /* return value is character count */
1758 /* This should not be marked tainted if the fp is marked clean */
1759 if (!(IoFLAGS(io) & IOf_UNTAINT))
1760 SvTAINTED_on(bufsv);
1773 int items = (SP - PL_stack_base) - TOPMARK;
1777 sv = sv_2mortal(newSViv(sv_len(*SP)));
1786 dSP; dMARK; dORIGMARK; dTARGET;
1797 if (PL_op->op_type == OP_SYSWRITE
1798 && gv && (io = GvIO(gv))
1799 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1804 *MARK = SvTIED_obj((SV*)io, mg);
1806 call_method("WRITE", G_SCALAR);
1817 #if Size_t_size > IVSIZE
1818 length = (Size_t)SvNVx(*++MARK);
1820 length = (Size_t)SvIVx(*++MARK);
1822 if ((SSize_t)length < 0)
1823 DIE(aTHX_ "Negative length");
1826 if (!io || !IoIFP(io)) {
1828 if (ckWARN(WARN_CLOSED))
1829 report_evil_fh(gv, io, PL_op->op_type);
1830 SETERRNO(EBADF,RMS_IFI);
1834 if (PerlIO_isutf8(IoIFP(io))) {
1835 if (!SvUTF8(bufsv)) {
1836 bufsv = sv_2mortal(newSVsv(bufsv));
1837 buffer = sv_2pvutf8(bufsv, &blen);
1839 buffer = SvPV(bufsv, blen);
1842 if (DO_UTF8(bufsv)) {
1843 /* Not modifying source SV, so making a temporary copy. */
1844 bufsv = sv_2mortal(newSVsv(bufsv));
1845 sv_utf8_downgrade(bufsv, FALSE);
1847 buffer = SvPV(bufsv, blen);
1850 if (PL_op->op_type == OP_SYSWRITE) {
1852 if (DO_UTF8(bufsv)) {
1853 /* length and offset are in chars */
1854 blen = sv_len_utf8(bufsv);
1857 offset = SvIVx(*++MARK);
1859 if (-offset > (IV)blen)
1860 DIE(aTHX_ "Offset outside string");
1862 } else if (offset >= (IV)blen && blen > 0)
1863 DIE(aTHX_ "Offset outside string");
1866 if (length > blen - offset)
1867 length = blen - offset;
1868 if (DO_UTF8(bufsv)) {
1869 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1870 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1873 buffer = buffer+offset;
1875 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1876 if (IoTYPE(io) == IoTYPE_SOCKET) {
1877 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1883 /* See the note at doio.c:do_print about filesize limits. --jhi */
1884 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1889 else if (SP > MARK) {
1892 sockbuf = SvPVx(*++MARK, mlen);
1893 /* length is really flags */
1894 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1895 length, (struct sockaddr *)sockbuf, mlen);
1898 /* length is really flags */
1899 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1902 DIE(aTHX_ PL_no_sock_func, "send");
1908 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1909 #if Size_t_size > IVSIZE
1923 return pp_sysread();
1934 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1936 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1938 if (io && !IoIFP(io)) {
1939 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1941 IoFLAGS(io) &= ~IOf_START;
1942 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1943 sv_setpvn(GvSV(gv), "-", 1);
1944 SvSETMAGIC(GvSV(gv));
1946 else if (!nextargv(gv))
1951 gv = PL_last_in_gv; /* eof */
1954 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1956 if (gv && (io = GvIO(gv))
1957 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1960 XPUSHs(SvTIED_obj((SV*)io, mg));
1963 call_method("EOF", G_SCALAR);
1969 PUSHs(boolSV(!gv || do_eof(gv)));
1983 gv = PL_last_in_gv = (GV*)POPs;
1985 if (gv && (io = GvIO(gv))
1986 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1989 XPUSHs(SvTIED_obj((SV*)io, mg));
1992 call_method("TELL", G_SCALAR);
1998 #if LSEEKSIZE > IVSIZE
1999 PUSHn( do_tell(gv) );
2001 PUSHi( do_tell(gv) );
2008 return pp_sysseek();
2017 #if LSEEKSIZE > IVSIZE
2018 Off_t offset = (Off_t)SvNVx(POPs);
2020 Off_t offset = (Off_t)SvIVx(POPs);
2024 gv = PL_last_in_gv = (GV*)POPs;
2026 if (gv && (io = GvIO(gv))
2027 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2030 XPUSHs(SvTIED_obj((SV*)io, mg));
2031 #if LSEEKSIZE > IVSIZE
2032 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2034 XPUSHs(sv_2mortal(newSViv(offset)));
2036 XPUSHs(sv_2mortal(newSViv(whence)));
2039 call_method("SEEK", G_SCALAR);
2045 if (PL_op->op_type == OP_SEEK)
2046 PUSHs(boolSV(do_seek(gv, offset, whence)));
2048 Off_t sought = do_sysseek(gv, offset, whence);
2050 PUSHs(&PL_sv_undef);
2053 #if LSEEKSIZE > IVSIZE
2058 : newSVpvn(zero_but_true, ZBTLEN);
2059 PUSHs(sv_2mortal(sv));
2068 /* There seems to be no consensus on the length type of truncate()
2069 * and ftruncate(), both off_t and size_t have supporters. In
2070 * general one would think that when using large files, off_t is
2071 * at least as wide as size_t, so using an off_t should be okay. */
2072 /* XXX Configure probe for the length type of *truncate() needed XXX */
2075 #if Off_t_size > IVSIZE
2080 /* Checking for length < 0 is problematic as the type might or
2081 * might not be signed: if it is not, clever compilers will moan. */
2082 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2084 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2091 if (PL_op->op_flags & OPf_SPECIAL) {
2092 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2101 TAINT_PROPER("truncate");
2102 if (!(fp = IoIFP(io))) {
2108 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2110 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2120 if (SvTYPE(sv) == SVt_PVGV) {
2121 tmpgv = (GV*)sv; /* *main::FRED for example */
2122 goto do_ftruncate_gv;
2124 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2125 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2126 goto do_ftruncate_gv;
2128 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2129 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2130 goto do_ftruncate_io;
2133 name = SvPV(sv, n_a);
2134 TAINT_PROPER("truncate");
2136 if (truncate(name, len) < 0)
2142 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2145 if (my_chsize(tmpfd, len) < 0)
2147 PerlLIO_close(tmpfd);
2156 SETERRNO(EBADF,RMS_IFI);
2160 DIE(aTHX_ "truncate not implemented");
2173 unsigned int func = POPu;
2174 int optype = PL_op->op_type;
2178 IO *io = gv ? GvIOn(gv) : 0;
2180 if (!io || !argsv || !IoIFP(io)) {
2181 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2182 report_evil_fh(gv, io, PL_op->op_type);
2183 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2187 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2190 s = SvPV_force(argsv, len);
2191 need = IOCPARM_LEN(func);
2193 s = Sv_Grow(argsv, need + 1);
2194 SvCUR_set(argsv, need);
2197 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2200 retval = SvIV(argsv);
2201 s = INT2PTR(char*,retval); /* ouch */
2204 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2206 if (optype == OP_IOCTL)
2208 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2210 DIE(aTHX_ "ioctl is not implemented");
2214 DIE(aTHX_ "fcntl is not implemented");
2216 #if defined(OS2) && defined(__EMX__)
2217 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2219 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2223 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2225 if (s[SvCUR(argsv)] != 17)
2226 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2228 s[SvCUR(argsv)] = 0; /* put our null back */
2229 SvSETMAGIC(argsv); /* Assume it has changed */
2238 PUSHp(zero_but_true, ZBTLEN);
2259 if (gv && (io = GvIO(gv)))
2266 (void)PerlIO_flush(fp);
2267 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2270 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2271 report_evil_fh(gv, io, PL_op->op_type);
2273 SETERRNO(EBADF,RMS_IFI);
2278 DIE(aTHX_ PL_no_func, "flock()");
2290 int protocol = POPi;
2296 io = gv ? GvIOn(gv) : NULL;
2299 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2300 report_evil_fh(gv, io, PL_op->op_type);
2302 do_close(gv, FALSE);
2303 SETERRNO(EBADF,LIB_INVARG);
2308 do_close(gv, FALSE);
2310 TAINT_PROPER("socket");
2311 fd = PerlSock_socket(domain, type, protocol);
2314 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2315 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2316 IoTYPE(io) = IoTYPE_SOCKET;
2317 if (!IoIFP(io) || !IoOFP(io)) {
2318 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2319 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2320 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2323 #if defined(HAS_FCNTL) && defined(F_SETFD)
2324 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2328 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2333 DIE(aTHX_ PL_no_sock_func, "socket");
2339 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2345 int protocol = POPi;
2352 io1 = gv1 ? GvIOn(gv1) : NULL;
2353 io2 = gv2 ? GvIOn(gv2) : NULL;
2354 if (!gv1 || !gv2 || !io1 || !io2) {
2355 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2357 report_evil_fh(gv1, io1, PL_op->op_type);
2359 report_evil_fh(gv1, io2, PL_op->op_type);
2362 do_close(gv1, FALSE);
2364 do_close(gv2, FALSE);
2369 do_close(gv1, FALSE);
2371 do_close(gv2, FALSE);
2373 TAINT_PROPER("socketpair");
2374 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2376 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2377 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2378 IoTYPE(io1) = IoTYPE_SOCKET;
2379 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2380 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2381 IoTYPE(io2) = IoTYPE_SOCKET;
2382 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2383 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2384 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2385 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2386 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2387 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2388 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2391 #if defined(HAS_FCNTL) && defined(F_SETFD)
2392 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2393 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2398 DIE(aTHX_ PL_no_sock_func, "socketpair");
2406 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2407 extern void GETPRIVMODE();
2408 extern void GETUSERMODE();
2413 register IO *io = GvIOn(gv);
2417 int mpeprivmode = 0;
2420 if (!io || !IoIFP(io))
2423 addr = SvPV(addrsv, len);
2424 TAINT_PROPER("bind");
2425 #ifdef MPE /* Deal with MPE bind() peculiarities */
2426 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2427 /* The address *MUST* stupidly be zero. */
2428 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2429 /* PRIV mode is required to bind() to ports < 1024. */
2430 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2431 ((struct sockaddr_in *)addr)->sin_port > 0) {
2432 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2437 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2438 (struct sockaddr *)addr, len) >= 0)
2441 #ifdef MPE /* Switch back to USER mode */
2452 if (ckWARN(WARN_CLOSED))
2453 report_evil_fh(gv, io, PL_op->op_type);
2454 SETERRNO(EBADF,SS_IVCHAN);
2457 DIE(aTHX_ PL_no_sock_func, "bind");
2468 register IO *io = GvIOn(gv);
2471 if (!io || !IoIFP(io))
2474 addr = SvPV(addrsv, len);
2475 TAINT_PROPER("connect");
2476 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2482 if (ckWARN(WARN_CLOSED))
2483 report_evil_fh(gv, io, PL_op->op_type);
2484 SETERRNO(EBADF,SS_IVCHAN);
2487 DIE(aTHX_ PL_no_sock_func, "connect");
2497 register IO *io = gv ? GvIOn(gv) : NULL;
2499 if (!gv || !io || !IoIFP(io))
2502 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2508 if (ckWARN(WARN_CLOSED))
2509 report_evil_fh(gv, io, PL_op->op_type);
2510 SETERRNO(EBADF,SS_IVCHAN);
2513 DIE(aTHX_ PL_no_sock_func, "listen");
2525 char namebuf[MAXPATHLEN];
2526 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2527 Sock_size_t len = sizeof (struct sockaddr_in);
2529 Sock_size_t len = sizeof namebuf;
2542 if (!gstio || !IoIFP(gstio))
2546 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2550 do_close(ngv, FALSE);
2551 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2552 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2553 IoTYPE(nstio) = IoTYPE_SOCKET;
2554 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2555 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2556 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2557 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2560 #if defined(HAS_FCNTL) && defined(F_SETFD)
2561 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2565 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2566 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2568 #ifdef __SCO_VERSION__
2569 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2572 PUSHp(namebuf, len);
2576 if (ckWARN(WARN_CLOSED))
2577 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2578 SETERRNO(EBADF,SS_IVCHAN);
2584 DIE(aTHX_ PL_no_sock_func, "accept");
2594 register IO *io = GvIOn(gv);
2596 if (!io || !IoIFP(io))
2599 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2603 if (ckWARN(WARN_CLOSED))
2604 report_evil_fh(gv, io, PL_op->op_type);
2605 SETERRNO(EBADF,SS_IVCHAN);
2608 DIE(aTHX_ PL_no_sock_func, "shutdown");
2615 return pp_ssockopt();
2617 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2625 int optype = PL_op->op_type;
2628 unsigned int optname;
2634 if (optype == OP_GSOCKOPT)
2635 sv = sv_2mortal(NEWSV(22, 257));
2638 optname = (unsigned int) POPi;
2639 lvl = (unsigned int) POPi;
2643 if (!io || !IoIFP(io))
2646 fd = PerlIO_fileno(IoIFP(io));
2650 (void)SvPOK_only(sv);
2654 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2669 aint = (int)SvIV(sv);
2673 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2682 if (ckWARN(WARN_CLOSED))
2683 report_evil_fh(gv, io, optype);
2684 SETERRNO(EBADF,SS_IVCHAN);
2689 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2696 return pp_getpeername();
2698 DIE(aTHX_ PL_no_sock_func, "getsockname");
2706 int optype = PL_op->op_type;
2710 register IO *io = GvIOn(gv);
2713 if (!io || !IoIFP(io))
2716 sv = sv_2mortal(NEWSV(22, 257));
2717 (void)SvPOK_only(sv);
2721 fd = PerlIO_fileno(IoIFP(io));
2723 case OP_GETSOCKNAME:
2724 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2727 case OP_GETPEERNAME:
2728 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2730 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2732 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";
2733 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2734 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2735 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2736 sizeof(u_short) + sizeof(struct in_addr))) {
2743 #ifdef BOGUS_GETNAME_RETURN
2744 /* Interactive Unix, getpeername() and getsockname()
2745 does not return valid namelen */
2746 if (len == BOGUS_GETNAME_RETURN)
2747 len = sizeof(struct sockaddr);
2755 if (ckWARN(WARN_CLOSED))
2756 report_evil_fh(gv, io, optype);
2757 SETERRNO(EBADF,SS_IVCHAN);
2762 DIE(aTHX_ PL_no_sock_func, "getpeername");
2781 if (PL_op->op_flags & OPf_REF) {
2783 if (PL_op->op_type == OP_LSTAT) {
2784 if (gv != PL_defgv) {
2785 if (ckWARN(WARN_IO))
2786 Perl_warner(aTHX_ packWARN(WARN_IO),
2787 "lstat() on filehandle %s", GvENAME(gv));
2788 } else if (PL_laststype != OP_LSTAT)
2789 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2793 if (gv != PL_defgv) {
2794 PL_laststype = OP_STAT;
2796 sv_setpv(PL_statname, "");
2797 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2798 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2800 if (PL_laststatval < 0) {
2801 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2802 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2808 if (SvTYPE(sv) == SVt_PVGV) {
2812 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2814 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2815 Perl_warner(aTHX_ packWARN(WARN_IO),
2816 "lstat() on filehandle %s", GvENAME(gv));
2819 sv_setpv(PL_statname, SvPV(sv,n_a));
2822 PL_laststype = PL_op->op_type;
2823 if (PL_op->op_type == OP_LSTAT)
2824 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2827 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2828 if (PL_laststatval < 0) {
2829 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2830 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2836 if (gimme != G_ARRAY) {
2837 if (gimme != G_VOID)
2838 XPUSHs(boolSV(max));
2844 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2845 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2846 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2847 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2848 #if Uid_t_size > IVSIZE
2849 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2851 # if Uid_t_sign <= 0
2852 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2854 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2857 #if Gid_t_size > IVSIZE
2858 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2860 # if Gid_t_sign <= 0
2861 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2863 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2866 #ifdef USE_STAT_RDEV
2867 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2869 PUSHs(sv_2mortal(newSVpvn("", 0)));
2871 #if Off_t_size > IVSIZE
2872 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2874 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2877 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2878 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2879 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2881 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2882 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2883 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2885 #ifdef USE_STAT_BLOCKS
2886 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2887 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2889 PUSHs(sv_2mortal(newSVpvn("", 0)));
2890 PUSHs(sv_2mortal(newSVpvn("", 0)));
2896 /* This macro is used by the stacked filetest operators :
2897 * if the previous filetest failed, short-circuit and pass its value.
2898 * Else, discard it from the stack and continue. --rgs
2900 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2901 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2902 else { (void)POPs; PUTBACK; } \
2909 STACKED_FTEST_CHECK;
2910 #if defined(HAS_ACCESS) && defined(R_OK)
2911 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2913 result = access(POPpx, R_OK);
2928 if (cando(S_IRUSR, 0, &PL_statcache))
2937 STACKED_FTEST_CHECK;
2938 #if defined(HAS_ACCESS) && defined(W_OK)
2939 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2941 result = access(POPpx, W_OK);
2956 if (cando(S_IWUSR, 0, &PL_statcache))
2965 STACKED_FTEST_CHECK;
2966 #if defined(HAS_ACCESS) && defined(X_OK)
2967 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2969 result = access(POPpx, X_OK);
2984 if (cando(S_IXUSR, 0, &PL_statcache))
2993 STACKED_FTEST_CHECK;
2994 #ifdef PERL_EFF_ACCESS_R_OK
2995 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2997 result = PERL_EFF_ACCESS_R_OK(POPpx);
3012 if (cando(S_IRUSR, 1, &PL_statcache))
3021 STACKED_FTEST_CHECK;
3022 #ifdef PERL_EFF_ACCESS_W_OK
3023 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3025 result = PERL_EFF_ACCESS_W_OK(POPpx);
3040 if (cando(S_IWUSR, 1, &PL_statcache))
3049 STACKED_FTEST_CHECK;
3050 #ifdef PERL_EFF_ACCESS_X_OK
3051 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3053 result = PERL_EFF_ACCESS_X_OK(POPpx);
3068 if (cando(S_IXUSR, 1, &PL_statcache))
3077 STACKED_FTEST_CHECK;
3087 return pp_ftrowned();
3094 STACKED_FTEST_CHECK;
3099 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3109 STACKED_FTEST_CHECK;
3114 if (PL_statcache.st_size == 0)
3123 STACKED_FTEST_CHECK;
3128 #if Off_t_size > IVSIZE
3129 PUSHn(PL_statcache.st_size);
3131 PUSHi(PL_statcache.st_size);
3140 STACKED_FTEST_CHECK;
3145 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3153 STACKED_FTEST_CHECK;
3158 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3166 STACKED_FTEST_CHECK;
3171 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3179 STACKED_FTEST_CHECK;
3184 if (S_ISSOCK(PL_statcache.st_mode))
3193 STACKED_FTEST_CHECK;
3198 if (S_ISCHR(PL_statcache.st_mode))
3207 STACKED_FTEST_CHECK;
3212 if (S_ISBLK(PL_statcache.st_mode))
3221 STACKED_FTEST_CHECK;
3226 if (S_ISREG(PL_statcache.st_mode))
3235 STACKED_FTEST_CHECK;
3240 if (S_ISDIR(PL_statcache.st_mode))
3249 STACKED_FTEST_CHECK;
3254 if (S_ISFIFO(PL_statcache.st_mode))
3261 I32 result = my_lstat();
3265 if (S_ISLNK(PL_statcache.st_mode))
3275 STACKED_FTEST_CHECK;
3280 if (PL_statcache.st_mode & S_ISUID)
3291 STACKED_FTEST_CHECK;
3296 if (PL_statcache.st_mode & S_ISGID)
3307 STACKED_FTEST_CHECK;
3312 if (PL_statcache.st_mode & S_ISVTX)
3323 char *tmps = Nullch;
3326 STACKED_FTEST_CHECK;
3328 if (PL_op->op_flags & OPf_REF)
3330 else if (isGV(TOPs))
3332 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3333 gv = (GV*)SvRV(POPs);
3335 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3337 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3338 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3339 else if (tmps && isDIGIT(*tmps))
3343 if (PerlLIO_isatty(fd))
3348 #if defined(atarist) /* this will work with atariST. Configure will
3349 make guesses for other systems. */
3350 # define FILE_base(f) ((f)->_base)
3351 # define FILE_ptr(f) ((f)->_ptr)
3352 # define FILE_cnt(f) ((f)->_cnt)
3353 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3363 register STDCHAR *s;
3370 STACKED_FTEST_CHECK;
3372 if (PL_op->op_flags & OPf_REF)
3374 else if (isGV(TOPs))
3376 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3377 gv = (GV*)SvRV(POPs);
3383 if (gv == PL_defgv) {
3385 io = GvIO(PL_statgv);
3388 goto really_filename;
3393 PL_laststatval = -1;
3394 sv_setpv(PL_statname, "");
3395 io = GvIO(PL_statgv);
3397 if (io && IoIFP(io)) {
3398 if (! PerlIO_has_base(IoIFP(io)))
3399 DIE(aTHX_ "-T and -B not implemented on filehandles");
3400 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3401 if (PL_laststatval < 0)
3403 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3404 if (PL_op->op_type == OP_FTTEXT)
3409 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3410 i = PerlIO_getc(IoIFP(io));
3412 (void)PerlIO_ungetc(IoIFP(io),i);
3414 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3416 len = PerlIO_get_bufsiz(IoIFP(io));
3417 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3418 /* sfio can have large buffers - limit to 512 */
3423 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3425 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3427 SETERRNO(EBADF,RMS_IFI);
3435 PL_laststatval = -1;
3436 PL_laststype = OP_STAT;
3437 sv_setpv(PL_statname, SvPV(sv, n_a));
3438 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3439 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3440 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3443 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3444 if (PL_laststatval < 0) {
3445 (void)PerlIO_close(fp);
3448 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3449 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3450 (void)PerlIO_close(fp);
3452 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3453 RETPUSHNO; /* special case NFS directories */
3454 RETPUSHYES; /* null file is anything */
3459 /* now scan s to look for textiness */
3460 /* XXX ASCII dependent code */
3462 #if defined(DOSISH) || defined(USEMYBINMODE)
3463 /* ignore trailing ^Z on short files */
3464 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3468 for (i = 0; i < len; i++, s++) {
3469 if (!*s) { /* null never allowed in text */
3474 else if (!(isPRINT(*s) || isSPACE(*s)))
3477 else if (*s & 128) {
3479 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3482 /* utf8 characters don't count as odd */
3483 if (UTF8_IS_START(*s)) {
3484 int ulen = UTF8SKIP(s);
3485 if (ulen < len - i) {
3487 for (j = 1; j < ulen; j++) {
3488 if (!UTF8_IS_CONTINUATION(s[j]))
3491 --ulen; /* loop does extra increment */
3501 *s != '\n' && *s != '\r' && *s != '\b' &&
3502 *s != '\t' && *s != '\f' && *s != 27)
3507 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3532 if( !tmps || !*tmps ) {
3533 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3534 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3536 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3541 deprecate("chdir('') or chdir(undef) as chdir()");
3542 tmps = SvPV(*svp, n_a);
3546 TAINT_PROPER("chdir");
3551 TAINT_PROPER("chdir");
3552 PUSHi( PerlDir_chdir(tmps) >= 0 );
3554 /* Clear the DEFAULT element of ENV so we'll get the new value
3556 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3564 dSP; dMARK; dTARGET;
3565 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3571 DIE(aTHX_ PL_no_func, "chown");
3581 TAINT_PROPER("chroot");
3582 PUSHi( chroot(tmps) >= 0 );
3585 DIE(aTHX_ PL_no_func, "chroot");
3591 dSP; dMARK; dTARGET;
3593 value = (I32)apply(PL_op->op_type, MARK, SP);
3601 dSP; dMARK; dTARGET;
3603 value = (I32)apply(PL_op->op_type, MARK, SP);
3611 dSP; dMARK; dTARGET;
3613 value = (I32)apply(PL_op->op_type, MARK, SP);
3625 char *tmps2 = POPpx;
3626 char *tmps = SvPV(TOPs, n_a);
3627 TAINT_PROPER("rename");
3629 anum = PerlLIO_rename(tmps, tmps2);
3631 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3632 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3635 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3636 (void)UNLINK(tmps2);
3637 if (!(anum = link(tmps, tmps2)))
3638 anum = UNLINK(tmps);
3651 char *tmps2 = POPpx;
3652 char *tmps = SvPV(TOPs, n_a);
3653 TAINT_PROPER("link");
3654 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3657 DIE(aTHX_ PL_no_func, "link");
3666 char *tmps2 = POPpx;
3667 char *tmps = SvPV(TOPs, n_a);
3668 TAINT_PROPER("symlink");
3669 SETi( symlink(tmps, tmps2) >= 0 );
3672 DIE(aTHX_ PL_no_func, "symlink");
3682 char buf[MAXPATHLEN];
3686 #ifndef INCOMPLETE_TAINTS
3690 len = readlink(tmps, buf, sizeof(buf) - 1);
3698 RETSETUNDEF; /* just pretend it's a normal file */
3702 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3704 S_dooneliner(pTHX_ char *cmd, char *filename)
3706 char *save_filename = filename;
3712 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3713 strcpy(cmdline, cmd);
3714 strcat(cmdline, " ");
3715 for (s = cmdline + strlen(cmdline); *filename; ) {
3720 myfp = PerlProc_popen(cmdline, "r");
3724 SV *tmpsv = sv_newmortal();
3725 /* Need to save/restore 'PL_rs' ?? */
3726 s = sv_gets(tmpsv, myfp, 0);
3727 (void)PerlProc_pclose(myfp);
3731 #ifdef HAS_SYS_ERRLIST
3736 /* you don't see this */
3738 #ifdef HAS_SYS_ERRLIST
3746 if (instr(s, errmsg)) {
3753 #define EACCES EPERM
3755 if (instr(s, "cannot make"))
3756 SETERRNO(EEXIST,RMS_FEX);
3757 else if (instr(s, "existing file"))
3758 SETERRNO(EEXIST,RMS_FEX);
3759 else if (instr(s, "ile exists"))
3760 SETERRNO(EEXIST,RMS_FEX);
3761 else if (instr(s, "non-exist"))
3762 SETERRNO(ENOENT,RMS_FNF);
3763 else if (instr(s, "does not exist"))
3764 SETERRNO(ENOENT,RMS_FNF);
3765 else if (instr(s, "not empty"))
3766 SETERRNO(EBUSY,SS_DEVOFFLINE);
3767 else if (instr(s, "cannot access"))
3768 SETERRNO(EACCES,RMS_PRV);
3770 SETERRNO(EPERM,RMS_PRV);
3773 else { /* some mkdirs return no failure indication */
3774 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3775 if (PL_op->op_type == OP_RMDIR)
3780 SETERRNO(EACCES,RMS_PRV); /* a guess */
3789 /* This macro removes trailing slashes from a directory name.
3790 * Different operating and file systems take differently to
3791 * trailing slashes. According to POSIX 1003.1 1996 Edition
3792 * any number of trailing slashes should be allowed.
3793 * Thusly we snip them away so that even non-conforming
3794 * systems are happy.
3795 * We should probably do this "filtering" for all
3796 * the functions that expect (potentially) directory names:
3797 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3798 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3800 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3801 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3804 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3805 (tmps) = savepvn((tmps), (len)); \
3825 TRIMSLASHES(tmps,len,copy);
3827 TAINT_PROPER("mkdir");
3829 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3831 SETi( dooneliner("mkdir", tmps) );
3832 oldumask = PerlLIO_umask(0);
3833 PerlLIO_umask(oldumask);
3834 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3848 TRIMSLASHES(tmps,len,copy);
3849 TAINT_PROPER("rmdir");
3851 SETi( PerlDir_rmdir(tmps) >= 0 );
3853 SETi( dooneliner("rmdir", tmps) );
3860 /* Directory calls. */
3864 #if defined(Direntry_t) && defined(HAS_READDIR)
3867 char *dirname = POPpx;
3869 register IO *io = GvIOn(gv);
3875 PerlDir_close(IoDIRP(io));
3876 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3882 SETERRNO(EBADF,RMS_DIR);
3885 DIE(aTHX_ PL_no_dir_func, "opendir");
3891 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3892 DIE(aTHX_ PL_no_dir_func, "readdir");
3894 #if !defined(I_DIRENT) && !defined(VMS)
3895 Direntry_t *readdir (DIR *);
3901 GV *gv = (GV *)POPs;
3902 register Direntry_t *dp;
3903 register IO *io = GvIOn(gv);
3905 if (!io || !IoDIRP(io))
3909 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3913 sv = newSVpvn(dp->d_name, dp->d_namlen);
3915 sv = newSVpv(dp->d_name, 0);
3917 #ifndef INCOMPLETE_TAINTS
3918 if (!(IoFLAGS(io) & IOf_UNTAINT))
3921 XPUSHs(sv_2mortal(sv));
3923 while (gimme == G_ARRAY);
3925 if (!dp && gimme != G_ARRAY)
3932 SETERRNO(EBADF,RMS_ISI);
3933 if (GIMME == G_ARRAY)
3942 #if defined(HAS_TELLDIR) || defined(telldir)
3944 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3945 /* XXX netbsd still seemed to.
3946 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3947 --JHI 1999-Feb-02 */
3948 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3949 long telldir (DIR *);
3952 register IO *io = GvIOn(gv);
3954 if (!io || !IoDIRP(io))
3957 PUSHi( PerlDir_tell(IoDIRP(io)) );
3961 SETERRNO(EBADF,RMS_ISI);
3964 DIE(aTHX_ PL_no_dir_func, "telldir");
3970 #if defined(HAS_SEEKDIR) || defined(seekdir)
3974 register IO *io = GvIOn(gv);
3976 if (!io || !IoDIRP(io))
3979 (void)PerlDir_seek(IoDIRP(io), along);
3984 SETERRNO(EBADF,RMS_ISI);
3987 DIE(aTHX_ PL_no_dir_func, "seekdir");
3993 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3996 register IO *io = GvIOn(gv);
3998 if (!io || !IoDIRP(io))
4001 (void)PerlDir_rewind(IoDIRP(io));
4005 SETERRNO(EBADF,RMS_ISI);
4008 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4014 #if defined(Direntry_t) && defined(HAS_READDIR)
4017 register IO *io = GvIOn(gv);
4019 if (!io || !IoDIRP(io))
4022 #ifdef VOID_CLOSEDIR
4023 PerlDir_close(IoDIRP(io));
4025 if (PerlDir_close(IoDIRP(io)) < 0) {
4026 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4035 SETERRNO(EBADF,RMS_IFI);
4038 DIE(aTHX_ PL_no_dir_func, "closedir");
4042 /* Process control. */
4052 PERL_FLUSHALL_FOR_CHILD;
4053 childpid = PerlProc_fork();
4058 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4059 SvREADONLY_off(GvSV(tmpgv));
4060 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4061 SvREADONLY_on(GvSV(tmpgv));
4063 #ifdef THREADS_HAVE_PIDS
4064 PL_ppid = (IV)getppid();
4066 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4071 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4076 PERL_FLUSHALL_FOR_CHILD;
4077 childpid = PerlProc_fork();
4083 DIE(aTHX_ PL_no_func, "fork");
4090 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4095 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4096 childpid = wait4pid(-1, &argflags, 0);
4098 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4103 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4104 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4105 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4107 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4112 DIE(aTHX_ PL_no_func, "wait");
4118 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4127 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4128 result = wait4pid(pid, &argflags, optype);
4130 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4135 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4136 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4137 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4139 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4144 DIE(aTHX_ PL_no_func, "waitpid");
4150 dSP; dMARK; dORIGMARK; dTARGET;
4158 while (++MARK <= SP) {
4159 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4164 TAINT_PROPER("system");
4166 PERL_FLUSHALL_FOR_CHILD;
4167 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4172 if (PerlProc_pipe(pp) >= 0)
4174 while ((childpid = PerlProc_fork()) == -1) {
4175 if (errno != EAGAIN) {
4180 PerlLIO_close(pp[0]);
4181 PerlLIO_close(pp[1]);
4188 Sigsave_t ihand,qhand; /* place to save signals during system() */
4192 PerlLIO_close(pp[1]);
4194 rsignal_save(SIGINT, SIG_IGN, &ihand);
4195 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4198 result = wait4pid(childpid, &status, 0);
4199 } while (result == -1 && errno == EINTR);
4201 (void)rsignal_restore(SIGINT, &ihand);
4202 (void)rsignal_restore(SIGQUIT, &qhand);
4204 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4205 do_execfree(); /* free any memory child malloced on fork */
4211 while (n < sizeof(int)) {
4212 n1 = PerlLIO_read(pp[0],
4213 (void*)(((char*)&errkid)+n),
4219 PerlLIO_close(pp[0]);
4220 if (n) { /* Error */
4221 if (n != sizeof(int))
4222 DIE(aTHX_ "panic: kid popen errno read");
4223 errno = errkid; /* Propagate errno from kid */
4224 STATUS_CURRENT = -1;
4227 PUSHi(STATUS_CURRENT);
4231 PerlLIO_close(pp[0]);
4232 #if defined(HAS_FCNTL) && defined(F_SETFD)
4233 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4236 if (PL_op->op_flags & OPf_STACKED) {
4237 SV *really = *++MARK;
4238 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4240 else if (SP - MARK != 1)
4241 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4243 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4247 #else /* ! FORK or VMS or OS/2 */
4250 if (PL_op->op_flags & OPf_STACKED) {
4251 SV *really = *++MARK;
4252 # if defined(WIN32) || defined(OS2)
4253 value = (I32)do_aspawn(really, MARK, SP);
4255 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4258 else if (SP - MARK != 1) {
4259 # if defined(WIN32) || defined(OS2)
4260 value = (I32)do_aspawn(Nullsv, MARK, SP);
4262 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4266 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4268 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4270 STATUS_NATIVE_SET(value);
4273 PUSHi(result ? value : STATUS_CURRENT);
4274 #endif /* !FORK or VMS */
4280 dSP; dMARK; dORIGMARK; dTARGET;
4286 while (++MARK <= SP) {
4287 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4292 TAINT_PROPER("exec");
4294 PERL_FLUSHALL_FOR_CHILD;
4295 if (PL_op->op_flags & OPf_STACKED) {
4296 SV *really = *++MARK;
4297 value = (I32)do_aexec(really, MARK, SP);
4299 else if (SP - MARK != 1)
4301 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4305 (void ) do_aspawn(Nullsv, MARK, SP);
4309 value = (I32)do_aexec(Nullsv, MARK, SP);
4314 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4317 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4320 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4333 dSP; dMARK; dTARGET;
4335 value = (I32)apply(PL_op->op_type, MARK, SP);
4340 DIE(aTHX_ PL_no_func, "kill");
4348 # ifdef THREADS_HAVE_PIDS
4351 XPUSHi( getppid() );
4355 DIE(aTHX_ PL_no_func, "getppid");
4371 pgrp = (I32)BSD_GETPGRP(pid);
4373 if (pid != 0 && pid != PerlProc_getpid())
4374 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4380 DIE(aTHX_ PL_no_func, "getpgrp()");
4399 TAINT_PROPER("setpgrp");
4401 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4403 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4404 || (pid != 0 && pid != PerlProc_getpid()))
4406 DIE(aTHX_ "setpgrp can't take arguments");
4408 SETi( setpgrp() >= 0 );
4409 #endif /* USE_BSDPGRP */
4412 DIE(aTHX_ PL_no_func, "setpgrp()");
4418 #ifdef HAS_GETPRIORITY
4422 SETi( getpriority(which, who) );
4425 DIE(aTHX_ PL_no_func, "getpriority()");
4431 #ifdef HAS_SETPRIORITY
4436 TAINT_PROPER("setpriority");
4437 SETi( setpriority(which, who, niceval) >= 0 );
4440 DIE(aTHX_ PL_no_func, "setpriority()");
4450 XPUSHn( time(Null(Time_t*)) );
4452 XPUSHi( time(Null(Time_t*)) );
4463 (void)PerlProc_times(&PL_timesbuf);
4465 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4466 /* struct tms, though same data */
4470 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4471 if (GIMME == G_ARRAY) {
4472 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4473 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4474 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4480 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4482 if (GIMME == G_ARRAY) {
4483 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4484 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4485 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4489 DIE(aTHX_ "times not implemented");
4491 #endif /* HAS_TIMES */
4504 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4505 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4506 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4512 when = (Time_t)SvNVx(POPs);
4514 when = (Time_t)SvIVx(POPs);
4517 if (PL_op->op_type == OP_LOCALTIME)
4518 tmbuf = localtime(&when);
4520 tmbuf = gmtime(&when);
4522 if (GIMME != G_ARRAY) {
4528 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4529 dayname[tmbuf->tm_wday],
4530 monname[tmbuf->tm_mon],
4535 tmbuf->tm_year + 1900);
4536 PUSHs(sv_2mortal(tsv));
4541 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4542 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4543 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4544 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4545 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4546 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4547 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4548 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4549 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4560 anum = alarm((unsigned int)anum);
4567 DIE(aTHX_ PL_no_func, "alarm");
4578 (void)time(&lasttime);
4583 PerlProc_sleep((unsigned int)duration);
4586 XPUSHi(when - lasttime);
4590 /* Shared memory. */
4604 return pp_shmwrite();
4609 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4610 dSP; dMARK; dTARGET;
4611 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4620 /* Message passing. */
4634 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4635 dSP; dMARK; dTARGET;
4636 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4647 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4648 dSP; dMARK; dTARGET;
4649 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4662 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4663 dSP; dMARK; dTARGET;
4664 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4671 DIE(aTHX_ "System V IPC is not implemented on this machine");
4677 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4678 dSP; dMARK; dTARGET;
4679 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4687 PUSHp(zero_but_true, ZBTLEN);
4697 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4698 dSP; dMARK; dTARGET;
4699 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4708 /* Get system info. */
4712 #ifdef HAS_GETHOSTBYNAME
4713 return pp_ghostent();
4715 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4721 #ifdef HAS_GETHOSTBYADDR
4722 return pp_ghostent();
4724 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4730 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4732 I32 which = PL_op->op_type;
4733 register char **elem;
4735 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4736 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4737 struct hostent *gethostbyname(Netdb_name_t);
4738 struct hostent *gethostent(void);
4740 struct hostent *hent;
4745 if (which == OP_GHBYNAME) {
4746 #ifdef HAS_GETHOSTBYNAME
4747 char* name = POPpbytex;
4748 hent = PerlSock_gethostbyname(name);
4750 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4753 else if (which == OP_GHBYADDR) {
4754 #ifdef HAS_GETHOSTBYADDR
4755 int addrtype = POPi;
4758 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4760 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4762 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4766 #ifdef HAS_GETHOSTENT
4767 hent = PerlSock_gethostent();
4769 DIE(aTHX_ PL_no_sock_func, "gethostent");
4772 #ifdef HOST_NOT_FOUND
4774 #ifdef USE_REENTRANT_API
4775 # ifdef USE_GETHOSTENT_ERRNO
4776 h_errno = PL_reentrant_buffer->_gethostent_errno;
4779 STATUS_NATIVE_SET(h_errno);
4783 if (GIMME != G_ARRAY) {
4784 PUSHs(sv = sv_newmortal());
4786 if (which == OP_GHBYNAME) {
4788 sv_setpvn(sv, hent->h_addr, hent->h_length);
4791 sv_setpv(sv, (char*)hent->h_name);
4797 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4798 sv_setpv(sv, (char*)hent->h_name);
4799 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4800 for (elem = hent->h_aliases; elem && *elem; elem++) {
4801 sv_catpv(sv, *elem);
4803 sv_catpvn(sv, " ", 1);
4805 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4806 sv_setiv(sv, (IV)hent->h_addrtype);
4807 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4808 len = hent->h_length;
4809 sv_setiv(sv, (IV)len);
4811 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4812 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4813 sv_setpvn(sv, *elem, len);
4816 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4818 sv_setpvn(sv, hent->h_addr, len);
4823 DIE(aTHX_ PL_no_sock_func, "gethostent");
4829 #ifdef HAS_GETNETBYNAME
4830 return pp_gnetent();
4832 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4838 #ifdef HAS_GETNETBYADDR
4839 return pp_gnetent();
4841 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4847 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4849 I32 which = PL_op->op_type;
4850 register char **elem;
4852 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4853 struct netent *getnetbyaddr(Netdb_net_t, int);
4854 struct netent *getnetbyname(Netdb_name_t);
4855 struct netent *getnetent(void);
4857 struct netent *nent;
4860 if (which == OP_GNBYNAME){
4861 #ifdef HAS_GETNETBYNAME
4862 char *name = POPpbytex;
4863 nent = PerlSock_getnetbyname(name);
4865 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4868 else if (which == OP_GNBYADDR) {
4869 #ifdef HAS_GETNETBYADDR
4870 int addrtype = POPi;
4871 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4872 nent = PerlSock_getnetbyaddr(addr, addrtype);
4874 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4878 #ifdef HAS_GETNETENT
4879 nent = PerlSock_getnetent();
4881 DIE(aTHX_ PL_no_sock_func, "getnetent");
4884 #ifdef HOST_NOT_FOUND
4886 #ifdef USE_REENTRANT_API
4887 # ifdef USE_GETNETENT_ERRNO
4888 h_errno = PL_reentrant_buffer->_getnetent_errno;
4891 STATUS_NATIVE_SET(h_errno);
4896 if (GIMME != G_ARRAY) {
4897 PUSHs(sv = sv_newmortal());
4899 if (which == OP_GNBYNAME)
4900 sv_setiv(sv, (IV)nent->n_net);
4902 sv_setpv(sv, nent->n_name);
4908 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4909 sv_setpv(sv, nent->n_name);
4910 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4911 for (elem = nent->n_aliases; elem && *elem; elem++) {
4912 sv_catpv(sv, *elem);
4914 sv_catpvn(sv, " ", 1);
4916 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4917 sv_setiv(sv, (IV)nent->n_addrtype);
4918 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4919 sv_setiv(sv, (IV)nent->n_net);
4924 DIE(aTHX_ PL_no_sock_func, "getnetent");
4930 #ifdef HAS_GETPROTOBYNAME
4931 return pp_gprotoent();
4933 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4939 #ifdef HAS_GETPROTOBYNUMBER
4940 return pp_gprotoent();
4942 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4948 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4950 I32 which = PL_op->op_type;
4951 register char **elem;
4953 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4954 struct protoent *getprotobyname(Netdb_name_t);
4955 struct protoent *getprotobynumber(int);
4956 struct protoent *getprotoent(void);
4958 struct protoent *pent;
4961 if (which == OP_GPBYNAME) {
4962 #ifdef HAS_GETPROTOBYNAME
4963 char* name = POPpbytex;
4964 pent = PerlSock_getprotobyname(name);
4966 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4969 else if (which == OP_GPBYNUMBER) {
4970 #ifdef HAS_GETPROTOBYNUMBER
4972 pent = PerlSock_getprotobynumber(number);
4974 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4978 #ifdef HAS_GETPROTOENT
4979 pent = PerlSock_getprotoent();
4981 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4985 if (GIMME != G_ARRAY) {
4986 PUSHs(sv = sv_newmortal());
4988 if (which == OP_GPBYNAME)
4989 sv_setiv(sv, (IV)pent->p_proto);
4991 sv_setpv(sv, pent->p_name);
4997 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4998 sv_setpv(sv, pent->p_name);
4999 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5000 for (elem = pent->p_aliases; elem && *elem; elem++) {
5001 sv_catpv(sv, *elem);
5003 sv_catpvn(sv, " ", 1);
5005 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5006 sv_setiv(sv, (IV)pent->p_proto);
5011 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5017 #ifdef HAS_GETSERVBYNAME
5018 return pp_gservent();
5020 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5026 #ifdef HAS_GETSERVBYPORT
5027 return pp_gservent();
5029 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5035 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5037 I32 which = PL_op->op_type;
5038 register char **elem;
5040 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5041 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5042 struct servent *getservbyport(int, Netdb_name_t);
5043 struct servent *getservent(void);
5045 struct servent *sent;
5048 if (which == OP_GSBYNAME) {
5049 #ifdef HAS_GETSERVBYNAME
5050 char *proto = POPpbytex;
5051 char *name = POPpbytex;
5053 if (proto && !*proto)
5056 sent = PerlSock_getservbyname(name, proto);
5058 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5061 else if (which == OP_GSBYPORT) {
5062 #ifdef HAS_GETSERVBYPORT
5063 char *proto = POPpbytex;
5064 unsigned short port = (unsigned short)POPu;
5066 if (proto && !*proto)
5070 port = PerlSock_htons(port);
5072 sent = PerlSock_getservbyport(port, proto);
5074 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5078 #ifdef HAS_GETSERVENT
5079 sent = PerlSock_getservent();
5081 DIE(aTHX_ PL_no_sock_func, "getservent");
5085 if (GIMME != G_ARRAY) {
5086 PUSHs(sv = sv_newmortal());
5088 if (which == OP_GSBYNAME) {
5090 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5092 sv_setiv(sv, (IV)(sent->s_port));
5096 sv_setpv(sv, sent->s_name);
5102 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5103 sv_setpv(sv, sent->s_name);
5104 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5105 for (elem = sent->s_aliases; elem && *elem; elem++) {
5106 sv_catpv(sv, *elem);
5108 sv_catpvn(sv, " ", 1);
5110 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5112 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5114 sv_setiv(sv, (IV)(sent->s_port));
5116 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5117 sv_setpv(sv, sent->s_proto);
5122 DIE(aTHX_ PL_no_sock_func, "getservent");
5128 #ifdef HAS_SETHOSTENT
5130 PerlSock_sethostent(TOPi);
5133 DIE(aTHX_ PL_no_sock_func, "sethostent");
5139 #ifdef HAS_SETNETENT
5141 PerlSock_setnetent(TOPi);
5144 DIE(aTHX_ PL_no_sock_func, "setnetent");
5150 #ifdef HAS_SETPROTOENT
5152 PerlSock_setprotoent(TOPi);
5155 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5161 #ifdef HAS_SETSERVENT
5163 PerlSock_setservent(TOPi);
5166 DIE(aTHX_ PL_no_sock_func, "setservent");
5172 #ifdef HAS_ENDHOSTENT
5174 PerlSock_endhostent();
5178 DIE(aTHX_ PL_no_sock_func, "endhostent");
5184 #ifdef HAS_ENDNETENT
5186 PerlSock_endnetent();
5190 DIE(aTHX_ PL_no_sock_func, "endnetent");
5196 #ifdef HAS_ENDPROTOENT
5198 PerlSock_endprotoent();
5202 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5208 #ifdef HAS_ENDSERVENT
5210 PerlSock_endservent();
5214 DIE(aTHX_ PL_no_sock_func, "endservent");
5223 DIE(aTHX_ PL_no_func, "getpwnam");
5232 DIE(aTHX_ PL_no_func, "getpwuid");
5240 I32 which = PL_op->op_type;
5243 struct passwd *pwent = NULL;
5245 * We currently support only the SysV getsp* shadow password interface.
5246 * The interface is declared in <shadow.h> and often one needs to link
5247 * with -lsecurity or some such.
5248 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5251 * AIX getpwnam() is clever enough to return the encrypted password
5252 * only if the caller (euid?) is root.
5254 * There are at least three other shadow password APIs. Many platforms
5255 * seem to contain more than one interface for accessing the shadow
5256 * password databases, possibly for compatibility reasons.
5257 * The getsp*() is by far he simplest one, the other two interfaces
5258 * are much more complicated, but also very similar to each other.
5263 * struct pr_passwd *getprpw*();
5264 * The password is in
5265 * char getprpw*(...).ufld.fd_encrypt[]
5266 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5271 * struct es_passwd *getespw*();
5272 * The password is in
5273 * char *(getespw*(...).ufld.fd_encrypt)
5274 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5277 * struct userpw *getuserpw();
5278 * The password is in
5279 * char *(getuserpw(...)).spw_upw_passwd
5280 * (but the de facto standard getpwnam() should work okay)
5282 * Mention I_PROT here so that Configure probes for it.
5284 * In HP-UX for getprpw*() the manual page claims that one should include
5285 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5286 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5287 * and pp_sys.c already includes <shadow.h> if there is such.
5289 * Note that <sys/security.h> is already probed for, but currently
5290 * it is only included in special cases.
5292 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5293 * be preferred interface, even though also the getprpw*() interface
5294 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5295 * One also needs to call set_auth_parameters() in main() before
5296 * doing anything else, whether one is using getespw*() or getprpw*().
5298 * Note that accessing the shadow databases can be magnitudes
5299 * slower than accessing the standard databases.
5304 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5305 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5306 * the pw_comment is left uninitialized. */
5307 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5313 char* name = POPpbytex;
5314 pwent = getpwnam(name);
5320 pwent = getpwuid(uid);
5324 # ifdef HAS_GETPWENT
5326 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5327 if (pwent) pwent = getpwnam(pwent->pw_name);
5330 DIE(aTHX_ PL_no_func, "getpwent");
5336 if (GIMME != G_ARRAY) {
5337 PUSHs(sv = sv_newmortal());
5339 if (which == OP_GPWNAM)
5340 # if Uid_t_sign <= 0
5341 sv_setiv(sv, (IV)pwent->pw_uid);
5343 sv_setuv(sv, (UV)pwent->pw_uid);
5346 sv_setpv(sv, pwent->pw_name);
5352 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5353 sv_setpv(sv, pwent->pw_name);
5355 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5357 /* If we have getspnam(), we try to dig up the shadow
5358 * password. If we are underprivileged, the shadow
5359 * interface will set the errno to EACCES or similar,
5360 * and return a null pointer. If this happens, we will
5361 * use the dummy password (usually "*" or "x") from the
5362 * standard password database.
5364 * In theory we could skip the shadow call completely
5365 * if euid != 0 but in practice we cannot know which
5366 * security measures are guarding the shadow databases
5367 * on a random platform.
5369 * Resist the urge to use additional shadow interfaces.
5370 * Divert the urge to writing an extension instead.
5373 /* Some AIX setups falsely(?) detect some getspnam(), which
5374 * has a different API than the Solaris/IRIX one. */
5375 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5377 struct spwd *spwent;
5378 int saverrno; /* Save and restore errno so that
5379 * underprivileged attempts seem
5380 * to have never made the unsccessful
5381 * attempt to retrieve the shadow password. */
5384 spwent = getspnam(pwent->pw_name);
5386 if (spwent && spwent->sp_pwdp)
5387 sv_setpv(sv, spwent->sp_pwdp);
5391 if (!SvPOK(sv)) /* Use the standard password, then. */
5392 sv_setpv(sv, pwent->pw_passwd);
5395 # ifndef INCOMPLETE_TAINTS
5396 /* passwd is tainted because user himself can diddle with it.
5397 * admittedly not much and in a very limited way, but nevertheless. */
5401 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5402 # if Uid_t_sign <= 0
5403 sv_setiv(sv, (IV)pwent->pw_uid);
5405 sv_setuv(sv, (UV)pwent->pw_uid);
5408 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5409 # if Uid_t_sign <= 0
5410 sv_setiv(sv, (IV)pwent->pw_gid);
5412 sv_setuv(sv, (UV)pwent->pw_gid);
5414 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5415 * because of the poor interface of the Perl getpw*(),
5416 * not because there's some standard/convention saying so.
5417 * A better interface would have been to return a hash,
5418 * but we are accursed by our history, alas. --jhi. */
5419 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5421 sv_setiv(sv, (IV)pwent->pw_change);
5424 sv_setiv(sv, (IV)pwent->pw_quota);
5427 sv_setpv(sv, pwent->pw_age);
5432 /* pw_class and pw_comment are mutually exclusive--.
5433 * see the above note for pw_change, pw_quota, and pw_age. */
5434 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5436 sv_setpv(sv, pwent->pw_class);
5439 sv_setpv(sv, pwent->pw_comment);
5443 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5445 sv_setpv(sv, pwent->pw_gecos);
5447 # ifndef INCOMPLETE_TAINTS
5448 /* pw_gecos is tainted because user himself can diddle with it. */
5452 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5453 sv_setpv(sv, pwent->pw_dir);
5455 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5456 sv_setpv(sv, pwent->pw_shell);
5457 # ifndef INCOMPLETE_TAINTS
5458 /* pw_shell is tainted because user himself can diddle with it. */
5463 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5464 sv_setiv(sv, (IV)pwent->pw_expire);
5469 DIE(aTHX_ PL_no_func, "getpwent");
5475 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5480 DIE(aTHX_ PL_no_func, "setpwent");
5486 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5491 DIE(aTHX_ PL_no_func, "endpwent");
5500 DIE(aTHX_ PL_no_func, "getgrnam");
5509 DIE(aTHX_ PL_no_func, "getgrgid");
5517 I32 which = PL_op->op_type;
5518 register char **elem;
5520 struct group *grent;
5523 if (which == OP_GGRNAM) {
5524 char* name = POPpbytex;
5525 grent = (struct group *)getgrnam(name);
5527 else if (which == OP_GGRGID) {
5529 grent = (struct group *)getgrgid(gid);
5533 grent = (struct group *)getgrent();
5535 DIE(aTHX_ PL_no_func, "getgrent");
5539 if (GIMME != G_ARRAY) {
5540 PUSHs(sv = sv_newmortal());
5542 if (which == OP_GGRNAM)
5543 sv_setiv(sv, (IV)grent->gr_gid);
5545 sv_setpv(sv, grent->gr_name);
5551 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5552 sv_setpv(sv, grent->gr_name);
5554 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5556 sv_setpv(sv, grent->gr_passwd);
5559 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5560 sv_setiv(sv, (IV)grent->gr_gid);
5562 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5563 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5564 /* In UNICOS/mk (_CRAYMPP) the multithreading
5565 * versions (getgrnam_r, getgrgid_r)
5566 * seem to return an illegal pointer
5567 * as the group members list, gr_mem.
5568 * getgrent() doesn't even have a _r version
5569 * but the gr_mem is poisonous anyway.
5570 * So yes, you cannot get the list of group
5571 * members if building multithreaded in UNICOS/mk. */
5572 for (elem = grent->gr_mem; elem && *elem; elem++) {
5573 sv_catpv(sv, *elem);
5575 sv_catpvn(sv, " ", 1);
5582 DIE(aTHX_ PL_no_func, "getgrent");
5588 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5593 DIE(aTHX_ PL_no_func, "setgrent");
5599 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5604 DIE(aTHX_ PL_no_func, "endgrent");
5614 if (!(tmps = PerlProc_getlogin()))
5616 PUSHp(tmps, strlen(tmps));
5619 DIE(aTHX_ PL_no_func, "getlogin");
5623 /* Miscellaneous. */
5628 dSP; dMARK; dORIGMARK; dTARGET;
5629 register I32 items = SP - MARK;
5630 unsigned long a[20];
5636 while (++MARK <= SP) {
5637 if (SvTAINTED(*MARK)) {
5643 TAINT_PROPER("syscall");
5646 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5647 * or where sizeof(long) != sizeof(char*). But such machines will
5648 * not likely have syscall implemented either, so who cares?
5650 while (++MARK <= SP) {
5651 if (SvNIOK(*MARK) || !i)
5652 a[i++] = SvIV(*MARK);
5653 else if (*MARK == &PL_sv_undef)
5656 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5662 DIE(aTHX_ "Too many args to syscall");
5664 DIE(aTHX_ "Too few args to syscall");
5666 retval = syscall(a[0]);
5669 retval = syscall(a[0],a[1]);
5672 retval = syscall(a[0],a[1],a[2]);
5675 retval = syscall(a[0],a[1],a[2],a[3]);
5678 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5681 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5684 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5687 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5691 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5694 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5697 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5701 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5705 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5709 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5710 a[10],a[11],a[12],a[13]);
5712 #endif /* atarist */
5718 DIE(aTHX_ PL_no_func, "syscall");
5722 #ifdef FCNTL_EMULATE_FLOCK
5724 /* XXX Emulate flock() with fcntl().
5725 What's really needed is a good file locking module.
5729 fcntl_emulate_flock(int fd, int operation)
5733 switch (operation & ~LOCK_NB) {
5735 flock.l_type = F_RDLCK;
5738 flock.l_type = F_WRLCK;
5741 flock.l_type = F_UNLCK;
5747 flock.l_whence = SEEK_SET;
5748 flock.l_start = flock.l_len = (Off_t)0;
5750 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5753 #endif /* FCNTL_EMULATE_FLOCK */
5755 #ifdef LOCKF_EMULATE_FLOCK
5757 /* XXX Emulate flock() with lockf(). This is just to increase
5758 portability of scripts. The calls are not completely
5759 interchangeable. What's really needed is a good file
5763 /* The lockf() constants might have been defined in <unistd.h>.
5764 Unfortunately, <unistd.h> causes troubles on some mixed
5765 (BSD/POSIX) systems, such as SunOS 4.1.3.
5767 Further, the lockf() constants aren't POSIX, so they might not be
5768 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5769 just stick in the SVID values and be done with it. Sigh.
5773 # define F_ULOCK 0 /* Unlock a previously locked region */
5776 # define F_LOCK 1 /* Lock a region for exclusive use */
5779 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5782 # define F_TEST 3 /* Test a region for other processes locks */
5786 lockf_emulate_flock(int fd, int operation)
5792 /* flock locks entire file so for lockf we need to do the same */
5794 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5795 if (pos > 0) /* is seekable and needs to be repositioned */
5796 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5797 pos = -1; /* seek failed, so don't seek back afterwards */
5800 switch (operation) {
5802 /* LOCK_SH - get a shared lock */
5804 /* LOCK_EX - get an exclusive lock */
5806 i = lockf (fd, F_LOCK, 0);
5809 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5810 case LOCK_SH|LOCK_NB:
5811 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5812 case LOCK_EX|LOCK_NB:
5813 i = lockf (fd, F_TLOCK, 0);
5815 if ((errno == EAGAIN) || (errno == EACCES))
5816 errno = EWOULDBLOCK;
5819 /* LOCK_UN - unlock (non-blocking is a no-op) */
5821 case LOCK_UN|LOCK_NB:
5822 i = lockf (fd, F_ULOCK, 0);
5825 /* Default - can't decipher operation */
5832 if (pos > 0) /* need to restore position of the handle */
5833 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5838 #endif /* LOCKF_EMULATE_FLOCK */