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>. */
49 # include <sys/wait.h>
53 # include <sys/resource.h>
62 # include <sys/select.h>
66 /* XXX Configure test needed.
67 h_errno might not be a simple 'int', especially for multi-threaded
68 applications, see "extern int errno in perl.h". Creating such
69 a test requires taking into account the differences between
70 compiling multithreaded and singlethreaded ($ccflags et al).
71 HOST_NOT_FOUND is typically defined in <netdb.h>.
73 #if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
82 struct passwd *getpwnam (char *);
83 struct passwd *getpwuid (Uid_t);
88 struct passwd *getpwent (void);
89 #elif defined (VMS) && defined (my_getpwent)
90 struct passwd *Perl_my_getpwent (pTHX);
99 struct group *getgrnam (char *);
100 struct group *getgrgid (Gid_t);
104 struct group *getgrent (void);
110 # if defined(_MSC_VER) || defined(__MINGW32__)
111 # include <sys/utime.h>
118 # ifdef my_chsize /* Probably #defined to Perl_my_chsize in embed.h */
121 # define my_chsize PerlLIO_chsize
124 # define my_chsize PerlLIO_chsize
126 I32 my_chsize(int fd, Off_t length);
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 const 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 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
205 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
206 * available in the 32bit environment, which could warrant Configure
207 * checks in the future.
210 #define LOCALTIME_EDGECASE_BROKEN
213 /* F_OK unused: if stat() cannot find it... */
215 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
216 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
217 # define PERL_EFF_ACCESS_R_OK(p) (access((p), R_OK | EFF_ONLY_OK))
218 # define PERL_EFF_ACCESS_W_OK(p) (access((p), W_OK | EFF_ONLY_OK))
219 # define PERL_EFF_ACCESS_X_OK(p) (access((p), X_OK | EFF_ONLY_OK))
222 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_EACCESS)
223 # ifdef I_SYS_SECURITY
224 # include <sys/security.h>
228 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
229 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
230 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
233 # define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
234 # define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
235 # define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
239 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
241 # define PERL_EFF_ACCESS_R_OK(p) (accessx((p), R_OK, ACC_SELF))
242 # define PERL_EFF_ACCESS_W_OK(p) (accessx((p), W_OK, ACC_SELF))
243 # define PERL_EFF_ACCESS_X_OK(p) (accessx((p), X_OK, ACC_SELF))
246 #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESS) \
247 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
248 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
251 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
253 const Uid_t ruid = getuid();
254 const Uid_t euid = geteuid();
255 const Gid_t rgid = getgid();
256 const Gid_t egid = getegid();
260 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
261 Perl_croak(aTHX_ "switching effective uid is not implemented");
264 if (setreuid(euid, ruid))
267 if (setresuid(euid, ruid, (Uid_t)-1))
270 Perl_croak(aTHX_ "entering effective uid failed");
273 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
274 Perl_croak(aTHX_ "switching effective gid is not implemented");
277 if (setregid(egid, rgid))
280 if (setresgid(egid, rgid, (Gid_t)-1))
283 Perl_croak(aTHX_ "entering effective gid failed");
286 res = access(path, mode);
289 if (setreuid(ruid, euid))
292 if (setresuid(ruid, euid, (Uid_t)-1))
295 Perl_croak(aTHX_ "leaving effective uid failed");
298 if (setregid(rgid, egid))
301 if (setresgid(rgid, egid, (Gid_t)-1))
304 Perl_croak(aTHX_ "leaving effective gid failed");
309 # define PERL_EFF_ACCESS_R_OK(p) (emulate_eaccess((p), R_OK))
310 # define PERL_EFF_ACCESS_W_OK(p) (emulate_eaccess((p), W_OK))
311 # define PERL_EFF_ACCESS_X_OK(p) (emulate_eaccess((p), X_OK))
314 #if !defined(PERL_EFF_ACCESS_R_OK)
315 /* With it or without it: anyway you get a warning: either that
316 it is unused, or it is declared static and never defined.
319 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
323 Perl_croak(aTHX_ "switching effective uid is not implemented");
333 const char * const tmps = POPpconstx;
334 const I32 gimme = GIMME_V;
335 const char *mode = "r";
338 if (PL_op->op_private & OPpOPEN_IN_RAW)
340 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
342 fp = PerlProc_popen(tmps, mode);
344 const char *type = NULL;
345 if (PL_curcop->cop_io) {
346 type = SvPV_nolen_const(PL_curcop->cop_io);
349 PerlIO_apply_layers(aTHX_ fp,mode,type);
351 if (gimme == G_VOID) {
353 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
356 else if (gimme == G_SCALAR) {
359 PL_rs = &PL_sv_undef;
360 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
361 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
369 SV * const sv = NEWSV(56, 79);
370 if (sv_gets(sv, fp, 0) == Nullch) {
374 XPUSHs(sv_2mortal(sv));
375 if (SvLEN(sv) - SvCUR(sv) > 20) {
376 SvPV_shrink_to_cur(sv);
381 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
382 TAINT; /* "I believe that this is not gratuitous!" */
385 STATUS_NATIVE_CHILD_SET(-1);
386 if (gimme == G_SCALAR)
397 tryAMAGICunTARGET(iter, -1);
399 /* Note that we only ever get here if File::Glob fails to load
400 * without at the same time croaking, for some reason, or if
401 * perl was built with PERL_EXTERNAL_GLOB */
408 * The external globbing program may use things we can't control,
409 * so for security reasons we must assume the worst.
412 taint_proper(PL_no_security, "glob");
416 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
417 PL_last_in_gv = (GV*)*PL_stack_sp--;
419 SAVESPTR(PL_rs); /* This is not permanent, either. */
420 PL_rs = sv_2mortal(newSVpvn("\000", 1));
423 *SvPVX(PL_rs) = '\n';
427 result = do_readline();
434 PL_last_in_gv = cGVOP_gv;
435 return do_readline();
444 if (SP - MARK != 1) {
446 do_join(TARG, &PL_sv_no, MARK, SP);
453 tmps = SvPV_const(tmpsv, len);
454 if ((!tmps || !len) && PL_errgv) {
455 SV * const error = ERRSV;
456 SvUPGRADE(error, SVt_PV);
457 if (SvPOK(error) && SvCUR(error))
458 sv_catpv(error, "\t...caught");
460 tmps = SvPV_const(tmpsv, len);
463 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
465 Perl_warn(aTHX_ "%"SVf, tmpsv);
477 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
479 if (SP - MARK != 1) {
481 do_join(TARG, &PL_sv_no, MARK, SP);
483 tmps = SvPV_const(tmpsv, len);
489 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
493 SvUPGRADE(error, SVt_PV);
494 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
496 SvSetSV(error,tmpsv);
497 else if (sv_isobject(error)) {
498 HV *stash = SvSTASH(SvRV(error));
499 GV *gv = gv_fetchmethod(stash, "PROPAGATE");
501 SV *file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
502 SV *line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
509 call_sv((SV*)GvCV(gv),
510 G_SCALAR|G_EVAL|G_KEEPERR);
511 sv_setsv(error,*PL_stack_sp--);
517 if (SvPOK(error) && SvCUR(error))
518 sv_catpv(error, "\t...propagated");
521 tmps = SvPV_const(tmpsv, len);
527 tmpsv = sv_2mortal(newSVpvn("Died", 4));
529 DIE(aTHX_ "%"SVf, tmpsv);
545 GV * const gv = (GV *)*++MARK;
548 DIE(aTHX_ PL_no_usym, "filehandle");
549 if ((io = GvIOp(gv)))
550 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
553 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
555 /* Method's args are same as ours ... */
556 /* ... except handle is replaced by the object */
557 *MARK-- = SvTIED_obj((SV*)io, mg);
561 call_method("OPEN", G_SCALAR);
575 tmps = SvPV_const(sv, len);
576 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
579 PUSHi( (I32)PL_forkprocess );
580 else if (PL_forkprocess == 0) /* we are a new child */
592 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
594 if (gv && (io = GvIO(gv))
595 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
598 XPUSHs(SvTIED_obj((SV*)io, mg));
601 call_method("CLOSE", G_SCALAR);
607 PUSHs(boolSV(do_close(gv, TRUE)));
619 GV * const wgv = (GV*)POPs;
620 GV * const rgv = (GV*)POPs;
625 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
626 DIE(aTHX_ PL_no_usym, "filehandle");
631 do_close(rgv, FALSE);
633 do_close(wgv, FALSE);
635 if (PerlProc_pipe(fd) < 0)
638 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
639 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
640 IoOFP(rstio) = IoIFP(rstio);
641 IoIFP(wstio) = IoOFP(wstio);
642 IoTYPE(rstio) = IoTYPE_RDONLY;
643 IoTYPE(wstio) = IoTYPE_WRONLY;
645 if (!IoIFP(rstio) || !IoOFP(wstio)) {
646 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
647 else PerlLIO_close(fd[0]);
648 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
649 else PerlLIO_close(fd[1]);
652 #if defined(HAS_FCNTL) && defined(F_SETFD)
653 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
654 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
661 DIE(aTHX_ PL_no_func, "pipe");
677 if (gv && (io = GvIO(gv))
678 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
681 XPUSHs(SvTIED_obj((SV*)io, mg));
684 call_method("FILENO", G_SCALAR);
690 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
691 /* Can't do this because people seem to do things like
692 defined(fileno($foo)) to check whether $foo is a valid fh.
693 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
694 report_evil_fh(gv, io, PL_op->op_type);
699 PUSHi(PerlIO_fileno(fp));
711 anum = PerlLIO_umask(0);
712 (void)PerlLIO_umask(anum);
715 anum = PerlLIO_umask(POPi);
716 TAINT_PROPER("umask");
719 /* Only DIE if trying to restrict permissions on "user" (self).
720 * Otherwise it's harmless and more useful to just return undef
721 * since 'group' and 'other' concepts probably don't exist here. */
722 if (MAXARG >= 1 && (POPi & 0700))
723 DIE(aTHX_ "umask not implemented");
724 XPUSHs(&PL_sv_undef);
746 if (gv && (io = GvIO(gv))
747 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
750 XPUSHs(SvTIED_obj((SV*)io, mg));
755 call_method("BINMODE", G_SCALAR);
762 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
763 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
764 report_evil_fh(gv, io, PL_op->op_type);
765 SETERRNO(EBADF,RMS_IFI);
770 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
771 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
772 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
773 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
774 mode_from_discipline(discp),
775 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
795 const I32 markoff = MARK - PL_stack_base;
796 const char *methname;
797 int how = PERL_MAGIC_tied;
801 switch(SvTYPE(varsv)) {
803 methname = "TIEHASH";
804 HvEITER_set((HV *)varsv, 0);
807 methname = "TIEARRAY";
810 #ifdef GV_UNIQUE_CHECK
811 if (GvUNIQUE((GV*)varsv)) {
812 Perl_croak(aTHX_ "Attempt to tie unique GV");
815 methname = "TIEHANDLE";
816 how = PERL_MAGIC_tiedscalar;
817 /* For tied filehandles, we apply tiedscalar magic to the IO
818 slot of the GP rather than the GV itself. AMS 20010812 */
820 GvIOp(varsv) = newIO();
821 varsv = (SV *)GvIOp(varsv);
824 methname = "TIESCALAR";
825 how = PERL_MAGIC_tiedscalar;
829 if (sv_isobject(*MARK)) {
831 PUSHSTACKi(PERLSI_MAGIC);
833 EXTEND(SP,(I32)items);
837 call_method(methname, G_SCALAR);
840 /* Not clear why we don't call call_method here too.
841 * perhaps to get different error message ?
843 stash = gv_stashsv(*MARK, FALSE);
844 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
845 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
849 PUSHSTACKi(PERLSI_MAGIC);
851 EXTEND(SP,(I32)items);
855 call_sv((SV*)GvCV(gv), G_SCALAR);
861 if (sv_isobject(sv)) {
862 sv_unmagic(varsv, how);
863 /* Croak if a self-tie on an aggregate is attempted. */
864 if (varsv == SvRV(sv) &&
865 (SvTYPE(varsv) == SVt_PVAV ||
866 SvTYPE(varsv) == SVt_PVHV))
868 "Self-ties of arrays and hashes are not supported");
869 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
872 SP = PL_stack_base + markoff;
882 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
883 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
885 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
888 if ((mg = SvTIED_mg(sv, how))) {
889 SV * const obj = SvRV(SvTIED_obj(sv, mg));
892 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
893 if (gv && isGV(gv) && (cv = GvCV(gv))) {
895 XPUSHs(SvTIED_obj((SV*)gv, mg));
896 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
899 call_sv((SV *)cv, G_VOID);
903 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
904 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
905 "untie attempted while %"UVuf" inner references still exist",
906 (UV)SvREFCNT(obj) - 1 ) ;
910 sv_unmagic(sv, how) ;
919 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
920 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
922 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
925 if ((mg = SvTIED_mg(sv, how))) {
926 SV *osv = SvTIED_obj(sv, mg);
927 if (osv == mg->mg_obj)
928 osv = sv_mortalcopy(osv);
942 HV * const hv = (HV*)POPs;
943 SV * const 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);
1001 struct timeval timebuf;
1002 struct timeval *tbuf = &timebuf;
1005 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1010 # if BYTEORDER & 0xf0000
1011 # define ORDERBYTE (0x88888888 - BYTEORDER)
1013 # define ORDERBYTE (0x4444 - BYTEORDER)
1019 for (i = 1; i <= 3; i++) {
1020 SV * const sv = SP[i];
1023 if (SvREADONLY(sv)) {
1025 sv_force_normal_flags(sv, 0);
1026 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1027 DIE(aTHX_ PL_no_modify);
1030 if (ckWARN(WARN_MISC))
1031 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1032 SvPV_force_nolen(sv); /* force string conversion */
1039 /* little endians can use vecs directly */
1040 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1047 masksize = NFDBITS / NBBY;
1049 masksize = sizeof(long); /* documented int, everyone seems to use long */
1051 Zero(&fd_sets[0], 4, char*);
1054 # if SELECT_MIN_BITS == 1
1055 growsize = sizeof(fd_set);
1057 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1058 # undef SELECT_MIN_BITS
1059 # define SELECT_MIN_BITS __FD_SETSIZE
1061 /* If SELECT_MIN_BITS is greater than one we most probably will want
1062 * to align the sizes with SELECT_MIN_BITS/8 because for example
1063 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1064 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1065 * on (sets/tests/clears bits) is 32 bits. */
1066 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1074 timebuf.tv_sec = (long)value;
1075 value -= (NV)timebuf.tv_sec;
1076 timebuf.tv_usec = (long)(value * 1000000.0);
1079 tbuf = Null(struct timeval*);
1081 for (i = 1; i <= 3; i++) {
1083 if (!SvOK(sv) || SvCUR(sv) == 0) {
1090 Sv_Grow(sv, growsize);
1094 while (++j <= growsize) {
1098 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1100 Newx(fd_sets[i], growsize, char);
1101 for (offset = 0; offset < growsize; offset += masksize) {
1102 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1103 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1106 fd_sets[i] = SvPVX(sv);
1110 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1111 /* Can't make just the (void*) conditional because that would be
1112 * cpp #if within cpp macro, and not all compilers like that. */
1113 nfound = PerlSock_select(
1115 (Select_fd_set_t) fd_sets[1],
1116 (Select_fd_set_t) fd_sets[2],
1117 (Select_fd_set_t) fd_sets[3],
1118 (void*) tbuf); /* Workaround for compiler bug. */
1120 nfound = PerlSock_select(
1122 (Select_fd_set_t) fd_sets[1],
1123 (Select_fd_set_t) fd_sets[2],
1124 (Select_fd_set_t) fd_sets[3],
1127 for (i = 1; i <= 3; i++) {
1130 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1132 for (offset = 0; offset < growsize; offset += masksize) {
1133 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1134 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1136 Safefree(fd_sets[i]);
1143 if (GIMME == G_ARRAY && tbuf) {
1144 value = (NV)(timebuf.tv_sec) +
1145 (NV)(timebuf.tv_usec) / 1000000.0;
1146 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1147 sv_setnv(sv, value);
1151 DIE(aTHX_ "select not implemented");
1156 Perl_setdefout(pTHX_ GV *gv)
1159 (void)SvREFCNT_inc(gv);
1161 SvREFCNT_dec(PL_defoutgv);
1171 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1173 egv = GvEGV(PL_defoutgv);
1178 XPUSHs(&PL_sv_undef);
1180 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1181 if (gvp && *gvp == egv) {
1182 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1186 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1191 if (!GvIO(newdefout))
1192 gv_IOadd(newdefout);
1193 setdefout(newdefout);
1204 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1206 if (gv && (io = GvIO(gv))
1207 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1209 const 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 ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1223 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1224 report_evil_fh(gv, io, PL_op->op_type);
1225 SETERRNO(EBADF,RMS_IFI);
1229 sv_setpvn(TARG, " ", 1);
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_const(TARG));
1236 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1237 SvCUR_set(TARG,1+len);
1246 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1249 register PERL_CONTEXT *cx;
1250 const I32 gimme = GIMME_V;
1255 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1257 cx->blk_sub.retop = retop;
1259 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1261 setdefout(gv); /* locally select filehandle so $% et al work */
1293 SV * const tmpsv = sv_newmortal();
1295 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1296 name = SvPV_nolen_const(tmpsv);
1298 DIE(aTHX_ "Undefined format \"%s\" called", name);
1300 DIE(aTHX_ "Not a format reference");
1303 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1305 IoFLAGS(io) &= ~IOf_DIDTOP;
1306 return doform(cv,gv,PL_op->op_next);
1312 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1313 register IO * const io = GvIOp(gv);
1314 PerlIO * const ofp = IoOFP(io);
1318 register PERL_CONTEXT *cx;
1320 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1321 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1324 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1325 PL_formtarget != PL_toptarget)
1329 if (!IoTOP_GV(io)) {
1332 if (!IoTOP_NAME(io)) {
1334 if (!IoFMT_NAME(io))
1335 IoFMT_NAME(io) = savepv(GvNAME(gv));
1336 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1337 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1338 if ((topgv && GvFORM(topgv)) ||
1339 !gv_fetchpv("top",FALSE,SVt_PVFM))
1340 IoTOP_NAME(io) = savesvpv(topname);
1342 IoTOP_NAME(io) = savepvn("top", 3);
1344 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1345 if (!topgv || !GvFORM(topgv)) {
1346 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1349 IoTOP_GV(io) = topgv;
1351 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1352 I32 lines = IoLINES_LEFT(io);
1353 const char *s = SvPVX_const(PL_formtarget);
1354 if (lines <= 0) /* Yow, header didn't even fit!!! */
1356 while (lines-- > 0) {
1357 s = strchr(s, '\n');
1363 const STRLEN save = SvCUR(PL_formtarget);
1364 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1365 do_print(PL_formtarget, ofp);
1366 SvCUR_set(PL_formtarget, save);
1367 sv_chop(PL_formtarget, s);
1368 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1371 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1372 do_print(PL_formfeed, ofp);
1373 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1375 PL_formtarget = PL_toptarget;
1376 IoFLAGS(io) |= IOf_DIDTOP;
1379 DIE(aTHX_ "bad top format reference");
1382 SV * const sv = sv_newmortal();
1384 gv_efullname4(sv, fgv, Nullch, FALSE);
1385 name = SvPV_nolen_const(sv);
1387 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1391 DIE(aTHX_ "Undefined top format called");
1394 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1395 return doform(cv,gv,PL_op);
1399 POPBLOCK(cx,PL_curpm);
1405 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1407 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1408 else if (ckWARN(WARN_CLOSED))
1409 report_evil_fh(gv, io, PL_op->op_type);
1414 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1415 if (ckWARN(WARN_IO))
1416 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1418 if (!do_print(PL_formtarget, fp))
1421 FmLINES(PL_formtarget) = 0;
1422 SvCUR_set(PL_formtarget, 0);
1423 *SvEND(PL_formtarget) = '\0';
1424 if (IoFLAGS(io) & IOf_FLUSH)
1425 (void)PerlIO_flush(fp);
1430 PL_formtarget = PL_bodytarget;
1432 PERL_UNUSED_VAR(newsp);
1433 PERL_UNUSED_VAR(gimme);
1434 return cx->blk_sub.retop;
1439 dVAR; dSP; dMARK; dORIGMARK;
1445 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1447 if (gv && (io = GvIO(gv))
1448 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1450 if (MARK == ORIGMARK) {
1453 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1457 *MARK = SvTIED_obj((SV*)io, mg);
1460 call_method("PRINTF", G_SCALAR);
1463 MARK = ORIGMARK + 1;
1470 if (!(io = GvIO(gv))) {
1471 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1472 report_evil_fh(gv, io, PL_op->op_type);
1473 SETERRNO(EBADF,RMS_IFI);
1476 else if (!(fp = IoOFP(io))) {
1477 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1479 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1480 else if (ckWARN(WARN_CLOSED))
1481 report_evil_fh(gv, io, PL_op->op_type);
1483 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1487 do_sprintf(sv, SP - MARK, MARK + 1);
1488 if (!do_print(sv, fp))
1491 if (IoFLAGS(io) & IOf_FLUSH)
1492 if (PerlIO_flush(fp) == EOF)
1503 PUSHs(&PL_sv_undef);
1510 const int perm = (MAXARG > 3) ? POPi : 0666;
1511 const int mode = POPi;
1512 SV * const sv = POPs;
1513 GV * const gv = (GV *)POPs;
1516 /* Need TIEHANDLE method ? */
1517 const char * const tmps = SvPV_const(sv, len);
1518 /* FIXME? do_open should do const */
1519 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1520 IoLINES(GvIOp(gv)) = 0;
1524 PUSHs(&PL_sv_undef);
1531 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1537 Sock_size_t bufsize;
1545 bool charstart = FALSE;
1546 STRLEN charskip = 0;
1549 GV * const gv = (GV*)*++MARK;
1550 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1551 && gv && (io = GvIO(gv)) )
1553 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1557 *MARK = SvTIED_obj((SV*)io, mg);
1559 call_method("READ", G_SCALAR);
1573 sv_setpvn(bufsv, "", 0);
1574 length = SvIVx(*++MARK);
1577 offset = SvIVx(*++MARK);
1581 if (!io || !IoIFP(io)) {
1582 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1583 report_evil_fh(gv, io, PL_op->op_type);
1584 SETERRNO(EBADF,RMS_IFI);
1587 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1588 buffer = SvPVutf8_force(bufsv, blen);
1589 /* UTF-8 may not have been set if they are all low bytes */
1594 buffer = SvPV_force(bufsv, blen);
1595 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1598 DIE(aTHX_ "Negative length");
1606 if (PL_op->op_type == OP_RECV) {
1607 char namebuf[MAXPATHLEN];
1608 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1609 bufsize = sizeof (struct sockaddr_in);
1611 bufsize = sizeof namebuf;
1613 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1617 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1618 /* 'offset' means 'flags' here */
1619 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1620 (struct sockaddr *)namebuf, &bufsize);
1624 /* Bogus return without padding */
1625 bufsize = sizeof (struct sockaddr_in);
1627 SvCUR_set(bufsv, count);
1628 *SvEND(bufsv) = '\0';
1629 (void)SvPOK_only(bufsv);
1633 /* This should not be marked tainted if the fp is marked clean */
1634 if (!(IoFLAGS(io) & IOf_UNTAINT))
1635 SvTAINTED_on(bufsv);
1637 sv_setpvn(TARG, namebuf, bufsize);
1642 if (PL_op->op_type == OP_RECV)
1643 DIE(aTHX_ PL_no_sock_func, "recv");
1645 if (DO_UTF8(bufsv)) {
1646 /* offset adjust in characters not bytes */
1647 blen = sv_len_utf8(bufsv);
1650 if (-offset > (int)blen)
1651 DIE(aTHX_ "Offset outside string");
1654 if (DO_UTF8(bufsv)) {
1655 /* convert offset-as-chars to offset-as-bytes */
1656 if (offset >= (int)blen)
1657 offset += SvCUR(bufsv) - blen;
1659 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1662 bufsize = SvCUR(bufsv);
1663 /* Allocating length + offset + 1 isn't perfect in the case of reading
1664 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1666 (should be 2 * length + offset + 1, or possibly something longer if
1667 PL_encoding is true) */
1668 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1669 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1670 Zero(buffer+bufsize, offset-bufsize, char);
1672 buffer = buffer + offset;
1674 read_target = bufsv;
1676 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1677 concatenate it to the current buffer. */
1679 /* Truncate the existing buffer to the start of where we will be
1681 SvCUR_set(bufsv, offset);
1683 read_target = sv_newmortal();
1684 SvUPGRADE(read_target, SVt_PV);
1685 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1688 if (PL_op->op_type == OP_SYSREAD) {
1689 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1690 if (IoTYPE(io) == IoTYPE_SOCKET) {
1691 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1697 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1702 #ifdef HAS_SOCKET__bad_code_maybe
1703 if (IoTYPE(io) == IoTYPE_SOCKET) {
1704 char namebuf[MAXPATHLEN];
1705 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1706 bufsize = sizeof (struct sockaddr_in);
1708 bufsize = sizeof namebuf;
1710 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1711 (struct sockaddr *)namebuf, &bufsize);
1716 count = PerlIO_read(IoIFP(io), buffer, length);
1717 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1718 if (count == 0 && PerlIO_error(IoIFP(io)))
1722 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1723 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1726 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1727 *SvEND(read_target) = '\0';
1728 (void)SvPOK_only(read_target);
1729 if (fp_utf8 && !IN_BYTES) {
1730 /* Look at utf8 we got back and count the characters */
1731 const char *bend = buffer + count;
1732 while (buffer < bend) {
1734 skip = UTF8SKIP(buffer);
1737 if (buffer - charskip + skip > bend) {
1738 /* partial character - try for rest of it */
1739 length = skip - (bend-buffer);
1740 offset = bend - SvPVX_const(bufsv);
1752 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1753 provided amount read (count) was what was requested (length)
1755 if (got < wanted && count == length) {
1756 length = wanted - got;
1757 offset = bend - SvPVX_const(bufsv);
1760 /* return value is character count */
1764 else if (buffer_utf8) {
1765 /* Let svcatsv upgrade the bytes we read in to utf8.
1766 The buffer is a mortal so will be freed soon. */
1767 sv_catsv_nomg(bufsv, read_target);
1770 /* This should not be marked tainted if the fp is marked clean */
1771 if (!(IoFLAGS(io) & IOf_UNTAINT))
1772 SvTAINTED_on(bufsv);
1785 const int items = (SP - PL_stack_base) - TOPMARK;
1789 sv = sv_2mortal(newSViv(sv_len(*SP)));
1798 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1809 if (PL_op->op_type == OP_SYSWRITE
1810 && gv && (io = GvIO(gv))
1811 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1816 *MARK = SvTIED_obj((SV*)io, mg);
1818 call_method("WRITE", G_SCALAR);
1829 #if Size_t_size > IVSIZE
1830 length = (Size_t)SvNVx(*++MARK);
1832 length = (Size_t)SvIVx(*++MARK);
1834 if ((SSize_t)length < 0)
1835 DIE(aTHX_ "Negative length");
1838 if (!io || !IoIFP(io)) {
1840 if (ckWARN(WARN_CLOSED))
1841 report_evil_fh(gv, io, PL_op->op_type);
1842 SETERRNO(EBADF,RMS_IFI);
1846 if (PerlIO_isutf8(IoIFP(io))) {
1847 if (!SvUTF8(bufsv)) {
1848 bufsv = sv_2mortal(newSVsv(bufsv));
1849 buffer = sv_2pvutf8(bufsv, &blen);
1851 buffer = SvPV_const(bufsv, blen);
1854 if (DO_UTF8(bufsv)) {
1855 /* Not modifying source SV, so making a temporary copy. */
1856 bufsv = sv_2mortal(newSVsv(bufsv));
1857 sv_utf8_downgrade(bufsv, FALSE);
1859 buffer = SvPV_const(bufsv, blen);
1862 if (PL_op->op_type == OP_SYSWRITE) {
1864 if (DO_UTF8(bufsv)) {
1865 /* length and offset are in chars */
1866 blen = sv_len_utf8(bufsv);
1869 offset = SvIVx(*++MARK);
1871 if (-offset > (IV)blen)
1872 DIE(aTHX_ "Offset outside string");
1874 } else if (offset >= (IV)blen && blen > 0)
1875 DIE(aTHX_ "Offset outside string");
1878 if (length > blen - offset)
1879 length = blen - offset;
1880 if (DO_UTF8(bufsv)) {
1881 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1882 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1885 buffer = buffer+offset;
1887 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1888 if (IoTYPE(io) == IoTYPE_SOCKET) {
1889 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1895 /* See the note at doio.c:do_print about filesize limits. --jhi */
1896 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1901 else if (SP > MARK) {
1903 char * const sockbuf = SvPVx(*++MARK, mlen);
1904 /* length is really flags */
1905 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1906 length, (struct sockaddr *)sockbuf, mlen);
1909 /* length is really flags */
1910 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, length);
1913 DIE(aTHX_ PL_no_sock_func, "send");
1919 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1920 #if Size_t_size > IVSIZE
1940 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1942 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1944 if (io && !IoIFP(io)) {
1945 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1947 IoFLAGS(io) &= ~IOf_START;
1948 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1949 sv_setpvn(GvSV(gv), "-", 1);
1950 SvSETMAGIC(GvSV(gv));
1952 else if (!nextargv(gv))
1957 gv = PL_last_in_gv; /* eof */
1960 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1962 if (gv && (io = GvIO(gv))
1963 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1966 XPUSHs(SvTIED_obj((SV*)io, mg));
1969 call_method("EOF", G_SCALAR);
1975 PUSHs(boolSV(!gv || do_eof(gv)));
1987 PL_last_in_gv = (GV*)POPs;
1990 if (gv && (io = GvIO(gv))
1991 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1994 XPUSHs(SvTIED_obj((SV*)io, mg));
1997 call_method("TELL", G_SCALAR);
2003 #if LSEEKSIZE > IVSIZE
2004 PUSHn( do_tell(gv) );
2006 PUSHi( do_tell(gv) );
2016 const int whence = POPi;
2017 #if LSEEKSIZE > IVSIZE
2018 Off_t offset = (Off_t)SvNVx(POPs);
2020 Off_t offset = (Off_t)SvIVx(POPs);
2024 gv = PL_last_in_gv = (GV*)POPs;
2026 if (gv && (io = GvIO(gv))
2027 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2030 XPUSHs(SvTIED_obj((SV*)io, mg));
2031 #if LSEEKSIZE > IVSIZE
2032 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2034 XPUSHs(sv_2mortal(newSViv(offset)));
2036 XPUSHs(sv_2mortal(newSViv(whence)));
2039 call_method("SEEK", G_SCALAR);
2045 if (PL_op->op_type == OP_SEEK)
2046 PUSHs(boolSV(do_seek(gv, offset, whence)));
2048 Off_t sought = do_sysseek(gv, offset, whence);
2050 PUSHs(&PL_sv_undef);
2053 #if LSEEKSIZE > IVSIZE
2058 : newSVpvn(zero_but_true, ZBTLEN);
2059 PUSHs(sv_2mortal(sv));
2068 /* There seems to be no consensus on the length type of truncate()
2069 * and ftruncate(), both off_t and size_t have supporters. In
2070 * general one would think that when using large files, off_t is
2071 * at least as wide as size_t, so using an off_t should be okay. */
2072 /* XXX Configure probe for the length type of *truncate() needed XXX */
2075 #if Off_t_size > IVSIZE
2080 /* Checking for length < 0 is problematic as the type might or
2081 * might not be signed: if it is not, clever compilers will moan. */
2082 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2089 if (PL_op->op_flags & OPf_SPECIAL) {
2090 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2099 TAINT_PROPER("truncate");
2100 if (!(fp = IoIFP(io))) {
2106 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2108 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2118 if (SvTYPE(sv) == SVt_PVGV) {
2119 tmpgv = (GV*)sv; /* *main::FRED for example */
2120 goto do_ftruncate_gv;
2122 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2123 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2124 goto do_ftruncate_gv;
2126 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2127 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2128 goto do_ftruncate_io;
2131 name = SvPV_nolen_const(sv);
2132 TAINT_PROPER("truncate");
2134 if (truncate(name, len) < 0)
2140 if ((tmpfd = PerlLIO_open(name, O_RDWR)) < 0)
2143 if (my_chsize(tmpfd, len) < 0)
2145 PerlLIO_close(tmpfd);
2154 SETERRNO(EBADF,RMS_IFI);
2163 const unsigned int func = POPu;
2164 const int optype = PL_op->op_type;
2168 IO *io = gv ? GvIOn(gv) : 0;
2170 if (!io || !argsv || !IoIFP(io)) {
2171 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2172 report_evil_fh(gv, io, PL_op->op_type);
2173 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2177 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2180 s = SvPV_force(argsv, len);
2181 need = IOCPARM_LEN(func);
2183 s = Sv_Grow(argsv, need + 1);
2184 SvCUR_set(argsv, need);
2187 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2190 retval = SvIV(argsv);
2191 s = INT2PTR(char*,retval); /* ouch */
2194 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2196 if (optype == OP_IOCTL)
2198 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2200 DIE(aTHX_ "ioctl is not implemented");
2204 DIE(aTHX_ "fcntl is not implemented");
2206 #if defined(OS2) && defined(__EMX__)
2207 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2209 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2213 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2215 if (s[SvCUR(argsv)] != 17)
2216 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2218 s[SvCUR(argsv)] = 0; /* put our null back */
2219 SvSETMAGIC(argsv); /* Assume it has changed */
2228 PUSHp(zero_but_true, ZBTLEN);
2249 if (gv && (io = GvIO(gv)))
2256 (void)PerlIO_flush(fp);
2257 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2260 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2261 report_evil_fh(gv, io, PL_op->op_type);
2263 SETERRNO(EBADF,RMS_IFI);
2268 DIE(aTHX_ PL_no_func, "flock()");
2280 int protocol = POPi;
2286 io = gv ? GvIOn(gv) : NULL;
2289 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2290 report_evil_fh(gv, io, PL_op->op_type);
2292 do_close(gv, FALSE);
2293 SETERRNO(EBADF,LIB_INVARG);
2298 do_close(gv, FALSE);
2300 TAINT_PROPER("socket");
2301 fd = PerlSock_socket(domain, type, protocol);
2304 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2305 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2306 IoTYPE(io) = IoTYPE_SOCKET;
2307 if (!IoIFP(io) || !IoOFP(io)) {
2308 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2309 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2310 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2313 #if defined(HAS_FCNTL) && defined(F_SETFD)
2314 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2318 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2323 DIE(aTHX_ PL_no_sock_func, "socket");
2329 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2335 int protocol = POPi;
2342 io1 = gv1 ? GvIOn(gv1) : NULL;
2343 io2 = gv2 ? GvIOn(gv2) : NULL;
2344 if (!gv1 || !gv2 || !io1 || !io2) {
2345 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2347 report_evil_fh(gv1, io1, PL_op->op_type);
2349 report_evil_fh(gv1, io2, PL_op->op_type);
2352 do_close(gv1, FALSE);
2354 do_close(gv2, FALSE);
2359 do_close(gv1, FALSE);
2361 do_close(gv2, FALSE);
2363 TAINT_PROPER("socketpair");
2364 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2366 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2367 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2368 IoTYPE(io1) = IoTYPE_SOCKET;
2369 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2370 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2371 IoTYPE(io2) = IoTYPE_SOCKET;
2372 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2373 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2374 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2375 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2376 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2377 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2378 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2381 #if defined(HAS_FCNTL) && defined(F_SETFD)
2382 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2383 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2388 DIE(aTHX_ PL_no_sock_func, "socketpair");
2396 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2397 extern void GETPRIVMODE();
2398 extern void GETUSERMODE();
2401 /* OK, so on what platform does bind modify addr? */
2404 register IO *io = GvIOn(gv);
2408 int mpeprivmode = 0;
2411 if (!io || !IoIFP(io))
2414 addr = SvPV_const(addrsv, len);
2415 TAINT_PROPER("bind");
2416 #ifdef MPE /* Deal with MPE bind() peculiarities */
2417 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2418 /* The address *MUST* stupidly be zero. */
2419 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2420 /* PRIV mode is required to bind() to ports < 1024. */
2421 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2422 ((struct sockaddr_in *)addr)->sin_port > 0) {
2423 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2428 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2429 (struct sockaddr *)addr, len) >= 0)
2432 #ifdef MPE /* Switch back to USER mode */
2443 if (ckWARN(WARN_CLOSED))
2444 report_evil_fh(gv, io, PL_op->op_type);
2445 SETERRNO(EBADF,SS_IVCHAN);
2448 DIE(aTHX_ PL_no_sock_func, "bind");
2459 register IO *io = GvIOn(gv);
2462 if (!io || !IoIFP(io))
2465 addr = SvPV_const(addrsv, len);
2466 TAINT_PROPER("connect");
2467 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2473 if (ckWARN(WARN_CLOSED))
2474 report_evil_fh(gv, io, PL_op->op_type);
2475 SETERRNO(EBADF,SS_IVCHAN);
2478 DIE(aTHX_ PL_no_sock_func, "connect");
2488 register IO *io = gv ? GvIOn(gv) : NULL;
2490 if (!gv || !io || !IoIFP(io))
2493 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2499 if (ckWARN(WARN_CLOSED))
2500 report_evil_fh(gv, io, PL_op->op_type);
2501 SETERRNO(EBADF,SS_IVCHAN);
2504 DIE(aTHX_ PL_no_sock_func, "listen");
2516 char namebuf[MAXPATHLEN];
2517 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2518 Sock_size_t len = sizeof (struct sockaddr_in);
2520 Sock_size_t len = sizeof namebuf;
2533 if (!gstio || !IoIFP(gstio))
2537 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2541 do_close(ngv, FALSE);
2542 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2543 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2544 IoTYPE(nstio) = IoTYPE_SOCKET;
2545 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2546 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2547 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2548 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2551 #if defined(HAS_FCNTL) && defined(F_SETFD)
2552 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2556 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2557 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2559 #ifdef __SCO_VERSION__
2560 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2563 PUSHp(namebuf, len);
2567 if (ckWARN(WARN_CLOSED))
2568 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2569 SETERRNO(EBADF,SS_IVCHAN);
2575 DIE(aTHX_ PL_no_sock_func, "accept");
2585 register IO *io = GvIOn(gv);
2587 if (!io || !IoIFP(io))
2590 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2594 if (ckWARN(WARN_CLOSED))
2595 report_evil_fh(gv, io, PL_op->op_type);
2596 SETERRNO(EBADF,SS_IVCHAN);
2599 DIE(aTHX_ PL_no_sock_func, "shutdown");
2607 int optype = PL_op->op_type;
2610 unsigned int optname;
2616 if (optype == OP_GSOCKOPT)
2617 sv = sv_2mortal(NEWSV(22, 257));
2620 optname = (unsigned int) POPi;
2621 lvl = (unsigned int) POPi;
2625 if (!io || !IoIFP(io))
2628 fd = PerlIO_fileno(IoIFP(io));
2632 (void)SvPOK_only(sv);
2636 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2643 #if defined(__SYMBIAN32__)
2644 # define SETSOCKOPT_OPTION_VALUE_T void *
2646 # define SETSOCKOPT_OPTION_VALUE_T const char *
2648 /* XXX TODO: We need to have a proper type (a Configure probe,
2649 * etc.) for what the C headers think of the third argument of
2650 * setsockopt(), the option_value read-only buffer: is it
2651 * a "char *", or a "void *", const or not. Some compilers
2652 * don't take kindly to e.g. assuming that "char *" implicitly
2653 * promotes to a "void *", or to explicitly promoting/demoting
2654 * consts to non/vice versa. The "const void *" is the SUS
2655 * definition, but that does not fly everywhere for the above
2657 SETSOCKOPT_OPTION_VALUE_T buf;
2661 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2665 aint = (int)SvIV(sv);
2666 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2669 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2678 if (ckWARN(WARN_CLOSED))
2679 report_evil_fh(gv, io, optype);
2680 SETERRNO(EBADF,SS_IVCHAN);
2685 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2693 int optype = PL_op->op_type;
2697 register IO *io = GvIOn(gv);
2700 if (!io || !IoIFP(io))
2703 sv = sv_2mortal(NEWSV(22, 257));
2704 (void)SvPOK_only(sv);
2708 fd = PerlIO_fileno(IoIFP(io));
2710 case OP_GETSOCKNAME:
2711 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2714 case OP_GETPEERNAME:
2715 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2717 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2719 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";
2720 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2721 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2722 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2723 sizeof(u_short) + sizeof(struct in_addr))) {
2730 #ifdef BOGUS_GETNAME_RETURN
2731 /* Interactive Unix, getpeername() and getsockname()
2732 does not return valid namelen */
2733 if (len == BOGUS_GETNAME_RETURN)
2734 len = sizeof(struct sockaddr);
2742 if (ckWARN(WARN_CLOSED))
2743 report_evil_fh(gv, io, optype);
2744 SETERRNO(EBADF,SS_IVCHAN);
2749 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2762 if (PL_op->op_flags & OPf_REF) {
2764 if (PL_op->op_type == OP_LSTAT) {
2765 if (gv != PL_defgv) {
2766 if (ckWARN(WARN_IO))
2767 Perl_warner(aTHX_ packWARN(WARN_IO),
2768 "lstat() on filehandle %s", GvENAME(gv));
2769 } else if (PL_laststype != OP_LSTAT)
2770 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2774 if (gv != PL_defgv) {
2775 PL_laststype = OP_STAT;
2777 sv_setpvn(PL_statname, "", 0);
2778 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2779 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2781 if (PL_laststatval < 0) {
2782 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2783 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2789 if (SvTYPE(sv) == SVt_PVGV) {
2793 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2795 if (PL_op->op_type == OP_LSTAT && ckWARN(WARN_IO))
2796 Perl_warner(aTHX_ packWARN(WARN_IO),
2797 "lstat() on filehandle %s", GvENAME(gv));
2800 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2802 PL_laststype = PL_op->op_type;
2803 if (PL_op->op_type == OP_LSTAT)
2804 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2806 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2807 if (PL_laststatval < 0) {
2808 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2809 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2815 if (gimme != G_ARRAY) {
2816 if (gimme != G_VOID)
2817 XPUSHs(boolSV(max));
2823 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2824 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2825 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2826 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2827 #if Uid_t_size > IVSIZE
2828 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2830 # if Uid_t_sign <= 0
2831 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2833 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2836 #if Gid_t_size > IVSIZE
2837 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2839 # if Gid_t_sign <= 0
2840 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2842 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2845 #ifdef USE_STAT_RDEV
2846 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2848 PUSHs(sv_2mortal(newSVpvn("", 0)));
2850 #if Off_t_size > IVSIZE
2851 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2853 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2856 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2857 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2858 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2860 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2861 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2862 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2864 #ifdef USE_STAT_BLOCKS
2865 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2866 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2868 PUSHs(sv_2mortal(newSVpvn("", 0)));
2869 PUSHs(sv_2mortal(newSVpvn("", 0)));
2875 /* This macro is used by the stacked filetest operators :
2876 * if the previous filetest failed, short-circuit and pass its value.
2877 * Else, discard it from the stack and continue. --rgs
2879 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2880 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2881 else { (void)POPs; PUTBACK; } \
2888 STACKED_FTEST_CHECK;
2889 #if defined(HAS_ACCESS) && defined(R_OK)
2890 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2891 result = access(POPpx, R_OK);
2906 if (cando(S_IRUSR, 0, &PL_statcache))
2915 STACKED_FTEST_CHECK;
2916 #if defined(HAS_ACCESS) && defined(W_OK)
2917 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2918 result = access(POPpx, W_OK);
2933 if (cando(S_IWUSR, 0, &PL_statcache))
2942 STACKED_FTEST_CHECK;
2943 #if defined(HAS_ACCESS) && defined(X_OK)
2944 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2945 result = access(POPpx, X_OK);
2960 if (cando(S_IXUSR, 0, &PL_statcache))
2969 STACKED_FTEST_CHECK;
2970 #ifdef PERL_EFF_ACCESS_R_OK
2971 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2972 result = PERL_EFF_ACCESS_R_OK(POPpx);
2987 if (cando(S_IRUSR, 1, &PL_statcache))
2996 STACKED_FTEST_CHECK;
2997 #ifdef PERL_EFF_ACCESS_W_OK
2998 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
2999 result = PERL_EFF_ACCESS_W_OK(POPpx);
3014 if (cando(S_IWUSR, 1, &PL_statcache))
3023 STACKED_FTEST_CHECK;
3024 #ifdef PERL_EFF_ACCESS_X_OK
3025 if ((PL_op->op_private & OPpFT_ACCESS) && SvPOK(TOPs)) {
3026 result = PERL_EFF_ACCESS_X_OK(POPpx);
3041 if (cando(S_IXUSR, 1, &PL_statcache))
3050 STACKED_FTEST_CHECK;
3062 STACKED_FTEST_CHECK;
3067 if (PL_statcache.st_uid == (PL_op->op_type == OP_FTEOWNED ?
3077 STACKED_FTEST_CHECK;
3082 if (PL_statcache.st_size == 0)
3091 STACKED_FTEST_CHECK;
3096 #if Off_t_size > IVSIZE
3097 PUSHn(PL_statcache.st_size);
3099 PUSHi(PL_statcache.st_size);
3108 STACKED_FTEST_CHECK;
3113 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
3121 STACKED_FTEST_CHECK;
3126 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
3134 STACKED_FTEST_CHECK;
3139 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3147 STACKED_FTEST_CHECK;
3152 if (S_ISSOCK(PL_statcache.st_mode))
3161 STACKED_FTEST_CHECK;
3166 if (S_ISCHR(PL_statcache.st_mode))
3175 STACKED_FTEST_CHECK;
3180 if (S_ISBLK(PL_statcache.st_mode))
3189 STACKED_FTEST_CHECK;
3194 if (S_ISREG(PL_statcache.st_mode))
3203 STACKED_FTEST_CHECK;
3208 if (S_ISDIR(PL_statcache.st_mode))
3217 STACKED_FTEST_CHECK;
3222 if (S_ISFIFO(PL_statcache.st_mode))
3229 I32 result = my_lstat();
3233 if (S_ISLNK(PL_statcache.st_mode))
3243 STACKED_FTEST_CHECK;
3248 if (PL_statcache.st_mode & S_ISUID)
3259 STACKED_FTEST_CHECK;
3264 if (PL_statcache.st_mode & S_ISGID)
3275 STACKED_FTEST_CHECK;
3280 if (PL_statcache.st_mode & S_ISVTX)
3293 STACKED_FTEST_CHECK;
3295 if (PL_op->op_flags & OPf_REF)
3297 else if (isGV(TOPs))
3299 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3300 gv = (GV*)SvRV(POPs);
3302 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3304 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3305 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3306 else if (tmpsv && SvOK(tmpsv)) {
3307 const char *tmps = SvPV_nolen_const(tmpsv);
3315 if (PerlLIO_isatty(fd))
3320 #if defined(atarist) /* this will work with atariST. Configure will
3321 make guesses for other systems. */
3322 # define FILE_base(f) ((f)->_base)
3323 # define FILE_ptr(f) ((f)->_ptr)
3324 # define FILE_cnt(f) ((f)->_cnt)
3325 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3335 register STDCHAR *s;
3341 STACKED_FTEST_CHECK;
3343 if (PL_op->op_flags & OPf_REF)
3345 else if (isGV(TOPs))
3347 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3348 gv = (GV*)SvRV(POPs);
3354 if (gv == PL_defgv) {
3356 io = GvIO(PL_statgv);
3359 goto really_filename;
3364 PL_laststatval = -1;
3365 sv_setpvn(PL_statname, "", 0);
3366 io = GvIO(PL_statgv);
3368 if (io && IoIFP(io)) {
3369 if (! PerlIO_has_base(IoIFP(io)))
3370 DIE(aTHX_ "-T and -B not implemented on filehandles");
3371 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3372 if (PL_laststatval < 0)
3374 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3375 if (PL_op->op_type == OP_FTTEXT)
3380 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3381 i = PerlIO_getc(IoIFP(io));
3383 (void)PerlIO_ungetc(IoIFP(io),i);
3385 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3387 len = PerlIO_get_bufsiz(IoIFP(io));
3388 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3389 /* sfio can have large buffers - limit to 512 */
3394 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3396 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3398 SETERRNO(EBADF,RMS_IFI);
3406 PL_laststype = OP_STAT;
3407 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3408 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3409 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3411 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3414 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3415 if (PL_laststatval < 0) {
3416 (void)PerlIO_close(fp);
3419 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3420 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3421 (void)PerlIO_close(fp);
3423 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3424 RETPUSHNO; /* special case NFS directories */
3425 RETPUSHYES; /* null file is anything */
3430 /* now scan s to look for textiness */
3431 /* XXX ASCII dependent code */
3433 #if defined(DOSISH) || defined(USEMYBINMODE)
3434 /* ignore trailing ^Z on short files */
3435 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3439 for (i = 0; i < len; i++, s++) {
3440 if (!*s) { /* null never allowed in text */
3445 else if (!(isPRINT(*s) || isSPACE(*s)))
3448 else if (*s & 128) {
3450 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3453 /* utf8 characters don't count as odd */
3454 if (UTF8_IS_START(*s)) {
3455 int ulen = UTF8SKIP(s);
3456 if (ulen < len - i) {
3458 for (j = 1; j < ulen; j++) {
3459 if (!UTF8_IS_CONTINUATION(s[j]))
3462 --ulen; /* loop does extra increment */
3472 *s != '\n' && *s != '\r' && *s != '\b' &&
3473 *s != '\t' && *s != '\f' && *s != 27)
3478 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3489 const char *tmps = 0;
3493 SV * const sv = POPs;
3494 if (SvTYPE(sv) == SVt_PVGV) {
3497 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3501 tmps = SvPVx_nolen_const(sv);
3505 if( !gv && (!tmps || !*tmps) ) {
3506 HV * const table = GvHVn(PL_envgv);
3509 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3510 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3512 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3517 deprecate("chdir('') or chdir(undef) as chdir()");
3518 tmps = SvPV_nolen_const(*svp);
3522 TAINT_PROPER("chdir");
3527 TAINT_PROPER("chdir");
3530 IO* const io = GvIO(gv);
3533 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3535 else if (IoDIRP(io)) {
3537 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3539 DIE(aTHX_ PL_no_func, "dirfd");
3550 DIE(aTHX_ PL_no_func, "fchdir");
3554 PUSHi( PerlDir_chdir(tmps) >= 0 );
3556 /* Clear the DEFAULT element of ENV so we'll get the new value
3558 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3566 dSP; dMARK; dTARGET;
3567 I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3573 DIE(aTHX_ PL_no_func, "chown");
3582 TAINT_PROPER("chroot");
3583 PUSHi( chroot(tmps) >= 0 );
3586 DIE(aTHX_ PL_no_func, "chroot");
3592 dSP; dMARK; dTARGET;
3594 value = (I32)apply(PL_op->op_type, MARK, SP);
3602 dSP; dMARK; dTARGET;
3604 value = (I32)apply(PL_op->op_type, MARK, SP);
3612 dSP; dMARK; dTARGET;
3614 value = (I32)apply(PL_op->op_type, MARK, SP);
3624 const char *tmps2 = POPpconstx;
3625 const char *tmps = SvPV_nolen_const(TOPs);
3626 TAINT_PROPER("rename");
3628 anum = PerlLIO_rename(tmps, tmps2);
3630 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3631 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3634 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3635 (void)UNLINK(tmps2);
3636 if (!(anum = link(tmps, tmps2)))
3637 anum = UNLINK(tmps);
3649 const char *tmps2 = POPpconstx;
3650 const char *tmps = SvPV_nolen_const(TOPs);
3651 TAINT_PROPER("link");
3652 SETi( PerlLIO_link(tmps, tmps2) >= 0 );
3655 DIE(aTHX_ PL_no_func, "link");
3663 const char *tmps2 = POPpconstx;
3664 const char *tmps = SvPV_nolen_const(TOPs);
3665 TAINT_PROPER("symlink");
3666 SETi( symlink(tmps, tmps2) >= 0 );
3669 DIE(aTHX_ PL_no_func, "symlink");
3679 char buf[MAXPATHLEN];
3682 #ifndef INCOMPLETE_TAINTS
3686 len = readlink(tmps, buf, sizeof(buf) - 1);
3694 RETSETUNDEF; /* just pretend it's a normal file */
3698 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3700 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3702 char * const save_filename = filename;
3708 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3709 strcpy(cmdline, cmd);
3710 strcat(cmdline, " ");
3711 for (s = cmdline + strlen(cmdline); *filename; ) {
3716 myfp = PerlProc_popen(cmdline, "r");
3720 SV *tmpsv = sv_newmortal();
3721 /* Need to save/restore 'PL_rs' ?? */
3722 s = sv_gets(tmpsv, myfp, 0);
3723 (void)PerlProc_pclose(myfp);
3727 #ifdef HAS_SYS_ERRLIST
3732 /* you don't see this */
3734 #ifdef HAS_SYS_ERRLIST
3742 if (instr(s, errmsg)) {
3749 #define EACCES EPERM
3751 if (instr(s, "cannot make"))
3752 SETERRNO(EEXIST,RMS_FEX);
3753 else if (instr(s, "existing file"))
3754 SETERRNO(EEXIST,RMS_FEX);
3755 else if (instr(s, "ile exists"))
3756 SETERRNO(EEXIST,RMS_FEX);
3757 else if (instr(s, "non-exist"))
3758 SETERRNO(ENOENT,RMS_FNF);
3759 else if (instr(s, "does not exist"))
3760 SETERRNO(ENOENT,RMS_FNF);
3761 else if (instr(s, "not empty"))
3762 SETERRNO(EBUSY,SS_DEVOFFLINE);
3763 else if (instr(s, "cannot access"))
3764 SETERRNO(EACCES,RMS_PRV);
3766 SETERRNO(EPERM,RMS_PRV);
3769 else { /* some mkdirs return no failure indication */
3770 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3771 if (PL_op->op_type == OP_RMDIR)
3776 SETERRNO(EACCES,RMS_PRV); /* a guess */
3785 /* This macro removes trailing slashes from a directory name.
3786 * Different operating and file systems take differently to
3787 * trailing slashes. According to POSIX 1003.1 1996 Edition
3788 * any number of trailing slashes should be allowed.
3789 * Thusly we snip them away so that even non-conforming
3790 * systems are happy.
3791 * We should probably do this "filtering" for all
3792 * the functions that expect (potentially) directory names:
3793 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3794 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3796 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3797 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3800 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3801 (tmps) = savepvn((tmps), (len)); \
3821 TRIMSLASHES(tmps,len,copy);
3823 TAINT_PROPER("mkdir");
3825 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3827 SETi( dooneliner("mkdir", tmps) );
3828 oldumask = PerlLIO_umask(0);
3829 PerlLIO_umask(oldumask);
3830 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3844 TRIMSLASHES(tmps,len,copy);
3845 TAINT_PROPER("rmdir");
3847 SETi( PerlDir_rmdir(tmps) >= 0 );
3849 SETi( dooneliner("rmdir", tmps) );
3856 /* Directory calls. */
3860 #if defined(Direntry_t) && defined(HAS_READDIR)
3862 const char *dirname = POPpconstx;
3864 register IO *io = GvIOn(gv);
3870 PerlDir_close(IoDIRP(io));
3871 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3877 SETERRNO(EBADF,RMS_DIR);
3880 DIE(aTHX_ PL_no_dir_func, "opendir");
3886 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3887 DIE(aTHX_ PL_no_dir_func, "readdir");
3889 #if !defined(I_DIRENT) && !defined(VMS)
3890 Direntry_t *readdir (DIR *);
3895 const I32 gimme = GIMME;
3896 GV *gv = (GV *)POPs;
3897 register Direntry_t *dp;
3898 register IO *io = GvIOn(gv);
3900 if (!io || !IoDIRP(io))
3904 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3908 sv = newSVpvn(dp->d_name, dp->d_namlen);
3910 sv = newSVpv(dp->d_name, 0);
3912 #ifndef INCOMPLETE_TAINTS
3913 if (!(IoFLAGS(io) & IOf_UNTAINT))
3916 XPUSHs(sv_2mortal(sv));
3918 while (gimme == G_ARRAY);
3920 if (!dp && gimme != G_ARRAY)
3927 SETERRNO(EBADF,RMS_ISI);
3928 if (GIMME == G_ARRAY)
3937 #if defined(HAS_TELLDIR) || defined(telldir)
3939 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3940 /* XXX netbsd still seemed to.
3941 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3942 --JHI 1999-Feb-02 */
3943 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3944 long telldir (DIR *);
3947 register IO *io = GvIOn(gv);
3949 if (!io || !IoDIRP(io))
3952 PUSHi( PerlDir_tell(IoDIRP(io)) );
3956 SETERRNO(EBADF,RMS_ISI);
3959 DIE(aTHX_ PL_no_dir_func, "telldir");
3965 #if defined(HAS_SEEKDIR) || defined(seekdir)
3969 register IO *io = GvIOn(gv);
3971 if (!io || !IoDIRP(io))
3974 (void)PerlDir_seek(IoDIRP(io), along);
3979 SETERRNO(EBADF,RMS_ISI);
3982 DIE(aTHX_ PL_no_dir_func, "seekdir");
3988 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3991 register IO *io = GvIOn(gv);
3993 if (!io || !IoDIRP(io))
3996 (void)PerlDir_rewind(IoDIRP(io));
4000 SETERRNO(EBADF,RMS_ISI);
4003 DIE(aTHX_ PL_no_dir_func, "rewinddir");
4009 #if defined(Direntry_t) && defined(HAS_READDIR)
4012 register IO *io = GvIOn(gv);
4014 if (!io || !IoDIRP(io))
4017 #ifdef VOID_CLOSEDIR
4018 PerlDir_close(IoDIRP(io));
4020 if (PerlDir_close(IoDIRP(io)) < 0) {
4021 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
4030 SETERRNO(EBADF,RMS_IFI);
4033 DIE(aTHX_ PL_no_dir_func, "closedir");
4037 /* Process control. */
4047 PERL_FLUSHALL_FOR_CHILD;
4048 childpid = PerlProc_fork();
4052 if ((tmpgv = gv_fetchpv("$", TRUE, SVt_PV))) {
4053 SvREADONLY_off(GvSV(tmpgv));
4054 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
4055 SvREADONLY_on(GvSV(tmpgv));
4057 #ifdef THREADS_HAVE_PIDS
4058 PL_ppid = (IV)getppid();
4060 #ifdef PERL_USES_PL_PIDSTATUS
4061 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
4067 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4072 PERL_FLUSHALL_FOR_CHILD;
4073 childpid = PerlProc_fork();
4079 DIE(aTHX_ PL_no_func, "fork");
4086 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4091 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4092 childpid = wait4pid(-1, &argflags, 0);
4094 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
4099 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
4100 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
4101 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
4103 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
4108 DIE(aTHX_ PL_no_func, "wait");
4114 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
4123 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
4124 result = wait4pid(pid, &argflags, optype);
4126 while ((result = wait4pid(pid, &argflags, optype)) == -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_CHILD_SET((result && result != -1) ? argflags : -1);
4135 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
4140 DIE(aTHX_ PL_no_func, "waitpid");
4146 dSP; dMARK; dORIGMARK; dTARGET;
4152 while (++MARK <= SP) {
4153 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4158 TAINT_PROPER("system");
4160 PERL_FLUSHALL_FOR_CHILD;
4161 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
4167 if (PerlProc_pipe(pp) >= 0)
4169 while ((childpid = PerlProc_fork()) == -1) {
4170 if (errno != EAGAIN) {
4175 PerlLIO_close(pp[0]);
4176 PerlLIO_close(pp[1]);
4183 Sigsave_t ihand,qhand; /* place to save signals during system() */
4187 PerlLIO_close(pp[1]);
4189 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
4190 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
4193 result = wait4pid(childpid, &status, 0);
4194 } while (result == -1 && errno == EINTR);
4196 (void)rsignal_restore(SIGINT, &ihand);
4197 (void)rsignal_restore(SIGQUIT, &qhand);
4199 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
4200 do_execfree(); /* free any memory child malloced on fork */
4206 while (n < sizeof(int)) {
4207 n1 = PerlLIO_read(pp[0],
4208 (void*)(((char*)&errkid)+n),
4214 PerlLIO_close(pp[0]);
4215 if (n) { /* Error */
4216 if (n != sizeof(int))
4217 DIE(aTHX_ "panic: kid popen errno read");
4218 errno = errkid; /* Propagate errno from kid */
4219 STATUS_NATIVE_CHILD_SET(-1);
4222 PUSHi(STATUS_CURRENT);
4226 PerlLIO_close(pp[0]);
4227 #if defined(HAS_FCNTL) && defined(F_SETFD)
4228 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4231 if (PL_op->op_flags & OPf_STACKED) {
4232 SV *really = *++MARK;
4233 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4235 else if (SP - MARK != 1)
4236 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4238 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4242 #else /* ! FORK or VMS or OS/2 */
4245 if (PL_op->op_flags & OPf_STACKED) {
4246 SV *really = *++MARK;
4247 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4248 value = (I32)do_aspawn(really, MARK, SP);
4250 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4253 else if (SP - MARK != 1) {
4254 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4255 value = (I32)do_aspawn(Nullsv, MARK, SP);
4257 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4261 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4263 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4265 STATUS_NATIVE_CHILD_SET(value);
4268 PUSHi(result ? value : STATUS_CURRENT);
4269 #endif /* !FORK or VMS */
4275 dSP; dMARK; dORIGMARK; dTARGET;
4280 while (++MARK <= SP) {
4281 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4286 TAINT_PROPER("exec");
4288 PERL_FLUSHALL_FOR_CHILD;
4289 if (PL_op->op_flags & OPf_STACKED) {
4290 SV *really = *++MARK;
4291 value = (I32)do_aexec(really, MARK, SP);
4293 else if (SP - MARK != 1)
4295 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4299 (void ) do_aspawn(Nullsv, MARK, SP);
4303 value = (I32)do_aexec(Nullsv, MARK, SP);
4308 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4311 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4314 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4327 dSP; dMARK; dTARGET;
4329 value = (I32)apply(PL_op->op_type, MARK, SP);
4334 DIE(aTHX_ PL_no_func, "kill");
4342 # ifdef THREADS_HAVE_PIDS
4343 if (PL_ppid != 1 && getppid() == 1)
4344 /* maybe the parent process has died. Refresh ppid cache */
4348 XPUSHi( getppid() );
4352 DIE(aTHX_ PL_no_func, "getppid");
4368 pgrp = (I32)BSD_GETPGRP(pid);
4370 if (pid != 0 && pid != PerlProc_getpid())
4371 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4377 DIE(aTHX_ PL_no_func, "getpgrp()");
4396 TAINT_PROPER("setpgrp");
4398 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4400 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4401 || (pid != 0 && pid != PerlProc_getpid()))
4403 DIE(aTHX_ "setpgrp can't take arguments");
4405 SETi( setpgrp() >= 0 );
4406 #endif /* USE_BSDPGRP */
4409 DIE(aTHX_ PL_no_func, "setpgrp()");
4415 #ifdef HAS_GETPRIORITY
4419 SETi( getpriority(which, who) );
4422 DIE(aTHX_ PL_no_func, "getpriority()");
4428 #ifdef HAS_SETPRIORITY
4433 TAINT_PROPER("setpriority");
4434 SETi( setpriority(which, who, niceval) >= 0 );
4437 DIE(aTHX_ PL_no_func, "setpriority()");
4447 XPUSHn( time(Null(Time_t*)) );
4449 XPUSHi( time(Null(Time_t*)) );
4460 (void)PerlProc_times(&PL_timesbuf);
4462 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4463 /* struct tms, though same data */
4467 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4468 if (GIMME == G_ARRAY) {
4469 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4470 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4471 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4477 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4479 if (GIMME == G_ARRAY) {
4480 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4481 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4482 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4486 DIE(aTHX_ "times not implemented");
4488 #endif /* HAS_TIMES */
4491 #ifdef LOCALTIME_EDGECASE_BROKEN
4492 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4497 /* No workarounds in the valid range */
4498 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4499 return (localtime (tp));
4501 /* This edge case is to workaround the undefined behaviour, where the
4502 * TIMEZONE makes the time go beyond the defined range.
4503 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4504 * If there is a negative offset in TZ, like MET-1METDST, some broken
4505 * implementations of localtime () (like AIX 5.2) barf with bogus
4507 * 0x7fffffff gmtime 2038-01-19 03:14:07
4508 * 0x7fffffff localtime 1901-12-13 21:45:51
4509 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4510 * 0x3c19137f gmtime 2001-12-13 20:45:51
4511 * 0x3c19137f localtime 2001-12-13 21:45:51
4512 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4513 * Given that legal timezones are typically between GMT-12 and GMT+12
4514 * we turn back the clock 23 hours before calling the localtime
4515 * function, and add those to the return value. This will never cause
4516 * day wrapping problems, since the edge case is Tue Jan *19*
4518 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4521 if (P->tm_hour >= 24) {
4523 P->tm_mday++; /* 18 -> 19 */
4524 P->tm_wday++; /* Mon -> Tue */
4525 P->tm_yday++; /* 18 -> 19 */
4528 } /* S_my_localtime */
4535 const struct tm *tmbuf;
4536 static const char * const dayname[] =
4537 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4538 static const char * const monname[] =
4539 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4540 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4546 when = (Time_t)SvNVx(POPs);
4548 when = (Time_t)SvIVx(POPs);
4551 if (PL_op->op_type == OP_LOCALTIME)
4552 #ifdef LOCALTIME_EDGECASE_BROKEN
4553 tmbuf = S_my_localtime(aTHX_ &when);
4555 tmbuf = localtime(&when);
4558 tmbuf = gmtime(&when);
4560 if (GIMME != G_ARRAY) {
4566 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4567 dayname[tmbuf->tm_wday],
4568 monname[tmbuf->tm_mon],
4573 tmbuf->tm_year + 1900);
4574 PUSHs(sv_2mortal(tsv));
4579 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4580 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4581 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4582 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4583 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4584 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4585 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4586 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4587 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4598 anum = alarm((unsigned int)anum);
4605 DIE(aTHX_ PL_no_func, "alarm");
4616 (void)time(&lasttime);
4621 PerlProc_sleep((unsigned int)duration);
4624 XPUSHi(when - lasttime);
4628 /* Shared memory. */
4632 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4633 dSP; dMARK; dTARGET;
4634 I32 value = (I32)(do_shmio(PL_op->op_type, MARK, SP) >= 0);
4643 /* Message passing. */
4647 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4648 dSP; dMARK; dTARGET;
4649 I32 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4660 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4661 dSP; dMARK; dTARGET;
4662 I32 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4675 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4676 dSP; dMARK; dTARGET;
4677 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4684 DIE(aTHX_ "System V IPC is not implemented on this machine");
4690 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4691 dSP; dMARK; dTARGET;
4692 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4700 PUSHp(zero_but_true, ZBTLEN);
4710 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4711 dSP; dMARK; dTARGET;
4712 I32 value = (I32)(do_semop(MARK, SP) >= 0);
4721 /* Get system info. */
4725 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4727 I32 which = PL_op->op_type;
4728 register char **elem;
4730 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4731 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4732 struct hostent *gethostbyname(Netdb_name_t);
4733 struct hostent *gethostent(void);
4735 struct hostent *hent;
4739 if (which == OP_GHBYNAME) {
4740 #ifdef HAS_GETHOSTBYNAME
4741 char* name = POPpbytex;
4742 hent = PerlSock_gethostbyname(name);
4744 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4747 else if (which == OP_GHBYADDR) {
4748 #ifdef HAS_GETHOSTBYADDR
4749 int addrtype = POPi;
4752 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4754 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4756 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4760 #ifdef HAS_GETHOSTENT
4761 hent = PerlSock_gethostent();
4763 DIE(aTHX_ PL_no_sock_func, "gethostent");
4766 #ifdef HOST_NOT_FOUND
4768 #ifdef USE_REENTRANT_API
4769 # ifdef USE_GETHOSTENT_ERRNO
4770 h_errno = PL_reentrant_buffer->_gethostent_errno;
4773 STATUS_UNIX_SET(h_errno);
4777 if (GIMME != G_ARRAY) {
4778 PUSHs(sv = sv_newmortal());
4780 if (which == OP_GHBYNAME) {
4782 sv_setpvn(sv, hent->h_addr, hent->h_length);
4785 sv_setpv(sv, (char*)hent->h_name);
4791 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4792 sv_setpv(sv, (char*)hent->h_name);
4793 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4794 for (elem = hent->h_aliases; elem && *elem; elem++) {
4795 sv_catpv(sv, *elem);
4797 sv_catpvn(sv, " ", 1);
4799 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4800 sv_setiv(sv, (IV)hent->h_addrtype);
4801 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4802 len = hent->h_length;
4803 sv_setiv(sv, (IV)len);
4805 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4806 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4807 sv_setpvn(sv, *elem, len);
4810 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4812 sv_setpvn(sv, hent->h_addr, len);
4817 DIE(aTHX_ PL_no_sock_func, "gethostent");
4823 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4825 I32 which = PL_op->op_type;
4826 register char **elem;
4828 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4829 struct netent *getnetbyaddr(Netdb_net_t, int);
4830 struct netent *getnetbyname(Netdb_name_t);
4831 struct netent *getnetent(void);
4833 struct netent *nent;
4835 if (which == OP_GNBYNAME){
4836 #ifdef HAS_GETNETBYNAME
4837 char *name = POPpbytex;
4838 nent = PerlSock_getnetbyname(name);
4840 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4843 else if (which == OP_GNBYADDR) {
4844 #ifdef HAS_GETNETBYADDR
4845 int addrtype = POPi;
4846 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4847 nent = PerlSock_getnetbyaddr(addr, addrtype);
4849 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4853 #ifdef HAS_GETNETENT
4854 nent = PerlSock_getnetent();
4856 DIE(aTHX_ PL_no_sock_func, "getnetent");
4859 #ifdef HOST_NOT_FOUND
4861 #ifdef USE_REENTRANT_API
4862 # ifdef USE_GETNETENT_ERRNO
4863 h_errno = PL_reentrant_buffer->_getnetent_errno;
4866 STATUS_UNIX_SET(h_errno);
4871 if (GIMME != G_ARRAY) {
4872 PUSHs(sv = sv_newmortal());
4874 if (which == OP_GNBYNAME)
4875 sv_setiv(sv, (IV)nent->n_net);
4877 sv_setpv(sv, nent->n_name);
4883 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4884 sv_setpv(sv, nent->n_name);
4885 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4886 for (elem = nent->n_aliases; elem && *elem; elem++) {
4887 sv_catpv(sv, *elem);
4889 sv_catpvn(sv, " ", 1);
4891 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4892 sv_setiv(sv, (IV)nent->n_addrtype);
4893 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4894 sv_setiv(sv, (IV)nent->n_net);
4899 DIE(aTHX_ PL_no_sock_func, "getnetent");
4905 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4907 I32 which = PL_op->op_type;
4908 register char **elem;
4910 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4911 struct protoent *getprotobyname(Netdb_name_t);
4912 struct protoent *getprotobynumber(int);
4913 struct protoent *getprotoent(void);
4915 struct protoent *pent;
4917 if (which == OP_GPBYNAME) {
4918 #ifdef HAS_GETPROTOBYNAME
4919 char* name = POPpbytex;
4920 pent = PerlSock_getprotobyname(name);
4922 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4925 else if (which == OP_GPBYNUMBER) {
4926 #ifdef HAS_GETPROTOBYNUMBER
4928 pent = PerlSock_getprotobynumber(number);
4930 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4934 #ifdef HAS_GETPROTOENT
4935 pent = PerlSock_getprotoent();
4937 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4941 if (GIMME != G_ARRAY) {
4942 PUSHs(sv = sv_newmortal());
4944 if (which == OP_GPBYNAME)
4945 sv_setiv(sv, (IV)pent->p_proto);
4947 sv_setpv(sv, pent->p_name);
4953 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4954 sv_setpv(sv, pent->p_name);
4955 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4956 for (elem = pent->p_aliases; elem && *elem; elem++) {
4957 sv_catpv(sv, *elem);
4959 sv_catpvn(sv, " ", 1);
4961 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4962 sv_setiv(sv, (IV)pent->p_proto);
4967 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4973 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4975 I32 which = PL_op->op_type;
4976 register char **elem;
4978 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4979 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4980 struct servent *getservbyport(int, Netdb_name_t);
4981 struct servent *getservent(void);
4983 struct servent *sent;
4985 if (which == OP_GSBYNAME) {
4986 #ifdef HAS_GETSERVBYNAME
4987 char *proto = POPpbytex;
4988 char *name = POPpbytex;
4990 if (proto && !*proto)
4993 sent = PerlSock_getservbyname(name, proto);
4995 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4998 else if (which == OP_GSBYPORT) {
4999 #ifdef HAS_GETSERVBYPORT
5000 char *proto = POPpbytex;
5001 unsigned short port = (unsigned short)POPu;
5003 if (proto && !*proto)
5007 port = PerlSock_htons(port);
5009 sent = PerlSock_getservbyport(port, proto);
5011 DIE(aTHX_ PL_no_sock_func, "getservbyport");
5015 #ifdef HAS_GETSERVENT
5016 sent = PerlSock_getservent();
5018 DIE(aTHX_ PL_no_sock_func, "getservent");
5022 if (GIMME != G_ARRAY) {
5023 PUSHs(sv = sv_newmortal());
5025 if (which == OP_GSBYNAME) {
5027 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5029 sv_setiv(sv, (IV)(sent->s_port));
5033 sv_setpv(sv, sent->s_name);
5039 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5040 sv_setpv(sv, sent->s_name);
5041 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5042 for (elem = sent->s_aliases; elem && *elem; elem++) {
5043 sv_catpv(sv, *elem);
5045 sv_catpvn(sv, " ", 1);
5047 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5049 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
5051 sv_setiv(sv, (IV)(sent->s_port));
5053 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5054 sv_setpv(sv, sent->s_proto);
5059 DIE(aTHX_ PL_no_sock_func, "getservent");
5065 #ifdef HAS_SETHOSTENT
5067 PerlSock_sethostent(TOPi);
5070 DIE(aTHX_ PL_no_sock_func, "sethostent");
5076 #ifdef HAS_SETNETENT
5078 PerlSock_setnetent(TOPi);
5081 DIE(aTHX_ PL_no_sock_func, "setnetent");
5087 #ifdef HAS_SETPROTOENT
5089 PerlSock_setprotoent(TOPi);
5092 DIE(aTHX_ PL_no_sock_func, "setprotoent");
5098 #ifdef HAS_SETSERVENT
5100 PerlSock_setservent(TOPi);
5103 DIE(aTHX_ PL_no_sock_func, "setservent");
5109 #ifdef HAS_ENDHOSTENT
5111 PerlSock_endhostent();
5115 DIE(aTHX_ PL_no_sock_func, "endhostent");
5121 #ifdef HAS_ENDNETENT
5123 PerlSock_endnetent();
5127 DIE(aTHX_ PL_no_sock_func, "endnetent");
5133 #ifdef HAS_ENDPROTOENT
5135 PerlSock_endprotoent();
5139 DIE(aTHX_ PL_no_sock_func, "endprotoent");
5145 #ifdef HAS_ENDSERVENT
5147 PerlSock_endservent();
5151 DIE(aTHX_ PL_no_sock_func, "endservent");
5159 I32 which = PL_op->op_type;
5161 struct passwd *pwent = NULL;
5163 * We currently support only the SysV getsp* shadow password interface.
5164 * The interface is declared in <shadow.h> and often one needs to link
5165 * with -lsecurity or some such.
5166 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
5169 * AIX getpwnam() is clever enough to return the encrypted password
5170 * only if the caller (euid?) is root.
5172 * There are at least three other shadow password APIs. Many platforms
5173 * seem to contain more than one interface for accessing the shadow
5174 * password databases, possibly for compatibility reasons.
5175 * The getsp*() is by far he simplest one, the other two interfaces
5176 * are much more complicated, but also very similar to each other.
5181 * struct pr_passwd *getprpw*();
5182 * The password is in
5183 * char getprpw*(...).ufld.fd_encrypt[]
5184 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
5189 * struct es_passwd *getespw*();
5190 * The password is in
5191 * char *(getespw*(...).ufld.fd_encrypt)
5192 * Mention HAS_GETESPWNAM here so that Configure probes for it.
5195 * struct userpw *getuserpw();
5196 * The password is in
5197 * char *(getuserpw(...)).spw_upw_passwd
5198 * (but the de facto standard getpwnam() should work okay)
5200 * Mention I_PROT here so that Configure probes for it.
5202 * In HP-UX for getprpw*() the manual page claims that one should include
5203 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
5204 * if one includes <shadow.h> as that includes <hpsecurity.h>,
5205 * and pp_sys.c already includes <shadow.h> if there is such.
5207 * Note that <sys/security.h> is already probed for, but currently
5208 * it is only included in special cases.
5210 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
5211 * be preferred interface, even though also the getprpw*() interface
5212 * is available) one needs to link with -lsecurity -ldb -laud -lm.
5213 * One also needs to call set_auth_parameters() in main() before
5214 * doing anything else, whether one is using getespw*() or getprpw*().
5216 * Note that accessing the shadow databases can be magnitudes
5217 * slower than accessing the standard databases.
5222 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
5223 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
5224 * the pw_comment is left uninitialized. */
5225 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
5231 char* name = POPpbytex;
5232 pwent = getpwnam(name);
5238 pwent = getpwuid(uid);
5242 # ifdef HAS_GETPWENT
5244 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
5245 if (pwent) pwent = getpwnam(pwent->pw_name);
5248 DIE(aTHX_ PL_no_func, "getpwent");
5254 if (GIMME != G_ARRAY) {
5255 PUSHs(sv = sv_newmortal());
5257 if (which == OP_GPWNAM)
5258 # if Uid_t_sign <= 0
5259 sv_setiv(sv, (IV)pwent->pw_uid);
5261 sv_setuv(sv, (UV)pwent->pw_uid);
5264 sv_setpv(sv, pwent->pw_name);
5270 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5271 sv_setpv(sv, pwent->pw_name);
5273 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5275 /* If we have getspnam(), we try to dig up the shadow
5276 * password. If we are underprivileged, the shadow
5277 * interface will set the errno to EACCES or similar,
5278 * and return a null pointer. If this happens, we will
5279 * use the dummy password (usually "*" or "x") from the
5280 * standard password database.
5282 * In theory we could skip the shadow call completely
5283 * if euid != 0 but in practice we cannot know which
5284 * security measures are guarding the shadow databases
5285 * on a random platform.
5287 * Resist the urge to use additional shadow interfaces.
5288 * Divert the urge to writing an extension instead.
5291 /* Some AIX setups falsely(?) detect some getspnam(), which
5292 * has a different API than the Solaris/IRIX one. */
5293 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5295 struct spwd *spwent;
5296 int saverrno; /* Save and restore errno so that
5297 * underprivileged attempts seem
5298 * to have never made the unsccessful
5299 * attempt to retrieve the shadow password. */
5302 spwent = getspnam(pwent->pw_name);
5304 if (spwent && spwent->sp_pwdp)
5305 sv_setpv(sv, spwent->sp_pwdp);
5309 if (!SvPOK(sv)) /* Use the standard password, then. */
5310 sv_setpv(sv, pwent->pw_passwd);
5313 # ifndef INCOMPLETE_TAINTS
5314 /* passwd is tainted because user himself can diddle with it.
5315 * admittedly not much and in a very limited way, but nevertheless. */
5319 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5320 # if Uid_t_sign <= 0
5321 sv_setiv(sv, (IV)pwent->pw_uid);
5323 sv_setuv(sv, (UV)pwent->pw_uid);
5326 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5327 # if Uid_t_sign <= 0
5328 sv_setiv(sv, (IV)pwent->pw_gid);
5330 sv_setuv(sv, (UV)pwent->pw_gid);
5332 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5333 * because of the poor interface of the Perl getpw*(),
5334 * not because there's some standard/convention saying so.
5335 * A better interface would have been to return a hash,
5336 * but we are accursed by our history, alas. --jhi. */
5337 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5339 sv_setiv(sv, (IV)pwent->pw_change);
5342 sv_setiv(sv, (IV)pwent->pw_quota);
5345 sv_setpv(sv, pwent->pw_age);
5350 /* pw_class and pw_comment are mutually exclusive--.
5351 * see the above note for pw_change, pw_quota, and pw_age. */
5352 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5354 sv_setpv(sv, pwent->pw_class);
5357 sv_setpv(sv, pwent->pw_comment);
5361 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5363 sv_setpv(sv, pwent->pw_gecos);
5365 # ifndef INCOMPLETE_TAINTS
5366 /* pw_gecos is tainted because user himself can diddle with it. */
5370 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5371 sv_setpv(sv, pwent->pw_dir);
5373 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5374 sv_setpv(sv, pwent->pw_shell);
5375 # ifndef INCOMPLETE_TAINTS
5376 /* pw_shell is tainted because user himself can diddle with it. */
5381 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5382 sv_setiv(sv, (IV)pwent->pw_expire);
5387 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5393 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5398 DIE(aTHX_ PL_no_func, "setpwent");
5404 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5409 DIE(aTHX_ PL_no_func, "endpwent");
5417 I32 which = PL_op->op_type;
5418 register char **elem;
5420 struct group *grent;
5422 if (which == OP_GGRNAM) {
5423 char* name = POPpbytex;
5424 grent = (struct group *)getgrnam(name);
5426 else if (which == OP_GGRGID) {
5428 grent = (struct group *)getgrgid(gid);
5432 grent = (struct group *)getgrent();
5434 DIE(aTHX_ PL_no_func, "getgrent");
5438 if (GIMME != G_ARRAY) {
5439 PUSHs(sv = sv_newmortal());
5441 if (which == OP_GGRNAM)
5442 sv_setiv(sv, (IV)grent->gr_gid);
5444 sv_setpv(sv, grent->gr_name);
5450 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5451 sv_setpv(sv, grent->gr_name);
5453 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5455 sv_setpv(sv, grent->gr_passwd);
5458 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5459 sv_setiv(sv, (IV)grent->gr_gid);
5461 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5462 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5463 /* In UNICOS/mk (_CRAYMPP) the multithreading
5464 * versions (getgrnam_r, getgrgid_r)
5465 * seem to return an illegal pointer
5466 * as the group members list, gr_mem.
5467 * getgrent() doesn't even have a _r version
5468 * but the gr_mem is poisonous anyway.
5469 * So yes, you cannot get the list of group
5470 * members if building multithreaded in UNICOS/mk. */
5471 for (elem = grent->gr_mem; elem && *elem; elem++) {
5472 sv_catpv(sv, *elem);
5474 sv_catpvn(sv, " ", 1);
5481 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5487 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5492 DIE(aTHX_ PL_no_func, "setgrent");
5498 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5503 DIE(aTHX_ PL_no_func, "endgrent");
5513 if (!(tmps = PerlProc_getlogin()))
5515 PUSHp(tmps, strlen(tmps));
5518 DIE(aTHX_ PL_no_func, "getlogin");
5522 /* Miscellaneous. */
5527 dSP; dMARK; dORIGMARK; dTARGET;
5528 register I32 items = SP - MARK;
5529 unsigned long a[20];
5534 while (++MARK <= SP) {
5535 if (SvTAINTED(*MARK)) {
5541 TAINT_PROPER("syscall");
5544 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5545 * or where sizeof(long) != sizeof(char*). But such machines will
5546 * not likely have syscall implemented either, so who cares?
5548 while (++MARK <= SP) {
5549 if (SvNIOK(*MARK) || !i)
5550 a[i++] = SvIV(*MARK);
5551 else if (*MARK == &PL_sv_undef)
5554 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5560 DIE(aTHX_ "Too many args to syscall");
5562 DIE(aTHX_ "Too few args to syscall");
5564 retval = syscall(a[0]);
5567 retval = syscall(a[0],a[1]);
5570 retval = syscall(a[0],a[1],a[2]);
5573 retval = syscall(a[0],a[1],a[2],a[3]);
5576 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5579 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5582 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5585 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5589 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5592 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5595 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5599 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5603 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5607 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5608 a[10],a[11],a[12],a[13]);
5610 #endif /* atarist */
5616 DIE(aTHX_ PL_no_func, "syscall");
5620 #ifdef FCNTL_EMULATE_FLOCK
5622 /* XXX Emulate flock() with fcntl().
5623 What's really needed is a good file locking module.
5627 fcntl_emulate_flock(int fd, int operation)
5631 switch (operation & ~LOCK_NB) {
5633 flock.l_type = F_RDLCK;
5636 flock.l_type = F_WRLCK;
5639 flock.l_type = F_UNLCK;
5645 flock.l_whence = SEEK_SET;
5646 flock.l_start = flock.l_len = (Off_t)0;
5648 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5651 #endif /* FCNTL_EMULATE_FLOCK */
5653 #ifdef LOCKF_EMULATE_FLOCK
5655 /* XXX Emulate flock() with lockf(). This is just to increase
5656 portability of scripts. The calls are not completely
5657 interchangeable. What's really needed is a good file
5661 /* The lockf() constants might have been defined in <unistd.h>.
5662 Unfortunately, <unistd.h> causes troubles on some mixed
5663 (BSD/POSIX) systems, such as SunOS 4.1.3.
5665 Further, the lockf() constants aren't POSIX, so they might not be
5666 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5667 just stick in the SVID values and be done with it. Sigh.
5671 # define F_ULOCK 0 /* Unlock a previously locked region */
5674 # define F_LOCK 1 /* Lock a region for exclusive use */
5677 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5680 # define F_TEST 3 /* Test a region for other processes locks */
5684 lockf_emulate_flock(int fd, int operation)
5690 /* flock locks entire file so for lockf we need to do the same */
5692 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5693 if (pos > 0) /* is seekable and needs to be repositioned */
5694 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5695 pos = -1; /* seek failed, so don't seek back afterwards */
5698 switch (operation) {
5700 /* LOCK_SH - get a shared lock */
5702 /* LOCK_EX - get an exclusive lock */
5704 i = lockf (fd, F_LOCK, 0);
5707 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5708 case LOCK_SH|LOCK_NB:
5709 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5710 case LOCK_EX|LOCK_NB:
5711 i = lockf (fd, F_TLOCK, 0);
5713 if ((errno == EAGAIN) || (errno == EACCES))
5714 errno = EWOULDBLOCK;
5717 /* LOCK_UN - unlock (non-blocking is a no-op) */
5719 case LOCK_UN|LOCK_NB:
5720 i = lockf (fd, F_ULOCK, 0);
5723 /* Default - can't decipher operation */
5730 if (pos > 0) /* need to restore position of the handle */
5731 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5736 #endif /* LOCKF_EMULATE_FLOCK */
5740 * c-indentation-style: bsd
5742 * indent-tabs-mode: t
5745 * ex: set ts=8 sts=4 sw=4 noet: