3 * Copyright (c) 1991-2003, 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],
1076 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1077 (void*) /* Workaround for a compiler bug. */
1080 for (i = 1; i <= 3; i++) {
1083 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1085 for (offset = 0; offset < growsize; offset += masksize) {
1086 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1087 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1089 Safefree(fd_sets[i]);
1096 if (GIMME == G_ARRAY && tbuf) {
1097 value = (NV)(timebuf.tv_sec) +
1098 (NV)(timebuf.tv_usec) / 1000000.0;
1099 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1100 sv_setnv(sv, value);
1104 DIE(aTHX_ "select not implemented");
1109 Perl_setdefout(pTHX_ GV *gv)
1112 (void)SvREFCNT_inc(gv);
1114 SvREFCNT_dec(PL_defoutgv);
1121 GV *newdefout, *egv;
1124 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1126 egv = GvEGV(PL_defoutgv);
1131 XPUSHs(&PL_sv_undef);
1133 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1134 if (gvp && *gvp == egv) {
1135 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1139 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1144 if (!GvIO(newdefout))
1145 gv_IOadd(newdefout);
1146 setdefout(newdefout);
1164 if (gv && (io = GvIO(gv))
1165 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1167 I32 gimme = GIMME_V;
1169 XPUSHs(SvTIED_obj((SV*)io, mg));
1172 call_method("GETC", gimme);
1175 if (gimme == G_SCALAR)
1176 SvSetMagicSV_nosteal(TARG, TOPs);
1179 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1180 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1181 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1182 report_evil_fh(gv, io, PL_op->op_type);
1183 SETERRNO(EBADF,RMS_IFI);
1187 sv_setpv(TARG, " ");
1188 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1189 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1190 /* Find out how many bytes the char needs */
1191 Size_t len = UTF8SKIP(SvPVX(TARG));
1194 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1195 SvCUR_set(TARG,1+len);
1205 return pp_sysread();
1209 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1211 register PERL_CONTEXT *cx;
1212 I32 gimme = GIMME_V;
1218 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1220 PAD_SET_CUR(CvPADLIST(cv), 1);
1222 setdefout(gv); /* locally select filehandle so $% et al work */
1255 SV *tmpsv = sv_newmortal();
1256 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1257 name = SvPV_nolen(tmpsv);
1260 DIE(aTHX_ "Undefined format \"%s\" called", name);
1261 DIE(aTHX_ "Not a format reference");
1264 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1266 IoFLAGS(io) &= ~IOf_DIDTOP;
1267 return doform(cv,gv,PL_op->op_next);
1273 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1274 register IO *io = GvIOp(gv);
1275 PerlIO *ofp = IoOFP(io);
1279 register PERL_CONTEXT *cx;
1281 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1282 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1285 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1286 PL_formtarget != PL_toptarget)
1290 if (!IoTOP_GV(io)) {
1294 if (!IoTOP_NAME(io)) {
1295 if (!IoFMT_NAME(io))
1296 IoFMT_NAME(io) = savepv(GvNAME(gv));
1297 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1298 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1299 if ((topgv && GvFORM(topgv)) ||
1300 !gv_fetchpv("top",FALSE,SVt_PVFM))
1301 IoTOP_NAME(io) = savepv(SvPVX(topname));
1303 IoTOP_NAME(io) = savepv("top");
1305 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1306 if (!topgv || !GvFORM(topgv)) {
1307 IoLINES_LEFT(io) = 100000000;
1310 IoTOP_GV(io) = topgv;
1312 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1313 I32 lines = IoLINES_LEFT(io);
1314 char *s = SvPVX(PL_formtarget);
1315 if (lines <= 0) /* Yow, header didn't even fit!!! */
1317 while (lines-- > 0) {
1318 s = strchr(s, '\n');
1324 STRLEN save = SvCUR(PL_formtarget);
1325 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1326 do_print(PL_formtarget, ofp);
1327 SvCUR_set(PL_formtarget, save);
1328 sv_chop(PL_formtarget, s);
1329 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1332 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1333 do_print(PL_formfeed, ofp);
1334 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1336 PL_formtarget = PL_toptarget;
1337 IoFLAGS(io) |= IOf_DIDTOP;
1340 DIE(aTHX_ "bad top format reference");
1345 SV *sv = sv_newmortal();
1346 gv_efullname4(sv, fgv, Nullch, FALSE);
1347 name = SvPV_nolen(sv);
1350 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1353 DIE(aTHX_ "Undefined top format called");
1357 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1358 return doform(cv,gv,PL_op);
1362 POPBLOCK(cx,PL_curpm);
1368 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1370 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1371 else if (ckWARN(WARN_CLOSED))
1372 report_evil_fh(gv, io, PL_op->op_type);
1377 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1378 if (ckWARN(WARN_IO))
1379 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1381 if (!do_print(PL_formtarget, fp))
1384 FmLINES(PL_formtarget) = 0;
1385 SvCUR_set(PL_formtarget, 0);
1386 *SvEND(PL_formtarget) = '\0';
1387 if (IoFLAGS(io) & IOf_FLUSH)
1388 (void)PerlIO_flush(fp);
1393 PL_formtarget = PL_bodytarget;
1395 return pop_return();
1400 dSP; dMARK; dORIGMARK;
1407 if (PL_op->op_flags & OPf_STACKED)
1412 if (gv && (io = GvIO(gv))
1413 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1415 if (MARK == ORIGMARK) {
1418 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1422 *MARK = SvTIED_obj((SV*)io, mg);
1425 call_method("PRINTF", G_SCALAR);
1428 MARK = ORIGMARK + 1;
1435 if (!(io = GvIO(gv))) {
1436 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1437 report_evil_fh(gv, io, PL_op->op_type);
1438 SETERRNO(EBADF,RMS_IFI);
1441 else if (!(fp = IoOFP(io))) {
1442 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1444 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1445 else if (ckWARN(WARN_CLOSED))
1446 report_evil_fh(gv, io, PL_op->op_type);
1448 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1452 do_sprintf(sv, SP - MARK, MARK + 1);
1453 if (!do_print(sv, fp))
1456 if (IoFLAGS(io) & IOf_FLUSH)
1457 if (PerlIO_flush(fp) == EOF)
1468 PUSHs(&PL_sv_undef);
1489 /* Need TIEHANDLE method ? */
1491 tmps = SvPV(sv, len);
1492 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1493 IoLINES(GvIOp(gv)) = 0;
1497 PUSHs(&PL_sv_undef);
1504 dSP; dMARK; dORIGMARK; dTARGET;
1511 Sock_size_t bufsize;
1518 bool charstart = FALSE;
1519 STRLEN charskip = 0;
1523 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1524 && gv && (io = GvIO(gv))
1525 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1530 *MARK = SvTIED_obj((SV*)io, mg);
1532 call_method("READ", G_SCALAR);
1545 sv_setpvn(bufsv, "", 0);
1546 length = SvIVx(*++MARK);
1549 offset = SvIVx(*++MARK);
1553 if (!io || !IoIFP(io)) {
1554 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1555 report_evil_fh(gv, io, PL_op->op_type);
1556 SETERRNO(EBADF,RMS_IFI);
1559 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1560 buffer = SvPVutf8_force(bufsv, blen);
1561 /* UTF8 may not have been set if they are all low bytes */
1565 buffer = SvPV_force(bufsv, blen);
1568 DIE(aTHX_ "Negative length");
1576 if (PL_op->op_type == OP_RECV) {
1577 char namebuf[MAXPATHLEN];
1578 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1579 bufsize = sizeof (struct sockaddr_in);
1581 bufsize = sizeof namebuf;
1583 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1587 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1588 /* 'offset' means 'flags' here */
1589 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1590 (struct sockaddr *)namebuf, &bufsize);
1594 /* Bogus return without padding */
1595 bufsize = sizeof (struct sockaddr_in);
1597 SvCUR_set(bufsv, count);
1598 *SvEND(bufsv) = '\0';
1599 (void)SvPOK_only(bufsv);
1603 /* This should not be marked tainted if the fp is marked clean */
1604 if (!(IoFLAGS(io) & IOf_UNTAINT))
1605 SvTAINTED_on(bufsv);
1607 sv_setpvn(TARG, namebuf, bufsize);
1612 if (PL_op->op_type == OP_RECV)
1613 DIE(aTHX_ PL_no_sock_func, "recv");
1615 if (DO_UTF8(bufsv)) {
1616 /* offset adjust in characters not bytes */
1617 blen = sv_len_utf8(bufsv);
1620 if (-offset > (int)blen)
1621 DIE(aTHX_ "Offset outside string");
1624 if (DO_UTF8(bufsv)) {
1625 /* convert offset-as-chars to offset-as-bytes */
1626 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1629 bufsize = SvCUR(bufsv);
1630 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1631 if (offset > bufsize) { /* Zero any newly allocated space */
1632 Zero(buffer+bufsize, offset-bufsize, char);
1634 buffer = buffer + offset;
1636 if (PL_op->op_type == OP_SYSREAD) {
1637 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1638 if (IoTYPE(io) == IoTYPE_SOCKET) {
1639 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1645 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1650 #ifdef HAS_SOCKET__bad_code_maybe
1651 if (IoTYPE(io) == IoTYPE_SOCKET) {
1652 char namebuf[MAXPATHLEN];
1653 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1654 bufsize = sizeof (struct sockaddr_in);
1656 bufsize = sizeof namebuf;
1658 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1659 (struct sockaddr *)namebuf, &bufsize);
1664 count = PerlIO_read(IoIFP(io), buffer, length);
1665 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1666 if (count == 0 && PerlIO_error(IoIFP(io)))
1670 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1671 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1674 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1675 *SvEND(bufsv) = '\0';
1676 (void)SvPOK_only(bufsv);
1677 if (fp_utf8 && !IN_BYTES) {
1678 /* Look at utf8 we got back and count the characters */
1679 char *bend = buffer + count;
1680 while (buffer < bend) {
1682 skip = UTF8SKIP(buffer);
1685 if (buffer - charskip + skip > bend) {
1686 /* partial character - try for rest of it */
1687 length = skip - (bend-buffer);
1688 offset = bend - SvPVX(bufsv);
1700 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1701 provided amount read (count) was what was requested (length)
1703 if (got < wanted && count == length) {
1704 length = wanted - got;
1705 offset = bend - SvPVX(bufsv);
1708 /* return value is character count */
1713 /* This should not be marked tainted if the fp is marked clean */
1714 if (!(IoFLAGS(io) & IOf_UNTAINT))
1715 SvTAINTED_on(bufsv);
1728 int items = (SP - PL_stack_base) - TOPMARK;
1732 sv = sv_2mortal(newSViv(sv_len(*SP)));
1741 dSP; dMARK; dORIGMARK; dTARGET;
1752 if (PL_op->op_type == OP_SYSWRITE
1753 && gv && (io = GvIO(gv))
1754 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1759 *MARK = SvTIED_obj((SV*)io, mg);
1761 call_method("WRITE", G_SCALAR);
1772 #if Size_t_size > IVSIZE
1773 length = (Size_t)SvNVx(*++MARK);
1775 length = (Size_t)SvIVx(*++MARK);
1777 if ((SSize_t)length < 0)
1778 DIE(aTHX_ "Negative length");
1781 if (!io || !IoIFP(io)) {
1783 if (ckWARN(WARN_CLOSED))
1784 report_evil_fh(gv, io, PL_op->op_type);
1785 SETERRNO(EBADF,RMS_IFI);
1789 if (PerlIO_isutf8(IoIFP(io))) {
1790 buffer = SvPVutf8(bufsv, blen);
1794 sv_utf8_downgrade(bufsv, FALSE);
1795 buffer = SvPV(bufsv, blen);
1798 if (PL_op->op_type == OP_SYSWRITE) {
1800 if (DO_UTF8(bufsv)) {
1801 /* length and offset are in chars */
1802 blen = sv_len_utf8(bufsv);
1805 offset = SvIVx(*++MARK);
1807 if (-offset > (IV)blen)
1808 DIE(aTHX_ "Offset outside string");
1810 } else if (offset >= (IV)blen && blen > 0)
1811 DIE(aTHX_ "Offset outside string");
1814 if (length > blen - offset)
1815 length = blen - offset;
1816 if (DO_UTF8(bufsv)) {
1817 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1818 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1821 buffer = buffer+offset;
1823 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1824 if (IoTYPE(io) == IoTYPE_SOCKET) {
1825 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1831 /* See the note at doio.c:do_print about filesize limits. --jhi */
1832 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1837 else if (SP > MARK) {
1840 sockbuf = SvPVx(*++MARK, mlen);
1841 /* length is really flags */
1842 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1843 length, (struct sockaddr *)sockbuf, mlen);
1846 /* length is really flags */
1847 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1850 DIE(aTHX_ PL_no_sock_func, "send");
1856 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1857 #if Size_t_size > IVSIZE
1871 return pp_sysread();
1882 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1884 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1886 if (io && !IoIFP(io)) {
1887 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1889 IoFLAGS(io) &= ~IOf_START;
1890 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1891 sv_setpvn(GvSV(gv), "-", 1);
1892 SvSETMAGIC(GvSV(gv));
1894 else if (!nextargv(gv))
1899 gv = PL_last_in_gv; /* eof */
1902 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1904 if (gv && (io = GvIO(gv))
1905 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1908 XPUSHs(SvTIED_obj((SV*)io, mg));
1911 call_method("EOF", G_SCALAR);
1917 PUSHs(boolSV(!gv || do_eof(gv)));
1931 gv = PL_last_in_gv = (GV*)POPs;
1933 if (gv && (io = GvIO(gv))
1934 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1937 XPUSHs(SvTIED_obj((SV*)io, mg));
1940 call_method("TELL", G_SCALAR);
1946 #if LSEEKSIZE > IVSIZE
1947 PUSHn( do_tell(gv) );
1949 PUSHi( do_tell(gv) );
1956 return pp_sysseek();
1965 #if LSEEKSIZE > IVSIZE
1966 Off_t offset = (Off_t)SvNVx(POPs);
1968 Off_t offset = (Off_t)SvIVx(POPs);
1972 gv = PL_last_in_gv = (GV*)POPs;
1974 if (gv && (io = GvIO(gv))
1975 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1978 XPUSHs(SvTIED_obj((SV*)io, mg));
1979 #if LSEEKSIZE > IVSIZE
1980 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1982 XPUSHs(sv_2mortal(newSViv(offset)));
1984 XPUSHs(sv_2mortal(newSViv(whence)));
1987 call_method("SEEK", G_SCALAR);
1993 if (PL_op->op_type == OP_SEEK)
1994 PUSHs(boolSV(do_seek(gv, offset, whence)));
1996 Off_t sought = do_sysseek(gv, offset, whence);
1998 PUSHs(&PL_sv_undef);
2001 #if LSEEKSIZE > IVSIZE
2006 : newSVpvn(zero_but_true, ZBTLEN);
2007 PUSHs(sv_2mortal(sv));
2016 /* There seems to be no consensus on the length type of truncate()
2017 * and ftruncate(), both off_t and size_t have supporters. In
2018 * general one would think that when using large files, off_t is
2019 * at least as wide as size_t, so using an off_t should be okay. */
2020 /* XXX Configure probe for the length type of *truncate() needed XXX */
2023 #if Off_t_size > IVSIZE
2028 /* Checking for length < 0 is problematic as the type might or
2029 * might not be signed: if it is not, clever compilers will moan. */
2030 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2032 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2039 if (PL_op->op_flags & OPf_SPECIAL) {
2040 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2049 TAINT_PROPER("truncate");
2050 if (!(fp = IoIFP(io))) {
2056 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2058 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2068 if (SvTYPE(sv) == SVt_PVGV) {
2069 tmpgv = (GV*)sv; /* *main::FRED for example */
2070 goto do_ftruncate_gv;
2072 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2073 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2074 goto do_ftruncate_gv;
2076 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2077 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2078 goto do_ftruncate_io;
2081 name = SvPV(sv, n_a);
2082 TAINT_PROPER("truncate");
2084 if (truncate(name, len) < 0)
2090 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2093 if (my_chsize(tmpfd, len) < 0)
2095 PerlLIO_close(tmpfd);
2104 SETERRNO(EBADF,RMS_IFI);
2108 DIE(aTHX_ "truncate not implemented");
2121 unsigned int func = POPu;
2122 int optype = PL_op->op_type;
2126 IO *io = gv ? GvIOn(gv) : 0;
2128 if (!io || !argsv || !IoIFP(io)) {
2129 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2130 report_evil_fh(gv, io, PL_op->op_type);
2131 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2135 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2138 s = SvPV_force(argsv, len);
2139 need = IOCPARM_LEN(func);
2141 s = Sv_Grow(argsv, need + 1);
2142 SvCUR_set(argsv, need);
2145 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2148 retval = SvIV(argsv);
2149 s = INT2PTR(char*,retval); /* ouch */
2152 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2154 if (optype == OP_IOCTL)
2156 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2158 DIE(aTHX_ "ioctl is not implemented");
2162 DIE(aTHX_ "fcntl is not implemented");
2164 #if defined(OS2) && defined(__EMX__)
2165 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2167 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2171 if (s[SvCUR(argsv)] != 17)
2172 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2174 s[SvCUR(argsv)] = 0; /* put our null back */
2175 SvSETMAGIC(argsv); /* Assume it has changed */
2184 PUSHp(zero_but_true, ZBTLEN);
2205 if (gv && (io = GvIO(gv)))
2212 (void)PerlIO_flush(fp);
2213 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2216 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2217 report_evil_fh(gv, io, PL_op->op_type);
2219 SETERRNO(EBADF,RMS_IFI);
2224 DIE(aTHX_ PL_no_func, "flock()");
2236 int protocol = POPi;
2242 io = gv ? GvIOn(gv) : NULL;
2245 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2246 report_evil_fh(gv, io, PL_op->op_type);
2248 do_close(gv, FALSE);
2249 SETERRNO(EBADF,LIB_INVARG);
2254 do_close(gv, FALSE);
2256 TAINT_PROPER("socket");
2257 fd = PerlSock_socket(domain, type, protocol);
2260 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2261 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2262 IoTYPE(io) = IoTYPE_SOCKET;
2263 if (!IoIFP(io) || !IoOFP(io)) {
2264 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2265 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2266 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2269 #if defined(HAS_FCNTL) && defined(F_SETFD)
2270 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2274 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2279 DIE(aTHX_ PL_no_sock_func, "socket");
2285 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2291 int protocol = POPi;
2298 io1 = gv1 ? GvIOn(gv1) : NULL;
2299 io2 = gv2 ? GvIOn(gv2) : NULL;
2300 if (!gv1 || !gv2 || !io1 || !io2) {
2301 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2303 report_evil_fh(gv1, io1, PL_op->op_type);
2305 report_evil_fh(gv1, io2, PL_op->op_type);
2308 do_close(gv1, FALSE);
2310 do_close(gv2, FALSE);
2315 do_close(gv1, FALSE);
2317 do_close(gv2, FALSE);
2319 TAINT_PROPER("socketpair");
2320 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2322 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2323 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2324 IoTYPE(io1) = IoTYPE_SOCKET;
2325 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2326 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2327 IoTYPE(io2) = IoTYPE_SOCKET;
2328 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2329 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2330 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2331 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2332 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2333 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2334 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2337 #if defined(HAS_FCNTL) && defined(F_SETFD)
2338 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2339 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2344 DIE(aTHX_ PL_no_sock_func, "socketpair");
2352 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2353 extern void GETPRIVMODE();
2354 extern void GETUSERMODE();
2359 register IO *io = GvIOn(gv);
2363 int mpeprivmode = 0;
2366 if (!io || !IoIFP(io))
2369 addr = SvPV(addrsv, len);
2370 TAINT_PROPER("bind");
2371 #ifdef MPE /* Deal with MPE bind() peculiarities */
2372 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2373 /* The address *MUST* stupidly be zero. */
2374 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2375 /* PRIV mode is required to bind() to ports < 1024. */
2376 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2377 ((struct sockaddr_in *)addr)->sin_port > 0) {
2378 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2383 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2384 (struct sockaddr *)addr, len) >= 0)
2387 #ifdef MPE /* Switch back to USER mode */
2398 if (ckWARN(WARN_CLOSED))
2399 report_evil_fh(gv, io, PL_op->op_type);
2400 SETERRNO(EBADF,SS_IVCHAN);
2403 DIE(aTHX_ PL_no_sock_func, "bind");
2414 register IO *io = GvIOn(gv);
2417 if (!io || !IoIFP(io))
2420 addr = SvPV(addrsv, len);
2421 TAINT_PROPER("connect");
2422 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2428 if (ckWARN(WARN_CLOSED))
2429 report_evil_fh(gv, io, PL_op->op_type);
2430 SETERRNO(EBADF,SS_IVCHAN);
2433 DIE(aTHX_ PL_no_sock_func, "connect");
2443 register IO *io = gv ? GvIOn(gv) : NULL;
2445 if (!gv || !io || !IoIFP(io))
2448 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2454 if (ckWARN(WARN_CLOSED))
2455 report_evil_fh(gv, io, PL_op->op_type);
2456 SETERRNO(EBADF,SS_IVCHAN);
2459 DIE(aTHX_ PL_no_sock_func, "listen");
2471 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2472 Sock_size_t len = sizeof saddr;
2484 if (!gstio || !IoIFP(gstio))
2488 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2492 do_close(ngv, FALSE);
2493 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2494 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2495 IoTYPE(nstio) = IoTYPE_SOCKET;
2496 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2497 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2498 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2499 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2502 #if defined(HAS_FCNTL) && defined(F_SETFD)
2503 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2507 len = sizeof saddr; /* EPOC somehow truncates info */
2508 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2511 PUSHp((char *)&saddr, len);
2515 if (ckWARN(WARN_CLOSED))
2516 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2517 SETERRNO(EBADF,SS_IVCHAN);
2523 DIE(aTHX_ PL_no_sock_func, "accept");
2533 register IO *io = GvIOn(gv);
2535 if (!io || !IoIFP(io))
2538 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2542 if (ckWARN(WARN_CLOSED))
2543 report_evil_fh(gv, io, PL_op->op_type);
2544 SETERRNO(EBADF,SS_IVCHAN);
2547 DIE(aTHX_ PL_no_sock_func, "shutdown");
2554 return pp_ssockopt();
2556 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2564 int optype = PL_op->op_type;
2567 unsigned int optname;
2573 if (optype == OP_GSOCKOPT)
2574 sv = sv_2mortal(NEWSV(22, 257));
2577 optname = (unsigned int) POPi;
2578 lvl = (unsigned int) POPi;
2582 if (!io || !IoIFP(io))
2585 fd = PerlIO_fileno(IoIFP(io));
2589 (void)SvPOK_only(sv);
2593 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2608 aint = (int)SvIV(sv);
2612 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2621 if (ckWARN(WARN_CLOSED))
2622 report_evil_fh(gv, io, optype);
2623 SETERRNO(EBADF,SS_IVCHAN);
2628 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2635 return pp_getpeername();
2637 DIE(aTHX_ PL_no_sock_func, "getsockname");
2645 int optype = PL_op->op_type;
2649 register IO *io = GvIOn(gv);
2652 if (!io || !IoIFP(io))
2655 sv = sv_2mortal(NEWSV(22, 257));
2656 (void)SvPOK_only(sv);
2660 fd = PerlIO_fileno(IoIFP(io));
2662 case OP_GETSOCKNAME:
2663 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2666 case OP_GETPEERNAME:
2667 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2669 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2671 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";
2672 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2673 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2674 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2675 sizeof(u_short) + sizeof(struct in_addr))) {
2682 #ifdef BOGUS_GETNAME_RETURN
2683 /* Interactive Unix, getpeername() and getsockname()
2684 does not return valid namelen */
2685 if (len == BOGUS_GETNAME_RETURN)
2686 len = sizeof(struct sockaddr);
2694 if (ckWARN(WARN_CLOSED))
2695 report_evil_fh(gv, io, optype);
2696 SETERRNO(EBADF,SS_IVCHAN);
2701 DIE(aTHX_ PL_no_sock_func, "getpeername");
2720 if (PL_op->op_flags & OPf_REF) {
2722 if (PL_op->op_type == OP_LSTAT) {
2723 if (gv != PL_defgv) {
2724 if (ckWARN(WARN_IO))
2725 Perl_warner(aTHX_ packWARN(WARN_IO),
2726 "lstat() on filehandle %s", GvENAME(gv));
2727 } else if (PL_laststype != OP_LSTAT)
2728 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2732 if (gv != PL_defgv) {
2733 PL_laststype = OP_STAT;
2735 sv_setpv(PL_statname, "");
2736 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2737 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2739 if (PL_laststatval < 0) {
2740 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2741 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2747 if (SvTYPE(sv) == SVt_PVGV) {
2751 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2753 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2754 Perl_warner(aTHX_ packWARN(WARN_IO),
2755 "lstat() on filehandle %s", GvENAME(gv));
2758 sv_setpv(PL_statname, SvPV(sv,n_a));
2761 PL_laststype = PL_op->op_type;
2762 if (PL_op->op_type == OP_LSTAT)
2763 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2766 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2767 if (PL_laststatval < 0) {
2768 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2769 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2775 if (gimme != G_ARRAY) {
2776 if (gimme != G_VOID)
2777 XPUSHs(boolSV(max));
2783 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2784 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2785 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2786 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2787 #if Uid_t_size > IVSIZE
2788 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2790 # if Uid_t_sign <= 0
2791 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2793 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2796 #if Gid_t_size > IVSIZE
2797 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2799 # if Gid_t_sign <= 0
2800 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2802 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2805 #ifdef USE_STAT_RDEV
2806 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2808 PUSHs(sv_2mortal(newSVpvn("", 0)));
2810 #if Off_t_size > IVSIZE
2811 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2816 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2817 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2818 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2820 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2821 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2822 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2824 #ifdef USE_STAT_BLOCKS
2825 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2826 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2828 PUSHs(sv_2mortal(newSVpvn("", 0)));
2829 PUSHs(sv_2mortal(newSVpvn("", 0)));
2839 #if defined(HAS_ACCESS) && defined(R_OK)
2841 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2842 result = access(TOPpx, R_OK);
2857 if (cando(S_IRUSR, 0, &PL_statcache))
2866 #if defined(HAS_ACCESS) && defined(W_OK)
2868 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2869 result = access(TOPpx, W_OK);
2884 if (cando(S_IWUSR, 0, &PL_statcache))
2893 #if defined(HAS_ACCESS) && defined(X_OK)
2895 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2896 result = access(TOPpx, X_OK);
2911 if (cando(S_IXUSR, 0, &PL_statcache))
2920 #ifdef PERL_EFF_ACCESS_R_OK
2922 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2923 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2938 if (cando(S_IRUSR, 1, &PL_statcache))
2947 #ifdef PERL_EFF_ACCESS_W_OK
2949 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2950 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2965 if (cando(S_IWUSR, 1, &PL_statcache))
2974 #ifdef PERL_EFF_ACCESS_X_OK
2976 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2977 result = PERL_EFF_ACCESS_X_OK(TOPpx);
2992 if (cando(S_IXUSR, 1, &PL_statcache))
2999 I32 result = my_stat();
3008 return pp_ftrowned();
3013 I32 result = my_stat();
3017 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3025 I32 result = my_stat();
3029 if (PL_statcache.st_size == 0)
3036 I32 result = my_stat();
3040 #if Off_t_size > IVSIZE
3041 PUSHn(PL_statcache.st_size);
3043 PUSHi(PL_statcache.st_size);
3050 I32 result = my_stat();
3054 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3060 I32 result = my_stat();
3064 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3070 I32 result = my_stat();
3074 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3080 I32 result = my_stat();
3084 if (S_ISSOCK(PL_statcache.st_mode))
3091 I32 result = my_stat();
3095 if (S_ISCHR(PL_statcache.st_mode))
3102 I32 result = my_stat();
3106 if (S_ISBLK(PL_statcache.st_mode))
3113 I32 result = my_stat();
3117 if (S_ISREG(PL_statcache.st_mode))
3124 I32 result = my_stat();
3128 if (S_ISDIR(PL_statcache.st_mode))
3135 I32 result = my_stat();
3139 if (S_ISFIFO(PL_statcache.st_mode))
3146 I32 result = my_lstat();
3150 if (S_ISLNK(PL_statcache.st_mode))
3159 I32 result = my_stat();
3163 if (PL_statcache.st_mode & S_ISUID)
3173 I32 result = my_stat();
3177 if (PL_statcache.st_mode & S_ISGID)
3187 I32 result = my_stat();
3191 if (PL_statcache.st_mode & S_ISVTX)
3202 char *tmps = Nullch;
3205 if (PL_op->op_flags & OPf_REF)
3207 else if (isGV(TOPs))
3209 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3210 gv = (GV*)SvRV(POPs);
3212 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3214 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3215 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3216 else if (tmps && isDIGIT(*tmps))
3220 if (PerlLIO_isatty(fd))
3225 #if defined(atarist) /* this will work with atariST. Configure will
3226 make guesses for other systems. */
3227 # define FILE_base(f) ((f)->_base)
3228 # define FILE_ptr(f) ((f)->_ptr)
3229 # define FILE_cnt(f) ((f)->_cnt)
3230 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3240 register STDCHAR *s;
3247 if (PL_op->op_flags & OPf_REF)
3249 else if (isGV(TOPs))
3251 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3252 gv = (GV*)SvRV(POPs);
3258 if (gv == PL_defgv) {
3260 io = GvIO(PL_statgv);
3263 goto really_filename;
3268 PL_laststatval = -1;
3269 sv_setpv(PL_statname, "");
3270 io = GvIO(PL_statgv);
3272 if (io && IoIFP(io)) {
3273 if (! PerlIO_has_base(IoIFP(io)))
3274 DIE(aTHX_ "-T and -B not implemented on filehandles");
3275 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3276 if (PL_laststatval < 0)
3278 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3279 if (PL_op->op_type == OP_FTTEXT)
3284 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3285 i = PerlIO_getc(IoIFP(io));
3287 (void)PerlIO_ungetc(IoIFP(io),i);
3289 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3291 len = PerlIO_get_bufsiz(IoIFP(io));
3292 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3293 /* sfio can have large buffers - limit to 512 */
3298 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3300 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3302 SETERRNO(EBADF,RMS_IFI);
3310 PL_laststatval = -1;
3311 PL_laststype = OP_STAT;
3312 sv_setpv(PL_statname, SvPV(sv, n_a));
3313 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3314 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3315 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3318 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3319 if (PL_laststatval < 0) {
3320 (void)PerlIO_close(fp);
3323 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3324 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3325 (void)PerlIO_close(fp);
3327 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3328 RETPUSHNO; /* special case NFS directories */
3329 RETPUSHYES; /* null file is anything */
3334 /* now scan s to look for textiness */
3335 /* XXX ASCII dependent code */
3337 #if defined(DOSISH) || defined(USEMYBINMODE)
3338 /* ignore trailing ^Z on short files */
3339 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3343 for (i = 0; i < len; i++, s++) {
3344 if (!*s) { /* null never allowed in text */
3349 else if (!(isPRINT(*s) || isSPACE(*s)))
3352 else if (*s & 128) {
3354 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3357 /* utf8 characters don't count as odd */
3358 if (UTF8_IS_START(*s)) {
3359 int ulen = UTF8SKIP(s);
3360 if (ulen < len - i) {
3362 for (j = 1; j < ulen; j++) {
3363 if (!UTF8_IS_CONTINUATION(s[j]))
3366 --ulen; /* loop does extra increment */
3376 *s != '\n' && *s != '\r' && *s != '\b' &&
3377 *s != '\t' && *s != '\f' && *s != 27)
3382 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3407 if( !tmps || !*tmps ) {
3408 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3409 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3411 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3416 deprecate("chdir('') or chdir(undef) as chdir()");
3417 tmps = SvPV(*svp, n_a);
3421 TAINT_PROPER("chdir");
3426 TAINT_PROPER("chdir");
3427 PUSHi( PerlDir_chdir(tmps) >= 0 );
3429 /* Clear the DEFAULT element of ENV so we'll get the new value
3431 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3439 dSP; dMARK; dTARGET;
3440 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3446 DIE(aTHX_ PL_no_func, "chown");
3456 TAINT_PROPER("chroot");
3457 PUSHi( chroot(tmps) >= 0 );
3460 DIE(aTHX_ PL_no_func, "chroot");
3466 dSP; dMARK; dTARGET;
3468 value = (I32)apply(PL_op->op_type, MARK, SP);
3476 dSP; dMARK; dTARGET;
3478 value = (I32)apply(PL_op->op_type, MARK, SP);
3486 dSP; dMARK; dTARGET;
3488 value = (I32)apply(PL_op->op_type, MARK, SP);
3500 char *tmps2 = POPpx;
3501 char *tmps = SvPV(TOPs, n_a);
3502 TAINT_PROPER("rename");
3504 anum = PerlLIO_rename(tmps, tmps2);
3506 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3507 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3510 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3511 (void)UNLINK(tmps2);
3512 if (!(anum = link(tmps, tmps2)))
3513 anum = UNLINK(tmps);
3526 char *tmps2 = POPpx;
3527 char *tmps = SvPV(TOPs, n_a);
3528 TAINT_PROPER("link");
3529 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3532 DIE(aTHX_ PL_no_func, "link");
3541 char *tmps2 = POPpx;
3542 char *tmps = SvPV(TOPs, n_a);
3543 TAINT_PROPER("symlink");
3544 SETi( symlink(tmps, tmps2) >= 0 );
3547 DIE(aTHX_ PL_no_func, "symlink");
3557 char buf[MAXPATHLEN];
3561 #ifndef INCOMPLETE_TAINTS
3565 len = readlink(tmps, buf, sizeof(buf) - 1);
3573 RETSETUNDEF; /* just pretend it's a normal file */
3577 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3579 S_dooneliner(pTHX_ char *cmd, char *filename)
3581 char *save_filename = filename;
3587 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3588 strcpy(cmdline, cmd);
3589 strcat(cmdline, " ");
3590 for (s = cmdline + strlen(cmdline); *filename; ) {
3595 myfp = PerlProc_popen(cmdline, "r");
3599 SV *tmpsv = sv_newmortal();
3600 /* Need to save/restore 'PL_rs' ?? */
3601 s = sv_gets(tmpsv, myfp, 0);
3602 (void)PerlProc_pclose(myfp);
3606 #ifdef HAS_SYS_ERRLIST
3611 /* you don't see this */
3613 #ifdef HAS_SYS_ERRLIST
3621 if (instr(s, errmsg)) {
3628 #define EACCES EPERM
3630 if (instr(s, "cannot make"))
3631 SETERRNO(EEXIST,RMS_FEX);
3632 else if (instr(s, "existing file"))
3633 SETERRNO(EEXIST,RMS_FEX);
3634 else if (instr(s, "ile exists"))
3635 SETERRNO(EEXIST,RMS_FEX);
3636 else if (instr(s, "non-exist"))
3637 SETERRNO(ENOENT,RMS_FNF);
3638 else if (instr(s, "does not exist"))
3639 SETERRNO(ENOENT,RMS_FNF);
3640 else if (instr(s, "not empty"))
3641 SETERRNO(EBUSY,SS_DEVOFFLINE);
3642 else if (instr(s, "cannot access"))
3643 SETERRNO(EACCES,RMS_PRV);
3645 SETERRNO(EPERM,RMS_PRV);
3648 else { /* some mkdirs return no failure indication */
3649 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3650 if (PL_op->op_type == OP_RMDIR)
3655 SETERRNO(EACCES,RMS_PRV); /* a guess */
3664 /* This macro removes trailing slashes from a directory name.
3665 * Different operating and file systems take differently to
3666 * trailing slashes. According to POSIX 1003.1 1996 Edition
3667 * any number of trailing slashes should be allowed.
3668 * Thusly we snip them away so that even non-conforming
3669 * systems are happy.
3670 * We should probably do this "filtering" for all
3671 * the functions that expect (potentially) directory names:
3672 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3673 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3675 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3676 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3679 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3680 (tmps) = savepvn((tmps), (len)); \
3700 TRIMSLASHES(tmps,len,copy);
3702 TAINT_PROPER("mkdir");
3704 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3706 SETi( dooneliner("mkdir", tmps) );
3707 oldumask = PerlLIO_umask(0);
3708 PerlLIO_umask(oldumask);
3709 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3723 TRIMSLASHES(tmps,len,copy);
3724 TAINT_PROPER("rmdir");
3726 SETi( PerlDir_rmdir(tmps) >= 0 );
3728 SETi( dooneliner("rmdir", tmps) );
3735 /* Directory calls. */
3739 #if defined(Direntry_t) && defined(HAS_READDIR)
3742 char *dirname = POPpx;
3744 register IO *io = GvIOn(gv);
3750 PerlDir_close(IoDIRP(io));
3751 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3757 SETERRNO(EBADF,RMS_DIR);
3760 DIE(aTHX_ PL_no_dir_func, "opendir");
3766 #if defined(Direntry_t) && defined(HAS_READDIR)
3768 #if !defined(I_DIRENT) && !defined(VMS)
3769 Direntry_t *readdir (DIR *);
3771 register Direntry_t *dp;
3773 register IO *io = GvIOn(gv);
3776 if (!io || !IoDIRP(io))
3779 if (GIMME == G_ARRAY) {
3781 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3783 sv = newSVpvn(dp->d_name, dp->d_namlen);
3785 sv = newSVpv(dp->d_name, 0);
3787 #ifndef INCOMPLETE_TAINTS
3788 if (!(IoFLAGS(io) & IOf_UNTAINT))
3791 XPUSHs(sv_2mortal(sv));
3795 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3798 sv = newSVpvn(dp->d_name, dp->d_namlen);
3800 sv = newSVpv(dp->d_name, 0);
3802 #ifndef INCOMPLETE_TAINTS
3803 if (!(IoFLAGS(io) & IOf_UNTAINT))
3806 XPUSHs(sv_2mortal(sv));
3812 SETERRNO(EBADF,RMS_ISI);
3813 if (GIMME == G_ARRAY)
3818 DIE(aTHX_ PL_no_dir_func, "readdir");
3824 #if defined(HAS_TELLDIR) || defined(telldir)
3826 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3827 /* XXX netbsd still seemed to.
3828 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3829 --JHI 1999-Feb-02 */
3830 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3831 long telldir (DIR *);
3834 register IO *io = GvIOn(gv);
3836 if (!io || !IoDIRP(io))
3839 PUSHi( PerlDir_tell(IoDIRP(io)) );
3843 SETERRNO(EBADF,RMS_ISI);
3846 DIE(aTHX_ PL_no_dir_func, "telldir");
3852 #if defined(HAS_SEEKDIR) || defined(seekdir)
3856 register IO *io = GvIOn(gv);
3858 if (!io || !IoDIRP(io))
3861 (void)PerlDir_seek(IoDIRP(io), along);
3866 SETERRNO(EBADF,RMS_ISI);
3869 DIE(aTHX_ PL_no_dir_func, "seekdir");
3875 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3878 register IO *io = GvIOn(gv);
3880 if (!io || !IoDIRP(io))
3883 (void)PerlDir_rewind(IoDIRP(io));
3887 SETERRNO(EBADF,RMS_ISI);
3890 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3896 #if defined(Direntry_t) && defined(HAS_READDIR)
3899 register IO *io = GvIOn(gv);
3901 if (!io || !IoDIRP(io))
3904 #ifdef VOID_CLOSEDIR
3905 PerlDir_close(IoDIRP(io));
3907 if (PerlDir_close(IoDIRP(io)) < 0) {
3908 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3917 SETERRNO(EBADF,RMS_IFI);
3920 DIE(aTHX_ PL_no_dir_func, "closedir");
3924 /* Process control. */
3934 PERL_FLUSHALL_FOR_CHILD;
3935 childpid = PerlProc_fork();
3940 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3941 SvREADONLY_off(GvSV(tmpgv));
3942 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3943 SvREADONLY_on(GvSV(tmpgv));
3945 #ifdef THREADS_HAVE_PIDS
3946 PL_ppid = (IV)getppid();
3948 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3953 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3958 PERL_FLUSHALL_FOR_CHILD;
3959 childpid = PerlProc_fork();
3965 DIE(aTHX_ PL_no_func, "fork");
3972 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3977 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3978 childpid = wait4pid(-1, &argflags, 0);
3980 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3985 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3986 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3987 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3989 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3994 DIE(aTHX_ PL_no_func, "wait");
4000 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4008 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4009 childpid = wait4pid(childpid, &argflags, optype);
4011 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4016 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4017 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4018 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4020 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4025 DIE(aTHX_ PL_no_func, "waitpid");
4031 dSP; dMARK; dORIGMARK; dTARGET;
4039 while (++MARK <= SP) {
4040 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4045 TAINT_PROPER("system");
4047 PERL_FLUSHALL_FOR_CHILD;
4048 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4053 if (PerlProc_pipe(pp) >= 0)
4055 while ((childpid = PerlProc_fork()) == -1) {
4056 if (errno != EAGAIN) {
4061 PerlLIO_close(pp[0]);
4062 PerlLIO_close(pp[1]);
4069 Sigsave_t ihand,qhand; /* place to save signals during system() */
4073 PerlLIO_close(pp[1]);
4075 rsignal_save(SIGINT, SIG_IGN, &ihand);
4076 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4079 result = wait4pid(childpid, &status, 0);
4080 } while (result == -1 && errno == EINTR);
4082 (void)rsignal_restore(SIGINT, &ihand);
4083 (void)rsignal_restore(SIGQUIT, &qhand);
4085 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4086 do_execfree(); /* free any memory child malloced on fork */
4092 while (n < sizeof(int)) {
4093 n1 = PerlLIO_read(pp[0],
4094 (void*)(((char*)&errkid)+n),
4100 PerlLIO_close(pp[0]);
4101 if (n) { /* Error */
4102 if (n != sizeof(int))
4103 DIE(aTHX_ "panic: kid popen errno read");
4104 errno = errkid; /* Propagate errno from kid */
4105 STATUS_CURRENT = -1;
4108 PUSHi(STATUS_CURRENT);
4112 PerlLIO_close(pp[0]);
4113 #if defined(HAS_FCNTL) && defined(F_SETFD)
4114 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4117 if (PL_op->op_flags & OPf_STACKED) {
4118 SV *really = *++MARK;
4119 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4121 else if (SP - MARK != 1)
4122 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4124 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4128 #else /* ! FORK or VMS or OS/2 */
4131 if (PL_op->op_flags & OPf_STACKED) {
4132 SV *really = *++MARK;
4134 value = (I32)do_aspawn(really, MARK, SP);
4136 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4139 else if (SP - MARK != 1) {
4141 value = (I32)do_aspawn(Nullsv, MARK, SP);
4143 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4147 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4149 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4151 STATUS_NATIVE_SET(value);
4154 PUSHi(result ? value : STATUS_CURRENT);
4155 #endif /* !FORK or VMS */
4161 dSP; dMARK; dORIGMARK; dTARGET;
4167 while (++MARK <= SP) {
4168 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4173 TAINT_PROPER("exec");
4175 PERL_FLUSHALL_FOR_CHILD;
4176 if (PL_op->op_flags & OPf_STACKED) {
4177 SV *really = *++MARK;
4178 value = (I32)do_aexec(really, MARK, SP);
4180 else if (SP - MARK != 1)
4182 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4186 (void ) do_aspawn(Nullsv, MARK, SP);
4190 value = (I32)do_aexec(Nullsv, MARK, SP);
4195 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4198 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4201 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4214 dSP; dMARK; dTARGET;
4216 value = (I32)apply(PL_op->op_type, MARK, SP);
4221 DIE(aTHX_ PL_no_func, "kill");
4229 # ifdef THREADS_HAVE_PIDS
4232 XPUSHi( getppid() );
4236 DIE(aTHX_ PL_no_func, "getppid");
4252 pgrp = (I32)BSD_GETPGRP(pid);
4254 if (pid != 0 && pid != PerlProc_getpid())
4255 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4261 DIE(aTHX_ PL_no_func, "getpgrp()");
4280 TAINT_PROPER("setpgrp");
4282 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4284 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4285 || (pid != 0 && pid != PerlProc_getpid()))
4287 DIE(aTHX_ "setpgrp can't take arguments");
4289 SETi( setpgrp() >= 0 );
4290 #endif /* USE_BSDPGRP */
4293 DIE(aTHX_ PL_no_func, "setpgrp()");
4299 #ifdef HAS_GETPRIORITY
4303 SETi( getpriority(which, who) );
4306 DIE(aTHX_ PL_no_func, "getpriority()");
4312 #ifdef HAS_SETPRIORITY
4317 TAINT_PROPER("setpriority");
4318 SETi( setpriority(which, who, niceval) >= 0 );
4321 DIE(aTHX_ PL_no_func, "setpriority()");
4331 XPUSHn( time(Null(Time_t*)) );
4333 XPUSHi( time(Null(Time_t*)) );
4344 (void)PerlProc_times(&PL_timesbuf);
4346 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4347 /* struct tms, though same data */
4351 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4352 if (GIMME == G_ARRAY) {
4353 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4354 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4355 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4359 DIE(aTHX_ "times not implemented");
4360 #endif /* HAS_TIMES */
4373 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4374 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4375 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4381 when = (Time_t)SvNVx(POPs);
4383 when = (Time_t)SvIVx(POPs);
4386 if (PL_op->op_type == OP_LOCALTIME)
4387 tmbuf = localtime(&when);
4389 tmbuf = gmtime(&when);
4391 if (GIMME != G_ARRAY) {
4397 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4398 dayname[tmbuf->tm_wday],
4399 monname[tmbuf->tm_mon],
4404 tmbuf->tm_year + 1900);
4405 PUSHs(sv_2mortal(tsv));
4410 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4411 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4412 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4413 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4414 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4415 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4416 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4417 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4418 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4429 anum = alarm((unsigned int)anum);
4436 DIE(aTHX_ PL_no_func, "alarm");
4447 (void)time(&lasttime);
4452 PerlProc_sleep((unsigned int)duration);
4455 XPUSHi(when - lasttime);
4459 /* Shared memory. */
4473 return pp_shmwrite();
4478 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4479 dSP; dMARK; dTARGET;
4480 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4489 /* Message passing. */
4503 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4504 dSP; dMARK; dTARGET;
4505 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4516 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4517 dSP; dMARK; dTARGET;
4518 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4531 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4532 dSP; dMARK; dTARGET;
4533 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4540 DIE(aTHX_ "System V IPC is not implemented on this machine");
4546 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4547 dSP; dMARK; dTARGET;
4548 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4556 PUSHp(zero_but_true, ZBTLEN);
4566 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4567 dSP; dMARK; dTARGET;
4568 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4577 /* Get system info. */
4581 #ifdef HAS_GETHOSTBYNAME
4582 return pp_ghostent();
4584 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4590 #ifdef HAS_GETHOSTBYADDR
4591 return pp_ghostent();
4593 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4599 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4601 I32 which = PL_op->op_type;
4602 register char **elem;
4604 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4605 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4606 struct hostent *gethostbyname(Netdb_name_t);
4607 struct hostent *gethostent(void);
4609 struct hostent *hent;
4614 if (which == OP_GHBYNAME) {
4615 #ifdef HAS_GETHOSTBYNAME
4616 char* name = POPpbytex;
4617 hent = PerlSock_gethostbyname(name);
4619 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4622 else if (which == OP_GHBYADDR) {
4623 #ifdef HAS_GETHOSTBYADDR
4624 int addrtype = POPi;
4627 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4629 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4631 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4635 #ifdef HAS_GETHOSTENT
4636 hent = PerlSock_gethostent();
4638 DIE(aTHX_ PL_no_sock_func, "gethostent");
4641 #ifdef HOST_NOT_FOUND
4643 #ifdef USE_REENTRANT_API
4644 # ifdef USE_GETHOSTENT_ERRNO
4645 h_errno = PL_reentrant_buffer->_gethostent_errno;
4648 STATUS_NATIVE_SET(h_errno);
4652 if (GIMME != G_ARRAY) {
4653 PUSHs(sv = sv_newmortal());
4655 if (which == OP_GHBYNAME) {
4657 sv_setpvn(sv, hent->h_addr, hent->h_length);
4660 sv_setpv(sv, (char*)hent->h_name);
4666 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4667 sv_setpv(sv, (char*)hent->h_name);
4668 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4669 for (elem = hent->h_aliases; elem && *elem; elem++) {
4670 sv_catpv(sv, *elem);
4672 sv_catpvn(sv, " ", 1);
4674 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4675 sv_setiv(sv, (IV)hent->h_addrtype);
4676 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4677 len = hent->h_length;
4678 sv_setiv(sv, (IV)len);
4680 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4681 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4682 sv_setpvn(sv, *elem, len);
4685 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4687 sv_setpvn(sv, hent->h_addr, len);
4692 DIE(aTHX_ PL_no_sock_func, "gethostent");
4698 #ifdef HAS_GETNETBYNAME
4699 return pp_gnetent();
4701 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4707 #ifdef HAS_GETNETBYADDR
4708 return pp_gnetent();
4710 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4716 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4718 I32 which = PL_op->op_type;
4719 register char **elem;
4721 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4722 struct netent *getnetbyaddr(Netdb_net_t, int);
4723 struct netent *getnetbyname(Netdb_name_t);
4724 struct netent *getnetent(void);
4726 struct netent *nent;
4729 if (which == OP_GNBYNAME){
4730 #ifdef HAS_GETNETBYNAME
4731 char *name = POPpbytex;
4732 nent = PerlSock_getnetbyname(name);
4734 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4737 else if (which == OP_GNBYADDR) {
4738 #ifdef HAS_GETNETBYADDR
4739 int addrtype = POPi;
4740 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4741 nent = PerlSock_getnetbyaddr(addr, addrtype);
4743 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4747 #ifdef HAS_GETNETENT
4748 nent = PerlSock_getnetent();
4750 DIE(aTHX_ PL_no_sock_func, "getnetent");
4753 #ifdef HOST_NOT_FOUND
4755 #ifdef USE_REENTRANT_API
4756 # ifdef USE_GETNETENT_ERRNO
4757 h_errno = PL_reentrant_buffer->_getnetent_errno;
4760 STATUS_NATIVE_SET(h_errno);
4765 if (GIMME != G_ARRAY) {
4766 PUSHs(sv = sv_newmortal());
4768 if (which == OP_GNBYNAME)
4769 sv_setiv(sv, (IV)nent->n_net);
4771 sv_setpv(sv, nent->n_name);
4777 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4778 sv_setpv(sv, nent->n_name);
4779 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4780 for (elem = nent->n_aliases; elem && *elem; elem++) {
4781 sv_catpv(sv, *elem);
4783 sv_catpvn(sv, " ", 1);
4785 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4786 sv_setiv(sv, (IV)nent->n_addrtype);
4787 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4788 sv_setiv(sv, (IV)nent->n_net);
4793 DIE(aTHX_ PL_no_sock_func, "getnetent");
4799 #ifdef HAS_GETPROTOBYNAME
4800 return pp_gprotoent();
4802 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4808 #ifdef HAS_GETPROTOBYNUMBER
4809 return pp_gprotoent();
4811 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4817 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4819 I32 which = PL_op->op_type;
4820 register char **elem;
4822 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4823 struct protoent *getprotobyname(Netdb_name_t);
4824 struct protoent *getprotobynumber(int);
4825 struct protoent *getprotoent(void);
4827 struct protoent *pent;
4830 if (which == OP_GPBYNAME) {
4831 #ifdef HAS_GETPROTOBYNAME
4832 char* name = POPpbytex;
4833 pent = PerlSock_getprotobyname(name);
4835 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4838 else if (which == OP_GPBYNUMBER) {
4839 #ifdef HAS_GETPROTOBYNUMBER
4841 pent = PerlSock_getprotobynumber(number);
4843 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4847 #ifdef HAS_GETPROTOENT
4848 pent = PerlSock_getprotoent();
4850 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4854 if (GIMME != G_ARRAY) {
4855 PUSHs(sv = sv_newmortal());
4857 if (which == OP_GPBYNAME)
4858 sv_setiv(sv, (IV)pent->p_proto);
4860 sv_setpv(sv, pent->p_name);
4866 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4867 sv_setpv(sv, pent->p_name);
4868 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4869 for (elem = pent->p_aliases; elem && *elem; elem++) {
4870 sv_catpv(sv, *elem);
4872 sv_catpvn(sv, " ", 1);
4874 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4875 sv_setiv(sv, (IV)pent->p_proto);
4880 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4886 #ifdef HAS_GETSERVBYNAME
4887 return pp_gservent();
4889 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4895 #ifdef HAS_GETSERVBYPORT
4896 return pp_gservent();
4898 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4904 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4906 I32 which = PL_op->op_type;
4907 register char **elem;
4909 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4910 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4911 struct servent *getservbyport(int, Netdb_name_t);
4912 struct servent *getservent(void);
4914 struct servent *sent;
4917 if (which == OP_GSBYNAME) {
4918 #ifdef HAS_GETSERVBYNAME
4919 char *proto = POPpbytex;
4920 char *name = POPpbytex;
4922 if (proto && !*proto)
4925 sent = PerlSock_getservbyname(name, proto);
4927 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4930 else if (which == OP_GSBYPORT) {
4931 #ifdef HAS_GETSERVBYPORT
4932 char *proto = POPpbytex;
4933 unsigned short port = (unsigned short)POPu;
4935 if (proto && !*proto)
4939 port = PerlSock_htons(port);
4941 sent = PerlSock_getservbyport(port, proto);
4943 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4947 #ifdef HAS_GETSERVENT
4948 sent = PerlSock_getservent();
4950 DIE(aTHX_ PL_no_sock_func, "getservent");
4954 if (GIMME != G_ARRAY) {
4955 PUSHs(sv = sv_newmortal());
4957 if (which == OP_GSBYNAME) {
4959 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4961 sv_setiv(sv, (IV)(sent->s_port));
4965 sv_setpv(sv, sent->s_name);
4971 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4972 sv_setpv(sv, sent->s_name);
4973 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4974 for (elem = sent->s_aliases; elem && *elem; elem++) {
4975 sv_catpv(sv, *elem);
4977 sv_catpvn(sv, " ", 1);
4979 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4981 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4983 sv_setiv(sv, (IV)(sent->s_port));
4985 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4986 sv_setpv(sv, sent->s_proto);
4991 DIE(aTHX_ PL_no_sock_func, "getservent");
4997 #ifdef HAS_SETHOSTENT
4999 PerlSock_sethostent(TOPi);
5002 DIE(aTHX_ PL_no_sock_func, "sethostent");
5008 #ifdef HAS_SETNETENT
5010 PerlSock_setnetent(TOPi);
5013 DIE(aTHX_ PL_no_sock_func, "setnetent");
5019 #ifdef HAS_SETPROTOENT
5021 PerlSock_setprotoent(TOPi);
5024 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5030 #ifdef HAS_SETSERVENT
5032 PerlSock_setservent(TOPi);
5035 DIE(aTHX_ PL_no_sock_func, "setservent");
5041 #ifdef HAS_ENDHOSTENT
5043 PerlSock_endhostent();
5047 DIE(aTHX_ PL_no_sock_func, "endhostent");
5053 #ifdef HAS_ENDNETENT
5055 PerlSock_endnetent();
5059 DIE(aTHX_ PL_no_sock_func, "endnetent");
5065 #ifdef HAS_ENDPROTOENT
5067 PerlSock_endprotoent();
5071 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5077 #ifdef HAS_ENDSERVENT
5079 PerlSock_endservent();
5083 DIE(aTHX_ PL_no_sock_func, "endservent");
5092 DIE(aTHX_ PL_no_func, "getpwnam");
5101 DIE(aTHX_ PL_no_func, "getpwuid");
5109 I32 which = PL_op->op_type;
5112 struct passwd *pwent = NULL;
5114 * We currently support only the SysV getsp* shadow password interface.
5115 * The interface is declared in <shadow.h> and often one needs to link
5116 * with -lsecurity or some such.
5117 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5120 * AIX getpwnam() is clever enough to return the encrypted password
5121 * only if the caller (euid?) is root.
5123 * There are at least two other shadow password APIs. Many platforms
5124 * seem to contain more than one interface for accessing the shadow
5125 * password databases, possibly for compatibility reasons.
5126 * The getsp*() is by far he simplest one, the other two interfaces
5127 * are much more complicated, but also very similar to each other.
5132 * struct pr_passwd *getprpw*();
5133 * The password is in
5134 * char getprpw*(...).ufld.fd_encrypt[]
5135 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5140 * struct es_passwd *getespw*();
5141 * The password is in
5142 * char *(getespw*(...).ufld.fd_encrypt)
5143 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5145 * Mention I_PROT here so that Configure probes for it.
5147 * In HP-UX for getprpw*() the manual page claims that one should include
5148 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5149 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5150 * and pp_sys.c already includes <shadow.h> if there is such.
5152 * Note that <sys/security.h> is already probed for, but currently
5153 * it is only included in special cases.
5155 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5156 * be preferred interface, even though also the getprpw*() interface
5157 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5158 * One also needs to call set_auth_parameters() in main() before
5159 * doing anything else, whether one is using getespw*() or getprpw*().
5161 * Note that accessing the shadow databases can be magnitudes
5162 * slower than accessing the standard databases.
5170 char* name = POPpbytex;
5171 pwent = getpwnam(name);
5177 pwent = getpwuid(uid);
5181 # ifdef HAS_GETPWENT
5183 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5184 if (pwent) pwent = getpwnam(pwent->pw_name);
5187 DIE(aTHX_ PL_no_func, "getpwent");
5193 if (GIMME != G_ARRAY) {
5194 PUSHs(sv = sv_newmortal());
5196 if (which == OP_GPWNAM)
5197 # if Uid_t_sign <= 0
5198 sv_setiv(sv, (IV)pwent->pw_uid);
5200 sv_setuv(sv, (UV)pwent->pw_uid);
5203 sv_setpv(sv, pwent->pw_name);
5209 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5210 sv_setpv(sv, pwent->pw_name);
5212 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5214 /* If we have getspnam(), we try to dig up the shadow
5215 * password. If we are underprivileged, the shadow
5216 * interface will set the errno to EACCES or similar,
5217 * and return a null pointer. If this happens, we will
5218 * use the dummy password (usually "*" or "x") from the
5219 * standard password database.
5221 * In theory we could skip the shadow call completely
5222 * if euid != 0 but in practice we cannot know which
5223 * security measures are guarding the shadow databases
5224 * on a random platform.
5226 * Resist the urge to use additional shadow interfaces.
5227 * Divert the urge to writing an extension instead.
5230 # ifdef HAS_GETSPNAM
5232 struct spwd *spwent;
5233 int saverrno; /* Save and restore errno so that
5234 * underprivileged attempts seem
5235 * to have never made the unsccessful
5236 * attempt to retrieve the shadow password. */
5239 spwent = getspnam(pwent->pw_name);
5241 if (spwent && spwent->sp_pwdp)
5242 sv_setpv(sv, spwent->sp_pwdp);
5246 if (!SvPOK(sv)) /* Use the standard password, then. */
5247 sv_setpv(sv, pwent->pw_passwd);
5250 # ifndef INCOMPLETE_TAINTS
5251 /* passwd is tainted because user himself can diddle with it.
5252 * admittedly not much and in a very limited way, but nevertheless. */
5256 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5257 # if Uid_t_sign <= 0
5258 sv_setiv(sv, (IV)pwent->pw_uid);
5260 sv_setuv(sv, (UV)pwent->pw_uid);
5263 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5264 # if Uid_t_sign <= 0
5265 sv_setiv(sv, (IV)pwent->pw_gid);
5267 sv_setuv(sv, (UV)pwent->pw_gid);
5269 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5270 * because of the poor interface of the Perl getpw*(),
5271 * not because there's some standard/convention saying so.
5272 * A better interface would have been to return a hash,
5273 * but we are accursed by our history, alas. --jhi. */
5274 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5276 sv_setiv(sv, (IV)pwent->pw_change);
5279 sv_setiv(sv, (IV)pwent->pw_quota);
5282 sv_setpv(sv, pwent->pw_age);
5287 /* pw_class and pw_comment are mutually exclusive--.
5288 * see the above note for pw_change, pw_quota, and pw_age. */
5289 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5291 sv_setpv(sv, pwent->pw_class);
5294 sv_setpv(sv, pwent->pw_comment);
5298 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5300 sv_setpv(sv, pwent->pw_gecos);
5302 # ifndef INCOMPLETE_TAINTS
5303 /* pw_gecos is tainted because user himself can diddle with it. */
5307 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5308 sv_setpv(sv, pwent->pw_dir);
5310 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5311 sv_setpv(sv, pwent->pw_shell);
5312 # ifndef INCOMPLETE_TAINTS
5313 /* pw_shell is tainted because user himself can diddle with it. */
5318 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5319 sv_setiv(sv, (IV)pwent->pw_expire);
5324 DIE(aTHX_ PL_no_func, "getpwent");
5330 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5335 DIE(aTHX_ PL_no_func, "setpwent");
5341 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5346 DIE(aTHX_ PL_no_func, "endpwent");
5355 DIE(aTHX_ PL_no_func, "getgrnam");
5364 DIE(aTHX_ PL_no_func, "getgrgid");
5372 I32 which = PL_op->op_type;
5373 register char **elem;
5375 struct group *grent;
5378 if (which == OP_GGRNAM) {
5379 char* name = POPpbytex;
5380 grent = (struct group *)getgrnam(name);
5382 else if (which == OP_GGRGID) {
5384 grent = (struct group *)getgrgid(gid);
5388 grent = (struct group *)getgrent();
5390 DIE(aTHX_ PL_no_func, "getgrent");
5394 if (GIMME != G_ARRAY) {
5395 PUSHs(sv = sv_newmortal());
5397 if (which == OP_GGRNAM)
5398 sv_setiv(sv, (IV)grent->gr_gid);
5400 sv_setpv(sv, grent->gr_name);
5406 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5407 sv_setpv(sv, grent->gr_name);
5409 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5411 sv_setpv(sv, grent->gr_passwd);
5414 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5415 sv_setiv(sv, (IV)grent->gr_gid);
5417 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5418 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5419 /* In UNICOS/mk (_CRAYMPP) the multithreading
5420 * versions (getgrnam_r, getgrgid_r)
5421 * seem to return an illegal pointer
5422 * as the group members list, gr_mem.
5423 * getgrent() doesn't even have a _r version
5424 * but the gr_mem is poisonous anyway.
5425 * So yes, you cannot get the list of group
5426 * members if building multithreaded in UNICOS/mk. */
5427 for (elem = grent->gr_mem; elem && *elem; elem++) {
5428 sv_catpv(sv, *elem);
5430 sv_catpvn(sv, " ", 1);
5437 DIE(aTHX_ PL_no_func, "getgrent");
5443 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5448 DIE(aTHX_ PL_no_func, "setgrent");
5454 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5459 DIE(aTHX_ PL_no_func, "endgrent");
5469 if (!(tmps = PerlProc_getlogin()))
5471 PUSHp(tmps, strlen(tmps));
5474 DIE(aTHX_ PL_no_func, "getlogin");
5478 /* Miscellaneous. */
5483 dSP; dMARK; dORIGMARK; dTARGET;
5484 register I32 items = SP - MARK;
5485 unsigned long a[20];
5491 while (++MARK <= SP) {
5492 if (SvTAINTED(*MARK)) {
5498 TAINT_PROPER("syscall");
5501 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5502 * or where sizeof(long) != sizeof(char*). But such machines will
5503 * not likely have syscall implemented either, so who cares?
5505 while (++MARK <= SP) {
5506 if (SvNIOK(*MARK) || !i)
5507 a[i++] = SvIV(*MARK);
5508 else if (*MARK == &PL_sv_undef)
5511 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5517 DIE(aTHX_ "Too many args to syscall");
5519 DIE(aTHX_ "Too few args to syscall");
5521 retval = syscall(a[0]);
5524 retval = syscall(a[0],a[1]);
5527 retval = syscall(a[0],a[1],a[2]);
5530 retval = syscall(a[0],a[1],a[2],a[3]);
5533 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5536 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5539 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5542 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5546 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5549 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5552 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5556 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5560 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5564 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5565 a[10],a[11],a[12],a[13]);
5567 #endif /* atarist */
5573 DIE(aTHX_ PL_no_func, "syscall");
5577 #ifdef FCNTL_EMULATE_FLOCK
5579 /* XXX Emulate flock() with fcntl().
5580 What's really needed is a good file locking module.
5584 fcntl_emulate_flock(int fd, int operation)
5588 switch (operation & ~LOCK_NB) {
5590 flock.l_type = F_RDLCK;
5593 flock.l_type = F_WRLCK;
5596 flock.l_type = F_UNLCK;
5602 flock.l_whence = SEEK_SET;
5603 flock.l_start = flock.l_len = (Off_t)0;
5605 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5608 #endif /* FCNTL_EMULATE_FLOCK */
5610 #ifdef LOCKF_EMULATE_FLOCK
5612 /* XXX Emulate flock() with lockf(). This is just to increase
5613 portability of scripts. The calls are not completely
5614 interchangeable. What's really needed is a good file
5618 /* The lockf() constants might have been defined in <unistd.h>.
5619 Unfortunately, <unistd.h> causes troubles on some mixed
5620 (BSD/POSIX) systems, such as SunOS 4.1.3.
5622 Further, the lockf() constants aren't POSIX, so they might not be
5623 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5624 just stick in the SVID values and be done with it. Sigh.
5628 # define F_ULOCK 0 /* Unlock a previously locked region */
5631 # define F_LOCK 1 /* Lock a region for exclusive use */
5634 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5637 # define F_TEST 3 /* Test a region for other processes locks */
5641 lockf_emulate_flock(int fd, int operation)
5647 /* flock locks entire file so for lockf we need to do the same */
5649 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5650 if (pos > 0) /* is seekable and needs to be repositioned */
5651 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5652 pos = -1; /* seek failed, so don't seek back afterwards */
5655 switch (operation) {
5657 /* LOCK_SH - get a shared lock */
5659 /* LOCK_EX - get an exclusive lock */
5661 i = lockf (fd, F_LOCK, 0);
5664 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5665 case LOCK_SH|LOCK_NB:
5666 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5667 case LOCK_EX|LOCK_NB:
5668 i = lockf (fd, F_TLOCK, 0);
5670 if ((errno == EAGAIN) || (errno == EACCES))
5671 errno = EWOULDBLOCK;
5674 /* LOCK_UN - unlock (non-blocking is a no-op) */
5676 case LOCK_UN|LOCK_NB:
5677 i = lockf (fd, F_ULOCK, 0);
5680 /* Default - can't decipher operation */
5687 if (pos > 0) /* need to restore position of the handle */
5688 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5693 #endif /* LOCKF_EMULATE_FLOCK */