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 */
179 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
180 #undef PERL_EFF_ACCESS_W_OK
181 #undef PERL_EFF_ACCESS_X_OK
183 /* F_OK unused: if stat() cannot find it... */
185 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
186 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
187 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
188 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
189 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
192 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
193 # ifdef I_SYS_SECURITY
194 # include <sys/security.h>
198 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
200 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
203 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
204 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
205 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
209 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
211 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
212 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
213 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
216 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
217 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
218 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
221 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
223 Uid_t ruid = getuid();
224 Uid_t euid = geteuid();
225 Gid_t rgid = getgid();
226 Gid_t egid = getegid();
230 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
231 Perl_croak(aTHX_ "switching effective uid is not implemented");
234 if (setreuid(euid, ruid))
237 if (setresuid(euid, ruid, (Uid_t)-1))
240 Perl_croak(aTHX_ "entering effective uid failed");
243 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
244 Perl_croak(aTHX_ "switching effective gid is not implemented");
247 if (setregid(egid, rgid))
250 if (setresgid(egid, rgid, (Gid_t)-1))
253 Perl_croak(aTHX_ "entering effective gid failed");
256 res = access(path, mode);
259 if (setreuid(ruid, euid))
262 if (setresuid(ruid, euid, (Uid_t)-1))
265 Perl_croak(aTHX_ "leaving effective uid failed");
268 if (setregid(rgid, egid))
271 if (setresgid(rgid, egid, (Gid_t)-1))
274 Perl_croak(aTHX_ "leaving effective gid failed");
279 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
280 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
281 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
284 #if !defined(PERL_EFF_ACCESS_R_OK)
285 /* With it or without it: anyway you get a warning: either that
286 it is unused, or it is declared static and never defined.
289 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
291 Perl_croak(aTHX_ "switching effective uid is not implemented");
307 if (PL_op->op_private & OPpOPEN_IN_RAW)
309 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
311 fp = PerlProc_popen(tmps, mode);
314 if (PL_curcop->cop_io) {
315 type = SvPV_nolen(PL_curcop->cop_io);
318 PerlIO_apply_layers(aTHX_ fp,mode,type);
320 if (gimme == G_VOID) {
322 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
326 else if (gimme == G_SCALAR) {
328 PL_rs = &PL_sv_undef;
329 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
330 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
342 if (sv_gets(sv, fp, 0) == Nullch) {
346 XPUSHs(sv_2mortal(sv));
347 if (SvLEN(sv) - SvCUR(sv) > 20) {
348 SvLEN_set(sv, SvCUR(sv)+1);
349 Renew(SvPVX(sv), SvLEN(sv), char);
354 STATUS_NATIVE_SET(PerlProc_pclose(fp));
355 TAINT; /* "I believe that this is not gratuitous!" */
358 STATUS_NATIVE_SET(-1);
359 if (gimme == G_SCALAR)
369 tryAMAGICunTARGET(iter, -1);
371 /* Note that we only ever get here if File::Glob fails to load
372 * without at the same time croaking, for some reason, or if
373 * perl was built with PERL_EXTERNAL_GLOB */
380 * The external globbing program may use things we can't control,
381 * so for security reasons we must assume the worst.
384 taint_proper(PL_no_security, "glob");
388 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
389 PL_last_in_gv = (GV*)*PL_stack_sp--;
391 SAVESPTR(PL_rs); /* This is not permanent, either. */
392 PL_rs = sv_2mortal(newSVpvn("\000", 1));
395 *SvPVX(PL_rs) = '\n';
399 result = do_readline();
406 PL_last_in_gv = cGVOP_gv;
407 return do_readline();
416 if (SP - MARK != 1) {
418 do_join(TARG, &PL_sv_no, MARK, SP);
425 tmps = SvPV(tmpsv, len);
426 if ((!tmps || !len) && PL_errgv) {
428 (void)SvUPGRADE(error, SVt_PV);
429 if (SvPOK(error) && SvCUR(error))
430 sv_catpv(error, "\t...caught");
432 tmps = SvPV(tmpsv, len);
435 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
437 Perl_warn(aTHX_ "%"SVf, tmpsv);
449 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
451 if (SP - MARK != 1) {
453 do_join(TARG, &PL_sv_no, MARK, SP);
455 tmps = SvPV(tmpsv, len);
461 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
465 (void)SvUPGRADE(error, SVt_PV);
466 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
468 SvSetSV(error,tmpsv);
469 else if (sv_isobject(error)) {
470 HV *stash = SvSTASH(SvRV(error));
471 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
473 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
474 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
481 call_sv((SV*)GvCV(gv),
482 G_SCALAR|G_EVAL|G_KEEPERR);
483 sv_setsv(error,*PL_stack_sp--);
486 DIE(aTHX_ Nullformat);
489 if (SvPOK(error) && SvCUR(error))
490 sv_catpv(error, "\t...propagated");
492 tmps = SvPV(tmpsv, len);
496 tmpsv = sv_2mortal(newSVpvn("Died", 4));
498 DIE(aTHX_ "%"SVf, tmpsv);
518 DIE(aTHX_ PL_no_usym, "filehandle");
519 if ((io = GvIOp(gv)))
520 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
522 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
523 /* Method's args are same as ours ... */
524 /* ... except handle is replaced by the object */
525 *MARK-- = SvTIED_obj((SV*)io, mg);
529 call_method("OPEN", G_SCALAR);
542 tmps = SvPV(sv, len);
543 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
546 PUSHi( (I32)PL_forkprocess );
547 else if (PL_forkprocess == 0) /* we are a new child */
566 if (gv && (io = GvIO(gv))
567 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
570 XPUSHs(SvTIED_obj((SV*)io, mg));
573 call_method("CLOSE", G_SCALAR);
579 PUSHs(boolSV(do_close(gv, TRUE)));
599 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
600 DIE(aTHX_ PL_no_usym, "filehandle");
605 do_close(rgv, FALSE);
607 do_close(wgv, FALSE);
609 if (PerlProc_pipe(fd) < 0)
612 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
613 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
614 IoOFP(rstio) = IoIFP(rstio);
615 IoIFP(wstio) = IoOFP(wstio);
616 IoTYPE(rstio) = IoTYPE_RDONLY;
617 IoTYPE(wstio) = IoTYPE_WRONLY;
619 if (!IoIFP(rstio) || !IoOFP(wstio)) {
620 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
621 else PerlLIO_close(fd[0]);
622 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
623 else PerlLIO_close(fd[1]);
626 #if defined(HAS_FCNTL) && defined(F_SETFD)
627 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
628 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
635 DIE(aTHX_ PL_no_func, "pipe");
651 if (gv && (io = GvIO(gv))
652 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
655 XPUSHs(SvTIED_obj((SV*)io, mg));
658 call_method("FILENO", G_SCALAR);
664 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
665 /* Can't do this because people seem to do things like
666 defined(fileno($foo)) to check whether $foo is a valid fh.
667 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
668 report_evil_fh(gv, io, PL_op->op_type);
673 PUSHi(PerlIO_fileno(fp));
684 anum = PerlLIO_umask(0);
685 (void)PerlLIO_umask(anum);
688 anum = PerlLIO_umask(POPi);
689 TAINT_PROPER("umask");
692 /* Only DIE if trying to restrict permissions on `user' (self).
693 * Otherwise it's harmless and more useful to just return undef
694 * since 'group' and 'other' concepts probably don't exist here. */
695 if (MAXARG >= 1 && (POPi & 0700))
696 DIE(aTHX_ "umask not implemented");
697 XPUSHs(&PL_sv_undef);
719 if (gv && (io = GvIO(gv))
720 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
723 XPUSHs(SvTIED_obj((SV*)io, mg));
728 call_method("BINMODE", G_SCALAR);
735 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
736 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
737 report_evil_fh(gv, io, PL_op->op_type);
738 SETERRNO(EBADF,RMS_IFI);
743 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
744 (discp) ? SvPV_nolen(discp) : Nullch)) {
745 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
746 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
747 mode_from_discipline(discp),
748 (discp) ? SvPV_nolen(discp) : Nullch)) {
770 I32 markoff = MARK - PL_stack_base;
772 int how = PERL_MAGIC_tied;
776 switch(SvTYPE(varsv)) {
778 methname = "TIEHASH";
779 HvEITER((HV *)varsv) = Null(HE *);
782 methname = "TIEARRAY";
785 #ifdef GV_UNIQUE_CHECK
786 if (GvUNIQUE((GV*)varsv)) {
787 Perl_croak(aTHX_ "Attempt to tie unique GV");
790 methname = "TIEHANDLE";
791 how = PERL_MAGIC_tiedscalar;
792 /* For tied filehandles, we apply tiedscalar magic to the IO
793 slot of the GP rather than the GV itself. AMS 20010812 */
795 GvIOp(varsv) = newIO();
796 varsv = (SV *)GvIOp(varsv);
799 methname = "TIESCALAR";
800 how = PERL_MAGIC_tiedscalar;
804 if (sv_isobject(*MARK)) {
806 PUSHSTACKi(PERLSI_MAGIC);
808 EXTEND(SP,(I32)items);
812 call_method(methname, G_SCALAR);
815 /* Not clear why we don't call call_method here too.
816 * perhaps to get different error message ?
818 stash = gv_stashsv(*MARK, FALSE);
819 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
820 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
824 PUSHSTACKi(PERLSI_MAGIC);
826 EXTEND(SP,(I32)items);
830 call_sv((SV*)GvCV(gv), G_SCALAR);
836 if (sv_isobject(sv)) {
837 sv_unmagic(varsv, how);
838 /* Croak if a self-tie on an aggregate is attempted. */
839 if (varsv == SvRV(sv) &&
840 (SvTYPE(varsv) == SVt_PVAV ||
841 SvTYPE(varsv) == SVt_PVHV))
843 "Self-ties of arrays and hashes are not supported");
844 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
847 SP = PL_stack_base + markoff;
857 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
858 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
860 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
863 if ((mg = SvTIED_mg(sv, how))) {
864 SV *obj = SvRV(SvTIED_obj(sv, mg));
868 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
869 isGV(gv) && (cv = GvCV(gv))) {
871 XPUSHs(SvTIED_obj((SV*)gv, mg));
872 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
875 call_sv((SV *)cv, G_VOID);
879 else if (ckWARN(WARN_UNTIE)) {
880 if (mg && SvREFCNT(obj) > 1)
881 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
882 "untie attempted while %"UVuf" inner references still exist",
883 (UV)SvREFCNT(obj) - 1 ) ;
887 sv_unmagic(sv, how) ;
896 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
897 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
899 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
902 if ((mg = SvTIED_mg(sv, how))) {
903 SV *osv = SvTIED_obj(sv, mg);
904 if (osv == mg->mg_obj)
905 osv = sv_mortalcopy(osv);
923 sv = sv_mortalcopy(&PL_sv_no);
924 sv_setpv(sv, "AnyDBM_File");
925 stash = gv_stashsv(sv, FALSE);
926 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
928 require_pv("AnyDBM_File.pm");
930 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
931 DIE(aTHX_ "No dbm on this machine");
941 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
943 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
946 call_sv((SV*)GvCV(gv), G_SCALAR);
949 if (!sv_isobject(TOPs)) {
954 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
957 call_sv((SV*)GvCV(gv), G_SCALAR);
961 if (sv_isobject(TOPs)) {
962 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
963 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
985 struct timeval timebuf;
986 struct timeval *tbuf = &timebuf;
990 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
995 # if BYTEORDER & 0xf0000
996 # define ORDERBYTE (0x88888888 - BYTEORDER)
998 # define ORDERBYTE (0x4444 - BYTEORDER)
1004 for (i = 1; i <= 3; i++) {
1012 /* little endians can use vecs directly */
1013 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1020 masksize = NFDBITS / NBBY;
1022 masksize = sizeof(long); /* documented int, everyone seems to use long */
1024 Zero(&fd_sets[0], 4, char*);
1027 # if SELECT_MIN_BITS == 1
1028 growsize = sizeof(fd_set);
1030 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1031 # undef SELECT_MIN_BITS
1032 # define SELECT_MIN_BITS __FD_SETSIZE
1034 /* If SELECT_MIN_BITS is greater than one we most probably will want
1035 * to align the sizes with SELECT_MIN_BITS/8 because for example
1036 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1037 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1038 * on (sets/tests/clears bits) is 32 bits. */
1039 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1047 timebuf.tv_sec = (long)value;
1048 value -= (NV)timebuf.tv_sec;
1049 timebuf.tv_usec = (long)(value * 1000000.0);
1052 tbuf = Null(struct timeval*);
1054 for (i = 1; i <= 3; i++) {
1060 else if (!SvPOK(sv))
1061 SvPV_force(sv,n_a); /* force string conversion */
1064 Sv_Grow(sv, growsize);
1068 while (++j <= growsize) {
1072 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1074 New(403, fd_sets[i], growsize, char);
1075 for (offset = 0; offset < growsize; offset += masksize) {
1076 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1077 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1080 fd_sets[i] = SvPVX(sv);
1084 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1085 /* Can't make just the (void*) conditional because that would be
1086 * cpp #if within cpp macro, and not all compilers like that. */
1087 nfound = PerlSock_select(
1089 (Select_fd_set_t) fd_sets[1],
1090 (Select_fd_set_t) fd_sets[2],
1091 (Select_fd_set_t) fd_sets[3],
1092 (void*) tbuf); /* Workaround for compiler bug. */
1094 nfound = PerlSock_select(
1096 (Select_fd_set_t) fd_sets[1],
1097 (Select_fd_set_t) fd_sets[2],
1098 (Select_fd_set_t) fd_sets[3],
1101 for (i = 1; i <= 3; i++) {
1104 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1106 for (offset = 0; offset < growsize; offset += masksize) {
1107 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1108 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1110 Safefree(fd_sets[i]);
1117 if (GIMME == G_ARRAY && tbuf) {
1118 value = (NV)(timebuf.tv_sec) +
1119 (NV)(timebuf.tv_usec) / 1000000.0;
1120 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1121 sv_setnv(sv, value);
1125 DIE(aTHX_ "select not implemented");
1130 Perl_setdefout(pTHX_ GV *gv)
1133 (void)SvREFCNT_inc(gv);
1135 SvREFCNT_dec(PL_defoutgv);
1142 GV *newdefout, *egv;
1145 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1147 egv = GvEGV(PL_defoutgv);
1152 XPUSHs(&PL_sv_undef);
1154 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1155 if (gvp && *gvp == egv) {
1156 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1160 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1165 if (!GvIO(newdefout))
1166 gv_IOadd(newdefout);
1167 setdefout(newdefout);
1185 if (gv && (io = GvIO(gv))
1186 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1188 I32 gimme = GIMME_V;
1190 XPUSHs(SvTIED_obj((SV*)io, mg));
1193 call_method("GETC", gimme);
1196 if (gimme == G_SCALAR)
1197 SvSetMagicSV_nosteal(TARG, TOPs);
1200 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1201 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1202 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1203 report_evil_fh(gv, io, PL_op->op_type);
1204 SETERRNO(EBADF,RMS_IFI);
1208 sv_setpv(TARG, " ");
1209 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1210 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1211 /* Find out how many bytes the char needs */
1212 Size_t len = UTF8SKIP(SvPVX(TARG));
1215 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1216 SvCUR_set(TARG,1+len);
1226 return pp_sysread();
1230 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1232 register PERL_CONTEXT *cx;
1233 I32 gimme = GIMME_V;
1239 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1241 PAD_SET_CUR(CvPADLIST(cv), 1);
1243 setdefout(gv); /* locally select filehandle so $% et al work */
1276 SV *tmpsv = sv_newmortal();
1277 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1278 name = SvPV_nolen(tmpsv);
1281 DIE(aTHX_ "Undefined format \"%s\" called", name);
1282 DIE(aTHX_ "Not a format reference");
1285 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1287 IoFLAGS(io) &= ~IOf_DIDTOP;
1288 return doform(cv,gv,PL_op->op_next);
1294 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1295 register IO *io = GvIOp(gv);
1296 PerlIO *ofp = IoOFP(io);
1300 register PERL_CONTEXT *cx;
1302 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1303 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1306 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1307 PL_formtarget != PL_toptarget)
1311 if (!IoTOP_GV(io)) {
1315 if (!IoTOP_NAME(io)) {
1316 if (!IoFMT_NAME(io))
1317 IoFMT_NAME(io) = savepv(GvNAME(gv));
1318 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1319 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1320 if ((topgv && GvFORM(topgv)) ||
1321 !gv_fetchpv("top",FALSE,SVt_PVFM))
1322 IoTOP_NAME(io) = savepv(SvPVX(topname));
1324 IoTOP_NAME(io) = savepv("top");
1326 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1327 if (!topgv || !GvFORM(topgv)) {
1328 IoLINES_LEFT(io) = 100000000;
1331 IoTOP_GV(io) = topgv;
1333 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1334 I32 lines = IoLINES_LEFT(io);
1335 char *s = SvPVX(PL_formtarget);
1336 if (lines <= 0) /* Yow, header didn't even fit!!! */
1338 while (lines-- > 0) {
1339 s = strchr(s, '\n');
1345 STRLEN save = SvCUR(PL_formtarget);
1346 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1347 do_print(PL_formtarget, ofp);
1348 SvCUR_set(PL_formtarget, save);
1349 sv_chop(PL_formtarget, s);
1350 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1353 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1354 do_print(PL_formfeed, ofp);
1355 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1357 PL_formtarget = PL_toptarget;
1358 IoFLAGS(io) |= IOf_DIDTOP;
1361 DIE(aTHX_ "bad top format reference");
1366 SV *sv = sv_newmortal();
1367 gv_efullname4(sv, fgv, Nullch, FALSE);
1368 name = SvPV_nolen(sv);
1371 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1374 DIE(aTHX_ "Undefined top format called");
1378 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1379 return doform(cv,gv,PL_op);
1383 POPBLOCK(cx,PL_curpm);
1389 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1391 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1392 else if (ckWARN(WARN_CLOSED))
1393 report_evil_fh(gv, io, PL_op->op_type);
1398 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1399 if (ckWARN(WARN_IO))
1400 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1402 if (!do_print(PL_formtarget, fp))
1405 FmLINES(PL_formtarget) = 0;
1406 SvCUR_set(PL_formtarget, 0);
1407 *SvEND(PL_formtarget) = '\0';
1408 if (IoFLAGS(io) & IOf_FLUSH)
1409 (void)PerlIO_flush(fp);
1414 PL_formtarget = PL_bodytarget;
1416 return pop_return();
1421 dSP; dMARK; dORIGMARK;
1428 if (PL_op->op_flags & OPf_STACKED)
1433 if (gv && (io = GvIO(gv))
1434 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1436 if (MARK == ORIGMARK) {
1439 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1443 *MARK = SvTIED_obj((SV*)io, mg);
1446 call_method("PRINTF", G_SCALAR);
1449 MARK = ORIGMARK + 1;
1456 if (!(io = GvIO(gv))) {
1457 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1458 report_evil_fh(gv, io, PL_op->op_type);
1459 SETERRNO(EBADF,RMS_IFI);
1462 else if (!(fp = IoOFP(io))) {
1463 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1465 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1466 else if (ckWARN(WARN_CLOSED))
1467 report_evil_fh(gv, io, PL_op->op_type);
1469 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1473 do_sprintf(sv, SP - MARK, MARK + 1);
1474 if (!do_print(sv, fp))
1477 if (IoFLAGS(io) & IOf_FLUSH)
1478 if (PerlIO_flush(fp) == EOF)
1489 PUSHs(&PL_sv_undef);
1510 /* Need TIEHANDLE method ? */
1512 tmps = SvPV(sv, len);
1513 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1514 IoLINES(GvIOp(gv)) = 0;
1518 PUSHs(&PL_sv_undef);
1525 dSP; dMARK; dORIGMARK; dTARGET;
1532 Sock_size_t bufsize;
1539 bool charstart = FALSE;
1540 STRLEN charskip = 0;
1544 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1545 && gv && (io = GvIO(gv))
1546 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1551 *MARK = SvTIED_obj((SV*)io, mg);
1553 call_method("READ", G_SCALAR);
1566 sv_setpvn(bufsv, "", 0);
1567 length = SvIVx(*++MARK);
1570 offset = SvIVx(*++MARK);
1574 if (!io || !IoIFP(io)) {
1575 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1576 report_evil_fh(gv, io, PL_op->op_type);
1577 SETERRNO(EBADF,RMS_IFI);
1580 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1581 buffer = SvPVutf8_force(bufsv, blen);
1582 /* UTF8 may not have been set if they are all low bytes */
1586 buffer = SvPV_force(bufsv, blen);
1589 DIE(aTHX_ "Negative length");
1597 if (PL_op->op_type == OP_RECV) {
1598 char namebuf[MAXPATHLEN];
1599 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1600 bufsize = sizeof (struct sockaddr_in);
1602 bufsize = sizeof namebuf;
1604 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1608 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1609 /* 'offset' means 'flags' here */
1610 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1611 (struct sockaddr *)namebuf, &bufsize);
1615 /* Bogus return without padding */
1616 bufsize = sizeof (struct sockaddr_in);
1618 SvCUR_set(bufsv, count);
1619 *SvEND(bufsv) = '\0';
1620 (void)SvPOK_only(bufsv);
1624 /* This should not be marked tainted if the fp is marked clean */
1625 if (!(IoFLAGS(io) & IOf_UNTAINT))
1626 SvTAINTED_on(bufsv);
1628 sv_setpvn(TARG, namebuf, bufsize);
1633 if (PL_op->op_type == OP_RECV)
1634 DIE(aTHX_ PL_no_sock_func, "recv");
1636 if (DO_UTF8(bufsv)) {
1637 /* offset adjust in characters not bytes */
1638 blen = sv_len_utf8(bufsv);
1641 if (-offset > (int)blen)
1642 DIE(aTHX_ "Offset outside string");
1645 if (DO_UTF8(bufsv)) {
1646 /* convert offset-as-chars to offset-as-bytes */
1647 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1650 bufsize = SvCUR(bufsv);
1651 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1652 if (offset > bufsize) { /* Zero any newly allocated space */
1653 Zero(buffer+bufsize, offset-bufsize, char);
1655 buffer = buffer + offset;
1657 if (PL_op->op_type == OP_SYSREAD) {
1658 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1659 if (IoTYPE(io) == IoTYPE_SOCKET) {
1660 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1666 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1671 #ifdef HAS_SOCKET__bad_code_maybe
1672 if (IoTYPE(io) == IoTYPE_SOCKET) {
1673 char namebuf[MAXPATHLEN];
1674 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1675 bufsize = sizeof (struct sockaddr_in);
1677 bufsize = sizeof namebuf;
1679 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1680 (struct sockaddr *)namebuf, &bufsize);
1685 count = PerlIO_read(IoIFP(io), buffer, length);
1686 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1687 if (count == 0 && PerlIO_error(IoIFP(io)))
1691 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1692 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1695 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1696 *SvEND(bufsv) = '\0';
1697 (void)SvPOK_only(bufsv);
1698 if (fp_utf8 && !IN_BYTES) {
1699 /* Look at utf8 we got back and count the characters */
1700 char *bend = buffer + count;
1701 while (buffer < bend) {
1703 skip = UTF8SKIP(buffer);
1706 if (buffer - charskip + skip > bend) {
1707 /* partial character - try for rest of it */
1708 length = skip - (bend-buffer);
1709 offset = bend - SvPVX(bufsv);
1721 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1722 provided amount read (count) was what was requested (length)
1724 if (got < wanted && count == length) {
1725 length = wanted - got;
1726 offset = bend - SvPVX(bufsv);
1729 /* return value is character count */
1734 /* This should not be marked tainted if the fp is marked clean */
1735 if (!(IoFLAGS(io) & IOf_UNTAINT))
1736 SvTAINTED_on(bufsv);
1749 int items = (SP - PL_stack_base) - TOPMARK;
1753 sv = sv_2mortal(newSViv(sv_len(*SP)));
1762 dSP; dMARK; dORIGMARK; dTARGET;
1773 if (PL_op->op_type == OP_SYSWRITE
1774 && gv && (io = GvIO(gv))
1775 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1780 *MARK = SvTIED_obj((SV*)io, mg);
1782 call_method("WRITE", G_SCALAR);
1793 #if Size_t_size > IVSIZE
1794 length = (Size_t)SvNVx(*++MARK);
1796 length = (Size_t)SvIVx(*++MARK);
1798 if ((SSize_t)length < 0)
1799 DIE(aTHX_ "Negative length");
1802 if (!io || !IoIFP(io)) {
1804 if (ckWARN(WARN_CLOSED))
1805 report_evil_fh(gv, io, PL_op->op_type);
1806 SETERRNO(EBADF,RMS_IFI);
1810 if (PerlIO_isutf8(IoIFP(io))) {
1811 buffer = SvPVutf8(bufsv, blen);
1814 if (DO_UTF8(bufsv)) {
1815 /* Not modifying source SV, so making a temporary copy. */
1816 bufsv = sv_2mortal(newSVsv(bufsv));
1817 sv_utf8_downgrade(bufsv, FALSE);
1819 buffer = SvPV(bufsv, blen);
1822 if (PL_op->op_type == OP_SYSWRITE) {
1824 if (DO_UTF8(bufsv)) {
1825 /* length and offset are in chars */
1826 blen = sv_len_utf8(bufsv);
1829 offset = SvIVx(*++MARK);
1831 if (-offset > (IV)blen)
1832 DIE(aTHX_ "Offset outside string");
1834 } else if (offset >= (IV)blen && blen > 0)
1835 DIE(aTHX_ "Offset outside string");
1838 if (length > blen - offset)
1839 length = blen - offset;
1840 if (DO_UTF8(bufsv)) {
1841 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1842 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1845 buffer = buffer+offset;
1847 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1848 if (IoTYPE(io) == IoTYPE_SOCKET) {
1849 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1855 /* See the note at doio.c:do_print about filesize limits. --jhi */
1856 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1861 else if (SP > MARK) {
1864 sockbuf = SvPVx(*++MARK, mlen);
1865 /* length is really flags */
1866 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1867 length, (struct sockaddr *)sockbuf, mlen);
1870 /* length is really flags */
1871 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1874 DIE(aTHX_ PL_no_sock_func, "send");
1880 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1881 #if Size_t_size > IVSIZE
1895 return pp_sysread();
1906 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1908 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1910 if (io && !IoIFP(io)) {
1911 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1913 IoFLAGS(io) &= ~IOf_START;
1914 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1915 sv_setpvn(GvSV(gv), "-", 1);
1916 SvSETMAGIC(GvSV(gv));
1918 else if (!nextargv(gv))
1923 gv = PL_last_in_gv; /* eof */
1926 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1928 if (gv && (io = GvIO(gv))
1929 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1932 XPUSHs(SvTIED_obj((SV*)io, mg));
1935 call_method("EOF", G_SCALAR);
1941 PUSHs(boolSV(!gv || do_eof(gv)));
1955 gv = PL_last_in_gv = (GV*)POPs;
1957 if (gv && (io = GvIO(gv))
1958 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1961 XPUSHs(SvTIED_obj((SV*)io, mg));
1964 call_method("TELL", G_SCALAR);
1970 #if LSEEKSIZE > IVSIZE
1971 PUSHn( do_tell(gv) );
1973 PUSHi( do_tell(gv) );
1980 return pp_sysseek();
1989 #if LSEEKSIZE > IVSIZE
1990 Off_t offset = (Off_t)SvNVx(POPs);
1992 Off_t offset = (Off_t)SvIVx(POPs);
1996 gv = PL_last_in_gv = (GV*)POPs;
1998 if (gv && (io = GvIO(gv))
1999 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2002 XPUSHs(SvTIED_obj((SV*)io, mg));
2003 #if LSEEKSIZE > IVSIZE
2004 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2006 XPUSHs(sv_2mortal(newSViv(offset)));
2008 XPUSHs(sv_2mortal(newSViv(whence)));
2011 call_method("SEEK", G_SCALAR);
2017 if (PL_op->op_type == OP_SEEK)
2018 PUSHs(boolSV(do_seek(gv, offset, whence)));
2020 Off_t sought = do_sysseek(gv, offset, whence);
2022 PUSHs(&PL_sv_undef);
2025 #if LSEEKSIZE > IVSIZE
2030 : newSVpvn(zero_but_true, ZBTLEN);
2031 PUSHs(sv_2mortal(sv));
2040 /* There seems to be no consensus on the length type of truncate()
2041 * and ftruncate(), both off_t and size_t have supporters. In
2042 * general one would think that when using large files, off_t is
2043 * at least as wide as size_t, so using an off_t should be okay. */
2044 /* XXX Configure probe for the length type of *truncate() needed XXX */
2047 #if Off_t_size > IVSIZE
2052 /* Checking for length < 0 is problematic as the type might or
2053 * might not be signed: if it is not, clever compilers will moan. */
2054 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2056 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2063 if (PL_op->op_flags & OPf_SPECIAL) {
2064 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2073 TAINT_PROPER("truncate");
2074 if (!(fp = IoIFP(io))) {
2080 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2082 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2092 if (SvTYPE(sv) == SVt_PVGV) {
2093 tmpgv = (GV*)sv; /* *main::FRED for example */
2094 goto do_ftruncate_gv;
2096 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2097 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2098 goto do_ftruncate_gv;
2100 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2101 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2102 goto do_ftruncate_io;
2105 name = SvPV(sv, n_a);
2106 TAINT_PROPER("truncate");
2108 if (truncate(name, len) < 0)
2114 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2117 if (my_chsize(tmpfd, len) < 0)
2119 PerlLIO_close(tmpfd);
2128 SETERRNO(EBADF,RMS_IFI);
2132 DIE(aTHX_ "truncate not implemented");
2145 unsigned int func = POPu;
2146 int optype = PL_op->op_type;
2150 IO *io = gv ? GvIOn(gv) : 0;
2152 if (!io || !argsv || !IoIFP(io)) {
2153 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2154 report_evil_fh(gv, io, PL_op->op_type);
2155 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2159 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2162 s = SvPV_force(argsv, len);
2163 need = IOCPARM_LEN(func);
2165 s = Sv_Grow(argsv, need + 1);
2166 SvCUR_set(argsv, need);
2169 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2172 retval = SvIV(argsv);
2173 s = INT2PTR(char*,retval); /* ouch */
2176 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2178 if (optype == OP_IOCTL)
2180 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2182 DIE(aTHX_ "ioctl is not implemented");
2186 DIE(aTHX_ "fcntl is not implemented");
2188 #if defined(OS2) && defined(__EMX__)
2189 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2191 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2195 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2197 if (s[SvCUR(argsv)] != 17)
2198 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2200 s[SvCUR(argsv)] = 0; /* put our null back */
2201 SvSETMAGIC(argsv); /* Assume it has changed */
2210 PUSHp(zero_but_true, ZBTLEN);
2231 if (gv && (io = GvIO(gv)))
2238 (void)PerlIO_flush(fp);
2239 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2242 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2243 report_evil_fh(gv, io, PL_op->op_type);
2245 SETERRNO(EBADF,RMS_IFI);
2250 DIE(aTHX_ PL_no_func, "flock()");
2262 int protocol = POPi;
2268 io = gv ? GvIOn(gv) : NULL;
2271 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2272 report_evil_fh(gv, io, PL_op->op_type);
2274 do_close(gv, FALSE);
2275 SETERRNO(EBADF,LIB_INVARG);
2280 do_close(gv, FALSE);
2282 TAINT_PROPER("socket");
2283 fd = PerlSock_socket(domain, type, protocol);
2286 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2287 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2288 IoTYPE(io) = IoTYPE_SOCKET;
2289 if (!IoIFP(io) || !IoOFP(io)) {
2290 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2291 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2292 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2295 #if defined(HAS_FCNTL) && defined(F_SETFD)
2296 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2300 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2305 DIE(aTHX_ PL_no_sock_func, "socket");
2311 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2317 int protocol = POPi;
2324 io1 = gv1 ? GvIOn(gv1) : NULL;
2325 io2 = gv2 ? GvIOn(gv2) : NULL;
2326 if (!gv1 || !gv2 || !io1 || !io2) {
2327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2329 report_evil_fh(gv1, io1, PL_op->op_type);
2331 report_evil_fh(gv1, io2, PL_op->op_type);
2334 do_close(gv1, FALSE);
2336 do_close(gv2, FALSE);
2341 do_close(gv1, FALSE);
2343 do_close(gv2, FALSE);
2345 TAINT_PROPER("socketpair");
2346 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2348 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2349 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2350 IoTYPE(io1) = IoTYPE_SOCKET;
2351 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2352 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2353 IoTYPE(io2) = IoTYPE_SOCKET;
2354 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2355 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2356 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2357 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2358 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2359 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2360 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2363 #if defined(HAS_FCNTL) && defined(F_SETFD)
2364 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2365 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2370 DIE(aTHX_ PL_no_sock_func, "socketpair");
2378 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2379 extern void GETPRIVMODE();
2380 extern void GETUSERMODE();
2385 register IO *io = GvIOn(gv);
2389 int mpeprivmode = 0;
2392 if (!io || !IoIFP(io))
2395 addr = SvPV(addrsv, len);
2396 TAINT_PROPER("bind");
2397 #ifdef MPE /* Deal with MPE bind() peculiarities */
2398 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2399 /* The address *MUST* stupidly be zero. */
2400 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2401 /* PRIV mode is required to bind() to ports < 1024. */
2402 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2403 ((struct sockaddr_in *)addr)->sin_port > 0) {
2404 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2409 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2410 (struct sockaddr *)addr, len) >= 0)
2413 #ifdef MPE /* Switch back to USER mode */
2424 if (ckWARN(WARN_CLOSED))
2425 report_evil_fh(gv, io, PL_op->op_type);
2426 SETERRNO(EBADF,SS_IVCHAN);
2429 DIE(aTHX_ PL_no_sock_func, "bind");
2440 register IO *io = GvIOn(gv);
2443 if (!io || !IoIFP(io))
2446 addr = SvPV(addrsv, len);
2447 TAINT_PROPER("connect");
2448 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2454 if (ckWARN(WARN_CLOSED))
2455 report_evil_fh(gv, io, PL_op->op_type);
2456 SETERRNO(EBADF,SS_IVCHAN);
2459 DIE(aTHX_ PL_no_sock_func, "connect");
2469 register IO *io = gv ? GvIOn(gv) : NULL;
2471 if (!gv || !io || !IoIFP(io))
2474 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2480 if (ckWARN(WARN_CLOSED))
2481 report_evil_fh(gv, io, PL_op->op_type);
2482 SETERRNO(EBADF,SS_IVCHAN);
2485 DIE(aTHX_ PL_no_sock_func, "listen");
2497 char namebuf[MAXPATHLEN];
2498 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2499 Sock_size_t len = sizeof (struct sockaddr_in);
2501 Sock_size_t len = sizeof namebuf;
2514 if (!gstio || !IoIFP(gstio))
2518 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2522 do_close(ngv, FALSE);
2523 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2524 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2525 IoTYPE(nstio) = IoTYPE_SOCKET;
2526 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2527 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2528 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2529 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2532 #if defined(HAS_FCNTL) && defined(F_SETFD)
2533 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2537 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2538 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2540 #ifdef __SCO_VERSION__
2541 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2544 PUSHp(namebuf, len);
2548 if (ckWARN(WARN_CLOSED))
2549 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2550 SETERRNO(EBADF,SS_IVCHAN);
2556 DIE(aTHX_ PL_no_sock_func, "accept");
2566 register IO *io = GvIOn(gv);
2568 if (!io || !IoIFP(io))
2571 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2575 if (ckWARN(WARN_CLOSED))
2576 report_evil_fh(gv, io, PL_op->op_type);
2577 SETERRNO(EBADF,SS_IVCHAN);
2580 DIE(aTHX_ PL_no_sock_func, "shutdown");
2587 return pp_ssockopt();
2589 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2597 int optype = PL_op->op_type;
2600 unsigned int optname;
2606 if (optype == OP_GSOCKOPT)
2607 sv = sv_2mortal(NEWSV(22, 257));
2610 optname = (unsigned int) POPi;
2611 lvl = (unsigned int) POPi;
2615 if (!io || !IoIFP(io))
2618 fd = PerlIO_fileno(IoIFP(io));
2622 (void)SvPOK_only(sv);
2626 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2641 aint = (int)SvIV(sv);
2645 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2654 if (ckWARN(WARN_CLOSED))
2655 report_evil_fh(gv, io, optype);
2656 SETERRNO(EBADF,SS_IVCHAN);
2661 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2668 return pp_getpeername();
2670 DIE(aTHX_ PL_no_sock_func, "getsockname");
2678 int optype = PL_op->op_type;
2682 register IO *io = GvIOn(gv);
2685 if (!io || !IoIFP(io))
2688 sv = sv_2mortal(NEWSV(22, 257));
2689 (void)SvPOK_only(sv);
2693 fd = PerlIO_fileno(IoIFP(io));
2695 case OP_GETSOCKNAME:
2696 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2699 case OP_GETPEERNAME:
2700 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2702 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2704 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";
2705 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2706 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2707 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2708 sizeof(u_short) + sizeof(struct in_addr))) {
2715 #ifdef BOGUS_GETNAME_RETURN
2716 /* Interactive Unix, getpeername() and getsockname()
2717 does not return valid namelen */
2718 if (len == BOGUS_GETNAME_RETURN)
2719 len = sizeof(struct sockaddr);
2727 if (ckWARN(WARN_CLOSED))
2728 report_evil_fh(gv, io, optype);
2729 SETERRNO(EBADF,SS_IVCHAN);
2734 DIE(aTHX_ PL_no_sock_func, "getpeername");
2753 if (PL_op->op_flags & OPf_REF) {
2755 if (PL_op->op_type == OP_LSTAT) {
2756 if (gv != PL_defgv) {
2757 if (ckWARN(WARN_IO))
2758 Perl_warner(aTHX_ packWARN(WARN_IO),
2759 "lstat() on filehandle %s", GvENAME(gv));
2760 } else if (PL_laststype != OP_LSTAT)
2761 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2765 if (gv != PL_defgv) {
2766 PL_laststype = OP_STAT;
2768 sv_setpv(PL_statname, "");
2769 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2770 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2772 if (PL_laststatval < 0) {
2773 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2774 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2780 if (SvTYPE(sv) == SVt_PVGV) {
2784 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2786 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2787 Perl_warner(aTHX_ packWARN(WARN_IO),
2788 "lstat() on filehandle %s", GvENAME(gv));
2791 sv_setpv(PL_statname, SvPV(sv,n_a));
2794 PL_laststype = PL_op->op_type;
2795 if (PL_op->op_type == OP_LSTAT)
2796 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2799 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2800 if (PL_laststatval < 0) {
2801 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2802 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2808 if (gimme != G_ARRAY) {
2809 if (gimme != G_VOID)
2810 XPUSHs(boolSV(max));
2816 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2817 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2818 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2819 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2820 #if Uid_t_size > IVSIZE
2821 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2823 # if Uid_t_sign <= 0
2824 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2826 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2829 #if Gid_t_size > IVSIZE
2830 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2832 # if Gid_t_sign <= 0
2833 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2835 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2838 #ifdef USE_STAT_RDEV
2839 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2841 PUSHs(sv_2mortal(newSVpvn("", 0)));
2843 #if Off_t_size > IVSIZE
2844 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2846 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2849 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2850 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2851 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2853 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2854 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2855 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2857 #ifdef USE_STAT_BLOCKS
2858 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2859 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2861 PUSHs(sv_2mortal(newSVpvn("", 0)));
2862 PUSHs(sv_2mortal(newSVpvn("", 0)));
2872 #if defined(HAS_ACCESS) && defined(R_OK)
2874 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2875 result = access(POPpx, R_OK);
2890 if (cando(S_IRUSR, 0, &PL_statcache))
2899 #if defined(HAS_ACCESS) && defined(W_OK)
2901 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2902 result = access(POPpx, W_OK);
2917 if (cando(S_IWUSR, 0, &PL_statcache))
2926 #if defined(HAS_ACCESS) && defined(X_OK)
2928 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2929 result = access(POPpx, X_OK);
2944 if (cando(S_IXUSR, 0, &PL_statcache))
2953 #ifdef PERL_EFF_ACCESS_R_OK
2955 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2956 result = PERL_EFF_ACCESS_R_OK(POPpx);
2971 if (cando(S_IRUSR, 1, &PL_statcache))
2980 #ifdef PERL_EFF_ACCESS_W_OK
2982 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2983 result = PERL_EFF_ACCESS_W_OK(POPpx);
2998 if (cando(S_IWUSR, 1, &PL_statcache))
3007 #ifdef PERL_EFF_ACCESS_X_OK
3009 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3010 result = PERL_EFF_ACCESS_X_OK(POPpx);
3025 if (cando(S_IXUSR, 1, &PL_statcache))
3032 I32 result = my_stat();
3041 return pp_ftrowned();
3046 I32 result = my_stat();
3050 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3058 I32 result = my_stat();
3062 if (PL_statcache.st_size == 0)
3069 I32 result = my_stat();
3073 #if Off_t_size > IVSIZE
3074 PUSHn(PL_statcache.st_size);
3076 PUSHi(PL_statcache.st_size);
3083 I32 result = my_stat();
3087 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3093 I32 result = my_stat();
3097 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3103 I32 result = my_stat();
3107 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3113 I32 result = my_stat();
3117 if (S_ISSOCK(PL_statcache.st_mode))
3124 I32 result = my_stat();
3128 if (S_ISCHR(PL_statcache.st_mode))
3135 I32 result = my_stat();
3139 if (S_ISBLK(PL_statcache.st_mode))
3146 I32 result = my_stat();
3150 if (S_ISREG(PL_statcache.st_mode))
3157 I32 result = my_stat();
3161 if (S_ISDIR(PL_statcache.st_mode))
3168 I32 result = my_stat();
3172 if (S_ISFIFO(PL_statcache.st_mode))
3179 I32 result = my_lstat();
3183 if (S_ISLNK(PL_statcache.st_mode))
3192 I32 result = my_stat();
3196 if (PL_statcache.st_mode & S_ISUID)
3206 I32 result = my_stat();
3210 if (PL_statcache.st_mode & S_ISGID)
3220 I32 result = my_stat();
3224 if (PL_statcache.st_mode & S_ISVTX)
3235 char *tmps = Nullch;
3238 if (PL_op->op_flags & OPf_REF)
3240 else if (isGV(TOPs))
3242 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3243 gv = (GV*)SvRV(POPs);
3245 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3247 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3248 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3249 else if (tmps && isDIGIT(*tmps))
3253 if (PerlLIO_isatty(fd))
3258 #if defined(atarist) /* this will work with atariST. Configure will
3259 make guesses for other systems. */
3260 # define FILE_base(f) ((f)->_base)
3261 # define FILE_ptr(f) ((f)->_ptr)
3262 # define FILE_cnt(f) ((f)->_cnt)
3263 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3273 register STDCHAR *s;
3280 if (PL_op->op_flags & OPf_REF)
3282 else if (isGV(TOPs))
3284 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3285 gv = (GV*)SvRV(POPs);
3291 if (gv == PL_defgv) {
3293 io = GvIO(PL_statgv);
3296 goto really_filename;
3301 PL_laststatval = -1;
3302 sv_setpv(PL_statname, "");
3303 io = GvIO(PL_statgv);
3305 if (io && IoIFP(io)) {
3306 if (! PerlIO_has_base(IoIFP(io)))
3307 DIE(aTHX_ "-T and -B not implemented on filehandles");
3308 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3309 if (PL_laststatval < 0)
3311 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3312 if (PL_op->op_type == OP_FTTEXT)
3317 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3318 i = PerlIO_getc(IoIFP(io));
3320 (void)PerlIO_ungetc(IoIFP(io),i);
3322 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3324 len = PerlIO_get_bufsiz(IoIFP(io));
3325 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3326 /* sfio can have large buffers - limit to 512 */
3331 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3333 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3335 SETERRNO(EBADF,RMS_IFI);
3343 PL_laststatval = -1;
3344 PL_laststype = OP_STAT;
3345 sv_setpv(PL_statname, SvPV(sv, n_a));
3346 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3347 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3348 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3351 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3352 if (PL_laststatval < 0) {
3353 (void)PerlIO_close(fp);
3356 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3357 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3358 (void)PerlIO_close(fp);
3360 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3361 RETPUSHNO; /* special case NFS directories */
3362 RETPUSHYES; /* null file is anything */
3367 /* now scan s to look for textiness */
3368 /* XXX ASCII dependent code */
3370 #if defined(DOSISH) || defined(USEMYBINMODE)
3371 /* ignore trailing ^Z on short files */
3372 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3376 for (i = 0; i < len; i++, s++) {
3377 if (!*s) { /* null never allowed in text */
3382 else if (!(isPRINT(*s) || isSPACE(*s)))
3385 else if (*s & 128) {
3387 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3390 /* utf8 characters don't count as odd */
3391 if (UTF8_IS_START(*s)) {
3392 int ulen = UTF8SKIP(s);
3393 if (ulen < len - i) {
3395 for (j = 1; j < ulen; j++) {
3396 if (!UTF8_IS_CONTINUATION(s[j]))
3399 --ulen; /* loop does extra increment */
3409 *s != '\n' && *s != '\r' && *s != '\b' &&
3410 *s != '\t' && *s != '\f' && *s != 27)
3415 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3440 if( !tmps || !*tmps ) {
3441 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3442 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3444 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3449 deprecate("chdir('') or chdir(undef) as chdir()");
3450 tmps = SvPV(*svp, n_a);
3454 TAINT_PROPER("chdir");
3459 TAINT_PROPER("chdir");
3460 PUSHi( PerlDir_chdir(tmps) >= 0 );
3462 /* Clear the DEFAULT element of ENV so we'll get the new value
3464 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3472 dSP; dMARK; dTARGET;
3473 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3479 DIE(aTHX_ PL_no_func, "chown");
3489 TAINT_PROPER("chroot");
3490 PUSHi( chroot(tmps) >= 0 );
3493 DIE(aTHX_ PL_no_func, "chroot");
3499 dSP; dMARK; dTARGET;
3501 value = (I32)apply(PL_op->op_type, MARK, SP);
3509 dSP; dMARK; dTARGET;
3511 value = (I32)apply(PL_op->op_type, MARK, SP);
3519 dSP; dMARK; dTARGET;
3521 value = (I32)apply(PL_op->op_type, MARK, SP);
3533 char *tmps2 = POPpx;
3534 char *tmps = SvPV(TOPs, n_a);
3535 TAINT_PROPER("rename");
3537 anum = PerlLIO_rename(tmps, tmps2);
3539 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3540 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3543 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3544 (void)UNLINK(tmps2);
3545 if (!(anum = link(tmps, tmps2)))
3546 anum = UNLINK(tmps);
3559 char *tmps2 = POPpx;
3560 char *tmps = SvPV(TOPs, n_a);
3561 TAINT_PROPER("link");
3562 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3565 DIE(aTHX_ PL_no_func, "link");
3574 char *tmps2 = POPpx;
3575 char *tmps = SvPV(TOPs, n_a);
3576 TAINT_PROPER("symlink");
3577 SETi( symlink(tmps, tmps2) >= 0 );
3580 DIE(aTHX_ PL_no_func, "symlink");
3590 char buf[MAXPATHLEN];
3594 #ifndef INCOMPLETE_TAINTS
3598 len = readlink(tmps, buf, sizeof(buf) - 1);
3606 RETSETUNDEF; /* just pretend it's a normal file */
3610 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3612 S_dooneliner(pTHX_ char *cmd, char *filename)
3614 char *save_filename = filename;
3620 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3621 strcpy(cmdline, cmd);
3622 strcat(cmdline, " ");
3623 for (s = cmdline + strlen(cmdline); *filename; ) {
3628 myfp = PerlProc_popen(cmdline, "r");
3632 SV *tmpsv = sv_newmortal();
3633 /* Need to save/restore 'PL_rs' ?? */
3634 s = sv_gets(tmpsv, myfp, 0);
3635 (void)PerlProc_pclose(myfp);
3639 #ifdef HAS_SYS_ERRLIST
3644 /* you don't see this */
3646 #ifdef HAS_SYS_ERRLIST
3654 if (instr(s, errmsg)) {
3661 #define EACCES EPERM
3663 if (instr(s, "cannot make"))
3664 SETERRNO(EEXIST,RMS_FEX);
3665 else if (instr(s, "existing file"))
3666 SETERRNO(EEXIST,RMS_FEX);
3667 else if (instr(s, "ile exists"))
3668 SETERRNO(EEXIST,RMS_FEX);
3669 else if (instr(s, "non-exist"))
3670 SETERRNO(ENOENT,RMS_FNF);
3671 else if (instr(s, "does not exist"))
3672 SETERRNO(ENOENT,RMS_FNF);
3673 else if (instr(s, "not empty"))
3674 SETERRNO(EBUSY,SS_DEVOFFLINE);
3675 else if (instr(s, "cannot access"))
3676 SETERRNO(EACCES,RMS_PRV);
3678 SETERRNO(EPERM,RMS_PRV);
3681 else { /* some mkdirs return no failure indication */
3682 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3683 if (PL_op->op_type == OP_RMDIR)
3688 SETERRNO(EACCES,RMS_PRV); /* a guess */
3697 /* This macro removes trailing slashes from a directory name.
3698 * Different operating and file systems take differently to
3699 * trailing slashes. According to POSIX 1003.1 1996 Edition
3700 * any number of trailing slashes should be allowed.
3701 * Thusly we snip them away so that even non-conforming
3702 * systems are happy.
3703 * We should probably do this "filtering" for all
3704 * the functions that expect (potentially) directory names:
3705 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3706 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3708 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3709 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3712 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3713 (tmps) = savepvn((tmps), (len)); \
3733 TRIMSLASHES(tmps,len,copy);
3735 TAINT_PROPER("mkdir");
3737 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3739 SETi( dooneliner("mkdir", tmps) );
3740 oldumask = PerlLIO_umask(0);
3741 PerlLIO_umask(oldumask);
3742 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3756 TRIMSLASHES(tmps,len,copy);
3757 TAINT_PROPER("rmdir");
3759 SETi( PerlDir_rmdir(tmps) >= 0 );
3761 SETi( dooneliner("rmdir", tmps) );
3768 /* Directory calls. */
3772 #if defined(Direntry_t) && defined(HAS_READDIR)
3775 char *dirname = POPpx;
3777 register IO *io = GvIOn(gv);
3783 PerlDir_close(IoDIRP(io));
3784 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3790 SETERRNO(EBADF,RMS_DIR);
3793 DIE(aTHX_ PL_no_dir_func, "opendir");
3799 #if defined(Direntry_t) && defined(HAS_READDIR)
3801 #if !defined(I_DIRENT) && !defined(VMS)
3802 Direntry_t *readdir (DIR *);
3804 register Direntry_t *dp;
3806 register IO *io = GvIOn(gv);
3809 if (!io || !IoDIRP(io))
3812 if (GIMME == G_ARRAY) {
3814 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3816 sv = newSVpvn(dp->d_name, dp->d_namlen);
3818 sv = newSVpv(dp->d_name, 0);
3820 #ifndef INCOMPLETE_TAINTS
3821 if (!(IoFLAGS(io) & IOf_UNTAINT))
3824 XPUSHs(sv_2mortal(sv));
3828 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3831 sv = newSVpvn(dp->d_name, dp->d_namlen);
3833 sv = newSVpv(dp->d_name, 0);
3835 #ifndef INCOMPLETE_TAINTS
3836 if (!(IoFLAGS(io) & IOf_UNTAINT))
3839 XPUSHs(sv_2mortal(sv));
3845 SETERRNO(EBADF,RMS_ISI);
3846 if (GIMME == G_ARRAY)
3851 DIE(aTHX_ PL_no_dir_func, "readdir");
3857 #if defined(HAS_TELLDIR) || defined(telldir)
3859 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3860 /* XXX netbsd still seemed to.
3861 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3862 --JHI 1999-Feb-02 */
3863 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3864 long telldir (DIR *);
3867 register IO *io = GvIOn(gv);
3869 if (!io || !IoDIRP(io))
3872 PUSHi( PerlDir_tell(IoDIRP(io)) );
3876 SETERRNO(EBADF,RMS_ISI);
3879 DIE(aTHX_ PL_no_dir_func, "telldir");
3885 #if defined(HAS_SEEKDIR) || defined(seekdir)
3889 register IO *io = GvIOn(gv);
3891 if (!io || !IoDIRP(io))
3894 (void)PerlDir_seek(IoDIRP(io), along);
3899 SETERRNO(EBADF,RMS_ISI);
3902 DIE(aTHX_ PL_no_dir_func, "seekdir");
3908 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3911 register IO *io = GvIOn(gv);
3913 if (!io || !IoDIRP(io))
3916 (void)PerlDir_rewind(IoDIRP(io));
3920 SETERRNO(EBADF,RMS_ISI);
3923 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3929 #if defined(Direntry_t) && defined(HAS_READDIR)
3932 register IO *io = GvIOn(gv);
3934 if (!io || !IoDIRP(io))
3937 #ifdef VOID_CLOSEDIR
3938 PerlDir_close(IoDIRP(io));
3940 if (PerlDir_close(IoDIRP(io)) < 0) {
3941 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3950 SETERRNO(EBADF,RMS_IFI);
3953 DIE(aTHX_ PL_no_dir_func, "closedir");
3957 /* Process control. */
3967 PERL_FLUSHALL_FOR_CHILD;
3968 childpid = PerlProc_fork();
3973 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3974 SvREADONLY_off(GvSV(tmpgv));
3975 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3976 SvREADONLY_on(GvSV(tmpgv));
3978 #ifdef THREADS_HAVE_PIDS
3979 PL_ppid = (IV)getppid();
3981 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3986 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3991 PERL_FLUSHALL_FOR_CHILD;
3992 childpid = PerlProc_fork();
3998 DIE(aTHX_ PL_no_func, "fork");
4005 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4010 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4011 childpid = wait4pid(-1, &argflags, 0);
4013 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4018 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4019 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4020 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4022 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4027 DIE(aTHX_ PL_no_func, "wait");
4033 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4041 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4042 childpid = wait4pid(childpid, &argflags, optype);
4044 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4049 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4050 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4051 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4053 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4058 DIE(aTHX_ PL_no_func, "waitpid");
4064 dSP; dMARK; dORIGMARK; dTARGET;
4072 while (++MARK <= SP) {
4073 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4078 TAINT_PROPER("system");
4080 PERL_FLUSHALL_FOR_CHILD;
4081 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4086 if (PerlProc_pipe(pp) >= 0)
4088 while ((childpid = PerlProc_fork()) == -1) {
4089 if (errno != EAGAIN) {
4094 PerlLIO_close(pp[0]);
4095 PerlLIO_close(pp[1]);
4102 Sigsave_t ihand,qhand; /* place to save signals during system() */
4106 PerlLIO_close(pp[1]);
4108 rsignal_save(SIGINT, SIG_IGN, &ihand);
4109 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4112 result = wait4pid(childpid, &status, 0);
4113 } while (result == -1 && errno == EINTR);
4115 (void)rsignal_restore(SIGINT, &ihand);
4116 (void)rsignal_restore(SIGQUIT, &qhand);
4118 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4119 do_execfree(); /* free any memory child malloced on fork */
4125 while (n < sizeof(int)) {
4126 n1 = PerlLIO_read(pp[0],
4127 (void*)(((char*)&errkid)+n),
4133 PerlLIO_close(pp[0]);
4134 if (n) { /* Error */
4135 if (n != sizeof(int))
4136 DIE(aTHX_ "panic: kid popen errno read");
4137 errno = errkid; /* Propagate errno from kid */
4138 STATUS_CURRENT = -1;
4141 PUSHi(STATUS_CURRENT);
4145 PerlLIO_close(pp[0]);
4146 #if defined(HAS_FCNTL) && defined(F_SETFD)
4147 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4150 if (PL_op->op_flags & OPf_STACKED) {
4151 SV *really = *++MARK;
4152 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4154 else if (SP - MARK != 1)
4155 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4157 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4161 #else /* ! FORK or VMS or OS/2 */
4164 if (PL_op->op_flags & OPf_STACKED) {
4165 SV *really = *++MARK;
4166 # if defined(WIN32) || defined(OS2)
4167 value = (I32)do_aspawn(really, MARK, SP);
4169 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4172 else if (SP - MARK != 1) {
4173 # if defined(WIN32) || defined(OS2)
4174 value = (I32)do_aspawn(Nullsv, MARK, SP);
4176 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4180 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4182 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4184 STATUS_NATIVE_SET(value);
4187 PUSHi(result ? value : STATUS_CURRENT);
4188 #endif /* !FORK or VMS */
4194 dSP; dMARK; dORIGMARK; dTARGET;
4200 while (++MARK <= SP) {
4201 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4206 TAINT_PROPER("exec");
4208 PERL_FLUSHALL_FOR_CHILD;
4209 if (PL_op->op_flags & OPf_STACKED) {
4210 SV *really = *++MARK;
4211 value = (I32)do_aexec(really, MARK, SP);
4213 else if (SP - MARK != 1)
4215 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4219 (void ) do_aspawn(Nullsv, MARK, SP);
4223 value = (I32)do_aexec(Nullsv, MARK, SP);
4228 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4231 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4234 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4247 dSP; dMARK; dTARGET;
4249 value = (I32)apply(PL_op->op_type, MARK, SP);
4254 DIE(aTHX_ PL_no_func, "kill");
4262 # ifdef THREADS_HAVE_PIDS
4265 XPUSHi( getppid() );
4269 DIE(aTHX_ PL_no_func, "getppid");
4285 pgrp = (I32)BSD_GETPGRP(pid);
4287 if (pid != 0 && pid != PerlProc_getpid())
4288 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4294 DIE(aTHX_ PL_no_func, "getpgrp()");
4313 TAINT_PROPER("setpgrp");
4315 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4317 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4318 || (pid != 0 && pid != PerlProc_getpid()))
4320 DIE(aTHX_ "setpgrp can't take arguments");
4322 SETi( setpgrp() >= 0 );
4323 #endif /* USE_BSDPGRP */
4326 DIE(aTHX_ PL_no_func, "setpgrp()");
4332 #ifdef HAS_GETPRIORITY
4336 SETi( getpriority(which, who) );
4339 DIE(aTHX_ PL_no_func, "getpriority()");
4345 #ifdef HAS_SETPRIORITY
4350 TAINT_PROPER("setpriority");
4351 SETi( setpriority(which, who, niceval) >= 0 );
4354 DIE(aTHX_ PL_no_func, "setpriority()");
4364 XPUSHn( time(Null(Time_t*)) );
4366 XPUSHi( time(Null(Time_t*)) );
4377 (void)PerlProc_times(&PL_timesbuf);
4379 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4380 /* struct tms, though same data */
4384 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4385 if (GIMME == G_ARRAY) {
4386 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4387 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4388 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4394 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4396 if (GIMME == G_ARRAY) {
4397 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4398 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4399 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4403 DIE(aTHX_ "times not implemented");
4405 #endif /* HAS_TIMES */
4418 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4419 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4420 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4426 when = (Time_t)SvNVx(POPs);
4428 when = (Time_t)SvIVx(POPs);
4431 if (PL_op->op_type == OP_LOCALTIME)
4432 tmbuf = localtime(&when);
4434 tmbuf = gmtime(&when);
4436 if (GIMME != G_ARRAY) {
4442 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4443 dayname[tmbuf->tm_wday],
4444 monname[tmbuf->tm_mon],
4449 tmbuf->tm_year + 1900);
4450 PUSHs(sv_2mortal(tsv));
4455 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4456 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4457 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4458 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4459 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4460 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4461 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4462 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4463 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4474 anum = alarm((unsigned int)anum);
4481 DIE(aTHX_ PL_no_func, "alarm");
4492 (void)time(&lasttime);
4497 PerlProc_sleep((unsigned int)duration);
4500 XPUSHi(when - lasttime);
4504 /* Shared memory. */
4518 return pp_shmwrite();
4523 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4524 dSP; dMARK; dTARGET;
4525 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4534 /* Message passing. */
4548 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4549 dSP; dMARK; dTARGET;
4550 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4561 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4562 dSP; dMARK; dTARGET;
4563 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4576 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4577 dSP; dMARK; dTARGET;
4578 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4585 DIE(aTHX_ "System V IPC is not implemented on this machine");
4591 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4592 dSP; dMARK; dTARGET;
4593 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4601 PUSHp(zero_but_true, ZBTLEN);
4611 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4612 dSP; dMARK; dTARGET;
4613 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4622 /* Get system info. */
4626 #ifdef HAS_GETHOSTBYNAME
4627 return pp_ghostent();
4629 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4635 #ifdef HAS_GETHOSTBYADDR
4636 return pp_ghostent();
4638 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4644 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4646 I32 which = PL_op->op_type;
4647 register char **elem;
4649 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4650 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4651 struct hostent *gethostbyname(Netdb_name_t);
4652 struct hostent *gethostent(void);
4654 struct hostent *hent;
4659 if (which == OP_GHBYNAME) {
4660 #ifdef HAS_GETHOSTBYNAME
4661 char* name = POPpbytex;
4662 hent = PerlSock_gethostbyname(name);
4664 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4667 else if (which == OP_GHBYADDR) {
4668 #ifdef HAS_GETHOSTBYADDR
4669 int addrtype = POPi;
4672 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4674 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4676 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4680 #ifdef HAS_GETHOSTENT
4681 hent = PerlSock_gethostent();
4683 DIE(aTHX_ PL_no_sock_func, "gethostent");
4686 #ifdef HOST_NOT_FOUND
4688 #ifdef USE_REENTRANT_API
4689 # ifdef USE_GETHOSTENT_ERRNO
4690 h_errno = PL_reentrant_buffer->_gethostent_errno;
4693 STATUS_NATIVE_SET(h_errno);
4697 if (GIMME != G_ARRAY) {
4698 PUSHs(sv = sv_newmortal());
4700 if (which == OP_GHBYNAME) {
4702 sv_setpvn(sv, hent->h_addr, hent->h_length);
4705 sv_setpv(sv, (char*)hent->h_name);
4711 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4712 sv_setpv(sv, (char*)hent->h_name);
4713 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4714 for (elem = hent->h_aliases; elem && *elem; elem++) {
4715 sv_catpv(sv, *elem);
4717 sv_catpvn(sv, " ", 1);
4719 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4720 sv_setiv(sv, (IV)hent->h_addrtype);
4721 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4722 len = hent->h_length;
4723 sv_setiv(sv, (IV)len);
4725 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4726 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4727 sv_setpvn(sv, *elem, len);
4730 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4732 sv_setpvn(sv, hent->h_addr, len);
4737 DIE(aTHX_ PL_no_sock_func, "gethostent");
4743 #ifdef HAS_GETNETBYNAME
4744 return pp_gnetent();
4746 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4752 #ifdef HAS_GETNETBYADDR
4753 return pp_gnetent();
4755 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4761 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4763 I32 which = PL_op->op_type;
4764 register char **elem;
4766 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4767 struct netent *getnetbyaddr(Netdb_net_t, int);
4768 struct netent *getnetbyname(Netdb_name_t);
4769 struct netent *getnetent(void);
4771 struct netent *nent;
4774 if (which == OP_GNBYNAME){
4775 #ifdef HAS_GETNETBYNAME
4776 char *name = POPpbytex;
4777 nent = PerlSock_getnetbyname(name);
4779 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4782 else if (which == OP_GNBYADDR) {
4783 #ifdef HAS_GETNETBYADDR
4784 int addrtype = POPi;
4785 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4786 nent = PerlSock_getnetbyaddr(addr, addrtype);
4788 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4792 #ifdef HAS_GETNETENT
4793 nent = PerlSock_getnetent();
4795 DIE(aTHX_ PL_no_sock_func, "getnetent");
4798 #ifdef HOST_NOT_FOUND
4800 #ifdef USE_REENTRANT_API
4801 # ifdef USE_GETNETENT_ERRNO
4802 h_errno = PL_reentrant_buffer->_getnetent_errno;
4805 STATUS_NATIVE_SET(h_errno);
4810 if (GIMME != G_ARRAY) {
4811 PUSHs(sv = sv_newmortal());
4813 if (which == OP_GNBYNAME)
4814 sv_setiv(sv, (IV)nent->n_net);
4816 sv_setpv(sv, nent->n_name);
4822 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4823 sv_setpv(sv, nent->n_name);
4824 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4825 for (elem = nent->n_aliases; elem && *elem; elem++) {
4826 sv_catpv(sv, *elem);
4828 sv_catpvn(sv, " ", 1);
4830 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4831 sv_setiv(sv, (IV)nent->n_addrtype);
4832 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4833 sv_setiv(sv, (IV)nent->n_net);
4838 DIE(aTHX_ PL_no_sock_func, "getnetent");
4844 #ifdef HAS_GETPROTOBYNAME
4845 return pp_gprotoent();
4847 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4853 #ifdef HAS_GETPROTOBYNUMBER
4854 return pp_gprotoent();
4856 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4862 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4864 I32 which = PL_op->op_type;
4865 register char **elem;
4867 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4868 struct protoent *getprotobyname(Netdb_name_t);
4869 struct protoent *getprotobynumber(int);
4870 struct protoent *getprotoent(void);
4872 struct protoent *pent;
4875 if (which == OP_GPBYNAME) {
4876 #ifdef HAS_GETPROTOBYNAME
4877 char* name = POPpbytex;
4878 pent = PerlSock_getprotobyname(name);
4880 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4883 else if (which == OP_GPBYNUMBER) {
4884 #ifdef HAS_GETPROTOBYNUMBER
4886 pent = PerlSock_getprotobynumber(number);
4888 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4892 #ifdef HAS_GETPROTOENT
4893 pent = PerlSock_getprotoent();
4895 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4899 if (GIMME != G_ARRAY) {
4900 PUSHs(sv = sv_newmortal());
4902 if (which == OP_GPBYNAME)
4903 sv_setiv(sv, (IV)pent->p_proto);
4905 sv_setpv(sv, pent->p_name);
4911 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4912 sv_setpv(sv, pent->p_name);
4913 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4914 for (elem = pent->p_aliases; elem && *elem; elem++) {
4915 sv_catpv(sv, *elem);
4917 sv_catpvn(sv, " ", 1);
4919 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4920 sv_setiv(sv, (IV)pent->p_proto);
4925 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4931 #ifdef HAS_GETSERVBYNAME
4932 return pp_gservent();
4934 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4940 #ifdef HAS_GETSERVBYPORT
4941 return pp_gservent();
4943 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4949 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4951 I32 which = PL_op->op_type;
4952 register char **elem;
4954 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4955 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4956 struct servent *getservbyport(int, Netdb_name_t);
4957 struct servent *getservent(void);
4959 struct servent *sent;
4962 if (which == OP_GSBYNAME) {
4963 #ifdef HAS_GETSERVBYNAME
4964 char *proto = POPpbytex;
4965 char *name = POPpbytex;
4967 if (proto && !*proto)
4970 sent = PerlSock_getservbyname(name, proto);
4972 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4975 else if (which == OP_GSBYPORT) {
4976 #ifdef HAS_GETSERVBYPORT
4977 char *proto = POPpbytex;
4978 unsigned short port = (unsigned short)POPu;
4980 if (proto && !*proto)
4984 port = PerlSock_htons(port);
4986 sent = PerlSock_getservbyport(port, proto);
4988 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4992 #ifdef HAS_GETSERVENT
4993 sent = PerlSock_getservent();
4995 DIE(aTHX_ PL_no_sock_func, "getservent");
4999 if (GIMME != G_ARRAY) {
5000 PUSHs(sv = sv_newmortal());
5002 if (which == OP_GSBYNAME) {
5004 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5006 sv_setiv(sv, (IV)(sent->s_port));
5010 sv_setpv(sv, sent->s_name);
5016 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5017 sv_setpv(sv, sent->s_name);
5018 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5019 for (elem = sent->s_aliases; elem && *elem; elem++) {
5020 sv_catpv(sv, *elem);
5022 sv_catpvn(sv, " ", 1);
5024 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5026 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5028 sv_setiv(sv, (IV)(sent->s_port));
5030 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5031 sv_setpv(sv, sent->s_proto);
5036 DIE(aTHX_ PL_no_sock_func, "getservent");
5042 #ifdef HAS_SETHOSTENT
5044 PerlSock_sethostent(TOPi);
5047 DIE(aTHX_ PL_no_sock_func, "sethostent");
5053 #ifdef HAS_SETNETENT
5055 PerlSock_setnetent(TOPi);
5058 DIE(aTHX_ PL_no_sock_func, "setnetent");
5064 #ifdef HAS_SETPROTOENT
5066 PerlSock_setprotoent(TOPi);
5069 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5075 #ifdef HAS_SETSERVENT
5077 PerlSock_setservent(TOPi);
5080 DIE(aTHX_ PL_no_sock_func, "setservent");
5086 #ifdef HAS_ENDHOSTENT
5088 PerlSock_endhostent();
5092 DIE(aTHX_ PL_no_sock_func, "endhostent");
5098 #ifdef HAS_ENDNETENT
5100 PerlSock_endnetent();
5104 DIE(aTHX_ PL_no_sock_func, "endnetent");
5110 #ifdef HAS_ENDPROTOENT
5112 PerlSock_endprotoent();
5116 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5122 #ifdef HAS_ENDSERVENT
5124 PerlSock_endservent();
5128 DIE(aTHX_ PL_no_sock_func, "endservent");
5137 DIE(aTHX_ PL_no_func, "getpwnam");
5146 DIE(aTHX_ PL_no_func, "getpwuid");
5154 I32 which = PL_op->op_type;
5157 struct passwd *pwent = NULL;
5159 * We currently support only the SysV getsp* shadow password interface.
5160 * The interface is declared in <shadow.h> and often one needs to link
5161 * with -lsecurity or some such.
5162 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5165 * AIX getpwnam() is clever enough to return the encrypted password
5166 * only if the caller (euid?) is root.
5168 * There are at least three other shadow password APIs. Many platforms
5169 * seem to contain more than one interface for accessing the shadow
5170 * password databases, possibly for compatibility reasons.
5171 * The getsp*() is by far he simplest one, the other two interfaces
5172 * are much more complicated, but also very similar to each other.
5177 * struct pr_passwd *getprpw*();
5178 * The password is in
5179 * char getprpw*(...).ufld.fd_encrypt[]
5180 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5185 * struct es_passwd *getespw*();
5186 * The password is in
5187 * char *(getespw*(...).ufld.fd_encrypt)
5188 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5191 * struct userpw *getuserpw();
5192 * The password is in
5193 * char *(getuserpw(...)).spw_upw_passwd
5194 * (but the de facto standard getpwnam() should work okay)
5196 * Mention I_PROT here so that Configure probes for it.
5198 * In HP-UX for getprpw*() the manual page claims that one should include
5199 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5200 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5201 * and pp_sys.c already includes <shadow.h> if there is such.
5203 * Note that <sys/security.h> is already probed for, but currently
5204 * it is only included in special cases.
5206 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5207 * be preferred interface, even though also the getprpw*() interface
5208 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5209 * One also needs to call set_auth_parameters() in main() before
5210 * doing anything else, whether one is using getespw*() or getprpw*().
5212 * Note that accessing the shadow databases can be magnitudes
5213 * slower than accessing the standard databases.
5221 char* name = POPpbytex;
5222 pwent = getpwnam(name);
5228 pwent = getpwuid(uid);
5232 # ifdef HAS_GETPWENT
5234 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5235 if (pwent) pwent = getpwnam(pwent->pw_name);
5238 DIE(aTHX_ PL_no_func, "getpwent");
5244 if (GIMME != G_ARRAY) {
5245 PUSHs(sv = sv_newmortal());
5247 if (which == OP_GPWNAM)
5248 # if Uid_t_sign <= 0
5249 sv_setiv(sv, (IV)pwent->pw_uid);
5251 sv_setuv(sv, (UV)pwent->pw_uid);
5254 sv_setpv(sv, pwent->pw_name);
5260 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5261 sv_setpv(sv, pwent->pw_name);
5263 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5265 /* If we have getspnam(), we try to dig up the shadow
5266 * password. If we are underprivileged, the shadow
5267 * interface will set the errno to EACCES or similar,
5268 * and return a null pointer. If this happens, we will
5269 * use the dummy password (usually "*" or "x") from the
5270 * standard password database.
5272 * In theory we could skip the shadow call completely
5273 * if euid != 0 but in practice we cannot know which
5274 * security measures are guarding the shadow databases
5275 * on a random platform.
5277 * Resist the urge to use additional shadow interfaces.
5278 * Divert the urge to writing an extension instead.
5281 /* Some AIX setups falsely(?) detect some getspnam(), which
5282 * has a different API than the Solaris/IRIX one. */
5283 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5285 struct spwd *spwent;
5286 int saverrno; /* Save and restore errno so that
5287 * underprivileged attempts seem
5288 * to have never made the unsccessful
5289 * attempt to retrieve the shadow password. */
5292 spwent = getspnam(pwent->pw_name);
5294 if (spwent && spwent->sp_pwdp)
5295 sv_setpv(sv, spwent->sp_pwdp);
5299 if (!SvPOK(sv)) /* Use the standard password, then. */
5300 sv_setpv(sv, pwent->pw_passwd);
5303 # ifndef INCOMPLETE_TAINTS
5304 /* passwd is tainted because user himself can diddle with it.
5305 * admittedly not much and in a very limited way, but nevertheless. */
5309 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5310 # if Uid_t_sign <= 0
5311 sv_setiv(sv, (IV)pwent->pw_uid);
5313 sv_setuv(sv, (UV)pwent->pw_uid);
5316 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5317 # if Uid_t_sign <= 0
5318 sv_setiv(sv, (IV)pwent->pw_gid);
5320 sv_setuv(sv, (UV)pwent->pw_gid);
5322 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5323 * because of the poor interface of the Perl getpw*(),
5324 * not because there's some standard/convention saying so.
5325 * A better interface would have been to return a hash,
5326 * but we are accursed by our history, alas. --jhi. */
5327 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5329 sv_setiv(sv, (IV)pwent->pw_change);
5332 sv_setiv(sv, (IV)pwent->pw_quota);
5335 sv_setpv(sv, pwent->pw_age);
5340 /* pw_class and pw_comment are mutually exclusive--.
5341 * see the above note for pw_change, pw_quota, and pw_age. */
5342 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5344 sv_setpv(sv, pwent->pw_class);
5347 sv_setpv(sv, pwent->pw_comment);
5351 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5353 sv_setpv(sv, pwent->pw_gecos);
5355 # ifndef INCOMPLETE_TAINTS
5356 /* pw_gecos is tainted because user himself can diddle with it. */
5360 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5361 sv_setpv(sv, pwent->pw_dir);
5363 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5364 sv_setpv(sv, pwent->pw_shell);
5365 # ifndef INCOMPLETE_TAINTS
5366 /* pw_shell is tainted because user himself can diddle with it. */
5371 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5372 sv_setiv(sv, (IV)pwent->pw_expire);
5377 DIE(aTHX_ PL_no_func, "getpwent");
5383 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5388 DIE(aTHX_ PL_no_func, "setpwent");
5394 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5399 DIE(aTHX_ PL_no_func, "endpwent");
5408 DIE(aTHX_ PL_no_func, "getgrnam");
5417 DIE(aTHX_ PL_no_func, "getgrgid");
5425 I32 which = PL_op->op_type;
5426 register char **elem;
5428 struct group *grent;
5431 if (which == OP_GGRNAM) {
5432 char* name = POPpbytex;
5433 grent = (struct group *)getgrnam(name);
5435 else if (which == OP_GGRGID) {
5437 grent = (struct group *)getgrgid(gid);
5441 grent = (struct group *)getgrent();
5443 DIE(aTHX_ PL_no_func, "getgrent");
5447 if (GIMME != G_ARRAY) {
5448 PUSHs(sv = sv_newmortal());
5450 if (which == OP_GGRNAM)
5451 sv_setiv(sv, (IV)grent->gr_gid);
5453 sv_setpv(sv, grent->gr_name);
5459 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5460 sv_setpv(sv, grent->gr_name);
5462 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5464 sv_setpv(sv, grent->gr_passwd);
5467 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5468 sv_setiv(sv, (IV)grent->gr_gid);
5470 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5471 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5472 /* In UNICOS/mk (_CRAYMPP) the multithreading
5473 * versions (getgrnam_r, getgrgid_r)
5474 * seem to return an illegal pointer
5475 * as the group members list, gr_mem.
5476 * getgrent() doesn't even have a _r version
5477 * but the gr_mem is poisonous anyway.
5478 * So yes, you cannot get the list of group
5479 * members if building multithreaded in UNICOS/mk. */
5480 for (elem = grent->gr_mem; elem && *elem; elem++) {
5481 sv_catpv(sv, *elem);
5483 sv_catpvn(sv, " ", 1);
5490 DIE(aTHX_ PL_no_func, "getgrent");
5496 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5501 DIE(aTHX_ PL_no_func, "setgrent");
5507 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5512 DIE(aTHX_ PL_no_func, "endgrent");
5522 if (!(tmps = PerlProc_getlogin()))
5524 PUSHp(tmps, strlen(tmps));
5527 DIE(aTHX_ PL_no_func, "getlogin");
5531 /* Miscellaneous. */
5536 dSP; dMARK; dORIGMARK; dTARGET;
5537 register I32 items = SP - MARK;
5538 unsigned long a[20];
5544 while (++MARK <= SP) {
5545 if (SvTAINTED(*MARK)) {
5551 TAINT_PROPER("syscall");
5554 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5555 * or where sizeof(long) != sizeof(char*). But such machines will
5556 * not likely have syscall implemented either, so who cares?
5558 while (++MARK <= SP) {
5559 if (SvNIOK(*MARK) || !i)
5560 a[i++] = SvIV(*MARK);
5561 else if (*MARK == &PL_sv_undef)
5564 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5570 DIE(aTHX_ "Too many args to syscall");
5572 DIE(aTHX_ "Too few args to syscall");
5574 retval = syscall(a[0]);
5577 retval = syscall(a[0],a[1]);
5580 retval = syscall(a[0],a[1],a[2]);
5583 retval = syscall(a[0],a[1],a[2],a[3]);
5586 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5589 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5592 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5595 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5599 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5602 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5605 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5609 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5613 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],
5618 a[10],a[11],a[12],a[13]);
5620 #endif /* atarist */
5626 DIE(aTHX_ PL_no_func, "syscall");
5630 #ifdef FCNTL_EMULATE_FLOCK
5632 /* XXX Emulate flock() with fcntl().
5633 What's really needed is a good file locking module.
5637 fcntl_emulate_flock(int fd, int operation)
5641 switch (operation & ~LOCK_NB) {
5643 flock.l_type = F_RDLCK;
5646 flock.l_type = F_WRLCK;
5649 flock.l_type = F_UNLCK;
5655 flock.l_whence = SEEK_SET;
5656 flock.l_start = flock.l_len = (Off_t)0;
5658 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5661 #endif /* FCNTL_EMULATE_FLOCK */
5663 #ifdef LOCKF_EMULATE_FLOCK
5665 /* XXX Emulate flock() with lockf(). This is just to increase
5666 portability of scripts. The calls are not completely
5667 interchangeable. What's really needed is a good file
5671 /* The lockf() constants might have been defined in <unistd.h>.
5672 Unfortunately, <unistd.h> causes troubles on some mixed
5673 (BSD/POSIX) systems, such as SunOS 4.1.3.
5675 Further, the lockf() constants aren't POSIX, so they might not be
5676 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5677 just stick in the SVID values and be done with it. Sigh.
5681 # define F_ULOCK 0 /* Unlock a previously locked region */
5684 # define F_LOCK 1 /* Lock a region for exclusive use */
5687 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5690 # define F_TEST 3 /* Test a region for other processes locks */
5694 lockf_emulate_flock(int fd, int operation)
5700 /* flock locks entire file so for lockf we need to do the same */
5702 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5703 if (pos > 0) /* is seekable and needs to be repositioned */
5704 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5705 pos = -1; /* seek failed, so don't seek back afterwards */
5708 switch (operation) {
5710 /* LOCK_SH - get a shared lock */
5712 /* LOCK_EX - get an exclusive lock */
5714 i = lockf (fd, F_LOCK, 0);
5717 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5718 case LOCK_SH|LOCK_NB:
5719 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5720 case LOCK_EX|LOCK_NB:
5721 i = lockf (fd, F_TLOCK, 0);
5723 if ((errno == EAGAIN) || (errno == EACCES))
5724 errno = EWOULDBLOCK;
5727 /* LOCK_UN - unlock (non-blocking is a no-op) */
5729 case LOCK_UN|LOCK_NB:
5730 i = lockf (fd, F_ULOCK, 0);
5733 /* Default - can't decipher operation */
5740 if (pos > 0) /* need to restore position of the handle */
5741 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5746 #endif /* LOCKF_EMULATE_FLOCK */