3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 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.
19 #define PERL_IN_PP_SYS_C
23 /* Shadow password support for solaris - pdo@cs.umd.edu
24 * Not just Solaris: at least HP-UX, IRIX, Linux.
25 * The API is from SysV.
27 * There are at least two more shadow interfaces,
28 * see the comments in pp_gpwent().
32 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
33 * and another MAXINT from "perl.h" <- <sys/param.h>. */
41 extern "C" int syscall(unsigned long,...);
46 # include <sys/wait.h>
50 # include <sys/resource.h>
59 # include <sys/select.h>
63 /* XXX Configure test needed.
64 h_errno might not be a simple 'int', especially for multi-threaded
65 applications, see "extern int errno in perl.h". Creating such
66 a test requires taking into account the differences between
67 compiling multithreaded and singlethreaded ($ccflags et al).
68 HOST_NOT_FOUND is typically defined in <netdb.h>.
70 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
79 struct passwd *getpwnam (char *);
80 struct passwd *getpwuid (Uid_t);
85 struct passwd *getpwent (void);
86 #elif defined (VMS) && defined (my_getpwent)
87 struct passwd *Perl_my_getpwent (void);
96 struct group *getgrnam (char *);
97 struct group *getgrgid (Gid_t);
101 struct group *getgrent (void);
107 # if defined(_MSC_VER) || defined(__MINGW32__)
108 # include <sys/utime.h>
115 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
118 # define my_chsize PerlLIO_chsize
123 #else /* no flock() */
125 /* fcntl.h might not have been included, even if it exists, because
126 the current Configure only sets I_FCNTL if it's needed to pick up
127 the *_OK constants. Make sure it has been included before testing
128 the fcntl() locking constants. */
129 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
133 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
134 # define FLOCK fcntl_emulate_flock
135 # define FCNTL_EMULATE_FLOCK
136 # else /* no flock() or fcntl(F_SETLK,...) */
138 # define FLOCK lockf_emulate_flock
139 # define LOCKF_EMULATE_FLOCK
141 # endif /* no flock() or fcntl(F_SETLK,...) */
144 static int FLOCK (int, int);
147 * These are the flock() constants. Since this sytems doesn't have
148 * flock(), the values of the constants are probably not available.
162 # endif /* emulating flock() */
164 #endif /* no flock() */
167 static char zero_but_true[ZBTLEN + 1] = "0 but true";
169 #if defined(I_SYS_ACCESS) && !defined(R_OK)
170 # include <sys/access.h>
173 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
174 # define FD_CLOEXEC 1 /* NeXT needs this */
180 /* Missing protos on LynxOS */
181 void sethostent(int);
182 void endhostent(void);
184 void endnetent(void);
185 void setprotoent(int);
186 void endprotoent(void);
187 void setservent(int);
188 void endservent(void);
191 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
192 #undef PERL_EFF_ACCESS_W_OK
193 #undef PERL_EFF_ACCESS_X_OK
195 /* F_OK unused: if stat() cannot find it... */
197 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
198 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
199 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
200 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
201 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
204 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
205 # ifdef I_SYS_SECURITY
206 # include <sys/security.h>
210 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
211 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
212 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
215 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
216 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
217 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
221 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
223 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
225 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
228 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
229 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
230 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
233 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
235 Uid_t ruid = getuid();
236 Uid_t euid = geteuid();
237 Gid_t rgid = getgid();
238 Gid_t egid = getegid();
242 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
243 Perl_croak(aTHX_ "switching effective uid is not implemented");
246 if (setreuid(euid, ruid))
249 if (setresuid(euid, ruid, (Uid_t)-1))
252 Perl_croak(aTHX_ "entering effective uid failed");
255 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
256 Perl_croak(aTHX_ "switching effective gid is not implemented");
259 if (setregid(egid, rgid))
262 if (setresgid(egid, rgid, (Gid_t)-1))
265 Perl_croak(aTHX_ "entering effective gid failed");
268 res = access(path, mode);
271 if (setreuid(ruid, euid))
274 if (setresuid(ruid, euid, (Uid_t)-1))
277 Perl_croak(aTHX_ "leaving effective uid failed");
280 if (setregid(rgid, egid))
283 if (setresgid(rgid, egid, (Gid_t)-1))
286 Perl_croak(aTHX_ "leaving effective gid failed");
291 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
292 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
293 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
296 #if !defined(PERL_EFF_ACCESS_R_OK)
297 /* With it or without it: anyway you get a warning: either that
298 it is unused, or it is declared static and never defined.
301 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
303 Perl_croak(aTHX_ "switching effective uid is not implemented");
319 if (PL_op->op_private & OPpOPEN_IN_RAW)
321 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
323 fp = PerlProc_popen(tmps, mode);
326 if (PL_curcop->cop_io) {
327 type = SvPV_nolen(PL_curcop->cop_io);
330 PerlIO_apply_layers(aTHX_ fp,mode,type);
332 if (gimme == G_VOID) {
334 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
338 else if (gimme == G_SCALAR) {
340 PL_rs = &PL_sv_undef;
341 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
342 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
354 if (sv_gets(sv, fp, 0) == Nullch) {
358 XPUSHs(sv_2mortal(sv));
359 if (SvLEN(sv) - SvCUR(sv) > 20) {
360 SvLEN_set(sv, SvCUR(sv)+1);
361 Renew(SvPVX(sv), SvLEN(sv), char);
366 STATUS_NATIVE_SET(PerlProc_pclose(fp));
367 TAINT; /* "I believe that this is not gratuitous!" */
370 STATUS_NATIVE_SET(-1);
371 if (gimme == G_SCALAR)
381 tryAMAGICunTARGET(iter, -1);
383 /* Note that we only ever get here if File::Glob fails to load
384 * without at the same time croaking, for some reason, or if
385 * perl was built with PERL_EXTERNAL_GLOB */
392 * The external globbing program may use things we can't control,
393 * so for security reasons we must assume the worst.
396 taint_proper(PL_no_security, "glob");
400 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
401 PL_last_in_gv = (GV*)*PL_stack_sp--;
403 SAVESPTR(PL_rs); /* This is not permanent, either. */
404 PL_rs = sv_2mortal(newSVpvn("\000", 1));
407 *SvPVX(PL_rs) = '\n';
411 result = do_readline();
418 PL_last_in_gv = cGVOP_gv;
419 return do_readline();
428 if (SP - MARK != 1) {
430 do_join(TARG, &PL_sv_no, MARK, SP);
437 tmps = SvPV(tmpsv, len);
438 if ((!tmps || !len) && PL_errgv) {
440 (void)SvUPGRADE(error, SVt_PV);
441 if (SvPOK(error) && SvCUR(error))
442 sv_catpv(error, "\t...caught");
444 tmps = SvPV(tmpsv, len);
447 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
449 Perl_warn(aTHX_ "%"SVf, tmpsv);
461 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
463 if (SP - MARK != 1) {
465 do_join(TARG, &PL_sv_no, MARK, SP);
467 tmps = SvPV(tmpsv, len);
473 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
477 (void)SvUPGRADE(error, SVt_PV);
478 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
480 SvSetSV(error,tmpsv);
481 else if (sv_isobject(error)) {
482 HV *stash = SvSTASH(SvRV(error));
483 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
485 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
486 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
493 call_sv((SV*)GvCV(gv),
494 G_SCALAR|G_EVAL|G_KEEPERR);
495 sv_setsv(error,*PL_stack_sp--);
498 DIE(aTHX_ Nullformat);
501 if (SvPOK(error) && SvCUR(error))
502 sv_catpv(error, "\t...propagated");
504 tmps = SvPV(tmpsv, len);
508 tmpsv = sv_2mortal(newSVpvn("Died", 4));
510 DIE(aTHX_ "%"SVf, tmpsv);
530 DIE(aTHX_ PL_no_usym, "filehandle");
531 if ((io = GvIOp(gv)))
532 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
534 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
535 /* Method's args are same as ours ... */
536 /* ... except handle is replaced by the object */
537 *MARK-- = SvTIED_obj((SV*)io, mg);
541 call_method("OPEN", G_SCALAR);
554 tmps = SvPV(sv, len);
555 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
558 PUSHi( (I32)PL_forkprocess );
559 else if (PL_forkprocess == 0) /* we are a new child */
578 if (gv && (io = GvIO(gv))
579 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
582 XPUSHs(SvTIED_obj((SV*)io, mg));
585 call_method("CLOSE", G_SCALAR);
591 PUSHs(boolSV(do_close(gv, TRUE)));
611 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
612 DIE(aTHX_ PL_no_usym, "filehandle");
617 do_close(rgv, FALSE);
619 do_close(wgv, FALSE);
621 if (PerlProc_pipe(fd) < 0)
624 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
625 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
626 IoOFP(rstio) = IoIFP(rstio);
627 IoIFP(wstio) = IoOFP(wstio);
628 IoTYPE(rstio) = IoTYPE_RDONLY;
629 IoTYPE(wstio) = IoTYPE_WRONLY;
631 if (!IoIFP(rstio) || !IoOFP(wstio)) {
632 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
633 else PerlLIO_close(fd[0]);
634 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
635 else PerlLIO_close(fd[1]);
638 #if defined(HAS_FCNTL) && defined(F_SETFD)
639 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
640 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
647 DIE(aTHX_ PL_no_func, "pipe");
663 if (gv && (io = GvIO(gv))
664 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
667 XPUSHs(SvTIED_obj((SV*)io, mg));
670 call_method("FILENO", G_SCALAR);
676 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
677 /* Can't do this because people seem to do things like
678 defined(fileno($foo)) to check whether $foo is a valid fh.
679 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
680 report_evil_fh(gv, io, PL_op->op_type);
685 PUSHi(PerlIO_fileno(fp));
696 anum = PerlLIO_umask(0);
697 (void)PerlLIO_umask(anum);
700 anum = PerlLIO_umask(POPi);
701 TAINT_PROPER("umask");
704 /* Only DIE if trying to restrict permissions on `user' (self).
705 * Otherwise it's harmless and more useful to just return undef
706 * since 'group' and 'other' concepts probably don't exist here. */
707 if (MAXARG >= 1 && (POPi & 0700))
708 DIE(aTHX_ "umask not implemented");
709 XPUSHs(&PL_sv_undef);
731 if (gv && (io = GvIO(gv))
732 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
735 XPUSHs(SvTIED_obj((SV*)io, mg));
740 call_method("BINMODE", G_SCALAR);
747 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
748 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
749 report_evil_fh(gv, io, PL_op->op_type);
750 SETERRNO(EBADF,RMS_IFI);
755 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
756 (discp) ? SvPV_nolen(discp) : Nullch)) {
757 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
758 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
759 mode_from_discipline(discp),
760 (discp) ? SvPV_nolen(discp) : Nullch)) {
782 I32 markoff = MARK - PL_stack_base;
784 int how = PERL_MAGIC_tied;
788 switch(SvTYPE(varsv)) {
790 methname = "TIEHASH";
791 HvEITER((HV *)varsv) = Null(HE *);
794 methname = "TIEARRAY";
797 #ifdef GV_UNIQUE_CHECK
798 if (GvUNIQUE((GV*)varsv)) {
799 Perl_croak(aTHX_ "Attempt to tie unique GV");
802 methname = "TIEHANDLE";
803 how = PERL_MAGIC_tiedscalar;
804 /* For tied filehandles, we apply tiedscalar magic to the IO
805 slot of the GP rather than the GV itself. AMS 20010812 */
807 GvIOp(varsv) = newIO();
808 varsv = (SV *)GvIOp(varsv);
811 methname = "TIESCALAR";
812 how = PERL_MAGIC_tiedscalar;
816 if (sv_isobject(*MARK)) {
818 PUSHSTACKi(PERLSI_MAGIC);
820 EXTEND(SP,(I32)items);
824 call_method(methname, G_SCALAR);
827 /* Not clear why we don't call call_method here too.
828 * perhaps to get different error message ?
830 stash = gv_stashsv(*MARK, FALSE);
831 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
832 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
836 PUSHSTACKi(PERLSI_MAGIC);
838 EXTEND(SP,(I32)items);
842 call_sv((SV*)GvCV(gv), G_SCALAR);
848 if (sv_isobject(sv)) {
849 sv_unmagic(varsv, how);
850 /* Croak if a self-tie on an aggregate is attempted. */
851 if (varsv == SvRV(sv) &&
852 (SvTYPE(varsv) == SVt_PVAV ||
853 SvTYPE(varsv) == SVt_PVHV))
855 "Self-ties of arrays and hashes are not supported");
856 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
859 SP = PL_stack_base + markoff;
869 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
870 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
872 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
875 if ((mg = SvTIED_mg(sv, how))) {
876 SV *obj = SvRV(SvTIED_obj(sv, mg));
880 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
881 isGV(gv) && (cv = GvCV(gv))) {
883 XPUSHs(SvTIED_obj((SV*)gv, mg));
884 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
887 call_sv((SV *)cv, G_VOID);
891 else if (ckWARN(WARN_UNTIE)) {
892 if (mg && SvREFCNT(obj) > 1)
893 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
894 "untie attempted while %"UVuf" inner references still exist",
895 (UV)SvREFCNT(obj) - 1 ) ;
899 sv_unmagic(sv, how) ;
908 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
909 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
911 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
914 if ((mg = SvTIED_mg(sv, how))) {
915 SV *osv = SvTIED_obj(sv, mg);
916 if (osv == mg->mg_obj)
917 osv = sv_mortalcopy(osv);
935 sv = sv_mortalcopy(&PL_sv_no);
936 sv_setpv(sv, "AnyDBM_File");
937 stash = gv_stashsv(sv, FALSE);
938 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
940 require_pv("AnyDBM_File.pm");
942 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
943 DIE(aTHX_ "No dbm on this machine");
953 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
955 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
958 call_sv((SV*)GvCV(gv), G_SCALAR);
961 if (!sv_isobject(TOPs)) {
966 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
969 call_sv((SV*)GvCV(gv), G_SCALAR);
973 if (sv_isobject(TOPs)) {
974 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
975 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
997 struct timeval timebuf;
998 struct timeval *tbuf = &timebuf;
1002 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1007 # if BYTEORDER & 0xf0000
1008 # define ORDERBYTE (0x88888888 - BYTEORDER)
1010 # define ORDERBYTE (0x4444 - BYTEORDER)
1016 for (i = 1; i <= 3; i++) {
1024 /* little endians can use vecs directly */
1025 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1032 masksize = NFDBITS / NBBY;
1034 masksize = sizeof(long); /* documented int, everyone seems to use long */
1036 Zero(&fd_sets[0], 4, char*);
1039 # if SELECT_MIN_BITS == 1
1040 growsize = sizeof(fd_set);
1042 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1043 # undef SELECT_MIN_BITS
1044 # define SELECT_MIN_BITS __FD_SETSIZE
1046 /* If SELECT_MIN_BITS is greater than one we most probably will want
1047 * to align the sizes with SELECT_MIN_BITS/8 because for example
1048 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1049 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1050 * on (sets/tests/clears bits) is 32 bits. */
1051 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1059 timebuf.tv_sec = (long)value;
1060 value -= (NV)timebuf.tv_sec;
1061 timebuf.tv_usec = (long)(value * 1000000.0);
1064 tbuf = Null(struct timeval*);
1066 for (i = 1; i <= 3; i++) {
1072 else if (!SvPOK(sv))
1073 SvPV_force(sv,n_a); /* force string conversion */
1076 Sv_Grow(sv, growsize);
1080 while (++j <= growsize) {
1084 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1086 New(403, fd_sets[i], growsize, char);
1087 for (offset = 0; offset < growsize; offset += masksize) {
1088 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1089 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1092 fd_sets[i] = SvPVX(sv);
1096 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1097 /* Can't make just the (void*) conditional because that would be
1098 * cpp #if within cpp macro, and not all compilers like that. */
1099 nfound = PerlSock_select(
1101 (Select_fd_set_t) fd_sets[1],
1102 (Select_fd_set_t) fd_sets[2],
1103 (Select_fd_set_t) fd_sets[3],
1104 (void*) tbuf); /* Workaround for compiler bug. */
1106 nfound = PerlSock_select(
1108 (Select_fd_set_t) fd_sets[1],
1109 (Select_fd_set_t) fd_sets[2],
1110 (Select_fd_set_t) fd_sets[3],
1113 for (i = 1; i <= 3; i++) {
1116 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1118 for (offset = 0; offset < growsize; offset += masksize) {
1119 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1120 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1122 Safefree(fd_sets[i]);
1129 if (GIMME == G_ARRAY && tbuf) {
1130 value = (NV)(timebuf.tv_sec) +
1131 (NV)(timebuf.tv_usec) / 1000000.0;
1132 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1133 sv_setnv(sv, value);
1137 DIE(aTHX_ "select not implemented");
1142 Perl_setdefout(pTHX_ GV *gv)
1145 (void)SvREFCNT_inc(gv);
1147 SvREFCNT_dec(PL_defoutgv);
1154 GV *newdefout, *egv;
1157 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1159 egv = GvEGV(PL_defoutgv);
1164 XPUSHs(&PL_sv_undef);
1166 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1167 if (gvp && *gvp == egv) {
1168 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1172 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1177 if (!GvIO(newdefout))
1178 gv_IOadd(newdefout);
1179 setdefout(newdefout);
1197 if (gv && (io = GvIO(gv))
1198 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1200 I32 gimme = GIMME_V;
1202 XPUSHs(SvTIED_obj((SV*)io, mg));
1205 call_method("GETC", gimme);
1208 if (gimme == G_SCALAR)
1209 SvSetMagicSV_nosteal(TARG, TOPs);
1212 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1213 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1214 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1215 report_evil_fh(gv, io, PL_op->op_type);
1216 SETERRNO(EBADF,RMS_IFI);
1220 sv_setpv(TARG, " ");
1221 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1222 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1223 /* Find out how many bytes the char needs */
1224 Size_t len = UTF8SKIP(SvPVX(TARG));
1227 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1228 SvCUR_set(TARG,1+len);
1238 return pp_sysread();
1242 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1244 register PERL_CONTEXT *cx;
1245 I32 gimme = GIMME_V;
1251 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1253 PAD_SET_CUR(CvPADLIST(cv), 1);
1255 setdefout(gv); /* locally select filehandle so $% et al work */
1288 SV *tmpsv = sv_newmortal();
1289 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1290 name = SvPV_nolen(tmpsv);
1293 DIE(aTHX_ "Undefined format \"%s\" called", name);
1294 DIE(aTHX_ "Not a format reference");
1297 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1299 IoFLAGS(io) &= ~IOf_DIDTOP;
1300 return doform(cv,gv,PL_op->op_next);
1306 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1307 register IO *io = GvIOp(gv);
1308 PerlIO *ofp = IoOFP(io);
1312 register PERL_CONTEXT *cx;
1314 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1315 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1318 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1319 PL_formtarget != PL_toptarget)
1323 if (!IoTOP_GV(io)) {
1327 if (!IoTOP_NAME(io)) {
1328 if (!IoFMT_NAME(io))
1329 IoFMT_NAME(io) = savepv(GvNAME(gv));
1330 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1331 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1332 if ((topgv && GvFORM(topgv)) ||
1333 !gv_fetchpv("top",FALSE,SVt_PVFM))
1334 IoTOP_NAME(io) = savepv(SvPVX(topname));
1336 IoTOP_NAME(io) = savepv("top");
1338 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1339 if (!topgv || !GvFORM(topgv)) {
1340 IoLINES_LEFT(io) = 100000000;
1343 IoTOP_GV(io) = topgv;
1345 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1346 I32 lines = IoLINES_LEFT(io);
1347 char *s = SvPVX(PL_formtarget);
1348 if (lines <= 0) /* Yow, header didn't even fit!!! */
1350 while (lines-- > 0) {
1351 s = strchr(s, '\n');
1357 STRLEN save = SvCUR(PL_formtarget);
1358 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1359 do_print(PL_formtarget, ofp);
1360 SvCUR_set(PL_formtarget, save);
1361 sv_chop(PL_formtarget, s);
1362 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1365 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1366 do_print(PL_formfeed, ofp);
1367 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1369 PL_formtarget = PL_toptarget;
1370 IoFLAGS(io) |= IOf_DIDTOP;
1373 DIE(aTHX_ "bad top format reference");
1378 SV *sv = sv_newmortal();
1379 gv_efullname4(sv, fgv, Nullch, FALSE);
1380 name = SvPV_nolen(sv);
1383 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1386 DIE(aTHX_ "Undefined top format called");
1390 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1391 return doform(cv,gv,PL_op);
1395 POPBLOCK(cx,PL_curpm);
1401 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1403 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1404 else if (ckWARN(WARN_CLOSED))
1405 report_evil_fh(gv, io, PL_op->op_type);
1410 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1411 if (ckWARN(WARN_IO))
1412 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1414 if (!do_print(PL_formtarget, fp))
1417 FmLINES(PL_formtarget) = 0;
1418 SvCUR_set(PL_formtarget, 0);
1419 *SvEND(PL_formtarget) = '\0';
1420 if (IoFLAGS(io) & IOf_FLUSH)
1421 (void)PerlIO_flush(fp);
1426 PL_formtarget = PL_bodytarget;
1428 return pop_return();
1433 dSP; dMARK; dORIGMARK;
1440 if (PL_op->op_flags & OPf_STACKED)
1445 if (gv && (io = GvIO(gv))
1446 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1448 if (MARK == ORIGMARK) {
1451 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1455 *MARK = SvTIED_obj((SV*)io, mg);
1458 call_method("PRINTF", G_SCALAR);
1461 MARK = ORIGMARK + 1;
1468 if (!(io = GvIO(gv))) {
1469 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1470 report_evil_fh(gv, io, PL_op->op_type);
1471 SETERRNO(EBADF,RMS_IFI);
1474 else if (!(fp = IoOFP(io))) {
1475 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1477 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1478 else if (ckWARN(WARN_CLOSED))
1479 report_evil_fh(gv, io, PL_op->op_type);
1481 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1485 do_sprintf(sv, SP - MARK, MARK + 1);
1486 if (!do_print(sv, fp))
1489 if (IoFLAGS(io) & IOf_FLUSH)
1490 if (PerlIO_flush(fp) == EOF)
1501 PUSHs(&PL_sv_undef);
1522 /* Need TIEHANDLE method ? */
1524 tmps = SvPV(sv, len);
1525 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1526 IoLINES(GvIOp(gv)) = 0;
1530 PUSHs(&PL_sv_undef);
1537 dSP; dMARK; dORIGMARK; dTARGET;
1544 Sock_size_t bufsize;
1551 bool charstart = FALSE;
1552 STRLEN charskip = 0;
1556 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1557 && gv && (io = GvIO(gv))
1558 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1563 *MARK = SvTIED_obj((SV*)io, mg);
1565 call_method("READ", G_SCALAR);
1578 sv_setpvn(bufsv, "", 0);
1579 length = SvIVx(*++MARK);
1582 offset = SvIVx(*++MARK);
1586 if (!io || !IoIFP(io)) {
1587 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1588 report_evil_fh(gv, io, PL_op->op_type);
1589 SETERRNO(EBADF,RMS_IFI);
1592 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1593 buffer = SvPVutf8_force(bufsv, blen);
1594 /* UTF-8 may not have been set if they are all low bytes */
1598 buffer = SvPV_force(bufsv, blen);
1601 DIE(aTHX_ "Negative length");
1609 if (PL_op->op_type == OP_RECV) {
1610 char namebuf[MAXPATHLEN];
1611 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1612 bufsize = sizeof (struct sockaddr_in);
1614 bufsize = sizeof namebuf;
1616 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1620 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1621 /* 'offset' means 'flags' here */
1622 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1623 (struct sockaddr *)namebuf, &bufsize);
1627 /* Bogus return without padding */
1628 bufsize = sizeof (struct sockaddr_in);
1630 SvCUR_set(bufsv, count);
1631 *SvEND(bufsv) = '\0';
1632 (void)SvPOK_only(bufsv);
1636 /* This should not be marked tainted if the fp is marked clean */
1637 if (!(IoFLAGS(io) & IOf_UNTAINT))
1638 SvTAINTED_on(bufsv);
1640 sv_setpvn(TARG, namebuf, bufsize);
1645 if (PL_op->op_type == OP_RECV)
1646 DIE(aTHX_ PL_no_sock_func, "recv");
1648 if (DO_UTF8(bufsv)) {
1649 /* offset adjust in characters not bytes */
1650 blen = sv_len_utf8(bufsv);
1653 if (-offset > (int)blen)
1654 DIE(aTHX_ "Offset outside string");
1657 if (DO_UTF8(bufsv)) {
1658 /* convert offset-as-chars to offset-as-bytes */
1659 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1662 bufsize = SvCUR(bufsv);
1663 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1664 if (offset > bufsize) { /* Zero any newly allocated space */
1665 Zero(buffer+bufsize, offset-bufsize, char);
1667 buffer = buffer + offset;
1669 if (PL_op->op_type == OP_SYSREAD) {
1670 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1671 if (IoTYPE(io) == IoTYPE_SOCKET) {
1672 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1678 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1683 #ifdef HAS_SOCKET__bad_code_maybe
1684 if (IoTYPE(io) == IoTYPE_SOCKET) {
1685 char namebuf[MAXPATHLEN];
1686 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1687 bufsize = sizeof (struct sockaddr_in);
1689 bufsize = sizeof namebuf;
1691 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1692 (struct sockaddr *)namebuf, &bufsize);
1697 count = PerlIO_read(IoIFP(io), buffer, length);
1698 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1699 if (count == 0 && PerlIO_error(IoIFP(io)))
1703 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1704 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1707 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1708 *SvEND(bufsv) = '\0';
1709 (void)SvPOK_only(bufsv);
1710 if (fp_utf8 && !IN_BYTES) {
1711 /* Look at utf8 we got back and count the characters */
1712 char *bend = buffer + count;
1713 while (buffer < bend) {
1715 skip = UTF8SKIP(buffer);
1718 if (buffer - charskip + skip > bend) {
1719 /* partial character - try for rest of it */
1720 length = skip - (bend-buffer);
1721 offset = bend - SvPVX(bufsv);
1733 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1734 provided amount read (count) was what was requested (length)
1736 if (got < wanted && count == length) {
1737 length = wanted - got;
1738 offset = bend - SvPVX(bufsv);
1741 /* return value is character count */
1746 /* This should not be marked tainted if the fp is marked clean */
1747 if (!(IoFLAGS(io) & IOf_UNTAINT))
1748 SvTAINTED_on(bufsv);
1761 int items = (SP - PL_stack_base) - TOPMARK;
1765 sv = sv_2mortal(newSViv(sv_len(*SP)));
1774 dSP; dMARK; dORIGMARK; dTARGET;
1785 if (PL_op->op_type == OP_SYSWRITE
1786 && gv && (io = GvIO(gv))
1787 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1792 *MARK = SvTIED_obj((SV*)io, mg);
1794 call_method("WRITE", G_SCALAR);
1805 #if Size_t_size > IVSIZE
1806 length = (Size_t)SvNVx(*++MARK);
1808 length = (Size_t)SvIVx(*++MARK);
1810 if ((SSize_t)length < 0)
1811 DIE(aTHX_ "Negative length");
1814 if (!io || !IoIFP(io)) {
1816 if (ckWARN(WARN_CLOSED))
1817 report_evil_fh(gv, io, PL_op->op_type);
1818 SETERRNO(EBADF,RMS_IFI);
1822 if (PerlIO_isutf8(IoIFP(io))) {
1823 buffer = SvPVutf8(bufsv, blen);
1826 if (DO_UTF8(bufsv)) {
1827 /* Not modifying source SV, so making a temporary copy. */
1828 bufsv = sv_2mortal(newSVsv(bufsv));
1829 sv_utf8_downgrade(bufsv, FALSE);
1831 buffer = SvPV(bufsv, blen);
1834 if (PL_op->op_type == OP_SYSWRITE) {
1836 if (DO_UTF8(bufsv)) {
1837 /* length and offset are in chars */
1838 blen = sv_len_utf8(bufsv);
1841 offset = SvIVx(*++MARK);
1843 if (-offset > (IV)blen)
1844 DIE(aTHX_ "Offset outside string");
1846 } else if (offset >= (IV)blen && blen > 0)
1847 DIE(aTHX_ "Offset outside string");
1850 if (length > blen - offset)
1851 length = blen - offset;
1852 if (DO_UTF8(bufsv)) {
1853 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1854 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1857 buffer = buffer+offset;
1859 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1860 if (IoTYPE(io) == IoTYPE_SOCKET) {
1861 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1867 /* See the note at doio.c:do_print about filesize limits. --jhi */
1868 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1873 else if (SP > MARK) {
1876 sockbuf = SvPVx(*++MARK, mlen);
1877 /* length is really flags */
1878 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1879 length, (struct sockaddr *)sockbuf, mlen);
1882 /* length is really flags */
1883 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1886 DIE(aTHX_ PL_no_sock_func, "send");
1892 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1893 #if Size_t_size > IVSIZE
1907 return pp_sysread();
1918 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1920 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1922 if (io && !IoIFP(io)) {
1923 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1925 IoFLAGS(io) &= ~IOf_START;
1926 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1927 sv_setpvn(GvSV(gv), "-", 1);
1928 SvSETMAGIC(GvSV(gv));
1930 else if (!nextargv(gv))
1935 gv = PL_last_in_gv; /* eof */
1938 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1940 if (gv && (io = GvIO(gv))
1941 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1944 XPUSHs(SvTIED_obj((SV*)io, mg));
1947 call_method("EOF", G_SCALAR);
1953 PUSHs(boolSV(!gv || do_eof(gv)));
1967 gv = PL_last_in_gv = (GV*)POPs;
1969 if (gv && (io = GvIO(gv))
1970 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1973 XPUSHs(SvTIED_obj((SV*)io, mg));
1976 call_method("TELL", G_SCALAR);
1982 #if LSEEKSIZE > IVSIZE
1983 PUSHn( do_tell(gv) );
1985 PUSHi( do_tell(gv) );
1992 return pp_sysseek();
2001 #if LSEEKSIZE > IVSIZE
2002 Off_t offset = (Off_t)SvNVx(POPs);
2004 Off_t offset = (Off_t)SvIVx(POPs);
2008 gv = PL_last_in_gv = (GV*)POPs;
2010 if (gv && (io = GvIO(gv))
2011 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2014 XPUSHs(SvTIED_obj((SV*)io, mg));
2015 #if LSEEKSIZE > IVSIZE
2016 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2018 XPUSHs(sv_2mortal(newSViv(offset)));
2020 XPUSHs(sv_2mortal(newSViv(whence)));
2023 call_method("SEEK", G_SCALAR);
2029 if (PL_op->op_type == OP_SEEK)
2030 PUSHs(boolSV(do_seek(gv, offset, whence)));
2032 Off_t sought = do_sysseek(gv, offset, whence);
2034 PUSHs(&PL_sv_undef);
2037 #if LSEEKSIZE > IVSIZE
2042 : newSVpvn(zero_but_true, ZBTLEN);
2043 PUSHs(sv_2mortal(sv));
2052 /* There seems to be no consensus on the length type of truncate()
2053 * and ftruncate(), both off_t and size_t have supporters. In
2054 * general one would think that when using large files, off_t is
2055 * at least as wide as size_t, so using an off_t should be okay. */
2056 /* XXX Configure probe for the length type of *truncate() needed XXX */
2059 #if Off_t_size > IVSIZE
2064 /* Checking for length < 0 is problematic as the type might or
2065 * might not be signed: if it is not, clever compilers will moan. */
2066 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2068 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2075 if (PL_op->op_flags & OPf_SPECIAL) {
2076 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2085 TAINT_PROPER("truncate");
2086 if (!(fp = IoIFP(io))) {
2092 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2094 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2104 if (SvTYPE(sv) == SVt_PVGV) {
2105 tmpgv = (GV*)sv; /* *main::FRED for example */
2106 goto do_ftruncate_gv;
2108 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2109 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2110 goto do_ftruncate_gv;
2112 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2113 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2114 goto do_ftruncate_io;
2117 name = SvPV(sv, n_a);
2118 TAINT_PROPER("truncate");
2120 if (truncate(name, len) < 0)
2126 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2129 if (my_chsize(tmpfd, len) < 0)
2131 PerlLIO_close(tmpfd);
2140 SETERRNO(EBADF,RMS_IFI);
2144 DIE(aTHX_ "truncate not implemented");
2157 unsigned int func = POPu;
2158 int optype = PL_op->op_type;
2162 IO *io = gv ? GvIOn(gv) : 0;
2164 if (!io || !argsv || !IoIFP(io)) {
2165 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2166 report_evil_fh(gv, io, PL_op->op_type);
2167 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2171 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2174 s = SvPV_force(argsv, len);
2175 need = IOCPARM_LEN(func);
2177 s = Sv_Grow(argsv, need + 1);
2178 SvCUR_set(argsv, need);
2181 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2184 retval = SvIV(argsv);
2185 s = INT2PTR(char*,retval); /* ouch */
2188 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2190 if (optype == OP_IOCTL)
2192 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2194 DIE(aTHX_ "ioctl is not implemented");
2198 DIE(aTHX_ "fcntl is not implemented");
2200 #if defined(OS2) && defined(__EMX__)
2201 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2203 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2207 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2209 if (s[SvCUR(argsv)] != 17)
2210 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2212 s[SvCUR(argsv)] = 0; /* put our null back */
2213 SvSETMAGIC(argsv); /* Assume it has changed */
2222 PUSHp(zero_but_true, ZBTLEN);
2243 if (gv && (io = GvIO(gv)))
2250 (void)PerlIO_flush(fp);
2251 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255 report_evil_fh(gv, io, PL_op->op_type);
2257 SETERRNO(EBADF,RMS_IFI);
2262 DIE(aTHX_ PL_no_func, "flock()");
2274 int protocol = POPi;
2280 io = gv ? GvIOn(gv) : NULL;
2283 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2284 report_evil_fh(gv, io, PL_op->op_type);
2286 do_close(gv, FALSE);
2287 SETERRNO(EBADF,LIB_INVARG);
2292 do_close(gv, FALSE);
2294 TAINT_PROPER("socket");
2295 fd = PerlSock_socket(domain, type, protocol);
2298 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2299 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2300 IoTYPE(io) = IoTYPE_SOCKET;
2301 if (!IoIFP(io) || !IoOFP(io)) {
2302 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2303 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2304 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2307 #if defined(HAS_FCNTL) && defined(F_SETFD)
2308 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2312 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2317 DIE(aTHX_ PL_no_sock_func, "socket");
2323 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2329 int protocol = POPi;
2336 io1 = gv1 ? GvIOn(gv1) : NULL;
2337 io2 = gv2 ? GvIOn(gv2) : NULL;
2338 if (!gv1 || !gv2 || !io1 || !io2) {
2339 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2341 report_evil_fh(gv1, io1, PL_op->op_type);
2343 report_evil_fh(gv1, io2, PL_op->op_type);
2346 do_close(gv1, FALSE);
2348 do_close(gv2, FALSE);
2353 do_close(gv1, FALSE);
2355 do_close(gv2, FALSE);
2357 TAINT_PROPER("socketpair");
2358 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2360 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2361 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2362 IoTYPE(io1) = IoTYPE_SOCKET;
2363 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2364 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2365 IoTYPE(io2) = IoTYPE_SOCKET;
2366 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2367 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2368 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2369 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2370 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2371 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2372 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2375 #if defined(HAS_FCNTL) && defined(F_SETFD)
2376 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2377 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2382 DIE(aTHX_ PL_no_sock_func, "socketpair");
2390 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2391 extern void GETPRIVMODE();
2392 extern void GETUSERMODE();
2397 register IO *io = GvIOn(gv);
2401 int mpeprivmode = 0;
2404 if (!io || !IoIFP(io))
2407 addr = SvPV(addrsv, len);
2408 TAINT_PROPER("bind");
2409 #ifdef MPE /* Deal with MPE bind() peculiarities */
2410 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2411 /* The address *MUST* stupidly be zero. */
2412 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2413 /* PRIV mode is required to bind() to ports < 1024. */
2414 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2415 ((struct sockaddr_in *)addr)->sin_port > 0) {
2416 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2421 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2422 (struct sockaddr *)addr, len) >= 0)
2425 #ifdef MPE /* Switch back to USER mode */
2436 if (ckWARN(WARN_CLOSED))
2437 report_evil_fh(gv, io, PL_op->op_type);
2438 SETERRNO(EBADF,SS_IVCHAN);
2441 DIE(aTHX_ PL_no_sock_func, "bind");
2452 register IO *io = GvIOn(gv);
2455 if (!io || !IoIFP(io))
2458 addr = SvPV(addrsv, len);
2459 TAINT_PROPER("connect");
2460 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2466 if (ckWARN(WARN_CLOSED))
2467 report_evil_fh(gv, io, PL_op->op_type);
2468 SETERRNO(EBADF,SS_IVCHAN);
2471 DIE(aTHX_ PL_no_sock_func, "connect");
2481 register IO *io = gv ? GvIOn(gv) : NULL;
2483 if (!gv || !io || !IoIFP(io))
2486 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2492 if (ckWARN(WARN_CLOSED))
2493 report_evil_fh(gv, io, PL_op->op_type);
2494 SETERRNO(EBADF,SS_IVCHAN);
2497 DIE(aTHX_ PL_no_sock_func, "listen");
2509 char namebuf[MAXPATHLEN];
2510 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2511 Sock_size_t len = sizeof (struct sockaddr_in);
2513 Sock_size_t len = sizeof namebuf;
2526 if (!gstio || !IoIFP(gstio))
2530 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2534 do_close(ngv, FALSE);
2535 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2536 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2537 IoTYPE(nstio) = IoTYPE_SOCKET;
2538 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2539 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2540 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2541 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2544 #if defined(HAS_FCNTL) && defined(F_SETFD)
2545 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2549 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2550 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2552 #ifdef __SCO_VERSION__
2553 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2556 PUSHp(namebuf, len);
2560 if (ckWARN(WARN_CLOSED))
2561 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2562 SETERRNO(EBADF,SS_IVCHAN);
2568 DIE(aTHX_ PL_no_sock_func, "accept");
2578 register IO *io = GvIOn(gv);
2580 if (!io || !IoIFP(io))
2583 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2587 if (ckWARN(WARN_CLOSED))
2588 report_evil_fh(gv, io, PL_op->op_type);
2589 SETERRNO(EBADF,SS_IVCHAN);
2592 DIE(aTHX_ PL_no_sock_func, "shutdown");
2599 return pp_ssockopt();
2601 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2609 int optype = PL_op->op_type;
2612 unsigned int optname;
2618 if (optype == OP_GSOCKOPT)
2619 sv = sv_2mortal(NEWSV(22, 257));
2622 optname = (unsigned int) POPi;
2623 lvl = (unsigned int) POPi;
2627 if (!io || !IoIFP(io))
2630 fd = PerlIO_fileno(IoIFP(io));
2634 (void)SvPOK_only(sv);
2638 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2653 aint = (int)SvIV(sv);
2657 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2666 if (ckWARN(WARN_CLOSED))
2667 report_evil_fh(gv, io, optype);
2668 SETERRNO(EBADF,SS_IVCHAN);
2673 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2680 return pp_getpeername();
2682 DIE(aTHX_ PL_no_sock_func, "getsockname");
2690 int optype = PL_op->op_type;
2694 register IO *io = GvIOn(gv);
2697 if (!io || !IoIFP(io))
2700 sv = sv_2mortal(NEWSV(22, 257));
2701 (void)SvPOK_only(sv);
2705 fd = PerlIO_fileno(IoIFP(io));
2707 case OP_GETSOCKNAME:
2708 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2711 case OP_GETPEERNAME:
2712 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2714 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2716 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";
2717 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2718 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2719 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2720 sizeof(u_short) + sizeof(struct in_addr))) {
2727 #ifdef BOGUS_GETNAME_RETURN
2728 /* Interactive Unix, getpeername() and getsockname()
2729 does not return valid namelen */
2730 if (len == BOGUS_GETNAME_RETURN)
2731 len = sizeof(struct sockaddr);
2739 if (ckWARN(WARN_CLOSED))
2740 report_evil_fh(gv, io, optype);
2741 SETERRNO(EBADF,SS_IVCHAN);
2746 DIE(aTHX_ PL_no_sock_func, "getpeername");
2765 if (PL_op->op_flags & OPf_REF) {
2767 if (PL_op->op_type == OP_LSTAT) {
2768 if (gv != PL_defgv) {
2769 if (ckWARN(WARN_IO))
2770 Perl_warner(aTHX_ packWARN(WARN_IO),
2771 "lstat() on filehandle %s", GvENAME(gv));
2772 } else if (PL_laststype != OP_LSTAT)
2773 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2777 if (gv != PL_defgv) {
2778 PL_laststype = OP_STAT;
2780 sv_setpv(PL_statname, "");
2781 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2782 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2784 if (PL_laststatval < 0) {
2785 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2786 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2792 if (SvTYPE(sv) == SVt_PVGV) {
2796 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2798 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2799 Perl_warner(aTHX_ packWARN(WARN_IO),
2800 "lstat() on filehandle %s", GvENAME(gv));
2803 sv_setpv(PL_statname, SvPV(sv,n_a));
2806 PL_laststype = PL_op->op_type;
2807 if (PL_op->op_type == OP_LSTAT)
2808 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2811 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2812 if (PL_laststatval < 0) {
2813 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2814 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2820 if (gimme != G_ARRAY) {
2821 if (gimme != G_VOID)
2822 XPUSHs(boolSV(max));
2828 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2830 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2832 #if Uid_t_size > IVSIZE
2833 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2835 # if Uid_t_sign <= 0
2836 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2838 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2841 #if Gid_t_size > IVSIZE
2842 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2844 # if Gid_t_sign <= 0
2845 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2847 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2850 #ifdef USE_STAT_RDEV
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2853 PUSHs(sv_2mortal(newSVpvn("", 0)));
2855 #if Off_t_size > IVSIZE
2856 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2858 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2861 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2862 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2863 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2865 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2866 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2867 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2869 #ifdef USE_STAT_BLOCKS
2870 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2871 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2873 PUSHs(sv_2mortal(newSVpvn("", 0)));
2874 PUSHs(sv_2mortal(newSVpvn("", 0)));
2884 #if defined(HAS_ACCESS) && defined(R_OK)
2886 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2887 result = access(POPpx, R_OK);
2902 if (cando(S_IRUSR, 0, &PL_statcache))
2911 #if defined(HAS_ACCESS) && defined(W_OK)
2913 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2914 result = access(POPpx, W_OK);
2929 if (cando(S_IWUSR, 0, &PL_statcache))
2938 #if defined(HAS_ACCESS) && defined(X_OK)
2940 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2941 result = access(POPpx, X_OK);
2956 if (cando(S_IXUSR, 0, &PL_statcache))
2965 #ifdef PERL_EFF_ACCESS_R_OK
2967 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2968 result = PERL_EFF_ACCESS_R_OK(POPpx);
2983 if (cando(S_IRUSR, 1, &PL_statcache))
2992 #ifdef PERL_EFF_ACCESS_W_OK
2994 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2995 result = PERL_EFF_ACCESS_W_OK(POPpx);
3010 if (cando(S_IWUSR, 1, &PL_statcache))
3019 #ifdef PERL_EFF_ACCESS_X_OK
3021 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3022 result = PERL_EFF_ACCESS_X_OK(POPpx);
3037 if (cando(S_IXUSR, 1, &PL_statcache))
3044 I32 result = my_stat();
3053 return pp_ftrowned();
3058 I32 result = my_stat();
3062 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3070 I32 result = my_stat();
3074 if (PL_statcache.st_size == 0)
3081 I32 result = my_stat();
3085 #if Off_t_size > IVSIZE
3086 PUSHn(PL_statcache.st_size);
3088 PUSHi(PL_statcache.st_size);
3095 I32 result = my_stat();
3099 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3105 I32 result = my_stat();
3109 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3115 I32 result = my_stat();
3119 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3125 I32 result = my_stat();
3129 if (S_ISSOCK(PL_statcache.st_mode))
3136 I32 result = my_stat();
3140 if (S_ISCHR(PL_statcache.st_mode))
3147 I32 result = my_stat();
3151 if (S_ISBLK(PL_statcache.st_mode))
3158 I32 result = my_stat();
3162 if (S_ISREG(PL_statcache.st_mode))
3169 I32 result = my_stat();
3173 if (S_ISDIR(PL_statcache.st_mode))
3180 I32 result = my_stat();
3184 if (S_ISFIFO(PL_statcache.st_mode))
3191 I32 result = my_lstat();
3195 if (S_ISLNK(PL_statcache.st_mode))
3204 I32 result = my_stat();
3208 if (PL_statcache.st_mode & S_ISUID)
3218 I32 result = my_stat();
3222 if (PL_statcache.st_mode & S_ISGID)
3232 I32 result = my_stat();
3236 if (PL_statcache.st_mode & S_ISVTX)
3247 char *tmps = Nullch;
3250 if (PL_op->op_flags & OPf_REF)
3252 else if (isGV(TOPs))
3254 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3255 gv = (GV*)SvRV(POPs);
3257 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3259 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3260 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3261 else if (tmps && isDIGIT(*tmps))
3265 if (PerlLIO_isatty(fd))
3270 #if defined(atarist) /* this will work with atariST. Configure will
3271 make guesses for other systems. */
3272 # define FILE_base(f) ((f)->_base)
3273 # define FILE_ptr(f) ((f)->_ptr)
3274 # define FILE_cnt(f) ((f)->_cnt)
3275 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3285 register STDCHAR *s;
3292 if (PL_op->op_flags & OPf_REF)
3294 else if (isGV(TOPs))
3296 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3297 gv = (GV*)SvRV(POPs);
3303 if (gv == PL_defgv) {
3305 io = GvIO(PL_statgv);
3308 goto really_filename;
3313 PL_laststatval = -1;
3314 sv_setpv(PL_statname, "");
3315 io = GvIO(PL_statgv);
3317 if (io && IoIFP(io)) {
3318 if (! PerlIO_has_base(IoIFP(io)))
3319 DIE(aTHX_ "-T and -B not implemented on filehandles");
3320 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3321 if (PL_laststatval < 0)
3323 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3324 if (PL_op->op_type == OP_FTTEXT)
3329 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3330 i = PerlIO_getc(IoIFP(io));
3332 (void)PerlIO_ungetc(IoIFP(io),i);
3334 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3336 len = PerlIO_get_bufsiz(IoIFP(io));
3337 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3338 /* sfio can have large buffers - limit to 512 */
3343 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3345 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3347 SETERRNO(EBADF,RMS_IFI);
3355 PL_laststatval = -1;
3356 PL_laststype = OP_STAT;
3357 sv_setpv(PL_statname, SvPV(sv, n_a));
3358 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3359 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3360 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3363 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3364 if (PL_laststatval < 0) {
3365 (void)PerlIO_close(fp);
3368 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3369 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3370 (void)PerlIO_close(fp);
3372 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3373 RETPUSHNO; /* special case NFS directories */
3374 RETPUSHYES; /* null file is anything */
3379 /* now scan s to look for textiness */
3380 /* XXX ASCII dependent code */
3382 #if defined(DOSISH) || defined(USEMYBINMODE)
3383 /* ignore trailing ^Z on short files */
3384 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3388 for (i = 0; i < len; i++, s++) {
3389 if (!*s) { /* null never allowed in text */
3394 else if (!(isPRINT(*s) || isSPACE(*s)))
3397 else if (*s & 128) {
3399 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3402 /* utf8 characters don't count as odd */
3403 if (UTF8_IS_START(*s)) {
3404 int ulen = UTF8SKIP(s);
3405 if (ulen < len - i) {
3407 for (j = 1; j < ulen; j++) {
3408 if (!UTF8_IS_CONTINUATION(s[j]))
3411 --ulen; /* loop does extra increment */
3421 *s != '\n' && *s != '\r' && *s != '\b' &&
3422 *s != '\t' && *s != '\f' && *s != 27)
3427 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3452 if( !tmps || !*tmps ) {
3453 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3454 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3456 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3461 deprecate("chdir('') or chdir(undef) as chdir()");
3462 tmps = SvPV(*svp, n_a);
3466 TAINT_PROPER("chdir");
3471 TAINT_PROPER("chdir");
3472 PUSHi( PerlDir_chdir(tmps) >= 0 );
3474 /* Clear the DEFAULT element of ENV so we'll get the new value
3476 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3484 dSP; dMARK; dTARGET;
3485 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3491 DIE(aTHX_ PL_no_func, "chown");
3501 TAINT_PROPER("chroot");
3502 PUSHi( chroot(tmps) >= 0 );
3505 DIE(aTHX_ PL_no_func, "chroot");
3511 dSP; dMARK; dTARGET;
3513 value = (I32)apply(PL_op->op_type, MARK, SP);
3521 dSP; dMARK; dTARGET;
3523 value = (I32)apply(PL_op->op_type, MARK, SP);
3531 dSP; dMARK; dTARGET;
3533 value = (I32)apply(PL_op->op_type, MARK, SP);
3545 char *tmps2 = POPpx;
3546 char *tmps = SvPV(TOPs, n_a);
3547 TAINT_PROPER("rename");
3549 anum = PerlLIO_rename(tmps, tmps2);
3551 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3552 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3555 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3556 (void)UNLINK(tmps2);
3557 if (!(anum = link(tmps, tmps2)))
3558 anum = UNLINK(tmps);
3571 char *tmps2 = POPpx;
3572 char *tmps = SvPV(TOPs, n_a);
3573 TAINT_PROPER("link");
3574 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3577 DIE(aTHX_ PL_no_func, "link");
3586 char *tmps2 = POPpx;
3587 char *tmps = SvPV(TOPs, n_a);
3588 TAINT_PROPER("symlink");
3589 SETi( symlink(tmps, tmps2) >= 0 );
3592 DIE(aTHX_ PL_no_func, "symlink");
3602 char buf[MAXPATHLEN];
3606 #ifndef INCOMPLETE_TAINTS
3610 len = readlink(tmps, buf, sizeof(buf) - 1);
3618 RETSETUNDEF; /* just pretend it's a normal file */
3622 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3624 S_dooneliner(pTHX_ char *cmd, char *filename)
3626 char *save_filename = filename;
3632 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3633 strcpy(cmdline, cmd);
3634 strcat(cmdline, " ");
3635 for (s = cmdline + strlen(cmdline); *filename; ) {
3640 myfp = PerlProc_popen(cmdline, "r");
3644 SV *tmpsv = sv_newmortal();
3645 /* Need to save/restore 'PL_rs' ?? */
3646 s = sv_gets(tmpsv, myfp, 0);
3647 (void)PerlProc_pclose(myfp);
3651 #ifdef HAS_SYS_ERRLIST
3656 /* you don't see this */
3658 #ifdef HAS_SYS_ERRLIST
3666 if (instr(s, errmsg)) {
3673 #define EACCES EPERM
3675 if (instr(s, "cannot make"))
3676 SETERRNO(EEXIST,RMS_FEX);
3677 else if (instr(s, "existing file"))
3678 SETERRNO(EEXIST,RMS_FEX);
3679 else if (instr(s, "ile exists"))
3680 SETERRNO(EEXIST,RMS_FEX);
3681 else if (instr(s, "non-exist"))
3682 SETERRNO(ENOENT,RMS_FNF);
3683 else if (instr(s, "does not exist"))
3684 SETERRNO(ENOENT,RMS_FNF);
3685 else if (instr(s, "not empty"))
3686 SETERRNO(EBUSY,SS_DEVOFFLINE);
3687 else if (instr(s, "cannot access"))
3688 SETERRNO(EACCES,RMS_PRV);
3690 SETERRNO(EPERM,RMS_PRV);
3693 else { /* some mkdirs return no failure indication */
3694 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3695 if (PL_op->op_type == OP_RMDIR)
3700 SETERRNO(EACCES,RMS_PRV); /* a guess */
3709 /* This macro removes trailing slashes from a directory name.
3710 * Different operating and file systems take differently to
3711 * trailing slashes. According to POSIX 1003.1 1996 Edition
3712 * any number of trailing slashes should be allowed.
3713 * Thusly we snip them away so that even non-conforming
3714 * systems are happy.
3715 * We should probably do this "filtering" for all
3716 * the functions that expect (potentially) directory names:
3717 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3718 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3720 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3721 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3724 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3725 (tmps) = savepvn((tmps), (len)); \
3745 TRIMSLASHES(tmps,len,copy);
3747 TAINT_PROPER("mkdir");
3749 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3751 SETi( dooneliner("mkdir", tmps) );
3752 oldumask = PerlLIO_umask(0);
3753 PerlLIO_umask(oldumask);
3754 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3768 TRIMSLASHES(tmps,len,copy);
3769 TAINT_PROPER("rmdir");
3771 SETi( PerlDir_rmdir(tmps) >= 0 );
3773 SETi( dooneliner("rmdir", tmps) );
3780 /* Directory calls. */
3784 #if defined(Direntry_t) && defined(HAS_READDIR)
3787 char *dirname = POPpx;
3789 register IO *io = GvIOn(gv);
3795 PerlDir_close(IoDIRP(io));
3796 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3802 SETERRNO(EBADF,RMS_DIR);
3805 DIE(aTHX_ PL_no_dir_func, "opendir");
3811 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3812 DIE(aTHX_ PL_no_dir_func, "readdir");
3814 #if !defined(I_DIRENT) && !defined(VMS)
3815 Direntry_t *readdir (DIR *);
3821 GV *gv = (GV *)POPs;
3822 register Direntry_t *dp;
3823 register IO *io = GvIOn(gv);
3825 if (!io || !IoDIRP(io))
3829 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3833 sv = newSVpvn(dp->d_name, dp->d_namlen);
3835 sv = newSVpv(dp->d_name, 0);
3837 #ifndef INCOMPLETE_TAINTS
3838 if (!(IoFLAGS(io) & IOf_UNTAINT))
3841 XPUSHs(sv_2mortal(sv));
3843 while (gimme == G_ARRAY);
3845 if (!dp && gimme != G_ARRAY)
3852 SETERRNO(EBADF,RMS_ISI);
3853 if (GIMME == G_ARRAY)
3862 #if defined(HAS_TELLDIR) || defined(telldir)
3864 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3865 /* XXX netbsd still seemed to.
3866 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3867 --JHI 1999-Feb-02 */
3868 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3869 long telldir (DIR *);
3872 register IO *io = GvIOn(gv);
3874 if (!io || !IoDIRP(io))
3877 PUSHi( PerlDir_tell(IoDIRP(io)) );
3881 SETERRNO(EBADF,RMS_ISI);
3884 DIE(aTHX_ PL_no_dir_func, "telldir");
3890 #if defined(HAS_SEEKDIR) || defined(seekdir)
3894 register IO *io = GvIOn(gv);
3896 if (!io || !IoDIRP(io))
3899 (void)PerlDir_seek(IoDIRP(io), along);
3904 SETERRNO(EBADF,RMS_ISI);
3907 DIE(aTHX_ PL_no_dir_func, "seekdir");
3913 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3916 register IO *io = GvIOn(gv);
3918 if (!io || !IoDIRP(io))
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)
3937 register IO *io = GvIOn(gv);
3939 if (!io || !IoDIRP(io))
3942 #ifdef VOID_CLOSEDIR
3943 PerlDir_close(IoDIRP(io));
3945 if (PerlDir_close(IoDIRP(io)) < 0) {
3946 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3955 SETERRNO(EBADF,RMS_IFI);
3958 DIE(aTHX_ PL_no_dir_func, "closedir");
3962 /* Process control. */
3972 PERL_FLUSHALL_FOR_CHILD;
3973 childpid = PerlProc_fork();
3978 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3979 SvREADONLY_off(GvSV(tmpgv));
3980 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3981 SvREADONLY_on(GvSV(tmpgv));
3983 #ifdef THREADS_HAVE_PIDS
3984 PL_ppid = (IV)getppid();
3986 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3991 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3996 PERL_FLUSHALL_FOR_CHILD;
3997 childpid = PerlProc_fork();
4003 DIE(aTHX_ PL_no_func, "fork");
4010 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4015 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4016 childpid = wait4pid(-1, &argflags, 0);
4018 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4023 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4024 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4025 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4027 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4032 DIE(aTHX_ PL_no_func, "wait");
4038 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4047 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4048 result = wait4pid(pid, &argflags, optype);
4050 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4055 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4056 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4057 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4059 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4064 DIE(aTHX_ PL_no_func, "waitpid");
4070 dSP; dMARK; dORIGMARK; dTARGET;
4078 while (++MARK <= SP) {
4079 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4084 TAINT_PROPER("system");
4086 PERL_FLUSHALL_FOR_CHILD;
4087 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4092 if (PerlProc_pipe(pp) >= 0)
4094 while ((childpid = PerlProc_fork()) == -1) {
4095 if (errno != EAGAIN) {
4100 PerlLIO_close(pp[0]);
4101 PerlLIO_close(pp[1]);
4108 Sigsave_t ihand,qhand; /* place to save signals during system() */
4112 PerlLIO_close(pp[1]);
4114 rsignal_save(SIGINT, SIG_IGN, &ihand);
4115 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4118 result = wait4pid(childpid, &status, 0);
4119 } while (result == -1 && errno == EINTR);
4121 (void)rsignal_restore(SIGINT, &ihand);
4122 (void)rsignal_restore(SIGQUIT, &qhand);
4124 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4125 do_execfree(); /* free any memory child malloced on fork */
4131 while (n < sizeof(int)) {
4132 n1 = PerlLIO_read(pp[0],
4133 (void*)(((char*)&errkid)+n),
4139 PerlLIO_close(pp[0]);
4140 if (n) { /* Error */
4141 if (n != sizeof(int))
4142 DIE(aTHX_ "panic: kid popen errno read");
4143 errno = errkid; /* Propagate errno from kid */
4144 STATUS_CURRENT = -1;
4147 PUSHi(STATUS_CURRENT);
4151 PerlLIO_close(pp[0]);
4152 #if defined(HAS_FCNTL) && defined(F_SETFD)
4153 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4156 if (PL_op->op_flags & OPf_STACKED) {
4157 SV *really = *++MARK;
4158 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4160 else if (SP - MARK != 1)
4161 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4163 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4167 #else /* ! FORK or VMS or OS/2 */
4170 if (PL_op->op_flags & OPf_STACKED) {
4171 SV *really = *++MARK;
4172 # if defined(WIN32) || defined(OS2)
4173 value = (I32)do_aspawn(really, MARK, SP);
4175 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4178 else if (SP - MARK != 1) {
4179 # if defined(WIN32) || defined(OS2)
4180 value = (I32)do_aspawn(Nullsv, MARK, SP);
4182 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4186 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4188 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4190 STATUS_NATIVE_SET(value);
4193 PUSHi(result ? value : STATUS_CURRENT);
4194 #endif /* !FORK or VMS */
4200 dSP; dMARK; dORIGMARK; dTARGET;
4206 while (++MARK <= SP) {
4207 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4212 TAINT_PROPER("exec");
4214 PERL_FLUSHALL_FOR_CHILD;
4215 if (PL_op->op_flags & OPf_STACKED) {
4216 SV *really = *++MARK;
4217 value = (I32)do_aexec(really, MARK, SP);
4219 else if (SP - MARK != 1)
4221 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4225 (void ) do_aspawn(Nullsv, MARK, SP);
4229 value = (I32)do_aexec(Nullsv, MARK, SP);
4234 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4237 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4240 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4253 dSP; dMARK; dTARGET;
4255 value = (I32)apply(PL_op->op_type, MARK, SP);
4260 DIE(aTHX_ PL_no_func, "kill");
4268 # ifdef THREADS_HAVE_PIDS
4271 XPUSHi( getppid() );
4275 DIE(aTHX_ PL_no_func, "getppid");
4291 pgrp = (I32)BSD_GETPGRP(pid);
4293 if (pid != 0 && pid != PerlProc_getpid())
4294 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4300 DIE(aTHX_ PL_no_func, "getpgrp()");
4319 TAINT_PROPER("setpgrp");
4321 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4323 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4324 || (pid != 0 && pid != PerlProc_getpid()))
4326 DIE(aTHX_ "setpgrp can't take arguments");
4328 SETi( setpgrp() >= 0 );
4329 #endif /* USE_BSDPGRP */
4332 DIE(aTHX_ PL_no_func, "setpgrp()");
4338 #ifdef HAS_GETPRIORITY
4342 SETi( getpriority(which, who) );
4345 DIE(aTHX_ PL_no_func, "getpriority()");
4351 #ifdef HAS_SETPRIORITY
4356 TAINT_PROPER("setpriority");
4357 SETi( setpriority(which, who, niceval) >= 0 );
4360 DIE(aTHX_ PL_no_func, "setpriority()");
4370 XPUSHn( time(Null(Time_t*)) );
4372 XPUSHi( time(Null(Time_t*)) );
4383 (void)PerlProc_times(&PL_timesbuf);
4385 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4386 /* struct tms, though same data */
4390 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4391 if (GIMME == G_ARRAY) {
4392 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4393 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4394 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4400 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4402 if (GIMME == G_ARRAY) {
4403 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4404 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4405 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4409 DIE(aTHX_ "times not implemented");
4411 #endif /* HAS_TIMES */
4424 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4425 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4426 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4432 when = (Time_t)SvNVx(POPs);
4434 when = (Time_t)SvIVx(POPs);
4437 if (PL_op->op_type == OP_LOCALTIME)
4438 tmbuf = localtime(&when);
4440 tmbuf = gmtime(&when);
4442 if (GIMME != G_ARRAY) {
4448 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4449 dayname[tmbuf->tm_wday],
4450 monname[tmbuf->tm_mon],
4455 tmbuf->tm_year + 1900);
4456 PUSHs(sv_2mortal(tsv));
4461 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4462 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4463 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4464 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4465 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4466 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4467 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4468 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4469 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4480 anum = alarm((unsigned int)anum);
4487 DIE(aTHX_ PL_no_func, "alarm");
4498 (void)time(&lasttime);
4503 PerlProc_sleep((unsigned int)duration);
4506 XPUSHi(when - lasttime);
4510 /* Shared memory. */
4524 return pp_shmwrite();
4529 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4530 dSP; dMARK; dTARGET;
4531 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4540 /* Message passing. */
4554 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4555 dSP; dMARK; dTARGET;
4556 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4567 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4568 dSP; dMARK; dTARGET;
4569 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4582 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4583 dSP; dMARK; dTARGET;
4584 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4591 DIE(aTHX_ "System V IPC is not implemented on this machine");
4597 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4598 dSP; dMARK; dTARGET;
4599 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4607 PUSHp(zero_but_true, ZBTLEN);
4617 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4618 dSP; dMARK; dTARGET;
4619 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4628 /* Get system info. */
4632 #ifdef HAS_GETHOSTBYNAME
4633 return pp_ghostent();
4635 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4641 #ifdef HAS_GETHOSTBYADDR
4642 return pp_ghostent();
4644 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4650 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4652 I32 which = PL_op->op_type;
4653 register char **elem;
4655 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4656 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4657 struct hostent *gethostbyname(Netdb_name_t);
4658 struct hostent *gethostent(void);
4660 struct hostent *hent;
4665 if (which == OP_GHBYNAME) {
4666 #ifdef HAS_GETHOSTBYNAME
4667 char* name = POPpbytex;
4668 hent = PerlSock_gethostbyname(name);
4670 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4673 else if (which == OP_GHBYADDR) {
4674 #ifdef HAS_GETHOSTBYADDR
4675 int addrtype = POPi;
4678 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4680 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4682 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4686 #ifdef HAS_GETHOSTENT
4687 hent = PerlSock_gethostent();
4689 DIE(aTHX_ PL_no_sock_func, "gethostent");
4692 #ifdef HOST_NOT_FOUND
4694 #ifdef USE_REENTRANT_API
4695 # ifdef USE_GETHOSTENT_ERRNO
4696 h_errno = PL_reentrant_buffer->_gethostent_errno;
4699 STATUS_NATIVE_SET(h_errno);
4703 if (GIMME != G_ARRAY) {
4704 PUSHs(sv = sv_newmortal());
4706 if (which == OP_GHBYNAME) {
4708 sv_setpvn(sv, hent->h_addr, hent->h_length);
4711 sv_setpv(sv, (char*)hent->h_name);
4717 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4718 sv_setpv(sv, (char*)hent->h_name);
4719 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4720 for (elem = hent->h_aliases; elem && *elem; elem++) {
4721 sv_catpv(sv, *elem);
4723 sv_catpvn(sv, " ", 1);
4725 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4726 sv_setiv(sv, (IV)hent->h_addrtype);
4727 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4728 len = hent->h_length;
4729 sv_setiv(sv, (IV)len);
4731 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4732 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4733 sv_setpvn(sv, *elem, len);
4736 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4738 sv_setpvn(sv, hent->h_addr, len);
4743 DIE(aTHX_ PL_no_sock_func, "gethostent");
4749 #ifdef HAS_GETNETBYNAME
4750 return pp_gnetent();
4752 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4758 #ifdef HAS_GETNETBYADDR
4759 return pp_gnetent();
4761 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4767 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4769 I32 which = PL_op->op_type;
4770 register char **elem;
4772 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4773 struct netent *getnetbyaddr(Netdb_net_t, int);
4774 struct netent *getnetbyname(Netdb_name_t);
4775 struct netent *getnetent(void);
4777 struct netent *nent;
4780 if (which == OP_GNBYNAME){
4781 #ifdef HAS_GETNETBYNAME
4782 char *name = POPpbytex;
4783 nent = PerlSock_getnetbyname(name);
4785 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4788 else if (which == OP_GNBYADDR) {
4789 #ifdef HAS_GETNETBYADDR
4790 int addrtype = POPi;
4791 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4792 nent = PerlSock_getnetbyaddr(addr, addrtype);
4794 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4798 #ifdef HAS_GETNETENT
4799 nent = PerlSock_getnetent();
4801 DIE(aTHX_ PL_no_sock_func, "getnetent");
4804 #ifdef HOST_NOT_FOUND
4806 #ifdef USE_REENTRANT_API
4807 # ifdef USE_GETNETENT_ERRNO
4808 h_errno = PL_reentrant_buffer->_getnetent_errno;
4811 STATUS_NATIVE_SET(h_errno);
4816 if (GIMME != G_ARRAY) {
4817 PUSHs(sv = sv_newmortal());
4819 if (which == OP_GNBYNAME)
4820 sv_setiv(sv, (IV)nent->n_net);
4822 sv_setpv(sv, nent->n_name);
4828 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829 sv_setpv(sv, nent->n_name);
4830 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 for (elem = nent->n_aliases; elem && *elem; elem++) {
4832 sv_catpv(sv, *elem);
4834 sv_catpvn(sv, " ", 1);
4836 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4837 sv_setiv(sv, (IV)nent->n_addrtype);
4838 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4839 sv_setiv(sv, (IV)nent->n_net);
4844 DIE(aTHX_ PL_no_sock_func, "getnetent");
4850 #ifdef HAS_GETPROTOBYNAME
4851 return pp_gprotoent();
4853 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4859 #ifdef HAS_GETPROTOBYNUMBER
4860 return pp_gprotoent();
4862 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4868 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4870 I32 which = PL_op->op_type;
4871 register char **elem;
4873 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4874 struct protoent *getprotobyname(Netdb_name_t);
4875 struct protoent *getprotobynumber(int);
4876 struct protoent *getprotoent(void);
4878 struct protoent *pent;
4881 if (which == OP_GPBYNAME) {
4882 #ifdef HAS_GETPROTOBYNAME
4883 char* name = POPpbytex;
4884 pent = PerlSock_getprotobyname(name);
4886 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4889 else if (which == OP_GPBYNUMBER) {
4890 #ifdef HAS_GETPROTOBYNUMBER
4892 pent = PerlSock_getprotobynumber(number);
4894 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4898 #ifdef HAS_GETPROTOENT
4899 pent = PerlSock_getprotoent();
4901 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4905 if (GIMME != G_ARRAY) {
4906 PUSHs(sv = sv_newmortal());
4908 if (which == OP_GPBYNAME)
4909 sv_setiv(sv, (IV)pent->p_proto);
4911 sv_setpv(sv, pent->p_name);
4917 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4918 sv_setpv(sv, pent->p_name);
4919 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4920 for (elem = pent->p_aliases; elem && *elem; elem++) {
4921 sv_catpv(sv, *elem);
4923 sv_catpvn(sv, " ", 1);
4925 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4926 sv_setiv(sv, (IV)pent->p_proto);
4931 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4937 #ifdef HAS_GETSERVBYNAME
4938 return pp_gservent();
4940 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4946 #ifdef HAS_GETSERVBYPORT
4947 return pp_gservent();
4949 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4955 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4957 I32 which = PL_op->op_type;
4958 register char **elem;
4960 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4961 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4962 struct servent *getservbyport(int, Netdb_name_t);
4963 struct servent *getservent(void);
4965 struct servent *sent;
4968 if (which == OP_GSBYNAME) {
4969 #ifdef HAS_GETSERVBYNAME
4970 char *proto = POPpbytex;
4971 char *name = POPpbytex;
4973 if (proto && !*proto)
4976 sent = PerlSock_getservbyname(name, proto);
4978 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4981 else if (which == OP_GSBYPORT) {
4982 #ifdef HAS_GETSERVBYPORT
4983 char *proto = POPpbytex;
4984 unsigned short port = (unsigned short)POPu;
4986 if (proto && !*proto)
4990 port = PerlSock_htons(port);
4992 sent = PerlSock_getservbyport(port, proto);
4994 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4998 #ifdef HAS_GETSERVENT
4999 sent = PerlSock_getservent();
5001 DIE(aTHX_ PL_no_sock_func, "getservent");
5005 if (GIMME != G_ARRAY) {
5006 PUSHs(sv = sv_newmortal());
5008 if (which == OP_GSBYNAME) {
5010 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5012 sv_setiv(sv, (IV)(sent->s_port));
5016 sv_setpv(sv, sent->s_name);
5022 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5023 sv_setpv(sv, sent->s_name);
5024 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5025 for (elem = sent->s_aliases; elem && *elem; elem++) {
5026 sv_catpv(sv, *elem);
5028 sv_catpvn(sv, " ", 1);
5030 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5032 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5034 sv_setiv(sv, (IV)(sent->s_port));
5036 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5037 sv_setpv(sv, sent->s_proto);
5042 DIE(aTHX_ PL_no_sock_func, "getservent");
5048 #ifdef HAS_SETHOSTENT
5050 PerlSock_sethostent(TOPi);
5053 DIE(aTHX_ PL_no_sock_func, "sethostent");
5059 #ifdef HAS_SETNETENT
5061 PerlSock_setnetent(TOPi);
5064 DIE(aTHX_ PL_no_sock_func, "setnetent");
5070 #ifdef HAS_SETPROTOENT
5072 PerlSock_setprotoent(TOPi);
5075 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5081 #ifdef HAS_SETSERVENT
5083 PerlSock_setservent(TOPi);
5086 DIE(aTHX_ PL_no_sock_func, "setservent");
5092 #ifdef HAS_ENDHOSTENT
5094 PerlSock_endhostent();
5098 DIE(aTHX_ PL_no_sock_func, "endhostent");
5104 #ifdef HAS_ENDNETENT
5106 PerlSock_endnetent();
5110 DIE(aTHX_ PL_no_sock_func, "endnetent");
5116 #ifdef HAS_ENDPROTOENT
5118 PerlSock_endprotoent();
5122 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5128 #ifdef HAS_ENDSERVENT
5130 PerlSock_endservent();
5134 DIE(aTHX_ PL_no_sock_func, "endservent");
5143 DIE(aTHX_ PL_no_func, "getpwnam");
5152 DIE(aTHX_ PL_no_func, "getpwuid");
5160 I32 which = PL_op->op_type;
5163 struct passwd *pwent = NULL;
5165 * We currently support only the SysV getsp* shadow password interface.
5166 * The interface is declared in <shadow.h> and often one needs to link
5167 * with -lsecurity or some such.
5168 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5171 * AIX getpwnam() is clever enough to return the encrypted password
5172 * only if the caller (euid?) is root.
5174 * There are at least three other shadow password APIs. Many platforms
5175 * seem to contain more than one interface for accessing the shadow
5176 * password databases, possibly for compatibility reasons.
5177 * The getsp*() is by far he simplest one, the other two interfaces
5178 * are much more complicated, but also very similar to each other.
5183 * struct pr_passwd *getprpw*();
5184 * The password is in
5185 * char getprpw*(...).ufld.fd_encrypt[]
5186 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5191 * struct es_passwd *getespw*();
5192 * The password is in
5193 * char *(getespw*(...).ufld.fd_encrypt)
5194 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5197 * struct userpw *getuserpw();
5198 * The password is in
5199 * char *(getuserpw(...)).spw_upw_passwd
5200 * (but the de facto standard getpwnam() should work okay)
5202 * Mention I_PROT here so that Configure probes for it.
5204 * In HP-UX for getprpw*() the manual page claims that one should include
5205 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5206 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5207 * and pp_sys.c already includes <shadow.h> if there is such.
5209 * Note that <sys/security.h> is already probed for, but currently
5210 * it is only included in special cases.
5212 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5213 * be preferred interface, even though also the getprpw*() interface
5214 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5215 * One also needs to call set_auth_parameters() in main() before
5216 * doing anything else, whether one is using getespw*() or getprpw*().
5218 * Note that accessing the shadow databases can be magnitudes
5219 * slower than accessing the standard databases.
5224 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5225 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5226 * the pw_comment is left uninitialized. */
5227 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5233 char* name = POPpbytex;
5234 pwent = getpwnam(name);
5240 pwent = getpwuid(uid);
5244 # ifdef HAS_GETPWENT
5246 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5247 if (pwent) pwent = getpwnam(pwent->pw_name);
5250 DIE(aTHX_ PL_no_func, "getpwent");
5256 if (GIMME != G_ARRAY) {
5257 PUSHs(sv = sv_newmortal());
5259 if (which == OP_GPWNAM)
5260 # if Uid_t_sign <= 0
5261 sv_setiv(sv, (IV)pwent->pw_uid);
5263 sv_setuv(sv, (UV)pwent->pw_uid);
5266 sv_setpv(sv, pwent->pw_name);
5272 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5273 sv_setpv(sv, pwent->pw_name);
5275 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5277 /* If we have getspnam(), we try to dig up the shadow
5278 * password. If we are underprivileged, the shadow
5279 * interface will set the errno to EACCES or similar,
5280 * and return a null pointer. If this happens, we will
5281 * use the dummy password (usually "*" or "x") from the
5282 * standard password database.
5284 * In theory we could skip the shadow call completely
5285 * if euid != 0 but in practice we cannot know which
5286 * security measures are guarding the shadow databases
5287 * on a random platform.
5289 * Resist the urge to use additional shadow interfaces.
5290 * Divert the urge to writing an extension instead.
5293 /* Some AIX setups falsely(?) detect some getspnam(), which
5294 * has a different API than the Solaris/IRIX one. */
5295 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5297 struct spwd *spwent;
5298 int saverrno; /* Save and restore errno so that
5299 * underprivileged attempts seem
5300 * to have never made the unsccessful
5301 * attempt to retrieve the shadow password. */
5304 spwent = getspnam(pwent->pw_name);
5306 if (spwent && spwent->sp_pwdp)
5307 sv_setpv(sv, spwent->sp_pwdp);
5311 if (!SvPOK(sv)) /* Use the standard password, then. */
5312 sv_setpv(sv, pwent->pw_passwd);
5315 # ifndef INCOMPLETE_TAINTS
5316 /* passwd is tainted because user himself can diddle with it.
5317 * admittedly not much and in a very limited way, but nevertheless. */
5321 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5322 # if Uid_t_sign <= 0
5323 sv_setiv(sv, (IV)pwent->pw_uid);
5325 sv_setuv(sv, (UV)pwent->pw_uid);
5328 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5329 # if Uid_t_sign <= 0
5330 sv_setiv(sv, (IV)pwent->pw_gid);
5332 sv_setuv(sv, (UV)pwent->pw_gid);
5334 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5335 * because of the poor interface of the Perl getpw*(),
5336 * not because there's some standard/convention saying so.
5337 * A better interface would have been to return a hash,
5338 * but we are accursed by our history, alas. --jhi. */
5339 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5341 sv_setiv(sv, (IV)pwent->pw_change);
5344 sv_setiv(sv, (IV)pwent->pw_quota);
5347 sv_setpv(sv, pwent->pw_age);
5352 /* pw_class and pw_comment are mutually exclusive--.
5353 * see the above note for pw_change, pw_quota, and pw_age. */
5354 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5356 sv_setpv(sv, pwent->pw_class);
5359 sv_setpv(sv, pwent->pw_comment);
5363 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5365 sv_setpv(sv, pwent->pw_gecos);
5367 # ifndef INCOMPLETE_TAINTS
5368 /* pw_gecos is tainted because user himself can diddle with it. */
5372 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5373 sv_setpv(sv, pwent->pw_dir);
5375 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5376 sv_setpv(sv, pwent->pw_shell);
5377 # ifndef INCOMPLETE_TAINTS
5378 /* pw_shell is tainted because user himself can diddle with it. */
5383 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5384 sv_setiv(sv, (IV)pwent->pw_expire);
5389 DIE(aTHX_ PL_no_func, "getpwent");
5395 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5400 DIE(aTHX_ PL_no_func, "setpwent");
5406 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5411 DIE(aTHX_ PL_no_func, "endpwent");
5420 DIE(aTHX_ PL_no_func, "getgrnam");
5429 DIE(aTHX_ PL_no_func, "getgrgid");
5437 I32 which = PL_op->op_type;
5438 register char **elem;
5440 struct group *grent;
5443 if (which == OP_GGRNAM) {
5444 char* name = POPpbytex;
5445 grent = (struct group *)getgrnam(name);
5447 else if (which == OP_GGRGID) {
5449 grent = (struct group *)getgrgid(gid);
5453 grent = (struct group *)getgrent();
5455 DIE(aTHX_ PL_no_func, "getgrent");
5459 if (GIMME != G_ARRAY) {
5460 PUSHs(sv = sv_newmortal());
5462 if (which == OP_GGRNAM)
5463 sv_setiv(sv, (IV)grent->gr_gid);
5465 sv_setpv(sv, grent->gr_name);
5471 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5472 sv_setpv(sv, grent->gr_name);
5474 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5476 sv_setpv(sv, grent->gr_passwd);
5479 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5480 sv_setiv(sv, (IV)grent->gr_gid);
5482 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5483 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5484 /* In UNICOS/mk (_CRAYMPP) the multithreading
5485 * versions (getgrnam_r, getgrgid_r)
5486 * seem to return an illegal pointer
5487 * as the group members list, gr_mem.
5488 * getgrent() doesn't even have a _r version
5489 * but the gr_mem is poisonous anyway.
5490 * So yes, you cannot get the list of group
5491 * members if building multithreaded in UNICOS/mk. */
5492 for (elem = grent->gr_mem; elem && *elem; elem++) {
5493 sv_catpv(sv, *elem);
5495 sv_catpvn(sv, " ", 1);
5502 DIE(aTHX_ PL_no_func, "getgrent");
5508 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5513 DIE(aTHX_ PL_no_func, "setgrent");
5519 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5524 DIE(aTHX_ PL_no_func, "endgrent");
5534 if (!(tmps = PerlProc_getlogin()))
5536 PUSHp(tmps, strlen(tmps));
5539 DIE(aTHX_ PL_no_func, "getlogin");
5543 /* Miscellaneous. */
5548 dSP; dMARK; dORIGMARK; dTARGET;
5549 register I32 items = SP - MARK;
5550 unsigned long a[20];
5556 while (++MARK <= SP) {
5557 if (SvTAINTED(*MARK)) {
5563 TAINT_PROPER("syscall");
5566 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5567 * or where sizeof(long) != sizeof(char*). But such machines will
5568 * not likely have syscall implemented either, so who cares?
5570 while (++MARK <= SP) {
5571 if (SvNIOK(*MARK) || !i)
5572 a[i++] = SvIV(*MARK);
5573 else if (*MARK == &PL_sv_undef)
5576 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5582 DIE(aTHX_ "Too many args to syscall");
5584 DIE(aTHX_ "Too few args to syscall");
5586 retval = syscall(a[0]);
5589 retval = syscall(a[0],a[1]);
5592 retval = syscall(a[0],a[1],a[2]);
5595 retval = syscall(a[0],a[1],a[2],a[3]);
5598 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5601 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5604 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5607 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5611 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5614 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5617 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5621 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5625 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5629 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5630 a[10],a[11],a[12],a[13]);
5632 #endif /* atarist */
5638 DIE(aTHX_ PL_no_func, "syscall");
5642 #ifdef FCNTL_EMULATE_FLOCK
5644 /* XXX Emulate flock() with fcntl().
5645 What's really needed is a good file locking module.
5649 fcntl_emulate_flock(int fd, int operation)
5653 switch (operation & ~LOCK_NB) {
5655 flock.l_type = F_RDLCK;
5658 flock.l_type = F_WRLCK;
5661 flock.l_type = F_UNLCK;
5667 flock.l_whence = SEEK_SET;
5668 flock.l_start = flock.l_len = (Off_t)0;
5670 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5673 #endif /* FCNTL_EMULATE_FLOCK */
5675 #ifdef LOCKF_EMULATE_FLOCK
5677 /* XXX Emulate flock() with lockf(). This is just to increase
5678 portability of scripts. The calls are not completely
5679 interchangeable. What's really needed is a good file
5683 /* The lockf() constants might have been defined in <unistd.h>.
5684 Unfortunately, <unistd.h> causes troubles on some mixed
5685 (BSD/POSIX) systems, such as SunOS 4.1.3.
5687 Further, the lockf() constants aren't POSIX, so they might not be
5688 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5689 just stick in the SVID values and be done with it. Sigh.
5693 # define F_ULOCK 0 /* Unlock a previously locked region */
5696 # define F_LOCK 1 /* Lock a region for exclusive use */
5699 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5702 # define F_TEST 3 /* Test a region for other processes locks */
5706 lockf_emulate_flock(int fd, int operation)
5712 /* flock locks entire file so for lockf we need to do the same */
5714 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5715 if (pos > 0) /* is seekable and needs to be repositioned */
5716 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5717 pos = -1; /* seek failed, so don't seek back afterwards */
5720 switch (operation) {
5722 /* LOCK_SH - get a shared lock */
5724 /* LOCK_EX - get an exclusive lock */
5726 i = lockf (fd, F_LOCK, 0);
5729 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5730 case LOCK_SH|LOCK_NB:
5731 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5732 case LOCK_EX|LOCK_NB:
5733 i = lockf (fd, F_TLOCK, 0);
5735 if ((errno == EAGAIN) || (errno == EACCES))
5736 errno = EWOULDBLOCK;
5739 /* LOCK_UN - unlock (non-blocking is a no-op) */
5741 case LOCK_UN|LOCK_NB:
5742 i = lockf (fd, F_ULOCK, 0);
5745 /* Default - can't decipher operation */
5752 if (pos > 0) /* need to restore position of the handle */
5753 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5758 #endif /* LOCKF_EMULATE_FLOCK */