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 /* If SELECT_MIN_BITS is greater than one we most probably will want
1029 * to align the sizes with SELECT_MIN_BITS/8 because for example
1030 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1031 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1032 * on (sets/tests/clears bits) is 32 bits. */
1033 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1035 growsize = sizeof(fd_set);
1043 timebuf.tv_sec = (long)value;
1044 value -= (NV)timebuf.tv_sec;
1045 timebuf.tv_usec = (long)(value * 1000000.0);
1048 tbuf = Null(struct timeval*);
1050 for (i = 1; i <= 3; i++) {
1056 else if (!SvPOK(sv))
1057 SvPV_force(sv,n_a); /* force string conversion */
1060 Sv_Grow(sv, growsize);
1064 while (++j <= growsize) {
1068 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1070 New(403, fd_sets[i], growsize, char);
1071 for (offset = 0; offset < growsize; offset += masksize) {
1072 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1073 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1076 fd_sets[i] = SvPVX(sv);
1080 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1081 /* Can't make just the (void*) conditional because that would be
1082 * cpp #if within cpp macro, and not all compilers like that. */
1083 nfound = PerlSock_select(
1085 (Select_fd_set_t) fd_sets[1],
1086 (Select_fd_set_t) fd_sets[2],
1087 (Select_fd_set_t) fd_sets[3],
1088 (void*) tbuf); /* Workaround for compiler bug. */
1090 nfound = PerlSock_select(
1092 (Select_fd_set_t) fd_sets[1],
1093 (Select_fd_set_t) fd_sets[2],
1094 (Select_fd_set_t) fd_sets[3],
1097 for (i = 1; i <= 3; i++) {
1100 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1102 for (offset = 0; offset < growsize; offset += masksize) {
1103 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1104 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1106 Safefree(fd_sets[i]);
1113 if (GIMME == G_ARRAY && tbuf) {
1114 value = (NV)(timebuf.tv_sec) +
1115 (NV)(timebuf.tv_usec) / 1000000.0;
1116 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1117 sv_setnv(sv, value);
1121 DIE(aTHX_ "select not implemented");
1126 Perl_setdefout(pTHX_ GV *gv)
1129 (void)SvREFCNT_inc(gv);
1131 SvREFCNT_dec(PL_defoutgv);
1138 GV *newdefout, *egv;
1141 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1143 egv = GvEGV(PL_defoutgv);
1148 XPUSHs(&PL_sv_undef);
1150 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1151 if (gvp && *gvp == egv) {
1152 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1156 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1161 if (!GvIO(newdefout))
1162 gv_IOadd(newdefout);
1163 setdefout(newdefout);
1181 if (gv && (io = GvIO(gv))
1182 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1184 I32 gimme = GIMME_V;
1186 XPUSHs(SvTIED_obj((SV*)io, mg));
1189 call_method("GETC", gimme);
1192 if (gimme == G_SCALAR)
1193 SvSetMagicSV_nosteal(TARG, TOPs);
1196 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1197 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1198 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1199 report_evil_fh(gv, io, PL_op->op_type);
1200 SETERRNO(EBADF,RMS_IFI);
1204 sv_setpv(TARG, " ");
1205 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1206 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1207 /* Find out how many bytes the char needs */
1208 Size_t len = UTF8SKIP(SvPVX(TARG));
1211 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1212 SvCUR_set(TARG,1+len);
1222 return pp_sysread();
1226 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1228 register PERL_CONTEXT *cx;
1229 I32 gimme = GIMME_V;
1235 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1237 PAD_SET_CUR(CvPADLIST(cv), 1);
1239 setdefout(gv); /* locally select filehandle so $% et al work */
1272 SV *tmpsv = sv_newmortal();
1273 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1274 name = SvPV_nolen(tmpsv);
1277 DIE(aTHX_ "Undefined format \"%s\" called", name);
1278 DIE(aTHX_ "Not a format reference");
1281 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1283 IoFLAGS(io) &= ~IOf_DIDTOP;
1284 return doform(cv,gv,PL_op->op_next);
1290 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1291 register IO *io = GvIOp(gv);
1292 PerlIO *ofp = IoOFP(io);
1296 register PERL_CONTEXT *cx;
1298 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1299 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1302 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1303 PL_formtarget != PL_toptarget)
1307 if (!IoTOP_GV(io)) {
1311 if (!IoTOP_NAME(io)) {
1312 if (!IoFMT_NAME(io))
1313 IoFMT_NAME(io) = savepv(GvNAME(gv));
1314 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1315 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1316 if ((topgv && GvFORM(topgv)) ||
1317 !gv_fetchpv("top",FALSE,SVt_PVFM))
1318 IoTOP_NAME(io) = savepv(SvPVX(topname));
1320 IoTOP_NAME(io) = savepv("top");
1322 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1323 if (!topgv || !GvFORM(topgv)) {
1324 IoLINES_LEFT(io) = 100000000;
1327 IoTOP_GV(io) = topgv;
1329 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1330 I32 lines = IoLINES_LEFT(io);
1331 char *s = SvPVX(PL_formtarget);
1332 if (lines <= 0) /* Yow, header didn't even fit!!! */
1334 while (lines-- > 0) {
1335 s = strchr(s, '\n');
1341 STRLEN save = SvCUR(PL_formtarget);
1342 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1343 do_print(PL_formtarget, ofp);
1344 SvCUR_set(PL_formtarget, save);
1345 sv_chop(PL_formtarget, s);
1346 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1349 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1350 do_print(PL_formfeed, ofp);
1351 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1353 PL_formtarget = PL_toptarget;
1354 IoFLAGS(io) |= IOf_DIDTOP;
1357 DIE(aTHX_ "bad top format reference");
1362 SV *sv = sv_newmortal();
1363 gv_efullname4(sv, fgv, Nullch, FALSE);
1364 name = SvPV_nolen(sv);
1367 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1370 DIE(aTHX_ "Undefined top format called");
1374 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1375 return doform(cv,gv,PL_op);
1379 POPBLOCK(cx,PL_curpm);
1385 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1387 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1388 else if (ckWARN(WARN_CLOSED))
1389 report_evil_fh(gv, io, PL_op->op_type);
1394 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1395 if (ckWARN(WARN_IO))
1396 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1398 if (!do_print(PL_formtarget, fp))
1401 FmLINES(PL_formtarget) = 0;
1402 SvCUR_set(PL_formtarget, 0);
1403 *SvEND(PL_formtarget) = '\0';
1404 if (IoFLAGS(io) & IOf_FLUSH)
1405 (void)PerlIO_flush(fp);
1410 PL_formtarget = PL_bodytarget;
1412 return pop_return();
1417 dSP; dMARK; dORIGMARK;
1424 if (PL_op->op_flags & OPf_STACKED)
1429 if (gv && (io = GvIO(gv))
1430 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1432 if (MARK == ORIGMARK) {
1435 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1439 *MARK = SvTIED_obj((SV*)io, mg);
1442 call_method("PRINTF", G_SCALAR);
1445 MARK = ORIGMARK + 1;
1452 if (!(io = GvIO(gv))) {
1453 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1454 report_evil_fh(gv, io, PL_op->op_type);
1455 SETERRNO(EBADF,RMS_IFI);
1458 else if (!(fp = IoOFP(io))) {
1459 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1461 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1462 else if (ckWARN(WARN_CLOSED))
1463 report_evil_fh(gv, io, PL_op->op_type);
1465 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1469 do_sprintf(sv, SP - MARK, MARK + 1);
1470 if (!do_print(sv, fp))
1473 if (IoFLAGS(io) & IOf_FLUSH)
1474 if (PerlIO_flush(fp) == EOF)
1485 PUSHs(&PL_sv_undef);
1506 /* Need TIEHANDLE method ? */
1508 tmps = SvPV(sv, len);
1509 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1510 IoLINES(GvIOp(gv)) = 0;
1514 PUSHs(&PL_sv_undef);
1521 dSP; dMARK; dORIGMARK; dTARGET;
1528 Sock_size_t bufsize;
1535 bool charstart = FALSE;
1536 STRLEN charskip = 0;
1540 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1541 && gv && (io = GvIO(gv))
1542 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1547 *MARK = SvTIED_obj((SV*)io, mg);
1549 call_method("READ", G_SCALAR);
1562 sv_setpvn(bufsv, "", 0);
1563 length = SvIVx(*++MARK);
1566 offset = SvIVx(*++MARK);
1570 if (!io || !IoIFP(io)) {
1571 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1572 report_evil_fh(gv, io, PL_op->op_type);
1573 SETERRNO(EBADF,RMS_IFI);
1576 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1577 buffer = SvPVutf8_force(bufsv, blen);
1578 /* UTF8 may not have been set if they are all low bytes */
1582 buffer = SvPV_force(bufsv, blen);
1585 DIE(aTHX_ "Negative length");
1593 if (PL_op->op_type == OP_RECV) {
1594 char namebuf[MAXPATHLEN];
1595 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1596 bufsize = sizeof (struct sockaddr_in);
1598 bufsize = sizeof namebuf;
1600 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1604 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1605 /* 'offset' means 'flags' here */
1606 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1607 (struct sockaddr *)namebuf, &bufsize);
1611 /* Bogus return without padding */
1612 bufsize = sizeof (struct sockaddr_in);
1614 SvCUR_set(bufsv, count);
1615 *SvEND(bufsv) = '\0';
1616 (void)SvPOK_only(bufsv);
1620 /* This should not be marked tainted if the fp is marked clean */
1621 if (!(IoFLAGS(io) & IOf_UNTAINT))
1622 SvTAINTED_on(bufsv);
1624 sv_setpvn(TARG, namebuf, bufsize);
1629 if (PL_op->op_type == OP_RECV)
1630 DIE(aTHX_ PL_no_sock_func, "recv");
1632 if (DO_UTF8(bufsv)) {
1633 /* offset adjust in characters not bytes */
1634 blen = sv_len_utf8(bufsv);
1637 if (-offset > (int)blen)
1638 DIE(aTHX_ "Offset outside string");
1641 if (DO_UTF8(bufsv)) {
1642 /* convert offset-as-chars to offset-as-bytes */
1643 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1646 bufsize = SvCUR(bufsv);
1647 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1648 if (offset > bufsize) { /* Zero any newly allocated space */
1649 Zero(buffer+bufsize, offset-bufsize, char);
1651 buffer = buffer + offset;
1653 if (PL_op->op_type == OP_SYSREAD) {
1654 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1655 if (IoTYPE(io) == IoTYPE_SOCKET) {
1656 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1662 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1667 #ifdef HAS_SOCKET__bad_code_maybe
1668 if (IoTYPE(io) == IoTYPE_SOCKET) {
1669 char namebuf[MAXPATHLEN];
1670 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1671 bufsize = sizeof (struct sockaddr_in);
1673 bufsize = sizeof namebuf;
1675 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1676 (struct sockaddr *)namebuf, &bufsize);
1681 count = PerlIO_read(IoIFP(io), buffer, length);
1682 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1683 if (count == 0 && PerlIO_error(IoIFP(io)))
1687 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1688 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1691 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1692 *SvEND(bufsv) = '\0';
1693 (void)SvPOK_only(bufsv);
1694 if (fp_utf8 && !IN_BYTES) {
1695 /* Look at utf8 we got back and count the characters */
1696 char *bend = buffer + count;
1697 while (buffer < bend) {
1699 skip = UTF8SKIP(buffer);
1702 if (buffer - charskip + skip > bend) {
1703 /* partial character - try for rest of it */
1704 length = skip - (bend-buffer);
1705 offset = bend - SvPVX(bufsv);
1717 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1718 provided amount read (count) was what was requested (length)
1720 if (got < wanted && count == length) {
1721 length = wanted - got;
1722 offset = bend - SvPVX(bufsv);
1725 /* return value is character count */
1730 /* This should not be marked tainted if the fp is marked clean */
1731 if (!(IoFLAGS(io) & IOf_UNTAINT))
1732 SvTAINTED_on(bufsv);
1745 int items = (SP - PL_stack_base) - TOPMARK;
1749 sv = sv_2mortal(newSViv(sv_len(*SP)));
1758 dSP; dMARK; dORIGMARK; dTARGET;
1769 if (PL_op->op_type == OP_SYSWRITE
1770 && gv && (io = GvIO(gv))
1771 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1776 *MARK = SvTIED_obj((SV*)io, mg);
1778 call_method("WRITE", G_SCALAR);
1789 #if Size_t_size > IVSIZE
1790 length = (Size_t)SvNVx(*++MARK);
1792 length = (Size_t)SvIVx(*++MARK);
1794 if ((SSize_t)length < 0)
1795 DIE(aTHX_ "Negative length");
1798 if (!io || !IoIFP(io)) {
1800 if (ckWARN(WARN_CLOSED))
1801 report_evil_fh(gv, io, PL_op->op_type);
1802 SETERRNO(EBADF,RMS_IFI);
1806 if (PerlIO_isutf8(IoIFP(io))) {
1807 buffer = SvPVutf8(bufsv, blen);
1810 if (DO_UTF8(bufsv)) {
1811 /* Not modifying source SV, so making a temporary copy. */
1812 bufsv = sv_2mortal(newSVsv(bufsv));
1813 sv_utf8_downgrade(bufsv, FALSE);
1815 buffer = SvPV(bufsv, blen);
1818 if (PL_op->op_type == OP_SYSWRITE) {
1820 if (DO_UTF8(bufsv)) {
1821 /* length and offset are in chars */
1822 blen = sv_len_utf8(bufsv);
1825 offset = SvIVx(*++MARK);
1827 if (-offset > (IV)blen)
1828 DIE(aTHX_ "Offset outside string");
1830 } else if (offset >= (IV)blen && blen > 0)
1831 DIE(aTHX_ "Offset outside string");
1834 if (length > blen - offset)
1835 length = blen - offset;
1836 if (DO_UTF8(bufsv)) {
1837 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1838 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1841 buffer = buffer+offset;
1843 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1844 if (IoTYPE(io) == IoTYPE_SOCKET) {
1845 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1851 /* See the note at doio.c:do_print about filesize limits. --jhi */
1852 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1857 else if (SP > MARK) {
1860 sockbuf = SvPVx(*++MARK, mlen);
1861 /* length is really flags */
1862 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1863 length, (struct sockaddr *)sockbuf, mlen);
1866 /* length is really flags */
1867 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1870 DIE(aTHX_ PL_no_sock_func, "send");
1876 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1877 #if Size_t_size > IVSIZE
1891 return pp_sysread();
1902 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1904 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1906 if (io && !IoIFP(io)) {
1907 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1909 IoFLAGS(io) &= ~IOf_START;
1910 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1911 sv_setpvn(GvSV(gv), "-", 1);
1912 SvSETMAGIC(GvSV(gv));
1914 else if (!nextargv(gv))
1919 gv = PL_last_in_gv; /* eof */
1922 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1924 if (gv && (io = GvIO(gv))
1925 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1928 XPUSHs(SvTIED_obj((SV*)io, mg));
1931 call_method("EOF", G_SCALAR);
1937 PUSHs(boolSV(!gv || do_eof(gv)));
1951 gv = PL_last_in_gv = (GV*)POPs;
1953 if (gv && (io = GvIO(gv))
1954 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1957 XPUSHs(SvTIED_obj((SV*)io, mg));
1960 call_method("TELL", G_SCALAR);
1966 #if LSEEKSIZE > IVSIZE
1967 PUSHn( do_tell(gv) );
1969 PUSHi( do_tell(gv) );
1976 return pp_sysseek();
1985 #if LSEEKSIZE > IVSIZE
1986 Off_t offset = (Off_t)SvNVx(POPs);
1988 Off_t offset = (Off_t)SvIVx(POPs);
1992 gv = PL_last_in_gv = (GV*)POPs;
1994 if (gv && (io = GvIO(gv))
1995 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1998 XPUSHs(SvTIED_obj((SV*)io, mg));
1999 #if LSEEKSIZE > IVSIZE
2000 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2002 XPUSHs(sv_2mortal(newSViv(offset)));
2004 XPUSHs(sv_2mortal(newSViv(whence)));
2007 call_method("SEEK", G_SCALAR);
2013 if (PL_op->op_type == OP_SEEK)
2014 PUSHs(boolSV(do_seek(gv, offset, whence)));
2016 Off_t sought = do_sysseek(gv, offset, whence);
2018 PUSHs(&PL_sv_undef);
2021 #if LSEEKSIZE > IVSIZE
2026 : newSVpvn(zero_but_true, ZBTLEN);
2027 PUSHs(sv_2mortal(sv));
2036 /* There seems to be no consensus on the length type of truncate()
2037 * and ftruncate(), both off_t and size_t have supporters. In
2038 * general one would think that when using large files, off_t is
2039 * at least as wide as size_t, so using an off_t should be okay. */
2040 /* XXX Configure probe for the length type of *truncate() needed XXX */
2043 #if Off_t_size > IVSIZE
2048 /* Checking for length < 0 is problematic as the type might or
2049 * might not be signed: if it is not, clever compilers will moan. */
2050 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2052 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2059 if (PL_op->op_flags & OPf_SPECIAL) {
2060 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2069 TAINT_PROPER("truncate");
2070 if (!(fp = IoIFP(io))) {
2076 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2078 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2088 if (SvTYPE(sv) == SVt_PVGV) {
2089 tmpgv = (GV*)sv; /* *main::FRED for example */
2090 goto do_ftruncate_gv;
2092 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2093 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2094 goto do_ftruncate_gv;
2096 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2097 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2098 goto do_ftruncate_io;
2101 name = SvPV(sv, n_a);
2102 TAINT_PROPER("truncate");
2104 if (truncate(name, len) < 0)
2110 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2113 if (my_chsize(tmpfd, len) < 0)
2115 PerlLIO_close(tmpfd);
2124 SETERRNO(EBADF,RMS_IFI);
2128 DIE(aTHX_ "truncate not implemented");
2141 unsigned int func = POPu;
2142 int optype = PL_op->op_type;
2146 IO *io = gv ? GvIOn(gv) : 0;
2148 if (!io || !argsv || !IoIFP(io)) {
2149 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2150 report_evil_fh(gv, io, PL_op->op_type);
2151 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2155 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2158 s = SvPV_force(argsv, len);
2159 need = IOCPARM_LEN(func);
2161 s = Sv_Grow(argsv, need + 1);
2162 SvCUR_set(argsv, need);
2165 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2168 retval = SvIV(argsv);
2169 s = INT2PTR(char*,retval); /* ouch */
2172 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2174 if (optype == OP_IOCTL)
2176 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2178 DIE(aTHX_ "ioctl is not implemented");
2182 DIE(aTHX_ "fcntl is not implemented");
2184 #if defined(OS2) && defined(__EMX__)
2185 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2187 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2191 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2193 if (s[SvCUR(argsv)] != 17)
2194 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2196 s[SvCUR(argsv)] = 0; /* put our null back */
2197 SvSETMAGIC(argsv); /* Assume it has changed */
2206 PUSHp(zero_but_true, ZBTLEN);
2227 if (gv && (io = GvIO(gv)))
2234 (void)PerlIO_flush(fp);
2235 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2238 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2239 report_evil_fh(gv, io, PL_op->op_type);
2241 SETERRNO(EBADF,RMS_IFI);
2246 DIE(aTHX_ PL_no_func, "flock()");
2258 int protocol = POPi;
2264 io = gv ? GvIOn(gv) : NULL;
2267 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2268 report_evil_fh(gv, io, PL_op->op_type);
2270 do_close(gv, FALSE);
2271 SETERRNO(EBADF,LIB_INVARG);
2276 do_close(gv, FALSE);
2278 TAINT_PROPER("socket");
2279 fd = PerlSock_socket(domain, type, protocol);
2282 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2283 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2284 IoTYPE(io) = IoTYPE_SOCKET;
2285 if (!IoIFP(io) || !IoOFP(io)) {
2286 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2287 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2288 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2291 #if defined(HAS_FCNTL) && defined(F_SETFD)
2292 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2296 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2301 DIE(aTHX_ PL_no_sock_func, "socket");
2307 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2313 int protocol = POPi;
2320 io1 = gv1 ? GvIOn(gv1) : NULL;
2321 io2 = gv2 ? GvIOn(gv2) : NULL;
2322 if (!gv1 || !gv2 || !io1 || !io2) {
2323 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2325 report_evil_fh(gv1, io1, PL_op->op_type);
2327 report_evil_fh(gv1, io2, PL_op->op_type);
2330 do_close(gv1, FALSE);
2332 do_close(gv2, FALSE);
2337 do_close(gv1, FALSE);
2339 do_close(gv2, FALSE);
2341 TAINT_PROPER("socketpair");
2342 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2344 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2345 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2346 IoTYPE(io1) = IoTYPE_SOCKET;
2347 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2348 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2349 IoTYPE(io2) = IoTYPE_SOCKET;
2350 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2351 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2352 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2353 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2354 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2355 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2356 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2359 #if defined(HAS_FCNTL) && defined(F_SETFD)
2360 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2361 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2366 DIE(aTHX_ PL_no_sock_func, "socketpair");
2374 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2375 extern void GETPRIVMODE();
2376 extern void GETUSERMODE();
2381 register IO *io = GvIOn(gv);
2385 int mpeprivmode = 0;
2388 if (!io || !IoIFP(io))
2391 addr = SvPV(addrsv, len);
2392 TAINT_PROPER("bind");
2393 #ifdef MPE /* Deal with MPE bind() peculiarities */
2394 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2395 /* The address *MUST* stupidly be zero. */
2396 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2397 /* PRIV mode is required to bind() to ports < 1024. */
2398 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2399 ((struct sockaddr_in *)addr)->sin_port > 0) {
2400 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2405 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2406 (struct sockaddr *)addr, len) >= 0)
2409 #ifdef MPE /* Switch back to USER mode */
2420 if (ckWARN(WARN_CLOSED))
2421 report_evil_fh(gv, io, PL_op->op_type);
2422 SETERRNO(EBADF,SS_IVCHAN);
2425 DIE(aTHX_ PL_no_sock_func, "bind");
2436 register IO *io = GvIOn(gv);
2439 if (!io || !IoIFP(io))
2442 addr = SvPV(addrsv, len);
2443 TAINT_PROPER("connect");
2444 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2450 if (ckWARN(WARN_CLOSED))
2451 report_evil_fh(gv, io, PL_op->op_type);
2452 SETERRNO(EBADF,SS_IVCHAN);
2455 DIE(aTHX_ PL_no_sock_func, "connect");
2465 register IO *io = gv ? GvIOn(gv) : NULL;
2467 if (!gv || !io || !IoIFP(io))
2470 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2476 if (ckWARN(WARN_CLOSED))
2477 report_evil_fh(gv, io, PL_op->op_type);
2478 SETERRNO(EBADF,SS_IVCHAN);
2481 DIE(aTHX_ PL_no_sock_func, "listen");
2493 char namebuf[MAXPATHLEN];
2494 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2495 Sock_size_t len = sizeof (struct sockaddr_in);
2497 Sock_size_t len = sizeof namebuf;
2510 if (!gstio || !IoIFP(gstio))
2514 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2518 do_close(ngv, FALSE);
2519 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2520 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2521 IoTYPE(nstio) = IoTYPE_SOCKET;
2522 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2523 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2524 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2525 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2528 #if defined(HAS_FCNTL) && defined(F_SETFD)
2529 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2533 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2534 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2536 #ifdef __SCO_VERSION__
2537 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2540 PUSHp(namebuf, len);
2544 if (ckWARN(WARN_CLOSED))
2545 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2546 SETERRNO(EBADF,SS_IVCHAN);
2552 DIE(aTHX_ PL_no_sock_func, "accept");
2562 register IO *io = GvIOn(gv);
2564 if (!io || !IoIFP(io))
2567 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2571 if (ckWARN(WARN_CLOSED))
2572 report_evil_fh(gv, io, PL_op->op_type);
2573 SETERRNO(EBADF,SS_IVCHAN);
2576 DIE(aTHX_ PL_no_sock_func, "shutdown");
2583 return pp_ssockopt();
2585 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2593 int optype = PL_op->op_type;
2596 unsigned int optname;
2602 if (optype == OP_GSOCKOPT)
2603 sv = sv_2mortal(NEWSV(22, 257));
2606 optname = (unsigned int) POPi;
2607 lvl = (unsigned int) POPi;
2611 if (!io || !IoIFP(io))
2614 fd = PerlIO_fileno(IoIFP(io));
2618 (void)SvPOK_only(sv);
2622 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2637 aint = (int)SvIV(sv);
2641 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2650 if (ckWARN(WARN_CLOSED))
2651 report_evil_fh(gv, io, optype);
2652 SETERRNO(EBADF,SS_IVCHAN);
2657 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2664 return pp_getpeername();
2666 DIE(aTHX_ PL_no_sock_func, "getsockname");
2674 int optype = PL_op->op_type;
2678 register IO *io = GvIOn(gv);
2681 if (!io || !IoIFP(io))
2684 sv = sv_2mortal(NEWSV(22, 257));
2685 (void)SvPOK_only(sv);
2689 fd = PerlIO_fileno(IoIFP(io));
2691 case OP_GETSOCKNAME:
2692 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2695 case OP_GETPEERNAME:
2696 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2698 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2700 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";
2701 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2702 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2703 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2704 sizeof(u_short) + sizeof(struct in_addr))) {
2711 #ifdef BOGUS_GETNAME_RETURN
2712 /* Interactive Unix, getpeername() and getsockname()
2713 does not return valid namelen */
2714 if (len == BOGUS_GETNAME_RETURN)
2715 len = sizeof(struct sockaddr);
2723 if (ckWARN(WARN_CLOSED))
2724 report_evil_fh(gv, io, optype);
2725 SETERRNO(EBADF,SS_IVCHAN);
2730 DIE(aTHX_ PL_no_sock_func, "getpeername");
2749 if (PL_op->op_flags & OPf_REF) {
2751 if (PL_op->op_type == OP_LSTAT) {
2752 if (gv != PL_defgv) {
2753 if (ckWARN(WARN_IO))
2754 Perl_warner(aTHX_ packWARN(WARN_IO),
2755 "lstat() on filehandle %s", GvENAME(gv));
2756 } else if (PL_laststype != OP_LSTAT)
2757 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2761 if (gv != PL_defgv) {
2762 PL_laststype = OP_STAT;
2764 sv_setpv(PL_statname, "");
2765 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2766 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2768 if (PL_laststatval < 0) {
2769 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2770 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2776 if (SvTYPE(sv) == SVt_PVGV) {
2780 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2782 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2783 Perl_warner(aTHX_ packWARN(WARN_IO),
2784 "lstat() on filehandle %s", GvENAME(gv));
2787 sv_setpv(PL_statname, SvPV(sv,n_a));
2790 PL_laststype = PL_op->op_type;
2791 if (PL_op->op_type == OP_LSTAT)
2792 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2795 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2796 if (PL_laststatval < 0) {
2797 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2798 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2804 if (gimme != G_ARRAY) {
2805 if (gimme != G_VOID)
2806 XPUSHs(boolSV(max));
2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2814 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2815 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2816 #if Uid_t_size > IVSIZE
2817 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2819 # if Uid_t_sign <= 0
2820 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2822 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2825 #if Gid_t_size > IVSIZE
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2828 # if Gid_t_sign <= 0
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2834 #ifdef USE_STAT_RDEV
2835 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
2839 #if Off_t_size > IVSIZE
2840 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2842 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2845 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2846 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2847 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2849 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2850 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2853 #ifdef USE_STAT_BLOCKS
2854 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2855 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2857 PUSHs(sv_2mortal(newSVpvn("", 0)));
2858 PUSHs(sv_2mortal(newSVpvn("", 0)));
2868 #if defined(HAS_ACCESS) && defined(R_OK)
2870 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2871 result = access(POPpx, R_OK);
2886 if (cando(S_IRUSR, 0, &PL_statcache))
2895 #if defined(HAS_ACCESS) && defined(W_OK)
2897 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2898 result = access(POPpx, W_OK);
2913 if (cando(S_IWUSR, 0, &PL_statcache))
2922 #if defined(HAS_ACCESS) && defined(X_OK)
2924 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2925 result = access(POPpx, X_OK);
2940 if (cando(S_IXUSR, 0, &PL_statcache))
2949 #ifdef PERL_EFF_ACCESS_R_OK
2951 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2952 result = PERL_EFF_ACCESS_R_OK(POPpx);
2967 if (cando(S_IRUSR, 1, &PL_statcache))
2976 #ifdef PERL_EFF_ACCESS_W_OK
2978 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2979 result = PERL_EFF_ACCESS_W_OK(POPpx);
2994 if (cando(S_IWUSR, 1, &PL_statcache))
3003 #ifdef PERL_EFF_ACCESS_X_OK
3005 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3006 result = PERL_EFF_ACCESS_X_OK(POPpx);
3021 if (cando(S_IXUSR, 1, &PL_statcache))
3028 I32 result = my_stat();
3037 return pp_ftrowned();
3042 I32 result = my_stat();
3046 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3054 I32 result = my_stat();
3058 if (PL_statcache.st_size == 0)
3065 I32 result = my_stat();
3069 #if Off_t_size > IVSIZE
3070 PUSHn(PL_statcache.st_size);
3072 PUSHi(PL_statcache.st_size);
3079 I32 result = my_stat();
3083 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3089 I32 result = my_stat();
3093 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3099 I32 result = my_stat();
3103 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3109 I32 result = my_stat();
3113 if (S_ISSOCK(PL_statcache.st_mode))
3120 I32 result = my_stat();
3124 if (S_ISCHR(PL_statcache.st_mode))
3131 I32 result = my_stat();
3135 if (S_ISBLK(PL_statcache.st_mode))
3142 I32 result = my_stat();
3146 if (S_ISREG(PL_statcache.st_mode))
3153 I32 result = my_stat();
3157 if (S_ISDIR(PL_statcache.st_mode))
3164 I32 result = my_stat();
3168 if (S_ISFIFO(PL_statcache.st_mode))
3175 I32 result = my_lstat();
3179 if (S_ISLNK(PL_statcache.st_mode))
3188 I32 result = my_stat();
3192 if (PL_statcache.st_mode & S_ISUID)
3202 I32 result = my_stat();
3206 if (PL_statcache.st_mode & S_ISGID)
3216 I32 result = my_stat();
3220 if (PL_statcache.st_mode & S_ISVTX)
3231 char *tmps = Nullch;
3234 if (PL_op->op_flags & OPf_REF)
3236 else if (isGV(TOPs))
3238 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3239 gv = (GV*)SvRV(POPs);
3241 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3243 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3244 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3245 else if (tmps && isDIGIT(*tmps))
3249 if (PerlLIO_isatty(fd))
3254 #if defined(atarist) /* this will work with atariST. Configure will
3255 make guesses for other systems. */
3256 # define FILE_base(f) ((f)->_base)
3257 # define FILE_ptr(f) ((f)->_ptr)
3258 # define FILE_cnt(f) ((f)->_cnt)
3259 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3269 register STDCHAR *s;
3276 if (PL_op->op_flags & OPf_REF)
3278 else if (isGV(TOPs))
3280 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3281 gv = (GV*)SvRV(POPs);
3287 if (gv == PL_defgv) {
3289 io = GvIO(PL_statgv);
3292 goto really_filename;
3297 PL_laststatval = -1;
3298 sv_setpv(PL_statname, "");
3299 io = GvIO(PL_statgv);
3301 if (io && IoIFP(io)) {
3302 if (! PerlIO_has_base(IoIFP(io)))
3303 DIE(aTHX_ "-T and -B not implemented on filehandles");
3304 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3305 if (PL_laststatval < 0)
3307 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3308 if (PL_op->op_type == OP_FTTEXT)
3313 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3314 i = PerlIO_getc(IoIFP(io));
3316 (void)PerlIO_ungetc(IoIFP(io),i);
3318 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3320 len = PerlIO_get_bufsiz(IoIFP(io));
3321 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3322 /* sfio can have large buffers - limit to 512 */
3327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3329 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3331 SETERRNO(EBADF,RMS_IFI);
3339 PL_laststatval = -1;
3340 PL_laststype = OP_STAT;
3341 sv_setpv(PL_statname, SvPV(sv, n_a));
3342 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3343 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3344 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3347 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3348 if (PL_laststatval < 0) {
3349 (void)PerlIO_close(fp);
3352 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3353 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3354 (void)PerlIO_close(fp);
3356 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3357 RETPUSHNO; /* special case NFS directories */
3358 RETPUSHYES; /* null file is anything */
3363 /* now scan s to look for textiness */
3364 /* XXX ASCII dependent code */
3366 #if defined(DOSISH) || defined(USEMYBINMODE)
3367 /* ignore trailing ^Z on short files */
3368 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3372 for (i = 0; i < len; i++, s++) {
3373 if (!*s) { /* null never allowed in text */
3378 else if (!(isPRINT(*s) || isSPACE(*s)))
3381 else if (*s & 128) {
3383 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3386 /* utf8 characters don't count as odd */
3387 if (UTF8_IS_START(*s)) {
3388 int ulen = UTF8SKIP(s);
3389 if (ulen < len - i) {
3391 for (j = 1; j < ulen; j++) {
3392 if (!UTF8_IS_CONTINUATION(s[j]))
3395 --ulen; /* loop does extra increment */
3405 *s != '\n' && *s != '\r' && *s != '\b' &&
3406 *s != '\t' && *s != '\f' && *s != 27)
3411 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3436 if( !tmps || !*tmps ) {
3437 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3438 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3440 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3445 deprecate("chdir('') or chdir(undef) as chdir()");
3446 tmps = SvPV(*svp, n_a);
3450 TAINT_PROPER("chdir");
3455 TAINT_PROPER("chdir");
3456 PUSHi( PerlDir_chdir(tmps) >= 0 );
3458 /* Clear the DEFAULT element of ENV so we'll get the new value
3460 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3468 dSP; dMARK; dTARGET;
3469 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3475 DIE(aTHX_ PL_no_func, "chown");
3485 TAINT_PROPER("chroot");
3486 PUSHi( chroot(tmps) >= 0 );
3489 DIE(aTHX_ PL_no_func, "chroot");
3495 dSP; dMARK; dTARGET;
3497 value = (I32)apply(PL_op->op_type, MARK, SP);
3505 dSP; dMARK; dTARGET;
3507 value = (I32)apply(PL_op->op_type, MARK, SP);
3515 dSP; dMARK; dTARGET;
3517 value = (I32)apply(PL_op->op_type, MARK, SP);
3529 char *tmps2 = POPpx;
3530 char *tmps = SvPV(TOPs, n_a);
3531 TAINT_PROPER("rename");
3533 anum = PerlLIO_rename(tmps, tmps2);
3535 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3536 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3539 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3540 (void)UNLINK(tmps2);
3541 if (!(anum = link(tmps, tmps2)))
3542 anum = UNLINK(tmps);
3555 char *tmps2 = POPpx;
3556 char *tmps = SvPV(TOPs, n_a);
3557 TAINT_PROPER("link");
3558 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3561 DIE(aTHX_ PL_no_func, "link");
3570 char *tmps2 = POPpx;
3571 char *tmps = SvPV(TOPs, n_a);
3572 TAINT_PROPER("symlink");
3573 SETi( symlink(tmps, tmps2) >= 0 );
3576 DIE(aTHX_ PL_no_func, "symlink");
3586 char buf[MAXPATHLEN];
3590 #ifndef INCOMPLETE_TAINTS
3594 len = readlink(tmps, buf, sizeof(buf) - 1);
3602 RETSETUNDEF; /* just pretend it's a normal file */
3606 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3608 S_dooneliner(pTHX_ char *cmd, char *filename)
3610 char *save_filename = filename;
3616 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3617 strcpy(cmdline, cmd);
3618 strcat(cmdline, " ");
3619 for (s = cmdline + strlen(cmdline); *filename; ) {
3624 myfp = PerlProc_popen(cmdline, "r");
3628 SV *tmpsv = sv_newmortal();
3629 /* Need to save/restore 'PL_rs' ?? */
3630 s = sv_gets(tmpsv, myfp, 0);
3631 (void)PerlProc_pclose(myfp);
3635 #ifdef HAS_SYS_ERRLIST
3640 /* you don't see this */
3642 #ifdef HAS_SYS_ERRLIST
3650 if (instr(s, errmsg)) {
3657 #define EACCES EPERM
3659 if (instr(s, "cannot make"))
3660 SETERRNO(EEXIST,RMS_FEX);
3661 else if (instr(s, "existing file"))
3662 SETERRNO(EEXIST,RMS_FEX);
3663 else if (instr(s, "ile exists"))
3664 SETERRNO(EEXIST,RMS_FEX);
3665 else if (instr(s, "non-exist"))
3666 SETERRNO(ENOENT,RMS_FNF);
3667 else if (instr(s, "does not exist"))
3668 SETERRNO(ENOENT,RMS_FNF);
3669 else if (instr(s, "not empty"))
3670 SETERRNO(EBUSY,SS_DEVOFFLINE);
3671 else if (instr(s, "cannot access"))
3672 SETERRNO(EACCES,RMS_PRV);
3674 SETERRNO(EPERM,RMS_PRV);
3677 else { /* some mkdirs return no failure indication */
3678 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3679 if (PL_op->op_type == OP_RMDIR)
3684 SETERRNO(EACCES,RMS_PRV); /* a guess */
3693 /* This macro removes trailing slashes from a directory name.
3694 * Different operating and file systems take differently to
3695 * trailing slashes. According to POSIX 1003.1 1996 Edition
3696 * any number of trailing slashes should be allowed.
3697 * Thusly we snip them away so that even non-conforming
3698 * systems are happy.
3699 * We should probably do this "filtering" for all
3700 * the functions that expect (potentially) directory names:
3701 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3702 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3704 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3705 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3708 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3709 (tmps) = savepvn((tmps), (len)); \
3729 TRIMSLASHES(tmps,len,copy);
3731 TAINT_PROPER("mkdir");
3733 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3735 SETi( dooneliner("mkdir", tmps) );
3736 oldumask = PerlLIO_umask(0);
3737 PerlLIO_umask(oldumask);
3738 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3752 TRIMSLASHES(tmps,len,copy);
3753 TAINT_PROPER("rmdir");
3755 SETi( PerlDir_rmdir(tmps) >= 0 );
3757 SETi( dooneliner("rmdir", tmps) );
3764 /* Directory calls. */
3768 #if defined(Direntry_t) && defined(HAS_READDIR)
3771 char *dirname = POPpx;
3773 register IO *io = GvIOn(gv);
3779 PerlDir_close(IoDIRP(io));
3780 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3786 SETERRNO(EBADF,RMS_DIR);
3789 DIE(aTHX_ PL_no_dir_func, "opendir");
3795 #if defined(Direntry_t) && defined(HAS_READDIR)
3797 #if !defined(I_DIRENT) && !defined(VMS)
3798 Direntry_t *readdir (DIR *);
3800 register Direntry_t *dp;
3802 register IO *io = GvIOn(gv);
3805 if (!io || !IoDIRP(io))
3808 if (GIMME == G_ARRAY) {
3810 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3812 sv = newSVpvn(dp->d_name, dp->d_namlen);
3814 sv = newSVpv(dp->d_name, 0);
3816 #ifndef INCOMPLETE_TAINTS
3817 if (!(IoFLAGS(io) & IOf_UNTAINT))
3820 XPUSHs(sv_2mortal(sv));
3824 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3827 sv = newSVpvn(dp->d_name, dp->d_namlen);
3829 sv = newSVpv(dp->d_name, 0);
3831 #ifndef INCOMPLETE_TAINTS
3832 if (!(IoFLAGS(io) & IOf_UNTAINT))
3835 XPUSHs(sv_2mortal(sv));
3841 SETERRNO(EBADF,RMS_ISI);
3842 if (GIMME == G_ARRAY)
3847 DIE(aTHX_ PL_no_dir_func, "readdir");
3853 #if defined(HAS_TELLDIR) || defined(telldir)
3855 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3856 /* XXX netbsd still seemed to.
3857 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3858 --JHI 1999-Feb-02 */
3859 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3860 long telldir (DIR *);
3863 register IO *io = GvIOn(gv);
3865 if (!io || !IoDIRP(io))
3868 PUSHi( PerlDir_tell(IoDIRP(io)) );
3872 SETERRNO(EBADF,RMS_ISI);
3875 DIE(aTHX_ PL_no_dir_func, "telldir");
3881 #if defined(HAS_SEEKDIR) || defined(seekdir)
3885 register IO *io = GvIOn(gv);
3887 if (!io || !IoDIRP(io))
3890 (void)PerlDir_seek(IoDIRP(io), along);
3895 SETERRNO(EBADF,RMS_ISI);
3898 DIE(aTHX_ PL_no_dir_func, "seekdir");
3904 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3907 register IO *io = GvIOn(gv);
3909 if (!io || !IoDIRP(io))
3912 (void)PerlDir_rewind(IoDIRP(io));
3916 SETERRNO(EBADF,RMS_ISI);
3919 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3925 #if defined(Direntry_t) && defined(HAS_READDIR)
3928 register IO *io = GvIOn(gv);
3930 if (!io || !IoDIRP(io))
3933 #ifdef VOID_CLOSEDIR
3934 PerlDir_close(IoDIRP(io));
3936 if (PerlDir_close(IoDIRP(io)) < 0) {
3937 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3946 SETERRNO(EBADF,RMS_IFI);
3949 DIE(aTHX_ PL_no_dir_func, "closedir");
3953 /* Process control. */
3963 PERL_FLUSHALL_FOR_CHILD;
3964 childpid = PerlProc_fork();
3969 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3970 SvREADONLY_off(GvSV(tmpgv));
3971 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3972 SvREADONLY_on(GvSV(tmpgv));
3974 #ifdef THREADS_HAVE_PIDS
3975 PL_ppid = (IV)getppid();
3977 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3982 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3987 PERL_FLUSHALL_FOR_CHILD;
3988 childpid = PerlProc_fork();
3994 DIE(aTHX_ PL_no_func, "fork");
4001 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4006 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4007 childpid = wait4pid(-1, &argflags, 0);
4009 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4014 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4015 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4016 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4018 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4023 DIE(aTHX_ PL_no_func, "wait");
4029 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4037 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4038 childpid = wait4pid(childpid, &argflags, optype);
4040 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4045 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4046 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4047 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4049 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4054 DIE(aTHX_ PL_no_func, "waitpid");
4060 dSP; dMARK; dORIGMARK; dTARGET;
4068 while (++MARK <= SP) {
4069 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4074 TAINT_PROPER("system");
4076 PERL_FLUSHALL_FOR_CHILD;
4077 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4082 if (PerlProc_pipe(pp) >= 0)
4084 while ((childpid = PerlProc_fork()) == -1) {
4085 if (errno != EAGAIN) {
4090 PerlLIO_close(pp[0]);
4091 PerlLIO_close(pp[1]);
4098 Sigsave_t ihand,qhand; /* place to save signals during system() */
4102 PerlLIO_close(pp[1]);
4104 rsignal_save(SIGINT, SIG_IGN, &ihand);
4105 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4108 result = wait4pid(childpid, &status, 0);
4109 } while (result == -1 && errno == EINTR);
4111 (void)rsignal_restore(SIGINT, &ihand);
4112 (void)rsignal_restore(SIGQUIT, &qhand);
4114 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4115 do_execfree(); /* free any memory child malloced on fork */
4121 while (n < sizeof(int)) {
4122 n1 = PerlLIO_read(pp[0],
4123 (void*)(((char*)&errkid)+n),
4129 PerlLIO_close(pp[0]);
4130 if (n) { /* Error */
4131 if (n != sizeof(int))
4132 DIE(aTHX_ "panic: kid popen errno read");
4133 errno = errkid; /* Propagate errno from kid */
4134 STATUS_CURRENT = -1;
4137 PUSHi(STATUS_CURRENT);
4141 PerlLIO_close(pp[0]);
4142 #if defined(HAS_FCNTL) && defined(F_SETFD)
4143 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4146 if (PL_op->op_flags & OPf_STACKED) {
4147 SV *really = *++MARK;
4148 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4150 else if (SP - MARK != 1)
4151 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4153 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4157 #else /* ! FORK or VMS or OS/2 */
4160 if (PL_op->op_flags & OPf_STACKED) {
4161 SV *really = *++MARK;
4162 # if defined(WIN32) || defined(OS2)
4163 value = (I32)do_aspawn(really, MARK, SP);
4165 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4168 else if (SP - MARK != 1) {
4169 # if defined(WIN32) || defined(OS2)
4170 value = (I32)do_aspawn(Nullsv, MARK, SP);
4172 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4176 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4178 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4180 STATUS_NATIVE_SET(value);
4183 PUSHi(result ? value : STATUS_CURRENT);
4184 #endif /* !FORK or VMS */
4190 dSP; dMARK; dORIGMARK; dTARGET;
4196 while (++MARK <= SP) {
4197 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4202 TAINT_PROPER("exec");
4204 PERL_FLUSHALL_FOR_CHILD;
4205 if (PL_op->op_flags & OPf_STACKED) {
4206 SV *really = *++MARK;
4207 value = (I32)do_aexec(really, MARK, SP);
4209 else if (SP - MARK != 1)
4211 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4215 (void ) do_aspawn(Nullsv, MARK, SP);
4219 value = (I32)do_aexec(Nullsv, MARK, SP);
4224 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4227 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4230 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4243 dSP; dMARK; dTARGET;
4245 value = (I32)apply(PL_op->op_type, MARK, SP);
4250 DIE(aTHX_ PL_no_func, "kill");
4258 # ifdef THREADS_HAVE_PIDS
4261 XPUSHi( getppid() );
4265 DIE(aTHX_ PL_no_func, "getppid");
4281 pgrp = (I32)BSD_GETPGRP(pid);
4283 if (pid != 0 && pid != PerlProc_getpid())
4284 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4290 DIE(aTHX_ PL_no_func, "getpgrp()");
4309 TAINT_PROPER("setpgrp");
4311 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4313 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4314 || (pid != 0 && pid != PerlProc_getpid()))
4316 DIE(aTHX_ "setpgrp can't take arguments");
4318 SETi( setpgrp() >= 0 );
4319 #endif /* USE_BSDPGRP */
4322 DIE(aTHX_ PL_no_func, "setpgrp()");
4328 #ifdef HAS_GETPRIORITY
4332 SETi( getpriority(which, who) );
4335 DIE(aTHX_ PL_no_func, "getpriority()");
4341 #ifdef HAS_SETPRIORITY
4346 TAINT_PROPER("setpriority");
4347 SETi( setpriority(which, who, niceval) >= 0 );
4350 DIE(aTHX_ PL_no_func, "setpriority()");
4360 XPUSHn( time(Null(Time_t*)) );
4362 XPUSHi( time(Null(Time_t*)) );
4373 (void)PerlProc_times(&PL_timesbuf);
4375 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4376 /* struct tms, though same data */
4380 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4381 if (GIMME == G_ARRAY) {
4382 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4383 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4384 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4390 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4392 if (GIMME == G_ARRAY) {
4393 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4394 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4395 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4399 DIE(aTHX_ "times not implemented");
4401 #endif /* HAS_TIMES */
4414 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4415 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4416 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4422 when = (Time_t)SvNVx(POPs);
4424 when = (Time_t)SvIVx(POPs);
4427 if (PL_op->op_type == OP_LOCALTIME)
4428 tmbuf = localtime(&when);
4430 tmbuf = gmtime(&when);
4432 if (GIMME != G_ARRAY) {
4438 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4439 dayname[tmbuf->tm_wday],
4440 monname[tmbuf->tm_mon],
4445 tmbuf->tm_year + 1900);
4446 PUSHs(sv_2mortal(tsv));
4451 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4452 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4453 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4454 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4455 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4456 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4457 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4458 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4459 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4470 anum = alarm((unsigned int)anum);
4477 DIE(aTHX_ PL_no_func, "alarm");
4488 (void)time(&lasttime);
4493 PerlProc_sleep((unsigned int)duration);
4496 XPUSHi(when - lasttime);
4500 /* Shared memory. */
4514 return pp_shmwrite();
4519 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4520 dSP; dMARK; dTARGET;
4521 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4530 /* Message passing. */
4544 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4545 dSP; dMARK; dTARGET;
4546 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4557 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4558 dSP; dMARK; dTARGET;
4559 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4572 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4573 dSP; dMARK; dTARGET;
4574 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4581 DIE(aTHX_ "System V IPC is not implemented on this machine");
4587 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4588 dSP; dMARK; dTARGET;
4589 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4597 PUSHp(zero_but_true, ZBTLEN);
4607 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4608 dSP; dMARK; dTARGET;
4609 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4618 /* Get system info. */
4622 #ifdef HAS_GETHOSTBYNAME
4623 return pp_ghostent();
4625 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4631 #ifdef HAS_GETHOSTBYADDR
4632 return pp_ghostent();
4634 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4640 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4642 I32 which = PL_op->op_type;
4643 register char **elem;
4645 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4646 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4647 struct hostent *gethostbyname(Netdb_name_t);
4648 struct hostent *gethostent(void);
4650 struct hostent *hent;
4655 if (which == OP_GHBYNAME) {
4656 #ifdef HAS_GETHOSTBYNAME
4657 char* name = POPpbytex;
4658 hent = PerlSock_gethostbyname(name);
4660 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4663 else if (which == OP_GHBYADDR) {
4664 #ifdef HAS_GETHOSTBYADDR
4665 int addrtype = POPi;
4668 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4670 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4672 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4676 #ifdef HAS_GETHOSTENT
4677 hent = PerlSock_gethostent();
4679 DIE(aTHX_ PL_no_sock_func, "gethostent");
4682 #ifdef HOST_NOT_FOUND
4684 #ifdef USE_REENTRANT_API
4685 # ifdef USE_GETHOSTENT_ERRNO
4686 h_errno = PL_reentrant_buffer->_gethostent_errno;
4689 STATUS_NATIVE_SET(h_errno);
4693 if (GIMME != G_ARRAY) {
4694 PUSHs(sv = sv_newmortal());
4696 if (which == OP_GHBYNAME) {
4698 sv_setpvn(sv, hent->h_addr, hent->h_length);
4701 sv_setpv(sv, (char*)hent->h_name);
4707 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4708 sv_setpv(sv, (char*)hent->h_name);
4709 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4710 for (elem = hent->h_aliases; elem && *elem; elem++) {
4711 sv_catpv(sv, *elem);
4713 sv_catpvn(sv, " ", 1);
4715 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4716 sv_setiv(sv, (IV)hent->h_addrtype);
4717 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4718 len = hent->h_length;
4719 sv_setiv(sv, (IV)len);
4721 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4722 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4723 sv_setpvn(sv, *elem, len);
4726 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4728 sv_setpvn(sv, hent->h_addr, len);
4733 DIE(aTHX_ PL_no_sock_func, "gethostent");
4739 #ifdef HAS_GETNETBYNAME
4740 return pp_gnetent();
4742 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4748 #ifdef HAS_GETNETBYADDR
4749 return pp_gnetent();
4751 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4757 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4759 I32 which = PL_op->op_type;
4760 register char **elem;
4762 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4763 struct netent *getnetbyaddr(Netdb_net_t, int);
4764 struct netent *getnetbyname(Netdb_name_t);
4765 struct netent *getnetent(void);
4767 struct netent *nent;
4770 if (which == OP_GNBYNAME){
4771 #ifdef HAS_GETNETBYNAME
4772 char *name = POPpbytex;
4773 nent = PerlSock_getnetbyname(name);
4775 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4778 else if (which == OP_GNBYADDR) {
4779 #ifdef HAS_GETNETBYADDR
4780 int addrtype = POPi;
4781 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4782 nent = PerlSock_getnetbyaddr(addr, addrtype);
4784 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4788 #ifdef HAS_GETNETENT
4789 nent = PerlSock_getnetent();
4791 DIE(aTHX_ PL_no_sock_func, "getnetent");
4794 #ifdef HOST_NOT_FOUND
4796 #ifdef USE_REENTRANT_API
4797 # ifdef USE_GETNETENT_ERRNO
4798 h_errno = PL_reentrant_buffer->_getnetent_errno;
4801 STATUS_NATIVE_SET(h_errno);
4806 if (GIMME != G_ARRAY) {
4807 PUSHs(sv = sv_newmortal());
4809 if (which == OP_GNBYNAME)
4810 sv_setiv(sv, (IV)nent->n_net);
4812 sv_setpv(sv, nent->n_name);
4818 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4819 sv_setpv(sv, nent->n_name);
4820 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4821 for (elem = nent->n_aliases; elem && *elem; elem++) {
4822 sv_catpv(sv, *elem);
4824 sv_catpvn(sv, " ", 1);
4826 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4827 sv_setiv(sv, (IV)nent->n_addrtype);
4828 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4829 sv_setiv(sv, (IV)nent->n_net);
4834 DIE(aTHX_ PL_no_sock_func, "getnetent");
4840 #ifdef HAS_GETPROTOBYNAME
4841 return pp_gprotoent();
4843 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4849 #ifdef HAS_GETPROTOBYNUMBER
4850 return pp_gprotoent();
4852 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4858 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4860 I32 which = PL_op->op_type;
4861 register char **elem;
4863 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4864 struct protoent *getprotobyname(Netdb_name_t);
4865 struct protoent *getprotobynumber(int);
4866 struct protoent *getprotoent(void);
4868 struct protoent *pent;
4871 if (which == OP_GPBYNAME) {
4872 #ifdef HAS_GETPROTOBYNAME
4873 char* name = POPpbytex;
4874 pent = PerlSock_getprotobyname(name);
4876 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4879 else if (which == OP_GPBYNUMBER) {
4880 #ifdef HAS_GETPROTOBYNUMBER
4882 pent = PerlSock_getprotobynumber(number);
4884 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4888 #ifdef HAS_GETPROTOENT
4889 pent = PerlSock_getprotoent();
4891 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4895 if (GIMME != G_ARRAY) {
4896 PUSHs(sv = sv_newmortal());
4898 if (which == OP_GPBYNAME)
4899 sv_setiv(sv, (IV)pent->p_proto);
4901 sv_setpv(sv, pent->p_name);
4907 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4908 sv_setpv(sv, pent->p_name);
4909 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4910 for (elem = pent->p_aliases; elem && *elem; elem++) {
4911 sv_catpv(sv, *elem);
4913 sv_catpvn(sv, " ", 1);
4915 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4916 sv_setiv(sv, (IV)pent->p_proto);
4921 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4927 #ifdef HAS_GETSERVBYNAME
4928 return pp_gservent();
4930 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4936 #ifdef HAS_GETSERVBYPORT
4937 return pp_gservent();
4939 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4945 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4947 I32 which = PL_op->op_type;
4948 register char **elem;
4950 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4951 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4952 struct servent *getservbyport(int, Netdb_name_t);
4953 struct servent *getservent(void);
4955 struct servent *sent;
4958 if (which == OP_GSBYNAME) {
4959 #ifdef HAS_GETSERVBYNAME
4960 char *proto = POPpbytex;
4961 char *name = POPpbytex;
4963 if (proto && !*proto)
4966 sent = PerlSock_getservbyname(name, proto);
4968 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4971 else if (which == OP_GSBYPORT) {
4972 #ifdef HAS_GETSERVBYPORT
4973 char *proto = POPpbytex;
4974 unsigned short port = (unsigned short)POPu;
4976 if (proto && !*proto)
4980 port = PerlSock_htons(port);
4982 sent = PerlSock_getservbyport(port, proto);
4984 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4988 #ifdef HAS_GETSERVENT
4989 sent = PerlSock_getservent();
4991 DIE(aTHX_ PL_no_sock_func, "getservent");
4995 if (GIMME != G_ARRAY) {
4996 PUSHs(sv = sv_newmortal());
4998 if (which == OP_GSBYNAME) {
5000 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5002 sv_setiv(sv, (IV)(sent->s_port));
5006 sv_setpv(sv, sent->s_name);
5012 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5013 sv_setpv(sv, sent->s_name);
5014 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5015 for (elem = sent->s_aliases; elem && *elem; elem++) {
5016 sv_catpv(sv, *elem);
5018 sv_catpvn(sv, " ", 1);
5020 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5022 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5024 sv_setiv(sv, (IV)(sent->s_port));
5026 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5027 sv_setpv(sv, sent->s_proto);
5032 DIE(aTHX_ PL_no_sock_func, "getservent");
5038 #ifdef HAS_SETHOSTENT
5040 PerlSock_sethostent(TOPi);
5043 DIE(aTHX_ PL_no_sock_func, "sethostent");
5049 #ifdef HAS_SETNETENT
5051 PerlSock_setnetent(TOPi);
5054 DIE(aTHX_ PL_no_sock_func, "setnetent");
5060 #ifdef HAS_SETPROTOENT
5062 PerlSock_setprotoent(TOPi);
5065 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5071 #ifdef HAS_SETSERVENT
5073 PerlSock_setservent(TOPi);
5076 DIE(aTHX_ PL_no_sock_func, "setservent");
5082 #ifdef HAS_ENDHOSTENT
5084 PerlSock_endhostent();
5088 DIE(aTHX_ PL_no_sock_func, "endhostent");
5094 #ifdef HAS_ENDNETENT
5096 PerlSock_endnetent();
5100 DIE(aTHX_ PL_no_sock_func, "endnetent");
5106 #ifdef HAS_ENDPROTOENT
5108 PerlSock_endprotoent();
5112 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5118 #ifdef HAS_ENDSERVENT
5120 PerlSock_endservent();
5124 DIE(aTHX_ PL_no_sock_func, "endservent");
5133 DIE(aTHX_ PL_no_func, "getpwnam");
5142 DIE(aTHX_ PL_no_func, "getpwuid");
5150 I32 which = PL_op->op_type;
5153 struct passwd *pwent = NULL;
5155 * We currently support only the SysV getsp* shadow password interface.
5156 * The interface is declared in <shadow.h> and often one needs to link
5157 * with -lsecurity or some such.
5158 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5161 * AIX getpwnam() is clever enough to return the encrypted password
5162 * only if the caller (euid?) is root.
5164 * There are at least three other shadow password APIs. Many platforms
5165 * seem to contain more than one interface for accessing the shadow
5166 * password databases, possibly for compatibility reasons.
5167 * The getsp*() is by far he simplest one, the other two interfaces
5168 * are much more complicated, but also very similar to each other.
5173 * struct pr_passwd *getprpw*();
5174 * The password is in
5175 * char getprpw*(...).ufld.fd_encrypt[]
5176 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5181 * struct es_passwd *getespw*();
5182 * The password is in
5183 * char *(getespw*(...).ufld.fd_encrypt)
5184 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5186 * <userpw.h> /* AIX */
5187 * struct userpw *getuserpw();
5188 * The password is in
5189 * char *(getuserpw(...)).spw_upw_passwd
5190 * (but the de facto standard getpwnam() should work okay)
5192 * Mention I_PROT here so that Configure probes for it.
5194 * In HP-UX for getprpw*() the manual page claims that one should include
5195 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5196 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5197 * and pp_sys.c already includes <shadow.h> if there is such.
5199 * Note that <sys/security.h> is already probed for, but currently
5200 * it is only included in special cases.
5202 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5203 * be preferred interface, even though also the getprpw*() interface
5204 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5205 * One also needs to call set_auth_parameters() in main() before
5206 * doing anything else, whether one is using getespw*() or getprpw*().
5208 * Note that accessing the shadow databases can be magnitudes
5209 * slower than accessing the standard databases.
5217 char* name = POPpbytex;
5218 pwent = getpwnam(name);
5224 pwent = getpwuid(uid);
5228 # ifdef HAS_GETPWENT
5230 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5231 if (pwent) pwent = getpwnam(pwent->pw_name);
5234 DIE(aTHX_ PL_no_func, "getpwent");
5240 if (GIMME != G_ARRAY) {
5241 PUSHs(sv = sv_newmortal());
5243 if (which == OP_GPWNAM)
5244 # if Uid_t_sign <= 0
5245 sv_setiv(sv, (IV)pwent->pw_uid);
5247 sv_setuv(sv, (UV)pwent->pw_uid);
5250 sv_setpv(sv, pwent->pw_name);
5256 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5257 sv_setpv(sv, pwent->pw_name);
5259 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5261 /* If we have getspnam(), we try to dig up the shadow
5262 * password. If we are underprivileged, the shadow
5263 * interface will set the errno to EACCES or similar,
5264 * and return a null pointer. If this happens, we will
5265 * use the dummy password (usually "*" or "x") from the
5266 * standard password database.
5268 * In theory we could skip the shadow call completely
5269 * if euid != 0 but in practice we cannot know which
5270 * security measures are guarding the shadow databases
5271 * on a random platform.
5273 * Resist the urge to use additional shadow interfaces.
5274 * Divert the urge to writing an extension instead.
5277 /* Some AIX setups falsely(?) detect some getspnam(), which
5278 * has a different API than the Solaris/IRIX one. */
5279 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5281 struct spwd *spwent;
5282 int saverrno; /* Save and restore errno so that
5283 * underprivileged attempts seem
5284 * to have never made the unsccessful
5285 * attempt to retrieve the shadow password. */
5288 spwent = getspnam(pwent->pw_name);
5290 if (spwent && spwent->sp_pwdp)
5291 sv_setpv(sv, spwent->sp_pwdp);
5295 if (!SvPOK(sv)) /* Use the standard password, then. */
5296 sv_setpv(sv, pwent->pw_passwd);
5299 # ifndef INCOMPLETE_TAINTS
5300 /* passwd is tainted because user himself can diddle with it.
5301 * admittedly not much and in a very limited way, but nevertheless. */
5305 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5306 # if Uid_t_sign <= 0
5307 sv_setiv(sv, (IV)pwent->pw_uid);
5309 sv_setuv(sv, (UV)pwent->pw_uid);
5312 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5313 # if Uid_t_sign <= 0
5314 sv_setiv(sv, (IV)pwent->pw_gid);
5316 sv_setuv(sv, (UV)pwent->pw_gid);
5318 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5319 * because of the poor interface of the Perl getpw*(),
5320 * not because there's some standard/convention saying so.
5321 * A better interface would have been to return a hash,
5322 * but we are accursed by our history, alas. --jhi. */
5323 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5325 sv_setiv(sv, (IV)pwent->pw_change);
5328 sv_setiv(sv, (IV)pwent->pw_quota);
5331 sv_setpv(sv, pwent->pw_age);
5336 /* pw_class and pw_comment are mutually exclusive--.
5337 * see the above note for pw_change, pw_quota, and pw_age. */
5338 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5340 sv_setpv(sv, pwent->pw_class);
5343 sv_setpv(sv, pwent->pw_comment);
5347 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5349 sv_setpv(sv, pwent->pw_gecos);
5351 # ifndef INCOMPLETE_TAINTS
5352 /* pw_gecos is tainted because user himself can diddle with it. */
5356 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5357 sv_setpv(sv, pwent->pw_dir);
5359 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5360 sv_setpv(sv, pwent->pw_shell);
5361 # ifndef INCOMPLETE_TAINTS
5362 /* pw_shell is tainted because user himself can diddle with it. */
5367 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5368 sv_setiv(sv, (IV)pwent->pw_expire);
5373 DIE(aTHX_ PL_no_func, "getpwent");
5379 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5384 DIE(aTHX_ PL_no_func, "setpwent");
5390 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5395 DIE(aTHX_ PL_no_func, "endpwent");
5404 DIE(aTHX_ PL_no_func, "getgrnam");
5413 DIE(aTHX_ PL_no_func, "getgrgid");
5421 I32 which = PL_op->op_type;
5422 register char **elem;
5424 struct group *grent;
5427 if (which == OP_GGRNAM) {
5428 char* name = POPpbytex;
5429 grent = (struct group *)getgrnam(name);
5431 else if (which == OP_GGRGID) {
5433 grent = (struct group *)getgrgid(gid);
5437 grent = (struct group *)getgrent();
5439 DIE(aTHX_ PL_no_func, "getgrent");
5443 if (GIMME != G_ARRAY) {
5444 PUSHs(sv = sv_newmortal());
5446 if (which == OP_GGRNAM)
5447 sv_setiv(sv, (IV)grent->gr_gid);
5449 sv_setpv(sv, grent->gr_name);
5455 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5456 sv_setpv(sv, grent->gr_name);
5458 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5460 sv_setpv(sv, grent->gr_passwd);
5463 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5464 sv_setiv(sv, (IV)grent->gr_gid);
5466 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5467 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5468 /* In UNICOS/mk (_CRAYMPP) the multithreading
5469 * versions (getgrnam_r, getgrgid_r)
5470 * seem to return an illegal pointer
5471 * as the group members list, gr_mem.
5472 * getgrent() doesn't even have a _r version
5473 * but the gr_mem is poisonous anyway.
5474 * So yes, you cannot get the list of group
5475 * members if building multithreaded in UNICOS/mk. */
5476 for (elem = grent->gr_mem; elem && *elem; elem++) {
5477 sv_catpv(sv, *elem);
5479 sv_catpvn(sv, " ", 1);
5486 DIE(aTHX_ PL_no_func, "getgrent");
5492 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5497 DIE(aTHX_ PL_no_func, "setgrent");
5503 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5508 DIE(aTHX_ PL_no_func, "endgrent");
5518 if (!(tmps = PerlProc_getlogin()))
5520 PUSHp(tmps, strlen(tmps));
5523 DIE(aTHX_ PL_no_func, "getlogin");
5527 /* Miscellaneous. */
5532 dSP; dMARK; dORIGMARK; dTARGET;
5533 register I32 items = SP - MARK;
5534 unsigned long a[20];
5540 while (++MARK <= SP) {
5541 if (SvTAINTED(*MARK)) {
5547 TAINT_PROPER("syscall");
5550 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5551 * or where sizeof(long) != sizeof(char*). But such machines will
5552 * not likely have syscall implemented either, so who cares?
5554 while (++MARK <= SP) {
5555 if (SvNIOK(*MARK) || !i)
5556 a[i++] = SvIV(*MARK);
5557 else if (*MARK == &PL_sv_undef)
5560 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5566 DIE(aTHX_ "Too many args to syscall");
5568 DIE(aTHX_ "Too few args to syscall");
5570 retval = syscall(a[0]);
5573 retval = syscall(a[0],a[1]);
5576 retval = syscall(a[0],a[1],a[2]);
5579 retval = syscall(a[0],a[1],a[2],a[3]);
5582 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5585 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5588 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5591 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5595 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5598 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5601 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],
5614 a[10],a[11],a[12],a[13]);
5616 #endif /* atarist */
5622 DIE(aTHX_ PL_no_func, "syscall");
5626 #ifdef FCNTL_EMULATE_FLOCK
5628 /* XXX Emulate flock() with fcntl().
5629 What's really needed is a good file locking module.
5633 fcntl_emulate_flock(int fd, int operation)
5637 switch (operation & ~LOCK_NB) {
5639 flock.l_type = F_RDLCK;
5642 flock.l_type = F_WRLCK;
5645 flock.l_type = F_UNLCK;
5651 flock.l_whence = SEEK_SET;
5652 flock.l_start = flock.l_len = (Off_t)0;
5654 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5657 #endif /* FCNTL_EMULATE_FLOCK */
5659 #ifdef LOCKF_EMULATE_FLOCK
5661 /* XXX Emulate flock() with lockf(). This is just to increase
5662 portability of scripts. The calls are not completely
5663 interchangeable. What's really needed is a good file
5667 /* The lockf() constants might have been defined in <unistd.h>.
5668 Unfortunately, <unistd.h> causes troubles on some mixed
5669 (BSD/POSIX) systems, such as SunOS 4.1.3.
5671 Further, the lockf() constants aren't POSIX, so they might not be
5672 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5673 just stick in the SVID values and be done with it. Sigh.
5677 # define F_ULOCK 0 /* Unlock a previously locked region */
5680 # define F_LOCK 1 /* Lock a region for exclusive use */
5683 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5686 # define F_TEST 3 /* Test a region for other processes locks */
5690 lockf_emulate_flock(int fd, int operation)
5696 /* flock locks entire file so for lockf we need to do the same */
5698 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5699 if (pos > 0) /* is seekable and needs to be repositioned */
5700 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5701 pos = -1; /* seek failed, so don't seek back afterwards */
5704 switch (operation) {
5706 /* LOCK_SH - get a shared lock */
5708 /* LOCK_EX - get an exclusive lock */
5710 i = lockf (fd, F_LOCK, 0);
5713 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5714 case LOCK_SH|LOCK_NB:
5715 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5716 case LOCK_EX|LOCK_NB:
5717 i = lockf (fd, F_TLOCK, 0);
5719 if ((errno == EAGAIN) || (errno == EACCES))
5720 errno = EWOULDBLOCK;
5723 /* LOCK_UN - unlock (non-blocking is a no-op) */
5725 case LOCK_UN|LOCK_NB:
5726 i = lockf (fd, F_ULOCK, 0);
5729 /* Default - can't decipher operation */
5736 if (pos > 0) /* need to restore position of the handle */
5737 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5742 #endif /* LOCKF_EMULATE_FLOCK */