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 */
176 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
177 #undef PERL_EFF_ACCESS_W_OK
178 #undef PERL_EFF_ACCESS_X_OK
180 /* F_OK unused: if stat() cannot find it... */
182 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
183 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
184 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
185 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
186 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
189 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
190 # ifdef I_SYS_SECURITY
191 # include <sys/security.h>
195 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
196 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
197 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
200 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
201 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
202 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
206 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
208 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
209 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
210 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
213 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
214 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
215 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
218 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
220 Uid_t ruid = getuid();
221 Uid_t euid = geteuid();
222 Gid_t rgid = getgid();
223 Gid_t egid = getegid();
227 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
228 Perl_croak(aTHX_ "switching effective uid is not implemented");
231 if (setreuid(euid, ruid))
234 if (setresuid(euid, ruid, (Uid_t)-1))
237 Perl_croak(aTHX_ "entering effective uid failed");
240 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
241 Perl_croak(aTHX_ "switching effective gid is not implemented");
244 if (setregid(egid, rgid))
247 if (setresgid(egid, rgid, (Gid_t)-1))
250 Perl_croak(aTHX_ "entering effective gid failed");
253 res = access(path, mode);
256 if (setreuid(ruid, euid))
259 if (setresuid(ruid, euid, (Uid_t)-1))
262 Perl_croak(aTHX_ "leaving effective uid failed");
265 if (setregid(rgid, egid))
268 if (setresgid(rgid, egid, (Gid_t)-1))
271 Perl_croak(aTHX_ "leaving effective gid failed");
276 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
277 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
278 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
281 #if !defined(PERL_EFF_ACCESS_R_OK)
282 /* With it or without it: anyway you get a warning: either that
283 it is unused, or it is declared static and never defined.
286 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
288 Perl_croak(aTHX_ "switching effective uid is not implemented");
304 if (PL_op->op_private & OPpOPEN_IN_RAW)
306 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
308 fp = PerlProc_popen(tmps, mode);
311 if (PL_curcop->cop_io) {
312 type = SvPV_nolen(PL_curcop->cop_io);
315 PerlIO_apply_layers(aTHX_ fp,mode,type);
317 if (gimme == G_VOID) {
319 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
323 else if (gimme == G_SCALAR) {
324 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
325 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
336 if (sv_gets(sv, fp, 0) == Nullch) {
340 XPUSHs(sv_2mortal(sv));
341 if (SvLEN(sv) - SvCUR(sv) > 20) {
342 SvLEN_set(sv, SvCUR(sv)+1);
343 Renew(SvPVX(sv), SvLEN(sv), char);
348 STATUS_NATIVE_SET(PerlProc_pclose(fp));
349 TAINT; /* "I believe that this is not gratuitous!" */
352 STATUS_NATIVE_SET(-1);
353 if (gimme == G_SCALAR)
363 tryAMAGICunTARGET(iter, -1);
365 /* Note that we only ever get here if File::Glob fails to load
366 * without at the same time croaking, for some reason, or if
367 * perl was built with PERL_EXTERNAL_GLOB */
374 * The external globbing program may use things we can't control,
375 * so for security reasons we must assume the worst.
378 taint_proper(PL_no_security, "glob");
382 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
383 PL_last_in_gv = (GV*)*PL_stack_sp--;
385 SAVESPTR(PL_rs); /* This is not permanent, either. */
386 PL_rs = sv_2mortal(newSVpvn("\000", 1));
389 *SvPVX(PL_rs) = '\n';
393 result = do_readline();
400 PL_last_in_gv = cGVOP_gv;
401 return do_readline();
410 if (SP - MARK != 1) {
412 do_join(TARG, &PL_sv_no, MARK, SP);
419 tmps = SvPV(tmpsv, len);
422 (void)SvUPGRADE(error, SVt_PV);
423 if (SvPOK(error) && SvCUR(error))
424 sv_catpv(error, "\t...caught");
426 tmps = SvPV(tmpsv, len);
429 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
431 Perl_warn(aTHX_ "%"SVf, tmpsv);
443 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
445 if (SP - MARK != 1) {
447 do_join(TARG, &PL_sv_no, MARK, SP);
449 tmps = SvPV(tmpsv, len);
455 tmps = (SvROK(tmpsv) && PL_in_eval) ? Nullch : SvPV(tmpsv, len);
459 (void)SvUPGRADE(error, SVt_PV);
460 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
462 SvSetSV(error,tmpsv);
463 else if (sv_isobject(error)) {
464 HV *stash = SvSTASH(SvRV(error));
465 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
467 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
468 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
475 call_sv((SV*)GvCV(gv),
476 G_SCALAR|G_EVAL|G_KEEPERR);
477 sv_setsv(error,*PL_stack_sp--);
480 DIE(aTHX_ Nullformat);
483 if (SvPOK(error) && SvCUR(error))
484 sv_catpv(error, "\t...propagated");
486 tmps = SvPV(tmpsv, len);
490 tmpsv = sv_2mortal(newSVpvn("Died", 4));
492 DIE(aTHX_ "%"SVf, tmpsv);
512 DIE(aTHX_ PL_no_usym, "filehandle");
513 if ((io = GvIOp(gv)))
514 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
516 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
517 /* Method's args are same as ours ... */
518 /* ... except handle is replaced by the object */
519 *MARK-- = SvTIED_obj((SV*)io, mg);
523 call_method("OPEN", G_SCALAR);
536 tmps = SvPV(sv, len);
537 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
540 PUSHi( (I32)PL_forkprocess );
541 else if (PL_forkprocess == 0) /* we are a new child */
560 if (gv && (io = GvIO(gv))
561 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
564 XPUSHs(SvTIED_obj((SV*)io, mg));
567 call_method("CLOSE", G_SCALAR);
573 PUSHs(boolSV(do_close(gv, TRUE)));
593 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
594 DIE(aTHX_ PL_no_usym, "filehandle");
599 do_close(rgv, FALSE);
601 do_close(wgv, FALSE);
603 if (PerlProc_pipe(fd) < 0)
606 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
607 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
608 IoIFP(wstio) = IoOFP(wstio);
609 IoTYPE(rstio) = IoTYPE_RDONLY;
610 IoTYPE(wstio) = IoTYPE_WRONLY;
612 if (!IoIFP(rstio) || !IoOFP(wstio)) {
613 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
614 else PerlLIO_close(fd[0]);
615 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
616 else PerlLIO_close(fd[1]);
619 #if defined(HAS_FCNTL) && defined(F_SETFD)
620 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
621 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
628 DIE(aTHX_ PL_no_func, "pipe");
644 if (gv && (io = GvIO(gv))
645 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
648 XPUSHs(SvTIED_obj((SV*)io, mg));
651 call_method("FILENO", G_SCALAR);
657 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
658 /* Can't do this because people seem to do things like
659 defined(fileno($foo)) to check whether $foo is a valid fh.
660 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
661 report_evil_fh(gv, io, PL_op->op_type);
666 PUSHi(PerlIO_fileno(fp));
677 anum = PerlLIO_umask(0);
678 (void)PerlLIO_umask(anum);
681 anum = PerlLIO_umask(POPi);
682 TAINT_PROPER("umask");
685 /* Only DIE if trying to restrict permissions on `user' (self).
686 * Otherwise it's harmless and more useful to just return undef
687 * since 'group' and 'other' concepts probably don't exist here. */
688 if (MAXARG >= 1 && (POPi & 0700))
689 DIE(aTHX_ "umask not implemented");
690 XPUSHs(&PL_sv_undef);
712 if (gv && (io = GvIO(gv))
713 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
716 XPUSHs(SvTIED_obj((SV*)io, mg));
721 call_method("BINMODE", G_SCALAR);
728 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
729 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
730 report_evil_fh(gv, io, PL_op->op_type);
734 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
735 (discp) ? SvPV_nolen(discp) : Nullch))
749 I32 markoff = MARK - PL_stack_base;
751 int how = PERL_MAGIC_tied;
756 switch(SvTYPE(varsv)) {
758 methname = "TIEHASH";
759 HvEITER((HV *)varsv) = Null(HE *);
762 methname = "TIEARRAY";
765 #ifdef GV_UNIQUE_CHECK
766 if (GvUNIQUE((GV*)varsv)) {
767 Perl_croak(aTHX_ "Attempt to tie unique GV");
770 methname = "TIEHANDLE";
771 how = PERL_MAGIC_tiedscalar;
772 /* For tied filehandles, we apply tiedscalar magic to the IO
773 slot of the GP rather than the GV itself. AMS 20010812 */
775 GvIOp(varsv) = newIO();
776 varsv = (SV *)GvIOp(varsv);
779 methname = "TIESCALAR";
780 how = PERL_MAGIC_tiedscalar;
784 if (sv_isobject(*MARK)) {
786 PUSHSTACKi(PERLSI_MAGIC);
792 call_method(methname, G_SCALAR);
795 /* Not clear why we don't call call_method here too.
796 * perhaps to get different error message ?
798 stash = gv_stashsv(*MARK, FALSE);
799 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
800 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%s\"",
801 methname, SvPV(*MARK,n_a));
804 PUSHSTACKi(PERLSI_MAGIC);
810 call_sv((SV*)GvCV(gv), G_SCALAR);
816 if (sv_isobject(sv)) {
817 sv_unmagic(varsv, how);
818 /* Croak if a self-tie on an aggregate is attempted. */
819 if (varsv == SvRV(sv) &&
820 (SvTYPE(sv) == SVt_PVAV ||
821 SvTYPE(sv) == SVt_PVHV))
823 "Self-ties of arrays and hashes are not supported");
824 sv_magic(varsv, sv, how, Nullch, 0);
827 SP = PL_stack_base + markoff;
837 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
838 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
840 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
843 if ((mg = SvTIED_mg(sv, how))) {
844 SV *obj = SvRV(mg->mg_obj);
848 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
849 isGV(gv) && (cv = GvCV(gv))) {
851 XPUSHs(SvTIED_obj((SV*)gv, mg));
852 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
855 call_sv((SV *)cv, G_VOID);
859 else if (ckWARN(WARN_UNTIE)) {
860 if (mg && SvREFCNT(obj) > 1)
861 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
862 "untie attempted while %"UVuf" inner references still exist",
863 (UV)SvREFCNT(obj) - 1 ) ;
866 sv_unmagic(sv, how) ;
876 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
877 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
879 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
882 if ((mg = SvTIED_mg(sv, how))) {
883 SV *osv = SvTIED_obj(sv, mg);
884 if (osv == mg->mg_obj)
885 osv = sv_mortalcopy(osv);
903 sv = sv_mortalcopy(&PL_sv_no);
904 sv_setpv(sv, "AnyDBM_File");
905 stash = gv_stashsv(sv, FALSE);
906 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
908 require_pv("AnyDBM_File.pm");
910 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
911 DIE(aTHX_ "No dbm on this machine");
921 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
923 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
926 call_sv((SV*)GvCV(gv), G_SCALAR);
929 if (!sv_isobject(TOPs)) {
934 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
937 call_sv((SV*)GvCV(gv), G_SCALAR);
941 if (sv_isobject(TOPs)) {
942 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
943 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
965 struct timeval timebuf;
966 struct timeval *tbuf = &timebuf;
970 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
975 # if BYTEORDER & 0xf0000
976 # define ORDERBYTE (0x88888888 - BYTEORDER)
978 # define ORDERBYTE (0x4444 - BYTEORDER)
984 for (i = 1; i <= 3; i++) {
992 /* little endians can use vecs directly */
993 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1000 masksize = NFDBITS / NBBY;
1002 masksize = sizeof(long); /* documented int, everyone seems to use long */
1004 Zero(&fd_sets[0], 4, char*);
1007 # if SELECT_MIN_BITS > 1
1008 /* If SELECT_MIN_BITS is greater than one we most probably will want
1009 * to align the sizes with SELECT_MIN_BITS/8 because for example
1010 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1011 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1012 * on (sets/tests/clears bits) is 32 bits. */
1013 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1015 growsize = sizeof(fd_set);
1023 timebuf.tv_sec = (long)value;
1024 value -= (NV)timebuf.tv_sec;
1025 timebuf.tv_usec = (long)(value * 1000000.0);
1028 tbuf = Null(struct timeval*);
1030 for (i = 1; i <= 3; i++) {
1036 else if (!SvPOK(sv))
1037 SvPV_force(sv,n_a); /* force string conversion */
1040 Sv_Grow(sv, growsize);
1044 while (++j <= growsize) {
1048 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1050 New(403, fd_sets[i], growsize, char);
1051 for (offset = 0; offset < growsize; offset += masksize) {
1052 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1053 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1056 fd_sets[i] = SvPVX(sv);
1060 nfound = PerlSock_select(
1062 (Select_fd_set_t) fd_sets[1],
1063 (Select_fd_set_t) fd_sets[2],
1064 (Select_fd_set_t) fd_sets[3],
1066 for (i = 1; i <= 3; i++) {
1069 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1071 for (offset = 0; offset < growsize; offset += masksize) {
1072 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1073 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1075 Safefree(fd_sets[i]);
1082 if (GIMME == G_ARRAY && tbuf) {
1083 value = (NV)(timebuf.tv_sec) +
1084 (NV)(timebuf.tv_usec) / 1000000.0;
1085 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1086 sv_setnv(sv, value);
1090 DIE(aTHX_ "select not implemented");
1095 Perl_setdefout(pTHX_ GV *gv)
1098 (void)SvREFCNT_inc(gv);
1100 SvREFCNT_dec(PL_defoutgv);
1107 GV *newdefout, *egv;
1110 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1112 egv = GvEGV(PL_defoutgv);
1117 XPUSHs(&PL_sv_undef);
1119 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1120 if (gvp && *gvp == egv) {
1121 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1125 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1130 if (!GvIO(newdefout))
1131 gv_IOadd(newdefout);
1132 setdefout(newdefout);
1150 if (gv && (io = GvIO(gv))
1151 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1153 I32 gimme = GIMME_V;
1155 XPUSHs(SvTIED_obj((SV*)io, mg));
1158 call_method("GETC", gimme);
1161 if (gimme == G_SCALAR)
1162 SvSetMagicSV_nosteal(TARG, TOPs);
1165 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1166 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1167 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1168 report_evil_fh(gv, io, PL_op->op_type);
1172 sv_setpv(TARG, " ");
1173 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1174 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1175 /* Find out how many bytes the char needs */
1176 Size_t len = UTF8SKIP(SvPVX(TARG));
1179 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1180 SvCUR_set(TARG,1+len);
1190 return pp_sysread();
1194 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1196 register PERL_CONTEXT *cx;
1197 I32 gimme = GIMME_V;
1198 AV* padlist = CvPADLIST(cv);
1199 SV** svp = AvARRAY(padlist);
1205 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1207 SAVEVPTR(PL_curpad);
1208 PL_curpad = AvARRAY((AV*)svp[1]);
1210 setdefout(gv); /* locally select filehandle so $% et al work */
1243 SV *tmpsv = sv_newmortal();
1244 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1245 name = SvPV_nolen(tmpsv);
1248 DIE(aTHX_ "Undefined format \"%s\" called", name);
1249 DIE(aTHX_ "Not a format reference");
1252 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1254 IoFLAGS(io) &= ~IOf_DIDTOP;
1255 return doform(cv,gv,PL_op->op_next);
1261 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1262 register IO *io = GvIOp(gv);
1263 PerlIO *ofp = IoOFP(io);
1267 register PERL_CONTEXT *cx;
1269 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1270 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1273 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1274 PL_formtarget != PL_toptarget)
1278 if (!IoTOP_GV(io)) {
1282 if (!IoTOP_NAME(io)) {
1283 if (!IoFMT_NAME(io))
1284 IoFMT_NAME(io) = savepv(GvNAME(gv));
1285 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1286 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1287 if ((topgv && GvFORM(topgv)) ||
1288 !gv_fetchpv("top",FALSE,SVt_PVFM))
1289 IoTOP_NAME(io) = savepv(SvPVX(topname));
1291 IoTOP_NAME(io) = savepv("top");
1293 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1294 if (!topgv || !GvFORM(topgv)) {
1295 IoLINES_LEFT(io) = 100000000;
1298 IoTOP_GV(io) = topgv;
1300 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1301 I32 lines = IoLINES_LEFT(io);
1302 char *s = SvPVX(PL_formtarget);
1303 if (lines <= 0) /* Yow, header didn't even fit!!! */
1305 while (lines-- > 0) {
1306 s = strchr(s, '\n');
1312 STRLEN save = SvCUR(PL_formtarget);
1313 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1314 do_print(PL_formtarget, ofp);
1315 SvCUR_set(PL_formtarget, save);
1316 sv_chop(PL_formtarget, s);
1317 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1320 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1321 do_print(PL_formfeed, ofp);
1322 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1324 PL_formtarget = PL_toptarget;
1325 IoFLAGS(io) |= IOf_DIDTOP;
1328 DIE(aTHX_ "bad top format reference");
1333 SV *sv = sv_newmortal();
1334 gv_efullname4(sv, fgv, Nullch, FALSE);
1335 name = SvPV_nolen(sv);
1338 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1341 DIE(aTHX_ "Undefined top format called");
1345 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1346 return doform(cv,gv,PL_op);
1350 POPBLOCK(cx,PL_curpm);
1356 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1358 /* integrate with report_evil_fh()? */
1361 SV* sv = sv_newmortal();
1362 gv_efullname4(sv, gv, Nullch, FALSE);
1363 name = SvPV_nolen(sv);
1366 Perl_warner(aTHX_ packWARN(WARN_IO),
1367 "Filehandle %s opened only for input", name);
1369 Perl_warner(aTHX_ packWARN(WARN_IO),
1370 "Filehandle opened only for input");
1372 else if (ckWARN(WARN_CLOSED))
1373 report_evil_fh(gv, io, PL_op->op_type);
1378 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1379 if (ckWARN(WARN_IO))
1380 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1382 if (!do_print(PL_formtarget, fp))
1385 FmLINES(PL_formtarget) = 0;
1386 SvCUR_set(PL_formtarget, 0);
1387 *SvEND(PL_formtarget) = '\0';
1388 if (IoFLAGS(io) & IOf_FLUSH)
1389 (void)PerlIO_flush(fp);
1394 PL_formtarget = PL_bodytarget;
1396 return pop_return();
1401 dSP; dMARK; dORIGMARK;
1408 if (PL_op->op_flags & OPf_STACKED)
1413 if (gv && (io = GvIO(gv))
1414 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1416 if (MARK == ORIGMARK) {
1419 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1423 *MARK = SvTIED_obj((SV*)io, mg);
1426 call_method("PRINTF", G_SCALAR);
1429 MARK = ORIGMARK + 1;
1436 if (!(io = GvIO(gv))) {
1437 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1438 report_evil_fh(gv, io, PL_op->op_type);
1439 SETERRNO(EBADF,RMS$_IFI);
1442 else if (!(fp = IoOFP(io))) {
1443 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1444 /* integrate with report_evil_fh()? */
1448 gv_efullname4(sv, gv, Nullch, FALSE);
1449 name = SvPV_nolen(sv);
1452 Perl_warner(aTHX_ packWARN(WARN_IO),
1453 "Filehandle %s opened only for input", name);
1455 Perl_warner(aTHX_ packWARN(WARN_IO),
1456 "Filehandle opened only for input");
1458 else if (ckWARN(WARN_CLOSED))
1459 report_evil_fh(gv, io, PL_op->op_type);
1461 SETERRNO(EBADF,IoIFP(io)?RMS$_FAC:RMS$_IFI);
1465 do_sprintf(sv, SP - MARK, MARK + 1);
1466 if (!do_print(sv, fp))
1469 if (IoFLAGS(io) & IOf_FLUSH)
1470 if (PerlIO_flush(fp) == EOF)
1481 PUSHs(&PL_sv_undef);
1502 /* Need TIEHANDLE method ? */
1504 tmps = SvPV(sv, len);
1505 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1506 IoLINES(GvIOp(gv)) = 0;
1510 PUSHs(&PL_sv_undef);
1517 dSP; dMARK; dORIGMARK; dTARGET;
1524 Sock_size_t bufsize;
1531 bool charstart = FALSE;
1532 STRLEN charskip = 0;
1536 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1537 && gv && (io = GvIO(gv))
1538 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1543 *MARK = SvTIED_obj((SV*)io, mg);
1545 call_method("READ", G_SCALAR);
1558 sv_setpvn(bufsv, "", 0);
1559 length = SvIVx(*++MARK);
1562 offset = SvIVx(*++MARK);
1566 if (!io || !IoIFP(io))
1568 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1569 buffer = SvPVutf8_force(bufsv, blen);
1570 /* UTF8 may not have been set if they are all low bytes */
1574 buffer = SvPV_force(bufsv, blen);
1577 DIE(aTHX_ "Negative length");
1585 if (PL_op->op_type == OP_RECV) {
1586 char namebuf[MAXPATHLEN];
1587 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1588 bufsize = sizeof (struct sockaddr_in);
1590 bufsize = sizeof namebuf;
1592 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1596 buffer = SvGROW(bufsv, length+1);
1597 /* 'offset' means 'flags' here */
1598 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1599 (struct sockaddr *)namebuf, &bufsize);
1603 /* Bogus return without padding */
1604 bufsize = sizeof (struct sockaddr_in);
1606 SvCUR_set(bufsv, count);
1607 *SvEND(bufsv) = '\0';
1608 (void)SvPOK_only(bufsv);
1612 /* This should not be marked tainted if the fp is marked clean */
1613 if (!(IoFLAGS(io) & IOf_UNTAINT))
1614 SvTAINTED_on(bufsv);
1616 sv_setpvn(TARG, namebuf, bufsize);
1621 if (PL_op->op_type == OP_RECV)
1622 DIE(aTHX_ PL_no_sock_func, "recv");
1624 if (DO_UTF8(bufsv)) {
1625 /* offset adjust in characters not bytes */
1626 blen = sv_len_utf8(bufsv);
1630 DIE(aTHX_ "Offset outside string");
1633 if (DO_UTF8(bufsv)) {
1634 /* convert offset-as-chars to offset-as-bytes */
1635 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1638 bufsize = SvCUR(bufsv);
1639 buffer = SvGROW(bufsv, length+offset+1);
1640 if (offset > bufsize) { /* Zero any newly allocated space */
1641 Zero(buffer+bufsize, offset-bufsize, char);
1643 buffer = buffer + offset;
1645 if (PL_op->op_type == OP_SYSREAD) {
1646 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1647 if (IoTYPE(io) == IoTYPE_SOCKET) {
1648 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1654 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1659 #ifdef HAS_SOCKET__bad_code_maybe
1660 if (IoTYPE(io) == IoTYPE_SOCKET) {
1661 char namebuf[MAXPATHLEN];
1662 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1663 bufsize = sizeof (struct sockaddr_in);
1665 bufsize = sizeof namebuf;
1667 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1668 (struct sockaddr *)namebuf, &bufsize);
1673 count = PerlIO_read(IoIFP(io), buffer, length);
1674 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1675 if (count == 0 && PerlIO_error(IoIFP(io)))
1679 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1681 /* integrate with report_evil_fh()? */
1684 SV* sv = sv_newmortal();
1685 gv_efullname4(sv, gv, Nullch, FALSE);
1686 name = SvPV_nolen(sv);
1689 Perl_warner(aTHX_ packWARN(WARN_IO),
1690 "Filehandle %s opened only for output", name);
1692 Perl_warner(aTHX_ packWARN(WARN_IO),
1693 "Filehandle opened only for output");
1697 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1698 *SvEND(bufsv) = '\0';
1699 (void)SvPOK_only(bufsv);
1700 if (fp_utf8 && !IN_BYTES) {
1701 /* Look at utf8 we got back and count the characters */
1702 char *bend = buffer + count;
1703 while (buffer < bend) {
1705 skip = UTF8SKIP(buffer);
1708 if (buffer - charskip + skip > bend) {
1709 /* partial character - try for rest of it */
1710 length = skip - (bend-buffer);
1711 offset = bend - SvPVX(bufsv);
1723 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1724 provided amount read (count) was what was requested (length)
1726 if (got < wanted && count == length) {
1727 length = wanted - got;
1728 offset = bend - SvPVX(bufsv);
1731 /* return value is character count */
1736 /* This should not be marked tainted if the fp is marked clean */
1737 if (!(IoFLAGS(io) & IOf_UNTAINT))
1738 SvTAINTED_on(bufsv);
1751 int items = (SP - PL_stack_base) - TOPMARK;
1755 sv = sv_2mortal(newSViv(sv_len(*SP)));
1764 dSP; dMARK; dORIGMARK; dTARGET;
1775 if (PL_op->op_type == OP_SYSWRITE
1776 && gv && (io = GvIO(gv))
1777 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1782 *MARK = SvTIED_obj((SV*)io, mg);
1784 call_method("WRITE", G_SCALAR);
1795 #if Size_t_size > IVSIZE
1796 length = (Size_t)SvNVx(*++MARK);
1798 length = (Size_t)SvIVx(*++MARK);
1800 if ((SSize_t)length < 0)
1801 DIE(aTHX_ "Negative length");
1804 if (!io || !IoIFP(io)) {
1806 if (ckWARN(WARN_CLOSED))
1807 report_evil_fh(gv, io, PL_op->op_type);
1811 if (PerlIO_isutf8(IoIFP(io))) {
1812 buffer = SvPVutf8(bufsv, blen);
1816 sv_utf8_downgrade(bufsv, FALSE);
1817 buffer = SvPV(bufsv, blen);
1820 if (PL_op->op_type == OP_SYSWRITE) {
1822 if (DO_UTF8(bufsv)) {
1823 /* length and offset are in chars */
1824 blen = sv_len_utf8(bufsv);
1827 offset = SvIVx(*++MARK);
1830 DIE(aTHX_ "Offset outside string");
1832 } else if (offset >= blen && blen > 0)
1833 DIE(aTHX_ "Offset outside string");
1836 if (length > blen - offset)
1837 length = blen - offset;
1838 if (DO_UTF8(bufsv)) {
1839 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1840 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1843 buffer = buffer+offset;
1845 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1846 if (IoTYPE(io) == IoTYPE_SOCKET) {
1847 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1853 /* See the note at doio.c:do_print about filesize limits. --jhi */
1854 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1859 else if (SP > MARK) {
1862 sockbuf = SvPVx(*++MARK, mlen);
1863 /* length is really flags */
1864 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1865 length, (struct sockaddr *)sockbuf, mlen);
1868 /* length is really flags */
1869 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1872 DIE(aTHX_ PL_no_sock_func, "send");
1878 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1879 #if Size_t_size > IVSIZE
1893 return pp_sysread();
1904 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1906 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1908 if (io && !IoIFP(io)) {
1909 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1911 IoFLAGS(io) &= ~IOf_START;
1912 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1913 sv_setpvn(GvSV(gv), "-", 1);
1914 SvSETMAGIC(GvSV(gv));
1916 else if (!nextargv(gv))
1921 gv = PL_last_in_gv; /* eof */
1924 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1926 if (gv && (io = GvIO(gv))
1927 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1930 XPUSHs(SvTIED_obj((SV*)io, mg));
1933 call_method("EOF", G_SCALAR);
1939 PUSHs(boolSV(!gv || do_eof(gv)));
1953 gv = PL_last_in_gv = (GV*)POPs;
1955 if (gv && (io = GvIO(gv))
1956 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1959 XPUSHs(SvTIED_obj((SV*)io, mg));
1962 call_method("TELL", G_SCALAR);
1968 #if LSEEKSIZE > IVSIZE
1969 PUSHn( do_tell(gv) );
1971 PUSHi( do_tell(gv) );
1978 return pp_sysseek();
1987 #if LSEEKSIZE > IVSIZE
1988 Off_t offset = (Off_t)SvNVx(POPs);
1990 Off_t offset = (Off_t)SvIVx(POPs);
1994 gv = PL_last_in_gv = (GV*)POPs;
1996 if (gv && (io = GvIO(gv))
1997 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2000 XPUSHs(SvTIED_obj((SV*)io, mg));
2001 #if LSEEKSIZE > IVSIZE
2002 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2004 XPUSHs(sv_2mortal(newSViv(offset)));
2006 XPUSHs(sv_2mortal(newSViv(whence)));
2009 call_method("SEEK", G_SCALAR);
2015 if (PL_op->op_type == OP_SEEK)
2016 PUSHs(boolSV(do_seek(gv, offset, whence)));
2018 Off_t sought = do_sysseek(gv, offset, whence);
2020 PUSHs(&PL_sv_undef);
2023 #if LSEEKSIZE > IVSIZE
2028 : newSVpvn(zero_but_true, ZBTLEN);
2029 PUSHs(sv_2mortal(sv));
2038 /* There seems to be no consensus on the length type of truncate()
2039 * and ftruncate(), both off_t and size_t have supporters. In
2040 * general one would think that when using large files, off_t is
2041 * at least as wide as size_t, so using an off_t should be okay. */
2042 /* XXX Configure probe for the length type of *truncate() needed XXX */
2045 #if Size_t_size > IVSIZE
2050 /* Checking for length < 0 is problematic as the type might or
2051 * might not be signed: if it is not, clever compilers will moan. */
2052 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2054 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2060 if (PL_op->op_flags & OPf_SPECIAL) {
2061 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2064 TAINT_PROPER("truncate");
2065 if (!GvIO(tmpgv) || !IoIFP(GvIOp(tmpgv)))
2068 PerlIO_flush(IoIFP(GvIOp(tmpgv)));
2070 if (ftruncate(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2072 if (my_chsize(PerlIO_fileno(IoIFP(GvIOn(tmpgv))), len) < 0)
2081 if (SvTYPE(sv) == SVt_PVGV) {
2082 tmpgv = (GV*)sv; /* *main::FRED for example */
2085 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2086 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2090 name = SvPV(sv, n_a);
2091 TAINT_PROPER("truncate");
2093 if (truncate(name, len) < 0)
2099 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2102 if (my_chsize(tmpfd, len) < 0)
2104 PerlLIO_close(tmpfd);
2113 SETERRNO(EBADF,RMS$_IFI);
2117 DIE(aTHX_ "truncate not implemented");
2130 unsigned int func = POPu;
2131 int optype = PL_op->op_type;
2135 IO *io = gv ? GvIOn(gv) : 0;
2137 if (!io || !argsv || !IoIFP(io)) {
2138 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2139 report_evil_fh(gv, io, PL_op->op_type);
2140 SETERRNO(EBADF,RMS$_IFI); /* well, sort of... */
2144 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2147 s = SvPV_force(argsv, len);
2148 need = IOCPARM_LEN(func);
2150 s = Sv_Grow(argsv, need + 1);
2151 SvCUR_set(argsv, need);
2154 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2157 retval = SvIV(argsv);
2158 s = INT2PTR(char*,retval); /* ouch */
2161 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2163 if (optype == OP_IOCTL)
2165 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2167 DIE(aTHX_ "ioctl is not implemented");
2171 #if defined(OS2) && defined(__EMX__)
2172 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2174 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2177 DIE(aTHX_ "fcntl is not implemented");
2181 if (s[SvCUR(argsv)] != 17)
2182 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2184 s[SvCUR(argsv)] = 0; /* put our null back */
2185 SvSETMAGIC(argsv); /* Assume it has changed */
2194 PUSHp(zero_but_true, ZBTLEN);
2214 if (gv && (io = GvIO(gv)))
2221 (void)PerlIO_flush(fp);
2222 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2225 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2226 report_evil_fh(gv, io, PL_op->op_type);
2228 SETERRNO(EBADF,RMS$_IFI);
2233 DIE(aTHX_ PL_no_func, "flock()");
2245 int protocol = POPi;
2251 io = gv ? GvIOn(gv) : NULL;
2254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2255 report_evil_fh(gv, io, PL_op->op_type);
2257 do_close(gv, FALSE);
2258 SETERRNO(EBADF,LIB$_INVARG);
2263 do_close(gv, FALSE);
2265 TAINT_PROPER("socket");
2266 fd = PerlSock_socket(domain, type, protocol);
2269 IoIFP(io) = PerlIO_fdopen(fd, "r"); /* stdio gets confused about sockets */
2270 IoOFP(io) = PerlIO_fdopen(fd, "w");
2271 IoTYPE(io) = IoTYPE_SOCKET;
2272 if (!IoIFP(io) || !IoOFP(io)) {
2273 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2274 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2275 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2278 #if defined(HAS_FCNTL) && defined(F_SETFD)
2279 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2283 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2288 DIE(aTHX_ PL_no_sock_func, "socket");
2294 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2300 int protocol = POPi;
2307 io1 = gv1 ? GvIOn(gv1) : NULL;
2308 io2 = gv2 ? GvIOn(gv2) : NULL;
2309 if (!gv1 || !gv2 || !io1 || !io2) {
2310 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2312 report_evil_fh(gv1, io1, PL_op->op_type);
2314 report_evil_fh(gv1, io2, PL_op->op_type);
2317 do_close(gv1, FALSE);
2319 do_close(gv2, FALSE);
2324 do_close(gv1, FALSE);
2326 do_close(gv2, FALSE);
2328 TAINT_PROPER("socketpair");
2329 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2331 IoIFP(io1) = PerlIO_fdopen(fd[0], "r");
2332 IoOFP(io1) = PerlIO_fdopen(fd[0], "w");
2333 IoTYPE(io1) = IoTYPE_SOCKET;
2334 IoIFP(io2) = PerlIO_fdopen(fd[1], "r");
2335 IoOFP(io2) = PerlIO_fdopen(fd[1], "w");
2336 IoTYPE(io2) = IoTYPE_SOCKET;
2337 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2338 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2339 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2340 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2341 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2342 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2343 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2346 #if defined(HAS_FCNTL) && defined(F_SETFD)
2347 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2348 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2353 DIE(aTHX_ PL_no_sock_func, "socketpair");
2361 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2362 extern void GETPRIVMODE();
2363 extern void GETUSERMODE();
2368 register IO *io = GvIOn(gv);
2372 int mpeprivmode = 0;
2375 if (!io || !IoIFP(io))
2378 addr = SvPV(addrsv, len);
2379 TAINT_PROPER("bind");
2380 #ifdef MPE /* Deal with MPE bind() peculiarities */
2381 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2382 /* The address *MUST* stupidly be zero. */
2383 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2384 /* PRIV mode is required to bind() to ports < 1024. */
2385 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2386 ((struct sockaddr_in *)addr)->sin_port > 0) {
2387 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2392 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2393 (struct sockaddr *)addr, len) >= 0)
2396 #ifdef MPE /* Switch back to USER mode */
2407 if (ckWARN(WARN_CLOSED))
2408 report_evil_fh(gv, io, PL_op->op_type);
2409 SETERRNO(EBADF,SS$_IVCHAN);
2412 DIE(aTHX_ PL_no_sock_func, "bind");
2423 register IO *io = GvIOn(gv);
2426 if (!io || !IoIFP(io))
2429 addr = SvPV(addrsv, len);
2430 TAINT_PROPER("connect");
2431 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2437 if (ckWARN(WARN_CLOSED))
2438 report_evil_fh(gv, io, PL_op->op_type);
2439 SETERRNO(EBADF,SS$_IVCHAN);
2442 DIE(aTHX_ PL_no_sock_func, "connect");
2452 register IO *io = gv ? GvIOn(gv) : NULL;
2454 if (!gv || !io || !IoIFP(io))
2457 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2463 if (ckWARN(WARN_CLOSED))
2464 report_evil_fh(gv, io, PL_op->op_type);
2465 SETERRNO(EBADF,SS$_IVCHAN);
2468 DIE(aTHX_ PL_no_sock_func, "listen");
2480 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2481 Sock_size_t len = sizeof saddr;
2494 if (!gstio || !IoIFP(gstio))
2498 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2502 do_close(ngv, FALSE);
2503 IoIFP(nstio) = PerlIO_fdopen(fd, "r");
2504 /* FIXME: we dup(fd) here so that refcounting of fd's does not inhibit
2505 fclose of IoOFP's FILE * - and hence leak memory.
2506 Special treatment of _this_ case of IoIFP != IoOFP seems wrong.
2508 IoOFP(nstio) = PerlIO_fdopen(fd2 = PerlLIO_dup(fd), "w");
2509 IoTYPE(nstio) = IoTYPE_SOCKET;
2510 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2511 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2512 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2513 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2516 #if defined(HAS_FCNTL) && defined(F_SETFD)
2517 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2518 fcntl(fd2, F_SETFD, fd2 > PL_maxsysfd); /* ensure close-on-exec */
2522 len = sizeof saddr; /* EPOC somehow truncates info */
2523 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2526 PUSHp((char *)&saddr, len);
2530 if (ckWARN(WARN_CLOSED))
2531 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2532 SETERRNO(EBADF,SS$_IVCHAN);
2538 DIE(aTHX_ PL_no_sock_func, "accept");
2548 register IO *io = GvIOn(gv);
2550 if (!io || !IoIFP(io))
2553 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2557 if (ckWARN(WARN_CLOSED))
2558 report_evil_fh(gv, io, PL_op->op_type);
2559 SETERRNO(EBADF,SS$_IVCHAN);
2562 DIE(aTHX_ PL_no_sock_func, "shutdown");
2569 return pp_ssockopt();
2571 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2579 int optype = PL_op->op_type;
2582 unsigned int optname;
2588 if (optype == OP_GSOCKOPT)
2589 sv = sv_2mortal(NEWSV(22, 257));
2592 optname = (unsigned int) POPi;
2593 lvl = (unsigned int) POPi;
2597 if (!io || !IoIFP(io))
2600 fd = PerlIO_fileno(IoIFP(io));
2604 (void)SvPOK_only(sv);
2608 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2623 aint = (int)SvIV(sv);
2627 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2636 if (ckWARN(WARN_CLOSED))
2637 report_evil_fh(gv, io, optype);
2638 SETERRNO(EBADF,SS$_IVCHAN);
2643 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2650 return pp_getpeername();
2652 DIE(aTHX_ PL_no_sock_func, "getsockname");
2660 int optype = PL_op->op_type;
2664 register IO *io = GvIOn(gv);
2667 if (!io || !IoIFP(io))
2670 sv = sv_2mortal(NEWSV(22, 257));
2671 (void)SvPOK_only(sv);
2675 fd = PerlIO_fileno(IoIFP(io));
2677 case OP_GETSOCKNAME:
2678 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2681 case OP_GETPEERNAME:
2682 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2684 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2686 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";
2687 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2688 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2689 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2690 sizeof(u_short) + sizeof(struct in_addr))) {
2697 #ifdef BOGUS_GETNAME_RETURN
2698 /* Interactive Unix, getpeername() and getsockname()
2699 does not return valid namelen */
2700 if (len == BOGUS_GETNAME_RETURN)
2701 len = sizeof(struct sockaddr);
2709 if (ckWARN(WARN_CLOSED))
2710 report_evil_fh(gv, io, optype);
2711 SETERRNO(EBADF,SS$_IVCHAN);
2716 DIE(aTHX_ PL_no_sock_func, "getpeername");
2735 if (PL_op->op_flags & OPf_REF) {
2737 if (PL_op->op_type == OP_LSTAT) {
2738 if (gv != PL_defgv) {
2739 if (ckWARN(WARN_IO))
2740 Perl_warner(aTHX_ packWARN(WARN_IO),
2741 "lstat() on filehandle %s", GvENAME(gv));
2742 } else if (PL_laststype != OP_LSTAT)
2743 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2747 if (gv != PL_defgv) {
2748 PL_laststype = OP_STAT;
2750 sv_setpv(PL_statname, "");
2751 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2752 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2754 if (PL_laststatval < 0) {
2755 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2756 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2762 if (SvTYPE(sv) == SVt_PVGV) {
2766 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2768 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2769 Perl_warner(aTHX_ packWARN(WARN_IO),
2770 "lstat() on filehandle %s", GvENAME(gv));
2773 sv_setpv(PL_statname, SvPV(sv,n_a));
2776 PL_laststype = PL_op->op_type;
2777 if (PL_op->op_type == OP_LSTAT)
2778 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2781 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2782 if (PL_laststatval < 0) {
2783 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2784 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2790 if (gimme != G_ARRAY) {
2791 if (gimme != G_VOID)
2792 XPUSHs(boolSV(max));
2798 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2799 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2800 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2801 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2802 #if Uid_t_size > IVSIZE
2803 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2805 # if Uid_t_sign <= 0
2806 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2808 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2811 #if Gid_t_size > IVSIZE
2812 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2814 # if Gid_t_sign <= 0
2815 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2817 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2820 #ifdef USE_STAT_RDEV
2821 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2823 PUSHs(sv_2mortal(newSVpvn("", 0)));
2825 #if Off_t_size > IVSIZE
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_size)));
2828 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2831 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2832 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2833 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2835 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2836 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2837 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2839 #ifdef USE_STAT_BLOCKS
2840 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2841 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2843 PUSHs(sv_2mortal(newSVpvn("", 0)));
2844 PUSHs(sv_2mortal(newSVpvn("", 0)));
2854 #if defined(HAS_ACCESS) && defined(R_OK)
2856 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2857 result = access(TOPpx, R_OK);
2872 if (cando(S_IRUSR, 0, &PL_statcache))
2881 #if defined(HAS_ACCESS) && defined(W_OK)
2883 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2884 result = access(TOPpx, W_OK);
2899 if (cando(S_IWUSR, 0, &PL_statcache))
2908 #if defined(HAS_ACCESS) && defined(X_OK)
2910 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2911 result = access(TOPpx, X_OK);
2926 if (cando(S_IXUSR, 0, &PL_statcache))
2935 #ifdef PERL_EFF_ACCESS_R_OK
2937 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2938 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2953 if (cando(S_IRUSR, 1, &PL_statcache))
2962 #ifdef PERL_EFF_ACCESS_W_OK
2964 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2965 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2980 if (cando(S_IWUSR, 1, &PL_statcache))
2989 #ifdef PERL_EFF_ACCESS_X_OK
2991 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2992 result = PERL_EFF_ACCESS_X_OK(TOPpx);
3007 if (cando(S_IXUSR, 1, &PL_statcache))
3014 I32 result = my_stat();
3023 return pp_ftrowned();
3028 I32 result = my_stat();
3032 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3040 I32 result = my_stat();
3044 if (PL_statcache.st_size == 0)
3051 I32 result = my_stat();
3055 #if Off_t_size > IVSIZE
3056 PUSHn(PL_statcache.st_size);
3058 PUSHi(PL_statcache.st_size);
3065 I32 result = my_stat();
3069 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3075 I32 result = my_stat();
3079 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3085 I32 result = my_stat();
3089 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3095 I32 result = my_stat();
3099 if (S_ISSOCK(PL_statcache.st_mode))
3106 I32 result = my_stat();
3110 if (S_ISCHR(PL_statcache.st_mode))
3117 I32 result = my_stat();
3121 if (S_ISBLK(PL_statcache.st_mode))
3128 I32 result = my_stat();
3132 if (S_ISREG(PL_statcache.st_mode))
3139 I32 result = my_stat();
3143 if (S_ISDIR(PL_statcache.st_mode))
3150 I32 result = my_stat();
3154 if (S_ISFIFO(PL_statcache.st_mode))
3161 I32 result = my_lstat();
3165 if (S_ISLNK(PL_statcache.st_mode))
3174 I32 result = my_stat();
3178 if (PL_statcache.st_mode & S_ISUID)
3188 I32 result = my_stat();
3192 if (PL_statcache.st_mode & S_ISGID)
3202 I32 result = my_stat();
3206 if (PL_statcache.st_mode & S_ISVTX)
3217 char *tmps = Nullch;
3220 if (PL_op->op_flags & OPf_REF)
3222 else if (isGV(TOPs))
3224 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3225 gv = (GV*)SvRV(POPs);
3227 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3229 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3230 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3231 else if (tmps && isDIGIT(*tmps))
3235 if (PerlLIO_isatty(fd))
3240 #if defined(atarist) /* this will work with atariST. Configure will
3241 make guesses for other systems. */
3242 # define FILE_base(f) ((f)->_base)
3243 # define FILE_ptr(f) ((f)->_ptr)
3244 # define FILE_cnt(f) ((f)->_cnt)
3245 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3255 register STDCHAR *s;
3262 if (PL_op->op_flags & OPf_REF)
3264 else if (isGV(TOPs))
3266 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3267 gv = (GV*)SvRV(POPs);
3273 if (gv == PL_defgv) {
3275 io = GvIO(PL_statgv);
3278 goto really_filename;
3283 PL_laststatval = -1;
3284 sv_setpv(PL_statname, "");
3285 io = GvIO(PL_statgv);
3287 if (io && IoIFP(io)) {
3288 if (! PerlIO_has_base(IoIFP(io)))
3289 DIE(aTHX_ "-T and -B not implemented on filehandles");
3290 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3291 if (PL_laststatval < 0)
3293 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3294 if (PL_op->op_type == OP_FTTEXT)
3299 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3300 i = PerlIO_getc(IoIFP(io));
3302 (void)PerlIO_ungetc(IoIFP(io),i);
3304 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3306 len = PerlIO_get_bufsiz(IoIFP(io));
3307 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3308 /* sfio can have large buffers - limit to 512 */
3313 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3315 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3317 SETERRNO(EBADF,RMS$_IFI);
3325 PL_laststatval = -1;
3326 PL_laststype = OP_STAT;
3327 sv_setpv(PL_statname, SvPV(sv, n_a));
3328 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3329 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3330 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3333 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3334 if (PL_laststatval < 0) {
3335 (void)PerlIO_close(fp);
3338 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3339 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3340 (void)PerlIO_close(fp);
3342 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3343 RETPUSHNO; /* special case NFS directories */
3344 RETPUSHYES; /* null file is anything */
3349 /* now scan s to look for textiness */
3350 /* XXX ASCII dependent code */
3352 #if defined(DOSISH) || defined(USEMYBINMODE)
3353 /* ignore trailing ^Z on short files */
3354 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3358 for (i = 0; i < len; i++, s++) {
3359 if (!*s) { /* null never allowed in text */
3364 else if (!(isPRINT(*s) || isSPACE(*s)))
3367 else if (*s & 128) {
3369 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3372 /* utf8 characters don't count as odd */
3373 if (UTF8_IS_START(*s)) {
3374 int ulen = UTF8SKIP(s);
3375 if (ulen < len - i) {
3377 for (j = 1; j < ulen; j++) {
3378 if (!UTF8_IS_CONTINUATION(s[j]))
3381 --ulen; /* loop does extra increment */
3391 *s != '\n' && *s != '\r' && *s != '\b' &&
3392 *s != '\t' && *s != '\f' && *s != 27)
3397 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3422 if( !tmps || !*tmps ) {
3423 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3424 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3426 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3431 deprecate("chdir('') or chdir(undef) as chdir()");
3432 tmps = SvPV(*svp, n_a);
3436 TAINT_PROPER("chdir");
3441 TAINT_PROPER("chdir");
3442 PUSHi( PerlDir_chdir(tmps) >= 0 );
3444 /* Clear the DEFAULT element of ENV so we'll get the new value
3446 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3454 dSP; dMARK; dTARGET;
3455 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3461 DIE(aTHX_ PL_no_func, "chown");
3471 TAINT_PROPER("chroot");
3472 PUSHi( chroot(tmps) >= 0 );
3475 DIE(aTHX_ PL_no_func, "chroot");
3481 dSP; dMARK; dTARGET;
3483 value = (I32)apply(PL_op->op_type, MARK, SP);
3491 dSP; dMARK; dTARGET;
3493 value = (I32)apply(PL_op->op_type, MARK, SP);
3501 dSP; dMARK; dTARGET;
3503 value = (I32)apply(PL_op->op_type, MARK, SP);
3515 char *tmps2 = POPpx;
3516 char *tmps = SvPV(TOPs, n_a);
3517 TAINT_PROPER("rename");
3519 anum = PerlLIO_rename(tmps, tmps2);
3521 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3522 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3525 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3526 (void)UNLINK(tmps2);
3527 if (!(anum = link(tmps, tmps2)))
3528 anum = UNLINK(tmps);
3541 char *tmps2 = POPpx;
3542 char *tmps = SvPV(TOPs, n_a);
3543 TAINT_PROPER("link");
3544 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3547 DIE(aTHX_ PL_no_func, "link");
3556 char *tmps2 = POPpx;
3557 char *tmps = SvPV(TOPs, n_a);
3558 TAINT_PROPER("symlink");
3559 SETi( symlink(tmps, tmps2) >= 0 );
3562 DIE(aTHX_ PL_no_func, "symlink");
3572 char buf[MAXPATHLEN];
3576 #ifndef INCOMPLETE_TAINTS
3580 len = readlink(tmps, buf, sizeof(buf) - 1);
3588 RETSETUNDEF; /* just pretend it's a normal file */
3592 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3594 S_dooneliner(pTHX_ char *cmd, char *filename)
3596 char *save_filename = filename;
3602 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3603 strcpy(cmdline, cmd);
3604 strcat(cmdline, " ");
3605 for (s = cmdline + strlen(cmdline); *filename; ) {
3610 myfp = PerlProc_popen(cmdline, "r");
3614 SV *tmpsv = sv_newmortal();
3615 /* Need to save/restore 'PL_rs' ?? */
3616 s = sv_gets(tmpsv, myfp, 0);
3617 (void)PerlProc_pclose(myfp);
3621 #ifdef HAS_SYS_ERRLIST
3626 /* you don't see this */
3628 #ifdef HAS_SYS_ERRLIST
3636 if (instr(s, errmsg)) {
3643 #define EACCES EPERM
3645 if (instr(s, "cannot make"))
3646 SETERRNO(EEXIST,RMS$_FEX);
3647 else if (instr(s, "existing file"))
3648 SETERRNO(EEXIST,RMS$_FEX);
3649 else if (instr(s, "ile exists"))
3650 SETERRNO(EEXIST,RMS$_FEX);
3651 else if (instr(s, "non-exist"))
3652 SETERRNO(ENOENT,RMS$_FNF);
3653 else if (instr(s, "does not exist"))
3654 SETERRNO(ENOENT,RMS$_FNF);
3655 else if (instr(s, "not empty"))
3656 SETERRNO(EBUSY,SS$_DEVOFFLINE);
3657 else if (instr(s, "cannot access"))
3658 SETERRNO(EACCES,RMS$_PRV);
3660 SETERRNO(EPERM,RMS$_PRV);
3663 else { /* some mkdirs return no failure indication */
3664 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3665 if (PL_op->op_type == OP_RMDIR)
3670 SETERRNO(EACCES,RMS$_PRV); /* a guess */
3695 tmps = SvPV(TOPs, len);
3696 /* Different operating and file systems take differently to
3697 * trailing slashes. According to POSIX 1003.1 1996 Edition
3698 * any number of trailing slashes should be allowed.
3699 * Thusly we snip them away so that even non-conforming
3700 * systems are happy. */
3701 /* We should probably do this "filtering" for all
3702 * the functions that expect (potentially) directory names:
3703 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3704 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3705 if (len > 1 && tmps[len-1] == '/') {
3706 while (tmps[len] == '/' && len > 1)
3708 tmps = savepvn(tmps, len);
3712 TAINT_PROPER("mkdir");
3714 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3716 SETi( dooneliner("mkdir", tmps) );
3717 oldumask = PerlLIO_umask(0);
3718 PerlLIO_umask(oldumask);
3719 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3733 TAINT_PROPER("rmdir");
3735 XPUSHi( PerlDir_rmdir(tmps) >= 0 );
3737 XPUSHi( dooneliner("rmdir", tmps) );
3742 /* Directory calls. */
3746 #if defined(Direntry_t) && defined(HAS_READDIR)
3749 char *dirname = POPpx;
3751 register IO *io = GvIOn(gv);
3757 PerlDir_close(IoDIRP(io));
3758 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3764 SETERRNO(EBADF,RMS$_DIR);
3767 DIE(aTHX_ PL_no_dir_func, "opendir");
3773 #if defined(Direntry_t) && defined(HAS_READDIR)
3775 #if !defined(I_DIRENT) && !defined(VMS)
3776 Direntry_t *readdir (DIR *);
3778 register Direntry_t *dp;
3780 register IO *io = GvIOn(gv);
3783 if (!io || !IoDIRP(io))
3786 if (GIMME == G_ARRAY) {
3788 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3790 sv = newSVpvn(dp->d_name, dp->d_namlen);
3792 sv = newSVpv(dp->d_name, 0);
3794 #ifndef INCOMPLETE_TAINTS
3795 if (!(IoFLAGS(io) & IOf_UNTAINT))
3798 XPUSHs(sv_2mortal(sv));
3802 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3805 sv = newSVpvn(dp->d_name, dp->d_namlen);
3807 sv = newSVpv(dp->d_name, 0);
3809 #ifndef INCOMPLETE_TAINTS
3810 if (!(IoFLAGS(io) & IOf_UNTAINT))
3813 XPUSHs(sv_2mortal(sv));
3819 SETERRNO(EBADF,RMS$_ISI);
3820 if (GIMME == G_ARRAY)
3825 DIE(aTHX_ PL_no_dir_func, "readdir");
3831 #if defined(HAS_TELLDIR) || defined(telldir)
3833 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3834 /* XXX netbsd still seemed to.
3835 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3836 --JHI 1999-Feb-02 */
3837 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3838 long telldir (DIR *);
3841 register IO *io = GvIOn(gv);
3843 if (!io || !IoDIRP(io))
3846 PUSHi( PerlDir_tell(IoDIRP(io)) );
3850 SETERRNO(EBADF,RMS$_ISI);
3853 DIE(aTHX_ PL_no_dir_func, "telldir");
3859 #if defined(HAS_SEEKDIR) || defined(seekdir)
3863 register IO *io = GvIOn(gv);
3865 if (!io || !IoDIRP(io))
3868 (void)PerlDir_seek(IoDIRP(io), along);
3873 SETERRNO(EBADF,RMS$_ISI);
3876 DIE(aTHX_ PL_no_dir_func, "seekdir");
3882 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3885 register IO *io = GvIOn(gv);
3887 if (!io || !IoDIRP(io))
3890 (void)PerlDir_rewind(IoDIRP(io));
3894 SETERRNO(EBADF,RMS$_ISI);
3897 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3903 #if defined(Direntry_t) && defined(HAS_READDIR)
3906 register IO *io = GvIOn(gv);
3908 if (!io || !IoDIRP(io))
3911 #ifdef VOID_CLOSEDIR
3912 PerlDir_close(IoDIRP(io));
3914 if (PerlDir_close(IoDIRP(io)) < 0) {
3915 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3924 SETERRNO(EBADF,RMS$_IFI);
3927 DIE(aTHX_ PL_no_dir_func, "closedir");
3931 /* Process control. */
3941 PERL_FLUSHALL_FOR_CHILD;
3942 childpid = PerlProc_fork();
3947 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3948 SvREADONLY_off(GvSV(tmpgv));
3949 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3950 SvREADONLY_on(GvSV(tmpgv));
3952 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3957 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3962 PERL_FLUSHALL_FOR_CHILD;
3963 childpid = PerlProc_fork();
3969 DIE(aTHX_ PL_no_func, "fork");
3976 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3981 #ifdef PERL_OLD_SIGNALS
3982 childpid = wait4pid(-1, &argflags, 0);
3984 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 && errno == EINTR) {
3988 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3989 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3990 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3992 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
3997 DIE(aTHX_ PL_no_func, "wait");
4003 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4011 #ifdef PERL_OLD_SIGNALS
4012 childpid = wait4pid(childpid, &argflags, optype);
4014 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 && errno == EINTR) {
4018 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4019 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4020 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4022 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4027 DIE(aTHX_ PL_no_func, "waitpid");
4033 dSP; dMARK; dORIGMARK; dTARGET;
4042 while (++MARK <= SP) {
4043 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4048 /* XXX Remove warning at end of deprecation cycle --RD 2002-02 */
4049 if (SP - MARK == 1) {
4050 TAINT_PROPER("system");
4052 else if (ckWARN2(WARN_TAINT, WARN_DEPRECATED)) {
4053 Perl_warner(aTHX_ packWARN2(WARN_TAINT, WARN_DEPRECATED),
4054 "Use of tainted arguments in %s is deprecated", "system");
4057 PERL_FLUSHALL_FOR_CHILD;
4058 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4062 Sigsave_t ihand,qhand; /* place to save signals during system() */
4064 if (PerlProc_pipe(pp) >= 0)
4066 while ((childpid = PerlProc_fork()) == -1) {
4067 if (errno != EAGAIN) {
4072 PerlLIO_close(pp[0]);
4073 PerlLIO_close(pp[1]);
4081 PerlLIO_close(pp[1]);
4083 rsignal_save(SIGINT, SIG_IGN, &ihand);
4084 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4087 result = wait4pid(childpid, &status, 0);
4088 } while (result == -1 && errno == EINTR);
4090 (void)rsignal_restore(SIGINT, &ihand);
4091 (void)rsignal_restore(SIGQUIT, &qhand);
4093 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4094 do_execfree(); /* free any memory child malloced on fork */
4100 while (n < sizeof(int)) {
4101 n1 = PerlLIO_read(pp[0],
4102 (void*)(((char*)&errkid)+n),
4108 PerlLIO_close(pp[0]);
4109 if (n) { /* Error */
4110 if (n != sizeof(int))
4111 DIE(aTHX_ "panic: kid popen errno read");
4112 errno = errkid; /* Propagate errno from kid */
4113 STATUS_CURRENT = -1;
4116 PUSHi(STATUS_CURRENT);
4120 PerlLIO_close(pp[0]);
4121 #if defined(HAS_FCNTL) && defined(F_SETFD)
4122 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4126 if (PL_op->op_flags & OPf_STACKED) {
4127 SV *really = *++MARK;
4128 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4130 else if (SP - MARK != 1)
4131 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4133 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4136 #else /* ! FORK or VMS or OS/2 */
4139 if (PL_op->op_flags & OPf_STACKED) {
4140 SV *really = *++MARK;
4141 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4143 else if (SP - MARK != 1)
4144 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4146 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4148 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4150 STATUS_NATIVE_SET(value);
4153 PUSHi(result ? value : STATUS_CURRENT);
4154 #endif /* !FORK or VMS */
4160 dSP; dMARK; dORIGMARK; dTARGET;
4166 while (++MARK <= SP) {
4167 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4172 /* XXX Remove warning at end of deprecation cycle --RD 2002-02 */
4173 if (SP - MARK == 1) {
4174 TAINT_PROPER("exec");
4176 else if (ckWARN2(WARN_TAINT, WARN_DEPRECATED)) {
4177 Perl_warner(aTHX_ packWARN2(WARN_TAINT, WARN_DEPRECATED),
4178 "Use of tainted arguments in %s is deprecated", "exec");
4181 PERL_FLUSHALL_FOR_CHILD;
4182 if (PL_op->op_flags & OPf_STACKED) {
4183 SV *really = *++MARK;
4184 value = (I32)do_aexec(really, MARK, SP);
4186 else if (SP - MARK != 1)
4188 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4192 (void ) do_aspawn(Nullsv, MARK, SP);
4196 value = (I32)do_aexec(Nullsv, MARK, SP);
4201 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4204 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4207 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4220 dSP; dMARK; dTARGET;
4222 value = (I32)apply(PL_op->op_type, MARK, SP);
4227 DIE(aTHX_ PL_no_func, "kill");
4235 XPUSHi( getppid() );
4238 DIE(aTHX_ PL_no_func, "getppid");
4254 pgrp = (I32)BSD_GETPGRP(pid);
4256 if (pid != 0 && pid != PerlProc_getpid())
4257 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4263 DIE(aTHX_ PL_no_func, "getpgrp()");
4282 TAINT_PROPER("setpgrp");
4284 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4286 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4287 || (pid != 0 && pid != PerlProc_getpid()))
4289 DIE(aTHX_ "setpgrp can't take arguments");
4291 SETi( setpgrp() >= 0 );
4292 #endif /* USE_BSDPGRP */
4295 DIE(aTHX_ PL_no_func, "setpgrp()");
4301 #ifdef HAS_GETPRIORITY
4305 SETi( getpriority(which, who) );
4308 DIE(aTHX_ PL_no_func, "getpriority()");
4314 #ifdef HAS_SETPRIORITY
4319 TAINT_PROPER("setpriority");
4320 SETi( setpriority(which, who, niceval) >= 0 );
4323 DIE(aTHX_ PL_no_func, "setpriority()");
4333 XPUSHn( time(Null(Time_t*)) );
4335 XPUSHi( time(Null(Time_t*)) );
4340 /* XXX The POSIX name is CLK_TCK; it is to be preferred
4341 to HZ. Probably. For now, assume that if the system
4342 defines HZ, it does so correctly. (Will this break
4344 Probably we ought to use _sysconf(_SC_CLK_TCK), if
4345 it's supported. --AD 9/96.
4366 (void)PerlProc_times(&PL_timesbuf);
4368 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4369 /* struct tms, though same data */
4373 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/HZ)));
4374 if (GIMME == G_ARRAY) {
4375 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/HZ)));
4376 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/HZ)));
4377 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/HZ)));
4381 DIE(aTHX_ "times not implemented");
4382 #endif /* HAS_TIMES */
4395 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4396 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4397 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4403 when = (Time_t)SvNVx(POPs);
4405 when = (Time_t)SvIVx(POPs);
4408 if (PL_op->op_type == OP_LOCALTIME)
4409 tmbuf = localtime(&when);
4411 tmbuf = gmtime(&when);
4413 if (GIMME != G_ARRAY) {
4419 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4420 dayname[tmbuf->tm_wday],
4421 monname[tmbuf->tm_mon],
4426 tmbuf->tm_year + 1900);
4427 PUSHs(sv_2mortal(tsv));
4432 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4433 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4434 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4435 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4436 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4437 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4438 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4439 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4440 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4451 anum = alarm((unsigned int)anum);
4458 DIE(aTHX_ PL_no_func, "alarm");
4469 (void)time(&lasttime);
4474 PerlProc_sleep((unsigned int)duration);
4477 XPUSHi(when - lasttime);
4481 /* Shared memory. */
4495 return pp_shmwrite();
4500 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4501 dSP; dMARK; dTARGET;
4502 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4511 /* Message passing. */
4525 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4526 dSP; dMARK; dTARGET;
4527 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4538 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4539 dSP; dMARK; dTARGET;
4540 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4553 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4554 dSP; dMARK; dTARGET;
4555 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4562 DIE(aTHX_ "System V IPC is not implemented on this machine");
4568 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4569 dSP; dMARK; dTARGET;
4570 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4578 PUSHp(zero_but_true, ZBTLEN);
4588 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4589 dSP; dMARK; dTARGET;
4590 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4599 /* Get system info. */
4603 #ifdef HAS_GETHOSTBYNAME
4604 return pp_ghostent();
4606 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4612 #ifdef HAS_GETHOSTBYADDR
4613 return pp_ghostent();
4615 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4621 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4623 I32 which = PL_op->op_type;
4624 register char **elem;
4626 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4627 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4628 struct hostent *gethostbyname(Netdb_name_t);
4629 struct hostent *gethostent(void);
4631 struct hostent *hent;
4636 if (which == OP_GHBYNAME) {
4637 #ifdef HAS_GETHOSTBYNAME
4638 char* name = POPpbytex;
4639 hent = PerlSock_gethostbyname(name);
4641 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4644 else if (which == OP_GHBYADDR) {
4645 #ifdef HAS_GETHOSTBYADDR
4646 int addrtype = POPi;
4649 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4651 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4653 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4657 #ifdef HAS_GETHOSTENT
4658 hent = PerlSock_gethostent();
4660 DIE(aTHX_ PL_no_sock_func, "gethostent");
4663 #ifdef HOST_NOT_FOUND
4665 #ifdef USE_REENTRANT_API
4666 # ifdef USE_GETHOSTENT_ERRNO
4667 h_errno = PL_reentrant_buffer->_gethostent_errno;
4670 STATUS_NATIVE_SET(h_errno);
4674 if (GIMME != G_ARRAY) {
4675 PUSHs(sv = sv_newmortal());
4677 if (which == OP_GHBYNAME) {
4679 sv_setpvn(sv, hent->h_addr, hent->h_length);
4682 sv_setpv(sv, (char*)hent->h_name);
4688 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4689 sv_setpv(sv, (char*)hent->h_name);
4690 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4691 for (elem = hent->h_aliases; elem && *elem; elem++) {
4692 sv_catpv(sv, *elem);
4694 sv_catpvn(sv, " ", 1);
4696 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4697 sv_setiv(sv, (IV)hent->h_addrtype);
4698 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4699 len = hent->h_length;
4700 sv_setiv(sv, (IV)len);
4702 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4703 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4704 sv_setpvn(sv, *elem, len);
4707 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4709 sv_setpvn(sv, hent->h_addr, len);
4714 DIE(aTHX_ PL_no_sock_func, "gethostent");
4720 #ifdef HAS_GETNETBYNAME
4721 return pp_gnetent();
4723 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4729 #ifdef HAS_GETNETBYADDR
4730 return pp_gnetent();
4732 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4738 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4740 I32 which = PL_op->op_type;
4741 register char **elem;
4743 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4744 struct netent *getnetbyaddr(Netdb_net_t, int);
4745 struct netent *getnetbyname(Netdb_name_t);
4746 struct netent *getnetent(void);
4748 struct netent *nent;
4751 if (which == OP_GNBYNAME){
4752 #ifdef HAS_GETNETBYNAME
4753 char *name = POPpbytex;
4754 nent = PerlSock_getnetbyname(name);
4756 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4759 else if (which == OP_GNBYADDR) {
4760 #ifdef HAS_GETNETBYADDR
4761 int addrtype = POPi;
4762 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4763 nent = PerlSock_getnetbyaddr(addr, addrtype);
4765 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4769 #ifdef HAS_GETNETENT
4770 nent = PerlSock_getnetent();
4772 DIE(aTHX_ PL_no_sock_func, "getnetent");
4775 #ifdef HOST_NOT_FOUND
4777 #ifdef USE_REENTRANT_API
4778 # ifdef USE_GETNETENT_ERRNO
4779 h_errno = PL_reentrant_buffer->_getnetent_errno;
4782 STATUS_NATIVE_SET(h_errno);
4787 if (GIMME != G_ARRAY) {
4788 PUSHs(sv = sv_newmortal());
4790 if (which == OP_GNBYNAME)
4791 sv_setiv(sv, (IV)nent->n_net);
4793 sv_setpv(sv, nent->n_name);
4799 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4800 sv_setpv(sv, nent->n_name);
4801 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4802 for (elem = nent->n_aliases; elem && *elem; elem++) {
4803 sv_catpv(sv, *elem);
4805 sv_catpvn(sv, " ", 1);
4807 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4808 sv_setiv(sv, (IV)nent->n_addrtype);
4809 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4810 sv_setiv(sv, (IV)nent->n_net);
4815 DIE(aTHX_ PL_no_sock_func, "getnetent");
4821 #ifdef HAS_GETPROTOBYNAME
4822 return pp_gprotoent();
4824 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4830 #ifdef HAS_GETPROTOBYNUMBER
4831 return pp_gprotoent();
4833 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4839 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4841 I32 which = PL_op->op_type;
4842 register char **elem;
4844 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4845 struct protoent *getprotobyname(Netdb_name_t);
4846 struct protoent *getprotobynumber(int);
4847 struct protoent *getprotoent(void);
4849 struct protoent *pent;
4852 if (which == OP_GPBYNAME) {
4853 #ifdef HAS_GETPROTOBYNAME
4854 char* name = POPpbytex;
4855 pent = PerlSock_getprotobyname(name);
4857 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4860 else if (which == OP_GPBYNUMBER) {
4861 #ifdef HAS_GETPROTOBYNUMBER
4863 pent = PerlSock_getprotobynumber(number);
4865 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4869 #ifdef HAS_GETPROTOENT
4870 pent = PerlSock_getprotoent();
4872 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4876 if (GIMME != G_ARRAY) {
4877 PUSHs(sv = sv_newmortal());
4879 if (which == OP_GPBYNAME)
4880 sv_setiv(sv, (IV)pent->p_proto);
4882 sv_setpv(sv, pent->p_name);
4888 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4889 sv_setpv(sv, pent->p_name);
4890 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4891 for (elem = pent->p_aliases; elem && *elem; elem++) {
4892 sv_catpv(sv, *elem);
4894 sv_catpvn(sv, " ", 1);
4896 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4897 sv_setiv(sv, (IV)pent->p_proto);
4902 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4908 #ifdef HAS_GETSERVBYNAME
4909 return pp_gservent();
4911 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4917 #ifdef HAS_GETSERVBYPORT
4918 return pp_gservent();
4920 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4926 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4928 I32 which = PL_op->op_type;
4929 register char **elem;
4931 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4932 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4933 struct servent *getservbyport(int, Netdb_name_t);
4934 struct servent *getservent(void);
4936 struct servent *sent;
4939 if (which == OP_GSBYNAME) {
4940 #ifdef HAS_GETSERVBYNAME
4941 char *proto = POPpbytex;
4942 char *name = POPpbytex;
4944 if (proto && !*proto)
4947 sent = PerlSock_getservbyname(name, proto);
4949 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4952 else if (which == OP_GSBYPORT) {
4953 #ifdef HAS_GETSERVBYPORT
4954 char *proto = POPpbytex;
4955 unsigned short port = POPu;
4958 port = PerlSock_htons(port);
4960 sent = PerlSock_getservbyport(port, proto);
4962 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4966 #ifdef HAS_GETSERVENT
4967 sent = PerlSock_getservent();
4969 DIE(aTHX_ PL_no_sock_func, "getservent");
4973 if (GIMME != G_ARRAY) {
4974 PUSHs(sv = sv_newmortal());
4976 if (which == OP_GSBYNAME) {
4978 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4980 sv_setiv(sv, (IV)(sent->s_port));
4984 sv_setpv(sv, sent->s_name);
4990 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4991 sv_setpv(sv, sent->s_name);
4992 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4993 for (elem = sent->s_aliases; elem && *elem; elem++) {
4994 sv_catpv(sv, *elem);
4996 sv_catpvn(sv, " ", 1);
4998 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5000 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5002 sv_setiv(sv, (IV)(sent->s_port));
5004 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5005 sv_setpv(sv, sent->s_proto);
5010 DIE(aTHX_ PL_no_sock_func, "getservent");
5016 #ifdef HAS_SETHOSTENT
5018 PerlSock_sethostent(TOPi);
5021 DIE(aTHX_ PL_no_sock_func, "sethostent");
5027 #ifdef HAS_SETNETENT
5029 PerlSock_setnetent(TOPi);
5032 DIE(aTHX_ PL_no_sock_func, "setnetent");
5038 #ifdef HAS_SETPROTOENT
5040 PerlSock_setprotoent(TOPi);
5043 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5049 #ifdef HAS_SETSERVENT
5051 PerlSock_setservent(TOPi);
5054 DIE(aTHX_ PL_no_sock_func, "setservent");
5060 #ifdef HAS_ENDHOSTENT
5062 PerlSock_endhostent();
5066 DIE(aTHX_ PL_no_sock_func, "endhostent");
5072 #ifdef HAS_ENDNETENT
5074 PerlSock_endnetent();
5078 DIE(aTHX_ PL_no_sock_func, "endnetent");
5084 #ifdef HAS_ENDPROTOENT
5086 PerlSock_endprotoent();
5090 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5096 #ifdef HAS_ENDSERVENT
5098 PerlSock_endservent();
5102 DIE(aTHX_ PL_no_sock_func, "endservent");
5111 DIE(aTHX_ PL_no_func, "getpwnam");
5120 DIE(aTHX_ PL_no_func, "getpwuid");
5128 I32 which = PL_op->op_type;
5131 struct passwd *pwent = NULL;
5133 * We currently support only the SysV getsp* shadow password interface.
5134 * The interface is declared in <shadow.h> and often one needs to link
5135 * with -lsecurity or some such.
5136 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5139 * AIX getpwnam() is clever enough to return the encrypted password
5140 * only if the caller (euid?) is root.
5142 * There are at least two other shadow password APIs. Many platforms
5143 * seem to contain more than one interface for accessing the shadow
5144 * password databases, possibly for compatibility reasons.
5145 * The getsp*() is by far he simplest one, the other two interfaces
5146 * are much more complicated, but also very similar to each other.
5151 * struct pr_passwd *getprpw*();
5152 * The password is in
5153 * char getprpw*(...).ufld.fd_encrypt[]
5154 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5159 * struct es_passwd *getespw*();
5160 * The password is in
5161 * char *(getespw*(...).ufld.fd_encrypt)
5162 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5164 * Mention I_PROT here so that Configure probes for it.
5166 * In HP-UX for getprpw*() the manual page claims that one should include
5167 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5168 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5169 * and pp_sys.c already includes <shadow.h> if there is such.
5171 * Note that <sys/security.h> is already probed for, but currently
5172 * it is only included in special cases.
5174 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5175 * be preferred interface, even though also the getprpw*() interface
5176 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5177 * One also needs to call set_auth_parameters() in main() before
5178 * doing anything else, whether one is using getespw*() or getprpw*().
5180 * Note that accessing the shadow databases can be magnitudes
5181 * slower than accessing the standard databases.
5189 char* name = POPpbytex;
5190 pwent = getpwnam(name);
5196 pwent = getpwuid(uid);
5200 # ifdef HAS_GETPWENT
5203 DIE(aTHX_ PL_no_func, "getpwent");
5209 if (GIMME != G_ARRAY) {
5210 PUSHs(sv = sv_newmortal());
5212 if (which == OP_GPWNAM)
5213 # if Uid_t_sign <= 0
5214 sv_setiv(sv, (IV)pwent->pw_uid);
5216 sv_setuv(sv, (UV)pwent->pw_uid);
5219 sv_setpv(sv, pwent->pw_name);
5225 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5226 sv_setpv(sv, pwent->pw_name);
5228 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5230 /* If we have getspnam(), we try to dig up the shadow
5231 * password. If we are underprivileged, the shadow
5232 * interface will set the errno to EACCES or similar,
5233 * and return a null pointer. If this happens, we will
5234 * use the dummy password (usually "*" or "x") from the
5235 * standard password database.
5237 * In theory we could skip the shadow call completely
5238 * if euid != 0 but in practice we cannot know which
5239 * security measures are guarding the shadow databases
5240 * on a random platform.
5242 * Resist the urge to use additional shadow interfaces.
5243 * Divert the urge to writing an extension instead.
5246 # ifdef HAS_GETSPNAM
5248 struct spwd *spwent;
5249 int saverrno; /* Save and restore errno so that
5250 * underprivileged attempts seem
5251 * to have never made the unsccessful
5252 * attempt to retrieve the shadow password. */
5255 spwent = getspnam(pwent->pw_name);
5257 if (spwent && spwent->sp_pwdp)
5258 sv_setpv(sv, spwent->sp_pwdp);
5262 if (!SvPOK(sv)) /* Use the standard password, then. */
5263 sv_setpv(sv, pwent->pw_passwd);
5266 # ifndef INCOMPLETE_TAINTS
5267 /* passwd is tainted because user himself can diddle with it.
5268 * admittedly not much and in a very limited way, but nevertheless. */
5272 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5273 # if Uid_t_sign <= 0
5274 sv_setiv(sv, (IV)pwent->pw_uid);
5276 sv_setuv(sv, (UV)pwent->pw_uid);
5279 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5280 # if Uid_t_sign <= 0
5281 sv_setiv(sv, (IV)pwent->pw_gid);
5283 sv_setuv(sv, (UV)pwent->pw_gid);
5285 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5286 * because of the poor interface of the Perl getpw*(),
5287 * not because there's some standard/convention saying so.
5288 * A better interface would have been to return a hash,
5289 * but we are accursed by our history, alas. --jhi. */
5290 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5292 sv_setiv(sv, (IV)pwent->pw_change);
5295 sv_setiv(sv, (IV)pwent->pw_quota);
5298 sv_setpv(sv, pwent->pw_age);
5303 /* pw_class and pw_comment are mutually exclusive--.
5304 * see the above note for pw_change, pw_quota, and pw_age. */
5305 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5307 sv_setpv(sv, pwent->pw_class);
5310 sv_setpv(sv, pwent->pw_comment);
5314 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5316 sv_setpv(sv, pwent->pw_gecos);
5318 # ifndef INCOMPLETE_TAINTS
5319 /* pw_gecos is tainted because user himself can diddle with it. */
5323 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5324 sv_setpv(sv, pwent->pw_dir);
5326 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5327 sv_setpv(sv, pwent->pw_shell);
5328 # ifndef INCOMPLETE_TAINTS
5329 /* pw_shell is tainted because user himself can diddle with it. */
5334 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5335 sv_setiv(sv, (IV)pwent->pw_expire);
5340 DIE(aTHX_ PL_no_func, "getpwent");
5346 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5351 DIE(aTHX_ PL_no_func, "setpwent");
5357 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5362 DIE(aTHX_ PL_no_func, "endpwent");
5371 DIE(aTHX_ PL_no_func, "getgrnam");
5380 DIE(aTHX_ PL_no_func, "getgrgid");
5388 I32 which = PL_op->op_type;
5389 register char **elem;
5391 struct group *grent;
5394 if (which == OP_GGRNAM) {
5395 char* name = POPpbytex;
5396 grent = (struct group *)getgrnam(name);
5398 else if (which == OP_GGRGID) {
5400 grent = (struct group *)getgrgid(gid);
5404 grent = (struct group *)getgrent();
5406 DIE(aTHX_ PL_no_func, "getgrent");
5410 if (GIMME != G_ARRAY) {
5411 PUSHs(sv = sv_newmortal());
5413 if (which == OP_GGRNAM)
5414 sv_setiv(sv, (IV)grent->gr_gid);
5416 sv_setpv(sv, grent->gr_name);
5422 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5423 sv_setpv(sv, grent->gr_name);
5425 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5427 sv_setpv(sv, grent->gr_passwd);
5430 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5431 sv_setiv(sv, (IV)grent->gr_gid);
5433 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5434 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5435 /* In UNICOS/mk (_CRAYMPP) the multithreading
5436 * versions (getgrnam_r, getgrgid_r)
5437 * seem to return an illegal pointer
5438 * as the group members list, gr_mem.
5439 * getgrent() doesn't even have a _r version
5440 * but the gr_mem is poisonous anyway.
5441 * So yes, you cannot get the list of group
5442 * members if building multithreaded in UNICOS/mk. */
5443 for (elem = grent->gr_mem; elem && *elem; elem++) {
5444 sv_catpv(sv, *elem);
5446 sv_catpvn(sv, " ", 1);
5453 DIE(aTHX_ PL_no_func, "getgrent");
5459 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5464 DIE(aTHX_ PL_no_func, "setgrent");
5470 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5475 DIE(aTHX_ PL_no_func, "endgrent");
5485 if (!(tmps = PerlProc_getlogin()))
5487 PUSHp(tmps, strlen(tmps));
5490 DIE(aTHX_ PL_no_func, "getlogin");
5494 /* Miscellaneous. */
5499 dSP; dMARK; dORIGMARK; dTARGET;
5500 register I32 items = SP - MARK;
5501 unsigned long a[20];
5507 while (++MARK <= SP) {
5508 if (SvTAINTED(*MARK)) {
5514 TAINT_PROPER("syscall");
5517 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5518 * or where sizeof(long) != sizeof(char*). But such machines will
5519 * not likely have syscall implemented either, so who cares?
5521 while (++MARK <= SP) {
5522 if (SvNIOK(*MARK) || !i)
5523 a[i++] = SvIV(*MARK);
5524 else if (*MARK == &PL_sv_undef)
5527 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5533 DIE(aTHX_ "Too many args to syscall");
5535 DIE(aTHX_ "Too few args to syscall");
5537 retval = syscall(a[0]);
5540 retval = syscall(a[0],a[1]);
5543 retval = syscall(a[0],a[1],a[2]);
5546 retval = syscall(a[0],a[1],a[2],a[3]);
5549 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5552 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5555 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5558 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5562 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5565 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5568 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5572 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5576 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5580 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5581 a[10],a[11],a[12],a[13]);
5583 #endif /* atarist */
5589 DIE(aTHX_ PL_no_func, "syscall");
5593 #ifdef FCNTL_EMULATE_FLOCK
5595 /* XXX Emulate flock() with fcntl().
5596 What's really needed is a good file locking module.
5600 fcntl_emulate_flock(int fd, int operation)
5604 switch (operation & ~LOCK_NB) {
5606 flock.l_type = F_RDLCK;
5609 flock.l_type = F_WRLCK;
5612 flock.l_type = F_UNLCK;
5618 flock.l_whence = SEEK_SET;
5619 flock.l_start = flock.l_len = (Off_t)0;
5621 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5624 #endif /* FCNTL_EMULATE_FLOCK */
5626 #ifdef LOCKF_EMULATE_FLOCK
5628 /* XXX Emulate flock() with lockf(). This is just to increase
5629 portability of scripts. The calls are not completely
5630 interchangeable. What's really needed is a good file
5634 /* The lockf() constants might have been defined in <unistd.h>.
5635 Unfortunately, <unistd.h> causes troubles on some mixed
5636 (BSD/POSIX) systems, such as SunOS 4.1.3.
5638 Further, the lockf() constants aren't POSIX, so they might not be
5639 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5640 just stick in the SVID values and be done with it. Sigh.
5644 # define F_ULOCK 0 /* Unlock a previously locked region */
5647 # define F_LOCK 1 /* Lock a region for exclusive use */
5650 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5653 # define F_TEST 3 /* Test a region for other processes locks */
5657 lockf_emulate_flock(int fd, int operation)
5663 /* flock locks entire file so for lockf we need to do the same */
5665 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5666 if (pos > 0) /* is seekable and needs to be repositioned */
5667 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5668 pos = -1; /* seek failed, so don't seek back afterwards */
5671 switch (operation) {
5673 /* LOCK_SH - get a shared lock */
5675 /* LOCK_EX - get an exclusive lock */
5677 i = lockf (fd, F_LOCK, 0);
5680 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5681 case LOCK_SH|LOCK_NB:
5682 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5683 case LOCK_EX|LOCK_NB:
5684 i = lockf (fd, F_TLOCK, 0);
5686 if ((errno == EAGAIN) || (errno == EACCES))
5687 errno = EWOULDBLOCK;
5690 /* LOCK_UN - unlock (non-blocking is a no-op) */
5692 case LOCK_UN|LOCK_NB:
5693 i = lockf (fd, F_ULOCK, 0);
5696 /* Default - can't decipher operation */
5703 if (pos > 0) /* need to restore position of the handle */
5704 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5709 #endif /* LOCKF_EMULATE_FLOCK */