3 * Copyright (c) 1991-2003, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * But only a short way ahead its floor and the walls on either side were
12 * cloven by a great fissure, out of which the red glare came, now leaping
13 * up, now dying down into darkness; and all the while far below there was
14 * a rumour and a trouble as of great engines throbbing and labouring.
18 #define PERL_IN_PP_SYS_C
22 /* Shadow password support for solaris - pdo@cs.umd.edu
23 * Not just Solaris: at least HP-UX, IRIX, Linux.
24 * The API is from SysV.
26 * There are at least two more shadow interfaces,
27 * see the comments in pp_gpwent().
31 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
32 * and another MAXINT from "perl.h" <- <sys/param.h>. */
40 extern "C" int syscall(unsigned long,...);
45 # include <sys/wait.h>
49 # include <sys/resource.h>
58 # include <sys/select.h>
62 /* XXX Configure test needed.
63 h_errno might not be a simple 'int', especially for multi-threaded
64 applications, see "extern int errno in perl.h". Creating such
65 a test requires taking into account the differences between
66 compiling multithreaded and singlethreaded ($ccflags et al).
67 HOST_NOT_FOUND is typically defined in <netdb.h>.
69 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
78 struct passwd *getpwnam (char *);
79 struct passwd *getpwuid (Uid_t);
84 struct passwd *getpwent (void);
85 #elif defined (VMS) && defined (my_getpwent)
86 struct passwd *Perl_my_getpwent (void);
95 struct group *getgrnam (char *);
96 struct group *getgrgid (Gid_t);
100 struct group *getgrent (void);
106 # if defined(_MSC_VER) || defined(__MINGW32__)
107 # include <sys/utime.h>
114 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
117 # define my_chsize PerlLIO_chsize
122 #else /* no flock() */
124 /* fcntl.h might not have been included, even if it exists, because
125 the current Configure only sets I_FCNTL if it's needed to pick up
126 the *_OK constants. Make sure it has been included before testing
127 the fcntl() locking constants. */
128 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
132 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
133 # define FLOCK fcntl_emulate_flock
134 # define FCNTL_EMULATE_FLOCK
135 # else /* no flock() or fcntl(F_SETLK,...) */
137 # define FLOCK lockf_emulate_flock
138 # define LOCKF_EMULATE_FLOCK
140 # endif /* no flock() or fcntl(F_SETLK,...) */
143 static int FLOCK (int, int);
146 * These are the flock() constants. Since this sytems doesn't have
147 * flock(), the values of the constants are probably not available.
161 # endif /* emulating flock() */
163 #endif /* no flock() */
166 static char zero_but_true[ZBTLEN + 1] = "0 but true";
168 #if defined(I_SYS_ACCESS) && !defined(R_OK)
169 # include <sys/access.h>
172 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
173 # define FD_CLOEXEC 1 /* NeXT needs this */
178 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
179 #undef PERL_EFF_ACCESS_W_OK
180 #undef PERL_EFF_ACCESS_X_OK
182 /* F_OK unused: if stat() cannot find it... */
184 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
185 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
186 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
187 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
188 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
191 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
192 # ifdef I_SYS_SECURITY
193 # include <sys/security.h>
197 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
198 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
199 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
202 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
203 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
204 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
208 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
210 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
211 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
212 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
216 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
217 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
220 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
222 Uid_t ruid = getuid();
223 Uid_t euid = geteuid();
224 Gid_t rgid = getgid();
225 Gid_t egid = getegid();
229 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
230 Perl_croak(aTHX_ "switching effective uid is not implemented");
233 if (setreuid(euid, ruid))
236 if (setresuid(euid, ruid, (Uid_t)-1))
239 Perl_croak(aTHX_ "entering effective uid failed");
242 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
243 Perl_croak(aTHX_ "switching effective gid is not implemented");
246 if (setregid(egid, rgid))
249 if (setresgid(egid, rgid, (Gid_t)-1))
252 Perl_croak(aTHX_ "entering effective gid failed");
255 res = access(path, mode);
258 if (setreuid(ruid, euid))
261 if (setresuid(ruid, euid, (Uid_t)-1))
264 Perl_croak(aTHX_ "leaving effective uid failed");
267 if (setregid(rgid, egid))
270 if (setresgid(rgid, egid, (Gid_t)-1))
273 Perl_croak(aTHX_ "leaving effective gid failed");
278 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
279 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
280 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
283 #if !defined(PERL_EFF_ACCESS_R_OK)
284 /* With it or without it: anyway you get a warning: either that
285 it is unused, or it is declared static and never defined.
288 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
290 Perl_croak(aTHX_ "switching effective uid is not implemented");
306 if (PL_op->op_private & OPpOPEN_IN_RAW)
308 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
310 fp = PerlProc_popen(tmps, mode);
313 if (PL_curcop->cop_io) {
314 type = SvPV_nolen(PL_curcop->cop_io);
317 PerlIO_apply_layers(aTHX_ fp,mode,type);
319 if (gimme == G_VOID) {
321 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
325 else if (gimme == G_SCALAR) {
327 PL_rs = &PL_sv_undef;
328 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
329 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
341 if (sv_gets(sv, fp, 0) == Nullch) {
345 XPUSHs(sv_2mortal(sv));
346 if (SvLEN(sv) - SvCUR(sv) > 20) {
347 SvLEN_set(sv, SvCUR(sv)+1);
348 Renew(SvPVX(sv), SvLEN(sv), char);
353 STATUS_NATIVE_SET(PerlProc_pclose(fp));
354 TAINT; /* "I believe that this is not gratuitous!" */
357 STATUS_NATIVE_SET(-1);
358 if (gimme == G_SCALAR)
368 tryAMAGICunTARGET(iter, -1);
370 /* Note that we only ever get here if File::Glob fails to load
371 * without at the same time croaking, for some reason, or if
372 * perl was built with PERL_EXTERNAL_GLOB */
379 * The external globbing program may use things we can't control,
380 * so for security reasons we must assume the worst.
383 taint_proper(PL_no_security, "glob");
387 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
388 PL_last_in_gv = (GV*)*PL_stack_sp--;
390 SAVESPTR(PL_rs); /* This is not permanent, either. */
391 PL_rs = sv_2mortal(newSVpvn("\000", 1));
394 *SvPVX(PL_rs) = '\n';
398 result = do_readline();
405 PL_last_in_gv = cGVOP_gv;
406 return do_readline();
415 if (SP - MARK != 1) {
417 do_join(TARG, &PL_sv_no, MARK, SP);
424 tmps = SvPV(tmpsv, len);
427 (void)SvUPGRADE(error, SVt_PV);
428 if (SvPOK(error) && SvCUR(error))
429 sv_catpv(error, "\t...caught");
431 tmps = SvPV(tmpsv, len);
434 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
436 Perl_warn(aTHX_ "%"SVf, tmpsv);
448 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
450 if (SP - MARK != 1) {
452 do_join(TARG, &PL_sv_no, MARK, SP);
454 tmps = SvPV(tmpsv, len);
460 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
464 (void)SvUPGRADE(error, SVt_PV);
465 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
467 SvSetSV(error,tmpsv);
468 else if (sv_isobject(error)) {
469 HV *stash = SvSTASH(SvRV(error));
470 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
472 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
473 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
480 call_sv((SV*)GvCV(gv),
481 G_SCALAR|G_EVAL|G_KEEPERR);
482 sv_setsv(error,*PL_stack_sp--);
485 DIE(aTHX_ Nullformat);
488 if (SvPOK(error) && SvCUR(error))
489 sv_catpv(error, "\t...propagated");
491 tmps = SvPV(tmpsv, len);
495 tmpsv = sv_2mortal(newSVpvn("Died", 4));
497 DIE(aTHX_ "%"SVf, tmpsv);
517 DIE(aTHX_ PL_no_usym, "filehandle");
518 if ((io = GvIOp(gv)))
519 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
521 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
522 /* Method's args are same as ours ... */
523 /* ... except handle is replaced by the object */
524 *MARK-- = SvTIED_obj((SV*)io, mg);
528 call_method("OPEN", G_SCALAR);
541 tmps = SvPV(sv, len);
542 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
545 PUSHi( (I32)PL_forkprocess );
546 else if (PL_forkprocess == 0) /* we are a new child */
565 if (gv && (io = GvIO(gv))
566 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
569 XPUSHs(SvTIED_obj((SV*)io, mg));
572 call_method("CLOSE", G_SCALAR);
578 PUSHs(boolSV(do_close(gv, TRUE)));
598 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
599 DIE(aTHX_ PL_no_usym, "filehandle");
604 do_close(rgv, FALSE);
606 do_close(wgv, FALSE);
608 if (PerlProc_pipe(fd) < 0)
611 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
612 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
613 IoOFP(rstio) = IoIFP(rstio);
614 IoIFP(wstio) = IoOFP(wstio);
615 IoTYPE(rstio) = IoTYPE_RDONLY;
616 IoTYPE(wstio) = IoTYPE_WRONLY;
618 if (!IoIFP(rstio) || !IoOFP(wstio)) {
619 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
620 else PerlLIO_close(fd[0]);
621 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
622 else PerlLIO_close(fd[1]);
625 #if defined(HAS_FCNTL) && defined(F_SETFD)
626 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
627 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
634 DIE(aTHX_ PL_no_func, "pipe");
650 if (gv && (io = GvIO(gv))
651 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
654 XPUSHs(SvTIED_obj((SV*)io, mg));
657 call_method("FILENO", G_SCALAR);
663 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
664 /* Can't do this because people seem to do things like
665 defined(fileno($foo)) to check whether $foo is a valid fh.
666 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
667 report_evil_fh(gv, io, PL_op->op_type);
672 PUSHi(PerlIO_fileno(fp));
683 anum = PerlLIO_umask(0);
684 (void)PerlLIO_umask(anum);
687 anum = PerlLIO_umask(POPi);
688 TAINT_PROPER("umask");
691 /* Only DIE if trying to restrict permissions on `user' (self).
692 * Otherwise it's harmless and more useful to just return undef
693 * since 'group' and 'other' concepts probably don't exist here. */
694 if (MAXARG >= 1 && (POPi & 0700))
695 DIE(aTHX_ "umask not implemented");
696 XPUSHs(&PL_sv_undef);
718 if (gv && (io = GvIO(gv))
719 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
722 XPUSHs(SvTIED_obj((SV*)io, mg));
727 call_method("BINMODE", G_SCALAR);
734 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
735 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
736 report_evil_fh(gv, io, PL_op->op_type);
737 SETERRNO(EBADF,RMS_IFI);
742 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
743 (discp) ? SvPV_nolen(discp) : Nullch)) {
761 I32 markoff = MARK - PL_stack_base;
763 int how = PERL_MAGIC_tied;
767 switch(SvTYPE(varsv)) {
769 methname = "TIEHASH";
770 HvEITER((HV *)varsv) = Null(HE *);
773 methname = "TIEARRAY";
776 #ifdef GV_UNIQUE_CHECK
777 if (GvUNIQUE((GV*)varsv)) {
778 Perl_croak(aTHX_ "Attempt to tie unique GV");
781 methname = "TIEHANDLE";
782 how = PERL_MAGIC_tiedscalar;
783 /* For tied filehandles, we apply tiedscalar magic to the IO
784 slot of the GP rather than the GV itself. AMS 20010812 */
786 GvIOp(varsv) = newIO();
787 varsv = (SV *)GvIOp(varsv);
790 methname = "TIESCALAR";
791 how = PERL_MAGIC_tiedscalar;
795 if (sv_isobject(*MARK)) {
797 PUSHSTACKi(PERLSI_MAGIC);
799 EXTEND(SP,(I32)items);
803 call_method(methname, G_SCALAR);
806 /* Not clear why we don't call call_method here too.
807 * perhaps to get different error message ?
809 stash = gv_stashsv(*MARK, FALSE);
810 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
811 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
815 PUSHSTACKi(PERLSI_MAGIC);
817 EXTEND(SP,(I32)items);
821 call_sv((SV*)GvCV(gv), G_SCALAR);
827 if (sv_isobject(sv)) {
828 sv_unmagic(varsv, how);
829 /* Croak if a self-tie on an aggregate is attempted. */
830 if (varsv == SvRV(sv) &&
831 (SvTYPE(varsv) == SVt_PVAV ||
832 SvTYPE(varsv) == SVt_PVHV))
834 "Self-ties of arrays and hashes are not supported");
835 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
838 SP = PL_stack_base + markoff;
848 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
849 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
851 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
854 if ((mg = SvTIED_mg(sv, how))) {
855 SV *obj = SvRV(mg->mg_obj);
859 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
860 isGV(gv) && (cv = GvCV(gv))) {
862 XPUSHs(SvTIED_obj((SV*)gv, mg));
863 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
866 call_sv((SV *)cv, G_VOID);
870 else if (ckWARN(WARN_UNTIE)) {
871 if (mg && SvREFCNT(obj) > 1)
872 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
873 "untie attempted while %"UVuf" inner references still exist",
874 (UV)SvREFCNT(obj) - 1 ) ;
877 sv_unmagic(sv, how) ;
887 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
888 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
890 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
893 if ((mg = SvTIED_mg(sv, how))) {
894 SV *osv = SvTIED_obj(sv, mg);
895 if (osv == mg->mg_obj)
896 osv = sv_mortalcopy(osv);
914 sv = sv_mortalcopy(&PL_sv_no);
915 sv_setpv(sv, "AnyDBM_File");
916 stash = gv_stashsv(sv, FALSE);
917 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
919 require_pv("AnyDBM_File.pm");
921 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
922 DIE(aTHX_ "No dbm on this machine");
932 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
934 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
937 call_sv((SV*)GvCV(gv), G_SCALAR);
940 if (!sv_isobject(TOPs)) {
945 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
948 call_sv((SV*)GvCV(gv), G_SCALAR);
952 if (sv_isobject(TOPs)) {
953 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
954 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
976 struct timeval timebuf;
977 struct timeval *tbuf = &timebuf;
981 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
986 # if BYTEORDER & 0xf0000
987 # define ORDERBYTE (0x88888888 - BYTEORDER)
989 # define ORDERBYTE (0x4444 - BYTEORDER)
995 for (i = 1; i <= 3; i++) {
1003 /* little endians can use vecs directly */
1004 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1011 masksize = NFDBITS / NBBY;
1013 masksize = sizeof(long); /* documented int, everyone seems to use long */
1015 Zero(&fd_sets[0], 4, char*);
1018 # if SELECT_MIN_BITS > 1
1019 /* If SELECT_MIN_BITS is greater than one we most probably will want
1020 * to align the sizes with SELECT_MIN_BITS/8 because for example
1021 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1022 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1023 * on (sets/tests/clears bits) is 32 bits. */
1024 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1026 growsize = sizeof(fd_set);
1034 timebuf.tv_sec = (long)value;
1035 value -= (NV)timebuf.tv_sec;
1036 timebuf.tv_usec = (long)(value * 1000000.0);
1039 tbuf = Null(struct timeval*);
1041 for (i = 1; i <= 3; i++) {
1047 else if (!SvPOK(sv))
1048 SvPV_force(sv,n_a); /* force string conversion */
1051 Sv_Grow(sv, growsize);
1055 while (++j <= growsize) {
1059 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1061 New(403, fd_sets[i], growsize, char);
1062 for (offset = 0; offset < growsize; offset += masksize) {
1063 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1064 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1067 fd_sets[i] = SvPVX(sv);
1071 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1072 /* Can't make just the (void*) conditional because that would be
1073 * cpp #if within cpp macro, and not all compilers like that. */
1074 nfound = PerlSock_select(
1076 (Select_fd_set_t) fd_sets[1],
1077 (Select_fd_set_t) fd_sets[2],
1078 (Select_fd_set_t) fd_sets[3],
1079 (void*) tbuf); /* Workaround for compiler bug. */
1081 nfound = PerlSock_select(
1083 (Select_fd_set_t) fd_sets[1],
1084 (Select_fd_set_t) fd_sets[2],
1085 (Select_fd_set_t) fd_sets[3],
1088 for (i = 1; i <= 3; i++) {
1091 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1093 for (offset = 0; offset < growsize; offset += masksize) {
1094 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1095 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1097 Safefree(fd_sets[i]);
1104 if (GIMME == G_ARRAY && tbuf) {
1105 value = (NV)(timebuf.tv_sec) +
1106 (NV)(timebuf.tv_usec) / 1000000.0;
1107 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1108 sv_setnv(sv, value);
1112 DIE(aTHX_ "select not implemented");
1117 Perl_setdefout(pTHX_ GV *gv)
1120 (void)SvREFCNT_inc(gv);
1122 SvREFCNT_dec(PL_defoutgv);
1129 GV *newdefout, *egv;
1132 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1134 egv = GvEGV(PL_defoutgv);
1139 XPUSHs(&PL_sv_undef);
1141 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1142 if (gvp && *gvp == egv) {
1143 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1147 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1152 if (!GvIO(newdefout))
1153 gv_IOadd(newdefout);
1154 setdefout(newdefout);
1172 if (gv && (io = GvIO(gv))
1173 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1175 I32 gimme = GIMME_V;
1177 XPUSHs(SvTIED_obj((SV*)io, mg));
1180 call_method("GETC", gimme);
1183 if (gimme == G_SCALAR)
1184 SvSetMagicSV_nosteal(TARG, TOPs);
1187 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1188 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1189 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1190 report_evil_fh(gv, io, PL_op->op_type);
1191 SETERRNO(EBADF,RMS_IFI);
1195 sv_setpv(TARG, " ");
1196 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1197 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1198 /* Find out how many bytes the char needs */
1199 Size_t len = UTF8SKIP(SvPVX(TARG));
1202 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1203 SvCUR_set(TARG,1+len);
1213 return pp_sysread();
1217 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1219 register PERL_CONTEXT *cx;
1220 I32 gimme = GIMME_V;
1226 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1228 PAD_SET_CUR(CvPADLIST(cv), 1);
1230 setdefout(gv); /* locally select filehandle so $% et al work */
1263 SV *tmpsv = sv_newmortal();
1264 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1265 name = SvPV_nolen(tmpsv);
1268 DIE(aTHX_ "Undefined format \"%s\" called", name);
1269 DIE(aTHX_ "Not a format reference");
1272 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1274 IoFLAGS(io) &= ~IOf_DIDTOP;
1275 return doform(cv,gv,PL_op->op_next);
1281 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1282 register IO *io = GvIOp(gv);
1283 PerlIO *ofp = IoOFP(io);
1287 register PERL_CONTEXT *cx;
1289 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1290 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1293 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1294 PL_formtarget != PL_toptarget)
1298 if (!IoTOP_GV(io)) {
1302 if (!IoTOP_NAME(io)) {
1303 if (!IoFMT_NAME(io))
1304 IoFMT_NAME(io) = savepv(GvNAME(gv));
1305 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", IoFMT_NAME(io)));
1306 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1307 if ((topgv && GvFORM(topgv)) ||
1308 !gv_fetchpv("top",FALSE,SVt_PVFM))
1309 IoTOP_NAME(io) = savepv(SvPVX(topname));
1311 IoTOP_NAME(io) = savepv("top");
1313 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1314 if (!topgv || !GvFORM(topgv)) {
1315 IoLINES_LEFT(io) = 100000000;
1318 IoTOP_GV(io) = topgv;
1320 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1321 I32 lines = IoLINES_LEFT(io);
1322 char *s = SvPVX(PL_formtarget);
1323 if (lines <= 0) /* Yow, header didn't even fit!!! */
1325 while (lines-- > 0) {
1326 s = strchr(s, '\n');
1332 STRLEN save = SvCUR(PL_formtarget);
1333 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1334 do_print(PL_formtarget, ofp);
1335 SvCUR_set(PL_formtarget, save);
1336 sv_chop(PL_formtarget, s);
1337 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1340 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1341 do_print(PL_formfeed, ofp);
1342 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1344 PL_formtarget = PL_toptarget;
1345 IoFLAGS(io) |= IOf_DIDTOP;
1348 DIE(aTHX_ "bad top format reference");
1353 SV *sv = sv_newmortal();
1354 gv_efullname4(sv, fgv, Nullch, FALSE);
1355 name = SvPV_nolen(sv);
1358 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1361 DIE(aTHX_ "Undefined top format called");
1365 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1366 return doform(cv,gv,PL_op);
1370 POPBLOCK(cx,PL_curpm);
1376 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1378 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1379 else if (ckWARN(WARN_CLOSED))
1380 report_evil_fh(gv, io, PL_op->op_type);
1385 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1386 if (ckWARN(WARN_IO))
1387 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1389 if (!do_print(PL_formtarget, fp))
1392 FmLINES(PL_formtarget) = 0;
1393 SvCUR_set(PL_formtarget, 0);
1394 *SvEND(PL_formtarget) = '\0';
1395 if (IoFLAGS(io) & IOf_FLUSH)
1396 (void)PerlIO_flush(fp);
1401 PL_formtarget = PL_bodytarget;
1403 return pop_return();
1408 dSP; dMARK; dORIGMARK;
1415 if (PL_op->op_flags & OPf_STACKED)
1420 if (gv && (io = GvIO(gv))
1421 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1423 if (MARK == ORIGMARK) {
1426 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1430 *MARK = SvTIED_obj((SV*)io, mg);
1433 call_method("PRINTF", G_SCALAR);
1436 MARK = ORIGMARK + 1;
1443 if (!(io = GvIO(gv))) {
1444 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1445 report_evil_fh(gv, io, PL_op->op_type);
1446 SETERRNO(EBADF,RMS_IFI);
1449 else if (!(fp = IoOFP(io))) {
1450 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1452 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1453 else if (ckWARN(WARN_CLOSED))
1454 report_evil_fh(gv, io, PL_op->op_type);
1456 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1460 do_sprintf(sv, SP - MARK, MARK + 1);
1461 if (!do_print(sv, fp))
1464 if (IoFLAGS(io) & IOf_FLUSH)
1465 if (PerlIO_flush(fp) == EOF)
1476 PUSHs(&PL_sv_undef);
1497 /* Need TIEHANDLE method ? */
1499 tmps = SvPV(sv, len);
1500 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1501 IoLINES(GvIOp(gv)) = 0;
1505 PUSHs(&PL_sv_undef);
1512 dSP; dMARK; dORIGMARK; dTARGET;
1519 Sock_size_t bufsize;
1526 bool charstart = FALSE;
1527 STRLEN charskip = 0;
1531 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1532 && gv && (io = GvIO(gv))
1533 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1538 *MARK = SvTIED_obj((SV*)io, mg);
1540 call_method("READ", G_SCALAR);
1553 sv_setpvn(bufsv, "", 0);
1554 length = SvIVx(*++MARK);
1557 offset = SvIVx(*++MARK);
1561 if (!io || !IoIFP(io)) {
1562 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1563 report_evil_fh(gv, io, PL_op->op_type);
1564 SETERRNO(EBADF,RMS_IFI);
1567 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1568 buffer = SvPVutf8_force(bufsv, blen);
1569 /* UTF8 may not have been set if they are all low bytes */
1573 buffer = SvPV_force(bufsv, blen);
1576 DIE(aTHX_ "Negative length");
1584 if (PL_op->op_type == OP_RECV) {
1585 char namebuf[MAXPATHLEN];
1586 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1587 bufsize = sizeof (struct sockaddr_in);
1589 bufsize = sizeof namebuf;
1591 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1595 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1596 /* 'offset' means 'flags' here */
1597 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1598 (struct sockaddr *)namebuf, &bufsize);
1602 /* Bogus return without padding */
1603 bufsize = sizeof (struct sockaddr_in);
1605 SvCUR_set(bufsv, count);
1606 *SvEND(bufsv) = '\0';
1607 (void)SvPOK_only(bufsv);
1611 /* This should not be marked tainted if the fp is marked clean */
1612 if (!(IoFLAGS(io) & IOf_UNTAINT))
1613 SvTAINTED_on(bufsv);
1615 sv_setpvn(TARG, namebuf, bufsize);
1620 if (PL_op->op_type == OP_RECV)
1621 DIE(aTHX_ PL_no_sock_func, "recv");
1623 if (DO_UTF8(bufsv)) {
1624 /* offset adjust in characters not bytes */
1625 blen = sv_len_utf8(bufsv);
1628 if (-offset > (int)blen)
1629 DIE(aTHX_ "Offset outside string");
1632 if (DO_UTF8(bufsv)) {
1633 /* convert offset-as-chars to offset-as-bytes */
1634 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1637 bufsize = SvCUR(bufsv);
1638 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1639 if (offset > bufsize) { /* Zero any newly allocated space */
1640 Zero(buffer+bufsize, offset-bufsize, char);
1642 buffer = buffer + offset;
1644 if (PL_op->op_type == OP_SYSREAD) {
1645 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1646 if (IoTYPE(io) == IoTYPE_SOCKET) {
1647 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1653 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1658 #ifdef HAS_SOCKET__bad_code_maybe
1659 if (IoTYPE(io) == IoTYPE_SOCKET) {
1660 char namebuf[MAXPATHLEN];
1661 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1662 bufsize = sizeof (struct sockaddr_in);
1664 bufsize = sizeof namebuf;
1666 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1667 (struct sockaddr *)namebuf, &bufsize);
1672 count = PerlIO_read(IoIFP(io), buffer, length);
1673 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1674 if (count == 0 && PerlIO_error(IoIFP(io)))
1678 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1679 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1682 SvCUR_set(bufsv, count+(buffer - SvPVX(bufsv)));
1683 *SvEND(bufsv) = '\0';
1684 (void)SvPOK_only(bufsv);
1685 if (fp_utf8 && !IN_BYTES) {
1686 /* Look at utf8 we got back and count the characters */
1687 char *bend = buffer + count;
1688 while (buffer < bend) {
1690 skip = UTF8SKIP(buffer);
1693 if (buffer - charskip + skip > bend) {
1694 /* partial character - try for rest of it */
1695 length = skip - (bend-buffer);
1696 offset = bend - SvPVX(bufsv);
1708 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1709 provided amount read (count) was what was requested (length)
1711 if (got < wanted && count == length) {
1712 length = wanted - got;
1713 offset = bend - SvPVX(bufsv);
1716 /* return value is character count */
1721 /* This should not be marked tainted if the fp is marked clean */
1722 if (!(IoFLAGS(io) & IOf_UNTAINT))
1723 SvTAINTED_on(bufsv);
1736 int items = (SP - PL_stack_base) - TOPMARK;
1740 sv = sv_2mortal(newSViv(sv_len(*SP)));
1749 dSP; dMARK; dORIGMARK; dTARGET;
1760 if (PL_op->op_type == OP_SYSWRITE
1761 && gv && (io = GvIO(gv))
1762 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1767 *MARK = SvTIED_obj((SV*)io, mg);
1769 call_method("WRITE", G_SCALAR);
1780 #if Size_t_size > IVSIZE
1781 length = (Size_t)SvNVx(*++MARK);
1783 length = (Size_t)SvIVx(*++MARK);
1785 if ((SSize_t)length < 0)
1786 DIE(aTHX_ "Negative length");
1789 if (!io || !IoIFP(io)) {
1791 if (ckWARN(WARN_CLOSED))
1792 report_evil_fh(gv, io, PL_op->op_type);
1793 SETERRNO(EBADF,RMS_IFI);
1797 if (PerlIO_isutf8(IoIFP(io))) {
1798 buffer = SvPVutf8(bufsv, blen);
1802 sv_utf8_downgrade(bufsv, FALSE);
1803 buffer = SvPV(bufsv, blen);
1806 if (PL_op->op_type == OP_SYSWRITE) {
1808 if (DO_UTF8(bufsv)) {
1809 /* length and offset are in chars */
1810 blen = sv_len_utf8(bufsv);
1813 offset = SvIVx(*++MARK);
1815 if (-offset > (IV)blen)
1816 DIE(aTHX_ "Offset outside string");
1818 } else if (offset >= (IV)blen && blen > 0)
1819 DIE(aTHX_ "Offset outside string");
1822 if (length > blen - offset)
1823 length = blen - offset;
1824 if (DO_UTF8(bufsv)) {
1825 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1826 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1829 buffer = buffer+offset;
1831 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1832 if (IoTYPE(io) == IoTYPE_SOCKET) {
1833 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1839 /* See the note at doio.c:do_print about filesize limits. --jhi */
1840 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1845 else if (SP > MARK) {
1848 sockbuf = SvPVx(*++MARK, mlen);
1849 /* length is really flags */
1850 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1851 length, (struct sockaddr *)sockbuf, mlen);
1854 /* length is really flags */
1855 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1858 DIE(aTHX_ PL_no_sock_func, "send");
1864 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1865 #if Size_t_size > IVSIZE
1879 return pp_sysread();
1890 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1892 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1894 if (io && !IoIFP(io)) {
1895 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1897 IoFLAGS(io) &= ~IOf_START;
1898 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1899 sv_setpvn(GvSV(gv), "-", 1);
1900 SvSETMAGIC(GvSV(gv));
1902 else if (!nextargv(gv))
1907 gv = PL_last_in_gv; /* eof */
1910 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1912 if (gv && (io = GvIO(gv))
1913 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1916 XPUSHs(SvTIED_obj((SV*)io, mg));
1919 call_method("EOF", G_SCALAR);
1925 PUSHs(boolSV(!gv || do_eof(gv)));
1939 gv = PL_last_in_gv = (GV*)POPs;
1941 if (gv && (io = GvIO(gv))
1942 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1945 XPUSHs(SvTIED_obj((SV*)io, mg));
1948 call_method("TELL", G_SCALAR);
1954 #if LSEEKSIZE > IVSIZE
1955 PUSHn( do_tell(gv) );
1957 PUSHi( do_tell(gv) );
1964 return pp_sysseek();
1973 #if LSEEKSIZE > IVSIZE
1974 Off_t offset = (Off_t)SvNVx(POPs);
1976 Off_t offset = (Off_t)SvIVx(POPs);
1980 gv = PL_last_in_gv = (GV*)POPs;
1982 if (gv && (io = GvIO(gv))
1983 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1986 XPUSHs(SvTIED_obj((SV*)io, mg));
1987 #if LSEEKSIZE > IVSIZE
1988 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
1990 XPUSHs(sv_2mortal(newSViv(offset)));
1992 XPUSHs(sv_2mortal(newSViv(whence)));
1995 call_method("SEEK", G_SCALAR);
2001 if (PL_op->op_type == OP_SEEK)
2002 PUSHs(boolSV(do_seek(gv, offset, whence)));
2004 Off_t sought = do_sysseek(gv, offset, whence);
2006 PUSHs(&PL_sv_undef);
2009 #if LSEEKSIZE > IVSIZE
2014 : newSVpvn(zero_but_true, ZBTLEN);
2015 PUSHs(sv_2mortal(sv));
2024 /* There seems to be no consensus on the length type of truncate()
2025 * and ftruncate(), both off_t and size_t have supporters. In
2026 * general one would think that when using large files, off_t is
2027 * at least as wide as size_t, so using an off_t should be okay. */
2028 /* XXX Configure probe for the length type of *truncate() needed XXX */
2031 #if Off_t_size > IVSIZE
2036 /* Checking for length < 0 is problematic as the type might or
2037 * might not be signed: if it is not, clever compilers will moan. */
2038 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2040 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2047 if (PL_op->op_flags & OPf_SPECIAL) {
2048 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2057 TAINT_PROPER("truncate");
2058 if (!(fp = IoIFP(io))) {
2064 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2066 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2076 if (SvTYPE(sv) == SVt_PVGV) {
2077 tmpgv = (GV*)sv; /* *main::FRED for example */
2078 goto do_ftruncate_gv;
2080 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2081 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2082 goto do_ftruncate_gv;
2084 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2085 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2086 goto do_ftruncate_io;
2089 name = SvPV(sv, n_a);
2090 TAINT_PROPER("truncate");
2092 if (truncate(name, len) < 0)
2098 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2101 if (my_chsize(tmpfd, len) < 0)
2103 PerlLIO_close(tmpfd);
2112 SETERRNO(EBADF,RMS_IFI);
2116 DIE(aTHX_ "truncate not implemented");
2129 unsigned int func = POPu;
2130 int optype = PL_op->op_type;
2134 IO *io = gv ? GvIOn(gv) : 0;
2136 if (!io || !argsv || !IoIFP(io)) {
2137 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2138 report_evil_fh(gv, io, PL_op->op_type);
2139 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2143 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2146 s = SvPV_force(argsv, len);
2147 need = IOCPARM_LEN(func);
2149 s = Sv_Grow(argsv, need + 1);
2150 SvCUR_set(argsv, need);
2153 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2156 retval = SvIV(argsv);
2157 s = INT2PTR(char*,retval); /* ouch */
2160 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2162 if (optype == OP_IOCTL)
2164 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2166 DIE(aTHX_ "ioctl is not implemented");
2170 DIE(aTHX_ "fcntl is not implemented");
2172 #if defined(OS2) && defined(__EMX__)
2173 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2175 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2179 if (s[SvCUR(argsv)] != 17)
2180 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2182 s[SvCUR(argsv)] = 0; /* put our null back */
2183 SvSETMAGIC(argsv); /* Assume it has changed */
2192 PUSHp(zero_but_true, ZBTLEN);
2213 if (gv && (io = GvIO(gv)))
2220 (void)PerlIO_flush(fp);
2221 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2224 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2225 report_evil_fh(gv, io, PL_op->op_type);
2227 SETERRNO(EBADF,RMS_IFI);
2232 DIE(aTHX_ PL_no_func, "flock()");
2244 int protocol = POPi;
2250 io = gv ? GvIOn(gv) : NULL;
2253 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2254 report_evil_fh(gv, io, PL_op->op_type);
2256 do_close(gv, FALSE);
2257 SETERRNO(EBADF,LIB_INVARG);
2262 do_close(gv, FALSE);
2264 TAINT_PROPER("socket");
2265 fd = PerlSock_socket(domain, type, protocol);
2268 IoIFP(io) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE); /* stdio gets confused about sockets */
2269 IoOFP(io) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2270 IoTYPE(io) = IoTYPE_SOCKET;
2271 if (!IoIFP(io) || !IoOFP(io)) {
2272 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2273 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2274 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2277 #if defined(HAS_FCNTL) && defined(F_SETFD)
2278 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2282 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2287 DIE(aTHX_ PL_no_sock_func, "socket");
2293 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2299 int protocol = POPi;
2306 io1 = gv1 ? GvIOn(gv1) : NULL;
2307 io2 = gv2 ? GvIOn(gv2) : NULL;
2308 if (!gv1 || !gv2 || !io1 || !io2) {
2309 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2311 report_evil_fh(gv1, io1, PL_op->op_type);
2313 report_evil_fh(gv1, io2, PL_op->op_type);
2316 do_close(gv1, FALSE);
2318 do_close(gv2, FALSE);
2323 do_close(gv1, FALSE);
2325 do_close(gv2, FALSE);
2327 TAINT_PROPER("socketpair");
2328 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2330 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
2331 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"PIPESOCK_MODE);
2332 IoTYPE(io1) = IoTYPE_SOCKET;
2333 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"PIPESOCK_MODE);
2334 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
2335 IoTYPE(io2) = IoTYPE_SOCKET;
2336 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2337 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2338 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2339 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2340 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2341 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2342 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2345 #if defined(HAS_FCNTL) && defined(F_SETFD)
2346 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2347 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2352 DIE(aTHX_ PL_no_sock_func, "socketpair");
2360 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2361 extern void GETPRIVMODE();
2362 extern void GETUSERMODE();
2367 register IO *io = GvIOn(gv);
2371 int mpeprivmode = 0;
2374 if (!io || !IoIFP(io))
2377 addr = SvPV(addrsv, len);
2378 TAINT_PROPER("bind");
2379 #ifdef MPE /* Deal with MPE bind() peculiarities */
2380 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2381 /* The address *MUST* stupidly be zero. */
2382 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2383 /* PRIV mode is required to bind() to ports < 1024. */
2384 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2385 ((struct sockaddr_in *)addr)->sin_port > 0) {
2386 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2391 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2392 (struct sockaddr *)addr, len) >= 0)
2395 #ifdef MPE /* Switch back to USER mode */
2406 if (ckWARN(WARN_CLOSED))
2407 report_evil_fh(gv, io, PL_op->op_type);
2408 SETERRNO(EBADF,SS_IVCHAN);
2411 DIE(aTHX_ PL_no_sock_func, "bind");
2422 register IO *io = GvIOn(gv);
2425 if (!io || !IoIFP(io))
2428 addr = SvPV(addrsv, len);
2429 TAINT_PROPER("connect");
2430 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2436 if (ckWARN(WARN_CLOSED))
2437 report_evil_fh(gv, io, PL_op->op_type);
2438 SETERRNO(EBADF,SS_IVCHAN);
2441 DIE(aTHX_ PL_no_sock_func, "connect");
2451 register IO *io = gv ? GvIOn(gv) : NULL;
2453 if (!gv || !io || !IoIFP(io))
2456 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2462 if (ckWARN(WARN_CLOSED))
2463 report_evil_fh(gv, io, PL_op->op_type);
2464 SETERRNO(EBADF,SS_IVCHAN);
2467 DIE(aTHX_ PL_no_sock_func, "listen");
2479 struct sockaddr saddr; /* use a struct to avoid alignment problems */
2480 Sock_size_t len = sizeof saddr;
2492 if (!gstio || !IoIFP(gstio))
2496 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *)&saddr, &len);
2500 do_close(ngv, FALSE);
2501 IoIFP(nstio) = PerlIO_fdopen(fd, "r"PIPESOCK_MODE);
2502 IoOFP(nstio) = PerlIO_fdopen(fd, "w"PIPESOCK_MODE);
2503 IoTYPE(nstio) = IoTYPE_SOCKET;
2504 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2505 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2506 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2507 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2510 #if defined(HAS_FCNTL) && defined(F_SETFD)
2511 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2515 len = sizeof saddr; /* EPOC somehow truncates info */
2516 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2519 PUSHp((char *)&saddr, len);
2523 if (ckWARN(WARN_CLOSED))
2524 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2525 SETERRNO(EBADF,SS_IVCHAN);
2531 DIE(aTHX_ PL_no_sock_func, "accept");
2541 register IO *io = GvIOn(gv);
2543 if (!io || !IoIFP(io))
2546 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2550 if (ckWARN(WARN_CLOSED))
2551 report_evil_fh(gv, io, PL_op->op_type);
2552 SETERRNO(EBADF,SS_IVCHAN);
2555 DIE(aTHX_ PL_no_sock_func, "shutdown");
2562 return pp_ssockopt();
2564 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2572 int optype = PL_op->op_type;
2575 unsigned int optname;
2581 if (optype == OP_GSOCKOPT)
2582 sv = sv_2mortal(NEWSV(22, 257));
2585 optname = (unsigned int) POPi;
2586 lvl = (unsigned int) POPi;
2590 if (!io || !IoIFP(io))
2593 fd = PerlIO_fileno(IoIFP(io));
2597 (void)SvPOK_only(sv);
2601 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2616 aint = (int)SvIV(sv);
2620 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2629 if (ckWARN(WARN_CLOSED))
2630 report_evil_fh(gv, io, optype);
2631 SETERRNO(EBADF,SS_IVCHAN);
2636 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2643 return pp_getpeername();
2645 DIE(aTHX_ PL_no_sock_func, "getsockname");
2653 int optype = PL_op->op_type;
2657 register IO *io = GvIOn(gv);
2660 if (!io || !IoIFP(io))
2663 sv = sv_2mortal(NEWSV(22, 257));
2664 (void)SvPOK_only(sv);
2668 fd = PerlIO_fileno(IoIFP(io));
2670 case OP_GETSOCKNAME:
2671 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2674 case OP_GETPEERNAME:
2675 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2677 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2679 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";
2680 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2681 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2682 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2683 sizeof(u_short) + sizeof(struct in_addr))) {
2690 #ifdef BOGUS_GETNAME_RETURN
2691 /* Interactive Unix, getpeername() and getsockname()
2692 does not return valid namelen */
2693 if (len == BOGUS_GETNAME_RETURN)
2694 len = sizeof(struct sockaddr);
2702 if (ckWARN(WARN_CLOSED))
2703 report_evil_fh(gv, io, optype);
2704 SETERRNO(EBADF,SS_IVCHAN);
2709 DIE(aTHX_ PL_no_sock_func, "getpeername");
2728 if (PL_op->op_flags & OPf_REF) {
2730 if (PL_op->op_type == OP_LSTAT) {
2731 if (gv != PL_defgv) {
2732 if (ckWARN(WARN_IO))
2733 Perl_warner(aTHX_ packWARN(WARN_IO),
2734 "lstat() on filehandle %s", GvENAME(gv));
2735 } else if (PL_laststype != OP_LSTAT)
2736 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2740 if (gv != PL_defgv) {
2741 PL_laststype = OP_STAT;
2743 sv_setpv(PL_statname, "");
2744 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2745 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2747 if (PL_laststatval < 0) {
2748 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2749 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2755 if (SvTYPE(sv) == SVt_PVGV) {
2759 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2761 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2762 Perl_warner(aTHX_ packWARN(WARN_IO),
2763 "lstat() on filehandle %s", GvENAME(gv));
2766 sv_setpv(PL_statname, SvPV(sv,n_a));
2769 PL_laststype = PL_op->op_type;
2770 if (PL_op->op_type == OP_LSTAT)
2771 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2774 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2775 if (PL_laststatval < 0) {
2776 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2777 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2783 if (gimme != G_ARRAY) {
2784 if (gimme != G_VOID)
2785 XPUSHs(boolSV(max));
2791 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2792 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2793 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2794 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2795 #if Uid_t_size > IVSIZE
2796 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2798 # if Uid_t_sign <= 0
2799 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2801 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2804 #if Gid_t_size > IVSIZE
2805 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2807 # if Gid_t_sign <= 0
2808 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2810 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2813 #ifdef USE_STAT_RDEV
2814 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2816 PUSHs(sv_2mortal(newSVpvn("", 0)));
2818 #if Off_t_size > IVSIZE
2819 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2821 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2824 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2825 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2826 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2828 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2829 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2830 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2832 #ifdef USE_STAT_BLOCKS
2833 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2834 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2836 PUSHs(sv_2mortal(newSVpvn("", 0)));
2837 PUSHs(sv_2mortal(newSVpvn("", 0)));
2847 #if defined(HAS_ACCESS) && defined(R_OK)
2849 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2850 result = access(TOPpx, R_OK);
2865 if (cando(S_IRUSR, 0, &PL_statcache))
2874 #if defined(HAS_ACCESS) && defined(W_OK)
2876 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2877 result = access(TOPpx, W_OK);
2892 if (cando(S_IWUSR, 0, &PL_statcache))
2901 #if defined(HAS_ACCESS) && defined(X_OK)
2903 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2904 result = access(TOPpx, X_OK);
2919 if (cando(S_IXUSR, 0, &PL_statcache))
2928 #ifdef PERL_EFF_ACCESS_R_OK
2930 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2931 result = PERL_EFF_ACCESS_R_OK(TOPpx);
2946 if (cando(S_IRUSR, 1, &PL_statcache))
2955 #ifdef PERL_EFF_ACCESS_W_OK
2957 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2958 result = PERL_EFF_ACCESS_W_OK(TOPpx);
2973 if (cando(S_IWUSR, 1, &PL_statcache))
2982 #ifdef PERL_EFF_ACCESS_X_OK
2984 if ((PL_hints & HINT_FILETEST_ACCESS) && SvPOK(TOPs)) {
2985 result = PERL_EFF_ACCESS_X_OK(TOPpx);
3000 if (cando(S_IXUSR, 1, &PL_statcache))
3007 I32 result = my_stat();
3016 return pp_ftrowned();
3021 I32 result = my_stat();
3025 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3033 I32 result = my_stat();
3037 if (PL_statcache.st_size == 0)
3044 I32 result = my_stat();
3048 #if Off_t_size > IVSIZE
3049 PUSHn(PL_statcache.st_size);
3051 PUSHi(PL_statcache.st_size);
3058 I32 result = my_stat();
3062 PUSHn( (PL_basetime - PL_statcache.st_mtime) / 86400.0 );
3068 I32 result = my_stat();
3072 PUSHn( (PL_basetime - PL_statcache.st_atime) / 86400.0 );
3078 I32 result = my_stat();
3082 PUSHn( (PL_basetime - PL_statcache.st_ctime) / 86400.0 );
3088 I32 result = my_stat();
3092 if (S_ISSOCK(PL_statcache.st_mode))
3099 I32 result = my_stat();
3103 if (S_ISCHR(PL_statcache.st_mode))
3110 I32 result = my_stat();
3114 if (S_ISBLK(PL_statcache.st_mode))
3121 I32 result = my_stat();
3125 if (S_ISREG(PL_statcache.st_mode))
3132 I32 result = my_stat();
3136 if (S_ISDIR(PL_statcache.st_mode))
3143 I32 result = my_stat();
3147 if (S_ISFIFO(PL_statcache.st_mode))
3154 I32 result = my_lstat();
3158 if (S_ISLNK(PL_statcache.st_mode))
3167 I32 result = my_stat();
3171 if (PL_statcache.st_mode & S_ISUID)
3181 I32 result = my_stat();
3185 if (PL_statcache.st_mode & S_ISGID)
3195 I32 result = my_stat();
3199 if (PL_statcache.st_mode & S_ISVTX)
3210 char *tmps = Nullch;
3213 if (PL_op->op_flags & OPf_REF)
3215 else if (isGV(TOPs))
3217 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3218 gv = (GV*)SvRV(POPs);
3220 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3222 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3223 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3224 else if (tmps && isDIGIT(*tmps))
3228 if (PerlLIO_isatty(fd))
3233 #if defined(atarist) /* this will work with atariST. Configure will
3234 make guesses for other systems. */
3235 # define FILE_base(f) ((f)->_base)
3236 # define FILE_ptr(f) ((f)->_ptr)
3237 # define FILE_cnt(f) ((f)->_cnt)
3238 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3248 register STDCHAR *s;
3255 if (PL_op->op_flags & OPf_REF)
3257 else if (isGV(TOPs))
3259 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3260 gv = (GV*)SvRV(POPs);
3266 if (gv == PL_defgv) {
3268 io = GvIO(PL_statgv);
3271 goto really_filename;
3276 PL_laststatval = -1;
3277 sv_setpv(PL_statname, "");
3278 io = GvIO(PL_statgv);
3280 if (io && IoIFP(io)) {
3281 if (! PerlIO_has_base(IoIFP(io)))
3282 DIE(aTHX_ "-T and -B not implemented on filehandles");
3283 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3284 if (PL_laststatval < 0)
3286 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3287 if (PL_op->op_type == OP_FTTEXT)
3292 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3293 i = PerlIO_getc(IoIFP(io));
3295 (void)PerlIO_ungetc(IoIFP(io),i);
3297 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3299 len = PerlIO_get_bufsiz(IoIFP(io));
3300 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3301 /* sfio can have large buffers - limit to 512 */
3306 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3308 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3310 SETERRNO(EBADF,RMS_IFI);
3318 PL_laststatval = -1;
3319 PL_laststype = OP_STAT;
3320 sv_setpv(PL_statname, SvPV(sv, n_a));
3321 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3322 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
3323 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3326 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3327 if (PL_laststatval < 0) {
3328 (void)PerlIO_close(fp);
3331 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3332 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3333 (void)PerlIO_close(fp);
3335 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3336 RETPUSHNO; /* special case NFS directories */
3337 RETPUSHYES; /* null file is anything */
3342 /* now scan s to look for textiness */
3343 /* XXX ASCII dependent code */
3345 #if defined(DOSISH) || defined(USEMYBINMODE)
3346 /* ignore trailing ^Z on short files */
3347 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3351 for (i = 0; i < len; i++, s++) {
3352 if (!*s) { /* null never allowed in text */
3357 else if (!(isPRINT(*s) || isSPACE(*s)))
3360 else if (*s & 128) {
3362 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3365 /* utf8 characters don't count as odd */
3366 if (UTF8_IS_START(*s)) {
3367 int ulen = UTF8SKIP(s);
3368 if (ulen < len - i) {
3370 for (j = 1; j < ulen; j++) {
3371 if (!UTF8_IS_CONTINUATION(s[j]))
3374 --ulen; /* loop does extra increment */
3384 *s != '\n' && *s != '\r' && *s != '\b' &&
3385 *s != '\t' && *s != '\f' && *s != 27)
3390 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3415 if( !tmps || !*tmps ) {
3416 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3417 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3419 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3424 deprecate("chdir('') or chdir(undef) as chdir()");
3425 tmps = SvPV(*svp, n_a);
3429 TAINT_PROPER("chdir");
3434 TAINT_PROPER("chdir");
3435 PUSHi( PerlDir_chdir(tmps) >= 0 );
3437 /* Clear the DEFAULT element of ENV so we'll get the new value
3439 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3447 dSP; dMARK; dTARGET;
3448 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3454 DIE(aTHX_ PL_no_func, "chown");
3464 TAINT_PROPER("chroot");
3465 PUSHi( chroot(tmps) >= 0 );
3468 DIE(aTHX_ PL_no_func, "chroot");
3474 dSP; dMARK; dTARGET;
3476 value = (I32)apply(PL_op->op_type, MARK, SP);
3484 dSP; dMARK; dTARGET;
3486 value = (I32)apply(PL_op->op_type, MARK, SP);
3494 dSP; dMARK; dTARGET;
3496 value = (I32)apply(PL_op->op_type, MARK, SP);
3508 char *tmps2 = POPpx;
3509 char *tmps = SvPV(TOPs, n_a);
3510 TAINT_PROPER("rename");
3512 anum = PerlLIO_rename(tmps, tmps2);
3514 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3515 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3518 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3519 (void)UNLINK(tmps2);
3520 if (!(anum = link(tmps, tmps2)))
3521 anum = UNLINK(tmps);
3534 char *tmps2 = POPpx;
3535 char *tmps = SvPV(TOPs, n_a);
3536 TAINT_PROPER("link");
3537 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3540 DIE(aTHX_ PL_no_func, "link");
3549 char *tmps2 = POPpx;
3550 char *tmps = SvPV(TOPs, n_a);
3551 TAINT_PROPER("symlink");
3552 SETi( symlink(tmps, tmps2) >= 0 );
3555 DIE(aTHX_ PL_no_func, "symlink");
3565 char buf[MAXPATHLEN];
3569 #ifndef INCOMPLETE_TAINTS
3573 len = readlink(tmps, buf, sizeof(buf) - 1);
3581 RETSETUNDEF; /* just pretend it's a normal file */
3585 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3587 S_dooneliner(pTHX_ char *cmd, char *filename)
3589 char *save_filename = filename;
3595 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3596 strcpy(cmdline, cmd);
3597 strcat(cmdline, " ");
3598 for (s = cmdline + strlen(cmdline); *filename; ) {
3603 myfp = PerlProc_popen(cmdline, "r");
3607 SV *tmpsv = sv_newmortal();
3608 /* Need to save/restore 'PL_rs' ?? */
3609 s = sv_gets(tmpsv, myfp, 0);
3610 (void)PerlProc_pclose(myfp);
3614 #ifdef HAS_SYS_ERRLIST
3619 /* you don't see this */
3621 #ifdef HAS_SYS_ERRLIST
3629 if (instr(s, errmsg)) {
3636 #define EACCES EPERM
3638 if (instr(s, "cannot make"))
3639 SETERRNO(EEXIST,RMS_FEX);
3640 else if (instr(s, "existing file"))
3641 SETERRNO(EEXIST,RMS_FEX);
3642 else if (instr(s, "ile exists"))
3643 SETERRNO(EEXIST,RMS_FEX);
3644 else if (instr(s, "non-exist"))
3645 SETERRNO(ENOENT,RMS_FNF);
3646 else if (instr(s, "does not exist"))
3647 SETERRNO(ENOENT,RMS_FNF);
3648 else if (instr(s, "not empty"))
3649 SETERRNO(EBUSY,SS_DEVOFFLINE);
3650 else if (instr(s, "cannot access"))
3651 SETERRNO(EACCES,RMS_PRV);
3653 SETERRNO(EPERM,RMS_PRV);
3656 else { /* some mkdirs return no failure indication */
3657 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3658 if (PL_op->op_type == OP_RMDIR)
3663 SETERRNO(EACCES,RMS_PRV); /* a guess */
3672 /* This macro removes trailing slashes from a directory name.
3673 * Different operating and file systems take differently to
3674 * trailing slashes. According to POSIX 1003.1 1996 Edition
3675 * any number of trailing slashes should be allowed.
3676 * Thusly we snip them away so that even non-conforming
3677 * systems are happy.
3678 * We should probably do this "filtering" for all
3679 * the functions that expect (potentially) directory names:
3680 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3681 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3683 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3684 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3687 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3688 (tmps) = savepvn((tmps), (len)); \
3708 TRIMSLASHES(tmps,len,copy);
3710 TAINT_PROPER("mkdir");
3712 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3714 SETi( dooneliner("mkdir", tmps) );
3715 oldumask = PerlLIO_umask(0);
3716 PerlLIO_umask(oldumask);
3717 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3731 TRIMSLASHES(tmps,len,copy);
3732 TAINT_PROPER("rmdir");
3734 SETi( PerlDir_rmdir(tmps) >= 0 );
3736 SETi( dooneliner("rmdir", tmps) );
3743 /* Directory calls. */
3747 #if defined(Direntry_t) && defined(HAS_READDIR)
3750 char *dirname = POPpx;
3752 register IO *io = GvIOn(gv);
3758 PerlDir_close(IoDIRP(io));
3759 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3765 SETERRNO(EBADF,RMS_DIR);
3768 DIE(aTHX_ PL_no_dir_func, "opendir");
3774 #if defined(Direntry_t) && defined(HAS_READDIR)
3776 #if !defined(I_DIRENT) && !defined(VMS)
3777 Direntry_t *readdir (DIR *);
3779 register Direntry_t *dp;
3781 register IO *io = GvIOn(gv);
3784 if (!io || !IoDIRP(io))
3787 if (GIMME == G_ARRAY) {
3789 while ((dp = (Direntry_t *)PerlDir_read(IoDIRP(io)))) {
3791 sv = newSVpvn(dp->d_name, dp->d_namlen);
3793 sv = newSVpv(dp->d_name, 0);
3795 #ifndef INCOMPLETE_TAINTS
3796 if (!(IoFLAGS(io) & IOf_UNTAINT))
3799 XPUSHs(sv_2mortal(sv));
3803 if (!(dp = (Direntry_t *)PerlDir_read(IoDIRP(io))))
3806 sv = newSVpvn(dp->d_name, dp->d_namlen);
3808 sv = newSVpv(dp->d_name, 0);
3810 #ifndef INCOMPLETE_TAINTS
3811 if (!(IoFLAGS(io) & IOf_UNTAINT))
3814 XPUSHs(sv_2mortal(sv));
3820 SETERRNO(EBADF,RMS_ISI);
3821 if (GIMME == G_ARRAY)
3826 DIE(aTHX_ PL_no_dir_func, "readdir");
3832 #if defined(HAS_TELLDIR) || defined(telldir)
3834 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3835 /* XXX netbsd still seemed to.
3836 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3837 --JHI 1999-Feb-02 */
3838 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3839 long telldir (DIR *);
3842 register IO *io = GvIOn(gv);
3844 if (!io || !IoDIRP(io))
3847 PUSHi( PerlDir_tell(IoDIRP(io)) );
3851 SETERRNO(EBADF,RMS_ISI);
3854 DIE(aTHX_ PL_no_dir_func, "telldir");
3860 #if defined(HAS_SEEKDIR) || defined(seekdir)
3864 register IO *io = GvIOn(gv);
3866 if (!io || !IoDIRP(io))
3869 (void)PerlDir_seek(IoDIRP(io), along);
3874 SETERRNO(EBADF,RMS_ISI);
3877 DIE(aTHX_ PL_no_dir_func, "seekdir");
3883 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3886 register IO *io = GvIOn(gv);
3888 if (!io || !IoDIRP(io))
3891 (void)PerlDir_rewind(IoDIRP(io));
3895 SETERRNO(EBADF,RMS_ISI);
3898 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3904 #if defined(Direntry_t) && defined(HAS_READDIR)
3907 register IO *io = GvIOn(gv);
3909 if (!io || !IoDIRP(io))
3912 #ifdef VOID_CLOSEDIR
3913 PerlDir_close(IoDIRP(io));
3915 if (PerlDir_close(IoDIRP(io)) < 0) {
3916 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3925 SETERRNO(EBADF,RMS_IFI);
3928 DIE(aTHX_ PL_no_dir_func, "closedir");
3932 /* Process control. */
3942 PERL_FLUSHALL_FOR_CHILD;
3943 childpid = PerlProc_fork();
3948 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
3949 SvREADONLY_off(GvSV(tmpgv));
3950 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3951 SvREADONLY_on(GvSV(tmpgv));
3953 #ifdef THREADS_HAVE_PIDS
3954 PL_ppid = (IV)getppid();
3956 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3961 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3966 PERL_FLUSHALL_FOR_CHILD;
3967 childpid = PerlProc_fork();
3973 DIE(aTHX_ PL_no_func, "fork");
3980 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3985 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3986 childpid = wait4pid(-1, &argflags, 0);
3988 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3993 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3994 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3995 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
3997 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4002 DIE(aTHX_ PL_no_func, "wait");
4008 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4016 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4017 childpid = wait4pid(childpid, &argflags, optype);
4019 while ((childpid = wait4pid(childpid, &argflags, optype)) == -1 &&
4024 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4025 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4026 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4028 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4033 DIE(aTHX_ PL_no_func, "waitpid");
4039 dSP; dMARK; dORIGMARK; dTARGET;
4047 while (++MARK <= SP) {
4048 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4053 TAINT_PROPER("system");
4055 PERL_FLUSHALL_FOR_CHILD;
4056 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4061 if (PerlProc_pipe(pp) >= 0)
4063 while ((childpid = PerlProc_fork()) == -1) {
4064 if (errno != EAGAIN) {
4069 PerlLIO_close(pp[0]);
4070 PerlLIO_close(pp[1]);
4077 Sigsave_t ihand,qhand; /* place to save signals during system() */
4081 PerlLIO_close(pp[1]);
4083 rsignal_save(SIGINT, SIG_IGN, &ihand);
4084 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4087 result = wait4pid(childpid, &status, 0);
4088 } while (result == -1 && errno == EINTR);
4090 (void)rsignal_restore(SIGINT, &ihand);
4091 (void)rsignal_restore(SIGQUIT, &qhand);
4093 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4094 do_execfree(); /* free any memory child malloced on fork */
4100 while (n < sizeof(int)) {
4101 n1 = PerlLIO_read(pp[0],
4102 (void*)(((char*)&errkid)+n),
4108 PerlLIO_close(pp[0]);
4109 if (n) { /* Error */
4110 if (n != sizeof(int))
4111 DIE(aTHX_ "panic: kid popen errno read");
4112 errno = errkid; /* Propagate errno from kid */
4113 STATUS_CURRENT = -1;
4116 PUSHi(STATUS_CURRENT);
4120 PerlLIO_close(pp[0]);
4121 #if defined(HAS_FCNTL) && defined(F_SETFD)
4122 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4125 if (PL_op->op_flags & OPf_STACKED) {
4126 SV *really = *++MARK;
4127 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4129 else if (SP - MARK != 1)
4130 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4132 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4136 #else /* ! FORK or VMS or OS/2 */
4139 if (PL_op->op_flags & OPf_STACKED) {
4140 SV *really = *++MARK;
4142 value = (I32)do_aspawn(really, MARK, SP);
4144 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4147 else if (SP - MARK != 1) {
4149 value = (I32)do_aspawn(Nullsv, MARK, SP);
4151 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4155 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4157 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4159 STATUS_NATIVE_SET(value);
4162 PUSHi(result ? value : STATUS_CURRENT);
4163 #endif /* !FORK or VMS */
4169 dSP; dMARK; dORIGMARK; dTARGET;
4175 while (++MARK <= SP) {
4176 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4181 TAINT_PROPER("exec");
4183 PERL_FLUSHALL_FOR_CHILD;
4184 if (PL_op->op_flags & OPf_STACKED) {
4185 SV *really = *++MARK;
4186 value = (I32)do_aexec(really, MARK, SP);
4188 else if (SP - MARK != 1)
4190 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4194 (void ) do_aspawn(Nullsv, MARK, SP);
4198 value = (I32)do_aexec(Nullsv, MARK, SP);
4203 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4206 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4209 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4222 dSP; dMARK; dTARGET;
4224 value = (I32)apply(PL_op->op_type, MARK, SP);
4229 DIE(aTHX_ PL_no_func, "kill");
4237 # ifdef THREADS_HAVE_PIDS
4240 XPUSHi( getppid() );
4244 DIE(aTHX_ PL_no_func, "getppid");
4260 pgrp = (I32)BSD_GETPGRP(pid);
4262 if (pid != 0 && pid != PerlProc_getpid())
4263 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4269 DIE(aTHX_ PL_no_func, "getpgrp()");
4288 TAINT_PROPER("setpgrp");
4290 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4292 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4293 || (pid != 0 && pid != PerlProc_getpid()))
4295 DIE(aTHX_ "setpgrp can't take arguments");
4297 SETi( setpgrp() >= 0 );
4298 #endif /* USE_BSDPGRP */
4301 DIE(aTHX_ PL_no_func, "setpgrp()");
4307 #ifdef HAS_GETPRIORITY
4311 SETi( getpriority(which, who) );
4314 DIE(aTHX_ PL_no_func, "getpriority()");
4320 #ifdef HAS_SETPRIORITY
4325 TAINT_PROPER("setpriority");
4326 SETi( setpriority(which, who, niceval) >= 0 );
4329 DIE(aTHX_ PL_no_func, "setpriority()");
4339 XPUSHn( time(Null(Time_t*)) );
4341 XPUSHi( time(Null(Time_t*)) );
4352 (void)PerlProc_times(&PL_timesbuf);
4354 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4355 /* struct tms, though same data */
4359 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4360 if (GIMME == G_ARRAY) {
4361 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4362 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4363 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4367 DIE(aTHX_ "times not implemented");
4368 #endif /* HAS_TIMES */
4381 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4382 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4383 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4389 when = (Time_t)SvNVx(POPs);
4391 when = (Time_t)SvIVx(POPs);
4394 if (PL_op->op_type == OP_LOCALTIME)
4395 tmbuf = localtime(&when);
4397 tmbuf = gmtime(&when);
4399 if (GIMME != G_ARRAY) {
4405 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4406 dayname[tmbuf->tm_wday],
4407 monname[tmbuf->tm_mon],
4412 tmbuf->tm_year + 1900);
4413 PUSHs(sv_2mortal(tsv));
4418 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4419 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4420 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4421 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4422 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4423 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4424 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4425 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4426 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4437 anum = alarm((unsigned int)anum);
4444 DIE(aTHX_ PL_no_func, "alarm");
4455 (void)time(&lasttime);
4460 PerlProc_sleep((unsigned int)duration);
4463 XPUSHi(when - lasttime);
4467 /* Shared memory. */
4481 return pp_shmwrite();
4486 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4487 dSP; dMARK; dTARGET;
4488 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4497 /* Message passing. */
4511 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4512 dSP; dMARK; dTARGET;
4513 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4524 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4525 dSP; dMARK; dTARGET;
4526 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4539 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4540 dSP; dMARK; dTARGET;
4541 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4548 DIE(aTHX_ "System V IPC is not implemented on this machine");
4554 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4555 dSP; dMARK; dTARGET;
4556 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4564 PUSHp(zero_but_true, ZBTLEN);
4574 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4575 dSP; dMARK; dTARGET;
4576 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4585 /* Get system info. */
4589 #ifdef HAS_GETHOSTBYNAME
4590 return pp_ghostent();
4592 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4598 #ifdef HAS_GETHOSTBYADDR
4599 return pp_ghostent();
4601 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4607 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4609 I32 which = PL_op->op_type;
4610 register char **elem;
4612 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4613 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4614 struct hostent *gethostbyname(Netdb_name_t);
4615 struct hostent *gethostent(void);
4617 struct hostent *hent;
4622 if (which == OP_GHBYNAME) {
4623 #ifdef HAS_GETHOSTBYNAME
4624 char* name = POPpbytex;
4625 hent = PerlSock_gethostbyname(name);
4627 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4630 else if (which == OP_GHBYADDR) {
4631 #ifdef HAS_GETHOSTBYADDR
4632 int addrtype = POPi;
4635 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4637 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4639 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4643 #ifdef HAS_GETHOSTENT
4644 hent = PerlSock_gethostent();
4646 DIE(aTHX_ PL_no_sock_func, "gethostent");
4649 #ifdef HOST_NOT_FOUND
4651 #ifdef USE_REENTRANT_API
4652 # ifdef USE_GETHOSTENT_ERRNO
4653 h_errno = PL_reentrant_buffer->_gethostent_errno;
4656 STATUS_NATIVE_SET(h_errno);
4660 if (GIMME != G_ARRAY) {
4661 PUSHs(sv = sv_newmortal());
4663 if (which == OP_GHBYNAME) {
4665 sv_setpvn(sv, hent->h_addr, hent->h_length);
4668 sv_setpv(sv, (char*)hent->h_name);
4674 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4675 sv_setpv(sv, (char*)hent->h_name);
4676 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4677 for (elem = hent->h_aliases; elem && *elem; elem++) {
4678 sv_catpv(sv, *elem);
4680 sv_catpvn(sv, " ", 1);
4682 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4683 sv_setiv(sv, (IV)hent->h_addrtype);
4684 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4685 len = hent->h_length;
4686 sv_setiv(sv, (IV)len);
4688 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4689 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4690 sv_setpvn(sv, *elem, len);
4693 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4695 sv_setpvn(sv, hent->h_addr, len);
4700 DIE(aTHX_ PL_no_sock_func, "gethostent");
4706 #ifdef HAS_GETNETBYNAME
4707 return pp_gnetent();
4709 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4715 #ifdef HAS_GETNETBYADDR
4716 return pp_gnetent();
4718 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4724 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4726 I32 which = PL_op->op_type;
4727 register char **elem;
4729 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4730 struct netent *getnetbyaddr(Netdb_net_t, int);
4731 struct netent *getnetbyname(Netdb_name_t);
4732 struct netent *getnetent(void);
4734 struct netent *nent;
4737 if (which == OP_GNBYNAME){
4738 #ifdef HAS_GETNETBYNAME
4739 char *name = POPpbytex;
4740 nent = PerlSock_getnetbyname(name);
4742 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4745 else if (which == OP_GNBYADDR) {
4746 #ifdef HAS_GETNETBYADDR
4747 int addrtype = POPi;
4748 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4749 nent = PerlSock_getnetbyaddr(addr, addrtype);
4751 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4755 #ifdef HAS_GETNETENT
4756 nent = PerlSock_getnetent();
4758 DIE(aTHX_ PL_no_sock_func, "getnetent");
4761 #ifdef HOST_NOT_FOUND
4763 #ifdef USE_REENTRANT_API
4764 # ifdef USE_GETNETENT_ERRNO
4765 h_errno = PL_reentrant_buffer->_getnetent_errno;
4768 STATUS_NATIVE_SET(h_errno);
4773 if (GIMME != G_ARRAY) {
4774 PUSHs(sv = sv_newmortal());
4776 if (which == OP_GNBYNAME)
4777 sv_setiv(sv, (IV)nent->n_net);
4779 sv_setpv(sv, nent->n_name);
4785 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4786 sv_setpv(sv, nent->n_name);
4787 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4788 for (elem = nent->n_aliases; elem && *elem; elem++) {
4789 sv_catpv(sv, *elem);
4791 sv_catpvn(sv, " ", 1);
4793 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4794 sv_setiv(sv, (IV)nent->n_addrtype);
4795 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4796 sv_setiv(sv, (IV)nent->n_net);
4801 DIE(aTHX_ PL_no_sock_func, "getnetent");
4807 #ifdef HAS_GETPROTOBYNAME
4808 return pp_gprotoent();
4810 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4816 #ifdef HAS_GETPROTOBYNUMBER
4817 return pp_gprotoent();
4819 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4825 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4827 I32 which = PL_op->op_type;
4828 register char **elem;
4830 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4831 struct protoent *getprotobyname(Netdb_name_t);
4832 struct protoent *getprotobynumber(int);
4833 struct protoent *getprotoent(void);
4835 struct protoent *pent;
4838 if (which == OP_GPBYNAME) {
4839 #ifdef HAS_GETPROTOBYNAME
4840 char* name = POPpbytex;
4841 pent = PerlSock_getprotobyname(name);
4843 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4846 else if (which == OP_GPBYNUMBER) {
4847 #ifdef HAS_GETPROTOBYNUMBER
4849 pent = PerlSock_getprotobynumber(number);
4851 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4855 #ifdef HAS_GETPROTOENT
4856 pent = PerlSock_getprotoent();
4858 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4862 if (GIMME != G_ARRAY) {
4863 PUSHs(sv = sv_newmortal());
4865 if (which == OP_GPBYNAME)
4866 sv_setiv(sv, (IV)pent->p_proto);
4868 sv_setpv(sv, pent->p_name);
4874 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4875 sv_setpv(sv, pent->p_name);
4876 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4877 for (elem = pent->p_aliases; elem && *elem; elem++) {
4878 sv_catpv(sv, *elem);
4880 sv_catpvn(sv, " ", 1);
4882 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4883 sv_setiv(sv, (IV)pent->p_proto);
4888 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4894 #ifdef HAS_GETSERVBYNAME
4895 return pp_gservent();
4897 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4903 #ifdef HAS_GETSERVBYPORT
4904 return pp_gservent();
4906 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4912 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4914 I32 which = PL_op->op_type;
4915 register char **elem;
4917 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4918 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4919 struct servent *getservbyport(int, Netdb_name_t);
4920 struct servent *getservent(void);
4922 struct servent *sent;
4925 if (which == OP_GSBYNAME) {
4926 #ifdef HAS_GETSERVBYNAME
4927 char *proto = POPpbytex;
4928 char *name = POPpbytex;
4930 if (proto && !*proto)
4933 sent = PerlSock_getservbyname(name, proto);
4935 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4938 else if (which == OP_GSBYPORT) {
4939 #ifdef HAS_GETSERVBYPORT
4940 char *proto = POPpbytex;
4941 unsigned short port = (unsigned short)POPu;
4943 if (proto && !*proto)
4947 port = PerlSock_htons(port);
4949 sent = PerlSock_getservbyport(port, proto);
4951 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4955 #ifdef HAS_GETSERVENT
4956 sent = PerlSock_getservent();
4958 DIE(aTHX_ PL_no_sock_func, "getservent");
4962 if (GIMME != G_ARRAY) {
4963 PUSHs(sv = sv_newmortal());
4965 if (which == OP_GSBYNAME) {
4967 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4969 sv_setiv(sv, (IV)(sent->s_port));
4973 sv_setpv(sv, sent->s_name);
4979 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4980 sv_setpv(sv, sent->s_name);
4981 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4982 for (elem = sent->s_aliases; elem && *elem; elem++) {
4983 sv_catpv(sv, *elem);
4985 sv_catpvn(sv, " ", 1);
4987 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4989 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4991 sv_setiv(sv, (IV)(sent->s_port));
4993 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4994 sv_setpv(sv, sent->s_proto);
4999 DIE(aTHX_ PL_no_sock_func, "getservent");
5005 #ifdef HAS_SETHOSTENT
5007 PerlSock_sethostent(TOPi);
5010 DIE(aTHX_ PL_no_sock_func, "sethostent");
5016 #ifdef HAS_SETNETENT
5018 PerlSock_setnetent(TOPi);
5021 DIE(aTHX_ PL_no_sock_func, "setnetent");
5027 #ifdef HAS_SETPROTOENT
5029 PerlSock_setprotoent(TOPi);
5032 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5038 #ifdef HAS_SETSERVENT
5040 PerlSock_setservent(TOPi);
5043 DIE(aTHX_ PL_no_sock_func, "setservent");
5049 #ifdef HAS_ENDHOSTENT
5051 PerlSock_endhostent();
5055 DIE(aTHX_ PL_no_sock_func, "endhostent");
5061 #ifdef HAS_ENDNETENT
5063 PerlSock_endnetent();
5067 DIE(aTHX_ PL_no_sock_func, "endnetent");
5073 #ifdef HAS_ENDPROTOENT
5075 PerlSock_endprotoent();
5079 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5085 #ifdef HAS_ENDSERVENT
5087 PerlSock_endservent();
5091 DIE(aTHX_ PL_no_sock_func, "endservent");
5100 DIE(aTHX_ PL_no_func, "getpwnam");
5109 DIE(aTHX_ PL_no_func, "getpwuid");
5117 I32 which = PL_op->op_type;
5120 struct passwd *pwent = NULL;
5122 * We currently support only the SysV getsp* shadow password interface.
5123 * The interface is declared in <shadow.h> and often one needs to link
5124 * with -lsecurity or some such.
5125 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5128 * AIX getpwnam() is clever enough to return the encrypted password
5129 * only if the caller (euid?) is root.
5131 * There are at least two other shadow password APIs. Many platforms
5132 * seem to contain more than one interface for accessing the shadow
5133 * password databases, possibly for compatibility reasons.
5134 * The getsp*() is by far he simplest one, the other two interfaces
5135 * are much more complicated, but also very similar to each other.
5140 * struct pr_passwd *getprpw*();
5141 * The password is in
5142 * char getprpw*(...).ufld.fd_encrypt[]
5143 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5148 * struct es_passwd *getespw*();
5149 * The password is in
5150 * char *(getespw*(...).ufld.fd_encrypt)
5151 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5153 * Mention I_PROT here so that Configure probes for it.
5155 * In HP-UX for getprpw*() the manual page claims that one should include
5156 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5157 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5158 * and pp_sys.c already includes <shadow.h> if there is such.
5160 * Note that <sys/security.h> is already probed for, but currently
5161 * it is only included in special cases.
5163 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5164 * be preferred interface, even though also the getprpw*() interface
5165 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5166 * One also needs to call set_auth_parameters() in main() before
5167 * doing anything else, whether one is using getespw*() or getprpw*().
5169 * Note that accessing the shadow databases can be magnitudes
5170 * slower than accessing the standard databases.
5178 char* name = POPpbytex;
5179 pwent = getpwnam(name);
5185 pwent = getpwuid(uid);
5189 # ifdef HAS_GETPWENT
5191 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5192 if (pwent) pwent = getpwnam(pwent->pw_name);
5195 DIE(aTHX_ PL_no_func, "getpwent");
5201 if (GIMME != G_ARRAY) {
5202 PUSHs(sv = sv_newmortal());
5204 if (which == OP_GPWNAM)
5205 # if Uid_t_sign <= 0
5206 sv_setiv(sv, (IV)pwent->pw_uid);
5208 sv_setuv(sv, (UV)pwent->pw_uid);
5211 sv_setpv(sv, pwent->pw_name);
5217 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5218 sv_setpv(sv, pwent->pw_name);
5220 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5222 /* If we have getspnam(), we try to dig up the shadow
5223 * password. If we are underprivileged, the shadow
5224 * interface will set the errno to EACCES or similar,
5225 * and return a null pointer. If this happens, we will
5226 * use the dummy password (usually "*" or "x") from the
5227 * standard password database.
5229 * In theory we could skip the shadow call completely
5230 * if euid != 0 but in practice we cannot know which
5231 * security measures are guarding the shadow databases
5232 * on a random platform.
5234 * Resist the urge to use additional shadow interfaces.
5235 * Divert the urge to writing an extension instead.
5238 # ifdef HAS_GETSPNAM
5240 struct spwd *spwent;
5241 int saverrno; /* Save and restore errno so that
5242 * underprivileged attempts seem
5243 * to have never made the unsccessful
5244 * attempt to retrieve the shadow password. */
5247 spwent = getspnam(pwent->pw_name);
5249 if (spwent && spwent->sp_pwdp)
5250 sv_setpv(sv, spwent->sp_pwdp);
5254 if (!SvPOK(sv)) /* Use the standard password, then. */
5255 sv_setpv(sv, pwent->pw_passwd);
5258 # ifndef INCOMPLETE_TAINTS
5259 /* passwd is tainted because user himself can diddle with it.
5260 * admittedly not much and in a very limited way, but nevertheless. */
5264 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5265 # if Uid_t_sign <= 0
5266 sv_setiv(sv, (IV)pwent->pw_uid);
5268 sv_setuv(sv, (UV)pwent->pw_uid);
5271 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5272 # if Uid_t_sign <= 0
5273 sv_setiv(sv, (IV)pwent->pw_gid);
5275 sv_setuv(sv, (UV)pwent->pw_gid);
5277 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5278 * because of the poor interface of the Perl getpw*(),
5279 * not because there's some standard/convention saying so.
5280 * A better interface would have been to return a hash,
5281 * but we are accursed by our history, alas. --jhi. */
5282 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5284 sv_setiv(sv, (IV)pwent->pw_change);
5287 sv_setiv(sv, (IV)pwent->pw_quota);
5290 sv_setpv(sv, pwent->pw_age);
5295 /* pw_class and pw_comment are mutually exclusive--.
5296 * see the above note for pw_change, pw_quota, and pw_age. */
5297 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5299 sv_setpv(sv, pwent->pw_class);
5302 sv_setpv(sv, pwent->pw_comment);
5306 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5308 sv_setpv(sv, pwent->pw_gecos);
5310 # ifndef INCOMPLETE_TAINTS
5311 /* pw_gecos is tainted because user himself can diddle with it. */
5315 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5316 sv_setpv(sv, pwent->pw_dir);
5318 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5319 sv_setpv(sv, pwent->pw_shell);
5320 # ifndef INCOMPLETE_TAINTS
5321 /* pw_shell is tainted because user himself can diddle with it. */
5326 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5327 sv_setiv(sv, (IV)pwent->pw_expire);
5332 DIE(aTHX_ PL_no_func, "getpwent");
5338 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5343 DIE(aTHX_ PL_no_func, "setpwent");
5349 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5354 DIE(aTHX_ PL_no_func, "endpwent");
5363 DIE(aTHX_ PL_no_func, "getgrnam");
5372 DIE(aTHX_ PL_no_func, "getgrgid");
5380 I32 which = PL_op->op_type;
5381 register char **elem;
5383 struct group *grent;
5386 if (which == OP_GGRNAM) {
5387 char* name = POPpbytex;
5388 grent = (struct group *)getgrnam(name);
5390 else if (which == OP_GGRGID) {
5392 grent = (struct group *)getgrgid(gid);
5396 grent = (struct group *)getgrent();
5398 DIE(aTHX_ PL_no_func, "getgrent");
5402 if (GIMME != G_ARRAY) {
5403 PUSHs(sv = sv_newmortal());
5405 if (which == OP_GGRNAM)
5406 sv_setiv(sv, (IV)grent->gr_gid);
5408 sv_setpv(sv, grent->gr_name);
5414 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5415 sv_setpv(sv, grent->gr_name);
5417 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5419 sv_setpv(sv, grent->gr_passwd);
5422 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5423 sv_setiv(sv, (IV)grent->gr_gid);
5425 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5426 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5427 /* In UNICOS/mk (_CRAYMPP) the multithreading
5428 * versions (getgrnam_r, getgrgid_r)
5429 * seem to return an illegal pointer
5430 * as the group members list, gr_mem.
5431 * getgrent() doesn't even have a _r version
5432 * but the gr_mem is poisonous anyway.
5433 * So yes, you cannot get the list of group
5434 * members if building multithreaded in UNICOS/mk. */
5435 for (elem = grent->gr_mem; elem && *elem; elem++) {
5436 sv_catpv(sv, *elem);
5438 sv_catpvn(sv, " ", 1);
5445 DIE(aTHX_ PL_no_func, "getgrent");
5451 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5456 DIE(aTHX_ PL_no_func, "setgrent");
5462 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5467 DIE(aTHX_ PL_no_func, "endgrent");
5477 if (!(tmps = PerlProc_getlogin()))
5479 PUSHp(tmps, strlen(tmps));
5482 DIE(aTHX_ PL_no_func, "getlogin");
5486 /* Miscellaneous. */
5491 dSP; dMARK; dORIGMARK; dTARGET;
5492 register I32 items = SP - MARK;
5493 unsigned long a[20];
5499 while (++MARK <= SP) {
5500 if (SvTAINTED(*MARK)) {
5506 TAINT_PROPER("syscall");
5509 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5510 * or where sizeof(long) != sizeof(char*). But such machines will
5511 * not likely have syscall implemented either, so who cares?
5513 while (++MARK <= SP) {
5514 if (SvNIOK(*MARK) || !i)
5515 a[i++] = SvIV(*MARK);
5516 else if (*MARK == &PL_sv_undef)
5519 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5525 DIE(aTHX_ "Too many args to syscall");
5527 DIE(aTHX_ "Too few args to syscall");
5529 retval = syscall(a[0]);
5532 retval = syscall(a[0],a[1]);
5535 retval = syscall(a[0],a[1],a[2]);
5538 retval = syscall(a[0],a[1],a[2],a[3]);
5541 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5544 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5547 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5550 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5554 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5557 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5560 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5564 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5568 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5572 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5573 a[10],a[11],a[12],a[13]);
5575 #endif /* atarist */
5581 DIE(aTHX_ PL_no_func, "syscall");
5585 #ifdef FCNTL_EMULATE_FLOCK
5587 /* XXX Emulate flock() with fcntl().
5588 What's really needed is a good file locking module.
5592 fcntl_emulate_flock(int fd, int operation)
5596 switch (operation & ~LOCK_NB) {
5598 flock.l_type = F_RDLCK;
5601 flock.l_type = F_WRLCK;
5604 flock.l_type = F_UNLCK;
5610 flock.l_whence = SEEK_SET;
5611 flock.l_start = flock.l_len = (Off_t)0;
5613 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5616 #endif /* FCNTL_EMULATE_FLOCK */
5618 #ifdef LOCKF_EMULATE_FLOCK
5620 /* XXX Emulate flock() with lockf(). This is just to increase
5621 portability of scripts. The calls are not completely
5622 interchangeable. What's really needed is a good file
5626 /* The lockf() constants might have been defined in <unistd.h>.
5627 Unfortunately, <unistd.h> causes troubles on some mixed
5628 (BSD/POSIX) systems, such as SunOS 4.1.3.
5630 Further, the lockf() constants aren't POSIX, so they might not be
5631 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5632 just stick in the SVID values and be done with it. Sigh.
5636 # define F_ULOCK 0 /* Unlock a previously locked region */
5639 # define F_LOCK 1 /* Lock a region for exclusive use */
5642 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5645 # define F_TEST 3 /* Test a region for other processes locks */
5649 lockf_emulate_flock(int fd, int operation)
5655 /* flock locks entire file so for lockf we need to do the same */
5657 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5658 if (pos > 0) /* is seekable and needs to be repositioned */
5659 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5660 pos = -1; /* seek failed, so don't seek back afterwards */
5663 switch (operation) {
5665 /* LOCK_SH - get a shared lock */
5667 /* LOCK_EX - get an exclusive lock */
5669 i = lockf (fd, F_LOCK, 0);
5672 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5673 case LOCK_SH|LOCK_NB:
5674 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5675 case LOCK_EX|LOCK_NB:
5676 i = lockf (fd, F_TLOCK, 0);
5678 if ((errno == EAGAIN) || (errno == EACCES))
5679 errno = EWOULDBLOCK;
5682 /* LOCK_UN - unlock (non-blocking is a no-op) */
5684 case LOCK_UN|LOCK_NB:
5685 i = lockf (fd, F_ULOCK, 0);
5688 /* Default - can't decipher operation */
5695 if (pos > 0) /* need to restore position of the handle */
5696 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5701 #endif /* LOCKF_EMULATE_FLOCK */