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)) {
745 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
746 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
747 mode_from_discipline(discp),
748 (discp) ? SvPV_nolen(discp) : Nullch)) {
770 I32 markoff = MARK - PL_stack_base;
772 int how = PERL_MAGIC_tied;
776 switch(SvTYPE(varsv)) {
778 methname = "TIEHASH";
779 HvEITER((HV *)varsv) = Null(HE *);
782 methname = "TIEARRAY";
785 #ifdef GV_UNIQUE_CHECK
786 if (GvUNIQUE((GV*)varsv)) {
787 Perl_croak(aTHX_ "Attempt to tie unique GV");
790 methname = "TIEHANDLE";
791 how = PERL_MAGIC_tiedscalar;
792 /* For tied filehandles, we apply tiedscalar magic to the IO
793 slot of the GP rather than the GV itself. AMS 20010812 */
795 GvIOp(varsv) = newIO();
796 varsv = (SV *)GvIOp(varsv);
799 methname = "TIESCALAR";
800 how = PERL_MAGIC_tiedscalar;
804 if (sv_isobject(*MARK)) {
806 PUSHSTACKi(PERLSI_MAGIC);
808 EXTEND(SP,(I32)items);
812 call_method(methname, G_SCALAR);
815 /* Not clear why we don't call call_method here too.
816 * perhaps to get different error message ?
818 stash = gv_stashsv(*MARK, FALSE);
819 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
820 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
824 PUSHSTACKi(PERLSI_MAGIC);
826 EXTEND(SP,(I32)items);
830 call_sv((SV*)GvCV(gv), G_SCALAR);
836 if (sv_isobject(sv)) {
837 sv_unmagic(varsv, how);
838 /* Croak if a self-tie on an aggregate is attempted. */
839 if (varsv == SvRV(sv) &&
840 (SvTYPE(varsv) == SVt_PVAV ||
841 SvTYPE(varsv) == SVt_PVHV))
843 "Self-ties of arrays and hashes are not supported");
844 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
847 SP = PL_stack_base + markoff;
857 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
858 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
860 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
863 if ((mg = SvTIED_mg(sv, how))) {
864 SV *obj = SvRV(mg->mg_obj);
868 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
869 isGV(gv) && (cv = GvCV(gv))) {
871 XPUSHs(SvTIED_obj((SV*)gv, mg));
872 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
875 call_sv((SV *)cv, G_VOID);
879 else if (ckWARN(WARN_UNTIE)) {
880 if (mg && SvREFCNT(obj) > 1)
881 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
882 "untie attempted while %"UVuf" inner references still exist",
883 (UV)SvREFCNT(obj) - 1 ) ;
887 sv_unmagic(sv, how) ;
896 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
897 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
899 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
902 if ((mg = SvTIED_mg(sv, how))) {
903 SV *osv = SvTIED_obj(sv, mg);
904 if (osv == mg->mg_obj)
905 osv = sv_mortalcopy(osv);
923 sv = sv_mortalcopy(&PL_sv_no);
924 sv_setpv(sv, "AnyDBM_File");
925 stash = gv_stashsv(sv, FALSE);
926 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
928 require_pv("AnyDBM_File.pm");
930 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
931 DIE(aTHX_ "No dbm on this machine");
941 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
943 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
946 call_sv((SV*)GvCV(gv), G_SCALAR);
949 if (!sv_isobject(TOPs)) {
954 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
957 call_sv((SV*)GvCV(gv), G_SCALAR);
961 if (sv_isobject(TOPs)) {
962 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
963 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
985 struct timeval timebuf;
986 struct timeval *tbuf = &timebuf;
990 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
995 # if BYTEORDER & 0xf0000
996 # define ORDERBYTE (0x88888888 - BYTEORDER)
998 # define ORDERBYTE (0x4444 - BYTEORDER)
1004 for (i = 1; i <= 3; i++) {
1012 /* little endians can use vecs directly */
1013 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1020 masksize = NFDBITS / NBBY;
1022 masksize = sizeof(long); /* documented int, everyone seems to use long */
1024 Zero(&fd_sets[0], 4, char*);
1027 # if SELECT_MIN_BITS > 1
1028 /* If SELECT_MIN_BITS is greater than one we most probably will want
1029 * to align the sizes with SELECT_MIN_BITS/8 because for example
1030 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1031 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1032 * on (sets/tests/clears bits) is 32 bits. */
1033 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1035 growsize = sizeof(fd_set);
1043 timebuf.tv_sec = (long)value;
1044 value -= (NV)timebuf.tv_sec;
1045 timebuf.tv_usec = (long)(value * 1000000.0);
1048 tbuf = Null(struct timeval*);
1050 for (i = 1; i <= 3; i++) {
1056 else if (!SvPOK(sv))
1057 SvPV_force(sv,n_a); /* force string conversion */
1060 Sv_Grow(sv, growsize);
1064 while (++j <= growsize) {
1068 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1070 New(403, fd_sets[i], growsize, char);
1071 for (offset = 0; offset < growsize; offset += masksize) {
1072 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1073 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1076 fd_sets[i] = SvPVX(sv);
1080 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1081 /* Can't make just the (void*) conditional because that would be
1082 * cpp #if within cpp macro, and not all compilers like that. */
1083 nfound = PerlSock_select(
1085 (Select_fd_set_t) fd_sets[1],
1086 (Select_fd_set_t) fd_sets[2],
1087 (Select_fd_set_t) fd_sets[3],
1088 (void*) tbuf); /* Workaround for compiler bug. */
1090 nfound = PerlSock_select(
1092 (Select_fd_set_t) fd_sets[1],
1093 (Select_fd_set_t) fd_sets[2],
1094 (Select_fd_set_t) fd_sets[3],
1097 for (i = 1; i <= 3; i++) {
1100 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1102 for (offset = 0; offset < growsize; offset += masksize) {
1103 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1104 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1106 Safefree(fd_sets[i]);
1113 if (GIMME == G_ARRAY && tbuf) {
1114 value = (NV)(timebuf.tv_sec) +
1115 (NV)(timebuf.tv_usec) / 1000000.0;
1116 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1117 sv_setnv(sv, value);
1121 DIE(aTHX_ "select not implemented");
1126 Perl_setdefout(pTHX_ GV *gv)
1129 (void)SvREFCNT_inc(gv);
1131 SvREFCNT_dec(PL_defoutgv);
1138 GV *newdefout, *egv;
1141 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1143 egv = GvEGV(PL_defoutgv);
1148 XPUSHs(&PL_sv_undef);
1150 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1151 if (gvp && *gvp == egv) {
1152 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1156 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1161 if (!GvIO(newdefout))
1162 gv_IOadd(newdefout);
1163 setdefout(newdefout);
1181 if (gv && (io = GvIO(gv))
1182 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1184 I32 gimme = GIMME_V;
1186 XPUSHs(SvTIED_obj((SV*)io, mg));
1189 call_method("GETC", gimme);
1192 if (gimme == G_SCALAR)
1193 SvSetMagicSV_nosteal(TARG, TOPs);
1196 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1197 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1198 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1199 report_evil_fh(gv, io, PL_op->op_type);
1200 SETERRNO(EBADF,RMS_IFI);
1204 sv_setpv(TARG, " ");
1205 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1206 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1207 /* Find out how many bytes the char needs */
1208 Size_t len = UTF8SKIP(SvPVX(TARG));
1211 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1212 SvCUR_set(TARG,1+len);
1222 return pp_sysread();
1226 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1228 register PERL_CONTEXT *cx;
1229 I32 gimme = GIMME_V;
1235 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1237 PAD_SET_CUR(CvPADLIST(cv), 1);
1239 setdefout(gv); /* locally select filehandle so $% et al work */
1272 SV *tmpsv = sv_newmortal();
1273 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1274 name = SvPV_nolen(tmpsv);
1277 DIE(aTHX_ "Undefined format \"%s\" called", name);
1278 DIE(aTHX_ "Not a format reference");
1281 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1283 IoFLAGS(io) &= ~IOf_DIDTOP;
1284 return doform(cv,gv,PL_op->op_next);
1290 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1291 register IO *io = GvIOp(gv);
1292 PerlIO *ofp = IoOFP(io);
1296 register PERL_CONTEXT *cx;
1298 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1299 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1302 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1303 PL_formtarget != PL_toptarget)
1307 if (!IoTOP_GV(io)) {
1311 if (!IoTOP_NAME(io)) {
1312 if (!IoFMT_NAME(io))
1313 IoFMT_NAME(io) = savepv(GvNAME(gv));
1314 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1315 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1316 if ((topgv && GvFORM(topgv)) ||
1317 !gv_fetchpv("top",FALSE,SVt_PVFM))
1318 IoTOP_NAME(io) = savepv(SvPVX(topname));
1320 IoTOP_NAME(io) = savepv("top");
1322 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1323 if (!topgv || !GvFORM(topgv)) {
1324 IoLINES_LEFT(io) = 100000000;
1327 IoTOP_GV(io) = topgv;
1329 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1330 I32 lines = IoLINES_LEFT(io);
1331 char *s = SvPVX(PL_formtarget);
1332 if (lines <= 0) /* Yow, header didn't even fit!!! */
1334 while (lines-- > 0) {
1335 s = strchr(s, '\n');
1341 STRLEN save = SvCUR(PL_formtarget);
1342 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1343 do_print(PL_formtarget, ofp);
1344 SvCUR_set(PL_formtarget, save);
1345 sv_chop(PL_formtarget, s);
1346 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1349 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1350 do_print(PL_formfeed, ofp);
1351 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1353 PL_formtarget = PL_toptarget;
1354 IoFLAGS(io) |= IOf_DIDTOP;
1357 DIE(aTHX_ "bad top format reference");
1362 SV *sv = sv_newmortal();
1363 gv_efullname4(sv, fgv, Nullch, FALSE);
1364 name = SvPV_nolen(sv);
1367 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1370 DIE(aTHX_ "Undefined top format called");
1374 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1375 return doform(cv,gv,PL_op);
1379 POPBLOCK(cx,PL_curpm);
1385 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1387 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1388 else if (ckWARN(WARN_CLOSED))
1389 report_evil_fh(gv, io, PL_op->op_type);
1394 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1395 if (ckWARN(WARN_IO))
1396 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1398 if (!do_print(PL_formtarget, fp))
1401 FmLINES(PL_formtarget) = 0;
1402 SvCUR_set(PL_formtarget, 0);
1403 *SvEND(PL_formtarget) = '\0';
1404 if (IoFLAGS(io) & IOf_FLUSH)
1405 (void)PerlIO_flush(fp);
1410 PL_formtarget = PL_bodytarget;
1412 return pop_return();
1417 dSP; dMARK; dORIGMARK;
1424 if (PL_op->op_flags & OPf_STACKED)
1429 if (gv && (io = GvIO(gv))
1430 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1432 if (MARK == ORIGMARK) {
1435 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1439 *MARK = SvTIED_obj((SV*)io, mg);
1442 call_method("PRINTF", G_SCALAR);
1445 MARK = ORIGMARK + 1;
1452 if (!(io = GvIO(gv))) {
1453 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1454 report_evil_fh(gv, io, PL_op->op_type);
1455 SETERRNO(EBADF,RMS_IFI);
1458 else if (!(fp = IoOFP(io))) {
1459 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1461 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1462 else if (ckWARN(WARN_CLOSED))
1463 report_evil_fh(gv, io, PL_op->op_type);
1465 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1469 do_sprintf(sv, SP - MARK, MARK + 1);
1470 if (!do_print(sv, fp))
1473 if (IoFLAGS(io) & IOf_FLUSH)
1474 if (PerlIO_flush(fp) == EOF)
1485 PUSHs(&PL_sv_undef);
1506 /* Need TIEHANDLE method ? */
1508 tmps = SvPV(sv, len);
1509 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1510 IoLINES(GvIOp(gv)) = 0;
1514 PUSHs(&PL_sv_undef);
1521 dSP; dMARK; dORIGMARK; dTARGET;
1528 Sock_size_t bufsize;
1535 bool charstart = FALSE;
1536 STRLEN charskip = 0;
1540 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1541 && gv && (io = GvIO(gv))
1542 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1547 *MARK = SvTIED_obj((SV*)io, mg);
1549 call_method("READ", G_SCALAR);
1562 sv_setpvn(bufsv, "", 0);
1563 length = SvIVx(*++MARK);
1566 offset = SvIVx(*++MARK);
1570 if (!io || !IoIFP(io)) {
1571 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1572 report_evil_fh(gv, io, PL_op->op_type);
1573 SETERRNO(EBADF,RMS_IFI);
1576 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1577 buffer = SvPVutf8_force(bufsv, blen);
1578 /* UTF8 may not have been set if they are all low bytes */
1582 buffer = SvPV_force(bufsv, blen);
1585 DIE(aTHX_ "Negative length");
1593 if (PL_op->op_type == OP_RECV) {
1594 char namebuf[MAXPATHLEN];
1595 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1596 bufsize = sizeof (struct sockaddr_in);
1598 bufsize = sizeof namebuf;
1600 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1604 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1605 /* 'offset' means 'flags' here */
1606 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1607 (struct sockaddr *)namebuf, &bufsize);
1611 /* Bogus return without padding */
1612 bufsize = sizeof (struct sockaddr_in);
1614 SvCUR_set(bufsv, count);
1615 *SvEND(bufsv) = '\0';
1616 (void)SvPOK_only(bufsv);
1620 /* This should not be marked tainted if the fp is marked clean */
1621 if (!(IoFLAGS(io) & IOf_UNTAINT))
1622 SvTAINTED_on(bufsv);
1624 sv_setpvn(TARG, namebuf, bufsize);
1629 if (PL_op->op_type == OP_RECV)
1630 DIE(aTHX_ PL_no_sock_func, "recv");
1632 if (DO_UTF8(bufsv)) {
1633 /* offset adjust in characters not bytes */
1634 blen = sv_len_utf8(bufsv);
1637 if (-offset > (int)blen)
1638 DIE(aTHX_ "Offset outside string");
1641 if (DO_UTF8(bufsv)) {
1642 /* convert offset-as-chars to offset-as-bytes */
1643 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1646 bufsize = SvCUR(bufsv);
1647 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1648 if (offset > bufsize) { /* Zero any newly allocated space */
1649 Zero(buffer+bufsize, offset-bufsize, char);
1651 buffer = buffer + offset;
1653 if (PL_op->op_type == OP_SYSREAD) {
1654 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1655 if (IoTYPE(io) == IoTYPE_SOCKET) {
1656 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1662 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1667 #ifdef HAS_SOCKET__bad_code_maybe
1668 if (IoTYPE(io) == IoTYPE_SOCKET) {
1669 char namebuf[MAXPATHLEN];
1670 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1671 bufsize = sizeof (struct sockaddr_in);
1673 bufsize = sizeof namebuf;
1675 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1676 (struct sockaddr *)namebuf, &bufsize);
1681 count = PerlIO_read(IoIFP(io), buffer, length);
1682 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1683 if (count == 0 && PerlIO_error(IoIFP(io)))
1687 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1688 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1691 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1692 *SvEND(bufsv) = '\0';
1693 (void)SvPOK_only(bufsv);
1694 if (fp_utf8 && !IN_BYTES) {
1695 /* Look at utf8 we got back and count the characters */
1696 char *bend = buffer + count;
1697 while (buffer < bend) {
1699 skip = UTF8SKIP(buffer);
1702 if (buffer - charskip + skip > bend) {
1703 /* partial character - try for rest of it */
1704 length = skip - (bend-buffer);
1705 offset = bend - SvPVX(bufsv);
1717 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1718 provided amount read (count) was what was requested (length)
1720 if (got < wanted && count == length) {
1721 length = wanted - got;
1722 offset = bend - SvPVX(bufsv);
1725 /* return value is character count */
1730 /* This should not be marked tainted if the fp is marked clean */
1731 if (!(IoFLAGS(io) & IOf_UNTAINT))
1732 SvTAINTED_on(bufsv);
1745 int items = (SP - PL_stack_base) - TOPMARK;
1749 sv = sv_2mortal(newSViv(sv_len(*SP)));
1758 dSP; dMARK; dORIGMARK; dTARGET;
1769 if (PL_op->op_type == OP_SYSWRITE
1770 && gv && (io = GvIO(gv))
1771 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1776 *MARK = SvTIED_obj((SV*)io, mg);
1778 call_method("WRITE", G_SCALAR);
1789 #if Size_t_size > IVSIZE
1790 length = (Size_t)SvNVx(*++MARK);
1792 length = (Size_t)SvIVx(*++MARK);
1794 if ((SSize_t)length < 0)
1795 DIE(aTHX_ "Negative length");
1798 if (!io || !IoIFP(io)) {
1800 if (ckWARN(WARN_CLOSED))
1801 report_evil_fh(gv, io, PL_op->op_type);
1802 SETERRNO(EBADF,RMS_IFI);
1806 if (PerlIO_isutf8(IoIFP(io))) {
1807 buffer = SvPVutf8(bufsv, blen);
1811 sv_utf8_downgrade(bufsv, FALSE);
1812 buffer = SvPV(bufsv, blen);
1815 if (PL_op->op_type == OP_SYSWRITE) {
1817 if (DO_UTF8(bufsv)) {
1818 /* length and offset are in chars */
1819 blen = sv_len_utf8(bufsv);
1822 offset = SvIVx(*++MARK);
1824 if (-offset > (IV)blen)
1825 DIE(aTHX_ "Offset outside string");
1827 } else if (offset >= (IV)blen && blen > 0)
1828 DIE(aTHX_ "Offset outside string");
1831 if (length > blen - offset)
1832 length = blen - offset;
1833 if (DO_UTF8(bufsv)) {
1834 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1835 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1838 buffer = buffer+offset;
1840 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1841 if (IoTYPE(io) == IoTYPE_SOCKET) {
1842 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1848 /* See the note at doio.c:do_print about filesize limits. --jhi */
1849 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1854 else if (SP > MARK) {
1857 sockbuf = SvPVx(*++MARK, mlen);
1858 /* length is really flags */
1859 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1860 length, (struct sockaddr *)sockbuf, mlen);
1863 /* length is really flags */
1864 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1867 DIE(aTHX_ PL_no_sock_func, "send");
1873 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1874 #if Size_t_size > IVSIZE
1888 return pp_sysread();
1899 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1901 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1903 if (io && !IoIFP(io)) {
1904 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1906 IoFLAGS(io) &= ~IOf_START;
1907 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1908 sv_setpvn(GvSV(gv), "-", 1);
1909 SvSETMAGIC(GvSV(gv));
1911 else if (!nextargv(gv))
1916 gv = PL_last_in_gv; /* eof */
1919 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1921 if (gv && (io = GvIO(gv))
1922 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1925 XPUSHs(SvTIED_obj((SV*)io, mg));
1928 call_method("EOF", G_SCALAR);
1934 PUSHs(boolSV(!gv || do_eof(gv)));
1948 gv = PL_last_in_gv = (GV*)POPs;
1950 if (gv && (io = GvIO(gv))
1951 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1954 XPUSHs(SvTIED_obj((SV*)io, mg));
1957 call_method("TELL", G_SCALAR);
1963 #if LSEEKSIZE > IVSIZE
1964 PUSHn( do_tell(gv) );
1966 PUSHi( do_tell(gv) );
1973 return pp_sysseek();
1982 #if LSEEKSIZE > IVSIZE
1983 Off_t offset = (Off_t)SvNVx(POPs);
1985 Off_t offset = (Off_t)SvIVx(POPs);
1989 gv = PL_last_in_gv = (GV*)POPs;
1991 if (gv && (io = GvIO(gv))
1992 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1995 XPUSHs(SvTIED_obj((SV*)io, mg));
1996 #if LSEEKSIZE > IVSIZE
1997 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1999 XPUSHs(sv_2mortal(newSViv(offset)));
2001 XPUSHs(sv_2mortal(newSViv(whence)));
2004 call_method("SEEK", G_SCALAR);
2010 if (PL_op->op_type == OP_SEEK)
2011 PUSHs(boolSV(do_seek(gv, offset, whence)));
2013 Off_t sought = do_sysseek(gv, offset, whence);
2015 PUSHs(&PL_sv_undef);
2018 #if LSEEKSIZE > IVSIZE
2023 : newSVpvn(zero_but_true, ZBTLEN);
2024 PUSHs(sv_2mortal(sv));
2033 /* There seems to be no consensus on the length type of truncate()
2034 * and ftruncate(), both off_t and size_t have supporters. In
2035 * general one would think that when using large files, off_t is
2036 * at least as wide as size_t, so using an off_t should be okay. */
2037 /* XXX Configure probe for the length type of *truncate() needed XXX */
2040 #if Off_t_size > IVSIZE
2045 /* Checking for length < 0 is problematic as the type might or
2046 * might not be signed: if it is not, clever compilers will moan. */
2047 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2049 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2056 if (PL_op->op_flags & OPf_SPECIAL) {
2057 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2066 TAINT_PROPER("truncate");
2067 if (!(fp = IoIFP(io))) {
2073 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2075 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2085 if (SvTYPE(sv) == SVt_PVGV) {
2086 tmpgv = (GV*)sv; /* *main::FRED for example */
2087 goto do_ftruncate_gv;
2089 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2090 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2091 goto do_ftruncate_gv;
2093 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2094 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2095 goto do_ftruncate_io;
2098 name = SvPV(sv, n_a);
2099 TAINT_PROPER("truncate");
2101 if (truncate(name, len) < 0)
2107 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2110 if (my_chsize(tmpfd, len) < 0)
2112 PerlLIO_close(tmpfd);
2121 SETERRNO(EBADF,RMS_IFI);
2125 DIE(aTHX_ "truncate not implemented");
2138 unsigned int func = POPu;
2139 int optype = PL_op->op_type;
2143 IO *io = gv ? GvIOn(gv) : 0;
2145 if (!io || !argsv || !IoIFP(io)) {
2146 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2147 report_evil_fh(gv, io, PL_op->op_type);
2148 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2152 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2155 s = SvPV_force(argsv, len);
2156 need = IOCPARM_LEN(func);
2158 s = Sv_Grow(argsv, need + 1);
2159 SvCUR_set(argsv, need);
2162 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2165 retval = SvIV(argsv);
2166 s = INT2PTR(char*,retval); /* ouch */
2169 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2171 if (optype == OP_IOCTL)
2173 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2175 DIE(aTHX_ "ioctl is not implemented");
2179 DIE(aTHX_ "fcntl is not implemented");
2181 #if defined(OS2) && defined(__EMX__)
2182 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2184 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2188 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2190 if (s[SvCUR(argsv)] != 17)
2191 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2193 s[SvCUR(argsv)] = 0; /* put our null back */
2194 SvSETMAGIC(argsv); /* Assume it has changed */
2203 PUSHp(zero_but_true, ZBTLEN);
2224 if (gv && (io = GvIO(gv)))
2231 (void)PerlIO_flush(fp);
2232 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2235 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2236 report_evil_fh(gv, io, PL_op->op_type);
2238 SETERRNO(EBADF,RMS_IFI);
2243 DIE(aTHX_ PL_no_func, "flock()");
2255 int protocol = POPi;
2261 io = gv ? GvIOn(gv) : NULL;
2264 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2265 report_evil_fh(gv, io, PL_op->op_type);
2267 do_close(gv, FALSE);
2268 SETERRNO(EBADF,LIB_INVARG);
2273 do_close(gv, FALSE);
2275 TAINT_PROPER("socket");
2276 fd = PerlSock_socket(domain, type, protocol);
2279 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2280 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2281 IoTYPE(io) = IoTYPE_SOCKET;
2282 if (!IoIFP(io) || !IoOFP(io)) {
2283 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2284 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2285 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2288 #if defined(HAS_FCNTL) && defined(F_SETFD)
2289 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2293 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2298 DIE(aTHX_ PL_no_sock_func, "socket");
2304 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2310 int protocol = POPi;
2317 io1 = gv1 ? GvIOn(gv1) : NULL;
2318 io2 = gv2 ? GvIOn(gv2) : NULL;
2319 if (!gv1 || !gv2 || !io1 || !io2) {
2320 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2322 report_evil_fh(gv1, io1, PL_op->op_type);
2324 report_evil_fh(gv1, io2, PL_op->op_type);
2327 do_close(gv1, FALSE);
2329 do_close(gv2, FALSE);
2334 do_close(gv1, FALSE);
2336 do_close(gv2, FALSE);
2338 TAINT_PROPER("socketpair");
2339 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2341 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2342 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2343 IoTYPE(io1) = IoTYPE_SOCKET;
2344 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2345 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2346 IoTYPE(io2) = IoTYPE_SOCKET;
2347 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2348 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2349 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2350 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2351 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2352 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2353 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2356 #if defined(HAS_FCNTL) && defined(F_SETFD)
2357 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2358 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2363 DIE(aTHX_ PL_no_sock_func, "socketpair");
2371 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2372 extern void GETPRIVMODE();
2373 extern void GETUSERMODE();
2378 register IO *io = GvIOn(gv);
2382 int mpeprivmode = 0;
2385 if (!io || !IoIFP(io))
2388 addr = SvPV(addrsv, len);
2389 TAINT_PROPER("bind");
2390 #ifdef MPE /* Deal with MPE bind() peculiarities */
2391 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2392 /* The address *MUST* stupidly be zero. */
2393 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2394 /* PRIV mode is required to bind() to ports < 1024. */
2395 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2396 ((struct sockaddr_in *)addr)->sin_port > 0) {
2397 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2402 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2403 (struct sockaddr *)addr, len) >= 0)
2406 #ifdef MPE /* Switch back to USER mode */
2417 if (ckWARN(WARN_CLOSED))
2418 report_evil_fh(gv, io, PL_op->op_type);
2419 SETERRNO(EBADF,SS_IVCHAN);
2422 DIE(aTHX_ PL_no_sock_func, "bind");
2433 register IO *io = GvIOn(gv);
2436 if (!io || !IoIFP(io))
2439 addr = SvPV(addrsv, len);
2440 TAINT_PROPER("connect");
2441 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2447 if (ckWARN(WARN_CLOSED))
2448 report_evil_fh(gv, io, PL_op->op_type);
2449 SETERRNO(EBADF,SS_IVCHAN);
2452 DIE(aTHX_ PL_no_sock_func, "connect");
2462 register IO *io = gv ? GvIOn(gv) : NULL;
2464 if (!gv || !io || !IoIFP(io))
2467 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2473 if (ckWARN(WARN_CLOSED))
2474 report_evil_fh(gv, io, PL_op->op_type);
2475 SETERRNO(EBADF,SS_IVCHAN);
2478 DIE(aTHX_ PL_no_sock_func, "listen");
2490 char namebuf[MAXPATHLEN];
2491 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2492 Sock_size_t len = sizeof (struct sockaddr_in);
2494 Sock_size_t len = sizeof namebuf;
2507 if (!gstio || !IoIFP(gstio))
2511 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2515 do_close(ngv, FALSE);
2516 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2517 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2518 IoTYPE(nstio) = IoTYPE_SOCKET;
2519 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2520 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2521 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2522 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2525 #if defined(HAS_FCNTL) && defined(F_SETFD)
2526 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2530 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2531 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2533 #ifdef __SCO_VERSION__
2534 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2537 PUSHp(namebuf, len);
2541 if (ckWARN(WARN_CLOSED))
2542 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2543 SETERRNO(EBADF,SS_IVCHAN);
2549 DIE(aTHX_ PL_no_sock_func, "accept");
2559 register IO *io = GvIOn(gv);
2561 if (!io || !IoIFP(io))
2564 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2568 if (ckWARN(WARN_CLOSED))
2569 report_evil_fh(gv, io, PL_op->op_type);
2570 SETERRNO(EBADF,SS_IVCHAN);
2573 DIE(aTHX_ PL_no_sock_func, "shutdown");
2580 return pp_ssockopt();
2582 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2590 int optype = PL_op->op_type;
2593 unsigned int optname;
2599 if (optype == OP_GSOCKOPT)
2600 sv = sv_2mortal(NEWSV(22, 257));
2603 optname = (unsigned int) POPi;
2604 lvl = (unsigned int) POPi;
2608 if (!io || !IoIFP(io))
2611 fd = PerlIO_fileno(IoIFP(io));
2615 (void)SvPOK_only(sv);
2619 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2634 aint = (int)SvIV(sv);
2638 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2647 if (ckWARN(WARN_CLOSED))
2648 report_evil_fh(gv, io, optype);
2649 SETERRNO(EBADF,SS_IVCHAN);
2654 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2661 return pp_getpeername();
2663 DIE(aTHX_ PL_no_sock_func, "getsockname");
2671 int optype = PL_op->op_type;
2675 register IO *io = GvIOn(gv);
2678 if (!io || !IoIFP(io))
2681 sv = sv_2mortal(NEWSV(22, 257));
2682 (void)SvPOK_only(sv);
2686 fd = PerlIO_fileno(IoIFP(io));
2688 case OP_GETSOCKNAME:
2689 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2692 case OP_GETPEERNAME:
2693 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2695 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2697 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";
2698 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2699 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2700 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2701 sizeof(u_short) + sizeof(struct in_addr))) {
2708 #ifdef BOGUS_GETNAME_RETURN
2709 /* Interactive Unix, getpeername() and getsockname()
2710 does not return valid namelen */
2711 if (len == BOGUS_GETNAME_RETURN)
2712 len = sizeof(struct sockaddr);
2720 if (ckWARN(WARN_CLOSED))
2721 report_evil_fh(gv, io, optype);
2722 SETERRNO(EBADF,SS_IVCHAN);
2727 DIE(aTHX_ PL_no_sock_func, "getpeername");
2746 if (PL_op->op_flags & OPf_REF) {
2748 if (PL_op->op_type == OP_LSTAT) {
2749 if (gv != PL_defgv) {
2750 if (ckWARN(WARN_IO))
2751 Perl_warner(aTHX_ packWARN(WARN_IO),
2752 "lstat() on filehandle %s", GvENAME(gv));
2753 } else if (PL_laststype != OP_LSTAT)
2754 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2758 if (gv != PL_defgv) {
2759 PL_laststype = OP_STAT;
2761 sv_setpv(PL_statname, "");
2762 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2763 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2765 if (PL_laststatval < 0) {
2766 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2767 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2773 if (SvTYPE(sv) == SVt_PVGV) {
2777 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2779 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2780 Perl_warner(aTHX_ packWARN(WARN_IO),
2781 "lstat() on filehandle %s", GvENAME(gv));
2784 sv_setpv(PL_statname, SvPV(sv,n_a));
2787 PL_laststype = PL_op->op_type;
2788 if (PL_op->op_type == OP_LSTAT)
2789 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2792 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2793 if (PL_laststatval < 0) {
2794 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2795 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2801 if (gimme != G_ARRAY) {
2802 if (gimme != G_VOID)
2803 XPUSHs(boolSV(max));
2809 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2810 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2811 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2812 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2813 #if Uid_t_size > IVSIZE
2814 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2816 # if Uid_t_sign <= 0
2817 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2819 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2822 #if Gid_t_size > IVSIZE
2823 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2825 # if Gid_t_sign <= 0
2826 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2828 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2831 #ifdef USE_STAT_RDEV
2832 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2834 PUSHs(sv_2mortal(newSVpvn("", 0)));
2836 #if Off_t_size > IVSIZE
2837 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2839 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2842 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2843 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2844 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2846 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2847 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2848 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2850 #ifdef USE_STAT_BLOCKS
2851 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2852 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2854 PUSHs(sv_2mortal(newSVpvn("", 0)));
2855 PUSHs(sv_2mortal(newSVpvn("", 0)));
2865 #if defined(HAS_ACCESS) && defined(R_OK)
2867 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2868 result = access(POPpx, R_OK);
2883 if (cando(S_IRUSR, 0, &PL_statcache))
2892 #if defined(HAS_ACCESS) && defined(W_OK)
2894 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2895 result = access(POPpx, W_OK);
2910 if (cando(S_IWUSR, 0, &PL_statcache))
2919 #if defined(HAS_ACCESS) && defined(X_OK)
2921 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2922 result = access(POPpx, X_OK);
2937 if (cando(S_IXUSR, 0, &PL_statcache))
2946 #ifdef PERL_EFF_ACCESS_R_OK
2948 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2949 result = PERL_EFF_ACCESS_R_OK(POPpx);
2964 if (cando(S_IRUSR, 1, &PL_statcache))
2973 #ifdef PERL_EFF_ACCESS_W_OK
2975 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2976 result = PERL_EFF_ACCESS_W_OK(POPpx);
2991 if (cando(S_IWUSR, 1, &PL_statcache))
3000 #ifdef PERL_EFF_ACCESS_X_OK
3002 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3003 result = PERL_EFF_ACCESS_X_OK(POPpx);
3018 if (cando(S_IXUSR, 1, &PL_statcache))
3025 I32 result = my_stat();
3034 return pp_ftrowned();
3039 I32 result = my_stat();
3043 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3051 I32 result = my_stat();
3055 if (PL_statcache.st_size == 0)
3062 I32 result = my_stat();
3066 #if Off_t_size > IVSIZE
3067 PUSHn(PL_statcache.st_size);
3069 PUSHi(PL_statcache.st_size);
3076 I32 result = my_stat();
3080 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3086 I32 result = my_stat();
3090 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3096 I32 result = my_stat();
3100 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3106 I32 result = my_stat();
3110 if (S_ISSOCK(PL_statcache.st_mode))
3117 I32 result = my_stat();
3121 if (S_ISCHR(PL_statcache.st_mode))
3128 I32 result = my_stat();
3132 if (S_ISBLK(PL_statcache.st_mode))
3139 I32 result = my_stat();
3143 if (S_ISREG(PL_statcache.st_mode))
3150 I32 result = my_stat();
3154 if (S_ISDIR(PL_statcache.st_mode))
3161 I32 result = my_stat();
3165 if (S_ISFIFO(PL_statcache.st_mode))
3172 I32 result = my_lstat();
3176 if (S_ISLNK(PL_statcache.st_mode))
3185 I32 result = my_stat();
3189 if (PL_statcache.st_mode & S_ISUID)
3199 I32 result = my_stat();
3203 if (PL_statcache.st_mode & S_ISGID)
3213 I32 result = my_stat();
3217 if (PL_statcache.st_mode & S_ISVTX)
3228 char *tmps = Nullch;
3231 if (PL_op->op_flags & OPf_REF)
3233 else if (isGV(TOPs))
3235 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3236 gv = (GV*)SvRV(POPs);
3238 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3240 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3241 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3242 else if (tmps && isDIGIT(*tmps))
3246 if (PerlLIO_isatty(fd))
3251 #if defined(atarist) /* this will work with atariST. Configure will
3252 make guesses for other systems. */
3253 # define FILE_base(f) ((f)->_base)
3254 # define FILE_ptr(f) ((f)->_ptr)
3255 # define FILE_cnt(f) ((f)->_cnt)
3256 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3266 register STDCHAR *s;
3273 if (PL_op->op_flags & OPf_REF)
3275 else if (isGV(TOPs))
3277 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3278 gv = (GV*)SvRV(POPs);
3284 if (gv == PL_defgv) {
3286 io = GvIO(PL_statgv);
3289 goto really_filename;
3294 PL_laststatval = -1;
3295 sv_setpv(PL_statname, "");
3296 io = GvIO(PL_statgv);
3298 if (io && IoIFP(io)) {
3299 if (! PerlIO_has_base(IoIFP(io)))
3300 DIE(aTHX_ "-T and -B not implemented on filehandles");
3301 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3302 if (PL_laststatval < 0)
3304 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3305 if (PL_op->op_type == OP_FTTEXT)
3310 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3311 i = PerlIO_getc(IoIFP(io));
3313 (void)PerlIO_ungetc(IoIFP(io),i);
3315 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3317 len = PerlIO_get_bufsiz(IoIFP(io));
3318 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3319 /* sfio can have large buffers - limit to 512 */
3324 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3326 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3328 SETERRNO(EBADF,RMS_IFI);
3336 PL_laststatval = -1;
3337 PL_laststype = OP_STAT;
3338 sv_setpv(PL_statname, SvPV(sv, n_a));
3339 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3340 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3341 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3344 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3345 if (PL_laststatval < 0) {
3346 (void)PerlIO_close(fp);
3349 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3350 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3351 (void)PerlIO_close(fp);
3353 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3354 RETPUSHNO; /* special case NFS directories */
3355 RETPUSHYES; /* null file is anything */
3360 /* now scan s to look for textiness */
3361 /* XXX ASCII dependent code */
3363 #if defined(DOSISH) || defined(USEMYBINMODE)
3364 /* ignore trailing ^Z on short files */
3365 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3369 for (i = 0; i < len; i++, s++) {
3370 if (!*s) { /* null never allowed in text */
3375 else if (!(isPRINT(*s) || isSPACE(*s)))
3378 else if (*s & 128) {
3380 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3383 /* utf8 characters don't count as odd */
3384 if (UTF8_IS_START(*s)) {
3385 int ulen = UTF8SKIP(s);
3386 if (ulen < len - i) {
3388 for (j = 1; j < ulen; j++) {
3389 if (!UTF8_IS_CONTINUATION(s[j]))
3392 --ulen; /* loop does extra increment */
3402 *s != '\n' && *s != '\r' && *s != '\b' &&
3403 *s != '\t' && *s != '\f' && *s != 27)
3408 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3433 if( !tmps || !*tmps ) {
3434 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3435 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3437 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3442 deprecate("chdir('') or chdir(undef) as chdir()");
3443 tmps = SvPV(*svp, n_a);
3447 TAINT_PROPER("chdir");
3452 TAINT_PROPER("chdir");
3453 PUSHi( PerlDir_chdir(tmps) >= 0 );
3455 /* Clear the DEFAULT element of ENV so we'll get the new value
3457 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3465 dSP; dMARK; dTARGET;
3466 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3472 DIE(aTHX_ PL_no_func, "chown");
3482 TAINT_PROPER("chroot");
3483 PUSHi( chroot(tmps) >= 0 );
3486 DIE(aTHX_ PL_no_func, "chroot");
3492 dSP; dMARK; dTARGET;
3494 value = (I32)apply(PL_op->op_type, MARK, SP);
3502 dSP; dMARK; dTARGET;
3504 value = (I32)apply(PL_op->op_type, MARK, SP);
3512 dSP; dMARK; dTARGET;
3514 value = (I32)apply(PL_op->op_type, MARK, SP);
3526 char *tmps2 = POPpx;
3527 char *tmps = SvPV(TOPs, n_a);
3528 TAINT_PROPER("rename");
3530 anum = PerlLIO_rename(tmps, tmps2);
3532 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3533 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3536 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3537 (void)UNLINK(tmps2);
3538 if (!(anum = link(tmps, tmps2)))
3539 anum = UNLINK(tmps);
3552 char *tmps2 = POPpx;
3553 char *tmps = SvPV(TOPs, n_a);
3554 TAINT_PROPER("link");
3555 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3558 DIE(aTHX_ PL_no_func, "link");
3567 char *tmps2 = POPpx;
3568 char *tmps = SvPV(TOPs, n_a);
3569 TAINT_PROPER("symlink");
3570 SETi( symlink(tmps, tmps2) >= 0 );
3573 DIE(aTHX_ PL_no_func, "symlink");
3583 char buf[MAXPATHLEN];
3587 #ifndef INCOMPLETE_TAINTS
3591 len = readlink(tmps, buf, sizeof(buf) - 1);
3599 RETSETUNDEF; /* just pretend it's a normal file */
3603 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3605 S_dooneliner(pTHX_ char *cmd, char *filename)
3607 char *save_filename = filename;
3613 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3614 strcpy(cmdline, cmd);
3615 strcat(cmdline, " ");
3616 for (s = cmdline + strlen(cmdline); *filename; ) {
3621 myfp = PerlProc_popen(cmdline, "r");
3625 SV *tmpsv = sv_newmortal();
3626 /* Need to save/restore 'PL_rs' ?? */
3627 s = sv_gets(tmpsv, myfp, 0);
3628 (void)PerlProc_pclose(myfp);
3632 #ifdef HAS_SYS_ERRLIST
3637 /* you don't see this */
3639 #ifdef HAS_SYS_ERRLIST
3647 if (instr(s, errmsg)) {
3654 #define EACCES EPERM
3656 if (instr(s, "cannot make"))
3657 SETERRNO(EEXIST,RMS_FEX);
3658 else if (instr(s, "existing file"))
3659 SETERRNO(EEXIST,RMS_FEX);
3660 else if (instr(s, "ile exists"))
3661 SETERRNO(EEXIST,RMS_FEX);
3662 else if (instr(s, "non-exist"))
3663 SETERRNO(ENOENT,RMS_FNF);
3664 else if (instr(s, "does not exist"))
3665 SETERRNO(ENOENT,RMS_FNF);
3666 else if (instr(s, "not empty"))
3667 SETERRNO(EBUSY,SS_DEVOFFLINE);
3668 else if (instr(s, "cannot access"))
3669 SETERRNO(EACCES,RMS_PRV);
3671 SETERRNO(EPERM,RMS_PRV);
3674 else { /* some mkdirs return no failure indication */
3675 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3676 if (PL_op->op_type == OP_RMDIR)
3681 SETERRNO(EACCES,RMS_PRV); /* a guess */
3690 /* This macro removes trailing slashes from a directory name.
3691 * Different operating and file systems take differently to
3692 * trailing slashes. According to POSIX 1003.1 1996 Edition
3693 * any number of trailing slashes should be allowed.
3694 * Thusly we snip them away so that even non-conforming
3695 * systems are happy.
3696 * We should probably do this "filtering" for all
3697 * the functions that expect (potentially) directory names:
3698 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3699 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3701 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3702 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3705 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3706 (tmps) = savepvn((tmps), (len)); \
3726 TRIMSLASHES(tmps,len,copy);
3728 TAINT_PROPER("mkdir");
3730 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3732 SETi( dooneliner("mkdir", tmps) );
3733 oldumask = PerlLIO_umask(0);
3734 PerlLIO_umask(oldumask);
3735 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3749 TRIMSLASHES(tmps,len,copy);
3750 TAINT_PROPER("rmdir");
3752 SETi( PerlDir_rmdir(tmps) >= 0 );
3754 SETi( dooneliner("rmdir", tmps) );
3761 /* Directory calls. */
3765 #if defined(Direntry_t) && defined(HAS_READDIR)
3768 char *dirname = POPpx;
3770 register IO *io = GvIOn(gv);
3776 PerlDir_close(IoDIRP(io));
3777 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3783 SETERRNO(EBADF,RMS_DIR);
3786 DIE(aTHX_ PL_no_dir_func, "opendir");
3792 #if defined(Direntry_t) && defined(HAS_READDIR)
3794 #if !defined(I_DIRENT) && !defined(VMS)
3795 Direntry_t *readdir (DIR *);
3797 register Direntry_t *dp;
3799 register IO *io = GvIOn(gv);
3802 if (!io || !IoDIRP(io))
3805 if (GIMME == G_ARRAY) {
3807 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3809 sv = newSVpvn(dp->d_name, dp->d_namlen);
3811 sv = newSVpv(dp->d_name, 0);
3813 #ifndef INCOMPLETE_TAINTS
3814 if (!(IoFLAGS(io) & IOf_UNTAINT))
3817 XPUSHs(sv_2mortal(sv));
3821 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3824 sv = newSVpvn(dp->d_name, dp->d_namlen);
3826 sv = newSVpv(dp->d_name, 0);
3828 #ifndef INCOMPLETE_TAINTS
3829 if (!(IoFLAGS(io) & IOf_UNTAINT))
3832 XPUSHs(sv_2mortal(sv));
3838 SETERRNO(EBADF,RMS_ISI);
3839 if (GIMME == G_ARRAY)
3844 DIE(aTHX_ PL_no_dir_func, "readdir");
3850 #if defined(HAS_TELLDIR) || defined(telldir)
3852 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3853 /* XXX netbsd still seemed to.
3854 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3855 --JHI 1999-Feb-02 */
3856 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3857 long telldir (DIR *);
3860 register IO *io = GvIOn(gv);
3862 if (!io || !IoDIRP(io))
3865 PUSHi( PerlDir_tell(IoDIRP(io)) );
3869 SETERRNO(EBADF,RMS_ISI);
3872 DIE(aTHX_ PL_no_dir_func, "telldir");
3878 #if defined(HAS_SEEKDIR) || defined(seekdir)
3882 register IO *io = GvIOn(gv);
3884 if (!io || !IoDIRP(io))
3887 (void)PerlDir_seek(IoDIRP(io), along);
3892 SETERRNO(EBADF,RMS_ISI);
3895 DIE(aTHX_ PL_no_dir_func, "seekdir");
3901 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3904 register IO *io = GvIOn(gv);
3906 if (!io || !IoDIRP(io))
3909 (void)PerlDir_rewind(IoDIRP(io));
3913 SETERRNO(EBADF,RMS_ISI);
3916 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3922 #if defined(Direntry_t) && defined(HAS_READDIR)
3925 register IO *io = GvIOn(gv);
3927 if (!io || !IoDIRP(io))
3930 #ifdef VOID_CLOSEDIR
3931 PerlDir_close(IoDIRP(io));
3933 if (PerlDir_close(IoDIRP(io)) < 0) {
3934 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3943 SETERRNO(EBADF,RMS_IFI);
3946 DIE(aTHX_ PL_no_dir_func, "closedir");
3950 /* Process control. */
3960 PERL_FLUSHALL_FOR_CHILD;
3961 childpid = PerlProc_fork();
3966 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3967 SvREADONLY_off(GvSV(tmpgv));
3968 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3969 SvREADONLY_on(GvSV(tmpgv));
3971 #ifdef THREADS_HAVE_PIDS
3972 PL_ppid = (IV)getppid();
3974 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3979 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3984 PERL_FLUSHALL_FOR_CHILD;
3985 childpid = PerlProc_fork();
3991 DIE(aTHX_ PL_no_func, "fork");
3998 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4003 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4004 childpid = wait4pid(-1, &argflags, 0);
4006 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4011 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4012 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4013 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4015 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4020 DIE(aTHX_ PL_no_func, "wait");
4026 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4034 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4035 childpid = wait4pid(childpid, &argflags, optype);
4037 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4042 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4043 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4044 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4046 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4051 DIE(aTHX_ PL_no_func, "waitpid");
4057 dSP; dMARK; dORIGMARK; dTARGET;
4065 while (++MARK <= SP) {
4066 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4071 TAINT_PROPER("system");
4073 PERL_FLUSHALL_FOR_CHILD;
4074 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4079 if (PerlProc_pipe(pp) >= 0)
4081 while ((childpid = PerlProc_fork()) == -1) {
4082 if (errno != EAGAIN) {
4087 PerlLIO_close(pp[0]);
4088 PerlLIO_close(pp[1]);
4095 Sigsave_t ihand,qhand; /* place to save signals during system() */
4099 PerlLIO_close(pp[1]);
4101 rsignal_save(SIGINT, SIG_IGN, &ihand);
4102 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4105 result = wait4pid(childpid, &status, 0);
4106 } while (result == -1 && errno == EINTR);
4108 (void)rsignal_restore(SIGINT, &ihand);
4109 (void)rsignal_restore(SIGQUIT, &qhand);
4111 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4112 do_execfree(); /* free any memory child malloced on fork */
4118 while (n < sizeof(int)) {
4119 n1 = PerlLIO_read(pp[0],
4120 (void*)(((char*)&errkid)+n),
4126 PerlLIO_close(pp[0]);
4127 if (n) { /* Error */
4128 if (n != sizeof(int))
4129 DIE(aTHX_ "panic: kid popen errno read");
4130 errno = errkid; /* Propagate errno from kid */
4131 STATUS_CURRENT = -1;
4134 PUSHi(STATUS_CURRENT);
4138 PerlLIO_close(pp[0]);
4139 #if defined(HAS_FCNTL) && defined(F_SETFD)
4140 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4143 if (PL_op->op_flags & OPf_STACKED) {
4144 SV *really = *++MARK;
4145 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4147 else if (SP - MARK != 1)
4148 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4150 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4154 #else /* ! FORK or VMS or OS/2 */
4157 if (PL_op->op_flags & OPf_STACKED) {
4158 SV *really = *++MARK;
4159 # if defined(WIN32) || defined(OS2)
4160 value = (I32)do_aspawn(really, MARK, SP);
4162 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4165 else if (SP - MARK != 1) {
4166 # if defined(WIN32) || defined(OS2)
4167 value = (I32)do_aspawn(Nullsv, MARK, SP);
4169 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4173 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4175 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4177 STATUS_NATIVE_SET(value);
4180 PUSHi(result ? value : STATUS_CURRENT);
4181 #endif /* !FORK or VMS */
4187 dSP; dMARK; dORIGMARK; dTARGET;
4193 while (++MARK <= SP) {
4194 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4199 TAINT_PROPER("exec");
4201 PERL_FLUSHALL_FOR_CHILD;
4202 if (PL_op->op_flags & OPf_STACKED) {
4203 SV *really = *++MARK;
4204 value = (I32)do_aexec(really, MARK, SP);
4206 else if (SP - MARK != 1)
4208 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4212 (void ) do_aspawn(Nullsv, MARK, SP);
4216 value = (I32)do_aexec(Nullsv, MARK, SP);
4221 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4224 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4227 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4240 dSP; dMARK; dTARGET;
4242 value = (I32)apply(PL_op->op_type, MARK, SP);
4247 DIE(aTHX_ PL_no_func, "kill");
4255 # ifdef THREADS_HAVE_PIDS
4258 XPUSHi( getppid() );
4262 DIE(aTHX_ PL_no_func, "getppid");
4278 pgrp = (I32)BSD_GETPGRP(pid);
4280 if (pid != 0 && pid != PerlProc_getpid())
4281 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4287 DIE(aTHX_ PL_no_func, "getpgrp()");
4306 TAINT_PROPER("setpgrp");
4308 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4310 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4311 || (pid != 0 && pid != PerlProc_getpid()))
4313 DIE(aTHX_ "setpgrp can't take arguments");
4315 SETi( setpgrp() >= 0 );
4316 #endif /* USE_BSDPGRP */
4319 DIE(aTHX_ PL_no_func, "setpgrp()");
4325 #ifdef HAS_GETPRIORITY
4329 SETi( getpriority(which, who) );
4332 DIE(aTHX_ PL_no_func, "getpriority()");
4338 #ifdef HAS_SETPRIORITY
4343 TAINT_PROPER("setpriority");
4344 SETi( setpriority(which, who, niceval) >= 0 );
4347 DIE(aTHX_ PL_no_func, "setpriority()");
4357 XPUSHn( time(Null(Time_t*)) );
4359 XPUSHi( time(Null(Time_t*)) );
4370 (void)PerlProc_times(&PL_timesbuf);
4372 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4373 /* struct tms, though same data */
4377 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4378 if (GIMME == G_ARRAY) {
4379 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4380 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4381 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4385 DIE(aTHX_ "times not implemented");
4386 #endif /* HAS_TIMES */
4399 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4400 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4401 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4407 when = (Time_t)SvNVx(POPs);
4409 when = (Time_t)SvIVx(POPs);
4412 if (PL_op->op_type == OP_LOCALTIME)
4413 tmbuf = localtime(&when);
4415 tmbuf = gmtime(&when);
4417 if (GIMME != G_ARRAY) {
4423 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4424 dayname[tmbuf->tm_wday],
4425 monname[tmbuf->tm_mon],
4430 tmbuf->tm_year + 1900);
4431 PUSHs(sv_2mortal(tsv));
4436 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4437 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4438 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4439 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4440 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4441 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4442 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4443 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4444 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4455 anum = alarm((unsigned int)anum);
4462 DIE(aTHX_ PL_no_func, "alarm");
4473 (void)time(&lasttime);
4478 PerlProc_sleep((unsigned int)duration);
4481 XPUSHi(when - lasttime);
4485 /* Shared memory. */
4499 return pp_shmwrite();
4504 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4505 dSP; dMARK; dTARGET;
4506 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4515 /* Message passing. */
4529 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4530 dSP; dMARK; dTARGET;
4531 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4542 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4543 dSP; dMARK; dTARGET;
4544 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4557 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4558 dSP; dMARK; dTARGET;
4559 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4566 DIE(aTHX_ "System V IPC is not implemented on this machine");
4572 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4573 dSP; dMARK; dTARGET;
4574 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4582 PUSHp(zero_but_true, ZBTLEN);
4592 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4593 dSP; dMARK; dTARGET;
4594 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4603 /* Get system info. */
4607 #ifdef HAS_GETHOSTBYNAME
4608 return pp_ghostent();
4610 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4616 #ifdef HAS_GETHOSTBYADDR
4617 return pp_ghostent();
4619 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4625 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4627 I32 which = PL_op->op_type;
4628 register char **elem;
4630 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4631 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4632 struct hostent *gethostbyname(Netdb_name_t);
4633 struct hostent *gethostent(void);
4635 struct hostent *hent;
4640 if (which == OP_GHBYNAME) {
4641 #ifdef HAS_GETHOSTBYNAME
4642 char* name = POPpbytex;
4643 hent = PerlSock_gethostbyname(name);
4645 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4648 else if (which == OP_GHBYADDR) {
4649 #ifdef HAS_GETHOSTBYADDR
4650 int addrtype = POPi;
4653 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4655 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4657 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4661 #ifdef HAS_GETHOSTENT
4662 hent = PerlSock_gethostent();
4664 DIE(aTHX_ PL_no_sock_func, "gethostent");
4667 #ifdef HOST_NOT_FOUND
4669 #ifdef USE_REENTRANT_API
4670 # ifdef USE_GETHOSTENT_ERRNO
4671 h_errno = PL_reentrant_buffer->_gethostent_errno;
4674 STATUS_NATIVE_SET(h_errno);
4678 if (GIMME != G_ARRAY) {
4679 PUSHs(sv = sv_newmortal());
4681 if (which == OP_GHBYNAME) {
4683 sv_setpvn(sv, hent->h_addr, hent->h_length);
4686 sv_setpv(sv, (char*)hent->h_name);
4692 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4693 sv_setpv(sv, (char*)hent->h_name);
4694 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4695 for (elem = hent->h_aliases; elem && *elem; elem++) {
4696 sv_catpv(sv, *elem);
4698 sv_catpvn(sv, " ", 1);
4700 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4701 sv_setiv(sv, (IV)hent->h_addrtype);
4702 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4703 len = hent->h_length;
4704 sv_setiv(sv, (IV)len);
4706 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4707 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4708 sv_setpvn(sv, *elem, len);
4711 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4713 sv_setpvn(sv, hent->h_addr, len);
4718 DIE(aTHX_ PL_no_sock_func, "gethostent");
4724 #ifdef HAS_GETNETBYNAME
4725 return pp_gnetent();
4727 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4733 #ifdef HAS_GETNETBYADDR
4734 return pp_gnetent();
4736 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4742 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4744 I32 which = PL_op->op_type;
4745 register char **elem;
4747 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4748 struct netent *getnetbyaddr(Netdb_net_t, int);
4749 struct netent *getnetbyname(Netdb_name_t);
4750 struct netent *getnetent(void);
4752 struct netent *nent;
4755 if (which == OP_GNBYNAME){
4756 #ifdef HAS_GETNETBYNAME
4757 char *name = POPpbytex;
4758 nent = PerlSock_getnetbyname(name);
4760 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4763 else if (which == OP_GNBYADDR) {
4764 #ifdef HAS_GETNETBYADDR
4765 int addrtype = POPi;
4766 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4767 nent = PerlSock_getnetbyaddr(addr, addrtype);
4769 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4773 #ifdef HAS_GETNETENT
4774 nent = PerlSock_getnetent();
4776 DIE(aTHX_ PL_no_sock_func, "getnetent");
4779 #ifdef HOST_NOT_FOUND
4781 #ifdef USE_REENTRANT_API
4782 # ifdef USE_GETNETENT_ERRNO
4783 h_errno = PL_reentrant_buffer->_getnetent_errno;
4786 STATUS_NATIVE_SET(h_errno);
4791 if (GIMME != G_ARRAY) {
4792 PUSHs(sv = sv_newmortal());
4794 if (which == OP_GNBYNAME)
4795 sv_setiv(sv, (IV)nent->n_net);
4797 sv_setpv(sv, nent->n_name);
4803 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4804 sv_setpv(sv, nent->n_name);
4805 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4806 for (elem = nent->n_aliases; elem && *elem; elem++) {
4807 sv_catpv(sv, *elem);
4809 sv_catpvn(sv, " ", 1);
4811 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4812 sv_setiv(sv, (IV)nent->n_addrtype);
4813 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4814 sv_setiv(sv, (IV)nent->n_net);
4819 DIE(aTHX_ PL_no_sock_func, "getnetent");
4825 #ifdef HAS_GETPROTOBYNAME
4826 return pp_gprotoent();
4828 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4834 #ifdef HAS_GETPROTOBYNUMBER
4835 return pp_gprotoent();
4837 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4843 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4845 I32 which = PL_op->op_type;
4846 register char **elem;
4848 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4849 struct protoent *getprotobyname(Netdb_name_t);
4850 struct protoent *getprotobynumber(int);
4851 struct protoent *getprotoent(void);
4853 struct protoent *pent;
4856 if (which == OP_GPBYNAME) {
4857 #ifdef HAS_GETPROTOBYNAME
4858 char* name = POPpbytex;
4859 pent = PerlSock_getprotobyname(name);
4861 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4864 else if (which == OP_GPBYNUMBER) {
4865 #ifdef HAS_GETPROTOBYNUMBER
4867 pent = PerlSock_getprotobynumber(number);
4869 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4873 #ifdef HAS_GETPROTOENT
4874 pent = PerlSock_getprotoent();
4876 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4880 if (GIMME != G_ARRAY) {
4881 PUSHs(sv = sv_newmortal());
4883 if (which == OP_GPBYNAME)
4884 sv_setiv(sv, (IV)pent->p_proto);
4886 sv_setpv(sv, pent->p_name);
4892 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4893 sv_setpv(sv, pent->p_name);
4894 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4895 for (elem = pent->p_aliases; elem && *elem; elem++) {
4896 sv_catpv(sv, *elem);
4898 sv_catpvn(sv, " ", 1);
4900 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4901 sv_setiv(sv, (IV)pent->p_proto);
4906 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4912 #ifdef HAS_GETSERVBYNAME
4913 return pp_gservent();
4915 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4921 #ifdef HAS_GETSERVBYPORT
4922 return pp_gservent();
4924 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4930 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4932 I32 which = PL_op->op_type;
4933 register char **elem;
4935 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4936 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4937 struct servent *getservbyport(int, Netdb_name_t);
4938 struct servent *getservent(void);
4940 struct servent *sent;
4943 if (which == OP_GSBYNAME) {
4944 #ifdef HAS_GETSERVBYNAME
4945 char *proto = POPpbytex;
4946 char *name = POPpbytex;
4948 if (proto && !*proto)
4951 sent = PerlSock_getservbyname(name, proto);
4953 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4956 else if (which == OP_GSBYPORT) {
4957 #ifdef HAS_GETSERVBYPORT
4958 char *proto = POPpbytex;
4959 unsigned short port = (unsigned short)POPu;
4961 if (proto && !*proto)
4965 port = PerlSock_htons(port);
4967 sent = PerlSock_getservbyport(port, proto);
4969 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4973 #ifdef HAS_GETSERVENT
4974 sent = PerlSock_getservent();
4976 DIE(aTHX_ PL_no_sock_func, "getservent");
4980 if (GIMME != G_ARRAY) {
4981 PUSHs(sv = sv_newmortal());
4983 if (which == OP_GSBYNAME) {
4985 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4987 sv_setiv(sv, (IV)(sent->s_port));
4991 sv_setpv(sv, sent->s_name);
4997 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4998 sv_setpv(sv, sent->s_name);
4999 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5000 for (elem = sent->s_aliases; elem && *elem; elem++) {
5001 sv_catpv(sv, *elem);
5003 sv_catpvn(sv, " ", 1);
5005 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5007 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5009 sv_setiv(sv, (IV)(sent->s_port));
5011 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5012 sv_setpv(sv, sent->s_proto);
5017 DIE(aTHX_ PL_no_sock_func, "getservent");
5023 #ifdef HAS_SETHOSTENT
5025 PerlSock_sethostent(TOPi);
5028 DIE(aTHX_ PL_no_sock_func, "sethostent");
5034 #ifdef HAS_SETNETENT
5036 PerlSock_setnetent(TOPi);
5039 DIE(aTHX_ PL_no_sock_func, "setnetent");
5045 #ifdef HAS_SETPROTOENT
5047 PerlSock_setprotoent(TOPi);
5050 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5056 #ifdef HAS_SETSERVENT
5058 PerlSock_setservent(TOPi);
5061 DIE(aTHX_ PL_no_sock_func, "setservent");
5067 #ifdef HAS_ENDHOSTENT
5069 PerlSock_endhostent();
5073 DIE(aTHX_ PL_no_sock_func, "endhostent");
5079 #ifdef HAS_ENDNETENT
5081 PerlSock_endnetent();
5085 DIE(aTHX_ PL_no_sock_func, "endnetent");
5091 #ifdef HAS_ENDPROTOENT
5093 PerlSock_endprotoent();
5097 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5103 #ifdef HAS_ENDSERVENT
5105 PerlSock_endservent();
5109 DIE(aTHX_ PL_no_sock_func, "endservent");
5118 DIE(aTHX_ PL_no_func, "getpwnam");
5127 DIE(aTHX_ PL_no_func, "getpwuid");
5135 I32 which = PL_op->op_type;
5138 struct passwd *pwent = NULL;
5140 * We currently support only the SysV getsp* shadow password interface.
5141 * The interface is declared in <shadow.h> and often one needs to link
5142 * with -lsecurity or some such.
5143 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5146 * AIX getpwnam() is clever enough to return the encrypted password
5147 * only if the caller (euid?) is root.
5149 * There are at least two other shadow password APIs. Many platforms
5150 * seem to contain more than one interface for accessing the shadow
5151 * password databases, possibly for compatibility reasons.
5152 * The getsp*() is by far he simplest one, the other two interfaces
5153 * are much more complicated, but also very similar to each other.
5158 * struct pr_passwd *getprpw*();
5159 * The password is in
5160 * char getprpw*(...).ufld.fd_encrypt[]
5161 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5166 * struct es_passwd *getespw*();
5167 * The password is in
5168 * char *(getespw*(...).ufld.fd_encrypt)
5169 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5171 * Mention I_PROT here so that Configure probes for it.
5173 * In HP-UX for getprpw*() the manual page claims that one should include
5174 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5175 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5176 * and pp_sys.c already includes <shadow.h> if there is such.
5178 * Note that <sys/security.h> is already probed for, but currently
5179 * it is only included in special cases.
5181 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5182 * be preferred interface, even though also the getprpw*() interface
5183 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5184 * One also needs to call set_auth_parameters() in main() before
5185 * doing anything else, whether one is using getespw*() or getprpw*().
5187 * Note that accessing the shadow databases can be magnitudes
5188 * slower than accessing the standard databases.
5196 char* name = POPpbytex;
5197 pwent = getpwnam(name);
5203 pwent = getpwuid(uid);
5207 # ifdef HAS_GETPWENT
5209 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5210 if (pwent) pwent = getpwnam(pwent->pw_name);
5213 DIE(aTHX_ PL_no_func, "getpwent");
5219 if (GIMME != G_ARRAY) {
5220 PUSHs(sv = sv_newmortal());
5222 if (which == OP_GPWNAM)
5223 # if Uid_t_sign <= 0
5224 sv_setiv(sv, (IV)pwent->pw_uid);
5226 sv_setuv(sv, (UV)pwent->pw_uid);
5229 sv_setpv(sv, pwent->pw_name);
5235 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5236 sv_setpv(sv, pwent->pw_name);
5238 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5240 /* If we have getspnam(), we try to dig up the shadow
5241 * password. If we are underprivileged, the shadow
5242 * interface will set the errno to EACCES or similar,
5243 * and return a null pointer. If this happens, we will
5244 * use the dummy password (usually "*" or "x") from the
5245 * standard password database.
5247 * In theory we could skip the shadow call completely
5248 * if euid != 0 but in practice we cannot know which
5249 * security measures are guarding the shadow databases
5250 * on a random platform.
5252 * Resist the urge to use additional shadow interfaces.
5253 * Divert the urge to writing an extension instead.
5256 # ifdef HAS_GETSPNAM
5258 struct spwd *spwent;
5259 int saverrno; /* Save and restore errno so that
5260 * underprivileged attempts seem
5261 * to have never made the unsccessful
5262 * attempt to retrieve the shadow password. */
5265 spwent = getspnam(pwent->pw_name);
5267 if (spwent && spwent->sp_pwdp)
5268 sv_setpv(sv, spwent->sp_pwdp);
5272 if (!SvPOK(sv)) /* Use the standard password, then. */
5273 sv_setpv(sv, pwent->pw_passwd);
5276 # ifndef INCOMPLETE_TAINTS
5277 /* passwd is tainted because user himself can diddle with it.
5278 * admittedly not much and in a very limited way, but nevertheless. */
5282 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5283 # if Uid_t_sign <= 0
5284 sv_setiv(sv, (IV)pwent->pw_uid);
5286 sv_setuv(sv, (UV)pwent->pw_uid);
5289 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5290 # if Uid_t_sign <= 0
5291 sv_setiv(sv, (IV)pwent->pw_gid);
5293 sv_setuv(sv, (UV)pwent->pw_gid);
5295 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5296 * because of the poor interface of the Perl getpw*(),
5297 * not because there's some standard/convention saying so.
5298 * A better interface would have been to return a hash,
5299 * but we are accursed by our history, alas. --jhi. */
5300 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5302 sv_setiv(sv, (IV)pwent->pw_change);
5305 sv_setiv(sv, (IV)pwent->pw_quota);
5308 sv_setpv(sv, pwent->pw_age);
5313 /* pw_class and pw_comment are mutually exclusive--.
5314 * see the above note for pw_change, pw_quota, and pw_age. */
5315 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5317 sv_setpv(sv, pwent->pw_class);
5320 sv_setpv(sv, pwent->pw_comment);
5324 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5326 sv_setpv(sv, pwent->pw_gecos);
5328 # ifndef INCOMPLETE_TAINTS
5329 /* pw_gecos is tainted because user himself can diddle with it. */
5333 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5334 sv_setpv(sv, pwent->pw_dir);
5336 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5337 sv_setpv(sv, pwent->pw_shell);
5338 # ifndef INCOMPLETE_TAINTS
5339 /* pw_shell is tainted because user himself can diddle with it. */
5344 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5345 sv_setiv(sv, (IV)pwent->pw_expire);
5350 DIE(aTHX_ PL_no_func, "getpwent");
5356 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5361 DIE(aTHX_ PL_no_func, "setpwent");
5367 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5372 DIE(aTHX_ PL_no_func, "endpwent");
5381 DIE(aTHX_ PL_no_func, "getgrnam");
5390 DIE(aTHX_ PL_no_func, "getgrgid");
5398 I32 which = PL_op->op_type;
5399 register char **elem;
5401 struct group *grent;
5404 if (which == OP_GGRNAM) {
5405 char* name = POPpbytex;
5406 grent = (struct group *)getgrnam(name);
5408 else if (which == OP_GGRGID) {
5410 grent = (struct group *)getgrgid(gid);
5414 grent = (struct group *)getgrent();
5416 DIE(aTHX_ PL_no_func, "getgrent");
5420 if (GIMME != G_ARRAY) {
5421 PUSHs(sv = sv_newmortal());
5423 if (which == OP_GGRNAM)
5424 sv_setiv(sv, (IV)grent->gr_gid);
5426 sv_setpv(sv, grent->gr_name);
5432 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5433 sv_setpv(sv, grent->gr_name);
5435 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5437 sv_setpv(sv, grent->gr_passwd);
5440 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5441 sv_setiv(sv, (IV)grent->gr_gid);
5443 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5444 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5445 /* In UNICOS/mk (_CRAYMPP) the multithreading
5446 * versions (getgrnam_r, getgrgid_r)
5447 * seem to return an illegal pointer
5448 * as the group members list, gr_mem.
5449 * getgrent() doesn't even have a _r version
5450 * but the gr_mem is poisonous anyway.
5451 * So yes, you cannot get the list of group
5452 * members if building multithreaded in UNICOS/mk. */
5453 for (elem = grent->gr_mem; elem && *elem; elem++) {
5454 sv_catpv(sv, *elem);
5456 sv_catpvn(sv, " ", 1);
5463 DIE(aTHX_ PL_no_func, "getgrent");
5469 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5474 DIE(aTHX_ PL_no_func, "setgrent");
5480 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5485 DIE(aTHX_ PL_no_func, "endgrent");
5495 if (!(tmps = PerlProc_getlogin()))
5497 PUSHp(tmps, strlen(tmps));
5500 DIE(aTHX_ PL_no_func, "getlogin");
5504 /* Miscellaneous. */
5509 dSP; dMARK; dORIGMARK; dTARGET;
5510 register I32 items = SP - MARK;
5511 unsigned long a[20];
5517 while (++MARK <= SP) {
5518 if (SvTAINTED(*MARK)) {
5524 TAINT_PROPER("syscall");
5527 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5528 * or where sizeof(long) != sizeof(char*). But such machines will
5529 * not likely have syscall implemented either, so who cares?
5531 while (++MARK <= SP) {
5532 if (SvNIOK(*MARK) || !i)
5533 a[i++] = SvIV(*MARK);
5534 else if (*MARK == &PL_sv_undef)
5537 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5543 DIE(aTHX_ "Too many args to syscall");
5545 DIE(aTHX_ "Too few args to syscall");
5547 retval = syscall(a[0]);
5550 retval = syscall(a[0],a[1]);
5553 retval = syscall(a[0],a[1],a[2]);
5556 retval = syscall(a[0],a[1],a[2],a[3]);
5559 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5562 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5565 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5568 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5572 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5575 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5578 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5582 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5586 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5590 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5591 a[10],a[11],a[12],a[13]);
5593 #endif /* atarist */
5599 DIE(aTHX_ PL_no_func, "syscall");
5603 #ifdef FCNTL_EMULATE_FLOCK
5605 /* XXX Emulate flock() with fcntl().
5606 What's really needed is a good file locking module.
5610 fcntl_emulate_flock(int fd, int operation)
5614 switch (operation & ~LOCK_NB) {
5616 flock.l_type = F_RDLCK;
5619 flock.l_type = F_WRLCK;
5622 flock.l_type = F_UNLCK;
5628 flock.l_whence = SEEK_SET;
5629 flock.l_start = flock.l_len = (Off_t)0;
5631 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5634 #endif /* FCNTL_EMULATE_FLOCK */
5636 #ifdef LOCKF_EMULATE_FLOCK
5638 /* XXX Emulate flock() with lockf(). This is just to increase
5639 portability of scripts. The calls are not completely
5640 interchangeable. What's really needed is a good file
5644 /* The lockf() constants might have been defined in <unistd.h>.
5645 Unfortunately, <unistd.h> causes troubles on some mixed
5646 (BSD/POSIX) systems, such as SunOS 4.1.3.
5648 Further, the lockf() constants aren't POSIX, so they might not be
5649 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5650 just stick in the SVID values and be done with it. Sigh.
5654 # define F_ULOCK 0 /* Unlock a previously locked region */
5657 # define F_LOCK 1 /* Lock a region for exclusive use */
5660 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5663 # define F_TEST 3 /* Test a region for other processes locks */
5667 lockf_emulate_flock(int fd, int operation)
5673 /* flock locks entire file so for lockf we need to do the same */
5675 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5676 if (pos > 0) /* is seekable and needs to be repositioned */
5677 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5678 pos = -1; /* seek failed, so don't seek back afterwards */
5681 switch (operation) {
5683 /* LOCK_SH - get a shared lock */
5685 /* LOCK_EX - get an exclusive lock */
5687 i = lockf (fd, F_LOCK, 0);
5690 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5691 case LOCK_SH|LOCK_NB:
5692 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5693 case LOCK_EX|LOCK_NB:
5694 i = lockf (fd, F_TLOCK, 0);
5696 if ((errno == EAGAIN) || (errno == EACCES))
5697 errno = EWOULDBLOCK;
5700 /* LOCK_UN - unlock (non-blocking is a no-op) */
5702 case LOCK_UN|LOCK_NB:
5703 i = lockf (fd, F_ULOCK, 0);
5706 /* Default - can't decipher operation */
5713 if (pos > 0) /* need to restore position of the handle */
5714 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5719 #endif /* LOCKF_EMULATE_FLOCK */