3 * Copyright (c) 1991-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * But only a short way ahead its floor and the walls on either side were
12 * cloven by a great fissure, out of which the red glare came, now leaping
13 * up, now dying down into darkness; and all the while far below there was
14 * a rumour and a trouble as of great engines throbbing and labouring.
18 #define PERL_IN_PP_SYS_C
22 /* Shadow password support for solaris - pdo@cs.umd.edu
23 * Not just Solaris: at least HP-UX, IRIX, Linux.
24 * The API is from SysV.
26 * There are at least two more shadow interfaces,
27 * see the comments in pp_gpwent().
31 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
32 * and another MAXINT from "perl.h" <- <sys/param.h>. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
58 # include <sys/select.h>
62 /* XXX Configure test needed.
63 h_errno might not be a simple 'int', especially for multi-threaded
64 applications, see "extern int errno in perl.h". Creating such
65 a test requires taking into account the differences between
66 compiling multithreaded and singlethreaded ($ccflags et al).
67 HOST_NOT_FOUND is typically defined in <netdb.h>.
69 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
78 struct passwd *getpwnam (char *);
79 struct passwd *getpwuid (Uid_t);
84 struct passwd *getpwent (void);
85 #elif defined (VMS) && defined (my_getpwent)
86 struct passwd *Perl_my_getpwent (void);
95 struct group *getgrnam (char *);
96 struct group *getgrgid (Gid_t);
100 struct group *getgrent (void);
106 # if defined(_MSC_VER) || defined(__MINGW32__)
107 # include <sys/utime.h>
114 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
117 # define my_chsize PerlLIO_chsize
122 #else /* no flock() */
124 /* fcntl.h might not have been included, even if it exists, because
125 the current Configure only sets I_FCNTL if it's needed to pick up
126 the *_OK constants. Make sure it has been included before testing
127 the fcntl() locking constants. */
128 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
132 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
133 # define FLOCK fcntl_emulate_flock
134 # define FCNTL_EMULATE_FLOCK
135 # else /* no flock() or fcntl(F_SETLK,...) */
137 # define FLOCK lockf_emulate_flock
138 # define LOCKF_EMULATE_FLOCK
140 # endif /* no flock() or fcntl(F_SETLK,...) */
143 static int FLOCK (int, int);
146 * These are the flock() constants. Since this sytems doesn't have
147 * flock(), the values of the constants are probably not available.
161 # endif /* emulating flock() */
163 #endif /* no flock() */
166 static char zero_but_true[ZBTLEN + 1] = "0 but true";
168 #if defined(I_SYS_ACCESS) && !defined(R_OK)
169 # include <sys/access.h>
172 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
173 # define FD_CLOEXEC 1 /* NeXT needs this */
178 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
179 #undef PERL_EFF_ACCESS_W_OK
180 #undef PERL_EFF_ACCESS_X_OK
182 /* F_OK unused: if stat() cannot find it... */
184 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
185 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
186 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
187 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
188 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
191 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
192 # ifdef I_SYS_SECURITY
193 # include <sys/security.h>
197 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
198 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
202 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
203 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
204 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
208 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
210 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
211 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
212 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
216 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
217 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
220 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
222 Uid_t ruid = getuid();
223 Uid_t euid = geteuid();
224 Gid_t rgid = getgid();
225 Gid_t egid = getegid();
229 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
230 Perl_croak(aTHX_ "switching effective uid is not implemented");
233 if (setreuid(euid, ruid))
236 if (setresuid(euid, ruid, (Uid_t)-1))
239 Perl_croak(aTHX_ "entering effective uid failed");
242 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
243 Perl_croak(aTHX_ "switching effective gid is not implemented");
246 if (setregid(egid, rgid))
249 if (setresgid(egid, rgid, (Gid_t)-1))
252 Perl_croak(aTHX_ "entering effective gid failed");
255 res = access(path, mode);
258 if (setreuid(ruid, euid))
261 if (setresuid(ruid, euid, (Uid_t)-1))
264 Perl_croak(aTHX_ "leaving effective uid failed");
267 if (setregid(rgid, egid))
270 if (setresgid(rgid, egid, (Gid_t)-1))
273 Perl_croak(aTHX_ "leaving effective gid failed");
278 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
279 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
280 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
283 #if !defined(PERL_EFF_ACCESS_R_OK)
284 /* With it or without it: anyway you get a warning: either that
285 it is unused, or it is declared static and never defined.
288 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
290 Perl_croak(aTHX_ "switching effective uid is not implemented");
306 if (PL_op->op_private & OPpOPEN_IN_RAW)
308 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
310 fp = PerlProc_popen(tmps, mode);
313 if (PL_curcop->cop_io) {
314 type = SvPV_nolen(PL_curcop->cop_io);
317 PerlIO_apply_layers(aTHX_ fp,mode,type);
319 if (gimme == G_VOID) {
321 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
325 else if (gimme == G_SCALAR) {
327 PL_rs = &PL_sv_undef;
328 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
329 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
341 if (sv_gets(sv, fp, 0) == Nullch) {
345 XPUSHs(sv_2mortal(sv));
346 if (SvLEN(sv) - SvCUR(sv) > 20) {
347 SvLEN_set(sv, SvCUR(sv)+1);
348 Renew(SvPVX(sv), SvLEN(sv), char);
353 STATUS_NATIVE_SET(PerlProc_pclose(fp));
354 TAINT; /* "I believe that this is not gratuitous!" */
357 STATUS_NATIVE_SET(-1);
358 if (gimme == G_SCALAR)
368 tryAMAGICunTARGET(iter, -1);
370 /* Note that we only ever get here if File::Glob fails to load
371 * without at the same time croaking, for some reason, or if
372 * perl was built with PERL_EXTERNAL_GLOB */
379 * The external globbing program may use things we can't control,
380 * so for security reasons we must assume the worst.
383 taint_proper(PL_no_security, "glob");
387 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
388 PL_last_in_gv = (GV*)*PL_stack_sp--;
390 SAVESPTR(PL_rs); /* This is not permanent, either. */
391 PL_rs = sv_2mortal(newSVpvn("\000", 1));
394 *SvPVX(PL_rs) = '\n';
398 result = do_readline();
405 PL_last_in_gv = cGVOP_gv;
406 return do_readline();
415 if (SP - MARK != 1) {
417 do_join(TARG, &PL_sv_no, MARK, SP);
424 tmps = SvPV(tmpsv, len);
427 (void)SvUPGRADE(error, SVt_PV);
428 if (SvPOK(error) && SvCUR(error))
429 sv_catpv(error, "\t...caught");
431 tmps = SvPV(tmpsv, len);
434 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
436 Perl_warn(aTHX_ "%"SVf, tmpsv);
448 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
450 if (SP - MARK != 1) {
452 do_join(TARG, &PL_sv_no, MARK, SP);
454 tmps = SvPV(tmpsv, len);
460 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
464 (void)SvUPGRADE(error, SVt_PV);
465 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
467 SvSetSV(error,tmpsv);
468 else if (sv_isobject(error)) {
469 HV *stash = SvSTASH(SvRV(error));
470 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
472 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
473 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
480 call_sv((SV*)GvCV(gv),
481 G_SCALAR|G_EVAL|G_KEEPERR);
482 sv_setsv(error,*PL_stack_sp--);
485 DIE(aTHX_ Nullformat);
488 if (SvPOK(error) && SvCUR(error))
489 sv_catpv(error, "\t...propagated");
491 tmps = SvPV(tmpsv, len);
495 tmpsv = sv_2mortal(newSVpvn("Died", 4));
497 DIE(aTHX_ "%"SVf, tmpsv);
517 DIE(aTHX_ PL_no_usym, "filehandle");
518 if ((io = GvIOp(gv)))
519 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
521 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
522 /* Method's args are same as ours ... */
523 /* ... except handle is replaced by the object */
524 *MARK-- = SvTIED_obj((SV*)io, mg);
528 call_method("OPEN", G_SCALAR);
541 tmps = SvPV(sv, len);
542 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
545 PUSHi( (I32)PL_forkprocess );
546 else if (PL_forkprocess == 0) /* we are a new child */
565 if (gv && (io = GvIO(gv))
566 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
569 XPUSHs(SvTIED_obj((SV*)io, mg));
572 call_method("CLOSE", G_SCALAR);
578 PUSHs(boolSV(do_close(gv, TRUE)));
598 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
599 DIE(aTHX_ PL_no_usym, "filehandle");
604 do_close(rgv, FALSE);
606 do_close(wgv, FALSE);
608 if (PerlProc_pipe(fd) < 0)
611 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
612 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
613 IoOFP(rstio) = IoIFP(rstio);
614 IoIFP(wstio) = IoOFP(wstio);
615 IoTYPE(rstio) = IoTYPE_RDONLY;
616 IoTYPE(wstio) = IoTYPE_WRONLY;
618 if (!IoIFP(rstio) || !IoOFP(wstio)) {
619 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
620 else PerlLIO_close(fd[0]);
621 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
622 else PerlLIO_close(fd[1]);
625 #if defined(HAS_FCNTL) && defined(F_SETFD)
626 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
627 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
634 DIE(aTHX_ PL_no_func, "pipe");
650 if (gv && (io = GvIO(gv))
651 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
654 XPUSHs(SvTIED_obj((SV*)io, mg));
657 call_method("FILENO", G_SCALAR);
663 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
664 /* Can't do this because people seem to do things like
665 defined(fileno($foo)) to check whether $foo is a valid fh.
666 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
667 report_evil_fh(gv, io, PL_op->op_type);
672 PUSHi(PerlIO_fileno(fp));
683 anum = PerlLIO_umask(0);
684 (void)PerlLIO_umask(anum);
687 anum = PerlLIO_umask(POPi);
688 TAINT_PROPER("umask");
691 /* Only DIE if trying to restrict permissions on `user' (self).
692 * Otherwise it's harmless and more useful to just return undef
693 * since 'group' and 'other' concepts probably don't exist here. */
694 if (MAXARG >= 1 && (POPi & 0700))
695 DIE(aTHX_ "umask not implemented");
696 XPUSHs(&PL_sv_undef);
718 if (gv && (io = GvIO(gv))
719 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
722 XPUSHs(SvTIED_obj((SV*)io, mg));
727 call_method("BINMODE", G_SCALAR);
734 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
735 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
736 report_evil_fh(gv, io, PL_op->op_type);
737 SETERRNO(EBADF,RMS_IFI);
742 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
743 (discp) ? SvPV_nolen(discp) : Nullch)) {
761 I32 markoff = MARK - PL_stack_base;
763 int how = PERL_MAGIC_tied;
767 switch(SvTYPE(varsv)) {
769 methname = "TIEHASH";
770 HvEITER((HV *)varsv) = Null(HE *);
773 methname = "TIEARRAY";
776 #ifdef GV_UNIQUE_CHECK
777 if (GvUNIQUE((GV*)varsv)) {
778 Perl_croak(aTHX_ "Attempt to tie unique GV");
781 methname = "TIEHANDLE";
782 how = PERL_MAGIC_tiedscalar;
783 /* For tied filehandles, we apply tiedscalar magic to the IO
784 slot of the GP rather than the GV itself. AMS 20010812 */
786 GvIOp(varsv) = newIO();
787 varsv = (SV *)GvIOp(varsv);
790 methname = "TIESCALAR";
791 how = PERL_MAGIC_tiedscalar;
795 if (sv_isobject(*MARK)) {
797 PUSHSTACKi(PERLSI_MAGIC);
799 EXTEND(SP,(I32)items);
803 call_method(methname, G_SCALAR);
806 /* Not clear why we don't call call_method here too.
807 * perhaps to get different error message ?
809 stash = gv_stashsv(*MARK, FALSE);
810 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
811 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
815 PUSHSTACKi(PERLSI_MAGIC);
817 EXTEND(SP,(I32)items);
821 call_sv((SV*)GvCV(gv), G_SCALAR);
827 if (sv_isobject(sv)) {
828 sv_unmagic(varsv, how);
829 /* Croak if a self-tie on an aggregate is attempted. */
830 if (varsv == SvRV(sv) &&
831 (SvTYPE(varsv) == SVt_PVAV ||
832 SvTYPE(varsv) == SVt_PVHV))
834 "Self-ties of arrays and hashes are not supported");
835 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
838 SP = PL_stack_base + markoff;
848 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
849 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
851 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
854 if ((mg = SvTIED_mg(sv, how))) {
855 SV *obj = SvRV(mg->mg_obj);
859 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
860 isGV(gv) && (cv = GvCV(gv))) {
862 XPUSHs(SvTIED_obj((SV*)gv, mg));
863 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
866 call_sv((SV *)cv, G_VOID);
870 else if (ckWARN(WARN_UNTIE)) {
871 if (mg && SvREFCNT(obj) > 1)
872 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
873 "untie attempted while %"UVuf" inner references still exist",
874 (UV)SvREFCNT(obj) - 1 ) ;
877 sv_unmagic(sv, how) ;
887 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
888 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
890 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
893 if ((mg = SvTIED_mg(sv, how))) {
894 SV *osv = SvTIED_obj(sv, mg);
895 if (osv == mg->mg_obj)
896 osv = sv_mortalcopy(osv);
914 sv = sv_mortalcopy(&PL_sv_no);
915 sv_setpv(sv, "AnyDBM_File");
916 stash = gv_stashsv(sv, FALSE);
917 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
919 require_pv("AnyDBM_File.pm");
921 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
922 DIE(aTHX_ "No dbm on this machine");
932 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
934 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
937 call_sv((SV*)GvCV(gv), G_SCALAR);
940 if (!sv_isobject(TOPs)) {
945 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
948 call_sv((SV*)GvCV(gv), G_SCALAR);
952 if (sv_isobject(TOPs)) {
953 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
954 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
976 struct timeval timebuf;
977 struct timeval *tbuf = &timebuf;
981 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
986 # if BYTEORDER & 0xf0000
987 # define ORDERBYTE (0x88888888 - BYTEORDER)
989 # define ORDERBYTE (0x4444 - BYTEORDER)
995 for (i = 1; i <= 3; i++) {
1003 /* little endians can use vecs directly */
1004 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1011 masksize = NFDBITS / NBBY;
1013 masksize = sizeof(long); /* documented int, everyone seems to use long */
1015 Zero(&fd_sets[0], 4, char*);
1018 # if SELECT_MIN_BITS > 1
1019 /* If SELECT_MIN_BITS is greater than one we most probably will want
1020 * to align the sizes with SELECT_MIN_BITS/8 because for example
1021 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1022 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1023 * on (sets/tests/clears bits) is 32 bits. */
1024 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1026 growsize = sizeof(fd_set);
1034 timebuf.tv_sec = (long)value;
1035 value -= (NV)timebuf.tv_sec;
1036 timebuf.tv_usec = (long)(value * 1000000.0);
1039 tbuf = Null(struct timeval*);
1041 for (i = 1; i <= 3; i++) {
1047 else if (!SvPOK(sv))
1048 SvPV_force(sv,n_a); /* force string conversion */
1051 Sv_Grow(sv, growsize);
1055 while (++j <= growsize) {
1059 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1061 New(403, fd_sets[i], growsize, char);
1062 for (offset = 0; offset < growsize; offset += masksize) {
1063 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1064 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1067 fd_sets[i] = SvPVX(sv);
1071 nfound = PerlSock_select(
1073 (Select_fd_set_t) fd_sets[1],
1074 (Select_fd_set_t) fd_sets[2],
1075 (Select_fd_set_t) fd_sets[3],
1077 for (i = 1; i <= 3; i++) {
1080 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1082 for (offset = 0; offset < growsize; offset += masksize) {
1083 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1084 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1086 Safefree(fd_sets[i]);
1093 if (GIMME == G_ARRAY && tbuf) {
1094 value = (NV)(timebuf.tv_sec) +
1095 (NV)(timebuf.tv_usec) / 1000000.0;
1096 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1097 sv_setnv(sv, value);
1101 DIE(aTHX_ "select not implemented");
1106 Perl_setdefout(pTHX_ GV *gv)
1109 (void)SvREFCNT_inc(gv);
1111 SvREFCNT_dec(PL_defoutgv);
1118 GV *newdefout, *egv;
1121 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1123 egv = GvEGV(PL_defoutgv);
1128 XPUSHs(&PL_sv_undef);
1130 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1131 if (gvp && *gvp == egv) {
1132 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1136 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1141 if (!GvIO(newdefout))
1142 gv_IOadd(newdefout);
1143 setdefout(newdefout);
1161 if (gv && (io = GvIO(gv))
1162 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1164 I32 gimme = GIMME_V;
1166 XPUSHs(SvTIED_obj((SV*)io, mg));
1169 call_method("GETC", gimme);
1172 if (gimme == G_SCALAR)
1173 SvSetMagicSV_nosteal(TARG, TOPs);
1176 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1177 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1178 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1179 report_evil_fh(gv, io, PL_op->op_type);
1180 SETERRNO(EBADF,RMS_IFI);
1184 sv_setpv(TARG, " ");
1185 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1186 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1187 /* Find out how many bytes the char needs */
1188 Size_t len = UTF8SKIP(SvPVX(TARG));
1191 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1192 SvCUR_set(TARG,1+len);
1202 return pp_sysread();
1206 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1208 register PERL_CONTEXT *cx;
1209 I32 gimme = GIMME_V;
1215 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1217 PAD_SET_CUR(CvPADLIST(cv), 1);
1219 setdefout(gv); /* locally select filehandle so $% et al work */
1252 SV *tmpsv = sv_newmortal();
1253 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1254 name = SvPV_nolen(tmpsv);
1257 DIE(aTHX_ "Undefined format \"%s\" called", name);
1258 DIE(aTHX_ "Not a format reference");
1261 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1263 IoFLAGS(io) &= ~IOf_DIDTOP;
1264 return doform(cv,gv,PL_op->op_next);
1270 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1271 register IO *io = GvIOp(gv);
1272 PerlIO *ofp = IoOFP(io);
1276 register PERL_CONTEXT *cx;
1278 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1279 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1282 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1283 PL_formtarget != PL_toptarget)
1287 if (!IoTOP_GV(io)) {
1291 if (!IoTOP_NAME(io)) {
1292 if (!IoFMT_NAME(io))
1293 IoFMT_NAME(io) = savepv(GvNAME(gv));
1294 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1295 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1296 if ((topgv && GvFORM(topgv)) ||
1297 !gv_fetchpv("top",FALSE,SVt_PVFM))
1298 IoTOP_NAME(io) = savepv(SvPVX(topname));
1300 IoTOP_NAME(io) = savepv("top");
1302 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1303 if (!topgv || !GvFORM(topgv)) {
1304 IoLINES_LEFT(io) = 100000000;
1307 IoTOP_GV(io) = topgv;
1309 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1310 I32 lines = IoLINES_LEFT(io);
1311 char *s = SvPVX(PL_formtarget);
1312 if (lines <= 0) /* Yow, header didn't even fit!!! */
1314 while (lines-- > 0) {
1315 s = strchr(s, '\n');
1321 STRLEN save = SvCUR(PL_formtarget);
1322 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1323 do_print(PL_formtarget, ofp);
1324 SvCUR_set(PL_formtarget, save);
1325 sv_chop(PL_formtarget, s);
1326 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1329 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1330 do_print(PL_formfeed, ofp);
1331 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1333 PL_formtarget = PL_toptarget;
1334 IoFLAGS(io) |= IOf_DIDTOP;
1337 DIE(aTHX_ "bad top format reference");
1342 SV *sv = sv_newmortal();
1343 gv_efullname4(sv, fgv, Nullch, FALSE);
1344 name = SvPV_nolen(sv);
1347 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1350 DIE(aTHX_ "Undefined top format called");
1354 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1355 return doform(cv,gv,PL_op);
1359 POPBLOCK(cx,PL_curpm);
1365 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1367 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1368 else if (ckWARN(WARN_CLOSED))
1369 report_evil_fh(gv, io, PL_op->op_type);
1374 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1375 if (ckWARN(WARN_IO))
1376 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1378 if (!do_print(PL_formtarget, fp))
1381 FmLINES(PL_formtarget) = 0;
1382 SvCUR_set(PL_formtarget, 0);
1383 *SvEND(PL_formtarget) = '\0';
1384 if (IoFLAGS(io) & IOf_FLUSH)
1385 (void)PerlIO_flush(fp);
1390 PL_formtarget = PL_bodytarget;
1392 return pop_return();
1397 dSP; dMARK; dORIGMARK;
1404 if (PL_op->op_flags & OPf_STACKED)
1409 if (gv && (io = GvIO(gv))
1410 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1412 if (MARK == ORIGMARK) {
1415 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1419 *MARK = SvTIED_obj((SV*)io, mg);
1422 call_method("PRINTF", G_SCALAR);
1425 MARK = ORIGMARK + 1;
1432 if (!(io = GvIO(gv))) {
1433 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1434 report_evil_fh(gv, io, PL_op->op_type);
1435 SETERRNO(EBADF,RMS_IFI);
1438 else if (!(fp = IoOFP(io))) {
1439 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1441 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1442 else if (ckWARN(WARN_CLOSED))
1443 report_evil_fh(gv, io, PL_op->op_type);
1445 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1449 do_sprintf(sv, SP - MARK, MARK + 1);
1450 if (!do_print(sv, fp))
1453 if (IoFLAGS(io) & IOf_FLUSH)
1454 if (PerlIO_flush(fp) == EOF)
1465 PUSHs(&PL_sv_undef);
1486 /* Need TIEHANDLE method ? */
1488 tmps = SvPV(sv, len);
1489 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1490 IoLINES(GvIOp(gv)) = 0;
1494 PUSHs(&PL_sv_undef);
1501 dSP; dMARK; dORIGMARK; dTARGET;
1508 Sock_size_t bufsize;
1515 bool charstart = FALSE;
1516 STRLEN charskip = 0;
1520 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1521 && gv && (io = GvIO(gv))
1522 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1527 *MARK = SvTIED_obj((SV*)io, mg);
1529 call_method("READ", G_SCALAR);
1542 sv_setpvn(bufsv, "", 0);
1543 length = SvIVx(*++MARK);
1546 offset = SvIVx(*++MARK);
1550 if (!io || !IoIFP(io)) {
1551 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1552 report_evil_fh(gv, io, PL_op->op_type);
1553 SETERRNO(EBADF,RMS_IFI);
1556 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1557 buffer = SvPVutf8_force(bufsv, blen);
1558 /* UTF8 may not have been set if they are all low bytes */
1562 buffer = SvPV_force(bufsv, blen);
1565 DIE(aTHX_ "Negative length");
1573 if (PL_op->op_type == OP_RECV) {
1574 char namebuf[MAXPATHLEN];
1575 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1576 bufsize = sizeof (struct sockaddr_in);
1578 bufsize = sizeof namebuf;
1580 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1584 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1585 /* 'offset' means 'flags' here */
1586 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1587 (struct sockaddr *)namebuf, &bufsize);
1591 /* Bogus return without padding */
1592 bufsize = sizeof (struct sockaddr_in);
1594 SvCUR_set(bufsv, count);
1595 *SvEND(bufsv) = '\0';
1596 (void)SvPOK_only(bufsv);
1600 /* This should not be marked tainted if the fp is marked clean */
1601 if (!(IoFLAGS(io) & IOf_UNTAINT))
1602 SvTAINTED_on(bufsv);
1604 sv_setpvn(TARG, namebuf, bufsize);
1609 if (PL_op->op_type == OP_RECV)
1610 DIE(aTHX_ PL_no_sock_func, "recv");
1612 if (DO_UTF8(bufsv)) {
1613 /* offset adjust in characters not bytes */
1614 blen = sv_len_utf8(bufsv);
1617 if (-offset > (int)blen)
1618 DIE(aTHX_ "Offset outside string");
1621 if (DO_UTF8(bufsv)) {
1622 /* convert offset-as-chars to offset-as-bytes */
1623 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1626 bufsize = SvCUR(bufsv);
1627 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1628 if (offset > bufsize) { /* Zero any newly allocated space */
1629 Zero(buffer+bufsize, offset-bufsize, char);
1631 buffer = buffer + offset;
1633 if (PL_op->op_type == OP_SYSREAD) {
1634 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1635 if (IoTYPE(io) == IoTYPE_SOCKET) {
1636 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1642 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1647 #ifdef HAS_SOCKET__bad_code_maybe
1648 if (IoTYPE(io) == IoTYPE_SOCKET) {
1649 char namebuf[MAXPATHLEN];
1650 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1651 bufsize = sizeof (struct sockaddr_in);
1653 bufsize = sizeof namebuf;
1655 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1656 (struct sockaddr *)namebuf, &bufsize);
1661 count = PerlIO_read(IoIFP(io), buffer, length);
1662 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1663 if (count == 0 && PerlIO_error(IoIFP(io)))
1667 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1668 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1671 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1672 *SvEND(bufsv) = '\0';
1673 (void)SvPOK_only(bufsv);
1674 if (fp_utf8 && !IN_BYTES) {
1675 /* Look at utf8 we got back and count the characters */
1676 char *bend = buffer + count;
1677 while (buffer < bend) {
1679 skip = UTF8SKIP(buffer);
1682 if (buffer - charskip + skip > bend) {
1683 /* partial character - try for rest of it */
1684 length = skip - (bend-buffer);
1685 offset = bend - SvPVX(bufsv);
1697 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1698 provided amount read (count) was what was requested (length)
1700 if (got < wanted && count == length) {
1701 length = wanted - got;
1702 offset = bend - SvPVX(bufsv);
1705 /* return value is character count */
1710 /* This should not be marked tainted if the fp is marked clean */
1711 if (!(IoFLAGS(io) & IOf_UNTAINT))
1712 SvTAINTED_on(bufsv);
1725 int items = (SP - PL_stack_base) - TOPMARK;
1729 sv = sv_2mortal(newSViv(sv_len(*SP)));
1738 dSP; dMARK; dORIGMARK; dTARGET;
1749 if (PL_op->op_type == OP_SYSWRITE
1750 && gv && (io = GvIO(gv))
1751 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1756 *MARK = SvTIED_obj((SV*)io, mg);
1758 call_method("WRITE", G_SCALAR);
1769 #if Size_t_size > IVSIZE
1770 length = (Size_t)SvNVx(*++MARK);
1772 length = (Size_t)SvIVx(*++MARK);
1774 if ((SSize_t)length < 0)
1775 DIE(aTHX_ "Negative length");
1778 if (!io || !IoIFP(io)) {
1780 if (ckWARN(WARN_CLOSED))
1781 report_evil_fh(gv, io, PL_op->op_type);
1782 SETERRNO(EBADF,RMS_IFI);
1786 if (PerlIO_isutf8(IoIFP(io))) {
1787 buffer = SvPVutf8(bufsv, blen);
1791 sv_utf8_downgrade(bufsv, FALSE);
1792 buffer = SvPV(bufsv, blen);
1795 if (PL_op->op_type == OP_SYSWRITE) {
1797 if (DO_UTF8(bufsv)) {
1798 /* length and offset are in chars */
1799 blen = sv_len_utf8(bufsv);
1802 offset = SvIVx(*++MARK);
1804 if (-offset > (IV)blen)
1805 DIE(aTHX_ "Offset outside string");
1807 } else if (offset >= (IV)blen && blen > 0)
1808 DIE(aTHX_ "Offset outside string");
1811 if (length > blen - offset)
1812 length = blen - offset;
1813 if (DO_UTF8(bufsv)) {
1814 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1815 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1818 buffer = buffer+offset;
1820 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1821 if (IoTYPE(io) == IoTYPE_SOCKET) {
1822 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1828 /* See the note at doio.c:do_print about filesize limits. --jhi */
1829 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1834 else if (SP > MARK) {
1837 sockbuf = SvPVx(*++MARK, mlen);
1838 /* length is really flags */
1839 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1840 length, (struct sockaddr *)sockbuf, mlen);
1843 /* length is really flags */
1844 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1847 DIE(aTHX_ PL_no_sock_func, "send");
1853 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1854 #if Size_t_size > IVSIZE
1868 return pp_sysread();
1879 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1881 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1883 if (io && !IoIFP(io)) {
1884 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1886 IoFLAGS(io) &= ~IOf_START;
1887 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1888 sv_setpvn(GvSV(gv), "-", 1);
1889 SvSETMAGIC(GvSV(gv));
1891 else if (!nextargv(gv))
1896 gv = PL_last_in_gv; /* eof */
1899 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1901 if (gv && (io = GvIO(gv))
1902 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1905 XPUSHs(SvTIED_obj((SV*)io, mg));
1908 call_method("EOF", G_SCALAR);
1914 PUSHs(boolSV(!gv || do_eof(gv)));
1928 gv = PL_last_in_gv = (GV*)POPs;
1930 if (gv && (io = GvIO(gv))
1931 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1934 XPUSHs(SvTIED_obj((SV*)io, mg));
1937 call_method("TELL", G_SCALAR);
1943 #if LSEEKSIZE > IVSIZE
1944 PUSHn( do_tell(gv) );
1946 PUSHi( do_tell(gv) );
1953 return pp_sysseek();
1962 #if LSEEKSIZE > IVSIZE
1963 Off_t offset = (Off_t)SvNVx(POPs);
1965 Off_t offset = (Off_t)SvIVx(POPs);
1969 gv = PL_last_in_gv = (GV*)POPs;
1971 if (gv && (io = GvIO(gv))
1972 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1975 XPUSHs(SvTIED_obj((SV*)io, mg));
1976 #if LSEEKSIZE > IVSIZE
1977 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1979 XPUSHs(sv_2mortal(newSViv(offset)));
1981 XPUSHs(sv_2mortal(newSViv(whence)));
1984 call_method("SEEK", G_SCALAR);
1990 if (PL_op->op_type == OP_SEEK)
1991 PUSHs(boolSV(do_seek(gv, offset, whence)));
1993 Off_t sought = do_sysseek(gv, offset, whence);
1995 PUSHs(&PL_sv_undef);
1998 #if LSEEKSIZE > IVSIZE
2003 : newSVpvn(zero_but_true, ZBTLEN);
2004 PUSHs(sv_2mortal(sv));
2013 /* There seems to be no consensus on the length type of truncate()
2014 * and ftruncate(), both off_t and size_t have supporters. In
2015 * general one would think that when using large files, off_t is
2016 * at least as wide as size_t, so using an off_t should be okay. */
2017 /* XXX Configure probe for the length type of *truncate() needed XXX */
2020 #if Off_t_size > IVSIZE
2025 /* Checking for length < 0 is problematic as the type might or
2026 * might not be signed: if it is not, clever compilers will moan. */
2027 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2029 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2036 if (PL_op->op_flags & OPf_SPECIAL) {
2037 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2046 TAINT_PROPER("truncate");
2047 if (!(fp = IoIFP(io))) {
2053 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2055 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2065 if (SvTYPE(sv) == SVt_PVGV) {
2066 tmpgv = (GV*)sv; /* *main::FRED for example */
2067 goto do_ftruncate_gv;
2069 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2070 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2071 goto do_ftruncate_gv;
2073 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2074 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2075 goto do_ftruncate_io;
2078 name = SvPV(sv, n_a);
2079 TAINT_PROPER("truncate");
2081 if (truncate(name, len) < 0)
2087 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2090 if (my_chsize(tmpfd, len) < 0)
2092 PerlLIO_close(tmpfd);
2101 SETERRNO(EBADF,RMS_IFI);
2105 DIE(aTHX_ "truncate not implemented");
2118 unsigned int func = POPu;
2119 int optype = PL_op->op_type;
2123 IO *io = gv ? GvIOn(gv) : 0;
2125 if (!io || !argsv || !IoIFP(io)) {
2126 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2127 report_evil_fh(gv, io, PL_op->op_type);
2128 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2132 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2135 s = SvPV_force(argsv, len);
2136 need = IOCPARM_LEN(func);
2138 s = Sv_Grow(argsv, need + 1);
2139 SvCUR_set(argsv, need);
2142 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2145 retval = SvIV(argsv);
2146 s = INT2PTR(char*,retval); /* ouch */
2149 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2151 if (optype == OP_IOCTL)
2153 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2155 DIE(aTHX_ "ioctl is not implemented");
2159 DIE(aTHX_ "fcntl is not implemented");
2161 #if defined(OS2) && defined(__EMX__)
2162 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2164 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2168 if (s[SvCUR(argsv)] != 17)
2169 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2171 s[SvCUR(argsv)] = 0; /* put our null back */
2172 SvSETMAGIC(argsv); /* Assume it has changed */
2181 PUSHp(zero_but_true, ZBTLEN);
2202 if (gv && (io = GvIO(gv)))
2209 (void)PerlIO_flush(fp);
2210 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2213 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2214 report_evil_fh(gv, io, PL_op->op_type);
2216 SETERRNO(EBADF,RMS_IFI);
2221 DIE(aTHX_ PL_no_func, "flock()");
2233 int protocol = POPi;
2239 io = gv ? GvIOn(gv) : NULL;
2242 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2243 report_evil_fh(gv, io, PL_op->op_type);
2245 do_close(gv, FALSE);
2246 SETERRNO(EBADF,LIB_INVARG);
2251 do_close(gv, FALSE);
2253 TAINT_PROPER("socket");
2254 fd = PerlSock_socket(domain, type, protocol);
2257 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2258 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2259 IoTYPE(io) = IoTYPE_SOCKET;
2260 if (!IoIFP(io) || !IoOFP(io)) {
2261 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2262 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2263 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2266 #if defined(HAS_FCNTL) && defined(F_SETFD)
2267 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2271 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2276 DIE(aTHX_ PL_no_sock_func, "socket");
2282 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2288 int protocol = POPi;
2295 io1 = gv1 ? GvIOn(gv1) : NULL;
2296 io2 = gv2 ? GvIOn(gv2) : NULL;
2297 if (!gv1 || !gv2 || !io1 || !io2) {
2298 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2300 report_evil_fh(gv1, io1, PL_op->op_type);
2302 report_evil_fh(gv1, io2, PL_op->op_type);
2305 do_close(gv1, FALSE);
2307 do_close(gv2, FALSE);
2312 do_close(gv1, FALSE);
2314 do_close(gv2, FALSE);
2316 TAINT_PROPER("socketpair");
2317 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2319 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2320 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2321 IoTYPE(io1) = IoTYPE_SOCKET;
2322 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2323 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2324 IoTYPE(io2) = IoTYPE_SOCKET;
2325 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2326 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2327 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2328 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2329 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2330 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2331 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2334 #if defined(HAS_FCNTL) && defined(F_SETFD)
2335 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2336 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2341 DIE(aTHX_ PL_no_sock_func, "socketpair");
2349 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2350 extern void GETPRIVMODE();
2351 extern void GETUSERMODE();
2356 register IO *io = GvIOn(gv);
2360 int mpeprivmode = 0;
2363 if (!io || !IoIFP(io))
2366 addr = SvPV(addrsv, len);
2367 TAINT_PROPER("bind");
2368 #ifdef MPE /* Deal with MPE bind() peculiarities */
2369 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2370 /* The address *MUST* stupidly be zero. */
2371 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2372 /* PRIV mode is required to bind() to ports < 1024. */
2373 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2374 ((struct sockaddr_in *)addr)->sin_port > 0) {
2375 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2380 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2381 (struct sockaddr *)addr, len) >= 0)
2384 #ifdef MPE /* Switch back to USER mode */
2395 if (ckWARN(WARN_CLOSED))
2396 report_evil_fh(gv, io, PL_op->op_type);
2397 SETERRNO(EBADF,SS_IVCHAN);
2400 DIE(aTHX_ PL_no_sock_func, "bind");
2411 register IO *io = GvIOn(gv);
2414 if (!io || !IoIFP(io))
2417 addr = SvPV(addrsv, len);
2418 TAINT_PROPER("connect");
2419 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2425 if (ckWARN(WARN_CLOSED))
2426 report_evil_fh(gv, io, PL_op->op_type);
2427 SETERRNO(EBADF,SS_IVCHAN);
2430 DIE(aTHX_ PL_no_sock_func, "connect");
2440 register IO *io = gv ? GvIOn(gv) : NULL;
2442 if (!gv || !io || !IoIFP(io))
2445 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2451 if (ckWARN(WARN_CLOSED))
2452 report_evil_fh(gv, io, PL_op->op_type);
2453 SETERRNO(EBADF,SS_IVCHAN);
2456 DIE(aTHX_ PL_no_sock_func, "listen");
2468 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2469 Sock_size_t len = sizeof saddr;
2481 if (!gstio || !IoIFP(gstio))
2485 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2489 do_close(ngv, FALSE);
2490 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2491 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2492 IoTYPE(nstio) = IoTYPE_SOCKET;
2493 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2494 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2495 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2496 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2499 #if defined(HAS_FCNTL) && defined(F_SETFD)
2500 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2504 len = sizeof saddr; /* EPOC somehow truncates info */
2505 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2508 PUSHp((char *)&saddr, len);
2512 if (ckWARN(WARN_CLOSED))
2513 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2514 SETERRNO(EBADF,SS_IVCHAN);
2520 DIE(aTHX_ PL_no_sock_func, "accept");
2530 register IO *io = GvIOn(gv);
2532 if (!io || !IoIFP(io))
2535 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2539 if (ckWARN(WARN_CLOSED))
2540 report_evil_fh(gv, io, PL_op->op_type);
2541 SETERRNO(EBADF,SS_IVCHAN);
2544 DIE(aTHX_ PL_no_sock_func, "shutdown");
2551 return pp_ssockopt();
2553 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2561 int optype = PL_op->op_type;
2564 unsigned int optname;
2570 if (optype == OP_GSOCKOPT)
2571 sv = sv_2mortal(NEWSV(22, 257));
2574 optname = (unsigned int) POPi;
2575 lvl = (unsigned int) POPi;
2579 if (!io || !IoIFP(io))
2582 fd = PerlIO_fileno(IoIFP(io));
2586 (void)SvPOK_only(sv);
2590 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2605 aint = (int)SvIV(sv);
2609 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2618 if (ckWARN(WARN_CLOSED))
2619 report_evil_fh(gv, io, optype);
2620 SETERRNO(EBADF,SS_IVCHAN);
2625 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2632 return pp_getpeername();
2634 DIE(aTHX_ PL_no_sock_func, "getsockname");
2642 int optype = PL_op->op_type;
2646 register IO *io = GvIOn(gv);
2649 if (!io || !IoIFP(io))
2652 sv = sv_2mortal(NEWSV(22, 257));
2653 (void)SvPOK_only(sv);
2657 fd = PerlIO_fileno(IoIFP(io));
2659 case OP_GETSOCKNAME:
2660 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2663 case OP_GETPEERNAME:
2664 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2666 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2668 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";
2669 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2670 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2671 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2672 sizeof(u_short) + sizeof(struct in_addr))) {
2679 #ifdef BOGUS_GETNAME_RETURN
2680 /* Interactive Unix, getpeername() and getsockname()
2681 does not return valid namelen */
2682 if (len == BOGUS_GETNAME_RETURN)
2683 len = sizeof(struct sockaddr);
2691 if (ckWARN(WARN_CLOSED))
2692 report_evil_fh(gv, io, optype);
2693 SETERRNO(EBADF,SS_IVCHAN);
2698 DIE(aTHX_ PL_no_sock_func, "getpeername");
2717 if (PL_op->op_flags & OPf_REF) {
2719 if (PL_op->op_type == OP_LSTAT) {
2720 if (gv != PL_defgv) {
2721 if (ckWARN(WARN_IO))
2722 Perl_warner(aTHX_ packWARN(WARN_IO),
2723 "lstat() on filehandle %s", GvENAME(gv));
2724 } else if (PL_laststype != OP_LSTAT)
2725 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2729 if (gv != PL_defgv) {
2730 PL_laststype = OP_STAT;
2732 sv_setpv(PL_statname, "");
2733 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2734 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2736 if (PL_laststatval < 0) {
2737 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2738 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2744 if (SvTYPE(sv) == SVt_PVGV) {
2748 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2750 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2751 Perl_warner(aTHX_ packWARN(WARN_IO),
2752 "lstat() on filehandle %s", GvENAME(gv));
2755 sv_setpv(PL_statname, SvPV(sv,n_a));
2758 PL_laststype = PL_op->op_type;
2759 if (PL_op->op_type == OP_LSTAT)
2760 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2763 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2764 if (PL_laststatval < 0) {
2765 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2766 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2772 if (gimme != G_ARRAY) {
2773 if (gimme != G_VOID)
2774 XPUSHs(boolSV(max));
2780 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2781 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2782 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2783 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2784 #if Uid_t_size > IVSIZE
2785 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2787 # if Uid_t_sign <= 0
2788 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2790 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2793 #if Gid_t_size > IVSIZE
2794 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2796 # if Gid_t_sign <= 0
2797 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2799 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2802 #ifdef USE_STAT_RDEV
2803 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2805 PUSHs(sv_2mortal(newSVpvn("", 0)));
2807 #if Off_t_size > IVSIZE
2808 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2810 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2813 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2814 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2815 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2817 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2818 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2819 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2821 #ifdef USE_STAT_BLOCKS
2822 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2823 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2825 PUSHs(sv_2mortal(newSVpvn("", 0)));
2826 PUSHs(sv_2mortal(newSVpvn("", 0)));
2836 #if defined(HAS_ACCESS) && defined(R_OK)
2838 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2839 result = access(TOPpx, R_OK);
2854 if (cando(S_IRUSR, 0, &PL_statcache))
2863 #if defined(HAS_ACCESS) && defined(W_OK)
2865 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2866 result = access(TOPpx, W_OK);
2881 if (cando(S_IWUSR, 0, &PL_statcache))
2890 #if defined(HAS_ACCESS) && defined(X_OK)
2892 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2893 result = access(TOPpx, X_OK);
2908 if (cando(S_IXUSR, 0, &PL_statcache))
2917 #ifdef PERL_EFF_ACCESS_R_OK
2919 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2920 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2935 if (cando(S_IRUSR, 1, &PL_statcache))
2944 #ifdef PERL_EFF_ACCESS_W_OK
2946 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2947 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2962 if (cando(S_IWUSR, 1, &PL_statcache))
2971 #ifdef PERL_EFF_ACCESS_X_OK
2973 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2974 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2989 if (cando(S_IXUSR, 1, &PL_statcache))
2996 I32 result = my_stat();
3005 return pp_ftrowned();
3010 I32 result = my_stat();
3014 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3022 I32 result = my_stat();
3026 if (PL_statcache.st_size == 0)
3033 I32 result = my_stat();
3037 #if Off_t_size > IVSIZE
3038 PUSHn(PL_statcache.st_size);
3040 PUSHi(PL_statcache.st_size);
3047 I32 result = my_stat();
3051 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3057 I32 result = my_stat();
3061 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3067 I32 result = my_stat();
3071 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3077 I32 result = my_stat();
3081 if (S_ISSOCK(PL_statcache.st_mode))
3088 I32 result = my_stat();
3092 if (S_ISCHR(PL_statcache.st_mode))
3099 I32 result = my_stat();
3103 if (S_ISBLK(PL_statcache.st_mode))
3110 I32 result = my_stat();
3114 if (S_ISREG(PL_statcache.st_mode))
3121 I32 result = my_stat();
3125 if (S_ISDIR(PL_statcache.st_mode))
3132 I32 result = my_stat();
3136 if (S_ISFIFO(PL_statcache.st_mode))
3143 I32 result = my_lstat();
3147 if (S_ISLNK(PL_statcache.st_mode))
3156 I32 result = my_stat();
3160 if (PL_statcache.st_mode & S_ISUID)
3170 I32 result = my_stat();
3174 if (PL_statcache.st_mode & S_ISGID)
3184 I32 result = my_stat();
3188 if (PL_statcache.st_mode & S_ISVTX)
3199 char *tmps = Nullch;
3202 if (PL_op->op_flags & OPf_REF)
3204 else if (isGV(TOPs))
3206 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3207 gv = (GV*)SvRV(POPs);
3209 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3211 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3212 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3213 else if (tmps && isDIGIT(*tmps))
3217 if (PerlLIO_isatty(fd))
3222 #if defined(atarist) /* this will work with atariST. Configure will
3223 make guesses for other systems. */
3224 # define FILE_base(f) ((f)->_base)
3225 # define FILE_ptr(f) ((f)->_ptr)
3226 # define FILE_cnt(f) ((f)->_cnt)
3227 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3237 register STDCHAR *s;
3244 if (PL_op->op_flags & OPf_REF)
3246 else if (isGV(TOPs))
3248 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3249 gv = (GV*)SvRV(POPs);
3255 if (gv == PL_defgv) {
3257 io = GvIO(PL_statgv);
3260 goto really_filename;
3265 PL_laststatval = -1;
3266 sv_setpv(PL_statname, "");
3267 io = GvIO(PL_statgv);
3269 if (io && IoIFP(io)) {
3270 if (! PerlIO_has_base(IoIFP(io)))
3271 DIE(aTHX_ "-T and -B not implemented on filehandles");
3272 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3273 if (PL_laststatval < 0)
3275 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3276 if (PL_op->op_type == OP_FTTEXT)
3281 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3282 i = PerlIO_getc(IoIFP(io));
3284 (void)PerlIO_ungetc(IoIFP(io),i);
3286 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3288 len = PerlIO_get_bufsiz(IoIFP(io));
3289 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3290 /* sfio can have large buffers - limit to 512 */
3295 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3297 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3299 SETERRNO(EBADF,RMS_IFI);
3307 PL_laststatval = -1;
3308 PL_laststype = OP_STAT;
3309 sv_setpv(PL_statname, SvPV(sv, n_a));
3310 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3311 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3312 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3315 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3316 if (PL_laststatval < 0) {
3317 (void)PerlIO_close(fp);
3320 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3321 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3322 (void)PerlIO_close(fp);
3324 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3325 RETPUSHNO; /* special case NFS directories */
3326 RETPUSHYES; /* null file is anything */
3331 /* now scan s to look for textiness */
3332 /* XXX ASCII dependent code */
3334 #if defined(DOSISH) || defined(USEMYBINMODE)
3335 /* ignore trailing ^Z on short files */
3336 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3340 for (i = 0; i < len; i++, s++) {
3341 if (!*s) { /* null never allowed in text */
3346 else if (!(isPRINT(*s) || isSPACE(*s)))
3349 else if (*s & 128) {
3351 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3354 /* utf8 characters don't count as odd */
3355 if (UTF8_IS_START(*s)) {
3356 int ulen = UTF8SKIP(s);
3357 if (ulen < len - i) {
3359 for (j = 1; j < ulen; j++) {
3360 if (!UTF8_IS_CONTINUATION(s[j]))
3363 --ulen; /* loop does extra increment */
3373 *s != '\n' && *s != '\r' && *s != '\b' &&
3374 *s != '\t' && *s != '\f' && *s != 27)
3379 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3404 if( !tmps || !*tmps ) {
3405 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3406 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3408 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3413 deprecate("chdir('') or chdir(undef) as chdir()");
3414 tmps = SvPV(*svp, n_a);
3418 TAINT_PROPER("chdir");
3423 TAINT_PROPER("chdir");
3424 PUSHi( PerlDir_chdir(tmps) >= 0 );
3426 /* Clear the DEFAULT element of ENV so we'll get the new value
3428 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3436 dSP; dMARK; dTARGET;
3437 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3443 DIE(aTHX_ PL_no_func, "chown");
3453 TAINT_PROPER("chroot");
3454 PUSHi( chroot(tmps) >= 0 );
3457 DIE(aTHX_ PL_no_func, "chroot");
3463 dSP; dMARK; dTARGET;
3465 value = (I32)apply(PL_op->op_type, MARK, SP);
3473 dSP; dMARK; dTARGET;
3475 value = (I32)apply(PL_op->op_type, MARK, SP);
3483 dSP; dMARK; dTARGET;
3485 value = (I32)apply(PL_op->op_type, MARK, SP);
3497 char *tmps2 = POPpx;
3498 char *tmps = SvPV(TOPs, n_a);
3499 TAINT_PROPER("rename");
3501 anum = PerlLIO_rename(tmps, tmps2);
3503 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3504 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3507 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3508 (void)UNLINK(tmps2);
3509 if (!(anum = link(tmps, tmps2)))
3510 anum = UNLINK(tmps);
3523 char *tmps2 = POPpx;
3524 char *tmps = SvPV(TOPs, n_a);
3525 TAINT_PROPER("link");
3526 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3529 DIE(aTHX_ PL_no_func, "link");
3538 char *tmps2 = POPpx;
3539 char *tmps = SvPV(TOPs, n_a);
3540 TAINT_PROPER("symlink");
3541 SETi( symlink(tmps, tmps2) >= 0 );
3544 DIE(aTHX_ PL_no_func, "symlink");
3554 char buf[MAXPATHLEN];
3558 #ifndef INCOMPLETE_TAINTS
3562 len = readlink(tmps, buf, sizeof(buf) - 1);
3570 RETSETUNDEF; /* just pretend it's a normal file */
3574 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3576 S_dooneliner(pTHX_ char *cmd, char *filename)
3578 char *save_filename = filename;
3584 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3585 strcpy(cmdline, cmd);
3586 strcat(cmdline, " ");
3587 for (s = cmdline + strlen(cmdline); *filename; ) {
3592 myfp = PerlProc_popen(cmdline, "r");
3596 SV *tmpsv = sv_newmortal();
3597 /* Need to save/restore 'PL_rs' ?? */
3598 s = sv_gets(tmpsv, myfp, 0);
3599 (void)PerlProc_pclose(myfp);
3603 #ifdef HAS_SYS_ERRLIST
3608 /* you don't see this */
3610 #ifdef HAS_SYS_ERRLIST
3618 if (instr(s, errmsg)) {
3625 #define EACCES EPERM
3627 if (instr(s, "cannot make"))
3628 SETERRNO(EEXIST,RMS_FEX);
3629 else if (instr(s, "existing file"))
3630 SETERRNO(EEXIST,RMS_FEX);
3631 else if (instr(s, "ile exists"))
3632 SETERRNO(EEXIST,RMS_FEX);
3633 else if (instr(s, "non-exist"))
3634 SETERRNO(ENOENT,RMS_FNF);
3635 else if (instr(s, "does not exist"))
3636 SETERRNO(ENOENT,RMS_FNF);
3637 else if (instr(s, "not empty"))
3638 SETERRNO(EBUSY,SS_DEVOFFLINE);
3639 else if (instr(s, "cannot access"))
3640 SETERRNO(EACCES,RMS_PRV);
3642 SETERRNO(EPERM,RMS_PRV);
3645 else { /* some mkdirs return no failure indication */
3646 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3647 if (PL_op->op_type == OP_RMDIR)
3652 SETERRNO(EACCES,RMS_PRV); /* a guess */
3677 tmps = SvPV(TOPs, len);
3678 /* Different operating and file systems take differently to
3679 * trailing slashes. According to POSIX 1003.1 1996 Edition
3680 * any number of trailing slashes should be allowed.
3681 * Thusly we snip them away so that even non-conforming
3682 * systems are happy. */
3683 /* We should probably do this "filtering" for all
3684 * the functions that expect (potentially) directory names:
3685 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3686 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3687 if (len > 1 && tmps[len-1] == '/') {
3688 while (tmps[len-1] == '/' && len > 1)
3690 tmps = savepvn(tmps, len);
3694 TAINT_PROPER("mkdir");
3696 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3698 SETi( dooneliner("mkdir", tmps) );
3699 oldumask = PerlLIO_umask(0);
3700 PerlLIO_umask(oldumask);
3701 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3715 TAINT_PROPER("rmdir");
3717 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3719 XPUSHi( dooneliner("rmdir", tmps) );
3724 /* Directory calls. */
3728 #if defined(Direntry_t) && defined(HAS_READDIR)
3731 char *dirname = POPpx;
3733 register IO *io = GvIOn(gv);
3739 PerlDir_close(IoDIRP(io));
3740 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3746 SETERRNO(EBADF,RMS_DIR);
3749 DIE(aTHX_ PL_no_dir_func, "opendir");
3755 #if defined(Direntry_t) && defined(HAS_READDIR)
3757 #if !defined(I_DIRENT) && !defined(VMS)
3758 Direntry_t *readdir (DIR *);
3760 register Direntry_t *dp;
3762 register IO *io = GvIOn(gv);
3765 if (!io || !IoDIRP(io))
3768 if (GIMME == G_ARRAY) {
3770 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3772 sv = newSVpvn(dp->d_name, dp->d_namlen);
3774 sv = newSVpv(dp->d_name, 0);
3776 #ifndef INCOMPLETE_TAINTS
3777 if (!(IoFLAGS(io) & IOf_UNTAINT))
3780 XPUSHs(sv_2mortal(sv));
3784 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3787 sv = newSVpvn(dp->d_name, dp->d_namlen);
3789 sv = newSVpv(dp->d_name, 0);
3791 #ifndef INCOMPLETE_TAINTS
3792 if (!(IoFLAGS(io) & IOf_UNTAINT))
3795 XPUSHs(sv_2mortal(sv));
3801 SETERRNO(EBADF,RMS_ISI);
3802 if (GIMME == G_ARRAY)
3807 DIE(aTHX_ PL_no_dir_func, "readdir");
3813 #if defined(HAS_TELLDIR) || defined(telldir)
3815 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3816 /* XXX netbsd still seemed to.
3817 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3818 --JHI 1999-Feb-02 */
3819 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3820 long telldir (DIR *);
3823 register IO *io = GvIOn(gv);
3825 if (!io || !IoDIRP(io))
3828 PUSHi( PerlDir_tell(IoDIRP(io)) );
3832 SETERRNO(EBADF,RMS_ISI);
3835 DIE(aTHX_ PL_no_dir_func, "telldir");
3841 #if defined(HAS_SEEKDIR) || defined(seekdir)
3845 register IO *io = GvIOn(gv);
3847 if (!io || !IoDIRP(io))
3850 (void)PerlDir_seek(IoDIRP(io), along);
3855 SETERRNO(EBADF,RMS_ISI);
3858 DIE(aTHX_ PL_no_dir_func, "seekdir");
3864 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3867 register IO *io = GvIOn(gv);
3869 if (!io || !IoDIRP(io))
3872 (void)PerlDir_rewind(IoDIRP(io));
3876 SETERRNO(EBADF,RMS_ISI);
3879 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3885 #if defined(Direntry_t) && defined(HAS_READDIR)
3888 register IO *io = GvIOn(gv);
3890 if (!io || !IoDIRP(io))
3893 #ifdef VOID_CLOSEDIR
3894 PerlDir_close(IoDIRP(io));
3896 if (PerlDir_close(IoDIRP(io)) < 0) {
3897 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3906 SETERRNO(EBADF,RMS_IFI);
3909 DIE(aTHX_ PL_no_dir_func, "closedir");
3913 /* Process control. */
3923 PERL_FLUSHALL_FOR_CHILD;
3924 childpid = PerlProc_fork();
3929 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3930 SvREADONLY_off(GvSV(tmpgv));
3931 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3932 SvREADONLY_on(GvSV(tmpgv));
3934 #ifdef THREADS_HAVE_PIDS
3935 PL_ppid = (IV)getppid();
3937 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3942 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3947 PERL_FLUSHALL_FOR_CHILD;
3948 childpid = PerlProc_fork();
3954 DIE(aTHX_ PL_no_func, "fork");
3961 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3966 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3967 childpid = wait4pid(-1, &argflags, 0);
3969 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3974 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3975 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3976 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3978 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3983 DIE(aTHX_ PL_no_func, "wait");
3989 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3997 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3998 childpid = wait4pid(childpid, &argflags, optype);
4000 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4005 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4006 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4007 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4009 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4014 DIE(aTHX_ PL_no_func, "waitpid");
4020 dSP; dMARK; dORIGMARK; dTARGET;
4028 while (++MARK <= SP) {
4029 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4034 TAINT_PROPER("system");
4036 PERL_FLUSHALL_FOR_CHILD;
4037 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4042 if (PerlProc_pipe(pp) >= 0)
4044 while ((childpid = PerlProc_fork()) == -1) {
4045 if (errno != EAGAIN) {
4050 PerlLIO_close(pp[0]);
4051 PerlLIO_close(pp[1]);
4058 Sigsave_t ihand,qhand; /* place to save signals during system() */
4062 PerlLIO_close(pp[1]);
4064 rsignal_save(SIGINT, SIG_IGN, &ihand);
4065 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4068 result = wait4pid(childpid, &status, 0);
4069 } while (result == -1 && errno == EINTR);
4071 (void)rsignal_restore(SIGINT, &ihand);
4072 (void)rsignal_restore(SIGQUIT, &qhand);
4074 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4075 do_execfree(); /* free any memory child malloced on fork */
4081 while (n < sizeof(int)) {
4082 n1 = PerlLIO_read(pp[0],
4083 (void*)(((char*)&errkid)+n),
4089 PerlLIO_close(pp[0]);
4090 if (n) { /* Error */
4091 if (n != sizeof(int))
4092 DIE(aTHX_ "panic: kid popen errno read");
4093 errno = errkid; /* Propagate errno from kid */
4094 STATUS_CURRENT = -1;
4097 PUSHi(STATUS_CURRENT);
4101 PerlLIO_close(pp[0]);
4102 #if defined(HAS_FCNTL) && defined(F_SETFD)
4103 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4106 if (PL_op->op_flags & OPf_STACKED) {
4107 SV *really = *++MARK;
4108 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4110 else if (SP - MARK != 1)
4111 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4113 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4117 #else /* ! FORK or VMS or OS/2 */
4120 if (PL_op->op_flags & OPf_STACKED) {
4121 SV *really = *++MARK;
4123 value = (I32)do_aspawn(really, MARK, SP);
4125 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4128 else if (SP - MARK != 1) {
4130 value = (I32)do_aspawn(Nullsv, MARK, SP);
4132 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4136 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4138 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4140 STATUS_NATIVE_SET(value);
4143 PUSHi(result ? value : STATUS_CURRENT);
4144 #endif /* !FORK or VMS */
4150 dSP; dMARK; dORIGMARK; dTARGET;
4156 while (++MARK <= SP) {
4157 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4162 TAINT_PROPER("exec");
4164 PERL_FLUSHALL_FOR_CHILD;
4165 if (PL_op->op_flags & OPf_STACKED) {
4166 SV *really = *++MARK;
4167 value = (I32)do_aexec(really, MARK, SP);
4169 else if (SP - MARK != 1)
4171 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4175 (void ) do_aspawn(Nullsv, MARK, SP);
4179 value = (I32)do_aexec(Nullsv, MARK, SP);
4184 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4187 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4190 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4203 dSP; dMARK; dTARGET;
4205 value = (I32)apply(PL_op->op_type, MARK, SP);
4210 DIE(aTHX_ PL_no_func, "kill");
4218 # ifdef THREADS_HAVE_PIDS
4221 XPUSHi( getppid() );
4225 DIE(aTHX_ PL_no_func, "getppid");
4241 pgrp = (I32)BSD_GETPGRP(pid);
4243 if (pid != 0 && pid != PerlProc_getpid())
4244 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4250 DIE(aTHX_ PL_no_func, "getpgrp()");
4269 TAINT_PROPER("setpgrp");
4271 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4273 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4274 || (pid != 0 && pid != PerlProc_getpid()))
4276 DIE(aTHX_ "setpgrp can't take arguments");
4278 SETi( setpgrp() >= 0 );
4279 #endif /* USE_BSDPGRP */
4282 DIE(aTHX_ PL_no_func, "setpgrp()");
4288 #ifdef HAS_GETPRIORITY
4292 SETi( getpriority(which, who) );
4295 DIE(aTHX_ PL_no_func, "getpriority()");
4301 #ifdef HAS_SETPRIORITY
4306 TAINT_PROPER("setpriority");
4307 SETi( setpriority(which, who, niceval) >= 0 );
4310 DIE(aTHX_ PL_no_func, "setpriority()");
4320 XPUSHn( time(Null(Time_t*)) );
4322 XPUSHi( time(Null(Time_t*)) );
4333 (void)PerlProc_times(&PL_timesbuf);
4335 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4336 /* struct tms, though same data */
4340 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4341 if (GIMME == G_ARRAY) {
4342 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4343 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4344 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4348 DIE(aTHX_ "times not implemented");
4349 #endif /* HAS_TIMES */
4362 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4363 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4364 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4370 when = (Time_t)SvNVx(POPs);
4372 when = (Time_t)SvIVx(POPs);
4375 if (PL_op->op_type == OP_LOCALTIME)
4376 tmbuf = localtime(&when);
4378 tmbuf = gmtime(&when);
4380 if (GIMME != G_ARRAY) {
4386 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4387 dayname[tmbuf->tm_wday],
4388 monname[tmbuf->tm_mon],
4393 tmbuf->tm_year + 1900);
4394 PUSHs(sv_2mortal(tsv));
4399 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4400 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4401 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4402 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4403 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4404 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4405 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4406 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4407 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4418 anum = alarm((unsigned int)anum);
4425 DIE(aTHX_ PL_no_func, "alarm");
4436 (void)time(&lasttime);
4441 PerlProc_sleep((unsigned int)duration);
4444 XPUSHi(when - lasttime);
4448 /* Shared memory. */
4462 return pp_shmwrite();
4467 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4468 dSP; dMARK; dTARGET;
4469 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4478 /* Message passing. */
4492 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4493 dSP; dMARK; dTARGET;
4494 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4505 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4506 dSP; dMARK; dTARGET;
4507 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4520 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4521 dSP; dMARK; dTARGET;
4522 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4529 DIE(aTHX_ "System V IPC is not implemented on this machine");
4535 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4536 dSP; dMARK; dTARGET;
4537 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4545 PUSHp(zero_but_true, ZBTLEN);
4555 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4556 dSP; dMARK; dTARGET;
4557 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4566 /* Get system info. */
4570 #ifdef HAS_GETHOSTBYNAME
4571 return pp_ghostent();
4573 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4579 #ifdef HAS_GETHOSTBYADDR
4580 return pp_ghostent();
4582 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4588 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4590 I32 which = PL_op->op_type;
4591 register char **elem;
4593 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4594 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4595 struct hostent *gethostbyname(Netdb_name_t);
4596 struct hostent *gethostent(void);
4598 struct hostent *hent;
4603 if (which == OP_GHBYNAME) {
4604 #ifdef HAS_GETHOSTBYNAME
4605 char* name = POPpbytex;
4606 hent = PerlSock_gethostbyname(name);
4608 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4611 else if (which == OP_GHBYADDR) {
4612 #ifdef HAS_GETHOSTBYADDR
4613 int addrtype = POPi;
4616 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4618 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4620 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4624 #ifdef HAS_GETHOSTENT
4625 hent = PerlSock_gethostent();
4627 DIE(aTHX_ PL_no_sock_func, "gethostent");
4630 #ifdef HOST_NOT_FOUND
4632 #ifdef USE_REENTRANT_API
4633 # ifdef USE_GETHOSTENT_ERRNO
4634 h_errno = PL_reentrant_buffer->_gethostent_errno;
4637 STATUS_NATIVE_SET(h_errno);
4641 if (GIMME != G_ARRAY) {
4642 PUSHs(sv = sv_newmortal());
4644 if (which == OP_GHBYNAME) {
4646 sv_setpvn(sv, hent->h_addr, hent->h_length);
4649 sv_setpv(sv, (char*)hent->h_name);
4655 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4656 sv_setpv(sv, (char*)hent->h_name);
4657 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4658 for (elem = hent->h_aliases; elem && *elem; elem++) {
4659 sv_catpv(sv, *elem);
4661 sv_catpvn(sv, " ", 1);
4663 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4664 sv_setiv(sv, (IV)hent->h_addrtype);
4665 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4666 len = hent->h_length;
4667 sv_setiv(sv, (IV)len);
4669 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4670 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4671 sv_setpvn(sv, *elem, len);
4674 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4676 sv_setpvn(sv, hent->h_addr, len);
4681 DIE(aTHX_ PL_no_sock_func, "gethostent");
4687 #ifdef HAS_GETNETBYNAME
4688 return pp_gnetent();
4690 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4696 #ifdef HAS_GETNETBYADDR
4697 return pp_gnetent();
4699 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4705 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4707 I32 which = PL_op->op_type;
4708 register char **elem;
4710 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4711 struct netent *getnetbyaddr(Netdb_net_t, int);
4712 struct netent *getnetbyname(Netdb_name_t);
4713 struct netent *getnetent(void);
4715 struct netent *nent;
4718 if (which == OP_GNBYNAME){
4719 #ifdef HAS_GETNETBYNAME
4720 char *name = POPpbytex;
4721 nent = PerlSock_getnetbyname(name);
4723 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4726 else if (which == OP_GNBYADDR) {
4727 #ifdef HAS_GETNETBYADDR
4728 int addrtype = POPi;
4729 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4730 nent = PerlSock_getnetbyaddr(addr, addrtype);
4732 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4736 #ifdef HAS_GETNETENT
4737 nent = PerlSock_getnetent();
4739 DIE(aTHX_ PL_no_sock_func, "getnetent");
4742 #ifdef HOST_NOT_FOUND
4744 #ifdef USE_REENTRANT_API
4745 # ifdef USE_GETNETENT_ERRNO
4746 h_errno = PL_reentrant_buffer->_getnetent_errno;
4749 STATUS_NATIVE_SET(h_errno);
4754 if (GIMME != G_ARRAY) {
4755 PUSHs(sv = sv_newmortal());
4757 if (which == OP_GNBYNAME)
4758 sv_setiv(sv, (IV)nent->n_net);
4760 sv_setpv(sv, nent->n_name);
4766 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4767 sv_setpv(sv, nent->n_name);
4768 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4769 for (elem = nent->n_aliases; elem && *elem; elem++) {
4770 sv_catpv(sv, *elem);
4772 sv_catpvn(sv, " ", 1);
4774 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4775 sv_setiv(sv, (IV)nent->n_addrtype);
4776 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4777 sv_setiv(sv, (IV)nent->n_net);
4782 DIE(aTHX_ PL_no_sock_func, "getnetent");
4788 #ifdef HAS_GETPROTOBYNAME
4789 return pp_gprotoent();
4791 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4797 #ifdef HAS_GETPROTOBYNUMBER
4798 return pp_gprotoent();
4800 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4806 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4808 I32 which = PL_op->op_type;
4809 register char **elem;
4811 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4812 struct protoent *getprotobyname(Netdb_name_t);
4813 struct protoent *getprotobynumber(int);
4814 struct protoent *getprotoent(void);
4816 struct protoent *pent;
4819 if (which == OP_GPBYNAME) {
4820 #ifdef HAS_GETPROTOBYNAME
4821 char* name = POPpbytex;
4822 pent = PerlSock_getprotobyname(name);
4824 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4827 else if (which == OP_GPBYNUMBER) {
4828 #ifdef HAS_GETPROTOBYNUMBER
4830 pent = PerlSock_getprotobynumber(number);
4832 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4836 #ifdef HAS_GETPROTOENT
4837 pent = PerlSock_getprotoent();
4839 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4843 if (GIMME != G_ARRAY) {
4844 PUSHs(sv = sv_newmortal());
4846 if (which == OP_GPBYNAME)
4847 sv_setiv(sv, (IV)pent->p_proto);
4849 sv_setpv(sv, pent->p_name);
4855 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4856 sv_setpv(sv, pent->p_name);
4857 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4858 for (elem = pent->p_aliases; elem && *elem; elem++) {
4859 sv_catpv(sv, *elem);
4861 sv_catpvn(sv, " ", 1);
4863 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4864 sv_setiv(sv, (IV)pent->p_proto);
4869 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4875 #ifdef HAS_GETSERVBYNAME
4876 return pp_gservent();
4878 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4884 #ifdef HAS_GETSERVBYPORT
4885 return pp_gservent();
4887 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4893 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4895 I32 which = PL_op->op_type;
4896 register char **elem;
4898 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4899 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4900 struct servent *getservbyport(int, Netdb_name_t);
4901 struct servent *getservent(void);
4903 struct servent *sent;
4906 if (which == OP_GSBYNAME) {
4907 #ifdef HAS_GETSERVBYNAME
4908 char *proto = POPpbytex;
4909 char *name = POPpbytex;
4911 if (proto && !*proto)
4914 sent = PerlSock_getservbyname(name, proto);
4916 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4919 else if (which == OP_GSBYPORT) {
4920 #ifdef HAS_GETSERVBYPORT
4921 char *proto = POPpbytex;
4922 unsigned short port = (unsigned short)POPu;
4924 if (proto && !*proto)
4928 port = PerlSock_htons(port);
4930 sent = PerlSock_getservbyport(port, proto);
4932 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4936 #ifdef HAS_GETSERVENT
4937 sent = PerlSock_getservent();
4939 DIE(aTHX_ PL_no_sock_func, "getservent");
4943 if (GIMME != G_ARRAY) {
4944 PUSHs(sv = sv_newmortal());
4946 if (which == OP_GSBYNAME) {
4948 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4950 sv_setiv(sv, (IV)(sent->s_port));
4954 sv_setpv(sv, sent->s_name);
4960 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4961 sv_setpv(sv, sent->s_name);
4962 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4963 for (elem = sent->s_aliases; elem && *elem; elem++) {
4964 sv_catpv(sv, *elem);
4966 sv_catpvn(sv, " ", 1);
4968 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4970 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4972 sv_setiv(sv, (IV)(sent->s_port));
4974 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4975 sv_setpv(sv, sent->s_proto);
4980 DIE(aTHX_ PL_no_sock_func, "getservent");
4986 #ifdef HAS_SETHOSTENT
4988 PerlSock_sethostent(TOPi);
4991 DIE(aTHX_ PL_no_sock_func, "sethostent");
4997 #ifdef HAS_SETNETENT
4999 PerlSock_setnetent(TOPi);
5002 DIE(aTHX_ PL_no_sock_func, "setnetent");
5008 #ifdef HAS_SETPROTOENT
5010 PerlSock_setprotoent(TOPi);
5013 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5019 #ifdef HAS_SETSERVENT
5021 PerlSock_setservent(TOPi);
5024 DIE(aTHX_ PL_no_sock_func, "setservent");
5030 #ifdef HAS_ENDHOSTENT
5032 PerlSock_endhostent();
5036 DIE(aTHX_ PL_no_sock_func, "endhostent");
5042 #ifdef HAS_ENDNETENT
5044 PerlSock_endnetent();
5048 DIE(aTHX_ PL_no_sock_func, "endnetent");
5054 #ifdef HAS_ENDPROTOENT
5056 PerlSock_endprotoent();
5060 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5066 #ifdef HAS_ENDSERVENT
5068 PerlSock_endservent();
5072 DIE(aTHX_ PL_no_sock_func, "endservent");
5081 DIE(aTHX_ PL_no_func, "getpwnam");
5090 DIE(aTHX_ PL_no_func, "getpwuid");
5098 I32 which = PL_op->op_type;
5101 struct passwd *pwent = NULL;
5103 * We currently support only the SysV getsp* shadow password interface.
5104 * The interface is declared in <shadow.h> and often one needs to link
5105 * with -lsecurity or some such.
5106 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5109 * AIX getpwnam() is clever enough to return the encrypted password
5110 * only if the caller (euid?) is root.
5112 * There are at least two other shadow password APIs. Many platforms
5113 * seem to contain more than one interface for accessing the shadow
5114 * password databases, possibly for compatibility reasons.
5115 * The getsp*() is by far he simplest one, the other two interfaces
5116 * are much more complicated, but also very similar to each other.
5121 * struct pr_passwd *getprpw*();
5122 * The password is in
5123 * char getprpw*(...).ufld.fd_encrypt[]
5124 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5129 * struct es_passwd *getespw*();
5130 * The password is in
5131 * char *(getespw*(...).ufld.fd_encrypt)
5132 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5134 * Mention I_PROT here so that Configure probes for it.
5136 * In HP-UX for getprpw*() the manual page claims that one should include
5137 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5138 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5139 * and pp_sys.c already includes <shadow.h> if there is such.
5141 * Note that <sys/security.h> is already probed for, but currently
5142 * it is only included in special cases.
5144 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5145 * be preferred interface, even though also the getprpw*() interface
5146 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5147 * One also needs to call set_auth_parameters() in main() before
5148 * doing anything else, whether one is using getespw*() or getprpw*().
5150 * Note that accessing the shadow databases can be magnitudes
5151 * slower than accessing the standard databases.
5159 char* name = POPpbytex;
5160 pwent = getpwnam(name);
5166 pwent = getpwuid(uid);
5170 # ifdef HAS_GETPWENT
5172 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5173 if (pwent) pwent = getpwnam(pwent->pw_name);
5176 DIE(aTHX_ PL_no_func, "getpwent");
5182 if (GIMME != G_ARRAY) {
5183 PUSHs(sv = sv_newmortal());
5185 if (which == OP_GPWNAM)
5186 # if Uid_t_sign <= 0
5187 sv_setiv(sv, (IV)pwent->pw_uid);
5189 sv_setuv(sv, (UV)pwent->pw_uid);
5192 sv_setpv(sv, pwent->pw_name);
5198 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5199 sv_setpv(sv, pwent->pw_name);
5201 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5203 /* If we have getspnam(), we try to dig up the shadow
5204 * password. If we are underprivileged, the shadow
5205 * interface will set the errno to EACCES or similar,
5206 * and return a null pointer. If this happens, we will
5207 * use the dummy password (usually "*" or "x") from the
5208 * standard password database.
5210 * In theory we could skip the shadow call completely
5211 * if euid != 0 but in practice we cannot know which
5212 * security measures are guarding the shadow databases
5213 * on a random platform.
5215 * Resist the urge to use additional shadow interfaces.
5216 * Divert the urge to writing an extension instead.
5219 # ifdef HAS_GETSPNAM
5221 struct spwd *spwent;
5222 int saverrno; /* Save and restore errno so that
5223 * underprivileged attempts seem
5224 * to have never made the unsccessful
5225 * attempt to retrieve the shadow password. */
5228 spwent = getspnam(pwent->pw_name);
5230 if (spwent && spwent->sp_pwdp)
5231 sv_setpv(sv, spwent->sp_pwdp);
5235 if (!SvPOK(sv)) /* Use the standard password, then. */
5236 sv_setpv(sv, pwent->pw_passwd);
5239 # ifndef INCOMPLETE_TAINTS
5240 /* passwd is tainted because user himself can diddle with it.
5241 * admittedly not much and in a very limited way, but nevertheless. */
5245 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5246 # if Uid_t_sign <= 0
5247 sv_setiv(sv, (IV)pwent->pw_uid);
5249 sv_setuv(sv, (UV)pwent->pw_uid);
5252 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5253 # if Uid_t_sign <= 0
5254 sv_setiv(sv, (IV)pwent->pw_gid);
5256 sv_setuv(sv, (UV)pwent->pw_gid);
5258 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5259 * because of the poor interface of the Perl getpw*(),
5260 * not because there's some standard/convention saying so.
5261 * A better interface would have been to return a hash,
5262 * but we are accursed by our history, alas. --jhi. */
5263 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5265 sv_setiv(sv, (IV)pwent->pw_change);
5268 sv_setiv(sv, (IV)pwent->pw_quota);
5271 sv_setpv(sv, pwent->pw_age);
5276 /* pw_class and pw_comment are mutually exclusive--.
5277 * see the above note for pw_change, pw_quota, and pw_age. */
5278 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5280 sv_setpv(sv, pwent->pw_class);
5283 sv_setpv(sv, pwent->pw_comment);
5287 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5289 sv_setpv(sv, pwent->pw_gecos);
5291 # ifndef INCOMPLETE_TAINTS
5292 /* pw_gecos is tainted because user himself can diddle with it. */
5296 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5297 sv_setpv(sv, pwent->pw_dir);
5299 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5300 sv_setpv(sv, pwent->pw_shell);
5301 # ifndef INCOMPLETE_TAINTS
5302 /* pw_shell is tainted because user himself can diddle with it. */
5307 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5308 sv_setiv(sv, (IV)pwent->pw_expire);
5313 DIE(aTHX_ PL_no_func, "getpwent");
5319 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5324 DIE(aTHX_ PL_no_func, "setpwent");
5330 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5335 DIE(aTHX_ PL_no_func, "endpwent");
5344 DIE(aTHX_ PL_no_func, "getgrnam");
5353 DIE(aTHX_ PL_no_func, "getgrgid");
5361 I32 which = PL_op->op_type;
5362 register char **elem;
5364 struct group *grent;
5367 if (which == OP_GGRNAM) {
5368 char* name = POPpbytex;
5369 grent = (struct group *)getgrnam(name);
5371 else if (which == OP_GGRGID) {
5373 grent = (struct group *)getgrgid(gid);
5377 grent = (struct group *)getgrent();
5379 DIE(aTHX_ PL_no_func, "getgrent");
5383 if (GIMME != G_ARRAY) {
5384 PUSHs(sv = sv_newmortal());
5386 if (which == OP_GGRNAM)
5387 sv_setiv(sv, (IV)grent->gr_gid);
5389 sv_setpv(sv, grent->gr_name);
5395 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5396 sv_setpv(sv, grent->gr_name);
5398 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5400 sv_setpv(sv, grent->gr_passwd);
5403 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5404 sv_setiv(sv, (IV)grent->gr_gid);
5406 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5407 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5408 /* In UNICOS/mk (_CRAYMPP) the multithreading
5409 * versions (getgrnam_r, getgrgid_r)
5410 * seem to return an illegal pointer
5411 * as the group members list, gr_mem.
5412 * getgrent() doesn't even have a _r version
5413 * but the gr_mem is poisonous anyway.
5414 * So yes, you cannot get the list of group
5415 * members if building multithreaded in UNICOS/mk. */
5416 for (elem = grent->gr_mem; elem && *elem; elem++) {
5417 sv_catpv(sv, *elem);
5419 sv_catpvn(sv, " ", 1);
5426 DIE(aTHX_ PL_no_func, "getgrent");
5432 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5437 DIE(aTHX_ PL_no_func, "setgrent");
5443 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5448 DIE(aTHX_ PL_no_func, "endgrent");
5458 if (!(tmps = PerlProc_getlogin()))
5460 PUSHp(tmps, strlen(tmps));
5463 DIE(aTHX_ PL_no_func, "getlogin");
5467 /* Miscellaneous. */
5472 dSP; dMARK; dORIGMARK; dTARGET;
5473 register I32 items = SP - MARK;
5474 unsigned long a[20];
5480 while (++MARK <= SP) {
5481 if (SvTAINTED(*MARK)) {
5487 TAINT_PROPER("syscall");
5490 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5491 * or where sizeof(long) != sizeof(char*). But such machines will
5492 * not likely have syscall implemented either, so who cares?
5494 while (++MARK <= SP) {
5495 if (SvNIOK(*MARK) || !i)
5496 a[i++] = SvIV(*MARK);
5497 else if (*MARK == &PL_sv_undef)
5500 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5506 DIE(aTHX_ "Too many args to syscall");
5508 DIE(aTHX_ "Too few args to syscall");
5510 retval = syscall(a[0]);
5513 retval = syscall(a[0],a[1]);
5516 retval = syscall(a[0],a[1],a[2]);
5519 retval = syscall(a[0],a[1],a[2],a[3]);
5522 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5525 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5528 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5531 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5535 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5538 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5541 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5545 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5549 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5553 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5554 a[10],a[11],a[12],a[13]);
5556 #endif /* atarist */
5562 DIE(aTHX_ PL_no_func, "syscall");
5566 #ifdef FCNTL_EMULATE_FLOCK
5568 /* XXX Emulate flock() with fcntl().
5569 What's really needed is a good file locking module.
5573 fcntl_emulate_flock(int fd, int operation)
5577 switch (operation & ~LOCK_NB) {
5579 flock.l_type = F_RDLCK;
5582 flock.l_type = F_WRLCK;
5585 flock.l_type = F_UNLCK;
5591 flock.l_whence = SEEK_SET;
5592 flock.l_start = flock.l_len = (Off_t)0;
5594 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5597 #endif /* FCNTL_EMULATE_FLOCK */
5599 #ifdef LOCKF_EMULATE_FLOCK
5601 /* XXX Emulate flock() with lockf(). This is just to increase
5602 portability of scripts. The calls are not completely
5603 interchangeable. What's really needed is a good file
5607 /* The lockf() constants might have been defined in <unistd.h>.
5608 Unfortunately, <unistd.h> causes troubles on some mixed
5609 (BSD/POSIX) systems, such as SunOS 4.1.3.
5611 Further, the lockf() constants aren't POSIX, so they might not be
5612 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5613 just stick in the SVID values and be done with it. Sigh.
5617 # define F_ULOCK 0 /* Unlock a previously locked region */
5620 # define F_LOCK 1 /* Lock a region for exclusive use */
5623 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5626 # define F_TEST 3 /* Test a region for other processes locks */
5630 lockf_emulate_flock(int fd, int operation)
5636 /* flock locks entire file so for lockf we need to do the same */
5638 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5639 if (pos > 0) /* is seekable and needs to be repositioned */
5640 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5641 pos = -1; /* seek failed, so don't seek back afterwards */
5644 switch (operation) {
5646 /* LOCK_SH - get a shared lock */
5648 /* LOCK_EX - get an exclusive lock */
5650 i = lockf (fd, F_LOCK, 0);
5653 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5654 case LOCK_SH|LOCK_NB:
5655 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5656 case LOCK_EX|LOCK_NB:
5657 i = lockf (fd, F_TLOCK, 0);
5659 if ((errno == EAGAIN) || (errno == EACCES))
5660 errno = EWOULDBLOCK;
5663 /* LOCK_UN - unlock (non-blocking is a no-op) */
5665 case LOCK_UN|LOCK_NB:
5666 i = lockf (fd, F_ULOCK, 0);
5669 /* Default - can't decipher operation */
5676 if (pos > 0) /* need to restore position of the handle */
5677 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5682 #endif /* LOCKF_EMULATE_FLOCK */