3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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.
18 /* This file contains system pp ("push/pop") functions that
19 * execute the opcodes that make up a perl program. A typical pp function
20 * expects to find its arguments on the stack, and usually pushes its
21 * results onto the stack, hence the 'pp' terminology. Each OP structure
22 * contains a pointer to the relevant pp_foo() function.
24 * By 'system', we mean ops which interact with the OS, such as pp_open().
28 #define PERL_IN_PP_SYS_C
32 /* Shadow password support for solaris - pdo@cs.umd.edu
33 * Not just Solaris: at least HP-UX, IRIX, Linux.
34 * The API is from SysV.
36 * There are at least two more shadow interfaces,
37 * see the comments in pp_gpwent().
41 /* There is a MAXINT coming from <shadow.h> <- <hpsecurity.h> <- <values.h>
42 * and another MAXINT from "perl.h" <- <sys/param.h>. */
50 extern "C" int syscall(unsigned long,...);
55 # include <sys/wait.h>
59 # include <sys/resource.h>
68 # include <sys/select.h>
72 /* XXX Configure test needed.
73 h_errno might not be a simple 'int', especially for multi-threaded
74 applications, see "extern int errno in perl.h". Creating such
75 a test requires taking into account the differences between
76 compiling multithreaded and singlethreaded ($ccflags et al).
77 HOST_NOT_FOUND is typically defined in <netdb.h>.
79 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
88 struct passwd *getpwnam (char *);
89 struct passwd *getpwuid (Uid_t);
94 struct passwd *getpwent (void);
95 #elif defined (VMS) && defined (my_getpwent)
96 struct passwd *Perl_my_getpwent (void);
105 struct group *getgrnam (char *);
106 struct group *getgrgid (Gid_t);
110 struct group *getgrent (void);
116 # if defined(_MSC_VER) || defined(__MINGW32__)
117 # include <sys/utime.h>
124 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
127 # define my_chsize PerlLIO_chsize
132 #else /* no flock() */
134 /* fcntl.h might not have been included, even if it exists, because
135 the current Configure only sets I_FCNTL if it's needed to pick up
136 the *_OK constants. Make sure it has been included before testing
137 the fcntl() locking constants. */
138 # if defined(HAS_FCNTL) && !defined(I_FCNTL)
142 # if defined(HAS_FCNTL) && defined(FCNTL_CAN_LOCK)
143 # define FLOCK fcntl_emulate_flock
144 # define FCNTL_EMULATE_FLOCK
145 # else /* no flock() or fcntl(F_SETLK,...) */
147 # define FLOCK lockf_emulate_flock
148 # define LOCKF_EMULATE_FLOCK
150 # endif /* no flock() or fcntl(F_SETLK,...) */
153 static int FLOCK (int, int);
156 * These are the flock() constants. Since this sytems doesn't have
157 * flock(), the values of the constants are probably not available.
171 # endif /* emulating flock() */
173 #endif /* no flock() */
176 static char zero_but_true[ZBTLEN + 1] = "0 but true";
178 #if defined(I_SYS_ACCESS) && !defined(R_OK)
179 # include <sys/access.h>
182 #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
183 # define FD_CLOEXEC 1 /* NeXT needs this */
189 /* Missing protos on LynxOS */
190 void sethostent(int);
191 void endhostent(void);
193 void endnetent(void);
194 void setprotoent(int);
195 void endprotoent(void);
196 void setservent(int);
197 void endservent(void);
200 #undef PERL_EFF_ACCESS_R_OK /* EFFective uid/gid ACCESS R_OK */
201 #undef PERL_EFF_ACCESS_W_OK
202 #undef PERL_EFF_ACCESS_X_OK
204 /* F_OK unused: if stat() cannot find it... */
206 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
207 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
208 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
209 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
210 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
213 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
214 # ifdef I_SYS_SECURITY
215 # include <sys/security.h>
219 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
220 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
221 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
224 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
225 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
226 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
230 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
232 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
234 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 Uid_t ruid = getuid();
245 Uid_t euid = geteuid();
246 Gid_t rgid = getgid();
247 Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
301 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
302 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
305 #if !defined(PERL_EFF_ACCESS_R_OK)
306 /* With it or without it: anyway you get a warning: either that
307 it is unused, or it is declared static and never defined.
310 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
312 Perl_croak(aTHX_ "switching effective uid is not implemented");
328 if (PL_op->op_private & OPpOPEN_IN_RAW)
330 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
332 fp = PerlProc_popen(tmps, mode);
335 if (PL_curcop->cop_io) {
336 type = SvPV_nolen(PL_curcop->cop_io);
339 PerlIO_apply_layers(aTHX_ fp,mode,type);
341 if (gimme == G_VOID) {
343 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
347 else if (gimme == G_SCALAR) {
349 PL_rs = &PL_sv_undef;
350 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
351 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
363 if (sv_gets(sv, fp, 0) == Nullch) {
367 XPUSHs(sv_2mortal(sv));
368 if (SvLEN(sv) - SvCUR(sv) > 20) {
369 SvLEN_set(sv, SvCUR(sv)+1);
370 Renew(SvPVX(sv), SvLEN(sv), char);
375 STATUS_NATIVE_SET(PerlProc_pclose(fp));
376 TAINT; /* "I believe that this is not gratuitous!" */
379 STATUS_NATIVE_SET(-1);
380 if (gimme == G_SCALAR)
390 tryAMAGICunTARGET(iter, -1);
392 /* Note that we only ever get here if File::Glob fails to load
393 * without at the same time croaking, for some reason, or if
394 * perl was built with PERL_EXTERNAL_GLOB */
401 * The external globbing program may use things we can't control,
402 * so for security reasons we must assume the worst.
405 taint_proper(PL_no_security, "glob");
409 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
410 PL_last_in_gv = (GV*)*PL_stack_sp--;
412 SAVESPTR(PL_rs); /* This is not permanent, either. */
413 PL_rs = sv_2mortal(newSVpvn("\000", 1));
416 *SvPVX(PL_rs) = '\n';
420 result = do_readline();
427 PL_last_in_gv = cGVOP_gv;
428 return do_readline();
437 if (SP - MARK != 1) {
439 do_join(TARG, &PL_sv_no, MARK, SP);
446 tmps = SvPV(tmpsv, len);
447 if ((!tmps || !len) && PL_errgv) {
449 (void)SvUPGRADE(error, SVt_PV);
450 if (SvPOK(error) && SvCUR(error))
451 sv_catpv(error, "\t...caught");
453 tmps = SvPV(tmpsv, len);
456 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
458 Perl_warn(aTHX_ "%"SVf, tmpsv);
470 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
472 if (SP - MARK != 1) {
474 do_join(TARG, &PL_sv_no, MARK, SP);
476 tmps = SvPV(tmpsv, len);
482 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
486 (void)SvUPGRADE(error, SVt_PV);
487 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
489 SvSetSV(error,tmpsv);
490 else if (sv_isobject(error)) {
491 HV *stash = SvSTASH(SvRV(error));
492 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
494 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
495 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
502 call_sv((SV*)GvCV(gv),
503 G_SCALAR|G_EVAL|G_KEEPERR);
504 sv_setsv(error,*PL_stack_sp--);
507 DIE(aTHX_ Nullformat);
510 if (SvPOK(error) && SvCUR(error))
511 sv_catpv(error, "\t...propagated");
513 tmps = SvPV(tmpsv, len);
517 tmpsv = sv_2mortal(newSVpvn("Died", 4));
519 DIE(aTHX_ "%"SVf, tmpsv);
539 DIE(aTHX_ PL_no_usym, "filehandle");
540 if ((io = GvIOp(gv)))
541 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
543 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
544 /* Method's args are same as ours ... */
545 /* ... except handle is replaced by the object */
546 *MARK-- = SvTIED_obj((SV*)io, mg);
550 call_method("OPEN", G_SCALAR);
563 tmps = SvPV(sv, len);
564 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
567 PUSHi( (I32)PL_forkprocess );
568 else if (PL_forkprocess == 0) /* we are a new child */
587 if (gv && (io = GvIO(gv))
588 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
591 XPUSHs(SvTIED_obj((SV*)io, mg));
594 call_method("CLOSE", G_SCALAR);
600 PUSHs(boolSV(do_close(gv, TRUE)));
620 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
621 DIE(aTHX_ PL_no_usym, "filehandle");
626 do_close(rgv, FALSE);
628 do_close(wgv, FALSE);
630 if (PerlProc_pipe(fd) < 0)
633 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
634 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
635 IoOFP(rstio) = IoIFP(rstio);
636 IoIFP(wstio) = IoOFP(wstio);
637 IoTYPE(rstio) = IoTYPE_RDONLY;
638 IoTYPE(wstio) = IoTYPE_WRONLY;
640 if (!IoIFP(rstio) || !IoOFP(wstio)) {
641 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
642 else PerlLIO_close(fd[0]);
643 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
644 else PerlLIO_close(fd[1]);
647 #if defined(HAS_FCNTL) && defined(F_SETFD)
648 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
649 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
656 DIE(aTHX_ PL_no_func, "pipe");
672 if (gv && (io = GvIO(gv))
673 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
676 XPUSHs(SvTIED_obj((SV*)io, mg));
679 call_method("FILENO", G_SCALAR);
685 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
686 /* Can't do this because people seem to do things like
687 defined(fileno($foo)) to check whether $foo is a valid fh.
688 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
689 report_evil_fh(gv, io, PL_op->op_type);
694 PUSHi(PerlIO_fileno(fp));
705 anum = PerlLIO_umask(0);
706 (void)PerlLIO_umask(anum);
709 anum = PerlLIO_umask(POPi);
710 TAINT_PROPER("umask");
713 /* Only DIE if trying to restrict permissions on `user' (self).
714 * Otherwise it's harmless and more useful to just return undef
715 * since 'group' and 'other' concepts probably don't exist here. */
716 if (MAXARG >= 1 && (POPi & 0700))
717 DIE(aTHX_ "umask not implemented");
718 XPUSHs(&PL_sv_undef);
740 if (gv && (io = GvIO(gv))
741 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
744 XPUSHs(SvTIED_obj((SV*)io, mg));
749 call_method("BINMODE", G_SCALAR);
756 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
757 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
758 report_evil_fh(gv, io, PL_op->op_type);
759 SETERRNO(EBADF,RMS_IFI);
764 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
765 (discp) ? SvPV_nolen(discp) : Nullch)) {
766 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
767 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
768 mode_from_discipline(discp),
769 (discp) ? SvPV_nolen(discp) : Nullch)) {
791 I32 markoff = MARK - PL_stack_base;
793 int how = PERL_MAGIC_tied;
797 switch(SvTYPE(varsv)) {
799 methname = "TIEHASH";
800 HvEITER((HV *)varsv) = Null(HE *);
803 methname = "TIEARRAY";
806 #ifdef GV_UNIQUE_CHECK
807 if (GvUNIQUE((GV*)varsv)) {
808 Perl_croak(aTHX_ "Attempt to tie unique GV");
811 methname = "TIEHANDLE";
812 how = PERL_MAGIC_tiedscalar;
813 /* For tied filehandles, we apply tiedscalar magic to the IO
814 slot of the GP rather than the GV itself. AMS 20010812 */
816 GvIOp(varsv) = newIO();
817 varsv = (SV *)GvIOp(varsv);
820 methname = "TIESCALAR";
821 how = PERL_MAGIC_tiedscalar;
825 if (sv_isobject(*MARK)) {
827 PUSHSTACKi(PERLSI_MAGIC);
829 EXTEND(SP,(I32)items);
833 call_method(methname, G_SCALAR);
836 /* Not clear why we don't call call_method here too.
837 * perhaps to get different error message ?
839 stash = gv_stashsv(*MARK, FALSE);
840 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
841 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
845 PUSHSTACKi(PERLSI_MAGIC);
847 EXTEND(SP,(I32)items);
851 call_sv((SV*)GvCV(gv), G_SCALAR);
857 if (sv_isobject(sv)) {
858 sv_unmagic(varsv, how);
859 /* Croak if a self-tie on an aggregate is attempted. */
860 if (varsv == SvRV(sv) &&
861 (SvTYPE(varsv) == SVt_PVAV ||
862 SvTYPE(varsv) == SVt_PVHV))
864 "Self-ties of arrays and hashes are not supported");
865 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
868 SP = PL_stack_base + markoff;
878 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
879 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
881 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
884 if ((mg = SvTIED_mg(sv, how))) {
885 SV *obj = SvRV(SvTIED_obj(sv, mg));
889 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
890 isGV(gv) && (cv = GvCV(gv))) {
892 XPUSHs(SvTIED_obj((SV*)gv, mg));
893 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
896 call_sv((SV *)cv, G_VOID);
900 else if (ckWARN(WARN_UNTIE)) {
901 if (mg && SvREFCNT(obj) > 1)
902 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
903 "untie attempted while %"UVuf" inner references still exist",
904 (UV)SvREFCNT(obj) - 1 ) ;
908 sv_unmagic(sv, how) ;
917 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
918 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
920 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
923 if ((mg = SvTIED_mg(sv, how))) {
924 SV *osv = SvTIED_obj(sv, mg);
925 if (osv == mg->mg_obj)
926 osv = sv_mortalcopy(osv);
944 sv = sv_mortalcopy(&PL_sv_no);
945 sv_setpv(sv, "AnyDBM_File");
946 stash = gv_stashsv(sv, FALSE);
947 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
949 require_pv("AnyDBM_File.pm");
951 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
952 DIE(aTHX_ "No dbm on this machine");
962 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
964 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
967 call_sv((SV*)GvCV(gv), G_SCALAR);
970 if (!sv_isobject(TOPs)) {
975 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
978 call_sv((SV*)GvCV(gv), G_SCALAR);
982 if (sv_isobject(TOPs)) {
983 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
984 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1006 struct timeval timebuf;
1007 struct timeval *tbuf = &timebuf;
1011 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1016 # if BYTEORDER & 0xf0000
1017 # define ORDERBYTE (0x88888888 - BYTEORDER)
1019 # define ORDERBYTE (0x4444 - BYTEORDER)
1025 for (i = 1; i <= 3; i++) {
1033 /* little endians can use vecs directly */
1034 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1041 masksize = NFDBITS / NBBY;
1043 masksize = sizeof(long); /* documented int, everyone seems to use long */
1045 Zero(&fd_sets[0], 4, char*);
1048 # if SELECT_MIN_BITS == 1
1049 growsize = sizeof(fd_set);
1051 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1052 # undef SELECT_MIN_BITS
1053 # define SELECT_MIN_BITS __FD_SETSIZE
1055 /* If SELECT_MIN_BITS is greater than one we most probably will want
1056 * to align the sizes with SELECT_MIN_BITS/8 because for example
1057 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1058 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1059 * on (sets/tests/clears bits) is 32 bits. */
1060 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1068 timebuf.tv_sec = (long)value;
1069 value -= (NV)timebuf.tv_sec;
1070 timebuf.tv_usec = (long)(value * 1000000.0);
1073 tbuf = Null(struct timeval*);
1075 for (i = 1; i <= 3; i++) {
1081 else if (!SvPOK(sv))
1082 SvPV_force(sv,n_a); /* force string conversion */
1085 Sv_Grow(sv, growsize);
1089 while (++j <= growsize) {
1093 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1095 New(403, fd_sets[i], growsize, char);
1096 for (offset = 0; offset < growsize; offset += masksize) {
1097 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1098 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1101 fd_sets[i] = SvPVX(sv);
1105 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1106 /* Can't make just the (void*) conditional because that would be
1107 * cpp #if within cpp macro, and not all compilers like that. */
1108 nfound = PerlSock_select(
1110 (Select_fd_set_t) fd_sets[1],
1111 (Select_fd_set_t) fd_sets[2],
1112 (Select_fd_set_t) fd_sets[3],
1113 (void*) tbuf); /* Workaround for compiler bug. */
1115 nfound = PerlSock_select(
1117 (Select_fd_set_t) fd_sets[1],
1118 (Select_fd_set_t) fd_sets[2],
1119 (Select_fd_set_t) fd_sets[3],
1122 for (i = 1; i <= 3; i++) {
1125 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1127 for (offset = 0; offset < growsize; offset += masksize) {
1128 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1129 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1131 Safefree(fd_sets[i]);
1138 if (GIMME == G_ARRAY && tbuf) {
1139 value = (NV)(timebuf.tv_sec) +
1140 (NV)(timebuf.tv_usec) / 1000000.0;
1141 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1142 sv_setnv(sv, value);
1146 DIE(aTHX_ "select not implemented");
1151 Perl_setdefout(pTHX_ GV *gv)
1154 (void)SvREFCNT_inc(gv);
1156 SvREFCNT_dec(PL_defoutgv);
1163 GV *newdefout, *egv;
1166 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1168 egv = GvEGV(PL_defoutgv);
1173 XPUSHs(&PL_sv_undef);
1175 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1176 if (gvp && *gvp == egv) {
1177 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1181 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1186 if (!GvIO(newdefout))
1187 gv_IOadd(newdefout);
1188 setdefout(newdefout);
1206 if (gv && (io = GvIO(gv))
1207 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1209 I32 gimme = GIMME_V;
1211 XPUSHs(SvTIED_obj((SV*)io, mg));
1214 call_method("GETC", gimme);
1217 if (gimme == G_SCALAR)
1218 SvSetMagicSV_nosteal(TARG, TOPs);
1221 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1222 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1223 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1224 report_evil_fh(gv, io, PL_op->op_type);
1225 SETERRNO(EBADF,RMS_IFI);
1229 sv_setpv(TARG, " ");
1230 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1231 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1232 /* Find out how many bytes the char needs */
1233 Size_t len = UTF8SKIP(SvPVX(TARG));
1236 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1237 SvCUR_set(TARG,1+len);
1247 return pp_sysread();
1251 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1253 register PERL_CONTEXT *cx;
1254 I32 gimme = GIMME_V;
1259 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1261 cx->blk_sub.retop = retop;
1262 PAD_SET_CUR(CvPADLIST(cv), 1);
1264 setdefout(gv); /* locally select filehandle so $% et al work */
1297 SV *tmpsv = sv_newmortal();
1298 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1299 name = SvPV_nolen(tmpsv);
1302 DIE(aTHX_ "Undefined format \"%s\" called", name);
1303 DIE(aTHX_ "Not a format reference");
1306 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1308 IoFLAGS(io) &= ~IOf_DIDTOP;
1309 return doform(cv,gv,PL_op->op_next);
1315 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1316 register IO *io = GvIOp(gv);
1317 PerlIO *ofp = IoOFP(io);
1321 register PERL_CONTEXT *cx;
1323 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1324 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1327 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1328 PL_formtarget != PL_toptarget)
1332 if (!IoTOP_GV(io)) {
1336 if (!IoTOP_NAME(io)) {
1337 if (!IoFMT_NAME(io))
1338 IoFMT_NAME(io) = savepv(GvNAME(gv));
1339 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1340 topgv = gv_fetchpv(SvPVX(topname), FALSE, SVt_PVFM);
1341 if ((topgv && GvFORM(topgv)) ||
1342 !gv_fetchpv("top",FALSE,SVt_PVFM))
1343 IoTOP_NAME(io) = savepv(SvPVX(topname));
1345 IoTOP_NAME(io) = savepv("top");
1347 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1348 if (!topgv || !GvFORM(topgv)) {
1349 IoLINES_LEFT(io) = 100000000;
1352 IoTOP_GV(io) = topgv;
1354 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1355 I32 lines = IoLINES_LEFT(io);
1356 char *s = SvPVX(PL_formtarget);
1357 if (lines <= 0) /* Yow, header didn't even fit!!! */
1359 while (lines-- > 0) {
1360 s = strchr(s, '\n');
1366 STRLEN save = SvCUR(PL_formtarget);
1367 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1368 do_print(PL_formtarget, ofp);
1369 SvCUR_set(PL_formtarget, save);
1370 sv_chop(PL_formtarget, s);
1371 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1374 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1375 do_print(PL_formfeed, ofp);
1376 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1378 PL_formtarget = PL_toptarget;
1379 IoFLAGS(io) |= IOf_DIDTOP;
1382 DIE(aTHX_ "bad top format reference");
1387 SV *sv = sv_newmortal();
1388 gv_efullname4(sv, fgv, Nullch, FALSE);
1389 name = SvPV_nolen(sv);
1392 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1395 DIE(aTHX_ "Undefined top format called");
1399 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1400 return doform(cv,gv,PL_op);
1404 POPBLOCK(cx,PL_curpm);
1410 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1412 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1413 else if (ckWARN(WARN_CLOSED))
1414 report_evil_fh(gv, io, PL_op->op_type);
1419 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1420 if (ckWARN(WARN_IO))
1421 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1423 if (!do_print(PL_formtarget, fp))
1426 FmLINES(PL_formtarget) = 0;
1427 SvCUR_set(PL_formtarget, 0);
1428 *SvEND(PL_formtarget) = '\0';
1429 if (IoFLAGS(io) & IOf_FLUSH)
1430 (void)PerlIO_flush(fp);
1435 PL_formtarget = PL_bodytarget;
1437 return cx->blk_sub.retop;
1442 dSP; dMARK; dORIGMARK;
1449 if (PL_op->op_flags & OPf_STACKED)
1454 if (gv && (io = GvIO(gv))
1455 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1457 if (MARK == ORIGMARK) {
1460 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1464 *MARK = SvTIED_obj((SV*)io, mg);
1467 call_method("PRINTF", G_SCALAR);
1470 MARK = ORIGMARK + 1;
1477 if (!(io = GvIO(gv))) {
1478 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1479 report_evil_fh(gv, io, PL_op->op_type);
1480 SETERRNO(EBADF,RMS_IFI);
1483 else if (!(fp = IoOFP(io))) {
1484 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1486 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1487 else if (ckWARN(WARN_CLOSED))
1488 report_evil_fh(gv, io, PL_op->op_type);
1490 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1494 do_sprintf(sv, SP - MARK, MARK + 1);
1495 if (!do_print(sv, fp))
1498 if (IoFLAGS(io) & IOf_FLUSH)
1499 if (PerlIO_flush(fp) == EOF)
1510 PUSHs(&PL_sv_undef);
1531 /* Need TIEHANDLE method ? */
1533 tmps = SvPV(sv, len);
1534 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1535 IoLINES(GvIOp(gv)) = 0;
1539 PUSHs(&PL_sv_undef);
1546 dSP; dMARK; dORIGMARK; dTARGET;
1553 Sock_size_t bufsize;
1562 bool charstart = FALSE;
1563 STRLEN charskip = 0;
1567 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1568 && gv && (io = GvIO(gv))
1569 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1574 *MARK = SvTIED_obj((SV*)io, mg);
1576 call_method("READ", G_SCALAR);
1589 sv_setpvn(bufsv, "", 0);
1590 length = SvIVx(*++MARK);
1593 offset = SvIVx(*++MARK);
1597 if (!io || !IoIFP(io)) {
1598 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1599 report_evil_fh(gv, io, PL_op->op_type);
1600 SETERRNO(EBADF,RMS_IFI);
1603 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1604 buffer = SvPVutf8_force(bufsv, blen);
1605 /* UTF-8 may not have been set if they are all low bytes */
1610 buffer = SvPV_force(bufsv, blen);
1611 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1614 DIE(aTHX_ "Negative length");
1622 if (PL_op->op_type == OP_RECV) {
1623 char namebuf[MAXPATHLEN];
1624 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1625 bufsize = sizeof (struct sockaddr_in);
1627 bufsize = sizeof namebuf;
1629 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1633 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1634 /* 'offset' means 'flags' here */
1635 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1636 (struct sockaddr *)namebuf, &bufsize);
1640 /* Bogus return without padding */
1641 bufsize = sizeof (struct sockaddr_in);
1643 SvCUR_set(bufsv, count);
1644 *SvEND(bufsv) = '\0';
1645 (void)SvPOK_only(bufsv);
1649 /* This should not be marked tainted if the fp is marked clean */
1650 if (!(IoFLAGS(io) & IOf_UNTAINT))
1651 SvTAINTED_on(bufsv);
1653 sv_setpvn(TARG, namebuf, bufsize);
1658 if (PL_op->op_type == OP_RECV)
1659 DIE(aTHX_ PL_no_sock_func, "recv");
1661 if (DO_UTF8(bufsv)) {
1662 /* offset adjust in characters not bytes */
1663 blen = sv_len_utf8(bufsv);
1666 if (-offset > (int)blen)
1667 DIE(aTHX_ "Offset outside string");
1670 if (DO_UTF8(bufsv)) {
1671 /* convert offset-as-chars to offset-as-bytes */
1672 if (offset >= (int)blen)
1673 offset += SvCUR(bufsv) - blen;
1675 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1678 bufsize = SvCUR(bufsv);
1679 /* Allocating length + offset + 1 isn't perfect in the case of reading
1680 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1682 (should be 2 * length + offset + 1, or possibly something longer if
1683 PL_encoding is true) */
1684 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1685 if (offset > bufsize) { /* Zero any newly allocated space */
1686 Zero(buffer+bufsize, offset-bufsize, char);
1688 buffer = buffer + offset;
1690 read_target = bufsv;
1692 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1693 concatenate it to the current buffer. */
1695 /* Truncate the existing buffer to the start of where we will be
1697 SvCUR_set(bufsv, offset);
1699 read_target = sv_newmortal();
1700 SvUPGRADE(read_target, SVt_PV);
1701 buffer = SvGROW(read_target, length + 1);
1704 if (PL_op->op_type == OP_SYSREAD) {
1705 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1706 if (IoTYPE(io) == IoTYPE_SOCKET) {
1707 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1713 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1718 #ifdef HAS_SOCKET__bad_code_maybe
1719 if (IoTYPE(io) == IoTYPE_SOCKET) {
1720 char namebuf[MAXPATHLEN];
1721 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1722 bufsize = sizeof (struct sockaddr_in);
1724 bufsize = sizeof namebuf;
1726 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1727 (struct sockaddr *)namebuf, &bufsize);
1732 count = PerlIO_read(IoIFP(io), buffer, length);
1733 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1734 if (count == 0 && PerlIO_error(IoIFP(io)))
1738 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1739 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1742 SvCUR_set(read_target, count+(buffer - SvPVX(read_target)));
1743 *SvEND(read_target) = '\0';
1744 (void)SvPOK_only(read_target);
1745 if (fp_utf8 && !IN_BYTES) {
1746 /* Look at utf8 we got back and count the characters */
1747 char *bend = buffer + count;
1748 while (buffer < bend) {
1750 skip = UTF8SKIP(buffer);
1753 if (buffer - charskip + skip > bend) {
1754 /* partial character - try for rest of it */
1755 length = skip - (bend-buffer);
1756 offset = bend - SvPVX(bufsv);
1768 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1769 provided amount read (count) was what was requested (length)
1771 if (got < wanted && count == length) {
1772 length = wanted - got;
1773 offset = bend - SvPVX(bufsv);
1776 /* return value is character count */
1780 else if (buffer_utf8) {
1781 /* Let svcatsv upgrade the bytes we read in to utf8.
1782 The buffer is a mortal so will be freed soon. */
1783 sv_catsv_nomg(bufsv, read_target);
1786 /* This should not be marked tainted if the fp is marked clean */
1787 if (!(IoFLAGS(io) & IOf_UNTAINT))
1788 SvTAINTED_on(bufsv);
1801 int items = (SP - PL_stack_base) - TOPMARK;
1805 sv = sv_2mortal(newSViv(sv_len(*SP)));
1814 dSP; dMARK; dORIGMARK; dTARGET;
1825 if (PL_op->op_type == OP_SYSWRITE
1826 && gv && (io = GvIO(gv))
1827 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1832 *MARK = SvTIED_obj((SV*)io, mg);
1834 call_method("WRITE", G_SCALAR);
1845 #if Size_t_size > IVSIZE
1846 length = (Size_t)SvNVx(*++MARK);
1848 length = (Size_t)SvIVx(*++MARK);
1850 if ((SSize_t)length < 0)
1851 DIE(aTHX_ "Negative length");
1854 if (!io || !IoIFP(io)) {
1856 if (ckWARN(WARN_CLOSED))
1857 report_evil_fh(gv, io, PL_op->op_type);
1858 SETERRNO(EBADF,RMS_IFI);
1862 if (PerlIO_isutf8(IoIFP(io))) {
1863 if (!SvUTF8(bufsv)) {
1864 bufsv = sv_2mortal(newSVsv(bufsv));
1865 buffer = sv_2pvutf8(bufsv, &blen);
1867 buffer = SvPV(bufsv, blen);
1870 if (DO_UTF8(bufsv)) {
1871 /* Not modifying source SV, so making a temporary copy. */
1872 bufsv = sv_2mortal(newSVsv(bufsv));
1873 sv_utf8_downgrade(bufsv, FALSE);
1875 buffer = SvPV(bufsv, blen);
1878 if (PL_op->op_type == OP_SYSWRITE) {
1880 if (DO_UTF8(bufsv)) {
1881 /* length and offset are in chars */
1882 blen = sv_len_utf8(bufsv);
1885 offset = SvIVx(*++MARK);
1887 if (-offset > (IV)blen)
1888 DIE(aTHX_ "Offset outside string");
1890 } else if (offset >= (IV)blen && blen > 0)
1891 DIE(aTHX_ "Offset outside string");
1894 if (length > blen - offset)
1895 length = blen - offset;
1896 if (DO_UTF8(bufsv)) {
1897 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1898 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1901 buffer = buffer+offset;
1903 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1904 if (IoTYPE(io) == IoTYPE_SOCKET) {
1905 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1911 /* See the note at doio.c:do_print about filesize limits. --jhi */
1912 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1917 else if (SP > MARK) {
1920 sockbuf = SvPVx(*++MARK, mlen);
1921 /* length is really flags */
1922 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1923 length, (struct sockaddr *)sockbuf, mlen);
1926 /* length is really flags */
1927 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1930 DIE(aTHX_ PL_no_sock_func, "send");
1936 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1937 #if Size_t_size > IVSIZE
1951 return pp_sysread();
1962 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1964 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1966 if (io && !IoIFP(io)) {
1967 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1969 IoFLAGS(io) &= ~IOf_START;
1970 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1971 sv_setpvn(GvSV(gv), "-", 1);
1972 SvSETMAGIC(GvSV(gv));
1974 else if (!nextargv(gv))
1979 gv = PL_last_in_gv; /* eof */
1982 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1984 if (gv && (io = GvIO(gv))
1985 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1988 XPUSHs(SvTIED_obj((SV*)io, mg));
1991 call_method("EOF", G_SCALAR);
1997 PUSHs(boolSV(!gv || do_eof(gv)));
2011 gv = PL_last_in_gv = (GV*)POPs;
2013 if (gv && (io = GvIO(gv))
2014 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2017 XPUSHs(SvTIED_obj((SV*)io, mg));
2020 call_method("TELL", G_SCALAR);
2026 #if LSEEKSIZE > IVSIZE
2027 PUSHn( do_tell(gv) );
2029 PUSHi( do_tell(gv) );
2036 return pp_sysseek();
2045 #if LSEEKSIZE > IVSIZE
2046 Off_t offset = (Off_t)SvNVx(POPs);
2048 Off_t offset = (Off_t)SvIVx(POPs);
2052 gv = PL_last_in_gv = (GV*)POPs;
2054 if (gv && (io = GvIO(gv))
2055 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2058 XPUSHs(SvTIED_obj((SV*)io, mg));
2059 #if LSEEKSIZE > IVSIZE
2060 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2062 XPUSHs(sv_2mortal(newSViv(offset)));
2064 XPUSHs(sv_2mortal(newSViv(whence)));
2067 call_method("SEEK", G_SCALAR);
2073 if (PL_op->op_type == OP_SEEK)
2074 PUSHs(boolSV(do_seek(gv, offset, whence)));
2076 Off_t sought = do_sysseek(gv, offset, whence);
2078 PUSHs(&PL_sv_undef);
2081 #if LSEEKSIZE > IVSIZE
2086 : newSVpvn(zero_but_true, ZBTLEN);
2087 PUSHs(sv_2mortal(sv));
2096 /* There seems to be no consensus on the length type of truncate()
2097 * and ftruncate(), both off_t and size_t have supporters. In
2098 * general one would think that when using large files, off_t is
2099 * at least as wide as size_t, so using an off_t should be okay. */
2100 /* XXX Configure probe for the length type of *truncate() needed XXX */
2103 #if Off_t_size > IVSIZE
2108 /* Checking for length < 0 is problematic as the type might or
2109 * might not be signed: if it is not, clever compilers will moan. */
2110 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2112 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2119 if (PL_op->op_flags & OPf_SPECIAL) {
2120 tmpgv = gv_fetchpv(POPpx, FALSE, SVt_PVIO);
2129 TAINT_PROPER("truncate");
2130 if (!(fp = IoIFP(io))) {
2136 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2138 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2148 if (SvTYPE(sv) == SVt_PVGV) {
2149 tmpgv = (GV*)sv; /* *main::FRED for example */
2150 goto do_ftruncate_gv;
2152 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2153 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2154 goto do_ftruncate_gv;
2156 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2157 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2158 goto do_ftruncate_io;
2161 name = SvPV(sv, n_a);
2162 TAINT_PROPER("truncate");
2164 if (truncate(name, len) < 0)
2170 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2173 if (my_chsize(tmpfd, len) < 0)
2175 PerlLIO_close(tmpfd);
2184 SETERRNO(EBADF,RMS_IFI);
2188 DIE(aTHX_ "truncate not implemented");
2201 unsigned int func = POPu;
2202 int optype = PL_op->op_type;
2206 IO *io = gv ? GvIOn(gv) : 0;
2208 if (!io || !argsv || !IoIFP(io)) {
2209 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2210 report_evil_fh(gv, io, PL_op->op_type);
2211 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2215 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2218 s = SvPV_force(argsv, len);
2219 need = IOCPARM_LEN(func);
2221 s = Sv_Grow(argsv, need + 1);
2222 SvCUR_set(argsv, need);
2225 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2228 retval = SvIV(argsv);
2229 s = INT2PTR(char*,retval); /* ouch */
2232 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2234 if (optype == OP_IOCTL)
2236 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2238 DIE(aTHX_ "ioctl is not implemented");
2242 DIE(aTHX_ "fcntl is not implemented");
2244 #if defined(OS2) && defined(__EMX__)
2245 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2247 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2251 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2253 if (s[SvCUR(argsv)] != 17)
2254 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2256 s[SvCUR(argsv)] = 0; /* put our null back */
2257 SvSETMAGIC(argsv); /* Assume it has changed */
2266 PUSHp(zero_but_true, ZBTLEN);
2287 if (gv && (io = GvIO(gv)))
2294 (void)PerlIO_flush(fp);
2295 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2298 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2299 report_evil_fh(gv, io, PL_op->op_type);
2301 SETERRNO(EBADF,RMS_IFI);
2306 DIE(aTHX_ PL_no_func, "flock()");
2318 int protocol = POPi;
2324 io = gv ? GvIOn(gv) : NULL;
2327 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2328 report_evil_fh(gv, io, PL_op->op_type);
2330 do_close(gv, FALSE);
2331 SETERRNO(EBADF,LIB_INVARG);
2336 do_close(gv, FALSE);
2338 TAINT_PROPER("socket");
2339 fd = PerlSock_socket(domain, type, protocol);
2342 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2343 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2344 IoTYPE(io) = IoTYPE_SOCKET;
2345 if (!IoIFP(io) || !IoOFP(io)) {
2346 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2347 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2348 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2351 #if defined(HAS_FCNTL) && defined(F_SETFD)
2352 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2356 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2361 DIE(aTHX_ PL_no_sock_func, "socket");
2367 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2373 int protocol = POPi;
2380 io1 = gv1 ? GvIOn(gv1) : NULL;
2381 io2 = gv2 ? GvIOn(gv2) : NULL;
2382 if (!gv1 || !gv2 || !io1 || !io2) {
2383 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2385 report_evil_fh(gv1, io1, PL_op->op_type);
2387 report_evil_fh(gv1, io2, PL_op->op_type);
2390 do_close(gv1, FALSE);
2392 do_close(gv2, FALSE);
2397 do_close(gv1, FALSE);
2399 do_close(gv2, FALSE);
2401 TAINT_PROPER("socketpair");
2402 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2404 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2405 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2406 IoTYPE(io1) = IoTYPE_SOCKET;
2407 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2408 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2409 IoTYPE(io2) = IoTYPE_SOCKET;
2410 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2411 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2412 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2413 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2414 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2415 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2416 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2419 #if defined(HAS_FCNTL) && defined(F_SETFD)
2420 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2421 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2426 DIE(aTHX_ PL_no_sock_func, "socketpair");
2434 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2435 extern void GETPRIVMODE();
2436 extern void GETUSERMODE();
2441 register IO *io = GvIOn(gv);
2445 int mpeprivmode = 0;
2448 if (!io || !IoIFP(io))
2451 addr = SvPV(addrsv, len);
2452 TAINT_PROPER("bind");
2453 #ifdef MPE /* Deal with MPE bind() peculiarities */
2454 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2455 /* The address *MUST* stupidly be zero. */
2456 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2457 /* PRIV mode is required to bind() to ports < 1024. */
2458 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2459 ((struct sockaddr_in *)addr)->sin_port > 0) {
2460 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2465 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2466 (struct sockaddr *)addr, len) >= 0)
2469 #ifdef MPE /* Switch back to USER mode */
2480 if (ckWARN(WARN_CLOSED))
2481 report_evil_fh(gv, io, PL_op->op_type);
2482 SETERRNO(EBADF,SS_IVCHAN);
2485 DIE(aTHX_ PL_no_sock_func, "bind");
2496 register IO *io = GvIOn(gv);
2499 if (!io || !IoIFP(io))
2502 addr = SvPV(addrsv, len);
2503 TAINT_PROPER("connect");
2504 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2510 if (ckWARN(WARN_CLOSED))
2511 report_evil_fh(gv, io, PL_op->op_type);
2512 SETERRNO(EBADF,SS_IVCHAN);
2515 DIE(aTHX_ PL_no_sock_func, "connect");
2525 register IO *io = gv ? GvIOn(gv) : NULL;
2527 if (!gv || !io || !IoIFP(io))
2530 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2536 if (ckWARN(WARN_CLOSED))
2537 report_evil_fh(gv, io, PL_op->op_type);
2538 SETERRNO(EBADF,SS_IVCHAN);
2541 DIE(aTHX_ PL_no_sock_func, "listen");
2553 char namebuf[MAXPATHLEN];
2554 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2555 Sock_size_t len = sizeof (struct sockaddr_in);
2557 Sock_size_t len = sizeof namebuf;
2570 if (!gstio || !IoIFP(gstio))
2574 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2578 do_close(ngv, FALSE);
2579 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2580 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2581 IoTYPE(nstio) = IoTYPE_SOCKET;
2582 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2583 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2584 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2585 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2588 #if defined(HAS_FCNTL) && defined(F_SETFD)
2589 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2593 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2594 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2596 #ifdef __SCO_VERSION__
2597 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2600 PUSHp(namebuf, len);
2604 if (ckWARN(WARN_CLOSED))
2605 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2606 SETERRNO(EBADF,SS_IVCHAN);
2612 DIE(aTHX_ PL_no_sock_func, "accept");
2622 register IO *io = GvIOn(gv);
2624 if (!io || !IoIFP(io))
2627 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2631 if (ckWARN(WARN_CLOSED))
2632 report_evil_fh(gv, io, PL_op->op_type);
2633 SETERRNO(EBADF,SS_IVCHAN);
2636 DIE(aTHX_ PL_no_sock_func, "shutdown");
2643 return pp_ssockopt();
2645 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2653 int optype = PL_op->op_type;
2656 unsigned int optname;
2662 if (optype == OP_GSOCKOPT)
2663 sv = sv_2mortal(NEWSV(22, 257));
2666 optname = (unsigned int) POPi;
2667 lvl = (unsigned int) POPi;
2671 if (!io || !IoIFP(io))
2674 fd = PerlIO_fileno(IoIFP(io));
2678 (void)SvPOK_only(sv);
2682 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2697 aint = (int)SvIV(sv);
2701 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2710 if (ckWARN(WARN_CLOSED))
2711 report_evil_fh(gv, io, optype);
2712 SETERRNO(EBADF,SS_IVCHAN);
2717 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2724 return pp_getpeername();
2726 DIE(aTHX_ PL_no_sock_func, "getsockname");
2734 int optype = PL_op->op_type;
2738 register IO *io = GvIOn(gv);
2741 if (!io || !IoIFP(io))
2744 sv = sv_2mortal(NEWSV(22, 257));
2745 (void)SvPOK_only(sv);
2749 fd = PerlIO_fileno(IoIFP(io));
2751 case OP_GETSOCKNAME:
2752 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2755 case OP_GETPEERNAME:
2756 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2758 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2760 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";
2761 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2762 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2763 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2764 sizeof(u_short) + sizeof(struct in_addr))) {
2771 #ifdef BOGUS_GETNAME_RETURN
2772 /* Interactive Unix, getpeername() and getsockname()
2773 does not return valid namelen */
2774 if (len == BOGUS_GETNAME_RETURN)
2775 len = sizeof(struct sockaddr);
2783 if (ckWARN(WARN_CLOSED))
2784 report_evil_fh(gv, io, optype);
2785 SETERRNO(EBADF,SS_IVCHAN);
2790 DIE(aTHX_ PL_no_sock_func, "getpeername");
2809 if (PL_op->op_flags & OPf_REF) {
2811 if (PL_op->op_type == OP_LSTAT) {
2812 if (gv != PL_defgv) {
2813 if (ckWARN(WARN_IO))
2814 Perl_warner(aTHX_ packWARN(WARN_IO),
2815 "lstat() on filehandle %s", GvENAME(gv));
2816 } else if (PL_laststype != OP_LSTAT)
2817 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2821 if (gv != PL_defgv) {
2822 PL_laststype = OP_STAT;
2824 sv_setpv(PL_statname, "");
2825 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2826 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2828 if (PL_laststatval < 0) {
2829 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2830 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2836 if (SvTYPE(sv) == SVt_PVGV) {
2840 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2842 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2843 Perl_warner(aTHX_ packWARN(WARN_IO),
2844 "lstat() on filehandle %s", GvENAME(gv));
2847 sv_setpv(PL_statname, SvPV(sv,n_a));
2850 PL_laststype = PL_op->op_type;
2851 if (PL_op->op_type == OP_LSTAT)
2852 PL_laststatval = PerlLIO_lstat(SvPV(PL_statname, n_a), &PL_statcache);
2855 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2856 if (PL_laststatval < 0) {
2857 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2858 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2864 if (gimme != G_ARRAY) {
2865 if (gimme != G_VOID)
2866 XPUSHs(boolSV(max));
2872 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2873 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2874 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2875 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2876 #if Uid_t_size > IVSIZE
2877 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2879 # if Uid_t_sign <= 0
2880 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2882 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2885 #if Gid_t_size > IVSIZE
2886 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2888 # if Gid_t_sign <= 0
2889 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2891 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2894 #ifdef USE_STAT_RDEV
2895 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2897 PUSHs(sv_2mortal(newSVpvn("", 0)));
2899 #if Off_t_size > IVSIZE
2900 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2902 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2905 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2906 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2907 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2909 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2910 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2911 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2913 #ifdef USE_STAT_BLOCKS
2914 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2915 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2917 PUSHs(sv_2mortal(newSVpvn("", 0)));
2918 PUSHs(sv_2mortal(newSVpvn("", 0)));
2924 /* This macro is used by the stacked filetest operators :
2925 * if the previous filetest failed, short-circuit and pass its value.
2926 * Else, discard it from the stack and continue. --rgs
2928 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2929 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2930 else { (void)POPs; PUTBACK; } \
2937 STACKED_FTEST_CHECK;
2938 #if defined(HAS_ACCESS) && defined(R_OK)
2939 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2941 result = access(POPpx, R_OK);
2956 if (cando(S_IRUSR, 0, &PL_statcache))
2965 STACKED_FTEST_CHECK;
2966 #if defined(HAS_ACCESS) && defined(W_OK)
2967 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2969 result = access(POPpx, W_OK);
2984 if (cando(S_IWUSR, 0, &PL_statcache))
2993 STACKED_FTEST_CHECK;
2994 #if defined(HAS_ACCESS) && defined(X_OK)
2995 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2997 result = access(POPpx, X_OK);
3012 if (cando(S_IXUSR, 0, &PL_statcache))
3021 STACKED_FTEST_CHECK;
3022 #ifdef PERL_EFF_ACCESS_R_OK
3023 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3025 result = PERL_EFF_ACCESS_R_OK(POPpx);
3040 if (cando(S_IRUSR, 1, &PL_statcache))
3049 STACKED_FTEST_CHECK;
3050 #ifdef PERL_EFF_ACCESS_W_OK
3051 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3053 result = PERL_EFF_ACCESS_W_OK(POPpx);
3068 if (cando(S_IWUSR, 1, &PL_statcache))
3077 STACKED_FTEST_CHECK;
3078 #ifdef PERL_EFF_ACCESS_X_OK
3079 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3081 result = PERL_EFF_ACCESS_X_OK(POPpx);
3096 if (cando(S_IXUSR, 1, &PL_statcache))
3105 STACKED_FTEST_CHECK;
3115 return pp_ftrowned();
3122 STACKED_FTEST_CHECK;
3127 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3137 STACKED_FTEST_CHECK;
3142 if (PL_statcache.st_size == 0)
3151 STACKED_FTEST_CHECK;
3156 #if Off_t_size > IVSIZE
3157 PUSHn(PL_statcache.st_size);
3159 PUSHi(PL_statcache.st_size);
3168 STACKED_FTEST_CHECK;
3173 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3181 STACKED_FTEST_CHECK;
3186 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3194 STACKED_FTEST_CHECK;
3199 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3207 STACKED_FTEST_CHECK;
3212 if (S_ISSOCK(PL_statcache.st_mode))
3221 STACKED_FTEST_CHECK;
3226 if (S_ISCHR(PL_statcache.st_mode))
3235 STACKED_FTEST_CHECK;
3240 if (S_ISBLK(PL_statcache.st_mode))
3249 STACKED_FTEST_CHECK;
3254 if (S_ISREG(PL_statcache.st_mode))
3263 STACKED_FTEST_CHECK;
3268 if (S_ISDIR(PL_statcache.st_mode))
3277 STACKED_FTEST_CHECK;
3282 if (S_ISFIFO(PL_statcache.st_mode))
3289 I32 result = my_lstat();
3293 if (S_ISLNK(PL_statcache.st_mode))
3303 STACKED_FTEST_CHECK;
3308 if (PL_statcache.st_mode & S_ISUID)
3319 STACKED_FTEST_CHECK;
3324 if (PL_statcache.st_mode & S_ISGID)
3335 STACKED_FTEST_CHECK;
3340 if (PL_statcache.st_mode & S_ISVTX)
3351 char *tmps = Nullch;
3354 STACKED_FTEST_CHECK;
3356 if (PL_op->op_flags & OPf_REF)
3358 else if (isGV(TOPs))
3360 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3361 gv = (GV*)SvRV(POPs);
3363 gv = gv_fetchpv(tmps = POPpx, FALSE, SVt_PVIO);
3365 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3366 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3367 else if (tmps && isDIGIT(*tmps))
3371 if (PerlLIO_isatty(fd))
3376 #if defined(atarist) /* this will work with atariST. Configure will
3377 make guesses for other systems. */
3378 # define FILE_base(f) ((f)->_base)
3379 # define FILE_ptr(f) ((f)->_ptr)
3380 # define FILE_cnt(f) ((f)->_cnt)
3381 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3391 register STDCHAR *s;
3398 STACKED_FTEST_CHECK;
3400 if (PL_op->op_flags & OPf_REF)
3402 else if (isGV(TOPs))
3404 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3405 gv = (GV*)SvRV(POPs);
3411 if (gv == PL_defgv) {
3413 io = GvIO(PL_statgv);
3416 goto really_filename;
3421 PL_laststatval = -1;
3422 sv_setpv(PL_statname, "");
3423 io = GvIO(PL_statgv);
3425 if (io && IoIFP(io)) {
3426 if (! PerlIO_has_base(IoIFP(io)))
3427 DIE(aTHX_ "-T and -B not implemented on filehandles");
3428 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3429 if (PL_laststatval < 0)
3431 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3432 if (PL_op->op_type == OP_FTTEXT)
3437 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3438 i = PerlIO_getc(IoIFP(io));
3440 (void)PerlIO_ungetc(IoIFP(io),i);
3442 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3444 len = PerlIO_get_bufsiz(IoIFP(io));
3445 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3446 /* sfio can have large buffers - limit to 512 */
3451 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3453 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3455 SETERRNO(EBADF,RMS_IFI);
3463 PL_laststatval = -1;
3464 PL_laststype = OP_STAT;
3465 sv_setpv(PL_statname, SvPV(sv, n_a));
3466 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3467 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3468 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3471 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3472 if (PL_laststatval < 0) {
3473 (void)PerlIO_close(fp);
3476 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3477 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3478 (void)PerlIO_close(fp);
3480 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3481 RETPUSHNO; /* special case NFS directories */
3482 RETPUSHYES; /* null file is anything */
3487 /* now scan s to look for textiness */
3488 /* XXX ASCII dependent code */
3490 #if defined(DOSISH) || defined(USEMYBINMODE)
3491 /* ignore trailing ^Z on short files */
3492 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3496 for (i = 0; i < len; i++, s++) {
3497 if (!*s) { /* null never allowed in text */
3502 else if (!(isPRINT(*s) || isSPACE(*s)))
3505 else if (*s & 128) {
3507 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3510 /* utf8 characters don't count as odd */
3511 if (UTF8_IS_START(*s)) {
3512 int ulen = UTF8SKIP(s);
3513 if (ulen < len - i) {
3515 for (j = 1; j < ulen; j++) {
3516 if (!UTF8_IS_CONTINUATION(s[j]))
3519 --ulen; /* loop does extra increment */
3529 *s != '\n' && *s != '\r' && *s != '\b' &&
3530 *s != '\t' && *s != '\f' && *s != 27)
3535 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3560 if( !tmps || !*tmps ) {
3561 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3562 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3564 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3569 deprecate("chdir('') or chdir(undef) as chdir()");
3570 tmps = SvPV(*svp, n_a);
3574 TAINT_PROPER("chdir");
3579 TAINT_PROPER("chdir");
3580 PUSHi( PerlDir_chdir(tmps) >= 0 );
3582 /* Clear the DEFAULT element of ENV so we'll get the new value
3584 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3592 dSP; dMARK; dTARGET;
3593 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3599 DIE(aTHX_ PL_no_func, "chown");
3609 TAINT_PROPER("chroot");
3610 PUSHi( chroot(tmps) >= 0 );
3613 DIE(aTHX_ PL_no_func, "chroot");
3619 dSP; dMARK; dTARGET;
3621 value = (I32)apply(PL_op->op_type, MARK, SP);
3629 dSP; dMARK; dTARGET;
3631 value = (I32)apply(PL_op->op_type, MARK, SP);
3639 dSP; dMARK; dTARGET;
3641 value = (I32)apply(PL_op->op_type, MARK, SP);
3653 char *tmps2 = POPpx;
3654 char *tmps = SvPV(TOPs, n_a);
3655 TAINT_PROPER("rename");
3657 anum = PerlLIO_rename(tmps, tmps2);
3659 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3660 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3663 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3664 (void)UNLINK(tmps2);
3665 if (!(anum = link(tmps, tmps2)))
3666 anum = UNLINK(tmps);
3679 char *tmps2 = POPpx;
3680 char *tmps = SvPV(TOPs, n_a);
3681 TAINT_PROPER("link");
3682 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3685 DIE(aTHX_ PL_no_func, "link");
3694 char *tmps2 = POPpx;
3695 char *tmps = SvPV(TOPs, n_a);
3696 TAINT_PROPER("symlink");
3697 SETi( symlink(tmps, tmps2) >= 0 );
3700 DIE(aTHX_ PL_no_func, "symlink");
3710 char buf[MAXPATHLEN];
3714 #ifndef INCOMPLETE_TAINTS
3718 len = readlink(tmps, buf, sizeof(buf) - 1);
3726 RETSETUNDEF; /* just pretend it's a normal file */
3730 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3732 S_dooneliner(pTHX_ char *cmd, char *filename)
3734 char *save_filename = filename;
3740 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3741 strcpy(cmdline, cmd);
3742 strcat(cmdline, " ");
3743 for (s = cmdline + strlen(cmdline); *filename; ) {
3748 myfp = PerlProc_popen(cmdline, "r");
3752 SV *tmpsv = sv_newmortal();
3753 /* Need to save/restore 'PL_rs' ?? */
3754 s = sv_gets(tmpsv, myfp, 0);
3755 (void)PerlProc_pclose(myfp);
3759 #ifdef HAS_SYS_ERRLIST
3764 /* you don't see this */
3766 #ifdef HAS_SYS_ERRLIST
3774 if (instr(s, errmsg)) {
3781 #define EACCES EPERM
3783 if (instr(s, "cannot make"))
3784 SETERRNO(EEXIST,RMS_FEX);
3785 else if (instr(s, "existing file"))
3786 SETERRNO(EEXIST,RMS_FEX);
3787 else if (instr(s, "ile exists"))
3788 SETERRNO(EEXIST,RMS_FEX);
3789 else if (instr(s, "non-exist"))
3790 SETERRNO(ENOENT,RMS_FNF);
3791 else if (instr(s, "does not exist"))
3792 SETERRNO(ENOENT,RMS_FNF);
3793 else if (instr(s, "not empty"))
3794 SETERRNO(EBUSY,SS_DEVOFFLINE);
3795 else if (instr(s, "cannot access"))
3796 SETERRNO(EACCES,RMS_PRV);
3798 SETERRNO(EPERM,RMS_PRV);
3801 else { /* some mkdirs return no failure indication */
3802 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3803 if (PL_op->op_type == OP_RMDIR)
3808 SETERRNO(EACCES,RMS_PRV); /* a guess */
3817 /* This macro removes trailing slashes from a directory name.
3818 * Different operating and file systems take differently to
3819 * trailing slashes. According to POSIX 1003.1 1996 Edition
3820 * any number of trailing slashes should be allowed.
3821 * Thusly we snip them away so that even non-conforming
3822 * systems are happy.
3823 * We should probably do this "filtering" for all
3824 * the functions that expect (potentially) directory names:
3825 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3826 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3828 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3829 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3832 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3833 (tmps) = savepvn((tmps), (len)); \
3853 TRIMSLASHES(tmps,len,copy);
3855 TAINT_PROPER("mkdir");
3857 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3859 SETi( dooneliner("mkdir", tmps) );
3860 oldumask = PerlLIO_umask(0);
3861 PerlLIO_umask(oldumask);
3862 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3876 TRIMSLASHES(tmps,len,copy);
3877 TAINT_PROPER("rmdir");
3879 SETi( PerlDir_rmdir(tmps) >= 0 );
3881 SETi( dooneliner("rmdir", tmps) );
3888 /* Directory calls. */
3892 #if defined(Direntry_t) && defined(HAS_READDIR)
3895 char *dirname = POPpx;
3897 register IO *io = GvIOn(gv);
3903 PerlDir_close(IoDIRP(io));
3904 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3910 SETERRNO(EBADF,RMS_DIR);
3913 DIE(aTHX_ PL_no_dir_func, "opendir");
3919 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3920 DIE(aTHX_ PL_no_dir_func, "readdir");
3922 #if !defined(I_DIRENT) && !defined(VMS)
3923 Direntry_t *readdir (DIR *);
3929 GV *gv = (GV *)POPs;
3930 register Direntry_t *dp;
3931 register IO *io = GvIOn(gv);
3933 if (!io || !IoDIRP(io))
3937 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3941 sv = newSVpvn(dp->d_name, dp->d_namlen);
3943 sv = newSVpv(dp->d_name, 0);
3945 #ifndef INCOMPLETE_TAINTS
3946 if (!(IoFLAGS(io) & IOf_UNTAINT))
3949 XPUSHs(sv_2mortal(sv));
3951 while (gimme == G_ARRAY);
3953 if (!dp && gimme != G_ARRAY)
3960 SETERRNO(EBADF,RMS_ISI);
3961 if (GIMME == G_ARRAY)
3970 #if defined(HAS_TELLDIR) || defined(telldir)
3972 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3973 /* XXX netbsd still seemed to.
3974 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3975 --JHI 1999-Feb-02 */
3976 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3977 long telldir (DIR *);
3980 register IO *io = GvIOn(gv);
3982 if (!io || !IoDIRP(io))
3985 PUSHi( PerlDir_tell(IoDIRP(io)) );
3989 SETERRNO(EBADF,RMS_ISI);
3992 DIE(aTHX_ PL_no_dir_func, "telldir");
3998 #if defined(HAS_SEEKDIR) || defined(seekdir)
4002 register IO *io = GvIOn(gv);
4004 if (!io || !IoDIRP(io))
4007 (void)PerlDir_seek(IoDIRP(io), along);
4012 SETERRNO(EBADF,RMS_ISI);
4015 DIE(aTHX_ PL_no_dir_func, "seekdir");
4021 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4024 register IO *io = GvIOn(gv);
4026 if (!io || !IoDIRP(io))
4029 (void)PerlDir_rewind(IoDIRP(io));
4033 SETERRNO(EBADF,RMS_ISI);
4036 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4042 #if defined(Direntry_t) && defined(HAS_READDIR)
4045 register IO *io = GvIOn(gv);
4047 if (!io || !IoDIRP(io))
4050 #ifdef VOID_CLOSEDIR
4051 PerlDir_close(IoDIRP(io));
4053 if (PerlDir_close(IoDIRP(io)) < 0) {
4054 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4063 SETERRNO(EBADF,RMS_IFI);
4066 DIE(aTHX_ PL_no_dir_func, "closedir");
4070 /* Process control. */
4080 PERL_FLUSHALL_FOR_CHILD;
4081 childpid = PerlProc_fork();
4086 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4087 SvREADONLY_off(GvSV(tmpgv));
4088 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4089 SvREADONLY_on(GvSV(tmpgv));
4091 #ifdef THREADS_HAVE_PIDS
4092 PL_ppid = (IV)getppid();
4094 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4099 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4104 PERL_FLUSHALL_FOR_CHILD;
4105 childpid = PerlProc_fork();
4111 DIE(aTHX_ PL_no_func, "fork");
4118 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4123 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4124 childpid = wait4pid(-1, &argflags, 0);
4126 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4131 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4132 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4133 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4135 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4140 DIE(aTHX_ PL_no_func, "wait");
4146 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4155 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4156 result = wait4pid(pid, &argflags, optype);
4158 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4163 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4164 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4165 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4167 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4172 DIE(aTHX_ PL_no_func, "waitpid");
4178 dSP; dMARK; dORIGMARK; dTARGET;
4186 while (++MARK <= SP) {
4187 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4192 TAINT_PROPER("system");
4194 PERL_FLUSHALL_FOR_CHILD;
4195 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4200 if (PerlProc_pipe(pp) >= 0)
4202 while ((childpid = PerlProc_fork()) == -1) {
4203 if (errno != EAGAIN) {
4208 PerlLIO_close(pp[0]);
4209 PerlLIO_close(pp[1]);
4216 Sigsave_t ihand,qhand; /* place to save signals during system() */
4220 PerlLIO_close(pp[1]);
4222 rsignal_save(SIGINT, SIG_IGN, &ihand);
4223 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4226 result = wait4pid(childpid, &status, 0);
4227 } while (result == -1 && errno == EINTR);
4229 (void)rsignal_restore(SIGINT, &ihand);
4230 (void)rsignal_restore(SIGQUIT, &qhand);
4232 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4233 do_execfree(); /* free any memory child malloced on fork */
4239 while (n < sizeof(int)) {
4240 n1 = PerlLIO_read(pp[0],
4241 (void*)(((char*)&errkid)+n),
4247 PerlLIO_close(pp[0]);
4248 if (n) { /* Error */
4249 if (n != sizeof(int))
4250 DIE(aTHX_ "panic: kid popen errno read");
4251 errno = errkid; /* Propagate errno from kid */
4252 STATUS_CURRENT = -1;
4255 PUSHi(STATUS_CURRENT);
4259 PerlLIO_close(pp[0]);
4260 #if defined(HAS_FCNTL) && defined(F_SETFD)
4261 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4264 if (PL_op->op_flags & OPf_STACKED) {
4265 SV *really = *++MARK;
4266 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4268 else if (SP - MARK != 1)
4269 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4271 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4275 #else /* ! FORK or VMS or OS/2 */
4278 if (PL_op->op_flags & OPf_STACKED) {
4279 SV *really = *++MARK;
4280 # if defined(WIN32) || defined(OS2)
4281 value = (I32)do_aspawn(really, MARK, SP);
4283 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4286 else if (SP - MARK != 1) {
4287 # if defined(WIN32) || defined(OS2)
4288 value = (I32)do_aspawn(Nullsv, MARK, SP);
4290 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4294 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4296 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4298 STATUS_NATIVE_SET(value);
4301 PUSHi(result ? value : STATUS_CURRENT);
4302 #endif /* !FORK or VMS */
4308 dSP; dMARK; dORIGMARK; dTARGET;
4314 while (++MARK <= SP) {
4315 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4320 TAINT_PROPER("exec");
4322 PERL_FLUSHALL_FOR_CHILD;
4323 if (PL_op->op_flags & OPf_STACKED) {
4324 SV *really = *++MARK;
4325 value = (I32)do_aexec(really, MARK, SP);
4327 else if (SP - MARK != 1)
4329 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4333 (void ) do_aspawn(Nullsv, MARK, SP);
4337 value = (I32)do_aexec(Nullsv, MARK, SP);
4342 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4345 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4348 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4361 dSP; dMARK; dTARGET;
4363 value = (I32)apply(PL_op->op_type, MARK, SP);
4368 DIE(aTHX_ PL_no_func, "kill");
4376 # ifdef THREADS_HAVE_PIDS
4379 XPUSHi( getppid() );
4383 DIE(aTHX_ PL_no_func, "getppid");
4399 pgrp = (I32)BSD_GETPGRP(pid);
4401 if (pid != 0 && pid != PerlProc_getpid())
4402 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4408 DIE(aTHX_ PL_no_func, "getpgrp()");
4427 TAINT_PROPER("setpgrp");
4429 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4431 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4432 || (pid != 0 && pid != PerlProc_getpid()))
4434 DIE(aTHX_ "setpgrp can't take arguments");
4436 SETi( setpgrp() >= 0 );
4437 #endif /* USE_BSDPGRP */
4440 DIE(aTHX_ PL_no_func, "setpgrp()");
4446 #ifdef HAS_GETPRIORITY
4450 SETi( getpriority(which, who) );
4453 DIE(aTHX_ PL_no_func, "getpriority()");
4459 #ifdef HAS_SETPRIORITY
4464 TAINT_PROPER("setpriority");
4465 SETi( setpriority(which, who, niceval) >= 0 );
4468 DIE(aTHX_ PL_no_func, "setpriority()");
4478 XPUSHn( time(Null(Time_t*)) );
4480 XPUSHi( time(Null(Time_t*)) );
4491 (void)PerlProc_times(&PL_timesbuf);
4493 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4494 /* struct tms, though same data */
4498 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4499 if (GIMME == G_ARRAY) {
4500 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4501 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4502 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4508 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4510 if (GIMME == G_ARRAY) {
4511 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4512 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4513 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4517 DIE(aTHX_ "times not implemented");
4519 #endif /* HAS_TIMES */
4532 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4533 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4534 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4540 when = (Time_t)SvNVx(POPs);
4542 when = (Time_t)SvIVx(POPs);
4545 if (PL_op->op_type == OP_LOCALTIME)
4546 tmbuf = localtime(&when);
4548 tmbuf = gmtime(&when);
4550 if (GIMME != G_ARRAY) {
4556 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4557 dayname[tmbuf->tm_wday],
4558 monname[tmbuf->tm_mon],
4563 tmbuf->tm_year + 1900);
4564 PUSHs(sv_2mortal(tsv));
4569 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4570 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4571 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4572 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4573 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4574 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4575 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4576 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4577 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4588 anum = alarm((unsigned int)anum);
4595 DIE(aTHX_ PL_no_func, "alarm");
4606 (void)time(&lasttime);
4611 PerlProc_sleep((unsigned int)duration);
4614 XPUSHi(when - lasttime);
4618 /* Shared memory. */
4632 return pp_shmwrite();
4637 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4638 dSP; dMARK; dTARGET;
4639 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4648 /* Message passing. */
4662 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4663 dSP; dMARK; dTARGET;
4664 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4675 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4676 dSP; dMARK; dTARGET;
4677 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4690 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4691 dSP; dMARK; dTARGET;
4692 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4699 DIE(aTHX_ "System V IPC is not implemented on this machine");
4705 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4706 dSP; dMARK; dTARGET;
4707 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4715 PUSHp(zero_but_true, ZBTLEN);
4725 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4726 dSP; dMARK; dTARGET;
4727 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4736 /* Get system info. */
4740 #ifdef HAS_GETHOSTBYNAME
4741 return pp_ghostent();
4743 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4749 #ifdef HAS_GETHOSTBYADDR
4750 return pp_ghostent();
4752 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4758 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4760 I32 which = PL_op->op_type;
4761 register char **elem;
4763 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4764 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4765 struct hostent *gethostbyname(Netdb_name_t);
4766 struct hostent *gethostent(void);
4768 struct hostent *hent;
4773 if (which == OP_GHBYNAME) {
4774 #ifdef HAS_GETHOSTBYNAME
4775 char* name = POPpbytex;
4776 hent = PerlSock_gethostbyname(name);
4778 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4781 else if (which == OP_GHBYADDR) {
4782 #ifdef HAS_GETHOSTBYADDR
4783 int addrtype = POPi;
4786 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4788 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4790 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4794 #ifdef HAS_GETHOSTENT
4795 hent = PerlSock_gethostent();
4797 DIE(aTHX_ PL_no_sock_func, "gethostent");
4800 #ifdef HOST_NOT_FOUND
4802 #ifdef USE_REENTRANT_API
4803 # ifdef USE_GETHOSTENT_ERRNO
4804 h_errno = PL_reentrant_buffer->_gethostent_errno;
4807 STATUS_NATIVE_SET(h_errno);
4811 if (GIMME != G_ARRAY) {
4812 PUSHs(sv = sv_newmortal());
4814 if (which == OP_GHBYNAME) {
4816 sv_setpvn(sv, hent->h_addr, hent->h_length);
4819 sv_setpv(sv, (char*)hent->h_name);
4825 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4826 sv_setpv(sv, (char*)hent->h_name);
4827 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4828 for (elem = hent->h_aliases; elem && *elem; elem++) {
4829 sv_catpv(sv, *elem);
4831 sv_catpvn(sv, " ", 1);
4833 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4834 sv_setiv(sv, (IV)hent->h_addrtype);
4835 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4836 len = hent->h_length;
4837 sv_setiv(sv, (IV)len);
4839 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4840 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4841 sv_setpvn(sv, *elem, len);
4844 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4846 sv_setpvn(sv, hent->h_addr, len);
4851 DIE(aTHX_ PL_no_sock_func, "gethostent");
4857 #ifdef HAS_GETNETBYNAME
4858 return pp_gnetent();
4860 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4866 #ifdef HAS_GETNETBYADDR
4867 return pp_gnetent();
4869 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4875 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4877 I32 which = PL_op->op_type;
4878 register char **elem;
4880 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4881 struct netent *getnetbyaddr(Netdb_net_t, int);
4882 struct netent *getnetbyname(Netdb_name_t);
4883 struct netent *getnetent(void);
4885 struct netent *nent;
4888 if (which == OP_GNBYNAME){
4889 #ifdef HAS_GETNETBYNAME
4890 char *name = POPpbytex;
4891 nent = PerlSock_getnetbyname(name);
4893 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4896 else if (which == OP_GNBYADDR) {
4897 #ifdef HAS_GETNETBYADDR
4898 int addrtype = POPi;
4899 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4900 nent = PerlSock_getnetbyaddr(addr, addrtype);
4902 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4906 #ifdef HAS_GETNETENT
4907 nent = PerlSock_getnetent();
4909 DIE(aTHX_ PL_no_sock_func, "getnetent");
4912 #ifdef HOST_NOT_FOUND
4914 #ifdef USE_REENTRANT_API
4915 # ifdef USE_GETNETENT_ERRNO
4916 h_errno = PL_reentrant_buffer->_getnetent_errno;
4919 STATUS_NATIVE_SET(h_errno);
4924 if (GIMME != G_ARRAY) {
4925 PUSHs(sv = sv_newmortal());
4927 if (which == OP_GNBYNAME)
4928 sv_setiv(sv, (IV)nent->n_net);
4930 sv_setpv(sv, nent->n_name);
4936 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4937 sv_setpv(sv, nent->n_name);
4938 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4939 for (elem = nent->n_aliases; elem && *elem; elem++) {
4940 sv_catpv(sv, *elem);
4942 sv_catpvn(sv, " ", 1);
4944 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4945 sv_setiv(sv, (IV)nent->n_addrtype);
4946 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4947 sv_setiv(sv, (IV)nent->n_net);
4952 DIE(aTHX_ PL_no_sock_func, "getnetent");
4958 #ifdef HAS_GETPROTOBYNAME
4959 return pp_gprotoent();
4961 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4967 #ifdef HAS_GETPROTOBYNUMBER
4968 return pp_gprotoent();
4970 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4976 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4978 I32 which = PL_op->op_type;
4979 register char **elem;
4981 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4982 struct protoent *getprotobyname(Netdb_name_t);
4983 struct protoent *getprotobynumber(int);
4984 struct protoent *getprotoent(void);
4986 struct protoent *pent;
4989 if (which == OP_GPBYNAME) {
4990 #ifdef HAS_GETPROTOBYNAME
4991 char* name = POPpbytex;
4992 pent = PerlSock_getprotobyname(name);
4994 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4997 else if (which == OP_GPBYNUMBER) {
4998 #ifdef HAS_GETPROTOBYNUMBER
5000 pent = PerlSock_getprotobynumber(number);
5002 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5006 #ifdef HAS_GETPROTOENT
5007 pent = PerlSock_getprotoent();
5009 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5013 if (GIMME != G_ARRAY) {
5014 PUSHs(sv = sv_newmortal());
5016 if (which == OP_GPBYNAME)
5017 sv_setiv(sv, (IV)pent->p_proto);
5019 sv_setpv(sv, pent->p_name);
5025 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5026 sv_setpv(sv, pent->p_name);
5027 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5028 for (elem = pent->p_aliases; elem && *elem; elem++) {
5029 sv_catpv(sv, *elem);
5031 sv_catpvn(sv, " ", 1);
5033 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5034 sv_setiv(sv, (IV)pent->p_proto);
5039 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5045 #ifdef HAS_GETSERVBYNAME
5046 return pp_gservent();
5048 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5054 #ifdef HAS_GETSERVBYPORT
5055 return pp_gservent();
5057 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5063 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5065 I32 which = PL_op->op_type;
5066 register char **elem;
5068 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5069 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5070 struct servent *getservbyport(int, Netdb_name_t);
5071 struct servent *getservent(void);
5073 struct servent *sent;
5076 if (which == OP_GSBYNAME) {
5077 #ifdef HAS_GETSERVBYNAME
5078 char *proto = POPpbytex;
5079 char *name = POPpbytex;
5081 if (proto && !*proto)
5084 sent = PerlSock_getservbyname(name, proto);
5086 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5089 else if (which == OP_GSBYPORT) {
5090 #ifdef HAS_GETSERVBYPORT
5091 char *proto = POPpbytex;
5092 unsigned short port = (unsigned short)POPu;
5094 if (proto && !*proto)
5098 port = PerlSock_htons(port);
5100 sent = PerlSock_getservbyport(port, proto);
5102 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5106 #ifdef HAS_GETSERVENT
5107 sent = PerlSock_getservent();
5109 DIE(aTHX_ PL_no_sock_func, "getservent");
5113 if (GIMME != G_ARRAY) {
5114 PUSHs(sv = sv_newmortal());
5116 if (which == OP_GSBYNAME) {
5118 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5120 sv_setiv(sv, (IV)(sent->s_port));
5124 sv_setpv(sv, sent->s_name);
5130 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5131 sv_setpv(sv, sent->s_name);
5132 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5133 for (elem = sent->s_aliases; elem && *elem; elem++) {
5134 sv_catpv(sv, *elem);
5136 sv_catpvn(sv, " ", 1);
5138 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5140 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5142 sv_setiv(sv, (IV)(sent->s_port));
5144 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5145 sv_setpv(sv, sent->s_proto);
5150 DIE(aTHX_ PL_no_sock_func, "getservent");
5156 #ifdef HAS_SETHOSTENT
5158 PerlSock_sethostent(TOPi);
5161 DIE(aTHX_ PL_no_sock_func, "sethostent");
5167 #ifdef HAS_SETNETENT
5169 PerlSock_setnetent(TOPi);
5172 DIE(aTHX_ PL_no_sock_func, "setnetent");
5178 #ifdef HAS_SETPROTOENT
5180 PerlSock_setprotoent(TOPi);
5183 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5189 #ifdef HAS_SETSERVENT
5191 PerlSock_setservent(TOPi);
5194 DIE(aTHX_ PL_no_sock_func, "setservent");
5200 #ifdef HAS_ENDHOSTENT
5202 PerlSock_endhostent();
5206 DIE(aTHX_ PL_no_sock_func, "endhostent");
5212 #ifdef HAS_ENDNETENT
5214 PerlSock_endnetent();
5218 DIE(aTHX_ PL_no_sock_func, "endnetent");
5224 #ifdef HAS_ENDPROTOENT
5226 PerlSock_endprotoent();
5230 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5236 #ifdef HAS_ENDSERVENT
5238 PerlSock_endservent();
5242 DIE(aTHX_ PL_no_sock_func, "endservent");
5251 DIE(aTHX_ PL_no_func, "getpwnam");
5260 DIE(aTHX_ PL_no_func, "getpwuid");
5268 I32 which = PL_op->op_type;
5271 struct passwd *pwent = NULL;
5273 * We currently support only the SysV getsp* shadow password interface.
5274 * The interface is declared in <shadow.h> and often one needs to link
5275 * with -lsecurity or some such.
5276 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5279 * AIX getpwnam() is clever enough to return the encrypted password
5280 * only if the caller (euid?) is root.
5282 * There are at least three other shadow password APIs. Many platforms
5283 * seem to contain more than one interface for accessing the shadow
5284 * password databases, possibly for compatibility reasons.
5285 * The getsp*() is by far he simplest one, the other two interfaces
5286 * are much more complicated, but also very similar to each other.
5291 * struct pr_passwd *getprpw*();
5292 * The password is in
5293 * char getprpw*(...).ufld.fd_encrypt[]
5294 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5299 * struct es_passwd *getespw*();
5300 * The password is in
5301 * char *(getespw*(...).ufld.fd_encrypt)
5302 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5305 * struct userpw *getuserpw();
5306 * The password is in
5307 * char *(getuserpw(...)).spw_upw_passwd
5308 * (but the de facto standard getpwnam() should work okay)
5310 * Mention I_PROT here so that Configure probes for it.
5312 * In HP-UX for getprpw*() the manual page claims that one should include
5313 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5314 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5315 * and pp_sys.c already includes <shadow.h> if there is such.
5317 * Note that <sys/security.h> is already probed for, but currently
5318 * it is only included in special cases.
5320 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5321 * be preferred interface, even though also the getprpw*() interface
5322 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5323 * One also needs to call set_auth_parameters() in main() before
5324 * doing anything else, whether one is using getespw*() or getprpw*().
5326 * Note that accessing the shadow databases can be magnitudes
5327 * slower than accessing the standard databases.
5332 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5333 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5334 * the pw_comment is left uninitialized. */
5335 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5341 char* name = POPpbytex;
5342 pwent = getpwnam(name);
5348 pwent = getpwuid(uid);
5352 # ifdef HAS_GETPWENT
5354 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5355 if (pwent) pwent = getpwnam(pwent->pw_name);
5358 DIE(aTHX_ PL_no_func, "getpwent");
5364 if (GIMME != G_ARRAY) {
5365 PUSHs(sv = sv_newmortal());
5367 if (which == OP_GPWNAM)
5368 # if Uid_t_sign <= 0
5369 sv_setiv(sv, (IV)pwent->pw_uid);
5371 sv_setuv(sv, (UV)pwent->pw_uid);
5374 sv_setpv(sv, pwent->pw_name);
5380 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5381 sv_setpv(sv, pwent->pw_name);
5383 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5385 /* If we have getspnam(), we try to dig up the shadow
5386 * password. If we are underprivileged, the shadow
5387 * interface will set the errno to EACCES or similar,
5388 * and return a null pointer. If this happens, we will
5389 * use the dummy password (usually "*" or "x") from the
5390 * standard password database.
5392 * In theory we could skip the shadow call completely
5393 * if euid != 0 but in practice we cannot know which
5394 * security measures are guarding the shadow databases
5395 * on a random platform.
5397 * Resist the urge to use additional shadow interfaces.
5398 * Divert the urge to writing an extension instead.
5401 /* Some AIX setups falsely(?) detect some getspnam(), which
5402 * has a different API than the Solaris/IRIX one. */
5403 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5405 struct spwd *spwent;
5406 int saverrno; /* Save and restore errno so that
5407 * underprivileged attempts seem
5408 * to have never made the unsccessful
5409 * attempt to retrieve the shadow password. */
5412 spwent = getspnam(pwent->pw_name);
5414 if (spwent && spwent->sp_pwdp)
5415 sv_setpv(sv, spwent->sp_pwdp);
5419 if (!SvPOK(sv)) /* Use the standard password, then. */
5420 sv_setpv(sv, pwent->pw_passwd);
5423 # ifndef INCOMPLETE_TAINTS
5424 /* passwd is tainted because user himself can diddle with it.
5425 * admittedly not much and in a very limited way, but nevertheless. */
5429 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5430 # if Uid_t_sign <= 0
5431 sv_setiv(sv, (IV)pwent->pw_uid);
5433 sv_setuv(sv, (UV)pwent->pw_uid);
5436 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5437 # if Uid_t_sign <= 0
5438 sv_setiv(sv, (IV)pwent->pw_gid);
5440 sv_setuv(sv, (UV)pwent->pw_gid);
5442 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5443 * because of the poor interface of the Perl getpw*(),
5444 * not because there's some standard/convention saying so.
5445 * A better interface would have been to return a hash,
5446 * but we are accursed by our history, alas. --jhi. */
5447 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5449 sv_setiv(sv, (IV)pwent->pw_change);
5452 sv_setiv(sv, (IV)pwent->pw_quota);
5455 sv_setpv(sv, pwent->pw_age);
5460 /* pw_class and pw_comment are mutually exclusive--.
5461 * see the above note for pw_change, pw_quota, and pw_age. */
5462 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5464 sv_setpv(sv, pwent->pw_class);
5467 sv_setpv(sv, pwent->pw_comment);
5471 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5473 sv_setpv(sv, pwent->pw_gecos);
5475 # ifndef INCOMPLETE_TAINTS
5476 /* pw_gecos is tainted because user himself can diddle with it. */
5480 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5481 sv_setpv(sv, pwent->pw_dir);
5483 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5484 sv_setpv(sv, pwent->pw_shell);
5485 # ifndef INCOMPLETE_TAINTS
5486 /* pw_shell is tainted because user himself can diddle with it. */
5491 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5492 sv_setiv(sv, (IV)pwent->pw_expire);
5497 DIE(aTHX_ PL_no_func, "getpwent");
5503 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5508 DIE(aTHX_ PL_no_func, "setpwent");
5514 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5519 DIE(aTHX_ PL_no_func, "endpwent");
5528 DIE(aTHX_ PL_no_func, "getgrnam");
5537 DIE(aTHX_ PL_no_func, "getgrgid");
5545 I32 which = PL_op->op_type;
5546 register char **elem;
5548 struct group *grent;
5551 if (which == OP_GGRNAM) {
5552 char* name = POPpbytex;
5553 grent = (struct group *)getgrnam(name);
5555 else if (which == OP_GGRGID) {
5557 grent = (struct group *)getgrgid(gid);
5561 grent = (struct group *)getgrent();
5563 DIE(aTHX_ PL_no_func, "getgrent");
5567 if (GIMME != G_ARRAY) {
5568 PUSHs(sv = sv_newmortal());
5570 if (which == OP_GGRNAM)
5571 sv_setiv(sv, (IV)grent->gr_gid);
5573 sv_setpv(sv, grent->gr_name);
5579 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5580 sv_setpv(sv, grent->gr_name);
5582 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5584 sv_setpv(sv, grent->gr_passwd);
5587 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5588 sv_setiv(sv, (IV)grent->gr_gid);
5590 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5591 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5592 /* In UNICOS/mk (_CRAYMPP) the multithreading
5593 * versions (getgrnam_r, getgrgid_r)
5594 * seem to return an illegal pointer
5595 * as the group members list, gr_mem.
5596 * getgrent() doesn't even have a _r version
5597 * but the gr_mem is poisonous anyway.
5598 * So yes, you cannot get the list of group
5599 * members if building multithreaded in UNICOS/mk. */
5600 for (elem = grent->gr_mem; elem && *elem; elem++) {
5601 sv_catpv(sv, *elem);
5603 sv_catpvn(sv, " ", 1);
5610 DIE(aTHX_ PL_no_func, "getgrent");
5616 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5621 DIE(aTHX_ PL_no_func, "setgrent");
5627 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5632 DIE(aTHX_ PL_no_func, "endgrent");
5642 if (!(tmps = PerlProc_getlogin()))
5644 PUSHp(tmps, strlen(tmps));
5647 DIE(aTHX_ PL_no_func, "getlogin");
5651 /* Miscellaneous. */
5656 dSP; dMARK; dORIGMARK; dTARGET;
5657 register I32 items = SP - MARK;
5658 unsigned long a[20];
5664 while (++MARK <= SP) {
5665 if (SvTAINTED(*MARK)) {
5671 TAINT_PROPER("syscall");
5674 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5675 * or where sizeof(long) != sizeof(char*). But such machines will
5676 * not likely have syscall implemented either, so who cares?
5678 while (++MARK <= SP) {
5679 if (SvNIOK(*MARK) || !i)
5680 a[i++] = SvIV(*MARK);
5681 else if (*MARK == &PL_sv_undef)
5684 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5690 DIE(aTHX_ "Too many args to syscall");
5692 DIE(aTHX_ "Too few args to syscall");
5694 retval = syscall(a[0]);
5697 retval = syscall(a[0],a[1]);
5700 retval = syscall(a[0],a[1],a[2]);
5703 retval = syscall(a[0],a[1],a[2],a[3]);
5706 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5709 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5712 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5715 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5719 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5722 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5725 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5729 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5733 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5737 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5738 a[10],a[11],a[12],a[13]);
5740 #endif /* atarist */
5746 DIE(aTHX_ PL_no_func, "syscall");
5750 #ifdef FCNTL_EMULATE_FLOCK
5752 /* XXX Emulate flock() with fcntl().
5753 What's really needed is a good file locking module.
5757 fcntl_emulate_flock(int fd, int operation)
5761 switch (operation & ~LOCK_NB) {
5763 flock.l_type = F_RDLCK;
5766 flock.l_type = F_WRLCK;
5769 flock.l_type = F_UNLCK;
5775 flock.l_whence = SEEK_SET;
5776 flock.l_start = flock.l_len = (Off_t)0;
5778 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5781 #endif /* FCNTL_EMULATE_FLOCK */
5783 #ifdef LOCKF_EMULATE_FLOCK
5785 /* XXX Emulate flock() with lockf(). This is just to increase
5786 portability of scripts. The calls are not completely
5787 interchangeable. What's really needed is a good file
5791 /* The lockf() constants might have been defined in <unistd.h>.
5792 Unfortunately, <unistd.h> causes troubles on some mixed
5793 (BSD/POSIX) systems, such as SunOS 4.1.3.
5795 Further, the lockf() constants aren't POSIX, so they might not be
5796 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5797 just stick in the SVID values and be done with it. Sigh.
5801 # define F_ULOCK 0 /* Unlock a previously locked region */
5804 # define F_LOCK 1 /* Lock a region for exclusive use */
5807 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5810 # define F_TEST 3 /* Test a region for other processes locks */
5814 lockf_emulate_flock(int fd, int operation)
5820 /* flock locks entire file so for lockf we need to do the same */
5822 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5823 if (pos > 0) /* is seekable and needs to be repositioned */
5824 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5825 pos = -1; /* seek failed, so don't seek back afterwards */
5828 switch (operation) {
5830 /* LOCK_SH - get a shared lock */
5832 /* LOCK_EX - get an exclusive lock */
5834 i = lockf (fd, F_LOCK, 0);
5837 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5838 case LOCK_SH|LOCK_NB:
5839 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5840 case LOCK_EX|LOCK_NB:
5841 i = lockf (fd, F_TLOCK, 0);
5843 if ((errno == EAGAIN) || (errno == EACCES))
5844 errno = EWOULDBLOCK;
5847 /* LOCK_UN - unlock (non-blocking is a no-op) */
5849 case LOCK_UN|LOCK_NB:
5850 i = lockf (fd, F_ULOCK, 0);
5853 /* Default - can't decipher operation */
5860 if (pos > 0) /* need to restore position of the handle */
5861 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5866 #endif /* LOCKF_EMULATE_FLOCK */