3 * Copyright (C) 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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) {
350 PL_rs = &PL_sv_undef;
351 sv_setpv(TARG, ""); /* note that this preserves previous buffer */
352 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
364 if (sv_gets(sv, fp, 0) == Nullch) {
368 XPUSHs(sv_2mortal(sv));
369 if (SvLEN(sv) - SvCUR(sv) > 20) {
370 SvLEN_set(sv, SvCUR(sv)+1);
371 Renew(SvPVX(sv), SvLEN(sv), char);
376 STATUS_NATIVE_SET(PerlProc_pclose(fp));
377 TAINT; /* "I believe that this is not gratuitous!" */
380 STATUS_NATIVE_SET(-1);
381 if (gimme == G_SCALAR)
391 tryAMAGICunTARGET(iter, -1);
393 /* Note that we only ever get here if File::Glob fails to load
394 * without at the same time croaking, for some reason, or if
395 * perl was built with PERL_EXTERNAL_GLOB */
402 * The external globbing program may use things we can't control,
403 * so for security reasons we must assume the worst.
406 taint_proper(PL_no_security, "glob");
410 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
411 PL_last_in_gv = (GV*)*PL_stack_sp--;
413 SAVESPTR(PL_rs); /* This is not permanent, either. */
414 PL_rs = sv_2mortal(newSVpvn("\000", 1));
417 *SvPVX(PL_rs) = '\n';
421 result = do_readline();
428 PL_last_in_gv = cGVOP_gv;
429 return do_readline();
438 if (SP - MARK != 1) {
440 do_join(TARG, &PL_sv_no, MARK, SP);
447 tmps = SvPV(tmpsv, len);
448 if ((!tmps || !len) && PL_errgv) {
450 (void)SvUPGRADE(error, SVt_PV);
451 if (SvPOK(error) && SvCUR(error))
452 sv_catpv(error, "\t...caught");
454 tmps = SvPV(tmpsv, len);
457 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
459 Perl_warn(aTHX_ "%"SVf, tmpsv);
471 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
473 if (SP - MARK != 1) {
475 do_join(TARG, &PL_sv_no, MARK, SP);
477 tmps = SvPV(tmpsv, len);
483 tmps = SvROK(tmpsv) ? Nullch : SvPV(tmpsv, len);
487 (void)SvUPGRADE(error, SVt_PV);
488 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
490 SvSetSV(error,tmpsv);
491 else if (sv_isobject(error)) {
492 HV *stash = SvSTASH(SvRV(error));
493 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
495 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
496 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
503 call_sv((SV*)GvCV(gv),
504 G_SCALAR|G_EVAL|G_KEEPERR);
505 sv_setsv(error,*PL_stack_sp--);
511 if (SvPOK(error) && SvCUR(error))
512 sv_catpv(error, "\t...propagated");
514 tmps = SvPV(tmpsv, len);
518 tmpsv = sv_2mortal(newSVpvn("Died", 4));
520 DIE(aTHX_ "%"SVf, tmpsv);
540 DIE(aTHX_ PL_no_usym, "filehandle");
541 if ((io = GvIOp(gv)))
542 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
544 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
545 /* Method's args are same as ours ... */
546 /* ... except handle is replaced by the object */
547 *MARK-- = SvTIED_obj((SV*)io, mg);
551 call_method("OPEN", G_SCALAR);
564 tmps = SvPV(sv, len);
565 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
568 PUSHi( (I32)PL_forkprocess );
569 else if (PL_forkprocess == 0) /* we are a new child */
588 if (gv && (io = GvIO(gv))
589 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
592 XPUSHs(SvTIED_obj((SV*)io, mg));
595 call_method("CLOSE", G_SCALAR);
601 PUSHs(boolSV(do_close(gv, TRUE)));
621 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
622 DIE(aTHX_ PL_no_usym, "filehandle");
627 do_close(rgv, FALSE);
629 do_close(wgv, FALSE);
631 if (PerlProc_pipe(fd) < 0)
634 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
635 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
636 IoOFP(rstio) = IoIFP(rstio);
637 IoIFP(wstio) = IoOFP(wstio);
638 IoTYPE(rstio) = IoTYPE_RDONLY;
639 IoTYPE(wstio) = IoTYPE_WRONLY;
641 if (!IoIFP(rstio) || !IoOFP(wstio)) {
642 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
643 else PerlLIO_close(fd[0]);
644 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
645 else PerlLIO_close(fd[1]);
648 #if defined(HAS_FCNTL) && defined(F_SETFD)
649 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
650 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
657 DIE(aTHX_ PL_no_func, "pipe");
673 if (gv && (io = GvIO(gv))
674 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
677 XPUSHs(SvTIED_obj((SV*)io, mg));
680 call_method("FILENO", G_SCALAR);
686 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
687 /* Can't do this because people seem to do things like
688 defined(fileno($foo)) to check whether $foo is a valid fh.
689 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
690 report_evil_fh(gv, io, PL_op->op_type);
695 PUSHi(PerlIO_fileno(fp));
706 anum = PerlLIO_umask(0);
707 (void)PerlLIO_umask(anum);
710 anum = PerlLIO_umask(POPi);
711 TAINT_PROPER("umask");
714 /* Only DIE if trying to restrict permissions on `user' (self).
715 * Otherwise it's harmless and more useful to just return undef
716 * since 'group' and 'other' concepts probably don't exist here. */
717 if (MAXARG >= 1 && (POPi & 0700))
718 DIE(aTHX_ "umask not implemented");
719 XPUSHs(&PL_sv_undef);
741 if (gv && (io = GvIO(gv))
742 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
745 XPUSHs(SvTIED_obj((SV*)io, mg));
750 call_method("BINMODE", G_SCALAR);
757 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
758 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
759 report_evil_fh(gv, io, PL_op->op_type);
760 SETERRNO(EBADF,RMS_IFI);
765 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
766 (discp) ? SvPV_nolen(discp) : Nullch)) {
767 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
768 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
769 mode_from_discipline(discp),
770 (discp) ? SvPV_nolen(discp) : Nullch)) {
792 I32 markoff = MARK - PL_stack_base;
794 int how = PERL_MAGIC_tied;
798 switch(SvTYPE(varsv)) {
800 methname = "TIEHASH";
801 HvEITER((HV *)varsv) = Null(HE *);
804 methname = "TIEARRAY";
807 #ifdef GV_UNIQUE_CHECK
808 if (GvUNIQUE((GV*)varsv)) {
809 Perl_croak(aTHX_ "Attempt to tie unique GV");
812 methname = "TIEHANDLE";
813 how = PERL_MAGIC_tiedscalar;
814 /* For tied filehandles, we apply tiedscalar magic to the IO
815 slot of the GP rather than the GV itself. AMS 20010812 */
817 GvIOp(varsv) = newIO();
818 varsv = (SV *)GvIOp(varsv);
821 methname = "TIESCALAR";
822 how = PERL_MAGIC_tiedscalar;
826 if (sv_isobject(*MARK)) {
828 PUSHSTACKi(PERLSI_MAGIC);
830 EXTEND(SP,(I32)items);
834 call_method(methname, G_SCALAR);
837 /* Not clear why we don't call call_method here too.
838 * perhaps to get different error message ?
840 stash = gv_stashsv(*MARK, FALSE);
841 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
842 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
846 PUSHSTACKi(PERLSI_MAGIC);
848 EXTEND(SP,(I32)items);
852 call_sv((SV*)GvCV(gv), G_SCALAR);
858 if (sv_isobject(sv)) {
859 sv_unmagic(varsv, how);
860 /* Croak if a self-tie on an aggregate is attempted. */
861 if (varsv == SvRV(sv) &&
862 (SvTYPE(varsv) == SVt_PVAV ||
863 SvTYPE(varsv) == SVt_PVHV))
865 "Self-ties of arrays and hashes are not supported");
866 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
869 SP = PL_stack_base + markoff;
879 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
880 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
882 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
885 if ((mg = SvTIED_mg(sv, how))) {
886 SV *obj = SvRV(SvTIED_obj(sv, mg));
890 if ((gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE)) &&
891 isGV(gv) && (cv = GvCV(gv))) {
893 XPUSHs(SvTIED_obj((SV*)gv, mg));
894 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
897 call_sv((SV *)cv, G_VOID);
901 else if (ckWARN(WARN_UNTIE)) {
902 if (mg && SvREFCNT(obj) > 1)
903 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
904 "untie attempted while %"UVuf" inner references still exist",
905 (UV)SvREFCNT(obj) - 1 ) ;
909 sv_unmagic(sv, how) ;
918 char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
919 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
921 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
924 if ((mg = SvTIED_mg(sv, how))) {
925 SV *osv = SvTIED_obj(sv, mg);
926 if (osv == mg->mg_obj)
927 osv = sv_mortalcopy(osv);
945 sv = sv_mortalcopy(&PL_sv_no);
946 sv_setpv(sv, "AnyDBM_File");
947 stash = gv_stashsv(sv, FALSE);
948 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
950 require_pv("AnyDBM_File.pm");
952 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
953 DIE(aTHX_ "No dbm on this machine");
963 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
965 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
968 call_sv((SV*)GvCV(gv), G_SCALAR);
971 if (!sv_isobject(TOPs)) {
976 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
979 call_sv((SV*)GvCV(gv), G_SCALAR);
983 if (sv_isobject(TOPs)) {
984 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
985 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
1007 struct timeval timebuf;
1008 struct timeval *tbuf = &timebuf;
1012 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1017 # if BYTEORDER & 0xf0000
1018 # define ORDERBYTE (0x88888888 - BYTEORDER)
1020 # define ORDERBYTE (0x4444 - BYTEORDER)
1026 for (i = 1; i <= 3; i++) {
1034 /* little endians can use vecs directly */
1035 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1042 masksize = NFDBITS / NBBY;
1044 masksize = sizeof(long); /* documented int, everyone seems to use long */
1046 Zero(&fd_sets[0], 4, char*);
1049 # if SELECT_MIN_BITS == 1
1050 growsize = sizeof(fd_set);
1052 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1053 # undef SELECT_MIN_BITS
1054 # define SELECT_MIN_BITS __FD_SETSIZE
1056 /* If SELECT_MIN_BITS is greater than one we most probably will want
1057 * to align the sizes with SELECT_MIN_BITS/8 because for example
1058 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1059 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1060 * on (sets/tests/clears bits) is 32 bits. */
1061 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1069 timebuf.tv_sec = (long)value;
1070 value -= (NV)timebuf.tv_sec;
1071 timebuf.tv_usec = (long)(value * 1000000.0);
1074 tbuf = Null(struct timeval*);
1076 for (i = 1; i <= 3; i++) {
1082 else if (!SvPOK(sv))
1083 SvPV_force(sv,n_a); /* force string conversion */
1086 Sv_Grow(sv, growsize);
1090 while (++j <= growsize) {
1094 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1096 New(403, fd_sets[i], growsize, char);
1097 for (offset = 0; offset < growsize; offset += masksize) {
1098 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1099 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1102 fd_sets[i] = SvPVX(sv);
1106 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1107 /* Can't make just the (void*) conditional because that would be
1108 * cpp #if within cpp macro, and not all compilers like that. */
1109 nfound = PerlSock_select(
1111 (Select_fd_set_t) fd_sets[1],
1112 (Select_fd_set_t) fd_sets[2],
1113 (Select_fd_set_t) fd_sets[3],
1114 (void*) tbuf); /* Workaround for compiler bug. */
1116 nfound = PerlSock_select(
1118 (Select_fd_set_t) fd_sets[1],
1119 (Select_fd_set_t) fd_sets[2],
1120 (Select_fd_set_t) fd_sets[3],
1123 for (i = 1; i <= 3; i++) {
1126 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1128 for (offset = 0; offset < growsize; offset += masksize) {
1129 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1130 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1132 Safefree(fd_sets[i]);
1139 if (GIMME == G_ARRAY && tbuf) {
1140 value = (NV)(timebuf.tv_sec) +
1141 (NV)(timebuf.tv_usec) / 1000000.0;
1142 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1143 sv_setnv(sv, value);
1147 DIE(aTHX_ "select not implemented");
1152 Perl_setdefout(pTHX_ GV *gv)
1155 (void)SvREFCNT_inc(gv);
1157 SvREFCNT_dec(PL_defoutgv);
1164 GV *newdefout, *egv;
1167 newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1169 egv = GvEGV(PL_defoutgv);
1174 XPUSHs(&PL_sv_undef);
1176 GV **gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1177 if (gvp && *gvp == egv) {
1178 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1182 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1187 if (!GvIO(newdefout))
1188 gv_IOadd(newdefout);
1189 setdefout(newdefout);
1207 if (gv && (io = GvIO(gv))
1208 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1210 I32 gimme = GIMME_V;
1212 XPUSHs(SvTIED_obj((SV*)io, mg));
1215 call_method("GETC", gimme);
1218 if (gimme == G_SCALAR)
1219 SvSetMagicSV_nosteal(TARG, TOPs);
1222 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1223 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)
1224 && (!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY)))
1225 report_evil_fh(gv, io, PL_op->op_type);
1226 SETERRNO(EBADF,RMS_IFI);
1230 sv_setpv(TARG, " ");
1231 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1232 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1233 /* Find out how many bytes the char needs */
1234 Size_t len = UTF8SKIP(SvPVX(TARG));
1237 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1238 SvCUR_set(TARG,1+len);
1248 return pp_sysread();
1252 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1254 register PERL_CONTEXT *cx;
1255 I32 gimme = GIMME_V;
1260 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1262 cx->blk_sub.retop = retop;
1263 PAD_SET_CUR(CvPADLIST(cv), 1);
1265 setdefout(gv); /* locally select filehandle so $% et al work */
1298 SV *tmpsv = sv_newmortal();
1299 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1300 name = SvPV_nolen(tmpsv);
1303 DIE(aTHX_ "Undefined format \"%s\" called", name);
1304 DIE(aTHX_ "Not a format reference");
1307 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1309 IoFLAGS(io) &= ~IOf_DIDTOP;
1310 return doform(cv,gv,PL_op->op_next);
1316 GV *gv = cxstack[cxstack_ix].blk_sub.gv;
1317 register IO *io = GvIOp(gv);
1318 PerlIO *ofp = IoOFP(io);
1322 register PERL_CONTEXT *cx;
1324 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1325 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1328 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1329 PL_formtarget != PL_toptarget)
1333 if (!IoTOP_GV(io)) {
1337 if (!IoTOP_NAME(io)) {
1338 if (!IoFMT_NAME(io))
1339 IoFMT_NAME(io) = savepv(GvNAME(gv));
1340 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1341 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1342 if ((topgv && GvFORM(topgv)) ||
1343 !gv_fetchpv("top",FALSE,SVt_PVFM))
1344 IoTOP_NAME(io) = savesvpv(topname);
1346 IoTOP_NAME(io) = savepvn("top", 3);
1348 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1349 if (!topgv || !GvFORM(topgv)) {
1350 IoLINES_LEFT(io) = 100000000;
1353 IoTOP_GV(io) = topgv;
1355 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1356 I32 lines = IoLINES_LEFT(io);
1357 char *s = SvPVX(PL_formtarget);
1358 if (lines <= 0) /* Yow, header didn't even fit!!! */
1360 while (lines-- > 0) {
1361 s = strchr(s, '\n');
1367 STRLEN save = SvCUR(PL_formtarget);
1368 SvCUR_set(PL_formtarget, s - SvPVX(PL_formtarget));
1369 do_print(PL_formtarget, ofp);
1370 SvCUR_set(PL_formtarget, save);
1371 sv_chop(PL_formtarget, s);
1372 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1375 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1376 do_print(PL_formfeed, ofp);
1377 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1379 PL_formtarget = PL_toptarget;
1380 IoFLAGS(io) |= IOf_DIDTOP;
1383 DIE(aTHX_ "bad top format reference");
1388 SV *sv = sv_newmortal();
1389 gv_efullname4(sv, fgv, Nullch, FALSE);
1390 name = SvPV_nolen(sv);
1393 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1396 DIE(aTHX_ "Undefined top format called");
1400 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1401 return doform(cv,gv,PL_op);
1405 POPBLOCK(cx,PL_curpm);
1411 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1413 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1414 else if (ckWARN(WARN_CLOSED))
1415 report_evil_fh(gv, io, PL_op->op_type);
1420 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1421 if (ckWARN(WARN_IO))
1422 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1424 if (!do_print(PL_formtarget, fp))
1427 FmLINES(PL_formtarget) = 0;
1428 SvCUR_set(PL_formtarget, 0);
1429 *SvEND(PL_formtarget) = '\0';
1430 if (IoFLAGS(io) & IOf_FLUSH)
1431 (void)PerlIO_flush(fp);
1436 PL_formtarget = PL_bodytarget;
1438 return cx->blk_sub.retop;
1443 dSP; dMARK; dORIGMARK;
1450 if (PL_op->op_flags & OPf_STACKED)
1455 if (gv && (io = GvIO(gv))
1456 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1458 if (MARK == ORIGMARK) {
1461 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1465 *MARK = SvTIED_obj((SV*)io, mg);
1468 call_method("PRINTF", G_SCALAR);
1471 MARK = ORIGMARK + 1;
1478 if (!(io = GvIO(gv))) {
1479 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1480 report_evil_fh(gv, io, PL_op->op_type);
1481 SETERRNO(EBADF,RMS_IFI);
1484 else if (!(fp = IoOFP(io))) {
1485 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1487 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1488 else if (ckWARN(WARN_CLOSED))
1489 report_evil_fh(gv, io, PL_op->op_type);
1491 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1495 do_sprintf(sv, SP - MARK, MARK + 1);
1496 if (!do_print(sv, fp))
1499 if (IoFLAGS(io) & IOf_FLUSH)
1500 if (PerlIO_flush(fp) == EOF)
1511 PUSHs(&PL_sv_undef);
1532 /* Need TIEHANDLE method ? */
1534 tmps = SvPV(sv, len);
1535 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1536 IoLINES(GvIOp(gv)) = 0;
1540 PUSHs(&PL_sv_undef);
1547 dSP; dMARK; dORIGMARK; dTARGET;
1554 Sock_size_t bufsize;
1563 bool charstart = FALSE;
1564 STRLEN charskip = 0;
1568 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1569 && gv && (io = GvIO(gv))
1570 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1575 *MARK = SvTIED_obj((SV*)io, mg);
1577 call_method("READ", G_SCALAR);
1590 sv_setpvn(bufsv, "", 0);
1591 length = SvIVx(*++MARK);
1594 offset = SvIVx(*++MARK);
1598 if (!io || !IoIFP(io)) {
1599 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1600 report_evil_fh(gv, io, PL_op->op_type);
1601 SETERRNO(EBADF,RMS_IFI);
1604 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1605 buffer = SvPVutf8_force(bufsv, blen);
1606 /* UTF-8 may not have been set if they are all low bytes */
1611 buffer = SvPV_force(bufsv, blen);
1612 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1615 DIE(aTHX_ "Negative length");
1623 if (PL_op->op_type == OP_RECV) {
1624 char namebuf[MAXPATHLEN];
1625 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1626 bufsize = sizeof (struct sockaddr_in);
1628 bufsize = sizeof namebuf;
1630 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1634 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1635 /* 'offset' means 'flags' here */
1636 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1637 (struct sockaddr *)namebuf, &bufsize);
1641 /* Bogus return without padding */
1642 bufsize = sizeof (struct sockaddr_in);
1644 SvCUR_set(bufsv, count);
1645 *SvEND(bufsv) = '\0';
1646 (void)SvPOK_only(bufsv);
1650 /* This should not be marked tainted if the fp is marked clean */
1651 if (!(IoFLAGS(io) & IOf_UNTAINT))
1652 SvTAINTED_on(bufsv);
1654 sv_setpvn(TARG, namebuf, bufsize);
1659 if (PL_op->op_type == OP_RECV)
1660 DIE(aTHX_ PL_no_sock_func, "recv");
1662 if (DO_UTF8(bufsv)) {
1663 /* offset adjust in characters not bytes */
1664 blen = sv_len_utf8(bufsv);
1667 if (-offset > (int)blen)
1668 DIE(aTHX_ "Offset outside string");
1671 if (DO_UTF8(bufsv)) {
1672 /* convert offset-as-chars to offset-as-bytes */
1673 if (offset >= (int)blen)
1674 offset += SvCUR(bufsv) - blen;
1676 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1679 bufsize = SvCUR(bufsv);
1680 /* Allocating length + offset + 1 isn't perfect in the case of reading
1681 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1683 (should be 2 * length + offset + 1, or possibly something longer if
1684 PL_encoding is true) */
1685 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1686 if (offset > bufsize) { /* Zero any newly allocated space */
1687 Zero(buffer+bufsize, offset-bufsize, char);
1689 buffer = buffer + offset;
1691 read_target = bufsv;
1693 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1694 concatenate it to the current buffer. */
1696 /* Truncate the existing buffer to the start of where we will be
1698 SvCUR_set(bufsv, offset);
1700 read_target = sv_newmortal();
1701 SvUPGRADE(read_target, SVt_PV);
1702 buffer = SvGROW(read_target, length + 1);
1705 if (PL_op->op_type == OP_SYSREAD) {
1706 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1707 if (IoTYPE(io) == IoTYPE_SOCKET) {
1708 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1714 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1719 #ifdef HAS_SOCKET__bad_code_maybe
1720 if (IoTYPE(io) == IoTYPE_SOCKET) {
1721 char namebuf[MAXPATHLEN];
1722 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1723 bufsize = sizeof (struct sockaddr_in);
1725 bufsize = sizeof namebuf;
1727 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1728 (struct sockaddr *)namebuf, &bufsize);
1733 count = PerlIO_read(IoIFP(io), buffer, length);
1734 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1735 if (count == 0 && PerlIO_error(IoIFP(io)))
1739 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1740 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1743 SvCUR_set(read_target, count+(buffer - SvPVX(read_target)));
1744 *SvEND(read_target) = '\0';
1745 (void)SvPOK_only(read_target);
1746 if (fp_utf8 && !IN_BYTES) {
1747 /* Look at utf8 we got back and count the characters */
1748 char *bend = buffer + count;
1749 while (buffer < bend) {
1751 skip = UTF8SKIP(buffer);
1754 if (buffer - charskip + skip > bend) {
1755 /* partial character - try for rest of it */
1756 length = skip - (bend-buffer);
1757 offset = bend - SvPVX(bufsv);
1769 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1770 provided amount read (count) was what was requested (length)
1772 if (got < wanted && count == length) {
1773 length = wanted - got;
1774 offset = bend - SvPVX(bufsv);
1777 /* return value is character count */
1781 else if (buffer_utf8) {
1782 /* Let svcatsv upgrade the bytes we read in to utf8.
1783 The buffer is a mortal so will be freed soon. */
1784 sv_catsv_nomg(bufsv, read_target);
1787 /* This should not be marked tainted if the fp is marked clean */
1788 if (!(IoFLAGS(io) & IOf_UNTAINT))
1789 SvTAINTED_on(bufsv);
1802 int items = (SP - PL_stack_base) - TOPMARK;
1806 sv = sv_2mortal(newSViv(sv_len(*SP)));
1815 dSP; dMARK; dORIGMARK; dTARGET;
1826 if (PL_op->op_type == OP_SYSWRITE
1827 && gv && (io = GvIO(gv))
1828 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1833 *MARK = SvTIED_obj((SV*)io, mg);
1835 call_method("WRITE", G_SCALAR);
1846 #if Size_t_size > IVSIZE
1847 length = (Size_t)SvNVx(*++MARK);
1849 length = (Size_t)SvIVx(*++MARK);
1851 if ((SSize_t)length < 0)
1852 DIE(aTHX_ "Negative length");
1855 if (!io || !IoIFP(io)) {
1857 if (ckWARN(WARN_CLOSED))
1858 report_evil_fh(gv, io, PL_op->op_type);
1859 SETERRNO(EBADF,RMS_IFI);
1863 if (PerlIO_isutf8(IoIFP(io))) {
1864 if (!SvUTF8(bufsv)) {
1865 bufsv = sv_2mortal(newSVsv(bufsv));
1866 buffer = sv_2pvutf8(bufsv, &blen);
1868 buffer = SvPV(bufsv, blen);
1871 if (DO_UTF8(bufsv)) {
1872 /* Not modifying source SV, so making a temporary copy. */
1873 bufsv = sv_2mortal(newSVsv(bufsv));
1874 sv_utf8_downgrade(bufsv, FALSE);
1876 buffer = SvPV(bufsv, blen);
1879 if (PL_op->op_type == OP_SYSWRITE) {
1881 if (DO_UTF8(bufsv)) {
1882 /* length and offset are in chars */
1883 blen = sv_len_utf8(bufsv);
1886 offset = SvIVx(*++MARK);
1888 if (-offset > (IV)blen)
1889 DIE(aTHX_ "Offset outside string");
1891 } else if (offset >= (IV)blen && blen > 0)
1892 DIE(aTHX_ "Offset outside string");
1895 if (length > blen - offset)
1896 length = blen - offset;
1897 if (DO_UTF8(bufsv)) {
1898 buffer = (char*)utf8_hop((U8 *)buffer, offset);
1899 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1902 buffer = buffer+offset;
1904 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1905 if (IoTYPE(io) == IoTYPE_SOCKET) {
1906 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1912 /* See the note at doio.c:do_print about filesize limits. --jhi */
1913 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1918 else if (SP > MARK) {
1921 sockbuf = SvPVx(*++MARK, mlen);
1922 /* length is really flags */
1923 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1924 length, (struct sockaddr *)sockbuf, mlen);
1927 /* length is really flags */
1928 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1931 DIE(aTHX_ PL_no_sock_func, "send");
1937 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1938 #if Size_t_size > IVSIZE
1952 return pp_sysread();
1963 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1965 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1967 if (io && !IoIFP(io)) {
1968 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1970 IoFLAGS(io) &= ~IOf_START;
1971 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1972 sv_setpvn(GvSV(gv), "-", 1);
1973 SvSETMAGIC(GvSV(gv));
1975 else if (!nextargv(gv))
1980 gv = PL_last_in_gv; /* eof */
1983 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1985 if (gv && (io = GvIO(gv))
1986 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1989 XPUSHs(SvTIED_obj((SV*)io, mg));
1992 call_method("EOF", G_SCALAR);
1998 PUSHs(boolSV(!gv || do_eof(gv)));
2012 gv = PL_last_in_gv = (GV*)POPs;
2014 if (gv && (io = GvIO(gv))
2015 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2018 XPUSHs(SvTIED_obj((SV*)io, mg));
2021 call_method("TELL", G_SCALAR);
2027 #if LSEEKSIZE > IVSIZE
2028 PUSHn( do_tell(gv) );
2030 PUSHi( do_tell(gv) );
2037 return pp_sysseek();
2046 #if LSEEKSIZE > IVSIZE
2047 Off_t offset = (Off_t)SvNVx(POPs);
2049 Off_t offset = (Off_t)SvIVx(POPs);
2053 gv = PL_last_in_gv = (GV*)POPs;
2055 if (gv && (io = GvIO(gv))
2056 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2059 XPUSHs(SvTIED_obj((SV*)io, mg));
2060 #if LSEEKSIZE > IVSIZE
2061 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2063 XPUSHs(sv_2mortal(newSViv(offset)));
2065 XPUSHs(sv_2mortal(newSViv(whence)));
2068 call_method("SEEK", G_SCALAR);
2074 if (PL_op->op_type == OP_SEEK)
2075 PUSHs(boolSV(do_seek(gv, offset, whence)));
2077 Off_t sought = do_sysseek(gv, offset, whence);
2079 PUSHs(&PL_sv_undef);
2082 #if LSEEKSIZE > IVSIZE
2087 : newSVpvn(zero_but_true, ZBTLEN);
2088 PUSHs(sv_2mortal(sv));
2097 /* There seems to be no consensus on the length type of truncate()
2098 * and ftruncate(), both off_t and size_t have supporters. In
2099 * general one would think that when using large files, off_t is
2100 * at least as wide as size_t, so using an off_t should be okay. */
2101 /* XXX Configure probe for the length type of *truncate() needed XXX */
2104 #if Off_t_size > IVSIZE
2109 /* Checking for length < 0 is problematic as the type might or
2110 * might not be signed: if it is not, clever compilers will moan. */
2111 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2113 #if defined(HAS_TRUNCATE) || defined(HAS_CHSIZE) || defined(F_FREESP)
2119 if (PL_op->op_flags & OPf_SPECIAL) {
2120 tmpgv = gv_fetchsv(POPs, 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)
2149 if (SvTYPE(sv) == SVt_PVGV) {
2150 tmpgv = (GV*)sv; /* *main::FRED for example */
2151 goto do_ftruncate_gv;
2153 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2154 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2155 goto do_ftruncate_gv;
2157 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2158 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2159 goto do_ftruncate_io;
2162 name = SvPV(sv, n_a);
2163 TAINT_PROPER("truncate");
2165 if (truncate(name, len) < 0)
2171 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2174 if (my_chsize(tmpfd, len) < 0)
2176 PerlLIO_close(tmpfd);
2185 SETERRNO(EBADF,RMS_IFI);
2189 DIE(aTHX_ "truncate not implemented");
2202 unsigned int func = POPu;
2203 int optype = PL_op->op_type;
2207 IO *io = gv ? GvIOn(gv) : 0;
2209 if (!io || !argsv || !IoIFP(io)) {
2210 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2211 report_evil_fh(gv, io, PL_op->op_type);
2212 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2216 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2219 s = SvPV_force(argsv, len);
2220 need = IOCPARM_LEN(func);
2222 s = Sv_Grow(argsv, need + 1);
2223 SvCUR_set(argsv, need);
2226 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2229 retval = SvIV(argsv);
2230 s = INT2PTR(char*,retval); /* ouch */
2233 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2235 if (optype == OP_IOCTL)
2237 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2239 DIE(aTHX_ "ioctl is not implemented");
2243 DIE(aTHX_ "fcntl is not implemented");
2245 #if defined(OS2) && defined(__EMX__)
2246 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2248 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2252 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2254 if (s[SvCUR(argsv)] != 17)
2255 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2257 s[SvCUR(argsv)] = 0; /* put our null back */
2258 SvSETMAGIC(argsv); /* Assume it has changed */
2267 PUSHp(zero_but_true, ZBTLEN);
2288 if (gv && (io = GvIO(gv)))
2295 (void)PerlIO_flush(fp);
2296 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2299 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2300 report_evil_fh(gv, io, PL_op->op_type);
2302 SETERRNO(EBADF,RMS_IFI);
2307 DIE(aTHX_ PL_no_func, "flock()");
2319 int protocol = POPi;
2325 io = gv ? GvIOn(gv) : NULL;
2328 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2329 report_evil_fh(gv, io, PL_op->op_type);
2331 do_close(gv, FALSE);
2332 SETERRNO(EBADF,LIB_INVARG);
2337 do_close(gv, FALSE);
2339 TAINT_PROPER("socket");
2340 fd = PerlSock_socket(domain, type, protocol);
2343 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2344 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2345 IoTYPE(io) = IoTYPE_SOCKET;
2346 if (!IoIFP(io) || !IoOFP(io)) {
2347 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2348 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2349 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2352 #if defined(HAS_FCNTL) && defined(F_SETFD)
2353 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2357 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2362 DIE(aTHX_ PL_no_sock_func, "socket");
2368 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2374 int protocol = POPi;
2381 io1 = gv1 ? GvIOn(gv1) : NULL;
2382 io2 = gv2 ? GvIOn(gv2) : NULL;
2383 if (!gv1 || !gv2 || !io1 || !io2) {
2384 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2386 report_evil_fh(gv1, io1, PL_op->op_type);
2388 report_evil_fh(gv1, io2, PL_op->op_type);
2391 do_close(gv1, FALSE);
2393 do_close(gv2, FALSE);
2398 do_close(gv1, FALSE);
2400 do_close(gv2, FALSE);
2402 TAINT_PROPER("socketpair");
2403 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2405 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2406 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2407 IoTYPE(io1) = IoTYPE_SOCKET;
2408 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2409 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2410 IoTYPE(io2) = IoTYPE_SOCKET;
2411 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2412 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2413 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2414 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2415 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2416 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2417 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2420 #if defined(HAS_FCNTL) && defined(F_SETFD)
2421 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2422 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2427 DIE(aTHX_ PL_no_sock_func, "socketpair");
2435 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2436 extern void GETPRIVMODE();
2437 extern void GETUSERMODE();
2442 register IO *io = GvIOn(gv);
2446 int mpeprivmode = 0;
2449 if (!io || !IoIFP(io))
2452 addr = SvPV(addrsv, len);
2453 TAINT_PROPER("bind");
2454 #ifdef MPE /* Deal with MPE bind() peculiarities */
2455 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2456 /* The address *MUST* stupidly be zero. */
2457 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2458 /* PRIV mode is required to bind() to ports < 1024. */
2459 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2460 ((struct sockaddr_in *)addr)->sin_port > 0) {
2461 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2466 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2467 (struct sockaddr *)addr, len) >= 0)
2470 #ifdef MPE /* Switch back to USER mode */
2481 if (ckWARN(WARN_CLOSED))
2482 report_evil_fh(gv, io, PL_op->op_type);
2483 SETERRNO(EBADF,SS_IVCHAN);
2486 DIE(aTHX_ PL_no_sock_func, "bind");
2497 register IO *io = GvIOn(gv);
2500 if (!io || !IoIFP(io))
2503 addr = SvPV(addrsv, len);
2504 TAINT_PROPER("connect");
2505 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2511 if (ckWARN(WARN_CLOSED))
2512 report_evil_fh(gv, io, PL_op->op_type);
2513 SETERRNO(EBADF,SS_IVCHAN);
2516 DIE(aTHX_ PL_no_sock_func, "connect");
2526 register IO *io = gv ? GvIOn(gv) : NULL;
2528 if (!gv || !io || !IoIFP(io))
2531 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2537 if (ckWARN(WARN_CLOSED))
2538 report_evil_fh(gv, io, PL_op->op_type);
2539 SETERRNO(EBADF,SS_IVCHAN);
2542 DIE(aTHX_ PL_no_sock_func, "listen");
2554 char namebuf[MAXPATHLEN];
2555 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2556 Sock_size_t len = sizeof (struct sockaddr_in);
2558 Sock_size_t len = sizeof namebuf;
2571 if (!gstio || !IoIFP(gstio))
2575 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2579 do_close(ngv, FALSE);
2580 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2581 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2582 IoTYPE(nstio) = IoTYPE_SOCKET;
2583 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2584 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2585 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2586 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2589 #if defined(HAS_FCNTL) && defined(F_SETFD)
2590 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2594 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2595 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2597 #ifdef __SCO_VERSION__
2598 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2601 PUSHp(namebuf, len);
2605 if (ckWARN(WARN_CLOSED))
2606 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2607 SETERRNO(EBADF,SS_IVCHAN);
2613 DIE(aTHX_ PL_no_sock_func, "accept");
2623 register IO *io = GvIOn(gv);
2625 if (!io || !IoIFP(io))
2628 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2632 if (ckWARN(WARN_CLOSED))
2633 report_evil_fh(gv, io, PL_op->op_type);
2634 SETERRNO(EBADF,SS_IVCHAN);
2637 DIE(aTHX_ PL_no_sock_func, "shutdown");
2644 return pp_ssockopt();
2646 DIE(aTHX_ PL_no_sock_func, "getsockopt");
2654 int optype = PL_op->op_type;
2657 unsigned int optname;
2663 if (optype == OP_GSOCKOPT)
2664 sv = sv_2mortal(NEWSV(22, 257));
2667 optname = (unsigned int) POPi;
2668 lvl = (unsigned int) POPi;
2672 if (!io || !IoIFP(io))
2675 fd = PerlIO_fileno(IoIFP(io));
2679 (void)SvPOK_only(sv);
2683 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2698 aint = (int)SvIV(sv);
2702 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2711 if (ckWARN(WARN_CLOSED))
2712 report_evil_fh(gv, io, optype);
2713 SETERRNO(EBADF,SS_IVCHAN);
2718 DIE(aTHX_ PL_no_sock_func, "setsockopt");
2725 return pp_getpeername();
2727 DIE(aTHX_ PL_no_sock_func, "getsockname");
2735 int optype = PL_op->op_type;
2739 register IO *io = GvIOn(gv);
2742 if (!io || !IoIFP(io))
2745 sv = sv_2mortal(NEWSV(22, 257));
2746 (void)SvPOK_only(sv);
2750 fd = PerlIO_fileno(IoIFP(io));
2752 case OP_GETSOCKNAME:
2753 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2756 case OP_GETPEERNAME:
2757 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2759 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2761 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";
2762 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2763 if (((struct sockaddr *)SvPVX(sv))->sa_family == AF_INET &&
2764 !memcmp((char *)SvPVX(sv) + sizeof(u_short), nowhere,
2765 sizeof(u_short) + sizeof(struct in_addr))) {
2772 #ifdef BOGUS_GETNAME_RETURN
2773 /* Interactive Unix, getpeername() and getsockname()
2774 does not return valid namelen */
2775 if (len == BOGUS_GETNAME_RETURN)
2776 len = sizeof(struct sockaddr);
2784 if (ckWARN(WARN_CLOSED))
2785 report_evil_fh(gv, io, optype);
2786 SETERRNO(EBADF,SS_IVCHAN);
2791 DIE(aTHX_ PL_no_sock_func, "getpeername");
2810 if (PL_op->op_flags & OPf_REF) {
2812 if (PL_op->op_type == OP_LSTAT) {
2813 if (gv != PL_defgv) {
2814 if (ckWARN(WARN_IO))
2815 Perl_warner(aTHX_ packWARN(WARN_IO),
2816 "lstat() on filehandle %s", GvENAME(gv));
2817 } else if (PL_laststype != OP_LSTAT)
2818 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2822 if (gv != PL_defgv) {
2823 PL_laststype = OP_STAT;
2825 sv_setpv(PL_statname, "");
2826 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2827 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2829 if (PL_laststatval < 0) {
2830 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2831 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2837 if (SvTYPE(sv) == SVt_PVGV) {
2841 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2843 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2844 Perl_warner(aTHX_ packWARN(WARN_IO),
2845 "lstat() on filehandle %s", GvENAME(gv));
2848 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);
2854 PL_laststatval = PerlLIO_stat(SvPV(PL_statname, n_a), &PL_statcache);
2855 if (PL_laststatval < 0) {
2856 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
2857 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2863 if (gimme != G_ARRAY) {
2864 if (gimme != G_VOID)
2865 XPUSHs(boolSV(max));
2871 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2872 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2873 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2874 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2875 #if Uid_t_size > IVSIZE
2876 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2878 # if Uid_t_sign <= 0
2879 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2881 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2884 #if Gid_t_size > IVSIZE
2885 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2887 # if Gid_t_sign <= 0
2888 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2890 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2893 #ifdef USE_STAT_RDEV
2894 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2896 PUSHs(sv_2mortal(newSVpvn("", 0)));
2898 #if Off_t_size > IVSIZE
2899 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2901 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2904 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2905 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2906 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2908 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2909 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2910 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2912 #ifdef USE_STAT_BLOCKS
2913 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2914 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2916 PUSHs(sv_2mortal(newSVpvn("", 0)));
2917 PUSHs(sv_2mortal(newSVpvn("", 0)));
2923 /* This macro is used by the stacked filetest operators :
2924 * if the previous filetest failed, short-circuit and pass its value.
2925 * Else, discard it from the stack and continue. --rgs
2927 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2928 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2929 else { (void)POPs; PUTBACK; } \
2936 STACKED_FTEST_CHECK;
2937 #if defined(HAS_ACCESS) && defined(R_OK)
2938 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2940 result = access(POPpx, R_OK);
2955 if (cando(S_IRUSR, 0, &PL_statcache))
2964 STACKED_FTEST_CHECK;
2965 #if defined(HAS_ACCESS) && defined(W_OK)
2966 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2968 result = access(POPpx, W_OK);
2983 if (cando(S_IWUSR, 0, &PL_statcache))
2992 STACKED_FTEST_CHECK;
2993 #if defined(HAS_ACCESS) && defined(X_OK)
2994 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2996 result = access(POPpx, X_OK);
3011 if (cando(S_IXUSR, 0, &PL_statcache))
3020 STACKED_FTEST_CHECK;
3021 #ifdef PERL_EFF_ACCESS_R_OK
3022 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3024 result = PERL_EFF_ACCESS_R_OK(POPpx);
3039 if (cando(S_IRUSR, 1, &PL_statcache))
3048 STACKED_FTEST_CHECK;
3049 #ifdef PERL_EFF_ACCESS_W_OK
3050 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3052 result = PERL_EFF_ACCESS_W_OK(POPpx);
3067 if (cando(S_IWUSR, 1, &PL_statcache))
3076 STACKED_FTEST_CHECK;
3077 #ifdef PERL_EFF_ACCESS_X_OK
3078 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3080 result = PERL_EFF_ACCESS_X_OK(POPpx);
3095 if (cando(S_IXUSR, 1, &PL_statcache))
3104 STACKED_FTEST_CHECK;
3114 return pp_ftrowned();
3121 STACKED_FTEST_CHECK;
3126 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3136 STACKED_FTEST_CHECK;
3141 if (PL_statcache.st_size == 0)
3150 STACKED_FTEST_CHECK;
3155 #if Off_t_size > IVSIZE
3156 PUSHn(PL_statcache.st_size);
3158 PUSHi(PL_statcache.st_size);
3167 STACKED_FTEST_CHECK;
3172 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3180 STACKED_FTEST_CHECK;
3185 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3193 STACKED_FTEST_CHECK;
3198 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3206 STACKED_FTEST_CHECK;
3211 if (S_ISSOCK(PL_statcache.st_mode))
3220 STACKED_FTEST_CHECK;
3225 if (S_ISCHR(PL_statcache.st_mode))
3234 STACKED_FTEST_CHECK;
3239 if (S_ISBLK(PL_statcache.st_mode))
3248 STACKED_FTEST_CHECK;
3253 if (S_ISREG(PL_statcache.st_mode))
3262 STACKED_FTEST_CHECK;
3267 if (S_ISDIR(PL_statcache.st_mode))
3276 STACKED_FTEST_CHECK;
3281 if (S_ISFIFO(PL_statcache.st_mode))
3288 I32 result = my_lstat();
3292 if (S_ISLNK(PL_statcache.st_mode))
3302 STACKED_FTEST_CHECK;
3307 if (PL_statcache.st_mode & S_ISUID)
3318 STACKED_FTEST_CHECK;
3323 if (PL_statcache.st_mode & S_ISGID)
3334 STACKED_FTEST_CHECK;
3339 if (PL_statcache.st_mode & S_ISVTX)
3352 STACKED_FTEST_CHECK;
3354 if (PL_op->op_flags & OPf_REF)
3356 else if (isGV(TOPs))
3358 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3359 gv = (GV*)SvRV(POPs);
3361 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3363 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3364 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3365 else if (tmpsv && SvOK(tmpsv)) {
3367 char *tmps = SvPV(tmpsv, n_a);
3375 if (PerlLIO_isatty(fd))
3380 #if defined(atarist) /* this will work with atariST. Configure will
3381 make guesses for other systems. */
3382 # define FILE_base(f) ((f)->_base)
3383 # define FILE_ptr(f) ((f)->_ptr)
3384 # define FILE_cnt(f) ((f)->_cnt)
3385 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3395 register STDCHAR *s;
3402 STACKED_FTEST_CHECK;
3404 if (PL_op->op_flags & OPf_REF)
3406 else if (isGV(TOPs))
3408 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3409 gv = (GV*)SvRV(POPs);
3415 if (gv == PL_defgv) {
3417 io = GvIO(PL_statgv);
3420 goto really_filename;
3425 PL_laststatval = -1;
3426 sv_setpv(PL_statname, "");
3427 io = GvIO(PL_statgv);
3429 if (io && IoIFP(io)) {
3430 if (! PerlIO_has_base(IoIFP(io)))
3431 DIE(aTHX_ "-T and -B not implemented on filehandles");
3432 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3433 if (PL_laststatval < 0)
3435 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3436 if (PL_op->op_type == OP_FTTEXT)
3441 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3442 i = PerlIO_getc(IoIFP(io));
3444 (void)PerlIO_ungetc(IoIFP(io),i);
3446 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3448 len = PerlIO_get_bufsiz(IoIFP(io));
3449 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3450 /* sfio can have large buffers - limit to 512 */
3455 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3457 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3459 SETERRNO(EBADF,RMS_IFI);
3467 PL_laststatval = -1;
3468 PL_laststype = OP_STAT;
3469 sv_setpv(PL_statname, SvPV(sv, n_a));
3470 if (!(fp = PerlIO_open(SvPVX(PL_statname), "r"))) {
3471 if (ckWARN(WARN_NEWLINE) && strchr(SvPV(PL_statname, n_a), '\n'))
3472 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3475 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3476 if (PL_laststatval < 0) {
3477 (void)PerlIO_close(fp);
3480 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3481 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3482 (void)PerlIO_close(fp);
3484 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3485 RETPUSHNO; /* special case NFS directories */
3486 RETPUSHYES; /* null file is anything */
3491 /* now scan s to look for textiness */
3492 /* XXX ASCII dependent code */
3494 #if defined(DOSISH) || defined(USEMYBINMODE)
3495 /* ignore trailing ^Z on short files */
3496 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3500 for (i = 0; i < len; i++, s++) {
3501 if (!*s) { /* null never allowed in text */
3506 else if (!(isPRINT(*s) || isSPACE(*s)))
3509 else if (*s & 128) {
3511 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3514 /* utf8 characters don't count as odd */
3515 if (UTF8_IS_START(*s)) {
3516 int ulen = UTF8SKIP(s);
3517 if (ulen < len - i) {
3519 for (j = 1; j < ulen; j++) {
3520 if (!UTF8_IS_CONTINUATION(s[j]))
3523 --ulen; /* loop does extra increment */
3533 *s != '\n' && *s != '\r' && *s != '\b' &&
3534 *s != '\t' && *s != '\f' && *s != 27)
3539 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3564 if( !tmps || !*tmps ) {
3565 if ( (svp = hv_fetch(GvHVn(PL_envgv), "HOME", 4, FALSE))
3566 || (svp = hv_fetch(GvHVn(PL_envgv), "LOGDIR", 6, FALSE))
3568 || (svp = hv_fetch(GvHVn(PL_envgv), "SYS$LOGIN", 9, FALSE))
3573 deprecate("chdir('') or chdir(undef) as chdir()");
3574 tmps = SvPV(*svp, n_a);
3578 TAINT_PROPER("chdir");
3583 TAINT_PROPER("chdir");
3584 PUSHi( PerlDir_chdir(tmps) >= 0 );
3586 /* Clear the DEFAULT element of ENV so we'll get the new value
3588 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3596 dSP; dMARK; dTARGET;
3597 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3603 DIE(aTHX_ PL_no_func, "chown");
3613 TAINT_PROPER("chroot");
3614 PUSHi( chroot(tmps) >= 0 );
3617 DIE(aTHX_ PL_no_func, "chroot");
3623 dSP; dMARK; dTARGET;
3625 value = (I32)apply(PL_op->op_type, MARK, SP);
3633 dSP; dMARK; dTARGET;
3635 value = (I32)apply(PL_op->op_type, MARK, SP);
3643 dSP; dMARK; dTARGET;
3645 value = (I32)apply(PL_op->op_type, MARK, SP);
3657 char *tmps2 = POPpx;
3658 char *tmps = SvPV(TOPs, n_a);
3659 TAINT_PROPER("rename");
3661 anum = PerlLIO_rename(tmps, tmps2);
3663 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3664 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3667 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3668 (void)UNLINK(tmps2);
3669 if (!(anum = link(tmps, tmps2)))
3670 anum = UNLINK(tmps);
3683 char *tmps2 = POPpx;
3684 char *tmps = SvPV(TOPs, n_a);
3685 TAINT_PROPER("link");
3686 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3689 DIE(aTHX_ PL_no_func, "link");
3698 char *tmps2 = POPpx;
3699 char *tmps = SvPV(TOPs, n_a);
3700 TAINT_PROPER("symlink");
3701 SETi( symlink(tmps, tmps2) >= 0 );
3704 DIE(aTHX_ PL_no_func, "symlink");
3714 char buf[MAXPATHLEN];
3718 #ifndef INCOMPLETE_TAINTS
3722 len = readlink(tmps, buf, sizeof(buf) - 1);
3730 RETSETUNDEF; /* just pretend it's a normal file */
3734 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3736 S_dooneliner(pTHX_ char *cmd, char *filename)
3738 char *save_filename = filename;
3744 New(666, cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3745 strcpy(cmdline, cmd);
3746 strcat(cmdline, " ");
3747 for (s = cmdline + strlen(cmdline); *filename; ) {
3752 myfp = PerlProc_popen(cmdline, "r");
3756 SV *tmpsv = sv_newmortal();
3757 /* Need to save/restore 'PL_rs' ?? */
3758 s = sv_gets(tmpsv, myfp, 0);
3759 (void)PerlProc_pclose(myfp);
3763 #ifdef HAS_SYS_ERRLIST
3768 /* you don't see this */
3770 #ifdef HAS_SYS_ERRLIST
3778 if (instr(s, errmsg)) {
3785 #define EACCES EPERM
3787 if (instr(s, "cannot make"))
3788 SETERRNO(EEXIST,RMS_FEX);
3789 else if (instr(s, "existing file"))
3790 SETERRNO(EEXIST,RMS_FEX);
3791 else if (instr(s, "ile exists"))
3792 SETERRNO(EEXIST,RMS_FEX);
3793 else if (instr(s, "non-exist"))
3794 SETERRNO(ENOENT,RMS_FNF);
3795 else if (instr(s, "does not exist"))
3796 SETERRNO(ENOENT,RMS_FNF);
3797 else if (instr(s, "not empty"))
3798 SETERRNO(EBUSY,SS_DEVOFFLINE);
3799 else if (instr(s, "cannot access"))
3800 SETERRNO(EACCES,RMS_PRV);
3802 SETERRNO(EPERM,RMS_PRV);
3805 else { /* some mkdirs return no failure indication */
3806 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3807 if (PL_op->op_type == OP_RMDIR)
3812 SETERRNO(EACCES,RMS_PRV); /* a guess */
3821 /* This macro removes trailing slashes from a directory name.
3822 * Different operating and file systems take differently to
3823 * trailing slashes. According to POSIX 1003.1 1996 Edition
3824 * any number of trailing slashes should be allowed.
3825 * Thusly we snip them away so that even non-conforming
3826 * systems are happy.
3827 * We should probably do this "filtering" for all
3828 * the functions that expect (potentially) directory names:
3829 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3830 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3832 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV(TOPs, (len)); \
3833 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3836 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3837 (tmps) = savepvn((tmps), (len)); \
3857 TRIMSLASHES(tmps,len,copy);
3859 TAINT_PROPER("mkdir");
3861 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3863 SETi( dooneliner("mkdir", tmps) );
3864 oldumask = PerlLIO_umask(0);
3865 PerlLIO_umask(oldumask);
3866 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3880 TRIMSLASHES(tmps,len,copy);
3881 TAINT_PROPER("rmdir");
3883 SETi( PerlDir_rmdir(tmps) >= 0 );
3885 SETi( dooneliner("rmdir", tmps) );
3892 /* Directory calls. */
3896 #if defined(Direntry_t) && defined(HAS_READDIR)
3899 char *dirname = POPpx;
3901 register IO *io = GvIOn(gv);
3907 PerlDir_close(IoDIRP(io));
3908 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3914 SETERRNO(EBADF,RMS_DIR);
3917 DIE(aTHX_ PL_no_dir_func, "opendir");
3923 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3924 DIE(aTHX_ PL_no_dir_func, "readdir");
3926 #if !defined(I_DIRENT) && !defined(VMS)
3927 Direntry_t *readdir (DIR *);
3933 GV *gv = (GV *)POPs;
3934 register Direntry_t *dp;
3935 register IO *io = GvIOn(gv);
3937 if (!io || !IoDIRP(io))
3941 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3945 sv = newSVpvn(dp->d_name, dp->d_namlen);
3947 sv = newSVpv(dp->d_name, 0);
3949 #ifndef INCOMPLETE_TAINTS
3950 if (!(IoFLAGS(io) & IOf_UNTAINT))
3953 XPUSHs(sv_2mortal(sv));
3955 while (gimme == G_ARRAY);
3957 if (!dp && gimme != G_ARRAY)
3964 SETERRNO(EBADF,RMS_ISI);
3965 if (GIMME == G_ARRAY)
3974 #if defined(HAS_TELLDIR) || defined(telldir)
3976 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3977 /* XXX netbsd still seemed to.
3978 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3979 --JHI 1999-Feb-02 */
3980 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3981 long telldir (DIR *);
3984 register IO *io = GvIOn(gv);
3986 if (!io || !IoDIRP(io))
3989 PUSHi( PerlDir_tell(IoDIRP(io)) );
3993 SETERRNO(EBADF,RMS_ISI);
3996 DIE(aTHX_ PL_no_dir_func, "telldir");
4002 #if defined(HAS_SEEKDIR) || defined(seekdir)
4006 register IO *io = GvIOn(gv);
4008 if (!io || !IoDIRP(io))
4011 (void)PerlDir_seek(IoDIRP(io), along);
4016 SETERRNO(EBADF,RMS_ISI);
4019 DIE(aTHX_ PL_no_dir_func, "seekdir");
4025 #if defined(HAS_REWINDDIR) || defined(rewinddir)
4028 register IO *io = GvIOn(gv);
4030 if (!io || !IoDIRP(io))
4033 (void)PerlDir_rewind(IoDIRP(io));
4037 SETERRNO(EBADF,RMS_ISI);
4040 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4046 #if defined(Direntry_t) && defined(HAS_READDIR)
4049 register IO *io = GvIOn(gv);
4051 if (!io || !IoDIRP(io))
4054 #ifdef VOID_CLOSEDIR
4055 PerlDir_close(IoDIRP(io));
4057 if (PerlDir_close(IoDIRP(io)) < 0) {
4058 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4067 SETERRNO(EBADF,RMS_IFI);
4070 DIE(aTHX_ PL_no_dir_func, "closedir");
4074 /* Process control. */
4084 PERL_FLUSHALL_FOR_CHILD;
4085 childpid = PerlProc_fork();
4090 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4091 SvREADONLY_off(GvSV(tmpgv));
4092 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4093 SvREADONLY_on(GvSV(tmpgv));
4095 #ifdef THREADS_HAVE_PIDS
4096 PL_ppid = (IV)getppid();
4098 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4103 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4108 PERL_FLUSHALL_FOR_CHILD;
4109 childpid = PerlProc_fork();
4115 DIE(aTHX_ PL_no_func, "fork");
4122 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4127 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4128 childpid = wait4pid(-1, &argflags, 0);
4130 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4135 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4136 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4137 STATUS_NATIVE_SET((childpid && childpid != -1) ? argflags : -1);
4139 STATUS_NATIVE_SET((childpid > 0) ? argflags : -1);
4144 DIE(aTHX_ PL_no_func, "wait");
4150 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4159 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4160 result = wait4pid(pid, &argflags, optype);
4162 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
4167 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4168 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4169 STATUS_NATIVE_SET((result && result != -1) ? argflags : -1);
4171 STATUS_NATIVE_SET((result > 0) ? argflags : -1);
4176 DIE(aTHX_ PL_no_func, "waitpid");
4182 dSP; dMARK; dORIGMARK; dTARGET;
4190 while (++MARK <= SP) {
4191 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4196 TAINT_PROPER("system");
4198 PERL_FLUSHALL_FOR_CHILD;
4199 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4204 if (PerlProc_pipe(pp) >= 0)
4206 while ((childpid = PerlProc_fork()) == -1) {
4207 if (errno != EAGAIN) {
4212 PerlLIO_close(pp[0]);
4213 PerlLIO_close(pp[1]);
4220 Sigsave_t ihand,qhand; /* place to save signals during system() */
4224 PerlLIO_close(pp[1]);
4226 rsignal_save(SIGINT, SIG_IGN, &ihand);
4227 rsignal_save(SIGQUIT, SIG_IGN, &qhand);
4230 result = wait4pid(childpid, &status, 0);
4231 } while (result == -1 && errno == EINTR);
4233 (void)rsignal_restore(SIGINT, &ihand);
4234 (void)rsignal_restore(SIGQUIT, &qhand);
4236 STATUS_NATIVE_SET(result == -1 ? -1 : status);
4237 do_execfree(); /* free any memory child malloced on fork */
4243 while (n < sizeof(int)) {
4244 n1 = PerlLIO_read(pp[0],
4245 (void*)(((char*)&errkid)+n),
4251 PerlLIO_close(pp[0]);
4252 if (n) { /* Error */
4253 if (n != sizeof(int))
4254 DIE(aTHX_ "panic: kid popen errno read");
4255 errno = errkid; /* Propagate errno from kid */
4256 STATUS_CURRENT = -1;
4259 PUSHi(STATUS_CURRENT);
4263 PerlLIO_close(pp[0]);
4264 #if defined(HAS_FCNTL) && defined(F_SETFD)
4265 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4268 if (PL_op->op_flags & OPf_STACKED) {
4269 SV *really = *++MARK;
4270 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4272 else if (SP - MARK != 1)
4273 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4275 value = (I32)do_exec3(SvPVx(sv_mortalcopy(*SP), n_a), pp[1], did_pipes);
4279 #else /* ! FORK or VMS or OS/2 */
4282 if (PL_op->op_flags & OPf_STACKED) {
4283 SV *really = *++MARK;
4284 # if defined(WIN32) || defined(OS2)
4285 value = (I32)do_aspawn(really, MARK, SP);
4287 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4290 else if (SP - MARK != 1) {
4291 # if defined(WIN32) || defined(OS2)
4292 value = (I32)do_aspawn(Nullsv, MARK, SP);
4294 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4298 value = (I32)do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4300 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4302 STATUS_NATIVE_SET(value);
4305 PUSHi(result ? value : STATUS_CURRENT);
4306 #endif /* !FORK or VMS */
4312 dSP; dMARK; dORIGMARK; dTARGET;
4318 while (++MARK <= SP) {
4319 (void)SvPV_nolen(*MARK); /* stringify for taint check */
4324 TAINT_PROPER("exec");
4326 PERL_FLUSHALL_FOR_CHILD;
4327 if (PL_op->op_flags & OPf_STACKED) {
4328 SV *really = *++MARK;
4329 value = (I32)do_aexec(really, MARK, SP);
4331 else if (SP - MARK != 1)
4333 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4337 (void ) do_aspawn(Nullsv, MARK, SP);
4341 value = (I32)do_aexec(Nullsv, MARK, SP);
4346 value = (I32)vms_do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4349 (void) do_spawn(SvPVx(sv_mortalcopy(*SP), n_a));
4352 value = (I32)do_exec(SvPVx(sv_mortalcopy(*SP), n_a));
4365 dSP; dMARK; dTARGET;
4367 value = (I32)apply(PL_op->op_type, MARK, SP);
4372 DIE(aTHX_ PL_no_func, "kill");
4380 # ifdef THREADS_HAVE_PIDS
4383 XPUSHi( getppid() );
4387 DIE(aTHX_ PL_no_func, "getppid");
4403 pgrp = (I32)BSD_GETPGRP(pid);
4405 if (pid != 0 && pid != PerlProc_getpid())
4406 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4412 DIE(aTHX_ PL_no_func, "getpgrp()");
4431 TAINT_PROPER("setpgrp");
4433 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4435 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4436 || (pid != 0 && pid != PerlProc_getpid()))
4438 DIE(aTHX_ "setpgrp can't take arguments");
4440 SETi( setpgrp() >= 0 );
4441 #endif /* USE_BSDPGRP */
4444 DIE(aTHX_ PL_no_func, "setpgrp()");
4450 #ifdef HAS_GETPRIORITY
4454 SETi( getpriority(which, who) );
4457 DIE(aTHX_ PL_no_func, "getpriority()");
4463 #ifdef HAS_SETPRIORITY
4468 TAINT_PROPER("setpriority");
4469 SETi( setpriority(which, who, niceval) >= 0 );
4472 DIE(aTHX_ PL_no_func, "setpriority()");
4482 XPUSHn( time(Null(Time_t*)) );
4484 XPUSHi( time(Null(Time_t*)) );
4495 (void)PerlProc_times(&PL_timesbuf);
4497 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4498 /* struct tms, though same data */
4502 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4503 if (GIMME == G_ARRAY) {
4504 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4505 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4506 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4512 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4514 if (GIMME == G_ARRAY) {
4515 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4516 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4517 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4521 DIE(aTHX_ "times not implemented");
4523 #endif /* HAS_TIMES */
4536 static char *dayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4537 static char *monname[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4538 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4544 when = (Time_t)SvNVx(POPs);
4546 when = (Time_t)SvIVx(POPs);
4549 if (PL_op->op_type == OP_LOCALTIME)
4550 tmbuf = localtime(&when);
4552 tmbuf = gmtime(&when);
4554 if (GIMME != G_ARRAY) {
4560 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4561 dayname[tmbuf->tm_wday],
4562 monname[tmbuf->tm_mon],
4567 tmbuf->tm_year + 1900);
4568 PUSHs(sv_2mortal(tsv));
4573 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4574 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4575 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4576 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4577 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4578 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4579 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4580 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4581 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4592 anum = alarm((unsigned int)anum);
4599 DIE(aTHX_ PL_no_func, "alarm");
4610 (void)time(&lasttime);
4615 PerlProc_sleep((unsigned int)duration);
4618 XPUSHi(when - lasttime);
4622 /* Shared memory. */
4636 return pp_shmwrite();
4641 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4642 dSP; dMARK; dTARGET;
4643 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4652 /* Message passing. */
4666 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4667 dSP; dMARK; dTARGET;
4668 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4679 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4680 dSP; dMARK; dTARGET;
4681 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4694 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4695 dSP; dMARK; dTARGET;
4696 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4703 DIE(aTHX_ "System V IPC is not implemented on this machine");
4709 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4710 dSP; dMARK; dTARGET;
4711 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4719 PUSHp(zero_but_true, ZBTLEN);
4729 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4730 dSP; dMARK; dTARGET;
4731 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4740 /* Get system info. */
4744 #ifdef HAS_GETHOSTBYNAME
4745 return pp_ghostent();
4747 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4753 #ifdef HAS_GETHOSTBYADDR
4754 return pp_ghostent();
4756 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4762 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4764 I32 which = PL_op->op_type;
4765 register char **elem;
4767 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4768 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4769 struct hostent *gethostbyname(Netdb_name_t);
4770 struct hostent *gethostent(void);
4772 struct hostent *hent;
4777 if (which == OP_GHBYNAME) {
4778 #ifdef HAS_GETHOSTBYNAME
4779 char* name = POPpbytex;
4780 hent = PerlSock_gethostbyname(name);
4782 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4785 else if (which == OP_GHBYADDR) {
4786 #ifdef HAS_GETHOSTBYADDR
4787 int addrtype = POPi;
4790 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4792 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4794 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4798 #ifdef HAS_GETHOSTENT
4799 hent = PerlSock_gethostent();
4801 DIE(aTHX_ PL_no_sock_func, "gethostent");
4804 #ifdef HOST_NOT_FOUND
4806 #ifdef USE_REENTRANT_API
4807 # ifdef USE_GETHOSTENT_ERRNO
4808 h_errno = PL_reentrant_buffer->_gethostent_errno;
4811 STATUS_NATIVE_SET(h_errno);
4815 if (GIMME != G_ARRAY) {
4816 PUSHs(sv = sv_newmortal());
4818 if (which == OP_GHBYNAME) {
4820 sv_setpvn(sv, hent->h_addr, hent->h_length);
4823 sv_setpv(sv, (char*)hent->h_name);
4829 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4830 sv_setpv(sv, (char*)hent->h_name);
4831 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4832 for (elem = hent->h_aliases; elem && *elem; elem++) {
4833 sv_catpv(sv, *elem);
4835 sv_catpvn(sv, " ", 1);
4837 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4838 sv_setiv(sv, (IV)hent->h_addrtype);
4839 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4840 len = hent->h_length;
4841 sv_setiv(sv, (IV)len);
4843 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4844 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4845 sv_setpvn(sv, *elem, len);
4848 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4850 sv_setpvn(sv, hent->h_addr, len);
4855 DIE(aTHX_ PL_no_sock_func, "gethostent");
4861 #ifdef HAS_GETNETBYNAME
4862 return pp_gnetent();
4864 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4870 #ifdef HAS_GETNETBYADDR
4871 return pp_gnetent();
4873 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4879 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4881 I32 which = PL_op->op_type;
4882 register char **elem;
4884 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4885 struct netent *getnetbyaddr(Netdb_net_t, int);
4886 struct netent *getnetbyname(Netdb_name_t);
4887 struct netent *getnetent(void);
4889 struct netent *nent;
4892 if (which == OP_GNBYNAME){
4893 #ifdef HAS_GETNETBYNAME
4894 char *name = POPpbytex;
4895 nent = PerlSock_getnetbyname(name);
4897 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4900 else if (which == OP_GNBYADDR) {
4901 #ifdef HAS_GETNETBYADDR
4902 int addrtype = POPi;
4903 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4904 nent = PerlSock_getnetbyaddr(addr, addrtype);
4906 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4910 #ifdef HAS_GETNETENT
4911 nent = PerlSock_getnetent();
4913 DIE(aTHX_ PL_no_sock_func, "getnetent");
4916 #ifdef HOST_NOT_FOUND
4918 #ifdef USE_REENTRANT_API
4919 # ifdef USE_GETNETENT_ERRNO
4920 h_errno = PL_reentrant_buffer->_getnetent_errno;
4923 STATUS_NATIVE_SET(h_errno);
4928 if (GIMME != G_ARRAY) {
4929 PUSHs(sv = sv_newmortal());
4931 if (which == OP_GNBYNAME)
4932 sv_setiv(sv, (IV)nent->n_net);
4934 sv_setpv(sv, nent->n_name);
4940 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4941 sv_setpv(sv, nent->n_name);
4942 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4943 for (elem = nent->n_aliases; elem && *elem; elem++) {
4944 sv_catpv(sv, *elem);
4946 sv_catpvn(sv, " ", 1);
4948 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4949 sv_setiv(sv, (IV)nent->n_addrtype);
4950 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4951 sv_setiv(sv, (IV)nent->n_net);
4956 DIE(aTHX_ PL_no_sock_func, "getnetent");
4962 #ifdef HAS_GETPROTOBYNAME
4963 return pp_gprotoent();
4965 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4971 #ifdef HAS_GETPROTOBYNUMBER
4972 return pp_gprotoent();
4974 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4980 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4982 I32 which = PL_op->op_type;
4983 register char **elem;
4985 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4986 struct protoent *getprotobyname(Netdb_name_t);
4987 struct protoent *getprotobynumber(int);
4988 struct protoent *getprotoent(void);
4990 struct protoent *pent;
4993 if (which == OP_GPBYNAME) {
4994 #ifdef HAS_GETPROTOBYNAME
4995 char* name = POPpbytex;
4996 pent = PerlSock_getprotobyname(name);
4998 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
5001 else if (which == OP_GPBYNUMBER) {
5002 #ifdef HAS_GETPROTOBYNUMBER
5004 pent = PerlSock_getprotobynumber(number);
5006 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
5010 #ifdef HAS_GETPROTOENT
5011 pent = PerlSock_getprotoent();
5013 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5017 if (GIMME != G_ARRAY) {
5018 PUSHs(sv = sv_newmortal());
5020 if (which == OP_GPBYNAME)
5021 sv_setiv(sv, (IV)pent->p_proto);
5023 sv_setpv(sv, pent->p_name);
5029 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5030 sv_setpv(sv, pent->p_name);
5031 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5032 for (elem = pent->p_aliases; elem && *elem; elem++) {
5033 sv_catpv(sv, *elem);
5035 sv_catpvn(sv, " ", 1);
5037 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5038 sv_setiv(sv, (IV)pent->p_proto);
5043 DIE(aTHX_ PL_no_sock_func, "getprotoent");
5049 #ifdef HAS_GETSERVBYNAME
5050 return pp_gservent();
5052 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5058 #ifdef HAS_GETSERVBYPORT
5059 return pp_gservent();
5061 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5067 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
5069 I32 which = PL_op->op_type;
5070 register char **elem;
5072 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
5073 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
5074 struct servent *getservbyport(int, Netdb_name_t);
5075 struct servent *getservent(void);
5077 struct servent *sent;
5080 if (which == OP_GSBYNAME) {
5081 #ifdef HAS_GETSERVBYNAME
5082 char *proto = POPpbytex;
5083 char *name = POPpbytex;
5085 if (proto && !*proto)
5088 sent = PerlSock_getservbyname(name, proto);
5090 DIE(aTHX_ PL_no_sock_func, "getservbyname");
5093 else if (which == OP_GSBYPORT) {
5094 #ifdef HAS_GETSERVBYPORT
5095 char *proto = POPpbytex;
5096 unsigned short port = (unsigned short)POPu;
5098 if (proto && !*proto)
5102 port = PerlSock_htons(port);
5104 sent = PerlSock_getservbyport(port, proto);
5106 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5110 #ifdef HAS_GETSERVENT
5111 sent = PerlSock_getservent();
5113 DIE(aTHX_ PL_no_sock_func, "getservent");
5117 if (GIMME != G_ARRAY) {
5118 PUSHs(sv = sv_newmortal());
5120 if (which == OP_GSBYNAME) {
5122 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5124 sv_setiv(sv, (IV)(sent->s_port));
5128 sv_setpv(sv, sent->s_name);
5134 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5135 sv_setpv(sv, sent->s_name);
5136 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5137 for (elem = sent->s_aliases; elem && *elem; elem++) {
5138 sv_catpv(sv, *elem);
5140 sv_catpvn(sv, " ", 1);
5142 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5144 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5146 sv_setiv(sv, (IV)(sent->s_port));
5148 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5149 sv_setpv(sv, sent->s_proto);
5154 DIE(aTHX_ PL_no_sock_func, "getservent");
5160 #ifdef HAS_SETHOSTENT
5162 PerlSock_sethostent(TOPi);
5165 DIE(aTHX_ PL_no_sock_func, "sethostent");
5171 #ifdef HAS_SETNETENT
5173 PerlSock_setnetent(TOPi);
5176 DIE(aTHX_ PL_no_sock_func, "setnetent");
5182 #ifdef HAS_SETPROTOENT
5184 PerlSock_setprotoent(TOPi);
5187 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5193 #ifdef HAS_SETSERVENT
5195 PerlSock_setservent(TOPi);
5198 DIE(aTHX_ PL_no_sock_func, "setservent");
5204 #ifdef HAS_ENDHOSTENT
5206 PerlSock_endhostent();
5210 DIE(aTHX_ PL_no_sock_func, "endhostent");
5216 #ifdef HAS_ENDNETENT
5218 PerlSock_endnetent();
5222 DIE(aTHX_ PL_no_sock_func, "endnetent");
5228 #ifdef HAS_ENDPROTOENT
5230 PerlSock_endprotoent();
5234 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5240 #ifdef HAS_ENDSERVENT
5242 PerlSock_endservent();
5246 DIE(aTHX_ PL_no_sock_func, "endservent");
5255 DIE(aTHX_ PL_no_func, "getpwnam");
5264 DIE(aTHX_ PL_no_func, "getpwuid");
5272 I32 which = PL_op->op_type;
5275 struct passwd *pwent = NULL;
5277 * We currently support only the SysV getsp* shadow password interface.
5278 * The interface is declared in <shadow.h> and often one needs to link
5279 * with -lsecurity or some such.
5280 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5283 * AIX getpwnam() is clever enough to return the encrypted password
5284 * only if the caller (euid?) is root.
5286 * There are at least three other shadow password APIs. Many platforms
5287 * seem to contain more than one interface for accessing the shadow
5288 * password databases, possibly for compatibility reasons.
5289 * The getsp*() is by far he simplest one, the other two interfaces
5290 * are much more complicated, but also very similar to each other.
5295 * struct pr_passwd *getprpw*();
5296 * The password is in
5297 * char getprpw*(...).ufld.fd_encrypt[]
5298 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5303 * struct es_passwd *getespw*();
5304 * The password is in
5305 * char *(getespw*(...).ufld.fd_encrypt)
5306 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5309 * struct userpw *getuserpw();
5310 * The password is in
5311 * char *(getuserpw(...)).spw_upw_passwd
5312 * (but the de facto standard getpwnam() should work okay)
5314 * Mention I_PROT here so that Configure probes for it.
5316 * In HP-UX for getprpw*() the manual page claims that one should include
5317 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5318 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5319 * and pp_sys.c already includes <shadow.h> if there is such.
5321 * Note that <sys/security.h> is already probed for, but currently
5322 * it is only included in special cases.
5324 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5325 * be preferred interface, even though also the getprpw*() interface
5326 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5327 * One also needs to call set_auth_parameters() in main() before
5328 * doing anything else, whether one is using getespw*() or getprpw*().
5330 * Note that accessing the shadow databases can be magnitudes
5331 * slower than accessing the standard databases.
5336 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5337 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5338 * the pw_comment is left uninitialized. */
5339 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5345 char* name = POPpbytex;
5346 pwent = getpwnam(name);
5352 pwent = getpwuid(uid);
5356 # ifdef HAS_GETPWENT
5358 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5359 if (pwent) pwent = getpwnam(pwent->pw_name);
5362 DIE(aTHX_ PL_no_func, "getpwent");
5368 if (GIMME != G_ARRAY) {
5369 PUSHs(sv = sv_newmortal());
5371 if (which == OP_GPWNAM)
5372 # if Uid_t_sign <= 0
5373 sv_setiv(sv, (IV)pwent->pw_uid);
5375 sv_setuv(sv, (UV)pwent->pw_uid);
5378 sv_setpv(sv, pwent->pw_name);
5384 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5385 sv_setpv(sv, pwent->pw_name);
5387 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5389 /* If we have getspnam(), we try to dig up the shadow
5390 * password. If we are underprivileged, the shadow
5391 * interface will set the errno to EACCES or similar,
5392 * and return a null pointer. If this happens, we will
5393 * use the dummy password (usually "*" or "x") from the
5394 * standard password database.
5396 * In theory we could skip the shadow call completely
5397 * if euid != 0 but in practice we cannot know which
5398 * security measures are guarding the shadow databases
5399 * on a random platform.
5401 * Resist the urge to use additional shadow interfaces.
5402 * Divert the urge to writing an extension instead.
5405 /* Some AIX setups falsely(?) detect some getspnam(), which
5406 * has a different API than the Solaris/IRIX one. */
5407 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5409 struct spwd *spwent;
5410 int saverrno; /* Save and restore errno so that
5411 * underprivileged attempts seem
5412 * to have never made the unsccessful
5413 * attempt to retrieve the shadow password. */
5416 spwent = getspnam(pwent->pw_name);
5418 if (spwent && spwent->sp_pwdp)
5419 sv_setpv(sv, spwent->sp_pwdp);
5423 if (!SvPOK(sv)) /* Use the standard password, then. */
5424 sv_setpv(sv, pwent->pw_passwd);
5427 # ifndef INCOMPLETE_TAINTS
5428 /* passwd is tainted because user himself can diddle with it.
5429 * admittedly not much and in a very limited way, but nevertheless. */
5433 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5434 # if Uid_t_sign <= 0
5435 sv_setiv(sv, (IV)pwent->pw_uid);
5437 sv_setuv(sv, (UV)pwent->pw_uid);
5440 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5441 # if Uid_t_sign <= 0
5442 sv_setiv(sv, (IV)pwent->pw_gid);
5444 sv_setuv(sv, (UV)pwent->pw_gid);
5446 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5447 * because of the poor interface of the Perl getpw*(),
5448 * not because there's some standard/convention saying so.
5449 * A better interface would have been to return a hash,
5450 * but we are accursed by our history, alas. --jhi. */
5451 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5453 sv_setiv(sv, (IV)pwent->pw_change);
5456 sv_setiv(sv, (IV)pwent->pw_quota);
5459 sv_setpv(sv, pwent->pw_age);
5464 /* pw_class and pw_comment are mutually exclusive--.
5465 * see the above note for pw_change, pw_quota, and pw_age. */
5466 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5468 sv_setpv(sv, pwent->pw_class);
5471 sv_setpv(sv, pwent->pw_comment);
5475 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5477 sv_setpv(sv, pwent->pw_gecos);
5479 # ifndef INCOMPLETE_TAINTS
5480 /* pw_gecos is tainted because user himself can diddle with it. */
5484 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5485 sv_setpv(sv, pwent->pw_dir);
5487 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5488 sv_setpv(sv, pwent->pw_shell);
5489 # ifndef INCOMPLETE_TAINTS
5490 /* pw_shell is tainted because user himself can diddle with it. */
5495 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5496 sv_setiv(sv, (IV)pwent->pw_expire);
5501 DIE(aTHX_ PL_no_func, "getpwent");
5507 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5512 DIE(aTHX_ PL_no_func, "setpwent");
5518 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5523 DIE(aTHX_ PL_no_func, "endpwent");
5532 DIE(aTHX_ PL_no_func, "getgrnam");
5541 DIE(aTHX_ PL_no_func, "getgrgid");
5549 I32 which = PL_op->op_type;
5550 register char **elem;
5552 struct group *grent;
5555 if (which == OP_GGRNAM) {
5556 char* name = POPpbytex;
5557 grent = (struct group *)getgrnam(name);
5559 else if (which == OP_GGRGID) {
5561 grent = (struct group *)getgrgid(gid);
5565 grent = (struct group *)getgrent();
5567 DIE(aTHX_ PL_no_func, "getgrent");
5571 if (GIMME != G_ARRAY) {
5572 PUSHs(sv = sv_newmortal());
5574 if (which == OP_GGRNAM)
5575 sv_setiv(sv, (IV)grent->gr_gid);
5577 sv_setpv(sv, grent->gr_name);
5583 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5584 sv_setpv(sv, grent->gr_name);
5586 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5588 sv_setpv(sv, grent->gr_passwd);
5591 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5592 sv_setiv(sv, (IV)grent->gr_gid);
5594 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5595 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5596 /* In UNICOS/mk (_CRAYMPP) the multithreading
5597 * versions (getgrnam_r, getgrgid_r)
5598 * seem to return an illegal pointer
5599 * as the group members list, gr_mem.
5600 * getgrent() doesn't even have a _r version
5601 * but the gr_mem is poisonous anyway.
5602 * So yes, you cannot get the list of group
5603 * members if building multithreaded in UNICOS/mk. */
5604 for (elem = grent->gr_mem; elem && *elem; elem++) {
5605 sv_catpv(sv, *elem);
5607 sv_catpvn(sv, " ", 1);
5614 DIE(aTHX_ PL_no_func, "getgrent");
5620 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5625 DIE(aTHX_ PL_no_func, "setgrent");
5631 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5636 DIE(aTHX_ PL_no_func, "endgrent");
5646 if (!(tmps = PerlProc_getlogin()))
5648 PUSHp(tmps, strlen(tmps));
5651 DIE(aTHX_ PL_no_func, "getlogin");
5655 /* Miscellaneous. */
5660 dSP; dMARK; dORIGMARK; dTARGET;
5661 register I32 items = SP - MARK;
5662 unsigned long a[20];
5668 while (++MARK <= SP) {
5669 if (SvTAINTED(*MARK)) {
5675 TAINT_PROPER("syscall");
5678 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5679 * or where sizeof(long) != sizeof(char*). But such machines will
5680 * not likely have syscall implemented either, so who cares?
5682 while (++MARK <= SP) {
5683 if (SvNIOK(*MARK) || !i)
5684 a[i++] = SvIV(*MARK);
5685 else if (*MARK == &PL_sv_undef)
5688 a[i++] = (unsigned long)SvPV_force(*MARK, n_a);
5694 DIE(aTHX_ "Too many args to syscall");
5696 DIE(aTHX_ "Too few args to syscall");
5698 retval = syscall(a[0]);
5701 retval = syscall(a[0],a[1]);
5704 retval = syscall(a[0],a[1],a[2]);
5707 retval = syscall(a[0],a[1],a[2],a[3]);
5710 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5713 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5716 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5719 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5723 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5726 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],
5741 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5742 a[10],a[11],a[12],a[13]);
5744 #endif /* atarist */
5750 DIE(aTHX_ PL_no_func, "syscall");
5754 #ifdef FCNTL_EMULATE_FLOCK
5756 /* XXX Emulate flock() with fcntl().
5757 What's really needed is a good file locking module.
5761 fcntl_emulate_flock(int fd, int operation)
5765 switch (operation & ~LOCK_NB) {
5767 flock.l_type = F_RDLCK;
5770 flock.l_type = F_WRLCK;
5773 flock.l_type = F_UNLCK;
5779 flock.l_whence = SEEK_SET;
5780 flock.l_start = flock.l_len = (Off_t)0;
5782 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5785 #endif /* FCNTL_EMULATE_FLOCK */
5787 #ifdef LOCKF_EMULATE_FLOCK
5789 /* XXX Emulate flock() with lockf(). This is just to increase
5790 portability of scripts. The calls are not completely
5791 interchangeable. What's really needed is a good file
5795 /* The lockf() constants might have been defined in <unistd.h>.
5796 Unfortunately, <unistd.h> causes troubles on some mixed
5797 (BSD/POSIX) systems, such as SunOS 4.1.3.
5799 Further, the lockf() constants aren't POSIX, so they might not be
5800 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5801 just stick in the SVID values and be done with it. Sigh.
5805 # define F_ULOCK 0 /* Unlock a previously locked region */
5808 # define F_LOCK 1 /* Lock a region for exclusive use */
5811 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5814 # define F_TEST 3 /* Test a region for other processes locks */
5818 lockf_emulate_flock(int fd, int operation)
5824 /* flock locks entire file so for lockf we need to do the same */
5826 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5827 if (pos > 0) /* is seekable and needs to be repositioned */
5828 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5829 pos = -1; /* seek failed, so don't seek back afterwards */
5832 switch (operation) {
5834 /* LOCK_SH - get a shared lock */
5836 /* LOCK_EX - get an exclusive lock */
5838 i = lockf (fd, F_LOCK, 0);
5841 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5842 case LOCK_SH|LOCK_NB:
5843 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5844 case LOCK_EX|LOCK_NB:
5845 i = lockf (fd, F_TLOCK, 0);
5847 if ((errno == EAGAIN) || (errno == EACCES))
5848 errno = EWOULDBLOCK;
5851 /* LOCK_UN - unlock (non-blocking is a no-op) */
5853 case LOCK_UN|LOCK_NB:
5854 i = lockf (fd, F_ULOCK, 0);
5857 /* Default - can't decipher operation */
5864 if (pos > 0) /* need to restore position of the handle */
5865 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5870 #endif /* LOCKF_EMULATE_FLOCK */
5874 * c-indentation-style: bsd
5876 * indent-tabs-mode: t
5879 * vim: shiftwidth=4: