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 #ifdef PERL_OLD_SIGNALS
3967 childpid = wait4pid(-1, &argflags, 0);
3969 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 && errno == EINTR) {
3973 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3974 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3975 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3977 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3982 DIE(aTHX_ PL_no_func, "wait");
3988 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3996 #ifdef PERL_OLD_SIGNALS
3997 childpid = wait4pid(childpid, &argflags, optype);
3999 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 && errno == EINTR) {
4003 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4004 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4005 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4007 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4012 DIE(aTHX_ PL_no_func, "waitpid");
4018 dSP; dMARK; dORIGMARK; dTARGET;
4026 while (++MARK <= SP) {
4027 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4032 TAINT_PROPER("system");
4034 PERL_FLUSHALL_FOR_CHILD;
4035 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4040 if (PerlProc_pipe(pp) >= 0)
4042 while ((childpid = PerlProc_fork()) == -1) {
4043 if (errno != EAGAIN) {
4048 PerlLIO_close(pp[0]);
4049 PerlLIO_close(pp[1]);
4056 Sigsave_t ihand,qhand; /* place to save signals during system() */
4060 PerlLIO_close(pp[1]);
4062 rsignal_save(SIGINT, SIG_IGN, &ihand);
4063 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4066 result = wait4pid(childpid, &status, 0);
4067 } while (result == -1 && errno == EINTR);
4069 (void)rsignal_restore(SIGINT, &ihand);
4070 (void)rsignal_restore(SIGQUIT, &qhand);
4072 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4073 do_execfree(); /* free any memory child malloced on fork */
4079 while (n < sizeof(int)) {
4080 n1 = PerlLIO_read(pp[0],
4081 (void*)(((char*)&errkid)+n),
4087 PerlLIO_close(pp[0]);
4088 if (n) { /* Error */
4089 if (n != sizeof(int))
4090 DIE(aTHX_ "panic: kid popen errno read");
4091 errno = errkid; /* Propagate errno from kid */
4092 STATUS_CURRENT = -1;
4095 PUSHi(STATUS_CURRENT);
4099 PerlLIO_close(pp[0]);
4100 #if defined(HAS_FCNTL) && defined(F_SETFD)
4101 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4104 if (PL_op->op_flags & OPf_STACKED) {
4105 SV *really = *++MARK;
4106 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4108 else if (SP - MARK != 1)
4109 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4111 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4115 #else /* ! FORK or VMS or OS/2 */
4118 if (PL_op->op_flags & OPf_STACKED) {
4119 SV *really = *++MARK;
4121 value = (I32)do_aspawn(really, MARK, SP);
4123 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4126 else if (SP - MARK != 1) {
4128 value = (I32)do_aspawn(Nullsv, MARK, SP);
4130 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4134 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4136 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4138 STATUS_NATIVE_SET(value);
4141 PUSHi(result ? value : STATUS_CURRENT);
4142 #endif /* !FORK or VMS */
4148 dSP; dMARK; dORIGMARK; dTARGET;
4154 while (++MARK <= SP) {
4155 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4160 TAINT_PROPER("exec");
4162 PERL_FLUSHALL_FOR_CHILD;
4163 if (PL_op->op_flags & OPf_STACKED) {
4164 SV *really = *++MARK;
4165 value = (I32)do_aexec(really, MARK, SP);
4167 else if (SP - MARK != 1)
4169 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4173 (void ) do_aspawn(Nullsv, MARK, SP);
4177 value = (I32)do_aexec(Nullsv, MARK, SP);
4182 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4185 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4188 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4201 dSP; dMARK; dTARGET;
4203 value = (I32)apply(PL_op->op_type, MARK, SP);
4208 DIE(aTHX_ PL_no_func, "kill");
4216 # ifdef THREADS_HAVE_PIDS
4219 XPUSHi( getppid() );
4223 DIE(aTHX_ PL_no_func, "getppid");
4239 pgrp = (I32)BSD_GETPGRP(pid);
4241 if (pid != 0 && pid != PerlProc_getpid())
4242 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4248 DIE(aTHX_ PL_no_func, "getpgrp()");
4267 TAINT_PROPER("setpgrp");
4269 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4271 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4272 || (pid != 0 && pid != PerlProc_getpid()))
4274 DIE(aTHX_ "setpgrp can't take arguments");
4276 SETi( setpgrp() >= 0 );
4277 #endif /* USE_BSDPGRP */
4280 DIE(aTHX_ PL_no_func, "setpgrp()");
4286 #ifdef HAS_GETPRIORITY
4290 SETi( getpriority(which, who) );
4293 DIE(aTHX_ PL_no_func, "getpriority()");
4299 #ifdef HAS_SETPRIORITY
4304 TAINT_PROPER("setpriority");
4305 SETi( setpriority(which, who, niceval) >= 0 );
4308 DIE(aTHX_ PL_no_func, "setpriority()");
4318 XPUSHn( time(Null(Time_t*)) );
4320 XPUSHi( time(Null(Time_t*)) );
4331 (void)PerlProc_times(&PL_timesbuf);
4333 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4334 /* struct tms, though same data */
4338 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4339 if (GIMME == G_ARRAY) {
4340 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4341 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4342 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4346 DIE(aTHX_ "times not implemented");
4347 #endif /* HAS_TIMES */
4360 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4361 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4362 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4368 when = (Time_t)SvNVx(POPs);
4370 when = (Time_t)SvIVx(POPs);
4373 if (PL_op->op_type == OP_LOCALTIME)
4374 tmbuf = localtime(&when);
4376 tmbuf = gmtime(&when);
4378 if (GIMME != G_ARRAY) {
4384 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4385 dayname[tmbuf->tm_wday],
4386 monname[tmbuf->tm_mon],
4391 tmbuf->tm_year + 1900);
4392 PUSHs(sv_2mortal(tsv));
4397 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4398 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4399 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4400 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4401 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4402 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4403 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4404 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4405 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4416 anum = alarm((unsigned int)anum);
4423 DIE(aTHX_ PL_no_func, "alarm");
4434 (void)time(&lasttime);
4439 PerlProc_sleep((unsigned int)duration);
4442 XPUSHi(when - lasttime);
4446 /* Shared memory. */
4460 return pp_shmwrite();
4465 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4466 dSP; dMARK; dTARGET;
4467 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4476 /* Message passing. */
4490 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4491 dSP; dMARK; dTARGET;
4492 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4503 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4504 dSP; dMARK; dTARGET;
4505 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4518 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4519 dSP; dMARK; dTARGET;
4520 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4527 DIE(aTHX_ "System V IPC is not implemented on this machine");
4533 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4534 dSP; dMARK; dTARGET;
4535 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4543 PUSHp(zero_but_true, ZBTLEN);
4553 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4554 dSP; dMARK; dTARGET;
4555 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4564 /* Get system info. */
4568 #ifdef HAS_GETHOSTBYNAME
4569 return pp_ghostent();
4571 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4577 #ifdef HAS_GETHOSTBYADDR
4578 return pp_ghostent();
4580 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4586 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4588 I32 which = PL_op->op_type;
4589 register char **elem;
4591 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4592 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4593 struct hostent *gethostbyname(Netdb_name_t);
4594 struct hostent *gethostent(void);
4596 struct hostent *hent;
4601 if (which == OP_GHBYNAME) {
4602 #ifdef HAS_GETHOSTBYNAME
4603 char* name = POPpbytex;
4604 hent = PerlSock_gethostbyname(name);
4606 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4609 else if (which == OP_GHBYADDR) {
4610 #ifdef HAS_GETHOSTBYADDR
4611 int addrtype = POPi;
4614 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4616 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4618 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4622 #ifdef HAS_GETHOSTENT
4623 hent = PerlSock_gethostent();
4625 DIE(aTHX_ PL_no_sock_func, "gethostent");
4628 #ifdef HOST_NOT_FOUND
4630 #ifdef USE_REENTRANT_API
4631 # ifdef USE_GETHOSTENT_ERRNO
4632 h_errno = PL_reentrant_buffer->_gethostent_errno;
4635 STATUS_NATIVE_SET(h_errno);
4639 if (GIMME != G_ARRAY) {
4640 PUSHs(sv = sv_newmortal());
4642 if (which == OP_GHBYNAME) {
4644 sv_setpvn(sv, hent->h_addr, hent->h_length);
4647 sv_setpv(sv, (char*)hent->h_name);
4653 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4654 sv_setpv(sv, (char*)hent->h_name);
4655 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4656 for (elem = hent->h_aliases; elem && *elem; elem++) {
4657 sv_catpv(sv, *elem);
4659 sv_catpvn(sv, " ", 1);
4661 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4662 sv_setiv(sv, (IV)hent->h_addrtype);
4663 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4664 len = hent->h_length;
4665 sv_setiv(sv, (IV)len);
4667 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4668 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4669 sv_setpvn(sv, *elem, len);
4672 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4674 sv_setpvn(sv, hent->h_addr, len);
4679 DIE(aTHX_ PL_no_sock_func, "gethostent");
4685 #ifdef HAS_GETNETBYNAME
4686 return pp_gnetent();
4688 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4694 #ifdef HAS_GETNETBYADDR
4695 return pp_gnetent();
4697 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4703 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4705 I32 which = PL_op->op_type;
4706 register char **elem;
4708 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4709 struct netent *getnetbyaddr(Netdb_net_t, int);
4710 struct netent *getnetbyname(Netdb_name_t);
4711 struct netent *getnetent(void);
4713 struct netent *nent;
4716 if (which == OP_GNBYNAME){
4717 #ifdef HAS_GETNETBYNAME
4718 char *name = POPpbytex;
4719 nent = PerlSock_getnetbyname(name);
4721 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4724 else if (which == OP_GNBYADDR) {
4725 #ifdef HAS_GETNETBYADDR
4726 int addrtype = POPi;
4727 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4728 nent = PerlSock_getnetbyaddr(addr, addrtype);
4730 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4734 #ifdef HAS_GETNETENT
4735 nent = PerlSock_getnetent();
4737 DIE(aTHX_ PL_no_sock_func, "getnetent");
4740 #ifdef HOST_NOT_FOUND
4742 #ifdef USE_REENTRANT_API
4743 # ifdef USE_GETNETENT_ERRNO
4744 h_errno = PL_reentrant_buffer->_getnetent_errno;
4747 STATUS_NATIVE_SET(h_errno);
4752 if (GIMME != G_ARRAY) {
4753 PUSHs(sv = sv_newmortal());
4755 if (which == OP_GNBYNAME)
4756 sv_setiv(sv, (IV)nent->n_net);
4758 sv_setpv(sv, nent->n_name);
4764 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4765 sv_setpv(sv, nent->n_name);
4766 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4767 for (elem = nent->n_aliases; elem && *elem; elem++) {
4768 sv_catpv(sv, *elem);
4770 sv_catpvn(sv, " ", 1);
4772 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4773 sv_setiv(sv, (IV)nent->n_addrtype);
4774 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4775 sv_setiv(sv, (IV)nent->n_net);
4780 DIE(aTHX_ PL_no_sock_func, "getnetent");
4786 #ifdef HAS_GETPROTOBYNAME
4787 return pp_gprotoent();
4789 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4795 #ifdef HAS_GETPROTOBYNUMBER
4796 return pp_gprotoent();
4798 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4804 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4806 I32 which = PL_op->op_type;
4807 register char **elem;
4809 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4810 struct protoent *getprotobyname(Netdb_name_t);
4811 struct protoent *getprotobynumber(int);
4812 struct protoent *getprotoent(void);
4814 struct protoent *pent;
4817 if (which == OP_GPBYNAME) {
4818 #ifdef HAS_GETPROTOBYNAME
4819 char* name = POPpbytex;
4820 pent = PerlSock_getprotobyname(name);
4822 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4825 else if (which == OP_GPBYNUMBER) {
4826 #ifdef HAS_GETPROTOBYNUMBER
4828 pent = PerlSock_getprotobynumber(number);
4830 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4834 #ifdef HAS_GETPROTOENT
4835 pent = PerlSock_getprotoent();
4837 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4841 if (GIMME != G_ARRAY) {
4842 PUSHs(sv = sv_newmortal());
4844 if (which == OP_GPBYNAME)
4845 sv_setiv(sv, (IV)pent->p_proto);
4847 sv_setpv(sv, pent->p_name);
4853 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4854 sv_setpv(sv, pent->p_name);
4855 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4856 for (elem = pent->p_aliases; elem && *elem; elem++) {
4857 sv_catpv(sv, *elem);
4859 sv_catpvn(sv, " ", 1);
4861 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4862 sv_setiv(sv, (IV)pent->p_proto);
4867 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4873 #ifdef HAS_GETSERVBYNAME
4874 return pp_gservent();
4876 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4882 #ifdef HAS_GETSERVBYPORT
4883 return pp_gservent();
4885 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4891 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4893 I32 which = PL_op->op_type;
4894 register char **elem;
4896 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4897 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4898 struct servent *getservbyport(int, Netdb_name_t);
4899 struct servent *getservent(void);
4901 struct servent *sent;
4904 if (which == OP_GSBYNAME) {
4905 #ifdef HAS_GETSERVBYNAME
4906 char *proto = POPpbytex;
4907 char *name = POPpbytex;
4909 if (proto && !*proto)
4912 sent = PerlSock_getservbyname(name, proto);
4914 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4917 else if (which == OP_GSBYPORT) {
4918 #ifdef HAS_GETSERVBYPORT
4919 char *proto = POPpbytex;
4920 unsigned short port = (unsigned short)POPu;
4922 if (proto && !*proto)
4926 port = PerlSock_htons(port);
4928 sent = PerlSock_getservbyport(port, proto);
4930 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4934 #ifdef HAS_GETSERVENT
4935 sent = PerlSock_getservent();
4937 DIE(aTHX_ PL_no_sock_func, "getservent");
4941 if (GIMME != G_ARRAY) {
4942 PUSHs(sv = sv_newmortal());
4944 if (which == OP_GSBYNAME) {
4946 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4948 sv_setiv(sv, (IV)(sent->s_port));
4952 sv_setpv(sv, sent->s_name);
4958 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4959 sv_setpv(sv, sent->s_name);
4960 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4961 for (elem = sent->s_aliases; elem && *elem; elem++) {
4962 sv_catpv(sv, *elem);
4964 sv_catpvn(sv, " ", 1);
4966 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4968 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4970 sv_setiv(sv, (IV)(sent->s_port));
4972 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4973 sv_setpv(sv, sent->s_proto);
4978 DIE(aTHX_ PL_no_sock_func, "getservent");
4984 #ifdef HAS_SETHOSTENT
4986 PerlSock_sethostent(TOPi);
4989 DIE(aTHX_ PL_no_sock_func, "sethostent");
4995 #ifdef HAS_SETNETENT
4997 PerlSock_setnetent(TOPi);
5000 DIE(aTHX_ PL_no_sock_func, "setnetent");
5006 #ifdef HAS_SETPROTOENT
5008 PerlSock_setprotoent(TOPi);
5011 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5017 #ifdef HAS_SETSERVENT
5019 PerlSock_setservent(TOPi);
5022 DIE(aTHX_ PL_no_sock_func, "setservent");
5028 #ifdef HAS_ENDHOSTENT
5030 PerlSock_endhostent();
5034 DIE(aTHX_ PL_no_sock_func, "endhostent");
5040 #ifdef HAS_ENDNETENT
5042 PerlSock_endnetent();
5046 DIE(aTHX_ PL_no_sock_func, "endnetent");
5052 #ifdef HAS_ENDPROTOENT
5054 PerlSock_endprotoent();
5058 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5064 #ifdef HAS_ENDSERVENT
5066 PerlSock_endservent();
5070 DIE(aTHX_ PL_no_sock_func, "endservent");
5079 DIE(aTHX_ PL_no_func, "getpwnam");
5088 DIE(aTHX_ PL_no_func, "getpwuid");
5096 I32 which = PL_op->op_type;
5099 struct passwd *pwent = NULL;
5101 * We currently support only the SysV getsp* shadow password interface.
5102 * The interface is declared in <shadow.h> and often one needs to link
5103 * with -lsecurity or some such.
5104 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5107 * AIX getpwnam() is clever enough to return the encrypted password
5108 * only if the caller (euid?) is root.
5110 * There are at least two other shadow password APIs. Many platforms
5111 * seem to contain more than one interface for accessing the shadow
5112 * password databases, possibly for compatibility reasons.
5113 * The getsp*() is by far he simplest one, the other two interfaces
5114 * are much more complicated, but also very similar to each other.
5119 * struct pr_passwd *getprpw*();
5120 * The password is in
5121 * char getprpw*(...).ufld.fd_encrypt[]
5122 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5127 * struct es_passwd *getespw*();
5128 * The password is in
5129 * char *(getespw*(...).ufld.fd_encrypt)
5130 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5132 * Mention I_PROT here so that Configure probes for it.
5134 * In HP-UX for getprpw*() the manual page claims that one should include
5135 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5136 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5137 * and pp_sys.c already includes <shadow.h> if there is such.
5139 * Note that <sys/security.h> is already probed for, but currently
5140 * it is only included in special cases.
5142 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5143 * be preferred interface, even though also the getprpw*() interface
5144 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5145 * One also needs to call set_auth_parameters() in main() before
5146 * doing anything else, whether one is using getespw*() or getprpw*().
5148 * Note that accessing the shadow databases can be magnitudes
5149 * slower than accessing the standard databases.
5157 char* name = POPpbytex;
5158 pwent = getpwnam(name);
5164 pwent = getpwuid(uid);
5168 # ifdef HAS_GETPWENT
5170 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5171 if (pwent) pwent = getpwnam(pwent->pw_name);
5174 DIE(aTHX_ PL_no_func, "getpwent");
5180 if (GIMME != G_ARRAY) {
5181 PUSHs(sv = sv_newmortal());
5183 if (which == OP_GPWNAM)
5184 # if Uid_t_sign <= 0
5185 sv_setiv(sv, (IV)pwent->pw_uid);
5187 sv_setuv(sv, (UV)pwent->pw_uid);
5190 sv_setpv(sv, pwent->pw_name);
5196 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5197 sv_setpv(sv, pwent->pw_name);
5199 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5201 /* If we have getspnam(), we try to dig up the shadow
5202 * password. If we are underprivileged, the shadow
5203 * interface will set the errno to EACCES or similar,
5204 * and return a null pointer. If this happens, we will
5205 * use the dummy password (usually "*" or "x") from the
5206 * standard password database.
5208 * In theory we could skip the shadow call completely
5209 * if euid != 0 but in practice we cannot know which
5210 * security measures are guarding the shadow databases
5211 * on a random platform.
5213 * Resist the urge to use additional shadow interfaces.
5214 * Divert the urge to writing an extension instead.
5217 # ifdef HAS_GETSPNAM
5219 struct spwd *spwent;
5220 int saverrno; /* Save and restore errno so that
5221 * underprivileged attempts seem
5222 * to have never made the unsccessful
5223 * attempt to retrieve the shadow password. */
5226 spwent = getspnam(pwent->pw_name);
5228 if (spwent && spwent->sp_pwdp)
5229 sv_setpv(sv, spwent->sp_pwdp);
5233 if (!SvPOK(sv)) /* Use the standard password, then. */
5234 sv_setpv(sv, pwent->pw_passwd);
5237 # ifndef INCOMPLETE_TAINTS
5238 /* passwd is tainted because user himself can diddle with it.
5239 * admittedly not much and in a very limited way, but nevertheless. */
5243 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5244 # if Uid_t_sign <= 0
5245 sv_setiv(sv, (IV)pwent->pw_uid);
5247 sv_setuv(sv, (UV)pwent->pw_uid);
5250 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5251 # if Uid_t_sign <= 0
5252 sv_setiv(sv, (IV)pwent->pw_gid);
5254 sv_setuv(sv, (UV)pwent->pw_gid);
5256 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5257 * because of the poor interface of the Perl getpw*(),
5258 * not because there's some standard/convention saying so.
5259 * A better interface would have been to return a hash,
5260 * but we are accursed by our history, alas. --jhi. */
5261 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5263 sv_setiv(sv, (IV)pwent->pw_change);
5266 sv_setiv(sv, (IV)pwent->pw_quota);
5269 sv_setpv(sv, pwent->pw_age);
5274 /* pw_class and pw_comment are mutually exclusive--.
5275 * see the above note for pw_change, pw_quota, and pw_age. */
5276 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5278 sv_setpv(sv, pwent->pw_class);
5281 sv_setpv(sv, pwent->pw_comment);
5285 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5287 sv_setpv(sv, pwent->pw_gecos);
5289 # ifndef INCOMPLETE_TAINTS
5290 /* pw_gecos is tainted because user himself can diddle with it. */
5294 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5295 sv_setpv(sv, pwent->pw_dir);
5297 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5298 sv_setpv(sv, pwent->pw_shell);
5299 # ifndef INCOMPLETE_TAINTS
5300 /* pw_shell is tainted because user himself can diddle with it. */
5305 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5306 sv_setiv(sv, (IV)pwent->pw_expire);
5311 DIE(aTHX_ PL_no_func, "getpwent");
5317 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5322 DIE(aTHX_ PL_no_func, "setpwent");
5328 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5333 DIE(aTHX_ PL_no_func, "endpwent");
5342 DIE(aTHX_ PL_no_func, "getgrnam");
5351 DIE(aTHX_ PL_no_func, "getgrgid");
5359 I32 which = PL_op->op_type;
5360 register char **elem;
5362 struct group *grent;
5365 if (which == OP_GGRNAM) {
5366 char* name = POPpbytex;
5367 grent = (struct group *)getgrnam(name);
5369 else if (which == OP_GGRGID) {
5371 grent = (struct group *)getgrgid(gid);
5375 grent = (struct group *)getgrent();
5377 DIE(aTHX_ PL_no_func, "getgrent");
5381 if (GIMME != G_ARRAY) {
5382 PUSHs(sv = sv_newmortal());
5384 if (which == OP_GGRNAM)
5385 sv_setiv(sv, (IV)grent->gr_gid);
5387 sv_setpv(sv, grent->gr_name);
5393 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5394 sv_setpv(sv, grent->gr_name);
5396 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5398 sv_setpv(sv, grent->gr_passwd);
5401 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5402 sv_setiv(sv, (IV)grent->gr_gid);
5404 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5405 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5406 /* In UNICOS/mk (_CRAYMPP) the multithreading
5407 * versions (getgrnam_r, getgrgid_r)
5408 * seem to return an illegal pointer
5409 * as the group members list, gr_mem.
5410 * getgrent() doesn't even have a _r version
5411 * but the gr_mem is poisonous anyway.
5412 * So yes, you cannot get the list of group
5413 * members if building multithreaded in UNICOS/mk. */
5414 for (elem = grent->gr_mem; elem && *elem; elem++) {
5415 sv_catpv(sv, *elem);
5417 sv_catpvn(sv, " ", 1);
5424 DIE(aTHX_ PL_no_func, "getgrent");
5430 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5435 DIE(aTHX_ PL_no_func, "setgrent");
5441 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5446 DIE(aTHX_ PL_no_func, "endgrent");
5456 if (!(tmps = PerlProc_getlogin()))
5458 PUSHp(tmps, strlen(tmps));
5461 DIE(aTHX_ PL_no_func, "getlogin");
5465 /* Miscellaneous. */
5470 dSP; dMARK; dORIGMARK; dTARGET;
5471 register I32 items = SP - MARK;
5472 unsigned long a[20];
5478 while (++MARK <= SP) {
5479 if (SvTAINTED(*MARK)) {
5485 TAINT_PROPER("syscall");
5488 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5489 * or where sizeof(long) != sizeof(char*). But such machines will
5490 * not likely have syscall implemented either, so who cares?
5492 while (++MARK <= SP) {
5493 if (SvNIOK(*MARK) || !i)
5494 a[i++] = SvIV(*MARK);
5495 else if (*MARK == &PL_sv_undef)
5498 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5504 DIE(aTHX_ "Too many args to syscall");
5506 DIE(aTHX_ "Too few args to syscall");
5508 retval = syscall(a[0]);
5511 retval = syscall(a[0],a[1]);
5514 retval = syscall(a[0],a[1],a[2]);
5517 retval = syscall(a[0],a[1],a[2],a[3]);
5520 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5523 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5526 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5529 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5533 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5536 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5539 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5543 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5547 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5551 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5552 a[10],a[11],a[12],a[13]);
5554 #endif /* atarist */
5560 DIE(aTHX_ PL_no_func, "syscall");
5564 #ifdef FCNTL_EMULATE_FLOCK
5566 /* XXX Emulate flock() with fcntl().
5567 What's really needed is a good file locking module.
5571 fcntl_emulate_flock(int fd, int operation)
5575 switch (operation & ~LOCK_NB) {
5577 flock.l_type = F_RDLCK;
5580 flock.l_type = F_WRLCK;
5583 flock.l_type = F_UNLCK;
5589 flock.l_whence = SEEK_SET;
5590 flock.l_start = flock.l_len = (Off_t)0;
5592 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5595 #endif /* FCNTL_EMULATE_FLOCK */
5597 #ifdef LOCKF_EMULATE_FLOCK
5599 /* XXX Emulate flock() with lockf(). This is just to increase
5600 portability of scripts. The calls are not completely
5601 interchangeable. What's really needed is a good file
5605 /* The lockf() constants might have been defined in <unistd.h>.
5606 Unfortunately, <unistd.h> causes troubles on some mixed
5607 (BSD/POSIX) systems, such as SunOS 4.1.3.
5609 Further, the lockf() constants aren't POSIX, so they might not be
5610 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5611 just stick in the SVID values and be done with it. Sigh.
5615 # define F_ULOCK 0 /* Unlock a previously locked region */
5618 # define F_LOCK 1 /* Lock a region for exclusive use */
5621 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5624 # define F_TEST 3 /* Test a region for other processes locks */
5628 lockf_emulate_flock(int fd, int operation)
5634 /* flock locks entire file so for lockf we need to do the same */
5636 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5637 if (pos > 0) /* is seekable and needs to be repositioned */
5638 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5639 pos = -1; /* seek failed, so don't seek back afterwards */
5642 switch (operation) {
5644 /* LOCK_SH - get a shared lock */
5646 /* LOCK_EX - get an exclusive lock */
5648 i = lockf (fd, F_LOCK, 0);
5651 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5652 case LOCK_SH|LOCK_NB:
5653 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5654 case LOCK_EX|LOCK_NB:
5655 i = lockf (fd, F_TLOCK, 0);
5657 if ((errno == EAGAIN) || (errno == EACCES))
5658 errno = EWOULDBLOCK;
5661 /* LOCK_UN - unlock (non-blocking is a no-op) */
5663 case LOCK_UN|LOCK_NB:
5664 i = lockf (fd, F_ULOCK, 0);
5667 /* Default - can't decipher operation */
5674 if (pos > 0) /* need to restore position of the handle */
5675 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5680 #endif /* LOCKF_EMULATE_FLOCK */