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 /* EFFective uid/gid ACCESS */
202 /* AIX 5.2 and below use mktime for localtime, and defines the edge case
203 * for time 0x7fffffff to be valid only in UTC. AIX 5.3 provides localtime64
204 * available in the 32bit environment, which could warrant Configure
205 * checks in the future.
208 #define LOCALTIME_EDGECASE_BROKEN
211 /* F_OK unused: if stat() cannot find it... */
213 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) && defined(EFF_ONLY_OK) && !defined(NO_EFF_ONLY_OK)
214 /* Digital UNIX (when the EFF_ONLY_OK gets fixed), UnixWare */
215 # define PERL_EFF_ACCESS(p,f) (access((p), (f) | EFF_ONLY_OK))
218 #if !defined(PERL_EFF_ACCESS) && defined(HAS_EACCESS)
219 # ifdef I_SYS_SECURITY
220 # include <sys/security.h>
224 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f), ACC_SELF))
227 # define PERL_EFF_ACCESS(p,f) (eaccess((p), (f)))
231 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESSX) && defined(ACC_SELF)
233 # define PERL_EFF_ACCESS(p,f) (accessx((p), (f), ACC_SELF))
237 #if !defined(PERL_EFF_ACCESS) && defined(HAS_ACCESS) \
238 && (defined(HAS_SETREUID) || defined(HAS_SETRESUID) \
239 || defined(HAS_SETREGID) || defined(HAS_SETRESGID))
242 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
244 const Uid_t ruid = getuid();
245 const Uid_t euid = geteuid();
246 const Gid_t rgid = getgid();
247 const Gid_t egid = getegid();
251 #if !defined(HAS_SETREUID) && !defined(HAS_SETRESUID)
252 Perl_croak(aTHX_ "switching effective uid is not implemented");
255 if (setreuid(euid, ruid))
258 if (setresuid(euid, ruid, (Uid_t)-1))
261 Perl_croak(aTHX_ "entering effective uid failed");
264 #if !defined(HAS_SETREGID) && !defined(HAS_SETRESGID)
265 Perl_croak(aTHX_ "switching effective gid is not implemented");
268 if (setregid(egid, rgid))
271 if (setresgid(egid, rgid, (Gid_t)-1))
274 Perl_croak(aTHX_ "entering effective gid failed");
277 res = access(path, mode);
280 if (setreuid(ruid, euid))
283 if (setresuid(ruid, euid, (Uid_t)-1))
286 Perl_croak(aTHX_ "leaving effective uid failed");
289 if (setregid(rgid, egid))
292 if (setresgid(rgid, egid, (Gid_t)-1))
295 Perl_croak(aTHX_ "leaving effective gid failed");
300 # define PERL_EFF_ACCESS(p,f) (emulate_eaccess((p), (f)))
303 #if !defined(PERL_EFF_ACCESS)
304 /* With it or without it: anyway you get a warning: either that
305 it is unused, or it is declared static and never defined.
308 S_emulate_eaccess(pTHX_ const char* path, Mode_t mode)
312 Perl_croak(aTHX_ "switching effective uid is not implemented");
322 const char * const tmps = POPpconstx;
323 const I32 gimme = GIMME_V;
324 const char *mode = "r";
327 if (PL_op->op_private & OPpOPEN_IN_RAW)
329 else if (PL_op->op_private & OPpOPEN_IN_CRLF)
331 fp = PerlProc_popen(tmps, mode);
333 const char * const type = PL_curcop->cop_io ? SvPV_nolen_const(PL_curcop->cop_io) : NULL;
335 PerlIO_apply_layers(aTHX_ fp,mode,type);
337 if (gimme == G_VOID) {
339 while (PerlIO_read(fp, tmpbuf, sizeof tmpbuf) > 0)
342 else if (gimme == G_SCALAR) {
345 PL_rs = &PL_sv_undef;
346 sv_setpvn(TARG, "", 0); /* note that this preserves previous buffer */
347 while (sv_gets(TARG, fp, SvCUR(TARG)) != Nullch)
355 SV * const sv = NEWSV(56, 79);
356 if (sv_gets(sv, fp, 0) == Nullch) {
360 XPUSHs(sv_2mortal(sv));
361 if (SvLEN(sv) - SvCUR(sv) > 20) {
362 SvPV_shrink_to_cur(sv);
367 STATUS_NATIVE_CHILD_SET(PerlProc_pclose(fp));
368 TAINT; /* "I believe that this is not gratuitous!" */
371 STATUS_NATIVE_CHILD_SET(-1);
372 if (gimme == G_SCALAR)
383 tryAMAGICunTARGET(iter, -1);
385 /* Note that we only ever get here if File::Glob fails to load
386 * without at the same time croaking, for some reason, or if
387 * perl was built with PERL_EXTERNAL_GLOB */
394 * The external globbing program may use things we can't control,
395 * so for security reasons we must assume the worst.
398 taint_proper(PL_no_security, "glob");
402 SAVESPTR(PL_last_in_gv); /* We don't want this to be permanent. */
403 PL_last_in_gv = (GV*)*PL_stack_sp--;
405 SAVESPTR(PL_rs); /* This is not permanent, either. */
406 PL_rs = sv_2mortal(newSVpvn("\000", 1));
409 *SvPVX(PL_rs) = '\n';
413 result = do_readline();
420 PL_last_in_gv = cGVOP_gv;
421 return do_readline();
432 do_join(TARG, &PL_sv_no, MARK, SP);
436 else if (SP == MARK) {
443 tmps = SvPV_const(tmpsv, len);
444 if ((!tmps || !len) && PL_errgv) {
445 SV * const error = ERRSV;
446 SvUPGRADE(error, SVt_PV);
447 if (SvPOK(error) && SvCUR(error))
448 sv_catpv(error, "\t...caught");
450 tmps = SvPV_const(tmpsv, len);
453 tmpsv = sv_2mortal(newSVpvn("Warning: something's wrong", 26));
455 Perl_warn(aTHX_ "%"SVf, tmpsv);
467 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
469 if (SP - MARK != 1) {
471 do_join(TARG, &PL_sv_no, MARK, SP);
473 tmps = SvPV_const(tmpsv, len);
479 tmps = SvROK(tmpsv) ? Nullch : SvPV_const(tmpsv, len);
483 SvUPGRADE(error, SVt_PV);
484 if (multiarg ? SvROK(error) : SvROK(tmpsv)) {
486 SvSetSV(error,tmpsv);
487 else if (sv_isobject(error)) {
488 HV * const stash = SvSTASH(SvRV(error));
489 GV * const gv = gv_fetchmethod(stash, "PROPAGATE");
491 SV * const file = sv_2mortal(newSVpv(CopFILE(PL_curcop),0));
492 SV * const line = sv_2mortal(newSVuv(CopLINE(PL_curcop)));
499 call_sv((SV*)GvCV(gv),
500 G_SCALAR|G_EVAL|G_KEEPERR);
501 sv_setsv(error,*PL_stack_sp--);
507 if (SvPOK(error) && SvCUR(error))
508 sv_catpv(error, "\t...propagated");
511 tmps = SvPV_const(tmpsv, len);
517 tmpsv = sv_2mortal(newSVpvn("Died", 4));
519 DIE(aTHX_ "%"SVf, tmpsv);
535 GV * const gv = (GV *)*++MARK;
538 DIE(aTHX_ PL_no_usym, "filehandle");
539 if ((io = GvIOp(gv)))
540 IoFLAGS(GvIOp(gv)) &= ~IOf_UNTAINT;
543 MAGIC * const mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
545 /* Method's args are same as ours ... */
546 /* ... except handle is replaced by the object */
547 *MARK-- = SvTIED_obj((SV*)io, mg);
551 call_method("OPEN", G_SCALAR);
565 tmps = SvPV_const(sv, len);
566 ok = do_openn(gv, tmps, len, FALSE, O_RDONLY, 0, Nullfp, MARK+1, (SP-MARK));
569 PUSHi( (I32)PL_forkprocess );
570 else if (PL_forkprocess == 0) /* we are a new child */
582 GV * const gv = (MAXARG == 0) ? PL_defoutgv : (GV*)POPs;
584 if (gv && (io = GvIO(gv))
585 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
588 XPUSHs(SvTIED_obj((SV*)io, mg));
591 call_method("CLOSE", G_SCALAR);
597 PUSHs(boolSV(do_close(gv, TRUE)));
609 GV * const wgv = (GV*)POPs;
610 GV * const rgv = (GV*)POPs;
615 if (SvTYPE(rgv) != SVt_PVGV || SvTYPE(wgv) != SVt_PVGV)
616 DIE(aTHX_ PL_no_usym, "filehandle");
621 do_close(rgv, FALSE);
623 do_close(wgv, FALSE);
625 if (PerlProc_pipe(fd) < 0)
628 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPE_OPEN_MODE);
629 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPE_OPEN_MODE);
630 IoOFP(rstio) = IoIFP(rstio);
631 IoIFP(wstio) = IoOFP(wstio);
632 IoTYPE(rstio) = IoTYPE_RDONLY;
633 IoTYPE(wstio) = IoTYPE_WRONLY;
635 if (!IoIFP(rstio) || !IoOFP(wstio)) {
636 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
637 else PerlLIO_close(fd[0]);
638 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
639 else PerlLIO_close(fd[1]);
642 #if defined(HAS_FCNTL) && defined(F_SETFD)
643 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
644 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
651 DIE(aTHX_ PL_no_func, "pipe");
667 if (gv && (io = GvIO(gv))
668 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
671 XPUSHs(SvTIED_obj((SV*)io, mg));
674 call_method("FILENO", G_SCALAR);
680 if (!gv || !(io = GvIO(gv)) || !(fp = IoIFP(io))) {
681 /* Can't do this because people seem to do things like
682 defined(fileno($foo)) to check whether $foo is a valid fh.
683 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
684 report_evil_fh(gv, io, PL_op->op_type);
689 PUSHi(PerlIO_fileno(fp));
701 anum = PerlLIO_umask(0);
702 (void)PerlLIO_umask(anum);
705 anum = PerlLIO_umask(POPi);
706 TAINT_PROPER("umask");
709 /* Only DIE if trying to restrict permissions on "user" (self).
710 * Otherwise it's harmless and more useful to just return undef
711 * since 'group' and 'other' concepts probably don't exist here. */
712 if (MAXARG >= 1 && (POPi & 0700))
713 DIE(aTHX_ "umask not implemented");
714 XPUSHs(&PL_sv_undef);
736 if (gv && (io = GvIO(gv))
737 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
740 XPUSHs(SvTIED_obj((SV*)io, mg));
745 call_method("BINMODE", G_SCALAR);
752 if (!(io = GvIO(gv)) || !(fp = IoIFP(io))) {
753 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
754 report_evil_fh(gv, io, PL_op->op_type);
755 SETERRNO(EBADF,RMS_IFI);
760 if (PerlIO_binmode(aTHX_ fp,IoTYPE(io),mode_from_discipline(discp),
761 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
762 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
763 if (!PerlIO_binmode(aTHX_ IoOFP(io),IoTYPE(io),
764 mode_from_discipline(discp),
765 (discp) ? SvPV_nolen_const(discp) : Nullch)) {
785 const I32 markoff = MARK - PL_stack_base;
786 const char *methname;
787 int how = PERL_MAGIC_tied;
791 switch(SvTYPE(varsv)) {
793 methname = "TIEHASH";
794 HvEITER_set((HV *)varsv, 0);
797 methname = "TIEARRAY";
800 #ifdef GV_UNIQUE_CHECK
801 if (GvUNIQUE((GV*)varsv)) {
802 Perl_croak(aTHX_ "Attempt to tie unique GV");
805 methname = "TIEHANDLE";
806 how = PERL_MAGIC_tiedscalar;
807 /* For tied filehandles, we apply tiedscalar magic to the IO
808 slot of the GP rather than the GV itself. AMS 20010812 */
810 GvIOp(varsv) = newIO();
811 varsv = (SV *)GvIOp(varsv);
814 methname = "TIESCALAR";
815 how = PERL_MAGIC_tiedscalar;
819 if (sv_isobject(*MARK)) {
821 PUSHSTACKi(PERLSI_MAGIC);
823 EXTEND(SP,(I32)items);
827 call_method(methname, G_SCALAR);
830 /* Not clear why we don't call call_method here too.
831 * perhaps to get different error message ?
833 stash = gv_stashsv(*MARK, FALSE);
834 if (!stash || !(gv = gv_fetchmethod(stash, methname))) {
835 DIE(aTHX_ "Can't locate object method \"%s\" via package \"%"SVf"\"",
839 PUSHSTACKi(PERLSI_MAGIC);
841 EXTEND(SP,(I32)items);
845 call_sv((SV*)GvCV(gv), G_SCALAR);
851 if (sv_isobject(sv)) {
852 sv_unmagic(varsv, how);
853 /* Croak if a self-tie on an aggregate is attempted. */
854 if (varsv == SvRV(sv) &&
855 (SvTYPE(varsv) == SVt_PVAV ||
856 SvTYPE(varsv) == SVt_PVHV))
858 "Self-ties of arrays and hashes are not supported");
859 sv_magic(varsv, (SvRV(sv) == varsv ? Nullsv : sv), how, Nullch, 0);
862 SP = PL_stack_base + markoff;
872 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
873 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
875 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
878 if ((mg = SvTIED_mg(sv, how))) {
879 SV * const obj = SvRV(SvTIED_obj(sv, mg));
882 GV * const gv = gv_fetchmethod_autoload(SvSTASH(obj), "UNTIE", FALSE);
883 if (gv && isGV(gv) && (cv = GvCV(gv))) {
885 XPUSHs(SvTIED_obj((SV*)gv, mg));
886 XPUSHs(sv_2mortal(newSViv(SvREFCNT(obj)-1)));
889 call_sv((SV *)cv, G_VOID);
893 else if (mg && SvREFCNT(obj) > 1 && ckWARN(WARN_UNTIE)) {
894 Perl_warner(aTHX_ packWARN(WARN_UNTIE),
895 "untie attempted while %"UVuf" inner references still exist",
896 (UV)SvREFCNT(obj) - 1 ) ;
900 sv_unmagic(sv, how) ;
909 const char how = (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV)
910 ? PERL_MAGIC_tied : PERL_MAGIC_tiedscalar;
912 if (SvTYPE(sv) == SVt_PVGV && !(sv = (SV *)GvIOp(sv)))
915 if ((mg = SvTIED_mg(sv, how))) {
916 SV *osv = SvTIED_obj(sv, mg);
917 if (osv == mg->mg_obj)
918 osv = sv_mortalcopy(osv);
932 HV * const hv = (HV*)POPs;
933 SV * const sv = sv_mortalcopy(&PL_sv_no);
935 sv_setpv(sv, "AnyDBM_File");
936 stash = gv_stashsv(sv, FALSE);
937 if (!stash || !(gv = gv_fetchmethod(stash, "TIEHASH"))) {
939 require_pv("AnyDBM_File.pm");
941 if (!(gv = gv_fetchmethod(stash, "TIEHASH")))
942 DIE(aTHX_ "No dbm on this machine");
952 PUSHs(sv_2mortal(newSVuv(O_RDWR|O_CREAT)));
954 PUSHs(sv_2mortal(newSVuv(O_RDWR)));
957 call_sv((SV*)GvCV(gv), G_SCALAR);
960 if (!sv_isobject(TOPs)) {
965 PUSHs(sv_2mortal(newSVuv(O_RDONLY)));
968 call_sv((SV*)GvCV(gv), G_SCALAR);
972 if (sv_isobject(TOPs)) {
973 sv_unmagic((SV *) hv, PERL_MAGIC_tied);
974 sv_magic((SV*)hv, TOPs, PERL_MAGIC_tied, Nullch, 0);
991 struct timeval timebuf;
992 struct timeval *tbuf = &timebuf;
995 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1000 # if BYTEORDER & 0xf0000
1001 # define ORDERBYTE (0x88888888 - BYTEORDER)
1003 # define ORDERBYTE (0x4444 - BYTEORDER)
1009 for (i = 1; i <= 3; i++) {
1010 SV * const sv = SP[i];
1013 if (SvREADONLY(sv)) {
1015 sv_force_normal_flags(sv, 0);
1016 if (SvREADONLY(sv) && !(SvPOK(sv) && SvCUR(sv) == 0))
1017 DIE(aTHX_ PL_no_modify);
1020 if (ckWARN(WARN_MISC))
1021 Perl_warner(aTHX_ packWARN(WARN_MISC), "Non-string passed as bitmask");
1022 SvPV_force_nolen(sv); /* force string conversion */
1029 /* little endians can use vecs directly */
1030 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1037 masksize = NFDBITS / NBBY;
1039 masksize = sizeof(long); /* documented int, everyone seems to use long */
1041 Zero(&fd_sets[0], 4, char*);
1044 # if SELECT_MIN_BITS == 1
1045 growsize = sizeof(fd_set);
1047 # if defined(__GLIBC__) && defined(__FD_SETSIZE)
1048 # undef SELECT_MIN_BITS
1049 # define SELECT_MIN_BITS __FD_SETSIZE
1051 /* If SELECT_MIN_BITS is greater than one we most probably will want
1052 * to align the sizes with SELECT_MIN_BITS/8 because for example
1053 * in many little-endian (Intel, Alpha) systems (Linux, OS/2, Digital
1054 * UNIX, Solaris, NeXT, Darwin) the smallest quantum select() operates
1055 * on (sets/tests/clears bits) is 32 bits. */
1056 growsize = maxlen + (SELECT_MIN_BITS/8 - (maxlen % (SELECT_MIN_BITS/8)));
1064 timebuf.tv_sec = (long)value;
1065 value -= (NV)timebuf.tv_sec;
1066 timebuf.tv_usec = (long)(value * 1000000.0);
1069 tbuf = Null(struct timeval*);
1071 for (i = 1; i <= 3; i++) {
1073 if (!SvOK(sv) || SvCUR(sv) == 0) {
1080 Sv_Grow(sv, growsize);
1084 while (++j <= growsize) {
1088 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1090 Newx(fd_sets[i], growsize, char);
1091 for (offset = 0; offset < growsize; offset += masksize) {
1092 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1093 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1096 fd_sets[i] = SvPVX(sv);
1100 #ifdef PERL_IRIX5_SELECT_TIMEVAL_VOID_CAST
1101 /* Can't make just the (void*) conditional because that would be
1102 * cpp #if within cpp macro, and not all compilers like that. */
1103 nfound = PerlSock_select(
1105 (Select_fd_set_t) fd_sets[1],
1106 (Select_fd_set_t) fd_sets[2],
1107 (Select_fd_set_t) fd_sets[3],
1108 (void*) tbuf); /* Workaround for compiler bug. */
1110 nfound = PerlSock_select(
1112 (Select_fd_set_t) fd_sets[1],
1113 (Select_fd_set_t) fd_sets[2],
1114 (Select_fd_set_t) fd_sets[3],
1117 for (i = 1; i <= 3; i++) {
1120 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1122 for (offset = 0; offset < growsize; offset += masksize) {
1123 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1124 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1126 Safefree(fd_sets[i]);
1133 if (GIMME == G_ARRAY && tbuf) {
1134 value = (NV)(timebuf.tv_sec) +
1135 (NV)(timebuf.tv_usec) / 1000000.0;
1136 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
1137 sv_setnv(sv, value);
1141 DIE(aTHX_ "select not implemented");
1146 Perl_setdefout(pTHX_ GV *gv)
1149 (void)SvREFCNT_inc(gv);
1151 SvREFCNT_dec(PL_defoutgv);
1159 GV * const newdefout = (PL_op->op_private > 0) ? ((GV *) POPs) : (GV *) NULL;
1160 GV * egv = GvEGV(PL_defoutgv);
1166 XPUSHs(&PL_sv_undef);
1168 GV * const * const gvp = (GV**)hv_fetch(hv, GvNAME(egv), GvNAMELEN(egv), FALSE);
1169 if (gvp && *gvp == egv) {
1170 gv_efullname4(TARG, PL_defoutgv, Nullch, TRUE);
1174 XPUSHs(sv_2mortal(newRV((SV*)egv)));
1179 if (!GvIO(newdefout))
1180 gv_IOadd(newdefout);
1181 setdefout(newdefout);
1192 GV * const gv = (MAXARG==0) ? PL_stdingv : (GV*)POPs;
1194 if (gv && (io = GvIO(gv))
1195 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1197 const I32 gimme = GIMME_V;
1199 XPUSHs(SvTIED_obj((SV*)io, mg));
1202 call_method("GETC", gimme);
1205 if (gimme == G_SCALAR)
1206 SvSetMagicSV_nosteal(TARG, TOPs);
1209 if (!gv || do_eof(gv)) { /* make sure we have fp with something */
1210 if ((!io || (!IoIFP(io) && IoTYPE(io) != IoTYPE_WRONLY))
1211 && ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1212 report_evil_fh(gv, io, PL_op->op_type);
1213 SETERRNO(EBADF,RMS_IFI);
1217 sv_setpvn(TARG, " ", 1);
1218 *SvPVX(TARG) = PerlIO_getc(IoIFP(GvIOp(gv))); /* should never be EOF */
1219 if (PerlIO_isutf8(IoIFP(GvIOp(gv)))) {
1220 /* Find out how many bytes the char needs */
1221 Size_t len = UTF8SKIP(SvPVX_const(TARG));
1224 len = PerlIO_read(IoIFP(GvIOp(gv)),SvPVX(TARG)+1,len-1);
1225 SvCUR_set(TARG,1+len);
1234 S_doform(pTHX_ CV *cv, GV *gv, OP *retop)
1237 register PERL_CONTEXT *cx;
1238 const I32 gimme = GIMME_V;
1243 PUSHBLOCK(cx, CXt_FORMAT, PL_stack_sp);
1245 cx->blk_sub.retop = retop;
1247 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1249 setdefout(gv); /* locally select filehandle so $% et al work */
1281 SV * const tmpsv = sv_newmortal();
1283 gv_efullname4(tmpsv, fgv, Nullch, FALSE);
1284 name = SvPV_nolen_const(tmpsv);
1286 DIE(aTHX_ "Undefined format \"%s\" called", name);
1288 DIE(aTHX_ "Not a format reference");
1291 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1293 IoFLAGS(io) &= ~IOf_DIDTOP;
1294 return doform(cv,gv,PL_op->op_next);
1300 GV * const gv = cxstack[cxstack_ix].blk_sub.gv;
1301 register IO * const io = GvIOp(gv);
1302 PerlIO * const ofp = IoOFP(io);
1306 register PERL_CONTEXT *cx;
1308 DEBUG_f(PerlIO_printf(Perl_debug_log, "left=%ld, todo=%ld\n",
1309 (long)IoLINES_LEFT(io), (long)FmLINES(PL_formtarget)));
1312 if (IoLINES_LEFT(io) < FmLINES(PL_formtarget) &&
1313 PL_formtarget != PL_toptarget)
1317 if (!IoTOP_GV(io)) {
1320 if (!IoTOP_NAME(io)) {
1322 if (!IoFMT_NAME(io))
1323 IoFMT_NAME(io) = savepv(GvNAME(gv));
1324 topname = sv_2mortal(Perl_newSVpvf(aTHX_ "%s_TOP", GvNAME(gv)));
1325 topgv = gv_fetchsv(topname, FALSE, SVt_PVFM);
1326 if ((topgv && GvFORM(topgv)) ||
1327 !gv_fetchpv("top",FALSE,SVt_PVFM))
1328 IoTOP_NAME(io) = savesvpv(topname);
1330 IoTOP_NAME(io) = savepvn("top", 3);
1332 topgv = gv_fetchpv(IoTOP_NAME(io),FALSE, SVt_PVFM);
1333 if (!topgv || !GvFORM(topgv)) {
1334 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1337 IoTOP_GV(io) = topgv;
1339 if (IoFLAGS(io) & IOf_DIDTOP) { /* Oh dear. It still doesn't fit. */
1340 I32 lines = IoLINES_LEFT(io);
1341 const char *s = SvPVX_const(PL_formtarget);
1342 if (lines <= 0) /* Yow, header didn't even fit!!! */
1344 while (lines-- > 0) {
1345 s = strchr(s, '\n');
1351 const STRLEN save = SvCUR(PL_formtarget);
1352 SvCUR_set(PL_formtarget, s - SvPVX_const(PL_formtarget));
1353 do_print(PL_formtarget, ofp);
1354 SvCUR_set(PL_formtarget, save);
1355 sv_chop(PL_formtarget, s);
1356 FmLINES(PL_formtarget) -= IoLINES_LEFT(io);
1359 if (IoLINES_LEFT(io) >= 0 && IoPAGE(io) > 0)
1360 do_print(PL_formfeed, ofp);
1361 IoLINES_LEFT(io) = IoPAGE_LEN(io);
1363 PL_formtarget = PL_toptarget;
1364 IoFLAGS(io) |= IOf_DIDTOP;
1367 DIE(aTHX_ "bad top format reference");
1370 SV * const sv = sv_newmortal();
1372 gv_efullname4(sv, fgv, Nullch, FALSE);
1373 name = SvPV_nolen_const(sv);
1375 DIE(aTHX_ "Undefined top format \"%s\" called",name);
1379 DIE(aTHX_ "Undefined top format called");
1382 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
1383 return doform(cv,gv,PL_op);
1387 POPBLOCK(cx,PL_curpm);
1393 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1395 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1396 else if (ckWARN(WARN_CLOSED))
1397 report_evil_fh(gv, io, PL_op->op_type);
1402 if ((IoLINES_LEFT(io) -= FmLINES(PL_formtarget)) < 0) {
1403 if (ckWARN(WARN_IO))
1404 Perl_warner(aTHX_ packWARN(WARN_IO), "page overflow");
1406 if (!do_print(PL_formtarget, fp))
1409 FmLINES(PL_formtarget) = 0;
1410 SvCUR_set(PL_formtarget, 0);
1411 *SvEND(PL_formtarget) = '\0';
1412 if (IoFLAGS(io) & IOf_FLUSH)
1413 (void)PerlIO_flush(fp);
1418 PL_formtarget = PL_bodytarget;
1420 PERL_UNUSED_VAR(newsp);
1421 PERL_UNUSED_VAR(gimme);
1422 return cx->blk_sub.retop;
1427 dVAR; dSP; dMARK; dORIGMARK;
1433 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
1435 if (gv && (io = GvIO(gv))
1436 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1438 if (MARK == ORIGMARK) {
1441 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1445 *MARK = SvTIED_obj((SV*)io, mg);
1448 call_method("PRINTF", G_SCALAR);
1451 MARK = ORIGMARK + 1;
1458 if (!(io = GvIO(gv))) {
1459 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1460 report_evil_fh(gv, io, PL_op->op_type);
1461 SETERRNO(EBADF,RMS_IFI);
1464 else if (!(fp = IoOFP(io))) {
1465 if (ckWARN2(WARN_CLOSED,WARN_IO)) {
1467 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
1468 else if (ckWARN(WARN_CLOSED))
1469 report_evil_fh(gv, io, PL_op->op_type);
1471 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1475 do_sprintf(sv, SP - MARK, MARK + 1);
1476 if (!do_print(sv, fp))
1479 if (IoFLAGS(io) & IOf_FLUSH)
1480 if (PerlIO_flush(fp) == EOF)
1491 PUSHs(&PL_sv_undef);
1498 const int perm = (MAXARG > 3) ? POPi : 0666;
1499 const int mode = POPi;
1500 SV * const sv = POPs;
1501 GV * const gv = (GV *)POPs;
1504 /* Need TIEHANDLE method ? */
1505 const char * const tmps = SvPV_const(sv, len);
1506 /* FIXME? do_open should do const */
1507 if (do_open(gv, tmps, len, TRUE, mode, perm, Nullfp)) {
1508 IoLINES(GvIOp(gv)) = 0;
1512 PUSHs(&PL_sv_undef);
1519 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1525 Sock_size_t bufsize;
1533 bool charstart = FALSE;
1534 STRLEN charskip = 0;
1537 GV * const gv = (GV*)*++MARK;
1538 if ((PL_op->op_type == OP_READ || PL_op->op_type == OP_SYSREAD)
1539 && gv && (io = GvIO(gv)) )
1541 const MAGIC * mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
1545 *MARK = SvTIED_obj((SV*)io, mg);
1547 call_method("READ", G_SCALAR);
1561 sv_setpvn(bufsv, "", 0);
1562 length = SvIVx(*++MARK);
1565 offset = SvIVx(*++MARK);
1569 if (!io || !IoIFP(io)) {
1570 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1571 report_evil_fh(gv, io, PL_op->op_type);
1572 SETERRNO(EBADF,RMS_IFI);
1575 if ((fp_utf8 = PerlIO_isutf8(IoIFP(io))) && !IN_BYTES) {
1576 buffer = SvPVutf8_force(bufsv, blen);
1577 /* UTF-8 may not have been set if they are all low bytes */
1582 buffer = SvPV_force(bufsv, blen);
1583 buffer_utf8 = !IN_BYTES && SvUTF8(bufsv);
1586 DIE(aTHX_ "Negative length");
1594 if (PL_op->op_type == OP_RECV) {
1595 char namebuf[MAXPATHLEN];
1596 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
1597 bufsize = sizeof (struct sockaddr_in);
1599 bufsize = sizeof namebuf;
1601 #ifdef OS2 /* At least Warp3+IAK: only the first byte of bufsize set */
1605 buffer = SvGROW(bufsv, (STRLEN)(length+1));
1606 /* 'offset' means 'flags' here */
1607 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, offset,
1608 (struct sockaddr *)namebuf, &bufsize);
1612 /* Bogus return without padding */
1613 bufsize = sizeof (struct sockaddr_in);
1615 SvCUR_set(bufsv, count);
1616 *SvEND(bufsv) = '\0';
1617 (void)SvPOK_only(bufsv);
1621 /* This should not be marked tainted if the fp is marked clean */
1622 if (!(IoFLAGS(io) & IOf_UNTAINT))
1623 SvTAINTED_on(bufsv);
1625 sv_setpvn(TARG, namebuf, bufsize);
1630 if (PL_op->op_type == OP_RECV)
1631 DIE(aTHX_ PL_no_sock_func, "recv");
1633 if (DO_UTF8(bufsv)) {
1634 /* offset adjust in characters not bytes */
1635 blen = sv_len_utf8(bufsv);
1638 if (-offset > (int)blen)
1639 DIE(aTHX_ "Offset outside string");
1642 if (DO_UTF8(bufsv)) {
1643 /* convert offset-as-chars to offset-as-bytes */
1644 if (offset >= (int)blen)
1645 offset += SvCUR(bufsv) - blen;
1647 offset = utf8_hop((U8 *)buffer,offset) - (U8 *) buffer;
1650 bufsize = SvCUR(bufsv);
1651 /* Allocating length + offset + 1 isn't perfect in the case of reading
1652 bytes from a byte file handle into a UTF8 buffer, but it won't harm us
1654 (should be 2 * length + offset + 1, or possibly something longer if
1655 PL_encoding is true) */
1656 buffer = SvGROW(bufsv, (STRLEN)(length+offset+1));
1657 if (offset > 0 && (Sock_size_t)offset > bufsize) { /* Zero any newly allocated space */
1658 Zero(buffer+bufsize, offset-bufsize, char);
1660 buffer = buffer + offset;
1662 read_target = bufsv;
1664 /* Best to read the bytes into a new SV, upgrade that to UTF8, then
1665 concatenate it to the current buffer. */
1667 /* Truncate the existing buffer to the start of where we will be
1669 SvCUR_set(bufsv, offset);
1671 read_target = sv_newmortal();
1672 SvUPGRADE(read_target, SVt_PV);
1673 buffer = SvGROW(read_target, (STRLEN)(length + 1));
1676 if (PL_op->op_type == OP_SYSREAD) {
1677 #ifdef PERL_SOCK_SYSREAD_IS_RECV
1678 if (IoTYPE(io) == IoTYPE_SOCKET) {
1679 count = PerlSock_recv(PerlIO_fileno(IoIFP(io)),
1685 count = PerlLIO_read(PerlIO_fileno(IoIFP(io)),
1690 #ifdef HAS_SOCKET__bad_code_maybe
1691 if (IoTYPE(io) == IoTYPE_SOCKET) {
1692 char namebuf[MAXPATHLEN];
1693 #if defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)
1694 bufsize = sizeof (struct sockaddr_in);
1696 bufsize = sizeof namebuf;
1698 count = PerlSock_recvfrom(PerlIO_fileno(IoIFP(io)), buffer, length, 0,
1699 (struct sockaddr *)namebuf, &bufsize);
1704 count = PerlIO_read(IoIFP(io), buffer, length);
1705 /* PerlIO_read() - like fread() returns 0 on both error and EOF */
1706 if (count == 0 && PerlIO_error(IoIFP(io)))
1710 if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
1711 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
1714 SvCUR_set(read_target, count+(buffer - SvPVX_const(read_target)));
1715 *SvEND(read_target) = '\0';
1716 (void)SvPOK_only(read_target);
1717 if (fp_utf8 && !IN_BYTES) {
1718 /* Look at utf8 we got back and count the characters */
1719 const char *bend = buffer + count;
1720 while (buffer < bend) {
1722 skip = UTF8SKIP(buffer);
1725 if (buffer - charskip + skip > bend) {
1726 /* partial character - try for rest of it */
1727 length = skip - (bend-buffer);
1728 offset = bend - SvPVX_const(bufsv);
1740 /* If we have not 'got' the number of _characters_ we 'wanted' get some more
1741 provided amount read (count) was what was requested (length)
1743 if (got < wanted && count == length) {
1744 length = wanted - got;
1745 offset = bend - SvPVX_const(bufsv);
1748 /* return value is character count */
1752 else if (buffer_utf8) {
1753 /* Let svcatsv upgrade the bytes we read in to utf8.
1754 The buffer is a mortal so will be freed soon. */
1755 sv_catsv_nomg(bufsv, read_target);
1758 /* This should not be marked tainted if the fp is marked clean */
1759 if (!(IoFLAGS(io) & IOf_UNTAINT))
1760 SvTAINTED_on(bufsv);
1772 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
1780 const int op_type = PL_op->op_type;
1782 GV *const gv = (GV*)*++MARK;
1783 if (PL_op->op_type == OP_SYSWRITE
1784 && gv && (io = GvIO(gv))
1785 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1789 if (MARK == SP - 1) {
1791 sv = sv_2mortal(newSViv(sv_len(*SP)));
1797 *(ORIGMARK+1) = SvTIED_obj((SV*)io, mg);
1799 call_method("WRITE", G_SCALAR);
1812 if (op_type == OP_SYSWRITE) {
1814 length = (Size_t) sv_len(bufsv);
1816 #if Size_t_size > IVSIZE
1817 length = (Size_t)SvNVx(*++MARK);
1819 length = (Size_t)SvIVx(*++MARK);
1821 if ((SSize_t)length < 0)
1822 DIE(aTHX_ "Negative length");
1827 if (!io || !IoIFP(io)) {
1829 if (ckWARN(WARN_CLOSED))
1830 report_evil_fh(gv, io, PL_op->op_type);
1831 SETERRNO(EBADF,RMS_IFI);
1835 if (PerlIO_isutf8(IoIFP(io))) {
1836 if (!SvUTF8(bufsv)) {
1837 bufsv = sv_2mortal(newSVsv(bufsv));
1838 buffer = sv_2pvutf8(bufsv, &blen);
1840 buffer = SvPV_const(bufsv, blen);
1843 if (DO_UTF8(bufsv)) {
1844 /* Not modifying source SV, so making a temporary copy. */
1845 bufsv = sv_2mortal(newSVsv(bufsv));
1846 sv_utf8_downgrade(bufsv, FALSE);
1848 buffer = SvPV_const(bufsv, blen);
1851 if (op_type == OP_SYSWRITE) {
1853 if (DO_UTF8(bufsv)) {
1854 /* length and offset are in chars */
1855 blen = sv_len_utf8(bufsv);
1858 offset = SvIVx(*++MARK);
1860 if (-offset > (IV)blen)
1861 DIE(aTHX_ "Offset outside string");
1863 } else if (offset >= (IV)blen && blen > 0)
1864 DIE(aTHX_ "Offset outside string");
1867 if (length > blen - offset)
1868 length = blen - offset;
1869 if (DO_UTF8(bufsv)) {
1870 buffer = (const char*)utf8_hop((const U8 *)buffer, offset);
1871 length = utf8_hop((U8 *)buffer, length) - (U8 *)buffer;
1874 buffer = buffer+offset;
1876 #ifdef PERL_SOCK_SYSWRITE_IS_SEND
1877 if (IoTYPE(io) == IoTYPE_SOCKET) {
1878 retval = PerlSock_send(PerlIO_fileno(IoIFP(io)),
1884 /* See the note at doio.c:do_print about filesize limits. --jhi */
1885 retval = PerlLIO_write(PerlIO_fileno(IoIFP(io)),
1891 const int flags = SvIVx(*++MARK);
1894 char * const sockbuf = SvPVx(*++MARK, mlen);
1895 retval = PerlSock_sendto(PerlIO_fileno(IoIFP(io)), buffer, blen,
1896 flags, (struct sockaddr *)sockbuf, mlen);
1900 = PerlSock_send(PerlIO_fileno(IoIFP(io)), buffer, blen, flags);
1905 DIE(aTHX_ PL_no_sock_func, "send");
1911 retval = utf8_length((U8*)buffer, (U8*)buffer + retval);
1912 #if Size_t_size > IVSIZE
1932 if (PL_op->op_flags & OPf_SPECIAL) { /* eof() */
1934 gv = PL_last_in_gv = GvEGV(PL_argvgv);
1936 if (io && !IoIFP(io)) {
1937 if ((IoFLAGS(io) & IOf_START) && av_len(GvAVn(gv)) < 0) {
1939 IoFLAGS(io) &= ~IOf_START;
1940 do_open(gv, "-", 1, FALSE, O_RDONLY, 0, Nullfp);
1941 sv_setpvn(GvSV(gv), "-", 1);
1942 SvSETMAGIC(GvSV(gv));
1944 else if (!nextargv(gv))
1949 gv = PL_last_in_gv; /* eof */
1952 gv = PL_last_in_gv = (GV*)POPs; /* eof(FH) */
1954 if (gv && (io = GvIO(gv))
1955 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1958 XPUSHs(SvTIED_obj((SV*)io, mg));
1961 call_method("EOF", G_SCALAR);
1967 PUSHs(boolSV(!gv || do_eof(gv)));
1979 PL_last_in_gv = (GV*)POPs;
1982 if (gv && (io = GvIO(gv))
1983 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1986 XPUSHs(SvTIED_obj((SV*)io, mg));
1989 call_method("TELL", G_SCALAR);
1995 #if LSEEKSIZE > IVSIZE
1996 PUSHn( do_tell(gv) );
1998 PUSHi( do_tell(gv) );
2007 const int whence = POPi;
2008 #if LSEEKSIZE > IVSIZE
2009 const Off_t offset = (Off_t)SvNVx(POPs);
2011 const Off_t offset = (Off_t)SvIVx(POPs);
2015 GV * const gv = PL_last_in_gv = (GV*)POPs;
2017 if (gv && (io = GvIO(gv))
2018 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
2021 XPUSHs(SvTIED_obj((SV*)io, mg));
2022 #if LSEEKSIZE > IVSIZE
2023 XPUSHs(sv_2mortal(newSVnv((NV) offset)));
2025 XPUSHs(sv_2mortal(newSViv(offset)));
2027 XPUSHs(sv_2mortal(newSViv(whence)));
2030 call_method("SEEK", G_SCALAR);
2036 if (PL_op->op_type == OP_SEEK)
2037 PUSHs(boolSV(do_seek(gv, offset, whence)));
2039 Off_t sought = do_sysseek(gv, offset, whence);
2041 PUSHs(&PL_sv_undef);
2043 SV* const sv = sought ?
2044 #if LSEEKSIZE > IVSIZE
2049 : newSVpvn(zero_but_true, ZBTLEN);
2050 PUSHs(sv_2mortal(sv));
2059 /* There seems to be no consensus on the length type of truncate()
2060 * and ftruncate(), both off_t and size_t have supporters. In
2061 * general one would think that when using large files, off_t is
2062 * at least as wide as size_t, so using an off_t should be okay. */
2063 /* XXX Configure probe for the length type of *truncate() needed XXX */
2065 #if Off_t_size > IVSIZE
2066 const Off_t len = (Off_t)POPn;
2068 const Off_t len = (Off_t)POPi;
2070 /* Checking for length < 0 is problematic as the type might or
2071 * might not be signed: if it is not, clever compilers will moan. */
2072 /* XXX Configure probe for the signedness of the length type of *truncate() needed? XXX */
2079 if (PL_op->op_flags & OPf_SPECIAL) {
2080 tmpgv = gv_fetchsv(POPs, FALSE, SVt_PVIO);
2089 TAINT_PROPER("truncate");
2090 if (!(fp = IoIFP(io))) {
2096 if (ftruncate(PerlIO_fileno(fp), len) < 0)
2098 if (my_chsize(PerlIO_fileno(fp), len) < 0)
2105 SV * const sv = POPs;
2108 if (SvTYPE(sv) == SVt_PVGV) {
2109 tmpgv = (GV*)sv; /* *main::FRED for example */
2110 goto do_ftruncate_gv;
2112 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2113 tmpgv = (GV*) SvRV(sv); /* \*main::FRED for example */
2114 goto do_ftruncate_gv;
2116 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
2117 io = (IO*) SvRV(sv); /* *main::FRED{IO} for example */
2118 goto do_ftruncate_io;
2121 name = SvPV_nolen_const(sv);
2122 TAINT_PROPER("truncate");
2124 if (truncate(name, len) < 0)
2128 const int tmpfd = PerlLIO_open(name, O_RDWR);
2133 if (my_chsize(tmpfd, len) < 0)
2135 PerlLIO_close(tmpfd);
2144 SETERRNO(EBADF,RMS_IFI);
2152 SV * const argsv = POPs;
2153 const unsigned int func = POPu;
2154 const int optype = PL_op->op_type;
2155 GV * const gv = (GV*)POPs;
2156 IO * const io = gv ? GvIOn(gv) : Null(IO*);
2160 if (!io || !argsv || !IoIFP(io)) {
2161 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2162 report_evil_fh(gv, io, PL_op->op_type);
2163 SETERRNO(EBADF,RMS_IFI); /* well, sort of... */
2167 if (SvPOK(argsv) || !SvNIOK(argsv)) {
2170 s = SvPV_force(argsv, len);
2171 need = IOCPARM_LEN(func);
2173 s = Sv_Grow(argsv, need + 1);
2174 SvCUR_set(argsv, need);
2177 s[SvCUR(argsv)] = 17; /* a little sanity check here */
2180 retval = SvIV(argsv);
2181 s = INT2PTR(char*,retval); /* ouch */
2184 TAINT_PROPER(optype == OP_IOCTL ? "ioctl" : "fcntl");
2186 if (optype == OP_IOCTL)
2188 retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
2190 DIE(aTHX_ "ioctl is not implemented");
2194 DIE(aTHX_ "fcntl is not implemented");
2196 #if defined(OS2) && defined(__EMX__)
2197 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, (int)s);
2199 retval = fcntl(PerlIO_fileno(IoIFP(io)), func, s);
2203 #if defined(HAS_IOCTL) || defined(HAS_FCNTL)
2205 if (s[SvCUR(argsv)] != 17)
2206 DIE(aTHX_ "Possible memory corruption: %s overflowed 3rd argument",
2208 s[SvCUR(argsv)] = 0; /* put our null back */
2209 SvSETMAGIC(argsv); /* Assume it has changed */
2218 PUSHp(zero_but_true, ZBTLEN);
2231 const int argtype = POPi;
2232 GV * const gv = (MAXARG == 0) ? PL_last_in_gv : (GV*)POPs;
2234 if (gv && (io = GvIO(gv)))
2241 (void)PerlIO_flush(fp);
2242 value = (I32)(PerlLIO_flock(PerlIO_fileno(fp), argtype) >= 0);
2245 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2246 report_evil_fh(gv, io, PL_op->op_type);
2248 SETERRNO(EBADF,RMS_IFI);
2253 DIE(aTHX_ PL_no_func, "flock()");
2263 const int protocol = POPi;
2264 const int type = POPi;
2265 const int domain = POPi;
2266 GV * const gv = (GV*)POPs;
2267 register IO * const io = gv ? GvIOn(gv) : NULL;
2271 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2272 report_evil_fh(gv, io, PL_op->op_type);
2274 do_close(gv, FALSE);
2275 SETERRNO(EBADF,LIB_INVARG);
2280 do_close(gv, FALSE);
2282 TAINT_PROPER("socket");
2283 fd = PerlSock_socket(domain, type, protocol);
2286 IoIFP(io) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE); /* stdio gets confused about sockets */
2287 IoOFP(io) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2288 IoTYPE(io) = IoTYPE_SOCKET;
2289 if (!IoIFP(io) || !IoOFP(io)) {
2290 if (IoIFP(io)) PerlIO_close(IoIFP(io));
2291 if (IoOFP(io)) PerlIO_close(IoOFP(io));
2292 if (!IoIFP(io) && !IoOFP(io)) PerlLIO_close(fd);
2295 #if defined(HAS_FCNTL) && defined(F_SETFD)
2296 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2300 setbuf( IoIFP(io), NULL); /* EPOC gets confused about sockets */
2305 DIE(aTHX_ PL_no_sock_func, "socket");
2311 #if defined (HAS_SOCKETPAIR) || (defined (HAS_SOCKET) && defined(SOCK_DGRAM) && defined(AF_INET) && defined(PF_INET))
2313 const int protocol = POPi;
2314 const int type = POPi;
2315 const int domain = POPi;
2316 GV * const gv2 = (GV*)POPs;
2317 GV * const gv1 = (GV*)POPs;
2318 register IO * const io1 = gv1 ? GvIOn(gv1) : NULL;
2319 register IO * const io2 = gv2 ? GvIOn(gv2) : NULL;
2322 if (!gv1 || !gv2 || !io1 || !io2) {
2323 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
2325 report_evil_fh(gv1, io1, PL_op->op_type);
2327 report_evil_fh(gv1, io2, PL_op->op_type);
2330 do_close(gv1, FALSE);
2332 do_close(gv2, FALSE);
2337 do_close(gv1, FALSE);
2339 do_close(gv2, FALSE);
2341 TAINT_PROPER("socketpair");
2342 if (PerlSock_socketpair(domain, type, protocol, fd) < 0)
2344 IoIFP(io1) = PerlIO_fdopen(fd[0], "r"SOCKET_OPEN_MODE);
2345 IoOFP(io1) = PerlIO_fdopen(fd[0], "w"SOCKET_OPEN_MODE);
2346 IoTYPE(io1) = IoTYPE_SOCKET;
2347 IoIFP(io2) = PerlIO_fdopen(fd[1], "r"SOCKET_OPEN_MODE);
2348 IoOFP(io2) = PerlIO_fdopen(fd[1], "w"SOCKET_OPEN_MODE);
2349 IoTYPE(io2) = IoTYPE_SOCKET;
2350 if (!IoIFP(io1) || !IoOFP(io1) || !IoIFP(io2) || !IoOFP(io2)) {
2351 if (IoIFP(io1)) PerlIO_close(IoIFP(io1));
2352 if (IoOFP(io1)) PerlIO_close(IoOFP(io1));
2353 if (!IoIFP(io1) && !IoOFP(io1)) PerlLIO_close(fd[0]);
2354 if (IoIFP(io2)) PerlIO_close(IoIFP(io2));
2355 if (IoOFP(io2)) PerlIO_close(IoOFP(io2));
2356 if (!IoIFP(io2) && !IoOFP(io2)) PerlLIO_close(fd[1]);
2359 #if defined(HAS_FCNTL) && defined(F_SETFD)
2360 fcntl(fd[0],F_SETFD,fd[0] > PL_maxsysfd); /* ensure close-on-exec */
2361 fcntl(fd[1],F_SETFD,fd[1] > PL_maxsysfd); /* ensure close-on-exec */
2366 DIE(aTHX_ PL_no_sock_func, "socketpair");
2374 #ifdef MPE /* Requires PRIV mode to bind() to ports < 1024 */
2375 extern void GETPRIVMODE();
2376 extern void GETUSERMODE();
2378 SV * const addrsv = POPs;
2379 /* OK, so on what platform does bind modify addr? */
2381 GV * const gv = (GV*)POPs;
2382 register IO * const io = GvIOn(gv);
2386 int mpeprivmode = 0;
2389 if (!io || !IoIFP(io))
2392 addr = SvPV_const(addrsv, len);
2393 TAINT_PROPER("bind");
2394 #ifdef MPE /* Deal with MPE bind() peculiarities */
2395 if (((struct sockaddr *)addr)->sa_family == AF_INET) {
2396 /* The address *MUST* stupidly be zero. */
2397 ((struct sockaddr_in *)addr)->sin_addr.s_addr = INADDR_ANY;
2398 /* PRIV mode is required to bind() to ports < 1024. */
2399 if (((struct sockaddr_in *)addr)->sin_port < 1024 &&
2400 ((struct sockaddr_in *)addr)->sin_port > 0) {
2401 GETPRIVMODE(); /* If this fails, we are aborted by MPE/iX. */
2406 if (PerlSock_bind(PerlIO_fileno(IoIFP(io)),
2407 (struct sockaddr *)addr, len) >= 0)
2410 #ifdef MPE /* Switch back to USER mode */
2421 if (ckWARN(WARN_CLOSED))
2422 report_evil_fh(gv, io, PL_op->op_type);
2423 SETERRNO(EBADF,SS_IVCHAN);
2426 DIE(aTHX_ PL_no_sock_func, "bind");
2434 SV * const addrsv = POPs;
2435 GV * const gv = (GV*)POPs;
2436 register IO * const io = GvIOn(gv);
2440 if (!io || !IoIFP(io))
2443 addr = SvPV_const(addrsv, len);
2444 TAINT_PROPER("connect");
2445 if (PerlSock_connect(PerlIO_fileno(IoIFP(io)), (struct sockaddr *)addr, len) >= 0)
2451 if (ckWARN(WARN_CLOSED))
2452 report_evil_fh(gv, io, PL_op->op_type);
2453 SETERRNO(EBADF,SS_IVCHAN);
2456 DIE(aTHX_ PL_no_sock_func, "connect");
2464 const int backlog = POPi;
2465 GV * const gv = (GV*)POPs;
2466 register IO * const io = gv ? GvIOn(gv) : NULL;
2468 if (!gv || !io || !IoIFP(io))
2471 if (PerlSock_listen(PerlIO_fileno(IoIFP(io)), backlog) >= 0)
2477 if (ckWARN(WARN_CLOSED))
2478 report_evil_fh(gv, io, PL_op->op_type);
2479 SETERRNO(EBADF,SS_IVCHAN);
2482 DIE(aTHX_ PL_no_sock_func, "listen");
2492 char namebuf[MAXPATHLEN];
2493 #if (defined(VMS_DO_SOCKETS) && defined(DECCRTL_SOCKETS)) || defined(MPE) || defined(__QNXNTO__)
2494 Sock_size_t len = sizeof (struct sockaddr_in);
2496 Sock_size_t len = sizeof namebuf;
2498 GV * const ggv = (GV*)POPs;
2499 GV * const ngv = (GV*)POPs;
2508 if (!gstio || !IoIFP(gstio))
2512 fd = PerlSock_accept(PerlIO_fileno(IoIFP(gstio)), (struct sockaddr *) namebuf, &len);
2516 do_close(ngv, FALSE);
2517 IoIFP(nstio) = PerlIO_fdopen(fd, "r"SOCKET_OPEN_MODE);
2518 IoOFP(nstio) = PerlIO_fdopen(fd, "w"SOCKET_OPEN_MODE);
2519 IoTYPE(nstio) = IoTYPE_SOCKET;
2520 if (!IoIFP(nstio) || !IoOFP(nstio)) {
2521 if (IoIFP(nstio)) PerlIO_close(IoIFP(nstio));
2522 if (IoOFP(nstio)) PerlIO_close(IoOFP(nstio));
2523 if (!IoIFP(nstio) && !IoOFP(nstio)) PerlLIO_close(fd);
2526 #if defined(HAS_FCNTL) && defined(F_SETFD)
2527 fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
2531 len = sizeof (struct sockaddr_in); /* EPOC somehow truncates info */
2532 setbuf( IoIFP(nstio), NULL); /* EPOC gets confused about sockets */
2534 #ifdef __SCO_VERSION__
2535 len = sizeof (struct sockaddr_in); /* OpenUNIX 8 somehow truncates info */
2538 PUSHp(namebuf, len);
2542 if (ckWARN(WARN_CLOSED))
2543 report_evil_fh(ggv, ggv ? GvIO(ggv) : 0, PL_op->op_type);
2544 SETERRNO(EBADF,SS_IVCHAN);
2550 DIE(aTHX_ PL_no_sock_func, "accept");
2558 const int how = POPi;
2559 GV * const gv = (GV*)POPs;
2560 register IO * const io = GvIOn(gv);
2562 if (!io || !IoIFP(io))
2565 PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
2569 if (ckWARN(WARN_CLOSED))
2570 report_evil_fh(gv, io, PL_op->op_type);
2571 SETERRNO(EBADF,SS_IVCHAN);
2574 DIE(aTHX_ PL_no_sock_func, "shutdown");
2582 const int optype = PL_op->op_type;
2583 SV * const sv = (optype == OP_GSOCKOPT) ? sv_2mortal(NEWSV(22, 257)) : POPs;
2584 const unsigned int optname = (unsigned int) POPi;
2585 const unsigned int lvl = (unsigned int) POPi;
2586 GV * const gv = (GV*)POPs;
2587 register IO * const io = GvIOn(gv);
2591 if (!io || !IoIFP(io))
2594 fd = PerlIO_fileno(IoIFP(io));
2598 (void)SvPOK_only(sv);
2602 if (PerlSock_getsockopt(fd, lvl, optname, SvPVX(sv), &len) < 0)
2609 #if defined(__SYMBIAN32__)
2610 # define SETSOCKOPT_OPTION_VALUE_T void *
2612 # define SETSOCKOPT_OPTION_VALUE_T const char *
2614 /* XXX TODO: We need to have a proper type (a Configure probe,
2615 * etc.) for what the C headers think of the third argument of
2616 * setsockopt(), the option_value read-only buffer: is it
2617 * a "char *", or a "void *", const or not. Some compilers
2618 * don't take kindly to e.g. assuming that "char *" implicitly
2619 * promotes to a "void *", or to explicitly promoting/demoting
2620 * consts to non/vice versa. The "const void *" is the SUS
2621 * definition, but that does not fly everywhere for the above
2623 SETSOCKOPT_OPTION_VALUE_T buf;
2627 buf = (SETSOCKOPT_OPTION_VALUE_T) SvPV_const(sv, l);
2631 aint = (int)SvIV(sv);
2632 buf = (SETSOCKOPT_OPTION_VALUE_T) &aint;
2635 if (PerlSock_setsockopt(fd, lvl, optname, buf, len) < 0)
2644 if (ckWARN(WARN_CLOSED))
2645 report_evil_fh(gv, io, optype);
2646 SETERRNO(EBADF,SS_IVCHAN);
2651 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2659 const int optype = PL_op->op_type;
2660 GV * const gv = (GV*)POPs;
2661 register IO * const io = GvIOn(gv);
2666 if (!io || !IoIFP(io))
2669 sv = sv_2mortal(NEWSV(22, 257));
2670 (void)SvPOK_only(sv);
2674 fd = PerlIO_fileno(IoIFP(io));
2676 case OP_GETSOCKNAME:
2677 if (PerlSock_getsockname(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2680 case OP_GETPEERNAME:
2681 if (PerlSock_getpeername(fd, (struct sockaddr *)SvPVX(sv), &len) < 0)
2683 #if defined(VMS_DO_SOCKETS) && defined (DECCRTL_SOCKETS)
2685 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";
2686 /* If the call succeeded, make sure we don't have a zeroed port/addr */
2687 if (((struct sockaddr *)SvPVX_const(sv))->sa_family == AF_INET &&
2688 !memcmp(SvPVX_const(sv) + sizeof(u_short), nowhere,
2689 sizeof(u_short) + sizeof(struct in_addr))) {
2696 #ifdef BOGUS_GETNAME_RETURN
2697 /* Interactive Unix, getpeername() and getsockname()
2698 does not return valid namelen */
2699 if (len == BOGUS_GETNAME_RETURN)
2700 len = sizeof(struct sockaddr);
2708 if (ckWARN(WARN_CLOSED))
2709 report_evil_fh(gv, io, optype);
2710 SETERRNO(EBADF,SS_IVCHAN);
2715 DIE(aTHX_ PL_no_sock_func, PL_op_desc[PL_op->op_type]);
2728 if (PL_op->op_flags & OPf_REF) {
2730 if (PL_op->op_type == OP_LSTAT) {
2731 if (gv != PL_defgv) {
2732 do_fstat_warning_check:
2733 if (ckWARN(WARN_IO))
2734 Perl_warner(aTHX_ packWARN(WARN_IO),
2735 "lstat() on filehandle %s", GvENAME(gv));
2736 } else if (PL_laststype != OP_LSTAT)
2737 Perl_croak(aTHX_ "The stat preceding lstat() wasn't an lstat");
2741 if (gv != PL_defgv) {
2742 PL_laststype = OP_STAT;
2744 sv_setpvn(PL_statname, "", 0);
2745 PL_laststatval = (GvIO(gv) && IoIFP(GvIOp(gv))
2746 ? PerlLIO_fstat(PerlIO_fileno(IoIFP(GvIOn(gv))), &PL_statcache) : -1);
2748 if (PL_laststatval < 0) {
2749 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
2750 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
2755 SV* const sv = POPs;
2756 if (SvTYPE(sv) == SVt_PVGV) {
2760 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2762 if (PL_op->op_type == OP_LSTAT)
2763 goto do_fstat_warning_check;
2766 sv_setpv(PL_statname, SvPV_nolen_const(sv));
2768 PL_laststype = PL_op->op_type;
2769 if (PL_op->op_type == OP_LSTAT)
2770 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(PL_statname), &PL_statcache);
2772 PL_laststatval = PerlLIO_stat(SvPV_nolen_const(PL_statname), &PL_statcache);
2773 if (PL_laststatval < 0) {
2774 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname), '\n'))
2775 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
2781 if (gimme != G_ARRAY) {
2782 if (gimme != G_VOID)
2783 XPUSHs(boolSV(max));
2789 PUSHs(sv_2mortal(newSViv(PL_statcache.st_dev)));
2790 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ino)));
2791 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_mode)));
2792 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_nlink)));
2793 #if Uid_t_size > IVSIZE
2794 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_uid)));
2796 # if Uid_t_sign <= 0
2797 PUSHs(sv_2mortal(newSViv(PL_statcache.st_uid)));
2799 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_uid)));
2802 #if Gid_t_size > IVSIZE
2803 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_gid)));
2805 # if Gid_t_sign <= 0
2806 PUSHs(sv_2mortal(newSViv(PL_statcache.st_gid)));
2808 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_gid)));
2811 #ifdef USE_STAT_RDEV
2812 PUSHs(sv_2mortal(newSViv(PL_statcache.st_rdev)));
2814 PUSHs(sv_2mortal(newSVpvn("", 0)));
2816 #if Off_t_size > IVSIZE
2817 PUSHs(sv_2mortal(newSVnv((NV)PL_statcache.st_size)));
2819 PUSHs(sv_2mortal(newSViv(PL_statcache.st_size)));
2822 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_atime)));
2823 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_mtime)));
2824 PUSHs(sv_2mortal(newSVnv(PL_statcache.st_ctime)));
2826 PUSHs(sv_2mortal(newSViv(PL_statcache.st_atime)));
2827 PUSHs(sv_2mortal(newSViv(PL_statcache.st_mtime)));
2828 PUSHs(sv_2mortal(newSViv(PL_statcache.st_ctime)));
2830 #ifdef USE_STAT_BLOCKS
2831 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blksize)));
2832 PUSHs(sv_2mortal(newSVuv(PL_statcache.st_blocks)));
2834 PUSHs(sv_2mortal(newSVpvn("", 0)));
2835 PUSHs(sv_2mortal(newSVpvn("", 0)));
2841 /* This macro is used by the stacked filetest operators :
2842 * if the previous filetest failed, short-circuit and pass its value.
2843 * Else, discard it from the stack and continue. --rgs
2845 #define STACKED_FTEST_CHECK if (PL_op->op_private & OPpFT_STACKED) { \
2846 if (TOPs == &PL_sv_no || TOPs == &PL_sv_undef) { RETURN; } \
2847 else { (void)POPs; PUTBACK; } \
2853 /* Not const, because things tweak this below. Not bool, because there's
2854 no guarantee that OPp_FT_ACCESS is <= CHAR_MAX */
2855 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2856 I32 use_access = PL_op->op_private & OPpFT_ACCESS;
2857 /* Giving some sort of initial value silences compilers. */
2859 int access_mode = R_OK;
2861 int access_mode = 0;
2864 /* access_mode is never used, but leaving use_access in makes the
2865 conditional compiling below much clearer. */
2868 int stat_mode = S_IRUSR;
2870 bool effective = FALSE;
2873 STACKED_FTEST_CHECK;
2875 switch (PL_op->op_type) {
2877 #if !(defined(HAS_ACCESS) && defined(R_OK))
2883 #if defined(HAS_ACCESS) && defined(W_OK)
2888 stat_mode = S_IWUSR;
2892 #if defined(HAS_ACCESS) && defined(X_OK)
2897 stat_mode = S_IXUSR;
2901 #ifdef PERL_EFF_ACCESS
2904 stat_mode = S_IWUSR;
2908 #ifndef PERL_EFF_ACCESS
2916 #ifdef PERL_EFF_ACCESS
2921 stat_mode = S_IXUSR;
2927 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
2928 const char *const name = POPpx;
2930 # ifdef PERL_EFF_ACCESS
2931 result = PERL_EFF_ACCESS(name, access_mode);
2933 DIE(aTHX_ "panic: attempt to call PERL_EFF_ACCESS in %s",
2939 result = access(name, access_mode);
2941 DIE(aTHX_ "panic: attempt to call access() in %s", OP_NAME(PL_op));
2956 if (cando(stat_mode, effective, &PL_statcache))
2964 const int op_type = PL_op->op_type;
2966 STACKED_FTEST_CHECK;
2971 if (op_type == OP_FTIS)
2974 /* You can't dTARGET inside OP_FTIS, because you'll get
2975 "panic: pad_sv po" - the op is not flagged to have a target. */
2979 #if Off_t_size > IVSIZE
2980 PUSHn(PL_statcache.st_size);
2982 PUSHi(PL_statcache.st_size);
2986 PUSHn( (((NV)PL_basetime - PL_statcache.st_mtime)) / 86400.0 );
2989 PUSHn( (((NV)PL_basetime - PL_statcache.st_atime)) / 86400.0 );
2992 PUSHn( (((NV)PL_basetime - PL_statcache.st_ctime)) / 86400.0 );
3004 /* I believe that all these three are likely to be defined on most every
3005 system these days. */
3007 if(PL_op->op_type == OP_FTSUID)
3011 if(PL_op->op_type == OP_FTSGID)
3015 if(PL_op->op_type == OP_FTSVTX)
3019 STACKED_FTEST_CHECK;
3024 switch (PL_op->op_type) {
3026 if (PL_statcache.st_uid == PL_uid)
3030 if (PL_statcache.st_uid == PL_euid)
3034 if (PL_statcache.st_size == 0)
3038 if (S_ISSOCK(PL_statcache.st_mode))
3042 if (S_ISCHR(PL_statcache.st_mode))
3046 if (S_ISBLK(PL_statcache.st_mode))
3050 if (S_ISREG(PL_statcache.st_mode))
3054 if (S_ISDIR(PL_statcache.st_mode))
3058 if (S_ISFIFO(PL_statcache.st_mode))
3063 if (PL_statcache.st_mode & S_ISUID)
3069 if (PL_statcache.st_mode & S_ISGID)
3075 if (PL_statcache.st_mode & S_ISVTX)
3085 I32 result = my_lstat();
3089 if (S_ISLNK(PL_statcache.st_mode))
3101 STACKED_FTEST_CHECK;
3103 if (PL_op->op_flags & OPf_REF)
3105 else if (isGV(TOPs))
3107 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3108 gv = (GV*)SvRV(POPs);
3110 gv = gv_fetchsv(tmpsv = POPs, FALSE, SVt_PVIO);
3112 if (GvIO(gv) && IoIFP(GvIOp(gv)))
3113 fd = PerlIO_fileno(IoIFP(GvIOp(gv)));
3114 else if (tmpsv && SvOK(tmpsv)) {
3115 const char *tmps = SvPV_nolen_const(tmpsv);
3123 if (PerlLIO_isatty(fd))
3128 #if defined(atarist) /* this will work with atariST. Configure will
3129 make guesses for other systems. */
3130 # define FILE_base(f) ((f)->_base)
3131 # define FILE_ptr(f) ((f)->_ptr)
3132 # define FILE_cnt(f) ((f)->_cnt)
3133 # define FILE_bufsiz(f) ((f)->_cnt + ((f)->_ptr - (f)->_base))
3143 register STDCHAR *s;
3149 STACKED_FTEST_CHECK;
3151 if (PL_op->op_flags & OPf_REF)
3153 else if (isGV(TOPs))
3155 else if (SvROK(TOPs) && isGV(SvRV(TOPs)))
3156 gv = (GV*)SvRV(POPs);
3162 if (gv == PL_defgv) {
3164 io = GvIO(PL_statgv);
3167 goto really_filename;
3172 PL_laststatval = -1;
3173 sv_setpvn(PL_statname, "", 0);
3174 io = GvIO(PL_statgv);
3176 if (io && IoIFP(io)) {
3177 if (! PerlIO_has_base(IoIFP(io)))
3178 DIE(aTHX_ "-T and -B not implemented on filehandles");
3179 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache);
3180 if (PL_laststatval < 0)
3182 if (S_ISDIR(PL_statcache.st_mode)) { /* handle NFS glitch */
3183 if (PL_op->op_type == OP_FTTEXT)
3188 if (PerlIO_get_cnt(IoIFP(io)) <= 0) {
3189 i = PerlIO_getc(IoIFP(io));
3191 (void)PerlIO_ungetc(IoIFP(io),i);
3193 if (PerlIO_get_cnt(IoIFP(io)) <= 0) /* null file is anything */
3195 len = PerlIO_get_bufsiz(IoIFP(io));
3196 s = (STDCHAR *) PerlIO_get_base(IoIFP(io));
3197 /* sfio can have large buffers - limit to 512 */
3202 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) {
3204 report_evil_fh(gv, GvIO(gv), PL_op->op_type);
3206 SETERRNO(EBADF,RMS_IFI);
3214 PL_laststype = OP_STAT;
3215 sv_setpv(PL_statname, SvPV_nolen_const(sv));
3216 if (!(fp = PerlIO_open(SvPVX_const(PL_statname), "r"))) {
3217 if (ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(PL_statname),
3219 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
3222 PL_laststatval = PerlLIO_fstat(PerlIO_fileno(fp), &PL_statcache);
3223 if (PL_laststatval < 0) {
3224 (void)PerlIO_close(fp);
3227 PerlIO_binmode(aTHX_ fp, '<', O_BINARY, Nullch);
3228 len = PerlIO_read(fp, tbuf, sizeof(tbuf));
3229 (void)PerlIO_close(fp);
3231 if (S_ISDIR(PL_statcache.st_mode) && PL_op->op_type == OP_FTTEXT)
3232 RETPUSHNO; /* special case NFS directories */
3233 RETPUSHYES; /* null file is anything */
3238 /* now scan s to look for textiness */
3239 /* XXX ASCII dependent code */
3241 #if defined(DOSISH) || defined(USEMYBINMODE)
3242 /* ignore trailing ^Z on short files */
3243 if (len && len < sizeof(tbuf) && tbuf[len-1] == 26)
3247 for (i = 0; i < len; i++, s++) {
3248 if (!*s) { /* null never allowed in text */
3253 else if (!(isPRINT(*s) || isSPACE(*s)))
3256 else if (*s & 128) {
3258 if (IN_LOCALE_RUNTIME && isALPHA_LC(*s))
3261 /* utf8 characters don't count as odd */
3262 if (UTF8_IS_START(*s)) {
3263 int ulen = UTF8SKIP(s);
3264 if (ulen < len - i) {
3266 for (j = 1; j < ulen; j++) {
3267 if (!UTF8_IS_CONTINUATION(s[j]))
3270 --ulen; /* loop does extra increment */
3280 *s != '\n' && *s != '\r' && *s != '\b' &&
3281 *s != '\t' && *s != '\f' && *s != 27)
3286 if ((odd * 3 > len) == (PL_op->op_type == OP_FTTEXT)) /* allow 1/3 odd */
3297 const char *tmps = 0;
3301 SV * const sv = POPs;
3302 if (SvTYPE(sv) == SVt_PVGV) {
3305 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
3309 tmps = SvPVx_nolen_const(sv);
3313 if( !gv && (!tmps || !*tmps) ) {
3314 HV * const table = GvHVn(PL_envgv);
3317 if ( (svp = hv_fetch(table, "HOME", 4, FALSE))
3318 || (svp = hv_fetch(table, "LOGDIR", 6, FALSE))
3320 || (svp = hv_fetch(table, "SYS$LOGIN", 9, FALSE))
3325 deprecate("chdir('') or chdir(undef) as chdir()");
3326 tmps = SvPV_nolen_const(*svp);
3330 TAINT_PROPER("chdir");
3335 TAINT_PROPER("chdir");
3338 IO* const io = GvIO(gv);
3341 PUSHi(fchdir(PerlIO_fileno(IoIFP(io))) >= 0);
3343 else if (IoDIRP(io)) {
3345 PUSHi(fchdir(dirfd(IoDIRP(io))) >= 0);
3347 DIE(aTHX_ PL_no_func, "dirfd");
3358 DIE(aTHX_ PL_no_func, "fchdir");
3362 PUSHi( PerlDir_chdir(tmps) >= 0 );
3364 /* Clear the DEFAULT element of ENV so we'll get the new value
3366 hv_delete(GvHVn(PL_envgv),"DEFAULT",7,G_DISCARD);
3373 dSP; dMARK; dTARGET;
3374 const I32 value = (I32)apply(PL_op->op_type, MARK, SP);
3385 char * const tmps = POPpx;
3386 TAINT_PROPER("chroot");
3387 PUSHi( chroot(tmps) >= 0 );
3390 DIE(aTHX_ PL_no_func, "chroot");
3398 const char * const tmps2 = POPpconstx;
3399 const char * const tmps = SvPV_nolen_const(TOPs);
3400 TAINT_PROPER("rename");
3402 anum = PerlLIO_rename(tmps, tmps2);
3404 if (!(anum = PerlLIO_stat(tmps, &PL_statbuf))) {
3405 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
3408 if (PL_euid || PerlLIO_stat(tmps2, &PL_statbuf) < 0 || !S_ISDIR(PL_statbuf.st_mode))
3409 (void)UNLINK(tmps2);
3410 if (!(anum = link(tmps, tmps2)))
3411 anum = UNLINK(tmps);
3419 #if defined(HAS_LINK) || defined(HAS_SYMLINK)
3423 const int op_type = PL_op->op_type;
3427 if (op_type == OP_LINK)
3428 DIE(aTHX_ PL_no_func, "link");
3430 # ifndef HAS_SYMLINK
3431 if (op_type == OP_SYMLINK)
3432 DIE(aTHX_ PL_no_func, "symlink");
3436 const char * const tmps2 = POPpconstx;
3437 const char * const tmps = SvPV_nolen_const(TOPs);
3438 TAINT_PROPER(PL_op_desc[op_type]);
3440 # if defined(HAS_LINK)
3441 # if defined(HAS_SYMLINK)
3442 /* Both present - need to choose which. */
3443 (op_type == OP_LINK) ?
3444 PerlLIO_link(tmps, tmps2) : symlink(tmps, tmps2);
3446 /* Only have link, so calls to pp_symlink will have DIE()d above. */
3447 PerlLIO_link(tmps, tmps2);
3450 # if defined(HAS_SYMLINK)
3451 /* Only have symlink, so calls to pp_link will have DIE()d above. */
3452 symlink(tmps, tmps2);
3457 SETi( result >= 0 );
3464 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
3474 char buf[MAXPATHLEN];
3477 #ifndef INCOMPLETE_TAINTS
3481 len = readlink(tmps, buf, sizeof(buf) - 1);
3489 RETSETUNDEF; /* just pretend it's a normal file */
3493 #if !defined(HAS_MKDIR) || !defined(HAS_RMDIR)
3495 S_dooneliner(pTHX_ const char *cmd, const char *filename)
3497 char * const save_filename = filename;
3503 Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
3504 strcpy(cmdline, cmd);
3505 strcat(cmdline, " ");
3506 for (s = cmdline + strlen(cmdline); *filename; ) {
3511 myfp = PerlProc_popen(cmdline, "r");
3515 SV *tmpsv = sv_newmortal();
3516 /* Need to save/restore 'PL_rs' ?? */
3517 s = sv_gets(tmpsv, myfp, 0);
3518 (void)PerlProc_pclose(myfp);
3522 #ifdef HAS_SYS_ERRLIST
3527 /* you don't see this */
3529 #ifdef HAS_SYS_ERRLIST
3537 if (instr(s, errmsg)) {
3544 #define EACCES EPERM
3546 if (instr(s, "cannot make"))
3547 SETERRNO(EEXIST,RMS_FEX);
3548 else if (instr(s, "existing file"))
3549 SETERRNO(EEXIST,RMS_FEX);
3550 else if (instr(s, "ile exists"))
3551 SETERRNO(EEXIST,RMS_FEX);
3552 else if (instr(s, "non-exist"))
3553 SETERRNO(ENOENT,RMS_FNF);
3554 else if (instr(s, "does not exist"))
3555 SETERRNO(ENOENT,RMS_FNF);
3556 else if (instr(s, "not empty"))
3557 SETERRNO(EBUSY,SS_DEVOFFLINE);
3558 else if (instr(s, "cannot access"))
3559 SETERRNO(EACCES,RMS_PRV);
3561 SETERRNO(EPERM,RMS_PRV);
3564 else { /* some mkdirs return no failure indication */
3565 anum = (PerlLIO_stat(save_filename, &PL_statbuf) >= 0);
3566 if (PL_op->op_type == OP_RMDIR)
3571 SETERRNO(EACCES,RMS_PRV); /* a guess */
3580 /* This macro removes trailing slashes from a directory name.
3581 * Different operating and file systems take differently to
3582 * trailing slashes. According to POSIX 1003.1 1996 Edition
3583 * any number of trailing slashes should be allowed.
3584 * Thusly we snip them away so that even non-conforming
3585 * systems are happy.
3586 * We should probably do this "filtering" for all
3587 * the functions that expect (potentially) directory names:
3588 * -d, chdir(), chmod(), chown(), chroot(), fcntl()?,
3589 * (mkdir()), opendir(), rename(), rmdir(), stat(). --jhi */
3591 #define TRIMSLASHES(tmps,len,copy) (tmps) = SvPV_const(TOPs, (len)); \
3592 if ((len) > 1 && (tmps)[(len)-1] == '/') { \
3595 } while ((len) > 1 && (tmps)[(len)-1] == '/'); \
3596 (tmps) = savepvn((tmps), (len)); \
3609 const int mode = (MAXARG > 1) ? POPi : 0777;
3611 TRIMSLASHES(tmps,len,copy);
3613 TAINT_PROPER("mkdir");
3615 SETi( PerlDir_mkdir(tmps, mode) >= 0 );
3617 SETi( dooneliner("mkdir", tmps) );
3618 oldumask = PerlLIO_umask(0);
3619 PerlLIO_umask(oldumask);
3620 PerlLIO_chmod(tmps, (mode & ~oldumask) & 0777);
3634 TRIMSLASHES(tmps,len,copy);
3635 TAINT_PROPER("rmdir");
3637 SETi( PerlDir_rmdir(tmps) >= 0 );
3639 SETi( dooneliner("rmdir", tmps) );
3646 /* Directory calls. */
3650 #if defined(Direntry_t) && defined(HAS_READDIR)
3652 const char * const dirname = POPpconstx;
3653 GV * const gv = (GV*)POPs;
3654 register IO * const io = GvIOn(gv);
3660 PerlDir_close(IoDIRP(io));
3661 if (!(IoDIRP(io) = PerlDir_open(dirname)))
3667 SETERRNO(EBADF,RMS_DIR);
3670 DIE(aTHX_ PL_no_dir_func, "opendir");
3676 #if !defined(Direntry_t) || !defined(HAS_READDIR)
3677 DIE(aTHX_ PL_no_dir_func, "readdir");
3679 #if !defined(I_DIRENT) && !defined(VMS)
3680 Direntry_t *readdir (DIR *);
3685 const I32 gimme = GIMME;
3686 GV * const gv = (GV *)POPs;
3687 register const Direntry_t *dp;
3688 register IO * const io = GvIOn(gv);
3690 if (!io || !IoDIRP(io))
3694 dp = (Direntry_t *)PerlDir_read(IoDIRP(io));
3698 sv = newSVpvn(dp->d_name, dp->d_namlen);
3700 sv = newSVpv(dp->d_name, 0);
3702 #ifndef INCOMPLETE_TAINTS
3703 if (!(IoFLAGS(io) & IOf_UNTAINT))
3706 XPUSHs(sv_2mortal(sv));
3708 while (gimme == G_ARRAY);
3710 if (!dp && gimme != G_ARRAY)
3717 SETERRNO(EBADF,RMS_ISI);
3718 if (GIMME == G_ARRAY)
3727 #if defined(HAS_TELLDIR) || defined(telldir)
3729 /* XXX does _anyone_ need this? --AD 2/20/1998 */
3730 /* XXX netbsd still seemed to.
3731 XXX HAS_TELLDIR_PROTO is new style, NEED_TELLDIR_PROTO is old style.
3732 --JHI 1999-Feb-02 */
3733 # if !defined(HAS_TELLDIR_PROTO) || defined(NEED_TELLDIR_PROTO)
3734 long telldir (DIR *);
3736 GV * const gv = (GV*)POPs;
3737 register IO * const io = GvIOn(gv);
3739 if (!io || !IoDIRP(io))
3742 PUSHi( PerlDir_tell(IoDIRP(io)) );
3746 SETERRNO(EBADF,RMS_ISI);
3749 DIE(aTHX_ PL_no_dir_func, "telldir");
3755 #if defined(HAS_SEEKDIR) || defined(seekdir)
3757 const long along = POPl;
3758 GV * const gv = (GV*)POPs;
3759 register IO * const io = GvIOn(gv);
3761 if (!io || !IoDIRP(io))
3764 (void)PerlDir_seek(IoDIRP(io), along);
3769 SETERRNO(EBADF,RMS_ISI);
3772 DIE(aTHX_ PL_no_dir_func, "seekdir");
3778 #if defined(HAS_REWINDDIR) || defined(rewinddir)
3780 GV * const gv = (GV*)POPs;
3781 register IO * const io = GvIOn(gv);
3783 if (!io || !IoDIRP(io))
3786 (void)PerlDir_rewind(IoDIRP(io));
3790 SETERRNO(EBADF,RMS_ISI);
3793 DIE(aTHX_ PL_no_dir_func, "rewinddir");
3799 #if defined(Direntry_t) && defined(HAS_READDIR)
3801 GV * const gv = (GV*)POPs;
3802 register IO * const io = GvIOn(gv);
3804 if (!io || !IoDIRP(io))
3807 #ifdef VOID_CLOSEDIR
3808 PerlDir_close(IoDIRP(io));
3810 if (PerlDir_close(IoDIRP(io)) < 0) {
3811 IoDIRP(io) = 0; /* Don't try to close again--coredumps on SysV */
3820 SETERRNO(EBADF,RMS_IFI);
3823 DIE(aTHX_ PL_no_dir_func, "closedir");
3827 /* Process control. */
3836 PERL_FLUSHALL_FOR_CHILD;
3837 childpid = PerlProc_fork();
3841 GV * const tmpgv = gv_fetchpv("$", TRUE, SVt_PV);
3843 SvREADONLY_off(GvSV(tmpgv));
3844 sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid());
3845 SvREADONLY_on(GvSV(tmpgv));
3847 #ifdef THREADS_HAVE_PIDS
3848 PL_ppid = (IV)getppid();
3850 #ifdef PERL_USES_PL_PIDSTATUS
3851 hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
3857 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3862 PERL_FLUSHALL_FOR_CHILD;
3863 childpid = PerlProc_fork();
3869 DIE(aTHX_ PL_no_func, "fork");
3876 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3881 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3882 childpid = wait4pid(-1, &argflags, 0);
3884 while ((childpid = wait4pid(-1, &argflags, 0)) == -1 &&
3889 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3890 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3891 STATUS_NATIVE_CHILD_SET((childpid && childpid != -1) ? argflags : -1);
3893 STATUS_NATIVE_CHILD_SET((childpid > 0) ? argflags : -1);
3898 DIE(aTHX_ PL_no_func, "wait");
3904 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(MACOS_TRADITIONAL)
3913 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3914 result = wait4pid(pid, &argflags, optype);
3916 while ((result = wait4pid(pid, &argflags, optype)) == -1 &&
3921 # if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
3922 /* 0 and -1 are both error returns (the former applies to WNOHANG case) */
3923 STATUS_NATIVE_CHILD_SET((result && result != -1) ? argflags : -1);
3925 STATUS_NATIVE_CHILD_SET((result > 0) ? argflags : -1);
3930 DIE(aTHX_ PL_no_func, "waitpid");
3936 dSP; dMARK; dORIGMARK; dTARGET;
3942 while (++MARK <= SP) {
3943 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
3948 TAINT_PROPER("system");
3950 PERL_FLUSHALL_FOR_CHILD;
3951 #if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
3957 if (PerlProc_pipe(pp) >= 0)
3959 while ((childpid = PerlProc_fork()) == -1) {
3960 if (errno != EAGAIN) {
3965 PerlLIO_close(pp[0]);
3966 PerlLIO_close(pp[1]);
3973 Sigsave_t ihand,qhand; /* place to save signals during system() */
3977 PerlLIO_close(pp[1]);
3979 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &ihand);
3980 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qhand);
3983 result = wait4pid(childpid, &status, 0);
3984 } while (result == -1 && errno == EINTR);
3986 (void)rsignal_restore(SIGINT, &ihand);
3987 (void)rsignal_restore(SIGQUIT, &qhand);
3989 STATUS_NATIVE_CHILD_SET(result == -1 ? -1 : status);
3990 do_execfree(); /* free any memory child malloced on fork */
3996 while (n < sizeof(int)) {
3997 n1 = PerlLIO_read(pp[0],
3998 (void*)(((char*)&errkid)+n),
4004 PerlLIO_close(pp[0]);
4005 if (n) { /* Error */
4006 if (n != sizeof(int))
4007 DIE(aTHX_ "panic: kid popen errno read");
4008 errno = errkid; /* Propagate errno from kid */
4009 STATUS_NATIVE_CHILD_SET(-1);
4012 XPUSHi(STATUS_CURRENT);
4016 PerlLIO_close(pp[0]);
4017 #if defined(HAS_FCNTL) && defined(F_SETFD)
4018 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
4021 if (PL_op->op_flags & OPf_STACKED) {
4022 SV *really = *++MARK;
4023 value = (I32)do_aexec5(really, MARK, SP, pp[1], did_pipes);
4025 else if (SP - MARK != 1)
4026 value = (I32)do_aexec5(Nullsv, MARK, SP, pp[1], did_pipes);
4028 value = (I32)do_exec3(SvPVx_nolen(sv_mortalcopy(*SP)), pp[1], did_pipes);
4032 #else /* ! FORK or VMS or OS/2 */
4035 if (PL_op->op_flags & OPf_STACKED) {
4036 SV *really = *++MARK;
4037 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4038 value = (I32)do_aspawn(really, MARK, SP);
4040 value = (I32)do_aspawn(really, (void **)MARK, (void **)SP);
4043 else if (SP - MARK != 1) {
4044 # if defined(WIN32) || defined(OS2) || defined(__SYMBIAN32__)
4045 value = (I32)do_aspawn(Nullsv, MARK, SP);
4047 value = (I32)do_aspawn(Nullsv, (void **)MARK, (void **)SP);
4051 value = (I32)do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4053 if (PL_statusvalue == -1) /* hint that value must be returned as is */
4055 STATUS_NATIVE_CHILD_SET(value);
4058 XPUSHi(result ? value : STATUS_CURRENT);
4059 #endif /* !FORK or VMS */
4065 dSP; dMARK; dORIGMARK; dTARGET;
4070 while (++MARK <= SP) {
4071 (void)SvPV_nolen_const(*MARK); /* stringify for taint check */
4076 TAINT_PROPER("exec");
4078 PERL_FLUSHALL_FOR_CHILD;
4079 if (PL_op->op_flags & OPf_STACKED) {
4080 SV *really = *++MARK;
4081 value = (I32)do_aexec(really, MARK, SP);
4083 else if (SP - MARK != 1)
4085 value = (I32)vms_do_aexec(Nullsv, MARK, SP);
4089 (void ) do_aspawn(Nullsv, MARK, SP);
4093 value = (I32)do_aexec(Nullsv, MARK, SP);
4098 value = (I32)vms_do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4101 (void) do_spawn(SvPVx_nolen(sv_mortalcopy(*SP)));
4104 value = (I32)do_exec(SvPVx_nolen(sv_mortalcopy(*SP)));
4118 # ifdef THREADS_HAVE_PIDS
4119 if (PL_ppid != 1 && getppid() == 1)
4120 /* maybe the parent process has died. Refresh ppid cache */
4124 XPUSHi( getppid() );
4128 DIE(aTHX_ PL_no_func, "getppid");
4144 pgrp = (I32)BSD_GETPGRP(pid);
4146 if (pid != 0 && pid != PerlProc_getpid())
4147 DIE(aTHX_ "POSIX getpgrp can't take an argument");
4153 DIE(aTHX_ PL_no_func, "getpgrp()");
4172 TAINT_PROPER("setpgrp");
4174 SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
4176 if ((pgrp != 0 && pgrp != PerlProc_getpid())
4177 || (pid != 0 && pid != PerlProc_getpid()))
4179 DIE(aTHX_ "setpgrp can't take arguments");
4181 SETi( setpgrp() >= 0 );
4182 #endif /* USE_BSDPGRP */
4185 DIE(aTHX_ PL_no_func, "setpgrp()");
4191 #ifdef HAS_GETPRIORITY
4195 SETi( getpriority(which, who) );
4198 DIE(aTHX_ PL_no_func, "getpriority()");
4204 #ifdef HAS_SETPRIORITY
4209 TAINT_PROPER("setpriority");
4210 SETi( setpriority(which, who, niceval) >= 0 );
4213 DIE(aTHX_ PL_no_func, "setpriority()");
4223 XPUSHn( time(Null(Time_t*)) );
4225 XPUSHi( time(Null(Time_t*)) );
4236 (void)PerlProc_times(&PL_timesbuf);
4238 (void)PerlProc_times((tbuffer_t *)&PL_timesbuf); /* time.h uses different name for */
4239 /* struct tms, though same data */
4243 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_utime)/(NV)PL_clocktick)));
4244 if (GIMME == G_ARRAY) {
4245 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_stime)/(NV)PL_clocktick)));
4246 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cutime)/(NV)PL_clocktick)));
4247 PUSHs(sv_2mortal(newSVnv(((NV)PL_timesbuf.tms_cstime)/(NV)PL_clocktick)));
4253 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4255 if (GIMME == G_ARRAY) {
4256 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4257 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4258 PUSHs(sv_2mortal(newSVnv((NV)0.0)));
4262 DIE(aTHX_ "times not implemented");
4264 #endif /* HAS_TIMES */
4267 #ifdef LOCALTIME_EDGECASE_BROKEN
4268 static struct tm *S_my_localtime (pTHX_ Time_t *tp)
4273 /* No workarounds in the valid range */
4274 if (!tp || *tp < 0x7fff573f || *tp >= 0x80000000)
4275 return (localtime (tp));
4277 /* This edge case is to workaround the undefined behaviour, where the
4278 * TIMEZONE makes the time go beyond the defined range.
4279 * gmtime (0x7fffffff) => 2038-01-19 03:14:07
4280 * If there is a negative offset in TZ, like MET-1METDST, some broken
4281 * implementations of localtime () (like AIX 5.2) barf with bogus
4283 * 0x7fffffff gmtime 2038-01-19 03:14:07
4284 * 0x7fffffff localtime 1901-12-13 21:45:51
4285 * 0x7fffffff mylocaltime 2038-01-19 04:14:07
4286 * 0x3c19137f gmtime 2001-12-13 20:45:51
4287 * 0x3c19137f localtime 2001-12-13 21:45:51
4288 * 0x3c19137f mylocaltime 2001-12-13 21:45:51
4289 * Given that legal timezones are typically between GMT-12 and GMT+12
4290 * we turn back the clock 23 hours before calling the localtime
4291 * function, and add those to the return value. This will never cause
4292 * day wrapping problems, since the edge case is Tue Jan *19*
4294 T = *tp - 82800; /* 23 hour. allows up to GMT-23 */
4297 if (P->tm_hour >= 24) {
4299 P->tm_mday++; /* 18 -> 19 */
4300 P->tm_wday++; /* Mon -> Tue */
4301 P->tm_yday++; /* 18 -> 19 */
4304 } /* S_my_localtime */
4311 const struct tm *tmbuf;
4312 static const char * const dayname[] =
4313 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
4314 static const char * const monname[] =
4315 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4316 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
4322 when = (Time_t)SvNVx(POPs);
4324 when = (Time_t)SvIVx(POPs);
4327 if (PL_op->op_type == OP_LOCALTIME)
4328 #ifdef LOCALTIME_EDGECASE_BROKEN
4329 tmbuf = S_my_localtime(aTHX_ &when);
4331 tmbuf = localtime(&when);
4334 tmbuf = gmtime(&when);
4336 if (GIMME != G_ARRAY) {
4342 tsv = Perl_newSVpvf(aTHX_ "%s %s %2d %02d:%02d:%02d %d",
4343 dayname[tmbuf->tm_wday],
4344 monname[tmbuf->tm_mon],
4349 tmbuf->tm_year + 1900);
4350 PUSHs(sv_2mortal(tsv));
4355 PUSHs(sv_2mortal(newSViv(tmbuf->tm_sec)));
4356 PUSHs(sv_2mortal(newSViv(tmbuf->tm_min)));
4357 PUSHs(sv_2mortal(newSViv(tmbuf->tm_hour)));
4358 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mday)));
4359 PUSHs(sv_2mortal(newSViv(tmbuf->tm_mon)));
4360 PUSHs(sv_2mortal(newSViv(tmbuf->tm_year)));
4361 PUSHs(sv_2mortal(newSViv(tmbuf->tm_wday)));
4362 PUSHs(sv_2mortal(newSViv(tmbuf->tm_yday)));
4363 PUSHs(sv_2mortal(newSViv(tmbuf->tm_isdst)));
4374 anum = alarm((unsigned int)anum);
4381 DIE(aTHX_ PL_no_func, "alarm");
4392 (void)time(&lasttime);
4397 PerlProc_sleep((unsigned int)duration);
4400 XPUSHi(when - lasttime);
4404 /* Shared memory. */
4405 /* Merged with some message passing. */
4409 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4410 dSP; dMARK; dTARGET;
4411 const int op_type = PL_op->op_type;
4416 value = (I32)(do_msgsnd(MARK, SP) >= 0);
4419 value = (I32)(do_msgrcv(MARK, SP) >= 0);
4422 value = (I32)(do_semop(MARK, SP) >= 0);
4425 value = (I32)(do_shmio(op_type, MARK, SP) >= 0);
4441 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4442 dSP; dMARK; dTARGET;
4443 int anum = do_ipcget(PL_op->op_type, MARK, SP);
4450 DIE(aTHX_ "System V IPC is not implemented on this machine");
4456 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
4457 dSP; dMARK; dTARGET;
4458 int anum = do_ipcctl(PL_op->op_type, MARK, SP);
4466 PUSHp(zero_but_true, ZBTLEN);
4474 /* Get system info. */
4478 #if defined(HAS_GETHOSTBYNAME) || defined(HAS_GETHOSTBYADDR) || defined(HAS_GETHOSTENT)
4480 I32 which = PL_op->op_type;
4481 register char **elem;
4483 #ifndef HAS_GETHOST_PROTOS /* XXX Do we need individual probes? */
4484 struct hostent *gethostbyaddr(Netdb_host_t, Netdb_hlen_t, int);
4485 struct hostent *gethostbyname(Netdb_name_t);
4486 struct hostent *gethostent(void);
4488 struct hostent *hent;
4492 if (which == OP_GHBYNAME) {
4493 #ifdef HAS_GETHOSTBYNAME
4494 char* name = POPpbytex;
4495 hent = PerlSock_gethostbyname(name);
4497 DIE(aTHX_ PL_no_sock_func, "gethostbyname");
4500 else if (which == OP_GHBYADDR) {
4501 #ifdef HAS_GETHOSTBYADDR
4502 int addrtype = POPi;
4505 Netdb_host_t addr = (Netdb_host_t) SvPVbyte(addrsv, addrlen);
4507 hent = PerlSock_gethostbyaddr(addr, (Netdb_hlen_t) addrlen, addrtype);
4509 DIE(aTHX_ PL_no_sock_func, "gethostbyaddr");
4513 #ifdef HAS_GETHOSTENT
4514 hent = PerlSock_gethostent();
4516 DIE(aTHX_ PL_no_sock_func, "gethostent");
4519 #ifdef HOST_NOT_FOUND
4521 #ifdef USE_REENTRANT_API
4522 # ifdef USE_GETHOSTENT_ERRNO
4523 h_errno = PL_reentrant_buffer->_gethostent_errno;
4526 STATUS_UNIX_SET(h_errno);
4530 if (GIMME != G_ARRAY) {
4531 PUSHs(sv = sv_newmortal());
4533 if (which == OP_GHBYNAME) {
4535 sv_setpvn(sv, hent->h_addr, hent->h_length);
4538 sv_setpv(sv, (char*)hent->h_name);
4544 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4545 sv_setpv(sv, (char*)hent->h_name);
4546 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4547 for (elem = hent->h_aliases; elem && *elem; elem++) {
4548 sv_catpv(sv, *elem);
4550 sv_catpvn(sv, " ", 1);
4552 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4553 sv_setiv(sv, (IV)hent->h_addrtype);
4554 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4555 len = hent->h_length;
4556 sv_setiv(sv, (IV)len);
4558 for (elem = hent->h_addr_list; elem && *elem; elem++) {
4559 XPUSHs(sv = sv_mortalcopy(&PL_sv_no));
4560 sv_setpvn(sv, *elem, len);
4563 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4565 sv_setpvn(sv, hent->h_addr, len);
4570 DIE(aTHX_ PL_no_sock_func, "gethostent");
4576 #if defined(HAS_GETNETBYNAME) || defined(HAS_GETNETBYADDR) || defined(HAS_GETNETENT)
4578 I32 which = PL_op->op_type;
4579 register char **elem;
4581 #ifndef HAS_GETNET_PROTOS /* XXX Do we need individual probes? */
4582 struct netent *getnetbyaddr(Netdb_net_t, int);
4583 struct netent *getnetbyname(Netdb_name_t);
4584 struct netent *getnetent(void);
4586 struct netent *nent;
4588 if (which == OP_GNBYNAME){
4589 #ifdef HAS_GETNETBYNAME
4590 char *name = POPpbytex;
4591 nent = PerlSock_getnetbyname(name);
4593 DIE(aTHX_ PL_no_sock_func, "getnetbyname");
4596 else if (which == OP_GNBYADDR) {
4597 #ifdef HAS_GETNETBYADDR
4598 int addrtype = POPi;
4599 Netdb_net_t addr = (Netdb_net_t) (U32)POPu;
4600 nent = PerlSock_getnetbyaddr(addr, addrtype);
4602 DIE(aTHX_ PL_no_sock_func, "getnetbyaddr");
4606 #ifdef HAS_GETNETENT
4607 nent = PerlSock_getnetent();
4609 DIE(aTHX_ PL_no_sock_func, "getnetent");
4612 #ifdef HOST_NOT_FOUND
4614 #ifdef USE_REENTRANT_API
4615 # ifdef USE_GETNETENT_ERRNO
4616 h_errno = PL_reentrant_buffer->_getnetent_errno;
4619 STATUS_UNIX_SET(h_errno);
4624 if (GIMME != G_ARRAY) {
4625 PUSHs(sv = sv_newmortal());
4627 if (which == OP_GNBYNAME)
4628 sv_setiv(sv, (IV)nent->n_net);
4630 sv_setpv(sv, nent->n_name);
4636 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4637 sv_setpv(sv, nent->n_name);
4638 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4639 for (elem = nent->n_aliases; elem && *elem; elem++) {
4640 sv_catpv(sv, *elem);
4642 sv_catpvn(sv, " ", 1);
4644 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4645 sv_setiv(sv, (IV)nent->n_addrtype);
4646 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4647 sv_setiv(sv, (IV)nent->n_net);
4652 DIE(aTHX_ PL_no_sock_func, "getnetent");
4658 #if defined(HAS_GETPROTOBYNAME) || defined(HAS_GETPROTOBYNUMBER) || defined(HAS_GETPROTOENT)
4660 I32 which = PL_op->op_type;
4661 register char **elem;
4663 #ifndef HAS_GETPROTO_PROTOS /* XXX Do we need individual probes? */
4664 struct protoent *getprotobyname(Netdb_name_t);
4665 struct protoent *getprotobynumber(int);
4666 struct protoent *getprotoent(void);
4668 struct protoent *pent;
4670 if (which == OP_GPBYNAME) {
4671 #ifdef HAS_GETPROTOBYNAME
4672 char* name = POPpbytex;
4673 pent = PerlSock_getprotobyname(name);
4675 DIE(aTHX_ PL_no_sock_func, "getprotobyname");
4678 else if (which == OP_GPBYNUMBER) {
4679 #ifdef HAS_GETPROTOBYNUMBER
4681 pent = PerlSock_getprotobynumber(number);
4683 DIE(aTHX_ PL_no_sock_func, "getprotobynumber");
4687 #ifdef HAS_GETPROTOENT
4688 pent = PerlSock_getprotoent();
4690 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4694 if (GIMME != G_ARRAY) {
4695 PUSHs(sv = sv_newmortal());
4697 if (which == OP_GPBYNAME)
4698 sv_setiv(sv, (IV)pent->p_proto);
4700 sv_setpv(sv, pent->p_name);
4706 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4707 sv_setpv(sv, pent->p_name);
4708 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4709 for (elem = pent->p_aliases; elem && *elem; elem++) {
4710 sv_catpv(sv, *elem);
4712 sv_catpvn(sv, " ", 1);
4714 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4715 sv_setiv(sv, (IV)pent->p_proto);
4720 DIE(aTHX_ PL_no_sock_func, "getprotoent");
4726 #if defined(HAS_GETSERVBYNAME) || defined(HAS_GETSERVBYPORT) || defined(HAS_GETSERVENT)
4728 I32 which = PL_op->op_type;
4729 register char **elem;
4731 #ifndef HAS_GETSERV_PROTOS /* XXX Do we need individual probes? */
4732 struct servent *getservbyname(Netdb_name_t, Netdb_name_t);
4733 struct servent *getservbyport(int, Netdb_name_t);
4734 struct servent *getservent(void);
4736 struct servent *sent;
4738 if (which == OP_GSBYNAME) {
4739 #ifdef HAS_GETSERVBYNAME
4740 char *proto = POPpbytex;
4741 char *name = POPpbytex;
4743 if (proto && !*proto)
4746 sent = PerlSock_getservbyname(name, proto);
4748 DIE(aTHX_ PL_no_sock_func, "getservbyname");
4751 else if (which == OP_GSBYPORT) {
4752 #ifdef HAS_GETSERVBYPORT
4753 char *proto = POPpbytex;
4754 unsigned short port = (unsigned short)POPu;
4756 if (proto && !*proto)
4760 port = PerlSock_htons(port);
4762 sent = PerlSock_getservbyport(port, proto);
4764 DIE(aTHX_ PL_no_sock_func, "getservbyport");
4768 #ifdef HAS_GETSERVENT
4769 sent = PerlSock_getservent();
4771 DIE(aTHX_ PL_no_sock_func, "getservent");
4775 if (GIMME != G_ARRAY) {
4776 PUSHs(sv = sv_newmortal());
4778 if (which == OP_GSBYNAME) {
4780 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4782 sv_setiv(sv, (IV)(sent->s_port));
4786 sv_setpv(sv, sent->s_name);
4792 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4793 sv_setpv(sv, sent->s_name);
4794 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4795 for (elem = sent->s_aliases; elem && *elem; elem++) {
4796 sv_catpv(sv, *elem);
4798 sv_catpvn(sv, " ", 1);
4800 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4802 sv_setiv(sv, (IV)PerlSock_ntohs(sent->s_port));
4804 sv_setiv(sv, (IV)(sent->s_port));
4806 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
4807 sv_setpv(sv, sent->s_proto);
4812 DIE(aTHX_ PL_no_sock_func, "getservent");
4818 #ifdef HAS_SETHOSTENT
4820 PerlSock_sethostent(TOPi);
4823 DIE(aTHX_ PL_no_sock_func, "sethostent");
4829 #ifdef HAS_SETNETENT
4831 PerlSock_setnetent(TOPi);
4834 DIE(aTHX_ PL_no_sock_func, "setnetent");
4840 #ifdef HAS_SETPROTOENT
4842 PerlSock_setprotoent(TOPi);
4845 DIE(aTHX_ PL_no_sock_func, "setprotoent");
4851 #ifdef HAS_SETSERVENT
4853 PerlSock_setservent(TOPi);
4856 DIE(aTHX_ PL_no_sock_func, "setservent");
4862 #ifdef HAS_ENDHOSTENT
4864 PerlSock_endhostent();
4868 DIE(aTHX_ PL_no_sock_func, "endhostent");
4874 #ifdef HAS_ENDNETENT
4876 PerlSock_endnetent();
4880 DIE(aTHX_ PL_no_sock_func, "endnetent");
4886 #ifdef HAS_ENDPROTOENT
4888 PerlSock_endprotoent();
4892 DIE(aTHX_ PL_no_sock_func, "endprotoent");
4898 #ifdef HAS_ENDSERVENT
4900 PerlSock_endservent();
4904 DIE(aTHX_ PL_no_sock_func, "endservent");
4912 I32 which = PL_op->op_type;
4914 struct passwd *pwent = NULL;
4916 * We currently support only the SysV getsp* shadow password interface.
4917 * The interface is declared in <shadow.h> and often one needs to link
4918 * with -lsecurity or some such.
4919 * This interface is used at least by Solaris, HP-UX, IRIX, and Linux.
4922 * AIX getpwnam() is clever enough to return the encrypted password
4923 * only if the caller (euid?) is root.
4925 * There are at least three other shadow password APIs. Many platforms
4926 * seem to contain more than one interface for accessing the shadow
4927 * password databases, possibly for compatibility reasons.
4928 * The getsp*() is by far he simplest one, the other two interfaces
4929 * are much more complicated, but also very similar to each other.
4934 * struct pr_passwd *getprpw*();
4935 * The password is in
4936 * char getprpw*(...).ufld.fd_encrypt[]
4937 * Mention HAS_GETPRPWNAM here so that Configure probes for it.
4942 * struct es_passwd *getespw*();
4943 * The password is in
4944 * char *(getespw*(...).ufld.fd_encrypt)
4945 * Mention HAS_GETESPWNAM here so that Configure probes for it.
4948 * struct userpw *getuserpw();
4949 * The password is in
4950 * char *(getuserpw(...)).spw_upw_passwd
4951 * (but the de facto standard getpwnam() should work okay)
4953 * Mention I_PROT here so that Configure probes for it.
4955 * In HP-UX for getprpw*() the manual page claims that one should include
4956 * <hpsecurity.h> instead of <sys/security.h>, but that is not needed
4957 * if one includes <shadow.h> as that includes <hpsecurity.h>,
4958 * and pp_sys.c already includes <shadow.h> if there is such.
4960 * Note that <sys/security.h> is already probed for, but currently
4961 * it is only included in special cases.
4963 * In Digital UNIX/Tru64 if using the getespw*() (which seems to be
4964 * be preferred interface, even though also the getprpw*() interface
4965 * is available) one needs to link with -lsecurity -ldb -laud -lm.
4966 * One also needs to call set_auth_parameters() in main() before
4967 * doing anything else, whether one is using getespw*() or getprpw*().
4969 * Note that accessing the shadow databases can be magnitudes
4970 * slower than accessing the standard databases.
4975 # if defined(__CYGWIN__) && defined(USE_REENTRANT_API)
4976 /* Cygwin 1.5.3-1 has buggy getpwnam_r() and getpwuid_r():
4977 * the pw_comment is left uninitialized. */
4978 PL_reentrant_buffer->_pwent_struct.pw_comment = NULL;
4984 char* name = POPpbytex;
4985 pwent = getpwnam(name);
4991 pwent = getpwuid(uid);
4995 # ifdef HAS_GETPWENT
4997 #ifdef POSIX_BC /* In some cases pw_passwd has invalid addresses */
4998 if (pwent) pwent = getpwnam(pwent->pw_name);
5001 DIE(aTHX_ PL_no_func, "getpwent");
5007 if (GIMME != G_ARRAY) {
5008 PUSHs(sv = sv_newmortal());
5010 if (which == OP_GPWNAM)
5011 # if Uid_t_sign <= 0
5012 sv_setiv(sv, (IV)pwent->pw_uid);
5014 sv_setuv(sv, (UV)pwent->pw_uid);
5017 sv_setpv(sv, pwent->pw_name);
5023 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5024 sv_setpv(sv, pwent->pw_name);
5026 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5028 /* If we have getspnam(), we try to dig up the shadow
5029 * password. If we are underprivileged, the shadow
5030 * interface will set the errno to EACCES or similar,
5031 * and return a null pointer. If this happens, we will
5032 * use the dummy password (usually "*" or "x") from the
5033 * standard password database.
5035 * In theory we could skip the shadow call completely
5036 * if euid != 0 but in practice we cannot know which
5037 * security measures are guarding the shadow databases
5038 * on a random platform.
5040 * Resist the urge to use additional shadow interfaces.
5041 * Divert the urge to writing an extension instead.
5044 /* Some AIX setups falsely(?) detect some getspnam(), which
5045 * has a different API than the Solaris/IRIX one. */
5046 # if defined(HAS_GETSPNAM) && !defined(_AIX)
5048 struct spwd *spwent;
5049 int saverrno; /* Save and restore errno so that
5050 * underprivileged attempts seem
5051 * to have never made the unsccessful
5052 * attempt to retrieve the shadow password. */
5055 spwent = getspnam(pwent->pw_name);
5057 if (spwent && spwent->sp_pwdp)
5058 sv_setpv(sv, spwent->sp_pwdp);
5062 if (!SvPOK(sv)) /* Use the standard password, then. */
5063 sv_setpv(sv, pwent->pw_passwd);
5066 # ifndef INCOMPLETE_TAINTS
5067 /* passwd is tainted because user himself can diddle with it.
5068 * admittedly not much and in a very limited way, but nevertheless. */
5072 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5073 # if Uid_t_sign <= 0
5074 sv_setiv(sv, (IV)pwent->pw_uid);
5076 sv_setuv(sv, (UV)pwent->pw_uid);
5079 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5080 # if Uid_t_sign <= 0
5081 sv_setiv(sv, (IV)pwent->pw_gid);
5083 sv_setuv(sv, (UV)pwent->pw_gid);
5085 /* pw_change, pw_quota, and pw_age are mutually exclusive--
5086 * because of the poor interface of the Perl getpw*(),
5087 * not because there's some standard/convention saying so.
5088 * A better interface would have been to return a hash,
5089 * but we are accursed by our history, alas. --jhi. */
5090 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5092 sv_setiv(sv, (IV)pwent->pw_change);
5095 sv_setiv(sv, (IV)pwent->pw_quota);
5098 sv_setpv(sv, pwent->pw_age);
5103 /* pw_class and pw_comment are mutually exclusive--.
5104 * see the above note for pw_change, pw_quota, and pw_age. */
5105 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5107 sv_setpv(sv, pwent->pw_class);
5110 sv_setpv(sv, pwent->pw_comment);
5114 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5116 sv_setpv(sv, pwent->pw_gecos);
5118 # ifndef INCOMPLETE_TAINTS
5119 /* pw_gecos is tainted because user himself can diddle with it. */
5123 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5124 sv_setpv(sv, pwent->pw_dir);
5126 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5127 sv_setpv(sv, pwent->pw_shell);
5128 # ifndef INCOMPLETE_TAINTS
5129 /* pw_shell is tainted because user himself can diddle with it. */
5134 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5135 sv_setiv(sv, (IV)pwent->pw_expire);
5140 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5146 #if defined(HAS_PASSWD) && defined(HAS_SETPWENT)
5151 DIE(aTHX_ PL_no_func, "setpwent");
5157 #if defined(HAS_PASSWD) && defined(HAS_ENDPWENT)
5162 DIE(aTHX_ PL_no_func, "endpwent");
5170 I32 which = PL_op->op_type;
5171 register char **elem;
5173 struct group *grent;
5175 if (which == OP_GGRNAM) {
5176 char* name = POPpbytex;
5177 grent = (struct group *)getgrnam(name);
5179 else if (which == OP_GGRGID) {
5181 grent = (struct group *)getgrgid(gid);
5185 grent = (struct group *)getgrent();
5187 DIE(aTHX_ PL_no_func, "getgrent");
5191 if (GIMME != G_ARRAY) {
5192 PUSHs(sv = sv_newmortal());
5194 if (which == OP_GGRNAM)
5195 sv_setiv(sv, (IV)grent->gr_gid);
5197 sv_setpv(sv, grent->gr_name);
5203 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5204 sv_setpv(sv, grent->gr_name);
5206 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5208 sv_setpv(sv, grent->gr_passwd);
5211 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5212 sv_setiv(sv, (IV)grent->gr_gid);
5214 #if !(defined(_CRAYMPP) && defined(USE_REENTRANT_API))
5215 PUSHs(sv = sv_mortalcopy(&PL_sv_no));
5216 /* In UNICOS/mk (_CRAYMPP) the multithreading
5217 * versions (getgrnam_r, getgrgid_r)
5218 * seem to return an illegal pointer
5219 * as the group members list, gr_mem.
5220 * getgrent() doesn't even have a _r version
5221 * but the gr_mem is poisonous anyway.
5222 * So yes, you cannot get the list of group
5223 * members if building multithreaded in UNICOS/mk. */
5224 for (elem = grent->gr_mem; elem && *elem; elem++) {
5225 sv_catpv(sv, *elem);
5227 sv_catpvn(sv, " ", 1);
5234 DIE(aTHX_ PL_no_func, PL_op_desc[PL_op->op_type]);
5240 #if defined(HAS_GROUP) && defined(HAS_SETGRENT)
5245 DIE(aTHX_ PL_no_func, "setgrent");
5251 #if defined(HAS_GROUP) && defined(HAS_ENDGRENT)
5256 DIE(aTHX_ PL_no_func, "endgrent");
5266 if (!(tmps = PerlProc_getlogin()))
5268 PUSHp(tmps, strlen(tmps));
5271 DIE(aTHX_ PL_no_func, "getlogin");
5275 /* Miscellaneous. */
5280 dSP; dMARK; dORIGMARK; dTARGET;
5281 register I32 items = SP - MARK;
5282 unsigned long a[20];
5287 while (++MARK <= SP) {
5288 if (SvTAINTED(*MARK)) {
5294 TAINT_PROPER("syscall");
5297 /* This probably won't work on machines where sizeof(long) != sizeof(int)
5298 * or where sizeof(long) != sizeof(char*). But such machines will
5299 * not likely have syscall implemented either, so who cares?
5301 while (++MARK <= SP) {
5302 if (SvNIOK(*MARK) || !i)
5303 a[i++] = SvIV(*MARK);
5304 else if (*MARK == &PL_sv_undef)
5307 a[i++] = (unsigned long)SvPV_force_nolen(*MARK);
5313 DIE(aTHX_ "Too many args to syscall");
5315 DIE(aTHX_ "Too few args to syscall");
5317 retval = syscall(a[0]);
5320 retval = syscall(a[0],a[1]);
5323 retval = syscall(a[0],a[1],a[2]);
5326 retval = syscall(a[0],a[1],a[2],a[3]);
5329 retval = syscall(a[0],a[1],a[2],a[3],a[4]);
5332 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5]);
5335 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
5338 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
5342 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
5345 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
5348 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5352 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5356 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5360 retval = syscall(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],
5361 a[10],a[11],a[12],a[13]);
5363 #endif /* atarist */
5369 DIE(aTHX_ PL_no_func, "syscall");
5373 #ifdef FCNTL_EMULATE_FLOCK
5375 /* XXX Emulate flock() with fcntl().
5376 What's really needed is a good file locking module.
5380 fcntl_emulate_flock(int fd, int operation)
5384 switch (operation & ~LOCK_NB) {
5386 flock.l_type = F_RDLCK;
5389 flock.l_type = F_WRLCK;
5392 flock.l_type = F_UNLCK;
5398 flock.l_whence = SEEK_SET;
5399 flock.l_start = flock.l_len = (Off_t)0;
5401 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &flock);
5404 #endif /* FCNTL_EMULATE_FLOCK */
5406 #ifdef LOCKF_EMULATE_FLOCK
5408 /* XXX Emulate flock() with lockf(). This is just to increase
5409 portability of scripts. The calls are not completely
5410 interchangeable. What's really needed is a good file
5414 /* The lockf() constants might have been defined in <unistd.h>.
5415 Unfortunately, <unistd.h> causes troubles on some mixed
5416 (BSD/POSIX) systems, such as SunOS 4.1.3.
5418 Further, the lockf() constants aren't POSIX, so they might not be
5419 visible if we're compiling with _POSIX_SOURCE defined. Thus, we'll
5420 just stick in the SVID values and be done with it. Sigh.
5424 # define F_ULOCK 0 /* Unlock a previously locked region */
5427 # define F_LOCK 1 /* Lock a region for exclusive use */
5430 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
5433 # define F_TEST 3 /* Test a region for other processes locks */
5437 lockf_emulate_flock(int fd, int operation)
5443 /* flock locks entire file so for lockf we need to do the same */
5445 pos = PerlLIO_lseek(fd, (Off_t)0, SEEK_CUR); /* get pos to restore later */
5446 if (pos > 0) /* is seekable and needs to be repositioned */
5447 if (PerlLIO_lseek(fd, (Off_t)0, SEEK_SET) < 0)
5448 pos = -1; /* seek failed, so don't seek back afterwards */
5451 switch (operation) {
5453 /* LOCK_SH - get a shared lock */
5455 /* LOCK_EX - get an exclusive lock */
5457 i = lockf (fd, F_LOCK, 0);
5460 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
5461 case LOCK_SH|LOCK_NB:
5462 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
5463 case LOCK_EX|LOCK_NB:
5464 i = lockf (fd, F_TLOCK, 0);
5466 if ((errno == EAGAIN) || (errno == EACCES))
5467 errno = EWOULDBLOCK;
5470 /* LOCK_UN - unlock (non-blocking is a no-op) */
5472 case LOCK_UN|LOCK_NB:
5473 i = lockf (fd, F_ULOCK, 0);
5476 /* Default - can't decipher operation */
5483 if (pos > 0) /* need to restore position of the handle */
5484 PerlLIO_lseek(fd, pos, SEEK_SET); /* ignore error here */
5489 #endif /* LOCKF_EMULATE_FLOCK */
5493 * c-indentation-style: bsd
5495 * indent-tabs-mode: t
5498 * ex: set ts=8 sts=4 sw=4 noet: