3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * But only a short way ahead its floor and the walls on either side were
13 * cloven by a great fissure, out of which the red glare came, now leaping
14 * up, now dying down into darkness; and all the while far below there was
15 * a rumour and a trouble as of great engines throbbing and labouring.
19 #define PERL_IN_PP_SYS_C
23 /* Shadow password support for solaris - pdo@cs.umd.edu
24 * Not just Solaris: at least HP-UX, IRIX, Linux.
25 * The API is from SysV.
27 * There are at least two more shadow interfaces,
28 * see the comments in pp_gpwent().
32 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
33 * and another MAXINT from "perl.h" <- <sys/param.h>. */
41 extern "C" int syscall(unsigned long,...);
46 # include <sys/wait.h>
50 # include <sys/resource.h>
59 # include <sys/select.h>
63 /* XXX Configure test needed.
64 h_errno might not be a simple 'int', especially for multi-threaded
65 applications, see "extern int errno in perl.h". Creating such
66 a test requires taking into account the differences between
67 compiling multithreaded and singlethreaded ($ccflags et al).
68 HOST_NOT_FOUND is typically defined in <netdb.h>.
70 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
79 struct passwd *getpwnam (char *);
80 struct passwd *getpwuid (Uid_t);
85 struct passwd *getpwent (void);
86 #elif defined (VMS) && defined (my_getpwent)
87 struct passwd *Perl_my_getpwent (void);
96 struct group *getgrnam (char *);
97 struct group *getgrgid (Gid_t);
101 struct group *getgrent (void);
107 # if defined(_MSC_VER) || defined(__MINGW32__)
108 # include <sys/utime.h>
115 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
118 # define my_chsize PerlLIO_chsize
123 #else /* no flock() */
125 /* fcntl.h might not have been included, even if it exists, because
126 the current Configure only sets I_FCNTL if it's needed to pick up
127 the *_OK constants. Make sure it has been included before testing
128 the fcntl() locking constants. */
129 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
133 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
134 # define FLOCK fcntl_emulate_flock
135 # define FCNTL_EMULATE_FLOCK
136 # else /* no flock() or fcntl(F_SETLK,...) */
138 # define FLOCK lockf_emulate_flock
139 # define LOCKF_EMULATE_FLOCK
141 # endif /* no flock() or fcntl(F_SETLK,...) */
144 static int FLOCK (int, int);
147 * These are the flock() constants. Since this sytems doesn't have
148 * flock(), the values of the constants are probably not available.
162 # endif /* emulating flock() */
164 #endif /* no flock() */
167 static char zero_but_true[ZBTLEN + 1] = "0 but true";
169 #if defined(I_SYS_ACCESS) && !defined(R_OK)
170 # include <sys/access.h>
173 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
174 # define FD_CLOEXEC 1 /* NeXT needs this */
179 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
180 #undef PERL_EFF_ACCESS_W_OK
181 #undef PERL_EFF_ACCESS_X_OK
183 /* F_OK unused: if stat() cannot find it... */
185 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
186 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
187 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
188 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
189 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
192 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
193 # ifdef I_SYS_SECURITY
194 # include <sys/security.h>
198 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
200 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
203 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
204 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
205 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
209 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
211 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
212 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
213 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
216 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
217 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
218 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
221 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
223 Uid_t ruid = getuid();
224 Uid_t euid = geteuid();
225 Gid_t rgid = getgid();
226 Gid_t egid = getegid();
230 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
231 Perl_croak(aTHX_ "switching effective uid is not implemented");
234 if (setreuid(euid, ruid))
237 if (setresuid(euid, ruid, (Uid_t)-1))
240 Perl_croak(aTHX_ "entering effective uid failed");
243 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
244 Perl_croak(aTHX_ "switching effective gid is not implemented");
247 if (setregid(egid, rgid))
250 if (setresgid(egid, rgid, (Gid_t)-1))
253 Perl_croak(aTHX_ "entering effective gid failed");
256 res = access(path, mode);
259 if (setreuid(ruid, euid))
262 if (setresuid(ruid, euid, (Uid_t)-1))
265 Perl_croak(aTHX_ "leaving effective uid failed");
268 if (setregid(rgid, egid))
271 if (setresgid(rgid, egid, (Gid_t)-1))
274 Perl_croak(aTHX_ "leaving effective gid failed");
279 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
280 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
281 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
284 #if !defined(PERL_EFF_ACCESS_R_OK)
285 /* With it or without it: anyway you get a warning: either that
286 it is unused, or it is declared static and never defined.
289 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
291 Perl_croak(aTHX_ "switching effective uid is not implemented");
307 if (PL_op->op_private & OPpOPEN_IN_RAW)
309 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
311 fp = PerlProc_popen(tmps, mode);
314 if (PL_curcop->cop_io) {
315 type = SvPV_nolen(PL_curcop->cop_io);
318 PerlIO_apply_layers(aTHX_ fp,mode,type);
320 if (gimme == G_VOID) {
322 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
326 else if (gimme == G_SCALAR) {
328 PL_rs = &PL_sv_undef;
329 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
330 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
342 if (sv_gets(sv, fp, 0) == Nullch) {
346 XPUSHs(sv_2mortal(sv));
347 if (SvLEN(sv) - SvCUR(sv) > 20) {
348 SvLEN_set(sv, SvCUR(sv)+1);
349 Renew(SvPVX(sv), SvLEN(sv), char);
354 STATUS_NATIVE_SET(PerlProc_pclose(fp));
355 TAINT; /* "I believe that this is not gratuitous!" */
358 STATUS_NATIVE_SET(-1);
359 if (gimme == G_SCALAR)
369 tryAMAGICunTARGET(iter, -1);
371 /* Note that we only ever get here if File::Glob fails to load
372 * without at the same time croaking, for some reason, or if
373 * perl was built with PERL_EXTERNAL_GLOB */
380 * The external globbing program may use things we can't control,
381 * so for security reasons we must assume the worst.
384 taint_proper(PL_no_security, "glob");
388 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
389 PL_last_in_gv = (GV*)*PL_stack_sp--;
391 SAVESPTR(PL_rs); /* This is not permanent, either. */
392 PL_rs = sv_2mortal(newSVpvn("\000", 1));
395 *SvPVX(PL_rs) = '\n';
399 result = do_readline();
406 PL_last_in_gv = cGVOP_gv;
407 return do_readline();
416 if (SP - MARK != 1) {
418 do_join(TARG, &PL_sv_no, MARK, SP);
425 tmps = SvPV(tmpsv, len);
426 if ((!tmps || !len) && PL_errgv) {
428 (void)SvUPGRADE(error, SVt_PV);
429 if (SvPOK(error) && SvCUR(error))
430 sv_catpv(error, "\t...caught");
432 tmps = SvPV(tmpsv, len);
435 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
437 Perl_warn(aTHX_ "%"SVf, tmpsv);
449 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
451 if (SP - MARK != 1) {
453 do_join(TARG, &PL_sv_no, MARK, SP);
455 tmps = SvPV(tmpsv, len);
461 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
465 (void)SvUPGRADE(error, SVt_PV);
466 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
468 SvSetSV(error,tmpsv);
469 else if (sv_isobject(error)) {
470 HV *stash = SvSTASH(SvRV(error));
471 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
473 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
474 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
481 call_sv((SV*)GvCV(gv),
482 G_SCALAR|G_EVAL|G_KEEPERR);
483 sv_setsv(error,*PL_stack_sp--);
486 DIE(aTHX_ Nullformat);
489 if (SvPOK(error) && SvCUR(error))
490 sv_catpv(error, "\t...propagated");
492 tmps = SvPV(tmpsv, len);
496 tmpsv = sv_2mortal(newSVpvn("Died", 4));
498 DIE(aTHX_ "%"SVf, tmpsv);
518 DIE(aTHX_ PL_no_usym, "filehandle");
519 if ((io = GvIOp(gv)))
520 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
522 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
523 /* Method's args are same as ours ... */
524 /* ... except handle is replaced by the object */
525 *MARK-- = SvTIED_obj((SV*)io, mg);
529 call_method("OPEN", G_SCALAR);
542 tmps = SvPV(sv, len);
543 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
546 PUSHi( (I32)PL_forkprocess );
547 else if (PL_forkprocess == 0) /* we are a new child */
566 if (gv && (io = GvIO(gv))
567 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
570 XPUSHs(SvTIED_obj((SV*)io, mg));
573 call_method("CLOSE", G_SCALAR);
579 PUSHs(boolSV(do_close(gv, TRUE)));
599 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
600 DIE(aTHX_ PL_no_usym, "filehandle");
605 do_close(rgv, FALSE);
607 do_close(wgv, FALSE);
609 if (PerlProc_pipe(fd) < 0)
612 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
613 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
614 IoOFP(rstio) = IoIFP(rstio);
615 IoIFP(wstio) = IoOFP(wstio);
616 IoTYPE(rstio) = IoTYPE_RDONLY;
617 IoTYPE(wstio) = IoTYPE_WRONLY;
619 if (!IoIFP(rstio) || !IoOFP(wstio)) {
620 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
621 else PerlLIO_close(fd[0]);
622 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
623 else PerlLIO_close(fd[1]);
626 #if defined(HAS_FCNTL) && defined(F_SETFD)
627 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
628 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
635 DIE(aTHX_ PL_no_func, "pipe");
651 if (gv && (io = GvIO(gv))
652 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
655 XPUSHs(SvTIED_obj((SV*)io, mg));
658 call_method("FILENO", G_SCALAR);
664 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
665 /* Can't do this because people seem to do things like
666 defined(fileno($foo)) to check whether $foo is a valid fh.
667 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
668 report_evil_fh(gv, io, PL_op->op_type);
673 PUSHi(PerlIO_fileno(fp));
684 anum = PerlLIO_umask(0);
685 (void)PerlLIO_umask(anum);
688 anum = PerlLIO_umask(POPi);
689 TAINT_PROPER("umask");
692 /* Only DIE if trying to restrict permissions on `user' (self).
693 * Otherwise it's harmless and more useful to just return undef
694 * since 'group' and 'other' concepts probably don't exist here. */
695 if (MAXARG >= 1 && (POPi & 0700))
696 DIE(aTHX_ "umask not implemented");
697 XPUSHs(&PL_sv_undef);
719 if (gv && (io = GvIO(gv))
720 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
723 XPUSHs(SvTIED_obj((SV*)io, mg));
728 call_method("BINMODE", G_SCALAR);
735 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
736 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
737 report_evil_fh(gv, io, PL_op->op_type);
738 SETERRNO(EBADF,RMS_IFI);
743 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
744 (discp) ? SvPV_nolen(discp) : Nullch)) {
762 I32 markoff = MARK - PL_stack_base;
764 int how = PERL_MAGIC_tied;
768 switch(SvTYPE(varsv)) {
770 methname = "TIEHASH";
771 HvEITER((HV *)varsv) = Null(HE *);
774 methname = "TIEARRAY";
777 #ifdef GV_UNIQUE_CHECK
778 if (GvUNIQUE((GV*)varsv)) {
779 Perl_croak(aTHX_ "Attempt to tie unique GV");
782 methname = "TIEHANDLE";
783 how = PERL_MAGIC_tiedscalar;
784 /* For tied filehandles, we apply tiedscalar magic to the IO
785 slot of the GP rather than the GV itself. AMS 20010812 */
787 GvIOp(varsv) = newIO();
788 varsv = (SV *)GvIOp(varsv);
791 methname = "TIESCALAR";
792 how = PERL_MAGIC_tiedscalar;
796 if (sv_isobject(*MARK)) {
798 PUSHSTACKi(PERLSI_MAGIC);
800 EXTEND(SP,(I32)items);
804 call_method(methname, G_SCALAR);
807 /* Not clear why we don't call call_method here too.
808 * perhaps to get different error message ?
810 stash = gv_stashsv(*MARK, FALSE);
811 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
812 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
816 PUSHSTACKi(PERLSI_MAGIC);
818 EXTEND(SP,(I32)items);
822 call_sv((SV*)GvCV(gv), G_SCALAR);
828 if (sv_isobject(sv)) {
829 sv_unmagic(varsv, how);
830 /* Croak if a self-tie on an aggregate is attempted. */
831 if (varsv == SvRV(sv) &&
832 (SvTYPE(varsv) == SVt_PVAV ||
833 SvTYPE(varsv) == SVt_PVHV))
835 "Self-ties of arrays and hashes are not supported");
836 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
839 SP = PL_stack_base + markoff;
849 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
850 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
852 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
855 if ((mg = SvTIED_mg(sv, how))) {
856 SV *obj = SvRV(mg->mg_obj);
860 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
861 isGV(gv) && (cv = GvCV(gv))) {
863 XPUSHs(SvTIED_obj((SV*)gv, mg));
864 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
867 call_sv((SV *)cv, G_VOID);
871 else if (ckWARN(WARN_UNTIE)) {
872 if (mg && SvREFCNT(obj) > 1)
873 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
874 "untie attempted while %"UVuf" inner references still exist",
875 (UV)SvREFCNT(obj) - 1 ) ;
878 sv_unmagic(sv, how) ;
888 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
889 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
891 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
894 if ((mg = SvTIED_mg(sv, how))) {
895 SV *osv = SvTIED_obj(sv, mg);
896 if (osv == mg->mg_obj)
897 osv = sv_mortalcopy(osv);
915 sv = sv_mortalcopy(&PL_sv_no);
916 sv_setpv(sv, "AnyDBM_File");
917 stash = gv_stashsv(sv, FALSE);
918 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
920 require_pv("AnyDBM_File.pm");
922 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
923 DIE(aTHX_ "No dbm on this machine");
933 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
935 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
938 call_sv((SV*)GvCV(gv), G_SCALAR);
941 if (!sv_isobject(TOPs)) {
946 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
949 call_sv((SV*)GvCV(gv), G_SCALAR);
953 if (sv_isobject(TOPs)) {
954 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
955 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
977 struct timeval timebuf;
978 struct timeval *tbuf = &timebuf;
982 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
987 # if BYTEORDER & 0xf0000
988 # define ORDERBYTE (0x88888888 - BYTEORDER)
990 # define ORDERBYTE (0x4444 - BYTEORDER)
996 for (i = 1; i <= 3; i++) {
1004 /* little endians can use vecs directly */
1005 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1012 masksize = NFDBITS / NBBY;
1014 masksize = sizeof(long); /* documented int, everyone seems to use long */
1016 Zero(&fd_sets[0], 4, char*);
1019 # if SELECT_MIN_BITS > 1
1020 /* If SELECT_MIN_BITS is greater than one we most probably will want
1021 * to align the sizes with SELECT_MIN_BITS/8 because for example
1022 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1023 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1024 * on (sets/tests/clears bits) is 32 bits. */
1025 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1027 growsize = sizeof(fd_set);
1035 timebuf.tv_sec = (long)value;
1036 value -= (NV)timebuf.tv_sec;
1037 timebuf.tv_usec = (long)(value * 1000000.0);
1040 tbuf = Null(struct timeval*);
1042 for (i = 1; i <= 3; i++) {
1048 else if (!SvPOK(sv))
1049 SvPV_force(sv,n_a); /* force string conversion */
1052 Sv_Grow(sv, growsize);
1056 while (++j <= growsize) {
1060 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1062 New(403, fd_sets[i], growsize, char);
1063 for (offset = 0; offset < growsize; offset += masksize) {
1064 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1065 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1068 fd_sets[i] = SvPVX(sv);
1072 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1073 /* Can't make just the (void*) conditional because that would be
1074 * cpp #if within cpp macro, and not all compilers like that. */
1075 nfound = PerlSock_select(
1077 (Select_fd_set_t) fd_sets[1],
1078 (Select_fd_set_t) fd_sets[2],
1079 (Select_fd_set_t) fd_sets[3],
1080 (void*) tbuf); /* Workaround for compiler bug. */
1082 nfound = PerlSock_select(
1084 (Select_fd_set_t) fd_sets[1],
1085 (Select_fd_set_t) fd_sets[2],
1086 (Select_fd_set_t) fd_sets[3],
1089 for (i = 1; i <= 3; i++) {
1092 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1094 for (offset = 0; offset < growsize; offset += masksize) {
1095 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1096 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1098 Safefree(fd_sets[i]);
1105 if (GIMME == G_ARRAY && tbuf) {
1106 value = (NV)(timebuf.tv_sec) +
1107 (NV)(timebuf.tv_usec) / 1000000.0;
1108 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1109 sv_setnv(sv, value);
1113 DIE(aTHX_ "select not implemented");
1118 Perl_setdefout(pTHX_ GV *gv)
1121 (void)SvREFCNT_inc(gv);
1123 SvREFCNT_dec(PL_defoutgv);
1130 GV *newdefout, *egv;
1133 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1135 egv = GvEGV(PL_defoutgv);
1140 XPUSHs(&PL_sv_undef);
1142 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1143 if (gvp && *gvp == egv) {
1144 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1148 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1153 if (!GvIO(newdefout))
1154 gv_IOadd(newdefout);
1155 setdefout(newdefout);
1173 if (gv && (io = GvIO(gv))
1174 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1176 I32 gimme = GIMME_V;
1178 XPUSHs(SvTIED_obj((SV*)io, mg));
1181 call_method("GETC", gimme);
1184 if (gimme == G_SCALAR)
1185 SvSetMagicSV_nosteal(TARG, TOPs);
1188 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1189 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1190 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1191 report_evil_fh(gv, io, PL_op->op_type);
1192 SETERRNO(EBADF,RMS_IFI);
1196 sv_setpv(TARG, " ");
1197 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1198 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1199 /* Find out how many bytes the char needs */
1200 Size_t len = UTF8SKIP(SvPVX(TARG));
1203 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1204 SvCUR_set(TARG,1+len);
1214 return pp_sysread();
1218 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1220 register PERL_CONTEXT *cx;
1221 I32 gimme = GIMME_V;
1227 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1229 PAD_SET_CUR(CvPADLIST(cv), 1);
1231 setdefout(gv); /* locally select filehandle so $% et al work */
1264 SV *tmpsv = sv_newmortal();
1265 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1266 name = SvPV_nolen(tmpsv);
1269 DIE(aTHX_ "Undefined format \"%s\" called", name);
1270 DIE(aTHX_ "Not a format reference");
1273 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1275 IoFLAGS(io) &= ~IOf_DIDTOP;
1276 return doform(cv,gv,PL_op->op_next);
1282 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1283 register IO *io = GvIOp(gv);
1284 PerlIO *ofp = IoOFP(io);
1288 register PERL_CONTEXT *cx;
1290 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1291 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1294 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1295 PL_formtarget != PL_toptarget)
1299 if (!IoTOP_GV(io)) {
1303 if (!IoTOP_NAME(io)) {
1304 if (!IoFMT_NAME(io))
1305 IoFMT_NAME(io) = savepv(GvNAME(gv));
1306 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1307 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1308 if ((topgv && GvFORM(topgv)) ||
1309 !gv_fetchpv("top",FALSE,SVt_PVFM))
1310 IoTOP_NAME(io) = savepv(SvPVX(topname));
1312 IoTOP_NAME(io) = savepv("top");
1314 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1315 if (!topgv || !GvFORM(topgv)) {
1316 IoLINES_LEFT(io) = 100000000;
1319 IoTOP_GV(io) = topgv;
1321 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1322 I32 lines = IoLINES_LEFT(io);
1323 char *s = SvPVX(PL_formtarget);
1324 if (lines <= 0) /* Yow, header didn't even fit!!! */
1326 while (lines-- > 0) {
1327 s = strchr(s, '\n');
1333 STRLEN save = SvCUR(PL_formtarget);
1334 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1335 do_print(PL_formtarget, ofp);
1336 SvCUR_set(PL_formtarget, save);
1337 sv_chop(PL_formtarget, s);
1338 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1341 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1342 do_print(PL_formfeed, ofp);
1343 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1345 PL_formtarget = PL_toptarget;
1346 IoFLAGS(io) |= IOf_DIDTOP;
1349 DIE(aTHX_ "bad top format reference");
1354 SV *sv = sv_newmortal();
1355 gv_efullname4(sv, fgv, Nullch, FALSE);
1356 name = SvPV_nolen(sv);
1359 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1362 DIE(aTHX_ "Undefined top format called");
1366 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1367 return doform(cv,gv,PL_op);
1371 POPBLOCK(cx,PL_curpm);
1377 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1379 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1380 else if (ckWARN(WARN_CLOSED))
1381 report_evil_fh(gv, io, PL_op->op_type);
1386 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1387 if (ckWARN(WARN_IO))
1388 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1390 if (!do_print(PL_formtarget, fp))
1393 FmLINES(PL_formtarget) = 0;
1394 SvCUR_set(PL_formtarget, 0);
1395 *SvEND(PL_formtarget) = '\0';
1396 if (IoFLAGS(io) & IOf_FLUSH)
1397 (void)PerlIO_flush(fp);
1402 PL_formtarget = PL_bodytarget;
1404 return pop_return();
1409 dSP; dMARK; dORIGMARK;
1416 if (PL_op->op_flags & OPf_STACKED)
1421 if (gv && (io = GvIO(gv))
1422 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1424 if (MARK == ORIGMARK) {
1427 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1431 *MARK = SvTIED_obj((SV*)io, mg);
1434 call_method("PRINTF", G_SCALAR);
1437 MARK = ORIGMARK + 1;
1444 if (!(io = GvIO(gv))) {
1445 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1446 report_evil_fh(gv, io, PL_op->op_type);
1447 SETERRNO(EBADF,RMS_IFI);
1450 else if (!(fp = IoOFP(io))) {
1451 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1453 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1454 else if (ckWARN(WARN_CLOSED))
1455 report_evil_fh(gv, io, PL_op->op_type);
1457 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1461 do_sprintf(sv, SP - MARK, MARK + 1);
1462 if (!do_print(sv, fp))
1465 if (IoFLAGS(io) & IOf_FLUSH)
1466 if (PerlIO_flush(fp) == EOF)
1477 PUSHs(&PL_sv_undef);
1498 /* Need TIEHANDLE method ? */
1500 tmps = SvPV(sv, len);
1501 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1502 IoLINES(GvIOp(gv)) = 0;
1506 PUSHs(&PL_sv_undef);
1513 dSP; dMARK; dORIGMARK; dTARGET;
1520 Sock_size_t bufsize;
1527 bool charstart = FALSE;
1528 STRLEN charskip = 0;
1533 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1534 && gv && (io = GvIO(gv))
1535 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1540 *MARK = SvTIED_obj((SV*)io, mg);
1542 call_method("READ", G_SCALAR);
1555 sv_setpvn(bufsv, "", 0);
1556 length = SvIVx(*++MARK);
1559 offset = SvIVx(*++MARK);
1563 if (!io || !IoIFP(io)) {
1564 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1565 report_evil_fh(gv, io, PL_op->op_type);
1566 SETERRNO(EBADF,RMS_IFI);
1570 #ifdef PERLIO_LAYERS
1571 if (PL_op->op_type == OP_SYSREAD) {
1572 /* sysread happens further down the stack
1573 and we need isutf8 of that layer
1575 pio = PerlIO_syslayer(aTHX_ pio);
1581 if ((fp_utf8 = PerlIO_isutf8(pio)) && !IN_BYTES) {
1582 buffer = SvPVutf8_force(bufsv, blen);
1583 /* UTF8 may not have been set if they are all low bytes */
1587 buffer = SvPV_force(bufsv, blen);
1590 DIE(aTHX_ "Negative length");
1598 if (PL_op->op_type == OP_RECV) {
1599 char namebuf[MAXPATHLEN];
1600 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1601 bufsize = sizeof (struct sockaddr_in);
1603 bufsize = sizeof namebuf;
1605 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1609 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1610 /* 'offset' means 'flags' here */
1611 count = PerlSock_recvfrom(PerlIO_fileno(pio), buffer, length, offset,
1612 (struct sockaddr *)namebuf, &bufsize);
1616 /* Bogus return without padding */
1617 bufsize = sizeof (struct sockaddr_in);
1619 SvCUR_set(bufsv, count);
1620 *SvEND(bufsv) = '\0';
1621 (void)SvPOK_only(bufsv);
1625 /* This should not be marked tainted if the fp is marked clean */
1626 if (!(IoFLAGS(io) & IOf_UNTAINT))
1627 SvTAINTED_on(bufsv);
1629 sv_setpvn(TARG, namebuf, bufsize);
1634 if (PL_op->op_type == OP_RECV)
1635 DIE(aTHX_ PL_no_sock_func, "recv");
1637 if (DO_UTF8(bufsv)) {
1638 /* offset adjust in characters not bytes */
1639 blen = sv_len_utf8(bufsv);
1642 if (-offset > (int)blen)
1643 DIE(aTHX_ "Offset outside string");
1646 if (DO_UTF8(bufsv)) {
1647 /* convert offset-as-chars to offset-as-bytes */
1648 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1651 bufsize = SvCUR(bufsv);
1652 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1653 if (offset > bufsize) { /* Zero any newly allocated space */
1654 Zero(buffer+bufsize, offset-bufsize, char);
1656 buffer = buffer + offset;
1658 #ifndef PERLIO_LAYERS
1659 if (PL_op->op_type == OP_SYSREAD) {
1660 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1661 if (IoTYPE(io) == IoTYPE_SOCKET) {
1662 count = PerlSock_recv(PerlIO_fileno(pio),
1668 count = PerlLIO_read(PerlIO_fileno(pio),
1674 #ifdef HAS_SOCKET__bad_code_maybe
1675 if (IoTYPE(io) == IoTYPE_SOCKET) {
1676 char namebuf[MAXPATHLEN];
1677 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1678 bufsize = sizeof (struct sockaddr_in);
1680 bufsize = sizeof namebuf;
1682 count = PerlSock_recvfrom(PerlIO_fileno(pio), buffer, length, 0,
1683 (struct sockaddr *)namebuf, &bufsize);
1688 count = PerlIO_read(pio, buffer, length);
1689 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1690 if (count == 0 && PerlIO_error(pio))
1694 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1695 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1698 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1699 *SvEND(bufsv) = '\0';
1700 (void)SvPOK_only(bufsv);
1701 if (fp_utf8 && !IN_BYTES) {
1702 /* Look at utf8 we got back and count the characters */
1703 char *bend = buffer + count;
1704 while (buffer < bend) {
1706 skip = UTF8SKIP(buffer);
1709 if (buffer - charskip + skip > bend) {
1710 /* partial character - try for rest of it */
1711 length = skip - (bend-buffer);
1712 offset = bend - SvPVX(bufsv);
1724 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1725 provided amount read (count) was what was requested (length)
1727 if (got < wanted && count == length) {
1728 length = wanted - got;
1729 offset = bend - SvPVX(bufsv);
1732 /* return value is character count */
1737 /* This should not be marked tainted if the fp is marked clean */
1738 if (!(IoFLAGS(io) & IOf_UNTAINT))
1739 SvTAINTED_on(bufsv);
1752 int items = (SP - PL_stack_base) - TOPMARK;
1756 sv = sv_2mortal(newSViv(sv_len(*SP)));
1765 dSP; dMARK; dORIGMARK; dTARGET;
1776 if (PL_op->op_type == OP_SYSWRITE
1777 && gv && (io = GvIO(gv))
1778 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1783 *MARK = SvTIED_obj((SV*)io, mg);
1785 call_method("WRITE", G_SCALAR);
1796 #if Size_t_size > IVSIZE
1797 length = (Size_t)SvNVx(*++MARK);
1799 length = (Size_t)SvIVx(*++MARK);
1801 if ((SSize_t)length < 0)
1802 DIE(aTHX_ "Negative length");
1805 if (!io || !IoIFP(io)) {
1807 if (ckWARN(WARN_CLOSED))
1808 report_evil_fh(gv, io, PL_op->op_type);
1809 SETERRNO(EBADF,RMS_IFI);
1813 if (PerlIO_isutf8(IoIFP(io))) {
1814 buffer = SvPVutf8(bufsv, blen);
1818 sv_utf8_downgrade(bufsv, FALSE);
1819 buffer = SvPV(bufsv, blen);
1822 if (PL_op->op_type == OP_SYSWRITE) {
1824 if (DO_UTF8(bufsv)) {
1825 /* length and offset are in chars */
1826 blen = sv_len_utf8(bufsv);
1829 offset = SvIVx(*++MARK);
1831 if (-offset > (IV)blen)
1832 DIE(aTHX_ "Offset outside string");
1834 } else if (offset >= (IV)blen && blen > 0)
1835 DIE(aTHX_ "Offset outside string");
1838 if (length > blen - offset)
1839 length = blen - offset;
1840 if (DO_UTF8(bufsv)) {
1841 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1842 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1845 buffer = buffer+offset;
1847 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1848 if (IoTYPE(io) == IoTYPE_SOCKET) {
1849 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1855 /* See the note at doio.c:do_print about filesize limits. --jhi */
1856 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1861 else if (SP > MARK) {
1864 sockbuf = SvPVx(*++MARK, mlen);
1865 /* length is really flags */
1866 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1867 length, (struct sockaddr *)sockbuf, mlen);
1870 /* length is really flags */
1871 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1874 DIE(aTHX_ PL_no_sock_func, "send");
1880 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1881 #if Size_t_size > IVSIZE
1895 return pp_sysread();
1906 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1908 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1910 if (io && !IoIFP(io)) {
1911 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1913 IoFLAGS(io) &= ~IOf_START;
1914 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1915 sv_setpvn(GvSV(gv), "-", 1);
1916 SvSETMAGIC(GvSV(gv));
1918 else if (!nextargv(gv))
1923 gv = PL_last_in_gv; /* eof */
1926 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1928 if (gv && (io = GvIO(gv))
1929 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1932 XPUSHs(SvTIED_obj((SV*)io, mg));
1935 call_method("EOF", G_SCALAR);
1941 PUSHs(boolSV(!gv || do_eof(gv)));
1955 gv = PL_last_in_gv = (GV*)POPs;
1957 if (gv && (io = GvIO(gv))
1958 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1961 XPUSHs(SvTIED_obj((SV*)io, mg));
1964 call_method("TELL", G_SCALAR);
1970 #if LSEEKSIZE > IVSIZE
1971 PUSHn( do_tell(gv) );
1973 PUSHi( do_tell(gv) );
1980 return pp_sysseek();
1989 #if LSEEKSIZE > IVSIZE
1990 Off_t offset = (Off_t)SvNVx(POPs);
1992 Off_t offset = (Off_t)SvIVx(POPs);
1996 gv = PL_last_in_gv = (GV*)POPs;
1998 if (gv && (io = GvIO(gv))
1999 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2002 XPUSHs(SvTIED_obj((SV*)io, mg));
2003 #if LSEEKSIZE > IVSIZE
2004 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2006 XPUSHs(sv_2mortal(newSViv(offset)));
2008 XPUSHs(sv_2mortal(newSViv(whence)));
2011 call_method("SEEK", G_SCALAR);
2017 if (PL_op->op_type == OP_SEEK)
2018 PUSHs(boolSV(do_seek(gv, offset, whence)));
2020 Off_t sought = do_sysseek(gv, offset, whence);
2022 PUSHs(&PL_sv_undef);
2025 #if LSEEKSIZE > IVSIZE
2030 : newSVpvn(zero_but_true, ZBTLEN);
2031 PUSHs(sv_2mortal(sv));
2040 /* There seems to be no consensus on the length type of truncate()
2041 * and ftruncate(), both off_t and size_t have supporters. In
2042 * general one would think that when using large files, off_t is
2043 * at least as wide as size_t, so using an off_t should be okay. */
2044 /* XXX Configure probe for the length type of *truncate() needed XXX */
2047 #if Off_t_size > IVSIZE
2052 /* Checking for length < 0 is problematic as the type might or
2053 * might not be signed: if it is not, clever compilers will moan. */
2054 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2056 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2063 if (PL_op->op_flags & OPf_SPECIAL) {
2064 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2073 TAINT_PROPER("truncate");
2074 if (!(fp = IoIFP(io))) {
2080 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2082 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2092 if (SvTYPE(sv) == SVt_PVGV) {
2093 tmpgv = (GV*)sv; /* *main::FRED for example */
2094 goto do_ftruncate_gv;
2096 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2097 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2098 goto do_ftruncate_gv;
2100 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2101 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2102 goto do_ftruncate_io;
2105 name = SvPV(sv, n_a);
2106 TAINT_PROPER("truncate");
2108 if (truncate(name, len) < 0)
2114 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2117 if (my_chsize(tmpfd, len) < 0)
2119 PerlLIO_close(tmpfd);
2128 SETERRNO(EBADF,RMS_IFI);
2132 DIE(aTHX_ "truncate not implemented");
2145 unsigned int func = POPu;
2146 int optype = PL_op->op_type;
2150 IO *io = gv ? GvIOn(gv) : 0;
2152 if (!io || !argsv || !IoIFP(io)) {
2153 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2154 report_evil_fh(gv, io, PL_op->op_type);
2155 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2159 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2162 s = SvPV_force(argsv, len);
2163 need = IOCPARM_LEN(func);
2165 s = Sv_Grow(argsv, need + 1);
2166 SvCUR_set(argsv, need);
2169 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2172 retval = SvIV(argsv);
2173 s = INT2PTR(char*,retval); /* ouch */
2176 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2178 if (optype == OP_IOCTL)
2180 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2182 DIE(aTHX_ "ioctl is not implemented");
2186 DIE(aTHX_ "fcntl is not implemented");
2188 #if defined(OS2) && defined(__EMX__)
2189 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2191 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2195 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2197 if (s[SvCUR(argsv)] != 17)
2198 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2200 s[SvCUR(argsv)] = 0; /* put our null back */
2201 SvSETMAGIC(argsv); /* Assume it has changed */
2210 PUSHp(zero_but_true, ZBTLEN);
2231 if (gv && (io = GvIO(gv)))
2238 (void)PerlIO_flush(fp);
2239 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2242 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2243 report_evil_fh(gv, io, PL_op->op_type);
2245 SETERRNO(EBADF,RMS_IFI);
2250 DIE(aTHX_ PL_no_func, "flock()");
2262 int protocol = POPi;
2268 io = gv ? GvIOn(gv) : NULL;
2271 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2272 report_evil_fh(gv, io, PL_op->op_type);
2274 do_close(gv, FALSE);
2275 SETERRNO(EBADF,LIB_INVARG);
2280 do_close(gv, FALSE);
2282 TAINT_PROPER("socket");
2283 fd = PerlSock_socket(domain, type, protocol);
2286 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2287 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2288 IoTYPE(io) = IoTYPE_SOCKET;
2289 if (!IoIFP(io) || !IoOFP(io)) {
2290 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2291 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2292 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2295 #if defined(HAS_FCNTL) && defined(F_SETFD)
2296 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2300 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2305 DIE(aTHX_ PL_no_sock_func, "socket");
2311 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2317 int protocol = POPi;
2324 io1 = gv1 ? GvIOn(gv1) : NULL;
2325 io2 = gv2 ? GvIOn(gv2) : NULL;
2326 if (!gv1 || !gv2 || !io1 || !io2) {
2327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2329 report_evil_fh(gv1, io1, PL_op->op_type);
2331 report_evil_fh(gv1, io2, PL_op->op_type);
2334 do_close(gv1, FALSE);
2336 do_close(gv2, FALSE);
2341 do_close(gv1, FALSE);
2343 do_close(gv2, FALSE);
2345 TAINT_PROPER("socketpair");
2346 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2348 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2349 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2350 IoTYPE(io1) = IoTYPE_SOCKET;
2351 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2352 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2353 IoTYPE(io2) = IoTYPE_SOCKET;
2354 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2355 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2356 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2357 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2358 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2359 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2360 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2363 #if defined(HAS_FCNTL) && defined(F_SETFD)
2364 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2365 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2370 DIE(aTHX_ PL_no_sock_func, "socketpair");
2378 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2379 extern void GETPRIVMODE();
2380 extern void GETUSERMODE();
2385 register IO *io = GvIOn(gv);
2389 int mpeprivmode = 0;
2392 if (!io || !IoIFP(io))
2395 addr = SvPV(addrsv, len);
2396 TAINT_PROPER("bind");
2397 #ifdef MPE /* Deal with MPE bind() peculiarities */
2398 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2399 /* The address *MUST* stupidly be zero. */
2400 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2401 /* PRIV mode is required to bind() to ports < 1024. */
2402 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2403 ((struct sockaddr_in *)addr)->sin_port > 0) {
2404 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2409 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2410 (struct sockaddr *)addr, len) >= 0)
2413 #ifdef MPE /* Switch back to USER mode */
2424 if (ckWARN(WARN_CLOSED))
2425 report_evil_fh(gv, io, PL_op->op_type);
2426 SETERRNO(EBADF,SS_IVCHAN);
2429 DIE(aTHX_ PL_no_sock_func, "bind");
2440 register IO *io = GvIOn(gv);
2443 if (!io || !IoIFP(io))
2446 addr = SvPV(addrsv, len);
2447 TAINT_PROPER("connect");
2448 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 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, "connect");
2469 register IO *io = gv ? GvIOn(gv) : NULL;
2471 if (!gv || !io || !IoIFP(io))
2474 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2480 if (ckWARN(WARN_CLOSED))
2481 report_evil_fh(gv, io, PL_op->op_type);
2482 SETERRNO(EBADF,SS_IVCHAN);
2485 DIE(aTHX_ PL_no_sock_func, "listen");
2497 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2498 Sock_size_t len = sizeof saddr;
2510 if (!gstio || !IoIFP(gstio))
2514 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2518 do_close(ngv, FALSE);
2519 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2520 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2521 IoTYPE(nstio) = IoTYPE_SOCKET;
2522 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2523 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2524 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2525 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2528 #if defined(HAS_FCNTL) && defined(F_SETFD)
2529 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2533 len = sizeof saddr; /* EPOC somehow truncates info */
2534 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2536 #ifdef __SCO_VERSION__
2537 len = sizeof saddr; /* OpenUNIX 8 somehow truncates info */
2540 PUSHp((char *)&saddr, len);
2544 if (ckWARN(WARN_CLOSED))
2545 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2546 SETERRNO(EBADF,SS_IVCHAN);
2552 DIE(aTHX_ PL_no_sock_func, "accept");
2562 register IO *io = GvIOn(gv);
2564 if (!io || !IoIFP(io))
2567 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2571 if (ckWARN(WARN_CLOSED))
2572 report_evil_fh(gv, io, PL_op->op_type);
2573 SETERRNO(EBADF,SS_IVCHAN);
2576 DIE(aTHX_ PL_no_sock_func, "shutdown");
2583 return pp_ssockopt();
2585 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2593 int optype = PL_op->op_type;
2596 unsigned int optname;
2602 if (optype == OP_GSOCKOPT)
2603 sv = sv_2mortal(NEWSV(22, 257));
2606 optname = (unsigned int) POPi;
2607 lvl = (unsigned int) POPi;
2611 if (!io || !IoIFP(io))
2614 fd = PerlIO_fileno(IoIFP(io));
2618 (void)SvPOK_only(sv);
2622 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2637 aint = (int)SvIV(sv);
2641 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2650 if (ckWARN(WARN_CLOSED))
2651 report_evil_fh(gv, io, optype);
2652 SETERRNO(EBADF,SS_IVCHAN);
2657 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2664 return pp_getpeername();
2666 DIE(aTHX_ PL_no_sock_func, "getsockname");
2674 int optype = PL_op->op_type;
2678 register IO *io = GvIOn(gv);
2681 if (!io || !IoIFP(io))
2684 sv = sv_2mortal(NEWSV(22, 257));
2685 (void)SvPOK_only(sv);
2689 fd = PerlIO_fileno(IoIFP(io));
2691 case OP_GETSOCKNAME:
2692 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2695 case OP_GETPEERNAME:
2696 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2698 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2700 static const char nowhere[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
2701 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2702 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2703 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2704 sizeof(u_short) + sizeof(struct in_addr))) {
2711 #ifdef BOGUS_GETNAME_RETURN
2712 /* Interactive Unix, getpeername() and getsockname()
2713 does not return valid namelen */
2714 if (len == BOGUS_GETNAME_RETURN)
2715 len = sizeof(struct sockaddr);
2723 if (ckWARN(WARN_CLOSED))
2724 report_evil_fh(gv, io, optype);
2725 SETERRNO(EBADF,SS_IVCHAN);
2730 DIE(aTHX_ PL_no_sock_func, "getpeername");
2749 if (PL_op->op_flags & OPf_REF) {
2751 if (PL_op->op_type == OP_LSTAT) {
2752 if (gv != PL_defgv) {
2753 if (ckWARN(WARN_IO))
2754 Perl_warner(aTHX_ packWARN(WARN_IO),
2755 "lstat() on filehandle %s", GvENAME(gv));
2756 } else if (PL_laststype != OP_LSTAT)
2757 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2761 if (gv != PL_defgv) {
2762 PL_laststype = OP_STAT;
2764 sv_setpv(PL_statname, "");
2765 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2766 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2768 if (PL_laststatval < 0) {
2769 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2770 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2776 if (SvTYPE(sv) == SVt_PVGV) {
2780 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2782 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2783 Perl_warner(aTHX_ packWARN(WARN_IO),
2784 "lstat() on filehandle %s", GvENAME(gv));
2787 sv_setpv(PL_statname, SvPV(sv,n_a));
2790 PL_laststype = PL_op->op_type;
2791 if (PL_op->op_type == OP_LSTAT)
2792 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2795 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2796 if (PL_laststatval < 0) {
2797 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2798 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2804 if (gimme != G_ARRAY) {
2805 if (gimme != G_VOID)
2806 XPUSHs(boolSV(max));
2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2813 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2814 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2815 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2816 #if Uid_t_size > IVSIZE
2817 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2819 # if Uid_t_sign <= 0
2820 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2822 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2825 #if Gid_t_size > IVSIZE
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2828 # if Gid_t_sign <= 0
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2834 #ifdef USE_STAT_RDEV
2835 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
2839 #if Off_t_size > IVSIZE
2840 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2842 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2845 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2846 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2847 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2849 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2850 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2851 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2853 #ifdef USE_STAT_BLOCKS
2854 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2855 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2857 PUSHs(sv_2mortal(newSVpvn("", 0)));
2858 PUSHs(sv_2mortal(newSVpvn("", 0)));
2868 #if defined(HAS_ACCESS) && defined(R_OK)
2870 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2871 result = access(POPpx, R_OK);
2886 if (cando(S_IRUSR, 0, &PL_statcache))
2895 #if defined(HAS_ACCESS) && defined(W_OK)
2897 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2898 result = access(POPpx, W_OK);
2913 if (cando(S_IWUSR, 0, &PL_statcache))
2922 #if defined(HAS_ACCESS) && defined(X_OK)
2924 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2925 result = access(POPpx, X_OK);
2940 if (cando(S_IXUSR, 0, &PL_statcache))
2949 #ifdef PERL_EFF_ACCESS_R_OK
2951 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2952 result = PERL_EFF_ACCESS_R_OK(POPpx);
2967 if (cando(S_IRUSR, 1, &PL_statcache))
2976 #ifdef PERL_EFF_ACCESS_W_OK
2978 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2979 result = PERL_EFF_ACCESS_W_OK(POPpx);
2994 if (cando(S_IWUSR, 1, &PL_statcache))
3003 #ifdef PERL_EFF_ACCESS_X_OK
3005 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3006 result = PERL_EFF_ACCESS_X_OK(POPpx);
3021 if (cando(S_IXUSR, 1, &PL_statcache))
3028 I32 result = my_stat();
3037 return pp_ftrowned();
3042 I32 result = my_stat();
3046 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3054 I32 result = my_stat();
3058 if (PL_statcache.st_size == 0)
3065 I32 result = my_stat();
3069 #if Off_t_size > IVSIZE
3070 PUSHn(PL_statcache.st_size);
3072 PUSHi(PL_statcache.st_size);
3079 I32 result = my_stat();
3083 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3089 I32 result = my_stat();
3093 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3099 I32 result = my_stat();
3103 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3109 I32 result = my_stat();
3113 if (S_ISSOCK(PL_statcache.st_mode))
3120 I32 result = my_stat();
3124 if (S_ISCHR(PL_statcache.st_mode))
3131 I32 result = my_stat();
3135 if (S_ISBLK(PL_statcache.st_mode))
3142 I32 result = my_stat();
3146 if (S_ISREG(PL_statcache.st_mode))
3153 I32 result = my_stat();
3157 if (S_ISDIR(PL_statcache.st_mode))
3164 I32 result = my_stat();
3168 if (S_ISFIFO(PL_statcache.st_mode))
3175 I32 result = my_lstat();
3179 if (S_ISLNK(PL_statcache.st_mode))
3188 I32 result = my_stat();
3192 if (PL_statcache.st_mode & S_ISUID)
3202 I32 result = my_stat();
3206 if (PL_statcache.st_mode & S_ISGID)
3216 I32 result = my_stat();
3220 if (PL_statcache.st_mode & S_ISVTX)
3231 char *tmps = Nullch;
3234 if (PL_op->op_flags & OPf_REF)
3236 else if (isGV(TOPs))
3238 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3239 gv = (GV*)SvRV(POPs);
3241 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3243 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3244 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3245 else if (tmps && isDIGIT(*tmps))
3249 if (PerlLIO_isatty(fd))
3254 #if defined(atarist) /* this will work with atariST. Configure will
3255 make guesses for other systems. */
3256 # define FILE_base(f) ((f)->_base)
3257 # define FILE_ptr(f) ((f)->_ptr)
3258 # define FILE_cnt(f) ((f)->_cnt)
3259 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3269 register STDCHAR *s;
3276 if (PL_op->op_flags & OPf_REF)
3278 else if (isGV(TOPs))
3280 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3281 gv = (GV*)SvRV(POPs);
3287 if (gv == PL_defgv) {
3289 io = GvIO(PL_statgv);
3292 goto really_filename;
3297 PL_laststatval = -1;
3298 sv_setpv(PL_statname, "");
3299 io = GvIO(PL_statgv);
3301 if (io && IoIFP(io)) {
3302 if (! PerlIO_has_base(IoIFP(io)))
3303 DIE(aTHX_ "-T and -B not implemented on filehandles");
3304 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3305 if (PL_laststatval < 0)
3307 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3308 if (PL_op->op_type == OP_FTTEXT)
3313 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3314 i = PerlIO_getc(IoIFP(io));
3316 (void)PerlIO_ungetc(IoIFP(io),i);
3318 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3320 len = PerlIO_get_bufsiz(IoIFP(io));
3321 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3322 /* sfio can have large buffers - limit to 512 */
3327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3329 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3331 SETERRNO(EBADF,RMS_IFI);
3339 PL_laststatval = -1;
3340 PL_laststype = OP_STAT;
3341 sv_setpv(PL_statname, SvPV(sv, n_a));
3342 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3343 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3344 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3347 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3348 if (PL_laststatval < 0) {
3349 (void)PerlIO_close(fp);
3352 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3353 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3354 (void)PerlIO_close(fp);
3356 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3357 RETPUSHNO; /* special case NFS directories */
3358 RETPUSHYES; /* null file is anything */
3363 /* now scan s to look for textiness */
3364 /* XXX ASCII dependent code */
3366 #if defined(DOSISH) || defined(USEMYBINMODE)
3367 /* ignore trailing ^Z on short files */
3368 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3372 for (i = 0; i < len; i++, s++) {
3373 if (!*s) { /* null never allowed in text */
3378 else if (!(isPRINT(*s) || isSPACE(*s)))
3381 else if (*s & 128) {
3383 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3386 /* utf8 characters don't count as odd */
3387 if (UTF8_IS_START(*s)) {
3388 int ulen = UTF8SKIP(s);
3389 if (ulen < len - i) {
3391 for (j = 1; j < ulen; j++) {
3392 if (!UTF8_IS_CONTINUATION(s[j]))
3395 --ulen; /* loop does extra increment */
3405 *s != '\n' && *s != '\r' && *s != '\b' &&
3406 *s != '\t' && *s != '\f' && *s != 27)
3411 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3436 if( !tmps || !*tmps ) {
3437 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3438 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3440 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3445 deprecate("chdir('') or chdir(undef) as chdir()");
3446 tmps = SvPV(*svp, n_a);
3450 TAINT_PROPER("chdir");
3455 TAINT_PROPER("chdir");
3456 PUSHi( PerlDir_chdir(tmps) >= 0 );
3458 /* Clear the DEFAULT element of ENV so we'll get the new value
3460 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3468 dSP; dMARK; dTARGET;
3469 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3475 DIE(aTHX_ PL_no_func, "chown");
3485 TAINT_PROPER("chroot");
3486 PUSHi( chroot(tmps) >= 0 );
3489 DIE(aTHX_ PL_no_func, "chroot");
3495 dSP; dMARK; dTARGET;
3497 value = (I32)apply(PL_op->op_type, MARK, SP);
3505 dSP; dMARK; dTARGET;
3507 value = (I32)apply(PL_op->op_type, MARK, SP);
3515 dSP; dMARK; dTARGET;
3517 value = (I32)apply(PL_op->op_type, MARK, SP);
3529 char *tmps2 = POPpx;
3530 char *tmps = SvPV(TOPs, n_a);
3531 TAINT_PROPER("rename");
3533 anum = PerlLIO_rename(tmps, tmps2);
3535 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3536 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3539 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3540 (void)UNLINK(tmps2);
3541 if (!(anum = link(tmps, tmps2)))
3542 anum = UNLINK(tmps);
3555 char *tmps2 = POPpx;
3556 char *tmps = SvPV(TOPs, n_a);
3557 TAINT_PROPER("link");
3558 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3561 DIE(aTHX_ PL_no_func, "link");
3570 char *tmps2 = POPpx;
3571 char *tmps = SvPV(TOPs, n_a);
3572 TAINT_PROPER("symlink");
3573 SETi( symlink(tmps, tmps2) >= 0 );
3576 DIE(aTHX_ PL_no_func, "symlink");
3586 char buf[MAXPATHLEN];
3590 #ifndef INCOMPLETE_TAINTS
3594 len = readlink(tmps, buf, sizeof(buf) - 1);
3602 RETSETUNDEF; /* just pretend it's a normal file */
3606 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3608 S_dooneliner(pTHX_ char *cmd, char *filename)
3610 char *save_filename = filename;
3616 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3617 strcpy(cmdline, cmd);
3618 strcat(cmdline, " ");
3619 for (s = cmdline + strlen(cmdline); *filename; ) {
3624 myfp = PerlProc_popen(cmdline, "r");
3628 SV *tmpsv = sv_newmortal();
3629 /* Need to save/restore 'PL_rs' ?? */
3630 s = sv_gets(tmpsv, myfp, 0);
3631 (void)PerlProc_pclose(myfp);
3635 #ifdef HAS_SYS_ERRLIST
3640 /* you don't see this */
3642 #ifdef HAS_SYS_ERRLIST
3650 if (instr(s, errmsg)) {
3657 #define EACCES EPERM
3659 if (instr(s, "cannot make"))
3660 SETERRNO(EEXIST,RMS_FEX);
3661 else if (instr(s, "existing file"))
3662 SETERRNO(EEXIST,RMS_FEX);
3663 else if (instr(s, "ile exists"))
3664 SETERRNO(EEXIST,RMS_FEX);
3665 else if (instr(s, "non-exist"))
3666 SETERRNO(ENOENT,RMS_FNF);
3667 else if (instr(s, "does not exist"))
3668 SETERRNO(ENOENT,RMS_FNF);
3669 else if (instr(s, "not empty"))
3670 SETERRNO(EBUSY,SS_DEVOFFLINE);
3671 else if (instr(s, "cannot access"))
3672 SETERRNO(EACCES,RMS_PRV);
3674 SETERRNO(EPERM,RMS_PRV);
3677 else { /* some mkdirs return no failure indication */
3678 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3679 if (PL_op->op_type == OP_RMDIR)
3684 SETERRNO(EACCES,RMS_PRV); /* a guess */
3693 /* This macro removes trailing slashes from a directory name.
3694 * Different operating and file systems take differently to
3695 * trailing slashes. According to POSIX 1003.1 1996 Edition
3696 * any number of trailing slashes should be allowed.
3697 * Thusly we snip them away so that even non-conforming
3698 * systems are happy.
3699 * We should probably do this "filtering" for all
3700 * the functions that expect (potentially) directory names:
3701 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3702 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3704 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3705 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3708 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3709 (tmps) = savepvn((tmps), (len)); \
3729 TRIMSLASHES(tmps,len,copy);
3731 TAINT_PROPER("mkdir");
3733 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3735 SETi( dooneliner("mkdir", tmps) );
3736 oldumask = PerlLIO_umask(0);
3737 PerlLIO_umask(oldumask);
3738 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3752 TRIMSLASHES(tmps,len,copy);
3753 TAINT_PROPER("rmdir");
3755 SETi( PerlDir_rmdir(tmps) >= 0 );
3757 SETi( dooneliner("rmdir", tmps) );
3764 /* Directory calls. */
3768 #if defined(Direntry_t) && defined(HAS_READDIR)
3771 char *dirname = POPpx;
3773 register IO *io = GvIOn(gv);
3779 PerlDir_close(IoDIRP(io));
3780 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3786 SETERRNO(EBADF,RMS_DIR);
3789 DIE(aTHX_ PL_no_dir_func, "opendir");
3795 #if defined(Direntry_t) && defined(HAS_READDIR)
3797 #if !defined(I_DIRENT) && !defined(VMS)
3798 Direntry_t *readdir (DIR *);
3800 register Direntry_t *dp;
3802 register IO *io = GvIOn(gv);
3805 if (!io || !IoDIRP(io))
3808 if (GIMME == G_ARRAY) {
3810 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3812 sv = newSVpvn(dp->d_name, dp->d_namlen);
3814 sv = newSVpv(dp->d_name, 0);
3816 #ifndef INCOMPLETE_TAINTS
3817 if (!(IoFLAGS(io) & IOf_UNTAINT))
3820 XPUSHs(sv_2mortal(sv));
3824 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3827 sv = newSVpvn(dp->d_name, dp->d_namlen);
3829 sv = newSVpv(dp->d_name, 0);
3831 #ifndef INCOMPLETE_TAINTS
3832 if (!(IoFLAGS(io) & IOf_UNTAINT))
3835 XPUSHs(sv_2mortal(sv));
3841 SETERRNO(EBADF,RMS_ISI);
3842 if (GIMME == G_ARRAY)
3847 DIE(aTHX_ PL_no_dir_func, "readdir");
3853 #if defined(HAS_TELLDIR) || defined(telldir)
3855 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3856 /* XXX netbsd still seemed to.
3857 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3858 --JHI 1999-Feb-02 */
3859 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3860 long telldir (DIR *);
3863 register IO *io = GvIOn(gv);
3865 if (!io || !IoDIRP(io))
3868 PUSHi( PerlDir_tell(IoDIRP(io)) );
3872 SETERRNO(EBADF,RMS_ISI);
3875 DIE(aTHX_ PL_no_dir_func, "telldir");
3881 #if defined(HAS_SEEKDIR) || defined(seekdir)
3885 register IO *io = GvIOn(gv);
3887 if (!io || !IoDIRP(io))
3890 (void)PerlDir_seek(IoDIRP(io), along);
3895 SETERRNO(EBADF,RMS_ISI);
3898 DIE(aTHX_ PL_no_dir_func, "seekdir");
3904 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3907 register IO *io = GvIOn(gv);
3909 if (!io || !IoDIRP(io))
3912 (void)PerlDir_rewind(IoDIRP(io));
3916 SETERRNO(EBADF,RMS_ISI);
3919 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3925 #if defined(Direntry_t) && defined(HAS_READDIR)
3928 register IO *io = GvIOn(gv);
3930 if (!io || !IoDIRP(io))
3933 #ifdef VOID_CLOSEDIR
3934 PerlDir_close(IoDIRP(io));
3936 if (PerlDir_close(IoDIRP(io)) < 0) {
3937 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3946 SETERRNO(EBADF,RMS_IFI);
3949 DIE(aTHX_ PL_no_dir_func, "closedir");
3953 /* Process control. */
3963 PERL_FLUSHALL_FOR_CHILD;
3964 childpid = PerlProc_fork();
3969 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3970 SvREADONLY_off(GvSV(tmpgv));
3971 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3972 SvREADONLY_on(GvSV(tmpgv));
3974 #ifdef THREADS_HAVE_PIDS
3975 PL_ppid = (IV)getppid();
3977 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3982 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3987 PERL_FLUSHALL_FOR_CHILD;
3988 childpid = PerlProc_fork();
3994 DIE(aTHX_ PL_no_func, "fork");
4001 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4006 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4007 childpid = wait4pid(-1, &argflags, 0);
4009 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4014 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4015 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4016 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4018 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4023 DIE(aTHX_ PL_no_func, "wait");
4029 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4037 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4038 childpid = wait4pid(childpid, &argflags, optype);
4040 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4045 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4046 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4047 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4049 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4054 DIE(aTHX_ PL_no_func, "waitpid");
4060 dSP; dMARK; dORIGMARK; dTARGET;
4068 while (++MARK <= SP) {
4069 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4074 TAINT_PROPER("system");
4076 PERL_FLUSHALL_FOR_CHILD;
4077 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4082 if (PerlProc_pipe(pp) >= 0)
4084 while ((childpid = PerlProc_fork()) == -1) {
4085 if (errno != EAGAIN) {
4090 PerlLIO_close(pp[0]);
4091 PerlLIO_close(pp[1]);
4098 Sigsave_t ihand,qhand; /* place to save signals during system() */
4102 PerlLIO_close(pp[1]);
4104 rsignal_save(SIGINT, SIG_IGN, &ihand);
4105 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4108 result = wait4pid(childpid, &status, 0);
4109 } while (result == -1 && errno == EINTR);
4111 (void)rsignal_restore(SIGINT, &ihand);
4112 (void)rsignal_restore(SIGQUIT, &qhand);
4114 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4115 do_execfree(); /* free any memory child malloced on fork */
4121 while (n < sizeof(int)) {
4122 n1 = PerlLIO_read(pp[0],
4123 (void*)(((char*)&errkid)+n),
4129 PerlLIO_close(pp[0]);
4130 if (n) { /* Error */
4131 if (n != sizeof(int))
4132 DIE(aTHX_ "panic: kid popen errno read");
4133 errno = errkid; /* Propagate errno from kid */
4134 STATUS_CURRENT = -1;
4137 PUSHi(STATUS_CURRENT);
4141 PerlLIO_close(pp[0]);
4142 #if defined(HAS_FCNTL) && defined(F_SETFD)
4143 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4146 if (PL_op->op_flags & OPf_STACKED) {
4147 SV *really = *++MARK;
4148 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4150 else if (SP - MARK != 1)
4151 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4153 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4157 #else /* ! FORK or VMS or OS/2 */
4160 if (PL_op->op_flags & OPf_STACKED) {
4161 SV *really = *++MARK;
4163 value = (I32)do_aspawn(really, MARK, SP);
4165 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4168 else if (SP - MARK != 1) {
4170 value = (I32)do_aspawn(Nullsv, MARK, SP);
4172 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4176 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4178 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4180 STATUS_NATIVE_SET(value);
4183 PUSHi(result ? value : STATUS_CURRENT);
4184 #endif /* !FORK or VMS */
4190 dSP; dMARK; dORIGMARK; dTARGET;
4196 while (++MARK <= SP) {
4197 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4202 TAINT_PROPER("exec");
4204 PERL_FLUSHALL_FOR_CHILD;
4205 if (PL_op->op_flags & OPf_STACKED) {
4206 SV *really = *++MARK;
4207 value = (I32)do_aexec(really, MARK, SP);
4209 else if (SP - MARK != 1)
4211 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4215 (void ) do_aspawn(Nullsv, MARK, SP);
4219 value = (I32)do_aexec(Nullsv, MARK, SP);
4224 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4227 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4230 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4243 dSP; dMARK; dTARGET;
4245 value = (I32)apply(PL_op->op_type, MARK, SP);
4250 DIE(aTHX_ PL_no_func, "kill");
4258 # ifdef THREADS_HAVE_PIDS
4261 XPUSHi( getppid() );
4265 DIE(aTHX_ PL_no_func, "getppid");
4281 pgrp = (I32)BSD_GETPGRP(pid);
4283 if (pid != 0 && pid != PerlProc_getpid())
4284 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4290 DIE(aTHX_ PL_no_func, "getpgrp()");
4309 TAINT_PROPER("setpgrp");
4311 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4313 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4314 || (pid != 0 && pid != PerlProc_getpid()))
4316 DIE(aTHX_ "setpgrp can't take arguments");
4318 SETi( setpgrp() >= 0 );
4319 #endif /* USE_BSDPGRP */
4322 DIE(aTHX_ PL_no_func, "setpgrp()");
4328 #ifdef HAS_GETPRIORITY
4332 SETi( getpriority(which, who) );
4335 DIE(aTHX_ PL_no_func, "getpriority()");
4341 #ifdef HAS_SETPRIORITY
4346 TAINT_PROPER("setpriority");
4347 SETi( setpriority(which, who, niceval) >= 0 );
4350 DIE(aTHX_ PL_no_func, "setpriority()");
4360 XPUSHn( time(Null(Time_t*)) );
4362 XPUSHi( time(Null(Time_t*)) );
4373 (void)PerlProc_times(&PL_timesbuf);
4375 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4376 /* struct tms, though same data */
4380 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4381 if (GIMME == G_ARRAY) {
4382 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4383 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4384 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4388 DIE(aTHX_ "times not implemented");
4389 #endif /* HAS_TIMES */
4402 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4403 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4404 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4410 when = (Time_t)SvNVx(POPs);
4412 when = (Time_t)SvIVx(POPs);
4415 if (PL_op->op_type == OP_LOCALTIME)
4416 tmbuf = localtime(&when);
4418 tmbuf = gmtime(&when);
4420 if (GIMME != G_ARRAY) {
4426 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4427 dayname[tmbuf->tm_wday],
4428 monname[tmbuf->tm_mon],
4433 tmbuf->tm_year + 1900);
4434 PUSHs(sv_2mortal(tsv));
4439 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4440 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4441 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4442 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4443 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4444 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4445 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4446 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4447 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4458 anum = alarm((unsigned int)anum);
4465 DIE(aTHX_ PL_no_func, "alarm");
4476 (void)time(&lasttime);
4481 PerlProc_sleep((unsigned int)duration);
4484 XPUSHi(when - lasttime);
4488 /* Shared memory. */
4502 return pp_shmwrite();
4507 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4508 dSP; dMARK; dTARGET;
4509 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4518 /* Message passing. */
4532 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4533 dSP; dMARK; dTARGET;
4534 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4545 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4546 dSP; dMARK; dTARGET;
4547 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4560 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4561 dSP; dMARK; dTARGET;
4562 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4569 DIE(aTHX_ "System V IPC is not implemented on this machine");
4575 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4576 dSP; dMARK; dTARGET;
4577 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4585 PUSHp(zero_but_true, ZBTLEN);
4595 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4596 dSP; dMARK; dTARGET;
4597 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4606 /* Get system info. */
4610 #ifdef HAS_GETHOSTBYNAME
4611 return pp_ghostent();
4613 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4619 #ifdef HAS_GETHOSTBYADDR
4620 return pp_ghostent();
4622 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4628 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4630 I32 which = PL_op->op_type;
4631 register char **elem;
4633 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4634 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4635 struct hostent *gethostbyname(Netdb_name_t);
4636 struct hostent *gethostent(void);
4638 struct hostent *hent;
4643 if (which == OP_GHBYNAME) {
4644 #ifdef HAS_GETHOSTBYNAME
4645 char* name = POPpbytex;
4646 hent = PerlSock_gethostbyname(name);
4648 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4651 else if (which == OP_GHBYADDR) {
4652 #ifdef HAS_GETHOSTBYADDR
4653 int addrtype = POPi;
4656 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4658 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4660 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4664 #ifdef HAS_GETHOSTENT
4665 hent = PerlSock_gethostent();
4667 DIE(aTHX_ PL_no_sock_func, "gethostent");
4670 #ifdef HOST_NOT_FOUND
4672 #ifdef USE_REENTRANT_API
4673 # ifdef USE_GETHOSTENT_ERRNO
4674 h_errno = PL_reentrant_buffer->_gethostent_errno;
4677 STATUS_NATIVE_SET(h_errno);
4681 if (GIMME != G_ARRAY) {
4682 PUSHs(sv = sv_newmortal());
4684 if (which == OP_GHBYNAME) {
4686 sv_setpvn(sv, hent->h_addr, hent->h_length);
4689 sv_setpv(sv, (char*)hent->h_name);
4695 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4696 sv_setpv(sv, (char*)hent->h_name);
4697 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4698 for (elem = hent->h_aliases; elem && *elem; elem++) {
4699 sv_catpv(sv, *elem);
4701 sv_catpvn(sv, " ", 1);
4703 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4704 sv_setiv(sv, (IV)hent->h_addrtype);
4705 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4706 len = hent->h_length;
4707 sv_setiv(sv, (IV)len);
4709 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4710 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4711 sv_setpvn(sv, *elem, len);
4714 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4716 sv_setpvn(sv, hent->h_addr, len);
4721 DIE(aTHX_ PL_no_sock_func, "gethostent");
4727 #ifdef HAS_GETNETBYNAME
4728 return pp_gnetent();
4730 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4736 #ifdef HAS_GETNETBYADDR
4737 return pp_gnetent();
4739 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4745 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4747 I32 which = PL_op->op_type;
4748 register char **elem;
4750 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4751 struct netent *getnetbyaddr(Netdb_net_t, int);
4752 struct netent *getnetbyname(Netdb_name_t);
4753 struct netent *getnetent(void);
4755 struct netent *nent;
4758 if (which == OP_GNBYNAME){
4759 #ifdef HAS_GETNETBYNAME
4760 char *name = POPpbytex;
4761 nent = PerlSock_getnetbyname(name);
4763 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4766 else if (which == OP_GNBYADDR) {
4767 #ifdef HAS_GETNETBYADDR
4768 int addrtype = POPi;
4769 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4770 nent = PerlSock_getnetbyaddr(addr, addrtype);
4772 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4776 #ifdef HAS_GETNETENT
4777 nent = PerlSock_getnetent();
4779 DIE(aTHX_ PL_no_sock_func, "getnetent");
4782 #ifdef HOST_NOT_FOUND
4784 #ifdef USE_REENTRANT_API
4785 # ifdef USE_GETNETENT_ERRNO
4786 h_errno = PL_reentrant_buffer->_getnetent_errno;
4789 STATUS_NATIVE_SET(h_errno);
4794 if (GIMME != G_ARRAY) {
4795 PUSHs(sv = sv_newmortal());
4797 if (which == OP_GNBYNAME)
4798 sv_setiv(sv, (IV)nent->n_net);
4800 sv_setpv(sv, nent->n_name);
4806 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4807 sv_setpv(sv, nent->n_name);
4808 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4809 for (elem = nent->n_aliases; elem && *elem; elem++) {
4810 sv_catpv(sv, *elem);
4812 sv_catpvn(sv, " ", 1);
4814 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4815 sv_setiv(sv, (IV)nent->n_addrtype);
4816 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4817 sv_setiv(sv, (IV)nent->n_net);
4822 DIE(aTHX_ PL_no_sock_func, "getnetent");
4828 #ifdef HAS_GETPROTOBYNAME
4829 return pp_gprotoent();
4831 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4837 #ifdef HAS_GETPROTOBYNUMBER
4838 return pp_gprotoent();
4840 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4846 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4848 I32 which = PL_op->op_type;
4849 register char **elem;
4851 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4852 struct protoent *getprotobyname(Netdb_name_t);
4853 struct protoent *getprotobynumber(int);
4854 struct protoent *getprotoent(void);
4856 struct protoent *pent;
4859 if (which == OP_GPBYNAME) {
4860 #ifdef HAS_GETPROTOBYNAME
4861 char* name = POPpbytex;
4862 pent = PerlSock_getprotobyname(name);
4864 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4867 else if (which == OP_GPBYNUMBER) {
4868 #ifdef HAS_GETPROTOBYNUMBER
4870 pent = PerlSock_getprotobynumber(number);
4872 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4876 #ifdef HAS_GETPROTOENT
4877 pent = PerlSock_getprotoent();
4879 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4883 if (GIMME != G_ARRAY) {
4884 PUSHs(sv = sv_newmortal());
4886 if (which == OP_GPBYNAME)
4887 sv_setiv(sv, (IV)pent->p_proto);
4889 sv_setpv(sv, pent->p_name);
4895 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4896 sv_setpv(sv, pent->p_name);
4897 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4898 for (elem = pent->p_aliases; elem && *elem; elem++) {
4899 sv_catpv(sv, *elem);
4901 sv_catpvn(sv, " ", 1);
4903 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4904 sv_setiv(sv, (IV)pent->p_proto);
4909 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4915 #ifdef HAS_GETSERVBYNAME
4916 return pp_gservent();
4918 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4924 #ifdef HAS_GETSERVBYPORT
4925 return pp_gservent();
4927 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4933 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4935 I32 which = PL_op->op_type;
4936 register char **elem;
4938 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4939 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4940 struct servent *getservbyport(int, Netdb_name_t);
4941 struct servent *getservent(void);
4943 struct servent *sent;
4946 if (which == OP_GSBYNAME) {
4947 #ifdef HAS_GETSERVBYNAME
4948 char *proto = POPpbytex;
4949 char *name = POPpbytex;
4951 if (proto && !*proto)
4954 sent = PerlSock_getservbyname(name, proto);
4956 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4959 else if (which == OP_GSBYPORT) {
4960 #ifdef HAS_GETSERVBYPORT
4961 char *proto = POPpbytex;
4962 unsigned short port = (unsigned short)POPu;
4964 if (proto && !*proto)
4968 port = PerlSock_htons(port);
4970 sent = PerlSock_getservbyport(port, proto);
4972 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4976 #ifdef HAS_GETSERVENT
4977 sent = PerlSock_getservent();
4979 DIE(aTHX_ PL_no_sock_func, "getservent");
4983 if (GIMME != G_ARRAY) {
4984 PUSHs(sv = sv_newmortal());
4986 if (which == OP_GSBYNAME) {
4988 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4990 sv_setiv(sv, (IV)(sent->s_port));
4994 sv_setpv(sv, sent->s_name);
5000 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5001 sv_setpv(sv, sent->s_name);
5002 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5003 for (elem = sent->s_aliases; elem && *elem; elem++) {
5004 sv_catpv(sv, *elem);
5006 sv_catpvn(sv, " ", 1);
5008 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5010 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5012 sv_setiv(sv, (IV)(sent->s_port));
5014 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5015 sv_setpv(sv, sent->s_proto);
5020 DIE(aTHX_ PL_no_sock_func, "getservent");
5026 #ifdef HAS_SETHOSTENT
5028 PerlSock_sethostent(TOPi);
5031 DIE(aTHX_ PL_no_sock_func, "sethostent");
5037 #ifdef HAS_SETNETENT
5039 PerlSock_setnetent(TOPi);
5042 DIE(aTHX_ PL_no_sock_func, "setnetent");
5048 #ifdef HAS_SETPROTOENT
5050 PerlSock_setprotoent(TOPi);
5053 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5059 #ifdef HAS_SETSERVENT
5061 PerlSock_setservent(TOPi);
5064 DIE(aTHX_ PL_no_sock_func, "setservent");
5070 #ifdef HAS_ENDHOSTENT
5072 PerlSock_endhostent();
5076 DIE(aTHX_ PL_no_sock_func, "endhostent");
5082 #ifdef HAS_ENDNETENT
5084 PerlSock_endnetent();
5088 DIE(aTHX_ PL_no_sock_func, "endnetent");
5094 #ifdef HAS_ENDPROTOENT
5096 PerlSock_endprotoent();
5100 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5106 #ifdef HAS_ENDSERVENT
5108 PerlSock_endservent();
5112 DIE(aTHX_ PL_no_sock_func, "endservent");
5121 DIE(aTHX_ PL_no_func, "getpwnam");
5130 DIE(aTHX_ PL_no_func, "getpwuid");
5138 I32 which = PL_op->op_type;
5141 struct passwd *pwent = NULL;
5143 * We currently support only the SysV getsp* shadow password interface.
5144 * The interface is declared in <shadow.h> and often one needs to link
5145 * with -lsecurity or some such.
5146 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5149 * AIX getpwnam() is clever enough to return the encrypted password
5150 * only if the caller (euid?) is root.
5152 * There are at least two other shadow password APIs. Many platforms
5153 * seem to contain more than one interface for accessing the shadow
5154 * password databases, possibly for compatibility reasons.
5155 * The getsp*() is by far he simplest one, the other two interfaces
5156 * are much more complicated, but also very similar to each other.
5161 * struct pr_passwd *getprpw*();
5162 * The password is in
5163 * char getprpw*(...).ufld.fd_encrypt[]
5164 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5169 * struct es_passwd *getespw*();
5170 * The password is in
5171 * char *(getespw*(...).ufld.fd_encrypt)
5172 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5174 * Mention I_PROT here so that Configure probes for it.
5176 * In HP-UX for getprpw*() the manual page claims that one should include
5177 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5178 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5179 * and pp_sys.c already includes <shadow.h> if there is such.
5181 * Note that <sys/security.h> is already probed for, but currently
5182 * it is only included in special cases.
5184 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5185 * be preferred interface, even though also the getprpw*() interface
5186 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5187 * One also needs to call set_auth_parameters() in main() before
5188 * doing anything else, whether one is using getespw*() or getprpw*().
5190 * Note that accessing the shadow databases can be magnitudes
5191 * slower than accessing the standard databases.
5199 char* name = POPpbytex;
5200 pwent = getpwnam(name);
5206 pwent = getpwuid(uid);
5210 # ifdef HAS_GETPWENT
5212 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5213 if (pwent) pwent = getpwnam(pwent->pw_name);
5216 DIE(aTHX_ PL_no_func, "getpwent");
5222 if (GIMME != G_ARRAY) {
5223 PUSHs(sv = sv_newmortal());
5225 if (which == OP_GPWNAM)
5226 # if Uid_t_sign <= 0
5227 sv_setiv(sv, (IV)pwent->pw_uid);
5229 sv_setuv(sv, (UV)pwent->pw_uid);
5232 sv_setpv(sv, pwent->pw_name);
5238 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5239 sv_setpv(sv, pwent->pw_name);
5241 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5243 /* If we have getspnam(), we try to dig up the shadow
5244 * password. If we are underprivileged, the shadow
5245 * interface will set the errno to EACCES or similar,
5246 * and return a null pointer. If this happens, we will
5247 * use the dummy password (usually "*" or "x") from the
5248 * standard password database.
5250 * In theory we could skip the shadow call completely
5251 * if euid != 0 but in practice we cannot know which
5252 * security measures are guarding the shadow databases
5253 * on a random platform.
5255 * Resist the urge to use additional shadow interfaces.
5256 * Divert the urge to writing an extension instead.
5259 # ifdef HAS_GETSPNAM
5261 struct spwd *spwent;
5262 int saverrno; /* Save and restore errno so that
5263 * underprivileged attempts seem
5264 * to have never made the unsccessful
5265 * attempt to retrieve the shadow password. */
5268 spwent = getspnam(pwent->pw_name);
5270 if (spwent && spwent->sp_pwdp)
5271 sv_setpv(sv, spwent->sp_pwdp);
5275 if (!SvPOK(sv)) /* Use the standard password, then. */
5276 sv_setpv(sv, pwent->pw_passwd);
5279 # ifndef INCOMPLETE_TAINTS
5280 /* passwd is tainted because user himself can diddle with it.
5281 * admittedly not much and in a very limited way, but nevertheless. */
5285 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5286 # if Uid_t_sign <= 0
5287 sv_setiv(sv, (IV)pwent->pw_uid);
5289 sv_setuv(sv, (UV)pwent->pw_uid);
5292 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5293 # if Uid_t_sign <= 0
5294 sv_setiv(sv, (IV)pwent->pw_gid);
5296 sv_setuv(sv, (UV)pwent->pw_gid);
5298 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5299 * because of the poor interface of the Perl getpw*(),
5300 * not because there's some standard/convention saying so.
5301 * A better interface would have been to return a hash,
5302 * but we are accursed by our history, alas. --jhi. */
5303 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5305 sv_setiv(sv, (IV)pwent->pw_change);
5308 sv_setiv(sv, (IV)pwent->pw_quota);
5311 sv_setpv(sv, pwent->pw_age);
5316 /* pw_class and pw_comment are mutually exclusive--.
5317 * see the above note for pw_change, pw_quota, and pw_age. */
5318 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5320 sv_setpv(sv, pwent->pw_class);
5323 sv_setpv(sv, pwent->pw_comment);
5327 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5329 sv_setpv(sv, pwent->pw_gecos);
5331 # ifndef INCOMPLETE_TAINTS
5332 /* pw_gecos is tainted because user himself can diddle with it. */
5336 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5337 sv_setpv(sv, pwent->pw_dir);
5339 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5340 sv_setpv(sv, pwent->pw_shell);
5341 # ifndef INCOMPLETE_TAINTS
5342 /* pw_shell is tainted because user himself can diddle with it. */
5347 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5348 sv_setiv(sv, (IV)pwent->pw_expire);
5353 DIE(aTHX_ PL_no_func, "getpwent");
5359 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5364 DIE(aTHX_ PL_no_func, "setpwent");
5370 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5375 DIE(aTHX_ PL_no_func, "endpwent");
5384 DIE(aTHX_ PL_no_func, "getgrnam");
5393 DIE(aTHX_ PL_no_func, "getgrgid");
5401 I32 which = PL_op->op_type;
5402 register char **elem;
5404 struct group *grent;
5407 if (which == OP_GGRNAM) {
5408 char* name = POPpbytex;
5409 grent = (struct group *)getgrnam(name);
5411 else if (which == OP_GGRGID) {
5413 grent = (struct group *)getgrgid(gid);
5417 grent = (struct group *)getgrent();
5419 DIE(aTHX_ PL_no_func, "getgrent");
5423 if (GIMME != G_ARRAY) {
5424 PUSHs(sv = sv_newmortal());
5426 if (which == OP_GGRNAM)
5427 sv_setiv(sv, (IV)grent->gr_gid);
5429 sv_setpv(sv, grent->gr_name);
5435 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5436 sv_setpv(sv, grent->gr_name);
5438 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5440 sv_setpv(sv, grent->gr_passwd);
5443 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5444 sv_setiv(sv, (IV)grent->gr_gid);
5446 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5447 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5448 /* In UNICOS/mk (_CRAYMPP) the multithreading
5449 * versions (getgrnam_r, getgrgid_r)
5450 * seem to return an illegal pointer
5451 * as the group members list, gr_mem.
5452 * getgrent() doesn't even have a _r version
5453 * but the gr_mem is poisonous anyway.
5454 * So yes, you cannot get the list of group
5455 * members if building multithreaded in UNICOS/mk. */
5456 for (elem = grent->gr_mem; elem && *elem; elem++) {
5457 sv_catpv(sv, *elem);
5459 sv_catpvn(sv, " ", 1);
5466 DIE(aTHX_ PL_no_func, "getgrent");
5472 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5477 DIE(aTHX_ PL_no_func, "setgrent");
5483 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5488 DIE(aTHX_ PL_no_func, "endgrent");
5498 if (!(tmps = PerlProc_getlogin()))
5500 PUSHp(tmps, strlen(tmps));
5503 DIE(aTHX_ PL_no_func, "getlogin");
5507 /* Miscellaneous. */
5512 dSP; dMARK; dORIGMARK; dTARGET;
5513 register I32 items = SP - MARK;
5514 unsigned long a[20];
5520 while (++MARK <= SP) {
5521 if (SvTAINTED(*MARK)) {
5527 TAINT_PROPER("syscall");
5530 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5531 * or where sizeof(long) != sizeof(char*). But such machines will
5532 * not likely have syscall implemented either, so who cares?
5534 while (++MARK <= SP) {
5535 if (SvNIOK(*MARK) || !i)
5536 a[i++] = SvIV(*MARK);
5537 else if (*MARK == &PL_sv_undef)
5540 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5546 DIE(aTHX_ "Too many args to syscall");
5548 DIE(aTHX_ "Too few args to syscall");
5550 retval = syscall(a[0]);
5553 retval = syscall(a[0],a[1]);
5556 retval = syscall(a[0],a[1],a[2]);
5559 retval = syscall(a[0],a[1],a[2],a[3]);
5562 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5565 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5568 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5571 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5575 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5578 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5581 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5585 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5589 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5593 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5594 a[10],a[11],a[12],a[13]);
5596 #endif /* atarist */
5602 DIE(aTHX_ PL_no_func, "syscall");
5606 #ifdef FCNTL_EMULATE_FLOCK
5608 /* XXX Emulate flock() with fcntl().
5609 What's really needed is a good file locking module.
5613 fcntl_emulate_flock(int fd, int operation)
5617 switch (operation & ~LOCK_NB) {
5619 flock.l_type = F_RDLCK;
5622 flock.l_type = F_WRLCK;
5625 flock.l_type = F_UNLCK;
5631 flock.l_whence = SEEK_SET;
5632 flock.l_start = flock.l_len = (Off_t)0;
5634 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5637 #endif /* FCNTL_EMULATE_FLOCK */
5639 #ifdef LOCKF_EMULATE_FLOCK
5641 /* XXX Emulate flock() with lockf(). This is just to increase
5642 portability of scripts. The calls are not completely
5643 interchangeable. What's really needed is a good file
5647 /* The lockf() constants might have been defined in <unistd.h>.
5648 Unfortunately, <unistd.h> causes troubles on some mixed
5649 (BSD/POSIX) systems, such as SunOS 4.1.3.
5651 Further, the lockf() constants aren't POSIX, so they might not be
5652 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5653 just stick in the SVID values and be done with it. Sigh.
5657 # define F_ULOCK 0 /* Unlock a previously locked region */
5660 # define F_LOCK 1 /* Lock a region for exclusive use */
5663 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5666 # define F_TEST 3 /* Test a region for other processes locks */
5670 lockf_emulate_flock(int fd, int operation)
5676 /* flock locks entire file so for lockf we need to do the same */
5678 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5679 if (pos > 0) /* is seekable and needs to be repositioned */
5680 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5681 pos = -1; /* seek failed, so don't seek back afterwards */
5684 switch (operation) {
5686 /* LOCK_SH - get a shared lock */
5688 /* LOCK_EX - get an exclusive lock */
5690 i = lockf (fd, F_LOCK, 0);
5693 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5694 case LOCK_SH|LOCK_NB:
5695 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5696 case LOCK_EX|LOCK_NB:
5697 i = lockf (fd, F_TLOCK, 0);
5699 if ((errno == EAGAIN) || (errno == EACCES))
5700 errno = EWOULDBLOCK;
5703 /* LOCK_UN - unlock (non-blocking is a no-op) */
5705 case LOCK_UN|LOCK_NB:
5706 i = lockf (fd, F_ULOCK, 0);
5709 /* Default - can't decipher operation */
5716 if (pos > 0) /* need to restore position of the handle */
5717 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5722 #endif /* LOCKF_EMULATE_FLOCK */